Context Free Grammars
Context Free Grammars
Context-Free Languages
Fall 2006 Costas Busch - RPI 2
Regular Languages
} 0 : { > n b a
n n
} {
R
ww
* *b a
* ) ( b a +
Context-Free Languages
Fall 2006 Costas Busch - RPI 3
Context-Free Languages
Pushdown
Automata
Context-Free
Grammars
stack
automaton
Fall 2006 Costas Busch - RPI 4
Context-Free Grammars
Fall 2006 Costas Busch - RPI 5
Grammars
Grammars express languages
Example: the English language grammar
verb predicate
noun article phrase noun
predicate phrase noun sentence
_
_
Fall 2006 Costas Busch - RPI 6
sleeps verb
runs verb
dog noun
cat noun
the article
a article
_
_
Fall 2006 Costas Busch - RPI 8
Derivation of string a cat runs:
runs cat a
verb cat a
verb noun a
verb noun article
verb phrase noun
predicate phrase noun sentence
_
_
Fall 2006 Costas Busch - RPI 9
Language of the grammar:
L = { a cat runs,
a cat sleeps,
the cat runs,
the cat sleeps,
a dog runs,
a dog sleeps,
the dog runs,
the dog sleeps }
Fall 2006 Costas Busch - RPI 10
cat noun
Variables
Sequence of
Terminals (symbols)
Productions
predicate phrase noun sentence _
Sequence of Variables
Fall 2006 Costas Busch - RPI 11
Another Example
S
aSb S
Grammar:
Variable
Sequence of
terminals and variables
The right side
may be
S
aSb S
ab aSb S
ab
aSb S S
Fall 2006 Costas Busch - RPI 13
Grammar:
Derivation of string :
aabb aaSbb aSb S
aSb S S
aabb
S
aSb S
Fall 2006 Costas Busch - RPI 14
aaabbb aaaSbbb aaSbb aSb S
aaaabbbb aaaaSbbbb
aaaSbbb aaSbb aSb S
Other derivations:
S
aSb S
Grammar:
Fall 2006 Costas Busch - RPI 15
S
aSb S
} 0 : { > = n b a L
n n
Grammar:
Language of the grammar:
Fall 2006 Costas Busch - RPI 16
We write:
Instead of:
aaabbb S
*
S
aSb S
aaabbb S
ab S
S
*
*
*
Example Grammar
Possible Derivations
b aaaaaSbbbb aaSbb S
- -
Fall 2006 Costas Busch - RPI 19
S
aSb S
| aSb S
the article
a article
the a article |
Another convenient notation:
Fall 2006 Costas Busch - RPI 20
Formal Definitions
( ) P S T V G , , , =
Set of
variables
Set of
terminal
symbols
Start
variable
Set of
productions
Grammar:
Fall 2006 Costas Busch - RPI 21
Context-Free Grammar:
All productions in are of the form
s A
String of
variables and
terminals
) , , , ( P S T V G =
P
Variable
Fall 2006 Costas Busch - RPI 22
| aSb S
( ) P S T V G , , , =
} {S V =
} , { b a T =
} , { = S aSb S P
variables
terminals
productions
start variable
Example of Context-Free Grammar
Fall 2006 Costas Busch - RPI 23
For a grammar with start variable
G S
String of terminals or
*} , : { ) (
*
T w w S w G L e =
Language of a Grammar:
Fall 2006 Costas Busch - RPI 24
context-free grammar :
} 0 : { ) ( > = n b a G L
n n
n n
b a S
-
G
Example:
Since, there is derivation
for any
0 > n
| aSb S
Fall 2006 Costas Busch - RPI 25
A language is context-free
if there is a context-free grammar
with
L
G
) (G L L =
Context-Free Language:
Fall 2006 Costas Busch - RPI 26
since context-free grammar :
} 0 : { > = n b a L
n n
G
Example:
is a context-free language
generates L G L = ) (
| aSb S
Fall 2006 Costas Busch - RPI 27
| |bSb aSa S
abba abSba aSa S
Context-free grammar : G
Example derivations:
abaaba abaSaba abSba aSa S
= ) (G L
}*} , { : { b a w ww
R
e
Palindromes of even length
Another Example
Fall 2006 Costas Busch - RPI 28
| | SS aSb S
ab abS aSbS SS S
Context-free grammar :
G
Example derivations:
abab abaSb abS aSbS SS S
} prefix any in
) ( ) ( and
), ( ) ( : {
v
v n v n
w n w n w
b a
b a
>
=
= ) (G L
() ((( ))) (( ))
Describes
matched
parentheses: ) b (, = = a
Another Example
Fall 2006 Costas Busch - RPI 29
Derivation Order
and
Derivation Trees
Fall 2006 Costas Busch - RPI 30
Derivation Order
Consider the following example grammar
with 5 productions:
AB S . 1
A
aaA A
. 3
. 2
B
Bb B
. 5
. 4
Fall 2006 Costas Busch - RPI 31
aab aaBb aaB aaAB AB S
5 4 3 2 1
Leftmost derivation order of string :
aab
At each step, we substitute the
leftmost variable
AB S . 1
A
aaA A
. 3
. 2
B
Bb B
. 5
. 4
Fall 2006 Costas Busch - RPI 32
aab aaAb Ab ABb AB S
3 2 5 4 1
Rightmost derivation order of string :
aab
At each step, we substitute the
rightmost variable
AB S . 1
A
aaA A
. 3
. 2
B
Bb B
. 5
. 4
Fall 2006 Costas Busch - RPI 33
aab aaAb Ab ABb AB S
3 2 5 4 1
Rightmost derivation of :
aab
aab aaBb aaB aaAB AB S
5 4 3 2 1
Leftmost derivation of :
aab
AB S . 1
A
aaA A
. 3
. 2
B
Bb B
. 5
. 4
Fall 2006 Costas Busch - RPI 34
Derivation Trees
Consider the same example grammar:
aab aaBb aaABb aaAB AB S
And a derivation of : aab
AB S
| aaA A | Bb B
Fall 2006 Costas Busch - RPI 35
AB S
S
B A
AB S
| aaA A | Bb B
yield AB
Fall 2006 Costas Busch - RPI 36
aaAB AB S
a a
A
S
B A
AB S
| aaA A | Bb B
yield
aaAB
Fall 2006 Costas Busch - RPI 37
aaABb aaAB AB S
S
B A
a a
A B b
AB S
| aaA A | Bb B
yield aaABb
Fall 2006 Costas Busch - RPI 38
aaBb aaABb aaAB AB S
S
B A
a a
A B b
AB S
| aaA A | Bb B
yield
aaBb Bb aa =
Fall 2006 Costas Busch - RPI 39
aab aaBb aaABb aaAB AB S
yield
aab b aa =
S
B A
a a
A B b
Derivation Tree
AB S
| aaA A | Bb B
(parse tree)
Fall 2006 Costas Busch - RPI 40
aab aaBb aaB aaAB AB S
aab aaAb Ab ABb AB S
S
B A
a a
A B b
Give same
derivation tree
Sometimes, derivation order doesnt matter
Leftmost derivation:
Rightmost derivation:
Fall 2006 Costas Busch - RPI 41
Ambiguity
Fall 2006 Costas Busch - RPI 42
Grammar for mathematical expressions
)) ( ( ) ( a a a a a a a + - + + - +
Example strings:
Denotes any number
a E E E E E E | ) ( | | - +
Fall 2006 Costas Busch - RPI 43
A leftmost derivation
for
a a a - +
E
E E
E E
+
a
a
a
-
a a a E a a
E E a E a E E E
* + - +
- + + +
a E E E E E E | ) ( | | - +
Fall 2006 Costas Busch - RPI 44
E
E E
+
a
a
-
E E
a
a a a E a a
E E a E E E E E E
- + - +
- + - + -
Another
leftmost derivation
for
a E E E E E E | ) ( | | - +
a a a - +
Fall 2006 Costas Busch - RPI 45
a a a - +
E
E E
+
a
a
-
E E
a
E
E E
E E
+
a
a
a
-
Two derivation trees
for
a E E E E E E | ) ( | | - +
Fall 2006 Costas Busch - RPI 46
E
E E
+
-
E E
E
E E
E E
+
- 2
2 2 2 2
2
2 2 2 - + = - + a a a
take 2 = a
Fall 2006 Costas Busch - RPI 47
E
E E
+
-
E E
E
E E
E E
+
-
6 2 2 2 = - +
2
2 2 2 2
2
8 2 2 2 = - +
4
2 2
2
6
2 2
2 4
8
Good Tree Bad Tree
Compute expression result
using the tree
Fall 2006 Costas Busch - RPI 48
Two different derivation trees
may cause problems in applications which
use the derivation trees:
Evaluating expressions
In general, in compilers
for programming languages
Fall 2006 Costas Busch - RPI 49
Ambiguous Grammar:
A context-free grammar is ambiguous
if there is a string which has:
two different derivation trees
or
two leftmost derivations
G
) (G L we
(Two different derivation trees give two
different leftmost derivations and vice-versa)
Fall 2006 Costas Busch - RPI 50
E
E E
+
a
a
-
E E
a
E
E E
E E
+
a
a
a
-
string a a a - + has two derivation trees
a E E E E E E | ) ( | | - +
this grammar is ambiguous since
Example:
Fall 2006 Costas Busch - RPI 51
string a a a - + has two leftmost derivations
a a a E a a
E E a E E E E E E
- + - +
- + - + -
a a a E a a
E E a E a E E E
* + - +
- + + +
a E E E E E E | ) ( | | - +
this grammar is ambiguous also because
Fall 2006 Costas Busch - RPI 52
IF_STMT if EXPR then STMT
|
if EXPR then STMT else STMT
Another ambiguous grammar:
Variables Terminals
Very common piece of grammar
in programming languages
Fall 2006 Costas Busch - RPI 53
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
expr1 then
else if expr2 then
STMT
stmt1
if
IF_STMT
expr1 then else
if expr2 then
STMT stmt2
if
stmt1
stmt2
Two derivation trees
Fall 2006 Costas Busch - RPI 54
In general, ambiguity is bad
and we want to remove it
Sometimes it is possible to find
a non-ambiguous grammar for a language
But, in general we cannot do so
Fall 2006 Costas Busch - RPI 55
a E
E E
E E E
E E E
-
+
) (
a E F
F F T T
T T E E
| ) (
|
|
-
+
Ambiguous
Grammar
Non-Ambiguous
Grammar
Equivalent
generates the same
language
A successful example:
Fall 2006 Costas Busch - RPI 56
a a a F a a F F a
F T a T a T F T T T E E
- + - + - +
- + + + + +
E
E T +
T
-
F
F
a
T
F
a
a
a E F
F F T T
T T E E
| ) (
|
|
-
+
Unique
derivation tree
for a a a - +
Fall 2006 Costas Busch - RPI 57
} { } {
m m n m n n
c b a c b a L =
0 , > m n
An un-successful example:
every grammar that generates this
language is ambiguous
L
is inherently ambiguous:
Fall 2006 Costas Busch - RPI 58
} { } {
m m n m n n
c b a c b a L =
|
|
1 1
aAb A
A c S S
|
|
2 2
bBc B
B aS S
2 1
| S S S
Example (ambiguous) grammar for :
L
Fall 2006 Costas Busch - RPI 59
The string
L c b a
n n n
e
has always two different derivation trees
(for any grammar)
S
1
S
S
2
S
1
S
c
2
S
a
For example