2016年秋季《编译原理》课程实验(2014级)表达式语法分析——递归子程序法
来源:互联网 发布:wind金融数据 编辑:程序博客网 时间:2024/05/22 00:39
#include<stdio.h>#include<string>#include<iostream>#include<algorithm>#include<stack>using namespace std;int num;stack<char> A;stack<char> B;int f(char x,char y){ if(x == 'E') { cout<<num<<" "<<"E-->TG"<<endl; A.pop(); A.push('G'); A.push('T'); num++; return 1; } else if(x == 'G'&&y=='+') { cout<<num<<" "<<"G-->+TG"<<endl; A.pop(); A.push('G'); A.push('T'); A.push('+'); num++; return 1; } else if(x == 'G'&&y!='+') { cout<<num<<" "<<"G-->&"<<endl; A.pop(); num++; return 1; } else if(x == 'T') { cout<<num<<" "<<"T-->FS"<<endl; A.pop(); A.push('S'); A.push('F'); num++; return 1; } else if(x =='S' && y == '*') { cout<<num<<" "<<"S-->*FS"<<endl; A.pop(); A.push('S'); A.push('F'); A.push('*'); num++; return 1; } else if(x =='S' && y !='*')//保持不变,E题不同 { cout<<num<<" "<<"S-->&"<<endl; A.pop(); num++; return 1; } else if(x == 'F' && y == 'i') { cout<<num<<" "<<"F-->i"<<endl; A.pop(); A.push('i'); num++; return 1; } else if(x == 'F' && y == '(') { cout<<num<<" "<<"F-->(E)"<<endl; A.pop(); A.push(')'); A.push('E'); A.push('('); num++; return 1; } else { if(x == y) { A.pop(); B.pop(); return 1; } else { return 0; } }}void reset(){ num = 0; while(!A.empty()) { A.pop(); } A.push('#'); A.push('E'); while(!B.empty()) { B.pop(); }}int main(){ string s; while(cin>>s) { reset(); int len =s.length(); for(int i = len -1;i >= 0;i--) { B.push(s[i]); } while(1) { if(A.top() == '#'&& B.top() == '#') { cout<<"accept"<<endl; break; } else { int key = f(A.top(),B.top()); if(key == 0) { cout<<"error"<<endl; break; } } } } return 0;}
//与E题不同也在G和S,为G时候不为‘+’,就G->&, S也一样
0 0
- 2016年秋季《编译原理》课程实验(2014级)表达式语法分析——递归子程序法
- 表达式语法分析——递归子程序法
- 表达式语法分析——递归子程序法
- 表达式语法分析——递归子程序法
- 表达式语法分析——递归子程序法
- 表达式语法分析——递归子程序法
- 表达式语法分析——递归子程序法
- 表达式语法分析——递归子程序法
- SDUT 2147 表达式语法分析——递归子程序法
- 2147 表达式语法分析——递归子程序法
- 编译原理实验三——语法分析(递归下降法)
- 编译原理实验二——语法分析(预测分析)
- 编译原理-递归子程序法
- 编译原理—语法分析
- 编译原理 实验3 递归下降语法分析程序设计
- 哈工大软件学院编译原理实验2——语法分析
- 哈工大编译原理实验2——语法分析
- 编译原理语法分析实验(Java实现)
- dos窗口%~dp0 失效
- show processlist 命令详解
- SQL四种语言:DDL,DML,DCL,TCL
- Xenserver vgpu环境搭建
- spring 定时任务cron在线表达式生成
- 2016年秋季《编译原理》课程实验(2014级)表达式语法分析——递归子程序法
- SSZipArchive的简单使用,不解压的情况下获取文件名
- appium获取屏幕分辨率
- IOS移动端(H5)alert/confirm提示信息去除网址(URL)
- Java Validation
- Oracle 使用MERGE INTO 语句更新数据
- 关于Spark运行流式计算程序中跑一段时间出现GC overhead limit exceeded
- Design a class named Queue for storing integers. Like a stack, a queue holds elements. In a stack,
- 算法学习-最长括号匹配