logo资料库

编译原理实验二_词法扫描并生成中间表达式.doc

第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
资料共5页,全文预览结束
淮海工学院计算机工程学院 实 验 报 告 书 课 程 名 :《 编译原理 》 题 目: 实验二 词法扫描并生成中间表达式 班 级: 学 号: 姓 名: 评语: 成绩: 指导教师: 批阅时间: 年 月 日
《编译原理》实验报告 - 1 - 一.目的与要求 进一步熟悉 c 语言编译环境;掌握词法扫描,掌握中缀表达式生成逆波兰表达式的方法。 二.实验内容与步骤 根据逆波兰式的生成方式,将形如下列的算术表达式: a*(b+c-d)+e/f-g 生成逆波兰式。 要求: (1)从键盘输入算术表达式。 (2)简单的算术表达式的两端用特殊符号“#”括起。 (3)输出简单算术表达式及其生成的逆波兰表达式。 三.实验源程序 #include #include #include #include #define maxbuffer 64 void main() { char display_out(char out_ch[maxbuffer], char ch[32]); static int i=0; static int j=0; char ch[maxbuffer],s[maxbuffer],out[maxbuffer]; cout<<"请输入您要转换的算术表达式(以“#”结束): "; cin>>ch; for(i=0;i
《编译原理》实验报告 - 2 - { while(isalnum(ch[i])) { out_ch[j]=ch[i]; j++; i++; } out_ch[j]=' ';j++; } else{ switch(ch[i]) { case '+': case '-': if(sta[top]=='('||top==-1) top++; sta[top]=ch[i]; i++; { } else { out_ch[j]=sta[top]; j++; top--; } break; case '*': case '/':if(sta[top]=='*'&&sta[top]=='/') out_ch[j]=sta[top]; j++; top--; { } else { top++; sta[top]=ch[i]; i++; } break ; case '(': top++; sta[top]=ch[i]; i++; break; case ')':
《编译原理》实验报告 - 3 - if(sta[top]=='(') { top--; i++; } if(top==-1) { cout<<"错误: 第"<
《编译原理》实验报告 - 4 - 五.结果分析与实验体会
分享到:
收藏