1 / 34

Lesson 5

Lesson 5. CDT301 – Compiler Theory , Spring 2011 Teacher : Linus Källberg. Outline. The sets first and follow Non-recursive predictive parsing Handling syntax errors. The sets first and follow. Motivation. Grammar problematic for predictive parsing: stmt → func_call

talen
Download Presentation

Lesson 5

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. Lesson 5 CDT301 – CompilerTheory, Spring 2011 Teacher: Linus Källberg

  2. Outline • The sets first and follow • Non-recursive predictive parsing • Handling syntax errors

  3. The sets first and follow

  4. Motivation • Grammarproblematic for predictive parsing: stmt → func_call | loop func_call → id ( args ) ; loop → while ( expr ) block | for ( expr ; expr ; expr ) block

  5. Motivation stmt → func_call | loop func_call → id ( args ) ; loop → while ( expr ) block | for ( expr ; expr ; expr ) block • FIRST(func_call) = { id } • FIRST(loop) = { while, for }

  6. FIRST(α) • Simple case: α starts with a terminal a: FIRST(α) = { a } • Hardercase: α starts with a nonterminal A • Must examinewhat A canproduce • Ifα ⇒* ε then ε ∊ FIRST(α)

  7. Computing FIRST(X) • Start with Ø • If X is a terminal then add X and return • If X ⇒* ε then add ε • For all rules X → Y1Y2...Yk do • For all Yi, where i = 1..k, do • Add FIRST(Yi) except for ε • If ε is not in FIRST(Yi) then break

  8. FIRST example (4.30 in the book) • Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id • FIRST sets: FIRST(E) = { (, id } FIRST(T) = { (, id } FIRST(F) = { (, id } FIRST(E') = { +, ε } FIRST(T') = { *, ε }

  9. FIRST example (4.30 in the book) • Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id E ⇒ T E' ⇒ F T' E' ⇒ ( E ) T' E' ⇒ … E ⇒ T E' ⇒ F T' E' ⇒ idT' E' ⇒ … FIRST(E) = { (, id } seemscorrect!

  10. FIRST example (4.30 in the book) • Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id E' ⇒ + T E' ⇒ … + ∈ FIRST(E') seemscorrect! T' ⇒ * F T' ⇒ … * ∈ FIRST(T') seemscorrect!

  11. Exercise (1) • Compute FIRST(K) and FIRST(M): K → K , i : M K → i : M M → M , i M → i • Compute FIRST(S), FIRST(A), and FIRST(B): S → 1 A : A S → 0 : A A → A B A → ε B → 0 B → 1

  12. FOLLOW(A) • “What can follow A?” • Examplegrammar: S → a A b A c A → d | e • FOLLOW(A) = { b, c }

  13. Computing FOLLOW(A) • Start with Ø • If A is the start symbol then add $ • For all rules B → α A βdo • Add everything except ε from FIRST(β) • For all rules B → α A, or B → α A βwhereε ∊ FIRST(β), do • Add everything from FOLLOW(B)

  14. FOLLOW example(4.30 in the book) • Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id • FOLLOW sets: FOLLOW(E) = { $, ) } FOLLOW(E') = { $, ) } FOLLOW(T) = { +, $, ) } FOLLOW(T‘) = { +, $, ) } FOLLOW(F) = { *, +, $, ) }

  15. FOLLOW example(4.30 in the book) • Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id E $ ⇒ T E' $ ⇒ F T' E' $ ⇒ ( E ) T' E' $ ⇒ ( T E' ) T' E' $ ⇒ … FOLLOW(E) = { $, ) } seems correct! FOLLOW(E') = { $, ) } seems correct!

  16. FOLLOW example(4.30 in the book) • Grammar: E → T E' E' → + T E' | ε T → F T' T' → * F T' | ε F → ( E ) | id E $ ⇒ T E' $ ⇒ T + T E' $ ⇒ T + T $ ⇒ T + F T' $ ⇒ T + ( E ) T' $ ⇒ T + ( T E' ) T' $ ⇒ T + ( T ) T' $ ⇒ … FOLLOW(T) = { +, $, ) } seemscorrect!

  17. Exercise (2) • Compute FOLLOW(K) and FOLLOW(M): K → K , i : M K → i : M M → M , i M → i • Compute FOLLOW(S), FOLLOW(A), and FOLLOW(B): S → 1 A : A S → 0 : A A → A B A → ε B → 0 B → 1

  18. LL(1) grammars • Not left-recursive • Not ambiguous • For all A → α | β: • FIRST(α) ∩ FIRST(β) = Ø • Ifε∊ FIRST(α) then FOLLOW(A) ∩ FIRST(β) = Ø • Ifε∊ FIRST(β) thenFOLLOW(A) ∩ FIRST(α) = Ø

  19. Non-recursive predictive parsing

  20. Types of top-down parsers • Predictive recursive descent parsers • Lab 1 • General recursive descent parsers • Non-recursive predictive parsers

  21. Non-recursive predictive parsers • Keeps a stack of expected symbols • Loops: • Pop a symbol X • If X is a terminal, match with lookahead • If X is a nonterminal, predict and push

  22. Parse table • Encodes predictions:

  23. Demonstration Parse the string id * id using the previous parse table

  24. Constructing the parse table • For each rule A → αdo • For each terminal a in FIRST(α) do • Write A → αin position M[A, a] • If ε is in FIRST(α) then • For each element b in FOLLOW(A) do • Add A → α in position M[A, b]

  25. Handling syntax errors

  26. Types of errors • Lexical • Syntactic • Semantic • Logical

  27. Handling errors • Point out the spot • Tell the reason • Try to recover and proceed compiling • Do not generate code

  28. Recovery strategies • Panic mode • Phrase-level • Error productions • Global correction

  29. Panic mode • Discard until synchronizing token • What are good synchronizing tokens? • Properties: • Simple and fast • Might miss errors in discarded input

  30. Phrase-level • Try to “fix” the input • Replace a comma by a semicolon • Delete or insert a semicolon • …

  31. Error productions • Anticipate common errors • Add productions for these • One variant supported in Bison

  32. Global correction • Try to find alternative parse tree • Minimize corrections • Too costly

  33. Conclusion • The sets first and follow • Definition of LL(1) grammars • Non-recursive predictive parsing • Handling syntax errors

  34. Next time • Code generation usingsyntax-directedtranslation • Lexicalanalysis

More Related