若有文法 statement→ assign-stmt | call-stmt| other; assign-stmt → identifier := exp; call-stmt → identifier ( exp-list ) 请写出由递归下降分析该文法的伪代码
时间: 2023-06-11 10:10:20 浏览: 90
以下是由递归下降分析该文法的伪代码:
```
1. statement():
if current_token == identifier:
if lookahead_token == ':=':
assign_stmt()
elif lookahead_token == '(':
call_stmt()
else:
other()
else:
error()
2. assign_stmt():
identifier()
match(':=')
exp()
3. call_stmt():
identifier()
match('(')
exp_list()
match(')')
4. exp_list():
exp()
while lookahead_token == ',':
match(',')
exp()
5. identifier():
match(identifier)
6. exp():
# Expression parsing logic goes here
7. match(token):
if current_token == token:
advance_to_next_token()
else:
error()
```
在该伪代码中,`statement()` 是主函数,它根据当前的 token 选择调用 `assign_stmt()`、`call_stmt()` 或 `other()` 函数。`assign_stmt()` 函数处理赋值语句,`call_stmt()` 函数处理函数调用语句,`exp_list()` 函数处理参数列表,`identifier()` 函数处理标识符,`exp()` 函数处理表达式。`match()` 函数用于匹配当前 token 是否为预期的 token,如果匹配失败则抛出错误。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)