• minic 类型声明与变量定义句型处理


       1 #include "token_define.h"
       2 //这个文件是用来处理句型的,对于算术表达式都是放在前面的那个头文件中来处理
       3 typedef struct _handle_tree_node//句型语法树的节点
       4 {
       5     union
       6     {
       7         int handle_type;//1代表基本句型,2代表代码块,
       8         enum keywords_type keyword;//代表关键字的类型
       9     }
      10     union//产生体
      11     {
      12         phrase_tree_node* orginal_phrase_node;//代表单个基本句型
      13         struct
      14         {
      15             struct _handle_tree_node* for_initiate;
      16             struct _handle_tree_node* for_judge;
      17             struct _handle_tree_node* for_modify;
      18             struct _handle_tree_node* for_codeblock;
      19         }for_code;//代表for句型
      20         struct
      21         {
      22             struct _handle_tree_node* while_judge;
      23             struct _handle_tree_node* while_codeblock;
      24         }while_code;//代表while句型
      25         struct
      26         {
      27             struct _handle_tree_node* branch_judge;
      28             struct _handle_tree_node* if_code;
      29             struct _handle_tree_node* else_code;
      30         }branch_code;//代表if else句型
      31     };
      32 }handle_tree_node;
      33 //上面的是动作执行代码,根据我们对文法的定义,还有类型定义部分,函数定义部分,变量声明部分
      34 //其中函数定义部分里面比较复杂,会有递归,我们放到最后去考虑
      35 //下面先谈最简单的变量声明部分,变量声明部分中最重要的是声明头部,因为声明头经常会被使用。
      36 //注意这里的声明头部与c语言不一样的地方就是*是跟着声明头的,而不是跟着变量名
      37 //对于声明头的结构,我们在符号表中已经定义过了
      38 //这里我们再次定义一下
      39 //对于变量的结构,在符号表中也已经定义过了
      40 //typedef struct _var_hash_node
      41 //{
      42 //    pbasic_type_pattern current;
      43 //    char* var_name;
      44 //    int pointer_layer;//代表指针的级数,我们限制为8级,即只占一个字节,其他三个字节以后再使用,先留着
      45 //    parray_description array_layer;//这里是数组描述,以链表串起多级数组
      46 //    ptype_avl_tree current_type;//代表分量的类型
      47 //}var_hash_node,*pvar_hash_node;
      48 //变量声明的产生式  
      49 //list_name: new_name|list_name[unsign_constant_int] 对应数组的情形
      50 //data_type_basic_body: int |char|float|double|long|unsigned  基础类型体
      51 //data_type_basic_pointer: data_type_basic *|data_type_basic_pointer * 基础类型的指针
      52 //data_type_basic:data_type_basic_body|data_type_basic_pointer 基础类型(包括指针)
      53 //data_type_struct:data_type_struct_body|data_type_struct_pointer                结构体类型声明头部
      54 //data_type_union:data_type_union_body|data_type_union_pointer                    联合类型声明头部
      55 //data_type_head:data_type_basic|data_type_struct|data_type_union                所有的类型头部
      56 //data_var_decl: data_type_head list_name;                                所有的显示变量声明
      57 //注意我们这里只允许一次生命一个变量名
      58 //实际处理的时候我们并不是按照前面的产生体严格执行的,我们在遇到变量声明的时候,首先查看第一个name
      59 //如果不是struct和union,则对这个名字进行查找类型avl,如果存在,则获得这个类型,然后再处理指针
      60 //然后是变量名字,然后是数组描述,这些工作很简单,我们没有必要去放到lr文法分析中去,那样只会增加复杂度
      61 //如果是struct和union,则获得下一个token,对这个token执行上述操作,都是一样的
      62 //所以我们碰到变量声明的时候,直接处理然后把data_var_decl放到句型栈中,让文法分析来处理
      63 //
      64 //
      65 //
      66 //现在来处理类型定义,由于类型定义也是嵌套的,所以我们也需要使用一个栈来处理类型定义,同时为当前定义
      67 //构建一个语法分析树来处理,但是类型定义除了嵌套定义外,还存在自引用定义,为此,我们每次处理符合类型的时候
      68 //首先记录各个分量的变量名字和类型名字(这里要求各个分量的类型名都已经被处理了)。当得到所有的分量之后
      69 //另外分配空间来构造产生体链表,并把类型名字通过查找avl来获得相应的avl节点,并把类型名修改为avl节点的指针
      70 //注意这里还有自引用,因此我们在插入avl的时候,只是先把产生体链表构造为空,插入之后再去填充产生体链表。
      71 //注意在处理类型声明的时候,我们还要为代码块维护一个类型链表,按照拓扑序来排列
      72 //因此在每处理完一个复合类型之后就插入到当前的类型链表中
      73 //我们对于每一个代码块都有一个类型链表,而每个函数又是一个代码块,因此会有嵌套的处理
      74 //为此我们又需要维护一个栈,栈中存放的是当前的类型链表。
      75 //我们为每一个函数代码块维护一个类型链表、一个变量链表、一个代码块语法树的头、一个参数链表
      76 //每当调用一个函数的时候,我们就把类型链表、变量链表、参数链表挂载到符号表里面,然后再用后序遍历语法树
      77 //来执行代码。
      78 //在调用返回之后,按照插入序的逆序来删除所有插入到符号表里面的项
      79 //至此一个代码块里面的类型链表已经构建完成了
      80 //由于这里只是构建链表,所以所有的类型都是靠名称来索引的,而不是靠指针来索引
      81 int code_scope;//这个用来表示当前的代码域
      82 struct _composition_list* current_block_pattern_list_rear;//这个代表了当前代码块的类型链表的末尾
      83 struct var_decl_link//这个是代码块的变量链表
      84 {
      85     struct _var_hash_node* current_var_node;
      86     struct var_decl_link* next;
      87 };
      88 struct type_decl_link//这个是代码块的类型链表
      89 {
      90     struct _type_description_ current_type_node;
      91     struct type_decl_link* next;
      92 };
      93 struct function_description
      94 {
      95     struct var_decl_link* current_var_link;
      96     struct type_decl_link* current_type_link;
      97     struct _handle_tree_node* current_action_link;//代表动作链表
      98 };
      99 
     100 int check_constant_type(char* in_constant)//1代表小数,0代表整数
     101 {
     102     while(*(in_constant)!='')
     103     {
     104         if(*(in_constant)=='.')
     105         {
     106             return 1;
     107         }
     108         else
     109         {
     110             in_constant++;
     111         }
     112     }
     113     return 0;
     114 }
     115 int check_duplicated_name( struct _composition_list list_head,char* in_name)//检查当前的产生体链表是不是已经有重名了
     116 {
     117     while(list_head!=NULL)
     118     {
     119         if(strcmp(list_head->current->faction_name,in_name)==0)
     120         {
     121             return 1;
     122         }
     123         else
     124         {
     125             list_head=list_head->next;
     126         }
     127     }
     128     return 0;
     129 }
     130 struct _composition_list* tackle_type_define(struct type_decl_link** type_link_head)
     131 //这个函数是用来处理内部定义的,在处理之前,头部信息已经被插入到了avl树中 
     132 //只不过生成体没有初始化,这样做的意义就是为了达到结构体自引用
     133 //结构体自引用只会在代码最外层的符合类型定义时才使用,对于内部的复合类型,自引用是被禁止的。
     134 //所以这个函数在碰到嵌套的符合类型定义的时候,会进行递归调用,
     135 //如果未初始化,则将类型名称域与变量名称域指向同一个字符串
     136 {
     137     //这里还没有写完,以后再写,先写最简单的 
     138     struct first_token_chain* current_chain_node;
     139     struct first_token_chain* next_chain_node;
     140     struct first_lex_token* current_lex_node;
     141     struct first_lex_token* next_lex_node;
     142     struct type_decl_link* return_decl_link;//作为插入到目前代码块的类型链表的节点,从头部插入
     143     struct _composition_list return_list_head;//代表返回链表的头部
     144     struct _composition_list return_list_rear;//代表返回链表的尾部,作为插入用
     145     struct _composition_list temp_list_node;//在遇到内部的符合类型声明的时候来使用的
     146     struct _basic_type_pattern temp_sub_pattern;//代表遇到的分量节点
     147     struct _type_description temp_type_desc;//代表查询avl得到的名称类型
     148     struct _type_description new_type_desc;//代表在遇到一个复合类型声明的时候新建的类型描述
     149     struct _array_description temp_array_desc;//代表临时的数组描述符
     150     return_list_head=return_list_rear=NULL;
     151     current_chain_node=first_chain_head;
     152     first_chain_head=first_chain_head->next;
     153     current_lex_node=current_chain_node->current_first_token;
     154     free(current_lex_node->token_name);
     155     free(current_lex_node);
     156     free(current_chain_node);
     157     current_chain_node=first_chain_head;
     158     current_lex_node=current_chain_node->current_first_token;
     159     //这里我们都已经做好参数检查了,所以没有必要再去检查,第一个词法单元应该是大括号,而且我们在传参
     160     //的时候已经检查过了
     161     while(*(current_lex_node->token_name)!='}')//对于内部嵌套的大括号,会被递归处理掉,因此不用担心
     162     {
     163         switch(current_lex_node->current_lex_type)
     164         {
     165         case new_line:
     166             first_chain_head=first_chain_head->next;
     167             free(current_lex_node->token_name);
     168             free(current_lex_node);
     169             free(current_chain_node);
     170             current_chain_node=first_chain_head;
     171             current_lex_node=current_chain_node->current_first_token;
     172             break;
     173         case name:
     174             temp_type_desc=search_avl_tree(current_lex_node->token_name);
     175             if(temp_type_desc->type_type!=4)//如果开头不是关键字
     176             {
     177                 printf("unexpected %s at the begining of declare statement 
    ",current_lex_node->token_name);
     178                 exit();
     179             }
     180             else
     181             {
     182                 switch(*(current_lex_node->token_name))
     183                 {
     184                 case 'i'://对应的是整数
     185                 case 'c'://对应的是字符
     186                 case 'f'://对应的是单精度浮点
     187                 case 'l'://对应的是长整数
     188                 case 'd'://对应的是双精度浮点
     189                     temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
     190                     temp_sub_pattern->pointer_layer=0;
     191                     temp_sub_pattern->array_layer=NULL;
     192                     temp_sub_pattern->pattern_name=current_lex_node->token_name;
     193                     first_chain_head=first_chain_head->next;
     194                     free(current_lex_node->token_name);
     195                     free(current_lex_node);
     196                     free(current_chain_node);
     197                     current_chain_node=first_chain_head;
     198                     current_lex_node=current_chain_node->current_first_token;
     199                     while(*(current_lex_node->token_name)=='*')//处理指针
     200                     {
     201                         temp_sub_pattern->pointer_layer=temp_sub_pattern->pointer_layer<<1+1;
     202                         first_chain_head=first_chain_head->next;
     203                         free(current_lex_node->token_name);
     204                         free(current_lex_node);
     205                         free(current_chain_node);
     206                         current_chain_node=first_chain_head;
     207                         current_lex_node=current_chain_node->current_first_token;
     208                     }
     209                     //现在开始来处理名字
     210                     if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
     211                     {
     212                         printf("unexpected %s as var_name
    ",current_lex_node->token_name);
     213                         exit();
     214                     }
     215                     else//如果遇到的是名字,则需要考虑当前产生体中是否已经有重名的了
     216                     {
     217                         if(return_list_head==NULL)//如果为空的话,就没必要去考虑了直接插入
     218                         {
     219                             temp_sub_pattern->faction_name=current_lex_node->token_name;
     220                             return_list_head=return_list_rear=temp_sub_pattern;
     221                         }
     222                         else
     223                         {
     224                             if(!check_duplicated_name(return_list_head,current_lex_node->token_name))
     225                             {
     226                                 temp_sub_pattern->faction_name=current_lex_node->token_name;
     227                             }
     228                             else
     229                             {
     230                                 printf("duplicated name %s encounted 
    ",current_lex_node->token_name);
     231                                 exit();
     232                             }
     233                         }
     234                         //名称检查已经完成,现在来处理数组
     235                         temp_list_node=malloc(sizeof(struct _composition_list));
     236                         temp_list_node->current=temp_sub_pattern;
     237                         temp_list_node->next=NULL;
     238                         if(return_list_head==NULL)
     239                         {
     240                             return_list_head=return_list_rear=temp_list_node;
     241                         }
     242                         else
     243                         {
     244                             return_list_rear->next=temp_list_node;
     245                             return_list_rear=temp_list_node;
     246                         }
     247                         first_chain_head=first_chain_head->next;
     248                         free(current_lex_node);
     249                         free(current_chain_node);
     250                         current_chain_node=first_chain_head;
     251                         current_lex_node=current_chain_node->current_first_token;
     252                         while(*(current_lex_node->token_name)=='[')//处理指针
     253                         {
     254                             first_chain_head=first_chain_head->next;
     255                             free(current_lex_node->token_name);
     256                             free(current_lex_node);
     257                             free(current_chain_node);
     258                             current_chain_node=first_chain_head;
     259                             current_lex_node=current_chain_node->current_first_token;
     260                             //处理数组索引
     261                             if(current_lex_node->current_lex_type!=constant)
     262                             {
     263                                 printf("need constant in array decl
    ");
     264                                 exit();
     265                             }
     266                             else
     267                             {
     268                                 if(check_constant_type(current_lex_node->token_name))
     269                                 {
     270                                     printf("need integer in array decl
    ");
     271                                     exit();
     272                                 }
     273                                 else
     274                                 {
     275                                     temp_array_desc=malloc(sizeof(struct _array_description));
     276                                     temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
     277                                     temp_array_desc->next=temp_sub_pattern->array_layer;
     278                                     temp_sub_pattern->array_layer=temp_array_desc;
     279                                 }
     280 
     281 
     282                             }
     283                             first_chain_head=first_chain_head->next;
     284                             free(current_lex_node->token_name);
     285                             free(current_lex_node);
     286                             free(current_chain_node);
     287                             current_chain_node=first_chain_head;
     288                             current_lex_node=current_chain_node->current_first_token;
     289                             if(*(current_lex_node->token_name)!=']')
     290                             {
     291                                 printf("need ] while encount %s 
    ",current_lex_node->token_name);
     292                                 exit();
     293                             }
     294                             else
     295                             {
     296                                 first_chain_head=first_chain_head->next;
     297                                 free(current_lex_node->token_name);
     298                                 free(current_lex_node);
     299                                 free(current_chain_node);
     300                                 current_chain_node=first_chain_head;
     301                                 current_lex_node=current_chain_node->current_first_token;
     302                             }
     303                         }
     304                         if(current_lex_node->current_lex_type==delimit)//吃掉分号
     305                         {
     306                             first_chain_head=first_chain_head->next;
     307                             free(current_lex_node->token_name);
     308                             free(current_lex_node);
     309                             free(current_chain_node);
     310                             current_chain_node=first_chain_head;
     311                             current_lex_node=current_chain_node->current_first_token;
     312                         }
     313                         else
     314                         {
     315                             printf("delimit needed after decl
    ");
     316                             exit();
     317                         }
     318                     }
     319                     break;
     320                 case 'u'://这里有两种情况,一个是联合,一个是无符号整形,我们需要看到第三个字母才能判断
     321                     if(*(current_lex_node->token_name+2)=='i')//对应的是联合体
     322                     {
     323                         temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
     324                         temp_sub_pattern->parray_description=NULL;
     325                         temp_sub_pattern->pointer_layer=0;
     326                         temp_sub_pattern->pattern_name=NULL;
     327                         first_chain_head=first_chain_head->next;
     328                         free(current_lex_node->token_name);
     329                         free(current_lex_node);
     330                         free(current_chain_node);
     331                         current_chain_node=first_chain_head;
     332                         current_lex_node=current_chain_node->current_first_token;
     333                         if(*(current_lex_node->token_name)!='{')//如果后面接的不是大括号,则开始进行联合类型检查
     334                         {
     335                             first_chain_head=first_chain_head->next;
     336                             free(current_lex_node->token_name);
     337                             free(current_lex_node);
     338                             free(current_chain_node);
     339                             current_chain_node=first_chain_head;
     340                             current_lex_node=current_chain_node->current_first_token;
     341                             //这里需要检查一下这个结构体是否已经声明了
     342                             temp_type_desc=search_avl_tree(current_lex_node->token_name);
     343                             if(temp_type_desc==NULL)
     344                             {
     345                                 printf("invalid union name %s encounted
    ",current_lex_node->token_name);
     346                                 exit();
     347                             }
     348                             else
     349                             {
     350                                 //确定类型
     351                                 temp_sub_pattern->pattern_name=current_lex_node->token_name;
     352                                 while(*(current_lex_node->token_name)=='*')//处理指针
     353                                 {
     354                                     temp_sub_pattern->pointer_layer=temp_sub_pattern->pointer_layer<<1+1;
     355                                     first_chain_head=first_chain_head->next;
     356                                     free(current_lex_node);
     357                                     free(current_chain_node);
     358                                     current_chain_node=first_chain_head;
     359                                     current_lex_node=current_chain_node->current_first_token;
     360                                 }
     361                                 //现在开始来处理名字
     362                                 if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
     363                                 {
     364                                     printf("unexpected %s as var_name
    ",current_lex_node->token_name);
     365                                     exit();
     366                                 }
     367                                 else//如果遇到的是名字,则需要考虑当前产生体中是否已经有重名的了
     368                                 {
     369                                     if(return_list_head==NULL)//如果为空的话,就没必要去考虑了直接插入
     370                                     {
     371                                         temp_sub_pattern->faction_name=current_lex_node->token_name;
     372                                         return_list_head=return_list_rear=temp_sub_pattern;
     373                                     }
     374                                     else
     375                                     {
     376                                         if(!check_duplicated_name(return_list_head,current_lex_node->token_name))
     377                                         {
     378                                             temp_sub_pattern->faction_name=current_lex_node->token_name;
     379                                         }
     380                                         else
     381                                         {
     382                                             printf("duplicated name %s encounted 
    ",current_lex_node->token_name);
     383                                             exit();
     384                                         }
     385                                     }
     386                                     //名称检查已经完成,现在来处理数组
     387                                     temp_list_node=malloc(sizeof(struct _composition_list));
     388                                     temp_list_node->current=temp_sub_pattern;
     389                                     temp_list_node->next=NULL;
     390                                     if(return_list_head==NULL)
     391                                     {
     392                                         return_list_head=return_list_rear=temp_list_node;
     393                                     }
     394                                     else
     395                                     {
     396                                         return_list_rear->next=temp_list_node;
     397                                         return_list_rear=temp_list_node;
     398                                     }
     399                                     first_chain_head=first_chain_head->next;
     400                                     free(current_lex_node);
     401                                     free(current_chain_node);
     402                                     current_chain_node=first_chain_head;
     403                                     current_lex_node=current_chain_node->current_first_token;
     404                                     while(*(current_lex_node->token_name)=='[')//处理指针
     405                                     {
     406                                         first_chain_head=first_chain_head->next;
     407                                         free(current_lex_node->token_name);
     408                                         free(current_lex_node);
     409                                         free(current_chain_node);
     410                                         current_chain_node=first_chain_head;
     411                                         current_lex_node=current_chain_node->current_first_token;
     412                                         //处理数组索引
     413                                         if(current_lex_node->current_lex_type!=constant)
     414                                         {
     415                                             printf("need constant in array decl
    ");
     416                                             exit();
     417                                         }
     418                                         else
     419                                         {
     420                                             if(check_constant_type(current_lex_node->token_name))
     421                                             {
     422                                                 printf("need integer in array decl
    ");
     423                                                 exit();
     424                                             }
     425                                             else
     426                                             {
     427                                                 temp_array_desc=malloc(sizeof(struct _array_description));
     428                                                 temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
     429                                                 temp_array_desc->next=temp_sub_pattern->array_layer;
     430                                                 temp_sub_pattern->array_layer=temp_array_desc;
     431                                             }
     432 
     433 
     434                                         }
     435                                         first_chain_head=first_chain_head->next;
     436                                         free(current_lex_node->token_name);
     437                                         free(current_lex_node);
     438                                         free(current_chain_node);
     439                                         current_chain_node=first_chain_head;
     440                                         current_lex_node=current_chain_node->current_first_token;
     441                                         if(*(current_lex_node->token_name)!=']')
     442                                         {
     443                                             printf("need ] while encount %s 
    ",current_lex_node->token_name);
     444                                             exit();
     445                                         }
     446                                         else
     447                                         {
     448                                             first_chain_head=first_chain_head->next;
     449                                             free(current_lex_node->token_name);
     450                                             free(current_lex_node);
     451                                             free(current_chain_node);
     452                                             current_chain_node=first_chain_head;
     453                                             current_lex_node=current_chain_node->current_first_token;
     454                                         }
     455                                     }
     456                                     if(current_lex_node->current_lex_type==delimit)//吃掉分号
     457                                     {
     458                                         first_chain_head=first_chain_head->next;
     459                                         free(current_lex_node->token_name);
     460                                         free(current_lex_node);
     461                                         free(current_chain_node);
     462                                         current_chain_node=first_chain_head;
     463                                         current_lex_node=current_chain_node->current_first_token;
     464                                     }
     465                                     else
     466                                     {
     467                                         printf("delimit needed after decl
    ");
     468                                         exit();
     469                                     }
     470                                 }
     471                             }
     472 
     473                         }
     474                         else//这里是联合类型声明
     475                         {
     476                             new_type_desc=malloc(sizeof(struct _type_description));
     477                             new_type_desc->current_gen_list=tackle_type_define(type_link_head);
     478                             new_type_desc->type_type=3;
     479                             return_decl_link=malloc(sizeof(struct type_decl_link));
     480                             return_decl_link->current_type_node=new_type_desc;
     481                             return_decl_link->next=(*type_link_head);
     482                             (*type_link_head)=return_decl_link;
     483                             //下面是抛弃大括号
     484                             current_chain_node=first_chain_head;
     485                             current_lex_node=current_chain_node->current_first_token;
     486                             //现在来获得分量名字
     487                             if(current_lex_node->current_lex_type!=name)//类型检查
     488                             {
     489                                 printf("need name as union name while encounted %s
    ",current_lex_node->token_name);
     490                                 exit();
     491                             }
     492                             else//检查重名
     493                             {
     494                                 if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果有重名的
     495                                 {
     496                                     printf("duplicated type name %s 
    ",current_lex_node->token_name);
     497                                     exit();
     498                                 }
     499                                 else
     500                                 {
     501                                     if(check_duplicated_name(return_list_head,current_lex_node->token_name))
     502                                     {
     503                                         printf("duplicated var name %s
    ",current_lex_node->token_name);
     504                                         exit();
     505                                     }
     506                                     else//如果没有重名的
     507                                     {
     508                                         new_type_desc->name=current_lex_node->token_name;
     509                                         insert_avl_node(new_type_desc);
     510                                         //这里将这个类型插入到avl符号表中
     511                                         //然后还要插入分量列表中
     512                                         temp_sub_pattern->faction_name=current_lex_node->token_name;
     513                                         temp_sub_pattern->pattern_name=current_lex_node->token_name;
     514                                         temp_list_node=malloc(sizeof(struct _composition_list));
     515                                         temp_list_node->current=temp_sub_pattern;
     516                                         temp_list_node->next=NULL;
     517                                         if(return_list_head==NULL)
     518                                         {
     519                                             return_list_head=return_list_rear=temp_list_head;
     520                                         }
     521                                         else
     522                                         {
     523                                             return_list_rear->next=temp_list_head;
     524                                             return_list_rear=temp_list_head;
     525                                         }
     526                                         //至此类型分量也已经插入完毕了
     527                                         first_chain_head=first_chain_head->next;
     528                                         free(current_lex_node);
     529                                         free(current_chain_node);
     530                                         current_chain_node=first_chain_head;
     531                                         current_lex_node=current_chain_node->current_first_token;
     532                                         //再次吃掉分号
     533                                         if(*(current_lex_node->token_name)!=';')
     534                                         {
     535                                             printf("missing ; after type define
    ");
     536                                             exit();
     537                                         }
     538                                         else
     539                                         {
     540                                             first_chain_head=first_chain_head->next;
     541                                             free(current_lex_node->token_name);
     542                                             free(current_lex_node);
     543                                             free(current_chain_node);
     544                                             current_chain_node=first_chain_head;
     545                                             current_lex_node=current_chain_node->current_first_token;
     546                                             //然后再吃掉所有的换行符
     547                                         }
     548                                     }
     549                                 }
     550                             }
     551                         }
     552                     }
     553                     else//对应的是无符号整数
     554                     {
     555                         temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
     556                         temp_sub_pattern->pointer_layer=0;
     557                         temp_sub_pattern->array_layer=NULL;
     558                         temp_sub_pattern->pattern_name=current_lex_node->token_name;
     559                         first_chain_head=first_chain_head->next;
     560                         free(current_lex_node);
     561                         free(current_chain_node);
     562                         current_chain_node=first_chain_head;
     563                         current_lex_node=current_chain_node->current_first_token;
     564                         while(*(current_lex_node->token_name)=='*')//处理指针
     565                         {
     566                             temp_sub_pattern->pointer_layer=temp_sub_pattern->pointer_layer<<1+1;
     567                             first_chain_head=first_chain_head->next;
     568                             free(current_lex_node->token_name);
     569                             free(current_lex_node);
     570                             free(current_chain_node);
     571                             current_chain_node=first_chain_head;
     572                             current_lex_node=current_chain_node->current_first_token;
     573                         }
     574                         //现在开始来处理名字
     575                         if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
     576                         {
     577                             printf("unexpected %s as var_name
    ",current_lex_node->token_name);
     578                             exit();
     579                         }
     580                         else//如果遇到的是名字,则需要考虑当前产生体中是否已经有重名的了
     581                         {
     582                             if(return_list_head==NULL)//如果为空的话,就没必要去考虑了直接插入
     583                             {
     584                                 temp_sub_pattern->faction_name=current_lex_node->token_name;
     585                                 return_list_head=return_list_rear=temp_sub_pattern;
     586                             }
     587                             else
     588                             {
     589                                 if(!check_duplicated_name(return_list_head,current_lex_node->token_name))
     590                                 {
     591                                     temp_sub_pattern->faction_name=current_lex_node->token_name;
     592                                 }
     593                                 else
     594                                 {
     595                                     printf("duplicated name %s encounted 
    ",current_lex_node->token_name);
     596                                     exit();
     597                                 }
     598                             }
     599                             //名称检查已经完成
     600                             first_chain_head=first_chain_head->next;
     601                             free(current_lex_node);
     602                             free(current_chain_node);
     603                             current_chain_node=first_chain_head;
     604                             current_lex_node=current_chain_node->current_first_token;
     605                             while(*(current_lex_node->token_name)=='[')//处理数组
     606                             {
     607                                 first_chain_head=first_chain_head->next;
     608                                 free(current_lex_node->token_name);
     609                                 free(current_lex_node);
     610                                 free(current_chain_node);
     611                                 current_chain_node=first_chain_head;
     612                                 current_lex_node=current_chain_node->current_first_token;
     613                                 //处理数组索引
     614                                 if(current_lex_node->current_lex_type!=constant)//判断是不是常数
     615                                 {
     616                                     printf("need constant in array decl
    ");
     617                                     exit();
     618                                 }
     619                                 else
     620                                 {
     621                                     if(check_constant_type(current_lex_node->token_name))//判断是不是整数
     622                                     {
     623                                         printf("need integer in array decl
    ");
     624                                         exit();
     625                                     }
     626                                     else
     627                                     {
     628                                         temp_array_desc=malloc(sizeof(struct _array_description));
     629                                         temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
     630                                         temp_array_desc->next=temp_sub_pattern->array_layer;
     631                                         temp_sub_pattern->array_layer=temp_array_desc;
     632                                     }
     633                                 }
     634                                 first_chain_head=first_chain_head->next;
     635                                 free(current_lex_node->token_name);
     636                                 free(current_lex_node);
     637                                 free(current_chain_node);
     638                                 current_chain_node=first_chain_head;
     639                                 current_lex_node=current_chain_node->current_first_token;
     640                                 if(*(current_lex_node->token_name)!=']')
     641                                 {
     642                                     printf("need ] while encount %s 
    ",current_lex_node->token_name);
     643                                     exit();
     644                                 }
     645                                 else
     646                                 {
     647                                     first_chain_head=first_chain_head->next;
     648                                     free(current_lex_node->token_name);
     649                                     free(current_lex_node);
     650                                     free(current_chain_node);
     651                                     current_chain_node=first_chain_head;
     652                                     current_lex_node=current_chain_node->current_first_token;
     653                                 }
     654                             }
     655                             temp_list_node=malloc(sizeof(struct _composition_list));
     656                             temp_list_node->current=temp_sub_pattern;
     657                             temp_list_node->next=NULL;
     658                             //进行分量的插入
     659                             if(return_list_head==NULL)
     660                             {
     661                                 return_list_head=return_list_rear=temp_list_node;
     662                             }
     663                             else
     664                             {
     665                                 return_list_rear->next=temp_list_node;
     666                                 return_list_rear=temp_list_node;
     667                             }
     668                             if(current_lex_node->current_lex_type==delimit)//吃掉分号
     669                             {
     670                                 first_chain_head=first_chain_head->next;
     671                                 free(current_lex_node->token_name);
     672                                 free(current_lex_node);
     673                                 free(current_chain_node);
     674                                 current_chain_node=first_chain_head;
     675                                 current_lex_node=current_chain_node->current_first_token;
     676                             }
     677                             else
     678                             {
     679                                 printf("delimit needed after decl
    ");
     680                                 exit();
     681                             }
     682                         }
     683                     }
     684                     break;
     685                 case 's'://对应的是结构体
     686                     temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
     687                     temp_sub_pattern->parray_description=NULL;
     688                     temp_sub_pattern->pointer_layer=0;
     689                     temp_sub_pattern->pattern_name=NULL;
     690                     first_chain_head=first_chain_head->next;
     691                     free(current_lex_node->token_name);
     692                     free(current_lex_node);
     693                     free(current_chain_node);
     694                     current_chain_node=first_chain_head;
     695                     current_lex_node=current_chain_node->current_first_token;
     696                     if(*(current_lex_node->token_name)!='{')//如果后面接的不是大括号,开始变量声明
     697                     {
     698                         first_chain_head=first_chain_head->next;
     699                         free(current_lex_node->token_name);
     700                         free(current_lex_node);
     701                         free(current_chain_node);
     702                         current_chain_node=first_chain_head;
     703                         current_lex_node=current_chain_node->current_first_token;
     704                         //这里需要检查一下这个结构体是否已经声明了
     705                         temp_type_desc=search_avl_tree(current_lex_node->token_name);
     706                         if(temp_type_desc==NULL)//检查结构体的名字是否有效
     707                         {
     708                             printf("unknown struct name %s encounted 
    ",current_lex_node->token_name);
     709                             exit();
     710                         }
     711                         else//结构体类型有效
     712                         {
     713                             temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
     714                             temp_sub_pattern->pointer_layer=0;
     715                             temp_sub_pattern->array_layer=NULL;
     716                             temp_sub_pattern->pattern_name=current_lex_node->token_name;
     717                             first_chain_head=first_chain_head->next;
     718                             free(current_lex_node);
     719                             free(current_chain_node);
     720                             current_chain_node=first_chain_head;
     721                             current_lex_node=current_chain_node->current_first_token;
     722                             while(*(current_lex_node->token_name)=='*')//处理指针
     723                             {
     724                                 temp_sub_pattern->pointer_layer=temp_sub_pattern->pointer_layer<<1+1;
     725                                 first_chain_head=first_chain_head->next;
     726                                 free(current_lex_node->token_name);
     727                                 free(current_lex_node);
     728                                 free(current_chain_node);
     729                                 current_chain_node=first_chain_head;
     730                                 current_lex_node=current_chain_node->current_first_token;
     731                             }
     732                             //现在开始来处理名字
     733                             if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
     734                             {
     735                                 printf("unexpected %s as var_name
    ",current_lex_node->token_name);
     736                                 exit();
     737                             }
     738                             else//如果遇到的是名字,则需要考虑当前产生体中是否已经有重名的了
     739                             {
     740                                 if(return_list_head==NULL)//如果为空的话,就没必要去考虑了直接插入
     741                                 {
     742                                     temp_sub_pattern->faction_name=current_lex_node->token_name;
     743                                     return_list_head=return_list_rear=temp_sub_pattern;
     744                                 }
     745                                 else
     746                                 {
     747                                     if(!check_duplicated_name(return_list_head,current_lex_node->token_name))
     748                                     {
     749                                         temp_sub_pattern->faction_name=current_lex_node->token_name;
     750                                     }
     751                                     else
     752                                     {
     753                                         printf("duplicated name %s encounted 
    ",current_lex_node->token_name);
     754                                         exit();
     755                                     }
     756                                 }
     757                                 //名称检查已经完成,现在来处理数组
     758                                 temp_list_node=malloc(sizeof(struct _composition_list));
     759                                 temp_list_node->current=temp_sub_pattern;
     760                                 temp_list_node->next=NULL;
     761                                 if(return_list_head==NULL)
     762                                 {
     763                                     return_list_head=return_list_rear=temp_list_node;
     764                                 }
     765                                 else
     766                                 {
     767                                     return_list_rear->next=temp_list_node;
     768                                     return_list_rear=temp_list_node;
     769                                 }
     770                                 first_chain_head=first_chain_head->next;
     771                                 free(current_lex_node);
     772                                 free(current_chain_node);
     773                                 current_chain_node=first_chain_head;
     774                                 current_lex_node=current_chain_node->current_first_token;
     775                                 while(*(current_lex_node->token_name)=='[')//处理指针
     776                                 {
     777                                     first_chain_head=first_chain_head->next;
     778                                     free(current_lex_node->token_name);
     779                                     free(current_lex_node);
     780                                     free(current_chain_node);
     781                                     current_chain_node=first_chain_head;
     782                                     current_lex_node=current_chain_node->current_first_token;
     783                                     //处理数组索引
     784                                     if(current_lex_node->current_lex_type!=constant)
     785                                     {
     786                                         printf("need constant in array decl
    ");
     787                                         exit();
     788                                     }
     789                                     else
     790                                     {
     791                                         if(check_constant_type(current_lex_node->token_name))
     792                                         {
     793                                             printf("need integer in array decl
    ");
     794                                             exit();
     795                                         }
     796                                         else
     797                                         {
     798                                             temp_array_desc=malloc(sizeof(struct _array_description));
     799                                             temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
     800                                             temp_array_desc->next=temp_sub_pattern->array_layer;
     801                                             temp_sub_pattern->array_layer=temp_array_desc;
     802                                         }
     803                                     }
     804                                     first_chain_head=first_chain_head->next;
     805                                     free(current_lex_node->token_name);
     806                                     free(current_lex_node);
     807                                     free(current_chain_node);
     808                                     current_chain_node=first_chain_head;
     809                                     current_lex_node=current_chain_node->current_first_token;
     810                                     if(*(current_lex_node->token_name)!=']')
     811                                     {
     812                                         printf("need ] while encount %s 
    ",current_lex_node->token_name);
     813                                         exit();
     814                                     }
     815                                     else
     816                                     {
     817                                         first_chain_head=first_chain_head->next;
     818                                         free(current_lex_node->token_name);
     819                                         free(current_lex_node);
     820                                         free(current_chain_node);
     821                                         current_chain_node=first_chain_head;
     822                                         current_lex_node=current_chain_node->current_first_token;
     823                                     }
     824                                 }
     825                                 if(current_lex_node->current_lex_type==delimit)//吃掉分号
     826                                 {
     827                                     first_chain_head=first_chain_head->next;
     828                                     free(current_lex_node->token_name);
     829                                     free(current_lex_node);
     830                                     free(current_chain_node);
     831                                     current_chain_node=first_chain_head;
     832                                     current_lex_node=current_chain_node->current_first_token;
     833                                 }
     834                                 else
     835                                 {
     836                                     printf("delimit needed after decl
    ");
     837                                     exit();
     838                                 }
     839                             }
     840                         }
     841                     }
     842                     else//如果是大括号,则开始内部类型声明
     843                     {
     844                         new_type_desc=malloc(sizeof(struct _type_description));
     845                         new_type_desc->current_gen_list=tackle_type_define();
     846                         new_type_desc->type_type=2;
     847                         return_decl_link=malloc(sizeof(struct type_decl_link));
     848                         return_decl_link->current_type_node=new_type_desc;
     849                         return_decl_link->next=(*type_link_head);
     850                         (*type_link_head)=return_decl_link;
     851                         //下面是抛弃大括号
     852                         current_chain_node=first_chain_head;
     853                         current_lex_node=current_chain_node->current_first_token;
     854                         //现在来获得分量名字
     855                         if(current_lex_node->current_lex_type!=name)//类型检查
     856                         {
     857                             printf("need name as struct name while encounted %s
    ",current_lex_node->token_name);
     858                             exit();
     859                         }
     860                         else//检查重名
     861                         {
     862                             if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果有重名的
     863                             {
     864                                 printf("duplicated type name %s 
    ",current_lex_node->token_name);
     865                                 exit();
     866                             }
     867                             else
     868                             {
     869                                 if(check_duplicated_name(return_list_head,current_lex_node->token_name))
     870                                 {
     871                                     printf("duplicated var name %s
    ",current_lex_node->token_name);
     872                                     exit();
     873                                 }
     874                                 else//如果没有重名的
     875                                 {
     876                                     new_type_desc->name=current_lex_node->token_name;
     877                                     insert_avl_node(new_type_desc);
     878                                     temp_sub_pattern->faction_name=current_lex_node->token_name;
     879                                     temp_sub_pattern->pattern_name=current_lex_node->token_name;
     880                                     temp_list_node=malloc(sizeof(struct _composition_list));
     881                                     temp_list_node->current=temp_sub_pattern;
     882                                     temp_list_node->next=NULL;
     883                                     if(return_list_head==NULL)
     884                                     {
     885                                         return_list_head=return_list_rear=temp_list_head;
     886                                     }
     887                                     else
     888                                     {
     889                                         return_list_rear->next=temp_list_head;
     890                                         return_list_rear=temp_list_head;
     891                                     }
     892                                     //至此类型分量也已经插入完毕了
     893                                     first_chain_head=first_chain_head->next;
     894                                     free(current_lex_node);
     895                                     free(current_chain_node);
     896                                     current_chain_node=first_chain_head;
     897                                     current_lex_node=current_chain_node->current_first_token;
     898                                     //再次吃掉分号
     899                                     if(*(current_lex_node->token_name)!=';')
     900                                     {
     901                                         printf("missing ; after type define
    ");
     902                                         exit();
     903                                     }
     904                                     else
     905                                     {
     906                                         first_chain_head=first_chain_head->next;
     907                                         free(current_lex_node->token_name);
     908                                         free(current_lex_node);
     909                                         free(current_chain_node);
     910                                         current_chain_node=first_chain_head;
     911                                         current_lex_node=current_chain_node->current_first_token;
     912                                     }
     913                                 }
     914                             }
     915                         }
     916                     }
     917                     break;
     918                 default:
     919                     printf("unexpected token %s as type 
    ",current_lex_node->token_name);
     920                     exit();
     921                 }
     922             }
     923             break;
     924         default:
     925             printf("unexpected token %s at the begining of new_line
    ",current_lex_node->token_name);
     926             exit();
     927         }
     928     }
     929     //释放掉这个大括号
     930     first_chain_head=first_chain_head->next;
     931     free(current_lex_node->token_name);
     932     free(current_lex_node);
     933     free(current_chain_node);
     934     return return_list_head;
     935 }
     936 struct var_hash_node* tackle_var_decl(void )//这里处理的是变量声明,不是函数声明,而且我们不进行重名检查
     937     //等函数返回之后再去进行重名检查
     938 {
     939     struct first_token_chain* current_chain_node;
     940     struct first_token_chain* next_chain_node;
     941     struct first_lex_token* current_lex_node;
     942     struct first_lex_token* next_lex_node;
     943     struct _type_description* temp_type_desc;//代表查询avl得到的名称类型
     944     struct _var_hash_node* return_var_node;//代表返回的值
     945     struct _array_description temp_array_desc;//代表临时的数组描述符
     946     current_chain_node=first_chain_head;
     947     current_lex_node=current_chain_node->current_first_token;
     948     temp_type_desc=search_avl_tree(current_lex_node->token_name);
     949     if(temp_type_desc->type_type!=4)//如果开头不是关键字
     950     {
     951         printf("unexpected %s at the begining of declare statement 
    ",current_lex_node->token_name);
     952         exit();
     953     }
     954     else
     955     {
     956         switch(*(current_lex_node->token_name))
     957         {
     958         case 'i':
     959         case 'l':
     960         case 'f':
     961         case 'd':
     962         case 'c':
     963         case 'v'://差点忘了这个货
     964             return_var_node=malloc(sizeof(struct _var_hash_node));
     965             return_var_node->var_type=temp_type_desc;
     966             return_var_node->array_layer=NULL;
     967             return_var_node->pointer_layer=0;
     968             first_chain_head=first_chain_head->next;
     969             free(current_lex_node->token_name);
     970             free(current_lex_node);
     971             free(current_chain_node);
     972             current_chain_node=first_chain_head;
     973             current_lex_node=current_chain_node->current_first_token;
     974             while(*(current_lex_node->token_name)=='*')//处理指针
     975             {
     976                 return_var_node->pointer_layer=return_var_node->pointer_layer<<1+1;
     977                 first_chain_head=first_chain_head->next;
     978                 free(current_lex_node->token_name);
     979                 free(current_lex_node);
     980                 free(current_chain_node);
     981                 current_chain_node=first_chain_head;
     982                 current_lex_node=current_chain_node->current_first_token;
     983             }
     984             //现在开始来处理名字
     985             if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
     986             {
     987                 printf("unexpected %s as var_name
    ",current_lex_node->token_name);
     988                 exit();
     989             }
     990             else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
     991                 //返回之后再去考虑
     992             {
     993                 if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
     994                 {
     995                     printf("var name %s is duplicated with type name
    ",current_lex_node->token_name);
     996                     exit();
     997                 }
     998                 //名称检查已经完成,现在来处理数组
     999                 return_var_node->var_name=current_lex_node->token_name;
    1000                 first_chain_head=first_chain_head->next;
    1001                 free(current_lex_node);
    1002                 free(current_chain_node);
    1003                 current_chain_node=first_chain_head;
    1004                 current_lex_node=current_chain_node->current_first_token;
    1005                 while(*(current_lex_node->token_name)=='[')//处理指针
    1006                 {
    1007                     first_chain_head=first_chain_head->next;
    1008                     free(current_lex_node->token_name);
    1009                     free(current_lex_node);
    1010                     free(current_chain_node);
    1011                     current_chain_node=first_chain_head;
    1012                     current_lex_node=current_chain_node->current_first_token;
    1013                     //处理数组索引
    1014                     if(current_lex_node->current_lex_type!=constant)//检查是不是常量
    1015                     {
    1016                         printf("need constant in array decl
    ");
    1017                         exit();
    1018                     }
    1019                     else//检查是不是整数
    1020                     {
    1021                         if(check_constant_type(current_lex_node->token_name))
    1022                         {
    1023                             printf("need integer in array decl
    ");
    1024                             exit();
    1025                         }
    1026                         else
    1027                         {
    1028                             temp_array_desc=malloc(sizeof(struct _array_description));
    1029                             temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
    1030                             temp_array_desc->next=return_var_node->array_layer;
    1031                             return_var_node->array_layer=temp_array_desc;
    1032                         }
    1033                     }
    1034                     first_chain_head=first_chain_head->next;
    1035                     free(current_lex_node->token_name);
    1036                     free(current_lex_node);
    1037                     free(current_chain_node);
    1038                     current_chain_node=first_chain_head;
    1039                     current_lex_node=current_chain_node->current_first_token;
    1040                     if(*(current_lex_node->token_name)!=']')
    1041                     {
    1042                         printf("need ] while encount %s 
    ",current_lex_node->token_name);
    1043                         exit();
    1044                     }
    1045                     else
    1046                     {
    1047                         first_chain_head=first_chain_head->next;
    1048                         free(current_lex_node->token_name);
    1049                         free(current_lex_node);
    1050                         free(current_chain_node);
    1051                         current_chain_node=first_chain_head;
    1052                         current_lex_node=current_chain_node->current_first_token;
    1053                     }
    1054                 }
    1055                 if(current_lex_node->current_lex_type==delimit)//吃掉分号
    1056                 {
    1057                     first_chain_head=first_chain_head->next;
    1058                     free(current_lex_node->token_name);
    1059                     free(current_lex_node);
    1060                     free(current_chain_node);
    1061                     current_chain_node=first_chain_head;
    1062                     current_lex_node=current_chain_node->current_first_token;
    1063                 }
    1064                 else
    1065                 {
    1066                     printf("delimit needed after decl
    ");
    1067                     exit();
    1068                 }
    1069             }
    1070 
    1071 
    1072 
    1073 
    1074             break;
    1075         case 's'://结构体
    1076             first_chain_head=first_chain_head->next;
    1077             free(current_lex_node->token_name);
    1078             free(current_lex_node);
    1079             free(current_chain_node);
    1080             current_chain_node=first_chain_head;
    1081             current_lex_node=current_chain_node->current_first_token;
    1082             temp_type_desc=search_avl_tree(current_lex_node->token_name);
    1083             if(temp_type_desc==NULL)
    1084             {
    1085                 printf(" invalid type_name %s is encounted 
    ",current_lex_node->token_name);
    1086                 exit();
    1087             }
    1088             else
    1089             {
    1090                 if(temp_type_desc->type_type!=2)
    1091                 {
    1092                     printf("the typename %s is not struct 
    ",current_lex_node->token_name);
    1093                     exit();
    1094                 }
    1095             }
    1096             //至此参数检查完成
    1097             return_var_node=malloc(sizeof(struct _var_hash_node));
    1098             return_var_node->var_type=temp_type_desc;
    1099             return_var_node->array_layer=NULL;
    1100             return_var_node->pointer_layer=0;
    1101             first_chain_head=first_chain_head->next;
    1102             free(current_lex_node->token_name);
    1103             free(current_lex_node);
    1104             free(current_chain_node);
    1105             current_chain_node=first_chain_head;
    1106             current_lex_node=current_chain_node->current_first_token;
    1107             while(*(current_lex_node->token_name)=='*')//处理指针
    1108             {
    1109                 return_var_node->pointer_layer=return_var_node->pointer_layer<<1+1;
    1110                 first_chain_head=first_chain_head->next;
    1111                 free(current_lex_node->token_name);
    1112                 free(current_lex_node);
    1113                 free(current_chain_node);
    1114                 current_chain_node=first_chain_head;
    1115                 current_lex_node=current_chain_node->current_first_token;
    1116             }
    1117             //现在开始来处理名字
    1118             if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
    1119             {
    1120                 printf("unexpected %s as var_name
    ",current_lex_node->token_name);
    1121                 exit();
    1122             }
    1123             else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
    1124                 //返回之后再去考虑
    1125             {
    1126                 if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
    1127                 {
    1128                     printf("var name %s is duplicated with type name
    ",current_lex_node->token_name);
    1129                     exit();
    1130                 }
    1131                 //名称检查已经完成,现在来处理数组
    1132                 return_var_node->var_name=current_lex_node->token_name;
    1133                 first_chain_head=first_chain_head->next;
    1134                 free(current_lex_node);
    1135                 free(current_chain_node);
    1136                 current_chain_node=first_chain_head;
    1137                 current_lex_node=current_chain_node->current_first_token;
    1138                 while(*(current_lex_node->token_name)=='[')//处理指针
    1139                 {
    1140                     first_chain_head=first_chain_head->next;
    1141                     free(current_lex_node->token_name);
    1142                     free(current_lex_node);
    1143                     free(current_chain_node);
    1144                     current_chain_node=first_chain_head;
    1145                     current_lex_node=current_chain_node->current_first_token;
    1146                     //处理数组索引
    1147                     if(current_lex_node->current_lex_type!=constant)//检查是不是常量
    1148                     {
    1149                         printf("need constant in array decl
    ");
    1150                         exit();
    1151                     }
    1152                     else//检查是不是整数
    1153                     {
    1154                         if(check_constant_type(current_lex_node->token_name))
    1155                         {
    1156                             printf("need integer in array decl
    ");
    1157                             exit();
    1158                         }
    1159                         else
    1160                         {
    1161                             temp_array_desc=malloc(sizeof(struct _array_description));
    1162                             temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
    1163                             temp_array_desc->next=return_var_node->array_layer;
    1164                             return_var_node->array_layer=temp_array_desc;
    1165                         }
    1166                     }
    1167                     first_chain_head=first_chain_head->next;
    1168                     free(current_lex_node->token_name);
    1169                     free(current_lex_node);
    1170                     free(current_chain_node);
    1171                     current_chain_node=first_chain_head;
    1172                     current_lex_node=current_chain_node->current_first_token;
    1173                     if(*(current_lex_node->token_name)!=']')
    1174                     {
    1175                         printf("need ] while encount %s 
    ",current_lex_node->token_name);
    1176                         exit();
    1177                     }
    1178                     else
    1179                     {
    1180                         first_chain_head=first_chain_head->next;
    1181                         free(current_lex_node->token_name);
    1182                         free(current_lex_node);
    1183                         free(current_chain_node);
    1184                         current_chain_node=first_chain_head;
    1185                         current_lex_node=current_chain_node->current_first_token;
    1186                     }
    1187                 }
    1188                 if(current_lex_node->current_lex_type==delimit)//吃掉分号
    1189                 {
    1190                     first_chain_head=first_chain_head->next;
    1191                     free(current_lex_node->token_name);
    1192                     free(current_lex_node);
    1193                     free(current_chain_node);
    1194                     current_chain_node=first_chain_head;
    1195                     current_lex_node=current_chain_node->current_first_token;
    1196                 }
    1197                 else
    1198                 {
    1199                     printf("delimit needed after decl
    ");
    1200                     exit();
    1201                 }
    1202             }
    1203             break;
    1204         case 'u':
    1205             if(*(current_lex_node->token_name+2)=='s')//代表的是无符号数
    1206             {
    1207                 return_var_node=malloc(sizeof(struct _var_hash_node));
    1208                 return_var_node->var_type=temp_type_desc;
    1209                 return_var_node->array_layer=NULL;
    1210                 return_var_node->pointer_layer=0;
    1211                 first_chain_head=first_chain_head->next;
    1212                 free(current_lex_node->token_name);
    1213                 free(current_lex_node);
    1214                 free(current_chain_node);
    1215                 current_chain_node=first_chain_head;
    1216                 current_lex_node=current_chain_node->current_first_token;
    1217                 while(*(current_lex_node->token_name)=='*')//处理指针
    1218                 {
    1219                     return_var_node->pointer_layer=return_var_node->pointer_layer<<1+1;
    1220                     first_chain_head=first_chain_head->next;
    1221                     free(current_lex_node->token_name);
    1222                     free(current_lex_node);
    1223                     free(current_chain_node);
    1224                     current_chain_node=first_chain_head;
    1225                     current_lex_node=current_chain_node->current_first_token;
    1226                 }
    1227                 //现在开始来处理名字
    1228                 if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
    1229                 {
    1230                     printf("unexpected %s as var_name
    ",current_lex_node->token_name);
    1231                     exit();
    1232                 }
    1233                 else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
    1234                     //返回之后再去考虑
    1235                 {
    1236                     if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
    1237                     {
    1238                         printf("var name %s is duplicated with type name
    ",current_lex_node->token_name);
    1239                         exit();
    1240                     }
    1241                     //名称检查已经完成,现在来处理数组
    1242                     return_var_node->var_name=current_lex_node->token_name;
    1243                     first_chain_head=first_chain_head->next;
    1244                     free(current_lex_node);
    1245                     free(current_chain_node);
    1246                     current_chain_node=first_chain_head;
    1247                     current_lex_node=current_chain_node->current_first_token;
    1248                     while(*(current_lex_node->token_name)=='[')//处理指针
    1249                     {
    1250                         first_chain_head=first_chain_head->next;
    1251                         free(current_lex_node->token_name);
    1252                         free(current_lex_node);
    1253                         free(current_chain_node);
    1254                         current_chain_node=first_chain_head;
    1255                         current_lex_node=current_chain_node->current_first_token;
    1256                         //处理数组索引
    1257                         if(current_lex_node->current_lex_type!=constant)//检查是不是常量
    1258                         {
    1259                             printf("need constant in array decl
    ");
    1260                             exit();
    1261                         }
    1262                         else//检查是不是整数
    1263                         {
    1264                             if(check_constant_type(current_lex_node->token_name))
    1265                             {
    1266                                 printf("need integer in array decl
    ");
    1267                                 exit();
    1268                             }
    1269                             else
    1270                             {
    1271                                 temp_array_desc=malloc(sizeof(struct _array_description));
    1272                                 temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
    1273                                 temp_array_desc->next=return_var_node->array_layer;
    1274                                 return_var_node->array_layer=temp_array_desc;
    1275                             }
    1276                         }
    1277                         first_chain_head=first_chain_head->next;
    1278                         free(current_lex_node->token_name);
    1279                         free(current_lex_node);
    1280                         free(current_chain_node);
    1281                         current_chain_node=first_chain_head;
    1282                         current_lex_node=current_chain_node->current_first_token;
    1283                         if(*(current_lex_node->token_name)!=']')
    1284                         {
    1285                             printf("need ] while encount %s 
    ",current_lex_node->token_name);
    1286                             exit();
    1287                         }
    1288                         else
    1289                         {
    1290                             first_chain_head=first_chain_head->next;
    1291                             free(current_lex_node->token_name);
    1292                             free(current_lex_node);
    1293                             free(current_chain_node);
    1294                             current_chain_node=first_chain_head;
    1295                             current_lex_node=current_chain_node->current_first_token;
    1296                         }
    1297                     }
    1298                     if(current_lex_node->current_lex_type==delimit)//吃掉分号
    1299                     {
    1300                         first_chain_head=first_chain_head->next;
    1301                         free(current_lex_node->token_name);
    1302                         free(current_lex_node);
    1303                         free(current_chain_node);
    1304                         current_chain_node=first_chain_head;
    1305                         current_lex_node=current_chain_node->current_first_token;
    1306                     }
    1307                     else
    1308                     {
    1309                         printf("delimit needed after decl
    ");
    1310                         exit();
    1311                     }
    1312                 }
    1313             }
    1314             else
    1315             {
    1316                 first_chain_head=first_chain_head->next;
    1317                 free(current_lex_node->token_name);
    1318                 free(current_lex_node);
    1319                 free(current_chain_node);
    1320                 current_chain_node=first_chain_head;
    1321                 current_lex_node=current_chain_node->current_first_token;
    1322                 temp_type_desc=search_avl_tree(current_lex_node->token_name);
    1323                 if(temp_type_desc==NULL)
    1324                 {
    1325                     printf(" invalid type_name %s is encounted 
    ",current_lex_node->token_name);
    1326                     exit();
    1327                 }
    1328                 else
    1329                 {
    1330                     if(temp_type_desc->type_type!=3)
    1331                     {
    1332                         printf("the typename %s is not union 
    ",current_lex_node->token_name);
    1333                         exit();
    1334                     }
    1335                 }
    1336                 //至此参数检查完成
    1337                 return_var_node=malloc(sizeof(struct _var_hash_node));
    1338                 return_var_node->var_type=temp_type_desc;
    1339                 return_var_node->array_layer=NULL;
    1340                 return_var_node->pointer_layer=0;
    1341                 first_chain_head=first_chain_head->next;
    1342                 free(current_lex_node->token_name);
    1343                 free(current_lex_node);
    1344                 free(current_chain_node);
    1345                 current_chain_node=first_chain_head;
    1346                 current_lex_node=current_chain_node->current_first_token;
    1347                 while(*(current_lex_node->token_name)=='*')//处理指针
    1348                 {
    1349                     return_var_node->pointer_layer=return_var_node->pointer_layer<<1+1;
    1350                     first_chain_head=first_chain_head->next;
    1351                     free(current_lex_node->token_name);
    1352                     free(current_lex_node);
    1353                     free(current_chain_node);
    1354                     current_chain_node=first_chain_head;
    1355                     current_lex_node=current_chain_node->current_first_token;
    1356                 }
    1357                 //现在开始来处理名字
    1358                 if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
    1359                 {
    1360                     printf("unexpected %s as var_name
    ",current_lex_node->token_name);
    1361                     exit();
    1362                 }
    1363                 else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
    1364                     //返回之后再去考虑
    1365                 {
    1366                     if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
    1367                     {
    1368                         printf("var name %s is duplicated with type name
    ",current_lex_node->token_name);
    1369                         exit();
    1370                     }
    1371                     //名称检查已经完成,现在来处理数组
    1372                     return_var_node->var_name=current_lex_node->token_name;
    1373                     first_chain_head=first_chain_head->next;
    1374                     free(current_lex_node);
    1375                     free(current_chain_node);
    1376                     current_chain_node=first_chain_head;
    1377                     current_lex_node=current_chain_node->current_first_token;
    1378                     while(*(current_lex_node->token_name)=='[')//处理指针
    1379                     {
    1380                         first_chain_head=first_chain_head->next;
    1381                         free(current_lex_node->token_name);
    1382                         free(current_lex_node);
    1383                         free(current_chain_node);
    1384                         current_chain_node=first_chain_head;
    1385                         current_lex_node=current_chain_node->current_first_token;
    1386                         //处理数组索引
    1387                         if(current_lex_node->current_lex_type!=constant)//检查是不是常量
    1388                         {
    1389                             printf("need constant in array decl
    ");
    1390                             exit();
    1391                         }
    1392                         else//检查是不是整数
    1393                         {
    1394                             if(check_constant_type(current_lex_node->token_name))
    1395                             {
    1396                                 printf("need integer in array decl
    ");
    1397                                 exit();
    1398                             }
    1399                             else
    1400                             {
    1401                                 temp_array_desc=malloc(sizeof(struct _array_description));
    1402                                 temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
    1403                                 temp_array_desc->next=return_var_node->array_layer;
    1404                                 return_var_node->array_layer=temp_array_desc;
    1405                             }
    1406                         }
    1407                         first_chain_head=first_chain_head->next;
    1408                         free(current_lex_node->token_name);
    1409                         free(current_lex_node);
    1410                         free(current_chain_node);
    1411                         current_chain_node=first_chain_head;
    1412                         current_lex_node=current_chain_node->current_first_token;
    1413                         if(*(current_lex_node->token_name)!=']')
    1414                         {
    1415                             printf("need ] while encount %s 
    ",current_lex_node->token_name);
    1416                             exit();
    1417                         }
    1418                         else
    1419                         {
    1420                             first_chain_head=first_chain_head->next;
    1421                             free(current_lex_node->token_name);
    1422                             free(current_lex_node);
    1423                             free(current_chain_node);
    1424                             current_chain_node=first_chain_head;
    1425                             current_lex_node=current_chain_node->current_first_token;
    1426                         }
    1427                     }
    1428                     if(current_lex_node->current_lex_type==delimit)//吃掉分号
    1429                     {
    1430                         first_chain_head=first_chain_head->next;
    1431                         free(current_lex_node->token_name);
    1432                         free(current_lex_node);
    1433                         free(current_chain_node);
    1434                         current_chain_node=first_chain_head;
    1435                         current_lex_node=current_chain_node->current_first_token;
    1436                     }
    1437                     else
    1438                     {
    1439                         printf("delimit needed after decl
    ");
    1440                         exit();
    1441                     }
    1442                 }
    1443             }
    1444             break;
    1445         default:
    1446             printf("unexpected decl_head %s is encounted 
    ",current_lex_node->token_name);
    1447             exit();
    1448             break;
    1449         }
    1450     }
    1451     return return_var_node;
    1452 }
    1453 struct var_decl_link* tackle_func_arg(void)
    1454 {
    1455     struct _var_hash_node* return_var_node;//代表临时得到的单个参数
    1456     struct var_decl_link* return_list_head;
    1457     struct var_decl_link* return_list_rear;
    1458     struct var_decl_link* temp_list_node;//这个是为了建立链表用的 
    1459     struct first_token_chain* current_chain_node;
    1460     struct first_token_chain* next_chain_node;
    1461     struct first_lex_token* current_lex_node;
    1462     struct first_lex_token* next_lex_node;
    1463     struct _type_description* temp_type_desc;//代表查询avl得到的名称类型
    1464     struct _var_hash_node* return_var_node;//代表返回的值
    1465     struct _array_description temp_array_desc;//代表临时的数组描述符
    1466     return_list_head=return_list_rear=NULL;
    1467     current_chain_node=first_chain_head;
    1468     first_chain_head=first_chain_head->next;
    1469     current_lex_node=current_chain_node->current_first_token;
    1470     free(current_lex_node->token_name);
    1471     free(current_lex_node);
    1472     free(current_chain_node);
    1473     current_chain_node=first_chain_head;
    1474     current_lex_node=current_chain_node->current_first_token;
    1475     while(*(current_lex_node->token_name)!=')')
    1476     {
    1477         while(current_lex_node->current_lex_type==new_line)
    1478         {
    1479             first_chain_head=first_chain_head->next;
    1480             current_lex_node=current_chain_node->current_first_token;
    1481             free(current_lex_node->token_name);
    1482             free(current_lex_node);
    1483             free(current_chain_node);
    1484             current_chain_node=first_chain_head;
    1485             current_lex_node=current_chain_node->current_first_token;
    1486         }
    1487         switch(*(current_lex_node->token_name))
    1488         {
    1489         case 'i':
    1490         case 'l':
    1491         case 'f':
    1492         case 'd':
    1493         case 'c':
    1494         case 'v'://差点忘了这个货
    1495             return_var_node=malloc(sizeof(struct _var_hash_node));
    1496             return_var_node->var_type=temp_type_desc;
    1497             return_var_node->array_layer=NULL;
    1498             return_var_node->pointer_layer=0;
    1499             first_chain_head=first_chain_head->next;
    1500             free(current_lex_node->token_name);
    1501             free(current_lex_node);
    1502             free(current_chain_node);
    1503             current_chain_node=first_chain_head;
    1504             current_lex_node=current_chain_node->current_first_token;
    1505             while(*(current_lex_node->token_name)=='*')//处理指针
    1506             {
    1507                 return_var_node->pointer_layer=return_var_node->pointer_layer<<1+1;
    1508                 first_chain_head=first_chain_head->next;
    1509                 free(current_lex_node->token_name);
    1510                 free(current_lex_node);
    1511                 free(current_chain_node);
    1512                 current_chain_node=first_chain_head;
    1513                 current_lex_node=current_chain_node->current_first_token;
    1514             }
    1515             //现在开始来处理名字
    1516             if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
    1517             {
    1518                 printf("unexpected %s as var_name
    ",current_lex_node->token_name);
    1519                 exit();
    1520             }
    1521             else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
    1522                 //返回之后再去考虑
    1523             {
    1524                 if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
    1525                 {
    1526                     printf("var name %s is duplicated with type name
    ",current_lex_node->token_name);
    1527                     exit();
    1528                 }
    1529                 //名称检查已经完成,现在来处理数组
    1530                 return_var_node->var_name=current_lex_node->token_name;
    1531                 first_chain_head=first_chain_head->next;
    1532                 free(current_lex_node);
    1533                 free(current_chain_node);
    1534                 current_chain_node=first_chain_head;
    1535                 current_lex_node=current_chain_node->current_first_token;
    1536                 while(*(current_lex_node->token_name)=='[')//处理指针
    1537                 {
    1538                     first_chain_head=first_chain_head->next;
    1539                     free(current_lex_node->token_name);
    1540                     free(current_lex_node);
    1541                     free(current_chain_node);
    1542                     current_chain_node=first_chain_head;
    1543                     current_lex_node=current_chain_node->current_first_token;
    1544                     //处理数组索引
    1545                     if(current_lex_node->current_lex_type!=constant)//检查是不是常量
    1546                     {
    1547                         printf("need constant in array decl
    ");
    1548                         exit();
    1549                     }
    1550                     else//检查是不是整数
    1551                     {
    1552                         if(check_constant_type(current_lex_node->token_name))
    1553                         {
    1554                             printf("need integer in array decl
    ");
    1555                             exit();
    1556                         }
    1557                         else
    1558                         {
    1559                             temp_array_desc=malloc(sizeof(struct _array_description));
    1560                             temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
    1561                             temp_array_desc->next=return_var_node->array_layer;
    1562                             return_var_node->array_layer=temp_array_desc;
    1563                         }
    1564                     }
    1565                     first_chain_head=first_chain_head->next;
    1566                     free(current_lex_node->token_name);
    1567                     free(current_lex_node);
    1568                     free(current_chain_node);
    1569                     current_chain_node=first_chain_head;
    1570                     current_lex_node=current_chain_node->current_first_token;
    1571                     if(*(current_lex_node->token_name)!=']')
    1572                     {
    1573                         printf("need ] while encount %s 
    ",current_lex_node->token_name);
    1574                         exit();
    1575                     }
    1576                     else
    1577                     {
    1578                         first_chain_head=first_chain_head->next;
    1579                         free(current_lex_node->token_name);
    1580                         free(current_lex_node);
    1581                         free(current_chain_node);
    1582                         current_chain_node=first_chain_head;
    1583                         current_lex_node=current_chain_node->current_first_token;
    1584                     }
    1585                 }
    1586                 if(current_lex_node->current_lex_type==delimit)//吃掉分号
    1587                 {
    1588                     first_chain_head=first_chain_head->next;
    1589                     free(current_lex_node->token_name);
    1590                     free(current_lex_node);
    1591                     free(current_chain_node);
    1592                     current_chain_node=first_chain_head;
    1593                     current_lex_node=current_chain_node->current_first_token;
    1594                 }
    1595                 else
    1596                 {
    1597                     printf("delimit needed after decl
    ");
    1598                     exit();
    1599                 }
    1600             }
    1601             break;
    1602         case 's'://结构体
    1603             first_chain_head=first_chain_head->next;
    1604             free(current_lex_node->token_name);
    1605             free(current_lex_node);
    1606             free(current_chain_node);
    1607             current_chain_node=first_chain_head;
    1608             current_lex_node=current_chain_node->current_first_token;
    1609             temp_type_desc=search_avl_tree(current_lex_node->token_name);
    1610             if(temp_type_desc==NULL)
    1611             {
    1612                 printf(" invalid type_name %s is encounted 
    ",current_lex_node->token_name);
    1613                 exit();
    1614             }
    1615             else
    1616             {
    1617                 if(temp_type_desc->type_type!=2)
    1618                 {
    1619                     printf("the typename %s is not struct 
    ",current_lex_node->token_name);
    1620                     exit();
    1621                 }
    1622             }
    1623             //至此参数检查完成
    1624             return_var_node=malloc(sizeof(struct _var_hash_node));
    1625             return_var_node->var_type=temp_type_desc;
    1626             return_var_node->array_layer=NULL;
    1627             return_var_node->pointer_layer=0;
    1628             first_chain_head=first_chain_head->next;
    1629             free(current_lex_node->token_name);
    1630             free(current_lex_node);
    1631             free(current_chain_node);
    1632             current_chain_node=first_chain_head;
    1633             current_lex_node=current_chain_node->current_first_token;
    1634             while(*(current_lex_node->token_name)=='*')//处理指针
    1635             {
    1636                 return_var_node->pointer_layer=return_var_node->pointer_layer<<1+1;
    1637                 first_chain_head=first_chain_head->next;
    1638                 free(current_lex_node->token_name);
    1639                 free(current_lex_node);
    1640                 free(current_chain_node);
    1641                 current_chain_node=first_chain_head;
    1642                 current_lex_node=current_chain_node->current_first_token;
    1643             }
    1644             //现在开始来处理名字
    1645             if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
    1646             {
    1647                 printf("unexpected %s as var_name
    ",current_lex_node->token_name);
    1648                 exit();
    1649             }
    1650             else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
    1651                 //返回之后再去考虑
    1652             {
    1653                 if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
    1654                 {
    1655                     printf("var name %s is duplicated with type name
    ",current_lex_node->token_name);
    1656                     exit();
    1657                 }
    1658                 //名称检查已经完成,现在来处理数组
    1659                 return_var_node->var_name=current_lex_node->token_name;
    1660                 first_chain_head=first_chain_head->next;
    1661                 free(current_lex_node);
    1662                 free(current_chain_node);
    1663                 current_chain_node=first_chain_head;
    1664                 current_lex_node=current_chain_node->current_first_token;
    1665                 while(*(current_lex_node->token_name)=='[')//处理指针
    1666                 {
    1667                     first_chain_head=first_chain_head->next;
    1668                     free(current_lex_node->token_name);
    1669                     free(current_lex_node);
    1670                     free(current_chain_node);
    1671                     current_chain_node=first_chain_head;
    1672                     current_lex_node=current_chain_node->current_first_token;
    1673                     //处理数组索引
    1674                     if(current_lex_node->current_lex_type!=constant)//检查是不是常量
    1675                     {
    1676                         printf("need constant in array decl
    ");
    1677                         exit();
    1678                     }
    1679                     else//检查是不是整数
    1680                     {
    1681                         if(check_constant_type(current_lex_node->token_name))
    1682                         {
    1683                             printf("need integer in array decl
    ");
    1684                             exit();
    1685                         }
    1686                         else
    1687                         {
    1688                             temp_array_desc=malloc(sizeof(struct _array_description));
    1689                             temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
    1690                             temp_array_desc->next=return_var_node->array_layer;
    1691                             return_var_node->array_layer=temp_array_desc;
    1692                         }
    1693                     }
    1694                     first_chain_head=first_chain_head->next;
    1695                     free(current_lex_node->token_name);
    1696                     free(current_lex_node);
    1697                     free(current_chain_node);
    1698                     current_chain_node=first_chain_head;
    1699                     current_lex_node=current_chain_node->current_first_token;
    1700                     if(*(current_lex_node->token_name)!=']')
    1701                     {
    1702                         printf("need ] while encount %s 
    ",current_lex_node->token_name);
    1703                         exit();
    1704                     }
    1705                     else
    1706                     {
    1707                         first_chain_head=first_chain_head->next;
    1708                         free(current_lex_node->token_name);
    1709                         free(current_lex_node);
    1710                         free(current_chain_node);
    1711                         current_chain_node=first_chain_head;
    1712                         current_lex_node=current_chain_node->current_first_token;
    1713                     }
    1714                 }
    1715                 if(current_lex_node->current_lex_type==delimit)//吃掉分号
    1716                 {
    1717                     first_chain_head=first_chain_head->next;
    1718                     free(current_lex_node->token_name);
    1719                     free(current_lex_node);
    1720                     free(current_chain_node);
    1721                     current_chain_node=first_chain_head;
    1722                     current_lex_node=current_chain_node->current_first_token;
    1723                 }
    1724                 else
    1725                 {
    1726                     printf("delimit needed after decl
    ");
    1727                     exit();
    1728                 }
    1729             }
    1730             break;
    1731         case 'u':
    1732             if(*(current_lex_node->token_name+2)=='s')//代表的是无符号数
    1733             {
    1734                 return_var_node=malloc(sizeof(struct _var_hash_node));
    1735                 return_var_node->var_type=temp_type_desc;
    1736                 return_var_node->array_layer=NULL;
    1737                 return_var_node->pointer_layer=0;
    1738                 first_chain_head=first_chain_head->next;
    1739                 free(current_lex_node->token_name);
    1740                 free(current_lex_node);
    1741                 free(current_chain_node);
    1742                 current_chain_node=first_chain_head;
    1743                 current_lex_node=current_chain_node->current_first_token;
    1744                 while(*(current_lex_node->token_name)=='*')//处理指针
    1745                 {
    1746                     return_var_node->pointer_layer=return_var_node->pointer_layer<<1+1;
    1747                     first_chain_head=first_chain_head->next;
    1748                     free(current_lex_node->token_name);
    1749                     free(current_lex_node);
    1750                     free(current_chain_node);
    1751                     current_chain_node=first_chain_head;
    1752                     current_lex_node=current_chain_node->current_first_token;
    1753                 }
    1754                 //现在开始来处理名字
    1755                 if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
    1756                 {
    1757                     printf("unexpected %s as var_name
    ",current_lex_node->token_name);
    1758                     exit();
    1759                 }
    1760                 else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
    1761                     //返回之后再去考虑
    1762                 {
    1763                     if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
    1764                     {
    1765                         printf("var name %s is duplicated with type name
    ",current_lex_node->token_name);
    1766                         exit();
    1767                     }
    1768                     //名称检查已经完成,现在来处理数组
    1769                     return_var_node->var_name=current_lex_node->token_name;
    1770                     first_chain_head=first_chain_head->next;
    1771                     free(current_lex_node);
    1772                     free(current_chain_node);
    1773                     current_chain_node=first_chain_head;
    1774                     current_lex_node=current_chain_node->current_first_token;
    1775                     while(*(current_lex_node->token_name)=='[')//处理指针
    1776                     {
    1777                         first_chain_head=first_chain_head->next;
    1778                         free(current_lex_node->token_name);
    1779                         free(current_lex_node);
    1780                         free(current_chain_node);
    1781                         current_chain_node=first_chain_head;
    1782                         current_lex_node=current_chain_node->current_first_token;
    1783                         //处理数组索引
    1784                         if(current_lex_node->current_lex_type!=constant)//检查是不是常量
    1785                         {
    1786                             printf("need constant in array decl
    ");
    1787                             exit();
    1788                         }
    1789                         else//检查是不是整数
    1790                         {
    1791                             if(check_constant_type(current_lex_node->token_name))
    1792                             {
    1793                                 printf("need integer in array decl
    ");
    1794                                 exit();
    1795                             }
    1796                             else
    1797                             {
    1798                                 temp_array_desc=malloc(sizeof(struct _array_description));
    1799                                 temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
    1800                                 temp_array_desc->next=return_var_node->array_layer;
    1801                                 return_var_node->array_layer=temp_array_desc;
    1802                             }
    1803                         }
    1804                         first_chain_head=first_chain_head->next;
    1805                         free(current_lex_node->token_name);
    1806                         free(current_lex_node);
    1807                         free(current_chain_node);
    1808                         current_chain_node=first_chain_head;
    1809                         current_lex_node=current_chain_node->current_first_token;
    1810                         if(*(current_lex_node->token_name)!=']')
    1811                         {
    1812                             printf("need ] while encount %s 
    ",current_lex_node->token_name);
    1813                             exit();
    1814                         }
    1815                         else
    1816                         {
    1817                             first_chain_head=first_chain_head->next;
    1818                             free(current_lex_node->token_name);
    1819                             free(current_lex_node);
    1820                             free(current_chain_node);
    1821                             current_chain_node=first_chain_head;
    1822                             current_lex_node=current_chain_node->current_first_token;
    1823                         }
    1824                     }
    1825                     if(current_lex_node->current_lex_type==delimit)//吃掉分号
    1826                     {
    1827                         first_chain_head=first_chain_head->next;
    1828                         free(current_lex_node->token_name);
    1829                         free(current_lex_node);
    1830                         free(current_chain_node);
    1831                         current_chain_node=first_chain_head;
    1832                         current_lex_node=current_chain_node->current_first_token;
    1833                     }
    1834                     else
    1835                     {
    1836                         printf("delimit needed after decl
    ");
    1837                         exit();
    1838                     }
    1839                 }
    1840             }
    1841             else
    1842             {
    1843                 first_chain_head=first_chain_head->next;
    1844                 free(current_lex_node->token_name);
    1845                 free(current_lex_node);
    1846                 free(current_chain_node);
    1847                 current_chain_node=first_chain_head;
    1848                 current_lex_node=current_chain_node->current_first_token;
    1849                 temp_type_desc=search_avl_tree(current_lex_node->token_name);
    1850                 if(temp_type_desc==NULL)
    1851                 {
    1852                     printf(" invalid type_name %s is encounted 
    ",current_lex_node->token_name);
    1853                     exit();
    1854                 }
    1855                 else
    1856                 {
    1857                     if(temp_type_desc->type_type!=3)
    1858                     {
    1859                         printf("the typename %s is not union 
    ",current_lex_node->token_name);
    1860                         exit();
    1861                     }
    1862                 }
    1863                 //至此参数检查完成
    1864                 return_var_node=malloc(sizeof(struct _var_hash_node));
    1865                 return_var_node->var_type=temp_type_desc;
    1866                 return_var_node->array_layer=NULL;
    1867                 return_var_node->pointer_layer=0;
    1868                 first_chain_head=first_chain_head->next;
    1869                 free(current_lex_node->token_name);
    1870                 free(current_lex_node);
    1871                 free(current_chain_node);
    1872                 current_chain_node=first_chain_head;
    1873                 current_lex_node=current_chain_node->current_first_token;
    1874                 while(*(current_lex_node->token_name)=='*')//处理指针
    1875                 {
    1876                     return_var_node->pointer_layer=return_var_node->pointer_layer<<1+1;
    1877                     first_chain_head=first_chain_head->next;
    1878                     free(current_lex_node->token_name);
    1879                     free(current_lex_node);
    1880                     free(current_chain_node);
    1881                     current_chain_node=first_chain_head;
    1882                     current_lex_node=current_chain_node->current_first_token;
    1883                 }
    1884                 //现在开始来处理名字
    1885                 if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
    1886                 {
    1887                     printf("unexpected %s as var_name
    ",current_lex_node->token_name);
    1888                     exit();
    1889                 }
    1890                 else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
    1891                     //返回之后再去考虑
    1892                 {
    1893                     if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
    1894                     {
    1895                         printf("var name %s is duplicated with type name
    ",current_lex_node->token_name);
    1896                         exit();
    1897                     }
    1898                     //名称检查已经完成,现在来处理数组
    1899                     return_var_node->var_name=current_lex_node->token_name;
    1900                     first_chain_head=first_chain_head->next;
    1901                     free(current_lex_node);
    1902                     free(current_chain_node);
    1903                     current_chain_node=first_chain_head;
    1904                     current_lex_node=current_chain_node->current_first_token;
    1905                     while(*(current_lex_node->token_name)=='[')//处理指针
    1906                     {
    1907                         first_chain_head=first_chain_head->next;
    1908                         free(current_lex_node->token_name);
    1909                         free(current_lex_node);
    1910                         free(current_chain_node);
    1911                         current_chain_node=first_chain_head;
    1912                         current_lex_node=current_chain_node->current_first_token;
    1913                         //处理数组索引
    1914                         if(current_lex_node->current_lex_type!=constant)//检查是不是常量
    1915                         {
    1916                             printf("need constant in array decl
    ");
    1917                             exit();
    1918                         }
    1919                         else//检查是不是整数
    1920                         {
    1921                             if(check_constant_type(current_lex_node->token_name))
    1922                             {
    1923                                 printf("need integer in array decl
    ");
    1924                                 exit();
    1925                             }
    1926                             else
    1927                             {
    1928                                 temp_array_desc=malloc(sizeof(struct _array_description));
    1929                                 temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
    1930                                 temp_array_desc->next=return_var_node->array_layer;
    1931                                 return_var_node->array_layer=temp_array_desc;
    1932                             }
    1933                         }
    1934                         first_chain_head=first_chain_head->next;
    1935                         free(current_lex_node->token_name);
    1936                         free(current_lex_node);
    1937                         free(current_chain_node);
    1938                         current_chain_node=first_chain_head;
    1939                         current_lex_node=current_chain_node->current_first_token;
    1940                         if(*(current_lex_node->token_name)!=']')
    1941                         {
    1942                             printf("need ] while encount %s 
    ",current_lex_node->token_name);
    1943                             exit();
    1944                         }
    1945                         else
    1946                         {
    1947                             first_chain_head=first_chain_head->next;
    1948                             free(current_lex_node->token_name);
    1949                             free(current_lex_node);
    1950                             free(current_chain_node);
    1951                             current_chain_node=first_chain_head;
    1952                             current_lex_node=current_chain_node->current_first_token;
    1953                         }
    1954                     }
    1955                     if(current_lex_node->current_lex_type==delimit)//吃掉分号
    1956                     {
    1957                         first_chain_head=first_chain_head->next;
    1958                         free(current_lex_node->token_name);
    1959                         free(current_lex_node);
    1960                         free(current_chain_node);
    1961                         current_chain_node=first_chain_head;
    1962                         current_lex_node=current_chain_node->current_first_token;
    1963                     }
    1964                     else
    1965                     {
    1966                         printf("delimit needed after decl
    ");
    1967                         exit();
    1968                     }
    1969                 }
    1970             }
    1971             break;
    1972         default:
    1973             printf("unexpected decl_head %s is encounted 
    ",current_lex_node->token_name);
    1974             exit();
    1975             break;
    1976         }
    1977         temp_list_node=malloc(sizeof(struct var_decl_link));
    1978         temp_list_node->current_var_node=return_var_node;
    1979         temp_list_node->next=NULL;
    1980         if(return_list_head==NULL)
    1981         {
    1982             return_list_rear=return_list_head=temp_list_node;
    1983         }
    1984         else
    1985         {
    1986             return_list_rear->next=temp_list_node;
    1987             return_list_rear=temp_list_node;
    1988         }
    1989 
    1990     }
    1991     //吃掉括号
    1992     first_chain_head=first_chain_head->next;
    1993     current_lex_node=current_chain_node->current_first_token;
    1994     free(current_lex_node->token_name);
    1995     free(current_lex_node);
    1996     free(current_chain_node);
    1997     current_chain_node=first_chain_head;
    1998     current_lex_node=current_chain_node->current_first_token;
    1999     //吃掉换行
    2000     while(current_lex_node->current_lex_type==new_line)
    2001     {
    2002         first_chain_head=first_chain_head->next;
    2003         current_lex_node=current_chain_node->current_first_token;
    2004         free(current_lex_node->token_name);
    2005         free(current_lex_node);
    2006         free(current_chain_node);
    2007         current_chain_node=first_chain_head;
    2008         current_lex_node=current_chain_node->current_first_token;
    2009     }
    2010     return return_list_head;
    2011 }
    2012 
    2013 
    2014 
    2015 void tackle_type_name(struct type_decl_link** type_link_head)
    2016 //这里我们要求这个函数被调用的时候已经做过参数检查了
    2017 //这里我们对于类型的插入是从头端插入的
    2018 //这里我们会吃掉分号
    2019 {
    2020     //这个函数是用来处理结构体、联合体声明的
    2021     struct first_token_chain* current_chain_node;
    2022     struct first_token_chain* next_chain_node;
    2023     struct first_lex_token* current_lex_node;
    2024     struct first_lex_token* next_lex_node;
    2025     struct _composition_list temp_com_list;//作为当前类型的产生体链表
    2026     struct _type_description* result_type_desc;
    2027     struct type_decl_link* return_decl_link;
    2028     result_type_desc=malloc(sizeof(struct _type_description));
    2029     current_chain_node=first_chain_head;
    2030     current_lex_node=current_chain_node->current_first_token;
    2031     if(*(current_lex_node->token_name)=='s')
    2032     {
    2033         result_type_desc->type_type=2;
    2034     }
    2035     else
    2036     {
    2037         result_type_desc->type_type=3;
    2038     }
    2039     first_chain_head=first_chain_head->next;
    2040     free(current_lex_node->token_name);
    2041     free(current_lex_node);
    2042     free(current_chain_node);
    2043     current_chain_node=first_chain_head;
    2044     current_lex_node=current_chain_node->current_first_token;
    2045     if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果已经有重名的了,则报错
    2046     {
    2047         printf("duplicated type_name %s 
    ",current_lex_node->token_name);
    2048         exit();
    2049     }
    2050     else
    2051     {
    2052         result_type_desc->name=current_lex_node->token_name;
    2053         insert_avl_node(result_type_desc);
    2054         //这里要先插入到avl符号表中,用来自引用的
    2055         first_chain_head=first_chain_head->next;
    2056         free(current_lex_node);
    2057         free(current_chain_node);
    2058         current_chain_node=first_chain_head;
    2059         current_lex_node=current_chain_node->current_first_token;
    2060         //吃掉换行
    2061         while(current_lex_node->current_lex_type==new_line)
    2062         {
    2063             first_chain_head=first_chain_head->next;
    2064             free(current_lex_node->token_name);
    2065             free(current_lex_node);
    2066             free(current_chain_node);
    2067             current_chain_node=first_chain_head;
    2068             current_lex_node=current_chain_node->current_first_token;
    2069         }
    2070         if(*(current_lex_node->token_name)!='{')
    2071         {
    2072             printf("missing { in struct decl
    ");
    2073             exit();
    2074         }
    2075         result_type_desc->current_gen_list=tackle_type_define(type_link_head);
    2076         result_type_desc->function_zone=NULL;
    2077         //吃掉分号
    2078         current_chain_node=first_chain_head;
    2079         current_lex_node=current_chain_node->current_first_token;
    2080         if(*(current_lex_node->token_name)!=';')
    2081         {
    2082             printf(" missing ; after type decl
    ");
    2083             exit();
    2084         }
    2085         //扔掉分号
    2086         first_chain_head=first_chain_head->next;
    2087         free(current_lex_node->token_name);
    2088         free(current_lex_node);
    2089         free(current_chain_node);
    2090     }
    2091     return_decl_link=malloc(sizeof(struct type_decl_link));
    2092     return_decl_link->current_type_node=result_type_desc;
    2093     return_decl_link->next=(*type_link_head);
    2094     (*type_link_head)=return_decl_link;
    2095 }    
    2096 
    2097 
    2098 struct _handle_tree_node* tackle_action()
    2099 struct function_description* tackle_code_scope(void)
    2100 {
    2101     struct first_token_chain* current_chain_node;
    2102     struct first_token_chain* next_chain_node;
    2103     struct first_lex_token* current_lex_node;
    2104     struct first_lex_token* next_lex_node;
    2105     struct function_description* return_function;
    2106     struct var_decl_link* current_scope_var;//代表当前代码域的变量链表
    2107     struct type_decl_link* current_scope_type;//代表当前代码域的类型链表
    2108     //这两个链表我们都采取在头部插入的方法
    2109     struct _handle_tree_node* current_scope_action;//代表当前代码域的动作链表
    2110     struct var_decl_link* temp_var_link;
    2111     current_scope_var=current_scope_type=current_scope_action=NULL;
    2112     current_chain_node=first_chain_head;
    2113     current_lex_node=current_chain_node->current_first_token;
    2114     first_chain_head=first_chain_head->next;
    2115     free(current_lex_node->token_name);
    2116     free(current_lex_node->current_lex_type);
    2117     free(current_chain_node);
    2118     current_chain_node=first_chain_head;
    2119     current_lex_node=current_chain_node->current_first_token;
    2120     //现在开始处理类型声明
    2121     while((strcmp(current_lex_node->token_name,"union")==0)||(strcmp(current_lex_node->token_name,"struct")))
    2122     {
    2123         //这个while是用来处理复合结构的声明的
    2124         next_chain_node=current_chain_node->next;
    2125         if(next_chain_node==NULL)
    2126         {
    2127             printf("unexpected end of file
    ");
    2128         }
    2129         next_chain_node=next_chain_node->next;
    2130         if(next_chain_node->current_first_token->current_lex_type==new_line)//注意这里有一个换行符
    2131         {
    2132             tackle_type_name(&current_scope_type);
    2133             current_chain_node=first_chain_head;
    2134             current_lex_node=current_chain_node->current_first_token;
    2135             while(current_lex_node->current_lex_type==new_line)//吃掉之后的换行
    2136             {
    2137                 first_chain_head=first_chain_head->next;
    2138                 free(current_lex_node->token_name);
    2139                 free(current_lex_node->current_lex_type);
    2140                 free(current_chain_node);
    2141                 current_chain_node=first_chain_head;
    2142                 current_lex_node=current_chain_node->current_first_token;
    2143             }
    2144         }
    2145         else
    2146         {
    2147             break;//开始处理变量定义
    2148         }
    2149     }
    2150     //现在开始处理变量定义
    2151     while(search_avl_tree(current_lex_node->token_name)->type_type==4)
    2152     {
    2153         temp_var_link=malloc(sizeof(struct var_decl_link));
    2154         temp_var_link->current_var_node=tackle_var_decl();
    2155         temp_var_link->next=current_scope_var;
    2156         current_scope_var=temp_var_link;
    2157         //然后吃掉所有的换行
    2158         current_chain_node=first_chain_head;
    2159         current_lex_node=current_chain_node->current_first_token;
    2160         while(current_lex_node->current_lex_type==new_line)//吃掉之后的换行
    2161         {
    2162             first_chain_head=first_chain_head->next;
    2163             free(current_lex_node->token_name);
    2164             free(current_lex_node->current_lex_type);
    2165             free(current_chain_node);
    2166             current_chain_node=first_chain_head;
    2167             current_lex_node=current_chain_node->current_first_token;
    2168         }
    2169     }
    2170     //现在开始处理动作列表
    2171     current_scope_action=tackle_action();
    2172     return_function=malloc(sizeof(struct function_description));
    2173     return_function->current_action_link=current_scope_action;
    2174     return_function->current_type_link=current_scope_type;
    2175     return_function->current_var_link=current_scope_var;
    2176 }
    2177 
    2178     
    2179 
    2180 
    2181 
    2182     
    2183 
    2184             
    2185                 
    2186 
    2187 
    2188 
    2189 
    2190 
    2191     
  • 相关阅读:
    Mysql之正则匹配
    定时任务之elastic-job概述
    清晰讲解LSB、MSB和大小端模式及网络字节序
    极光消息推送多环境配置
    基于TSUNG对MQTT进行压力测试-测试结果
    基于TSUNG对MQTT进行压力测试-基础概念温习
    阻塞式/非阻塞式与同步/异步的区别
    干货!Jenkins下配置findbugs、pmd及checkstyle实现代码自动检测
    jar包
    java环境变量及Eclipse自动编译问题
  • 原文地址:https://www.cnblogs.com/huangfeidian/p/3211171.html
Copyright © 2020-2023  润新知