Beruflich Dokumente
Kultur Dokumente
o o
o o
o o
o o o
o
o
Example
Accept bit strings that contain 101 as a substring.
0
1
1
0
q
1
q
0
10
q f
q
0 1
0,1
A simple example of grammar
aabb bb aa b a
aabb
b a
n b a L
n n
> =
o o o
o
o o
follows as generated be can example, For
: grammar following by the generated be can } 0 | {
Definition of Grammar
. over string a is and symbol,
l nontermina one least at with over string a is where
form the of of set finite A
where symbols terminal of set finite A
of set finite A
of consists grammar A
T N B
T N A
B A
rules production P
T N T
symbols l nontermina N
G
C =
Definitionlanguage generated by a grammar
} | { ) (
then , , if y, Inductivel
of closure e transitiv and reflexive the :
. ) ( , all for
then , in rule production a is If
) , , , (
*
* *
*
*
*
o o o
o | | o
o o
| o
| o
o
=
=
G L
T N y x
y x y x
P
P T N G
Backus Normal Form (BNF)
| ... | | ::
as combined be may
:: ,..., :: , ::
:: as written production The
, by embraced symbols l Nontermina
2 1
2 1
n
n
| | | o
| o | o | o
| o | o
=
= = =
=
> <
ExampleA grammar for integers
<digit>::=0|1|2|3|4|5|6|7|8|9
<integer>::=<signed integer>|<unsigned integer>
<signed integer>::=+<unsigned integer>|-<unsigned
integer>
<unsigned integer>::=<digit>|<digit><unsigned integer>
-45
digit -4
digit digit -
integer unsigned digit -
integer unsigned -
integer signed integer
> <
> >< <
> >< <
> <
> >< <
Examplearithmetic expressions
< E >::=<integer>|(< E >)|< E >+< E >|< E >*< E >
67 345 * 90
integer integer * integer
E integer * integer
* integer
*
*
+
> < + > < > <
> < + > < > <
> < + > < > <
> < + > < > <
> < + > >< <
E E
E E E
E E E
Definitionsthree types of grammars
. ) ( , e wher
form the of is in rule every if is ) 2 (
} { ) ( , , ) ( , e wher
form the of is in rule every if is (1)
Then grammar. a be ) , , , ( Let
*
* *
T N u N A
u A
P ee context-f r G
T N u N A T N
u A
P nsitive context-se G
P T N G
e e
e e e
=
| o
| o | o
o
Definition continued
. if are and grammars Two
. such that grammar
regular) free, - context resp. sensitive( - context a is there if
) (resp. is language A
. , , e wher
or ,
or ,
form the of is in rule every if is (3)
L(G') L(G) equivalent G' G
L L(G) G
r ee, regula context-f r nsitive context-se L
T a N B A
A
aB A
a A
P regular G
=
=
e e
= = =
=
=
o
Theorem
For every DFA M, there is a regular grammar G such
that L(G)=Ac(M).
. under ... .....
on ...
). ( ) (
) (
}. | { } ) , ( | {
;
) , , , (
let Then ). , , , , ( Suppose : Proof
1 2 2 1 1 1
2
1
1
G q x x q x x q x
M q
x x
q
x
M Ac G L
q
x
p
A p p q x p f xq p P
I T S N
P T N G
A f S I M
n n
n
n
=
e = =
= =
=
=
o
o
o
o
Example -- From regular grammars to FA
b C bC C aC b
C N b a T
= =
, , ,
} , { } , {
o o o
o
a
o
C
b
F
b
b
M F
b
C
b
C
a b
babb babC baC b
b C bC C aC b
on
, , ,
o o
o o
o o o
b
) , (
) , (
) ( } , { ) , (
?
: possible are
sequences n transitio one than more input On
, , ,
C =
C =
e =
b F f
a F f
S P F C b C f
b
F
b
C
a b
F
b
C
b
C
a b
babb
b C bC C aC b
o o
o o
o o o
Definition: Nondeterministic Finite Automata
state initial :
states accepting :
n) transitio (state function state - next ) ( :
states of set finite :
symbols output of set finiete :
symbols input of set finite :
) , , , , (
o
o
S A
S P I S f
S
O
I
A f S I M
c
=
Language accepted by NFA
strings. of set
same accept the they if equivalent are NFA Two
. by accepted strings of set the ) Ac(
) , (
....
) , (
...
on n transitio state of
sequence a is there if by accepted is , ... For
iff by accepted is
) , , , , (
1
1 1
2
1
1
1
M M
x f
x f
A
x x x
M x x
A M
A f S I M
n n n
n
n
n
=
e
e
e
=
e
=
o o
o o
o o o
o
o o
o
o
Theorem
For every regular grammar G there is an NFA M such
that L(G)=Ac(M).
} | { } {
} | { } | { ) , (
} {
) , , , , (
) , , , ( : Pr
P q q F A
P x p F P xq p q x p f
F N S
T I
A f S I M
P T N G oof
e =
e e =
=
=
=
=
o
o
Example
NFA that accepts strings over {a,b} starting with ab or
aba.
b
a
0
q
1
q
2
q
b
a
a,b
4
q
3
q
a
Example
NFA that accepts strings over {a,b} starting with ab
but not ending with ba.
a
0
q
1
q
2
q
3
q
b
a,b
b
a
b
Equivalence between NFA and DFA
Simulate an NFA M by tracking all states M can
enter after reading an input symbol, staring from the
initial state.
Construct a DFA that carries out the simulation.
Example
a
o
C
b
F
b
b
b
a
o
C
b
} , { F C
b
b
b
C
a
a
a,b
Algorithm Constructing DFA that simulates NFA
). Ac( ) ' Ac(
A. in state some contain that ' in states of consists '
}) { , ' , ' , ' , ( '
state.] - super a is ) , ( ' [ . ) , ( ) , ( '
, every for Then ). from states reachable of subset (a
state - super a as formed is suppose y, Inductivel
.] reading after from enter can M states of set the is ) }, ({ ' [
)}). , ( | { )( , ( ) }, ({ '
let , symbol input every For
} { state - super form - - state initial the Start with
M M
S A
A f S I M
x Q f x p f x Q f
I x
S Q
x x f
x f q q x f x f
x
Q p
=
=
=
e
c
e = =
e
o
o
o o
o o o
o
*
0
*
2 1 2 1
1 2
2 1 2 1
} ,... , | ... { ;
} , | {
alphabet an :
Theorem
,*,-. ,
under closed is languages regular of class the is, That
well. as languages regular are , , ' , ' Then
languages. regular two are ' and Suppose
*
-
- L L L L L L
L L
L=Ac(M)
a
0
q
1
q
2
q
b
a,b
4
q
3
q
b
L=Ac(M)
b
a,b
a
0
p
1
p
2
p
3
p
b
a
b
a
b
a
b
b
a a
LL=Ac(M) where M is obtained by gluing the accepting state of
M with the initial state of M. Note: M is an NFA.
a
0
p
1
p
2
p
3
p
b
a
a
b
a
b
b
a
0
q
1
q
2
q
b
a,b
4
q
3
q
b
a,b
a a
a
1
p
2
p
3
p
b
a
a
b
a
b
a
0
q
1
q
b
a,b
4
q
3
q
b
a,b
a a
Alternative construction for concatenation
a
0
p
1
p
2
p
3
p
b
a
a
b
a
b
b
a
0
q
1
q
2
q
b
a,b
4
q
3
q
b
a,b
a a
||
||
||
b a,
b a,
b
a
0
p
1
p
2
p
3
p
b
a
a
b
a
b
b
a
0
q
1
q
2
q
b
a,b
4
q
3
q
b
a,b
a a
Closed under concatenation
. , ' )}, , ( ' { ) , (
. ) , ( , , }, ), , ( { ) , g(
. ) , ( , , )}, , ( { ) , g(
NFA an ) , , ' , ' , ( "
'
DFA : ' ,
) , ' , ' , ' , ( ' ), ' ( '
) , , , , ( ), (
I x S p x p f x p g
A x p f I x S p x p f x p
A x p f I x S p x p f x p
g A S S I M
S S
M M
f A S I M M Ac L
f A S I M M Ac L
e e =
e e e =
e e e =
=
C =
= =
= =
t
o
t
o
NFA with s transition -
>
>
M M
L=Ac(M), L=AC(M).
The combined machine (NFA) accepts LL.
Example
0
1
1 , 0
>
) (M Ac L =
0
1 , 0
0
) ' ( ' M Ac L =
0
1
1 , 0
0
1 , 0
0
>
>
>
M
>
>
M
>
M
) ' ( ) (
*
M Ac L M Ac L = =
Removing
s transition
p q
x
s
p q
x
x
s
ExampleDesign an NFA that accepts
10. begin with that strings of set the is where
*
L L
0
1
1 , 0
>
) (M Ac L =
0
1
1 , 0
>
0
1
1 , 0
>
) ' (
*
M Ac L =
S
A B
0
1 , 0
0
1
1 , 0
> S
A B
0
1 , 0 S
C
0
A
1
B
0
1
1 , 0
S B,
0
1 , 0
A S B , ,
1
1 , 0
S >
C
0
A
1
F
0
1
1 , 0
1 , 0
Closed under compliment
. by rejected but by accepted - in state last The
. by rejected but by accepted in state last The
input given a for
machines both on ns transitio of seq. same fn n transitio Same
. ) (
Then
) , , , , (
DFA ) , , , , ( ), (
M M A S
M M A
f
L M Ac
f A S S I M
f A S I M M Ac L
=
=
= =
o
o
Suppose M is a DFA with a unique accepting state.
Reverse all arrows of M.
Make the accepting state the initial state.
Make the initial state the accepting state.
M
A C B A
C B A C B A
Example: a language which is not regular
ion. contradict a , ) ( 1
.. .. ...
is 1 for path n transitio
.. .. .. ...
. some for
cycle. a contains ... path the
.
1
...
1
...
. for path n transitio the and , 1 0 consider and , Let
. in states # Let
. DFA some for Otherwise,
regular. not is } 0 : 1 0 {
) (
1
) (
1
2 1
1 2 1
L M Ac o
A p q q
o
A p q q q q
i j q q
q
o
q
o
q
o
m n
A q p q
o
q
o
q
o
w L w m n
M m
M Ac(M) L
n L
n i j n
n n
o
i
o
n i j n
n n
o
i j
o
i
o
j i
n
n n
n n
n n
n j n i
n j n i j i
= e
e
e =
> =
>
e
e = >
=
=
> =
o
o
o
o