/* File: dfa4.pl System: SWI-Prolog 5.2.8 Author: Mihaela Malita 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 ?- check_word(0,[b,a,b,b]). No ?- start. A DFA that accepts only words starting with a Input a word as a list(Ex: [a,b,b]) :[a,b,a]. Word accepted by DFA(Yes or No): 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). % if finished wrod and state is favorable check_word(S,[H|T]) :- t(S,H,Q),check_word(Q,T). % if word not finisehd go next state % Ask for a word see if it is accepted (yes/no). start:- write('\nA DFA that accepts only words starting with a\n'), write('\nInput a word as a list(Ex: [a,b,b]) :'), read(W), write('\nWord accepted by DFA(Yes or No): '), start(S),check_word(S,W).