编译原理实验二源程序及结果
【源程序】
#include
S移近操作 1 【S115例外】 R归约操作 2 acc 3 识别成功 *///ACTON表
int ACTION[16][8]={
{15,0,0,0,0,14,0,0},{0,16,17,0,0,0,0,30},{0,23,23,18,19,0,23,23},{0,26,26,26,26,0,26,26}, {15,0,0,0,0,14,0,0},{0,28,28,28,28,0,28,28},{15,0,0,0,0,14,0,0},{15,0,0,0,0,14,0,0}, {15,0,0,0,0,14,0,0},{15,0,0,0,0,14,0,0},{0,16,17,0,0,0,115,0},{0,21,21,18,19,0,21,21},
{0,22,22,18,19,0,22,22},{0,24,24,24,24,0,24,24},{0,25,25,25,25,0,25,25},{0,27,27,27,27,0,27,27}};//GOTO表 int
GOTO[10][3]={{1,2,3},{0,0,0},{0,0,0},{0,0,0},{10,2,3},{0,0,0},{0,11,3},{0,12,3},{0,0,13},{0,0,14}};
class GrammerAnalysis { private: int LeftSymStr[M],*p1;//定义余留符号串和指向它的指针 int Status[M],*p2;//定义状态和指向它的指针 int Stack[M],*p3;//定义分析栈和指向它的指针 int TopSat,InpSym;//栈顶状态,当前输入单词 public: GrammerAnalysis(); ~GrammerAnalysis(); int ConcludeSwitch(int a);////用来归约的产生式号,返回ETF,9,10,11 int ETFtoNum(int a);//查GOTO表用来转换
int SearchAction(int a,int b);//查询动作表 int SearchGoto(int a,int b);//查询状态转移表 int BackChNum(int a);//归约产生式回退字符个数 void InputWords();//输入单词 void PushStatus(int a);//状态号压栈 void Advance(int a);//将余留符号串的当前字符移入分析栈 void PopStack(int a);//归约时分析栈回退字符 void PopStatus(int a);//归约时状态栈栈回退字符 void NumToChar(int a);//数字转换为字符 void StaNumToStaCh(int a);//状态号大于10时转换为字符 void Output(int *p1,int *p2,int *p3); void Analysis(); void Error(); };
int GrammerAnalysis::SearchAction(int a,int b) { return ACTION[a][b-1]; }
int GrammerAnalysis::SearchGoto(int a,int b) { return GOTO[a][b]; }
void GrammerAnalysis::StaNumToStaCh(int a) { switch(a) { case 10: cout<<\ break; case 11: cout<<\ break; case 12: cout<<\ break; case 13: cout<<\ break; case 14: cout<<\ break; case 15: cout<<\ break;
default: break; } }
void GrammerAnalysis::NumToChar(int a) { switch(a) { case 1: cout<<\ break; case 2: cout<<\ break; case 3: cout<<\ break; case 4: cout<<\ break; case 5: cout<<\ break; case 6: cout<<\ break; case 7: cout<<\ break; case 8: cout<<\ break; case 9: cout<<\ break; case 10: cout<<\ break; case 11: cout<<\ break; default: break; }