编写一个LR分析程序python: 1.任意输入一个输入串,可得到成功的分析或错误的提示,输出其分析过程. 2.通过语法分析的同时给出语义分析的结果。
时间: 2024-03-20 11:40:55 浏览: 21
这里提供一个使用Python的LR分析程序示例,以SLR算法为例:
```python
from collections import deque
# 定义文法规则
grammar = {
'S': ['E'],
'E': ['E+T', 'T'],
'T': ['T*F', 'F'],
'F': ['(E)', 'id']
}
# 定义SLR分析表
action_table = {
0: {'(': 'S5', 'id': 'S4'},
1: {'+': 'S6', '$': 'acc'},
2: {'+': 'R2', '*': 'S7', ')': 'R2', '$': 'R2'},
3: {'+': 'R4', '*': 'R4', ')': 'R4', '$': 'R4'},
4: {'(': 'S5', 'id': 'S4'},
5: {'(': 'S5', 'id': 'S4'},
6: {'(': 'S5', 'id': 'S4'},
7: {'(': 'S5', 'id': 'S4'},
8: {'+': 'R1', '*': 'R1', ')': 'R1', '$': 'R1'}
}
goto_table = {
0: {'E': 1, 'T': 2, 'F': 3},
4: {'E': 8, 'T': 2, 'F': 3},
5: {'E': None, 'T': 9, 'F': 3},
6: {'T': 10, 'F': 3},
7: {'F': 11}
}
# 定义语义动作
def semantic_action(action, stack):
if action == 'S4':
stack.append(('id', input_str.popleft()))
elif action == 'S5':
stack.append(('(', '('))
elif action == 'S6':
stack.append(('+', '+'))
elif action == 'S7':
stack.append(('*', '*'))
elif action == 'R1':
E2 = stack.pop()[1]
stack.pop()
E1 = stack.pop()[1]
stack.append(('E', E1 + '+' + E2))
elif action == 'R2':
T = stack.pop()[1]
stack.pop()
F = stack.pop()[1]
stack.append(('E', F + T))
elif action == 'R4':
F = stack.pop()[1]
stack.pop()
stack.append(('T', F))
elif action == 'acc':
return stack[0][1]
return None
# 定义SLR分析程序
def slr_parser(input_str):
input_str = deque(list(input_str) + ['$'])
stack = [(0, None)]
while True:
state = stack[-1][0]
lookahead = input_str[0]
action = action_table[state].get(lookahead)
if action is None:
return 'Error'
elif action.startswith('S'):
stack.append((int(action[1:]), lookahead))
input_str.popleft()
elif action.startswith('R'):
rule = grammar[list(grammar.keys())[int(action[1:]) - 1]]
for _ in range(len(rule)):
stack.pop()
state = stack[-1][0]
stack.append((goto_table[state][list(rule[0])[0]], None))
result = semantic_action(action, stack)
if result is not None:
return result
# 测试
input_str = 'id+id*id'
result = slr_parser(input_str)
print(result)
```
上述代码中,首先定义了文法规则和SLR分析表。然后定义了语义动作,在分析过程中通过语义动作来进行语义分析。最后定义了SLR分析程序,使用输入串进行分析,并输出分析结果。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)