Beruflich Dokumente
Kultur Dokumente
Discourse 03
Finite Automata
/ (CE)
xy
xy
xy
x
1
x1 x2
x1 x2
x1 x2
x1 x2
z1 (sum)
z2 (carry)
H half adder
x1
x2
x3
x1 x2
H
(old carry)
x1 x2
z1 (sum)
(x1 x2) x3
z2
(new carry)
F full adder
Table representing F
x1
x2
x3
z1
z2
z1 g1(x) x1 x2 x3
z2 g 2(x) ((x1 x2) x3) (x1 x2)
General scheme (n inputs, m outputs)
z g(x)
(z1, z2, K , zm ) g(x1, x2, K , xn)
( g 1(x1, x2, K , xn), K , g m(x1, x2, K , xn))
y(i 1)
y(i)
combinatorial
circuit
x2(0)
x1(i)
x2(i)
y1(i 1)
full
adder F
z1(i)
carry
y1(i)
(11)
q0
q0 / 0 q0 / 1 q0 / 1 q1 / 0
q1
q0 / 1 q1 / 0 q1 / 0 q1 / 1
next state/output
table
(01)/ 1
q0
(10)/ 1
(01)/ 0
(11)/ 0
(00)/ 1
q1
(11)/ 1
(10)/ 0
A 5-tuple
M (Q, , , , q 0)
Q {q 0 , q 1}
q0
where
start state
input alphabet
{(00),(01),(10),(11)}
{0,1}
output alphabet
:Q Q
(q 0,(00)) (q 0,0)
(q 0,(10)) (q 0,1)
(q 1,(01)) (q 1,0)
transition/output function
(q 0,(01)) (q 0,1)
(q 0,(11)) (q 1,0)
etc.
10
B {0,1}
z1
x1
: B
M
xn
n s
y1
M
ys
state
space =
Bs
m s
M
zm
is a
boolean
functio
n
ai
cn L c3c2c1
transducer
an L a3a2a1
Yes(1)
No(0)
recognizer (acceptor)
cn L c3c2c1
Enumerator (generator)
12
a
b
b
b
b
a
13
Ex: C Comments /* */
Filter in the lexical scanner
:
a:a
(transducer)
/:/
a:/a
/:
a:
empty
notation
in : out
/:
Recognizer
a {,/}
a:
/:
a
14
M (Q, , , q 0, F )
q2
q1
M0
a, b
15
How FA Compute
FA M (Q, , , q 0, F ) is a finite structurelike a
programfixed and static
Need to define the behavior of M on input w
Sequence of configurations
Like trace of a program on given data
Dynamic and input-dependent
(q0, a)(q1,
Since in accepting
M 0 state when input exhausted, w is
recognized by
16
www.jflap.org
JFLAP is a package of graphical tools which can be used as an aid in
learning the basic concepts of Formal Languages and Automata Theory.
Info on JFLAP
Website http://www.jflap.org/
Downloads
Tutorial
cd /usr/local/jflap
java -jar JFLAP.jar
ssh -X lectura
QuickTime and a
decompressor
are needed to see this picture.
18
(q, aw) (
,
w
, q, q Q
where
Notes: is a function, since is. (q,
Is undefined.
(q,
w
)
by M
Defn: A string w is recognized (accepted)
19
L( M)
M0
(q0, a)
20
Example:
50
q
10
45
n
q
40
15
25
30
d
n
20
35
n
d
21
Regular Expressions
A syntax for describing sets of strings (languages)
Terse
Eliminates fussy { }
Reminiscent of arithmetic expressions
Obeys some useful algebra, e.g., (E*F*)* = (E+F)*
(E*)
(some authors use )
a
for each a in
suppress (,) where possible: (a+b)*a not ( ( (a + b)* ) a )
23
expression E
(E+F)
(EF)
(E*)
language L(E)
{}
{a}
{}
L(E)L(F)
L(E)L(F)
L(E)*
24
{a, b}{
a}
({a}{
b})
{a, b}
(a+b)*a
L
(a*b*)*
=(a+b)*
(a+b)*a(a+b)*a(a+b)* {w : w has 2 a's}
{w
(b*ab*ab*ab*)*
PASCAL unsigned numbers. d={0,1,,9}
dd*( +.dd*)(+E(+ + + )dd*) {w : ?? }
a*a+b *b+a+b {w : w begins & ends same}
Defn: E=F L(E)= L(F)
*= (E*F*)*=(E+F)* E=E=
E(FG)=(EF)G E(F+G)=EF+EG E=E=E
25
Nondeterminism
Real computing devices are deterministic: the current
configuration and instruction determines the next
configuration. The relation is a function.
Why the concept of nondeterminism?
a
a,b
a,b
26
Nondeterminism (contd)
Ex: w* has penultimate symbol b: w = b?
b
a,b
a,b
a
a,b
a,b
27
d=digit
28
M (Q, , , q 0, F )
(q 0, b) {q 0, q 1}
a,b
a,b
(q 1, a) {q 1} (q 1, b) {q 1}
29
DFA vs NFA
DFA
a
q
NFA
a
a
30
(
q
,
w
)
Defn: configuration
Defn: yields in one step (or moves) relations
between configurations
(q, aw) (
(q, w) (
s
Defn: yields =
(-move)
by M
Defn: w is recognized (accepted)
31
L( M)
Ex: In NFA
M1
(q0, aabbba)
(q , aabbba)
32
Tree of Computations
Ex: NFA M1 (q 0, aabbba)
(q 0, abbba)
(q 0, bbba)
(q 1, bba)
(q 0, bba)
(q 0, ba)
(q 1, ba)
(q 0, a)
(q 1, a)
(q 0, )
null
evidence
(q 1, )
q 1 F
accepting
Computation
wL(M1)
33
a,b
(1, abab)
(2, baba)
(2, bab)
(2, aba)
(2, ab)
(2, ba)
(3, ba)
(2, a)
(2, )
(2, b)
(2, )
(3, ) 3F
accept ababa
some path to F
a
3
a,b
(3, b)
reject abab
path to F
34
(5, aa)
(2, a)
(6, a)
(3, )
(4, )
-moves
a
4
a
a
6
a
a 5
4F
Accept aaa
35
(0, aab)
(1, aab)
(0, ab)
(1, ab)
(0, b)
1
b
(1, b)
(1, )
1F
Accept aab
36
a,b
a,b
a
s
b
b
a,b
a
b
b
b
b
r
r
s
q
a
a
s F
q
s
38
q,
r}
q,
r,
q,
r,
s}
s}
abba
{p} {p, q, r, s}
abba L(N 0) since {p, q, r, s} F
39
Alphabet
Start state set {q }
0
Accepting states {X Q : X F }
Deterministic transition function : P (Q) P (Q)
Ex: For NFA N0:
p, q}, a) {p, q, s}
({
p, q}, b) {p, q, r}
({
p}, a) {p, q}
({
p}, b) {p, r}
({
K
40
41
Picture of
a
a
b
b
S Q
S Q
42
( p, w)
( p,
Step Suppose (IH) the lemma is true w. | w | k.
Let | w |
To show:
( p, ua)
43
Then ( p, u) N By (IH)
(*)
R, a). Let
By construction of M q (
R, a). Then
Q (
Using this with (*) results in:
So
44
R, a) SoQ.
(
( {p}, ua) M
By construction
(1)
and
r R. q (r, a)
(r , a) N
(2)
Since
( {p}, u)
we
have from (IH)
( p, u)
( p, ua)
So
( p, ua)
(3)
45
(s,
w
)
From the Lemma
N
f F.
f F
Q F .
for some
(s, w)
That is, (
L(M ) L(N ). W
for some
46
M 0 (P(Q), , ,{p}, F )
a
{p,q,s}
a
a
{p,q}
{p}
b
a,b
b
b
a
{p,r}
a
{p,q,r}
{p,q,r,s}
b
a
b
{p,r,s}
b
47
6
7
10
b
d
b
14
8
a
11
12
13
15
48
9
a
d
{1,2,3,4,5,6,7,8}
b
a
15
a
14
11
b
12
13
Note: still an
NFA
a
E({4,5})
E({9})
a
Etc.
E({13})
49
Verification.
(q, w) N
N1
1
b
b
b
3
51
Ex:
N1
b
3
Useful summary
E(1)
52
Ex:
N1
b
3
53
Ex:
N1
b
3
54
123
13
b
1234
55
a
a
Step: Assume (IH) the result is true of all expressions
with operator symbols (+,,*). Let E have k+1 ops.
Three cases:
Case E = (E1+E2). By IH, FA M1 , M2 with L(E1) = L(M1)
and L(E2) = L(M2). Construct the following NFA M.
56
Case +
M1
M
F1
M2
F2
F F1 F2
L(M ) L(M 1) L(M 2)
57
58
Case
F1
M1
F2
M2
L(M ) L(M 1)gL(M 2)
F F2
59
60
Case *
M
s
F F1 {s}
F1
M1
L(M ) L(M 1)
QED
61
a
b
a
Not very economical
62
Regular ExpressionsApplications
Regexp used in various development tools
qed interactive text editor. 1st version Lampson & Deutsch 1967
63
Applications (contd)
Regular expressions = patterns
meaning
matches >=1 r
matches >=0 r
matches 0 or 1 r
matches r then s
matches r or s
match literal c
match begin/end line
match any char
group exprs
character list
negated char list
awk regexp
r+
r*
r?
rs
r|s
\c
^
$
.
(s)
[abc]
[^abc]
64
Applications--Examples
-?[0-9]+
nonempty digit strings, optional sign
[^0-9]
any char except digit
\[.*\]
reference citations in a paper
g/^[ ]*$/d
delete blank lines
g/[ ]+/d
delete lines with a blank
Ex: match is always (1) leftmost and (2) longest
file: abcddddef
vi: s/d*/x/ xabcddddef
s/d+/x/ abcxef
Ex: csh: sort roll[1-5] | egrep C SC|MATH | pr
65
Applications--Examples
Ex: traditional spelling mnemonic
i before e, except after c,
or when pronounced a,
as in neighbor and weigh
--except for weird examples.
grep [^c]ei /usr/share/dict/words > foo
cat foo
abseil Aeneid ageing Alamein albeit atheist
Boeing Budweiser caffein canoeist deice deictic
dilettanteism dreidl ...
if you think this spelling rule is sufficient, you will be deficient,
inefficient, unscientific and far from omniscient
66
Applications--Examples
Ex: lex generates a lexical analyzer yylex(). Example: wordcount (wc)
%{
int nchar, nword, nline;
%}
%%
\n
{ nline++; nchar++; }
[^ \t\n]+
{ nword++, nchar += yyleng;}
// yyleng = length of matched string
.
{ nchar++;}
%%
int main(void) {
yylex();
// invoke generated lexer
printf("%d\t%d\t%d\n", nchar, nword, nline);
return 0;
}
67
1
A
1
B
1
A
A
S
a
2. Elim. C
1
0
01
00
1
0
1.
1(1+01)*00+0
4. Elim. A
3. Elim. B
(1(1+01)*00+0)*
Order: CBA
E =(1(1+01)*00+0)*
69
b
b
a
A
Add -moves:
S
b
B
b
b
a
A
70
b
B
(ACB)
ba*a
Elim. C:
bb
AC
bba*a
Elim. B:
(bb+bba*a)*b
ACB
71
b
B
b
b
a
A
72
bb
a
bb
Elim. A:
bb
CA= AC (above)
a
bba*a
Elim. B:
(bb+bba*a)*b
CAB=ACB
73
bb
a
bb
Elim. B:
(bb)*b
S
CB
a
(bb)*bb
Elim. A:
a(bb)*b
a
(bb)*b+(bb)*bba*a(bb)*b
CBA
74
Elim. B:
ab
b
A
Elim. A:
b
S
bb
C
BA =AB
a
ba*ab
Elim. C:
S
b(bb+ba*ab)*
BAC=ABC
75
b(bb+ba*ab)*
ACB
(bb+bba*a)*b
76
r = r = r
r+r=r
r =
r(s+t) = rs + rt
rt + st
(r+s)t =
* =
r*
r* = r +
(r*)* = r*
r* = + rr*
(r*s*)* = (r+s)*
77
X = a*b
Check: a[a*b] + b = aa*b + b = (aa*+)b = a*b
Ex:
X
X = aX + bY
Y = X = a*b
78
A 0A 1B
B
1B 0C
0
A
1
C 0A 1B
___ elim.B ____ B 10C
0
C
A 0A 11 0C
C 0A 110C
A 0A 110(110)0
A
[0 110(110)0]
A
[0 110(110)0]
Simplify using reg. algebra:
0 110(110)0
[ 110(11
Gauss-Jordan elimination
& back-substitution
79
b
b
a
A
A = a * aB
B = bC
Elim B:
Elim C: C = (ba * ab + bb) *
B = bC = b(ba * ab + bb) *
A = aA + aB
B=
bC
C = bA + bB
+e
B=
bC
C = ba * aB + bB + e
C = ba * abC + bbC + e
= (ba * ab + bbC
) +e
Simplifies to B =b(ba * b) *
80
Closure Properties
A class of languages is said to be closed under an operation if
applying that operation to members of the class results in a
language that is again a member of the class. Example: the
regular languages are closed under the operations of union,
concatenation and Kleene star.
Thm: The regular languages are closed under intersection
and complementation.
Pf: Complementation. Let L = L(M) where
M (Q, , , s, F)
L
(
M
)
L(M ).
So
81
L1 L2 (L1 L2).
Since the regular languages are closed under
complementation and union, the result follows.
82
( (q1, q2) , x)
f1 F1, f2 F2
( (s1, s2) , x) M
i.e., that
85
86
87
Pumping Lemma
Thm [Pumping Lemma for Regular Languages]. Suppose
that L is an infinite regular language. Then
(p)(w)[w L w p
88
89
x a1 L aj 1, y aj L ak 1, z ak L an
90
Picture:
x a1 L aj 1
r1
rk
=rj
z ak L an
rn+1
rn is
s picture,
r1 we see that there
From the
1 an accepting path
j all
the
k strings of
from
for
xy iz, to
i afinal
0 state
k p 1 it must
the form y .
. Also, since
be
so
WFurthermore,
xythat p.
.
91
Non-regular Examples
k2
(x, y, z) x a , y a (q 0), z a
p
q n
r
[(n 0) a (a ) a L.
2
0
Hence p r q (n0 1)
falls in between
W
perfect squaresa contradiction.
92
0
xy
z L1.
with
such that
Case 1. y is entirely in the as. Assume it is in the as
before the 2 bs (The other subcase is symmetric). Then
R
x ap, y aq , z ar bbas (q 0)
p r
s
p and
q r s.
a
a
bba
L
But then
where p r s.
This is a contradiction.
93
string of parentheses}
is not regular.
(
)
Pf: Suppose
B
is
regular.
Then
so
is
n n
{( ) : n 0} as is its homomorphic image
{an b n : n 0}. Contradiction. W
94
Decision Problems
Given: x
Question: Is P(x) true?
L(M 1)
L(M 2)