Normal Forms

10
Normal Forms Lecture 19 Naveen Z Quazilbash

description

Normal Forms. Lecture 19 Naveen Z Quazilbash. Overview. CNFs-Assignment Greibach Normal Forms. Assignment. Begin with the grammar: S ASB| ε AaAS|a BSbS|A|bb Are there any useless symbols? Eliminate them if so. Eliminate ε -productions. Eliminate unit productions. - PowerPoint PPT Presentation

Transcript of Normal Forms

Page 1: Normal Forms

Normal FormsLecture 19

Naveen Z Quazilbash

Page 2: Normal Forms

OverviewCNFs-AssignmentGreibach Normal Forms

Page 3: Normal Forms

AssignmentBegin with the grammar:

SASB|εAaAS|aBSbS|A|bb

1. Are there any useless symbols? Eliminate them if so.

2. Eliminate ε-productions.3. Eliminate unit productions.4. Put the grammar into Chomsky normal form.

Page 4: Normal Forms

Greibach Normal FormEach production is of the form As orAsB1B2B3…Bn

Where s is a symbol and B1, B2,…, Bn are variables.

Page 5: Normal Forms

S-grammars (from lecture 15)Simple grammars a.k.a s-grammars.Rules for s-grammars:

Productions of the form AsB1B2B3….Bn,where s is a symbol and B1B2B3….Bn are variables.

For each variable A and symbol s, there is at most one production of the form: AsB1B2B3….Bn

Page 6: Normal Forms

S-grammars (2) (from lecture 15)For example, we can not have

AaBCAaCDBecause we must only have one “Aa…”

production.

Page 7: Normal Forms

DifferenceDifference from s-grammars: for a given

variable, we may have multiple productions with the same symbol.

E.g. SaS|aTwo productions Sa…==== OK for Greibach; not OK for s-

grammars.

Page 8: Normal Forms

For every grammar that does not generate ε, there is an equivalent grammar in Greibach normal form.

Example :SaAbBAaaA|aBbB|b

SaAXBAaYA|aBbB|bXbYa

Page 9: Normal Forms

Solutiona)Only S is nullable, so we must choose, at each point

where S occurs in a body, to eliminate it or not. Since there is no body that consists only of S's, we do not have to invoke the rule about not eliminating an entire body. The resulting grammar:S -> ASB | AB A -> aAS | aA | a B -> SbS | bS | Sb | b | A | bb

b)The only unit production is B -> A. Thus, it suffices to replace this body A by the bodies of all the A-productions. The result:S -> ASB | AB A -> aAS | aA | a B -> SbS | bS | Sb | b | aAS | aA | a | bb

Page 10: Normal Forms

c)Observe that A and B each derive terminal strings, and therefore so does S. Thus, there are no useless symbols.

d)Introduce variables and productions C -> a and D -> b, and use the new variables in all bodies that are not a single terminal:S -> ASB | AB A -> CAS | CA | a B -> SDS | DS | SD | b | CAS | CA | a | DD C -> a D -> b Finally, there are bodies of length 3; one, CAS, appears twice. Introduce new variables E, F, and G to split these bodies, yielding the CNF grammar:S -> AE | AB A -> CF | CA | a B -> SG | DS | SD | b | CF | CA | a | DD C -> a D -> b E -> SB F -> AS G -> DS