Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 43

Context-Free Grammars

FLAT Dept. of CSE, VFSTR University 1


Formal Definitions

Grammar: G  V ,T , S , P 

Set of
variables
Set of Start Set of
terminal variable productions
symbols

FLAT Dept. of CSE, VFSTR University 2


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

All productions in P are of the form

A s
Variable String of
variables and
terminals

FLAT Dept. of CSE, VFSTR University 3


Example of Context-Free Grammar
S  aSb |  productions
P  {S  aSb, S  }

G  V ,T , S , P 

V  {S}
T  {a, b} start variable
variables
terminals
FLAT Dept. of CSE, VFSTR University 4
Language of a Grammar:

For a grammar G with start variable S


*
L(G )  {w : S  w, w  T *}

String of terminals or 

FLAT Dept. of CSE, VFSTR University 5


Example:
context-free grammar G : S  aSb | 

L(G )  {a b : n  0}
n n

Since, there is derivation



S a b n n
for any n 0
FLAT Dept. of CSE, VFSTR University 6
Context-Free Language:
A language L is context-free
if there is a context-free grammar G
with L  L(G )

FLAT Dept. of CSE, VFSTR University 7


Example:
L  {a b : n  0 }
n n

is a context-free language
since context-free grammar G :
S  aSb | 

generates L(G )  L

FLAT Dept. of CSE, VFSTR University 8


Another Example

Context-free grammar G :
S  aSa |bSb | 
Example derivations:
S  aSa  abSba  abba
S  aSa  abSba  abaSaba  abaaba

L(G )  {ww : w {a, b}*}


R

Palindromes of even length


FLAT Dept. of CSE, VFSTR University 9
Another Example
Context-free grammar G :
S  aSb | SS | 
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 )
FLAT Dept. of CSE, VFSTR University 10
Derivation Order
and
Derivation Trees

FLAT Dept. of CSE, VFSTR University 11


Derivation Order

Consider the following example grammar


with 5 productions:

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

FLAT Dept. of CSE, VFSTR University 12


1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  
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
FLAT Dept. of CSE, VFSTR University 13
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  

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
FLAT Dept. of CSE, VFSTR University 14
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  
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
FLAT Dept. of CSE, VFSTR University 15
Derivation Trees
Consider the same example grammar:

S  AB A  aaA |  B  Bb | 

And a derivation of aab :

S  AB  aaAB  aaABb  aaBb  aab

FLAT Dept. of CSE, VFSTR University 16


S  AB A  aaA |  B  Bb | 
S  AB
S

A B

yield AB

FLAT Dept. of CSE, VFSTR University 17


S  AB A  aaA |  B  Bb | 
S  AB  aaAB
S

A B

yield aaAB
a a A

FLAT Dept. of CSE, VFSTR University 18


S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb
S

A B

a a A B b

yield aaABb
FLAT Dept. of CSE, VFSTR University 19
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb
S

A B

a a A B b

yield
 aaBb  aaBb
FLAT Dept. of CSE, VFSTR University 20
S  AB A  aaA |  B  Bb | 
S  AB  aaAB  aaABb  aaBb  aab
Derivation Tree S
(parse tree)
A B

a a A B b
yield
  aab  aab
FLAT Dept. of CSE, VFSTR University 21
Sometimes, derivation order doesn’t
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

FLAT

Dept. of CSE, VFSTR University
 22
Ambiguity

FLAT Dept. of CSE, VFSTR University 23


Grammar for mathematical expressions
E  E  E | E  E | (E) | a

Example strings:
(a  a )  a  (a  a  (a  a ))

Denotes any number

FLAT Dept. of CSE, VFSTR University 24


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
FLAT Dept. of CSE, VFSTR University 25
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
FLAT Dept. of CSE, VFSTR University 26
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
FLAT Dept. of CSE, VFSTR University 27
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
FLAT Dept. of CSE, VFSTR University 28
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
FLAT Dept. of CSE, VFSTR University 29
Two different derivation trees
may cause problems in applications which
use the derivation trees:

• Evaluating expressions

• In general, in compilers
for programming languages

FLAT Dept. of CSE, VFSTR University 30


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)
FLAT Dept. of CSE, VFSTR University 31
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
FLAT Dept. of CSE, VFSTR University 32
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
FLAT Dept. of CSE, VFSTR University 33
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
FLAT Dept. of CSE, VFSTR University 34
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


FLAT Dept. of CSE, VFSTR University 35
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

FLAT Dept. of CSE, VFSTR University 36


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
FLAT Dept. of CSE, VFSTR University 37
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
FLAT Dept. of CSE, VFSTR University 38
An un-successful example:

L  {a b c }  {a b c }
n n m n m m

n, m  0

L is inherently ambiguous:

every grammar that generates this


language is ambiguous

FLAT Dept. of CSE, VFSTR University 39


Example (ambiguous) grammar for
L:

L  {a b c }  {a b c }
n n m n m m

S  S1 | S2 S1  S1c | A S2  aS2 | B
A  aAb |  B  bBc | 

FLAT Dept. of CSE, VFSTR University 40


The string a b c  L
n n n

has always two different derivation trees


(for any grammar)
For example
S S

S1 S2

S1 c a S2

FLAT Dept. of CSE, VFSTR University 41


Summary
• Context Free Grammar
– Derivations
– Derivation Trees
• Ambiguous Grammars

Formal Languages and


Automata Theory Dept. of CSE, VFSTR University 42
Acknowledgement

http://www.eecs.wsu.edu/~ananth/Cp
tS317/Lectures/

Formal Languages and


Automata Theory Dept. of CSE, VFSTR University 43

You might also like