📄 semantic.cpp
字号:
pFSAG1->des=this->GetIDName(pToken);
GetNextToken(token);
const char *str2=this->m_pWordCode[token->keycode];
if (strcmp(str2,"<")==0 ||
strcmp(str2,"<=")==0 ||
strcmp(str2,">")==0 ||
strcmp(str2,">=")==0 ||
strcmp(str2,"=")==0 ||
strcmp(str2,"<>")==0)
{
GetNextToken(token);
if (strcmp(this->m_pWordCode[token->keycode],"identifier")==0)
{
PFSAG pFSAG2=new FSAG;
pFSAG2->type=PT;
pFSAG2->value.token=token;
pFSAG2->des=this->GetIDName(token);
pTC=this->GetCurPFS();
Gencode(this->GetOP(str2),pFSAG1,pFSAG2,NULL);
pFC=this->GetCurPFS();
Gencode(GetOP("j"),NULL,NULL,NULL);
}
else
{
this->AddError("条件表达式错误!");
this->NoUseCurToken();
}
}
else
{
this->NoUseCurToken();
pTC=this->GetCurPFS();
Gencode(GetOP("jnz"),pFSAG1,NULL,NULL);
pFC=this->GetCurPFS();
Gencode(GetOP("j"),NULL,NULL,NULL);
}
}
else
{
this->AddError("条件表达式错误!");
this->NoUseCurToken();
}
}
/**
* 根据操作符返回操作类型
*
* @param char *strOp 操作符
*
*/
EOP CSemantic::GetOP(const char *strOp)
{
EOP op;
if (strcmp(strOp,"j")==0) op=OPJump;
else if (strcmp(strOp,">")==0) op=OPJumpLarger;
else if (strcmp(strOp,">=")==0) op=OPJumpLE;
else if (strcmp(strOp,"jnz")==0) op=OPJumpNZero;
else if (strcmp(strOp,"j=")==0) op=OPJumpEqual;
else if (strcmp(strOp,"j<>")==0) op=OPJumpUEqual;
else if (strcmp(strOp,"j<")==0) op=OPJumpSmaller;
else if (strcmp(strOp,"j<=")==0) op=OPjumpSE;
else if (strcmp(strOp,">")==0) op=OPLarger;
else if (strcmp(strOp,"<")==0) op=OPSmaller;
else if (strcmp(strOp,"=")==0) op=OPEqual;
else if (strcmp(strOp,":=")==0) op=OPEvaluate;
else if (strcmp(strOp,"<>")==0) op=OPUnequal;
else if (strcmp(strOp,"+")==0) op=OPPlus;
else if (strcmp(strOp,"-")==0) op=OPMinus;
else if (strcmp(strOp,"*")==0) op=OPMultiply;
else if (strcmp(strOp,"/")==0) op=OPDivide;
else if (strcmp(strOp,"program")==0) op=OPProgram;
else op=OPSys;
return op;
}
/**
* 根据操作类型返回操作符
*
* @param EOP op 操作符类型
*
*/
char *CSemantic::GetOPStr(EOP op)
{
switch(op)
{
case OPJump://无条件跳转
return (new char[]="j");
case OPJumpLarger://大于时跳转
return (new char[]="j>");
case OPJumpLE://大于等于时跳转
return (new char[]="j>=");
case OPJumpNZero://不等于零时跳转
return (new char[]="jnz");
case OPJumpEqual://等于时跳转
return (new char[]="j=");
case OPJumpUEqual://不等于时跳转
return (new char[]="j<>");
case OPJumpSmaller://小于时跳转
return (new char[]="j<");
case OPjumpSE://小于等于时跳转
return (new char[]="j<=");
case OPLarger://大于
return (new char[]=">");
case OPSmaller://小于
return (new char[]="<");
case OPEqual://等于
return (new char[]="=");
case OPEvaluate://赋值
return (new char[]=":=");
case OPUnequal://不等于
return (new char[]="<>");
case OPPlus://加
return (new char[]="+");
case OPMinus://减
return (new char[]="-");
case OPMultiply://乘
return (new char[]="*");
case OPDivide://除
return (new char[]="/");
case OPProgram://程序开始标识
return (new char[]="program");
case OPSys://程序结束标识
return (new char[]="sys");
}
return NULL;
}
/**
* 处理变量说明语句
*
* @param PTokenNode pToken token字
*
*/
void CSemantic::Varst(PTokenNode pToken)
{
PTokenNode stckToken[50];
int top=0;
const char *str=this->m_pWordCode[pToken->keycode];
if (strcmp(this->m_pWordCode[pToken->keycode],"identifier")!=0)
{
this->AddError("var后面没有变量名!");
this->NoUseCurToken();
}
for (int i=0;i<50;i++) stckToken[i]=NULL;
Ids(pToken,stckToken,top);
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],":")!=0)
{
this->AddError("期待:!");
this->NoUseCurToken();
}
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],"integer")!=0 &&
strcmp(this->m_pWordCode[pToken->keycode],"bool")!=0 &&
strcmp(this->m_pWordCode[pToken->keycode],"char")!=0 &&
strcmp(this->m_pWordCode[pToken->keycode],"real")!=0)
{
this->AddError("非法类型说明!");
this->NoUseCurToken();
}
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],";")!=0)
{
this->AddError("类型说明后面无分号!");
this->NoUseCurToken();
}
for (i=0;i<top && stckToken[i]!=NULL;i++)
{
this->FillTokenType(stckToken[i],this->m_pWordCode[stckToken[i]->keycode]);
}
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],"begin")!=0 &&
strcmp(this->m_pWordCode[pToken->keycode],"identifier")!=0)
{
this->AddError("变量说明中无变量或未后随begin!");
this->NoUseCurToken();
}
if (strcmp(this->m_pWordCode[pToken->keycode],"identifier")==0) Varst(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],"begin")==0) this->NoUseCurToken();
}
/**
* 把var语句中的变量表收集到stckToken中暂存,并回送给Varst过程
*
* @param PTokenNode pToken token字
* @param PTokenNode stckToken[] 变量栈
* @param int &top 变量栈顶指针
*
*/
void CSemantic::Ids(PTokenNode pToken,PTokenNode stckToken[],int &top)
{
stckToken[top++]=pToken;
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],",")!=0 &&
strcmp(this->m_pWordCode[pToken->keycode],":")!=0)
{
this->AddError("var语句中缺少“,”或“:”,已插入!");
this->NoUseCurToken();
}
else if (strcmp(this->m_pWordCode[pToken->keycode],",")==0)
{
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],"identifier")!=0)
{
this->AddError("var语句中缺少变量!");
this->NoUseCurToken();
return;
}
Ids(pToken,stckToken,top);
}
else
{
this->NoUseCurToken();
}
}
/**
* 处理复合语句中的语句串,即匹配产生式<STL>-><S>;<STL>|e,遇到end时返回
*
* @param PTokenNode pToken token字
*
*/
void CSemantic::Stl(PTokenNode pToken)
{
PFSymbol SChain=NULL;
const char *str=this->m_pWordCode[pToken->keycode];
if (strcmp(str,"identifier")==0 ||
strcmp(str,"if")==0 ||
strcmp(str,"for")==0 ||
strcmp(str,"begin")==0 ||
strcmp(str,"while")==0 ||
strcmp(str,"repeat")==0)
{
SChain=Stsort(pToken);
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],";")!=0)
{
this->AddError("插入分号!");
this->NoUseCurToken();
}
BackPatch(SChain,GetCurPFS());
GetNextToken(pToken);
Stl(pToken);
}
else if (strcmp(str,"end")!=0)
{
this->AddError("非法句首符或缺end!");
this->NoUseCurToken();
}
}
/**
* 处理赋值语句及散转处理各控制语句
*
* @param PTokenNode pToken token字
*
* @return PFSymbol 返回S·CHAIN
*
*/
PFSymbol CSemantic::Stsort(PTokenNode pToken)
{
const char *str=this->m_pWordCode[pToken->keycode];
PFSymbol SChain=NULL;
PFSAG pFSAG=NULL,result=NULL;
if (strcmp(str,"identifier")==0)
{
if(this->m_pSymbolTable->sSubTable[pToken->strId-1].type == NULL)
{
this->AddError("符号/变量 未定义!");
}
result=new FSAG;
result->des=this->GetIDName(pToken);
result->type=PT;
result->value.token=pToken;
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],":=")!=0)
{
this->AddError("缺少赋值符号!");
this->NoUseCurToken();
}
GetNextToken(pToken);
Gencode(this->GetOP(":="),this->Ae(pToken),NULL,result);
SChain=NULL;
}
else if (strcmp(str,"for")==0)
{
this->AddError("暂时无法识别for语句块!");
SChain=NULL;
}
else if (strcmp(str,"if")==0)
{
SChain=Ifs();
}
else if (strcmp(str,"while")==0)
{
this->AddError("暂时无法识别while语句块!");
SChain=NULL;
}
else if (strcmp(str,"repeat")==0)
{
this->AddError("暂时无法识别repeat语句块!");
SChain=NULL;
}
else if (strcmp(str,"begin")==0)
{
GetNextToken(pToken);
Stl(pToken);
SChain=NULL;
}
else
{
this->AddError("非法句首符!");
this->NoUseCurToken();
}
return SChain;
}
/**
* 处理if语句
*
* @return PFSymbol 返回S·CHAIN
*
*/
PFSymbol CSemantic::Ifs()
{
PTokenNode pToken=NULL;
PFSymbol eTC=NULL,eFC=NULL,SChain1=NULL,SChain2=NULL,SChain=NULL,q=NULL,TChain=NULL;
GetNextToken(pToken);
Be(pToken,eTC,eFC);
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],"then")!=0)
{
this->AddError("缺then!");
this->NoUseCurToken();
}
BackPatch(eTC,GetCurPFS());
GetNextToken(pToken);
SChain1=Stsort(pToken);
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],";")!=0)
{
this->AddError("缺少分号,已插入!");
this->NoUseCurToken();
}
GetNextToken(pToken);
if (strcmp(this->m_pWordCode[pToken->keycode],"else")==0)
{
q=GetCurPFS();
Gencode(GetOP("j"),NULL,NULL,NULL);
BackPatch(eFC,GetCurPFS());
TChain=Merg(SChain1,q);
GetNextToken(pToken);
SChain2=Stsort(pToken);
SChain=Merg(TChain,SChain2);
return SChain;
}
else
{
return Merg(SChain1,eFC);
}
}
/**
* SIMPLE语法制导翻译程序主控模块。
* 说明翻译用的全称量,文件管理,处理SYNDITER开头与结束工作,直到翻译PROGRAM产生式
*
*/
void CSemantic::Semantic()
{
PTokenNode pToken=NULL;
PFSAG pFSAG=NULL;
const char *str;
InitPFSymbol();
GetNextToken(pToken);
str=this->m_pWordCode[pToken->keycode];
if (strcmp(str,"program")!=0)
{
this->NoUseCurToken();
this->AddError("程序不以program开头!");
this->m_pMorpheme->CloseSource();
return;
}
GetNextToken(pToken);
str=this->m_pWordCode[pToken->keycode];
if (strcmp(str,"identifier")!=0)
{
this->NoUseCurToken();
this->AddError("缺程序名标示符!");
this->m_pMorpheme->CloseSource();
return;
}
//FILL(ENTRY(TOKEN),'program');
pFSAG=new FSAG;
pFSAG->des=this->GetIDName(pToken);
pFSAG->type=PT;
pFSAG->value.token=pToken;
Gencode(this->GetOP("program"),pFSAG,NULL,NULL);
GetNextToken(pToken);
str=this->m_pWordCode[pToken->keycode];
if (strcmp(str,";")!=0)
{
this->NoUseCurToken();
this->AddError("缺分号,已插入!");
}
//开始处理var语句
GetNextToken(pToken);
str=this->m_pWordCode[pToken->keycode];
if (strcmp(str,"var")!=0)
{
this->NoUseCurToken();
this->AddError("缺var!");
this->m_pMorpheme->CloseSource();
return;
}
GetNextToken(pToken);
this->Varst(pToken);
//处理各可执行语句
GetNextToken(pToken);
str=this->m_pWordCode[pToken->keycode];
if (strcmp(str,"begin")!=0)
{
this->NoUseCurToken();
this->AddError("缺begin!");
this->m_pMorpheme->CloseSource();
return;
}
this->Stl(pToken);
GetNextToken(pToken);
str=this->m_pWordCode[pToken->keycode];
if (strcmp(str,";")!=0)
{
this->NoUseCurToken();
this->AddError("缺程序结束符,已插入!");
}
Gencode(GetOP("sys"),NULL,NULL,NULL);
this->m_pMorpheme->CloseSource();
}
/**
* 回填pToken类型
*
* @param PTokenNode pToken token字
* @param const char *type 类型字符串
*
*/
void CSemantic::FillTokenType(PTokenNode pToken,const char *type)
{
if (pToken==NULL || pToken->strId==0)
{
return;
}
if (strcmp(type,"constint")==0)
{
this->m_pSymbolTable->sSubTable[pToken->strId-1].type=Integer;
}
else if (strcmp(type,"constfloat")==0)
{
this->m_pSymbolTable->sSubTable[pToken->strId-1].type=Real;
}
else if (strcmp(type,"constchar")==0)
{
this->m_pSymbolTable->sSubTable[pToken->strId-1].type=Char;
}
else if (strcmp(type,"bool")==0)
{
this->m_pSymbolTable->sSubTable[pToken->strId-1].type=Bool;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -