#include "lex_define.h"
enum keywords_type//代表一些关键字
{
loop_for=,//代表for关键字
loop_while,//代表while关键字
branch_if,//代表if关键字
branch_else,//代表else关键字
break,
};
//这里sizeof被分到运算符里了,而main被分到函数名里面去了,基本数据类型和符号前缀被分到数据类型名之中了
enum delimit_type
{
open_brace,//开大括号
close_brace,//闭大括号
semicolon//分号
};
//现在开始描述运算符的优先级
//0级: [ ] ( ) -> . 结合性 从左到右
//1级:! ~ $ @ - (cast) sizeof 这些都是单目运算符,注意cast的意思是强制类型转换
//2级: * / % 这些运算符都是从左到右
//3级: + - 这些也是从左到右
//4级: >> << 按道理木有结合性
//5级:> < >= <= 从左到右
//6级: == != 从左到右
//7级: & 从左到右
//8级:^ 从左到右
//9级:| 从左到右
//10级: && 从左到右
//11级: ||从左到右 struct operator_token//这个是操作符栈中的token,注意我们在处理数组运算符的时候采取了特殊的方法
{
enum basic_operator_type current_op_type;//这个域可以提供所有的信息
int priority;//运算符优先级
int sub_number;//代表有几个操作数
//我们可以根据前面的那个枚举类型得到所有的信息,但是我们另外提出这个优先级域,是为了简化判断的操作
}
//现在我们来定义一个抽象语法树的结构
//首先考虑的是语法树节点的种类
//这个域比较复杂,主要是因为各种不同的语法节点有大小不一的子节点个数
//首先对于没有子节点的情况,这里代表的是那些关键字,因为关键字不需要产生式
//然后还有所有的名字和常量,
//还有单字符和字符串,
//然后讨论的是只有一个子节点的情况,这里对应的是单目运算符
//然后讨论的是两个子节点的情况,这里代表了所有的运算符和赋值符和函数调用节点
//这里的函数调用节点包括两个子节点,一个是函数名,另外一个是参数列表
//而参数列表也是拥有两个域的节点,这两个节点中最多有一个是参数列表属性,其他的都是名字
//对于单语句块节点,这个也是拥有两个子节点的节点,构造方式与参数列表节点一样
//有三个子节点的是while语句,分别是while节点,判断节点,和代码块节点
//有五个子节点的是for语句,分别是for节点,初始化节点,判断节点,修改节点和代码块节点
//这里还有if else节点也是五个节点 if节点 判断节点 代码块节点,else节点,代码块节点
//但是我们采取的是双栈,所以这些节点会被分成两种。
//一种是单语句,即全部都是算术操作的那些节点,即expression
//另外一种是多语句,即所有的单语句即控制结构
//注意我们这里在调用函数的时候,参数里面不允许有变量值的修改操作,因此只能是变量与数组运算,结构运算
//指针运算这三种的结合,这样处理函数的时候就简单多了
//因此,我们这里有两棵语法分析树,第一个是句型树,第二个是表达式树,函数调用放在表达式树之中
//对于表达式树,我们只需要考虑操作符的种类,这里我们把赋值符放在操作符里面去考虑,这样
//还要提到一点就是句型树中的判断节点属于表达式树,break语句也属于表达式树 //这里我们来处理类型声明
typedef struct _phrase_tree_node//表达式语法树的节点
{
union
{
int phrase_type;
//0代表break,1代表常数,2代表名字,3代表函数调用,4代表单个参数,5代表参数列表
//6代表赋值运算,7代表单字符,8代表字符串,9及以后的代表操作符
enum basic_operator_type current_op_type;//因为我们定义操作符编号是从5开始的,所以不会与前面的那个重合
};
union
{
char* constant_str;//代表常量
char* var_name;//代表名字
struct
{
struct _phrase_tree_node* left_son;
struct _phrase_tree_node* right_son;
};//代表有两个分量的类型,包括函数调用,函数参数,双目运算符,赋值操作
struct _phrase_tree_node* original_node;//代表除了强制类型转换之外的单目运算符
struct
{
struct _phrase_tree_node* token_for_cast;//代表单目运算符
struct _type_description* cast_type_description;//这个是为了强制类型转换用的....不爽
int pointer_layer;//代表指针的层数
};
};
}phrase_tree_node;
//这里对于操作数栈,我们不需要再去定义其他的数据类型,可以直接在栈中使用上面定义的表达式语法树节点
//我们还需要去管理一个赋值id栈和id操作符栈,这样来处理是因为我们把赋值符从操作符中独立出来了,
//所以不得不这样做,规范化的代价啊。。。
//在处理赋值语句的时候id栈中存放的是id,在处理完expression后,最后处理赋值语句,生成赋值语法节点
//而对于函数调用,也是需要处理id的,这个时候也需要使用id栈。为了弄清楚我们使用的是赋值id还是参数id
//我们利用另外的一个指针来处理赋值id,因为赋值id之只有一个,而参数id可以有很多个
//我们在遇到=号的时候,把id栈中的语法节点的指针取出放在copytopointer这个指针中,然后清空id栈。
//所以在expression表达式处理完的时候,检查这个指针是不是空,如果不是,则需要生成赋值节点
//对于函数参数的处理,每次遇到函数名,把函数名压入操作数栈,把之后的括号压入操作符栈,
//然后对于之后的处理,都在id栈与id操作符栈中进行,
//对于遇到分号的时候,如果id栈不为空,则把id栈中所有的id节点合并成一个参数列表节点,当然当前只有一个就
//算了,
//因此我们需要四个栈,还有为了支持强制类型转化操作和sizeof操作,我们还需要一个类型参数变量来供使用
//为了支持赋值,我们还需要赋值的语法树节点,为了支持函数参数我们需要一个参数语法树节点
struct operator_token* phrase_operator_stack[];//优先级也就只有12个,40个足够了 struct _phrase_tree_node* phrase_token_stack[];//给40个是看他面子了 //id_0: name |(id)| id_0[constant] | id_0.name | id_0->name |id_0[name]
//id_1: id_0 | @id_0
//id : id_1
struct _phrase_tree_node* id_token_stack[];//打发叫花子
enum basic_operator_type id_operator_stack[];//id里面的运算符就更少了,因此10个也是足够了
int id_token_stack_pointer;
int id_operator_stack_pointer;
int phrase_token_stack_pointer;
int phrase_operator_stack_pointer;
struct _type_description* temp_cast_one;//这个是为强制类型转换而使用的
int cast_pointer_layer;//代表强制类型转换的指针层数
int predecent;
//这个是用来说明前面遇到的是操作符还是操作数,这个变量主要是为了处理负号和指针和取地址
void tackle_id_op(char* input_op)//这个函数是用来处理id的操作符栈
{
struct _phrase_tree_node* temp_node_one;
enum basic_operator_type current_top_op;
switch(*input_op)
{
case '(':
id_operator_stack_pointer++;
id_operator_stack[id_operator_stack_pointer]=parenthesis;
break;
case '[':
id_operator_stack_pointer++;
id_operator_stack[id_operator_stack_pointer]=array_op;
break;
case '.':
if(id_operator_stack_pointer==)//如果目前为空栈,则入栈
{
id_operator_stack_pointer++;
id_operator_stack[id_operator_stack_pointer]=str_sub;
}
else
{
current_top_op=id_operator_stack[id_operator_stack_pointer];
switch(current_top_op)
{
case parenthesis:
case array_op:
case get_mem:
id_operator_stack_pointer++;
id_operator_stack[id_operator_stack_pointer]=str_sub;
break;
case str_sub:
case p_str_sub:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=current_top_op;
if(id_token_stack_pointer>=)
{
temp_node_one->left_son=id_token_stack[id_token_stack_pointer-];
temp_node_one->right=id_token_stack[id_token_stack_pointer];
id_token_stack_pointer--;
id_token_stack[id_token_stack_pointer]=temp_node_one;
id_operator_stack_pointer++;
id_operator_stack[id_operator_stack_pointer]=str_sub;
}
else
{
printf("insufficient id token during pop operator\n");
exit();
}
break;
default:
printf("unknown op in id_op_stack\n");
exit();
break;
}
}
break;
case '>':
if(id_operator_stack_pointer==)//如果目前为空栈,则入栈
{
id_operator_stack_pointer++;
id_operator_stack[id_operator_stack_pointer]=p_str_sub;
}
else
{
current_top_op=id_operator_stack[id_operator_stack_pointer];
switch(current_top_op)
{
case parenthesis:
case array_op:
case get_mem:
id_operator_stack_pointer++;
id_operator_stack[id_operator_stack_pointer]=p_str_sub;
break;
case str_sub:
case p_str_sub:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=current_top_op;
if(id_token_stack_pointer>=)
{
temp_node_one->left_son=id_token_stack[id_token_stack_pointer-];
temp_node_one->right=id_token_stack[id_token_stack_pointer];
id_token_stack_pointer--;
id_token_stack[id_token_stack_pointer]=temp_node_one;
id_operator_stack[id_operator_stack_pointer]=p_str_sub;
}
else
{
printf("insufficient id token during pop operator\n");
exit();
}
break;
default:
printf("unknown op in id_op_stack\n");
exit();
break;
}
}
break;
case '@':
id_operator_stack_pointer++;
id_operator_stack[id_operator_stack_pointer]=get_mem;
break;
case ')':
while(id_operator_stack[id_operator_stack_pointer]!=parenthesis)
//这里我们之所以加上这个大于0,是因为在函数的参数列表的形式下,会有一个多余的闭括号
{
current_top_op=id_operator_stack[id_operator_stack_pointer];
switch(current_top_op)
{
case get_mem:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=get_mem;
if(id_token_stack_pointer>=)
{
temp_node_one->original_node=id_token_stack[id_token_stack_pointer];
id_token_stack[id_token_stack_pointer]=temp_node_one;
}
else
{
printf("null token stack while pop operator\n");
exit();
}
break;
case p_str_sub:
case str_sub:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=current_top_op;
if(id_token_stack_pointer>=)
{
temp_node_one->left_son=id_token_stack[id_token_stack_pointer-];
temp_node_one->right=id_token_stack[id_token_stack_pointer];
id_token_stack_pointer--;
id_token_stack[id_token_stack_pointer]=temp_node_one;
}
else
{
printf("insufficient id token during pop operator\n");
exit();
}
break;
case array_op:
printf("unmatched array_op during parenthesis\n");
exit();
break;
default:
printf("unknowd op in id_op_stack\n");
break;
}
id_operator_stack_pointer--;
}
if(id_token_stack_pointer<)
{
printf("null id token in stack during pop\n");
exit();
}
else
{
id_operator_stack_pointer--;
}
break;
case ']':
while(id_operator_stack[id_operator_stack_pointer]!=array_op)
{
current_top_op=id_operator_stack[id_operator_stack_pointer];
switch(current_top_op)
{
case get_mem:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=get_mem;
if(id_token_stack_pointer>=)
{
temp_node_one->original_node=id_token_stack[id_token_stack_pointer];
id_token_stack[id_token_stack_pointer]=temp_node_one;
}
else
{
printf("null token stack while pop operator\n");
exit();
}
break;
case p_str_sub:
case str_sub:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=current_top_op;
if(id_token_stack_pointer>=)
{
temp_node_one->left_son=id_token_stack[id_token_stack_pointer-];
temp_node_one->right=id_token_stack[id_token_stack_pointer];
id_token_stack_pointer--;
id_token_stack[id_token_stack_pointer]=temp_node_one;
}
else
{
printf("insufficient id token during pop operator\n");
exit();
}
break;
case parenthesis:
printf("unmatched parenthesis during array_op\n");
exit();
break;
default:
printf("unknowd op in id_op_stack\n");
exit();
break;
}
id_operator_stack_pointer--;
}
if(id_token_stack_pointer<)
{
printf("insufficient id token in stack during pop array_op\n");
exit();
}
else
{
id_operator_stack_pointer--;
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=array_op;
temp_node_one->left_son=id_token_stack[id_token_stack_pointer-];
temp_node_one->right=id_token_stack[id_token_stack_pointer];
id_token_stack_pointer--;
id_token_stack[id_token_stack_pointer]=temp_node_one;
}
break;
default:
printf("unknown op_type when tackcle_id_op\n");
break;
}
}
phrase_tree_node* get_id(void)//这个函数是用来得到id的
{ struct _phrase_tree_node* temp_node_one;
struct first_token_chain* temp_first_one;
struct first_token_chain* temp_first_two;
struct first_lex_token* current_lex_token;
struct first_lex_token* temp_lex_token;
struct operator_token* temp_op;
id_operator_stack_pointer=id_token_stack_pointer=;
temp_first_one=first_chain_head;
current_lex_token=temp_first_one->current_first_token;
while()
{
if(current_lex_token->current_lex_type==an_operator)//对于是操作符的时候
{
if(*(current_lex_token->token_name)!='=')//如果不是等于号
{
switch(*(current_lex_token->token_name))
{
case '-'://如果第一个字符是-.这个情况我们需要特殊考虑
temp_first_two=temp_first_one->next;
temp_lex_token=temp_first_two->current_first_token;
if(temp_lex_token->current_lex_type!=an_operator)//如果后面搭配的不是操作符,报错
{
printf("invalid - during id recognise\n");
exit();
}
else
{
if(*(temp_lex_token->token_name)!='>')//如果后面搭配的不是>,则报错
{
printf("invalid match operator %s after -\n",temp_lex_token->token_name);
exit();
}
else//如果刚好组成了搭配
{
free(current_lex_token->token_name);
free(current_lex_token);
free(temp_first_one);
first_chain_head=temp_first_two->next;
tackle_id_op(temp_lex_token->token_name);//这个是处理id操作符栈的主体函数
free(temp_lex_token->token_name);
free(temp_lex_token);
free(temp_first_two);
temp_first_one=first_chain_head;
current_lex_token=temp_first_one->current_first_token;
}
}
break;
case '(':
case ')':
case '[':
case ']':
case '@':
case '.':
tackle_id_op(*(current_lex_token->token_name));
first_chain_head=temp_first_one->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(temp_first_one);
temp_first_one=first_chain_head;
current_lex_token=temp_first_one->current_first_token;
break;
default:
printf("invalid operator %s during id recognisation\n",current_lex_token->token_name);
break;
}
}
else
//如果是等于号
//则我们需要将操作符栈里面的东西全都弹出
{
while(id_operator_stack_pointer>)
{
switch(id_operator_stack[id_operator_stack_pointer])
{
case p_str_sub:
case str_sub:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=id_operator_stack[id_operator_stack_pointer];
if(id_token_stack_pointer>=)
{
temp_node_one->left_son=id_token_stack[id_token_stack_pointer-];
temp_node_one->right=id_token_stack[id_token_stack_pointer];
id_token_stack_pointer--;
id_token_stack[id_token_stack_pointer]=temp_node_one;
id_operator_stack_pointer--;
}
else
{
printf("insufficient id token during pop operator\n");
exit();
}
break;
case get_mem:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=id_operator_stack[id_operator_stack_pointer];
temp_node_one->original_node=id_token_stack[id_token_stack_pointer];
id_token_stack[id_token_stack_pointer]=temp_node_one;
id_operator_stack_pointer--;
break;
case array_op:
printf("unmatched array_op in id_token\n");
exit();
break;
case parenthesis:
printf("unmatched parenthesis in id_token\n");
exit();
break;
default:
printf("unexpected op in id token\n");
exit();
break;
} }//至此,操作符栈处理完毕
//然后修正好词法链的头节点,使得这个=号被抛弃
first_chain_head=temp_first_one->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(temp_first_one);
id_token_stack_pointer--;
return id_token_stack[];
}
}
else
//对于是常量或者名字的情况,我们直接入栈,
//对于是分号的情况,我们要采取与等号相同的操作
{
if(current_lex_token->current_lex_type!=delimit)
{
switch(current_lex_token->current_lex_type)
{
case constant:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->constant_str=current_lex_token->token_name;
temp_node_one->phrase_type=;
id_token_stack_pointer++;
id_token_stack[id_token_stack_pointer]=temp_node_one;
first_chain_head=fisrt_chain_head->next;
free(current_lex_token);
free(temp_first_one);
temp_first_one=first_chain_head;
current_lex_token=temp_first_one->current_first_token;
break;
case name:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->var_name=current_lex_token->token_name;
temp_node_one->phrase_type=;
id_token_stack_pointer++;
id_token_stack[id_token_stack_pointer]=temp_node_one;
first_chain_head=fisrt_chain_head->next;
free(current_lex_token);
free(temp_first_one);
temp_first_one=first_chain_head;
current_lex_token=temp_first_one->current_first_token;
break;
default:
printf("invalid token %s in id recognization\n",current_lex_token->token_name);
exit();
break;
}
}
else//如果是分号
{
while(id_operator_stack_pointer>)//清空操作符栈
{
switch(id_operator_stack[id_operator_stack_pointer])
{
case p_str_sub:
case str_sub:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=id_operator_stack[id_operator_stack_pointer];
if(id_token_stack_pointer>=)
{
temp_node_one->left_son=id_token_stack[id_token_stack_pointer-];
temp_node_one->right=id_token_stack[id_token_stack_pointer];
id_token_stack_pointer--;
id_token_stack[id_token_stack_pointer]=temp_node_one;
id_operator_stack_pointer--;
}
else
{
printf("insufficient id token during pop operator\n");
exit();
}
break;
case get_mem:
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->current_op_type=id_operator_stack[id_operator_stack_pointer];
temp_node_one->original_node=id_token_stack[id_token_stack_pointer];
id_token_stack[id_token_stack_pointer]=temp_node_one;
id_operator_stack_pointer--;
break;
case array_op:
printf("unmatched array_op in id_token\n");
exit();
break;
case parenthesis:
printf("unmatched parenthesis in id_token\n");
exit();
break;
default:
printf("unexpected op in id token\n");
exit();
break;
} }//至此,操作符栈处理完毕
//然后修正好词法链的头节点,使得这个=号被抛弃
first_chain_head=temp_first_one->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(temp_first_one);
id_token_stack_pointer--;
return id_token_stack[];
}
}
}
}
phrase_tree_node* get_func(void)
{
phrase_tree_node* tree_node_one;
phrase_tree_node* tree_node_two;
phrase_tree_node* tree_node_three;
phrase_tree_node* tree_node_four;
phrase_tree_node* tree_node_five;
first_token_chain* chain_node_one;
first_token_chain* chain_node_two;
first_lex_token* current_token;
tree_node_three=malloc(sizeof(struct _phrase_tree_node));//这个是函数名称节点
tree_node_three->phrase_type=;
tree_node_three->var_name=first_chain_head->current_first_token->token_name;
chain_node_one=first_chain_head;
first_chain_head=chain_node_one->next;
free(chain_node_one->current_first_token);
free(chain_node_one);
chain_node_one=first_chain_head;
first_chain_head=chain_node_one->next;
free(chain_node_one->current_first_token->token_name);
free(chain_node_one->current_first_token);
free(chain_node_one);
tree_node_two=get_id();//获得第一个参数,
tree_node_five=malloc(sizeof(struct _phrase_tree_node));
tree_node_five->phrase_type=;
tree_node_five->original_node=tree_node_two;
tree_node_two=tree_node_five;
//由于不带参数的函数在调用的时候一定会有一个void参数,所以这个一定会成功
while(*(first_chain_head->current_first_token->token_name)!=')')//这里闭括号预示着参数列表的结尾
{
tree_node_four=get_id();
tree_node_five=malloc(sizeof(struct _phrase_tree_node));
tree_node_five->phrase_type=;
tree_node_five->original_node=tree_node_four;
tree_node_four=malloc(sizeof(struct _phrase_tree_node));
tree_node_four->phrase_type=;
tree_node_four->left_son=tree_node_two;
tree_node_four->right_son=tree_node_five;
tree_node_two=tree_node_four;
} chain_node_one=first_chain_head;
first_chain_head=chain_node_one->next;
free(chain_node_one->current_first_token->token_name);
free(chain_node_one->current_first_token);
free(chain_node_one);
//这里把输入指针下移一个
tree_node_one=malloc(sizeof(struct _phrase_tree_node));
tree_node_one->phrase_type=;
tree_node_one->left_son=tree_node_three;
tree_node_one->right_son=tree_node_two;
return tree_node_one;
} void phrase_operator_pop(int current_priority)//这个函数是为了从操作符栈中弹出一个操作符,对于是开括号的时候什么也不做
{
phrase_tree_node* tree_node_one;
int number_of_arg;
while(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=current_priority)
{
number_of_arg=phrase_operator_stack[phrase_operator_stack_pointer]->sub_number;
tree_node_one=malloc(sizeof(struct _phrase_tree_node));
tree_node_one->current_op_type=phrase_operator_stack[phrase_operator_stack_pointer]->current_op_type;
if(number_of_arg==)
{
if(tree_node_one->current_op_type!=array_op)//如果是数组运算符就什么都不干
{
if(phrase_token_stack_pointer<)
{
printf("need more token in phrase token stack\n");
free(tree_node_one);
exit();
}
else
{
tree_node_one->left_son=phrase_token_stack[phrase_token_stack_pointer-];
tree_node_one->right_son=phrase_token_stack[phrase_token_stack_pointer];
phrase_token_stack_pointer--;
phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
phrase_operator_stack_pointer--;
}
}
else
{
//do nothing
}
}
else//这里有三种种特殊情况,一个是括号,一个就是强制类型转换,还有一个是sizeof
{
switch(tree_node_one->current_op_type)
{
case parenthesis:
//do nothing 因为是开括号
break;
case type_cast:
tree_node_one->token_for_cast=phrase_token_stack[phrase_token_stack_pointer];
tree_node_one->cast_type_description=temp_cast_one;
temp_cast_one=NULL;
cast_pointer_layer=;
tree_node_one->pointer_layer=cast_pointer_layer;
phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
phrase_operator_stack_pointer--;
break;
case get_size://对于sizeof有两种形式
if(temp_cast_one!=NULL)//对应的是声明头形式
{
tree_node_one->token_for_cast=phrase_token_stack[phrase_token_stack_pointer];
tree_node_one->cast_type_description=temp_cast_one;
tree_node_one->pointer_layer=cast_pointer_layer;
temp_cast_one=NULL;
cast_pointer_layer=;
phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
phrase_operator_stack_pointer--;
}
else//对应的是变量形式
{
tree_node_one->original_node=phrase_token_stack[phrase_token_stack_pointer];
phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
phrase_operator_stack_pointer--;
}
break;
default:
if(phrase_token_stack_pointer==)
{
printf("need token in phrase token stack\n");
free(tree_node_one);
exit();
}
else
{
tree_node_one->original_node=phrase_token_stack[phrase_token_stack_pointer];
phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
phrase_operator_stack_pointer--;
}
break;
}
}
}
} void tackle_phrase_op(char* in_op)
{
struct first_lex_token* current_lex_token;
struct first_lex_token* next_lex_token;
struct _phrase_tree_node* temp_tree_node;
struct first_token_chain* current_chain_node;
struct first_token_chain* next_chain_node;
struct operator_token* current_operator_token;
switch(*in_op)
{
case '(':
//这里就有点复杂了因为要处理强制类型转换这个蛋疼的东西
current_chain_node=first_chain_head;
next_chain_node=current_chain_node->next;
next_lex_token=next_chain_node->current_first_token;
if(next_lex_token->current_lex_type==name)//准备强制类型转换
{
temp_cast_one=search_avl_tree(next_lex_token->token_name);
if(temp_cast_one->type_type==)//这里就意味着强制类型转换
{
first_chain_head=next_chain_node->next;
free(current_chain_node->current_first_chain->token_name);
free(current_chain_node->current_first_chain);
free(current_chain_node);
free(next_lex_token->token_name);
free(next_lex_token);
free(next_chain_node);
if(first_chain_head->current_first_chain->current_lex_type==name)//这个对应的是符合类型的转换
{
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
temp_cast_one=search_avl_tree(current_lex_token->token_name);//找出这个名字的类型
first_chain_head=first_chain_head->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
}
cast_pointer_layer=;
while(*(first_chain_head->current_first_token->token_name)!=')')//吃掉所有的指针符号
{
current_lex_token=first_chain_head->current_first_token;
if(*(current_lex_token->token_name)!='*')
{
printf("encount an unexpected token %s during cast_recognise\n",current_lex_token->token_name);
exit();
}
else
{
cast_pointer_layer=cast_pointer_layer<< + ;
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
//这里我们就先不纠结指针层数的限制了
}
}
//现在遇到的是括号 //直接把这个括号丢弃,然后生成一个强制类型转换操作符
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=type_cast;
phrase_operator_pop();
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
else//遇到没有被识别的名称类型
{
printf("unexpected type %s during cast_recognisation\n",next_lex_token->token_name);
exit();
}
}
else
//对应的是普通的括号
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=parenthesis;
//注意一旦碰到开括号,前面的优先级自动停止,因此不会碰到操作符出栈的情况
//所以这里不需要考虑优先级了
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
//这里两种情况都被处理完毕了,累
break;
case '[':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=array_op;
//这里数组运算符与括号不一样,数组运算符是一个运算符和界定符,而括号只是一个界定符。。。
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
case '.':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=str_sub;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
case '-':
if(predecent==)//代表前面是操作数的情况
{
next_chain_node=current_chain_node->next;
next_lex_token=next_chain_node->current_first_token;
if(next_lex_token->current_lex_type==an_operator)//如果后面跟的也是操作符
{
if((*(next_lex_token->token_name))=='>')//对应的是p_str_sub
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=p_str_sub;
phrase_operator_pop();
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
first_chain_head=next_chain_node->next;
free(current_lex_node->token_name);
free(current_lex_node);
free(current_chain_node);
free(next_lex_node->token_name);
free(next_lex_node);
free(next_chain_node);
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
}
else//对应的不是结构体指针运算的情况,那就是减号
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=minus;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
}
else//对应的不是结构体指针运算的情况,那就是减号
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=minus;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
}
else//对应的是前面的是操作符的情况,则这里是当作负号来使用的
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=negative;
if(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=)
{
phrase_operator_pop();
}
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
} break;
case ')':
//对于闭括号和闭方括号,我们需要一直弹出栈,直到遇到开括号和开方括号
phrase_operator_pop();
phrase_operator_stack_pointer--;
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
precedent=;
break;
case ']':
//处理方法同上,不过需要建立一个token节点
phrase_operator_pop();
temp_tree_node=malloc(sizeof(struct _phrase_tree_node));
temp_tree_node->current_op_type=array_op;
temp_tree_node->right_son=phrase_token_stack[phrase_token_stack_pointer];
phrase_token_stack_pointer--;
temp_tree_node->left_son=phrase_token_stack[phrase_token_stack_pointer];
phrase_token_stack[phrase_token_stack_pointer]=temp_tree_node;
phrase_operator_stack_pointer--;
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
precedent=;
break;
case '!':
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
free(current_chain_node->current_first_token->token_name);
free(current_chain_node->current_first_token);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
if(*(current_lex_token->token_name)!='=')
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=not;
phrase_operator_pop();
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
else//对应的是不等号
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type= nequal;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
break;
case '~':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=bit_rev;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
case '&':
if(predecent==)//如果前面已经有操作符了,说明这个是取地址运算
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=get_adr;
while(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=)
{
phrase_operator_pop();
}
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
else//说明这个不是取地址运算,而是布尔运算或者位运算
{
current_chain_node=first_chain_head;
next_chain_node=current_chain_node->next;
next_lex_token=next_chain_node->current_first_token;
if(next_lex_token->current_lex_type==an_operator)//如果后面跟的是操作符
{
if(*(next_lex_token->token_name)=='&')//如果是布尔运算
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=and;
phrase_operator_pop();
//这里需要吃掉两个字符
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
first_chain_head=first_chain_head->next;
free(next_lex_token->token_name);
free(next_lex_token);
free(next_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
} else//这里是位运算
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=bit_and;
while(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=)
{
phrase_operator_pop();
}
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token; free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
} }
else//这里还是位运算
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=bit_and;
while(phrase_operator_stack[phrase_operator_stack_pointer]->priority<=)
{
phrase_operator_pop();
}
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
}
break;
case '*':
if(predecent==)//这里对应的是指针运算
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=get_mem;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
else//对应的是乘法运算
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=multi;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
break;
case 's'://对应sizeof运算,这里又需要类型头部或者变量,这里我们强制要求有括号
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
free(current_chain_node->current_first_chain->token_name);
free(current_chain_node->current_first_chain);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
if(*(current_lex_token->token_name)=='(')//准备sizeof,至于括号里面的东西就交给括号去处理
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=get_size;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
else
{
printf("missing ( after sizeof\n");
exit();
}
break;
case '/':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=div;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
case '%':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=module;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
case '+':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=get_mem;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
case '<'://这里有三种情况。。。 移位和两个判断
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
free(current_chain_node->current_first_token->token_name);
free(current_chain_node->current_first_token);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
if(current_lex_token->current_lex_type==an_operator)//如果后面接的是操作符
{
switch(*(current_lex_token->token_name))
{
case '<':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=left_shift;
phrase_operator_pop();
first_chain_head=first_chain_head->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
case '=':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=sma_eqa;
phrase_operator_pop();
first_chain_head=first_chain_head->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
default:
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=smaller;
phrase_operator_pop();
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
}
}
else
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=smaller;
phrase_operator_pop();
first_chain_head=first_chain_head->next;
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
break;
case '>':
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
free(current_chain_node->current_first_token->token_name);
free(current_chain_node->current_first_token);
free(current_chain_node);
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
if(current_lex_token->current_lex_type==an_operator)//如果后面接的是操作符
{
switch(*(current_lex_token->token_name))
{
case '>':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=right_shift;
phrase_operator_pop();
first_chain_head=first_chain_head->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
case '=':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=lar_eqa;
phrase_operator_pop();
first_chain_head=first_chain_head->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
default:
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=larger;
phrase_operator_pop();
first_chain_head=first_chain_head->next;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
}
}
else
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=larger;
phrase_operator_pop();
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
break;
case '=':
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
first_chain_head=first_chain_head->next;
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
if(*(current_lex_token->token_name)=='=')
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=equal;
phrase_operator_pop();
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
else
{
printf("stand alone = is encounted\n");
exit();
}
break;
case '^':
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=bit_xor;
phrase_operator_pop();
current_chain_node=first_chain_head;
first_chain_head=first_chain_head->next;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
break;
case '|':
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
first_chain_head=first_chain_head->next;
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
if(*(current_lex_token->token_name)=='|')
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=or;
phrase_operator_pop();
free(current_lex_token->token_name);
free(current_lex_token);
free(current_chain_node);
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
else
{
current_operator_token=malloc(sizeof(struct operator_token));
current_operator_token->priority=;
current_operator_token->sub_number=;
current_operator_token->current_op_type=bit_or;
phrase_operator_pop();
phrase_operator_stack_pointer++;
phrase_operator_stack[phrase_operator_stack_pointer]=current_operator_token;
predecent=;
}
break;
default:
printf("unknown operator %s is encounted\n",in_op);
exit();
break;
} }
phrase_tree_node* get_expression(void)//这里才是他妈的重头戏啊,12级运算符不弄死你
{
phrase_tree_node* tree_node_one;
first_lex_token* current_lex_token;
first_lex_token* next_lex_token;
first_token_chain* current_chain_node;
first_token_chain* next_chain_node;
current_token_chain=first_chain_head;
predecent=;
phrase_token_stack_pointer=;
phrase_operator_stack_pointer=;
current_lex_token=current_token_chain->current_first_token;
while(current_lex_token->current_lex_type!=delimit)
{
switch(current_lex_token->current_lex_type)
{
case char_type:
tree_node_one=malloc(sizeof(struct _phrase_tree_node));
tree_node_one->phrase_type=;
tree_node_one->var_name=current_lex_token->token_name;
free(curren_lex_token);
first_chain_head=first_chain_head->next;
free(current_token_chain);
current_token_chain=first_chain_head;
current_lex_token=current_token_chain->current_first_token;
predecent=;
break;
case string_phrase:
tree_node_one=malloc(sizeof(struct _phrase_tree_node));
tree_node_one->phrase_type=;
tree_node_one->var_name=current_lex_token->token_name;
free(curren_lex_token);
first_chain_head=first_chain_head->next;
free(current_token_chain);
current_token_chain=first_chain_head;
current_lex_token=current_token_chain->current_first_token;
predecent=;
break;
case constant:
tree_node_one=malloc(sizeof(struct _phrase_tree_node));
tree_node_one->phrase_type=;
tree_node_one->var_name=current_lex_token->token_name;
free(curren_lex_token);
first_chain_head=first_chain_head->next;
free(current_token_chain);
current_token_chain=first_chain_head;
current_lex_token=current_token_chain->current_first_token;
predecent=;
break;
case name:
next_chain_node=current_chain_node->next;
next_lex_token=next_chain_node->current_first_token;
if(*(next_lex_token->token_name)=='(')//这里对应的是一个函数
{
tree_node_one=get_func();
phrase_token_stack_pointer++;
phrase_token_stack[phrase_token_stack_pointer]=tree_node_one;
current_chain_node=first_chain_head;
current_lex_token=current_chain_node->current_first_token;
}
else
{
tree_node_one=malloc(sizeof(struct _phrase_tree_node));
tree_node_one->phrase_type=;
tree_node_one->var_name=current_lex_token->token_name;
free(curren_lex_token);
first_chain_head=first_chain_head->next;
free(current_token_chain);
current_token_chain=first_chain_head;
current_lex_token=current_token_chain->current_first_token;
}
predecent=;
break;
case an_operator:
tackle_phrase_op(current_lex_token->token_name);
current_token_chain=first_chain_head;
current_lex_token=current_token_chain->current_first_token;
break;
default:
printf("unknown lex type is encounted\n");
exit();
break;
}
}
//现在开始清空栈
free(current_lex_token->token_name);
free(curren_lex_token);
first_chain_head=first_chain_head->next;
free(current_token_chain);
phrase_operator_pop();
return phrase_token_stack[];
} phrase_tree_node* get_phrase(void)
//这个函数是处理单个句型的函数,不包括定义和声明,那里我们特殊处理
//我们要确保调用者在调用这个函数的时候,已经吃掉了所有的换行符
{
struct first_lex_token current_lex_token;
struct _phrase_tree_node* copyto_address_node;//赋值的目标地址
struct _phrase_tree_node* temp_node_one;
struct _phrase_tree_node* temp_node_three;
struct _phrase_tree_node* temp_node_two;
struct first_token_chain* temp_first_one;
struct first_token_chain* temp_first_two;
copyto_address_node=NULL;
temp_first_one=first_chain_head;
temp_first_two=temp_first_one->next;
current_lex_token=temp_first_one->current_first_token;
if(current_lex_token->current_lex_type==name)//如果句子开头是名字
{
if(temp_first_two->current_first_token->current_lex_type==delimit)//如果紧接着的是分号,那么就是break语句
{
temp_node_one=malloc(sizeof(struct _phrase_tree_node));
temp_node_one->phrase_type=;//代表是break语句
free(temp_first_one);
temp_first_one=temp_first_two->next;
free(temp_first_two);
first_chain_head=temp_first_one;
return temp_node_one;
}
else//如果不是break语句
{
if(*(temp_first_two->current_first_token->token_name)=='(')//这里对应的是函数调用
{
if(serach_avl_tree(current_lex_token->token_name)->type_type==)//如果真是的函数名
{
return get_func();//这个是专门处理函数的
}
else//如果是不合理的搭配,则报错
{
printf("error encountered\n");
printf("invalid match\n");
printf("the match is %s and ( \n",current_lex_token->token_name);
exit();
}
}
else//如果后面接的不是开括号,则一定是赋值语句,则我们需要首先获得id,然后再去处理表达式
{
copyto_address_node=get_id();//利用get_id来获得地址,注意这里get_id会保留=的存在,需要自己去处理
temp_first_one=first_chain_head->next;
free(first_chain_head);
first_chain_head=temp_first_one;
temp_node_two=get_expression();//利用函数来获得表达式
temp_node_three=malloc(sizeof(struct _phrase_tree_node));//分配返回节点
temp_node_three->phrase_type=;
temp_node_three->left_son=copyto_address_node;
temp_node_three->right_son=temp_node_two;
return temp_node_three;
}//处理完毕
}
}
else//如果开头不是名字,那么就一定是赋值运算。
{
copyto_address_node=get_id();
temp_first_one=first_chain_head->next;
free(first_chain_head);
first_chain_head=temp_first_one;
temp_node_two=get_expression();//利用函数来获得表达式
temp_node_three=malloc(sizeof(struct _phrase_tree_node));//分配返回节点
temp_node_three->phrase_type=;
temp_node_three->left_son=copyto_address_node;
temp_node_three->right_son=temp_node_two;
return temp_node_three;
}
}
05-08 15:44