中间代码生成(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
原创粉丝点击