Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
-
Upload
horatio-briggs -
Category
Documents
-
view
213 -
download
1
Transcript of Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
![Page 1: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/1.jpg)
Lesson 2
CDT301 – Compiler Theory, Spring 2011Teacher: Linus Källberg
![Page 2: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/2.jpg)
2
Outline
• Context-free languages– Context-free grammars– Parse trees– Push down automata
![Page 3: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/3.jpg)
Grammars
S → a BB → εB → b B
• Regex: ab*• Derivation of “abb”:
S a B a b B a b b B a b b⇒ ⇒ ⇒ ⇒
3
![Page 4: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/4.jpg)
4
Parse trees
S a B⇒ a b B⇒ a b b B⇒ a b b⇒
S
B
B
B
ε
a
b
b
![Page 5: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/5.jpg)
Parse trees
S → a BB → εB → b B
String: “abb”
5
S
![Page 6: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/6.jpg)
Parse trees
S → a BB → εB → b B
String: “abb”
6
S
Ba
![Page 7: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/7.jpg)
Parse trees
S → a BB → εB → b B
String: “abb”
7
S
B
B
a
b
![Page 8: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/8.jpg)
Parse trees
S → a BB → εB → b B
String: “abb”
8
S
B
B
B
a
b
b
![Page 9: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/9.jpg)
Parse trees
S → a BB → εB → b B
String: “abb”
9
S
B
B
B
ε
a
b
b
![Page 10: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/10.jpg)
Parse trees
S → a BB → εB → b B
String: “abb”
10
S
B
B
B
εa b b
![Page 11: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/11.jpg)
Exercise (1)
• Write grammars that producea) 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 “[email protected]”. Try to derive the string “a@b@c” (this should not be possible).
• Recall: S+(.S+)*@S+(.S+)* 11
![Page 12: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/12.jpg)
PDA
• Push down automata• FA + stack
12
![Page 13: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/13.jpg)
PDA example
• Language: { (), (()), ((())), … }
13
![Page 14: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/14.jpg)
Exercise (2)
• Write grammars fora) 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
![Page 15: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/15.jpg)
Conclusion
• Context-free languages– Context-free grammars– Parse trees– Push down automata
15
![Page 16: Lesson 2 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.](https://reader036.fdocument.org/reader036/viewer/2022083005/56649f1a5503460f94c2f88d/html5/thumbnails/16.jpg)
Next time
• Specifying language syntax using CFGs• Introduction to parsing• Ambiguous grammars
16