好文档 - 专业文书写作范文服务资料分享网站

递归下降语法分析设计原理与实现技术实验报告

天下 分享 时间: 加入收藏 我要投稿 点赞

.

{ case '1': if(token[i]!='(') error1(); break; case '2': if(token[i]!=')') error1(); break; case '3': if(token[i]!='+') error1(); break; case '4': if(token[i]!='-') error1(); break; case '5': if(token[i]!='*') error1(); break; case '6': if(token[i]!='/') error1(); break; case '7': if(token[i]!='i') error1(); break; case '8': if(token[i]!='#') error1(); break; default: error1(); break; } }

void advance() { if(i==MAX) exit(0);

.

//读取下一个字符

.

current=token[++i]; }

void E() { if(current=='i' || current=='(') { T(); E1(); } else error(); }

void E1() { if(current=='+' || current=='-') { A(); T(); E1(); } else if(!(current==')' || current=='#')) error(); }

void T() { if(current=='i' || current=='(') { F(); T1(); } else error(); }

void T1() { if(current=='*' || current=='/') { M(); F();

.

.

T1(); } else if(!(current==')'||current=='#'||current=='+'||current=='-')) error(); }

void M() { if(current=='*') advance(); else if(current=='/') advance(); else error(); }

void A() { if(current=='+') advance(); else if(current=='-') advance(); else error(); }

void F() { if(current=='(') { advance(); E(); if(current==')') advance(); } else if(current=='i') advance(); else error(); }

.

.

void error() { cout<<\ exit(0); }

void error1() { cout<<\输入错误!\ exit(0); }

int main() { int flag; //提供输入和文件两种方式 int j=1; int i=0; char token1[MAX]; //用于检测文件输入是否匹配 FILE *fp; cout<<\输入方式:键盘(1)/文件(2):\ while(!(cin>>flag) || (flag!=1 && flag!=2)) { cout<<\ cin.clear(); cin.ignore(200,'\\n'); } if(flag==1) { cout<<\请输入长度不超过50且以#结束的字符串:\ cin>>token; } else { if((fp=fopen(\ { cout<<\文件打开失败!\ exit(0); } token1[0]=fgetc(fp); while(!feof(fp)) { token1[j++]=fgetc(fp); if((j%5)==0)

.

.

}

{ token2[i]=token1[j-4]; token[i]=token1[j-2]; justify(token2[i],i); i++; } } if((j-1)%5 != 0) error1(); cout<<\读入字符串为:\} init(); E();

if(current=='#') cout<<\return 1;

.

递归下降语法分析设计原理与实现技术实验报告

.{case'1':if(token[i]!='(')error1();break;case'2':if(token[i]!=')')error1();break;case'3':if(token[i]!='+')
推荐度:
点击下载文档文档为doc格式
4ycb630fvt6et871df8g8njyy26yjv018ht
领取福利

微信扫码领取福利

微信扫码分享