Lecture 8
Lecture 8
1
Context-Free Languages
Context-Free Pushdown
Grammars Automata
stack
automaton
2
Context-Free Grammars
3
Grammars
Grammars express languages
predicate verb
4
article a
article the
noun cat
noun dog
verb runs
verb walks
5
A derivation of “the dog walks”:
6
A derivation of “a cat runs”:
7
Language of the grammar:
L = { “a cat runs”,
“a cat walks”,
“the cat runs”,
“the cat walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”,
“the dog walks” }
8
Notation
Production Rules
noun cat
noun dog
Variable Terminal
9
Another Example
Grammar: S aSb
S
Derivation of sentence ab :
S aSb ab
S aSb S
10
Grammar: S aSb
S
S aSb S
11
Other derivations:
12
Language of the grammar
S aSb
S
n n
L {a b : n 0}
13
More Notation
Grammar G V ,T , S , P
V : Set of variables
Grammar G: S aSb
S
G V ,T , S , P
V {S } T {a, b}
P {S aSb, S }
15
More Notation
Sentential Form:
A sentence that contains
variables and terminals
Example:
Instead of:
17
*
In general we write: w1 wn
If: w1 w2 w3 wn
18
*
By default: w w
19
Example
Grammar Derivations
S aSb *
S
S
*
S ab
*
S aabb
*
S aaabbb
20
Example
Grammar Derivations
S aSb
S aaSbb
S
aaSbb aaaaaSbbbbb
21
Another Grammar Example
Grammar G : S Ab
A aAb
A
Derivations:
S ⇒ Ab ⇒ b
S ⇒ Ab ⇒ aAbb ⇒ abb
S ⇒ Ab ⇒ aAbb ⇒ aaAbbb ⇒ aabbb
22
More Derivations
S Ab aAbb aaAbbb aaaAbbbb
aaaaAbbbbb aaaabbbbb
S aaaabbbbb
S aaaaaabbbbbbb
n n
S a b b
23
Language of a Grammar
For a grammar G
with start variable S:
L(G ) {w : S w}
String of terminals
24
Example
For grammar G: S Ab
A aAb
A
n n
L(G ) {a b b : n 0}
n n
Since: S a b b
25
A Convenient Notation
A aAb
A aAb |
A
article a
article a | the
article the
26
Example
A derivation:
Another derivation:
n n
L(G ) {a b : n 0}
29
Example
A derivation:
Another derivation:
A derivation:
S SS aSbS abS ab
32
A context-free grammar G: S aSb
S SS
S
A derivation:
Grammar G (V , T , S , P)
*
L(G ) {w : S w, w T *}
35
Definition: Context-Free Languages
A language L is context-free
if and only if
36
Derivation Order
1. S AB 2. A aaA 4. B Bb
3. A 5. B
Leftmost derivation:
1 2 3 4 5
S AB aaAB aaB aaBb aab
Rightmost derivation:
1 4 5 2 3
S AB ABb Ab aaAb aab
37
S aAB
A bBb
B A|
Leftmost derivation:
S aAB abBbB abAbB abbBbbB
abbbbB abbbb
Rightmost derivation:
S aAB aA abBb abAb
abbBbb abbbb
38
Derivation Trees
39
S AB A aaA | B Bb |
S AB
S
A B
40
S AB A aaA | B Bb |
S AB aaAB
S
A B
a a A
41
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
42
S AB A aaA | B Bb |
A B
a a A B b
43
S AB A aaA | B Bb |
A B
a a A B b
44
S AB A aaA | B Bb |
A B
yield
a a A B b aab
aab
45
Ambiguity
46
E E E | E E | (E) | a
a aa
E E E E a E a EE
a a E a a*a
E E
leftmost derivation
a E E
a a
47
E E E | E E | (E) | a
a aa
E E a
a a
48
E E E | E E | (E) | a
a aa
Two derivation trees
E E
E E E E
a E E E E a
a a a a
49
The grammar E E E | E E | (E) | a
is ambiguous:
E E
E E E E
a E E E E a
a a a a
50
The grammar E E E | E E | (E) | a
is ambiguous:
E E E a E a EE
a a E a a*a
52
In other words:
53
Why do we care about ambiguity?
a aa
take a2
E E
E E E E
a E E E E a
a a a a
54
2 22
E E
E E E E
2 E E E E 2
2 2 2 2
55
2 22 6 2 22 8
6 8
E E
2 4 4 2
E E E E
2 2 2 2
2 E E E E 2
2 2 2 2
56
Correct result: 2 22 6
6
E
2 4
E E
2 2
2 E E
2 2
57
• Ambiguity is bad for programming languages
58
Another Ambiguous Grammar
59
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
IF_STMT
n n m n m m
Example: L {a b c } {a b c }
S S1 | S 2 S1 S1c | A S2 aS2 | B
A aAb | B bBc |
61
n n n
The string a b c
has two derivation trees
S S
S1 S2
S1 c a S2
62