-
Notifications
You must be signed in to change notification settings - Fork 0
/
npda.js
168 lines (139 loc) · 5 KB
/
npda.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/** Non-deterministic Push-Down Automaton */
class NPDA {
constructor(rules, init, ...accept) {
this.rules = rules
this.init = init
this.accept = accept
}
compute(input) {
return this._compute(
this.init, input, 0, new Stack())
? 'accepted' : 'rejected'
}
_compute(state, input, i, stack) {
if (i >= input.length &&
this.accept.includes(state)
) return true
const symbol = input[i]
const top = stack.peek()
// find all applicable rules
const rules = this.rules.filter(r =>
r.state === state && (
r.read === symbol || r.read === 'ε') && (
r.pop === top || r.pop === 'ε'))
// check the rules recursively
for (const r of rules) {
// copy the current stack
const s = new Stack(stack.store)
if (r.pop !== 'ε') s.pop()
if (r.push !== 'ε') s.push(r.push)
if (this._compute(
r.next,
input,
r.read !== 'ε' && i < input.length
? i + 1 : i,
s)
) return true
}
return false
}
}
class Rule {
constructor(state, read, pop, push, next) {
this.state = state
this.read = read
this.pop = pop
this.push = push
this.next = next
}
}
class Stack {
constructor(init = ['$']) {
this.store = [...init]
}
pop() {
return this.store.pop()
}
push(sequence) {
sequence.split('')
.reverse()
.forEach(s => this.store.push(s))
}
peek() {
return this.store[this.store.length - 1]
}
}
console.log('Rⁿrⁿ:')
const Rr = new NPDA([
new Rule('S', 'R', '$', 'X$', 'R'),
new Rule('R', 'R', 'X', 'XX', 'R'),
new Rule('R', 'r', 'X', 'ε', 'r'),
new Rule('r', 'r', 'X', 'ε', 'r'),
new Rule('r', 'ε', '$', '$', 'Rr')
], 'S', 'S', 'Rr'
)
console.log(Rr.compute('')) // accepted
console.log(Rr.compute('R')) // rejected
console.log(Rr.compute('r')) // rejected
console.log(Rr.compute('RR')) // rejected
console.log(Rr.compute('rr')) // rejected
console.log(Rr.compute('Rr')) // accepted
console.log(Rr.compute('RRr')) // rejected
console.log(Rr.compute('RRrr')) // accepted
console.log(Rr.compute('RRRrrr')) // accepted
console.log(Rr.compute('RrR')) // rejected
console.log(Rr.compute('RrRr')) // rejected
console.log(Rr.compute('rrRR')) // rejected
console.log('\nBalanced parenthesis:')
const balanced = new NPDA([
new Rule('S', '(', '$', 'X$', 'O'),
new Rule('O', '(', 'X', 'XX', 'O'),
new Rule('O', ')', 'X', 'ε', 'O'),
new Rule('O', 'ε', '$', '$', 'S')
], 'S', 'S', 'Rr'
)
console.log(balanced.compute('')) // accepted
console.log(balanced.compute('()')) // accepted
console.log(balanced.compute('(())')) // accepted
console.log(balanced.compute('((()))')) // accepted
console.log(balanced.compute('()()')) // accepted
console.log(balanced.compute('(())(())')) // accepted
console.log(balanced.compute('((())(()))')) // accepted
console.log(balanced.compute('(((())(())))')) // accepted
console.log(balanced.compute('(')) // rejected
console.log(balanced.compute(')')) // rejected
console.log(balanced.compute('())')) // rejected
console.log(balanced.compute('()(')) // rejected
console.log(balanced.compute('(()')) // rejected
console.log(balanced.compute('(()()')) // rejected
console.log(balanced.compute('()())')) // rejected
console.log('\nPalindromes:')
const pal = new NPDA([
new Rule('S', 'R', '$', 'X$', 'L'),
new Rule('S', 'r', '$', 'Y$', 'L'),
new Rule('L', 'R', 'X', 'XX', 'L'),
new Rule('L', 'R', 'Y', 'XY', 'L'),
new Rule('L', 'r', 'X', 'YX', 'L'),
new Rule('L', 'r', 'Y', 'YY', 'L'),
new Rule('L', 'ε', 'ε', 'ε', 'R'),
new Rule('L', 'R', 'ε', 'ε', 'R'),
new Rule('L', 'r', 'ε', 'ε', 'R'),
new Rule('R', 'R', 'X', 'ε', 'R'),
new Rule('R', 'r', 'Y', 'ε', 'R'),
new Rule('R', 'ε', '$', '$', 'OK')
], 'S', 'OK'
)
console.log(pal.compute('')) // rejected
console.log(pal.compute('R')) // rejected
console.log(pal.compute('r')) // rejected
console.log(pal.compute('rR')) // rejected
console.log(pal.compute('Rr')) // rejected
console.log(pal.compute('Rrr')) // rejected
console.log(pal.compute('RRrr')) // rejected
console.log(pal.compute('RR')) // accepted
console.log(pal.compute('rr')) // accepted
console.log(pal.compute('RrR')) // accepted
console.log(pal.compute('rRr')) // accepted
console.log(pal.compute('rrRrr')) // accepted
console.log(pal.compute('RrrR')) // accepted
console.log(pal.compute('RRrrRrrRR')) // accepted