Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.

Similar presentations


Presentation on theme: "Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg."— Presentation transcript:

1 Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg

2 2 Outline Context-free languages –Context-free grammars –Parse trees –Push down 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 3

4 Parse trees S ⇒ a B ⇒ a b B ⇒ a b b B ⇒ a b b 4 S B B B ε a b b

5 Parse trees S → a B B → ε B → b B String: “abb” 5 S

6 Parse trees S → a B B → ε B → b B String: “abb” 6 S Ba

7 Parse trees S → a B B → ε B → b B String: “abb” 7 S B B a b

8 Parse trees S → a B B → ε B → b B String: “abb” 8 S B B B a b b

9 Parse trees S → a B B → ε B → b B String: “abb” 9 S B B B ε a b b

10 Parse trees S → a B B → ε B → b B String: “abb” 10 S B B B εabb

11 Exercise (1) Write grammars that produce a)strings over { a, b } that start and end with the same letter. Draw the parse tree for “abaaa”. b)palindromes over { a, b }. Draw the parse tree for “ababa”. c)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+)* 11

12 PDA Push down automata FA + stack 12

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

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

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

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


Download ppt "Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg."

Similar presentations


Ads by Google