-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLALR.cpp
More file actions
395 lines (314 loc) · 12 KB
/
Copy pathLALR.cpp
File metadata and controls
395 lines (314 loc) · 12 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
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
385
386
387
388
389
390
391
392
393
394
395
/***************************************************************************
LALR.cpp - description
------------------------
begin : Fri Jun 14 2002
copyright : (C) 2002 by Manuel Astudillo
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/
#include <assert.h>
#include "LALR.h"
LALR::LALR (const LALRStateTable *stateTable, const SymbolTable *symbolTable,
const RuleTable *ruleTable, integer startState) {
this->stateTable = stateTable;
this->symbolTable = symbolTable;
this->ruleTable = ruleTable;
this->startState = startState;
errorTab = NULL;
nbrBackUpTokens = 10;
}
LALR::~LALR () {
delete errorTab;
// Clean the stack in case elements are still there
// TODO: Investigate why there are elements in the stack if the parser worked nicely
while (!symbolStack.empty()) {
// delete symbolStack.top();
symbolStack.pop();
}
}
/*!
Initializes the parser.
Call this function before calling buildParseTree()
/sa buildParseTree();
*/
void LALR::init (const vector <Token*> &tokens) {
// Copy the tokens vector (maybe just having a reference to it is enough...)
this->tokens = tokens;
// Initialize stack (And clear it in case there still are elements there)
while (!symbolStack.empty()) {
//delete symbolStack.top();
symbolStack.pop();
}
// Create the start symbol (maybe it is not needed to place it in both stack and reductionList
// Maybe is enough to put it in the stack and then at the end of the parsing return the top
// of the stack, which would possible be the desired reduction
Symbol *startReduction;
startReduction = new Symbol ();
startReduction->state = startState;
symbolStack.push (startReduction);
currentState = startState;
tokenIndex = 0;
delete errorTab;
errorTab = new ErrorTable();
trim = false;
}
/*!
Parse the tokens until it reduces a rule.
/param trimReduction especifies trimming enable or disable. Trimming will
simplify rules of the form: NonTerminal1 := Nonterminal2
/return next reduction or NULL if error or test accepted
/sa getResult (), parse ()
*/
Symbol *LALR::nextReduction (bool trimReductions, bool reportOnlyOneError) {
Action *actObj;
Token *tok;
int action;
m_trimReductions = trimReductions;
for (;tokenIndex < tokens.size();tokenIndex++) {
// Save this tokens information for the error system.
tok = tokens[tokenIndex];
lastTerminal.symbol = tok->symbol;
lastTerminal.image = tok->image;
lastTerminal.line = tok->line;
lastTerminal.col = tok->col;
// Get next action
actObj = getNextAction (tokens[tokenIndex]->symbolIndex, currentState);
// Test for errors
if (actObj == NULL) {
// Generate ERROR & recover pushing expected symbol in the stack
// RECOVERING IS IN THE TODO LIST!
// FOR THAT WE NEED A MECHANISM TO "ESTIMATE" THE NEXT TOKEN
// Or use Burke-Fisher recovering algorithm
// Create a symbol traceback vector.
vector <Symbol*> traceback;
vector <Symbol*> tmptokvector = symbolStack.get_vector();
for (short k = tmptokvector.size()-1; k >= 0; k--) {
traceback.push_back (tmptokvector[k]);
}
vector <wstring> expectedTokens = getPossibleTokens (currentState);
// Add the error to the Error class.
errorTab->addError (ERROR_PARSE, UNEXPECTED_TOKEN, prevReduction, &lastTerminal,
expectedTokens, traceback,
tokens[tokenIndex]->line,
tokens[tokenIndex]->col);
if (reportOnlyOneError) {
reductionResult = REDUCTION_ERROR;
return NULL;
}
} else {
// Update Global Error recovery system
if (action == ACTION_SHIFT) {
updateBurkeFisher (createTerminal(tokens[tokenIndex]));
}
Symbol *rdc = parseToken (actObj, symbolStack, tokenIndex, currentState);
if (rdc == NULL) {
if (reductionResult != REDUCTION_TOKEN_SHIFT) {
return NULL;
}
} else {
return rdc;
}
}
}
reductionResult = REDUCTION_ERROR;
return NULL;
}
/*!
Computes an Action object from the input parameters
/param symbolIndex the index in the symbol table that we want to match
/param index the current state in the LALR state machine
/return Action NULL if no action found for this symbol Index.
*/
Action *LALR::getNextAction (integer symbolIndex, integer index) {
for (integer i=0; i < stateTable->states[index].actions.size(); i++) {
if (stateTable->states[index].actions[i].symbolIndex == symbolIndex) {
return &stateTable->states[index].actions[i];
}
}
return NULL;
}
vector<wstring> LALR::getPossibleTokens (integer index) {
vector<wstring> tokenVector;
for (integer i=0; i < stateTable->states[index].actions.size(); i++) {
integer j = stateTable->states[index].actions[i].symbolIndex;
if (symbolTable->symbols[j].kind == TERMINAL) {
wstring tokenName = symbolTable->symbols[j].name;
tokenVector.push_back (tokenName);
}
}
return tokenVector;
}
/*!
Builds a parse tree with reductions as nodes.
Sets the Error object with the possible compiling errors.
/sa getError(), getNextReduction()
*/
Symbol *LALR::parse (const vector <Token*> &tokens, bool trimReductions,
bool reportOnlyOneError) {
init (tokens);
Symbol *reduction;
prevReduction = NULL;
while (true) {
reduction = nextReduction(trimReductions, reportOnlyOneError);
if ((reduction == NULL) && ((getResult() == REDUCTION_ERROR) ||
(getResult() == REDUCTION_TEXT_ACCEPTED))) {
break;
} else if (reduction) {
prevReduction = reduction;
}
}
if (getResult() == REDUCTION_TEXT_ACCEPTED) {
return prevReduction;
} else {
return NULL;
}
}
int LALR::getResult () {
return reductionResult;
}
ErrorTable *LALR::getErrors() {
return errorTab;
}
void LALR::printReductionTree (Symbol *reduction, int deep) {
integer i;
// print tabs
for (i=0; i < deep; i++) {
wprintf (L" ");
}
if (reduction == NULL) {
wprintf (L"NULL\n");
return;
}
if (reduction->type == NON_TERMINAL) {
wprintf (symbolTable->symbols[reduction->symbolIndex].name.c_str());
wprintf (L"\n");
for (i=0; i < ((NonTerminal*) reduction)->children.size(); i++) {
printReductionTree (((NonTerminal*)reduction)->children[i], deep+1);
}
} else {
wprintf (((Terminal*)reduction)->symbol.c_str());
wprintf (L":");
wprintf (((Terminal*)reduction)->image.c_str());
wprintf (L"\n");
}
}
void LALR::updateBurkeFisher (Symbol *symbol) {
if (errorQueue.size() == nbrBackUpTokens) {
errorStack.push (errorQueue.front());
errorQueue.pop();
}
errorQueue.push (symbol);
}
Symbol *LALR::parseToken (Action *actObj, SymbolStack &theStack, int tokenIndex,
integer ¤tState) {
NonTerminal *newNonTerminal;
Terminal *newTerminal;
integer index, i;
int action, target;
action = actObj->action;
target = actObj->target;
switch (action) {
/*
Pushes current token into the stack
*/
case ACTION_SHIFT:
// Push current token on the stack
currentState = target;
currentLine = tokens[tokenIndex]->line;
currentCol = tokens[tokenIndex]->col;
tokens[tokenIndex]->state = target;
// Create a terminal symbol and push it onto the stack
newTerminal = createTerminal (tokens[tokenIndex]);
theStack.push (newTerminal);
reductionResult = REDUCTION_TOKEN_SHIFT;
return NULL;
break;
/*
Creates a new reduction. Pops all the terminals and non terminals
for this rule and pushes the most left non terminal.
*/
case ACTION_REDUCE:
// Create a new Non Terminal (to represent this reduction)
index = ruleTable->rules[target].symbolIndex;
newNonTerminal = createNonTerminal (index, target);
// If the rule has only a nonterminal then we dont create a reduction
// node for this rule in the tree since its not usefull.
// User can decide to simplify this by enabling the trimming
if ((ruleTable->rules[target].symbols.size() == 1) &&
(symbolTable->symbols[ruleTable->rules[target].symbols[0]].kind ==
NON_TERMINAL) && m_trimReductions) {
trim = true;
newNonTerminal->trimmed = true;
} else {
newNonTerminal->trimmed = false;
trim = false;
}
// pop from the stack the tokens for the reduced rule
// and store them in the NonTerminal as its sons.
for (i=0; i < ruleTable->rules[target].symbols.size(); i++) {
Symbol *s = theStack.top ();
// If the symbol is trimmed we just pick up its children
if (s->trimmed) {
assert (s->type == NON_TERMINAL);
NonTerminal *trimmedNT = (NonTerminal*) s;
assert (trimmedNT->children.size() == 1);
newNonTerminal->children.push_front (trimmedNT->children[0]);
} else {
newNonTerminal->children.push_front (s);
}
theStack.pop();
}
// Perform GOTO
actObj = getNextAction (newNonTerminal->symbolIndex, theStack.top()->state);
if ((actObj != NULL) && (actObj->action == ACTION_GOTO)) {
currentState = actObj->target;
newNonTerminal->state = currentState;
// Push the reduced nonterminal in the stack
theStack.push (newNonTerminal);
} else {
wprintf (L"Internal Error!!\n");
reductionResult = REDUCTION_ERROR;
return NULL;
}
reductionResult = REDUCTION_COMPLETED;
return newNonTerminal;
break;
// This Action should never happen...
case ACTION_GOTO:
wprintf (L"Goto: %d", target);
currentState = target;
break;
case ACTION_ACCEPT:
reductionResult = REDUCTION_TEXT_ACCEPTED;
return NULL;
break;
}
return NULL;
}
Terminal *LALR::createTerminal (Token *tok) {
Terminal *newTerminal;
newTerminal = new Terminal();
newTerminal->symbol = tok->symbol;
newTerminal->image = tok->image;
newTerminal->symbolIndex = tok->symbolIndex;
newTerminal->state = tok->state;
newTerminal->line = tok->line;
newTerminal->col = tok->col;
return newTerminal;
}
NonTerminal *LALR::createNonTerminal (int index, int target) {
NonTerminal *newNonTerminal;
newNonTerminal = new NonTerminal();
newNonTerminal->symbolIndex = index;
newNonTerminal->symbol = symbolTable->symbols [index].name;
newNonTerminal->ruleIndex = ruleTable->rules[target].ruleIndex;
newNonTerminal->line = currentLine;
newNonTerminal->col = currentCol;
return newNonTerminal;
}