📄 analyzer.cpp
字号:
// Analyzer.cpp: implementation of the CAnalyzer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "compiler.h"
#include "Analyzer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAnalyzer::CAnalyzer()
{
m_ntCifaLen=0;
m_aSourse[0]=0;
}
CAnalyzer::~CAnalyzer()
{
}
void CAnalyzer::Initial()
{
::strcpy(m_tKeyWord[0],"");
::strcpy(m_tKeyWord[1],"");//标识符
::strcpy(m_tKeyWord[2],"");//数字
::strcpy(m_tKeyWord[3],"main");
::strcpy(m_tKeyWord[4],"break");
::strcpy(m_tKeyWord[5],"case");
::strcpy(m_tKeyWord[6],"char");
::strcpy(m_tKeyWord[7],"const");
::strcpy(m_tKeyWord[8],"continue");
::strcpy(m_tKeyWord[9],"defaut");
::strcpy(m_tKeyWord[10],"define");
::strcpy(m_tKeyWord[11],"do");
::strcpy(m_tKeyWord[12],"double");
::strcpy(m_tKeyWord[13],"else");
::strcpy(m_tKeyWord[14],"enum");
::strcpy(m_tKeyWord[15],"extern");
::strcpy(m_tKeyWord[16],"float");
::strcpy(m_tKeyWord[17],"for");
::strcpy(m_tKeyWord[18],"goto");
::strcpy(m_tKeyWord[19],"if");
::strcpy(m_tKeyWord[20],"include");
::strcpy(m_tKeyWord[21],"int");
::strcpy(m_tKeyWord[22],"long");
::strcpy(m_tKeyWord[23],"return");
::strcpy(m_tKeyWord[24],"short");
::strcpy(m_tKeyWord[25],"static");
::strcpy(m_tKeyWord[26],"struct");
::strcpy(m_tKeyWord[27],"switch");
::strcpy(m_tKeyWord[28],"signed");
::strcpy(m_tKeyWord[29],"typedef");
::strcpy(m_tKeyWord[30],"union");
::strcpy(m_tKeyWord[31],"unsigned");
::strcpy(m_tKeyWord[32],"void");
::strcpy(m_tKeyWord[33],"while");
::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[61],"&");
::strcpy(m_tKeyWord[62],".");
m_tKeyWord[63][0]='"';//将"进行初始化
m_tKeyWord[63][1]='\0';
::strcpy(m_tKeyWord[64],"");
::strcpy(m_tErrorMessage[0],"正确!");
::strcpy(m_tErrorMessage[1],"数字位数过长!");
::strcpy(m_tErrorMessage[2],"非法字符!");
::strcpy(m_tErrorMessage[3],"变量书写不合法!");
::strcpy(m_tErrorMessage[4],"缺少标识符“#”!");
::strcpy(m_tErrorMessage[5],"文件包含(include) 格式出错!");
::strcpy(m_tErrorMessage[6],"标识符“<”失配!!");
::strcpy(m_tErrorMessage[7],"标识符引号失配!");
::strcpy(m_tErrorMessage[8],"main函数格式错误!");
::strcpy(m_tErrorMessage[9],"界符“(”不匹配!");
::strcpy(m_tErrorMessage[10],"缺少语句结束符“;”!");
::strcpy(m_tErrorMessage[11],"缺少声明语句");
::strcpy(m_tErrorMessage[12],"缺少保留字“{”或“}”!");
::strcpy(m_tErrorMessage[13],"变量未定义!!");
::strcpy(m_tErrorMessage[14],"缺少变量类型");
::strcpy(m_tErrorMessage[15],"变量声明后缺少标识符!!");
::strcpy(m_tErrorMessage[16],"变量声明后缺少标识符");
::strcpy(m_tErrorMessage[17],"缺少 “ ) ”符号!");
::strcpy(m_tErrorMessage[18],"缺少标识符 !");
::strcpy(m_tErrorMessage[19],"缺少赋值符号“ = ”!");
::strcpy(m_tErrorMessage[20],"缺少保留字“ if ”!");
::strcpy(m_tErrorMessage[21],"缺少while");
::strcpy(m_tErrorMessage[22],"缺少保留字“ do ”!");
::strcpy(m_tErrorMessage[23]," ");
::strcpy(m_tErrorMessage[24],"缺少保留字“ while ”!");
::strcpy(m_tErrorMessage[25],"缺少保留字“ do ”!");
::strcpy(m_tErrorMessage[26]," ");
::strcpy(m_tErrorMessage[27]," ");
::strcpy(m_tErrorMessage[28],"运算式不完全,缺少因子");
::strcpy(m_tErrorMessage[29],"缺少'布尔表达式'");
::strcpy(m_tErrorMessage[30],"缺少关系(<,<=,>,>=,=,<>)!");
}
void CAnalyzer::CiFafenxi()
{
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())
{//while语句控制全过程
if (flag)
{//flag==true表示出现'/*'
while (!((GetCurCh()=='*')&&(GetNextCh()=='/')))//识别注释行
if (GetCurCh())
m_pCurCh++;
else
break;
if (GetCurCh())
{
m_pCurCh++; m_pCurCh++;
flag=FALSE;
}
}
while (1)
{//滤掉空格换行等
while ((GetCurCh()==32) || (GetCurCh()==9))
m_pCurCh++;//space,tab
if (!((GetCurCh()==13) && (GetNextCh()==10)))
break;//回车换行
m_pCurCh++;
m_pCurCh++;
}
if (isalpha(GetCurCh()))
{//a..z,A....Z
k=0;
BOOL error=false;//标志
int f=0;
while (1)
{
if (k<MAX_LEX_LEN-1) //小于允许的单词长度
{
if(isdigit(GetCurCh()))
f=1; //表示字母后存在数字
token[k++]=GetCh();//读取单词
if (isalpha(GetCurCh())&&1==f)//表示单词中嵌有数字
error=true;
}
else
m_pCurCh++;
if (!isalnum(GetCurCh())&&GetCurCh()!='.') //a....z,A..Z,0...9 , .
break;
}
token[k]=0;//分词结束,得到一个单词token中
if (error)
{//表示有错误:单词书写不合法
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=3; //错误号
m_tCifa[m_ntCifaLen]->type=0; //0表示此处有误
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh-k; //记录地址
m_ntCifaLen++;
}
v=FindInKeyWordTab(token); //返回关键字位置
if (v)
{//int 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 error1,error2;
error1=error2=FALSE;
while (1)
{
if (k<=MAX_NUMBER_LEN) //MAX_NUMBER_LEN允许的最长数字个数
{
token[k++]=GetCh();
}
else
{//数字超过允许长度
error1=TRUE;
m_pCurCh++;
}
if(isalpha(GetCurCh()))
{
error2=TRUE;
token[k++]=GetCh();
}
if (!isalnum(GetCurCh())&&GetCurCh()!='.') //不是数字
break;
}
token[k]=0;
v=::strtol(token,NULL,10);//将数字转换
if (error1)
{//表示有误:数字超长
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=1;//
m_tCifa[m_ntCifaLen]->type=0;//0表示此处有误
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh-k;//记录地址
m_ntCifaLen++;
}
else if(error2)
{//表示有错误:单词书写不合法
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=3; //错误号
m_tCifa[m_ntCifaLen]->type=0; //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 ']':
case '{':
case '"':
case 39://单引号
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++;
m_pCurCh++;
break;
case '+':
switch (GetNextCh())
{
case '+':
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++;
m_pCurCh++;
m_pCurCh++;
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++;
m_pCurCh++;
break;
}
break;
case '-':
switch (GetNextCh())
{
case '-':
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++;
m_pCurCh++;
m_pCurCh++;
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++;
m_pCurCh++;
m_pCurCh++;
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++;
m_pCurCh++;
break;
}
break;
case '\0'://字符串结束符
break;
case '/'://注释行
switch (GetNextCh())
{
case '*':
m_pCurCh++;
m_pCurCh++;
flag=TRUE;//表示注释行
break;
case '/':
token[0]=GetCurCh();
token[1]=GetNextCh();
token[2]=0;
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=2; //错误号
m_tCifa[m_ntCifaLen]->type=0; //0表示此处有误
::strcpy(m_tCifa[m_ntCifaLen]->text,token);
m_tCifa[m_ntCifaLen]->address=m_pCurCh-k; //记录地址
m_pCurCh++;
m_pCurCh++;
m_ntCifaLen++;
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++;
m_pCurCh++;
}
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++;
m_pCurCh++;
m_pCurCh++;
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++;
m_pCurCh++;
}
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++;
m_pCurCh++;
m_pCurCh++;
break;
default:
token[0]=GetCurCh();//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++;
m_pCurCh++;
}
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++;
m_pCurCh++;
m_pCurCh++;
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++;
m_pCurCh++;
}
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++;
m_pCurCh++;
m_pCurCh++;
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++;
m_pCurCh++;
}
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++;
m_pCurCh++;
}//switch
if (m_ntCifaLen==LEX_DUAD_TABLE_LEN-2)//out of table space
{//LEX_DUAD_TABLE_LEN 1000
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;
}
}//while语句控制全过程
//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 CAnalyzer::FindInKeyWordTab(char * a)
{
for (int i=0;i<KEY_WORD_TABLE_LEN;i++)
if (!::stricmp(m_tKeyWord[i],a)) return i;
return 0;
}
int CAnalyzer::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;
}
char CAnalyzer::GetCh()
{
return m_aSourse[m_pCurCh++];
}
char CAnalyzer::GetNextCh()
{
if (m_aSourse[m_pCurCh]==0)
return 0;
return m_aSourse[m_pCurCh+1];
}
char CAnalyzer::GetCurCh()
{
return m_aSourse[m_pCurCh];
}
int CAnalyzer::YuFafenxi()
{
YuErrorNo=0;
m_pCurRS=0; //指示词法分析结果表中单词的位置
while(2!=GetCurRS()->type&&32!=GetCurRS()->type)
{
if(48==GetCurRS()->type)
{//#
m_pCurRS++;
if(20==GetCurRS()->type)//include
{
m_pCurRS++;
switch(GetCurRS()->type)
{
case 54://<
m_pCurRS++;
if(55==GetNextRS()->type)//>
break;
else
{
YuErrorNo=6;//标识符"<"失配!
ErrorAddr=m_pCurRS;
return false;
}
case 63:
m_pCurRS++;
if(63==GetNextRS()->type)//>
break;
else
{
YuErrorNo=7;//标识符' " '失配!
ErrorAddr=m_pCurRS;
return false;
}
default:
YuErrorNo=5;//文件包含(include) 格式出错
ErrorAddr=m_pCurRS;
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -