1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
import sets, tables, strutils
import tmlib
type description = object
initial_state: state
transitions: ttable
accepting_states: HashSet[state]
type DTM* = ref object
acceptingStates*: HashSet[state]
transitions*: Table[(state, letter), transition]
configuration*: configuration
proc `[]`*(this: DTM, i: int): letter =
return this.configuration[i]
proc `[]=`*(this: DTM, i: int, l: letter) =
this.configuration[i] = l
proc `$`*(this: DTM): string =
return $this.configuration
proc make_configuration*(desc: description, input: string): configuration =
return make_configuration(desc.initial_state, input)
proc parse*(description: string): description =
## Construct a TM description from an iterator of rules from a .dtm file
var noStates = -1
var statesSeen = -1
var transitions = init_table[input, transition]()
var acceptingStates = init_set[state]()
var current_state = cast[state]("")
var alphabet = false
for line in description.split_lines:
# If the line is empty
if line.len == 0:
continue
# If we haven't seen any states yet
if no_states < 0:
no_states = line.split_white_space()[1].parseInt()
states_seen = 0
# If we're partway through seeing states
elif no_states > states_seen:
let split = line.split_white_space()
# If it's an accepting state
if split.len == 2:
acceptingStates.incl(split[0])
# If it's the first state mentioned
if statesSeen == 0:
current_state = (split[0])
statesSeen += 1
# If we haven't seen the alphabet yet
elif not alphabet:
alphabet = true
# If we're past all that and are at transitions
else:
let s = line.splitWhiteSpace()
let
ins = s[0]
inl = s[1][0]
outs = s[2]
outl = s[3][0]
outd = toDirection(s[4])
let transition = (
newState: outs,
toWrite: outl,
moveDir: outd)
transitions[(ins, inl)] = transition
# Next line
return description(
initial_state: current_state,
acceptingStates: acceptingStates,
transitions: transitions
)
proc toFile*(desc: description): string =
var states = initSet[state]()
var alphabet: set[letter] = {}
for k, v in desc.transitions:
states.incl k.state
states.incl v.newState
alphabet.incl k.letter
alphabet.incl v.toWrite
result = "states " & $states.len & "\n"
for v in states:
result &= $v & "\n"
result &= "alphabet " & $alphabet.card()
for v in alphabet:
result &= " " & $v
result &= "\n"
for k, v in desc.transitions:
result &= $k.state & " " & $k.letter
result &= " " & $v.new_state & " " & $v.to_write
result &= " " & $v.moveDir & "\n"
proc makeDTM*(desc: description, input: string): DTM =
return DTM(
configuration: make_configuration(desc, input),
accepting_states: desc.accepting_states,
transitions: desc.transitions,
)
proc test*(this: DTM, verbose: bool = true): bool =
while true:
let input = this.configuration.get_input()
if not this.transitions.has_key(input):
return false
let trans = this.transitions[input]
if trans.new_state in this.accepting_states:
return true
this.configuration.move(trans)
if verbose:
echo this.configuration
|