📄 grammer.cpp
字号:
}
GrammerNode* Grammer::Grammer_ActParamMore()
{
if(Token_curr->str_wom!=",")
return NULL;
Token_curr=Token_curr->T_next;
return Grammer_ActParamList();
}
GrammerNode* Grammer::Grammer_Exp()
{
GrammerNode* temp0=Grammer_simple_exp();
if(Token_curr->str_wom!="<"&&Token_curr->str_wom!="=")
return temp0;
GrammerNode* temp=new GrammerNode();
temp->child0=NULL;
temp->child1=NULL;
temp->child2=NULL;
temp->Sibling=NULL;
temp->name=NULL;
temp->i_idnum=0;
temp->i_Lineno=Token_curr->i_row;
temp->NK_Nodekind=ExpK;
temp->K_kind.exp=OpK;
if(Token_curr->str_wom=="<")
temp->at_attr.Exp.O_op=LT;
else
temp->at_attr.Exp.O_op=EQ;
temp->child0=temp0;
Token_curr=Token_curr->T_next;
temp->child1=Grammer_simple_exp();
return temp;
}
GrammerNode* Grammer::Grammer_simple_exp()
{
GrammerNode* temp0=Grammer_term();
if(Token_curr->str_wom!="+"&&Token_curr->str_wom!="-")
return temp0;
GrammerNode* temp=new GrammerNode();
temp->child0=NULL;
temp->child1=NULL;
temp->child2=NULL;
temp->Sibling=NULL;
temp->name=NULL;
temp->i_idnum=0;
temp->i_Lineno=Token_curr->i_row;
temp->NK_Nodekind=ExpK;
temp->K_kind.exp=OpK;
if(Token_curr->str_wom=="+")
temp->at_attr.Exp.O_op=PLUS;
else
temp->at_attr.Exp.O_op=MINUS;
temp->child0=temp0;
Token_curr=Token_curr->T_next;
temp->child1=Grammer_term();
return temp;
}
GrammerNode* Grammer::Grammer_term()
{
GrammerNode* temp0=Grammer_factor();
if(Token_curr->str_wom!="*"&&Token_curr->str_wom!="/")
return temp0;
GrammerNode* temp=new GrammerNode();
temp->child0=NULL;
temp->child1=NULL;
temp->child2=NULL;
temp->Sibling=NULL;
temp->name=NULL;
temp->i_idnum=0;
temp->i_Lineno=Token_curr->i_row;
temp->NK_Nodekind=ExpK;
temp->K_kind.exp=OpK;
if(Token_curr->str_wom=="*")
temp->at_attr.Exp.O_op=TIMES;
else
temp->at_attr.Exp.O_op=OVER;
temp->child0=temp0;
Token_curr=Token_curr->T_next;
temp->child1=Grammer_term();
return temp;
}
GrammerNode* Grammer::Grammer_factor()
{
if(Token_curr->str_wom=="integer")
{
GrammerNode* temp=new GrammerNode();
temp->child0=NULL;
temp->child1=NULL;
temp->child2=NULL;
temp->Sibling=NULL;
temp->name=NULL;
temp->i_idnum=0;
temp->i_Lineno=Token_curr->i_row;
temp->NK_Nodekind=ExpK;
temp->at_attr.Exp.val=TurnToInt(Token_curr->str_grm);
temp->K_kind.exp=ConstK;
Token_curr=Token_curr->T_next;
return temp;
}
else if(Token_curr->str_wom=="ID")
{
GrammerNode* temp=new GrammerNode();
temp->child0=NULL;
temp->child1=NULL;
temp->child2=NULL;
temp->Sibling=NULL;
temp->name=NULL;
temp->i_idnum=0;
temp->i_Lineno=Token_curr->i_row;
temp->NK_Nodekind=ExpK;
Grammer_variable(temp);
temp->K_kind.exp=IdK;
return temp;
}
else if(Token_curr->str_wom=="(")
{
Token_curr=Token_curr->T_next;
GrammerNode* temp=Grammer_Exp();
if(Token_curr->str_wom!=")")
{
i_IsBodyR--;
cout<<"第"<<Token_curr->i_row<<"行出错 factor"<<endl;
return NULL;
}
Token_curr=Token_curr->T_next;
return temp;
}
i_IsBodyR--;
cout<<"第"<<Token_curr->i_row<<"行出错 factor"<<endl;
return NULL;
}
void Grammer::Grammer_variable(GrammerNode* node_temp)
{
node_temp->name=Add_name(Token_curr->str_grm);
Token_curr=Token_curr->T_next;
if(Token_curr->str_wom=="[")
{
Token_curr=Token_curr->T_next;
node_temp->child0=Grammer_Exp();
if(Token_curr->str_wom!="]")
{
i_IsBodyR--;
cout<<"第"<<Token_curr->i_row<<"行出错 variable"<<endl;
return;
}
node_temp->at_attr.Exp.VK_varkind=ArrayMembV;
Token_curr=Token_curr->T_next;
return;
}
else if(Token_curr->str_wom==".")
{
Token_curr=Token_curr->T_next;
node_temp->child0=Grammer_fieldvar();
node_temp->at_attr.Exp.VK_varkind=FieldMembV;
return;
}
node_temp->at_attr.Exp.VK_varkind=IdV;
return;
}
GrammerNode* Grammer::Grammer_fieldvar()
{
if(Token_curr->str_wom!="ID")
{
i_IsBodyR--;
cout<<"第"<<Token_curr->i_row<<"行出错 fieldvar"<<endl;
return NULL;
}
GrammerNode* temp=new GrammerNode();
temp->child0=NULL;
temp->child1=NULL;
temp->child2=NULL;
temp->Sibling=NULL;
temp->name=NULL;
temp->i_idnum=0;
temp->i_Lineno=Token_curr->i_row;
temp->NK_Nodekind=ExpK;
temp->name=Add_name(Token_curr->str_grm);
temp->K_kind.exp=IdK;
Token_curr=Token_curr->T_next;
Grammer_fieldvarMore(temp);
return temp;
}
void Grammer::Grammer_fieldvarMore(GrammerNode* node_temp)
{
if(Token_curr->str_wom==":="||Token_curr->str_wom=="*"||Token_curr->str_wom=="/"||Token_curr->str_wom=="+"||Token_curr->str_wom=="-"||Token_curr->str_wom=="="||Token_curr->str_wom=="<"||Token_curr->str_wom=="end"||Token_curr->str_wom==";"||Token_curr->str_wom=="(")
{
node_temp->at_attr.Exp.VK_varkind=IdV;
return;
}
else if(Token_curr->str_wom=="[")
{
Token_curr=Token_curr->T_next;
node_temp->child0=Grammer_Exp();
if(Token_curr->str_wom!="]")
{
i_IsBodyR--;
cout<<"第"<<Token_curr->i_row<<"行出错 fieldvarMore"<<endl;
return;
}
node_temp->at_attr.Exp.VK_varkind=ArrayMembV;
Token_curr=Token_curr->T_next;
return;
}
i_IsBodyR--;
cout<<"第"<<Token_curr->i_row<<"行出错 fieldvarMore"<<endl;
return;
}
int Grammer::Grammer_match()
{
if(Token_curr->str_wom==".")
return 1;
cout<<"第"<<Token_curr->i_row<<"行缺少.match"<<endl;
return 0;
}
void Grammer::Grammer_out()
{
int fl=-1;
if(Gra_root!=NULL&&i_IsDecR==0&&i_IsVarR==0&&i_IsBodyR==0&&i_IsProcR==0)
Grammer_Node_out(Gra_root,fl);
}
void Grammer::Grammer_Node_out(GrammerNode* node_temp,int fl)
{
int i=0;
fl++;
switch(node_temp->NK_Nodekind)
{
case ProK:
for(i=0;i<=fl;i++)
cout<<" ";
cout<<"Proc"<<endl;
break;
case PheadK:
for(i=0;i<=fl;i++)
cout<<" ";
cout<<"PheadK"<<'\t'<<node_temp->name->str_name<<endl;
break;
case TypeK:
for(i=0;i<=fl;i++)
cout<<" ";
cout<<"TypeK"<<endl;
break;
case VarK:
for(i=0;i<=fl;i++)
cout<<" ";
cout<<"VarK"<<endl;
break;
case ProcDecK:
for(i=0;i<=fl;i++)
cout<<" ";
cout<<"ProcDecK "<<node_temp->name->str_name<<endl;
break;
case StmLK:
for(i=0;i<=fl;i++)
cout<<" ";
cout<<"StmLK"<<endl;
break;
case DecK:
for(i=0;i<=fl;i++)
cout<<" ";
Grammer_DecK_out(node_temp);
break;
case StmtK:
for(i=0;i<=fl;i++)
cout<<" ";
Grammer_StmtK_out(node_temp);
break;
case ExpK:
for(i=0;i<=fl;i++)
cout<<" ";
Grammer_ExpK_out(node_temp);
break;
default:break;
}
if(node_temp->child0!=NULL)
Grammer_Node_out(node_temp->child0,fl);
if(node_temp->child1!=NULL)
Grammer_Node_out(node_temp->child1,fl);
if(node_temp->child2!=NULL)
Grammer_Node_out(node_temp->child2,fl);
if(node_temp->Sibling!=NULL)
Grammer_Node_out(node_temp->Sibling,--fl);
}
void Grammer::Grammer_DecK_out(GrammerNode* node_temp)
{
cout<<"DecK"<<" ";
if(node_temp->at_attr.Proc.PT_param==valparamtype)
cout<<"value param: ";
else if(node_temp->at_attr.Proc.PT_param==varparamtype)
cout<<"var param: ";
Grammer_kind_dec(node_temp);
cout<<node_temp->name->str_name;
NameNode* temp=node_temp->name;
while(temp->NN_next!=NULL)
{
temp=temp->NN_next;
cout<<" "<<temp->str_name;
}
cout<<endl;
}
void Grammer::Grammer_StmtK_out(GrammerNode* node_temp)
{
cout<<"StmtK"<<" ";
Grammer_kind_stmt(node_temp);
if(node_temp->name!=NULL)
cout<<node_temp->name->str_name;
cout<<endl;
}
void Grammer::Grammer_ExpK_out(GrammerNode* node_temp)
{
cout<<"ExpK"<<" ";
Grammer_kind_exp(node_temp);
cout<<endl;
}
void Grammer::Grammer_kind_dec(GrammerNode* node_temp)
{
switch(node_temp->K_kind.dec)
{
case ArrayK:
cout<<"ArrayK"<<" ";
break;
case CharK:
cout<<"CharK"<<" ";
break;
case IntegerK:
cout<<"Integerk"<<" ";
break;
case RecordK:
cout<<"RecordK"<<" ";
break;
case IdK:
cout<<"IdK"<<" ";
break;
default:
cout<<"第"<<node_temp->i_Lineno<<"出错";
break;
}
}
void Grammer::Grammer_kind_stmt(GrammerNode* node_temp)
{
switch(node_temp->K_kind.stmt)
{
case IfK:
cout<<"IfK"<<" ";
break;
case WhileK:
cout<<"WhileK"<<" ";
break;
case AssignK:
cout<<"Assignk"<<" ";
break;
case ReadK:
cout<<"ReadK"<<" ";
break;
case WriteK:
cout<<"WriteK"<<" ";
break;
case CallK:
cout<<"CallK"<<" ";
break;
case ReturnK:
cout<<"ReturnK"<<" ";
break;
default:
cout<<"第"<<node_temp->i_Lineno<<"出错";
break;
}
}
void Grammer::Grammer_kind_exp(GrammerNode* node_temp)
{
switch(node_temp->K_kind.exp)
{
case OpK:
cout<<"OpK"<<" ";
switch(node_temp->at_attr.Exp.O_op)
{
case LT:
cout<<"<"<<" ";
break;
case EQ:
cout<<"="<<" ";
break;
case PLUS:
cout<<"+"<<" ";
break;
case MINUS:
cout<<"-"<<" ";
break;
case TIMES:
cout<<"*"<<" ";
break;
case OVER:
cout<<"/"<<" ";
break;
default:
cout<<"第"<<node_temp->i_Lineno<<"行出错";
break;
}
break;
case ConstK:
cout<<"ConstK"<<" "<<node_temp->at_attr.Exp.val;
break;
case IdK:
cout<<node_temp->name->str_name<<" ";
switch(node_temp->at_attr.Exp.VK_varkind)
{
case IdV:
cout<<"IdV";
break;
case ArrayMembV:
cout<<"ArrayMembV";
break;
case FieldMembV:
cout<<"FieldMembV";
break;
default:
cout<<"第"<<node_temp->i_Lineno<<"行出错";
break;
}
break;
default:
cout<<"第"<<node_temp->i_Lineno<<"行出错";
break;
}
}
void Grammer::Grammer_delete(GrammerNode* node_temp)
{
if(node_temp==NULL)
return;
Grammer_delete(node_temp->child0);
Grammer_delete(node_temp->child1);
Grammer_delete(node_temp->child2);
Grammer_delete(node_temp->Sibling);
delete node_temp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -