Post on 21-Dec-2015
LING 388: Language and Computers
Sandiway Fong
Lecture 10: 9/26
2
Adminstrivia
• Reminders– No class Thursday– Homework 3 due Thursday
• Correction– Homework Exercise 2– Question 4 (1pt)
• Why is the answer different from a simple spokes* search?
– Reword as: • Why could the answer different from a simple spokes*
search?
3
Last Time
• ... into detail with Microsoft Word’s regular expression facility– Find with wildcard option enabled – somewhat limited form of regular
expression search
4
Today’s Topic
• Finite State Automata (FSA)
• equivalent to the regular expressions we’ve been studying
5
Regular Expressions: Example
.... from lecture 8
• example (sheeptalk) – ba!– baa!– baaa! …
• regular expression– baa*!– ba+!
6
Regular Expressions: Example
.... from lecture 6
• example (sheeptalk) – ba!– baa!– baaa! …
• regular expression– baa*!– ba+!
s x
z
b
!
ya
a
>
7
Regular Expressions: Example
• step-by-step• regular expression
– baa*!
s>
8
Regular Expressions: Example
• step-by-step• regular expression
– baa*!
– b
– from s, – see a ‘b’, – move to x
s xb>
9
Regular Expressions: Example
• step-by-step• regular expression
– baa*!
– ba
– from x, – see an ‘a’, – move to y
s xb ya>
10
Regular Expressions: Example
• step-by-step• regular expression
– baa*!
– baa*– ba– baa– baaa– baaaa...– from y,– see an ‘a’, – move to ?
s xb ya>
y’
y”
a
a
a...
but machine musthave a finite numberof states!
11
Regular Expressions: Example
• step-by-step• regular expression
– baa*!
– baa*– ba– baa– baaa– baaaa...– from y,– see an ‘a’, – “loop” or move back to y
s xb ya
a
>
12
Regular Expressions: Example
• step-by-step• regular expression
– baa*!
– baa*!
– from y,– see an ‘!’, – move to final state z
(indicated in red)
s x
z
b
!
ya
a
>
Note: machine cannot finish (i.e. reach the end of the input string) in states s, x or y
13
Finite State Automata (FSA)
• construction– the step-by-step FSA construction method we just
used – works for any regular expression
• conclusion– anything we can encode with a regular expression,
we can build a FSA for it
– an important step in showing that FSA and REs are equivalent
14
Microsoft Word Wildcards
• basic wildcards– ? and *
• ? any single character
• e.g. p?t put, pit, pat, pet
• * zero or more characters
x yd
abc
e
z etc.
...
...
y
a etc.
one loopfor eachcharacter
15
Microsoft Word Wildcards
• basic wildcards– @
• one or more of the preceding character
• e.g. a@
– [ ]
• range of characters• e.g. [aeiou]
x ya
a
x yo
aei
u
16
Microsoft Word Wildcards
• basic wildcards– < >
• < • beginning of a word
• can think of there being a special symbol/invisible character marking the beginning of each word
• > • end of a word
• suppose there is an invisible character marking the end of each word
x y<
see anything but ‘<‘
x y>
see anything but ‘>‘
17
Microsoft Word Wildcards
• basic wildcards– < >
• > • end of a word
– Note• the see-anything-but loop
is implicit• m>• “word that ends in m”• example:
– mom is...
x y>
see anything but ‘>‘
x ym
see anything but ‘m‘
z>
18
Finite State Automata (FSA)
• more formally– (Q,s,f,Σ,)1. set of states (Q): {s,x,y,z} 4 states must be a finite set2. start state (s): s3. end state(s) (f): z
4. alphabet (Σ): {a, b, !}5. transition function :
signature: character × state → state1. (b,s)=x2. (a,x)=y3. (a,y)=y4. (!,y)=z
s x
z
b
!
ya
a
>
19
Finite State Automata (FSA)
• in Prolog– define one predicate for each state
• taking one argument (the input string L)• consume input character• call next state with remaining input string
– query•fsa(L) :- s(L).
i.e. call start state s
20
Finite State Automata (FSA)
• state s: (start state)– s([b|L]) :- x(L).match input string beginning with b and
call state x with remainder of input
• state x:– x([a|L]) :- y(L).
• state y:– y([a|L]) :- y(L).– y(['!'|L]) :- z(L).
• state z: (end state)– z([]).
s x
z
b
!
ya
a
>
21
Finite State Automata (FSA)
• query– ?- s([b,a,a,’!’]).
s x
z
b
!
ya
a
>
Databases([b|L]) :- x(L).
x([a|L]) :- y(L).
y([a|L]) :- y(L).
y(['!'|L]) :- z(L).
z([]).• query
–?- s([b,a,b,a,’!’]).–in which state does this query fail?
[b,a,a,’!’] [a,a,’!’]
[a,’!’]
[’!’]
[]
[b,a,b,a,’!’] [a,b,a,’!’]
[b,a,’!’]
22
FSA
• Finite State Automata (FSA) have a limited amount of expressive power
• Let’s look at a modification to FSA and its effect on its power
23
String Transitions
– so far...
• all machines have had just a single character label on the arc
• so if we allow strings to label arcs– do they endow the FSA with any
more power?
b
• Answer: No– because we can always convert a
machine with string-transitions into one without
abb
a b b
24
Finite State Automata (FSA)
• equivalent
s
z
ba
!
y
a
>s x
z
b
!
ya
a
>
4 state machine
25
Finite State Automata (FSA)
• equivalent
s
z
ba
!
y
a
>s x
z
b
!
ya
a
>
Databases([b|L]) :- x(L).
x([a|L]) :- y(L).
y([a|L]) :- y(L).
y(['!'|L]) :- z(L).
z([]).
Databases([b,a|L]) :- y(L).
y([a|L]) :- y(L).
y(['!'|L]) :- z(L).
z([]).