1
Chapter Twelve: Context-Free Languages
Formal Language, chapter 12, slide 1
Chapter Twelve: Context-Free Languages Formal Language, chapter 12, - - PowerPoint PPT Presentation
Chapter Twelve: Context-Free Languages Formal Language, chapter 12, slide 1 1 We defined the right-linear grammars by giving a simple restriction on the form of each production. By relaxing that restriction a bit, we get a broader class of
1
Formal Language, chapter 12, slide 1
2
Formal Language, chapter 12, slide 2
3
Formal Language, chapter 12, slide 3
4
Formal Language, chapter 12, slide 4
5
Formal Language, chapter 12, slide 5
6
Formal Language, chapter 12, slide 6
7
Formal Language, chapter 12, slide 7
8
Formal Language, chapter 12, slide 8
9
– A program is a finite, structured, mechanical thing that specifies a potentially infinite collection of runtime behaviors – You have to imagine how the code you are crafting will unfold when it executes
– A grammar is a finite, structured, mechanical thing that specifies a potentially infinite language – You have to imagine how the productions you are crafting will unfold in the derivations of terminal strings
Formal Language, chapter 12, slide 9
10
Formal Language, chapter 12, slide 10
11
S U 1 T 1 1
Formal Language, chapter 12, slide 11
12
Formal Language, chapter 12, slide 12
13
Formal Language, chapter 12, slide 13
14
Formal Language, chapter 12, slide 14
15
– Each a on the left can be paired with three bs on the right – That gives
– Each symbol on the left (either a or b) can be paired with one on the right (either c or d) – That gives
Formal Language, chapter 12, slide 15
16
– We can make grammars for {anbn} and {cmdm}: – Now every string in L consists of a string from the first followed by a string from the second – So combine the two grammars and add a new start symbol:
Formal Language, chapter 12, slide 16
17
Formal Language, chapter 12, slide 17
18
Formal Language, chapter 12, slide 18
19
Formal Language, chapter 12, slide 19
20
Formal Language, chapter 12, slide 20
21
Formal Language, chapter 12, slide 21
22
Formal Language, chapter 12, slide 22
23
Formal Language, chapter 12, slide 23
24
– while (a<b) { c = c * a; a = a + a; }
Formal Language, chapter 12, slide 24
25
Formal Language, chapter 12, slide 25
26
Formal Language, chapter 12, slide 26
27
Formal Language, chapter 12, slide 27
28
Formal Language, chapter 12, slide 28
29
– Syntax: it demonstrates that a-b*c is in the language – Also, the beginnings of semantics: it is a plan for evaluating the expression when the program is run – First evaluate a-b, then multiply that result by c
Formal Language, chapter 12, slide 29
30
Formal Language, chapter 12, slide 30
31
Formal Language, chapter 12, slide 31
32
Formal Language, chapter 12, slide 32
33
Formal Language, chapter 12, slide 33
34
Formal Language, chapter 12, slide 34
35
– Strict precedence: *, then -, then <, then = – Strict associativity: left, so a-b-c is computed as (a-b)-c
– The code for the parser part of a compiler can be generated automatically from the grammar by a parser-generator – Such programs really want unambiguous grammars
Formal Language, chapter 12, slide 35
36
Formal Language, chapter 12, slide 36
37
Formal Language, chapter 12, slide 37
38
Formal Language, chapter 12, slide 38
39
Formal Language, chapter 12, slide 39
40
Formal Language, chapter 12, slide 40
41
Formal Language, chapter 12, slide 41