CNF & Leftmost Derivation - Theory of Computation

15
CHOMSKY NORMAL FORM (CNF) & LEFTMOST DERIVATION (LMD) MADE BY: DIMPY CHUGH(1833) DRISHTI BHALLA(1838)

Transcript of CNF & Leftmost Derivation - Theory of Computation

Page 1: CNF & Leftmost Derivation - Theory of Computation

CHOMSKY NORMAL FORM(CNF)

&LEFTMOST

DERIVATION (LMD)

M A D E B Y : D I M P Y C H U G H ( 1 8 3 3 )

D R I S H T I B H A L L A ( 1 8 3 8 )

Page 2: CNF & Leftmost Derivation - Theory of Computation

DEFINITIONIf a Context Free Grammar or CFG has only productions of

the formNonterminal -> string of exactly two non-terminals

or Nonterminal -> one terminal then, it is said to be in Chomsky Normal Form, CNF.

Example: S → AS | a A → SA | b

Page 3: CNF & Leftmost Derivation - Theory of Computation

STEPS FOR CONVERSION

• The conversion to Chomsky Normal Form has four main steps:

1. Get rid of all λ-Productions .2. Eliminate Non-terminal Unit Productions. A → B , B → b A → b3. Elimination of more than one terminals by a non- terminal in a production.4. Replace every production that is too long by shorter productions. A → BCD A → BE E → CD

Page 4: CNF & Leftmost Derivation - Theory of Computation

THEOREM-1STATEMENT- If L is a language generated by some CFG, then there

is another CFG that generates all the non- λ words of L, all of whose productions are of one of two basic forms:

Nonterminal-> string of only Non-terminals or

Nonterminal -> one terminalPROOF- Let us start with the CFG-

S -> X1 | X2aX2 | aSb | bXl -> X2X2 | bX2 -> aX2 | aaX1

Now, in order to convert it into the above form , we introduce new productions : A->a and B->b

Page 5: CNF & Leftmost Derivation - Theory of Computation

After conversion we have,

S -> X1 X1 -> X2X2S -> X2AX2 X1 -> BS -> ASB X2 -> AX2S -> B X2 -> AAX1

Thus, we have converted the original CFG into the desired form.

Page 6: CNF & Leftmost Derivation - Theory of Computation

STATEMENT- For any context- free language L, the non- λ words of L can be generated by a grammar in which all the productions are in CNF.

PROOF - Let our initial CFG is S -> XI X2 X3 X8 S -> X3 X5 S -> b

Now, in order to get the given CFG in CNF form we’ll add certain new non-terminals , i.e. S -> XI X2 X3 X8 S -> XI R1 R1 -> X2 X3 X8 R1 -> X2 R2 R2 -> X3 X8

THEOREM-2

Page 7: CNF & Leftmost Derivation - Theory of Computation

Consider another CFG X -> X1aX2bX3Apply Theorem 1 and add productions A->a and B->b to get string of solid non-terminals.Now, X -> X1AX2BX3 X -> X1R1 R1 -> AX2BX3 R1 -> AR2 R2 -> X2BX3 R2 -> X2R3 R3 -> BX3

In this way, we have converted the long productions with long strings of non-terminals into sequences of productions with exactly 2 non-terminals on RHS. Thus, the new grammar in CNF generates the same language as the old grammar.

Page 8: CNF & Leftmost Derivation - Theory of Computation

Ques16 i). The following CFG has unit productions. Convert the following CGF into CNF.S -> X

X -> Y Y-> Z Z ->aa

Sol16). S -> X -> Y => S -> Y S -> Y -> Z => S -> Z S -> Z -> aa =>S ->aa Since S->aa is not in CNF , we introduce a new production A->a to transform it to CNF. Thus, the new grammar in CNF form is : A -> a S -> AA

QUESTIONS

Page 9: CNF & Leftmost Derivation - Theory of Computation

Ques14 v). Convert the following CGF into CNF. S -> ABABAB A -> a|λ B -> b|λSol13). Removing λ productions, A -> a ---------1 B -> b ---------2

S -> ABABAB Using Theorem 2, S -> AR1 ---------3 R1 -> BABAB R1 -> BR2 ---------4 R2 -> ABAB R2 -> AR3 ---------5 R3 -> BAB R3 -> BR4 ---------6 R4 -> AB ---------7Thus, the new grammar in CNF form comprises of (1,2,3,4,5,6,7).

Page 10: CNF & Leftmost Derivation - Theory of Computation

Ques14 iv). Convert the following CGF into CNF. E -> E + E E -> E*E E -> (E) E -> 7 The terminals here are +, *, (, ), 7.Sol12). E -> 7 --------------1 Using Theorem 1, we add new productions that will help in converting CGF to CNF. F -> + --------------2 G -> * --------------3 H-> ( --------------4 I -> ) --------------5 Now , we have E -> EFE E -> ER1 --------------6 R1 -> FE --------------7 E -> EGE E -> ER2 --------------8 R2 -> GE --------------9 E -> HEI E -> HR3 -------------10 R3 -> EI -------------11Thus, the new grammar in CNF form comprises of (1,2,3,4,5,6,7,8,9,10,11).

Page 11: CNF & Leftmost Derivation - Theory of Computation

LEFT MOST DERIVATIONS• The leftmost non-terminal in a working string is the first

non-terminal that we encounter when we scan the string from left to right.

For example : In the string abNbaXYa , the leftmost nonterminal is N.

• If a word w is generated by a CFG by a certain derivation and at each step in the derivation, a rule of production is applied to the leftmost non-terminal in the working string, then this derivation is called a leftmost derivation.

Page 12: CNF & Leftmost Derivation - Theory of Computation

Ques17 i)Find the leftmost derivation for the word “abba” in the grammar:

S -> AA A -> aB B -> bB | λ Sol18)(i). The following is a leftmost derivation:S -> AAS -> aBAS -> abBAS -> abbBAS -> abbλAS -> abbaBS -> abbaλS -> abba

QUESTIONS

Page 13: CNF & Leftmost Derivation - Theory of Computation

Ques17 ii) Find the leftmost derivation for the word “abbabaabbbabbab” in the CFG:

S->SSS|aXbX->ba|bba|abb

Sol18)(ii). The following is a leftmost derivation: S -> SSS S -> aXbSS S -> abbabSS S -> abbabaXbS S -> abbabaabbbS S -> abbabaabbbaXb S -> abbabaabbbabbab

Page 14: CNF & Leftmost Derivation - Theory of Computation

ADVANTAGES..• CNF Grammar can be used to determine whether a string is a

member of the language associated with it or not .• It can be used to find the no. of production steps required to

generate a string. The key advantage is that in Chomsky Normal Form, every derivation of a string of n letters has exactly 2 n − 1 steps.

Example : S -> AX|AB A -> a X -> SB B -> b

The string to be generated is “ aabb” having n(no. of letters in a string ) =4. The following is a leftmost derivation:1) S -> AX2) S -> aX3) S -> aSB4) S -> aABB5) S -> aaBB6) S -> aabB7) S -> aabbHere, A string of n(n=4) letters has exactly 2n-1(2*4-1=7) steps .

Page 15: CNF & Leftmost Derivation - Theory of Computation

THANK YOU !!!