160 likes | 295 Views
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.
E N D
Lesson 2 CDT301 – CompilerTheory, Spring 2011 Teacher: Linus Källberg
Outline • Context-freelanguages • Context-free grammars • Parsetrees • Pushdown 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
Parsetrees S ⇒ a B⇒ a b B⇒ a b bB⇒ a b b S a B b B b B ε
Parsetrees S → a B B → ε B → b B String: “abb” S
Parsetrees S → a B B → ε B → b B String: “abb” S a B
Parsetrees S → a B B → ε B → b B String: “abb” S a B b B
Parsetrees S → a B B → ε B → b B String: “abb” S a B b B b B
Parsetrees S → a B B → ε B → b B String: “abb” S a B b B b B ε
Parsetrees S → a B B → ε B → b B String: “abb” S B B B a b b ε
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+)*
PDA • Push down automata • FA + stack
PDA example • Language: { (), (()), ((())), … }
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.
Conclusion • Context-free languages • Context-free grammars • Parse trees • Push down automata
Next time • Specifying language syntax using CFGs • Introduction to parsing • Ambiguous grammars