📄 cifafenxi.cpp
字号:
// Compiler.cpp: implementation of the CCompiler class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "compiler.h"
#include "cifafenxi.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCompiler::CCompiler()
{
m_ntCifaLen=0;
m_aSourse[0]=0;
m_Interface.SourseBuffer=m_aSourse;
m_Interface.LexicalDuad=m_tCifa;
m_Interface.ErrorMessageTable=m_tErrorMessage;
}
CCompiler::~CCompiler()
{
}
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],"");//标识符
::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],"内存不足!词法分析终止!");
*/
}
char CCompiler::GetCurCh()//get ch and point does not ++
{
return m_aSourse[m_pCurCh];
}
void CCompiler::NextCh()//point ++
{
m_pCurCh++;
}
char CCompiler::GetCh()//get ch and point ++
{
return m_aSourse[m_pCurCh++];
}
char CCompiler::GetNextCh()//get next ch and point does not ++
{
if (m_aSourse[m_pCurCh]==0) return 0;
return m_aSourse[m_pCurCh+1];
}
void CCompiler::CiFaFengXi()
{
BOOL flag=FALSE;
char token[MAX_LEX_LEN];
int k,v;
Initial();
for (int i=0;i<m_ntCifaLen;i++)
delete m_tCifa[i];
m_pCurCh=0;
m_ntCifaLen=0;
while (GetCurCh())//GetSYM
{
if (flag)
{
while (!((GetCurCh()=='*')&&(GetNextCh()=='/')))
if (GetCurCh()) NextCh();else break;
if (GetCurCh())
{
NextCh();NextCh();
flag=FALSE;
}
}
while (1)
{
while ((GetCurCh()==32) || (GetCurCh()==9))
NextCh();
if (!((GetCurCh()==13) && (GetNextCh()==10)))
break;
NextCh();NextCh();
}
if (isalpha(GetCurCh()))//alpha
{
k=0;
while (1)
{
if (k<MAX_LEX_LEN-1) token[k++]=GetCh();
else NextCh();
if (!isalnum(GetCurCh())) break;
}
token[k]=0;
v=FindInKeyWordTab(token);
if (v)
{
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh-k;
m_ntCifaLen++;
}
else
{
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=FindInCifaTab(token);
m_tCifa[m_ntCifaLen]->type=1;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh-k;
m_ntCifaLen++;
}
}
else if (isdigit(GetCurCh()))//digit
{
k=0;
BOOL error=FALSE;
while (1)
{
if (k<=MAX_NUMBER_LEN) token[k++]=GetCh();
else
{
error=TRUE;
NextCh();
}
if (!isdigit(GetCurCh())) break;
}
token[k]=0;
v=::strtol(token,NULL,10);
if (error)
{
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=1;
m_tCifa[m_ntCifaLen]->type=0;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh-k;
m_ntCifaLen++;
}
else
{
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=v;
m_tCifa[m_ntCifaLen]->type=2;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh-k;
m_ntCifaLen++;
}
}
else
switch (GetCurCh())//other
{
case '+':
case '-':
case '*':
case '~':
case '&':
case '|':
case '=':
case ';':
case '.':
case ',':
case '(':
case ')':
token[0]=GetCurCh();
token[1]=0;
v=FindInKeyWordTab(token);
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
break;
case '\0':
break;
case '/':
switch (GetNextCh())
{
case '*':
NextCh();
NextCh();
flag=TRUE;
break;
default:
token[0]=GetCurCh();
token[1]=0;
v=FindInKeyWordTab(token);
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
}
break;
case '<':
switch (GetNextCh())
{
case '=':
token[0]=GetCurCh();
token[1]=GetNextCh();
token[2]=0;
v=FindInKeyWordTab(token);
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
NextCh();
break;
case '>':
token[0]=GetCurCh();
token[1]=GetNextCh();
token[2]=0;
v=FindInKeyWordTab(token);
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
NextCh();
break;
default:
token[0]=GetCurCh();
token[1]=0;
v=FindInKeyWordTab(token);
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
}
break;
case '>':
switch (GetNextCh())
{
case '=':
token[0]=GetCurCh();
token[1]=GetNextCh();
token[2]=0;
v=FindInKeyWordTab(token);
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
NextCh();
break;
default:
token[0]=GetCurCh();
token[1]=0;
v=FindInKeyWordTab(token);
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
}
break;
case ':':
switch (GetNextCh())
{
case '=':
token[0]=GetCurCh();
token[1]=GetNextCh();
token[2]=0;
v=FindInKeyWordTab(token);
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
NextCh();
break;
default:
token[0]=GetCurCh();
token[1]=0;
v=FindInKeyWordTab(token);
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=v;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
}
break;
default:
token[0]=GetCurCh();
token[1]=0;
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=2;
m_tCifa[m_ntCifaLen]->type=0;
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh;
m_ntCifaLen++;
NextCh();
}//switch
if (m_ntCifaLen==LEX_DUAD_TABLE_LEN-2)//out of table space
{
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=99;
m_tCifa[m_ntCifaLen]->type=0;
::strcpy(m_tCifa[m_ntCifaLen]->text,"");
m_tCifa[m_ntCifaLen]->address=m_pCurCh-1;
m_ntCifaLen++;
break;
}
}//GetSYM
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=0;
m_tCifa[m_ntCifaLen]->type=-1;//end node
::strcpy(m_tCifa[m_ntCifaLen]->text,"");
m_tCifa[m_ntCifaLen]->address=0;
m_ntCifaLen++;
return;
}
int CCompiler::FindInKeyWordTab(char * a)//查找关键字
{
for (int i=0;i<KEY_WORD_TABLE_LEN;i++)
if (!::stricmp(m_tKeyWord[i],a)) return i;
return 0;
}
int CCompiler::FindInCifaTab(char * a)
{
int v=1;
for (int i=0;i<m_ntCifaLen;i++)
{
if (m_tCifa[i]->type==1)
{
v++;
if (!::stricmp(m_tCifa[i]->text,a))
return m_tCifa[i]->value;
}
}
return v;
}
InterfaceType * CCompiler::GetTable()
{
return &m_Interface;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -