写出文法是:E=E+T|E-T|T,T=T*F|T/F|F,F=F!P|P,P=(E)|N(L)|id,N=sin|cos|exp|abs|log|int,L=L,E|E的句子x+y/y!2的派生树。
时间: 2023-10-26 20:05:27 浏览: 83
E -> E + T | E - T | T
T -> T * F | T / F | F
F -> F ! P | P
P -> (E) | N(L) | id
N -> sin | cos | exp | abs | log | int
L -> L , E | E
句子 x + y / y!2 的派生树如下:
E
|
E + T
| |
T F
| | |
F P P
| | | |
id E / F N(L)
| | | | |
x T F L |
| | | |
y / P E
| | |
y ! 2
相关问题
/* 0. S->E */ /* 1. E->E+T */ /* 2. E->T */ /* 3. T->T*P */ /* 4. T->P */ /* 5. P->i */ /* 6. P->(E) */写出基于表的LL(1)分析程序
基于表的LL(1)分析程序是一种语法分析方法,用于根据给定的文法和输入串来构建语法树。它使用一个预测分析表来指导分析过程。下面是一个基于表的LL(1)分析程序的示例:
```python
# 定义预测分析表
# 行表示非终结符,列表示终结符
# M[A, a]表示在非终结符A和终结符a的情况下,应该采取的产生式
M = {
'S': {'i': 0, '(': 0},
'E': {'i': 1, '(': 1},
'T': {'i': 2, '(': 2},
'P': {'i': 5, '(': 6, ')': -1, '+': -1, '*': -1, '$': -1}
}
# 输入串
input_str = 'i+i*i$'
# 分析栈
stack = ['$']
# 输入串指针
input_ptr = 0
# 开始符号
start_symbol = 'S'
# 将开始符号入栈
stack.append(start_symbol)
# 分析过程
while True:
# 栈顶符号
top = stack[-1]
# 输入串当前字符
current_char = input_str[input_ptr]
if top == '$' and current_char == '$':
# 分析成功
print('分析成功')
break
elif top in M and current_char in M[top]:
# 根据预测分析表进行相应操作
production = M[top][current_char]
if production == -1:
# 错误处理
print('语法错误')
break
else:
# 弹出栈顶符号
stack.pop()
# 将产生式右部逆序入栈
for symbol in reversed(production):
stack.append(symbol)
elif top == current_char:
# 匹配成功,弹出栈顶符号和输入串指针后移
stack.pop()
input_ptr += 1
else:
# 错误处理
print('语法错误')
break
```
用符号表示如下: E→T|E+T|E-T T→F|T*F|T/F F→i|(E) 2、消去上面文法的左递归 E→TE1 E1 →+TE1|-TE1|ε T→FT1 T1→*FT1|/FT1|ε F →i|(E) 3. 按消去左递归的文法,使用Python语言,写出对应的递归下降语法分析器。
以下是Python代码:
```
def match_terminal(expected_token, tokens, index):
if tokens[index] == expected_token:
return index + 1
else:
return -1
def parse_E(tokens, index):
index = parse_T(tokens, index)
if index == -1:
return -1
else:
return parse_E1(tokens, index)
def parse_E1(tokens, index):
next_index = match_terminal('+', tokens, index)
if next_index != -1:
next_index = parse_T(tokens, next_index)
if next_index == -1:
return -1
else:
return parse_E1(tokens, next_index)
else:
next_index = match_terminal('-', tokens, index)
if next_index != -1:
next_index = parse_T(tokens, next_index)
if next_index == -1:
return -1
else:
return parse_E1(tokens, next_index)
else:
return index
def parse_T(tokens, index):
index = parse_F(tokens, index)
if index == -1:
return -1
else:
return parse_T1(tokens, index)
def parse_T1(tokens, index):
next_index = match_terminal('*', tokens, index)
if next_index != -1:
next_index = parse_F(tokens, next_index)
if next_index == -1:
return -1
else:
return parse_T1(tokens, next_index)
else:
next_index = match_terminal('/', tokens, index)
if next_index != -1:
next_index = parse_F(tokens, next_index)
if next_index == -1:
return -1
else:
return parse_T1(tokens, next_index)
else:
return index
def parse_F(tokens, index):
next_index = match_terminal('i', tokens, index)
if next_index != -1:
return next_index
else:
next_index = match_terminal('(', tokens, index)
if next_index != -1:
next_index = parse_E(tokens, next_index)
if next_index == -1:
return -1
else:
return match_terminal(')', tokens, next_index)
else:
return -1
def parse(tokens):
index = parse_E(tokens, 0)
if index != -1 and index == len(tokens):
print("RIGHT, AGAIN! 语法分析完毕!")
else:
print("WRONG! 退出语法分析!")
tokens = ['i', '+', 'i', '*', 'i', '#']
parse(tokens)
```
阅读全文