📄 simplecompiler.cpp
字号:
// SimpleCompiler.cpp: implementation of the CSimpleCompiler class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "scanner.h"
#include "SimpleCompiler.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//单词编码定义
const char* CSimpleCompiler::m_pWordCode[]={
/*0*/ "",
/*1*/ "'",
/*2*/ "(",
/*3*/ ")",
/*4*/ "*",
/*5*/ "*/",
/*6*/ "+",
/*7*/ ",",
/*8*/ "-",
/*9*/ ".",
/*10*/ "..",
/*11*/ "/",
/*12*/ "/*",
/*13*/ ":",
/*14*/ ":=",
/*15*/ ";",
/*16*/ "<",
/*17*/ "<=",
/*18*/ "<>",
/*19*/ "=",
/*20*/ ">",
/*21*/ ">=",
/*22*/ "[",
/*23*/ "]",
/*24*/ "and",
/*25*/ "array",
/*26*/ "begin",
/*27*/ "bool",
/*28*/ "call",
/*29*/ "case",
/*30*/ "char",
/*31*/ "constant",
/*32*/ "constchar", //字符常数
/*33*/ "constfloat", //实常数
/*34*/ "constint", //整常数
/*35*/ "do",
/*36*/ "else",
/*37*/ "end",
/*38*/ "false",
/*39*/ "for",
/*40*/ "identifier", //标识符
/*41*/ "if",
/*42*/ "input",
/*43*/ "integer",
/*44*/ "not",
/*45*/ "of",
/*46*/ "or",
/*47*/ "output",
/*48*/ "procedure",
/*49*/ "program",
/*50*/ "read",
/*51*/ "real",
/*52*/ "repeat",
/*53*/ "set",
/*54*/ "then",
/*55*/ "to",
/*56*/ "true",
/*57*/ "until",
/*58*/ "var",
/*59*/ "while",
/*60*/ "write"
};
////////////////////////////////////////
/**
* 构造函数
*
*/
CSimpleCompiler::CSimpleCompiler()
{
/*
//初始化各分析器状态
m_readyMorpheme=true;
m_readyParsing=false;
m_readySemantic=false;
m_alreadyMorpheme=false;
m_alreadyParsing=false;
m_alreadySemantic=false;
*/
//初始化各指针
m_pTokenFile=new TokenNode; //token头结点
m_pTokenFile->next=NULL;
m_pSymbolTable=new STable; //符号表
m_pSymbolTable->sSubTable=NULL;
m_pSymbolTable->stringTable=NULL;
m_pSymbolTable->intSubTableLen=0; //符号子表长度=0
m_pSymbolTable->intTableLen=0; //符号串长度=0
m_pErrorCollection=new ErrorNode; //错误信息表
m_pErrorCollection->next=NULL;
m_pFSymbol=new FSymbol;
m_pFSymbol->next=NULL;
m_pFSymbol->ag1=NULL;
m_pFSymbol->ag2=NULL;
m_pFSymbol->result=NULL;
/*
//词法分析器实例
m_pMorpheme=new CMorpheme(m_pWordCode,m_pTokenFile,m_pSymbolTable,m_pErrorCollection);
//语法分析器实例
m_pParsing=new CParsing();
//语义分析器实例
m_pSemantic=new CSemantic();
*/
}
/**
* 构造函数
*
* @param strSourceFile 源程序文件路径
*
*/
CSimpleCompiler::CSimpleCompiler(CString strSourceFile)
{
//保留源程序文件地址
SetSourceFile(strSourceFile);
CSimpleCompiler();
}
/**
* 设置源程序文件路径
*
*/
void CSimpleCompiler::SetSourceFile(CString strSourceFile)
{
this->m_strSourceFile=strSourceFile;
}
/**
* 保存符号表、token文件、错误信息表、四元式用于显示
*
*/
void CSimpleCompiler::SaveAll()
{
char str[100];
PTokenNode token=NULL;
PSTable sTable=NULL;
PErrorNode error=NULL;
PFSymbol pFS=NULL;
CString sfile;
FILE* fpout;
sfile="token.dat";
fpout=fopen(sfile.GetBuffer(0),"w");
if (!fpout)
{
::AfxMessageBox("系统错误!");
return;
}
else
{
fprintf(fpout,"(%s,%s)\t%s\n\n","CONTENT","KEYCODE","STRID");
token=this->m_pTokenFile;
sTable=m_pSymbolTable;
int start,end;
//写token
while (token->next!=NULL)
{
token=token->next;
if (token->strId!=0)
{
end=start=sTable->sSubTable[token->strId-1].name.headp;
end+=sTable->sSubTable[token->strId-1].name.length;
if (end==start)
{
if (sTable->sSubTable[token->strId-1].type==Real)
{
fprintf(fpout,"( %ld\t, %ld\t)\t%f\n",token->keycode,token->strId,sTable->sSubTable[token->strId-1].val);
}
else
{
fprintf(fpout,"( %ld\t, %ld\t)\t%ld\n",token->keycode,token->strId,(long)sTable->sSubTable[token->strId-1].val);
}
}
else
{
for (int i=start;i<end;i++)
{
str[i-start]=sTable->stringTable[i];
}
str[i-start]='\0';
fprintf(fpout,"( %ld\t, %ld\t)\t%s\n",token->keycode,token->strId,str);
}
}
else
{
fprintf(fpout,"( %ld\t, %c\t)\t%s\n",token->keycode,'_',m_pWordCode[token->keycode]);
}
}
fclose(fpout);
}
//写符号表
sfile="symboltable.dat";
fpout=fopen(sfile.GetBuffer(0),"w");
if (!fpout)
{
::AfxMessageBox("系统错误!");
return;
}
else
{
fprintf(fpout,"(%s,%s)\t\t%s\t\t%s\n\n","HEADP","LENGTH","TYPE","VAL");
sTable=m_pSymbolTable;
for (int i=0;i<sTable->intSubTableLen;i++)
{
if (sTable->sSubTable[i].type==Char || sTable->sSubTable[i].type==Identifier)
{
fprintf(fpout,"( %ld\t, %ld\t)\t%s\n",sTable->sSubTable[i].name.headp,
sTable->sSubTable[i].name.length,
this->TypeToString(sTable->sSubTable[i].type));
}
else if (sTable->sSubTable[i].type==Real)
{
fprintf(fpout,"( %ld\t, %ld\t)\t%s\t\t%f\n",sTable->sSubTable[i].name.headp,
sTable->sSubTable[i].name.length,
this->TypeToString(sTable->sSubTable[i].type),
sTable->sSubTable[i].val);
}
else if (sTable->sSubTable[i].type==Integer)
{
fprintf(fpout,"( %ld\t, %ld\t)\t%s\t\t%ld\n",sTable->sSubTable[i].name.headp,
sTable->sSubTable[i].name.length,
this->TypeToString(sTable->sSubTable[i].type),
(long)sTable->sSubTable[i].val);
}
}
if (sTable->intTableLen!=0)
{
fprintf(fpout,"\n符号表:\n\n");
fprintf(fpout,"%s\n",sTable->stringTable);
}
fclose(fpout);
}
//写错误信息集合
sfile="error.dat";
fpout=fopen(sfile.GetBuffer(0),"w");
if (!fpout)
{
::AfxMessageBox("系统错误!");
return;
}
else
{
fprintf(fpout,"ROWS,COLS\t\tERROR TEXT\n\n");
error=m_pErrorCollection;
while (error->next!=NULL)
{
error=error->next;
fprintf(fpout,"%ldrows,%ldcols\t\t%s\n",error->intRows,error->intCols,error->description);
}
fclose(fpout);
}
//写四元式信息
sfile="fsymbol.dat";
fpout=fopen(sfile.GetBuffer(0),"w");
int i=0;
if (!fpout)
{
::AfxMessageBox("系统错误!");
return;
}
else
{
fprintf(fpout,"No.\t(OP,AG1,AG2,RESULT)\n\n");
pFS=this->m_pFSymbol->next;
while (pFS!=NULL && pFS->next!=NULL)
{
fprintf(fpout,"%d\t(%s,%s,%s,%s)\n",++i,this->GetOPStr(pFS->op),
pFS->ag1==NULL? "_":pFS->ag1->des,
pFS->ag2==NULL? "_":pFS->ag2->des,
pFS->result==NULL? "_":pFS->result->des);
pFS=pFS->next;
}
fclose(fpout);
}
}
/**
* 根据操作类型返回操作符
*
* @param EOP op 操作符类型
*
*/
char *CSimpleCompiler::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;
}
/**
* 将type转换成字符串
*
*/
char *CSimpleCompiler::TypeToString(SType type)
{
switch(type)
{
case Identifier:
return "Identifier";
break;
case Char:
return "Char";
break;
case Real:
return "Real";
break;
case Integer:
return "Integer";
break;
case Bool:
return "Bool";
break;
}
return "";
}
/**
* 运行词法分析,返回成功信息
*
*/
bool CSimpleCompiler::RunMorpheme()
{
//进行词法分析
Dispose();
CMorpheme morpheme(m_pWordCode,m_pTokenFile,m_pSymbolTable,m_pErrorCollection);
morpheme.Morpheme(m_strSourceFile);
////
return false;
}
/**
* 运行语法分析,返回成功信息
*
*/
bool CSimpleCompiler::RunParsing()
{
//进行语法分析
Dispose();
CParsing parsing(m_pWordCode,m_pTokenFile,m_pSymbolTable,m_pErrorCollection,m_strSourceFile);
parsing.Parsing();
////
return false;
}
/**
* 运行语义分析,返回成功信息
*
*/
bool CSimpleCompiler::RunSemantic()
{
//进行语义分析
Dispose();
CSemantic semantic(m_pWordCode,m_pTokenFile,m_pSymbolTable,m_pFSymbol,m_pErrorCollection,m_strSourceFile);
semantic.Semantic();
////
return false;
}
/**
* 返回符号表
*
*/
PSTable CSimpleCompiler::GetSTable()
{
return m_pSymbolTable;
}
/**
* 返回token文件
*
*/
PTokenNode CSimpleCompiler::GetTokenFile()
{
return m_pTokenFile;
}
/**
* 返回四元式表
*
*/
PFSymbol CSimpleCompiler::GetFSymbol()
{
return this->m_pFSymbol;
}
/**
* 返回错误信息
*
*/
PErrorNode CSimpleCompiler::GetErrorCollection()
{
return this->m_pErrorCollection;
}
/**
* 释放空间
*
*/
CSimpleCompiler::~CSimpleCompiler()
{
Dispose();
delete this->m_pErrorCollection;
delete this->m_pSymbolTable;
delete this->m_pTokenFile;
}
void CSimpleCompiler::Dispose()
{
//释放字符表中各项的入口地址
for (int i=0;i<m_pSymbolTable->intSubTableLen;i++)
{
delete m_pSymbolTable->sSubTable[i].addr;
}
if (m_pSymbolTable->sSubTable) delete m_pSymbolTable->sSubTable;
if (m_pSymbolTable->stringTable) delete m_pSymbolTable->stringTable;
m_pSymbolTable->intTableSize=0;
m_pSymbolTable->intSubTableSize=0;
::PTokenNode pTokenNode=m_pTokenFile->next;
::PTokenNode pNextTokenNode;
while (pTokenNode!=NULL)
{
pNextTokenNode=pTokenNode->next;
delete pTokenNode;
pTokenNode=pNextTokenNode;
}
::PErrorNode pError=m_pErrorCollection->next;
::PErrorNode pErrorNext;
while (pError!=NULL)
{
pErrorNext=pError->next;
delete pError;
pError=pErrorNext;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -