【编译原理实验】递归子程序法
文法:
E->TG
G->+TG|-TG|^
T->FS
S->*FS|/FS|^
F->i|(E)
表达式串的每个数符必须以i代替(懒得优化)
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #include<stdlib.h> 5 using namespace std; 6 7 8 int step_Counter=0;//计数第几步 9 char in[82];//存储输入串 10 char analized_str[82];int top=-1;//存储分析过的字符串 11 char *ch;//指向当前正在分析的字符 12 char * left_Function(char[]);//计算并返回剩余串的起始地址 老师请注意:left:剩余,不是左边 13 14 char finished_dri[82];int t_f=-1;//推导式中的已经完成分析的字符串和其栈顶指针 15 char unfinished_dri[82];int t_uf=-1;//推到是中的未完成分析的字符串和其栈顶指针 16 char record_div[10000];//记录推导过程的数组 17 void add(char arr[],char arr1[]);//add方法将未完成部分的栈倒序装入record_div记录数组 18 19 int E(); 20 int T(); 21 int F(); 22 int S(); 23 int G(); 24 25 int main() 26 { 27 cout<<"请输入长度不大于81的表达式:"; 28 cin>>in; 29 //cout<<strlen(in)<<endl; 30 char const_str[10]="分析串"; 31 printf("步骤 文法 %-40s分析字符 剩余串 ",const_str); 32 ch=in; 33 34 unfinished_dri[++t_uf]='E';//初始化非终结符栈 35 strcat(record_div,"E ");//记录第一条推导 36 37 38 int flag=E(); 39 if(flag==0) 40 cout<<endl<<" ERROR !"<<endl; 41 else 42 { 43 cout<<endl<<" ACCEPT !"<<endl; 44 cout<<"推导过程:"<<endl; 45 cout<<record_div<<endl; 46 } 47 return 0; 48 } 49 int E() 50 { 51 t_uf--;//非终结符栈栈顶元素出栈 52 unfinished_dri[++t_uf]='G';//非终结符倒序入非终结符栈 53 unfinished_dri[++t_uf]='T'; 54 55 unfinished_dri[t_uf+1]='