ll(1)的递归下降分析法完整代码
时间: 2023-12-31 22:06:38 浏览: 45
以下是 LL(1) 递归下降分析法的完整代码示例:
```python
# 定义文法
grammar = {
'S': ['AB', 'BC'],
'A': ['a'],
'B': ['b'],
'C': ['c']
}
# 定义预测分析表
predictive_table = {
('S', 'a'): ['A', 'B'],
('S', 'b'): ['B', 'C'],
('A', 'a'): ['a'],
('B', 'b'): ['b'],
('C', 'c'): ['c']
}
# 定义分析函数
def analyze(input_str):
stack = ['$']
input_str += '$'
index = 0
symbol = stack[-1]
while symbol != '$':
if symbol == input_str[index]:
stack.pop()
index += 1
else:
production = predictive_table.get((symbol, input_str[index]), None)
if production is None:
print('Error: invalid input string')
return False
stack.pop()
stack += reversed(production)
symbol = stack[-1]
print('Input string is valid')
return True
# 测试输入字符串
input_str = 'abc'
analyze(input_str)
```
注:上述代码仅为示例,文法和预测分析表可以根据实际情况进行修改。
相关推荐
![cpp](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)