Left Recursive Rules in Context Free Grammar

試著忘記壹切 提交于 2019-12-11 04:02:54

问题


So everything I have been reading says that left recursive rules in a CFG traverses infinitely and proceeds to show a process of converting it to right recursive rule and accomplishes this using using alpha and beta terms to represent multiple non-terminals (I think I got that part right). So in my mind left recursive rules=bad when dealing with LL parsers.

So is there a situation that left recursive rules are acceptable/desirable? If not, why do left recursive rules exist asside from bad design or term to describe the 'inverse' (term used lightly) of right recursive rules?

This is not technically a homework question, but it is related to my class.


回答1:


The key to your question is that left recursive rules are only bad when dealing with LL parsers. If you're using a LR parser I often feel the grammar becomes much clearer. Takes for instance the standard expression grammar

E : E + T | T
T : T * F | F
F : number | ( E )

Which clearly is left recursive, but is easier to read than its right recursive variant. When the parser in question has no problem dealing with the original grammar, then there would be no point in factoring this grammar to be right-recursive.

In this case you also get the benefit of having a grammar that will not require epsilon rules, which the corresponding LL compatible grammar would.



来源:https://stackoverflow.com/questions/9198080/left-recursive-rules-in-context-free-grammar

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