0% found this document useful (0 votes)
135 views38 pages

TBO VII-chomsky Normal Form

The document discusses Chomsky normal form for context-free grammars. It defines Chomsky normal form as productions that are either of the form A → BC (variables) or A → a (terminal). It provides an example of converting a grammar to Chomsky normal form by introducing new variables. The document also discusses parsing context-free grammars by using an exhaustive search approach, noting that the time complexity is exponential in the length of the input string.

Uploaded by

Inel Hasnely
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views38 pages

TBO VII-chomsky Normal Form

The document discusses Chomsky normal form for context-free grammars. It defines Chomsky normal form as productions that are either of the form A → BC (variables) or A → a (terminal). It provides an example of converting a grammar to Chomsky normal form by introducing new variables. The document also discusses parsing context-free grammars by using an exhaustive search approach, noting that the time complexity is exponential in the length of the input string.

Uploaded by

Inel Hasnely
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

Normal Forms

for
Context-free Grammars

1
Chomsky Normal Forms
Theorema:
Apapun CFG G = (V, T, S, P) dengan
 L(G) memiliki sebuah grammar equivalen G’
= (V’, T, S, P’) yang berbentuk Chomsky normal
form.
Chomsky Normal Form

Each productions has form:

A  BC or Aa

variable variable terminal

3
Examples:

S  AS S  AS
S a S  AAS
A  SA A  SA
Ab A  aa
Chomsky Not Chomsky
Normal Form Normal Form

4
Chomsky Normal Forms
Asumsi: G tidak memiliki:
 - production
Unit-production
Langkah 1
Buat sebuah grammar G1 = (V1, T, S, P1) dari G
dengan bentuk produksi pada P :
A  x1x2x3…xn (1)

dengan xi anggota dari V dan T.


Chomsky Normal Forms

Jika n = 1, maka xi harus merupakan


terminal krn tidak ada unit production,
dan letakkan produksi ini pada P1.
Jika n  2, buat variabel baru Ta untuk
setiap a  T. Untuk setiap produksi pada
P dalam bentuk (1) maka letakkan ke P1
produksi :
A  C1C2C3…Cn
dimana Ci = xi jika xi anggota V
dan Ci = Ta jika xi = a
Chomsky Normal Forms

Untuk setiap Ta kita juga meletakkan ke P1


produksi:
Ta  a
Pada akhir langkah ini kita mendapatkan
sebuah grammar G dengan produksi
berbentuk:
Aa (2)
atau A  C1C2C3…Cn (3)

dimana Ci  Vi
Chomsky Normal Forms

Langkah 2
Meletakkan semua produksi dengan format
(2), dan juga (3) jika n  2, ke P1.

Menambahkan variabel baru V1,V2,… untuk


mengurangi panjang sisi kanan produksi
jika diperlukan
A  C1V1
V1  C2V2
….

Vn-2  Cn-1Cn
Convertion to Chomsky Normal Form

Example: S  ABa
A  aab
B  Ac

Not Chomsky
Normal Form

9
Introduce variables for terminals: Ta , Tb , Tc

S  ABTa
S  ABa A  TaTaTb
A  aab B  ATc
B  Ac Ta  a
Tb  b
Tc  c
10
Introduce intermediate variable: V1

S  AV1
S  ABTa
V1  BTa
A  TaTaTb
A  TaTaTb
B  ATc
B  ATc
Ta  a
Ta  a
Tb  b
Tb  b
Tc  c
Tc  c
11
Introduce intermediate variable: V2
S  AV1
S  AV1
V1  BTa
V1  BTa
A  TaV2
A  TaTaTb
V2  TaTb
B  ATc
B  ATc
Ta  a
Ta  a
Tb  b
Tb  b
Tc  c
Tc  c 12
Final grammar in Chomsky Normal Form:
S  AV1
V1  BTa
A  TaV2
Initial grammar
V2  TaTb
S  ABa B  ATc
A  aab Ta  a
B  Ac Tb  b
Tc  c 13
Observations

• Chomsky normal forms are good


for parsing and proving theorems

• It is very easy to find the Chomsky normal


form for any context-free grammar

14
Greinbach Normal Form

All productions have form:

A  a V1V2 Vk k 0

symbol variables

15
Observations

• Greinbach normal forms are very good


for parsing

• It is hard to find the Greinbach normal


form of any context-free grammar

16
Compilers

17
Machine Code
Program Add v,v,0
v = 5; cmp v,5
if (v>5) jmplt ELSE
x = 12 + v; THEN:
while (x !=3) { Compiler
add x, 12,v
x = x - 3; ELSE:
v = 10; WHILE:
} cmp x,3
...... ...
18
Compiler

Lexical
parser
analyzer

input output

machine
program
code
19
A parser knows the grammar
of the programming language

20
Parser
PROGRAM  STMT_LIST
STMT_LIST  STMT; STMT_LIST | STMT;
STMT  EXPR | IF_STMT | WHILE_STMT
| { STMT_LIST }

EXPR  EXPR + EXPR | EXPR - EXPR | ID


IF_STMT  if (EXPR) then STMT
| if (EXPR) then STMT else STMT
WHILE_STMT while (EXPR) do STMT

21
The parser finds the derivation
of a particular input

derivation
Parser
input E => E + E
E -> E + E
=> E + E * E
10 + 2 * 5 |E*E
=> 10 + E*E
| INT
=> 10 + 2 * E
=> 10 + 2 * 5

22
derivation tree
derivation
E

E => E + E E + E
=> E + E * E
=> 10 + E*E 10
E * E
=> 10 + 2 * E
=> 10 + 2 * 5 2 5

23
derivation tree

E machine code

E + E mult a, 2, 5
add b, 10, a
10
E * E

2 5

24
Parsing

25
Parser
input
grammar derivation
string

26
Example:

Parser
S  SS derivation
input
S  aSb
aabb ?
S  bSa
S 

27
Exhaustive Search

S  SS | aSb | bSa | 

Phase 1: S  SS Find derivation of


S  aSb aabb
S  bSa
S 
All possible derivations of length 1
28
S  SS aabb
S  aSb
S  bSa
S 

29
Phase 2 S  SS | aSb | bSa | 
S  SS  SSS
S  SS  aSbS aabb
Phase 1 S  SS  bSaS
S  SS S  SS  S
S  aSb S  aSb  aSSb
S  aSb  aaSbb
S  aSb  abSab
S  aSb  ab 30
S  SS | aSb | bSa | 
Phase 2
S  SS  SSS
S  SS  aSbS aabb
S  SS  S

S  aSb  aSSb
S  aSb  aaSbb
Phase 3
S  aSb  aaSbb  aabb
31
Final result of exhaustive search
(top-down parsing)
Parser
S  SS
input
S  aSb
aabb
S  bSa
S 
derivation

S  aSb  aaSbb  aabb


32
Time complexity of exhaustive search

Suppose there are no productions of the form

A
A B
Number of phases for string w : approx. |w|

33
For grammar with k rules

Time for phase 1: k

k possible derivations

34
Time for phase 2: k 2

k 2 possible derivations

35
Time for phase |w| is 2|w|:

A total of 2|w| possible derivations

36
Total time needed for string w:

k  k  k
2 | w|

phase 1 phase 2 phase |w|

Extremely bad!!!
37
For general context-free grammars:

There exists a parsing algorithm


that parses a string | w |
in time | w |3

The CYK parser

38

You might also like