normal forms

41
Normal Forms Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki

Upload: colleen-murphy

Post on 02-Jan-2016

30 views

Category:

Documents


2 download

DESCRIPTION

Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki. Normal Forms. Grammar: G = (V,  , R, S).  = { a, b }. Terminals. Terminals and Non terminals. V = {A , B, C , a ,b, c}. S. Start Symbol. R = S → A. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Normal Forms

Normal FormsWestern Michigan University

CS6800 Advanced Theory of ComputationSpring 2014

By Abduljaleel Alhasnawi & Rihab Almalki

Page 2: Normal Forms

Introduction» Grammar: G = (V, , R, S)

= { a, b }Terminals

V = {A, B, C , a ,b, c}Terminals and Nonterminals

SStart Symbol

R = S → ARules

Page 3: Normal Forms

Regular Grammar » Rules: X Y , where:

» X V-

» Ex: S aS

» Y : , a, A, aB

» G1: R= S aS | ......?

» G2: R= S aSa | bSb | ....?

» G3: R= S SS | (S) | ......?

Page 4: Normal Forms

Context Free Grammar (CFG)» Rules: X Y , where:

» X V- and |X| = 1

» Ex: S aS

» Y V*

» Ex: S S aSb

» A a A bBBa

» A B S SS

» A aB S ABCD

Page 5: Normal Forms

Ambiguity» A grammar G is ambiguous iff there is at least

one string in L(G) for which G produces more

than one parse tree.

» Ex:

» Bal = { w {),(}*: the parentheses are balanced}

» G = {{S,),(}, {),(}, R, S} , where:

» R = S SS | (S) |

Page 6: Normal Forms

Ambiguity Assignment#6 Abduljaleel Alhasnawi 12/4/2013

S

S S

( S ) ( S )

( S )

For a string (())() , we have more than one parse trees

Page 7: Normal Forms

Normal Forms» Chomsky Normal Form (CNF)» Def.: A context-free grammar G = (V, Σ, R, S) is said

to be in Chomsky Normal Form (CNF), iff every rule in R is of one of the following forms:

» X a where a , or» X BC where B and C V-» Greibach Normal Form (GNF)» Def.: GNF is a context free grammar G = (V, , R, S),

where all rules have one of the following forms:» X a where a and (V-)*

Page 8: Normal Forms

Converting to Chomsky Normal Form» There exists 5-steps algorithm to convert a CFG G

into a new grammar Gc such that: L(G) = L(Gc) – {} » convertChomsky(G:CFG) =» 1- G' = removeEps(G:CFG) S » 2- G'' = removeUnits(G':CFG) A B» 3- G''' = removeMixed(G'':CFG) A aB» 4- G'v = removeLong(G''' :CFG) S ABCD» 5- G'v : L(G) = L(G'v) – {} Gv = atmostoneEps(G'v:CFG) S* S, S

Page 9: Normal Forms

Remove Epsilon » Find the set N of nullable variables in G.» X is nullable iff either X or » (X A , A ) : X » Ex1: G: S aACa» A B | a» B C | c» C cC | » Now, since C , C is nullable» since B C , B is nullable» since A B , A is nullable» Therefore N = { A,B,C}

Page 10: Normal Forms

Remove Epsilon » According to N, the rules will be:» S aACa» A B | a | » B C | c | » C cC | » Now, removeEps returns G':» S aACa | aAa | aCa | aa» A B | a» B C | c» C cC | c

Page 11: Normal Forms

Remove Unit Productions » Def.: unit production is a rule whose right

hand side consists of a single nonterminal symbol. Ex: A B

» Remove any unit production from G'.» Consider the remain rules » Ex1(continue), G‘: » S aACa | aAa | aCa | aa» A B | a» B C | c» C cC | c

Page 12: Normal Forms

Remove Unit Productions » Now by apply removeUnits(G':CFG) :» Remove A B But B C | c, so Add A C | c » Remove B C Add B cC (B c, already there) » Remove A C Add A cC (A c, already there) » So removeUnits returns G'' :» S aACa | aAa | aCa | aa» A a | c | cC » B c | cC» C cC | c

Page 13: Normal Forms

Remove Mixed » Def.: mixed is a rule whose right hand side consists

of combination of terminals or terminals with nonterminal symbol.

» Create a new nonterminal Ta for each terminal a

» For each Ta , add the rule Ta a

» Ex1(continue), G'' :» S aACa | aAa | aCa | aa» A a | c | cC » B c | cC» C cC | c

Page 14: Normal Forms

Remove Unit Productions » Now, by apply removeMixed(G'':CFG), G''' :» S TaACTa | TaATa | TaCTa | TaTa

» A a | c | TcC

» B c | TcC

» C TcC | c

» Ta a

» Tc c

Page 15: Normal Forms

Remove Long » Def.: long is a rule whose right hand side consists of more

than two nonterminal symbol. » R: A BCDE» By remove long, it will be: » A BM2

» M2 CM3

» M3 DE» Ex1(continue), G'' :» S aACa | aAa | aCa | aa» A a | c | cC » B c | cC» C cC | c

Page 16: Normal Forms

Remove Long » Ex1(continue), G''' :» S TaACTa | TaATa | TaCTa | TaTa

» A a | c | TcC

» B c | TcC

» C TcC | c

» Ta a

» Tc c

Page 17: Normal Forms

Remove Long » Now, by apply removeLong(G''' :CFG), G'v :» S TaS1 |TaS3 |TaS4 |TaTa

» S1 AS2 S2 CTa S3 ATa S4 CTa

» A a | c | TcC

» B c | TcC

» C TcC | c

» Ta a

» Tc c

Page 18: Normal Forms

Add Epsilon» Finally, atmostoneEps(G'v:CFG) does not apply in

Ex1, Gv = G'v since L does not contain (S is not nullable).

» S TaS1 |TaS3 |TaS4 |TaTa

» S1 AS2 S2 CTa S3 ATa S4 CTa

» A a | c | TcC

» B c | TcC

» C TcC | c

» Ta a

» Tc c

Page 19: Normal Forms

Example2» Convert the following CFG to CNF:

» S ABC

» A aC | D

» B bB | | A

» C Ac | | Cc

» D aa

Page 20: Normal Forms

1-Remove epsilon» N = {A,B,C} So » add S AB|BC|AC, A a, B b, C c» delete B , C » The result is: » S ABC| AB|BC|AC» A aC | D|a» B bB | A|b» C Ac | Cc| c» D aa

Page 21: Normal Forms

2- Remove units» S ABC| AB|BC|AC» A aC | D |a (A D, D aa) remove A D add A aa» B bB | A|b (B A, A aC|aa|a) remove B A add B aC|aa|a» C Ac | Cc|c» D aa

Page 22: Normal Forms

2- Remove units

» S ABC| AB|BC|AC

» A aC | aa |a

» B bB | aC|aa|a|b

» C Ac | Cc|c

» D aa

Page 23: Normal Forms

3- Remove Mixed

» S ABC| AB|BC|AC

» A TaC | TaTa |a

» B TbB | TaC| TaTa |a|b

» C ATc | CTc|c

» D TaTa

Page 24: Normal Forms

4- Remove Long

» S AM2|M2| AC|AB

» M2 BC

» A TaC | TaTa |a

» B TbB | TaC| TaTa |a |b

» C ATc | CTc|c

» D TaTa

» Ta a

» Tb b

» Tc c

Page 25: Normal Forms

5- Add Epsilon: N/A

» S AM2|M2| AC|AB

» M2 BC

» A TaC | TaTa |a

» B TbB | TaC| TaTa |a |b

» C ATc | CTc|c

» D TaTa

» Ta a

» Tb b

» Tc c

Page 26: Normal Forms

Example3» Convert the following CFG to CNF:» A → BAB | B | ε » B → 00 | ε» 1- remove epsilon:» A → BAB | B | BB | AB | BA » B → 00» 2- remove units:» A → BAB | 00 | BB | AB | BA » B → 00

Page 27: Normal Forms

» 3- remove mixed

» A → BC | T0T0 | BB | AB | BA

» C → AB, B → T0T0, T0 → 0

» 4- Add epsilon

» A → BC | T0T0 | BB | AB | BA |

» C → AB

» B → T0T0

» T0 → 0

Page 28: Normal Forms

Example4» Which one is Chomsky Normal Form ? Why?

Page 29: Normal Forms

bA

SAA

aS

ASS

Not ChomskyNormal Form

aaA

SAA

AASS

ASS

Chomsky Normal Form

Page 30: Normal Forms

Example5» Convert the following CFG to CNF:

Page 31: Normal Forms

AcB

aabA

ABaS

Introduce new variables for the terminals:

cT

bT

aT

ATB

TTTA

ABTS

c

b

a

c

baa

a

cba TTT ,,

Page 32: Normal Forms

Introduce new intermediate variable to break first production:

cT

bT

aT

ATB

TTTA

ABTS

c

b

a

c

baa

a

cT

bT

aT

ATB

TTTA

BTV

AVS

c

b

a

c

baa

a

1

1

1V

Page 33: Normal Forms

Introduce intermediate variable:

cT

bT

aT

ATB

TTV

VTA

BTV

AVS

c

b

a

c

ba

a

a

2

2

1

12V

cT

bT

aT

ATB

TTTA

BTV

AVS

c

b

a

c

baa

a

1

1

Page 34: Normal Forms

Final grammar in Chomsky Normal Form:

cT

bT

aT

ATB

TTV

VTA

BTV

AVS

c

b

a

c

ba

a

a

2

2

1

1

AcB

aabA

ABaS

Initial grammar

Page 35: Normal Forms

Example6» Convert the following CFG to CNF:» E E + T » E T» T T F » T F » F (E) » F id

Page 36: Normal Forms

1- Remove units» Remove E T , add E T F |F» Remove E F , add E (E) | id» Remove T F , add T (E) | id» The result:» E E + T | T F | (E) | id » T T F | (E) | id » F (E) | id

Page 37: Normal Forms

2- Remove Mixed

» E E T+ T | T T F | T(E T)| id

» T T T F | T(E T)| id

» F T(E T)| id

» T( (

» T) )

» T+ +

» T* *

Page 38: Normal Forms

3- Remove Long» E E M2 | T M3 | T( M4 | id

» T T M3 | T(M4 | id

» F T( M4 | id

» M2 T+ T

» M3 T* F

» M4 E T)

» T( (

» T) )

» T+ +

» T* *

Page 39: Normal Forms

5- Add Epsilon: N/A

» E E M2 | T M3 | T( M4 | id

» T T M3 | T(M4 | id

» F T( M4 | id

» M2 T+ T

» M3 T* F

» M4 E T)

» T( (

» T) )

» T+ +

» T* *

Page 40: Normal Forms

References» Elaine A. Rich (2008) Automata, Computability,

and Complexity: Theory and Applications, Pearson Prentice Hall.

» https://cs.wmich.edu/~elise/courses/cs680/presentations.htm

Page 41: Normal Forms