Professional Documents
Culture Documents
Context-Free Languages
{a b : n 0}
n n
{ww }
Regular Languages
a *b *
( a + b) *
Context-Free Languages
{a b }
n n
{ww }
Regular Languages
Context-Free Languages
Context-Free Grammars
Context-Free Grammars
Grammars
Grammars express languages Example: the English language
predicate noun
article a article the noun cat noun dog verb runs verb walks
8
Example 1:
A derivation of the dog walks:
sentence noun _ phrase noun _ phrase article the noun noun verb
Example 2:
A derivation of a cat runs:
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 }
11
Notation
Production Rules
12
Another Example
Grammar:
S aSb S
ab :
Derivation of sentence
S aSb ab S aSb
S
13
Grammar:
S aSb S aabb :
Derivation of sentence
S
14
Other derivations:
15
S aSb S
L = {a b : n 0}
n n
16
More Notation
Grammar
G = (V , T , S , P )
Set of variables Set of terminal symbols Start variable Set of Production rules
17
V: T: S: P:
Example
Grammar
S aSb S
G = (V , T , S , P )
V = {S } T = {a, b}
P = {S aSb, S }
18
More Notation
Sentential Form: A sentence that contains variables and terminals Example:
We write:
S aaabbb
Instead of:
In general we write:
w1 wn
If:
w1 w2 w3 wn
21
By default:
w w
22
Example
Grammar
S aSb S
Derivations *
S
* * *
S ab S aabb S aaabbb
23
A Convenient Notation
A aAb A
A aAb |
article a | the
25
G = (V , T , S , P )
Terminal symbols Start variable
A x
is context-free
27
Derivation Order
1. S AB
2. A aaA 3. A
2 3 4
4. B Bb 5. B
5
Leftmost derivation:
S aAB A bBb B A|
Leftmost derivation:
Derivation Trees
30
S AB
A aaA |
B Bb |
S AB
S A B
31
S AB
A aaA |
B Bb |
S AB aaAB
S A a a A B
32
S AB
A aaA |
B Bb |
S AB aaAB aaABb
S A a a A B B
b
33
S AB
A aaA |
B Bb |
34
S AB
A aaA |
B Bb |
35
S AB
A aaA |
B Bb |
aab = aab
36
S AB
A aaA |
B Bb |
S AB
Partial derivation tree
S B
37
S AB aaAB
Partial derivation tree
S B A
A a a
38
S AB aaAB
Partial derivation tree
sentential form
S B A
yield
A a a
aaAB
39
Leftmost:
B
b
40
Ambiguity
41
E E + E | E E | (E) | a
a + aa
E E a
+
E E + E a+ E a+ EE a + a E a + a*a
E
leftmost derivation
E a
E a
42
E E + E | E E | (E) | a
a + aa
E EE E + EE a+ EE E a + aE a + aa
leftmost derivation
E E a
+
E a
E a
43
E E + E | E E | (E) | a
a + aa
Two derivation trees
E E a
+
E E
E E a E a
+
E a
E a
E a
44
The grammar
E E + E | E E | (E) | a is ambiguous:
string
E
a
+
E
E
E
a
E
a
E
a
45
The grammar
E E + E | E E | (E) | a is ambiguous:
string
E E + E a+ E a+ EE a + a E a + a*a E EE E + EE a+ EE a + aE a + aa
46
Definition:
A context-free grammar if some string
is ambiguous
w L(G ) has:
47
is ambiguous
w L(G ) has:
48
a + aa
E E a
+
take
a=2
E
E a
E a
E a
E a
E a
49
2 + 22
E E 2
+
E E
E E 2 E 2
+
E 2
E 2
E 2
50
2 + 22 = 6
6 E 2 E 2
+
2 + 22 = 8
8 E
4 E
4 E 2 E 2 2 E 2
+
2 E 2
2 E 2
2 E 2
51
Correct result:
2 + 22 = 6
6 E
2 E 2
4 E
2 E 2
2 E 2
52
53
E E + E | E E | (E) | a
New non-ambiguous grammar:
E E +T E T T T F T F F (E) F a
54
E E +T T +T F +T a +T a +T F a + F F a + aF a + aa
a + aa
T T F a
55
E E +T E T T T F T F F (E) F a
E T F a
F a
E E T F a
+
a + aa
T T F a
56
F a
The grammar
G:
E E +T E T T T F T F F (E) F a
58
stmt2
RLs are used for simple patterns, while CFLs for more complicated aspects.
61
62