Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 65

UNIT 2

CONTEXT FREE GRAMMAR


Grammar
• Grammar is a finite set of formal rules that
generates syntactically correct sentences.
• Grammar is formally defined by 4 tuples
(V,T,P,S).
• V – finite set of non-terminals.
• T – finite set of terminal symbols.
• P – set of Production rules that generates a set
of strings.
• S – Starting Symbol

Prepared by S.S.KIRUTHIKA, 2
AP/CSE, SRMIST,RAMAPURAM
Grammar

• Terminal Symbols - Terminal symbols are the components of the


sentences that are generated using grammar and are denoted using
small case letters like a, b, c etc.
• Non-Terminal Symbols - Non-Terminal Symbols take part in the
generation of the sentence but are not the component of the
sentence. These types of symbols are also called Auxiliary Symbols
or Variables. They are represented using a capital letter like A, B, C,
etc.
• Production : Productions are used to generate the set of strings
which are in the form of α → β , where α and β are combination of
terminals and non-terminals which differ for each type of grammar.
• Starting Symbol: S is the starting production of the grammar.
Prepared by S.S.KIRUTHIKA, 3
AP/CSE, SRMIST,RAMAPURAM
Types of Grammar

Prepared by S.S.KIRUTHIKA, 4
AP/CSE, SRMIST,RAMAPURAM
Chomskian Hierarchy

Prepared by S.S.KIRUTHIKA, 5
AP/CSE, SRMIST,RAMAPURAM
Type 0 grammar
• It is also known ass unrestricted Grammar.
• The languages recognized by this grammar is known as recursively
enumerable languages.
• These languages aremodeledby Turing Machine.
• Production is of the form α → β
• Where α is ( V + T)* V ( V + T)*
V : Variables/NT
T : Terminals.
β  is ( V + T )*.
• In type 0 there must be at least one variable on Left side of
production.
Examples: SaA → ba , A → S.
S → ACaB, Bc → acB, CB → DB, aD → Db.

Prepared by S.S.KIRUTHIKA, 6
AP/CSE, SRMIST,RAMAPURAM
Type 1 grammar
• It is also known as Context sensitive grammar.
• It is modeled by linear bounded automata.
• All Type 1 grammar should be of Type 0.
• Production is of the form α → β
• α , β is ( V + T )+.
| α | <= | β |
i.e count of symbol in   α is less than or equal to  β
• Examples: S → AB,
A → a,
B → bB,
AB → AbBc

Prepared by S.S.KIRUTHIKA, 7
AP/CSE, SRMIST,RAMAPURAM
Type 2 Grammar
• It is also known as Context free grammar.
• It is recognized by Pushdown Automata(PDA).
• All Type 2 grammar should be of Type 1.
• Production is of the form α → β
• where α is a single non-terminal and β is ( V + T )*.
|α|=1
Examples:S → AB,
A → a,
B → aBb,
AB → ab

Prepared by S.S.KIRUTHIKA, 8
AP/CSE, SRMIST,RAMAPURAM
Type 3 Grammar
• It is also known as regular grammar.
• It is recognized by Finite Automata.
• It is the most restricted form of grammar.
• Production is of the form α → β
• where α is V and β is T*V / T*
• Examples: S →A | ϵ
A →a
A →abA
A → ab

Prepared by S.S.KIRUTHIKA, 9
AP/CSE, SRMIST,RAMAPURAM
Context Free Grammar
• CFG is a formal grammar which is used to generate all
posssible set of strings in the given formal language.
• CFG is defined by 4 tuples (V, T, P, S).
• By using the start symbol of the given grammar, a string
can be derived by repeatedly replacing a non-terminal by
the right hand side of the production, until all non-terminal
have been replaced by terminal symbols.
• It is used to generate all possible patterns of strings in a
given formal language.

Prepared by S.S.KIRUTHIKA, 10
AP/CSE, SRMIST,RAMAPURAM
Context Free Grammar
Example 1
Construct the CFG for the language having any number of
a's over the set ∑= {a}. R.E= a*   
Productions:
S →aS (Rule 1)
S→ϵ
Derive the String aaa:
Starting Symbol S → aS (rule 1)
→aaS (rule 1)
→aaaS (rule 1)
→aaa (rule 2)

Prepared by S.S.KIRUTHIKA, 11
AP/CSE, SRMIST,RAMAPURAM
Context Free Grammar
Example 2
Construct the CFG for the language having an equal
number of 0’s and 1’s over the set ∑= {0,1}. R.E= 0*1*   
Productions:
S →0S1 (Rule 1)
S→ϵ (Rule 2)
Derive the String 0011:
Starting Symbol S → 0S1 (rule 1)
→ 00S11 (rule 1)
→ 0011 (rule 2)

Prepared by S.S.KIRUTHIKA, 12
AP/CSE, SRMIST,RAMAPURAM
Context Free Grammar
Example 3
Construct the CFG for the R.E= (0+1)*   
Productions:
S →0S | 1S (Rule 1)
S→ϵ (Rule 2)
Derive the String 0110:
Starting Symbol S → 0S (rule 1)
→ 01S (rule 1)
→ 011S (rule 1)
→ 0110S (rule 1)
→ 0110ϵ (rule 2)
→ 0110
Prepared by S.S.KIRUTHIKA, 13
AP/CSE, SRMIST,RAMAPURAM
Context Free Grammar
Example 4
Construct the CFG for palindrome over the set ∑= {a,b}*.
Productions:
S → aSa | bSb (Rule 1)
S→a|b|ϵ (Rule 2)
Example 5
Construct the CFG for the language L={anban, n>0}
Productions:
S → aSa (Rule 1)
S→b (Rule 2)

Prepared by S.S.KIRUTHIKA, 14
AP/CSE, SRMIST,RAMAPURAM
Context Free Grammar
Example 5
Construct the CFG for the language L={anb2n, n>0}
Productions:
S → aSbb (Rule 1)
S → abb (Rule 2)
Example 6:
Construct the CFG for the language which contains equal
no of a’s and b’s.
Productions:
S → SaSbS (Rule 1)
S → SbSaS (Rule 2)
S→ϵ (Rule 3)
Prepared by S.S.KIRUTHIKA, 15
AP/CSE, SRMIST,RAMAPURAM
Context Free Grammar
Example 7:
Construct a CFG for the RE=(011+1)* (01)*
Productions:
S → AB      rule 1  
A →  ε /CA      rule 2 
C→ 011/1 rule 3
B → ε /DB rule 4
D  → 01 rule 5

Prepared by S.S.KIRUTHIKA, 16
AP/CSE, SRMIST,RAMAPURAM
Derivation
• Derivation is a sequence of production rules.
• It is used to derive the input string through these
production rules.
• During parsing, we have to take two decisions. These
are as follows:
– We have to decide the non-terminal which is to be replaced.
– We have to decide the production rule by which the non-terminal
will be replaced.
• We have two options to decide which non-terminal to be
placed with production rule.
 Left Most Derivation
 Right Most Derivation
• To illustrate a derivation, we can draw a derivation tree (also called
a parse tree)
Prepared by S.S.KIRUTHIKA, 17
AP/CSE, SRMIST,RAMAPURAM
Left Most Derivation (LMD)
• In the leftmost derivation, the input is scanned and
replaced with the production rule from left to right. So in
leftmost derivation, we read the input string from left to
right.
Example:
E  → E + E  
E  →  E - E  
E  →  a | b. Derive the string a-b+a using LMD.
Soln: E → E + E
→ E - E + E
→a-E+E
→a-b+E
→a-b+a
Prepared by S.S.KIRUTHIKA, 18
AP/CSE, SRMIST,RAMAPURAM
Right Most Derivation (LMD)
• In the rightmost derivation, the input is scanned and
replaced with the production rule from right to left. So in
leftmost derivation, we read the input string from right to
left.
Example:
E  → E + E  
E  →  E - E  
E  →  a | b. Derive the string a-b+a using RMD.
Soln: E → E + E
→ E + a
→E-E+a
→E-b+a
→a-b+a
Prepared by S.S.KIRUTHIKA, 19
AP/CSE, SRMIST,RAMAPURAM
Parse Tree
• Parse tree is the graphical representation of symbol. The
symbol can be terminal or non-terminal.
• In parsing, the string is derived using the start symbol.
• The root of the parse tree is that start symbol.
• All leaf nodes have to be terminals.
• All interior nodes have to be non-terminals.
• In-order traversal gives original input string.

Prepared by S.S.KIRUTHIKA, 20
AP/CSE, SRMIST,RAMAPURAM
Parse Tree - LMD
Grammar:
E  → E + E  
E  →  E - E  
E  →  a | b.
Construct parse tree for deriving the string a-b+a using
LMD.
E
Step 1 Step 2 Step 3
E
E
E + E
E + E
E + E E - E
E - E
Prepared by S.S.KIRUTHIKA, a 21
AP/CSE, SRMIST,RAMAPURAM
Parse Tree - LMD
Step 4 Step 5
E E

E + E E + E

E - E E - E a

a b a b

Prepared by S.S.KIRUTHIKA, 22
AP/CSE, SRMIST,RAMAPURAM
Parse Tree - RMD
Grammar:
E  → E + E  
E  →  E - E  
E  →  a | b.
Construct parse tree for deriving the string a-b+a using
RMD.
Step 1 Step 2 Step 3 E
E
E
E + E
E + E
E + E
a
a E - E
Prepared by S.S.KIRUTHIKA, 23
AP/CSE, SRMIST,RAMAPURAM
Parse Tree - RMD
Step 4 Step 5
E
E

E + E E + E

a
E - E a
E - E

b
a b

Prepared by S.S.KIRUTHIKA, 24
AP/CSE, SRMIST,RAMAPURAM
Ambiguous Grammar
• The given grammar is said to be ambiguous grammar if there exists
more than one LMD or more than one RMD for the given input
string.
• Grammar: E  → E + E  , E  →  E - E  , E  →  a | b.
LMD 1 E E
LMD 2

E + E E - E

E - E a a E + E

a b b a
• Since the string a-b+a has more than one LMD , the given grammar
is said to be ambiguous grammar.

Prepared by S.S.KIRUTHIKA, 25
AP/CSE, SRMIST,RAMAPURAM
LEFT RECURSION
• A Grammar G (V, T, P, S) is left recursive if it has
a production in the form.
A → A α |β
• It can be eliminated by replacing a pair of
production with
A → βA’
A’ → αA’|ϵ

Prepared by S.S.KIRUTHIKA, 26
AP/CSE, SRMIST,RAMAPURAM
Problem 1
Eliminate the left recursion in the grammar
E →E+T | T , T → T*F | F , F → (F) | id
Solution:
• If theproduction is of the form A → A α |β
• It can be eliminated by replacing a pair of production with
A → βA’
A’ → αA’|ϵ
Step 1: The producion E →E+T | T is left recursive. It can
be eliminated by using the above rule. Introduce a new non-
terminal E’. So, the production becomes
E →TE’ ----(1)
E’ →+TE’ -----(2)

Prepared by S.S.KIRUTHIKA, 27
AP/CSE, SRMIST,RAMAPURAM
Problem 1
Step 2: The producion T →T*F | F is left recursive. It can be eliminated
by using the above rule. Introduce a new non-terminal T’. So, the
production becomes
T →FT’ ----(3)
T’ →*FT’ -----(4)
Step 3: The producion F->(F) | id is not left recursive. So there is no
changes in the production.
F →(F) | id -----(5)
The final set of productions in the grammar G after elminating left
recursion are
E →TE’
E’ →+TE’
T →FT’
T’ →*FT’
F →(F) | id
Prepared by S.S.KIRUTHIKA, 28
AP/CSE, SRMIST,RAMAPURAM
Problems on Left Recursion
Eliminate the left recursion in the following
grammar.
1. S → a|^|(T)
T → T, S|S
2. E → E(T)|T
T → T(F)|F
F → id
3. S → A
A → Ad / Ae / aB / ac
B → bBc / f

Prepared by S.S.KIRUTHIKA, 29
AP/CSE, SRMIST,RAMAPURAM
Simplification of CFG
Simplification of CFG
• Various languages can efficiently be represented
by a context-free grammar.
• All the grammar are not always optimized that
means the grammar may consist of some extra
symbols(non-terminal).
• Having extra symbols, unnecessary increase the
length of grammar.

Prepared by S.S.KIRUTHIKA, 31
AP/CSE, SRMIST,RAMAPURAM
Simplification of CFG
• Simplification of grammar means reduction of
grammar by removing useless symbols. The
properties of reduced grammar are given below:
• Each variable (i.e. non-terminal) and each
terminal of G appears in the derivation of some
word in L.
• There should not be any production as X → Y
where X and Y are non-terminal.
• If ε is not in the language L then there need not
to be the production X → ε.

Prepared by S.S.KIRUTHIKA, 32
AP/CSE, SRMIST,RAMAPURAM
Simplification of CFG
• Simplification of CFG includes the followin:
– Elimination of Useless Symbol
– Elimination of unit Production
– Elimination o Null Production

Prepared by S.S.KIRUTHIKA, 33
AP/CSE, SRMIST,RAMAPURAM
Elimination of Useless Symbol
• A symbol can be useless if it does not appear on
the right-hand side of the production rule and
does not take part in the derivation of any string.
That symbol is known as a useless symbol.
• Similarly, a variable can be useless if it does not
take part in the derivation of any string. That
variable is known as a useless variable.
• To eliminate useless symbol, ne need to find the
generating symbols and reaching symbols.
• If any of the symbol is non-generating or
unreachable then the symbol is useless symbol.
Prepared by S.S.KIRUTHIKA, 34
AP/CSE, SRMIST,RAMAPURAM
Generating Symbols
• Generating variables
• A variable X is called as generating 

- if it derives a string of terminals.

- Note that the language accepted by a context-free grammar is non-


empty if and only if the start symbol is generating.
• Algorithm to find the non-generating variables in a CFG
▪ Mark a variable X as "generating"
- if it has a production X -> w, where w is a string of only terminals
and/or variables previously marked "generating".
▪ Repeat the above step until no further variables get marked
"generating".
▪ All variables not marked "generating" are non-generating

Prepared by S.S.KIRUTHIKA, 35
AP/CSE, SRMIST,RAMAPURAM
Reachable Symbols
• Reachable variables
• A variable X is called as reachable 
- if the start symbol derives a string containing the variable X.

• Algorithm to find the non-reachable variables in a CFG


• Mark the start variable as "reachable".
• Mark a variable Y as "reachable" if there is a production X -> w,
where X is a variable previously marked as "reachable" and
w is a string containing Y.
• Repeat the above step until no further variables get marked
"reachable".
• All variables not marked "reachable" are non-reachable

Prepared by S.S.KIRUTHIKA, 36
AP/CSE, SRMIST,RAMAPURAM
Elimination of Useless Symbols
• Example:
S -> abS | abA | abB
A ->cd
B->aB
C->dc
Solution:
1. Eliminate all variables which are non-generating. (ie) variables
which do not derive any set of teminals.
Here S,A and C are generating where as B doednot derive any set
of strings of terminals. So B is non-generating and the producitons
which contains B can be eliminated.
S->abB and B->aB are eliminated and the grammar becomes
S -> abS | abA
A ->cd
C->dc
Prepared by S.S.KIRUTHIKA, 37
AP/CSE, SRMIST,RAMAPURAM
Cont.
2. Eliminate all unreachable symbols (ie0 the variables
which are not reachable from the starting symbol of the
grammar.
From S, A can be reached but C is not reachable.
Hence C is useless Symbol. So remove the productions
which contains C. Hence the grammar becomes
S -> abS | abA
A ->cd
The abover productions are in the given grammar after
removing the useless symbols.

Prepared by S.S.KIRUTHIKA, 38
AP/CSE, SRMIST,RAMAPURAM
Order of elimination
• Elimination of useful symbols - Order of elimination
• Always Eliminate non-generating symbol first and then
eliminate non-reachable symbols
• Reversing the order of elimination would not work
S -> AB | a
A -> aA
B -> b
• Here A is non-generating, and after deleting A (along
with the production S -> AB) the variable B becomes
unreachable. Hence, it is considered as useless
variable
• However, if we would first test for reachability, all
variables would be reachable, and subsequently
eliminating non-generating variables would leave us
with B.
Prepared by S.S.KIRUTHIKA, 39
AP/CSE, SRMIST,RAMAPURAM
Elimination of Useless Symbols
2. Remove the useless symbol from the given context free
grammar
S -> aB / bX
A -> Bad / bSX / a
B -> aSB / bBX
X -> SBD / aBx / ad

3. Remove the useless symbols from the given grammar


A -> xyz / Xyzz
X -> Xz / xYz
Y -> yYy / Xz
Z -> Zy / z

Prepared by S.S.KIRUTHIKA, 40
AP/CSE, SRMIST,RAMAPURAM
Elimination of Null production
• The productions of type S → ε are called ε(null)
productions. These type of productions can only be
removed from those grammars that do not generate ε.
• Step 1: First find out all nullable non-terminal variable
which derives ε.
• Step 2: For each production A → a, construct all
production A → x, where x is obtained from a by
removing one or more non-terminal from step 1.
• Step 3: Now combine the result of step 2 with the
original production and remove ε productions.

Prepared by S.S.KIRUTHIKA, 41
AP/CSE, SRMIST,RAMAPURAM
Elimination of Null production

Example 1: Eliminate Null production from the given


grammar. S → XYX  , X → 0X | ε  , Y → 1Y | ε

Solution :
There are two null produtions are there X → ε & Y → ε
1. For eliminating X → ε, substitute x= ε in all the
productions which contains X in the right hand of the
production. So the grammar becomes
S → XYX | YX | XY | Y  ,
X → 0X
Y → 1Y | ε Prepared by S.S.KIRUTHIKA, 42
AP/CSE, SRMIST,RAMAPURAM
Cont..

2. For eliminating Y → ε, substitute Y= ε in all the


productions which contains Y in the right hand of the
production. So the grammar becomes

S → XYX | YX | XY | Y  |XX,
X → 0X | 0
Y → 1Y | 1
The abover productions are in the given grammar after removing
the Null Production.

Prepared by S.S.KIRUTHIKA, 43
AP/CSE, SRMIST,RAMAPURAM
Elimination of Null Prduction
2. Remove the ε production from the following CFG by
preserving the meaning of it.
S → ASA | aB | b
A→B
B→b|∈

3. Remove the null productions from the following grammar


S -> ABAC
A -> aA / ϵ
B -> bB / ϵ and
C -> c

Prepared by S.S.KIRUTHIKA, 44
AP/CSE, SRMIST,RAMAPURAM
Elimination of Unit Production

• The unit productions are the productions in which one


non-terminal gives another non-terminal. Use the
following steps to remove unit production:
• Step 1: To remove X → Y, add production X → a to the
grammar rule whenever Y → a occurs in the grammar.
• Step 2: Now delete X → Y from the grammar.
• Step 3: Repeat step 1 and step 2 until all unit
productions are removed.

Prepared by S.S.KIRUTHIKA, 45
AP/CSE, SRMIST,RAMAPURAM
Elimination of Unit Production
Example:
1. Eliminate Unit productions from the given grammar
S-> aX / bY / Y , X-> S, Y -> bY / b
Solution:
• There are two unit productions in the given grammar, S -> Y and X -> S
• Substituting the values of unit production S -> Y we get,
S-> aX / bY / bY / b ----🡪 S-> aX / bY / b
• Substituting the values of unit production X -> S we get,
X-> aX / bY / Y
• Final set of productions would be,
S-> aX / bY / b
X-> aX / bY / Y
Y -> bY / b
Prepared by S.S.KIRUTHIKA, 46
AP/CSE, SRMIST,RAMAPURAM
Elimination of Unit Production
2. Eliminate Unit productions from the given grammar
S -> AB
A -> a , B -> C , C -> D and D -> b.
3. Identify and remove the unit productions from the
following CFG
S -> S + T/ T
T -> T * F/ F
F -> (S)/a
4. Remove the unit productions from the following grammar
S -> AB
A -> a
B -> C / b
C -> D
D -> E
E -> a Prepared by S.S.KIRUTHIKA, 47
AP/CSE, SRMIST,RAMAPURAM
Normal Form
Normal Form
• Normalization is performed in order to standardize the
grammar.
• By reducing the grammar, the grammar gets minimized
but does not gets standardized.
• This is because the RHS of productions have no specific
format.
• In order to standardize the grammar, normalization is
performed using normal forms.

Prepared by S.S.KIRUTHIKA, 49
AP/CSE, SRMIST,RAMAPURAM
Types of Normal Form

Prepared by S.S.KIRUTHIKA, 50
AP/CSE, SRMIST,RAMAPURAM
Chomsky Normal Form
• A context free grammar is said to be in chomsky normal form (CNF)
if all its productions are of the form:
NT→ (NT)(NT) or NT → T
• where A, B, C are non-terminals and a is a terminal.
• To be in CNF, all the productions must derive either two non-
terminals or a single terminal.
• CNF restricts the number of symbols on the right side of a
production to be two.
• The two symbols must be non-terminals or a single terminal.
• Examples:
• S→ AB
• A→BC
• A→a
• B→ b

Prepared by S.S.KIRUTHIKA, 51
AP/CSE, SRMIST,RAMAPURAM
Chomsky Normal Form
Steps in converting CFG to CNF:
1. Eliminating useless productions,Eliminating ∈ productions,
Eliminating unit productions
2. Eliminate terminals from RHS if they exist with other terminals or
non-terminals.
Example:
If production is of the form S→aA ,
ntroduce a new non-terminal X. Then the productions become
X→a
S→XA
If production is of the form S→ABC ,
introduce a new non-terminal X. Then the productions become
x→BC
S→AX
Prepared by S.S.KIRUTHIKA, 52
AP/CSE, SRMIST,RAMAPURAM
CNF - Problem 1
1. Convert the following grammar into CNF.
S→bA | aB
A→bAA | aS | a
B→aBB | bS | a
Solution:
1. There is no useless symbol, no unit production and no null
production in the grammar.
2. S→bA|aB is not in CNF. So Introduce two non-terminals X and
Y.Now the productions become
X→a
Y→b
S→XA | YB
A→YAA | XS | a
B→XBB | YS | a

Prepared by S.S.KIRUTHIKA, 53
AP/CSE, SRMIST,RAMAPURAM
Cont..
3. A→YAA and B→XBB is not in CNF. So Introduce two non-terminals
U and V. Now the productions become
X→a
Y→b
U→AA
V→BB
S→XA | YB
A→YU | XS | a
B→XV | YS | a
Now the above resultant grammar is in CNF.

Prepared by S.S.KIRUTHIKA, 54
AP/CSE, SRMIST,RAMAPURAM
CNF Problem 2
2. Convert the following CFG to Chomsky normal form:
S→A / B / C
A→aAa / B
B→bB / bb
C→baD / abD / aa
D→ aCaa / D
Solution:
1. All the symbols are generating and reachable symbols. So there
are no uselss symbols. There is no Null productions.
2. Since there is UNIT productions, need to eliminate all UNIT
productions one by one.
Eliminate D→D directly since both LHS and RHS are same non-
terminal.
Eliminate A→B by replacing the productions of B in S.

Prepared by S.S.KIRUTHIKA, 55
AP/CSE, SRMIST,RAMAPURAM
CNF Problem 2
• Therefore, the prouctions become,
S→A / B / C
A→aAa / bB / bb
B→bB / bb
C→baD / abD / aa
D→ aCaa / D
Eliminate S→A/B/C by replacing the productions of A, B and C in S.
Therefore, the prouctions become,
S→aAa / bB / bb / baD / abD / aa
A→aAa / bB / bb
B→bB / bb
C→baD / abD / aa
D→ aCaa / D
Since bB / bb appears twice in S, keep one copy of it.

Prepared by S.S.KIRUTHIKA, 56
AP/CSE, SRMIST,RAMAPURAM
CNF Problem 2
3. Since all the productios are not in CNF. Introduce two non-treminals X and
Y. Now the productions are
X→a Y→b
S→XAX / YB /YY / YXD / XYD / XX
A→XAX / YB /YY
B→YB /YY
C→YXD / XYD / XX
D→ XCXX
Now again the productions are not inCNF. So Introduce new non-terminals
Z,U,V,W,P. The productions are
X→a Y→b
Z→AX U→XY V→YX W→XX P→XC
S→XZ / YB / YY / VD / UD / XX
A→XZ / YB / YY B→YB / YY
C→VD /UD / XX D→ PW
Now the above resultant grammar is in CNF.

Prepared by S.S.KIRUTHIKA, 57
AP/CSE, SRMIST,RAMAPURAM
CNF Practice Problems
3. Convert the following CFG to Chomsky normal form:
S→ ABC / BaB
A →aA / BaC / aaa
B →bBb / a / D
C →CA / AC
D→ ε.
4. Convert the following grammar into CNF.
S → cBA
S→A
A → cB | AbbS
B → aaa
5. Construct a equivalent grammar G in CNF for the grammar G1
where G1=({S,A,B},{a,b},{S →ASB/ ε , A→ aAS/a, B→ SbS/A/bb},S)

Prepared by S.S.KIRUTHIKA, 58
AP/CSE, SRMIST,RAMAPURAM
GREIBACH NORMAL FORM
• A context free grammar is said to be in chomsky normal
form (CNF) if all its productions are of the form:
NT → T or NT→ (T)(NT)*
• Examples:
S→c
A→a
A→aB
A→aBCD

Prepared by S.S.KIRUTHIKA, 59
AP/CSE, SRMIST,RAMAPURAM
Steps involved in GNF
1. Eliminating useless productions,Eliminating ∈ productions,
Eliminating unit productions
2. Check whether the grammar is in CNF form. If not, convert
it into CNF.
3. Replace all the variables by A1,A2,… by assigning A1 to
the starting symbol of the grammar.
4. Assign the new variable to the grammar. So the
productions become Ai->Aj β
i) i < j – Do Nothing
ii) i > j – Apply Rule 1
iii) i = j – Apply Rule 2
4. Repeat step 3 till all the productions to be in the GNF.
5. The resultant grammar isbyinS.S.KIRUTHIKA,
Prepared GNF. 60
AP/CSE, SRMIST,RAMAPURAM
Cont..
Rule 1/Lemma 1:
If G = (V,T,P,S) is a CFG and, the set of ‘A’ production belong
to P are

A → Aα ------ (1)
A → β1 | β2 | β3 | β4 ----- | βn ------ (2)
then Let G’ = (V’,T,P’,S)
Where P’ be
A → β1 α | β2 α | β3 α | β4 α ----- | βn α

Prepared by S.S.KIRUTHIKA, 61
AP/CSE, SRMIST,RAMAPURAM
Cont..
Rule 2/Lemma 2:
• If G = (V,T,P,S) is a CFG and, the set of ‘A’ production belong
to P are
A → Aα1 | Aα2 | Aα3 -----| Aαm | β1 | β2 | β3 ------ | βn
• Then introduce a new non-terminal X
• So,Let G’ = (V’,T,P’,S) , Where V’ = (V ∪ X) Where P’ can be
formed
A → β1 | β2 | β3------| βn
A → β1X | β2X | β3X------| βnX
X → α1 | α2 | α3-----| αm
X → α1X | α2X | α3X-----| αmX

Prepared by S.S.KIRUTHIKA, 62
AP/CSE, SRMIST,RAMAPURAM
GNF Problem 1
Convert the following CFG to GNF.
S->AA |0
A-> SS |1
Solution:
Step 1,2: The given grammar is in CNF.
Step 3 :Replace S by A1 and A by A2.
The productions becomes
A1→A2A2 | 0 ----- > (1)
A2→A1A1 | 1------ > (2)
Step 4: A1→A2A2 |0 (i=1, j=2, since i<j no changes)
A2→A1A1 |1 (i=2,j=1, i>j Apply Rule 1)
Sub (1) in (2)
Prepared by S.S.KIRUTHIKA, 63
AP/CSE, SRMIST,RAMAPURAM
GNF Problem 1
A2→A2A2 A1 | 0 A1|1 --------> (3) (i=2, j=2, i=j so apply Rule 2)
Introduce new variable Z.
(α1 =A2 A1, β= 0 A1|1 )
Z→ A2 A1--------> (4)
Z→ A2 A1Z--------> (5)
A2→ 0 A1|1--------> (6)
A2→ 0 A1Z|1Z --------> (7)
Sub (6)&(7) in (4),(5)&(1)
Z→0 A1 A1 | 1A1 | 0 A1 ZA1|1ZA1
Z→0 A1A1Z | 1A1Z | 0 A1ZA1Z |1ZA1Z
A1→ 0 A1A2 | 1A2 | 0 A1ZA2 | 1ZA2| 0

Prepared by S.S.KIRUTHIKA, 64
AP/CSE, SRMIST,RAMAPURAM
GNF Problem 1

Finallly the productions are


A1→ 0 A1A2 | 1A2 | 0 A1ZA2 | 1ZA2|0
A2→ 0 A1 | 1 | 0 A1Z | 1Z
Z→0 A1 A1 |1A1 |0 A1 ZA1 |1ZA1
Z→0 A1A1Z |1A1Z | 0 A1ZA1Z | 1ZA1Z

Prepared by S.S.KIRUTHIKA, 65
AP/CSE, SRMIST,RAMAPURAM

You might also like