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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
export function parseInstructionSet(code) {
const lines = code.split("\n");
const instructions = [];
const config = {
startState: null,
acceptStates: new Set(),
rejectStates: new Set()
};
lines.forEach((line, lineIndex) => {
const withoutComments = line.replace(/\/\/.*$/, "").trim();
if (!withoutComments) {
return;
}
if (withoutComments.startsWith("#")) {
applyDirective(withoutComments.slice(1).trim(), config, lineIndex + 1);
return;
}
const parts = withoutComments.split(/\s+/).filter(Boolean);
if (parts.length !== 5) {
throw new Error(`Invalid instruction on line ${lineIndex + 1}: expected 5 parts, received ${parts.length}`);
}
const [fromState, readSymbol, writeSymbol, direction, toState] = parts;
if (!config.startState) {
config.startState = fromState;
}
instructions.push({ fromState, readSymbol, writeSymbol, direction, toState, line: lineIndex + 1 });
});
if (!instructions.length) {
throw new Error("No instructions provided");
}
const { acceptStates, rejectStates } = deriveHaltingStates(instructions, config);
const rules = buildRuleMap(instructions);
return {
rules,
startState: config.startState ?? instructions[0].fromState,
acceptStates,
rejectStates
};
}
function applyDirective(directiveLine, config, lineNumber) {
if (!directiveLine) {
return;
}
const [keyword, ...values] = directiveLine.split(/\s+/).filter(Boolean);
if (!keyword) {
return;
}
switch (keyword.toLowerCase()) {
case "start": {
if (values.length !== 1) {
throw new Error(`#start on line ${lineNumber} must provide exactly one state`);
}
config.startState = values[0];
break;
}
case "accept":
case "accepts":
case "accepting": {
if (!values.length) {
throw new Error(`#${keyword} on line ${lineNumber} must include at least one state`);
}
values.forEach((value) => config.acceptStates.add(value));
break;
}
case "reject":
case "rejects":
case "rejecting": {
if (!values.length) {
throw new Error(`#${keyword} on line ${lineNumber} must include at least one state`);
}
values.forEach((value) => config.rejectStates.add(value));
break;
}
default:
throw new Error(`Unknown directive '#${keyword}' on line ${lineNumber}`);
}
}
function deriveHaltingStates(instructions, config) {
const fromStates = new Set();
const toStates = new Set();
const allStates = new Set();
instructions.forEach(({ fromState, toState }) => {
fromStates.add(fromState);
toStates.add(toState);
allStates.add(fromState);
allStates.add(toState);
});
// Remove any overlap so rejects always win
config.rejectStates.forEach((state) => config.acceptStates.delete(state));
if (!config.acceptStates.size) {
for (const state of allStates) {
if (!fromStates.has(state) && toStates.has(state) && !config.rejectStates.has(state)) {
config.acceptStates.add(state);
}
}
}
return {
acceptStates: Array.from(config.acceptStates),
rejectStates: Array.from(config.rejectStates)
};
}
function buildRuleMap(instructions) {
const rules = new Map();
instructions.forEach(({ fromState, readSymbol, writeSymbol, direction, toState, line }) => {
const dir = direction.toUpperCase();
if (!["L", "R", "S"].includes(dir)) {
throw new Error(`Invalid direction '${direction}' on line ${line}. Use L, R, or S.`);
}
const key = `${fromState}:${readSymbol}`;
if (rules.has(key)) {
throw new Error(`Duplicate rule for state '${fromState}' reading '${readSymbol}' (line ${line})`);
}
rules.set(key, {
nextState: toState,
writeSymbol,
direction: dir
});
});
return rules;
}
|