-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDA.ts
145 lines (124 loc) · 3.74 KB
/
PDA.ts
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
142
143
144
145
type PDA = {
startState: State;
transitions: Transition[];
acceptStates: State[];
};
type Transition = {
triggeringState: State;
triggeringChar: Character;
triggeringStackHead: Character;
charToPushToStack: Character;
stateToMoveTo: State;
};
type RunState = {
state: State;
inputIndex: number;
stack: Stack;
};
type State = string;
type Input = string;
type Stack = string;
type Character = string;
const EPSILON = "";
const EMPTY_STACK_MARKER = "$";
function getTransitions(
pda: PDA,
stack: Stack,
state: State,
char: Character
): Transition[] {
return pda.transitions.filter(
({ triggeringState, triggeringChar, triggeringStackHead }) =>
triggeringState === state &&
(triggeringChar === char || triggeringChar === EPSILON) &&
(triggeringStackHead === peek(stack) || triggeringStackHead === EPSILON)
);
}
function peek(stack: Stack): Character {
return stack[stack.length - 1];
}
function pop(stack: Stack): Stack {
return stack.slice(0, stack.length - 1);
}
function simulatePDA(pda: PDA, input: Input): boolean {
const runStateQueue: RunState[] = [
{ state: pda.startState, inputIndex: 0, stack: "" },
];
while (runStateQueue.length > 0) {
const { state, inputIndex, stack } = runStateQueue.shift() as RunState;
const char = input[inputIndex];
if (inputIndex === input.length && pda.acceptStates.includes(state)) {
console.log("Input used up and now in accept state, accept input");
return true;
}
getTransitions(pda, stack, state, char).forEach(transition => {
const nextInputIndex =
transition.triggeringChar === EPSILON ? inputIndex : inputIndex + 1;
const poppedStack =
transition.triggeringStackHead === EPSILON ? stack : pop(stack);
runStateQueue.push({
state: transition.stateToMoveTo,
inputIndex: nextInputIndex,
stack: poppedStack.concat(transition.charToPushToStack),
});
});
}
console.log("No path found ending in accept state, reject input");
return false;
}
// Accepts input in form {(0^n)(1^n) | n => 0}
const examplePDA = {
startState: "q1",
acceptStates: ["q1", "q4"],
transitions: [
{
triggeringState: "q1",
triggeringChar: EPSILON,
triggeringStackHead: EPSILON,
charToPushToStack: EMPTY_STACK_MARKER,
stateToMoveTo: "q2",
},
{
triggeringState: "q2",
triggeringChar: "0",
triggeringStackHead: EPSILON,
charToPushToStack: "0",
stateToMoveTo: "q2",
},
{
triggeringState: "q2",
triggeringChar: "1",
triggeringStackHead: "0",
charToPushToStack: EPSILON,
stateToMoveTo: "q3",
},
{
triggeringState: "q3",
triggeringChar: "1",
triggeringStackHead: "0",
charToPushToStack: EPSILON,
stateToMoveTo: "q3",
},
{
triggeringState: "q3",
triggeringChar: EPSILON,
triggeringStackHead: EMPTY_STACK_MARKER,
charToPushToStack: EPSILON,
stateToMoveTo: "q4",
},
],
};
// Basic test cases
console.assert(simulatePDA(examplePDA, "0") === false);
console.assert(simulatePDA(examplePDA, "1") === false);
console.assert(simulatePDA(examplePDA, "1001") === false);
console.assert(simulatePDA(examplePDA, "1100") === false);
console.assert(simulatePDA(examplePDA, "0101") === false);
console.assert(simulatePDA(examplePDA, "1010") === false);
console.assert(simulatePDA(examplePDA, "00111") === false);
console.assert(simulatePDA(examplePDA, "00011") === false);
for (let i = 0; i < 100; i++) {
console.assert(
simulatePDA(examplePDA, "0".repeat(i).concat("1".repeat(i))) === true
);
}