Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a recursive-descent parser handle left recursion

Tags:

Could someone please explain to me why recursive-descent parsers can't work with a grammar containing left recursion?

like image 731
Benjamin Confino Avatar asked May 11 '09 09:05

Benjamin Confino


People also ask

Which parser Cannot handle left recursion?

A top-down parser cannot handle left recursive productions.

Why top-down parsing Cannot handle left recursion?

In short, top-down parsers usually try to guess what to do from limited information about the string. Because of this, they get confused by left recursion because they can't always accurately predict which productions to use.

What are the limitations of recursive descent parser?

The main limitation of recursive descent parsing (and top-down parsing algorithms in general) is that they only work on grammars with certain properties. For example, if a grammar contains any left recursion, recursive descent parsing doesn't work.

What is the problem with left recursion?

Left recursion often poses problems for parsers, either because it leads them into infinite recursion (as in the case of most top-down parsers) or because they expect rules in a normal form that forbids it (as in the case of many bottom-up parsers, including the CYK algorithm).


1 Answers

consider:

A ::= A B 

the equivalent code is

boolean A() {     if (A()) {         return B();     }     return false; } 

see the infinite recursion?

like image 163
cadrian Avatar answered Oct 12 '22 00:10

cadrian