LL1 GRAMMARS COP 4620 Programming Language Translators Dr

  • Slides: 9
Download presentation
LL(1) GRAMMARS COP 4620 – Programming Language Translators Dr. Manuel E. Bermudez

LL(1) GRAMMARS COP 4620 – Programming Language Translators Dr. Manuel E. Bermudez

TOPICS Define LL(1) Grammar Examples. Select sets. Parse Tables. “Model” PL grammar: Not LL(1).

TOPICS Define LL(1) Grammar Examples. Select sets. Parse Tables. “Model” PL grammar: Not LL(1). Problems: Left recursion Common prefices.

LL(1) GRAMMARS Definition: A CFG G is LL(1) (Left-to-right, Left-most, 1 -symbol lookahead) iff

LL(1) GRAMMARS Definition: A CFG G is LL(1) (Left-to-right, Left-most, 1 -symbol lookahead) iff for all A∊ , and for all A→ , , Select (A → ) ∩ Select (A → ) = Select(A → ) = First( ) ∪ Follow(A) if ⇒* ℇ First ( ) = {t / =>* tβ, for some β} Follow (A) = {t / S =>* γAtβ, for some γ, β} We’ll describe First, Follow, Select sets in the next Lecture)

LL(1) GRAMMARS Example: S→A {b, } A → b. Ad {b} { d, }

LL(1) GRAMMARS Example: S→A {b, } A → b. Ad {b} { d, } → OPF d S A Disjoint ! A→ Grammar is LL(1) b S→ A S→A A → b. Ad A→ PT (not OPF): At most ONE entry in each table slot.

LL(1) GRAMMARS Example: { bn+1 dn / n ≥ 0 } S→A {b} A

LL(1) GRAMMARS Example: { bn+1 dn / n ≥ 0 } S→A {b} A → b. Ad {b} → b {b} OPF d b S S→ A A A →b. Ad A →b Not Disjoint ! Grammar is not LL(1) PT: More than one entry in at least one table slot.

LL(1) GRAMMARS S SL E → → → begin SL end id : =

LL(1) GRAMMARS S SL E → → → begin SL end id : = E; SL S S E+T T {begin} {id} {begin, id} {(, id} T P → → P*T P (E) id {(, id} {(} {id} Select sets not disjoint Not LL(1)

NON-LL(1) GRAMMARS Left recursion always produces a non-LL(1) grammar, e. g. SL → SL

NON-LL(1) GRAMMARS Left recursion always produces a non-LL(1) grammar, e. g. SL → SL S → S Common prefices always produce a non-LL(1) grammar, e. g T → P*T → P

MODEL GRAMMAR S SL E T P → → → → → begin SL

MODEL GRAMMAR S SL E T P → → → → → begin SL end id : = E; SL S S E+T T P*T P (E) id {begin} {id} {begin, id} {(, id} {(} {id} Problems: 1. SL is left recursive. 2. E is left recursive. 3. T → P * T →P have common prefices. Showing a grammar is not LL(1): easy. PL grammars: “mostly” LL(1). This is our “model” PL sample grammar. We’ll use it throughout.

SUMMARY Defined LL(1) Grammar Examples. Select sets. Parse Tables. “Model” PL grammar: Not LL(1)

SUMMARY Defined LL(1) Grammar Examples. Select sets. Parse Tables. “Model” PL grammar: Not LL(1) Problems w/grammar.