1 / 17

First, Follow and Select sets

First, Follow and Select sets. COP4620 – Programming Language Translators Dr. Manuel E. Bermudez. Graph Algorithm for First sets Graph algorithm for Follow sets Select sets Define LL(1) Grammar. Topics. Top-Down Parsing.

afellows
Download Presentation

First, Follow and Select sets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. First, Follow and Select sets COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

  2. Graph Algorithm for First sets Graph algorithm for Follow sets Select sets Define LL(1) Grammar Topics

  3. Top-Down Parsing • Most parsing methods impose bounds on the amount of stack lookback and input lookahead. For programming languages, a common choice is (1,1). • We must define OPF (A,t), where A is the top element of the stack, and t is the first symbol on the input. • Storage requirements: O(n2), where n is the size of the grammar vocabulary (a few hundred).

  4. Ominiscientparsingfunction A … OPF (A, t) = A → ω if • ω =>* t, for some . • ω =>* ε, and S =>* At, for some , , where  =>* ε. ω t … or

  5. OPF Example S → A A → BAd B → b (Illustrating case 1): → C C →c OPF b c d B B → b B → bB → b C C → cC → c C → c S S → A S → A S → A A A → BAd A → C ??? OPF (A, b) = A → BAd because BAd =>* bAd OPF (A, c) = A → C because C =>* c i.e., B begins with b, and C begins with c. Red entries are optional. So is the ??? entry.

  6. opf Example (illustrating case 2): S → A A → bAd → OPF b d  S S → A S → A A A → bAd A → A → OPF (S, b) = S → A , because A =>* bAd OPF (S, d) = -------- , because S =>* αSdβ OPF (S,  ) = S → A , because S is legal OPF (A, b) = A → bAd , because A =>* bAd OPF (A, d) = A → , because S =>* bAd OPF (A,  ) = A → , because S =>*A

  7. First and follow sets Definition: First (A) = {t / A =>* t, for some } Follow (A) = {t / S =>* Atβ, for some , β} Computing First sets: • Build graph (Ф, δ), where (A,B)  δ if B → A,  =>* ε (First(A)  First(B)) • Attach to each node an empty set of terminals. • Add t to the set for A if A → t,  =>* ε. • Propagate the elements of the sets along the edges of the graph.

  8. Firstsets Example: S → ABCD A → CDA C → A B → BC → a D → AC → b → Nullable = {A, C, D} {b} {a, b} S B Black: Steps 2 and 3. Red: Propagating in step 4 {a} {a} A C {a} D

  9. Follow sets • Build graph (Ф, δ), where (A,B)  δ if A → B,  =>* ε. Follow(A)  Follow(B): any symbol X that follows A, also follows B. X A  α B ε

  10. Follow sets • Attach to each node an empty set of terminals. Add  to the set for the start symbol. • Add First(X) to the set for A (i.e. Follow(A)) if B → AX,  =>* ε. • Propagate the elements of the sets along the edges of the graph.

  11. Follow sets So, Follow(S)={ } Follow(A)= Follow(C)= Follow(D)={a,b, } Follow(B)={a, } Example: S → ABCD A → CDA C → A B → BC → a D → AC → b → Nullable = {A, C, D}First(S) = {a, b} First(C) = {a} First(A) = {a} First(D) = {a} First(B) = {b} S B a   A C a  a b  b Now, propagate b. And, propagate . a b  D

  12. OPF and LL(1) parsing Back to Parsing … We want OPF(A, t) = A → ω if either • t  First(ω), i.e. ω =>* tβ • ω =>* ε and t  Follow(A), i.e. S =>* A =>* Atβ A α ω t β A α ω ε t β

  13. LL(1) Parsing Definition: Select (A→ ω) = First(ω) U ifω =>* ε then Follow(A) else ø So PT(A, t) = A → ω if t  Select(A → ω) Time to call it PT (Parse Table) rather than OPF, because it isn’t omniscient.

  14. LL(1) Parsing First and Follow sets: First (S) = {a, b} Follow (S) = { } First (A) = {a} Follow(A) = {a, b, } First (B) = {b} Follow(B) = {a, } First (C) = {a} Follow (C) = {a, b, } First (D) = {a} Follow(D) = {a, b, } Grammar Selects sets S → ABCD {a, b} B → BC {b} → b {b} A → CDA {a, b, } → a {a} → {a, b, } C → A {a, b, } D → AC {a, b, } not disjoint not pair-wise disjoint Grammar is not LL(1)

  15. S → ABCD {a, b} B → BC {b} → b {b} A → CDA {a, b, } → a {a} → {a, b, } C → A {a, b, } D → AC {a, b, } L(1) parsing Non LL(1) grammar: multiple entries in PT. PT a b ┴ S S → ABCD S → ABCD A A → CDA, A→ a, A → A → CDA, A → A → CDA,A → B B → BC, B → b C C → A C → A C → A D D → AC D → AC D → AC

  16. 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 productions A→, A → with   , Select (A → ) ∩ Select (A →) =  • Previous example: grammar is not LL(1). • More later on what do to about it.

  17. Graph Algorithm for First sets Graph algorithm for Follow sets Select sets Define LL(1) Grammar summary

More Related