// Compiler.h: interface for the CCompiler class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_COMPILER_H__9E236A77_B434_4D97_AF7E_38B4F107BF3A__INCLUDED_)
#define AFX_COMPILER_H__9E236A77_B434_4D97_AF7E_38B4F107BF3A__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define BOOL int
#define TRUE 1
#define FALSE 0
#define MAX_LEX_LEN 20
#define MAX_NUMBER_LEN 8
#define LEX_DUAD_TABLE_LEN 10000
#define ERROR_TABLE_LEN 100
#define ERROR_MESSAGE_LEN 100
#define QUAD_TABLE_LEN 3000
#define KEY_WORD_TABLE_LEN 50
#define SOURSE_BUFFER_LEN 30003
#define isalpha(a) (((a)>='a'&&(a)<='z')||((a)>='A'&&(a)<='Z')||((a)=='_'))
#define isdigit(a) ((a)>='0'&&(a)<='9')
#define isalnum(a) (isalpha(a)||isdigit(a))
struct CifaResult
{
int type;//0:error,1:id,2:num,3-:keyword and operator,-1:end note
float value;//二元式中value值
char text[MAX_LEX_LEN];//单词
int address;//源文件缓冲区中地址
};
typedef char (* USER_TYPE_cha100)[ERROR_MESSAGE_LEN];
class CCompiler
{
public:
void YuFaFengXi();
void CiFaFengXi();
CCompiler();
virtual ~CCompiler();
void InitStr(char * );
char * get_ErrorMessage(int num){ return m_tErrorMessage[num]; };
int get_ErrorNum(){return m_nErrorNum;};
CifaResult ** get_CifaResult(){return m_tCifa;}
protected:
int m_nErrorAddress;
int m_nErrorNum;
CifaResult * GetNextSym();
CifaResult * GetSym();
void NextSym();
CifaResult * GetCurSym();
int m_pCurSym;
void Initial();
int p_YingZhi();
int p_Xiang();
int p_BiaoDaShi();
int p_ShiChanBiao(int para_count);
int FindInCifaTab(char *);
int FindInKeyWordTab(char *);
char m_tErrorMessage[ERROR_TABLE_LEN][ERROR_MESSAGE_LEN];
char m_tKeyWord[KEY_WORD_TABLE_LEN][MAX_LEX_LEN];
CifaResult * m_tCifa[LEX_DUAD_TABLE_LEN];
int m_ntCifaLen;
char GetNextCh();
char GetCh();
void NextCh();
char GetCurCh();
int m_pCurCh;
char m_aSourse[SOURSE_BUFFER_LEN];
};
#endif // !defined(AFX_COMPILER_H__9E236A77_B434_4D97_AF7E_38B4F107BF3A__INCLUDED_)
// Compiler.cpp: implementation of the CCompiler class.
//
//////////////////////////////////////////////////////////////////////
#include "Compiler.h"
#include
#include
#include
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCompiler::CCompiler()
{
m_ntCifaLen=0;
m_aSourse[0]=0;
Initial();
}
CCompiler::~CCompiler()
{
}
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::InitStr(char *source ){
::strcpy(m_aSourse,source);
}
void CCompiler::CiFaFengXi()
{
BOOL flag=FALSE;
char token[MAX_LEX_LEN];
int k,v;
for (int i=0;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 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())|| GetCurCh()=='.')//digit
{
BOOL ok=isdigit(GetCurCh());
k=0;
int error_value=0;
while (ok)
{
if (k<=MAX_NUMBER_LEN) token[k++]=GetCh();
else
{
error_value=1;
NextCh();
}
if (!isdigit(GetCurCh())) break;
}
if(GetCurCh()=='.'){
token[k++]=GetCh();
if(isdigit(GetCurCh())){
ok=TRUE;
while (1)
{
if (k<=MAX_NUMBER_LEN) token[k++]=GetCh();
else
{
error_value=1;
NextCh();
}
if (!isdigit(GetCurCh())) break;
}
}
}
if(ok){
if(GetCurCh()=='e'||GetCurCh()=='E'){
token[k++]=GetCh();
if(isdigit(GetCurCh())||GetCurCh()=='+'|| GetCurCh()=='-'){
while (1)
{
if (k<=MAX_NUMBER_LEN) token[k++]=GetCh();
else
{
error_value=1;
NextCh();
}
if (!isdigit(GetCurCh())) break;
}
}
if(!isdigit(token[k-1]))
error_value=3;
}
}
else{
error_value=3;
}
token[k]=0;
float res;
if(error_value==0){
res=::strtod(token,NULL);
}
if (error_value)
{
m_tCifa[m_ntCifaLen]=new CifaResult;
m_tCifa[m_ntCifaLen]->value=error_value;
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=res;
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;
}
//////词法结束111111111111111111111111111111111111111111
int CCompiler::FindInKeyWordTab(char * a)
{
for (int i=0;i if (!::stricmp(m_tKeyWord[i],a)) return i;
return 0;
}
int CCompiler::FindInCifaTab(char * a)
{
int v=1;
for (int i=0;i {
if (m_tCifa[i]->type==1)
{
v++;
if (!::stricmp(m_tCifa[i]->text,a))
return m_tCifa[i]->value;
}
}
return v;
}
void CCompiler::YuFaFengXi()
{
if (m_ntCifaLen==0) return;//未进行词法分析
m_pCurSym=0;
m_nErrorNum=0;
if(p_BiaoDaShi()==TRUE){
switch (GetCurSym()->type)
{
case -1://end of cifa
m_nErrorAddress=m_pCurSym;
break;
default:
m_nErrorNum=96;
m_nErrorAddress=m_pCurSym;
}
}
return;
}
int CCompiler::p_ShiChanBiao(int shican_geshu)//实参表,当参数个数为-1时,表示不需要检查参数的个数。
{
int count=0;
switch (GetCurSym()->type)
{
case 33://(
break;
default:
m_nErrorNum=16;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
NextSym();
if (!p_BiaoDaShi()) return FALSE;
count++;
while (GetCurSym()->type==32)//,
{
NextSym();
if (!p_BiaoDaShi()) return FALSE;
count++;
}
if( (shican_geshu!=-1) && (count!=shican_geshu)){
m_nErrorNum=38;
m_nErrorAddress=m_pCurSym;
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(GetCurSym()->type==16 || GetCurSym()->type==17)
NextSym();
if (!p_Xiang()) return FALSE;
while ( GetCurSym()->type==16 || GetCurSym()->type==17)//+
{
NextSym();
if (!p_Xiang()) return FALSE;
}
m_nErrorNum=0;//Successful
m_nErrorAddress=m_pCurSym;
return TRUE;
}
int CCompiler::p_Xiang()//项
{
if (!p_YingZhi()) return FALSE;
while (GetCurSym()->type==18 || GetCurSym()->type==19)//*
{
NextSym();
if (!p_YingZhi()) return FALSE;
}
return TRUE;
}
int CCompiler::p_YingZhi()//
{
int count;//计算实参的个数,便于检查。
int current_type=GetCurSym()->type;
switch (current_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;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15://关键字,函数的处理
if(current_type==13 || current_type ==14 )
{
count=-1;//表示不检查参数的个数。
}
else if(current_type==15)
{
count=2;
}
else{
count=1;
}
NextSym();
switch (GetCurSym()->type)
{
case 33://(
break;
default:
return TRUE;
}
if (!p_ShiChanBiao(count))
return FALSE;
break;
default:
m_nErrorNum=28;
m_nErrorAddress=m_pCurSym;
return FALSE;
}
return TRUE;
}
void CCompiler::Initial()
{
::strcpy(m_tKeyWord[0],"");
::strcpy(m_tKeyWord[1],"");//标识符
::strcpy(m_tKeyWord[2],"");//正整数
::strcpy(m_tKeyWord[3],"log");
::strcpy(m_tKeyWord[4],"ln");
::strcpy(m_tKeyWord[5],"exp");
::strcpy(m_tKeyWord[6],"fabs");
::strcpy(m_tKeyWord[7],"cos");
::strcpy(m_tKeyWord[8],"sin");
::strcpy(m_tKeyWord[9],"tan");
::strcpy(m_tKeyWord[10],"acos");
::strcpy(m_tKeyWord[11],"asin");
::strcpy(m_tKeyWord[12],"atan");//以上是只有一个参数的函数
::strcpy(m_tKeyWord[13],"max");
::strcpy(m_tKeyWord[14],"min");
::strcpy(m_tKeyWord[15],"pow");//此处开始是有两个参数的函数。
::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],"解析数字错误!");
::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],"表达式结束符后还有多余单词!");
::strcpy(m_tErrorMessage[97],"语法错误太多,终止语法分析!");
::strcpy(m_tErrorMessage[98],"源程序不正常结束!");
::strcpy(m_tErrorMessage[99],"内存不足!词法分析终止!");
}
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];
}
#include "Compiler.h"
#include
#include
//接口说明:
//调用InitStr初始化需要进行词法分析的语句。
//调用CiFaFengXi函数进行词法分析。
//完成后可以通过调用get_ErrorMessageTable()获取错误信息,该函数返回char (*ep)[100];
//循环检查p[i]->type==0的表示错误。
//调用YuFaFengXi函数完成语法分析。
//通过get_ErrorNum函数得到语法错误的号码。如果为0则是没有错误。
int main()
{
CCompiler m_Compiler;
int ErrorCount=0;
int j=0;
char *str[]={//此处添加测试语句,进行稳定性测试。
"min( name1, 40, -3 ) * 2+name2-log(name3*1.1e1)",
"max(5,6,7)+log(name,2)",
"pow(2)+log(name)",
"1.1e1"
};
#define COUNT (sizeof(str)/sizeof(str[0]))
//调用InitStr初始化需要进行词法分析的语句。
while(j<100){
m_Compiler.InitStr(str[j%COUNT]);
printf("\n%s\n",str[j%COUNT]);
m_Compiler.CiFaFengXi();
//完成后可以通过调用get_ErrorMessageTable()获取错误信息,该函数返回char (*ep)[100];
ErrorCount=0;
CifaResult * * p=m_Compiler.get_CifaResult();
for (int i=0;p[i]->type>=0;i++)
{
if(p[i]->type==0)//type为0表示错误。
{
ErrorCount++;
if (((int)p[i]->value)==2 && (p[i]->text[0]>126 || p[i]->text[0]<32))
{
printf("错误号:%d %s 【 无法显示该字符! 】\n",
(int) p[i]->value,m_Compiler.get_ErrorMessage(p[i]->value));
}
else
{
printf("错误号:%d %s 【 %s 】\n",
(int)p[i]->value,m_Compiler.get_ErrorMessage(p[i]->value),p[i]->text);
}
}
else
break;
}
printf("发现 %d 个错误!\n",ErrorCount);
m_Compiler.YuFaFengXi();
printf("语法分析结果:\n");
if (m_Compiler.get_ErrorNum()==0)
{
printf("源程序在语法上正确!\n");
}
else
{
printf("源程序有语法错误!!\n");
printf("错误号:%d %s\n",
m_Compiler.get_ErrorNum(),
m_Compiler.get_ErrorMessage(m_Compiler.get_ErrorNum()));
}
j++;
}
return 0;
}
阅读(991) | 评论(0) | 转发(1) |