📄 compiler1.cpp
字号:
}
switch (GetCurSym()->type)
{
case 35://:
break;
default:
m_nErrorNum=19;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
switch (GetCurSym()->type)
{
case 14://integer
case 15://real
break;
default:
m_nErrorNum=12;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
return TRUE;
}
int CCompiler::p_YuJuChuan()
{
if (!p_YuJu()) return FALSE;
while (GetCurSym()->type==30)//;
{
NextSym();
if (!p_YuJu()) return FALSE;
}
return TRUE;
}
int CCompiler::p_YuJu()
{
switch (GetCurSym()->type)
{
case 6://begin
if (!p_FuHeYuJu()) return FALSE;
break;
case 1://id
if (!p_FuZhiYuJu()) return FALSE;
break;
case 8://if
if (!p_TiaoJianYuJu()) return FALSE;
break;
case 11://while
if (!p_XunHuanYuJu()) return FALSE;
break;
case 13://call
if (!p_GuoChengYuJu()) return FALSE;
break;
default:
m_nErrorNum=9;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
return TRUE;
}
int CCompiler::p_FuHeYuJu()//复合语句
{
switch (GetCurSym()->type)
{
case 6://begin
break;
default:
m_nErrorNum=8;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
if (!p_YuJuChuan()) return FALSE;
switch (GetCurSym()->type)
{
case 7://end
break;
default:
m_nErrorNum=10;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
return TRUE;
}
int CCompiler::p_FuZhiYuJu()
{
switch (GetCurSym()->type)
{
case 1://id
break;
default:
m_nErrorNum=20;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
switch (GetCurSym()->type)
{
case 29://:=
break;
default:
m_nErrorNum=21;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
if (!p_BiaoDaShi()) return FALSE;
return TRUE;
}
int CCompiler::p_TiaoJianYuJu()
{
switch (GetCurSym()->type)
{
case 8://if
break;
default:
m_nErrorNum=22;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
if (!p_BuErBiaoDaShi()) return FALSE;
switch (GetCurSym()->type)
{
case 9://then
break;
default:
m_nErrorNum=23;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
if (!p_YuJu()) return FALSE;
switch (GetCurSym()->type)
{
case 10://else
break;
default:
return TRUE;
}
NextSym();
if (!p_YuJu()) return FALSE;
return TRUE;
}
int CCompiler::p_XunHuanYuJu()
{
switch (GetCurSym()->type)
{
case 11://while
break;
default:
m_nErrorNum=24;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
if (!p_BuErBiaoDaShi()) return FALSE;
switch (GetCurSym()->type)
{
case 12://do
break;
default:
m_nErrorNum=25;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
if (!p_YuJu()) return FALSE;
return TRUE;
}
int CCompiler::p_GuoChengYuJu()
{
switch (GetCurSym()->type)
{
case 13://call
break;
default:
m_nErrorNum=26;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
switch (GetCurSym()->type)
{
case 1://id
break;
default:
m_nErrorNum=27;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
switch (GetCurSym()->type)
{
case 33://(
break;
default:
return TRUE;
}
if (!p_ShiChanBiao()) return FALSE;
return TRUE;
}
int CCompiler::p_ShiChanBiao()
{
switch (GetCurSym()->type)
{
case 33://(
break;
default:
m_nErrorNum=16;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
if (!p_BiaoDaShi()) return FALSE;
while (GetCurSym()->type==32)//,
{
NextSym();
if (!p_BiaoDaShi()) return FALSE;
}
switch (GetCurSym()->type)
{
case 34://)
break;
default:
m_nErrorNum=17;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
return TRUE;
}
int CCompiler::p_BiaoDaShi()
{
if (!p_Xiang()) return FALSE;
while (GetCurSym()->type==16)//+
{
NextSym();
if (!p_Xiang()) return FALSE;
}
return TRUE;
}
int CCompiler::p_Xiang()
{
if (!p_YingZhi()) return FALSE;
while (GetCurSym()->type==18)//*
{
NextSym();
if (!p_YingZhi()) return FALSE;
}
return TRUE;
}
int CCompiler::p_YingZhi()
{
switch (GetCurSym()->type)
{
case 1://id
NextSym();
break;
case 2://num
NextSym();
break;
case 33://(
NextSym();
if (!p_BiaoDaShi()) return FALSE;
switch (GetCurSym()->type)
{
case 34://)
break;
default:
m_nErrorNum=17;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
break;
default:
m_nErrorNum=28;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
return TRUE;
}
int CCompiler::p_BuErBiaoDaShi()
{
switch (GetCurSym()->type)
{
case 20://~
NextSym();
if (!p_BuErBiaoDaShi()) return FALSE;
break;
case 33://(
case 1://id
case 2://num
if (!p_GuanXiBiaoDaShi()) return FALSE;
while (GetCurSym()->type==21 || GetCurSym()->type==22)
{
NextSym();
if (!p_BuErBiaoDaShi()) return FALSE;
}
break;
default:
m_nErrorNum=29;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
return TRUE;
}
int CCompiler::p_GuanXiBiaoDaShi()
{
if (!p_BiaoDaShi()) return FALSE;
if (!p_GuanXi()) return FALSE;
if (!p_BiaoDaShi()) return FALSE;
return TRUE;
}
int CCompiler::p_GuanXi()
{
switch (GetCurSym()->type)
{
case 23://<
case 24://<=
case 25://>
case 26://>=
case 27://=
case 28://<>
break;
default:
m_nErrorNum=30;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
return TRUE;
}
void CCompiler::Initial()
{
::strcpy(m_QuadOpCodeTable[0], "_End_");
::strcpy(m_QuadOpCodeTable[1], "in");
::strcpy(m_QuadOpCodeTable[2], "call");
::strcpy(m_QuadOpCodeTable[3], "out");
::strcpy(m_QuadOpCodeTable[4], "ret");
::strcpy(m_QuadOpCodeTable[5], ":=I");
::strcpy(m_QuadOpCodeTable[6], ":=R");
::strcpy(m_QuadOpCodeTable[7], "par");
::strcpy(m_QuadOpCodeTable[8], "valueI");
::strcpy(m_QuadOpCodeTable[9], "valueR");
::strcpy(m_QuadOpCodeTable[10],"addr");
::strcpy(m_QuadOpCodeTable[11],"j");
::strcpy(m_QuadOpCodeTable[12],"j<");
::strcpy(m_QuadOpCodeTable[13],"j<=");
::strcpy(m_QuadOpCodeTable[14],"j>");
::strcpy(m_QuadOpCodeTable[15],"j>=");
::strcpy(m_QuadOpCodeTable[16],"j=");
::strcpy(m_QuadOpCodeTable[17],"j<>");
::strcpy(m_QuadOpCodeTable[18],"`");
::strcpy(m_QuadOpCodeTable[19],"+I");
::strcpy(m_QuadOpCodeTable[20],"+R");
::strcpy(m_QuadOpCodeTable[21],"*I");
::strcpy(m_QuadOpCodeTable[22],"*R");
::strcpy(m_QuadOpCodeTable[23],"`");
::strcpy(m_QuadOpCodeTable[24],"`");
::strcpy(m_QuadOpCodeTable[25],"`");
::strcpy(m_QuadOpCodeTable[26],"`");
::strcpy(m_QuadOpCodeTable[27],"`");
::strcpy(m_QuadOpCodeTable[28],"`");
::strcpy(m_QuadOpCodeTable[29],"_Error_");
//::strcpy(m_tKeyWord[0],"");
//::strcpy(m_tKeyWord[1],"and");//标识符
//::strcpy(m_tKeyWord[2],"arrary");//正整数
//::strcpy(m_tKeyWord[3],"begin");
//::strcpy(m_tKeyWord[4],"bool");
// ::strcpy(m_tKeyWord[5],"call");
//::strcpy(m_tKeyWord[6],"case");
//::strcpy(m_tKeyWord[7],"char");
//::strcpy(m_tKeyWord[8],"constant");
// ::strcpy(m_tKeyWord[9],"do");
// ::strcpy(m_tKeyWord[10],"else");
// ::strcpy(m_tKeyWord[11],"end");
// ::strcpy(m_tKeyWord[12],"false");
// ::strcpy(m_tKeyWord[13],"for");
// ::strcpy(m_tKeyWord[14],"if");
// ::strcpy(m_tKeyWord[15],"input");
// ::strcpy(m_tKeyWord[16],"integer");
// ::strcpy(m_tKeyWord[17],"not");
// ::strcpy(m_tKeyWord[18],"of");
// ::strcpy(m_tKeyWord[19],"or");
// ::strcpy(m_tKeyWord[20],"output");
// ::strcpy(m_tKeyWord[21],"procedure");
// ::strcpy(m_tKeyWord[22],"program");
// ::strcpy(m_tKeyWord[23],"read");
// ::strcpy(m_tKeyWord[24],"real");
// ::strcpy(m_tKeyWord[25],"repeat");
// ::strcpy(m_tKeyWord[26],"set");
// ::strcpy(m_tKeyWord[27],"then");
// ::strcpy(m_tKeyWord[28],"to");
// ::strcpy(m_tKeyWord[29],"true");
// ::strcpy(m_tKeyWord[30],"until");
// ::strcpy(m_tKeyWord[31],"var");
// ::strcpy(m_tKeyWord[32],"while");
// ::strcpy(m_tKeyWord[33],"write");
// ::strcpy(m_tKeyWord[34],"");
// ::strcpy(m_tKeyWord[35],"");
// ::strcpy(m_tKeyWord[36],"");
// ::strcpy(m_tKeyWord[37],"");
// ::strcpy(m_tKeyWord[38],"'");
// ::strcpy(m_tKeyWord[39],"(");
// ::strcpy(m_tKeyWord[40],")");
// ::strcpy(m_tKeyWord[41],"*");
// ::strcpy(m_tKeyWord[42],"*/");
// ::strcpy(m_tKeyWord[43],"+");
// ::strcpy(m_tKeyWord[44],",");
// ::strcpy(m_tKeyWord[45],"-");
// ::strcpy(m_tKeyWord[46],".");
// ::strcpy(m_tKeyWord[47],"..");
// ::strcpy(m_tKeyWord[48],"/");
// ::strcpy(m_tKeyWord[49],"/*");
// ::strcpy(m_tKeyWord[50],":");
// ::strcpy(m_tKeyWord[51],":=");
// ::strcpy(m_tKeyWord[52],";");
// ::strcpy(m_tKeyWord[53],"<");
// ::strcpy(m_tKeyWord[54],"<=");
// ::strcpy(m_tKeyWord[55],"<>");
// ::strcpy(m_tKeyWord[56],"=");
// ::strcpy(m_tKeyWord[57],">");
// ::strcpy(m_tKeyWord[58],">=");
// ::strcpy(m_tKeyWord[59],"[");
// ::strcpy(m_tKeyWord[60],"]");
::strcpy(m_tKeyWord[0],"");
::strcpy(m_tKeyWord[1],"");//标识符
::strcpy(m_tKeyWord[2],"");//正整数
::strcpy(m_tKeyWord[3],"program");
::strcpy(m_tKeyWord[4],"var");
::strcpy(m_tKeyWord[5],"procedure");
::strcpy(m_tKeyWord[6],"begin");
::strcpy(m_tKeyWord[7],"end");
::strcpy(m_tKeyWord[8],"if");
::strcpy(m_tKeyWord[9],"then");
::strcpy(m_tKeyWord[10],"else");
::strcpy(m_tKeyWord[11],"while");
::strcpy(m_tKeyWord[12],"do");
::strcpy(m_tKeyWord[13],"call");
::strcpy(m_tKeyWord[14],"integer");
::strcpy(m_tKeyWord[15],"real");
::strcpy(m_tKeyWord[16],"+");
::strcpy(m_tKeyWord[17],"-");
::strcpy(m_tKeyWord[18],"*");
::strcpy(m_tKeyWord[19],"/");
::strcpy(m_tKeyWord[20],"~");
::strcpy(m_tKeyWord[21],"&");
::strcpy(m_tKeyWord[22],"|");
::strcpy(m_tKeyWord[23],"<");
::strcpy(m_tKeyWord[24],"<=");
::strcpy(m_tKeyWord[25],">");
::strcpy(m_tKeyWord[26],">=");
::strcpy(m_tKeyWord[27],"=");
::strcpy(m_tKeyWord[28],"<>");
::strcpy(m_tKeyWord[29],":=");
::strcpy(m_tKeyWord[30],";");
::strcpy(m_tKeyWord[31],".");
::strcpy(m_tKeyWord[32],",");
::strcpy(m_tKeyWord[33],"(");
::strcpy(m_tKeyWord[34],")");
::strcpy(m_tKeyWord[35],":");
::strcpy(m_tKeyWord[36],"/*");
::strcpy(m_tKeyWord[37],"*/");
::strcpy(m_tKeyWord[38],"");
::strcpy(m_tKeyWord[39],"");
::strcpy(m_tKeyWord[40],"");
::strcpy(m_tKeyWord[41],"");
::strcpy(m_tKeyWord[42],"");
::strcpy(m_tKeyWord[43],"");
::strcpy(m_tKeyWord[44],"");
::strcpy(m_tKeyWord[45],"");
::strcpy(m_tKeyWord[46],"");
::strcpy(m_tKeyWord[47],"");
::strcpy(m_tKeyWord[48],"");
::strcpy(m_tKeyWord[49],"");
::strcpy(m_tErrorMessage[0],"正确!");
::strcpy(m_tErrorMessage[1],"数字位数过长!");
::strcpy(m_tErrorMessage[2],"非法字符!");
::strcpy(m_tErrorMessage[3],"缺少保留字“ program ”!");
::strcpy(m_tErrorMessage[4],"program 后缺少标识符!");
::strcpy(m_tErrorMessage[5],"缺少“ ; ”符号!");
::strcpy(m_tErrorMessage[6],"缺少程序结束符“ . ”符号!");
::strcpy(m_tErrorMessage[7],"缺少《程序体》,应为 begin,integer,real,procedure");
::strcpy(m_tErrorMessage[8],"缺少保留字“ begin ”!");
::strcpy(m_tErrorMessage[9],"缺少《语句》,应为 begin,ID,if,while,call");
::strcpy(m_tErrorMessage[10],"缺少保留字“ end ”!");
::strcpy(m_tErrorMessage[11],"缺少《说明》,应为 integer,real,procedure");
::strcpy(m_tErrorMessage[12],"缺少保留字 integer 或 real!");
::strcpy(m_tErrorMessage[13],"procedure 后缺少标识符!");
::strcpy(m_tErrorMessage[14],"简单变量说明后缺少标识符!");
::strcpy(m_tErrorMessage[15],"缺少保留字“ procedure ”!");
::strcpy(m_tErrorMessage[16],"缺少 “ ( ”符号!");
::strcpy(m_tErrorMessage[17],"缺少 “ ) ”符号!");
::strcpy(m_tErrorMessage[18],"缺少形参标识符 !");
::strcpy(m_tErrorMessage[19],"缺少 “ : ”符号!");
::strcpy(m_tErrorMessage[20],"缺少标识符!");
::strcpy(m_tErrorMessage[21],"缺少赋值符号“ := ”!");
::strcpy(m_tErrorMessage[22],"缺少保留字“ if ”!");
::strcpy(m_tErrorMessage[23],"缺少保留字“ then ”!");
::strcpy(m_tErrorMessage[24],"缺少保留字“ while ”!");
::strcpy(m_tErrorMessage[25],"缺少保留字“ do ”!");
::strcpy(m_tErrorMessage[26],"缺少保留字“ call ”!");
::strcpy(m_tErrorMessage[27],"缺少被调过程名标识符!");
::strcpy(m_tErrorMessage[28],"缺少《因子》,应为 (,ID,NUMBER");
::strcpy(m_tErrorMessage[29],"缺少《布尔表达式》,应为 ~,(,ID,NUMBER");
::strcpy(m_tErrorMessage[30],"缺少《关系》!");
::strcpy(m_tErrorMessage[31],"变量名不能和过程名相同!");
::strcpy(m_tErrorMessage[32],"标识符重复定义!");
::strcpy(m_tErrorMessage[33],"未定义标识符!");
::strcpy(m_tErrorMessage[34],"不能直接引用过程名!");
::strcpy(m_tErrorMessage[35],"不能从real转换为integer类型!");
::strcpy(m_tErrorMessage[36],"不能用常数作实参!");
::strcpy(m_tErrorMessage[37],"变参应为变量!");
::strcpy(m_tErrorMessage[38],"实参个数不足!");
::strcpy(m_tErrorMessage[39],"只有integer和integer才能比较!");
::strcpy(m_tErrorMessage[40],"不能这样调用过程!");
::strcpy(m_tErrorMessage[41],"");
::strcpy(m_tErrorMessage[42],"");
::strcpy(m_tErrorMessage[43],"");
::strcpy(m_tErrorMessage[96],"源程序结束符 end. 后还有多余单词!");
::strcpy(m_tErrorMessage[97],"语法错误太多,终止语法分析!");
::strcpy(m_tErrorMessage[98],"源程序不正常结束!");
::strcpy(m_tErrorMessage[99],"内存不足!词法分析终止!");
}
BOOL CCompiler::isalpha(char a)
{
return ((a>='a'&&a<='z')||(a>='A'&&a<='Z'));
}
BOOL CCompiler::isalnum(char a)
{
return ((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9'));
}
BOOL CCompiler::isdigit(char a)
{
return (a>='0'&&a<='9');
}
CifaResult * CCompiler::GetCurSym()
{
return m_tCifa[m_pCurSym];
}
void CCompiler::NextSym()
{
m_pCurSym++;
}
CifaResult * CCompiler::GetSym()
{
return m_tCifa[m_pCurSym++];
}
CifaResult * CCompiler::GetNextSym()
{
return m_tCifa[m_pCurSym+1];
}
void CCompiler::YuYiFengXi()
{
if (m_ntCifaLen==0) return;//Cifa not do
yuyivar.currbl=0;
yuyivar.lasent=0;
yuyivar.lastbl=0;
yuyivar.nxq=0;
yuyivar.tempp=0;
gen("",0,0,0,0,0,0);//idtab start from 1
addid("",0,0,0);//idtab start from 1
//temptab and leveltab start from 0
m_pCurSym=0;//* <--
r_ChengXu();
gen("_End_",0,0,0,0,0,0);//end note
m_Interface.ErrorAddress=m_nErrorAddress;
m_Interface.ErrorNumber=m_nErrorNum;
return;
}
int CCompiler::r_ChengXu()
{
switch (GetCurSym()->type)
{
case 3://program
break;
default:
m_nErrorNum=3;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
switch (GetCurSym()->type)
{
case 1://id
break;
default:
m_nErrorNum=4;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
//yy
leveltab[yuyivar.currbl].lnp=yuyivar.nxq+1;
leveltab[yuyivar.currbl].lnum=0;
::strcpy(leveltab[yuyivar.currbl].name,GetCurSym()->text);
leveltab[yuyivar.currbl].outern=0;
leveltab[yuyivar.currbl].parnum=0;
leveltab[yuyivar.currbl].pointer=yuyivar.lasent;
leveltab[yuyivar.currbl].recl=5;
leveltab[yuyivar.currbl].varnum=0;
leveltab[yuyivar.currbl].temp=yuyivar.tempp;
yuyivar.lastbl++;
gen("call",1,0,1,0,2,2);
int link=yuyivar.nxq;
gen("in",0,0,0,0,0,0);
//yy end
NextSym();
switch (GetCurSym()->type)
{
case 30://;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -