Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

25
Lecture # 25 Theory Of Automata By Dr. MM Alam 1

Transcript of Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

Page 1: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

1

Lecture # 25

Theory Of AutomataBy

Dr. MM Alam

Page 2: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

Lecture#24 at a glance…

• Unrestricted Grammars Example• User controlled Parsing in JFLAP• Regular Grammar Conversion to FA

Page 3: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

3

FA Conversion to Regular grammarEXAMPLE 3

• Consider the CFG:S → aaS l bbS I abXI baX I λX → aaX l bbX l abS l baS

• The algorithm tells us that there will be three states: -, X, +.

Page 4: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

4

FA Conversion to Regular grammarEXAMPLE 3

• Since there is only one production of the form

Np → wq • TG is

S → aaS l bbS I abXI baX I λX → aaX l bbX l abS l baS

Page 5: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

5

FA Conversion to Regular grammarEXAMPLE 4

• Consider the CFG:S → aA | bBA → aS | aB → bS | b

• This language can be defined by the regular expression (aa + bb)+.

• It does not have any productions of the formNx →ʎ

Page 6: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

6

Important PointFor a CFG to accept the word ʎ, it must have at least one production of this form, called a ʎ -production.

• A ʎ -production need not imply that ʎ is in the language, as with

S → aX X → ʎ

Page 7: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

7

Regular Grammar • The corresponding TG:

S → aA | bBA → aS | aB → bS | b

Page 8: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

• Regular Grammar Conversion to FA using JFLAP

• Practical Demonstration

Page 9: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

9

Elimination of null production Theorem

Statement:If L is a context-free language generated by a CFG that includes ʎ -productions then there is a different context-free grammar that has no ʎ -productions that generates either the whole language L (if L does not include the word ʎ) or else generates the language of all the words in L that are not ʎ.

Page 10: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

10

Proof (by Example)

• Consider CFG for EVENPALINDROME (the• palindromes with an even number of

letters):S → aSa I bSb I ʎ

• Following possible derivation:S => aSa => aaSaa => aabSbaa => aabbaa

Page 11: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

11

Proof (Cont’d…)• When we apply this replacement rule to

the following CFG.S → aSa I bSb I ʎ

• We remove the production S → ʎ and replace it with S → aa and S → bb,

• These are the first two productions with the right-side S deleted.

Page 12: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

12

Proof (Cont’d…)• The CFG is now:

S → aSa I bSb I aa I bb• Which also generates EVENPALINDROME,

except for the word ʎ, which can no longer be derived.

• For example, the derivation is generated in the old CFG: (Next slide)

Page 13: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

13

Regular Grammar • OLD CFG: Derivation Production

UsedS => aSa S → aSa => aaSaa S → aSa

=> aabSbaa S → bSb

=> aabbaa S → ʎ

Page 14: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

14

Regular Grammar • New CFG • We can combine the last two steps .

Derivation Production Used

S => aSa S → aSa => aaSaa S → aSa

=> aabbaa

S → bb

Derivation Production Used

S => aSa S → aSa => aaSaa S → aSa

=> aabSbaa S → bSb

=> aabbaa S → ʎ

Page 15: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

15

Null Production EliminationEXAMPLE • Consider the CFG for the language defined

by (a + b)*aS → XaX → aX I bX I ʎ

• The only nullable nonterminal here is X,

Page 16: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

16

Null Production Elimination• The productions that have right sides

including X are:Productions with Nullables

S → Xa X → aX X → bX

New Productions Formed by the Rule

S → aX → aX → b

Page 17: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

17

Null Production Elimination• The full new CFG is:

S → Xa I aX → aX | bX | a I b

• To produce the word baa we formerly used the derivation shown In the table.

Derivation Production Used

S => Xa S → Xa

=> bXa X → bX

=> baXa X → aX

=> baa X → ʎ

Page 18: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

18

Null Production Elimination• Combine the last two steps, and the new

derivation in the new CFG is:

Derivation Production Used

S => Xa S → Xa

=> bXa X → bX

=> baa X → a

Derivation Production Used

S => Xa S → Xa

=> bXa X → bX

=> baXa X → aX

=> baa X → ʎ

Page 19: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

19

Null Production Elimination• Consider this CFG for the language defined

by (a + b)*bb(a + b)*S → XYX → ZbY → bWZ → ABW → ZA → aA I bA I ʎB → Ba | Bb| ʎ

Page 20: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

20

Null Production Elimination• The modified replacement algorithm tells us

to generate new productions to replace the ʎ -productions as follows:

Old New ProductionsX → Zb X → bY → bW Y → bZ → AB Z → A and Z → BW → Z Nothing newA → aA A → aA → bA A → b B → Ba B → aB → Bb B → b

Page 21: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

21

Null Production Elimination• We do not eliminate all of the old

productions, only the old A-productions.• The fully modified new CFG is:

S → XYX → Zb | bY → bW | bZ → AB| A I BW → ZA → aA | bA | a | bB → Ba | Bb | a | b

Page 22: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

22

Eliminate Unit ProductionsDEFINITIONA production of the form

one Nonterminal → one Nonterminalis called a unit production.

See Next Slide for Details.

Page 23: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

23

Eliminate Unit Productions EXAMPLE

• ConsiderS → A IbbA → B IbB → S | a

• Separate the units from the nonunits:.

Unit Production Other ones

S → A S → bbA → B A → bB → S B → a

Page 24: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

24

Killing Unit Productions EXAMPLE

• We create the new productions that allow the first nonterminal to be replaced by any of the strings that could replace the last nonterminal in the sequence.

S → A gives S → bS → A → B gives S → aA → B gives A → aA → B → S gives A → bbB → S gives B → bbB → S → A gives B → b

Page 25: Lecture # 25 Theory Of Automata By Dr. MM Alam 1.

Lecture# 25 Summary

• Elimination of NULL Productions• Elimination of UNIT Productions• Chomsky NORMAL Form• Thanks to Daniel I.A. cohen. The material for

these slides has been taken from his book Automata Theory.