1 / 16

Lesson 2

Lesson 2. CDT301 – Compiler Theory , Spring 2011 Teacher : Linus Källberg. Outline. Context-free languages Context-free grammars Parse trees Push down automata. Grammars. S → a B B → ε B → b B Regex: ab* Derivation of “ abb ”: S ⇒ a B ⇒ a b B ⇒ a b b B ⇒ a b b.

gaerwn
Download Presentation

Lesson 2

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

  2. Outline • Context-freelanguages • Context-free grammars • Parsetrees • Pushdown automata

  3. Grammars S → a B B → ε B → b B • Regex: ab* • Derivation of “abb”: S ⇒ a B ⇒ a b B ⇒ a b b B ⇒ a b b

  4. Parsetrees S ⇒ a B⇒ a b B⇒ a b bB⇒ a b b S a B b B b B ε

  5. Parsetrees S → a B B → ε B → b B String: “abb” S

  6. Parsetrees S → a B B → ε B → b B String: “abb” S a B

  7. Parsetrees S → a B B → ε B → b B String: “abb” S a B b B

  8. Parsetrees S → a B B → ε B → b B String: “abb” S a B b B b B

  9. Parsetrees S → a B B → ε B → b B String: “abb” S a B b B b B ε

  10. Parsetrees S → a B B → ε B → b B String: “abb” S B B B a b b ε

  11. Exercise (1) • Write grammars that produce • strings over { a, b } that start and end with the same letter. Draw the parse tree for “abaaa”. • palindromes over { a, b }. Draw the parse tree for “ababa”. • valid e-mail addresses. Draw the parse tree for “ab@c.d”. Try to derive the string “a@b@c” (this should not be possible). • Recall: S+(.S+)*@S+(.S+)*

  12. PDA • Push down automata • FA + stack

  13. PDA example • Language: { (), (()), ((())), … }

  14. Exercise (2) • Write grammars for • general parenthesis expressions:{ ε, (), ()(), (()), ()(()), ((()))()(), … }. • simple infix addition between single-digit numbers, with support for prioritizing with parentheses:{ 7, 1 + 2, 1 + (9 + 1), ((6) + 0), … }. • Create PDAs for the above languages. • Create a PDA that accepts palindromes.

  15. Conclusion • Context-free languages • Context-free grammars • Parse trees • Push down automata

  16. Next time • Specifying language syntax using CFGs • Introduction to parsing • Ambiguous grammars

More Related