2008-09-21

Syntax analysis from the Dragoon Book

by Forrest Sheng Bao http://fsbao.net

I got quite confused about my compiler class this semester. The textbook we used is the famous "Dragoon Book" - Compilers, Principles, Techniques and Tools, by Alfred V. Aho, Monica S. Lam, Ravi Sethi and Jeffrey D. Ullman. Now I have figured everything out regarding syntax analysis. So I decide to write the algorithms in my way.

How to eliminate left recursion

Replace the rule B→BP1|⋯|BPm|Pm+1|⋯|Pn

by B→Pm+1B′|⋯|PnB′ andB′→P1B′|⋯|PmB′|ϵ

How to eliminate common prefixes

Replace the rule A→αβ1|αβ2|⋯|αβn|Σ1|⋯|Σn+m

by A→αA′|Σ1|⋯|Σn+m

and A′→β1|⋯|βn

Top-down parsing

To rule X→Y1Y2⋯Yn

First(X):

1. First(Y1)⊆First(X)
2. If Y1⋯Yk⇒*ϵ , then First(Yk+1)⊆First(X)
3. If Yi→ϵ,∀i , then ϵ∈First(X)

Follow(X):

1. $ is contained in Follow(X)
2. If ∀A→αSβ, then First(β)/ϵ⊆Follow(S)
3. If A→αB or A→αBβ and β⇒*ϵ , then Follow(A) ⊆ Follow(B)

No comments: