Sei sulla pagina 1di 9

Review

Languages and Grammars


Alphabets, strings, languages

CS 301 - Lecture 10
Chomsky and Greibach
Normal Forms
Fall 2008

Regular Languages

Deterministic Finite and Nondeterministic Automata


Equivalence of NFA and DFA
Regular Expressions
Regular Grammars
Properties of Regular Languages
Languages that are not regular and the pumping lemma

Context Free Languages

Context Free Grammars


Derivations: leftmost, rightmost and derivation trees
Parsing and ambiguity
Simplifying Context Free Grammars

Today:
More Simplifications
Normal Forms

Nullable Variables
production :

Nullable Variable:

Which Variables are Nullable?


0) Nullable Variables =
For every production
Add
to
1) For every variable
check each production
Add

to

if all

2) If step 1 added any


repeat step 1

to

Removing Nullable Variables

Final Grammar

Example Grammar:

S aMb
M aMb
M

S aMb
M aMb
M

Substitute

S aMb
S ab
M aMb
M ab

Nullable variable

Unit-Productions

Removing Unit Productions


Observation:

Unit Production:

A B

A A
Is removed immediately

(a single variable in both sides)

Example Grammar:

S aA
Aa
A B
BA
B bb

S aA | aB
Aa
B A| B
B bb

Remove

BB

S aA
Aa
A B
BA
B bb

S aA | aB
Aa
BA
B bb

Substitute

A B

S aA | aB
Aa
Substitute
BA
BA
B bb

S aA | aB
Aa
B A| B
B bb

S aA | aB | aA
Aa
B bb

Remove repeated productions

Removing All
Final grammar

S aA | aB | aA
Aa
B bb

S aA | aB
Aa
B bb

Step 1: Remove Nullable Variables


Step 2: Remove Unit-Productions
Step 3: Remove Useless Variables

Chomsky Normal Form

Normal Forms
for
Context-free Grammars

Each productions has form:

A BC
variable

or
variable

Aa
terminal

Examples:

S AS
S a
A SA
Ab
Chomsky
Normal Form

S AS
S AAS
A SA
A aa

Ta , Tb , Tc

S ABTa

B Ac

Example:

A TaTaTb
B ATc
Ta a
Tb b
Tc c

S ABa
A aab
B Ac

Not Chomsky
Normal Form

Not Chomsky
Normal Form

Introduce variables for terminals:

S ABa
A aab

Conversion to Chomsky
Normal Form

Introduce intermediate variable:

S ABTa
A TaTaTb
B ATc
Ta a
Tb b
Tc c

V1

S AV1
V1 BTa
A TaTaTb
B ATc
Ta a
Tb b
Tc c

Introduce intermediate variable:

S AV1
V1 BTa
A TaTaTb
B ATc
Ta a
Tb b
Tc c

V2

Final grammar in Chomsky Normal Form:

S AV1

S AV1

V1 BTa

V1 BTa
A TaV2
V2 TaTb
B ATc
Ta a
Tb b

Initial grammar

S ABa
A aab
B Ac

From any context-free grammar


(which doesnt produce )
not in Chomsky Normal Form
we can obtain:
An equivalent grammar
in Chomsky Normal Form

V2 TaTb
B ATc
Ta a
Tb b
Tc c

Tc c

In general:

A TaV2

The Procedure
First remove:
Nullable variables
Unit productions

Then, for every symbol

Replace any production

a:

with

Ta a

Add production

In productions: replace

New variable:

a with Ta

Ta

A C1C2 Cn

A C1V1
V1 C2V2

Vn2 Cn1Cn

New intermediate variables:

V1, V2 , ,Vn2

Observations
Theorem:

For any context-free grammar


(which doesnt produce )
there is an equivalent grammar
in Chomsky Normal Form

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

Examples:

Greibach Normal Form


All productions have form:

A a V1V2 Vk
symbol

k 0

variables

Conversion to Greibach Normal Form:

S abSb
S aa

S aTb STb
S aTa

S cAB
A aA | bB | b
Bb
Greibach
Normal Form

Theorem:

S abSb
S aa
Not Greibach
Normal Form

For any context-free grammar


(which doesnt produce )
there is an equivalent grammar
in Greibach Normal Form

Ta a
Tb b
Greibach
Normal Form

Observations
Greibach normal forms are very good
for parsing

Whats Next

Read
Linz Chapter 1,2.1, 2.2, 2.3, (skip 2.4), 3, 4, 5, 6.1, 6.2, (skip 6.3), and 7.1
JFLAP Chapter 1, 2.1, (skip 2.2), 3, 4, 5, 6.1, 7

Next Lecture Topics from Chapter 7.1

Quiz 2 in Recitation on Wednesday 10/1

Nondeterminstic Pushdown Automata


Covers Linz 2, 3, 4 and JFLAP 3, 4
Closed book, but you may bring one sheet of 8.5 x 11 inch paper with any
notes you like.

It is hard to find the Greibach normal


form of any context-free grammar

Quiz will take the full hour

Homework
Homework Due Today
New Homework Available Friday Morning
New Homework Due Next Thursday

Potrebbero piacerti anche