0

I would like to learn how to solve transferring grammar to LL(1). I have following problem:

E -> E + E
E -> E * E
E -> E[ E ]
E -> int
E -> id

Regarding operators '+' and '*' I know the solution:

E -> TA
A -> + TA
A -> epsilon

T -> FB
B -> * FB
B -> epsilon

the problem is what to do with indexing operator while in the same time we have to avoid left recursion?

Does anybody know the solution?

Thanks.

4

1 回答 1

1

(来自评论):

基于算术运算符的建议解决方案:

E -> T A
A -> + T A
A -> epsilon

T -> F B
B -> * F B
B -> epsilon

我们可以添加几乎类似的:

F -> G C
F -> int
C -> [ E ] C
C -> epsilon

并完成:

G -> id
G -> ( E )

最后一行带括号的表达式不在原始问题中,但添加它似乎是合理的。该F节与其他两个节的不同之处在于它拒绝整数文字的索引表达式(3[x]例如),尽管目标语言可能允许(C例如,它允许)在这种情况下F -> int应该用原始的G -> int.

于 2013-08-15T15:20:39.423 回答