Context Free Grammars PDF
Context Free Grammars PDF
Context Free Grammars PDF
n n R
{a b : n 0} {ww }
Regular Languages
a *b * ( a + b) *
Context-Free Pushdown
Grammars Automata
stack
automaton
Grammar: G = (V , T , S , P )
Set of
Variables/
Non-terminal Set of Start Set of
terminal Symbol productions
symbols
A s
Non-terminal String of
Non-terminal and
terminals
G = (V , T , S , P )
V = {S }
T = {a, b} start variable
variables
terminals
Winter 2014 Costas Busch - RPI 7
Language of a Grammar:
String of terminals or l
Sequence of
terminals and non-terminals
Grammar: S aSb
S l
Derivation of string ab :
S aSb ab
S aSb S l
Winter 2014 Costas Busch - RPI 10
Grammar: S aSb
S l
S aSb S l
Winter 2014 Costas Busch - RPI 11
Grammar: S aSb
S l
n n
L = {a b : n 0}
Instead of:
If: w1 w2 w3 ! wn
in zero or more derivation steps
*
Trivially: w w
Winter 2014 Costas Busch - RPI 14
Context-Free Language:
A language L is context-free
if there is a context-free grammar G
with L = L (G )
L = {a b : n 0 }
n n
is a context-free language
since context-free grammar G :
S aSb | l
generates L(G ) = L
Context-free grammar G :
S aSa | bSb | l
Example derivations:
S aSa abSba abba
S aSa abSba abaSaba abaaba
R
L(G ) = {ww : w {a, b}*}
Palindromes of even length
Winter 2014 Costas Busch - RPI 17
Another Example
Context-free grammar G:
S aSb | SS | l
Example derivations:
S SS aSbS abS ab
S SS aSbS abS abaSb abab
L(G ) = {w : na ( w) = nb ( w),
and na (v) nb (v)
Describes
in any prefix v}
matched
parentheses: () ((( ))) (( )) a = (, b =)
Winter 2014 Costas Busch - RPI 18
Derivation Order
and
Derivation Trees
1. S AB 2. A aaA 4. B Bb
3. A l 5. B l
S AB A aaA | l B Bb | l
S AB
S
A B
yield AB
S AB aaAB
S
A B
yield aaAB
a a A
S AB aaAB aaABb
S
A B
a a A B b
yield aaABb
Winter 2014 Costas Busch - RPI 27
S AB A aaA | l B Bb | l
S AB aaAB aaABb aaBb
S
A B
a a A B b
yield
Winter 2014
l
Costas Busch - RPI
aalBb = aaBb 28
S AB A aaA | l B Bb | l
S AB aaAB aaABb aaBb aab
Derivation Tree S
(parse tree)
A B
a a A B b
yield
l l aallb = aab
Winter 2014 Costas Busch - RPI 29
Sometimes, derivation order doesnt matter
Leftmost derivation:
S AB aaAB aaB aaBb aab
Rightmost derivation:
S AB ABb Ab aaAb aab
S
Give same
A B
derivation tree
a a A B b
E E + E | E * E | (E) | a
Example strings:
(a + a ) * a + (a + a * (a + a ))
E E + E a+ E a+ E*E
E
a + a* E a + a*a
E + E
A leftmost derivation
for a + a * a
a E * E
a a
Winter 2014 Costas Busch - RPI 33
E E + E | E * E | (E) | a
a a
Winter 2014 Costas Busch - RPI 34
E E + E | E * E | (E) | a
E + E E * E
a E * E E + E a
a a a a
Winter 2014 Costas Busch - RPI 35
take a=2
a + a *a = 2 + 2*2
E E
E + E E * E
2 E * E E + E 2
2 2 2 2
Winter 2014 Costas Busch - RPI 36
Good Tree Bad Tree
2 + 2*2 = 6 2 + 2*2 = 8
6 Compute expression result 8
E using the tree E
2 4 4 2
E + E E * E
2 2 2 2
2 E * E E + E 2
2 2 2 2
Winter 2014 Costas Busch - RPI 37
Two different derivation trees
may cause problems in applications which
use the derivation trees:
Evaluating expressions
In general, in compilers
for programming languages
E E
E + E E * E
a E * E E + E a
a a a a
Winter 2014 Costas Busch - RPI 40
E E + E | E * E | (E) | a
this grammar is ambiguous also because
string a + a*a has two leftmost derivations
E E + E a+ E a+ E*E
a + a* E a + a*a
Variables Terminals
Unique F F a
derivation tree
for a + a * a a a
Winter 2014 Costas Busch - RPI 46
An un-successful example:
n n m n m m
L = {a b c } {a b c }
n, m 0
L is inherently ambiguous:
S S1 | S2 S1 S1c | A S 2 aS2 | B
A aAb | l B bBc | l
E '
id * E | id | (E) * E | (E)
Enforces precedence of * over +
51
Associativity Declarations
Ambiguous
two parse trees of int + int + int
Left associativity declaration: %left +
E E
E + E E + E
E + E int int E + E
52
Precedence Declarations
Consider the string int + int * int
Precedence declarations: %left +
%left *
E E
E * E E + E
E + E int int E * E
53
Properties
of
Context-Free languages
Context-free languages
are closed under: Union
L1 is context free
L1 L2
L2 is context free is context-free
n n
L1 = {a b } S1 aS1b | l
R
L2 = {ww } S2 aS2a | bS2b | l
Union
n n R S S1 | S2
L = {a b } {ww }
Winter 2014 Costas Busch - RPI 56
In general:
For context-free languages L1, L2
with context-free grammars G1, G2
and start variables S1, S2
Context-free languages
are closed under: Concatenation
L1 is context free
L1L2
L2 is context free is context-free
n n
L1 = {a b } S1 aS1b | l
R
L2 = {ww } S2 aS2a | bS2b | l
Concatenation
n n R S S1S2
L = {a b }{ww }
Winter 2014 Costas Busch - RPI 59
In general:
Context-free languages
are closed under: Star-operation
L *
is context free L is context-free
Language Grammar
n n
L = {a b } S aSb | l
Star Operation
n n
L = {a b } * S1 SS1 | l
Winter 2014 Costas Busch - RPI 62
In general:
Context-free languages
are not closed under: intersection
L1 is context free
L1 L2
L2 is context free not necessarily
context-free
Context-free languages
are not closed under: complement
L1 context free
L1 L2
L2 regular context-free
NPDA M
q1, p1 a , b c
q2 , p2
transition
Winter 2014 Costas Busch - RPI 72
NPDA M1 DFA M2
l, b c
q1 q2 p1
transition
NPDA M
q1, p1 l , b c
q2 , p1
transition
Winter 2014 Costas Busch - RPI 73
NPDA M1 DFA M2
q0 p0
initial state initial state
NPDA M
q0 , p0
Initial state
Winter 2014 Costas Busch - RPI 74
NPDA M1 DFA M2
q1 p1 p2
final state final states
NPDA M
q1, p1 q1, p2
final states
Winter 2014 Costas Busch - RPI 75
Example: context-free
* *
L1 = {w1w2 : | w1 |=| w2 |, w1 {a, b} , w2 {c, d } }
NPDA M1
a, l 1 c,1 l
b, l 1 d ,1 l
q0 l , l l q1 l , l l q2 l , l l q3
DFA M2
a, c
p0
NPDA M
a, l 1 c,1 l
q0 , p0 l , l l q1, p0 l , l l q2 , p0 l , l l q3 , p0
M2 accepts string w
L( M ) = L( M1) L( M 2 )
Winter 2014 Costas Busch - RPI 79
Therefore:
M is NPDA
L( M1) L( M 2 ) is context-free
L1 L2 is context-free
Regular Closure
L1 context free
L1 L2
L2 regular context-free
Prove that: n n
L = {a b : n 100, n 0}
is context-free
n n
{a b : n 0} is context-free
100 100
L1 = {a b } is regular
n n
(regular closure) {a b } L1 context-free
n n n n
{a b } L1 = {a b : n 100, n 0} = L
is context-free
Winter 2014 Costas Busch - RPI 86
Another Application of Regular Closure
Prove that: L = {w : na = nb = nc }
is not context-free
(regular closure)
Then n n n
L {a * b * c*} = {a b c }