25
cs466(Prasad) L8Norm 1 Normal Forms Chomsky Normal Form Griebach Normal Form

Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

Embed Size (px)

Citation preview

Page 1: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 1

Normal Forms

Chomsky Normal Form

Griebach Normal Form

Page 2: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 2

• Language preserving transformations• Improve parsing efficiency

• Prove properties about languages and derivations

|SSS S

aSSS | aaSS |

Shorter derivations

Page 3: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 3

Elimination of rules

aaaS

abSL

bBL

bBB

aBSS

7

**)()(

*)(

|

|

Reduces the length of the derivation

aaaS

abSL

bBL

bbBB

aBSaSS

4

**)()(

)(

|

||

Page 4: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 4

• Aim: Restrict the grammar such that

• Approach:– Introduce S’

)'()(

)'},'{,},'{('

),,,(

GLGL

SSSPSVG

SPVG

L(G) iff Rules S

Page 5: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 5

– Add rules to capture the effect of -rules to be deleted.

(Ensures non-contracting rules)

bbBB

bbBB

bBB

|

||

|

.' add then)( If SGL

Page 6: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 6

Example

|

|

aAA

bBB

ABS

aaAA

bbBB

BAABS

SS

||

||

||

'

aaAA

bbBB

BAABS

SS

|

|

||

|'

baba

ba

**

Page 7: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 7

Determination of nullable non-terminals

AbCBcCC

CaAAACS

|

|

},,{},{}{ CASCAC

Bottom-up flow of information

Page 8: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 8

Algorithm Nullable Nonterminals

NULL := {A | A-> P};

repeat

PREV := NULL;

foreach A V do

if there is an A-rule A->w

and w PREV*

then NULL := NULL U {A}

until NULL = PREV;

Page 9: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 9

Proof of correctness

• Soundness– If A NULL(final) then A=>* .

• Induction on the number of iterations of the loop.

• Completeness– If A=>* then A NULL(final).

• Induction on the minimal derivation of the null string from a non-terminal.

• Termination• Bounded by the number of non-terminals.

Page 10: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 10

cC

cbbBB

cbbBaaAA

||

||||

Elimination of Chainrules

cC

CbbBB

BaaAA

||

||

Removing renaming rules: redundant procedure calls.

Top-down flow of information

Page 11: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 11

Construction of Chain(A)

Chain(A) := {A}; PREV := ;

repeat

NEW := Chain(A) - PREV;

PREV := Chain(A);

foreach B NEW do

if there is a rule B->C

then Chain(A) := Chain(A) U {C}

until Chain(A) = PREV;

Page 12: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 12

Examples

bB

BaaAA

BAABS

||

||

bB

baaAA

baaAABS

||

|||

SbBB

BSaA

AbaAS

|

|

||

SabaAbBB

baAbBSaA

bBSabaAS

|||

|||

|||

Page 13: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 13

Elimination of useless symbols

• A variable is usefuluseful if it occurs in a derivation that begins with the start symbol and generates a terminal string.

• Reachable from S

• Derives terminal string*

*

where

GX

*)(

where*

Vu,v

VXuXvS G

Page 14: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 14

• Construction of the set of variables that derive terminal string.– Bottom-up flow of information

• Similar to the computation of nullable variables.

• Construction of the set of variables that are reachable– Top-down flow of information

• Similar to the computation of chained variables.

Page 15: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 15

Examples

ccCC

aaAA

ABS

|

|

|

aaAA

S

|

B does not derive terminal string;C unreachable.

S

A unreachable.

DDBD

BBDB

BDS

|

|

Empty set of

productions

“Non-termination”

Page 16: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 16

• A CFG is in Chomsky Normal Form if each rule is of the form:

• Theorem: There is an algorithm to construct a grammar G’ in CNF that is equivalent to a CFG G.

Chomsky Normal Form

}{ where SVB,C

S

aA

BCA

Page 17: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 17

Construction

• Obtain an equivalent grammar that does not contain -rules, chain rules, and useless variables.

• Apply following conversion on rules of the form: bBcCA

cW

WCRBRQ

bPPQA

Page 18: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 18

Significance of CNF

• Length of derivation of a string of length n in CNF = (2n-1)(Cf. Number of nodes of a strictly binary tree with n-leaves)

• Maximum depth of a parse tree = n

• Minimum depth of a parse tree = 1log2 n

Page 19: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 19

Removal of direct left recursion

• Causes infinite loop in top-down (depth-first) parsers.

• Approach: Generate string from left to right.

aaZZ

bbZA

|

|

aZL

baAL

)(

*)(

*)( baAL bAaA |

Page 20: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 20

)|...||( | )|...||( 2121 kj vvvuuuAA

* )|...||)(|...||( : 2121 jk uuuvvvRΕ

ZuuuuuuZ

ZvvvvvvA

jj

kk

)|...||( | )|...||(

)|...||( | )|...||(

2121

2121

Note that absence of direct left recursion does not imply absence of left recursion.

Page 21: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 21

bAaB

aBbA

|

|

baaBbB

aBbA

|)|(

|

(Cf. Gaussian Elimination)

baaBbaB

aBbA

||

|

babaZZ

baaZbaaB

aBbA

|

)|(|)|(

|

Page 22: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 22

Griebach Normal Form (* Constructs terminal prefixes that facilitates discovery of dead-ends *)

• A CFG is in Griebach Normal Form if each rule is of the form

• Theorem: There is an algorithm to construct a grammar G’ in GNF that is equivalent to a CFG G.

}{ where

...21

SVA

S

aA

AAaAA

i

n

Page 23: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 23

Analogy: solving linear simultaneous equations

62

12

3

zxz

zyy

yx

6231

62 )3(

1

3

zz

zyz

zy

yx

What are the values of x,y, and z?

4

1

2

x

y

z

(Solving for z andthen back substituiting.)

Page 24: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 24

Example: conversion to GNF

aABC

bCAB

BCA

|

|

CBA

aBBCC

bCAB

BCA

|

|

aBCbBCCAC ||

CBARCBAR

a|bCB |

RabCBC

|

)|(

Elim

inat

ing

left

recu

rsio

n Introducing terminals as first element on RHS

)|()|...|(

|||

|

|||

|

|||

CBCBRbCbCBRACR

bCaACbCBAC

aRACbcBRACA

baAbCBA

aRAbcBRAB

abCBaRbCBRC

Page 25: Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form

cs466(Prasad) L8Norm 25

• The size of the equivalent GNF can be large compared to the original grammar.

• Example CFG has 5 rules, but the corresponding GNF has 24 rules!!

• Length of the derivation in GNF = Length of the string.• GNF is useful in relating CFGs

(“generators”) to pushdown automata (“recognizers”/”acceptors”).