Download presentation
Presentation is loading. Please wait.
1
LING 388 Language and Computers Lecture 7 9/23/03 Sandiway FONG
2
Administrivia Reminder Reminder Thursday Computer Lab Class Part 2…
3
Exercise 1: Finite State Automata (FSA) sx y a a b b L = {a + b + }
4
Exercise 1 From Lecture 4… From Lecture 4… A possible Prolog encoding strategy: Define one predicate for each state Name of state = name of predicateName of state = name of predicate Arity of predicate = 1Arity of predicate = 1 –taking one argument (the input string)
5
Exercise 1 Basic Idea 1: Basic Idea 1: Treat the input string as a list Consume one input character corresponding to transition arc from state, and Call next state with remaining input string Basic Idea 2: Basic Idea 2: For end state(s), match empty input i.e. we can stop only when all input has been consumed and we’re in an end state
6
Exercise 1 Machine for L = {a + b + }Machine for L = {a + b + } State s: s([a|L]) :- x(L). match input string beginning with a and call state x with remainder of input State x: x([a|L]) :- x(L). x([b|L]) :- y(L). State y: (end state) y([]). y([b|L]) :- y(L).
7
Exercise 1 Consult FSA Consult FSA Turn on tracing facility Turn on tracing facility ?- trace. Run query step-by-step Run query step-by-step ?- s([a,a,b,b]). Run query Run query ?- s([a,b,a]). Run query Run query ?- s([a,X]).
8
Exercise 1 Homework Question (A): Homework Question (A): What and how many answers does the query ?- s([X,Y,Z]). return? Homework Question (B): Homework Question (B): If the clause x([a|L]) :- x(L). is removed from the program, what language does the revised FSA accept?
9
Exercise 2: Finite State Automata (FSA) Also from Lecture 4… Also from Lecture 4… Formally: Set of states: {s,x,y} Start state: s, end state: y Alphabet: {a, b} Transition function with signature: character x state -> state (a,s)=x (a,x)=x (b,x)=y (b,y)=y
10
Exercise 2 Basic Idea: Basic Idea: Implement details of FSA as database facts Write a generic program to call database facts Note: Note: Compare implementation with Exercise 1 …where FSA details such as the layout of the FSA and program control, e.g. in the sense of what state to call next, are merged
11
Exercise 2 Database Facts: Database Facts: startState(s). endState(y). transition(s,a,x). transition(x,a,x). transition(x,b,y). transition(y,b,y).
12
Exercise 2 Generic Program: Generic Program: fsa(L) :- startState(S),fsa(S,L). fsa(S,[C|L]) :- transition(S,C,T),fsa(T,L). fsa(S,[]) :- endState(S).
13
Exercise 2 Consult FSA and turn on tracing facility Consult FSA and turn on tracing facility Re-run queries from Exercise 1: Re-run queries from Exercise 1: ?- fsa([a,a,b,b]). ?- fsa([a,b,a]). ?- fsa([a,X]).
14
Exercise 2 Homework Question (A): Homework Question (A): What happens when we run the following query? ?- fsa(L). Homework Question (B): Homework Question (B): Does the corresponding query for the program in Exercise 1 ?- s(L). do the same thing? Homework Question (C): Homework Question (C): Explain the behavior of the programs
15
Exercise 3 We’re going to implement a Finite State Transducer We’re going to implement a Finite State Transducer i.e. a FSA that not only accepts input but produces output as well Strategy: Strategy: Modify an existing FSA acceptor to take one more parameter (the output list)
16
Exercise 3: Finite State Transducer ab y : ies X : X Notation: input : output Variable denotes any character Example: convert word ending in –y to word ending in –ies
17
Exercise 3 Acceptor Program: Acceptor Program: a([y|L]) :- b(L). a([X|L]) :- a(L). b([]). Note: Note: FSA is non-deterministic
18
Exercise 3 Run queries: Run queries: ?- a([f,l,y]). ?- a([b,a,b,y]). ?- a([a,p,p,l,e]).
19
Exercise 3 Convert FSA into transducer: Convert FSA into transducer: a([y|L],M) :- b(L,M). a([X|L],[X|M]) :- a(L,M). b([],[i,e,s]). Notes: Notes: M represents the output list
20
Exercise 3 Re-do previous queries with the extra output parameter: Re-do previous queries with the extra output parameter: ?- a([f,l,y],X). ?- a([b,a,b,y],X). ?- a([a,p,p,l,e],X).
21
Exercise 3 Homework Question: Homework Question: How does the transducer handle more than one “y” in the input? Example: a([y,u,c,k,y],X). Hint: turn on tracing
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.