中间代码生成(C++)
来源:互联网 发布:云计算吧 编辑:程序博客网 时间:2024/06/05 04:31
题目:对于正确的表达式输出其中间代码,用四元式表示
思路:
用递归子程序法修改词法分析,每次在分析时记录该元素,判断表达式正确时则输出四元式
分别写因子,项,表达式处理函数
int* factor(int &index, int* temp,string &res,WORDLIST wordlist);int* term(int &index, int* temp, string &res, WORDLIST wordlist);int* expression(int &index, int* temp,string &res, WORDLIST wordlist);//index表示该字符的序号,temp为数组,暂存当前字符序号和表达式正确与否//res用于输出四元式,wordlist用于存储字符串
详细实现:
int* expression(int &index, int* temp, string &res, WORDLIST wordlist) {string r1,r0;if (wordlist.getSym(index) == "plus" || wordlist.getSym(index) == "minus") { //是+或者-开头,表示正负数r0 = wordlist.getWord(index);//存正负号index++; //检查下一个是否为项if (wordlist.getWord(index) != "") {//若该字符存在term(index, temp, res, wordlist);index = temp[0]; //记录返回值success = temp[1];if (success == 0) return temp;else {r1 = r0 + res;//带符号一起存}}elsereturn temp;}else {//没有符号时term(index,temp,res,wordlist);index = temp[0];success = temp[1];if (success == 0) return temp;else {r1 = res;}}while ((wordlist.getSym(index) == "plus" || wordlist.getSym(index) =="minus")&& success == 1) {string opr = wordlist.getWord(index);//获取+ -运算符if (wordlist.getWord(index) != "") {index++;term(index, temp, res, wordlist);index = temp[0];success = temp[1];if (success == 1) {sumout++;//统计已输出的行数cout << "(" << opr << "," << r1 << "," << res << ",t" << sumout << ")" << endl;stringstream ss;ss << "t" << sumout;res = ss.str();}}elsereturn temp;}temp[0] = index;temp[1] = success;return temp;}int* term(int &index,int* temp, string &res, WORDLIST wordlist) {string r1;factor(index,temp,res,wordlist); //检查是否为因子index = temp[0];//记录返回值success = temp[1];if (success == 0) return temp;else { //是因子则存值r1 = res;}while ((wordlist.getSym(index) == "times" || wordlist.getSym(index)=="slash")&& success == 1) //如果是*或者/且之前都符合要求{string opr = wordlist.getWord(index); //存乘法运算符if (wordlist.getWord(index) != "") {index++;//检查下一项是否为因子factor(index, temp, res, wordlist);index = temp[0];success = temp[1];if (success == 1) { //是因子则出结果sumout++;//统计已输出的行数cout << "(" << opr << "," << r1 << "," << res << ",t" << sumout << ")" << endl;stringstream ss;ss << "t" << sumout;res = ss.str();}}else return temp;}temp[0] = index;temp[1] = success;return temp;}int* factor(int &index, int* temp, string &res, WORDLIST wordlist) {success = 1;string r1;if (wordlist.getSym(index) == "ident" || wordlist.getSym(index) == "number") { //是标识符或者数字直接检查下一项res = wordlist.getWord(index);index++;}else if (wordlist.getSym(index) == "lparen") {//如果是(index++; //检查下一项是否是表达式temp[0] = index;temp[1] = success;if (wordlist.getWord(index) != "") {expression(index, temp, res, wordlist);index = temp[0];//记录返回值success = temp[1];if (success == 0) return temp;else {r1 = res;}if (wordlist.getSym(index) == "rparen") { //表达式后的是)则检查下一项index++;res = r1;//存值}}elsereturn temp;}else {cout << "表达式错误!" << endl;//所有错误都输出这句success = 0;}temp[0] = index;temp[1] = success;return temp;}
WORDLIST类函数说明
1.int getPosition(string s) 根据输入的字符串查询位置
2.string getWord(int i)根据位置获取字符串
3.void setWord(string s)设置字符
4.int getNum()获取总长度(包含0位未空)
5.string getSymW(string s)输入字符串,获取符号名
6.string getSym(int position)输入位置,获取符号名
阅读全文
0 0
- 中间代码生成(C++)
- 中间代码生成
- 中间代码生成中的优化
- C编译器剖析_5.1 中间代码生成及优化_简介
- C编译器剖析_5.2.1 中间代码生成及优化_布尔表达式的翻译
- C编译器剖析_5.2.4 中间代码生成及优化_后缀表达式的翻译
- C编译器剖析_5.2.5 中间代码生成及优化_赋值表达式的翻译
- C编译器剖析_5.3.2 中间代码生成及优化_switch语句的翻译
- C编译器剖析_5.4.2 中间代码生成及优化_基本块的合并
- 属性文法与中间代码生成
- 编译原理——中间代码生成
- 编译原理实验三:中间代码生成
- clang及参数和生成中间代码
- C编译器剖析_6.3.1 汇编代码生成_由中间指令产生汇编代码的主要流程
- C编译器剖析_5.2.2 中间代码生成及优化_再论符号symbol与公共子表达式
- C编译器剖析_5.2.3 中间代码生成及优化_通过“偏移”访问数组元素和结构体成员
- C编译器剖析_5.2.6 中间代码生成及优化_一元表达式及其他表达式的翻译
- C编译器剖析_5.3.1 中间代码生成及优化_If语句和复合语句的翻译
- 自定义Tab + ViewPager控件
- Java实现-最长上升连续子序列
- 安卓保存照片到手机数据库并在相册显示
- 资料链接总结
- hdu 4118 Holiday's Accommodation 树形dp
- 中间代码生成(C++)
- Ritorfit基础使用
- NKOJ-1521 芯片制作(染色法构图)
- Guava并发:ListenableFuture使用介绍以及示例
- 62 linux内核里基于GPIO口的SPI控制器驱动
- ABAP BINARY SEARCH--二分法
- 如何优雅地测量一只猫的体积
- java学习 —————— 集合类
- RAID详解