#include "token_define.h"
//这个文件是用来处理句型的,对于算术表达式都是放在前面的那个头文件中来处理
typedef struct _handle_tree_node//句型语法树的节点
{
union
{
int handle_type;//1代表基本句型,2代表代码块,
enum keywords_type keyword;//代表关键字的类型
}
union//产生体
{
phrase_tree_node* orginal_phrase_node;//代表单个基本句型
struct
{
struct _handle_tree_node* for_initiate;
struct _handle_tree_node* for_judge;
struct _handle_tree_node* for_modify;
struct _handle_tree_node* for_codeblock;
}for_code;//代表for句型
struct
{
struct _handle_tree_node* while_judge;
struct _handle_tree_node* while_codeblock;
}while_code;//代表while句型
struct
{
struct _handle_tree_node* branch_judge;
struct _handle_tree_node* if_code;
struct _handle_tree_node* else_code;
}branch_code;//代表if else句型
};
}handle_tree_node;
//上面的是动作执行代码,根据我们对文法的定义,还有类型定义部分,函数定义部分,变量声明部分
//其中函数定义部分里面比较复杂,会有递归,我们放到最后去考虑
//下面先谈最简单的变量声明部分,变量声明部分中最重要的是声明头部,因为声明头经常会被使用。
//注意这里的声明头部与c语言不一样的地方就是*是跟着声明头的,而不是跟着变量名
//对于声明头的结构,我们在符号表中已经定义过了
//这里我们再次定义一下
//对于变量的结构,在符号表中也已经定义过了
//typedef struct _var_hash_node
//{
// pbasic_type_pattern current;
// char* var_name;
// int pointer_layer;//代表指针的级数,我们限制为8级,即只占一个字节,其他三个字节以后再使用,先留着
// parray_description array_layer;//这里是数组描述,以链表串起多级数组
// ptype_avl_tree current_type;//代表分量的类型
//}var_hash_node,*pvar_hash_node;
//变量声明的产生式
//list_name: new_name|list_name[unsign_constant_int] 对应数组的情形
//data_type_basic_body: int |char|float|double|long|unsigned 基础类型体
//data_type_basic_pointer: data_type_basic *|data_type_basic_pointer * 基础类型的指针
//data_type_basic:data_type_basic_body|data_type_basic_pointer 基础类型(包括指针)
//data_type_struct:data_type_struct_body|data_type_struct_pointer 结构体类型声明头部
//data_type_union:data_type_union_body|data_type_union_pointer 联合类型声明头部
//data_type_head:data_type_basic|data_type_struct|data_type_union 所有的类型头部
//data_var_decl: data_type_head list_name; 所有的显示变量声明
//注意我们这里只允许一次生命一个变量名
//实际处理的时候我们并不是按照前面的产生体严格执行的,我们在遇到变量声明的时候,首先查看第一个name
//如果不是struct和union,则对这个名字进行查找类型avl,如果存在,则获得这个类型,然后再处理指针
//然后是变量名字,然后是数组描述,这些工作很简单,我们没有必要去放到lr文法分析中去,那样只会增加复杂度
//如果是struct和union,则获得下一个token,对这个token执行上述操作,都是一样的
//所以我们碰到变量声明的时候,直接处理然后把data_var_decl放到句型栈中,让文法分析来处理
//
//
//
//现在来处理类型定义,由于类型定义也是嵌套的,所以我们也需要使用一个栈来处理类型定义,同时为当前定义
//构建一个语法分析树来处理,但是类型定义除了嵌套定义外,还存在自引用定义,为此,我们每次处理符合类型的时候
//首先记录各个分量的变量名字和类型名字(这里要求各个分量的类型名都已经被处理了)。当得到所有的分量之后
//另外分配空间来构造产生体链表,并把类型名字通过查找avl来获得相应的avl节点,并把类型名修改为avl节点的指针
//注意这里还有自引用,因此我们在插入avl的时候,只是先把产生体链表构造为空,插入之后再去填充产生体链表。
//注意在处理类型声明的时候,我们还要为代码块维护一个类型链表,按照拓扑序来排列
//因此在每处理完一个复合类型之后就插入到当前的类型链表中
//我们对于每一个代码块都有一个类型链表,而每个函数又是一个代码块,因此会有嵌套的处理
//为此我们又需要维护一个栈,栈中存放的是当前的类型链表。
//我们为每一个函数代码块维护一个类型链表、一个变量链表、一个代码块语法树的头、一个参数链表
//每当调用一个函数的时候,我们就把类型链表、变量链表、参数链表挂载到符号表里面,然后再用后序遍历语法树
//来执行代码。
//在调用返回之后,按照插入序的逆序来删除所有插入到符号表里面的项
//至此一个代码块里面的类型链表已经构建完成了
//由于这里只是构建链表,所以所有的类型都是靠名称来索引的,而不是靠指针来索引
int code_scope;//这个用来表示当前的代码域
struct _composition_list* current_block_pattern_list_rear;//这个代表了当前代码块的类型链表的末尾
struct var_decl_link//这个是代码块的变量链表
{
struct _var_hash_node* current_var_node;
struct var_decl_link* next;
};
struct type_decl_link//这个是代码块的类型链表
{
struct _type_description_ current_type_node;
struct type_decl_link* next;
};
struct function_description
{
struct var_decl_link* current_var_link;
struct type_decl_link* current_type_link;
struct _handle_tree_node* current_action_link;//代表动作链表
}; int check_constant_type(char* in_constant)//1代表小数,0代表整数
{
while(*(in_constant)!='\0')
{
if(*(in_constant)=='.')
{
return ;
}
else
{
in_constant++;
}
}
return ;
}
int check_duplicated_name( struct _composition_list list_head,char* in_name)//检查当前的产生体链表是不是已经有重名了
{
while(list_head!=NULL)
{
if(strcmp(list_head->current->faction_name,in_name)==)
{
return ;
}
else
{
list_head=list_head->next;
}
}
return ;
}
struct _composition_list* tackle_type_define(struct type_decl_link** type_link_head)
//这个函数是用来处理内部定义的,在处理之前,头部信息已经被插入到了avl树中
//只不过生成体没有初始化,这样做的意义就是为了达到结构体自引用
//结构体自引用只会在代码最外层的符合类型定义时才使用,对于内部的复合类型,自引用是被禁止的。
//所以这个函数在碰到嵌套的符合类型定义的时候,会进行递归调用,
//如果未初始化,则将类型名称域与变量名称域指向同一个字符串
{
//这里还没有写完,以后再写,先写最简单的
struct first_token_chain* current_chain_node;
struct first_token_chain* next_chain_node;
struct first_lex_token* current_lex_node;
struct first_lex_token* next_lex_node;
struct type_decl_link* return_decl_link;//作为插入到目前代码块的类型链表的节点,从头部插入
struct _composition_list return_list_head;//代表返回链表的头部
struct _composition_list return_list_rear;//代表返回链表的尾部,作为插入用
struct _composition_list temp_list_node;//在遇到内部的符合类型声明的时候来使用的
struct _basic_type_pattern temp_sub_pattern;//代表遇到的分量节点
struct _type_description temp_type_desc;//代表查询avl得到的名称类型
struct _type_description new_type_desc;//代表在遇到一个复合类型声明的时候新建的类型描述
struct _array_description temp_array_desc;//代表临时的数组描述符
return_list_head=return_list_rear=NULL;
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_node=current_chain_node->current_first_token;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//这里我们都已经做好参数检查了,所以没有必要再去检查,第一个词法单元应该是大括号,而且我们在传参
//的时候已经检查过了
while(*(current_lex_node->token_name)!='}')//对于内部嵌套的大括号,会被递归处理掉,因此不用担心
{
switch(current_lex_node->current_lex_type)
{
case new_line:
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
break;
case name:
temp_type_desc=search_avl_tree(current_lex_node->token_name);
if(temp_type_desc->type_type!=)//如果开头不是关键字
{
printf("unexpected %s at the begining of declare statement \n",current_lex_node->token_name);
exit();
}
else
{
switch(*(current_lex_node->token_name))
{
case 'i'://对应的是整数
case 'c'://对应的是字符
case 'f'://对应的是单精度浮点
case 'l'://对应的是长整数
case 'd'://对应的是双精度浮点
temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
temp_sub_pattern->pointer_layer=;
temp_sub_pattern->array_layer=NULL;
temp_sub_pattern->pattern_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
temp_sub_pattern->pointer_layer=temp_sub_pattern->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑当前产生体中是否已经有重名的了
{
if(return_list_head==NULL)//如果为空的话,就没必要去考虑了直接插入
{
temp_sub_pattern->faction_name=current_lex_node->token_name;
return_list_head=return_list_rear=temp_sub_pattern;
}
else
{
if(!check_duplicated_name(return_list_head,current_lex_node->token_name))
{
temp_sub_pattern->faction_name=current_lex_node->token_name;
}
else
{
printf("duplicated name %s encounted \n",current_lex_node->token_name);
exit();
}
}
//名称检查已经完成,现在来处理数组
temp_list_node=malloc(sizeof(struct _composition_list));
temp_list_node->current=temp_sub_pattern;
temp_list_node->next=NULL;
if(return_list_head==NULL)
{
return_list_head=return_list_rear=temp_list_node;
}
else
{
return_list_rear->next=temp_list_node;
return_list_rear=temp_list_node;
}
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)
{
printf("need constant in array decl\n");
exit();
}
else
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=temp_sub_pattern->array_layer;
temp_sub_pattern->array_layer=temp_array_desc;
} }
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
break;
case 'u'://这里有两种情况,一个是联合,一个是无符号整形,我们需要看到第三个字母才能判断
if(*(current_lex_node->token_name+)=='i')//对应的是联合体
{
temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
temp_sub_pattern->parray_description=NULL;
temp_sub_pattern->pointer_layer=;
temp_sub_pattern->pattern_name=NULL;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!='{')//如果后面接的不是大括号,则开始进行联合类型检查
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//这里需要检查一下这个结构体是否已经声明了
temp_type_desc=search_avl_tree(current_lex_node->token_name);
if(temp_type_desc==NULL)
{
printf("invalid union name %s encounted\n",current_lex_node->token_name);
exit();
}
else
{
//确定类型
temp_sub_pattern->pattern_name=current_lex_node->token_name;
while(*(current_lex_node->token_name)=='*')//处理指针
{
temp_sub_pattern->pointer_layer=temp_sub_pattern->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑当前产生体中是否已经有重名的了
{
if(return_list_head==NULL)//如果为空的话,就没必要去考虑了直接插入
{
temp_sub_pattern->faction_name=current_lex_node->token_name;
return_list_head=return_list_rear=temp_sub_pattern;
}
else
{
if(!check_duplicated_name(return_list_head,current_lex_node->token_name))
{
temp_sub_pattern->faction_name=current_lex_node->token_name;
}
else
{
printf("duplicated name %s encounted \n",current_lex_node->token_name);
exit();
}
}
//名称检查已经完成,现在来处理数组
temp_list_node=malloc(sizeof(struct _composition_list));
temp_list_node->current=temp_sub_pattern;
temp_list_node->next=NULL;
if(return_list_head==NULL)
{
return_list_head=return_list_rear=temp_list_node;
}
else
{
return_list_rear->next=temp_list_node;
return_list_rear=temp_list_node;
}
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)
{
printf("need constant in array decl\n");
exit();
}
else
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=temp_sub_pattern->array_layer;
temp_sub_pattern->array_layer=temp_array_desc;
} }
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
} }
else//这里是联合类型声明
{
new_type_desc=malloc(sizeof(struct _type_description));
new_type_desc->current_gen_list=tackle_type_define(type_link_head);
new_type_desc->type_type=;
return_decl_link=malloc(sizeof(struct type_decl_link));
return_decl_link->current_type_node=new_type_desc;
return_decl_link->next=(*type_link_head);
(*type_link_head)=return_decl_link;
//下面是抛弃大括号
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//现在来获得分量名字
if(current_lex_node->current_lex_type!=name)//类型检查
{
printf("need name as union name while encounted %s\n",current_lex_node->token_name);
exit();
}
else//检查重名
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果有重名的
{
printf("duplicated type name %s \n",current_lex_node->token_name);
exit();
}
else
{
if(check_duplicated_name(return_list_head,current_lex_node->token_name))
{
printf("duplicated var name %s\n",current_lex_node->token_name);
exit();
}
else//如果没有重名的
{
new_type_desc->name=current_lex_node->token_name;
insert_avl_node(new_type_desc);
//这里将这个类型插入到avl符号表中
//然后还要插入分量列表中
temp_sub_pattern->faction_name=current_lex_node->token_name;
temp_sub_pattern->pattern_name=current_lex_node->token_name;
temp_list_node=malloc(sizeof(struct _composition_list));
temp_list_node->current=temp_sub_pattern;
temp_list_node->next=NULL;
if(return_list_head==NULL)
{
return_list_head=return_list_rear=temp_list_head;
}
else
{
return_list_rear->next=temp_list_head;
return_list_rear=temp_list_head;
}
//至此类型分量也已经插入完毕了
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//再次吃掉分号
if(*(current_lex_node->token_name)!=';')
{
printf("missing ; after type define\n");
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//然后再吃掉所有的换行符
}
}
}
}
}
}
else//对应的是无符号整数
{
temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
temp_sub_pattern->pointer_layer=;
temp_sub_pattern->array_layer=NULL;
temp_sub_pattern->pattern_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
temp_sub_pattern->pointer_layer=temp_sub_pattern->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑当前产生体中是否已经有重名的了
{
if(return_list_head==NULL)//如果为空的话,就没必要去考虑了直接插入
{
temp_sub_pattern->faction_name=current_lex_node->token_name;
return_list_head=return_list_rear=temp_sub_pattern;
}
else
{
if(!check_duplicated_name(return_list_head,current_lex_node->token_name))
{
temp_sub_pattern->faction_name=current_lex_node->token_name;
}
else
{
printf("duplicated name %s encounted \n",current_lex_node->token_name);
exit();
}
}
//名称检查已经完成
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理数组
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)//判断是不是常数
{
printf("need constant in array decl\n");
exit();
}
else
{
if(check_constant_type(current_lex_node->token_name))//判断是不是整数
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=temp_sub_pattern->array_layer;
temp_sub_pattern->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
temp_list_node=malloc(sizeof(struct _composition_list));
temp_list_node->current=temp_sub_pattern;
temp_list_node->next=NULL;
//进行分量的插入
if(return_list_head==NULL)
{
return_list_head=return_list_rear=temp_list_node;
}
else
{
return_list_rear->next=temp_list_node;
return_list_rear=temp_list_node;
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
}
break;
case 's'://对应的是结构体
temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
temp_sub_pattern->parray_description=NULL;
temp_sub_pattern->pointer_layer=;
temp_sub_pattern->pattern_name=NULL;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!='{')//如果后面接的不是大括号,开始变量声明
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//这里需要检查一下这个结构体是否已经声明了
temp_type_desc=search_avl_tree(current_lex_node->token_name);
if(temp_type_desc==NULL)//检查结构体的名字是否有效
{
printf("unknown struct name %s encounted \n",current_lex_node->token_name);
exit();
}
else//结构体类型有效
{
temp_sub_pattern=malloc(sizeof(struct _basic_type_pattern));
temp_sub_pattern->pointer_layer=;
temp_sub_pattern->array_layer=NULL;
temp_sub_pattern->pattern_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
temp_sub_pattern->pointer_layer=temp_sub_pattern->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑当前产生体中是否已经有重名的了
{
if(return_list_head==NULL)//如果为空的话,就没必要去考虑了直接插入
{
temp_sub_pattern->faction_name=current_lex_node->token_name;
return_list_head=return_list_rear=temp_sub_pattern;
}
else
{
if(!check_duplicated_name(return_list_head,current_lex_node->token_name))
{
temp_sub_pattern->faction_name=current_lex_node->token_name;
}
else
{
printf("duplicated name %s encounted \n",current_lex_node->token_name);
exit();
}
}
//名称检查已经完成,现在来处理数组
temp_list_node=malloc(sizeof(struct _composition_list));
temp_list_node->current=temp_sub_pattern;
temp_list_node->next=NULL;
if(return_list_head==NULL)
{
return_list_head=return_list_rear=temp_list_node;
}
else
{
return_list_rear->next=temp_list_node;
return_list_rear=temp_list_node;
}
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)
{
printf("need constant in array decl\n");
exit();
}
else
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=temp_sub_pattern->array_layer;
temp_sub_pattern->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
}
}
else//如果是大括号,则开始内部类型声明
{
new_type_desc=malloc(sizeof(struct _type_description));
new_type_desc->current_gen_list=tackle_type_define();
new_type_desc->type_type=;
return_decl_link=malloc(sizeof(struct type_decl_link));
return_decl_link->current_type_node=new_type_desc;
return_decl_link->next=(*type_link_head);
(*type_link_head)=return_decl_link;
//下面是抛弃大括号
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//现在来获得分量名字
if(current_lex_node->current_lex_type!=name)//类型检查
{
printf("need name as struct name while encounted %s\n",current_lex_node->token_name);
exit();
}
else//检查重名
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果有重名的
{
printf("duplicated type name %s \n",current_lex_node->token_name);
exit();
}
else
{
if(check_duplicated_name(return_list_head,current_lex_node->token_name))
{
printf("duplicated var name %s\n",current_lex_node->token_name);
exit();
}
else//如果没有重名的
{
new_type_desc->name=current_lex_node->token_name;
insert_avl_node(new_type_desc);
temp_sub_pattern->faction_name=current_lex_node->token_name;
temp_sub_pattern->pattern_name=current_lex_node->token_name;
temp_list_node=malloc(sizeof(struct _composition_list));
temp_list_node->current=temp_sub_pattern;
temp_list_node->next=NULL;
if(return_list_head==NULL)
{
return_list_head=return_list_rear=temp_list_head;
}
else
{
return_list_rear->next=temp_list_head;
return_list_rear=temp_list_head;
}
//至此类型分量也已经插入完毕了
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//再次吃掉分号
if(*(current_lex_node->token_name)!=';')
{
printf("missing ; after type define\n");
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
}
}
}
break;
default:
printf("unexpected token %s as type \n",current_lex_node->token_name);
exit();
}
}
break;
default:
printf("unexpected token %s at the begining of new_line\n",current_lex_node->token_name);
exit();
}
}
//释放掉这个大括号
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
return return_list_head;
}
struct var_hash_node* tackle_var_decl(void )//这里处理的是变量声明,不是函数声明,而且我们不进行重名检查
//等函数返回之后再去进行重名检查
{
struct first_token_chain* current_chain_node;
struct first_token_chain* next_chain_node;
struct first_lex_token* current_lex_node;
struct first_lex_token* next_lex_node;
struct _type_description* temp_type_desc;//代表查询avl得到的名称类型
struct _var_hash_node* return_var_node;//代表返回的值
struct _array_description temp_array_desc;//代表临时的数组描述符
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
temp_type_desc=search_avl_tree(current_lex_node->token_name);
if(temp_type_desc->type_type!=)//如果开头不是关键字
{
printf("unexpected %s at the begining of declare statement \n",current_lex_node->token_name);
exit();
}
else
{
switch(*(current_lex_node->token_name))
{
case 'i':
case 'l':
case 'f':
case 'd':
case 'c':
case 'v'://差点忘了这个货
return_var_node=malloc(sizeof(struct _var_hash_node));
return_var_node->var_type=temp_type_desc;
return_var_node->array_layer=NULL;
return_var_node->pointer_layer=;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
return_var_node->pointer_layer=return_var_node->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
//返回之后再去考虑
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
{
printf("var name %s is duplicated with type name\n",current_lex_node->token_name);
exit();
}
//名称检查已经完成,现在来处理数组
return_var_node->var_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)//检查是不是常量
{
printf("need constant in array decl\n");
exit();
}
else//检查是不是整数
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=return_var_node->array_layer;
return_var_node->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
} break;
case 's'://结构体
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
temp_type_desc=search_avl_tree(current_lex_node->token_name);
if(temp_type_desc==NULL)
{
printf(" invalid type_name %s is encounted \n",current_lex_node->token_name);
exit();
}
else
{
if(temp_type_desc->type_type!=)
{
printf("the typename %s is not struct \n",current_lex_node->token_name);
exit();
}
}
//至此参数检查完成
return_var_node=malloc(sizeof(struct _var_hash_node));
return_var_node->var_type=temp_type_desc;
return_var_node->array_layer=NULL;
return_var_node->pointer_layer=;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
return_var_node->pointer_layer=return_var_node->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
//返回之后再去考虑
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
{
printf("var name %s is duplicated with type name\n",current_lex_node->token_name);
exit();
}
//名称检查已经完成,现在来处理数组
return_var_node->var_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)//检查是不是常量
{
printf("need constant in array decl\n");
exit();
}
else//检查是不是整数
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=return_var_node->array_layer;
return_var_node->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
break;
case 'u':
if(*(current_lex_node->token_name+)=='s')//代表的是无符号数
{
return_var_node=malloc(sizeof(struct _var_hash_node));
return_var_node->var_type=temp_type_desc;
return_var_node->array_layer=NULL;
return_var_node->pointer_layer=;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
return_var_node->pointer_layer=return_var_node->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
//返回之后再去考虑
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
{
printf("var name %s is duplicated with type name\n",current_lex_node->token_name);
exit();
}
//名称检查已经完成,现在来处理数组
return_var_node->var_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)//检查是不是常量
{
printf("need constant in array decl\n");
exit();
}
else//检查是不是整数
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=return_var_node->array_layer;
return_var_node->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
temp_type_desc=search_avl_tree(current_lex_node->token_name);
if(temp_type_desc==NULL)
{
printf(" invalid type_name %s is encounted \n",current_lex_node->token_name);
exit();
}
else
{
if(temp_type_desc->type_type!=)
{
printf("the typename %s is not union \n",current_lex_node->token_name);
exit();
}
}
//至此参数检查完成
return_var_node=malloc(sizeof(struct _var_hash_node));
return_var_node->var_type=temp_type_desc;
return_var_node->array_layer=NULL;
return_var_node->pointer_layer=;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
return_var_node->pointer_layer=return_var_node->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
//返回之后再去考虑
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
{
printf("var name %s is duplicated with type name\n",current_lex_node->token_name);
exit();
}
//名称检查已经完成,现在来处理数组
return_var_node->var_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)//检查是不是常量
{
printf("need constant in array decl\n");
exit();
}
else//检查是不是整数
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=return_var_node->array_layer;
return_var_node->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
}
break;
default:
printf("unexpected decl_head %s is encounted \n",current_lex_node->token_name);
exit();
break;
}
}
return return_var_node;
}
struct var_decl_link* tackle_func_arg(void)
{
struct _var_hash_node* return_var_node;//代表临时得到的单个参数
struct var_decl_link* return_list_head;
struct var_decl_link* return_list_rear;
struct var_decl_link* temp_list_node;//这个是为了建立链表用的
struct first_token_chain* current_chain_node;
struct first_token_chain* next_chain_node;
struct first_lex_token* current_lex_node;
struct first_lex_token* next_lex_node;
struct _type_description* temp_type_desc;//代表查询avl得到的名称类型
struct _var_hash_node* return_var_node;//代表返回的值
struct _array_description temp_array_desc;//代表临时的数组描述符
return_list_head=return_list_rear=NULL;
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_node=current_chain_node->current_first_token;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)!=')')
{
while(current_lex_node->current_lex_type==new_line)
{
first_chain_head=first_chain_head->next;
current_lex_node=current_chain_node->current_first_token;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
switch(*(current_lex_node->token_name))
{
case 'i':
case 'l':
case 'f':
case 'd':
case 'c':
case 'v'://差点忘了这个货
return_var_node=malloc(sizeof(struct _var_hash_node));
return_var_node->var_type=temp_type_desc;
return_var_node->array_layer=NULL;
return_var_node->pointer_layer=;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
return_var_node->pointer_layer=return_var_node->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
//返回之后再去考虑
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
{
printf("var name %s is duplicated with type name\n",current_lex_node->token_name);
exit();
}
//名称检查已经完成,现在来处理数组
return_var_node->var_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)//检查是不是常量
{
printf("need constant in array decl\n");
exit();
}
else//检查是不是整数
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=return_var_node->array_layer;
return_var_node->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
break;
case 's'://结构体
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
temp_type_desc=search_avl_tree(current_lex_node->token_name);
if(temp_type_desc==NULL)
{
printf(" invalid type_name %s is encounted \n",current_lex_node->token_name);
exit();
}
else
{
if(temp_type_desc->type_type!=)
{
printf("the typename %s is not struct \n",current_lex_node->token_name);
exit();
}
}
//至此参数检查完成
return_var_node=malloc(sizeof(struct _var_hash_node));
return_var_node->var_type=temp_type_desc;
return_var_node->array_layer=NULL;
return_var_node->pointer_layer=;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
return_var_node->pointer_layer=return_var_node->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
//返回之后再去考虑
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
{
printf("var name %s is duplicated with type name\n",current_lex_node->token_name);
exit();
}
//名称检查已经完成,现在来处理数组
return_var_node->var_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)//检查是不是常量
{
printf("need constant in array decl\n");
exit();
}
else//检查是不是整数
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=return_var_node->array_layer;
return_var_node->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
break;
case 'u':
if(*(current_lex_node->token_name+)=='s')//代表的是无符号数
{
return_var_node=malloc(sizeof(struct _var_hash_node));
return_var_node->var_type=temp_type_desc;
return_var_node->array_layer=NULL;
return_var_node->pointer_layer=;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
return_var_node->pointer_layer=return_var_node->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
//返回之后再去考虑
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
{
printf("var name %s is duplicated with type name\n",current_lex_node->token_name);
exit();
}
//名称检查已经完成,现在来处理数组
return_var_node->var_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)//检查是不是常量
{
printf("need constant in array decl\n");
exit();
}
else//检查是不是整数
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=return_var_node->array_layer;
return_var_node->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
temp_type_desc=search_avl_tree(current_lex_node->token_name);
if(temp_type_desc==NULL)
{
printf(" invalid type_name %s is encounted \n",current_lex_node->token_name);
exit();
}
else
{
if(temp_type_desc->type_type!=)
{
printf("the typename %s is not union \n",current_lex_node->token_name);
exit();
}
}
//至此参数检查完成
return_var_node=malloc(sizeof(struct _var_hash_node));
return_var_node->var_type=temp_type_desc;
return_var_node->array_layer=NULL;
return_var_node->pointer_layer=;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='*')//处理指针
{
return_var_node->pointer_layer=return_var_node->pointer_layer<<+;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
//现在开始来处理名字
if(current_lex_node->current_lex_type!=name)//如果遇到的不是名字
{
printf("unexpected %s as var_name\n",current_lex_node->token_name);
exit();
}
else//如果遇到的是名字,则需要考虑是否与类型重名了,这里我们就不去考虑变量重名了
//返回之后再去考虑
{
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果与类型重名了
{
printf("var name %s is duplicated with type name\n",current_lex_node->token_name);
exit();
}
//名称检查已经完成,现在来处理数组
return_var_node->var_name=current_lex_node->token_name;
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(*(current_lex_node->token_name)=='[')//处理指针
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//处理数组索引
if(current_lex_node->current_lex_type!=constant)//检查是不是常量
{
printf("need constant in array decl\n");
exit();
}
else//检查是不是整数
{
if(check_constant_type(current_lex_node->token_name))
{
printf("need integer in array decl\n");
exit();
}
else
{
temp_array_desc=malloc(sizeof(struct _array_description));
temp_array_desc->array_size=atoi(currrent_lex_node->token_name);
temp_array_desc->next=return_var_node->array_layer;
return_var_node->array_layer=temp_array_desc;
}
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=']')
{
printf("need ] while encount %s \n",current_lex_node->token_name);
exit();
}
else
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
if(current_lex_node->current_lex_type==delimit)//吃掉分号
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
else
{
printf("delimit needed after decl\n");
exit();
}
}
}
break;
default:
printf("unexpected decl_head %s is encounted \n",current_lex_node->token_name);
exit();
break;
}
temp_list_node=malloc(sizeof(struct var_decl_link));
temp_list_node->current_var_node=return_var_node;
temp_list_node->next=NULL;
if(return_list_head==NULL)
{
return_list_rear=return_list_head=temp_list_node;
}
else
{
return_list_rear->next=temp_list_node;
return_list_rear=temp_list_node;
} }
//吃掉括号
first_chain_head=first_chain_head->next;
current_lex_node=current_chain_node->current_first_token;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//吃掉换行
while(current_lex_node->current_lex_type==new_line)
{
first_chain_head=first_chain_head->next;
current_lex_node=current_chain_node->current_first_token;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
return return_list_head;
} void tackle_type_name(struct type_decl_link** type_link_head)
//这里我们要求这个函数被调用的时候已经做过参数检查了
//这里我们对于类型的插入是从头端插入的
//这里我们会吃掉分号
{
//这个函数是用来处理结构体、联合体声明的
struct first_token_chain* current_chain_node;
struct first_token_chain* next_chain_node;
struct first_lex_token* current_lex_node;
struct first_lex_token* next_lex_node;
struct _composition_list temp_com_list;//作为当前类型的产生体链表
struct _type_description* result_type_desc;
struct type_decl_link* return_decl_link;
result_type_desc=malloc(sizeof(struct _type_description));
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)=='s')
{
result_type_desc->type_type=;
}
else
{
result_type_desc->type_type=;
}
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(search_avl_tree(current_lex_node->token_name)!=NULL)//如果已经有重名的了,则报错
{
printf("duplicated type_name %s \n",current_lex_node->token_name);
exit();
}
else
{
result_type_desc->name=current_lex_node->token_name;
insert_avl_node(result_type_desc);
//这里要先插入到avl符号表中,用来自引用的
first_chain_head=first_chain_head->next;
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//吃掉换行
while(current_lex_node->current_lex_type==new_line)
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
if(*(current_lex_node->token_name)!='{')
{
printf("missing { in struct decl\n");
exit();
}
result_type_desc->current_gen_list=tackle_type_define(type_link_head);
result_type_desc->function_zone=NULL;
//吃掉分号
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
if(*(current_lex_node->token_name)!=';')
{
printf(" missing ; after type decl\n");
exit();
}
//扔掉分号
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
}
return_decl_link=malloc(sizeof(struct type_decl_link));
return_decl_link->current_type_node=result_type_desc;
return_decl_link->next=(*type_link_head);
(*type_link_head)=return_decl_link;
} struct _handle_tree_node* tackle_action()
struct function_description* tackle_code_scope(void)
{
struct first_token_chain* current_chain_node;
struct first_token_chain* next_chain_node;
struct first_lex_token* current_lex_node;
struct first_lex_token* next_lex_node;
struct function_description* return_function;
struct var_decl_link* current_scope_var;//代表当前代码域的变量链表
struct type_decl_link* current_scope_type;//代表当前代码域的类型链表
//这两个链表我们都采取在头部插入的方法
struct _handle_tree_node* current_scope_action;//代表当前代码域的动作链表
struct var_decl_link* temp_var_link;
current_scope_var=current_scope_type=current_scope_action=NULL;
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node->current_lex_type);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
//现在开始处理类型声明
while((strcmp(current_lex_node->token_name,"union")==)||(strcmp(current_lex_node->token_name,"struct")))
{
//这个while是用来处理复合结构的声明的
next_chain_node=current_chain_node->next;
if(next_chain_node==NULL)
{
printf("unexpected end of file\n");
}
next_chain_node=next_chain_node->next;
if(next_chain_node->current_first_token->current_lex_type==new_line)//注意这里有一个换行符
{
tackle_type_name(¤t_scope_type);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(current_lex_node->current_lex_type==new_line)//吃掉之后的换行
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node->current_lex_type);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
else
{
break;//开始处理变量定义
}
}
//现在开始处理变量定义
while(search_avl_tree(current_lex_node->token_name)->type_type==)
{
temp_var_link=malloc(sizeof(struct var_decl_link));
temp_var_link->current_var_node=tackle_var_decl();
temp_var_link->next=current_scope_var;
current_scope_var=temp_var_link;
//然后吃掉所有的换行
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
while(current_lex_node->current_lex_type==new_line)//吃掉之后的换行
{
first_chain_head=first_chain_head->next;
free(current_lex_node->token_name);
free(current_lex_node->current_lex_type);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_node=current_chain_node->current_first_token;
}
}
//现在开始处理动作列表
current_scope_action=tackle_action();
return_function=malloc(sizeof(struct function_description));
return_function->current_action_link=current_scope_action;
return_function->current_type_link=current_scope_type;
return_function->current_var_link=current_scope_var;
}