-
Notifications
You must be signed in to change notification settings - Fork 0
/
earleyparser.js
384 lines (343 loc) · 14.9 KB
/
earleyparser.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//ENUM for state of edge.
complete = 0
inProgress = 1
// Used as a key for the graph
function Key(dst, status){
this.dst = dst;
this.status = status;
}
Key.prototype.toString = function(){
return "(" + this.dst + ", " + this.status +")";
}
// Used to parse key and return the destination
function getDestProg(key){
var dest_prog = key.substring(1, key.length - 1).split(", ");
return dest_prog;
}
//Used to represent an edge
function Edge(src, dst, edgeProgression){
this.src = src;
this.dst = dst;
this.edgeProgression = edgeProgression;
}
//(S -> E+E, 0)
function EdgeProgression(N, RHS, pos){
this.N = N;
this.RHS = RHS;
this.pos = pos;
}
//BECAUSE JAVASCRIPT HASH CANT HOLD TUPLE OR OBJECTS
function edgeInList(edge, lst){
for (index in lst){
var otherEdge = lst[index];
if(edge.src == otherEdge.src && edge.dst == otherEdge.dst && equalProgression(edge.edgeProgression, otherEdge.edgeProgression)){
return true;
}
}
return false;
}
function equalProgression(prog1, prog2){
return prog1.N == prog2.N && prog1.RHS == prog2.RHS && prog1.pos == prog2.pos;
}
function getDprec(rhs, lstRHS){
for (index in lstRHS){
if (lstRHS[index]["rhs"] == rhs){
return lstRHS[index]["dprec"];
}
}
return -1;
}
// returns the index of the edge in the list with given properties
function getIndexOfEdge(dest, src, lhs, rhs, pos, edgeList){
for (edgeIndex in edgeList){
var edge = edgeList[edgeIndex];
var srcE = edge.src;
var destE = edge.dst;
var edgeProgression = edge.edgeProgression;
var N = edgeProgression.N;
var RHS = edgeProgression.RHS;
var posE = edgeProgression.pos;
if (src == srcE && dest == destE && lhs == N && rhs == RHS && pos == posE) {
return edgeIndex;
}
}
return -1;
}
function earleyParseInput(grammars, input, completeEdgesOnly){
//grammar represented by rhs, prec, lhs
//a dictionary of edges
var initEdgeList = [];
var nodelist = [];
var finalEdges = []; // List of edges added that this function returns at the end
//initiation
for (count = 0; count < input.length; count++){
nodelist.push(count);
initEdgeList.push([count, count + 1, input[count]]);
if (count == input.length - 1) {
nodelist.push(count+1);
}
}
// Key is key representation.
// Value is an array. 1st element is list of edges. 2nd is set of edges
var graph = {};
function edgesIncomingTo(dst,status){
var key = new Key(dst,status);
if (key in graph){ //!!! What did Jisoo say? I forgot...
return graph[key];
}else{
graph[key] = [[],[]];
return graph[key];
}
}
function isUpperCase(c){
return /^[A-Z]/.test(c);
}
// Adds edge to the graph if not in the graph already
function addEdge(e){
var src = e.src;
var dst = e.dst;
var edgeProgression = e.edgeProgression;
var N = edgeProgression.N;
var RHS = edgeProgression.RHS;
var pos = edgeProgression.pos;
var status;
if (RHS.length == pos) {
status = complete;
} else {
status = inProgress;
}
var temp = edgesIncomingTo(dst, status);
var edgeList = temp[0];
var edgeSet = temp[1];
if (!(edgeInList(e, edgeSet))){
edgeList.push(e);
edgeSet.push(e);
if(!completeEdgesOnly || (completeEdgesOnly && status === complete)){
finalEdges.push(e);
}
return true;
}
return false;
}
//seeds with all starts
var RHSes = rhsesAsList("S");
for (index in RHSes){ //iterates through every RHS that has LHS as "S" (start terminal)
var rhs = RHSes[index];
addEdge(new Edge(0,0, new EdgeProgression("S", rhs, 0)));
}
for (j = 0; j < input.length + 1; j++){// !!! Why is this not 0?
// skip in first iteration; we need to complete and predict the
// start nonterminal S before we start advancing over the input
if (j > 0){
// ADVANCE TO THE NEXT TOKEN
// for each edge (i,j-1,N -> alpha . inp[j] beta)
// add edge (i,j,N -> alpha inp[j] . beta)
var edgeList = edgesIncomingTo(j-1,inProgress)[0];
for (edgeIndex in edgeList){ //(i, _j, n, rhs, pos)
var edge = edgeList[edgeIndex];
var i = edge.src;
var _j = edge.dst;
var edgeProgression = edge.edgeProgression;
var N = edgeProgression.N;
var RHS = edgeProgression.RHS;
var pos = edgeProgression.pos;
if ((pos < RHS.length) && (RHS[pos] === input[j-1])){
addEdge(new Edge(i,j, (new EdgeProgression(N, RHS, pos+1))));
}
}
}
var edgeWasInserted = true;
while (edgeWasInserted){
edgeWasInserted = false;
// COMPLETE productions
// for each edge (i,j,N -> alpha .)
// for each edge (k,i,M -> beta . N gamma)
// add edge (k,j,M -> beta N . gamma)
var edgeList = edgesIncomingTo(j, complete)[0];
for (edgeIndex in edgeList){ //(i,_j,n,rhs,pos)
var edge = edgeList[edgeIndex];
var i = edge.src;
var _j = edge.dst;
var edgeProgression = edge.edgeProgression;
var N = edgeProgression.N;
var RHS = edgeProgression.RHS;
var pos = edgeProgression.pos;
var edgeList2 = edgesIncomingTo(i,inProgress)[0];
for (edgeIndex2 in edgeList2){ //(k,_i,m,rhs2,pos2)
var edge2 = edgeList2[edgeIndex2];
var k = edge2.src;
var _i = edge2.dst;
var edgeProgression2 = edge2.edgeProgression;
var M = edgeProgression2.N;
var RHS2 = edgeProgression2.RHS;
var pos2 = edgeProgression2.pos;
if (RHS2[pos2] == N){
var x = addEdge(new Edge(k,j, new EdgeProgression(M, RHS2, pos2+1)));
edgeWasInserted = x || edgeWasInserted;
}
}
}
// PREDICT what the parser is to see on input (move dots in edges
// that are in progress)
//
// for each edge (i,j,N -> alpha . M beta)
// for each production M -> gamma
// add edge (j,j,M -> . gamma)
var edgeList = edgesIncomingTo(j,inProgress)[0];
for (edgeIndex in edgeList){ //(i,_j,n,rhs,pos)
var edge = edgeList[edgeIndex];
var i = edge.src;
var _j = edge.dst;
var edgeProgression = edge.edgeProgression;
var N = edgeProgression.N;
var RHS = edgeProgression.RHS;
var pos = edgeProgression.pos;
// Non terminals are upper case
if (isUpperCase(RHS[pos])) {
var M = RHS[pos];
//prediction: for all rules D->alpha add edge (j,j,.alpha)
// !!! What is the format
//var RHSes = grammars[M];
var RHSes = rhsesAsList(M);
for (RHSindex in RHSes){ //iterates through the list of RHS from the every grammar with this LHS
var RHS = RHSes[RHSindex];
var x = addEdge(new Edge(j,j, new EdgeProgression(M, RHS, 0)));
edgeWasInserted = x || edgeWasInserted;
}
}
}
}
}
// Checking ambiguity and modifying "graph" according to dprec (if given)
var is_amb = false;
var ambiguousStuff = [];
for (key in graph) {
var dest = getDestProg(key)[0];
var comp = getDestProg(key)[1];
if (comp == complete) {
var edgeList = edgesIncomingTo(dest, comp)[0];
var edgeSet = edgesIncomingTo(dest, comp)[1];
for (edgeIndex in edgeList) {
var edge = edgeList[edgeIndex];
var src = edge.src;
var edgeProgression = edge.edgeProgression;
var N = edgeProgression.N;
var RHS = edgeProgression.RHS;
var pos = edgeProgression.pos;
for (edgeIndex2 in edgeSet) {
var edge2 = edgeSet[edgeIndex2];
var src2 = edge2.src;
var edgeProgression2 = edge2.edgeProgression;
var N2 = edgeProgression2.N;
var RHS2 = edgeProgression2.RHS;
var pos2 = edgeProgression2.pos;
if (getIndexOfEdge(dest, src2, N2, RHS2, pos2, finalEdges) > -1) {
if (src == src2 && N == N2 && RHS != RHS2) { // AMBIGUOUS!
var opPrecExists = false;
var dprecRHS = getDprec(RHS, grammars[N]);
var dprecRHS2 = getDprec(RHS2, grammars[N]);
var opPrecVal1;
var opPrecVal2;
var assocDir;
// supporting only binary operator for now
if (RHS.length == 3 && RHS2.length == 3) {
var op1 = RHS[1];
var op2 = RHS2[1];
var assocs = grammars["assoc"];
var opPrec1 = assocs[op1];
var opPrec2 = assocs[op2];
if (opPrec1 != undefined && opPrec2 != undefined) {
opPrecExists = true;
opPrecVal1 = opPrec1[0];
opPrecVal2 = opPrec2[0];
assocDir = opPrec1[1];
}
}
if (!opPrecExists){
if (dprecRHS == dprecRHS2) {
is_amb = true;
ambiguousStuff.push(N + "->" + RHS.join().replace(/,/g, " ") + ': ' + "(" + src + ", " + dest + ")" );
} else if (dprecRHS > dprecRHS2) {
var index = getIndexOfEdge(dest, src2, N2, RHS2, pos2, finalEdges);
if (index > -1) {
finalEdges.splice(index, 1);
}
} else if (dprecRHS < dprecRHS2) {
var index = getIndexOfEdge(dest, src, N, RHS, pos, finalEdges);
if (index > -1) {
finalEdges.splice(index, 1);
}
}
} else {
if (opPrecVal1 < opPrecVal2) {
var index = getIndexOfEdge(dest, src2, N2, RHS2, pos2, finalEdges);
if (index > -1) {
finalEdges.splice(index, 1);
}
} else if (opPrecVal1 > opPrecVal2) {
var index = getIndexOfEdge(dest, src, N, RHS, pos, finalEdges);
if (index > -1) {
finalEdges.splice(index, 1);
}
} else {
// check for associativity
var completeEdges = edgesIncomingTo(src + 2, comp)[0];
var chooseRHS1 = true;
var ix1 = getIndexOfEdge(dest, src, N, RHS, pos, edgeList);
var ix2 = getIndexOfEdge(dest, src2, N2, RHS2, pos2, edgeList);
if (ix1 < ix2) {
var index;
if (assocDir == "left") {
index = getIndexOfEdge(dest, src2, N2, RHS2, pos2, finalEdges);
} else if (assocDir = "right") {
index = getIndexOfEdge(dest, src, N, RHS, pos, finalEdges);
}
if (index > -1) {
finalEdges.splice(index, 1);
}
} else {
var index;
if (assocDir == "left") {
index = getIndexOfEdge(dest, src, N, RHS, pos, finalEdges);
} else if (assocDir = "right") {
index = getIndexOfEdge(dest, src2, N2, RHS2, pos2, finalEdges);
}
if (index > -1) {
finalEdges.splice(index, 1);
}
}
}
}
}
}
}
}
}
}
var parsable = false;
for (key in graph) {
var dest = getDestProg(key)[0];
var comp = getDestProg(key)[1];
if (comp == complete && dest == nodelist[nodelist.length-1]) {
var edgeList = edgesIncomingTo(dest, comp)[0];
for (edgeIndex in edgeList) {
var edge = edgeList[edgeIndex];
var src = edge.src;
if (src == 0) {
parsable = true;
}
}
}
}
if (!parsable) {
throw new ExecError("Input not parsable: check your grammar and white space");
}
if (is_amb) {
var ambiguousOnes = "The following grammars are causing ambiguity: \n";
for (index in ambiguousStuff) {
ambiguousOnes = ambiguousOnes + ambiguousStuff[index] + "\n";
}
alert(ambiguousOnes);
}
return {"nodes": nodelist, "initEdges": initEdgeList, "edges": finalEdges};
}