You are on page 1of 26

Video Lectures for MBA

By:
Video.edhole.com

Normal Forms
Chomsky Normal Form
Griebach Normal Form
cs466(Prasad)
Video.edhole.com

L8Norm

Language preserving transformations


Improve parsing efficiency
Prove properties about languages and derivations

S SS |

S SS | a

S aS | a

Shorter derivations

cs466(Prasad)
Video.edhole.com

L8Norm

Elimination of -rules
Reduces the length of the derivation

S aBS |
B bB |

S aS | aBS |
B bB | b

L( B ) b *
L( S ) (ab*) *

L( B ) b
L( S ) ( ab*) *

S 7 aaa

S 4 aaa

cs466(Prasad)
Video.edhole.com

L8Norm

Aim: Restrict the grammar such that

S Rules iff L(G)


Approach:
Introduce S

G (V , , P, S )
G' (V {S ' }, , P {S ' S}, S ' )
L ( G ) L (G ' )

cs466(Prasad)
Video.edhole.com

L8Norm

Add rules to capture the effect of -rules to be


deleted.

B bB |

B bB | b |
B bB | b

If L(G) then add S ' .


(Ensures non-contracting rules)

cs466(Prasad)
Video.edhole.com

L8Norm

Example
S' S |
S AB
S AB | A | B
B bB |
B bB | b
A aA |
A aA | a
S' S
S AB | A | B
B bB | | b
A aA | | a
cs466(Prasad)
Video.edhole.com

a *b*

a b a b
L8Norm

Determination of nullable non-terminals

S AC
C cC |

A aA | C
B AbC

{C} {A, C} {S , A, C}
Bottom-up flow of information

cs466(Prasad)
Video.edhole.com

L8Norm

Algorithm Nullable Nonterminals


NULL := {A | A-> e P};
repeat
PREV := NULL;
foreach A e V do
if there is an A-rule A->w
and w e PREV*
then NULL := NULL U {A}
until NULL = PREV;
cs466(Prasad)
Video.edhole.com

L8Norm

Proof of correctness
Soundness
If A e NULL(final) then A=>* .
Induction on the number of iterations of the loop.

Completeness
If A=>* then A e NULL(final).
Induction on the minimal derivation of the null
string from a non-terminal.

Termination
Bounded by the number of non-terminals.
cs466(Prasad)
Video.edhole.com

L8Norm

10

Elimination of Chain rules


Removing renaming rules: redundant procedure calls.

A aA | a | B
B bB | b | C
Cc

A aA | a | bB | b | c
B bB | b | c
Cc
Top-down flow of information
cs466(Prasad)
Video.edhole.com

L8Norm

11

Construction of Chain(A)
Chain(A) := {A};
PREV := f;
repeat
NEW := Chain(A) - PREV;
PREV := Chain(A);
foreach B e NEW do
if there is a rule B->C
then Chain(A) := Chain(A) U {C}
until Chain(A) = PREV;
cs466(Prasad)
Video.edhole.com

L8Norm

12

Examples
S AB | A | B
A aA | a | B

S AB | aA | a | b
A aA | a | b

Bb

Bb

S aA | b | A

S aA | b | Sa | bB

A Sa | B

A Sa | bB | aA | b

B bB | S

B bB | aA | b | Sa

cs466(Prasad)
Video.edhole.com

L8Norm

13

Elimination of useless symbols


A variable is useful if it occurs in a
derivation that begins with the start symbol
and generates a terminal string.
Reachable from S

S *G uXv where X V
u,v (V ) *

Derives terminal string

X *G
where *

cs466(Prasad)
Video.edhole.com

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.
cs466(Prasad)
Video.edhole.com

L8Norm

15

Examples
S AB |
A aA | a
C cC | c

S
A aA | a

B does not derive


A unreachable.

terminal string;
C unreachable.

S BD
B BD | B

Empty set of
productions

D DB | D
Non-termination

cs466(Prasad)
Video.edhole.com

L8Norm

16

Chomsky Normal Form


A CFG is in Chomsky Normal Form if each
rule is of the form: A BC
A a
S
where B,C V - {S}

Theorem: There is an algorithm to construct


a grammar G in CNF that is equivalent to a
CFG G.
cs466(Prasad)
Video.edhole.com

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:
A bBcC

A PQ
Q BR

Pb
R WC

W c
cs466(Prasad)
Video.edhole.com

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 = log n 1
2

cs466(Prasad)
Video.edhole.com

L8Norm

19

Removal of direct left recursion


Causes infinite loop in top-down (depthfirst) parsers.
A Aa | b

L( A) ba *

Approach: Generate string from left to right.

A bZ | b
Z aZ | a
cs466(Prasad)
Video.edhole.com

L( A) ba *
L( Z ) a
L8Norm

20

A A(u1 | u2 | ... | u j ) | ( v1 | v2 | ... | vk )


R : ( v1 | v2 | ... | vk )( u1 | u2 | ... | u j ) *

A (v1 | v2 | ... | vk ) | (v1 | v2 | ... | vk ) Z


Z

(u1 | u2 | ... | u j ) | (u1 | u2 | ... | u j ) Z

Note that absence of direct left


recursion does not imply absence
of left recursion.

cs466(Prasad)
Video.edhole.com

L8Norm

21

A Bb | a
B Aa | b
(Cf. Gaussian
Elimination)

A Bb | a
B ( Bb | a )a | b

A Bb | a

A Bb | a
B Bba | aa | b

cs466(Prasad)
Video.edhole.com

B (aa | b) Z | (aa | b)
Z baZ | ba

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
A aA1 A2 ...An
Aa
S
where Ai V - {S}
Theorem: There is an algorithm to construct a
grammar G in GNF that is equivalent to a CFG
G.
cs466(Prasad)
Video.edhole.com

L8Norm

23

Analogy: solving linear simultaneous equations


x
y

y
3
2y z 1

z -x

2z 6

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

x
y

z -2
y 1

- z -1

z -( y 3) 2 z 6
z 1 - 3 2z 6

cs466(Prasad)
Video.edhole.com

L8Norm

x4

(Solving for z and


then back substituiting.)

24

Example: conversion to GNF


C (bCB | a ) R

A BC

| bCB | a

B CA | b

R ACBR | ACB

C AB | a

Introducing terminals
as first element on RHS

A B C

A BC
B CA | b
C BCB | a
C CAC B | bC B | a

C bCBR | aR | bCB | a
B bcBRA | aRA
| bCBA | aA | b
A bcBRAC | aRAC
| bCBAC | aAC | bC
R (bCBRAC | ... | bC )(CBR | CB )

cs466(Prasad)
Video.edhole.com

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).
cs466(Prasad)
Video.edhole.com

L8Norm

26

You might also like