📄 compile3.cpp
字号:
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
p_error("无法查找到"+name);
else
{
if(temp2->type=="")
p_error("变量名未声明就使用!");
temp->type=temp2->type;//把ID的类型传给var
}
}
/*38var ID [ expression ]*/
void varArray(string name)
{
//int arraymax;
int index1=hash_funct("var");
table_var* temp=tableVar[index1];
while(temp!=NULL)
{
if(temp->name=="var")
{
break;
}
else
temp=temp->next;
}
if(temp==NULL)
{
temp=new table_var();
tableVar[index1]=temp;
}
temp->name="var";
//找到name所在的位置
int index=hash_funct(name);
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name==name)
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
p_error("无法查找到"+name);
else
{
if(temp2->type=="")
p_error("数组变量名未声明就使用!");
temp->type=temp2->type;//把ID的类型传给var
//arraymax=temp2->value ;
}
//检查expression是否正确
table_var* temp3=tableVar[hash_funct("expression")];
bool b=true;
if(temp3==NULL)
p_error("无法查找到expression");
else
{
while(temp3!=NULL)
{
if(temp3->name =="expression")
{
b=false;
break;
}
}
if(b==true)
p_error("无法查找到expression");
}
if(temp3->type!="int")
p_error("数组的序列类型错误!");
if(temp3->value <0||(temp3->value > temp2->value) )
p_error("数组下标越界");
}
/*39simple-expression additive-expression relop additive-expression*/
void simple_relop()
{//暂且不考虑 relop 两边的类型是否一样
//构造simple-expression节点
int index=hash_funct("simple-expression");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="simple-expression")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->type="bool";
temp2->name ="simple-expression";
}
/*40simple-expression additive-expression*/
void simple_add()
{
int index=hash_funct("simple-expression");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="simple-expression")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->type="int";
temp2->name ="simple-expression";
}
/*41relop <=*/
/*42relop <*/
/*43relop >*/
/*44relop >=*/
/*45relop ==*/
/*46relop !=*/
/*47additive-expression additive-expression addop term*/
void additive_expression()
{
int index1=hash_funct("additive-expression");
int index2=hash_funct("term");
//查找到additive-expression
table_var* temp=tableVar[index1];
while(temp!=NULL)
{
if(temp->name=="additive-expression")
break;
else
temp=temp->next;
}
if(temp==NULL)
p_error("无法查找到additive-expression");
//查找到term
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="term")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到term");
if(temp->type!=temp1->type)
p_error("操作符两边的类型不同");
}
/*48additive-expression term*/
void additive_term()
{
//查找term节点
int index2=hash_funct("term");
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="term")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到term");
//创建additive-expression节点
int index=hash_funct("additive-expression");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="additive-expression")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->type=temp1->type;
temp2->name="additive-expression";
}
/*49addop +*/
/*50addop -*/
/*51addop ||*/
/*52addop |*/
/*53term term mulop unary-expression*/
void term_mulop()
{
int index1=hash_funct("unary-expression");
int index2=hash_funct("term");
//查找到additive-expression
table_var* temp=tableVar[index1];
while(temp!=NULL)
{
if(temp->name=="unary-expression")
break;
else
temp=temp->next;
}
if(temp==NULL)
p_error("无法查找到unary-expression");
//查找到term
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="term")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到term");
if(temp->type!=temp1->type)
p_error("操作符两边的类型不同");
}
/*54term unary-expression*/
void term_unarry()
{
//查找节点
int index2=hash_funct("unary-expression");
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="unary-expression")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到unary-expression");
//创建新节点
int index=hash_funct("term");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="term")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="term";
temp2->type=temp1->type;
}
/*55mulop **/
/*56mulop /*/
/*57mulop %*/
/*58mulop &&*/
/*59unary-expression unaryop unary-expression*/
void unaryexpression()
{
int index1=hash_funct("unaryop");
table_var* temp1=tableVar[index1];
while(temp1!=NULL)
{
if(temp1->name=="unaryop")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到unaryop");
int index2=hash_funct("unary-expression");
table_var* temp2=tableVar[index2];
while(temp2!=NULL)
{
if(temp2->name=="unary-expression")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
p_error("无法查找到unary-expression");
if(temp1->type!=temp2->type)
{
p_error("操作符两边的类型不匹配!");
}
}
/*60unary-expression factor*/
void unary_factor()
{
//查找节点
int index2=hash_funct("factor");
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="factor")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到factor");
//创建新节点
int index=hash_funct("unary-expression");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="unary-expression")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="unary-expression";
temp2->type=temp1->type;
}
/*61unaryop !*/
void unaryop_fist()
{
int index=hash_funct("unaryop");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="unaryop")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="unaryop";
temp2->value=(int)'!';
temp2->type="bool";
}
/*62unaryop -*/
void unaryop_secd()
{
int index=hash_funct("unaryop");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="unaryop")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="unaryop";
temp2->value=(int)'-';
temp2->type="int";
}
/*63factor ( expression )*/
void factor_expression()
{
//查找节点
int index2=hash_funct("expression");
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="expression")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到expression");
//创建新节点
int index=hash_funct("factor");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="factor")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="factor";
temp2->type=temp1->type;
}
/*64factor var*/
void factor_var()
{
//查找节点
int index2=hash_funct("var");
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="var")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到var");
//创建新节点
int index=hash_funct("factor");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="factor")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="factor";
temp2->type=temp1->type;
}
/*65factor call*/
void factor_call()
{
//查找节点
int index2=hash_funct("call");
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="call")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到call");
//创建新节点
int index=hash_funct("factor");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="factor")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="factor";
temp2->type=temp1->type;
}
/*66factor constant*/
void factor_constant()
{
//查找节点
int index2=hash_funct("constant");
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="constant")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到constant");
//创建新节点
int index=hash_funct("factor");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="factor")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="factor";
temp2->type=temp1->type;
}
/*67constant NUM*/
void constant_num()
{
int index=hash_funct("constant");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -