ling 388: language and computers sandiway fong lecture 11: 10/3

22
LING 388: Language and Computers Sandiway Fong Lecture 11: 10/3

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

LING 388: Language and Computers

Sandiway Fong

Lecture 11: 10/3

Adminstrivia

• Homework 2– Graded and returned

• Homework 3– you should’ve received an

acknowledgment email from me

Adminstrivia

• This Thursday– Lab class– Meet in Social Sciences 224

Last Tuesday

• Expressing regular expressions as FSA

Last Time

• Expressing regular expressions as FSA

• Microsoft Word Wildcards – @

• one or more of the preceding character• e.g. a@

– [ ]

• range of characters• e.g. [aeiou]

x ya

a

x yo

aei

u

Last Time

• Prolog FSA implementation– regular expression: ba+!

– query• ?- s([b,a,a,’!’]).

– code• one predicate per state (but

each predicate may have more than one rule)

– s([b|L]) :- x(L).

s x

z

b

!

ya

a

>

– y([a|L]) :- y(L).

– y([‘!’|L]) :- z(L).– z([]).

– x([a|L]) :- y(L).

Today’s Topic

• More on the power of FSA …

FSA

• Finite State Automata (FSA) have a limited amount of expressive power

• Let’s look at some modifications to FSA and their effects on its power

String Transitions

{covered at the end of last week’s lecture}

• 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

Empty Transitions

– so far...

• how about allowing the empty character?

– i.e. go from x to y without seeing a input character

– does this endow the FSA with any more power?

b

• Answer: No– because we can always convert a

machine with empty transitions into one without

x y

Empty Transitions

• example– (ab)|b

a

b a

b

b

Empty Transitions

• example– (ab)|(empty string)

a ba

b>

= final state

NDFSA

• Basic FSA– deterministic

• it’s clear which state we’re always in, or• deterministic = no choice point

• NDFSA– ND = non-deterministic

• i.e. we could be in more than one state• non-deterministic choice point

– example:• initially, either in state 1 or 2

s x

y

aa

b

b

1 2a

3b

>

>

NDFSA

• more generally– non-determinism can be had not just with -transitions but

with any symbol

• example:– given a, we can proceed to either state 2 or 3

1 2a

a

3b

>

NDFSA

• NDFSA– are they more powerful than FSA?– similar question asked earlier for -transitions – Answer: No– We can always convert a NDFSA into a FSA

• example– (set of states)

1 2a

a

3b

1 2,3a

3b

2,32> >

NDFSA

• example– (set of states)

• trick:– construct new

machine with states = set of possible states of the old machine

1 2a

a

3b

>

{1}>

a{1}> {2,3}

{3}ba

{1}> {2,3}

{3}ba

{1}> {2,3}

1 2,3a

3b

2,32>

Equivalence

FSANDFSA

FSA with-transitions

same expressive power

NDFSA and Prolog

• we have already seen how to encode a regular FSA in Prolog– (using one predicate per state)

s([b|L]) :- x(L).s([b|L]) :- x(L).x([a|L]) :- y(L).x([a|L]) :- y(L).y([a|L]) :- y(L).y([a|L]) :- y(L).y([‘!’|L]) :- z(L).y([‘!’|L]) :- z(L).z([]).z([]).

s([b|L]) :- x(L).s([b|L]) :- x(L).x([a|L]) :- y(L).x([a|L]) :- y(L).y([a|L]) :- y(L).y([a|L]) :- y(L).y([‘!’|L]) :- z(L).y([‘!’|L]) :- z(L).z([]).z([]).

s x

z

b

!

ya

a

>

NDFSA and Prolog

• we can do the same for NDFSA– without the set-of-states conversion to FSA

– let Prolog’s computation rule keep track of the possible states and choice points for us

s([a|L]) :- x(L).s([a|L]) :- x(L).s([a|L]) :- y(L).s([a|L]) :- y(L).x([b|L]) :- y(L).x([b|L]) :- y(L).y([]).y([]).

s([a|L]) :- x(L).s([a|L]) :- x(L).s([a|L]) :- y(L).s([a|L]) :- y(L).x([b|L]) :- y(L).x([b|L]) :- y(L).y([]).y([]).

s xa

a

yb

>

NDFSA and Prolog

• computation tree– ?- s([a]). L=[] rule 1

• ?- x([]).– No

• ?- y([]). L=[] rule 2– Yes rule 4

1.1. s([a|L]) :- x(L).s([a|L]) :- x(L).2.2. s([a|L]) :- y(L).s([a|L]) :- y(L).3.3. x([b|L]) :- y(L).x([b|L]) :- y(L).4.4. y([]).y([]).

1.1. s([a|L]) :- x(L).s([a|L]) :- x(L).2.2. s([a|L]) :- y(L).s([a|L]) :- y(L).3.3. x([b|L]) :- y(L).x([b|L]) :- y(L).4.4. y([]).y([]).

s xa

a

yb

>

2

-Transitions and Prolog

• earlier example

s([a|L]):- x(L).s([a|L]):- x(L).s(L):- x(L).s(L):- x(L).x([b|L]):- y(L).x([b|L]):- y(L).y([]).y([]).

s([a|L]):- x(L).s([a|L]):- x(L).s(L):- x(L).s(L):- x(L).x([b|L]):- y(L).x([b|L]):- y(L).y([]).y([]).

s xa

yb

>

pass on L from state s to state xunchanged

-Transitions and Prolog

• Computation tree:– ?- s([b]). rule 2

• ?- x([b]). L=[] rule 3– ?- y([]). rule 4

» Yes1.1. s([a|L]):- x(L).s([a|L]):- x(L).2.2. s(L):- x(L).s(L):- x(L).3.3. x([b|L]):- y(L).x([b|L]):- y(L).4.4. y([]).y([]).

1.1. s([a|L]):- x(L).s([a|L]):- x(L).2.2. s(L):- x(L).s(L):- x(L).3.3. x([b|L]):- y(L).x([b|L]):- y(L).4.4. y([]).y([]).

s xa

yb

>