Exp →LP Exp RP | MINUS Exp | ID LP Args RP | INT | FLOAT Args → Exp COMMA Args | Exp将该文法转化为LL1文法,并给出该文法预测分析表
时间: 2023-06-10 19:07:47 浏览: 99
将该文法转化为LL1文法的步骤如下:
1. 消除左递归
Exp → LP Exp RP | MINUS Exp | ID LP Args RP | INT | FLOAT
Args → Exp Args'
Args' → COMMA Exp Args' | ε
2. 提取公因子
Exp → LP Exp RP | MINUS Exp | ID LP Args RP | INT | FLOAT
Args → Exp Args'
Args' → COMMA Exp Args'' | ε
Args'' → Args'
3. 添加ε产生式
Exp → LP Exp RP | MINUS Exp | ID LP Args RP | INT | FLOAT
Args → Exp Args'
Args' → COMMA Exp Args'' | ε
Args'' → Exp Args''
4. 消除左递归
Exp → LP Exp RP Exp'
Exp' → MINUS Exp Exp' | ID LP Args RP Exp' | INT Exp' | FLOAT Exp'
Args → Exp Args'
Args' → COMMA Exp Args'' | ε
Args'' → Exp Args''
最终得到的LL1文法如下:
Exp → LP Exp RP Exp'
Exp' → MINUS Exp Exp' | ID LP Args RP Exp' | INT Exp' | FLOAT Exp' | ε
Args → Exp Args'
Args' → COMMA Exp Args'' | ε
Args'' → Exp Args''
预测分析表如下:
| | LP | RP | MINUS | ID | INT | FLOAT | COMMA | $ |
|----|----|----|-------|----|-----|-------|-------|---|
| Exp | Exp → LP Exp RP Exp' | | Exp → MINUS Exp Exp' | Exp → ID LP Args RP Exp' | Exp → INT Exp' | Exp → FLOAT Exp' | | |
| Exp' | | | Exp' → MINUS Exp Exp' | Exp' → ID LP Args RP Exp' | Exp' → INT Exp' | Exp' → FLOAT Exp' | Exp' → ε | ε |
| Args | Args → Exp Args' | | Args → Exp Args' | Args → Exp Args' | Args → Exp Args' | Args → Exp Args' | | |
| Args' | | Args' → ε | | Args' → COMMA Exp Args'' | Args' → ε | Args' → ε | Args' → COMMA Exp Args'' | ε |
| Args'' | Args'' → Exp Args'' | | Args'' → Exp Args'' | Args'' → Exp Args'' | Args'' → Exp Args'' | Args'' → Exp Args'' | | ε |
阅读全文