Anda di halaman 1dari 22

2014/2/3 CSC 3130 Formal Languages and Automata Theory 1

Class Discussion
Write a regular expression for the language
described by the following CFG:
S BC
B AB | c
A 011 | 1
C DC | c
D 01
2014/2/3 CSC 3130 Formal Languages and Automata Theory 2
Class Discussion
Write a CFG for each of the following
languages:
{0
i
1
i+k
0
k
| i, k > 1}
{0
i
1
j
0
k
| i, j, k > 0, j > i+k}

2014/2/3 CSC 3130 Formal Languages and Automata Theory 3
Simplification of CFG
There are 3 kinds of productions or variables
that we can remove:
Useless variables
c-productions, e.g., A c
Unit productions, e.g., A B
2014/2/3 CSC 3130 Formal Languages and Automata Theory 4
Useless Variables
A variable X is useless if:
(Type-1) X does not generate any string of
terminals, or
(Type-2) The start symbol, S, cannot
generate X.

S o X | e

where o, | e (VT)*, X e V and e e T*
* *
2014/2/3 CSC 3130 Formal Languages and Automata Theory 5
Removal of Type-1 Variables

1 Mark each production of the form:
X e where e e T*
2 Repeat
Mark X o where o consists of terminals
or variables which are on the left hand side
of some marked productions.
Until no new productions is marked.
3 Remove all unmarked productions.

2014/2/3 CSC 3130 Formal Languages and Automata Theory 6
Removal of Type-1 Variables
For example:

S ABE
A a
B c
C ED
D BC
E b
What is the grammar
after removing all the
type-1 variables?
2014/2/3 CSC 3130 Formal Languages and Automata Theory 7
Removal of Type-2 Variables

1 Mark each production of the form:
S o where o e (VT)*
2 Repeat
Mark X | where X appears on the right
hand side of some marked productions.
Until no new productions is marked.
3 Remove all unmarked productions.

2014/2/3 CSC 3130 Formal Languages and Automata Theory 8
Removal of Type-2 Variables
For example:
S AAb
S AB
A a
B b | bB
C A | B



What is the grammar
after removing all the
type-2 variables?
2014/2/3 CSC 3130 Formal Languages and Automata Theory 9
Class Discussion
Consider the following grammar:
S A A a
A BC C Ab
A aA D aD

Remove type-1 and then type-2 variables.
Remove type-2 and then type-1 variables.
Why are they different? Which is correct?
2014/2/3 CSC 3130 Formal Languages and Automata Theory 10
c-Productions
A variable A is nullable if:
A c
For example:
S ABCD
A a
B c
C ED | c
D BC
E b
*
B, C and D are nullable
2014/2/3 CSC 3130 Formal Languages and Automata Theory 11
Nullable Variables
How to find nullable variables ?
1 Mark all variables A for which there exists
a production of the form A c.
2 Repeat
Mark X for which there exists X | where
|eV* and all symbols in | have been marked.
Until no new variables is marked.
2014/2/3 CSC 3130 Formal Languages and Automata Theory 12
Removal of c-Productions
We can remove all c-productions (except
S c if S is nullable) by rewriting some
other productions.

e.g., If X
1
and X
3
are nullable, we should
replace A X
1
X
2
X
3
X
4
by:

A X
1
X
2
X
3
X
4
| X
2
X
3
X
4
| X
1
X
2
X
4
| X
2
X
4


2014/2/3 CSC 3130 Formal Languages and Automata Theory 13
Removal of c-Productions
For example, B, C and D are nullable, we can
re-write the following grammar:

S ABCD S ABCD|ABC|ABD|
ACD|AB|AC|AD|A
A a A a
B c
C ED | c C ED|E
D BC D BC|B|C
E b E b

remove
c-productions
2014/2/3 CSC 3130 Formal Languages and Automata Theory 14
Unit Productions
A production of the form A B where both A
and B are variables is called a unit production.

If there is a cycle of unit productions: A
1
A
2

A
3


A
1
, all occurrences of A
1
, A
2
, A
3
,

,
A
k
can be replaced by any one of them.

If A
1
A
2
A
3


, A
k
o, where A
1
, A
2
,
..., A
k
e V and o e (VT)*\V, we can add
productions A
1
o, A
2
o, , A
k
o and
remove all the unit productions.

2014/2/3 CSC 3130 Formal Languages and Automata Theory 15
Removel of Unit Productions
For example:
S A | B
A B | C | aB | b
B C
C B | Aa

Cycle: B C B
Chains: S A B C Aa; S A aB|b
What is the grammar
after removing the
unit productions?
2014/2/3 CSC 3130 Formal Languages and Automata Theory 16
Class Discussion
Consider the grammar:
S ABC
A aA | B
B c | C
C c | cC
Remove the c-productions and all the unit
productions.

2014/2/3 CSC 3130 Formal Languages and Automata Theory 17
Chomsky Normal Form (CNF)
A CFG is in Chomsky Normal Form if all its
productions are of the form:
A BC or
A a
where A, B, C e V and a e T. Also, S c may
be one of the productions.
2014/2/3 CSC 3130 Formal Languages and Automata Theory 18
Examples of CNF
Example 1: S AB
A BC | CC | a
B CB | b
C c
Example 2: S AB | BC | AC | c
A BC | a
B AC | b
C AB | c


2014/2/3 CSC 3130 Formal Languages and Automata Theory 19
CNF
Is that all Context Free Grammars can be expressed
in Chomsky Normal Form?

Consider the following simple grammar:
A cA | a
B ABC | b
C c
How to convert this grammar to CNF?

2014/2/3 CSC 3130 Formal Languages and Automata Theory 20
Conversion into CNF
Step 1: Convert every production into either:
A B
1
B
2
B
n
or
A a
e.g. A bCDeF becomes:
A BCDEF
B b
E e

2014/2/3 CSC 3130 Formal Languages and Automata Theory 21
Conversion into CNF
Step 2: Convert production of the form A B
1
B
2
B
n

into A C
1
C
2
:
e.g. A BCDEF becomes:
A BX
X CY
Y DZ
Z EF

2014/2/3 CSC 3130 Formal Languages and Automata Theory 22
Class Discussion
Convert the following CFG into Chomsky Normal
Form:
S c
S ABBA
B bCb
A a
C c

Anda mungkin juga menyukai