-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
237 lines (209 loc) · 6.85 KB
/
parser.js
File metadata and controls
237 lines (209 loc) · 6.85 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Token class
const Token = function(label) {
this.setPattern(label, "");
}
// Token prototype
Token.prototype = {
// set the pattern
"setPattern": function(label, text) {
if (label == null) {
this.label = "";
} else {
this.label = label;
}
if (text == null) {
this.text = "";
} else {
this.text = text;
}
this.length = text.length;
},
}
// Syntax tree class
const Tree = function(label, text) {
if (label == null) {
this.label = "";
} else {
this.label = label;
}
if (text == null) {
this.text = "";
} else {
this.text = text;
}
this.children = [];
}
// State stack class
const StateStack = function() {
this._stack = [];
}
// State stack prototype
StateStack.prototype = {
// push a state pair to the stack top
"push": function(tree, state) {
const pair = { "tree": tree, "state": state };
this._stack.push(pair);
},
// pop a state pair from the stack top, remove it, and return the tree
"popTree": function() {
const last = this._stack.length - 1;
if (last < 0) {
return null;
} else {
const pair = this._stack.pop();
return pair.tree;
}
},
// peek the state number of the stack top
"peekState": function() {
const last = this._stack.length - 1;
if (last < 0) {
return 0;
} else {
return this._stack[last].state;
}
},
// get the number of the stack items
"getCount": function() {
return this._stack.length;
},
}
// Syntax parser class
const Parser = function(grammar, converter) {
// terminal symbols
const terms = grammar.terminals.concat(grammar.dummies);
this._terminals = terms.map(this._quoteSingle);
this._dummies = grammar.dummies.map(this._quoteSingle);
this._elements = terms.map(elem => new RegExp(`^(${elem})`, grammar.flag));
// production rules
this._rules = [];
const nonterms = [];
for (let i = 0; i < grammar.rules.length; i++) {
const pair = grammar.rules[i].split("=");
const symbol = pair[0];
this._rules.push({ "symbol": symbol, "count": parseInt(pair[1], 10) });
if (0 < i && nonterms.indexOf(symbol) < 0) {
// non-terminal symbols
nonterms.push(symbol);
}
}
nonterms.unshift("$");
const symbols = grammar.terminals.map(this._quoteSingle).concat(nonterms);
// parsing table
this._table = [];
for (const line of grammar.table) {
const row = {};
for (let i = 0; i < symbols.length; i++) {
const match = line[i].match(/^(s|r|g)([0-9]+)$/);
if (match) {
const symbol = match[1];
const number = parseInt(match[2], 10);
row[symbols[i]] = { "symbol": symbol, "number": number };
}
}
this._table.push(row);
}
this._converter = converter;
}
// Syntax parser prototype
Parser.prototype = {
// lexical analysis
"tokenize": function(text) {
const tokens = [];
while (0 < text.length) {
const max = new Token();
for (let i = 0; i < this._elements.length; i++) {
const result = this._elements[i].exec(text);
if (result != null && max.length < result[0].length) {
// get the longest and the first token
max.setPattern(this._terminals[i], result[0]);
}
}
if (0 < max.length) {
// found a token
if (this._dummies.indexOf(max.label) < 0) {
tokens.push(max);
}
text = text.substring(max.length);
} else {
// not found a token
break;
}
}
// get the result
if (0 < text.length) {
const valid = tokens.map(elem => elem.text).join(" ");
return { "tokens": null, "valid": valid.trim(), "invalid": text };
}
return { "tokens": tokens };
},
// syntactic analysis
"parse": function(tokens) {
const stack = new StateStack();
// dealing all tokens
tokens.push(new Token("$"));
while (0 < tokens.length) {
const next = tokens[0];
const label = next.label;
// execute an action
const action = this._table[stack.peekState()][label];
if (!action) {
break;
}
if (action.symbol == "s") {
// shift
const leaf = new Tree(label, next.text);
stack.push(leaf, action.number);
tokens.shift();
} else {
// reduce
const rule = this._rules[action.number];
let nodes = [];
for (let i = 0; i < rule.count; i++) {
const top = stack.popTree();
if (top.label.charAt(0) == "#") {
// a non-terminal symbol that should be removed
nodes = top.children.concat(nodes);
} else {
nodes.unshift(top);
}
}
// create a syntax tree
const node = new Tree(rule.symbol);
node.children = nodes;
if (this._converter[node.label]) {
this._converter[node.label](node);
}
// accept
if (action.number == 0) {
return { "tree": node.children[0] };
}
// transit
const goto = this._table[stack.peekState()][node.label];
if (!goto) {
break;
}
stack.push(node, goto.number);
}
}
// the case of not to accept
let valid = "";
while (0 < stack.getCount()) {
valid = `${this._joinTree(stack.popTree())} ${valid}`;
}
const invalid = tokens.map(elem => elem.text).join(" ");
return { "tree": null, "valid": valid.trim(), "invalid": invalid.trim() };
},
// add the single quatations
"_quoteSingle": function(cur) {
const text = cur.replace(/\\(.)/g, "$1");
return `'${text}'`;
},
// join the tree strings
"_joinTree": function(tree) {
if (tree.text != "") {
return tree.text;
}
return tree.children.map(this._joinTree, this).join(" ");
},
}