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
|
/* $OpenBSD: llparse.c,v 1.2 1996/03/04 10:37:03 mickey Exp $ */
/* $NetBSD: llparse.c,v 1.4 1994/06/29 06:41:02 cgd Exp $ */
/*
* ************************* NOTICE *******************************
* This code is in the public domain. It cannot be copyrighted.
* This ll parser was originally written by Keith Thompson for the
* University of Wisconsin Crystal project.
* It was based on an FMQ lr parser written by Jon Mauney at the
* University of Wisconsin.
* It was subsequently modified very slightly by Nancy Hall at the
* University of Wisconsin for the Crystal project.
* ****************************************************************
*/
#include "xebec.h"
#include "llparse.h"
#include "main.h"
#include <stdio.h>
#include "debug.h"
#define LLMINACTION -LLINF
short llparsestack[STACKSIZE];
short llstackptr = 0;
LLtoken lltoken;
llparse()
{
register havetoken = FALSE;
register sym;
register LLtoken *t = &lltoken;
register parseaction;
register accepted = FALSE;
llpushprod(llnprods-1); /* $$$ ::= <start symbol> */
do {
sym = llparsestack[llstackptr];
IFDEBUG(L)
printf("llparse() top of loop, llstackptr=%d, sym=%d\n",
llstackptr, sym);
ENDDEBUG
if(sym < 0) {
/* action symbol */
if(sym <= LLMINACTION) {
for(;sym<=LLMINACTION;sym++) {
llaction(1, t); /* calls llfinprod */
}
llstackptr--;
continue;
} else { llaction(-sym, t);
llstackptr--;
continue;
}
}
if(sym < llnterms) {
/* it's a terminal symbol */
if(!havetoken) {
llgettoken(t);
havetoken = TRUE;
}
if(sym == t->llterm) {
llpushattr(t->llattrib);
llaccept(t);
llstackptr--; /* pop terminal */
if(t->llterm == llnterms-1) { /* end symbol $$$ */
accepted = TRUE;
} else {
havetoken = FALSE;
}
} else {
llparsererror(t); /* wrong terminal on input */
havetoken = FALSE;
}
continue;
}
/* non terminal */
if(!havetoken) {
llgettoken(t);
havetoken = TRUE;
}
/* consult parse table for new production */
parseaction = llfindaction(sym, t->llterm);
if(parseaction == 0) {
/* error entry */
llparsererror(t);
havetoken = FALSE;
continue;
}
if(llepsilon[parseaction]) {
/* epsilon production */
if(llepsilonok(t->llterm)) {
llstackptr--; /* pop nonterminal */
llpushprod(parseaction); /* push rhs of production */
} else {
llparsererror(t);
havetoken = FALSE;
}
} else {
llstackptr--; /* pop nonterminal */
llpushprod(parseaction); /* push rhs of production */
}
} while(!accepted);
return(0);
}
llpushprod(prod) /* recognize production prod - push rhs on stack */
short prod;
{
register start;
register length;
register count;
start = llprodindex[prod].llprodstart;
length = llprodindex[prod].llprodlength;
IFDEBUG(L)
printf("llpushprod(%d) llstackptr=0x%x(%d), length = 0x%x(%d)\n",
prod, llstackptr, llstackptr, length , length);
/*
dump_parse_stack();
*/
ENDDEBUG
if(llstackptr+length >= STACKSIZE) {
fprintf(stderr,"Parse stack overflow. llstackptr=0x%x, length=0x%x\n",
llstackptr, length);
Exit(-1);
}
llsetattr(llprodindex[prod].llprodtlen);
/* put a marker on the stack to mark beginning of production */
if(llparsestack[llstackptr] <= LLMINACTION) {
(llparsestack[llstackptr]) --; /* if there's already one there, don't
put another on; just let it represent all of
the adjacent markers */
}
else {
llstackptr++;
llparsestack[llstackptr] = LLMINACTION;
}
for(count=0; count<length; count++) {
llstackptr++;
llparsestack[llstackptr] = llproductions[start++];
}
if(llstackptr > STACKSIZE) {
fprintf(stderr, "PARSE STACK OVERFLOW! \n"); Exit(-1);
Exit(-1);
}
}
llepsilonok(term)
{
register ptr;
register sym;
register pact;
register nomore;
register rval;
IFDEBUG(L)
printf("llepsilonok() enter\n");
ENDDEBUG
rval = TRUE;
ptr = llstackptr;
do {
sym = llparsestack[ptr];
if(sym < 0) {
ptr--;
nomore = ptr == 0;
continue;
}
if(sym < llnterms) {
nomore = TRUE;
rval = sym == term;
continue;
}
pact = llfindaction(sym, term);
if(pact == 0) {
nomore = TRUE;
rval = FALSE;
continue;
}
if(llepsilon[pact] == TRUE) {
ptr--;
nomore = ptr == 0;
}
else {
nomore = TRUE;
}
} while(!nomore);
return(rval);
}
short llfindaction(sym, term)
{
register index;
IFDEBUG(L)
printf("llfindaction(sym=%d, term=%d) enter \n", sym, term);
ENDDEBUG
index = llparseindex[sym];
while(llparsetable[index].llterm != 0) {
if(llparsetable[index].llterm == term) {
return(llparsetable[index].llprod);
}
index++;
}
return(0);
}
llparsererror(token)
LLtoken *token;
{
IFDEBUG(L)
fprintf(stderr,"llparsererror() enter\n");
prt_token(token);
ENDDEBUG
fprintf(stderr, "Syntax error: ");
prt_token(token);
dump_buffer();
Exit(-1);
}
llgettoken(token)
LLtoken *token;
{
llscan(token);
token->llstate = NORMAL;
IFDEBUG(L)
printf("llgettoken(): ");
prt_token(token);
ENDDEBUG
}
/******************************************************************************
Attribute support routines
******************************************************************************/
/*
** attribute stack
**
** AttrStack = stack of record
** values : array of values;
** ptr : index;
** end;
**
*/
LLattrib llattributes[LLMAXATTR];
int llattrtop = 0;
struct llattr llattrdesc[LLMAXDESC];
int lldescindex = 1;
llsetattr(n)
{
register struct llattr *ptr;
IFDEBUG(L)
printf("llsetattr(%d) enter\n",n);
ENDDEBUG
if(lldescindex >= LLMAXDESC) {
fprintf(stdout, "llattribute stack overflow: desc\n");
fprintf(stdout,
"lldescindex=0x%x, llattrtop=0x%x\n",lldescindex, llattrtop);
Exit(-1);
}
ptr = &llattrdesc[lldescindex];
ptr->llabase = &llattributes[llattrtop];
ptr->lloldtop = ++llattrtop;
ptr->llaindex = 1;
ptr->llacnt = n+1; /* the lhs ALWAYS uses an attr; it remains on the
stack when the production is recognized */
lldescindex++;
}
llpushattr(attr)
LLattrib attr;
{
struct llattr *a;
IFDEBUG(L)
printf("llpushattr() enter\n");
ENDDEBUG
if(llattrtop + 1 > LLMAXATTR) {
fprintf(stderr, "ATTRIBUTE STACK OVERFLOW!\n");
Exit(-1);
}
a = &llattrdesc[lldescindex-1];
llattributes[llattrtop++] = attr;
a->llaindex++; /* inc count of attrs on the stack for this prod */
}
llfinprod()
{
IFDEBUG(L)
printf("llfinprod() enter\n");
ENDDEBUG
lldescindex--;
llattrtop = llattrdesc[lldescindex].lloldtop;
llattrdesc[lldescindex-1].llaindex++; /* lhs-of-prod.attr stays on
the stack; it is now one of the rhs attrs of the now-top production
on the stack */
}
#ifndef LINT
#ifdef DEBUG
dump_parse_stack()
{
int ind;
printf("PARSE STACK:\n");
for(ind=llstackptr; ind>=0; ind--) {
printf("%d\t%d\t%s\n",
ind, llparsestack[ind],
llparsestack[ind]<0? "Action symbol" : llstrings[llparsestack[ind]]);
}
}
#endif DEBUG
#endif LINT
prt_token(t)
LLtoken *t;
{
fprintf(stdout, "t at 0x%x\n", t);
fprintf(stdout, "t->llterm=0x%x\n", t->llterm); (void) fflush(stdout);
fprintf(stdout, "TOK: %s\n", llstrings[t->llterm]);
(void) fflush(stdout);
#ifdef LINT
/* to make lint shut up */
fprintf(stdout, "", llnterms, llnsyms, llnprods, llinfinite);
#endif LINT
}
|