-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlr0.c
401 lines (325 loc) · 10.2 KB
/
lr0.c
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
396
397
398
399
400
401
/*
This file is part of Hyacc, a LR(0)/LALR(1)/LR(1)/LR(k) parser generator.
Copyright (C) 2008, 2009 Xin Chen. [email protected]
Hyacc is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Hyacc is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hyacc; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* LR(0) functions.
*
* @Author: Xin Chen
* @Created on: 2/24/2008
* @Last modified: 3/24/2009
* @Copyright (C) 2008, 2009
*/
#include "y.h"
static void propagateOriginatorChange(State * s);
void addSuccessorConfigToState_LR0(State * s, int ruleID)
{
Configuration * c;
if (s->config_count >= s->config_max_count - 1) {
s->config_max_count *= 2;
HYY_EXPAND(s->config, Configuration *, s->config_max_count);
}
// marker = 0, isCoreConfig = 0.
c = createConfig(ruleID, 0, 0);
c->owner = s;
s->config[s->config_count] = c;
s->config_count ++;
}
/*
* Assumption: public variable config_queue contains
* the configurations to be processed.
*/
void getConfigSuccessors_LR0(State * s)
{
RuleIDNode * r;
SymbolTblNode * scanned_symbol = NULL;
Configuration * config;
int index;
while (queue_count(config_queue) > 0) {
config = s->config[queue_pop(config_queue)];
if (config->marker >= 0 &&
config->marker < grammar.rules[config->ruleID]->RHS_count) {
scanned_symbol = getScannedSymbol(config);
if (isNonTerminal(scanned_symbol) == TRUE) {
for (r = scanned_symbol->ruleIDList; r != NULL; r = r->next) {
// Grammar.rules[r->ruleID] starts with this scanned symbol.
// If not an existing config, add to state s.
index = isCompatibleSuccessorConfig(s, r->ruleID);
if (index == -1) { // new config.
addSuccessorConfigToState_LR0(s, r->ruleID);
queue_push(config_queue, s->config_count - 1);
index = s->config_count - 1;
} // else is an existing old config, do nothing.
} // end for
} // else, is a terminal, stop.
} // end if config-marker >= 0 ...
} // end of while
}
void getClosure_LR0(State * s)
{
int i;
//queue_clear(config_queue);
for (i = 0; i < s->config_count; i ++) {
queue_push(config_queue, i);
}
getConfigSuccessors_LR0(s);
}
/*
* For LR(0). Insert a/r actions to the ENTIRE row.
*/
void insertReductionToParsingTable_LR0(
Configuration * c, int state_no)
{
int col;
SymbolTblNode * n;
int max_col = grammar.terminal_count + 1;
if (grammar.rules[c->ruleID]->nLHS->snode ==
grammar.goal_symbol->snode) { //accept, action = "a";
// note this should happen only if the end is $end.
insertAction(hashTbl_find(strEnd), state_no, CONST_ACC);
} else { //reduct, action = "r";
for (col = 0; col < max_col; col ++) {
n = ParsingTblColHdr[col];
insertAction(n, state_no, (-1) * c->ruleID);
}
}
}
/*
* if context set is empty, fill all cells in the ENTIRE row;
* otherwise, fill cells with lookaheads in the context set.
*/
void insertReductionToParsingTable_LALR(
Configuration * c, int state_no)
{
SymbolNode * a;
int col;
SymbolTblNode * n;
int max_col = grammar.terminal_count + 1;
if (grammar.rules[c->ruleID]->nLHS->snode ==
grammar.goal_symbol->snode) { //accept, action = "a";
// note this should happen only if the end is $end.
insertAction(hashTbl_find(strEnd), state_no, CONST_ACC);
} else { //reduct, action = "r";
a = c->context->nContext;
if (a != NULL) {
for (a = c->context->nContext; a != NULL; a = a->next) {
insertAction(a->snode, state_no, (-1) * c->ruleID);
}
} else {
for (col = 0; col < max_col; col ++) {
n = ParsingTblColHdr[col];
insertAction(n, state_no, (-1) * c->ruleID);
}
}
}
}
/*
* For each of the new temp states,
* If it is not one of the existing states in
* states_new, then add it to states_new.
* Also always add the transition to parsing table.
*
* Note:
* The "is_compatible" variable is of NO use here,
* since configurations don't have contexts. So such
* states are always the "same", but not compatible.
*/
void addTransitionStates2New_LR0(
State_collection * coll, State * src_state)
{
State * os, * next, * s;
s = coll->states_head;
while (s != NULL) {
next = s->next;
// searchSameStateHashTbl() checks for SAME (not compatible) states.
if ((os = searchSameStateHashTbl(s)) == NULL) {
insert_state_to_PM(s);
// Add this new state as successor to src_state.
addSuccessor(src_state, s);
} else { // same with an existing state.
addSuccessor(src_state, os);
destroyState(s); // existing or compatible. No use.
}
s = next;
} // end of while.
}
/*
* Perform transition opertaion on a state to get successors.
*
* For each config c in the state s,
* If c is a final config, stop
* Else, get the scanned symbol x,
* If a new temp state for x does not exist yet, create it.
* add x to the temp state.
* (Now get several new temp states)
* Add these new temp states to states_new if not existed,
* and add transition to parsing table as well.
*/
void transition_LR0(State * s)
{
int i;
Configuration * c, * new_config;
SymbolTblNode * scanned_symbol = NULL;
State_collection * coll = createStateCollection();
State * new_state = NULL;
for (i = 0; i < s->config_count; i ++) {
c = s->config[i];
if (isFinalConfiguration(c)) {
// do nothing.
} else { // do transit operation.
scanned_symbol = getScannedSymbol(c);
if (strlen(scanned_symbol->symbol) == 0) { // empty reduction.
continue;
}
new_state =
findStateForScannedSymbol(coll, scanned_symbol);
if (new_state == NULL) {
new_state = createState();
// record which symbol this state is a successor by.
new_state->trans_symbol =
createSymbolNode(scanned_symbol);
addState2Collection(coll, new_state);
}
// create a new core config for new_state.
new_config = createConfig(-1, 0, 1);
new_config->owner = new_state;
copyConfig(new_config, c);
new_config->isCoreConfig = 1;
new_config->marker ++;
if (new_config->nMarker != NULL)
new_config->nMarker = new_config->nMarker->next;
addCoreConfig2State(new_state, new_config);
}
} // end for
if (coll->state_count > 0) {
addTransitionStates2New_LR0(coll, s);
}
}
/*
* First fill acc, s, g.
* Then fill r. r is filled to terminal symbols only. And if
* a cell is already a/s/g, don't fill this r.
*/
static void outputParsingTableRow_LR0(State * s)
{
int i, ct;
Configuration * c;
State * t;
ct = s->config_count;
//printf("\nstate %d. config count: %d\n", s->state_no, ct);
// insert a/r actions.
for (i = 0; i < ct; i ++) {
c = s->config[i];
//printf("%d.%d\n", s->state_no, c->ruleID);
if (isFinalConfiguration(c) == TRUE ||
isEmptyProduction(c) == TRUE) {
insertReductionToParsingTable_LR0(c, s->state_no);
}
}
// insert s/g actions.
ct = s->successor_count;
for (i = 0; i < ct; i ++) {
t = s->successor_list[i];
insertAction(t->trans_symbol->snode, s->state_no, t->state_no);
}
}
static void outputParsingTableRow_LALR(State * s)
{
int i, ct;
Configuration * c;
State * t;
ct = s->config_count;
// insert a/r actions.
for (i = 0; i < ct; i ++) {
c = s->config[i];
//printf("%d.%d\n", s->state_no, c->ruleID);
if (isFinalConfiguration(c) == TRUE ||
isEmptyProduction(c) == TRUE) {
insertReductionToParsingTable_LALR(c, s->state_no);
}
}
// insert s/g actions.
ct = s->successor_count;
for (i = 0; i < ct; i ++) {
t = s->successor_list[i];
insertAction(t->trans_symbol->snode, s->state_no, t->state_no);
}
}
/*
* Output parsing table from the parsing machine.
* This is different from before. The previous implementation
* for LR(1) is this is done when transitioning states. Now for
* LR(0) since all default actions are reduce, the previous
* method does not work very well.
*/
void outputParsingTable_LR0()
{
State * s;
int i, cols, rows;
rows = states_new_array->state_count;
cols = n_symbol + 1;
// expand size of parsing table array if needed.
if (rows >= PARSING_TABLE_SIZE) {
expandParsingTable();
}
memset((void *) ParsingTable, 0, cols * rows * 4);
for (i = 0; i < rows; i ++) {
s = states_new_array->state_list[i];
outputParsingTableRow_LR0(s);
}
}
/*
*
*/
void outputParsingTable_LALR()
{
State * s;
int i, cols, rows;
rows = states_new_array->state_count;
cols = n_symbol + 1;
// expand size of parsing table array if needed.
if (rows >= PARSING_TABLE_SIZE) {
expandParsingTable();
}
memset((void *) ParsingTable, 0, cols * rows * 4);
for (i = 0; i < rows; i ++) {
s = states_new_array->state_list[i];
outputParsingTableRow_LALR(s);
}
}
void updateParsingTable_LR0()
{
ParsingTblRows = states_new->state_count;
n_state_opt1 = states_new->state_count;
// this fills the conflict list, so is need for lalr processing.
outputParsingTable_LR0();
}
void generate_LR0_parsing_machine()
{
State * new_state = states_new->states_head;
if (DEBUG_GEN_PARSING_MACHINE == TRUE) {
yyprintf("\n\n--generate parsing machine--\n");
}
while (new_state != NULL) {
if (DEBUG_GEN_PARSING_MACHINE == TRUE) {
yyprintf3("%d states, current state is %d\n",
states_new->state_count, new_state->state_no);
}
getClosure_LR0(new_state); // get closure of this state.
// get successor states and add them to states_new.
transition_LR0(new_state);
new_state = new_state->next; // point to next unprocessed state.
}
updateParsingTable_LR0();
}