Why can't a recursive-descent parser handle le

2020-01-30 06:49发布

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

2条回答
不美不萌又怎样
2楼-- · 2020-01-30 07:35

consider:

A ::= A B

the equivalent code is

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

see the infinite recursion?

查看更多
来,给爷笑一个
3楼-- · 2020-01-30 07:36

For whoever is interested

 A ::= A B | A C | D | E

can be rewritten as:

 A ::= (D | E) (B | C)*

The general form of the transformation is: any one of the non left recursive disjuncts followed by any number of the left recursive disjuncts without the first element.

Reforming the action code is a bit trickery but I thing that can be plug-n-chug as well.

查看更多
登录 后发表回答