minic 动作句型处理

   1 #include "lex_define.h"
   2 enum keywords_type//代表一些关键字
   3 {
   4     loop_for=3,//代表for关键字
   5     loop_while,//代表while关键字
   6     branch_if,//代表if关键字
   7     branch_else,//代表else关键字
   8     break,
   9 };
  10 //这里sizeof被分到运算符里了,而main被分到函数名里面去了,基本数据类型和符号前缀被分到数据类型名之中了
  11 enum delimit_type
  12 {    
  13     open_brace,//开大括号
  14     close_brace,//闭大括号
  15     semicolon//分号
  16 };
  17 //现在开始描述运算符的优先级
  18 //0级: [ ] ( ) -> . 结合性 从左到右
  19 //1级:! ~  $ @ - (cast)  sizeof 这些都是单目运算符,注意cast的意思是强制类型转换
  20 //2级: * / % 这些运算符都是从左到右 
  21 //3级: + - 这些也是从左到右 
  22 //4级: >> << 按道理木有结合性
  23 //5级:> < >= <= 从左到右
  24 //6级: == != 从左到右
  25 //7级: & 从左到右
  26 //8级:^ 从左到右
  27 //9级:| 从左到右
  28 //10级: && 从左到右
  29 //11级: ||从左到右
  30 
  31 struct operator_token//这个是操作符栈中的token,注意我们在处理数组运算符的时候采取了特殊的方法
  32 {
  33     enum basic_operator_type current_op_type;//这个域可以提供所有的信息
  34     int priority;//运算符优先级
  35     int sub_number;//代表有几个操作数
  36     //我们可以根据前面的那个枚举类型得到所有的信息,但是我们另外提出这个优先级域,是为了简化判断的操作
  37 }
  38 //现在我们来定义一个抽象语法树的结构
  39 //首先考虑的是语法树节点的种类
  40 //这个域比较复杂,主要是因为各种不同的语法节点有大小不一的子节点个数
  41 //首先对于没有子节点的情况,这里代表的是那些关键字,因为关键字不需要产生式
  42 //然后还有所有的名字和常量,
  43 //还有单字符和字符串,
  44 //然后讨论的是只有一个子节点的情况,这里对应的是单目运算符
  45 //然后讨论的是两个子节点的情况,这里代表了所有的运算符和赋值符和函数调用节点
  46 //这里的函数调用节点包括两个子节点,一个是函数名,另外一个是参数列表
  47 //而参数列表也是拥有两个域的节点,这两个节点中最多有一个是参数列表属性,其他的都是名字
  48 //对于单语句块节点,这个也是拥有两个子节点的节点,构造方式与参数列表节点一样
  49 //有三个子节点的是while语句,分别是while节点,判断节点,和代码块节点
  50 //有五个子节点的是for语句,分别是for节点,初始化节点,判断节点,修改节点和代码块节点
  51 //这里还有if else节点也是五个节点 if节点 判断节点 代码块节点,else节点,代码块节点
  52 //但是我们采取的是双栈,所以这些节点会被分成两种。
  53 //一种是单语句,即全部都是算术操作的那些节点,即expression
  54 //另外一种是多语句,即所有的单语句即控制结构
  55 //注意我们这里在调用函数的时候,参数里面不允许有变量值的修改操作,因此只能是变量与数组运算,结构运算
  56 //指针运算这三种的结合,这样处理函数的时候就简单多了
  57 //因此,我们这里有两棵语法分析树,第一个是句型树,第二个是表达式树,函数调用放在表达式树之中
  58 //对于表达式树,我们只需要考虑操作符的种类,这里我们把赋值符放在操作符里面去考虑,这样
  59 //还要提到一点就是句型树中的判断节点属于表达式树,break语句也属于表达式树
  60 
  61 //这里我们来处理类型声明
  62 typedef struct _phrase_tree_node//表达式语法树的节点
  63 {
  64     union
  65     {
  66         int phrase_type;
  67     //0代表break,1代表常数,2代表名字,3代表函数调用,4代表单个参数,5代表参数列表
  68     //6代表赋值运算,7代表单字符,8代表字符串,9及以后的代表操作符
  69         enum basic_operator_type current_op_type;//因为我们定义操作符编号是从5开始的,所以不会与前面的那个重合
  70     };
  71     union
  72     {
  73         char* constant_str;//代表常量
  74         char* var_name;//代表名字
  75         struct
  76         {
  77             struct _phrase_tree_node* left_son;
  78             struct _phrase_tree_node* right_son;
  79         };//代表有两个分量的类型,包括函数调用,函数参数,双目运算符,赋值操作
  80         struct _phrase_tree_node* original_node;//代表除了强制类型转换之外的单目运算符
  81         struct
  82         {
  83             struct _phrase_tree_node* token_for_cast;//代表单目运算符
  84             struct _type_description* cast_type_description;//这个是为了强制类型转换用的....不爽
  85             int pointer_layer;//代表指针的层数
  86         };
  87     };
  88 }phrase_tree_node;
  89 //这里对于操作数栈,我们不需要再去定义其他的数据类型,可以直接在栈中使用上面定义的表达式语法树节点
  90 //我们还需要去管理一个赋值id栈和id操作符栈,这样来处理是因为我们把赋值符从操作符中独立出来了,
  91 //所以不得不这样做,规范化的代价啊。。。
  92 //在处理赋值语句的时候id栈中存放的是id,在处理完expression后,最后处理赋值语句,生成赋值语法节点
  93 //而对于函数调用,也是需要处理id的,这个时候也需要使用id栈。为了弄清楚我们使用的是赋值id还是参数id
  94 //我们利用另外的一个指针来处理赋值id,因为赋值id之只有一个,而参数id可以有很多个
  95 //我们在遇到=号的时候,把id栈中的语法节点的指针取出放在copytopointer这个指针中,然后清空id栈。
  96 //所以在expression表达式处理完的时候,检查这个指针是不是空,如果不是,则需要生成赋值节点
  97 //对于函数参数的处理,每次遇到函数名,把函数名压入操作数栈,把之后的括号压入操作符栈,
  98 //然后对于之后的处理,都在id栈与id操作符栈中进行,
  99 //对于遇到分号的时候,如果id栈不为空,则把id栈中所有的id节点合并成一个参数列表节点,当然当前只有一个就
 100 //算了,
 101 //因此我们需要四个栈,还有为了支持强制类型转化操作和sizeof操作,我们还需要一个类型参数变量来供使用
 102 //为了支持赋值,我们还需要赋值的语法树节点,为了支持函数参数我们需要一个参数语法树节点
 103 struct operator_token* phrase_operator_stack[40];//优先级也就只有12个,40个足够了
 104 
 105 struct _phrase_tree_node* phrase_token_stack[40];//给40个是看他面子了
 106 
 107 //id_0: name |(id)| id_0[constant] | id_0.name | id_0->name |id_0[name]
 108 //id_1: id_0 | @id_0 
 109 //id : id_1
 110 struct _phrase_tree_node* id_token_stack[10];//打发叫花子
 111 enum basic_operator_type id_operator_stack[10];//id里面的运算符就更少了,因此10个也是足够了
 112 int id_token_stack_pointer;
 113 int id_operator_stack_pointer;
 114 int phrase_token_stack_pointer;
 115 int phrase_operator_stack_pointer;
 116 struct _type_description* temp_cast_one;//这个是为强制类型转换而使用的
 117 int cast_pointer_layer;//代表强制类型转换的指针层数
 118 int predecent;
 119 //这个是用来说明前面遇到的是操作符还是操作数,这个变量主要是为了处理负号和指针和取地址
 120 void tackle_id_op(char* input_op)//这个函数是用来处理id的操作符栈
 121 {
 122     struct _phrase_tree_node* temp_node_one;
 123     enum basic_operator_type current_top_op;
 124     switch(*input_op)
 125     {
 126     case '(':
 127         id_operator_stack_pointer++;
 128         id_operator_stack[id_operator_stack_pointer]=parenthesis;
 129         break;
 130     case '[':
 131         id_operator_stack_pointer++;
 132         id_operator_stack[id_operator_stack_pointer]=array_op;
 133         break;
 134     case '.':
 135         if(id_operator_stack_pointer==0)//如果目前为空栈,则入栈
 136         {
 137             id_operator_stack_pointer++;
 138             id_operator_stack[id_operator_stack_pointer]=str_sub;
 139         }
 140         else
 141         {
 142             current_top_op=id_operator_stack[id_operator_stack_pointer];
 143             switch(current_top_op)
 144             {
 145             case parenthesis:
 146             case array_op:
 147             case get_mem:
 148                 id_operator_stack_pointer++;
 149                 id_operator_stack[id_operator_stack_pointer]=str_sub;
 150                 break;
 151             case str_sub:
 152             case p_str_sub:
 153                 temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 154                 temp_node_one->current_op_type=current_top_op;
 155                 if(id_token_stack_pointer>=2)
 156                 {
 157                     temp_node_one->left_son=id_token_stack[id_token_stack_pointer-1];
 158                     temp_node_one->right=id_token_stack[id_token_stack_pointer];
 159                     id_token_stack_pointer--;
 160                     id_token_stack[id_token_stack_pointer]=temp_node_one;
 161                     id_operator_stack_pointer++;
 162                     id_operator_stack[id_operator_stack_pointer]=str_sub;
 163                 }
 164                 else
 165                 {
 166                     printf("insufficient id token during pop operator
");
 167                     exit();
 168                 }
 169                 break;
 170             default:
 171                 printf("unknown op in id_op_stack
");
 172                 exit();
 173                 break;
 174             }
 175         }
 176         break;
 177     case '>':
 178         if(id_operator_stack_pointer==0)//如果目前为空栈,则入栈
 179         {
 180             id_operator_stack_pointer++;
 181             id_operator_stack[id_operator_stack_pointer]=p_str_sub;
 182         }
 183         else
 184         {
 185             current_top_op=id_operator_stack[id_operator_stack_pointer];
 186             switch(current_top_op)
 187             {
 188             case parenthesis:
 189             case array_op:
 190             case get_mem:
 191                 id_operator_stack_pointer++;
 192                 id_operator_stack[id_operator_stack_pointer]=p_str_sub;
 193                 break;
 194             case str_sub:
 195             case p_str_sub:
 196                 temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 197                 temp_node_one->current_op_type=current_top_op;
 198                 if(id_token_stack_pointer>=2)
 199                 {
 200                     temp_node_one->left_son=id_token_stack[id_token_stack_pointer-1];
 201                     temp_node_one->right=id_token_stack[id_token_stack_pointer];
 202                     id_token_stack_pointer--;
 203                     id_token_stack[id_token_stack_pointer]=temp_node_one;
 204                     id_operator_stack[id_operator_stack_pointer]=p_str_sub;
 205                 }
 206                 else
 207                 {
 208                     printf("insufficient id token during pop operator
");
 209                     exit();
 210                 }
 211                 break;
 212             default:
 213                 printf("unknown op in id_op_stack
");
 214                 exit();
 215                 break;
 216             }
 217         }
 218         break;
 219     case '@':
 220         id_operator_stack_pointer++;
 221         id_operator_stack[id_operator_stack_pointer]=get_mem;
 222         break;
 223     case ')':
 224         while(id_operator_stack[id_operator_stack_pointer]!=parenthesis)
 225             //这里我们之所以加上这个大于0,是因为在函数的参数列表的形式下,会有一个多余的闭括号
 226         {
 227             current_top_op=id_operator_stack[id_operator_stack_pointer];
 228             switch(current_top_op)
 229             {
 230             case get_mem:
 231                 temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 232                 temp_node_one->current_op_type=get_mem;
 233                 if(id_token_stack_pointer>=1)
 234                 {
 235                     temp_node_one->original_node=id_token_stack[id_token_stack_pointer];
 236                     id_token_stack[id_token_stack_pointer]=temp_node_one;
 237                 }
 238                 else
 239                 {
 240                     printf("null token stack while pop operator
");
 241                     exit();
 242                 }
 243                 break;
 244             case p_str_sub:
 245             case str_sub:
 246                 temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 247                 temp_node_one->current_op_type=current_top_op;
 248                 if(id_token_stack_pointer>=2)
 249                 {
 250                     temp_node_one->left_son=id_token_stack[id_token_stack_pointer-1];
 251                     temp_node_one->right=id_token_stack[id_token_stack_pointer];
 252                     id_token_stack_pointer--;
 253                     id_token_stack[id_token_stack_pointer]=temp_node_one;
 254                 }
 255                 else
 256                 {
 257                     printf("insufficient id token during pop operator
");
 258                     exit();
 259                 }
 260                 break;
 261             case array_op:
 262                 printf("unmatched array_op during parenthesis
");
 263                 exit();
 264                 break;
 265             default:
 266                 printf("unknowd op in id_op_stack
");
 267                 break;
 268             }
 269             id_operator_stack_pointer--;
 270         }
 271         if(id_token_stack_pointer<1)
 272         {
 273             printf("null id token in stack during pop
");
 274             exit();
 275         }
 276         else
 277         {
 278             id_operator_stack_pointer--;
 279         }
 280         break;
 281     case ']':
 282         while(id_operator_stack[id_operator_stack_pointer]!=array_op)
 283         {
 284             current_top_op=id_operator_stack[id_operator_stack_pointer];
 285             switch(current_top_op)
 286             {
 287             case get_mem:
 288                 temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 289                 temp_node_one->current_op_type=get_mem;
 290                 if(id_token_stack_pointer>=1)
 291                 {
 292                     temp_node_one->original_node=id_token_stack[id_token_stack_pointer];
 293                     id_token_stack[id_token_stack_pointer]=temp_node_one;
 294                 }
 295                 else
 296                 {
 297                     printf("null token stack while pop operator
");
 298                     exit();
 299                 }
 300                 break;
 301             case p_str_sub:
 302             case str_sub:
 303                 temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 304                 temp_node_one->current_op_type=current_top_op;
 305                 if(id_token_stack_pointer>=2)
 306                 {
 307                     temp_node_one->left_son=id_token_stack[id_token_stack_pointer-1];
 308                     temp_node_one->right=id_token_stack[id_token_stack_pointer];
 309                     id_token_stack_pointer--;
 310                     id_token_stack[id_token_stack_pointer]=temp_node_one;
 311                 }
 312                 else
 313                 {
 314                     printf("insufficient id token during pop operator
");
 315                     exit();
 316                 }
 317                 break;
 318             case parenthesis:
 319                 printf("unmatched parenthesis during array_op
");
 320                 exit();
 321                 break;
 322             default:
 323                 printf("unknowd op in id_op_stack
");
 324                 exit();
 325                 break;
 326             }
 327             id_operator_stack_pointer--;
 328         }
 329         if(id_token_stack_pointer<2)
 330         {
 331             printf("insufficient id token in stack during pop array_op
");
 332             exit();
 333         }
 334         else
 335         {
 336             id_operator_stack_pointer--;
 337             temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 338             temp_node_one->current_op_type=array_op;
 339             temp_node_one->left_son=id_token_stack[id_token_stack_pointer-1];
 340             temp_node_one->right=id_token_stack[id_token_stack_pointer];
 341             id_token_stack_pointer--;
 342             id_token_stack[id_token_stack_pointer]=temp_node_one;
 343         }
 344         break;
 345     default:
 346         printf("unknown op_type when tackcle_id_op
");
 347         break;
 348     }
 349 }
 350 phrase_tree_node* get_id(void)//这个函数是用来得到id的
 351 {
 352 
 353     struct _phrase_tree_node* temp_node_one;
 354     struct first_token_chain* temp_first_one;
 355     struct first_token_chain* temp_first_two;
 356     struct first_lex_token* current_lex_token;
 357     struct first_lex_token* temp_lex_token;
 358     struct operator_token* temp_op;
 359     id_operator_stack_pointer=id_token_stack_pointer=0;
 360     temp_first_one=first_chain_head;
 361     current_lex_token=temp_first_one->current_first_token;
 362     while(1)
 363     {
 364         if(current_lex_token->current_lex_type==an_operator)//对于是操作符的时候
 365         {
 366             if(*(current_lex_token->token_name)!='=')//如果不是等于号
 367             {
 368                 switch(*(current_lex_token->token_name))
 369                 {
 370                 case '-'://如果第一个字符是-.这个情况我们需要特殊考虑
 371                     temp_first_two=temp_first_one->next;
 372                     temp_lex_token=temp_first_two->current_first_token;
 373                     if(temp_lex_token->current_lex_type!=an_operator)//如果后面搭配的不是操作符,报错
 374                     {
 375                         printf("invalid - during id recognise
");
 376                         exit();
 377                     }
 378                     else
 379                     {
 380                         if(*(temp_lex_token->token_name)!='>')//如果后面搭配的不是>,则报错
 381                         {
 382                             printf("invalid match operator %s after -
",temp_lex_token->token_name);
 383                             exit();
 384                         }
 385                         else//如果刚好组成了搭配
 386                         {
 387                             free(current_lex_token->token_name);
 388                             free(current_lex_token);
 389                             free(temp_first_one);
 390                             first_chain_head=temp_first_two->next;
 391                             tackle_id_op(temp_lex_token->token_name);//这个是处理id操作符栈的主体函数
 392                             free(temp_lex_token->token_name);
 393                             free(temp_lex_token);
 394                             free(temp_first_two);
 395                             temp_first_one=first_chain_head;
 396                             current_lex_token=temp_first_one->current_first_token;
 397                         }
 398                     }
 399                     break;
 400                 case '(':
 401                 case ')':
 402                 case '[':
 403                 case ']':
 404                 case '@':
 405                 case '.':
 406                     tackle_id_op(*(current_lex_token->token_name));
 407                     first_chain_head=temp_first_one->next;
 408                     free(current_lex_token->token_name);
 409                     free(current_lex_token);
 410                     free(temp_first_one);
 411                     temp_first_one=first_chain_head;
 412                     current_lex_token=temp_first_one->current_first_token;
 413                     break;
 414                 default:
 415                     printf("invalid operator %s during id recognisation
",current_lex_token->token_name);
 416                     break;
 417                 }
 418             }    
 419             else
 420                 //如果是等于号
 421                 //则我们需要将操作符栈里面的东西全都弹出
 422             {
 423                 while(id_operator_stack_pointer>0)
 424                 {
 425                     switch(id_operator_stack[id_operator_stack_pointer])
 426                     {
 427                     case p_str_sub:
 428                     case str_sub:
 429                         temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 430                         temp_node_one->current_op_type=id_operator_stack[id_operator_stack_pointer];
 431                         if(id_token_stack_pointer>=2)
 432                         {
 433                             temp_node_one->left_son=id_token_stack[id_token_stack_pointer-1];
 434                             temp_node_one->right=id_token_stack[id_token_stack_pointer];
 435                             id_token_stack_pointer--;
 436                             id_token_stack[id_token_stack_pointer]=temp_node_one;
 437                             id_operator_stack_pointer--;
 438                         }
 439                         else
 440                         {
 441                             printf("insufficient id token during pop operator
");
 442                             exit();
 443                         }
 444                         break;
 445                     case get_mem:
 446                         temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 447                         temp_node_one->current_op_type=id_operator_stack[id_operator_stack_pointer];
 448                         temp_node_one->original_node=id_token_stack[id_token_stack_pointer];
 449                         id_token_stack[id_token_stack_pointer]=temp_node_one;
 450                         id_operator_stack_pointer--;
 451                         break;
 452                     case array_op:
 453                         printf("unmatched array_op in id_token
");
 454                         exit();
 455                         break;
 456                     case parenthesis:
 457                         printf("unmatched parenthesis in id_token
");
 458                         exit();
 459                         break;
 460                     default:
 461                         printf("unexpected op in id token
");
 462                         exit();
 463                         break;
 464                     }
 465 
 466 
 467                 }//至此,操作符栈处理完毕
 468                 //然后修正好词法链的头节点,使得这个=号被抛弃
 469                 first_chain_head=temp_first_one->next;
 470                 free(current_lex_token->token_name);
 471                 free(current_lex_token);
 472                 free(temp_first_one);
 473                 id_token_stack_pointer--;
 474                 return id_token_stack[1];
 475             }
 476         }
 477         else
 478             //对于是常量或者名字的情况,我们直接入栈,
 479             //对于是分号的情况,我们要采取与等号相同的操作
 480         {
 481             if(current_lex_token->current_lex_type!=delimit)
 482             {
 483                 switch(current_lex_token->current_lex_type)
 484             {
 485             case constant:
 486                 temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 487                 temp_node_one->constant_str=current_lex_token->token_name;
 488                 temp_node_one->phrase_type=1;
 489                 id_token_stack_pointer++;
 490                 id_token_stack[id_token_stack_pointer]=temp_node_one;
 491                 first_chain_head=fisrt_chain_head->next;
 492                 free(current_lex_token);
 493                 free(temp_first_one);
 494                 temp_first_one=first_chain_head;
 495                 current_lex_token=temp_first_one->current_first_token;
 496                 break;
 497             case name:
 498                 temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 499                 temp_node_one->var_name=current_lex_token->token_name;
 500                 temp_node_one->phrase_type=2;
 501                 id_token_stack_pointer++;
 502                 id_token_stack[id_token_stack_pointer]=temp_node_one;
 503                 first_chain_head=fisrt_chain_head->next;
 504                 free(current_lex_token);
 505                 free(temp_first_one);
 506                 temp_first_one=first_chain_head;
 507                 current_lex_token=temp_first_one->current_first_token;
 508                 break;
 509             default:
 510                 printf("invalid token %s in id recognization
",current_lex_token->token_name);
 511                 exit();
 512                 break;
 513             }
 514             }
 515             else//如果是分号
 516             {
 517                 while(id_operator_stack_pointer>0)//清空操作符栈
 518                 {
 519                     switch(id_operator_stack[id_operator_stack_pointer])
 520                     {
 521                     case p_str_sub:
 522                     case str_sub:
 523                         temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 524                         temp_node_one->current_op_type=id_operator_stack[id_operator_stack_pointer];
 525                         if(id_token_stack_pointer>=2)
 526                         {
 527                             temp_node_one->left_son=id_token_stack[id_token_stack_pointer-1];
 528                             temp_node_one->right=id_token_stack[id_token_stack_pointer];
 529                             id_token_stack_pointer--;
 530                             id_token_stack[id_token_stack_pointer]=temp_node_one;
 531                             id_operator_stack_pointer--;
 532                         }
 533                         else
 534                         {
 535                             printf("insufficient id token during pop operator
");
 536                             exit();
 537                         }
 538                         break;
 539                     case get_mem:
 540                         temp_node_one=malloc(sizeof(struct _phrase_tree_node));
 541                         temp_node_one->current_op_type=id_operator_stack[id_operator_stack_pointer];
 542                         temp_node_one->original_node=id_token_stack[id_token_stack_pointer];
 543                         id_token_stack[id_token_stack_pointer]=temp_node_one;
 544                         id_operator_stack_pointer--;
 545                         break;
 546                     case array_op:
 547                         printf("unmatched array_op in id_token
");
 548                         exit();
 549                         break;
 550                     case parenthesis:
 551                         printf("unmatched parenthesis in id_token
");
 552                         exit();
 553                         break;
 554                     default:
 555                         printf("unexpected op in id token
");
 556                         exit();
 557                         break;
 558                     }
 559 
 560 
 561                 }//至此,操作符栈处理完毕
 562                 //然后修正好词法链的头节点,使得这个=号被抛弃
 563                 first_chain_head=temp_first_one->next;
 564                 free(current_lex_token->token_name);
 565                 free(current_lex_token);
 566                 free(temp_first_one);
 567                 id_token_stack_pointer--;
 568                 return id_token_stack[1];
 569             }
 570         }
 571     }
 572 }
 573 phrase_tree_node* get_func(void)
 574 {
 575     phrase_tree_node* tree_node_one;
 576     phrase_tree_node* tree_node_two;
 577     phrase_tree_node* tree_node_three;
 578     phrase_tree_node* tree_node_four;
 579     phrase_tree_node* tree_node_five;
 580     first_token_chain* chain_node_one;
 581     first_token_chain* chain_node_two;
 582     first_lex_token* current_token;
 583     tree_node_three=malloc(sizeof(struct _phrase_tree_node));//这个是函数名称节点
 584     tree_node_three->phrase_type=2;
 585     tree_node_three->var_name=first_chain_head->current_first_token->token_name;
 586     chain_node_one=first_chain_head;
 587     first_chain_head=chain_node_one->next;
 588     free(chain_node_one->current_first_token);
 589     free(chain_node_one);
 590     chain_node_one=first_chain_head;
 591     first_chain_head=chain_node_one->next;
 592     free(chain_node_one->current_first_token->token_name);
 593     free(chain_node_one->current_first_token);
 594     free(chain_node_one);
 595     tree_node_two=get_id();//获得第一个参数,
 596     tree_node_five=malloc(sizeof(struct _phrase_tree_node));
 597     tree_node_five->phrase_type=4;
 598     tree_node_five->original_node=tree_node_two;
 599     tree_node_two=tree_node_five;
 600     //由于不带参数的函数在调用的时候一定会有一个void参数,所以这个一定会成功
 601     while(*(first_chain_head->current_first_token->token_name)!=')')//这里闭括号预示着参数列表的结尾
 602     {
 603         tree_node_four=get_id();
 604         tree_node_five=malloc(sizeof(struct _phrase_tree_node));
 605         tree_node_five->phrase_type=4;
 606         tree_node_five->original_node=tree_node_four;
 607         tree_node_four=malloc(sizeof(struct _phrase_tree_node));
 608         tree_node_four->phrase_type=5;
 609         tree_node_four->left_son=tree_node_two;
 610         tree_node_four->right_son=tree_node_five;
 611         tree_node_two=tree_node_four;
 612     }
 613 
 614     chain_node_one=first_chain_head;
 615     first_chain_head=chain_node_one->next;
 616     free(chain_node_one->current_first_token->token_name);
 617     free(chain_node_one->current_first_token);
 618     free(chain_node_one);
 619     //这里把输入指针下移一个
 620     tree_node_one=malloc(sizeof(struct _phrase_tree_node));
 621     tree_node_one->phrase_type=3;
 622     tree_node_one->left_son=tree_node_three;
 623     tree_node_one->right_son=tree_node_two;
 624     return tree_node_one;
 625 }
 626 
 627 void phrase_operator_pop(int current_priority)//这个函数是为了从操作符栈中弹出一个操作符,对于是开括号的时候什么也不做
 628 {
 629     phrase_tree_node* tree_node_one;
 630     int number_of_arg;
 631     while(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=current_priority)
 632     {
 633         number_of_arg=phrase_operator_stack[phrase_operator_stack_pointer]->sub_number;
 634         tree_node_one=malloc(sizeof(struct _phrase_tree_node));
 635         tree_node_one->current_op_type=phrase_operator_stack[phrase_operator_stack_pointer]->current_op_type;
 636         if(number_of_arg==2)
 637         {
 638             if(tree_node_one->current_op_type!=array_op)//如果是数组运算符就什么都不干
 639             {
 640                 if(phrase_token_stack_pointer<2)
 641                 {
 642                     printf("need more token in phrase token stack
");
 643                     free(tree_node_one);
 644                     exit();
 645                 }
 646                 else
 647                 {
 648                     tree_node_one->left_son=phrase_token_stack[phrase_token_stack_pointer-1];
 649                     tree_node_one->right_son=phrase_token_stack[phrase_token_stack_pointer];
 650                     phrase_token_stack_pointer--;
 651                     phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
 652                     phrase_operator_stack_pointer--;
 653                 }
 654             }
 655             else
 656             {
 657                 //do nothing
 658             }
 659         }
 660         else//这里有三种种特殊情况,一个是括号,一个就是强制类型转换,还有一个是sizeof
 661         {
 662             switch(tree_node_one->current_op_type)
 663             {
 664             case parenthesis:
 665                 //do nothing 因为是开括号
 666                 break;
 667             case type_cast:
 668                 tree_node_one->token_for_cast=phrase_token_stack[phrase_token_stack_pointer];
 669                 tree_node_one->cast_type_description=temp_cast_one;
 670                 temp_cast_one=NULL;
 671                 cast_pointer_layer=0;
 672                 tree_node_one->pointer_layer=cast_pointer_layer;
 673                 phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
 674                 phrase_operator_stack_pointer--;
 675                 break;
 676             case get_size://对于sizeof有两种形式
 677                 if(temp_cast_one!=NULL)//对应的是声明头形式
 678                 {
 679                     tree_node_one->token_for_cast=phrase_token_stack[phrase_token_stack_pointer];
 680                     tree_node_one->cast_type_description=temp_cast_one;
 681                     tree_node_one->pointer_layer=cast_pointer_layer;
 682                     temp_cast_one=NULL;
 683                     cast_pointer_layer=0;
 684                     phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
 685                     phrase_operator_stack_pointer--;
 686                 }
 687                 else//对应的是变量形式
 688                 {
 689                     tree_node_one->original_node=phrase_token_stack[phrase_token_stack_pointer];
 690                     phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
 691                     phrase_operator_stack_pointer--;
 692                 }
 693                 break;
 694             default:
 695                 if(phrase_token_stack_pointer==0)
 696                 {
 697                     printf("need token in phrase token stack
");
 698                     free(tree_node_one);
 699                     exit();
 700                 }
 701                 else
 702                 {
 703                     tree_node_one->original_node=phrase_token_stack[phrase_token_stack_pointer];
 704                     phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
 705                     phrase_operator_stack_pointer--;
 706                 }
 707                 break;
 708             }
 709         }
 710     }
 711 }
 712 
 713 
 714 void tackle_phrase_op(char* in_op)
 715 {
 716     struct first_lex_token* current_lex_token;
 717     struct first_lex_token* next_lex_token;
 718     struct _phrase_tree_node* temp_tree_node;
 719     struct first_token_chain* current_chain_node;
 720     struct first_token_chain* next_chain_node;
 721     struct operator_token* current_operator_token;
 722     switch(*in_op)
 723     {
 724     case '(':
 725         //这里就有点复杂了因为要处理强制类型转换这个蛋疼的东西
 726         current_chain_node=first_chain_head;
 727         next_chain_node=current_chain_node->next;
 728         next_lex_token=next_chain_node->current_first_token;
 729         if(next_lex_token->current_lex_type==name)//准备强制类型转换
 730         {
 731             temp_cast_one=search_avl_tree(next_lex_token->token_name);
 732             if(temp_cast_one->type_type==4)//这里就意味着强制类型转换
 733             {
 734                 first_chain_head=next_chain_node->next;
 735                 free(current_chain_node->current_first_chain->token_name);
 736                 free(current_chain_node->current_first_chain);
 737                 free(current_chain_node);
 738                 free(next_lex_token->token_name);
 739                 free(next_lex_token);
 740                 free(next_chain_node);
 741                 if(first_chain_head->current_first_chain->current_lex_type==name)//这个对应的是符合类型的转换
 742                 {
 743                     current_chain_node=first_chain_head;
 744                     current_lex_token=current_chain_node->current_first_token;
 745                     temp_cast_one=search_avl_tree(current_lex_token->token_name);//找出这个名字的类型
 746                     first_chain_head=first_chain_head->next;
 747                     free(current_lex_token->token_name);
 748                     free(current_lex_token);
 749                     free(current_chain_node);
 750                 }
 751                 cast_pointer_layer=0;
 752                 while(*(first_chain_head->current_first_token->token_name)!=')')//吃掉所有的指针符号
 753                 {
 754                     current_lex_token=first_chain_head->current_first_token;
 755                     if(*(current_lex_token->token_name)!='*')
 756                     {
 757                         printf("encount an unexpected token %s during cast_recognise
",current_lex_token->token_name);
 758                         exit();
 759                     }
 760                     else
 761                     {
 762                         cast_pointer_layer=cast_pointer_layer<<1 + 1;
 763                         current_chain_node=first_chain_head;
 764                         first_chain_head=first_chain_head->next;
 765                         free(current_lex_token->token_name);
 766                         free(current_lex_token);
 767                         free(current_chain_node);
 768                         //这里我们就先不纠结指针层数的限制了
 769                     }
 770                 }
 771                 //现在遇到的是括号

 772                 //直接把这个括号丢弃,然后生成一个强制类型转换操作符
 773                 current_chain_node=first_chain_head;
 774                 first_chain_head=first_chain_head->next;
 775                 free(current_lex_token->token_name);
 776                 free(current_lex_token);
 777                 free(current_chain_node);
 778                 current_operator_token=malloc(sizeof(struct operator_token));
 779                 current_operator_token->priority=2;
 780                 current_operator_token->sub_number=1;
 781                 current_operator_token->current_op_type=type_cast;
 782                 phrase_operator_pop(2);
 783                 phrase_operator_stack_pointer++;
 784                 phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 785                 predecent=1;
 786             }
 787             else//遇到没有被识别的名称类型
 788             {
 789                 printf("unexpected type %s during cast_recognisation
",next_lex_token->token_name);
 790                 exit();
 791             }
 792         }
 793         else
 794             //对应的是普通的括号
 795         {
 796             current_operator_token=malloc(sizeof(struct operator_token));
 797             current_operator_token->priority=1;
 798             current_operator_token->sub_number=1;
 799             current_operator_token->current_op_type=parenthesis;
 800             //注意一旦碰到开括号,前面的优先级自动停止,因此不会碰到操作符出栈的情况
 801             //所以这里不需要考虑优先级了
 802             current_chain_node=first_chain_head;
 803             first_chain_head=first_chain_head->next;
 804             current_lex_token=current_chain_node->current_first_token;
 805             free(current_lex_token->token_name);
 806             free(current_lex_token);
 807             free(current_chain_node);
 808             phrase_operator_stack_pointer++;
 809             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 810             predecent=1;
 811         }
 812         //这里两种情况都被处理完毕了,累
 813         break;
 814     case '[':
 815         current_operator_token=malloc(sizeof(struct operator_token));
 816         current_operator_token->priority=1;
 817         current_operator_token->sub_number=2;
 818         current_operator_token->current_op_type=array_op;
 819         //这里数组运算符与括号不一样,数组运算符是一个运算符和界定符,而括号只是一个界定符。。。
 820         phrase_operator_pop(1);
 821         current_chain_node=first_chain_head;
 822         first_chain_head=first_chain_head->next;
 823         current_lex_token=current_chain_node->current_first_token;
 824         free(current_lex_token->token_name);
 825         free(current_lex_token);
 826         free(current_chain_node);
 827         phrase_operator_stack_pointer++;
 828         phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 829         predecent=1;
 830         break;
 831     case '.':
 832         current_operator_token=malloc(sizeof(struct operator_token));
 833         current_operator_token->priority=1;
 834         current_operator_token->sub_number=2;
 835         current_operator_token->current_op_type=str_sub;
 836         phrase_operator_pop(1);
 837         current_chain_node=first_chain_head;
 838         first_chain_head=first_chain_head->next;
 839         current_lex_token=current_chain_node->current_first_token;
 840         free(current_lex_token->token_name);
 841         free(current_lex_token);
 842         free(current_chain_node);
 843         phrase_operator_stack_pointer++;
 844         phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 845         predecent=1;
 846         break;
 847     case '-':
 848         if(predecent==0)//代表前面是操作数的情况
 849         {
 850             next_chain_node=current_chain_node->next;
 851             next_lex_token=next_chain_node->current_first_token;
 852             if(next_lex_token->current_lex_type==an_operator)//如果后面跟的也是操作符
 853             {
 854                 if((*(next_lex_token->token_name))=='>')//对应的是p_str_sub
 855                 {
 856                     current_operator_token=malloc(sizeof(struct operator_token));
 857                     current_operator_token->priority=1;
 858                     current_operator_token->sub_number=2;
 859                     current_operator_token->current_op_type=p_str_sub;
 860                     phrase_operator_pop(1);
 861                     phrase_operator_stack_pointer++;
 862                     phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 863                     predecent=1;
 864                     first_chain_head=next_chain_node->next;
 865                     free(current_lex_node->token_name);
 866                     free(current_lex_node);
 867                     free(current_chain_node);
 868                     free(next_lex_node->token_name);
 869                     free(next_lex_node);
 870                     free(next_chain_node);
 871                     current_chain_node=first_chain_head;
 872                     current_lex_token=current_chain_node->current_first_token;
 873                 }
 874                 else//对应的不是结构体指针运算的情况,那就是减号
 875                 {
 876                     current_operator_token=malloc(sizeof(struct operator_token));
 877                     current_operator_token->priority=4;
 878                     current_operator_token->sub_number=2;
 879                     current_operator_token->current_op_type=minus;
 880                     phrase_operator_pop(4);
 881                     current_chain_node=first_chain_head;
 882                     first_chain_head=first_chain_head->next;
 883                     current_lex_token=current_chain_node->current_first_token;
 884                     free(current_lex_token->token_name);
 885                     free(current_lex_token);
 886                     free(current_chain_node);
 887                     phrase_operator_stack_pointer++;
 888                     phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 889                     predecent=1;
 890                 }
 891             }
 892             else//对应的不是结构体指针运算的情况,那就是减号
 893             {
 894                 current_operator_token=malloc(sizeof(struct operator_token));
 895                 current_operator_token->priority=4;
 896                 current_operator_token->sub_number=2;
 897                 current_operator_token->current_op_type=minus;
 898                 phrase_operator_pop(4);
 899                 current_chain_node=first_chain_head;
 900                 first_chain_head=first_chain_head->next;
 901                 current_lex_token=current_chain_node->current_first_token;
 902                 free(current_lex_token->token_name);
 903                 free(current_lex_token);
 904                 free(current_chain_node);
 905                 phrase_operator_stack_pointer++;
 906                 phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 907                 predecent=1;
 908             }
 909         }
 910         else//对应的是前面的是操作符的情况,则这里是当作负号来使用的
 911         {
 912             current_operator_token=malloc(sizeof(struct operator_token));
 913             current_operator_token->priority=2;
 914             current_operator_token->sub_number=1;
 915             current_operator_token->current_op_type=negative;
 916             if(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=2)
 917             {
 918                 phrase_operator_pop();
 919             }
 920             current_chain_node=first_chain_head;
 921             first_chain_head=first_chain_head->next;
 922             current_lex_token=current_chain_node->current_first_token;
 923             free(current_lex_token->token_name);
 924             free(current_lex_token);
 925             free(current_chain_node);
 926             phrase_operator_stack_pointer++;
 927             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 928             predecent=1;
 929         }
 930 
 931 
 932 
 933         break;
 934     case ')':
 935         //对于闭括号和闭方括号,我们需要一直弹出栈,直到遇到开括号和开方括号
 936         phrase_operator_pop(1);
 937         phrase_operator_stack_pointer--;
 938         current_chain_node=first_chain_head;
 939         first_chain_head=first_chain_head->next;
 940         current_lex_token=current_chain_node->current_first_token;
 941         free(current_lex_token->token_name);
 942         free(current_lex_token);
 943         free(current_chain_node);
 944         precedent=0;
 945         break;
 946     case ']':
 947         //处理方法同上,不过需要建立一个token节点
 948         phrase_operator_pop(1);
 949         temp_tree_node=malloc(sizeof(struct _phrase_tree_node));
 950         temp_tree_node->current_op_type=array_op;
 951         temp_tree_node->right_son=phrase_token_stack[phrase_token_stack_pointer];
 952         phrase_token_stack_pointer--;
 953         temp_tree_node->left_son=phrase_token_stack[phrase_token_stack_pointer];
 954         phrase_token_stack[phrase_token_stack_pointer]=temp_tree_node;
 955         phrase_operator_stack_pointer--;
 956         current_chain_node=first_chain_head;
 957         first_chain_head=first_chain_head->next;
 958         current_lex_token=current_chain_node->current_first_token;
 959         free(current_lex_token->token_name);
 960         free(current_lex_token);
 961         free(current_chain_node);
 962         precedent=0;
 963         break;
 964     case '!':
 965         current_chain_node=first_chain_head;
 966         first_chain_head=first_chain_head->next;
 967         free(current_chain_node->current_first_token->token_name);
 968         free(current_chain_node->current_first_token);
 969         free(current_chain_node);
 970         current_chain_node=first_chain_head;
 971         current_lex_token=current_chain_node->current_first_token;
 972         if(*(current_lex_token->token_name)!='=')
 973         {
 974             current_operator_token=malloc(sizeof(struct operator_token));
 975             current_operator_token->priority=2;
 976             current_operator_token->sub_number=1;
 977             current_operator_token->current_op_type=not;
 978             phrase_operator_pop(2);
 979             phrase_operator_stack_pointer++;
 980             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 981             predecent=1;
 982         }
 983         else//对应的是不等号
 984         {
 985             current_operator_token=malloc(sizeof(struct operator_token));
 986             current_operator_token->priority=7;
 987             current_operator_token->sub_number=2;
 988             current_operator_token->current_op_type= nequal;
 989             phrase_operator_pop(7);
 990             current_chain_node=first_chain_head;
 991             first_chain_head=first_chain_head->next;
 992             current_lex_token=current_chain_node->current_first_token;
 993             free(current_lex_token->token_name);
 994             free(current_lex_token);
 995             free(current_chain_node);
 996             phrase_operator_stack_pointer++;
 997             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
 998             predecent=1;
 999         }
1000         break;
1001     case '~':
1002         current_operator_token=malloc(sizeof(struct operator_token));
1003         current_operator_token->priority=2;
1004         current_operator_token->sub_number=1;
1005         current_operator_token->current_op_type=bit_rev;
1006         phrase_operator_pop(2);
1007         current_chain_node=first_chain_head;
1008         first_chain_head=first_chain_head->next;
1009         current_lex_token=current_chain_node->current_first_token;
1010         free(current_lex_token->token_name);
1011         free(current_lex_token);
1012         free(current_chain_node);
1013         phrase_operator_stack_pointer++;
1014         phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1015         predecent=1;
1016         break;
1017     case '&':
1018         if(predecent==1)//如果前面已经有操作符了,说明这个是取地址运算
1019         {
1020             current_operator_token=malloc(sizeof(struct operator_token));
1021             current_operator_token->priority=2;
1022             current_operator_token->sub_number=1;
1023             current_operator_token->current_op_type=get_adr;
1024             while(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=2)
1025             {
1026                 phrase_operator_pop();
1027             }
1028             current_chain_node=first_chain_head;
1029             first_chain_head=first_chain_head->next;
1030             current_lex_token=current_chain_node->current_first_token;
1031             free(current_lex_token->token_name);
1032             free(current_lex_token);
1033             free(current_chain_node);
1034             phrase_operator_stack_pointer++;
1035             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1036             predecent=1;
1037         }
1038         else//说明这个不是取地址运算,而是布尔运算或者位运算
1039         {
1040             current_chain_node=first_chain_head;
1041             next_chain_node=current_chain_node->next;
1042             next_lex_token=next_chain_node->current_first_token;
1043             if(next_lex_token->current_lex_type==an_operator)//如果后面跟的是操作符
1044             {
1045                 if(*(next_lex_token->token_name)=='&')//如果是布尔运算
1046                 {
1047                     current_operator_token=malloc(sizeof(struct operator_token));
1048                     current_operator_token->priority=11;
1049                     current_operator_token->sub_number=2;
1050                     current_operator_token->current_op_type=and;
1051                     phrase_operator_pop(11);
1052                     //这里需要吃掉两个字符
1053                     first_chain_head=first_chain_head->next;
1054                     current_lex_token=current_chain_node->current_first_token;
1055                     free(current_lex_token->token_name);
1056                     free(current_lex_token);
1057                     free(current_chain_node);
1058                     first_chain_head=first_chain_head->next;
1059                     free(next_lex_token->token_name);
1060                     free(next_lex_token);
1061                     free(next_chain_node);
1062                     phrase_operator_stack_pointer++;
1063                     phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1064                     predecent=1;
1065                 }
1066 
1067                 else//这里是位运算
1068                 {
1069                     current_operator_token=malloc(sizeof(struct operator_token));
1070                     current_operator_token->priority=7;
1071                     current_operator_token->sub_number=2;
1072                     current_operator_token->current_op_type=bit_and;
1073                     while(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=7)
1074                     {
1075                         phrase_operator_pop();
1076                     }
1077                     current_chain_node=first_chain_head;
1078                     first_chain_head=first_chain_head->next;
1079                     current_lex_token=current_chain_node->current_first_token;

1080                     free(current_lex_token->token_name);
1081                     free(current_lex_token);
1082                     free(current_chain_node);
1083                     phrase_operator_stack_pointer++;
1084                     phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1085                     predecent=1;
1086                 }
1087 
1088             }
1089             else//这里还是位运算
1090             {
1091                 current_operator_token=malloc(sizeof(struct operator_token));
1092                 current_operator_token->priority=7;
1093                 current_operator_token->sub_number=2;
1094                 current_operator_token->current_op_type=bit_and;
1095                 while(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=7)
1096                 {
1097                     phrase_operator_pop();
1098                 }
1099                 current_chain_node=first_chain_head;
1100                 first_chain_head=first_chain_head->next;
1101                 current_lex_token=current_chain_node->current_first_token;
1102                 free(current_lex_token->token_name);
1103                 free(current_lex_token);
1104                 free(current_chain_node);
1105                 phrase_operator_stack_pointer++;
1106                 phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1107                 predecent=1;
1108             }
1109         }
1110         break;
1111     case '*':
1112         if(predecent==1)//这里对应的是指针运算
1113         {
1114             current_operator_token=malloc(sizeof(struct operator_token));
1115             current_operator_token->priority=2;
1116             current_operator_token->sub_number=1;
1117             current_operator_token->current_op_type=get_mem;
1118             phrase_operator_pop(2);
1119             current_chain_node=first_chain_head;
1120             first_chain_head=first_chain_head->next;
1121             current_lex_token=current_chain_node->current_first_token;
1122             free(current_lex_token->token_name);
1123             free(current_lex_token);
1124             free(current_chain_node);
1125             phrase_operator_stack_pointer++;
1126             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1127             predecent=1;
1128         }
1129         else//对应的是乘法运算
1130         {
1131             current_operator_token=malloc(sizeof(struct operator_token));
1132             current_operator_token->priority=3;
1133             current_operator_token->sub_number=2;
1134             current_operator_token->current_op_type=multi;
1135             phrase_operator_pop(3);
1136             current_chain_node=first_chain_head;
1137             first_chain_head=first_chain_head->next;
1138             current_lex_token=current_chain_node->current_first_token;
1139             free(current_lex_token->token_name);
1140             free(current_lex_token);
1141             free(current_chain_node);
1142             phrase_operator_stack_pointer++;
1143             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1144             predecent=1;
1145         }
1146         break;
1147     case 's'://对应sizeof运算,这里又需要类型头部或者变量,这里我们强制要求有括号
1148         current_chain_node=first_chain_head;
1149         first_chain_head=first_chain_head->next;
1150         free(current_chain_node->current_first_chain->token_name);
1151         free(current_chain_node->current_first_chain);
1152         free(current_chain_node);
1153         current_chain_node=first_chain_head;
1154         current_lex_token=current_chain_node->current_first_token;
1155         if(*(current_lex_token->token_name)=='(')//准备sizeof,至于括号里面的东西就交给括号去处理
1156         {
1157             current_operator_token=malloc(sizeof(struct operator_token));
1158             current_operator_token->priority=2;
1159             current_operator_token->sub_number=1;
1160             current_operator_token->current_op_type=get_size;
1161             phrase_operator_pop(2);
1162             current_chain_node=first_chain_head;
1163             first_chain_head=first_chain_head->next;
1164             current_lex_token=current_chain_node->current_first_token;
1165             free(current_lex_token->token_name);
1166             free(current_lex_token);
1167             free(current_chain_node);
1168             phrase_operator_stack_pointer++;
1169             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1170             predecent=1;
1171         }
1172         else
1173         {
1174             printf("missing ( after sizeof
");
1175             exit();
1176         }
1177         break;
1178     case '/':
1179         current_operator_token=malloc(sizeof(struct operator_token));
1180         current_operator_token->priority=3;
1181         current_operator_token->sub_number=2;
1182         current_operator_token->current_op_type=div;
1183         phrase_operator_pop(3);
1184         current_chain_node=first_chain_head;
1185         first_chain_head=first_chain_head->next;
1186         current_lex_token=current_chain_node->current_first_token;
1187         free(current_lex_token->token_name);
1188         free(current_lex_token);
1189         free(current_chain_node);
1190         phrase_operator_stack_pointer++;
1191         phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1192         predecent=1;
1193         break;
1194     case '%':
1195         current_operator_token=malloc(sizeof(struct operator_token));
1196         current_operator_token->priority=3;
1197         current_operator_token->sub_number=2;
1198         current_operator_token->current_op_type=module;
1199         phrase_operator_pop(3);
1200         current_chain_node=first_chain_head;
1201         first_chain_head=first_chain_head->next;
1202         current_lex_token=current_chain_node->current_first_token;
1203         free(current_lex_token->token_name);
1204         free(current_lex_token);
1205         free(current_chain_node);
1206         phrase_operator_stack_pointer++;
1207         phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1208         predecent=1;
1209         break;
1210     case '+':
1211         current_operator_token=malloc(sizeof(struct operator_token));
1212         current_operator_token->priority=4;
1213         current_operator_token->sub_number=2;
1214         current_operator_token->current_op_type=get_mem;
1215         phrase_operator_pop(4);
1216         current_chain_node=first_chain_head;
1217         first_chain_head=first_chain_head->next;
1218         current_lex_token=current_chain_node->current_first_token;
1219         free(current_lex_token->token_name);
1220         free(current_lex_token);
1221         free(current_chain_node);
1222         phrase_operator_stack_pointer++;
1223         phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1224         predecent=1;
1225         break;
1226     case '<'://这里有三种情况。。。 移位和两个判断
1227         current_chain_node=first_chain_head;
1228         first_chain_head=first_chain_head->next;
1229         free(current_chain_node->current_first_token->token_name);
1230         free(current_chain_node->current_first_token);
1231         free(current_chain_node);
1232         current_chain_node=first_chain_head;
1233         current_lex_token=current_chain_node->current_first_token;
1234         if(current_lex_token->current_lex_type==an_operator)//如果后面接的是操作符
1235         {
1236             switch(*(current_lex_token->token_name))
1237             {
1238             case '<':
1239                 current_operator_token=malloc(sizeof(struct operator_token));
1240                 current_operator_token->priority=5;
1241                 current_operator_token->sub_number=2;
1242                 current_operator_token->current_op_type=left_shift;
1243                 phrase_operator_pop(5);
1244                 first_chain_head=first_chain_head->next;
1245                 free(current_lex_token->token_name);
1246                 free(current_lex_token);
1247                 free(current_chain_node);
1248                 phrase_operator_stack_pointer++;
1249                 phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1250                 predecent=1;
1251                 break;
1252             case '=':
1253                 current_operator_token=malloc(sizeof(struct operator_token));
1254                 current_operator_token->priority=6;
1255                 current_operator_token->sub_number=2;
1256                 current_operator_token->current_op_type=sma_eqa;
1257                 phrase_operator_pop(6);
1258                 first_chain_head=first_chain_head->next;
1259                 free(current_lex_token->token_name);
1260                 free(current_lex_token);
1261                 free(current_chain_node);
1262                 phrase_operator_stack_pointer++;
1263                 phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1264                 predecent=1;
1265                 break;
1266             default:
1267                 current_operator_token=malloc(sizeof(struct operator_token));
1268                 current_operator_token->priority=6;
1269                 current_operator_token->sub_number=2;
1270                 current_operator_token->current_op_type=smaller;
1271                 phrase_operator_pop(6);
1272                 phrase_operator_stack_pointer++;
1273                 phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1274                 predecent=1;
1275                 break;
1276             }
1277         }
1278         else
1279         {
1280             current_operator_token=malloc(sizeof(struct operator_token));
1281             current_operator_token->priority=6;
1282             current_operator_token->sub_number=2;
1283             current_operator_token->current_op_type=smaller;
1284             phrase_operator_pop(6);
1285             first_chain_head=first_chain_head->next;
1286             phrase_operator_stack_pointer++;
1287             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1288             predecent=1;
1289         }
1290         break;
1291     case '>':
1292         current_chain_node=first_chain_head;
1293         first_chain_head=first_chain_head->next;
1294         free(current_chain_node->current_first_token->token_name);
1295         free(current_chain_node->current_first_token);
1296         free(current_chain_node);
1297         current_chain_node=first_chain_head;
1298         current_lex_token=current_chain_node->current_first_token;
1299         if(current_lex_token->current_lex_type==an_operator)//如果后面接的是操作符
1300         {
1301             switch(*(current_lex_token->token_name))
1302             {
1303             case '>':
1304                 current_operator_token=malloc(sizeof(struct operator_token));
1305                 current_operator_token->priority=5;
1306                 current_operator_token->sub_number=2;
1307                 current_operator_token->current_op_type=right_shift;
1308                 phrase_operator_pop(5);
1309                 first_chain_head=first_chain_head->next;
1310                 free(current_lex_token->token_name);
1311                 free(current_lex_token);
1312                 free(current_chain_node);
1313                 phrase_operator_stack_pointer++;
1314                 phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1315                 predecent=1;
1316                 break;
1317             case '=':
1318                 current_operator_token=malloc(sizeof(struct operator_token));
1319                 current_operator_token->priority=6;
1320                 current_operator_token->sub_number=2;
1321                 current_operator_token->current_op_type=lar_eqa;
1322                 phrase_operator_pop(6);
1323                 first_chain_head=first_chain_head->next;
1324                 free(current_lex_token->token_name);
1325                 free(current_lex_token);
1326                 free(current_chain_node);
1327                 phrase_operator_stack_pointer++;
1328                 phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1329                 predecent=1;
1330                 break;
1331             default:
1332                 current_operator_token=malloc(sizeof(struct operator_token));
1333                 current_operator_token->priority=6;
1334                 current_operator_token->sub_number=2;
1335                 current_operator_token->current_op_type=larger;
1336                 phrase_operator_pop(6);
1337                 first_chain_head=first_chain_head->next;
1338                 free(current_lex_token->token_name);
1339                 free(current_lex_token);
1340                 free(current_chain_node);
1341                 phrase_operator_stack_pointer++;
1342                 phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1343                 predecent=1;
1344                 break;
1345             }
1346         }
1347         else
1348         {
1349             current_operator_token=malloc(sizeof(struct operator_token));
1350             current_operator_token->priority=6;
1351             current_operator_token->sub_number=2;
1352             current_operator_token->current_op_type=larger;
1353             phrase_operator_pop(6);
1354             phrase_operator_stack_pointer++;
1355             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1356             predecent=1;
1357         }
1358         break;
1359     case '=':
1360         current_chain_node=first_chain_head;
1361         current_lex_token=current_chain_node->current_first_token;
1362         free(current_lex_token->token_name);
1363         free(current_lex_token);
1364         free(current_chain_node);
1365         first_chain_head=first_chain_head->next;
1366         current_chain_node=first_chain_head;
1367         current_lex_token=current_chain_node->current_first_token;
1368         if(*(current_lex_token->token_name)=='=')
1369         {
1370             current_operator_token=malloc(sizeof(struct operator_token));
1371             current_operator_token->priority=7;
1372             current_operator_token->sub_number=2;
1373             current_operator_token->current_op_type=equal;
1374             phrase_operator_pop(2);
1375             free(current_lex_token->token_name);
1376             free(current_lex_token);
1377             free(current_chain_node);
1378             phrase_operator_stack_pointer++;
1379             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1380             predecent=1;
1381         }
1382         else
1383         {
1384             printf("stand alone = is encounted
");
1385             exit();
1386         }
1387         break;
1388     case '^':
1389         current_operator_token=malloc(sizeof(struct operator_token));
1390         current_operator_token->priority=9;
1391         current_operator_token->sub_number=2;
1392         current_operator_token->current_op_type=bit_xor;
1393         phrase_operator_pop(9);
1394         current_chain_node=first_chain_head;
1395         first_chain_head=first_chain_head->next;
1396         current_lex_token=current_chain_node->current_first_token;
1397         free(current_lex_token->token_name);
1398         free(current_lex_token);
1399         free(current_chain_node);
1400         phrase_operator_stack_pointer++;
1401         phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1402         predecent=1;
1403         break;
1404     case '|':
1405         current_chain_node=first_chain_head;
1406         current_lex_token=current_chain_node->current_first_token;
1407         free(current_lex_token->token_name);
1408         free(current_lex_token);
1409         free(current_chain_node);
1410         first_chain_head=first_chain_head->next;
1411         current_chain_node=first_chain_head;
1412         current_lex_token=current_chain_node->current_first_token;
1413         if(*(current_lex_token->token_name)=='|')
1414         {
1415             current_operator_token=malloc(sizeof(struct operator_token));
1416             current_operator_token->priority=12;
1417             current_operator_token->sub_number=2;
1418             current_operator_token->current_op_type=or;
1419             phrase_operator_pop(12);
1420             free(current_lex_token->token_name);
1421             free(current_lex_token);
1422             free(current_chain_node);
1423             phrase_operator_stack_pointer++;
1424             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1425             predecent=1;
1426         }
1427         else
1428         {
1429             current_operator_token=malloc(sizeof(struct operator_token));
1430             current_operator_token->priority=10;
1431             current_operator_token->sub_number=2;
1432             current_operator_token->current_op_type=bit_or;
1433             phrase_operator_pop(10);
1434             phrase_operator_stack_pointer++;
1435             phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
1436             predecent=1;
1437         }
1438         break;
1439     default:
1440         printf("unknown operator  %s is encounted
",in_op);
1441         exit();
1442         break;
1443     }
1444 
1445 }
1446 phrase_tree_node* get_expression(void)//这里才是他妈的重头戏啊,12级运算符不弄死你
1447 {
1448     phrase_tree_node* tree_node_one;
1449     first_lex_token* current_lex_token;
1450     first_lex_token* next_lex_token;
1451     first_token_chain* current_chain_node;
1452     first_token_chain* next_chain_node;
1453     current_token_chain=first_chain_head;
1454     predecent=1;
1455     phrase_token_stack_pointer=0;
1456     phrase_operator_stack_pointer=0;
1457     current_lex_token=current_token_chain->current_first_token;
1458     while(current_lex_token->current_lex_type!=delimit)
1459     {
1460         switch(current_lex_token->current_lex_type)
1461         {
1462         case char_type:
1463             tree_node_one=malloc(sizeof(struct _phrase_tree_node));
1464             tree_node_one->phrase_type=7;
1465             tree_node_one->var_name=current_lex_token->token_name;
1466             free(curren_lex_token);
1467             first_chain_head=first_chain_head->next;
1468             free(current_token_chain);
1469             current_token_chain=first_chain_head;
1470             current_lex_token=current_token_chain->current_first_token;
1471             predecent=0;
1472             break;
1473         case string_phrase:
1474             tree_node_one=malloc(sizeof(struct _phrase_tree_node));
1475             tree_node_one->phrase_type=8;
1476             tree_node_one->var_name=current_lex_token->token_name;
1477             free(curren_lex_token);
1478             first_chain_head=first_chain_head->next;
1479             free(current_token_chain);
1480             current_token_chain=first_chain_head;
1481             current_lex_token=current_token_chain->current_first_token;
1482             predecent=0;
1483             break;
1484         case constant:
1485             tree_node_one=malloc(sizeof(struct _phrase_tree_node));
1486             tree_node_one->phrase_type=1;
1487             tree_node_one->var_name=current_lex_token->token_name;
1488             free(curren_lex_token);
1489             first_chain_head=first_chain_head->next;
1490             free(current_token_chain);
1491             current_token_chain=first_chain_head;
1492             current_lex_token=current_token_chain->current_first_token;
1493             predecent=0;
1494             break;
1495         case name:
1496             next_chain_node=current_chain_node->next;
1497             next_lex_token=next_chain_node->current_first_token;
1498             if(*(next_lex_token->token_name)=='(')//这里对应的是一个函数
1499             {
1500                 tree_node_one=get_func();
1501                 phrase_token_stack_pointer++;
1502                 phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
1503                 current_chain_node=first_chain_head;
1504                 current_lex_token=current_chain_node->current_first_token;
1505             }
1506             else
1507             {
1508                 tree_node_one=malloc(sizeof(struct _phrase_tree_node));
1509                 tree_node_one->phrase_type=2;
1510                 tree_node_one->var_name=current_lex_token->token_name;
1511                 free(curren_lex_token);
1512                 first_chain_head=first_chain_head->next;
1513                 free(current_token_chain);
1514                 current_token_chain=first_chain_head;
1515                 current_lex_token=current_token_chain->current_first_token;
1516             }
1517             predecent=0;
1518             break;
1519         case an_operator:
1520             tackle_phrase_op(current_lex_token->token_name);
1521             current_token_chain=first_chain_head;
1522             current_lex_token=current_token_chain->current_first_token;
1523             break;
1524         default:
1525             printf("unknown lex type is encounted
");
1526             exit();
1527             break;
1528         }
1529     }
1530     //现在开始清空栈
1531     free(current_lex_token->token_name);
1532     free(curren_lex_token);
1533     first_chain_head=first_chain_head->next;
1534     free(current_token_chain);
1535     phrase_operator_pop(13);
1536     return phrase_token_stack[1];
1537 }
1538 
1539 phrase_tree_node* get_phrase(void)
1540 //这个函数是处理单个句型的函数,不包括定义和声明,那里我们特殊处理
1541 //我们要确保调用者在调用这个函数的时候,已经吃掉了所有的换行符
1542 {
1543     struct first_lex_token current_lex_token;
1544     struct _phrase_tree_node* copyto_address_node;//赋值的目标地址
1545     struct _phrase_tree_node* temp_node_one;
1546     struct _phrase_tree_node* temp_node_three;
1547     struct _phrase_tree_node* temp_node_two;
1548     struct first_token_chain* temp_first_one;
1549     struct first_token_chain* temp_first_two;
1550     copyto_address_node=NULL;
1551     temp_first_one=first_chain_head;
1552     temp_first_two=temp_first_one->next;
1553     current_lex_token=temp_first_one->current_first_token;
1554     if(current_lex_token->current_lex_type==name)//如果句子开头是名字
1555     {
1556         if(temp_first_two->current_first_token->current_lex_type==delimit)//如果紧接着的是分号,那么就是break语句
1557         {
1558             temp_node_one=malloc(sizeof(struct _phrase_tree_node));
1559             temp_node_one->phrase_type=0;//代表是break语句
1560             free(temp_first_one);
1561             temp_first_one=temp_first_two->next;
1562             free(temp_first_two);
1563             first_chain_head=temp_first_one;
1564             return temp_node_one;
1565         }
1566         else//如果不是break语句
1567         {
1568             if(*(temp_first_two->current_first_token->token_name)=='(')//这里对应的是函数调用
1569             {
1570                 if(serach_avl_tree(current_lex_token->token_name)->type_type==1)//如果真是的函数名
1571                 {
1572                     return get_func();//这个是专门处理函数的
1573                 }
1574                 else//如果是不合理的搭配,则报错
1575                 {
1576                     printf("error encountered
");
1577                     printf("invalid match
");
1578                     printf("the match is %s and ( 
",current_lex_token->token_name);
1579                     exit();
1580                 }
1581             }
1582             else//如果后面接的不是开括号,则一定是赋值语句,则我们需要首先获得id,然后再去处理表达式
1583             {
1584                 copyto_address_node=get_id();//利用get_id来获得地址,注意这里get_id会保留=的存在,需要自己去处理
1585                 temp_first_one=first_chain_head->next;
1586                 free(first_chain_head);
1587                 first_chain_head=temp_first_one;
1588                 temp_node_two=get_expression();//利用函数来获得表达式
1589                 temp_node_three=malloc(sizeof(struct _phrase_tree_node));//分配返回节点
1590                 temp_node_three->phrase_type=4;
1591                 temp_node_three->left_son=copyto_address_node;
1592                 temp_node_three->right_son=temp_node_two;
1593                 return temp_node_three;
1594             }//处理完毕
1595         }
1596     }
1597     else//如果开头不是名字,那么就一定是赋值运算。
1598     {
1599         copyto_address_node=get_id();
1600         temp_first_one=first_chain_head->next;
1601         free(first_chain_head);
1602         first_chain_head=temp_first_one;
1603         temp_node_two=get_expression();//利用函数来获得表达式
1604         temp_node_three=malloc(sizeof(struct _phrase_tree_node));//分配返回节点
1605         temp_node_three->phrase_type=4;
1606         temp_node_three->left_son=copyto_address_node;
1607         temp_node_three->right_son=temp_node_two;
1608         return temp_node_three;
1609     }
1610 }
原文地址:https://www.cnblogs.com/huangfeidian/p/3211165.html