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

Normal Forms

Chomsky Normal Form (CNF)


o The number of symbols on the right of a production is strictly limited.
o The string on the right of a production consist of no more than two
symbols.
Definition:
oA context-free grammar is in Chomsky normal form if all productions
are of the form
A → BC or A → a,
where A, B, C are in V , and a is in T.

oThe grammar should be in simplified form.


oAny context-free grammar G = (V, T, S, P) with λ L(G) has an
equivalent grammar G = (V , T, S, P) in Chomsky normal form.
09-06-2022
2
Example 1:
oThe grammar
S → AS|a
A → SA|b
is in Chomsky normal form.
oThe grammar
S → AS|AAS
A → SA|aa
is not in Chomsky normal form;
both productions S → AAS and A → aa violate the conditions
09-06-2022 3
Example 1:
S → AS | ab
A →SA | b
Convert to CNF
Sol:
S → AS | XY
A → SA | b
X→a
Y→b

09-06-2022 4
Example 2:
S → ABa
A → aab
B → Ac
Sol:
S → AP
S → ABX A → XQ
A → XXY B → AZ
B → AZ P → BX
X→a Q → XY
X→a
Y→b
Y→b
Z→c Z→c
09-06-2022 5
Example 3:
Before converting into CNF,
S → abAB grammar should be simplified
A → bAB | λ
B → BAa | A | λ

Sol:
1) Eliminating left recursion:
2) Eliminating λ productions:
S → abAB
S → abAB | ab |abA |abB
A → bAB | λ
A → b | bA | bB | bAB
B → AC | C B → AC | C | A
C → AaC | λ C → AaC | aC | Aa | a

09-06-2022 6
Example 3:
S → abAB
A → bAB | λ
B → BAa | A | λ
Sol:
2) Eliminating λ productions: 3) Eliminating unit productions:
S → abAB | ab |abA |abB S →abAB | ab | abA | abB
A → b | bA | bB | bAB A → bAB | b | bA | bB
B → AC | AaC | a | Aa | aC | bAB | b | bA | bB
B → AC | C | A C → AaC | a | Aa | aC
C → AaC | aC | Aa | a

09-06-2022 7
Example 3:
S → abAB 3) Eliminating unit productions:
S →abAB | ab | abA | abB
A → bAB | λ A → bAB | b | bA | bB
B → BAa | A | λ B → AC | AaC | a | Aa | aC | bAB | b | bA | bB
Sol: C → AaC | a | Aa | aC
4) Eliminating useless productions:
S → abAB | abA | abB
A → bA | bB | bAB
B → AC | AaC | aC | Aa | bA | bB | bAB
C → AaC | aC | Aa

09-06-2022 8
Example 3:
5) CNF:
4) Eliminating useless productions: S → XT | XR | XQ
S → abAB | abA | abB A → YP | YA | YB
A → bA | bB | bAB B → AC | AL | AX | XC | XP | XA | XB
B → AC | AaC | aC | Aa | bA | bB | bAB C → AL | AX | XC
C → AaC | aC | Aa X→a
Y→b
P → AB
R → YA
Q → YB
L → XC
T → YP
09-06-2022 9
Greibach Normal Form (GNF)
o Here restriction is not on the length of the right sides of a production, but
on the positions in which terminals and variables can appear.
Definition:
oA context-free grammar is said to be in Greibach normal form if all
productions have the form
A → ax,
where a ∈ T and x ∈ V∗.
o The definition is similar to s-grammar but GNF does not carry the
restriction that the pair (A, a) occur at most once.
oFor every context-free grammar G with λ L(G), there exists an
equivalent grammar G in Greibach normal form.
09-06-2022 10
Example-1:
The grammar
S → abSb|aa
Sol:

S → aBSB|aA
A→a
B→b

09-06-2022 11
Example-2:
The grammar
S → AB
A → aA |bB| b
B→b
Sol:
using the substitution
S → aAB |bBB| bB
A → aA |bB| b
B→b

09-06-2022 12
Example-3:
S → XY | AA
X → AB
Apply Substitution Apply Substitution
Y → SB Model Model
A→a
B→b S → ABY | AA S → aBY | aA
X → AB X → aB
Y → ABYB | AAB Y → aBYB | aAB
A→a A→a
B→b B→b
09-06-2022 13
Example-4 S → ABb | a
A → aaA | B
B → bAb

Eliminating Unit Production:


S → ABb | a S → ABY | a S → AP | a S → aQP | bRP | a
A → aaA | bAb A → XXA | YAY A → XQ | YR A → aQ | bR
B → bAb B → YAY B → YR B → bR
X→a P → BY P → bRY
Y→b Q → XA Q → aA
R → AY R → aQY | bRY
X→a Y→b
Y→b

09-06-2022 14
CYK algorithm
➢Originators J. Cocke, D. H. Younger, and T. Kasami.
➢Specific for Context free grammar
➢The algorithm works only if the grammar is in Chomsky normal form
➢Succeeds by breaking one problem into a sequence of smaller ones
➢Is w in L(G)?
➢Uses a “dynamic programming” or “table-filling algorithm”

09-06-2022 15
Construct a Triangular Table
• Each row corresponds to one length of substrings
– Bottom Row – Strings of length 1
– Second from Bottom Row – Strings of length 2

– Top Row – string ‘w’


Xi, j is the set of variables A such that
A → wi is a production of G
Compare at most n pairs of previously computed sets:
(Xi, j , Xi+1, j ), (Xi, j+1 , Xi+2, j ) … (Xi, j-1 , Xj, j )

09-06-2022 16
Construct a Triangular Table
Xn,1 wn

X4,n X4,1 w4

j X3,n X3,2 X3,1 w3

X2,n X2,3 X2,2 X2,1 w2

X1,n X1, 4 X1,3 X1,2 X1,1 w1

09-06-2022 17
Construct a Triangular Table
• Grammar:
1) is the grammar in CNF?
• S → AB
• A → BB | YES
a
• B → AB |
b
2) |w| = 5
• W= aabbb X5,1 b

X4,2 X4,1 b

X3,3 X3,2 X3,1 b

X2,4 X2,3 X2,2 X2,1 a

X1,5 X1, 4 X1,3 X1,2 X1,1 a

09-06-2022 18
Construct a Triangular Table
• S → AB
• A → BB | a
• B → AB | b
b
{B} • W= aabbb
b
{B}

{B} b

{A} a

{A} a

09-06-2022 19
Construct a Triangular Table
• S → AB X1,2 = (X1,1 , X2,1)
• A → BB | a
aa = {A}{A} = {AA}
• B → AB | b
• W= aabbb • Steps:
b – Look for production rules to generate AA
{B}
b – There is no production which produces
{B} AA. Therefore, ∅

{B} b

{A} a

∅ {A} a

09-06-2022 20
Construct a Triangular Table
• S → AB X2,2 = (X2,1 , X3,1)
• A → BB | a
ab = {A}{B} = {AB}
• B → AB | b
• W= aabbb • Steps:

{B} b – Look for production rules to generate


AB
{B}
b
– There are two: {S and B}
{B} b

{S,B} {A} a

∅ {A} a

09-06-2022 21
Construct a Triangular Table
• S → AB X3,2 = (X3,1 , X4,1)
• A → BB | a
bb= {B}{B} = {BB}
• B → AB | b
• W= aabbb • Steps:

{B} b – Look for production rules to generate


BB
{B}
b
– There is one: {A}
{A} {B} b

{S,B} {A} a

∅ {A} a

09-06-2022 22
Construct a Triangular Table
• S → AB X4,2 = (X4,1 , X5,1)
• A → BB | a
• B → AB | b
bb = {B}{B} = {BB}
• W= aabbb • Steps:
{B} b – Look for production rules to generate BB
{A} {B}
b – There is one: {A}

{A} {B} b

{S,B} {A} a

∅ {A} a

09-06-2022 23
Construct a Triangular Table
X1,3 = (X1,2 , X2,2)
{aab = {a}{ab} , {aa}{b}
• S → AB
={A}.{S,B}, {∅}.{B}
• A → BB | a
• B → AB | b = {AS, AB}, {∅}
• W= aabbb • Steps:
{B} b – Look for production rules to generate AS, AB

{A} {B}
b – There are two: {S,B}

{A} {B} b

{S,B} {A} a

{S,B} ∅ {A} a

09-06-2022 24
Construct a Triangular Table
X2,3 = (X2,2 , X3,2)
{abb = {a}{bb} , {ab}{b}
• S → AB
= {AA}, {SB, BB}
• A → BB | a
• B → AB | b • Steps:
• W= aabbb – Look for production rules to generate AA,SB, BB
{B} b – There is one: {A}

{A} {B}
b

{A} {B} b

{A} {S,B} {A} a

{S,B} ∅ {A} a

09-06-2022 25
Construct a Triangular Table X3,3 = (X3,2 , X4,2)
{bbb = {b}{bb} , {bb}{b}
• S → AB
= {BA}, {AB}
• A → BB | a
• B → AB | b • Steps:
• W= aabbb – Look for production rules to generate BA,AB
{B} b
– There are two: {S, B}

{A} {B}
b

{S,B} {A} {B} b

{A} {S,B} {A} a

{S,B} ∅ {A} a

09-06-2022 26
Construct a Triangular Table
X1,4 = (X1,3 , X2,3)

• S → AB {aabb = {a}{abb}, {aa}{bb}, {aab}{b}


• A → BB | a = {A}{A}, {∅}{A}, {S,B}{B}
• B → AB | b = {AA, ∅, SB, BB}
• W= aabbb
• Steps:

b – Look for production rules to generate AA, SB, BB


{B}
– There is one: {A}
{A} {B}
b

{S,B} {A} {B} b

{A} {S,B} {A} a

{A} {S,B} ∅ {A} a

09-06-2022 27
Construct a Triangular Table X2,4 = (X2,3 , X3,3)
{abbb = {a}{bbb}, {ab}{bb}, {abb}{b}
• S → AB = {A}{S,B}, {S,B}{A}, {A}{B}
• A → BB | a
={AS, AB, SA, BA, AB}
• B → AB | b
• W= aabbb • Steps:
– Look for production rules to generate AS, AB, SA, BA

b – There are two: {S, B}


{B}

{A} {B}
b

{S,B} {A} {B} b

{S,B} {A} {S,B} {A} a

{A} {S,B} ∅ {A} a

09-06-2022 28
Construct a Triangular Table X1,5 = (X1,4 , X2,4)

• S → AB aabbb = {a}{abbb}, {aa}{bbb}, {aab}{bb}, {aabb}{b}


• A → BB | a = {A}{S,B}, {∅}{S,B}, {S,B}{A}, {A}{B}
• B → AB | b
={AS, AB, ∅, SA, BA, AB}
• W= aabbb
• Steps:

b – Look for production rules to generate AS, AB, SA, BA, AB


{B}
b – There are two: {S, B}
{A} {B}

{S,B} {A} {B} b

{S,B} {A} {S,B} {A} a

{S,B} {A} {S,B} ∅ {A} a

If S ϵ X1,5 then w is in L(G)


09-06-2022 29
Construct a Triangular Table
Check if S is in (1,n), then
Therefore, aabbb ∈ L(G)
Accept the string
else
{B} b Reject the string
{A} {B}
b

{S,B} {A} {B} b

{S,B} {A} {S,B} {A} a

{S,B} {A} {S,B} ∅ {A} a

09-06-2022 30
Construct a Triangular Table
• Grammar:
• S → AB
1) is the grammar in CNF?
• A → BB | a
• B → AB | b YES
• W= aabb
2) |w| = 4
X4,1 b

X3,1 X3,2 b

X2,1 X2,2 X2,3 a

X1,1 X2,1 X3,1 X4,1 a

09-06-2022 31
Construct a Triangular Table
• Grammar
: Identify the production rule where
these terminals occur
• S → AB
• A → BB |
a
• B → AB |
b
• W= aabb {B}
b

{B} b

{A} a

{A} a

09-06-2022 32
Construct a Triangular Table
• Grammar
: Identify for {aa}, {ab}, {bb}
• S → AB
• A → BB |
a
• B → AB |
b
• W= aabb {B} b

{A} {B} b

{S,B} {A} a

∅ {A} a

09-06-2022 33
Construct a Triangular Table
• Grammar
: Identify for {aab}, {abb}
• S → AB
• A → BB |
a
• B → AB |
b
• W= aabb {B} b

{A} {B} b

{A} {S,B} {A} a

{S,B} ∅ {A} a

09-06-2022 34
Construct a Triangular Table
• Grammar
: Identify for {aabb}
• S → AB
Therefore, aabb does not belong to L(G)
• A → BB |
a
• B → AB |
b
• W= aabb {B} b

{A} {B} b

{A} {S,B} {A} a

{A} {S,B} ∅ {A} a

09-06-2022 35
Assignment
• 1) Apply CYK 2) Apply CNF 3) Apply GNF
Algorithm S → AB | aB a) S → aSb |ab
• S → XY A → aab | ∈ B→b

• X → XA | a | b B → bbA b) S → aSb | bSa | a | b

• Y → AY | a
•A→a
a) W = babaa
b) b) W = baaaa

09-06-2022 36

You might also like