Finding a grammar is not LL(1) without using classical methods and transforming it to LL(1)

时光总嘲笑我的痴心妄想 提交于 2019-12-31 06:04:06

问题


Let's say i have this grammar:

S -> A C x | u B A
A -> z A y | S u | ε
B -> C x | y B u
C -> B w B | w A

This grammar is obviously not LL(1), which i can find constructing the parsing table. But is there any way i can prove that this grammar is not LL(1) without using the classical methods i.e. without constructing the parsing table or finding any conflicts?

Also how can i convert this grammar to LL(1)? I think i have to use both epsilon-derivation elimination and left recursion elimination but its a bit tricky and as many times i've tried i couldn't transform it to LL(1).

Thank you in advance.


回答1:


Both S/A and B/C involve indirect left-recursion.

Since no left-recursive grammar (direct or indirect) is LL(k) for any k, you can prove the grammar is not LL(1) simply by showing the left-recursive cycle. (On the other hand, if you have a tool which computes FIRST and FOLLOW sets, the "classical" method is really very simple.)

Eliminating indirect left recursion involves first finding one possible topological sort of the non-terminals and then breaking the derivation cycle by substituting some uses of a non-terminal with its right-hand side. After that, the simple left recursion elimination algorithm can be applied.

You can find concrete examples of this transformation here on StackOverflow, or here, or in any good textbook on parsing theory. (Or, of course, by searching for the term "indirect left recursion" and looking for pages with some credibility.)



来源:https://stackoverflow.com/questions/53611803/finding-a-grammar-is-not-ll1-without-using-classical-methods-and-transforming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!