/* File: dfa5.pl System: SWI-Prolog 5.2.8 Author: MM Date: 10/02/2003 Title: Check if a word is accepted by the DFA DFA accepts words that start with a. Alphabet={a,b}, States={0,1,2} Favorable={1} start is state 0. Transition function is: t(0,a)=1 t(0,b)=2 t(1,a)=1 t(1,b)=1 t(2,a)=2 t(2,b)=2 ?- check_word(0,[a,b,b]). Yes ?- start. A DFA that accepts only words starting with a Input a word as a list(Ex: [a,b,b]) :[b,a,a,a]. Word accepted by DFA(Yes or No): 0b2a2a2a No ?- start. A DFA that accepts only words starting with a Input a word as a list(Ex: [a,b,b]) :[a,b,b,b]. Word accepted by DFA(Yes or No): 0a1b1b1b1 Yes *************************************************/ % This is the definition of our DFA in Prolog. t(0,a,1). t(0,b,2). t(1,a,1). t(1,b,1). t(2,a,2). t(2,b,2). start(0). % 0 is the starting state favorable(1). % 1 is a favorable state % Check if a word is accepted. check_word(S,[]):- favorable(S),write(S). check_word(S,[H|T]) :- write(S),write(H),delta(S,H,Q),check_word(Q,T). % Ask for a word and check if it is accepted (yes/no). start:- write('A DFA that accepts only words starting with a\n'), write('Input a word as a list(Ex: [a,b,b]) :'), read(W),write('Path followed by DFA: '), start(S),check_word(S,W).