Sicily1000. 自上而下语法分析(一)
来源:互联网 发布:java 两list合并 编辑:程序博客网 时间:2024/06/05 18:53
Sicily1000. 输入输出LL(1)语法分析程序
题目
输入开始符号,非终结符,终结符,产生式,LL(1)分析表
输出LL(1)分析表G[E]:E →E+T | E-T | T
T →T*F | T/F | F
F →(E) | D
D →x | y | z消除左递归G1[E]:
E →TA
A →+TA | -TA | e
T →FB
B →*FB | /FB | e
F →(E) | D
D →x | y | zInput
输入开始符号;
非终结符个数,非终结符,空格符分隔;
终结符个数,终结符,空格符分隔;
产生式的个数,各产生式的序号,产生式的左边和右边符号,空格符分隔;
LL(1)分析表中的产生式个数,序号,行符号,列符号,产生式编号,空格符分隔;Output
第一行:空,安终结符循序输出终结符,结束符‘#’,每个符号占5格;
其余行:非终结符符号,各对应终结符的产生式的右边,每个符号占5格;Sample Input
Sample Output
解题分析
通过题目的例子我们不难发现,题目就是要求我们根据产生式构造出LL(1)分析表。我们先结合上面的例子来看看LL(1)分析表是如何构建的。
可以看到,LL(1)分析表的行是各个非终结符,列是各个终结符。然后结合输入的产生式,比如第一个LL(1)分析表产生式是:1 E ( 1。通过题目我们知道这个式子表示行为E、列为(对应的位置是1号产生式,即1 E TA。其它式子依此类推,就可以得到LL(1)分析表。现在问题的关键是如何将非终结符、终结符、产生式、LL(1)产生式进行存储。根据题目的例子,我们知道上面每个都有几个表项,因此不难想到要用结构体。想到了这一点,问题就好办多了。
根据题目的样例,我们知道,#也属于终结符,但输入中并没有体现,因此,在输入的最后还要将其进行特殊处理、加入到终结符中。
然后根据我们上面提到的构造LL(1)分析表的思路,我们对所有的LL(1)分析表产生式进行遍历,根据产生式的行符号、列符号和产生式序号进行一一对应填充,最后将其输出,就顺利解决了。
源代码
#include<iostream>#include<string>using namespace std;struct non_terminal{ int count; string sign[100]; string LL[100][100];};struct terminal{ int count; string sign[100];};struct produce{ int count; int number[100]; string left[100]; string right[100];};struct LL1_produce{ int count; int number[100]; string row[100]; string col[100]; int order[100];};int main() { non_terminal non_terminal; terminal terminal; produce produce; LL1_produce LL1_produce; string start; cin >> start; cin >> non_terminal.count; for (int i = 0; i < non_terminal.count; i++) { cin >> non_terminal.sign[i]; } cin >> terminal.count; for (int i = 0; i < terminal.count; i++) { cin >> terminal.sign[i]; } cin >> produce.count; for (int i = 0; i < produce.count; i++) { cin >> produce.number[i] >> produce.left[i] >> produce.right[i]; } cin >> LL1_produce.count; for (int i = 0; i < LL1_produce.count; i++) { cin >> LL1_produce.number[i] >> LL1_produce.row[i] >> LL1_produce.col[i] >> LL1_produce.order[i]; } terminal.sign[terminal.count] = "#"; terminal.count++; for (int i = 0; i < LL1_produce.count; i++) { int row, col; for (int j = 0; j < non_terminal.count; j++) { if (non_terminal.sign[j] == LL1_produce.row[i]) { row = j; break; } } for (int k = 0; k < terminal.count; k++) { if (terminal.sign[k] == LL1_produce.col[i]) { col = k; break; } } non_terminal.LL[row][col] = produce.right[LL1_produce.order[i] - 1]; } cout << " "; for (int i = 0; i < terminal.count; i++) { cout << " " << terminal.sign[i]; } cout << endl; for (int i = 0; i < non_terminal.count; i++) { for (int j = 0; j < 5 - non_terminal.sign[i].size(); j++) { cout << " "; } cout << non_terminal.sign[i]; for (int j = 0; j < terminal.count; j++) { for (int k = 0; k < 5 - non_terminal.LL[i][j].size(); k++) { cout << " "; } cout << non_terminal.LL[i][j]; } cout << endl; } return 0;}
以上是我对这道问题的一些想法,有问题还请在评论区讨论留言~
- Sicily1000. 自上而下语法分析(一)
- 自上而下语法分析LL(1)
- Sicily1001. 自上而下语法分析(二)
- 语法分析:自上而下分析
- sicily1000. 输入输出LL(1)语法分析程序
- 编译原理,自上而下非递归语法分析自上而下的语法分析
- USB开发—自上而下(一)
- 自上而下的语法分析,LL(1)文法(消除左递归,提取左因子)
- 自上而下的语法分析(递归下降法,LL(1)预测分析法,预测分析表)
- 自上而下的语法分析方法之预测分析
- 【编译原理】语法分析(一)
- 【编译原理】自上而下的语法分析之预测分析法
- 编译原理笔记7 自上而下语法分析-…
- 编译原理笔记8 自上而下语法分析-…
- 编译原理笔记9 自上而下语法分析-…
- kotlin语法分析(一)
- 网络协议 2(自上而下)
- USB开发—自上而下(二)
- 图片
- SpringMVC配置
- Linux下使用Nginx端口转发出现502错误的一种解决办法
- 1.5 复用具体实现
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Sicily1000. 自上而下语法分析(一)
- bzoj1225 [HNOI2001] 求正整数 约数个数定理+对数
- Javascript闭包——懂不懂由你,反正我是懂了
- python 切片方法
- xss攻击防御
- Restful API 设计原则及其规范
- React学习笔记_css module
- ConcurrentHashMap的实现 get put remove 详解
- 吸顶条