CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi amp Rihab Almalki Introduction Grammar G V R S a b Terminals V A ID: 915788
Download Presentation The PPT/PDF document "Normal Forms Western Michigan University" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Normal Forms
Western Michigan University
CS6800 Advanced Theory of Computation
Spring 2014
By
Abduljaleel
Alhasnawi
&
Rihab
Almalki
Slide2Introduction
Grammar: G = (V,
, R, S)
= { a, b }
Terminals
V =
{A, B, C , a ,b, c}
Terminals and Nonterminals
S
Start Symbol
R
= S → A
Rules
Slide3Regular Grammar
Rules:
X
Y
, where:X V-
Ex: S aS Y : , a, A, aBG1: R= S
aS
| ......? G2: R= S aSa | bSb | ....?
G3: R= S SS | (S) | ......?
Slide4Context 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
Slide5Ambiguity
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) |
Slide6Ambiguity
For a string
(())()
, we have more than one parse trees
Slide7Normal 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
, orX 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
-)*
Slide8Converting 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 B3- G'''
=
removeMixed(G'':CFG) A aB4- G'v =
removeLong(G''' :CFG) S ABCD5- G'v : L(G) = L(G'v) – {} Gv = atmostoneEps(
G'v
:CFG) S* S, S
Slide9Remove 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 nullableTherefore N = { A,B,C}
Slide10Remove 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
Slide11Remove 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
Slide12Remove 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
Slide13Remove 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
Slide14Remove Unit Productions
Now,
by apply
removeMixed(G''
:CFG
), G''' :S TaACTa
| TaATa | TaCTa | TaTa A a | c | TcC B c | T
cC
C TcC | c Ta a Tc c
Slide15Remove Long
Def.:
long is
a rule whose right hand side consists of more than two nonterminal symbol.
R: A BCDEBy remove long, it will be: A BM2
M2 CM3 M3 DEEx1(continue), G'' : S aACa | aA
a |
aCa | aa A a | c | cC B c | cC C
cC | c
Slide16Remove Long
Ex1(continue),
G'''
:S
T
aACTa | TaATa | TaCTa |
TaTa A a | c | TcC B c | TcC C TcC | c
Ta
a Tc c
Slide17Remove Long
Now,
by apply
removeLong(G'''
:CFG
), G'v :S TaS1 |T
aS3 |TaS4 |TaTaS1 AS2 S2 CTa
S3
ATa S4 CTa A a | c | TcC B
c | TcCC TcC | cTa aTc c
Slide18Add 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 |TaS
3 |T
aS4 |TaTa S1 AS2 S2
CTa S3 ATa S4 CTa A a | c | TcC B c | TcCC
TcC
| cTa aTc
c
Slide19Example2
Convert the
following
CFG to CNF:S
ABCA aC | DB
bB | | AC Ac | | CcD aa
Slide201-Remove epsilon
N
= {A,B,C} So
add S
AB|BC
|AC, A a, B b, C cdelete
B , C The result is: S ABC| AB|BC|ACA aC | D|aB bB | A|bC Ac | Cc| c
D
aa
Slide212- Remove units
S
ABC| AB|BC|ACA aC
| D |a (A
D, D aa) remove A D add
A aaB bB | A|b (B A, A aC|aa|a) remove B A add B aC|aa|a
C
Ac | Cc|cD aa
Slide222- Remove units
S
ABC| AB|BC|AC
A
aC | aa |a B bB | aC|
aa|a|b C Ac | Cc|cD aa
Slide233-
Remove
Mixed
S
ABC| AB|BC|ACA TaC | TaT
a |a B TbB | TaC| TaTa |a|b C ATc | CTc
|c
D TaTa
Slide244- Remove
Long
S
AM
2|M2| AC|ABM2 BCA
TaC | TaTa |a B TbB | TaC| TaTa |a
|b
C ATc | CTc|cD TaTaTa
aTb bTc c
Slide255- Add
Epsilon: N/A
S
AM
2|M2| AC|AB M2 BCA
TaC | TaTa |a B TbB | TaC| TaTa |a |b C AT
c |
CTc|cD TaTaTa aTb bT
c c
Slide26Example3
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
Slide273- remove mixedA
→ BC |
T
0T0 | BB | AB | BA C → AB, B →
T0T
0, T0 → 04- Add epsilonA
→ BC | T0T0 | BB | AB | BA | C → AB B → T0T0T0 → 0
Slide28Example4
Which
one is Chomsky Normal Form ? Why?
Slide29Not
Chomsky
Normal Form
Chomsky
Normal Form
Slide30Example5
Convert the
following
CFG to CNF:
Slide31Introduce new variables for the terminals:
Slide32Introduce new intermediate variable
to break first production:
Slide33Introduce intermediate variable:
Slide34Final grammar in Chomsky Normal Form:
Initial grammar
Slide35Example6
Convert the
following
CFG to CNF:E
E + T
E TT T F T F
F (E) F id
Slide361- Remove units
Remove
E
T , add
E T
F |FRemove E F , add E (E) | idRemove T F
, add T (E) | idThe result:E E + T | T F | (E) | id T T F | (E) | id F (E) | id
Slide372- 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)| idT( ( T) ) T+ + T* *
Slide383- Remove
Long
E
E M
2 | T M
3 | T( M4 | id T T M3 | T(M
4 | id F T( M4 | id M2 T+ TM3 T* FM4 E T) T(
( T) ) T+ + T* *
Slide395- Add
Epsilon: N/A
E
E M
2 | T M3 | T( M4 | idT T M3
| T(M4 | id F T( M4 | id M2 T+ TM3 T* FM4 E T
) T
( ( T) ) T+ + T* *
Slide40References
Elaine A.
Rich (
2008) Automata, Computability, and Complexity: Theory and Applications, Pearson Prentice
Hall.
https://cs.wmich.edu/~elise/courses/cs680/presentations.htm
Slide41