Problem with creating a deterministic finite automata (DFA) - Mercury
Posted
by
Jabba The hut
on Programmers
See other posts from Programmers
or by Jabba The hut
Published on 2012-12-15T19:36:25Z
Indexed on
2012/12/15
23:18 UTC
Read the original article
Hit count: 284
I would like to have a deterministic finite automata (DFA) simulated in Mercury. But I’m s(t)uck at several places.
Formally, a DFA is described with the following characteristics:
- a setOfStates S,
- an inputAlphabet E <-- summation symbol,
- a transitionFunction : S × E --> S,
- a startState s € S,
a setOfAcceptableFinalStates F =C S.
A DFA will always starts in the start state. Then the DFA will read all the characters on the input, one by one. Based on the current input character and the current state, there will be made to a new state. These transitions are defined in the transitions function. when the DFA is in one of his acceptable final states, after reading the last character, then will the DFA accept the input, If not, then the input will be is rejected.
The figure shows a DFA the accepting strings where the amount of zeros, is a plurality of three. Condition 1 is the initial state, and also the only acceptable state. for each input character is the corresponding arc followed to the next state.
What must be done
A type “mystate” which represents a state. Each state has a number which is used for identification.
A type “transition” that represents a possible transition between states. Each transition has a source_state, an input_character, and a final_state.
A type “statemachine” that represents the entire DFA. In the solution, the DFA must have the following properties:
- The set of all states,
- the input alphabet,
- a transition function, represented as a set of possible transitions,
- a set of accepting final states,
- a current state of the DFA
A predicate “init_machine (state machine :: out)” which unifies his arguments with the DFA, as shown as in the Figure. The current state for the DFA is set to his initial state, namely, 1. The input alphabet of the DFA is composed of the characters '0'and '1'.
A user can enter a text, which will be controlled by the DFA. the program will continues until the user types Ctrl-D and simulates an EOF. If the user use characters that are not allowed into the input alphabet of the DFA, then there will be an error message end the program will close. (pred require)
Example
Enter a sentence: 0110
String is not ok!
Enter a sentence: 011101
String is not ok!
Enter a sentence: 110100
String is ok!
Enter a sentence: 000110010
String is ok!
Enter a sentence: 011102
Uncaught exception Mercury:
Software Error: Character does not belong to the input alphabet!
the thing wat I have.
:- module dfa.
:- interface.
:- import_module io.
:- pred main(io.state::di, io.state::uo) is det.
:- implementation.
:- import_module int,string,list,bool.
1
:- type mystate ---> state(int).
2
:- type transition ---> trans(source_state::mystate, input_character::bool, final_state::mystate).
3 (error, finale_state and current_state and input_character)
:- type statemachine ---> dfa(list(mystate),list(input_character),list(transition),list(final_state),current_state(mystate))
4 missing a lot
:- pred init_machine(statemachine :: out) is det.
%init_machine(statemachine(L_Mystate,0,L_transition,L_final_state,1)) :- <-probably fault
5 not perfect
main(!IO) :-
io.write_string("\nEnter a sentence: ", !IO),
io.read_line_as_string(Input, !IO),
(
Invoer = ok(StringVar),
S1 = string.strip(StringVar),
(if S1 = "mustbeabool" then
io.write_string("Sentenceis Ok! ", !IO)
else
io.write_string("Sentence is not Ok!.", !IO)),
main(!IO)
;
Invoer = eof
;
Invoer = error(ErrorCode),
io.format("%s\n", [s(io.error_message(ErrorCode))], !IO)
).
Hope you can help me
kind regards
© Programmers or respective owner