2 分钟阅读

DefinitionPermalink

Why eliminate?

Top-down parsing methods cannot handle left-recursive grammars. It causes endless recursive calls.

Definition: A grammar G=(V,T,S,P) is left-recursive AV such that a derivation A+Aα, where α(VT).

Eliminating Direct Left RecursionPermalink

Consider the easiest case:

AAαβ

where α,β(VT) and β does not begin with A.

It can be rewritten to:

AβAAαAε

and done! The rewriting simply converts left recursion to right recursion.

If you don’t like introducing an ε-production, rewrite this way: AββAAααA

If we have more productions like:

AAα1Aα2Aαmβ1β2βn

where αi,βj(VT), and βj does not begin with A, we can rewrite the same way:

Aβ1Aβ2AβnAAα1Aα2AαmAε

If you don’t like introducing an ε-production, rewrite this way: Aβ1β2βnβ1Aβ2AβnAAα1α2αmα1Aα2AαmA

Eliminating Indirect Left RecursionPermalink

Indirect left recursion involves multiple derivation steps, like {ABCBAD which essentially leads to AADC.

Intuition: If we know there would be an indirect left recursion A+Aα, we can repeatedly substitute the leftmost variable B in production ABβ until it becomes a direct left recursion.

Problem: How do we find such variable A that is bound to an indirect left recursion? It’ simply not practical to try all variables in V.

Method #1: VDG (Variable Dependency Graph) on LHS variables and leftmost variables on RHS. E.g. draw an edge AB if (ABβ)P. If vertex A has a loop in VDG, then it has an indirect left recursion.

Method #2: Variable Relabeling:

  1. Relabel the variables in some order A1,A2,,An. Any order should work, but the naive one (left-to-right in any single production, top-down in the production list) is the most straightforward.
  2. If there is an indirect left recursion, it must be like {AiAjαAjAiβ (or more productions involved, depending on the length of the Ai-loop in VDG described above).
  3. Therefore whenever we see a production AjAiβ where j>i, it is possible that Ai has an indirect left recursion.
    • We say “possible” because an AiAj path might not exist in the VDG to begin with.

However anyway we can just substitute Ai in such AjAiβ productions where j>i. It suffices to break any Ai-loop (if exists) in the VDG.

In the Dragon Book we have the following algorithm:

Note that it uses subscripts i,j the other way, where j<i is guaranteed in the double for-loops.

分类:

更新时间:

留下评论