Documente Academic
Documente Profesional
Documente Cultură
Theory of Automata:08
Lecture 09:
Theory of Automata:08
Lecture 09:
Theory of Automata:08
Example
Consider the CFG:
S aa | bX |aXX
X ab |b
The total language tree is
Lecture 09:
Theory of Automata:08
Four of its words (abb, aabb, abab, aabab) have two different
derivations because they appear as terminal nodes in two different
places.
Lecture 09:
Theory of Automata:08
Example
Consider the CFG:
S aSb | bS | a
The language of this CFG is infinite, so is the
total language tree: The tree may get arbitrary
wide as well as infinitely long.
Can you draw the beginning part of this total
language tree?
Lecture 09:
Theory of Automata:08
Semi Word
For a given CFG, semiword is a string of
terminals (may be none) concatenated with with
exactly one non-terminal (on the right).
In general semiword has the shape
(terminal) (terminal).(terminal) (Non-Terminal)
e.g. aaaX
abcY
bbY
Lecture 09:
Theory of Automata:08
Regular Grammar
Given an FA, there is a CFG that generates exactly the
language accepted by the FA.
In other words, all regular languages are CFLs
CFL
Regular
Lecture 09:
Theory of Automata:08
a
x
Lecture 09:
Theory of Automata:08
Example
b
a,b
a
a
S-
S aM
S bS
M aF
M bS
F aF
F bF
F
Lecture 09:
Theory of Automata:08
Regular Grammar
Theorem 22:
If all the productions in a given CFG fit one of the
two forms: Non-terminal semiword
or Non-terminal word
(Where the word may be a or string of terminal),
then the language generated by the CFG is
Regular.
Proof:
For a CFG to be regular is by constructing a TG
from the given CFG.
10
Lecture 09:
Theory of Automata:08
Proof contd.
Let us consider a general CFG in this form
N1 w1N2
N7 w10
N1 w2N3
N18 w23
N2 w3N4
---------------
N13
Nx
+
11
Lecture 09:
Theory of Automata:08
Proof contd.
Lecture 09:
Theory of Automata:08
Example
Consider the CFG S aaS | bbS |
aa
bb
aa,bb
ab, ba
X
ab, ba
13
Lecture 09:
Theory of Automata:08
Killing -Productions
-Productions:
In a given CFG, we call a non-terminal N null able
if there is a production N , or there is a
derivation that starts at N and lead to a .
N
-Productions are undesirable.
We can replace -production with appropriate
non- productions.
14
Lecture 09:
Theory of Automata:08
Theorem 23
Lecture 09:
Theory of Automata:08
Example
Consider the CFG
S a | Xb | aYa
XY|
Yb|X
Old nullable
Production
XY
X
YX
S Xb
S aYa
New
Production
nothing
nothing
nothing
Sb
S aa
16
Lecture 09:
Theory of Automata:08
Example
Consider the CFG
S Xa
X aX | bX |
Old nullable
Production
S Xa
New
Production
Sa
X aX
Xa
X bX
Xb
17
Lecture 09:
Theory of Automata:08
Example
S XY
X Zb
Y bW
Z AB
WZ
A aA | bA |
B Ba | Bb |
18
Lecture 09:
S XY
X Zb
Y bW
Z AB
WZ
A aA | bA |
B Ba | Bb |
Theory of Automata:08
Example Contd.
Old nullable
New
Production
Production
X Zb
Xb
Y bW
Yb
Z AB
Z A and Z B
W Z Nothing new
A aA A a
A bA A b
B Ba B a
B Bb B b
Lecture 09:
Theory of Automata:08
Killing unit-productions
Definition: A production of the form
Nonterminal one Nonterminal
is called a unit production.
The following theorem allows us to get rid of unit
productions:
Theorem 24:
If there is a CFG for the language L that has no
-productions, then there is also a CFG for L with
no -productions and no unit productions.
20
Lecture 09:
Theory of Automata:08
Proof of Theorem 24
This is another proof by constructive algorithm.
Algorithm: For every pair of nonterminals A and B, if the
CFG has a unit production A B, or if there is a chain
A X1 X2 B
where X1, X2, ... are nonterminals, create new productions
as follows:
If the non-unit productions from B are
B s1 | s 2 |
where s1, s2, ... are strings, we create the productions
A s1| s2|
21
Lecture 09:
Theory of Automata:08
Example
Consider the CFG
S A| bb
AB|b
BS|a
The non-unit productions are
S bb, A b ,B a
And unit productions are
SA
AB
BS
22
Lecture 09:
Theory of Automata:08
Example contd.
Lets list all unit productions and their sequences and create new
productions:
SA
gives
Sb
S A B
gives
Sa
A B
gives
A a
A B S
gives
A bb
BS
gives
B bb
BSA
gives
Bb
Eliminating all unit productions, the new CFG is
S bb | b | a
A b | a | bb
B a | bb | b
This CFG generates a finite language since there are no
nonterminals in any strings produced from S.
23
Lecture 09:
Theory of Automata:08
Useless Symbols
S UxV w
Where U and V (V U ) and w *. A symbol that is not useful is useless
A terminal is useful if it occurs in a string of the language of G.
A variable is useful if it occurs in a derivation that begins from S and
generates a terminal string
For a variable to be useful two conditions must be satisfied.
1.
2.
Lecture 09:
Theory of Automata:08
Useless Productions
S aSb
S
SA
A aA
Useless Production
S A aA aaA aa aA
28
Lecture 09:
Theory of Automata:08
Another grammar:
SA
A aA
A
B bA
Useless Production
29
Lecture 09:
Theory of Automata:08
In general:
if
contains only
terminals
S xAy w
w L(G )
then variable
is useful
otherwise, variable
is useless
30
Lecture 09:
Theory of Automata:08
A production A x is useless
if any of its variables is useless
S aSb
S
Variables
useless
useless
useless
SA
A aA
BC
CD
Productions
useless
useless
useless
useless
31
Lecture 09:
Theory of Automata:08
S aS | A | C
Aa
B aa
C aCb
32
Lecture 09:
First:
Theory of Automata:08
S aS | A | C
Aa
B aa
C aCb
Round 1:
{ A, B}
SA
Round 2:
{ A, B, S }
33
Lecture 09:
Theory of Automata:08
{ A, B, S }
S aS | A | C
Aa
B aa
C aCb
S aS | A
Aa
B aa
Lecture 09:
Theory of Automata:08
S aS | A
Aa
B aa
B
not
reachable
35
Lecture 09:
Theory of Automata:08
S aS | A
Aa
B aa
Final Grammar
S aS | A
Aa
Lecture 09:
Theory of Automata:08
PREV = TERM
For each variable in A V do
Lecture 09:
Theory of Automata:08
Example
Consider following CFG
G:
S AC | BS | B
A aA | aF
B CF | b
C cC | D
D aD | BD | C
E aA | BSA
F bB | b
38
Lecture 09:
Theory of Automata:08
S AC | BS | B
A aA | aF
B CF | b
C cC | D
D aD | BD | C
E aA | BSA
F bB | b
Iteration
0
1
2
3
TERM
{B, F}
{B, F, A, S}
{B, F, A, S, E}
{B, F, A, S, E}
PREV
{}
{B, F}
{B, F, A, S}
{B, F, A, S, E}
39
Lecture 09:
Theory of Automata:08
Lecture 09:
Theory of Automata:08
G T:
S BS | B
A aA | aF
Bb
E aA | BSA
F bB | b
Iteration
0
1
2
3
REACH
{S}
{S, B}
{S, B}
{S, B}
PREV
{}
{S}
{S, B}
{S, B}
NEW
{S}
{B}
41
Lecture 09:
Theory of Automata:08
Removing All
42