Video lecture for bca
Transcript of Video lecture for bca
cs466(Prasad) L8Norm 2
Normal Forms
Chomsky Normal Form
Griebach Normal Form
Video.edhole.com
cs466(Prasad) L8Norm 3
• Language preserving transformations• Improve parsing efficiency• Prove properties about languages and derivations
λ|SSS → λ→S
aSSS |→ aaSS |→Shorter derivations
Video.edhole.com
cs466(Prasad) L8Norm 4
Elimination of λ−rules
aaaS
abSL
bBL
bBB
aBSS
7
**)()(
*)(
|
|
⇒==
→→
λλ
Reduces the length of the derivation
aaaS
abSL
bBL
bbBB
aBSaSS
4
**)()(
)(
|
||
⇒=
+=
→→ λ
Video.edhole.com
cs466(Prasad) L8Norm 5
• Aim: Restrict the grammar such that
• Approach:– Introduce S’
)'()(
)'},'{,},'{('
),,,(
GLGL
SSSPSVG
SPVG
=→∪Σ∪=
Σ=
L(G) iff Rules ∈∈→ λλS
Video.edhole.com
cs466(Prasad) L8Norm 6
– Add rules to capture the effect of λ-rules to be deleted.
–
(Ensures non-contracting rules)
bbBB
bbBB
bBB
|
||
|
→→→
λλ
.' add then)( If λλ →∈ SGL
Video.edhole.com
cs466(Prasad) L8Norm 7
Example
λλ
|
|
aAA
bBB
ABS
→→→
aaAA
bbBB
BAABS
SS
||
||
||
'
λλ
→→→→ aaAA
bbBB
BAABS
SS
|
|
||
|'
→→→→ λ
++++ ∪∪∪
=
baba
ba
λ **
Video.edhole.com
cs466(Prasad) L8Norm 8
Determination of nullable non-terminals
AbCBcCC
CaAAACS
→→→→
|
|
λ
},,{},{}{ CASCAC
Bottom-up flow of information
Video.edhole.com
cs466(Prasad) L8Norm 9
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;
Video.edhole.com
cs466(Prasad) L8Norm 10
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.
Video.edhole.com
cs466(Prasad) L8Norm 11
cC
cbbBB
cbbBaaAA
→→→
||
||||
Elimination of Chain rules
cC
CbbBB
BaaAA
→→→
||
||
Removing renaming rules: redundant procedure calls.
Top-down flow of information
Video.edhole.com
cs466(Prasad) L8Norm 12
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;
Video.edhole.com
cs466(Prasad) L8Norm 13
Examples
bB
BaaAA
BAABS
→→→
||
||
bB
baaAA
baaAABS
→→→
||
|||
SbBB
BSaA
AbaAS
|
|
||
→→→
SabaAbBB
baAbBSaA
bBSabaAS
|||
|||
|||
→→→
Video.edhole.com
cs466(Prasad) L8Norm 14
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
Video.edhole.com
cs466(Prasad) L8Norm 15
• 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.
Video.edhole.com
cs466(Prasad) L8Norm 16
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”
Video.edhole.com
cs466(Prasad) L8Norm 17
• 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
−∈→→→
λ
Video.edhole.com
cs466(Prasad) L8Norm 18
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
→→→→→
Video.edhole.com
cs466(Prasad) L8Norm 19
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
Video.edhole.com
cs466(Prasad) L8Norm 20
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 |→
Video.edhole.com
cs466(Prasad) L8Norm 21
)|...||( | )|...||( 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.
Video.edhole.com
cs466(Prasad) L8Norm 22
bAaB
aBbA
|
|
→→
baaBbB
aBbA
|)|(
|
→→
(Cf. Gaussian Elimination)
baaBbaB
aBbA
||
|
→→
babaZZ
baaZbaaB
aBbA
|
)|(|)|(
|
→→→
Video.edhole.com
cs466(Prasad) L8Norm 23
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
−∈→→→
λ
Video.edhole.com
cs466(Prasad) L8Norm 24
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.)
Video.edhole.com
cs466(Prasad) L8Norm 25
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
→
→
→→
Video.edhole.com
cs466(Prasad) L8Norm 26
• 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”).
Video.edhole.com