Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Context Free Grammars PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 88

Review of CFGs and Parsing I

Context-free Languages and Grammars

Winter 2014 Costas Busch - RPI 1


Context-Free Languages

n n R
{a b : n 0} {ww }

Regular Languages
a *b * ( a + b) *

Winter 2014 Costas Busch - RPI 2


Context-Free Languages

Context-Free Pushdown
Grammars Automata

stack

automaton

Winter 2014 Costas Busch - RPI 3


Context-Free Grammars

Winter 2014 Costas Busch - RPI 4


Formal Definitions

Grammar: G = (V , T , S , P )

Set of
Variables/
Non-terminal Set of Start Set of
terminal Symbol productions
symbols

Winter 2014 Costas Busch - RPI 5


Context-Free Grammar: G = (V , T , S , P )

All productions in P are of the form

A s
Non-terminal String of
Non-terminal and
terminals

Winter 2014 Costas Busch - RPI 6


Example of Context-Free Grammar
S aSb | l
productions
P = {S aSb, S l}

G = (V , T , S , P )

V = {S }
T = {a, b} start variable
variables
terminals
Winter 2014 Costas Busch - RPI 7
Language of a Grammar:

For a grammar G with start variable S


*
L(G ) = {w : S w, w T *}

String of terminals or l

Winter 2014 Costas Busch - RPI 8


Example

Sequence of
terminals and non-terminals

Grammar: S aSb
S l

Non-terminals The right side


may be l

Winter 2014 Costas Busch - RPI 9


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

Derivation of string aabb :

S aSb aaSbb aabb

S aSb S l
Winter 2014 Costas Busch - RPI 11
Grammar: S aSb
S l

Language of the grammar:

n n
L = {a b : n 0}

Winter 2014 Costas Busch - RPI 12


A Convenient Notation
*
We write: S aaabbb
for zero or more derivation steps

Instead of:

S aSb aaSbb aaaSbbb aaabbb

Winter 2014 Costas Busch - RPI 13


*
In general we write: w1 wn

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 )

Winter 2014 Costas Busch - RPI 15


Example:

L = {a b : n 0 }
n n

is a context-free language
since context-free grammar G :
S aSb | l
generates L(G ) = L

Winter 2014 Costas Busch - RPI 16


Another Example

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

Winter 2014 Costas Busch - RPI 19


Derivation Order

Consider the following example grammar


with 5 productions:

1. S AB 2. A aaA 4. B Bb
3. A l 5. B l

Winter 2014 Costas Busch - RPI 20


1. S AB 2. A aaA 4. B Bb
3. A l 5. B l

Leftmost derivation order of string aab :


1 2 3 4 5
S AB aaAB aaB aaBb aab

At each step, we substitute the


leftmost variable
Winter 2014 Costas Busch - RPI 21
1. S AB 2. A aaA 4. B Bb
3. A l 5. B l

Rightmost derivation order of string aab :


1 4 5 2 3
S AB ABb Ab aaAb aab
At each step, we substitute the
rightmost variable
Winter 2014 Costas Busch - RPI 22
1. S AB 2. A aaA 4. B Bb
3. A l 5. B l

Leftmost derivation of aab :


1 2 3 4 5
S AB aaAB aaB aaBb aab

Rightmost derivation of aab :


1 4 5 2 3
S AB ABb Ab aaAb aab
Winter 2014 Costas Busch - RPI 23
Derivation Trees
Consider the same example grammar:

S AB A aaA | l B Bb | l

And a derivation of aab :


S AB aaAB aaABb aaBb aab

Winter 2014 Costas Busch - RPI 24


S AB A aaA | l B Bb | l

S AB
S

A B

yield AB

Winter 2014 Costas Busch - RPI 25


S AB A aaA | l B Bb | l

S AB aaAB
S

A B

yield aaAB
a a A

Winter 2014 Costas Busch - RPI 26


S AB A aaA | l B Bb | l

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

Winter 2014 Costas Busch - RPI


l l 30
Ambiguity

Winter 2014 Costas Busch - RPI 31


Grammar for mathematical expressions

E E + E | E * E | (E) | a

Example strings:

(a + a ) * a + (a + a * (a + a ))

Denotes any number

Winter 2014 Costas Busch - RPI 32


E E + E | E * E | (E) | 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

E E*E E + E*E a+ E*E


E
a + a*E a + a*a
E * E
Another
leftmost derivation
for a + a * a E + E a

a a
Winter 2014 Costas Busch - RPI 34
E E + E | E * E | (E) | a

Two derivation trees


for a + a * a
E E

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

Winter 2014 Costas Busch - RPI 38


Ambiguous Grammar:
A context-free grammar G is ambiguous
if there is a string w L (G ) which has:

two different derivation trees


or
two leftmost derivations

(Two different derivation trees give two


different leftmost derivations and vice-versa)
Winter 2014 Costas Busch - RPI 39
Example: E E + E | E * E | (E) | a
this grammar is ambiguous since
string a + a*a has two derivation trees

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

E E*E E + E*E a+ E*E


a + a*E a + a*a
Winter 2014 Costas Busch - RPI 41
Another ambiguous grammar:

IF_STMT if EXPR then STMT


| if EXPR then STMT else STMT

Variables Terminals

Very common piece of grammar


in programming languages
Winter 2014 Costas Busch - RPI 42
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT

if expr1 then STMT

if expr2 then stmt1 else stmt2

Two derivation trees


IF_STMT

if expr1 then STMT else stmt2

if expr2 then stmt1


Winter 2014 Costas Busch - RPI 43
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. (I.e., there


is no single algorithm that takes as input any
ambiguous grammar and produces an equivalent
Unambiguous grammar.)

Winter 2014 Costas Busch - RPI 44


A successful example:
Equivalent
Ambiguous
Non-Ambiguous
Grammar
Grammar
E E +E
E E +T |T
E E *E
T T * F | F
E (E )
E a F (E ) | a
generates the same
language
Winter 2014 Costas Busch - RPI 45
E E +T T +T F +T a +T a +T *F
a + F *F a + a*F a + a*a
E
E E +T |T
E + T
T T *F | F
F (E) | a T T * F

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:

every grammar that generates this


language is ambiguous

Winter 2014 Costas Busch - RPI 47


Example (ambiguous) grammar for L:
n n m n m m
L = {a b c } {a b c }

S S1 | S2 S1 S1c | A S 2 aS2 | B
A aAb | l B bBc | l

Winter 2014 Costas Busch - RPI 48


Dealing with Ambiguity

There are several ways to handle ambiguity

Most direct method is to rewrite grammar


unambiguously
E E +E | E
' '

E '
id * E | id | (E) * E | (E)
Enforces precedence of * over +

Professor Alex Aiken Lecture #5 49


Ambiguity

No general techniques for handling ambiguity

Impossible to convert automatically an


ambiguous grammar to an unambiguous one

Used with care, ambiguity can simplify the


grammar. Sometimes allows more natural
definitions. However, we still need to
eventually eliminate ambiguity. How?
50
Precedence and Associativity Declarations

Instead of rewriting the grammar


Use the more natural (ambiguous) grammar
Along with disambiguating declarations

Most tools allow precedence and associativity


declarations to disambiguate grammars

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

int int int int

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

int int int int

53
Properties
of
Context-Free languages

Winter 2014 Costas Busch - RPI 54


Union

Context-free languages
are closed under: Union

L1 is context free
L1 L2
L2 is context free is context-free

Winter 2014 Costas Busch - RPI 55


Example
Language Grammar

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

The grammar of the union L1 L2


has new start variable S
and additional production S S1 | S2
Winter 2014 Costas Busch - RPI 57
Concatenation

Context-free languages
are closed under: Concatenation

L1 is context free
L1L2
L2 is context free is context-free

Winter 2014 Costas Busch - RPI 58


Example
Language Grammar

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:

For context-free languages L1, L2


with context-free grammars G1, G2
and start variables S1, S2

The grammar of the concatenation L1L2


has new start variable S
and additional production S S1S2

Winter 2014 Costas Busch - RPI 60


Star Operation

Context-free languages
are closed under: Star-operation

L *
is context free L is context-free

Winter 2014 Costas Busch - RPI 61


Example

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:

For context-free language L


with context-free grammar G
and start variable
S

The grammar of the star operation L *


has new start variable S1
and additional production S1 SS1 | l

Winter 2014 Costas Busch - RPI 63


Context-free Languages
dont have the following
Properties

Winter 2014 Costas Busch - RPI 64


Intersection

Context-free languages
are not closed under: intersection

L1 is context free
L1 L2
L2 is context free not necessarily
context-free

Winter 2014 Costas Busch - RPI 65


Example
n n m n m m
L1 = {a b c } L2 = {a b c }
Context-free: Context-free:
S AC S AB
A aAb | l A aA | l
C cC | l B bBc | l
Intersection
n n n
L1 L2 = {a b c } NOT context-free
Winter 2014 Costas Busch - RPI 66
Complement

Context-free languages
are not closed under: complement

is context free L not necessarily


L context-free.

I.e., the complement of a context-free language may or may


not be context-free.

Winter 2014 Costas Busch - RPI 67


Example
n n m n m m
L1 = {a b c } L2 = {a b c }
Context-free: Context-free:
S AC S AB
A aAb | l A aA | l
C cC | l B bBc | l
Complement
n n n
L1 L2 = L1 L2 = {a b c }
NOT context-free
Winter 2014 Costas Busch - RPI 68
Intersection
of
Context-free languages
and
Regular Languages

Winter 2014 Costas Busch - RPI 69


The intersection of
a context-free language and
a regular language
is a context-free language

L1 context free
L1 L2
L2 regular context-free

Winter 2014 Costas Busch - RPI 70


Machine M1 Machine M2
NPDA for L1 DFA for L2
context-free regular

Construct a new NPDA machine M


that accepts L1 L2

M simulates in parallel M1 and M2


Winter 2014 Costas Busch - RPI 71
NPDA M1 DFA M2
a, b c a
q1 q2 p1 p2
transition transition

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

Winter 2014 Costas Busch - RPI 76


regular
L2 = {a, c}*

DFA M2
a, c

p0

Winter 2014 Costas Busch - RPI 77


context-free
Automaton for: L1 L2 = {a nc n : n 0}

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

Winter 2014 Costas Busch - RPI 78


In General:

M simulates in parallel M1 and M2

M accepts string w if and only if

M1 accepts string w and

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

Winter 2014 Costas Busch - RPI 80


Applications
of
Regular Closure

Winter 2014 Costas Busch - RPI 81


The intersection of
a context-free language and
a regular language
is a context-free language

Regular Closure
L1 context free
L1 L2
L2 regular context-free

Winter 2014 Costas Busch - RPI 82


An Application of Regular Closure

Prove that: n n
L = {a b : n 100, n 0}

is context-free

Winter 2014 Costas Busch - RPI 83


We know:

n n
{a b : n 0} is context-free

Winter 2014 Costas Busch - RPI 84


We also know:

100 100
L1 = {a b } is regular

* 100 100 is regular


L1 = {(a + b) } - {a b }

Winter 2014 Costas Busch - RPI 85


n n * 100 100
{a b } L1 = {(a + b) } - {a b }
context-free 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

Winter 2014 Costas Busch - RPI 87


If L = {w : na = nb = nc } is context-free

(regular closure)

Then n n n
L {a * b * c*} = {a b c }

context-free regular context-free


Impossible!!!

Therefore, L is not context free


Winter 2014 Costas Busch - RPI 88

You might also like