1 of 23

Context Free Grammars(CFG)-

Derivations, Parse trees and Ambiguous grammar

Department of Computer Science and Engineering

S R K R Engineering College,

Bhimavaram-543204

K V S S R Murthy

Assistant Professor

Formal Languages and Automata Theory

2 of 23

Context Free Languages--Examples

Determine the CFG for language L = { am bn cm+n / m, n>=1 }

Given CFL = { abcc, abbccc, aabccc,

aabbcccc, ---- }

S🡪 a S1 c

=> a bc c since S1 🡪 bc

S => abcc and so abcc is in L (G)

To generate these strings we start with S

S🡪 aSc / a S1 c

S1 🡪b S1 c / bc

next string Derivations:

S🡪 a S1 c

=> a b S1 c c since S1 🡪 b S1 c

=> a b bc c c since S1 🡪 b c

S => abbccc and so abbbcc is in L (G)

3 of 23

Context Free Languages--Examples

In a similar way remaining strings of L(G) are generated.

next string Derivations:

S 🡪 a S c

=> a a S1 c c since S 🡪 a S c

=> aa b S1 c cc since S1 🡪 b S1 c

=> aa b bc c cc since S1 🡪 b c

S => aabbcccc and so aabbcccc is in L (G)

4 of 23

Context Free Languages--problems

Generate the CFG for all the strings ending with 00 or (0+1)* 00

Generate the CFG for L = { ai bj ck / j=k }

Generate the CFG for L = { ai bj ck / i=j }

Generate the CFG for L = { ai bj ck / i=k ; i, j, k>=1}

Generate the CFG for L = { w ∈ {0, 1} * : |w|a = |w|b }.

Or

Generate CGF for equal number of a’s and b’s

Generate the CFG for all the strings starting with 00 or 00 (0+1)*

5 of 23

Context Free Languages--problems

Determine the CFG for the language L = { an / n >=0 } or a*

Generate the CFG for L = { ai bj ck / j=k }

Generate the CFG for generating balanced parenthesis.

Generate the CFG for L = { am bn / m, n >=1 }

Generate the CFG for L = { w ∈ {0, 1} * : |w|a = |w|b }.

Or

Generate CGF for equal number of a’s and b’s

Generate the CFG for all the (0+1) * 00 (0+1)*

6 of 23

Derivation and Parse tree

The process of deriving a string is called as derivation.

The geometrical representation of a derivation is called as a parse tree or derivation tree.

7 of 23

LMD and RMD

Definition : A left-most derivation of a sentential form (String ) is one in which always Left most variable is selected and apply one of the rule of that variable.

Definition : A Right-most derivation of a sentential form (String ) is one in which always Right most variable is selected and apply one of the rule of that variable.

8 of 23

LMD and RMD example

S A | A B

A e | a | A b | A A

B b | b c | B c | b B

S ⇒ AB

⇒ AbB

Abb

Aabb

Aabb

aabb

Leftmost Derivation.

Always picks leftmost variable.

S ⇒ AB

AAB

aAB

aaB

aabB

aabb

Right most derivation.

Always picks rightmost variable.

S

A

B

A

A

B

b

a

a

b

Parse tree -- LMD

Here same string is derived using LMD and RMD so both parse trees look alike.

S

A

B

A

A

B

b

a

a

b

Parse tree - RMD

9 of 23

LMD and RMD example

Consider the grammar and give LMD and for string (a, a)

S → (L) / a

L → L , S / S

S

L

L

S

S

a

,

a

LMD for the string :

S🡪 ( L )

=> ( L , S ) since L🡪L , S

=> ( S, S ) since L🡪S

=> ( a, S ) since S🡪 a

=> ( a, a ) since S🡪 a

S => (a , a )

(

)

Parse Tree

10 of 23

LMD and RMD example

Consider the grammar and give RMD and for string (a, a)

S → (L) / a

L → L , S / S

S

L

L

S

S

a

,

a

RMD for the string :

S🡪 ( L )

=> ( L , S ) since L🡪L , S

=> ( L , a ) since S 🡪 a

=> ( S, a ) since L🡪 S

=> ( a, a ) since S🡪 a

S => (a , a )

(

)

Parse Tree

11 of 23

LMD and RMD problems

Consider the grammar and give LMD and RMD and for string a* a + a

S → S+S / S* S /a

Consider the grammar and give LMD and RMD and for string w = aaabbabbba

S → aB / bA

A → aS / bAA / a

B → bS / aBB / b

12 of 23

LMD and RMD problems

13 of 23

LMD and RMD Example

14 of 23

LMD and RMD problems

Consider the grammar and give LMD and RMD and for string a* a + a

S → S+S / S* S /a

Consider the grammar and give LMD and RMD and for string w = aabb

S → aB / bA

S → aS / bAA / a

B → bS / aBB / b

15 of 23

Parse trees

S A | A B

A e | a | A b | A A

B b | b c | B c | b B

Derivations and parse trees are not unique

The following some derivation trees for string aabb

S

A

B

A

A

B

b

a

a

b

S

A

B

A

A

b

a

a

b

A

S

A

A

A

A

A

b

A

a

ε

a

b

A

16 of 23

Ambiguous grammar

Definition. A grammar G is ambiguous if there is a word w ∈ L(G) having at least two different parse trees

Let G be CFG and w is any string, if w is having at least two different derivations, or derivation trees then the string is ambiguous and hence the grammar is ambiguous.

17 of 23

Ambiguous grammar

Question : Show that the following grammar is ambiguous

S🡪 S+S / S * S / a

Consider the string : a * a + a First derivation

S 🡪 S + S

=> S * S + S since S 🡪 S * S

=> a * S + S since S 🡪 a

=> a * a + S since S 🡪 a

=> a * a + a since S 🡪 a

S => a * a + a and so a * a + a is in L (G)

Consider the string : a * a + a second derivation

S 🡪 S * S

=> a * S since S 🡪 a

=> a * S + S since S 🡪 S + S

=> a * a + S since S 🡪 a

=> a * a + a since S 🡪 a

S => a * a + a and so a * a + a is in L (G)

Here the string a * a + a is having two different derivations so, we will get two different parse trees and hence the string is ambiguous and the grammar is ambiguous.

18 of 23

Ambiguous grammar - example

Question : Show that the following grammar is ambiguous

S🡪 S+S / S * S / a

Consider the string : a * a + a First derivation

S 🡪 S + S

=> S * S + S since S 🡪 S * S

=> a * S + S since S 🡪 a

=> a * a + S since S 🡪 a

=> a * a + a since S 🡪 a

S => a * a + a and so a * a + a is in L (G)

Parse tree

19 of 23

Ambiguous grammar -example

Here the string a * a + a is having two different derivations and so we have two different parse trees and hence the string is ambiguous and the grammar is ambiguous.

Consider the string : a * a + a second derivation

S 🡪 S * S

=> a * S since S 🡪 a

=> a * S + S since S 🡪 S + S

=> a * a + S since S 🡪 a

=> a * a + a since S 🡪 a

S => a * a + a and so a * a + a is in L (G)

Parse tree

20 of 23

Ambiguous grammar -example

Here the string 3 * 2 + 5 is having two different derivation or derivation trees and hence the string is ambiguous and the grammar is ambiguous.

Show that the following grammar is ambiguous :

Two different parse trees for 3*2+5

21 of 23

Ambiguous grammar -example

Here the string aabb is having two different derivation or derivation trees and hence the string is ambiguous and the grammar is ambiguous.

Show that the following grammar is ambiguous S🡪SS/ aSb / ε

Two different parse trees for aabb

22 of 23

Ambiguous grammar Problems

Show that the following grammar is ambiguous

S🡪 aS / Sa/ a

Show that the following grammar is ambiguous

A 🡪 AA | (A) | a for the string a(a)aa

Show that the following grammar is ambiguous

S 🡪 SS | AB , A -> Aa | a , B -> Bb | b.

23 of 23

Thanks!

Contact us:

KVSSR MURTHY

Assistant Professor,

Department of CSE,

S R K R Engineering College,

China amiram,

Bhimavaram – 534204.