对一个文法,添加一段代码,输出推导过程的语法树
以下代码是文法G[E]的语法分析程序:E->TE'
E'->+TE'|ε
T->FT'
T'->*FT'|ε
F->(E)|i
请添加代码使其能在语法检查结束后输出推导过程的语法树。
程序代码:
#include <iostream.h> char inputstream[50]; //存储输入句子 int temp=0; //数组下标 int right; //判断输出信息 void e(); void e1(); void t(); void t1(); void f(); void main() { right=1; cout<<"请输入您要分析的字符串以#结束(^为空字符):"<<endl; cin>>inputstream; e(); if((inputstream[temp]=='#')&&right) cout<<"分析成功"<<endl; else cout<<"分析失败"<<endl; } void e() { cout<<"E->TE'"<<endl; t(); e1(); } void e1() { if(inputstream[temp]=='+') { cout<<"E'->+TE'"<<endl; temp++; t(); e1(); } else if (inputstream[temp]!='#'||inputstream[temp]!=')') { cout<<"T'->^"<<endl; return ; } else right=0; } void t() { cout<<"T->FT'"<<endl; f(); t1(); } void t1() { if(inputstream[temp]=='*') { cout<<"T'->*FT'"<<endl; temp++; f(); t1(); } else if (inputstream[temp]!='#'&&inputstream[temp]!=')'&&inputstream[temp]!='+') { cout<<"T'->^"<<endl; right=0; } } void f() { if(inputstream[temp]=='i') { cout<<"F->i"<<endl; temp++; } else if(inputstream[temp]=='(') { cout<<"F->(E)"<<endl; temp++; e(); if(inputstream[temp]==')') { cout<<"F->(E)"<<endl; temp++; } else right=0; } else right =0; }