Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: third_party/sqlite/sqlite-src-3100200/tool/lemon.c

Issue 2846743003: [sql] Remove SQLite 3.10.2 reference directory. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 ** This file contains all sources (including headers) to the LEMON
3 ** LALR(1) parser generator. The sources have been combined into a
4 ** single file to make it easy to include LEMON in the source tree
5 ** and Makefile of another program.
6 **
7 ** The author of this program disclaims copyright.
8 */
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <stdlib.h>
14 #include <assert.h>
15
16 #define ISSPACE(X) isspace((unsigned char)(X))
17 #define ISDIGIT(X) isdigit((unsigned char)(X))
18 #define ISALNUM(X) isalnum((unsigned char)(X))
19 #define ISALPHA(X) isalpha((unsigned char)(X))
20 #define ISUPPER(X) isupper((unsigned char)(X))
21 #define ISLOWER(X) islower((unsigned char)(X))
22
23
24 #ifndef __WIN32__
25 # if defined(_WIN32) || defined(WIN32)
26 # define __WIN32__
27 # endif
28 #endif
29
30 #ifdef __WIN32__
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 extern int access(const char *path, int mode);
35 #ifdef __cplusplus
36 }
37 #endif
38 #else
39 #include <unistd.h>
40 #endif
41
42 /* #define PRIVATE static */
43 #define PRIVATE
44
45 #ifdef TEST
46 #define MAXRHS 5 /* Set low to exercise exception code */
47 #else
48 #define MAXRHS 1000
49 #endif
50
51 static int showPrecedenceConflict = 0;
52 static char *msort(char*,char**,int(*)(const char*,const char*));
53
54 /*
55 ** Compilers are getting increasingly pedantic about type conversions
56 ** as C evolves ever closer to Ada.... To work around the latest problems
57 ** we have to define the following variant of strlen().
58 */
59 #define lemonStrlen(X) ((int)strlen(X))
60
61 /*
62 ** Compilers are starting to complain about the use of sprintf() and strcpy(),
63 ** saying they are unsafe. So we define our own versions of those routines too.
64 **
65 ** There are three routines here: lemon_sprintf(), lemon_vsprintf(), and
66 ** lemon_addtext(). The first two are replacements for sprintf() and vsprintf().
67 ** The third is a helper routine for vsnprintf() that adds texts to the end of a
68 ** buffer, making sure the buffer is always zero-terminated.
69 **
70 ** The string formatter is a minimal subset of stdlib sprintf() supporting only
71 ** a few simply conversions:
72 **
73 ** %d
74 ** %s
75 ** %.*s
76 **
77 */
78 static void lemon_addtext(
79 char *zBuf, /* The buffer to which text is added */
80 int *pnUsed, /* Slots of the buffer used so far */
81 const char *zIn, /* Text to add */
82 int nIn, /* Bytes of text to add. -1 to use strlen() */
83 int iWidth /* Field width. Negative to left justify */
84 ){
85 if( nIn<0 ) for(nIn=0; zIn[nIn]; nIn++){}
86 while( iWidth>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth--; }
87 if( nIn==0 ) return;
88 memcpy(&zBuf[*pnUsed], zIn, nIn);
89 *pnUsed += nIn;
90 while( (-iWidth)>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth++; }
91 zBuf[*pnUsed] = 0;
92 }
93 static int lemon_vsprintf(char *str, const char *zFormat, va_list ap){
94 int i, j, k, c;
95 int nUsed = 0;
96 const char *z;
97 char zTemp[50];
98 str[0] = 0;
99 for(i=j=0; (c = zFormat[i])!=0; i++){
100 if( c=='%' ){
101 int iWidth = 0;
102 lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0);
103 c = zFormat[++i];
104 if( ISDIGIT(c) || (c=='-' && ISDIGIT(zFormat[i+1])) ){
105 if( c=='-' ) i++;
106 while( ISDIGIT(zFormat[i]) ) iWidth = iWidth*10 + zFormat[i++] - '0';
107 if( c=='-' ) iWidth = -iWidth;
108 c = zFormat[i];
109 }
110 if( c=='d' ){
111 int v = va_arg(ap, int);
112 if( v<0 ){
113 lemon_addtext(str, &nUsed, "-", 1, iWidth);
114 v = -v;
115 }else if( v==0 ){
116 lemon_addtext(str, &nUsed, "0", 1, iWidth);
117 }
118 k = 0;
119 while( v>0 ){
120 k++;
121 zTemp[sizeof(zTemp)-k] = (v%10) + '0';
122 v /= 10;
123 }
124 lemon_addtext(str, &nUsed, &zTemp[sizeof(zTemp)-k], k, iWidth);
125 }else if( c=='s' ){
126 z = va_arg(ap, const char*);
127 lemon_addtext(str, &nUsed, z, -1, iWidth);
128 }else if( c=='.' && memcmp(&zFormat[i], ".*s", 3)==0 ){
129 i += 2;
130 k = va_arg(ap, int);
131 z = va_arg(ap, const char*);
132 lemon_addtext(str, &nUsed, z, k, iWidth);
133 }else if( c=='%' ){
134 lemon_addtext(str, &nUsed, "%", 1, 0);
135 }else{
136 fprintf(stderr, "illegal format\n");
137 exit(1);
138 }
139 j = i+1;
140 }
141 }
142 lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0);
143 return nUsed;
144 }
145 static int lemon_sprintf(char *str, const char *format, ...){
146 va_list ap;
147 int rc;
148 va_start(ap, format);
149 rc = lemon_vsprintf(str, format, ap);
150 va_end(ap);
151 return rc;
152 }
153 static void lemon_strcpy(char *dest, const char *src){
154 while( (*(dest++) = *(src++))!=0 ){}
155 }
156 static void lemon_strcat(char *dest, const char *src){
157 while( *dest ) dest++;
158 lemon_strcpy(dest, src);
159 }
160
161
162 /* a few forward declarations... */
163 struct rule;
164 struct lemon;
165 struct action;
166
167 static struct action *Action_new(void);
168 static struct action *Action_sort(struct action *);
169
170 /********** From the file "build.h" ************************************/
171 void FindRulePrecedences();
172 void FindFirstSets();
173 void FindStates();
174 void FindLinks();
175 void FindFollowSets();
176 void FindActions();
177
178 /********* From the file "configlist.h" *********************************/
179 void Configlist_init(void);
180 struct config *Configlist_add(struct rule *, int);
181 struct config *Configlist_addbasis(struct rule *, int);
182 void Configlist_closure(struct lemon *);
183 void Configlist_sort(void);
184 void Configlist_sortbasis(void);
185 struct config *Configlist_return(void);
186 struct config *Configlist_basis(void);
187 void Configlist_eat(struct config *);
188 void Configlist_reset(void);
189
190 /********* From the file "error.h" ***************************************/
191 void ErrorMsg(const char *, int,const char *, ...);
192
193 /****** From the file "option.h" ******************************************/
194 enum option_type { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR,
195 OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR};
196 struct s_options {
197 enum option_type type;
198 const char *label;
199 char *arg;
200 const char *message;
201 };
202 int OptInit(char**,struct s_options*,FILE*);
203 int OptNArgs(void);
204 char *OptArg(int);
205 void OptErr(int);
206 void OptPrint(void);
207
208 /******** From the file "parse.h" *****************************************/
209 void Parse(struct lemon *lemp);
210
211 /********* From the file "plink.h" ***************************************/
212 struct plink *Plink_new(void);
213 void Plink_add(struct plink **, struct config *);
214 void Plink_copy(struct plink **, struct plink *);
215 void Plink_delete(struct plink *);
216
217 /********** From the file "report.h" *************************************/
218 void Reprint(struct lemon *);
219 void ReportOutput(struct lemon *);
220 void ReportTable(struct lemon *, int);
221 void ReportHeader(struct lemon *);
222 void CompressTables(struct lemon *);
223 void ResortStates(struct lemon *);
224
225 /********** From the file "set.h" ****************************************/
226 void SetSize(int); /* All sets will be of size N */
227 char *SetNew(void); /* A new set for element 0..N */
228 void SetFree(char*); /* Deallocate a set */
229 int SetAdd(char*,int); /* Add element to a set */
230 int SetUnion(char *,char *); /* A <- A U B, thru element N */
231 #define SetFind(X,Y) (X[Y]) /* True if Y is in set X */
232
233 /********** From the file "struct.h" *************************************/
234 /*
235 ** Principal data structures for the LEMON parser generator.
236 */
237
238 typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean;
239
240 /* Symbols (terminals and nonterminals) of the grammar are stored
241 ** in the following: */
242 enum symbol_type {
243 TERMINAL,
244 NONTERMINAL,
245 MULTITERMINAL
246 };
247 enum e_assoc {
248 LEFT,
249 RIGHT,
250 NONE,
251 UNK
252 };
253 struct symbol {
254 const char *name; /* Name of the symbol */
255 int index; /* Index number for this symbol */
256 enum symbol_type type; /* Symbols are all either TERMINALS or NTs */
257 struct rule *rule; /* Linked list of rules of this (if an NT) */
258 struct symbol *fallback; /* fallback token in case this token doesn't parse */
259 int prec; /* Precedence if defined (-1 otherwise) */
260 enum e_assoc assoc; /* Associativity if precedence is defined */
261 char *firstset; /* First-set for all rules of this symbol */
262 Boolean lambda; /* True if NT and can generate an empty string */
263 int useCnt; /* Number of times used */
264 char *destructor; /* Code which executes whenever this symbol is
265 ** popped from the stack during error processing */
266 int destLineno; /* Line number for start of destructor */
267 char *datatype; /* The data type of information held by this
268 ** object. Only used if type==NONTERMINAL */
269 int dtnum; /* The data type number. In the parser, the value
270 ** stack is a union. The .yy%d element of this
271 ** union is the correct data type for this object */
272 /* The following fields are used by MULTITERMINALs only */
273 int nsubsym; /* Number of constituent symbols in the MULTI */
274 struct symbol **subsym; /* Array of constituent symbols */
275 };
276
277 /* Each production rule in the grammar is stored in the following
278 ** structure. */
279 struct rule {
280 struct symbol *lhs; /* Left-hand side of the rule */
281 const char *lhsalias; /* Alias for the LHS (NULL if none) */
282 int lhsStart; /* True if left-hand side is the start symbol */
283 int ruleline; /* Line number for the rule */
284 int nrhs; /* Number of RHS symbols */
285 struct symbol **rhs; /* The RHS symbols */
286 const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */
287 int line; /* Line number at which code begins */
288 const char *code; /* The code executed when this rule is reduced */
289 struct symbol *precsym; /* Precedence symbol for this rule */
290 int index; /* An index number for this rule */
291 Boolean canReduce; /* True if this rule is ever reduced */
292 struct rule *nextlhs; /* Next rule with the same LHS */
293 struct rule *next; /* Next rule in the global list */
294 };
295
296 /* A configuration is a production rule of the grammar together with
297 ** a mark (dot) showing how much of that rule has been processed so far.
298 ** Configurations also contain a follow-set which is a list of terminal
299 ** symbols which are allowed to immediately follow the end of the rule.
300 ** Every configuration is recorded as an instance of the following: */
301 enum cfgstatus {
302 COMPLETE,
303 INCOMPLETE
304 };
305 struct config {
306 struct rule *rp; /* The rule upon which the configuration is based */
307 int dot; /* The parse point */
308 char *fws; /* Follow-set for this configuration only */
309 struct plink *fplp; /* Follow-set forward propagation links */
310 struct plink *bplp; /* Follow-set backwards propagation links */
311 struct state *stp; /* Pointer to state which contains this */
312 enum cfgstatus status; /* used during followset and shift computations */
313 struct config *next; /* Next configuration in the state */
314 struct config *bp; /* The next basis configuration */
315 };
316
317 enum e_action {
318 SHIFT,
319 ACCEPT,
320 REDUCE,
321 ERROR,
322 SSCONFLICT, /* A shift/shift conflict */
323 SRCONFLICT, /* Was a reduce, but part of a conflict */
324 RRCONFLICT, /* Was a reduce, but part of a conflict */
325 SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
326 RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
327 NOT_USED, /* Deleted by compression */
328 SHIFTREDUCE /* Shift first, then reduce */
329 };
330
331 /* Every shift or reduce operation is stored as one of the following */
332 struct action {
333 struct symbol *sp; /* The look-ahead symbol */
334 enum e_action type;
335 union {
336 struct state *stp; /* The new state, if a shift */
337 struct rule *rp; /* The rule, if a reduce */
338 } x;
339 struct action *next; /* Next action for this state */
340 struct action *collide; /* Next action with the same hash */
341 };
342
343 /* Each state of the generated parser's finite state machine
344 ** is encoded as an instance of the following structure. */
345 struct state {
346 struct config *bp; /* The basis configurations for this state */
347 struct config *cfp; /* All configurations in this set */
348 int statenum; /* Sequential number for this state */
349 struct action *ap; /* Array of actions for this state */
350 int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */
351 int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */
352 int iDfltReduce; /* Default action is to REDUCE by this rule */
353 struct rule *pDfltReduce;/* The default REDUCE rule. */
354 int autoReduce; /* True if this is an auto-reduce state */
355 };
356 #define NO_OFFSET (-2147483647)
357
358 /* A followset propagation link indicates that the contents of one
359 ** configuration followset should be propagated to another whenever
360 ** the first changes. */
361 struct plink {
362 struct config *cfp; /* The configuration to which linked */
363 struct plink *next; /* The next propagate link */
364 };
365
366 /* The state vector for the entire parser generator is recorded as
367 ** follows. (LEMON uses no global variables and makes little use of
368 ** static variables. Fields in the following structure can be thought
369 ** of as begin global variables in the program.) */
370 struct lemon {
371 struct state **sorted; /* Table of states sorted by state number */
372 struct rule *rule; /* List of all rules */
373 int nstate; /* Number of states */
374 int nxstate; /* nstate with tail degenerate states removed */
375 int nrule; /* Number of rules */
376 int nsymbol; /* Number of terminal and nonterminal symbols */
377 int nterminal; /* Number of terminal symbols */
378 struct symbol **symbols; /* Sorted array of pointers to symbols */
379 int errorcnt; /* Number of errors */
380 struct symbol *errsym; /* The error symbol */
381 struct symbol *wildcard; /* Token that matches anything */
382 char *name; /* Name of the generated parser */
383 char *arg; /* Declaration of the 3th argument to parser */
384 char *tokentype; /* Type of terminal symbols in the parser stack */
385 char *vartype; /* The default type of non-terminal symbols */
386 char *start; /* Name of the start symbol for the grammar */
387 char *stacksize; /* Size of the parser stack */
388 char *include; /* Code to put at the start of the C file */
389 char *error; /* Code to execute when an error is seen */
390 char *overflow; /* Code to execute on a stack overflow */
391 char *failure; /* Code to execute on parser failure */
392 char *accept; /* Code to execute when the parser excepts */
393 char *extracode; /* Code appended to the generated file */
394 char *tokendest; /* Code to execute to destroy token data */
395 char *vardest; /* Code for the default non-terminal destructor */
396 char *filename; /* Name of the input file */
397 char *outname; /* Name of the current output file */
398 char *tokenprefix; /* A prefix added to token names in the .h file */
399 int nconflict; /* Number of parsing conflicts */
400 int nactiontab; /* Number of entries in the yy_action[] table */
401 int tablesize; /* Total table size of all tables in bytes */
402 int basisflag; /* Print only basis configurations */
403 int has_fallback; /* True if any %fallback is seen in the grammar */
404 int nolinenosflag; /* True if #line statements should not be printed */
405 char *argv0; /* Name of the program */
406 };
407
408 #define MemoryCheck(X) if((X)==0){ \
409 extern void memory_error(); \
410 memory_error(); \
411 }
412
413 /**************** From the file "table.h" *********************************/
414 /*
415 ** All code in this file has been automatically generated
416 ** from a specification in the file
417 ** "table.q"
418 ** by the associative array code building program "aagen".
419 ** Do not edit this file! Instead, edit the specification
420 ** file, then rerun aagen.
421 */
422 /*
423 ** Code for processing tables in the LEMON parser generator.
424 */
425 /* Routines for handling a strings */
426
427 const char *Strsafe(const char *);
428
429 void Strsafe_init(void);
430 int Strsafe_insert(const char *);
431 const char *Strsafe_find(const char *);
432
433 /* Routines for handling symbols of the grammar */
434
435 struct symbol *Symbol_new(const char *);
436 int Symbolcmpp(const void *, const void *);
437 void Symbol_init(void);
438 int Symbol_insert(struct symbol *, const char *);
439 struct symbol *Symbol_find(const char *);
440 struct symbol *Symbol_Nth(int);
441 int Symbol_count(void);
442 struct symbol **Symbol_arrayof(void);
443
444 /* Routines to manage the state table */
445
446 int Configcmp(const char *, const char *);
447 struct state *State_new(void);
448 void State_init(void);
449 int State_insert(struct state *, struct config *);
450 struct state *State_find(struct config *);
451 struct state **State_arrayof(/* */);
452
453 /* Routines used for efficiency in Configlist_add */
454
455 void Configtable_init(void);
456 int Configtable_insert(struct config *);
457 struct config *Configtable_find(struct config *);
458 void Configtable_clear(int(*)(struct config *));
459
460 /****************** From the file "action.c" *******************************/
461 /*
462 ** Routines processing parser actions in the LEMON parser generator.
463 */
464
465 /* Allocate a new parser action */
466 static struct action *Action_new(void){
467 static struct action *freelist = 0;
468 struct action *newaction;
469
470 if( freelist==0 ){
471 int i;
472 int amt = 100;
473 freelist = (struct action *)calloc(amt, sizeof(struct action));
474 if( freelist==0 ){
475 fprintf(stderr,"Unable to allocate memory for a new parser action.");
476 exit(1);
477 }
478 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
479 freelist[amt-1].next = 0;
480 }
481 newaction = freelist;
482 freelist = freelist->next;
483 return newaction;
484 }
485
486 /* Compare two actions for sorting purposes. Return negative, zero, or
487 ** positive if the first action is less than, equal to, or greater than
488 ** the first
489 */
490 static int actioncmp(
491 struct action *ap1,
492 struct action *ap2
493 ){
494 int rc;
495 rc = ap1->sp->index - ap2->sp->index;
496 if( rc==0 ){
497 rc = (int)ap1->type - (int)ap2->type;
498 }
499 if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){
500 rc = ap1->x.rp->index - ap2->x.rp->index;
501 }
502 if( rc==0 ){
503 rc = (int) (ap2 - ap1);
504 }
505 return rc;
506 }
507
508 /* Sort parser actions */
509 static struct action *Action_sort(
510 struct action *ap
511 ){
512 ap = (struct action *)msort((char *)ap,(char **)&ap->next,
513 (int(*)(const char*,const char*))actioncmp);
514 return ap;
515 }
516
517 void Action_add(
518 struct action **app,
519 enum e_action type,
520 struct symbol *sp,
521 char *arg
522 ){
523 struct action *newaction;
524 newaction = Action_new();
525 newaction->next = *app;
526 *app = newaction;
527 newaction->type = type;
528 newaction->sp = sp;
529 if( type==SHIFT ){
530 newaction->x.stp = (struct state *)arg;
531 }else{
532 newaction->x.rp = (struct rule *)arg;
533 }
534 }
535 /********************** New code to implement the "acttab" module ***********/
536 /*
537 ** This module implements routines use to construct the yy_action[] table.
538 */
539
540 /*
541 ** The state of the yy_action table under construction is an instance of
542 ** the following structure.
543 **
544 ** The yy_action table maps the pair (state_number, lookahead) into an
545 ** action_number. The table is an array of integers pairs. The state_number
546 ** determines an initial offset into the yy_action array. The lookahead
547 ** value is then added to this initial offset to get an index X into the
548 ** yy_action array. If the aAction[X].lookahead equals the value of the
549 ** of the lookahead input, then the value of the action_number output is
550 ** aAction[X].action. If the lookaheads do not match then the
551 ** default action for the state_number is returned.
552 **
553 ** All actions associated with a single state_number are first entered
554 ** into aLookahead[] using multiple calls to acttab_action(). Then the
555 ** actions for that single state_number are placed into the aAction[]
556 ** array with a single call to acttab_insert(). The acttab_insert() call
557 ** also resets the aLookahead[] array in preparation for the next
558 ** state number.
559 */
560 struct lookahead_action {
561 int lookahead; /* Value of the lookahead token */
562 int action; /* Action to take on the given lookahead */
563 };
564 typedef struct acttab acttab;
565 struct acttab {
566 int nAction; /* Number of used slots in aAction[] */
567 int nActionAlloc; /* Slots allocated for aAction[] */
568 struct lookahead_action
569 *aAction, /* The yy_action[] table under construction */
570 *aLookahead; /* A single new transaction set */
571 int mnLookahead; /* Minimum aLookahead[].lookahead */
572 int mnAction; /* Action associated with mnLookahead */
573 int mxLookahead; /* Maximum aLookahead[].lookahead */
574 int nLookahead; /* Used slots in aLookahead[] */
575 int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
576 };
577
578 /* Return the number of entries in the yy_action table */
579 #define acttab_size(X) ((X)->nAction)
580
581 /* The value for the N-th entry in yy_action */
582 #define acttab_yyaction(X,N) ((X)->aAction[N].action)
583
584 /* The value for the N-th entry in yy_lookahead */
585 #define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
586
587 /* Free all memory associated with the given acttab */
588 void acttab_free(acttab *p){
589 free( p->aAction );
590 free( p->aLookahead );
591 free( p );
592 }
593
594 /* Allocate a new acttab structure */
595 acttab *acttab_alloc(void){
596 acttab *p = (acttab *) calloc( 1, sizeof(*p) );
597 if( p==0 ){
598 fprintf(stderr,"Unable to allocate memory for a new acttab.");
599 exit(1);
600 }
601 memset(p, 0, sizeof(*p));
602 return p;
603 }
604
605 /* Add a new action to the current transaction set.
606 **
607 ** This routine is called once for each lookahead for a particular
608 ** state.
609 */
610 void acttab_action(acttab *p, int lookahead, int action){
611 if( p->nLookahead>=p->nLookaheadAlloc ){
612 p->nLookaheadAlloc += 25;
613 p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
614 sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
615 if( p->aLookahead==0 ){
616 fprintf(stderr,"malloc failed\n");
617 exit(1);
618 }
619 }
620 if( p->nLookahead==0 ){
621 p->mxLookahead = lookahead;
622 p->mnLookahead = lookahead;
623 p->mnAction = action;
624 }else{
625 if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
626 if( p->mnLookahead>lookahead ){
627 p->mnLookahead = lookahead;
628 p->mnAction = action;
629 }
630 }
631 p->aLookahead[p->nLookahead].lookahead = lookahead;
632 p->aLookahead[p->nLookahead].action = action;
633 p->nLookahead++;
634 }
635
636 /*
637 ** Add the transaction set built up with prior calls to acttab_action()
638 ** into the current action table. Then reset the transaction set back
639 ** to an empty set in preparation for a new round of acttab_action() calls.
640 **
641 ** Return the offset into the action table of the new transaction.
642 */
643 int acttab_insert(acttab *p){
644 int i, j, k, n;
645 assert( p->nLookahead>0 );
646
647 /* Make sure we have enough space to hold the expanded action table
648 ** in the worst case. The worst case occurs if the transaction set
649 ** must be appended to the current action table
650 */
651 n = p->mxLookahead + 1;
652 if( p->nAction + n >= p->nActionAlloc ){
653 int oldAlloc = p->nActionAlloc;
654 p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
655 p->aAction = (struct lookahead_action *) realloc( p->aAction,
656 sizeof(p->aAction[0])*p->nActionAlloc);
657 if( p->aAction==0 ){
658 fprintf(stderr,"malloc failed\n");
659 exit(1);
660 }
661 for(i=oldAlloc; i<p->nActionAlloc; i++){
662 p->aAction[i].lookahead = -1;
663 p->aAction[i].action = -1;
664 }
665 }
666
667 /* Scan the existing action table looking for an offset that is a
668 ** duplicate of the current transaction set. Fall out of the loop
669 ** if and when the duplicate is found.
670 **
671 ** i is the index in p->aAction[] where p->mnLookahead is inserted.
672 */
673 for(i=p->nAction-1; i>=0; i--){
674 if( p->aAction[i].lookahead==p->mnLookahead ){
675 /* All lookaheads and actions in the aLookahead[] transaction
676 ** must match against the candidate aAction[i] entry. */
677 if( p->aAction[i].action!=p->mnAction ) continue;
678 for(j=0; j<p->nLookahead; j++){
679 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
680 if( k<0 || k>=p->nAction ) break;
681 if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
682 if( p->aLookahead[j].action!=p->aAction[k].action ) break;
683 }
684 if( j<p->nLookahead ) continue;
685
686 /* No possible lookahead value that is not in the aLookahead[]
687 ** transaction is allowed to match aAction[i] */
688 n = 0;
689 for(j=0; j<p->nAction; j++){
690 if( p->aAction[j].lookahead<0 ) continue;
691 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
692 }
693 if( n==p->nLookahead ){
694 break; /* An exact match is found at offset i */
695 }
696 }
697 }
698
699 /* If no existing offsets exactly match the current transaction, find an
700 ** an empty offset in the aAction[] table in which we can add the
701 ** aLookahead[] transaction.
702 */
703 if( i<0 ){
704 /* Look for holes in the aAction[] table that fit the current
705 ** aLookahead[] transaction. Leave i set to the offset of the hole.
706 ** If no holes are found, i is left at p->nAction, which means the
707 ** transaction will be appended. */
708 for(i=0; i<p->nActionAlloc - p->mxLookahead; i++){
709 if( p->aAction[i].lookahead<0 ){
710 for(j=0; j<p->nLookahead; j++){
711 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
712 if( k<0 ) break;
713 if( p->aAction[k].lookahead>=0 ) break;
714 }
715 if( j<p->nLookahead ) continue;
716 for(j=0; j<p->nAction; j++){
717 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
718 }
719 if( j==p->nAction ){
720 break; /* Fits in empty slots */
721 }
722 }
723 }
724 }
725 /* Insert transaction set at index i. */
726 for(j=0; j<p->nLookahead; j++){
727 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
728 p->aAction[k] = p->aLookahead[j];
729 if( k>=p->nAction ) p->nAction = k+1;
730 }
731 p->nLookahead = 0;
732
733 /* Return the offset that is added to the lookahead in order to get the
734 ** index into yy_action of the action */
735 return i - p->mnLookahead;
736 }
737
738 /********************** From the file "build.c" *****************************/
739 /*
740 ** Routines to construction the finite state machine for the LEMON
741 ** parser generator.
742 */
743
744 /* Find a precedence symbol of every rule in the grammar.
745 **
746 ** Those rules which have a precedence symbol coded in the input
747 ** grammar using the "[symbol]" construct will already have the
748 ** rp->precsym field filled. Other rules take as their precedence
749 ** symbol the first RHS symbol with a defined precedence. If there
750 ** are not RHS symbols with a defined precedence, the precedence
751 ** symbol field is left blank.
752 */
753 void FindRulePrecedences(struct lemon *xp)
754 {
755 struct rule *rp;
756 for(rp=xp->rule; rp; rp=rp->next){
757 if( rp->precsym==0 ){
758 int i, j;
759 for(i=0; i<rp->nrhs && rp->precsym==0; i++){
760 struct symbol *sp = rp->rhs[i];
761 if( sp->type==MULTITERMINAL ){
762 for(j=0; j<sp->nsubsym; j++){
763 if( sp->subsym[j]->prec>=0 ){
764 rp->precsym = sp->subsym[j];
765 break;
766 }
767 }
768 }else if( sp->prec>=0 ){
769 rp->precsym = rp->rhs[i];
770 }
771 }
772 }
773 }
774 return;
775 }
776
777 /* Find all nonterminals which will generate the empty string.
778 ** Then go back and compute the first sets of every nonterminal.
779 ** The first set is the set of all terminal symbols which can begin
780 ** a string generated by that nonterminal.
781 */
782 void FindFirstSets(struct lemon *lemp)
783 {
784 int i, j;
785 struct rule *rp;
786 int progress;
787
788 for(i=0; i<lemp->nsymbol; i++){
789 lemp->symbols[i]->lambda = LEMON_FALSE;
790 }
791 for(i=lemp->nterminal; i<lemp->nsymbol; i++){
792 lemp->symbols[i]->firstset = SetNew();
793 }
794
795 /* First compute all lambdas */
796 do{
797 progress = 0;
798 for(rp=lemp->rule; rp; rp=rp->next){
799 if( rp->lhs->lambda ) continue;
800 for(i=0; i<rp->nrhs; i++){
801 struct symbol *sp = rp->rhs[i];
802 assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE );
803 if( sp->lambda==LEMON_FALSE ) break;
804 }
805 if( i==rp->nrhs ){
806 rp->lhs->lambda = LEMON_TRUE;
807 progress = 1;
808 }
809 }
810 }while( progress );
811
812 /* Now compute all first sets */
813 do{
814 struct symbol *s1, *s2;
815 progress = 0;
816 for(rp=lemp->rule; rp; rp=rp->next){
817 s1 = rp->lhs;
818 for(i=0; i<rp->nrhs; i++){
819 s2 = rp->rhs[i];
820 if( s2->type==TERMINAL ){
821 progress += SetAdd(s1->firstset,s2->index);
822 break;
823 }else if( s2->type==MULTITERMINAL ){
824 for(j=0; j<s2->nsubsym; j++){
825 progress += SetAdd(s1->firstset,s2->subsym[j]->index);
826 }
827 break;
828 }else if( s1==s2 ){
829 if( s1->lambda==LEMON_FALSE ) break;
830 }else{
831 progress += SetUnion(s1->firstset,s2->firstset);
832 if( s2->lambda==LEMON_FALSE ) break;
833 }
834 }
835 }
836 }while( progress );
837 return;
838 }
839
840 /* Compute all LR(0) states for the grammar. Links
841 ** are added to between some states so that the LR(1) follow sets
842 ** can be computed later.
843 */
844 PRIVATE struct state *getstate(struct lemon *); /* forward reference */
845 void FindStates(struct lemon *lemp)
846 {
847 struct symbol *sp;
848 struct rule *rp;
849
850 Configlist_init();
851
852 /* Find the start symbol */
853 if( lemp->start ){
854 sp = Symbol_find(lemp->start);
855 if( sp==0 ){
856 ErrorMsg(lemp->filename,0,
857 "The specified start symbol \"%s\" is not \
858 in a nonterminal of the grammar. \"%s\" will be used as the start \
859 symbol instead.",lemp->start,lemp->rule->lhs->name);
860 lemp->errorcnt++;
861 sp = lemp->rule->lhs;
862 }
863 }else{
864 sp = lemp->rule->lhs;
865 }
866
867 /* Make sure the start symbol doesn't occur on the right-hand side of
868 ** any rule. Report an error if it does. (YACC would generate a new
869 ** start symbol in this case.) */
870 for(rp=lemp->rule; rp; rp=rp->next){
871 int i;
872 for(i=0; i<rp->nrhs; i++){
873 if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */
874 ErrorMsg(lemp->filename,0,
875 "The start symbol \"%s\" occurs on the \
876 right-hand side of a rule. This will result in a parser which \
877 does not work properly.",sp->name);
878 lemp->errorcnt++;
879 }
880 }
881 }
882
883 /* The basis configuration set for the first state
884 ** is all rules which have the start symbol as their
885 ** left-hand side */
886 for(rp=sp->rule; rp; rp=rp->nextlhs){
887 struct config *newcfp;
888 rp->lhsStart = 1;
889 newcfp = Configlist_addbasis(rp,0);
890 SetAdd(newcfp->fws,0);
891 }
892
893 /* Compute the first state. All other states will be
894 ** computed automatically during the computation of the first one.
895 ** The returned pointer to the first state is not used. */
896 (void)getstate(lemp);
897 return;
898 }
899
900 /* Return a pointer to a state which is described by the configuration
901 ** list which has been built from calls to Configlist_add.
902 */
903 PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
904 PRIVATE struct state *getstate(struct lemon *lemp)
905 {
906 struct config *cfp, *bp;
907 struct state *stp;
908
909 /* Extract the sorted basis of the new state. The basis was constructed
910 ** by prior calls to "Configlist_addbasis()". */
911 Configlist_sortbasis();
912 bp = Configlist_basis();
913
914 /* Get a state with the same basis */
915 stp = State_find(bp);
916 if( stp ){
917 /* A state with the same basis already exists! Copy all the follow-set
918 ** propagation links from the state under construction into the
919 ** preexisting state, then return a pointer to the preexisting state */
920 struct config *x, *y;
921 for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
922 Plink_copy(&y->bplp,x->bplp);
923 Plink_delete(x->fplp);
924 x->fplp = x->bplp = 0;
925 }
926 cfp = Configlist_return();
927 Configlist_eat(cfp);
928 }else{
929 /* This really is a new state. Construct all the details */
930 Configlist_closure(lemp); /* Compute the configuration closure */
931 Configlist_sort(); /* Sort the configuration closure */
932 cfp = Configlist_return(); /* Get a pointer to the config list */
933 stp = State_new(); /* A new state structure */
934 MemoryCheck(stp);
935 stp->bp = bp; /* Remember the configuration basis */
936 stp->cfp = cfp; /* Remember the configuration closure */
937 stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
938 stp->ap = 0; /* No actions, yet. */
939 State_insert(stp,stp->bp); /* Add to the state table */
940 buildshifts(lemp,stp); /* Recursively compute successor states */
941 }
942 return stp;
943 }
944
945 /*
946 ** Return true if two symbols are the same.
947 */
948 int same_symbol(struct symbol *a, struct symbol *b)
949 {
950 int i;
951 if( a==b ) return 1;
952 if( a->type!=MULTITERMINAL ) return 0;
953 if( b->type!=MULTITERMINAL ) return 0;
954 if( a->nsubsym!=b->nsubsym ) return 0;
955 for(i=0; i<a->nsubsym; i++){
956 if( a->subsym[i]!=b->subsym[i] ) return 0;
957 }
958 return 1;
959 }
960
961 /* Construct all successor states to the given state. A "successor"
962 ** state is any state which can be reached by a shift action.
963 */
964 PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
965 {
966 struct config *cfp; /* For looping thru the config closure of "stp" */
967 struct config *bcfp; /* For the inner loop on config closure of "stp" */
968 struct config *newcfg; /* */
969 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
970 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
971 struct state *newstp; /* A pointer to a successor state */
972
973 /* Each configuration becomes complete after it contibutes to a successor
974 ** state. Initially, all configurations are incomplete */
975 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
976
977 /* Loop through all configurations of the state "stp" */
978 for(cfp=stp->cfp; cfp; cfp=cfp->next){
979 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
980 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
981 Configlist_reset(); /* Reset the new config set */
982 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
983
984 /* For every configuration in the state "stp" which has the symbol "sp"
985 ** following its dot, add the same configuration to the basis set under
986 ** construction but with the dot shifted one symbol to the right. */
987 for(bcfp=cfp; bcfp; bcfp=bcfp->next){
988 if( bcfp->status==COMPLETE ) continue; /* Already used */
989 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
990 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
991 if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
992 bcfp->status = COMPLETE; /* Mark this config as used */
993 newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
994 Plink_add(&newcfg->bplp,bcfp);
995 }
996
997 /* Get a pointer to the state described by the basis configuration set
998 ** constructed in the preceding loop */
999 newstp = getstate(lemp);
1000
1001 /* The state "newstp" is reached from the state "stp" by a shift action
1002 ** on the symbol "sp" */
1003 if( sp->type==MULTITERMINAL ){
1004 int i;
1005 for(i=0; i<sp->nsubsym; i++){
1006 Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
1007 }
1008 }else{
1009 Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
1010 }
1011 }
1012 }
1013
1014 /*
1015 ** Construct the propagation links
1016 */
1017 void FindLinks(struct lemon *lemp)
1018 {
1019 int i;
1020 struct config *cfp, *other;
1021 struct state *stp;
1022 struct plink *plp;
1023
1024 /* Housekeeping detail:
1025 ** Add to every propagate link a pointer back to the state to
1026 ** which the link is attached. */
1027 for(i=0; i<lemp->nstate; i++){
1028 stp = lemp->sorted[i];
1029 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1030 cfp->stp = stp;
1031 }
1032 }
1033
1034 /* Convert all backlinks into forward links. Only the forward
1035 ** links are used in the follow-set computation. */
1036 for(i=0; i<lemp->nstate; i++){
1037 stp = lemp->sorted[i];
1038 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1039 for(plp=cfp->bplp; plp; plp=plp->next){
1040 other = plp->cfp;
1041 Plink_add(&other->fplp,cfp);
1042 }
1043 }
1044 }
1045 }
1046
1047 /* Compute all followsets.
1048 **
1049 ** A followset is the set of all symbols which can come immediately
1050 ** after a configuration.
1051 */
1052 void FindFollowSets(struct lemon *lemp)
1053 {
1054 int i;
1055 struct config *cfp;
1056 struct plink *plp;
1057 int progress;
1058 int change;
1059
1060 for(i=0; i<lemp->nstate; i++){
1061 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1062 cfp->status = INCOMPLETE;
1063 }
1064 }
1065
1066 do{
1067 progress = 0;
1068 for(i=0; i<lemp->nstate; i++){
1069 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1070 if( cfp->status==COMPLETE ) continue;
1071 for(plp=cfp->fplp; plp; plp=plp->next){
1072 change = SetUnion(plp->cfp->fws,cfp->fws);
1073 if( change ){
1074 plp->cfp->status = INCOMPLETE;
1075 progress = 1;
1076 }
1077 }
1078 cfp->status = COMPLETE;
1079 }
1080 }
1081 }while( progress );
1082 }
1083
1084 static int resolve_conflict(struct action *,struct action *);
1085
1086 /* Compute the reduce actions, and resolve conflicts.
1087 */
1088 void FindActions(struct lemon *lemp)
1089 {
1090 int i,j;
1091 struct config *cfp;
1092 struct state *stp;
1093 struct symbol *sp;
1094 struct rule *rp;
1095
1096 /* Add all of the reduce actions
1097 ** A reduce action is added for each element of the followset of
1098 ** a configuration which has its dot at the extreme right.
1099 */
1100 for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
1101 stp = lemp->sorted[i];
1102 for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
1103 if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
1104 for(j=0; j<lemp->nterminal; j++){
1105 if( SetFind(cfp->fws,j) ){
1106 /* Add a reduce action to the state "stp" which will reduce by the
1107 ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
1108 Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
1109 }
1110 }
1111 }
1112 }
1113 }
1114
1115 /* Add the accepting token */
1116 if( lemp->start ){
1117 sp = Symbol_find(lemp->start);
1118 if( sp==0 ) sp = lemp->rule->lhs;
1119 }else{
1120 sp = lemp->rule->lhs;
1121 }
1122 /* Add to the first state (which is always the starting state of the
1123 ** finite state machine) an action to ACCEPT if the lookahead is the
1124 ** start nonterminal. */
1125 Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
1126
1127 /* Resolve conflicts */
1128 for(i=0; i<lemp->nstate; i++){
1129 struct action *ap, *nap;
1130 stp = lemp->sorted[i];
1131 /* assert( stp->ap ); */
1132 stp->ap = Action_sort(stp->ap);
1133 for(ap=stp->ap; ap && ap->next; ap=ap->next){
1134 for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
1135 /* The two actions "ap" and "nap" have the same lookahead.
1136 ** Figure out which one should be used */
1137 lemp->nconflict += resolve_conflict(ap,nap);
1138 }
1139 }
1140 }
1141
1142 /* Report an error for each rule that can never be reduced. */
1143 for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
1144 for(i=0; i<lemp->nstate; i++){
1145 struct action *ap;
1146 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
1147 if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE;
1148 }
1149 }
1150 for(rp=lemp->rule; rp; rp=rp->next){
1151 if( rp->canReduce ) continue;
1152 ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
1153 lemp->errorcnt++;
1154 }
1155 }
1156
1157 /* Resolve a conflict between the two given actions. If the
1158 ** conflict can't be resolved, return non-zero.
1159 **
1160 ** NO LONGER TRUE:
1161 ** To resolve a conflict, first look to see if either action
1162 ** is on an error rule. In that case, take the action which
1163 ** is not associated with the error rule. If neither or both
1164 ** actions are associated with an error rule, then try to
1165 ** use precedence to resolve the conflict.
1166 **
1167 ** If either action is a SHIFT, then it must be apx. This
1168 ** function won't work if apx->type==REDUCE and apy->type==SHIFT.
1169 */
1170 static int resolve_conflict(
1171 struct action *apx,
1172 struct action *apy
1173 ){
1174 struct symbol *spx, *spy;
1175 int errcnt = 0;
1176 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
1177 if( apx->type==SHIFT && apy->type==SHIFT ){
1178 apy->type = SSCONFLICT;
1179 errcnt++;
1180 }
1181 if( apx->type==SHIFT && apy->type==REDUCE ){
1182 spx = apx->sp;
1183 spy = apy->x.rp->precsym;
1184 if( spy==0 || spx->prec<0 || spy->prec<0 ){
1185 /* Not enough precedence information. */
1186 apy->type = SRCONFLICT;
1187 errcnt++;
1188 }else if( spx->prec>spy->prec ){ /* higher precedence wins */
1189 apy->type = RD_RESOLVED;
1190 }else if( spx->prec<spy->prec ){
1191 apx->type = SH_RESOLVED;
1192 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
1193 apy->type = RD_RESOLVED; /* associativity */
1194 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
1195 apx->type = SH_RESOLVED;
1196 }else{
1197 assert( spx->prec==spy->prec && spx->assoc==NONE );
1198 apx->type = ERROR;
1199 }
1200 }else if( apx->type==REDUCE && apy->type==REDUCE ){
1201 spx = apx->x.rp->precsym;
1202 spy = apy->x.rp->precsym;
1203 if( spx==0 || spy==0 || spx->prec<0 ||
1204 spy->prec<0 || spx->prec==spy->prec ){
1205 apy->type = RRCONFLICT;
1206 errcnt++;
1207 }else if( spx->prec>spy->prec ){
1208 apy->type = RD_RESOLVED;
1209 }else if( spx->prec<spy->prec ){
1210 apx->type = RD_RESOLVED;
1211 }
1212 }else{
1213 assert(
1214 apx->type==SH_RESOLVED ||
1215 apx->type==RD_RESOLVED ||
1216 apx->type==SSCONFLICT ||
1217 apx->type==SRCONFLICT ||
1218 apx->type==RRCONFLICT ||
1219 apy->type==SH_RESOLVED ||
1220 apy->type==RD_RESOLVED ||
1221 apy->type==SSCONFLICT ||
1222 apy->type==SRCONFLICT ||
1223 apy->type==RRCONFLICT
1224 );
1225 /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
1226 ** REDUCEs on the list. If we reach this point it must be because
1227 ** the parser conflict had already been resolved. */
1228 }
1229 return errcnt;
1230 }
1231 /********************* From the file "configlist.c" *************************/
1232 /*
1233 ** Routines to processing a configuration list and building a state
1234 ** in the LEMON parser generator.
1235 */
1236
1237 static struct config *freelist = 0; /* List of free configurations */
1238 static struct config *current = 0; /* Top of list of configurations */
1239 static struct config **currentend = 0; /* Last on list of configs */
1240 static struct config *basis = 0; /* Top of list of basis configs */
1241 static struct config **basisend = 0; /* End of list of basis configs */
1242
1243 /* Return a pointer to a new configuration */
1244 PRIVATE struct config *newconfig(){
1245 struct config *newcfg;
1246 if( freelist==0 ){
1247 int i;
1248 int amt = 3;
1249 freelist = (struct config *)calloc( amt, sizeof(struct config) );
1250 if( freelist==0 ){
1251 fprintf(stderr,"Unable to allocate memory for a new configuration.");
1252 exit(1);
1253 }
1254 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
1255 freelist[amt-1].next = 0;
1256 }
1257 newcfg = freelist;
1258 freelist = freelist->next;
1259 return newcfg;
1260 }
1261
1262 /* The configuration "old" is no longer used */
1263 PRIVATE void deleteconfig(struct config *old)
1264 {
1265 old->next = freelist;
1266 freelist = old;
1267 }
1268
1269 /* Initialized the configuration list builder */
1270 void Configlist_init(){
1271 current = 0;
1272 currentend = &current;
1273 basis = 0;
1274 basisend = &basis;
1275 Configtable_init();
1276 return;
1277 }
1278
1279 /* Initialized the configuration list builder */
1280 void Configlist_reset(){
1281 current = 0;
1282 currentend = &current;
1283 basis = 0;
1284 basisend = &basis;
1285 Configtable_clear(0);
1286 return;
1287 }
1288
1289 /* Add another configuration to the configuration list */
1290 struct config *Configlist_add(
1291 struct rule *rp, /* The rule */
1292 int dot /* Index into the RHS of the rule where the dot goes */
1293 ){
1294 struct config *cfp, model;
1295
1296 assert( currentend!=0 );
1297 model.rp = rp;
1298 model.dot = dot;
1299 cfp = Configtable_find(&model);
1300 if( cfp==0 ){
1301 cfp = newconfig();
1302 cfp->rp = rp;
1303 cfp->dot = dot;
1304 cfp->fws = SetNew();
1305 cfp->stp = 0;
1306 cfp->fplp = cfp->bplp = 0;
1307 cfp->next = 0;
1308 cfp->bp = 0;
1309 *currentend = cfp;
1310 currentend = &cfp->next;
1311 Configtable_insert(cfp);
1312 }
1313 return cfp;
1314 }
1315
1316 /* Add a basis configuration to the configuration list */
1317 struct config *Configlist_addbasis(struct rule *rp, int dot)
1318 {
1319 struct config *cfp, model;
1320
1321 assert( basisend!=0 );
1322 assert( currentend!=0 );
1323 model.rp = rp;
1324 model.dot = dot;
1325 cfp = Configtable_find(&model);
1326 if( cfp==0 ){
1327 cfp = newconfig();
1328 cfp->rp = rp;
1329 cfp->dot = dot;
1330 cfp->fws = SetNew();
1331 cfp->stp = 0;
1332 cfp->fplp = cfp->bplp = 0;
1333 cfp->next = 0;
1334 cfp->bp = 0;
1335 *currentend = cfp;
1336 currentend = &cfp->next;
1337 *basisend = cfp;
1338 basisend = &cfp->bp;
1339 Configtable_insert(cfp);
1340 }
1341 return cfp;
1342 }
1343
1344 /* Compute the closure of the configuration list */
1345 void Configlist_closure(struct lemon *lemp)
1346 {
1347 struct config *cfp, *newcfp;
1348 struct rule *rp, *newrp;
1349 struct symbol *sp, *xsp;
1350 int i, dot;
1351
1352 assert( currentend!=0 );
1353 for(cfp=current; cfp; cfp=cfp->next){
1354 rp = cfp->rp;
1355 dot = cfp->dot;
1356 if( dot>=rp->nrhs ) continue;
1357 sp = rp->rhs[dot];
1358 if( sp->type==NONTERMINAL ){
1359 if( sp->rule==0 && sp!=lemp->errsym ){
1360 ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
1361 sp->name);
1362 lemp->errorcnt++;
1363 }
1364 for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
1365 newcfp = Configlist_add(newrp,0);
1366 for(i=dot+1; i<rp->nrhs; i++){
1367 xsp = rp->rhs[i];
1368 if( xsp->type==TERMINAL ){
1369 SetAdd(newcfp->fws,xsp->index);
1370 break;
1371 }else if( xsp->type==MULTITERMINAL ){
1372 int k;
1373 for(k=0; k<xsp->nsubsym; k++){
1374 SetAdd(newcfp->fws, xsp->subsym[k]->index);
1375 }
1376 break;
1377 }else{
1378 SetUnion(newcfp->fws,xsp->firstset);
1379 if( xsp->lambda==LEMON_FALSE ) break;
1380 }
1381 }
1382 if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
1383 }
1384 }
1385 }
1386 return;
1387 }
1388
1389 /* Sort the configuration list */
1390 void Configlist_sort(){
1391 current = (struct config*)msort((char*)current,(char**)&(current->next),
1392 Configcmp);
1393 currentend = 0;
1394 return;
1395 }
1396
1397 /* Sort the basis configuration list */
1398 void Configlist_sortbasis(){
1399 basis = (struct config*)msort((char*)current,(char**)&(current->bp),
1400 Configcmp);
1401 basisend = 0;
1402 return;
1403 }
1404
1405 /* Return a pointer to the head of the configuration list and
1406 ** reset the list */
1407 struct config *Configlist_return(){
1408 struct config *old;
1409 old = current;
1410 current = 0;
1411 currentend = 0;
1412 return old;
1413 }
1414
1415 /* Return a pointer to the head of the configuration list and
1416 ** reset the list */
1417 struct config *Configlist_basis(){
1418 struct config *old;
1419 old = basis;
1420 basis = 0;
1421 basisend = 0;
1422 return old;
1423 }
1424
1425 /* Free all elements of the given configuration list */
1426 void Configlist_eat(struct config *cfp)
1427 {
1428 struct config *nextcfp;
1429 for(; cfp; cfp=nextcfp){
1430 nextcfp = cfp->next;
1431 assert( cfp->fplp==0 );
1432 assert( cfp->bplp==0 );
1433 if( cfp->fws ) SetFree(cfp->fws);
1434 deleteconfig(cfp);
1435 }
1436 return;
1437 }
1438 /***************** From the file "error.c" *********************************/
1439 /*
1440 ** Code for printing error message.
1441 */
1442
1443 void ErrorMsg(const char *filename, int lineno, const char *format, ...){
1444 va_list ap;
1445 fprintf(stderr, "%s:%d: ", filename, lineno);
1446 va_start(ap, format);
1447 vfprintf(stderr,format,ap);
1448 va_end(ap);
1449 fprintf(stderr, "\n");
1450 }
1451 /**************** From the file "main.c" ************************************/
1452 /*
1453 ** Main program file for the LEMON parser generator.
1454 */
1455
1456 /* Report an out-of-memory condition and abort. This function
1457 ** is used mostly by the "MemoryCheck" macro in struct.h
1458 */
1459 void memory_error(){
1460 fprintf(stderr,"Out of memory. Aborting...\n");
1461 exit(1);
1462 }
1463
1464 static int nDefine = 0; /* Number of -D options on the command line */
1465 static char **azDefine = 0; /* Name of the -D macros */
1466
1467 /* This routine is called with the argument to each -D command-line option.
1468 ** Add the macro defined to the azDefine array.
1469 */
1470 static void handle_D_option(char *z){
1471 char **paz;
1472 nDefine++;
1473 azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine);
1474 if( azDefine==0 ){
1475 fprintf(stderr,"out of memory\n");
1476 exit(1);
1477 }
1478 paz = &azDefine[nDefine-1];
1479 *paz = (char *) malloc( lemonStrlen(z)+1 );
1480 if( *paz==0 ){
1481 fprintf(stderr,"out of memory\n");
1482 exit(1);
1483 }
1484 lemon_strcpy(*paz, z);
1485 for(z=*paz; *z && *z!='='; z++){}
1486 *z = 0;
1487 }
1488
1489 static char *user_templatename = NULL;
1490 static void handle_T_option(char *z){
1491 user_templatename = (char *) malloc( lemonStrlen(z)+1 );
1492 if( user_templatename==0 ){
1493 memory_error();
1494 }
1495 lemon_strcpy(user_templatename, z);
1496 }
1497
1498 /* forward reference */
1499 static const char *minimum_size_type(int lwr, int upr, int *pnByte);
1500
1501 /* Print a single line of the "Parser Stats" output
1502 */
1503 static void stats_line(const char *zLabel, int iValue){
1504 int nLabel = lemonStrlen(zLabel);
1505 printf(" %s%.*s %5d\n", zLabel,
1506 35-nLabel, "................................",
1507 iValue);
1508 }
1509
1510 /* The main program. Parse the command line and do it... */
1511 int main(int argc, char **argv)
1512 {
1513 static int version = 0;
1514 static int rpflag = 0;
1515 static int basisflag = 0;
1516 static int compress = 0;
1517 static int quiet = 0;
1518 static int statistics = 0;
1519 static int mhflag = 0;
1520 static int nolinenosflag = 0;
1521 static int noResort = 0;
1522 static struct s_options options[] = {
1523 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1524 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
1525 {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
1526 {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"},
1527 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
1528 {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"},
1529 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
1530 {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
1531 {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"},
1532 {OPT_FLAG, "p", (char*)&showPrecedenceConflict,
1533 "Show conflicts resolved by precedence rules"},
1534 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
1535 {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"},
1536 {OPT_FLAG, "s", (char*)&statistics,
1537 "Print parser stats to standard output."},
1538 {OPT_FLAG, "x", (char*)&version, "Print the version number."},
1539 {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
1540 {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"},
1541 {OPT_FLAG,0,0,0}
1542 };
1543 int i;
1544 int exitcode;
1545 struct lemon lem;
1546
1547 OptInit(argv,options,stderr);
1548 if( version ){
1549 printf("Lemon version 1.0\n");
1550 exit(0);
1551 }
1552 if( OptNArgs()!=1 ){
1553 fprintf(stderr,"Exactly one filename argument is required.\n");
1554 exit(1);
1555 }
1556 memset(&lem, 0, sizeof(lem));
1557 lem.errorcnt = 0;
1558
1559 /* Initialize the machine */
1560 Strsafe_init();
1561 Symbol_init();
1562 State_init();
1563 lem.argv0 = argv[0];
1564 lem.filename = OptArg(0);
1565 lem.basisflag = basisflag;
1566 lem.nolinenosflag = nolinenosflag;
1567 Symbol_new("$");
1568 lem.errsym = Symbol_new("error");
1569 lem.errsym->useCnt = 0;
1570
1571 /* Parse the input file */
1572 Parse(&lem);
1573 if( lem.errorcnt ) exit(lem.errorcnt);
1574 if( lem.nrule==0 ){
1575 fprintf(stderr,"Empty grammar.\n");
1576 exit(1);
1577 }
1578
1579 /* Count and index the symbols of the grammar */
1580 Symbol_new("{default}");
1581 lem.nsymbol = Symbol_count();
1582 lem.symbols = Symbol_arrayof();
1583 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1584 qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp);
1585 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1586 while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; }
1587 assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 );
1588 lem.nsymbol = i - 1;
1589 for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++);
1590 lem.nterminal = i;
1591
1592 /* Generate a reprint of the grammar, if requested on the command line */
1593 if( rpflag ){
1594 Reprint(&lem);
1595 }else{
1596 /* Initialize the size for all follow and first sets */
1597 SetSize(lem.nterminal+1);
1598
1599 /* Find the precedence for every production rule (that has one) */
1600 FindRulePrecedences(&lem);
1601
1602 /* Compute the lambda-nonterminals and the first-sets for every
1603 ** nonterminal */
1604 FindFirstSets(&lem);
1605
1606 /* Compute all LR(0) states. Also record follow-set propagation
1607 ** links so that the follow-set can be computed later */
1608 lem.nstate = 0;
1609 FindStates(&lem);
1610 lem.sorted = State_arrayof();
1611
1612 /* Tie up loose ends on the propagation links */
1613 FindLinks(&lem);
1614
1615 /* Compute the follow set of every reducible configuration */
1616 FindFollowSets(&lem);
1617
1618 /* Compute the action tables */
1619 FindActions(&lem);
1620
1621 /* Compress the action tables */
1622 if( compress==0 ) CompressTables(&lem);
1623
1624 /* Reorder and renumber the states so that states with fewer choices
1625 ** occur at the end. This is an optimization that helps make the
1626 ** generated parser tables smaller. */
1627 if( noResort==0 ) ResortStates(&lem);
1628
1629 /* Generate a report of the parser generated. (the "y.output" file) */
1630 if( !quiet ) ReportOutput(&lem);
1631
1632 /* Generate the source code for the parser */
1633 ReportTable(&lem, mhflag);
1634
1635 /* Produce a header file for use by the scanner. (This step is
1636 ** omitted if the "-m" option is used because makeheaders will
1637 ** generate the file for us.) */
1638 if( !mhflag ) ReportHeader(&lem);
1639 }
1640 if( statistics ){
1641 printf("Parser statistics:\n");
1642 stats_line("terminal symbols", lem.nterminal);
1643 stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal);
1644 stats_line("total symbols", lem.nsymbol);
1645 stats_line("rules", lem.nrule);
1646 stats_line("states", lem.nxstate);
1647 stats_line("conflicts", lem.nconflict);
1648 stats_line("action table entries", lem.nactiontab);
1649 stats_line("total table size (bytes)", lem.tablesize);
1650 }
1651 if( lem.nconflict > 0 ){
1652 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
1653 }
1654
1655 /* return 0 on success, 1 on failure. */
1656 exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
1657 exit(exitcode);
1658 return (exitcode);
1659 }
1660 /******************** From the file "msort.c" *******************************/
1661 /*
1662 ** A generic merge-sort program.
1663 **
1664 ** USAGE:
1665 ** Let "ptr" be a pointer to some structure which is at the head of
1666 ** a null-terminated list. Then to sort the list call:
1667 **
1668 ** ptr = msort(ptr,&(ptr->next),cmpfnc);
1669 **
1670 ** In the above, "cmpfnc" is a pointer to a function which compares
1671 ** two instances of the structure and returns an integer, as in
1672 ** strcmp. The second argument is a pointer to the pointer to the
1673 ** second element of the linked list. This address is used to compute
1674 ** the offset to the "next" field within the structure. The offset to
1675 ** the "next" field must be constant for all structures in the list.
1676 **
1677 ** The function returns a new pointer which is the head of the list
1678 ** after sorting.
1679 **
1680 ** ALGORITHM:
1681 ** Merge-sort.
1682 */
1683
1684 /*
1685 ** Return a pointer to the next structure in the linked list.
1686 */
1687 #define NEXT(A) (*(char**)(((char*)A)+offset))
1688
1689 /*
1690 ** Inputs:
1691 ** a: A sorted, null-terminated linked list. (May be null).
1692 ** b: A sorted, null-terminated linked list. (May be null).
1693 ** cmp: A pointer to the comparison function.
1694 ** offset: Offset in the structure to the "next" field.
1695 **
1696 ** Return Value:
1697 ** A pointer to the head of a sorted list containing the elements
1698 ** of both a and b.
1699 **
1700 ** Side effects:
1701 ** The "next" pointers for elements in the lists a and b are
1702 ** changed.
1703 */
1704 static char *merge(
1705 char *a,
1706 char *b,
1707 int (*cmp)(const char*,const char*),
1708 int offset
1709 ){
1710 char *ptr, *head;
1711
1712 if( a==0 ){
1713 head = b;
1714 }else if( b==0 ){
1715 head = a;
1716 }else{
1717 if( (*cmp)(a,b)<=0 ){
1718 ptr = a;
1719 a = NEXT(a);
1720 }else{
1721 ptr = b;
1722 b = NEXT(b);
1723 }
1724 head = ptr;
1725 while( a && b ){
1726 if( (*cmp)(a,b)<=0 ){
1727 NEXT(ptr) = a;
1728 ptr = a;
1729 a = NEXT(a);
1730 }else{
1731 NEXT(ptr) = b;
1732 ptr = b;
1733 b = NEXT(b);
1734 }
1735 }
1736 if( a ) NEXT(ptr) = a;
1737 else NEXT(ptr) = b;
1738 }
1739 return head;
1740 }
1741
1742 /*
1743 ** Inputs:
1744 ** list: Pointer to a singly-linked list of structures.
1745 ** next: Pointer to pointer to the second element of the list.
1746 ** cmp: A comparison function.
1747 **
1748 ** Return Value:
1749 ** A pointer to the head of a sorted list containing the elements
1750 ** orginally in list.
1751 **
1752 ** Side effects:
1753 ** The "next" pointers for elements in list are changed.
1754 */
1755 #define LISTSIZE 30
1756 static char *msort(
1757 char *list,
1758 char **next,
1759 int (*cmp)(const char*,const char*)
1760 ){
1761 unsigned long offset;
1762 char *ep;
1763 char *set[LISTSIZE];
1764 int i;
1765 offset = (unsigned long)((char*)next - (char*)list);
1766 for(i=0; i<LISTSIZE; i++) set[i] = 0;
1767 while( list ){
1768 ep = list;
1769 list = NEXT(list);
1770 NEXT(ep) = 0;
1771 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1772 ep = merge(ep,set[i],cmp,offset);
1773 set[i] = 0;
1774 }
1775 set[i] = ep;
1776 }
1777 ep = 0;
1778 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset);
1779 return ep;
1780 }
1781 /************************ From the file "option.c" **************************/
1782 static char **argv;
1783 static struct s_options *op;
1784 static FILE *errstream;
1785
1786 #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1787
1788 /*
1789 ** Print the command line with a carrot pointing to the k-th character
1790 ** of the n-th field.
1791 */
1792 static void errline(int n, int k, FILE *err)
1793 {
1794 int spcnt, i;
1795 if( argv[0] ) fprintf(err,"%s",argv[0]);
1796 spcnt = lemonStrlen(argv[0]) + 1;
1797 for(i=1; i<n && argv[i]; i++){
1798 fprintf(err," %s",argv[i]);
1799 spcnt += lemonStrlen(argv[i])+1;
1800 }
1801 spcnt += k;
1802 for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1803 if( spcnt<20 ){
1804 fprintf(err,"\n%*s^-- here\n",spcnt,"");
1805 }else{
1806 fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1807 }
1808 }
1809
1810 /*
1811 ** Return the index of the N-th non-switch argument. Return -1
1812 ** if N is out of range.
1813 */
1814 static int argindex(int n)
1815 {
1816 int i;
1817 int dashdash = 0;
1818 if( argv!=0 && *argv!=0 ){
1819 for(i=1; argv[i]; i++){
1820 if( dashdash || !ISOPT(argv[i]) ){
1821 if( n==0 ) return i;
1822 n--;
1823 }
1824 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1825 }
1826 }
1827 return -1;
1828 }
1829
1830 static char emsg[] = "Command line syntax error: ";
1831
1832 /*
1833 ** Process a flag command line argument.
1834 */
1835 static int handleflags(int i, FILE *err)
1836 {
1837 int v;
1838 int errcnt = 0;
1839 int j;
1840 for(j=0; op[j].label; j++){
1841 if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
1842 }
1843 v = argv[i][0]=='-' ? 1 : 0;
1844 if( op[j].label==0 ){
1845 if( err ){
1846 fprintf(err,"%sundefined option.\n",emsg);
1847 errline(i,1,err);
1848 }
1849 errcnt++;
1850 }else if( op[j].arg==0 ){
1851 /* Ignore this option */
1852 }else if( op[j].type==OPT_FLAG ){
1853 *((int*)op[j].arg) = v;
1854 }else if( op[j].type==OPT_FFLAG ){
1855 (*(void(*)(int))(op[j].arg))(v);
1856 }else if( op[j].type==OPT_FSTR ){
1857 (*(void(*)(char *))(op[j].arg))(&argv[i][2]);
1858 }else{
1859 if( err ){
1860 fprintf(err,"%smissing argument on switch.\n",emsg);
1861 errline(i,1,err);
1862 }
1863 errcnt++;
1864 }
1865 return errcnt;
1866 }
1867
1868 /*
1869 ** Process a command line switch which has an argument.
1870 */
1871 static int handleswitch(int i, FILE *err)
1872 {
1873 int lv = 0;
1874 double dv = 0.0;
1875 char *sv = 0, *end;
1876 char *cp;
1877 int j;
1878 int errcnt = 0;
1879 cp = strchr(argv[i],'=');
1880 assert( cp!=0 );
1881 *cp = 0;
1882 for(j=0; op[j].label; j++){
1883 if( strcmp(argv[i],op[j].label)==0 ) break;
1884 }
1885 *cp = '=';
1886 if( op[j].label==0 ){
1887 if( err ){
1888 fprintf(err,"%sundefined option.\n",emsg);
1889 errline(i,0,err);
1890 }
1891 errcnt++;
1892 }else{
1893 cp++;
1894 switch( op[j].type ){
1895 case OPT_FLAG:
1896 case OPT_FFLAG:
1897 if( err ){
1898 fprintf(err,"%soption requires an argument.\n",emsg);
1899 errline(i,0,err);
1900 }
1901 errcnt++;
1902 break;
1903 case OPT_DBL:
1904 case OPT_FDBL:
1905 dv = strtod(cp,&end);
1906 if( *end ){
1907 if( err ){
1908 fprintf(err,
1909 "%sillegal character in floating-point argument.\n",emsg);
1910 errline(i,(int)((char*)end-(char*)argv[i]),err);
1911 }
1912 errcnt++;
1913 }
1914 break;
1915 case OPT_INT:
1916 case OPT_FINT:
1917 lv = strtol(cp,&end,0);
1918 if( *end ){
1919 if( err ){
1920 fprintf(err,"%sillegal character in integer argument.\n",emsg);
1921 errline(i,(int)((char*)end-(char*)argv[i]),err);
1922 }
1923 errcnt++;
1924 }
1925 break;
1926 case OPT_STR:
1927 case OPT_FSTR:
1928 sv = cp;
1929 break;
1930 }
1931 switch( op[j].type ){
1932 case OPT_FLAG:
1933 case OPT_FFLAG:
1934 break;
1935 case OPT_DBL:
1936 *(double*)(op[j].arg) = dv;
1937 break;
1938 case OPT_FDBL:
1939 (*(void(*)(double))(op[j].arg))(dv);
1940 break;
1941 case OPT_INT:
1942 *(int*)(op[j].arg) = lv;
1943 break;
1944 case OPT_FINT:
1945 (*(void(*)(int))(op[j].arg))((int)lv);
1946 break;
1947 case OPT_STR:
1948 *(char**)(op[j].arg) = sv;
1949 break;
1950 case OPT_FSTR:
1951 (*(void(*)(char *))(op[j].arg))(sv);
1952 break;
1953 }
1954 }
1955 return errcnt;
1956 }
1957
1958 int OptInit(char **a, struct s_options *o, FILE *err)
1959 {
1960 int errcnt = 0;
1961 argv = a;
1962 op = o;
1963 errstream = err;
1964 if( argv && *argv && op ){
1965 int i;
1966 for(i=1; argv[i]; i++){
1967 if( argv[i][0]=='+' || argv[i][0]=='-' ){
1968 errcnt += handleflags(i,err);
1969 }else if( strchr(argv[i],'=') ){
1970 errcnt += handleswitch(i,err);
1971 }
1972 }
1973 }
1974 if( errcnt>0 ){
1975 fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
1976 OptPrint();
1977 exit(1);
1978 }
1979 return 0;
1980 }
1981
1982 int OptNArgs(){
1983 int cnt = 0;
1984 int dashdash = 0;
1985 int i;
1986 if( argv!=0 && argv[0]!=0 ){
1987 for(i=1; argv[i]; i++){
1988 if( dashdash || !ISOPT(argv[i]) ) cnt++;
1989 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1990 }
1991 }
1992 return cnt;
1993 }
1994
1995 char *OptArg(int n)
1996 {
1997 int i;
1998 i = argindex(n);
1999 return i>=0 ? argv[i] : 0;
2000 }
2001
2002 void OptErr(int n)
2003 {
2004 int i;
2005 i = argindex(n);
2006 if( i>=0 ) errline(i,0,errstream);
2007 }
2008
2009 void OptPrint(){
2010 int i;
2011 int max, len;
2012 max = 0;
2013 for(i=0; op[i].label; i++){
2014 len = lemonStrlen(op[i].label) + 1;
2015 switch( op[i].type ){
2016 case OPT_FLAG:
2017 case OPT_FFLAG:
2018 break;
2019 case OPT_INT:
2020 case OPT_FINT:
2021 len += 9; /* length of "<integer>" */
2022 break;
2023 case OPT_DBL:
2024 case OPT_FDBL:
2025 len += 6; /* length of "<real>" */
2026 break;
2027 case OPT_STR:
2028 case OPT_FSTR:
2029 len += 8; /* length of "<string>" */
2030 break;
2031 }
2032 if( len>max ) max = len;
2033 }
2034 for(i=0; op[i].label; i++){
2035 switch( op[i].type ){
2036 case OPT_FLAG:
2037 case OPT_FFLAG:
2038 fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
2039 break;
2040 case OPT_INT:
2041 case OPT_FINT:
2042 fprintf(errstream," -%s<integer>%*s %s\n",op[i].label,
2043 (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message);
2044 break;
2045 case OPT_DBL:
2046 case OPT_FDBL:
2047 fprintf(errstream," -%s<real>%*s %s\n",op[i].label,
2048 (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message);
2049 break;
2050 case OPT_STR:
2051 case OPT_FSTR:
2052 fprintf(errstream," -%s<string>%*s %s\n",op[i].label,
2053 (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message);
2054 break;
2055 }
2056 }
2057 }
2058 /*********************** From the file "parse.c" ****************************/
2059 /*
2060 ** Input file parser for the LEMON parser generator.
2061 */
2062
2063 /* The state of the parser */
2064 enum e_state {
2065 INITIALIZE,
2066 WAITING_FOR_DECL_OR_RULE,
2067 WAITING_FOR_DECL_KEYWORD,
2068 WAITING_FOR_DECL_ARG,
2069 WAITING_FOR_PRECEDENCE_SYMBOL,
2070 WAITING_FOR_ARROW,
2071 IN_RHS,
2072 LHS_ALIAS_1,
2073 LHS_ALIAS_2,
2074 LHS_ALIAS_3,
2075 RHS_ALIAS_1,
2076 RHS_ALIAS_2,
2077 PRECEDENCE_MARK_1,
2078 PRECEDENCE_MARK_2,
2079 RESYNC_AFTER_RULE_ERROR,
2080 RESYNC_AFTER_DECL_ERROR,
2081 WAITING_FOR_DESTRUCTOR_SYMBOL,
2082 WAITING_FOR_DATATYPE_SYMBOL,
2083 WAITING_FOR_FALLBACK_ID,
2084 WAITING_FOR_WILDCARD_ID,
2085 WAITING_FOR_CLASS_ID,
2086 WAITING_FOR_CLASS_TOKEN
2087 };
2088 struct pstate {
2089 char *filename; /* Name of the input file */
2090 int tokenlineno; /* Linenumber at which current token starts */
2091 int errorcnt; /* Number of errors so far */
2092 char *tokenstart; /* Text of current token */
2093 struct lemon *gp; /* Global state vector */
2094 enum e_state state; /* The state of the parser */
2095 struct symbol *fallback; /* The fallback token */
2096 struct symbol *tkclass; /* Token class symbol */
2097 struct symbol *lhs; /* Left-hand side of current rule */
2098 const char *lhsalias; /* Alias for the LHS */
2099 int nrhs; /* Number of right-hand side symbols seen */
2100 struct symbol *rhs[MAXRHS]; /* RHS symbols */
2101 const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
2102 struct rule *prevrule; /* Previous rule parsed */
2103 const char *declkeyword; /* Keyword of a declaration */
2104 char **declargslot; /* Where the declaration argument should be put */
2105 int insertLineMacro; /* Add #line before declaration insert */
2106 int *decllinenoslot; /* Where to write declaration line number */
2107 enum e_assoc declassoc; /* Assign this association to decl arguments */
2108 int preccounter; /* Assign this precedence to decl arguments */
2109 struct rule *firstrule; /* Pointer to first rule in the grammar */
2110 struct rule *lastrule; /* Pointer to the most recently parsed rule */
2111 };
2112
2113 /* Parse a single token */
2114 static void parseonetoken(struct pstate *psp)
2115 {
2116 const char *x;
2117 x = Strsafe(psp->tokenstart); /* Save the token permanently */
2118 #if 0
2119 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
2120 x,psp->state);
2121 #endif
2122 switch( psp->state ){
2123 case INITIALIZE:
2124 psp->prevrule = 0;
2125 psp->preccounter = 0;
2126 psp->firstrule = psp->lastrule = 0;
2127 psp->gp->nrule = 0;
2128 /* Fall thru to next case */
2129 case WAITING_FOR_DECL_OR_RULE:
2130 if( x[0]=='%' ){
2131 psp->state = WAITING_FOR_DECL_KEYWORD;
2132 }else if( ISLOWER(x[0]) ){
2133 psp->lhs = Symbol_new(x);
2134 psp->nrhs = 0;
2135 psp->lhsalias = 0;
2136 psp->state = WAITING_FOR_ARROW;
2137 }else if( x[0]=='{' ){
2138 if( psp->prevrule==0 ){
2139 ErrorMsg(psp->filename,psp->tokenlineno,
2140 "There is no prior rule upon which to attach the code \
2141 fragment which begins on this line.");
2142 psp->errorcnt++;
2143 }else if( psp->prevrule->code!=0 ){
2144 ErrorMsg(psp->filename,psp->tokenlineno,
2145 "Code fragment beginning on this line is not the first \
2146 to follow the previous rule.");
2147 psp->errorcnt++;
2148 }else{
2149 psp->prevrule->line = psp->tokenlineno;
2150 psp->prevrule->code = &x[1];
2151 }
2152 }else if( x[0]=='[' ){
2153 psp->state = PRECEDENCE_MARK_1;
2154 }else{
2155 ErrorMsg(psp->filename,psp->tokenlineno,
2156 "Token \"%s\" should be either \"%%\" or a nonterminal name.",
2157 x);
2158 psp->errorcnt++;
2159 }
2160 break;
2161 case PRECEDENCE_MARK_1:
2162 if( !ISUPPER(x[0]) ){
2163 ErrorMsg(psp->filename,psp->tokenlineno,
2164 "The precedence symbol must be a terminal.");
2165 psp->errorcnt++;
2166 }else if( psp->prevrule==0 ){
2167 ErrorMsg(psp->filename,psp->tokenlineno,
2168 "There is no prior rule to assign precedence \"[%s]\".",x);
2169 psp->errorcnt++;
2170 }else if( psp->prevrule->precsym!=0 ){
2171 ErrorMsg(psp->filename,psp->tokenlineno,
2172 "Precedence mark on this line is not the first \
2173 to follow the previous rule.");
2174 psp->errorcnt++;
2175 }else{
2176 psp->prevrule->precsym = Symbol_new(x);
2177 }
2178 psp->state = PRECEDENCE_MARK_2;
2179 break;
2180 case PRECEDENCE_MARK_2:
2181 if( x[0]!=']' ){
2182 ErrorMsg(psp->filename,psp->tokenlineno,
2183 "Missing \"]\" on precedence mark.");
2184 psp->errorcnt++;
2185 }
2186 psp->state = WAITING_FOR_DECL_OR_RULE;
2187 break;
2188 case WAITING_FOR_ARROW:
2189 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2190 psp->state = IN_RHS;
2191 }else if( x[0]=='(' ){
2192 psp->state = LHS_ALIAS_1;
2193 }else{
2194 ErrorMsg(psp->filename,psp->tokenlineno,
2195 "Expected to see a \":\" following the LHS symbol \"%s\".",
2196 psp->lhs->name);
2197 psp->errorcnt++;
2198 psp->state = RESYNC_AFTER_RULE_ERROR;
2199 }
2200 break;
2201 case LHS_ALIAS_1:
2202 if( ISALPHA(x[0]) ){
2203 psp->lhsalias = x;
2204 psp->state = LHS_ALIAS_2;
2205 }else{
2206 ErrorMsg(psp->filename,psp->tokenlineno,
2207 "\"%s\" is not a valid alias for the LHS \"%s\"\n",
2208 x,psp->lhs->name);
2209 psp->errorcnt++;
2210 psp->state = RESYNC_AFTER_RULE_ERROR;
2211 }
2212 break;
2213 case LHS_ALIAS_2:
2214 if( x[0]==')' ){
2215 psp->state = LHS_ALIAS_3;
2216 }else{
2217 ErrorMsg(psp->filename,psp->tokenlineno,
2218 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2219 psp->errorcnt++;
2220 psp->state = RESYNC_AFTER_RULE_ERROR;
2221 }
2222 break;
2223 case LHS_ALIAS_3:
2224 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2225 psp->state = IN_RHS;
2226 }else{
2227 ErrorMsg(psp->filename,psp->tokenlineno,
2228 "Missing \"->\" following: \"%s(%s)\".",
2229 psp->lhs->name,psp->lhsalias);
2230 psp->errorcnt++;
2231 psp->state = RESYNC_AFTER_RULE_ERROR;
2232 }
2233 break;
2234 case IN_RHS:
2235 if( x[0]=='.' ){
2236 struct rule *rp;
2237 rp = (struct rule *)calloc( sizeof(struct rule) +
2238 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
2239 if( rp==0 ){
2240 ErrorMsg(psp->filename,psp->tokenlineno,
2241 "Can't allocate enough memory for this rule.");
2242 psp->errorcnt++;
2243 psp->prevrule = 0;
2244 }else{
2245 int i;
2246 rp->ruleline = psp->tokenlineno;
2247 rp->rhs = (struct symbol**)&rp[1];
2248 rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
2249 for(i=0; i<psp->nrhs; i++){
2250 rp->rhs[i] = psp->rhs[i];
2251 rp->rhsalias[i] = psp->alias[i];
2252 }
2253 rp->lhs = psp->lhs;
2254 rp->lhsalias = psp->lhsalias;
2255 rp->nrhs = psp->nrhs;
2256 rp->code = 0;
2257 rp->precsym = 0;
2258 rp->index = psp->gp->nrule++;
2259 rp->nextlhs = rp->lhs->rule;
2260 rp->lhs->rule = rp;
2261 rp->next = 0;
2262 if( psp->firstrule==0 ){
2263 psp->firstrule = psp->lastrule = rp;
2264 }else{
2265 psp->lastrule->next = rp;
2266 psp->lastrule = rp;
2267 }
2268 psp->prevrule = rp;
2269 }
2270 psp->state = WAITING_FOR_DECL_OR_RULE;
2271 }else if( ISALPHA(x[0]) ){
2272 if( psp->nrhs>=MAXRHS ){
2273 ErrorMsg(psp->filename,psp->tokenlineno,
2274 "Too many symbols on RHS of rule beginning at \"%s\".",
2275 x);
2276 psp->errorcnt++;
2277 psp->state = RESYNC_AFTER_RULE_ERROR;
2278 }else{
2279 psp->rhs[psp->nrhs] = Symbol_new(x);
2280 psp->alias[psp->nrhs] = 0;
2281 psp->nrhs++;
2282 }
2283 }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
2284 struct symbol *msp = psp->rhs[psp->nrhs-1];
2285 if( msp->type!=MULTITERMINAL ){
2286 struct symbol *origsp = msp;
2287 msp = (struct symbol *) calloc(1,sizeof(*msp));
2288 memset(msp, 0, sizeof(*msp));
2289 msp->type = MULTITERMINAL;
2290 msp->nsubsym = 1;
2291 msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*));
2292 msp->subsym[0] = origsp;
2293 msp->name = origsp->name;
2294 psp->rhs[psp->nrhs-1] = msp;
2295 }
2296 msp->nsubsym++;
2297 msp->subsym = (struct symbol **) realloc(msp->subsym,
2298 sizeof(struct symbol*)*msp->nsubsym);
2299 msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
2300 if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){
2301 ErrorMsg(psp->filename,psp->tokenlineno,
2302 "Cannot form a compound containing a non-terminal");
2303 psp->errorcnt++;
2304 }
2305 }else if( x[0]=='(' && psp->nrhs>0 ){
2306 psp->state = RHS_ALIAS_1;
2307 }else{
2308 ErrorMsg(psp->filename,psp->tokenlineno,
2309 "Illegal character on RHS of rule: \"%s\".",x);
2310 psp->errorcnt++;
2311 psp->state = RESYNC_AFTER_RULE_ERROR;
2312 }
2313 break;
2314 case RHS_ALIAS_1:
2315 if( ISALPHA(x[0]) ){
2316 psp->alias[psp->nrhs-1] = x;
2317 psp->state = RHS_ALIAS_2;
2318 }else{
2319 ErrorMsg(psp->filename,psp->tokenlineno,
2320 "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
2321 x,psp->rhs[psp->nrhs-1]->name);
2322 psp->errorcnt++;
2323 psp->state = RESYNC_AFTER_RULE_ERROR;
2324 }
2325 break;
2326 case RHS_ALIAS_2:
2327 if( x[0]==')' ){
2328 psp->state = IN_RHS;
2329 }else{
2330 ErrorMsg(psp->filename,psp->tokenlineno,
2331 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2332 psp->errorcnt++;
2333 psp->state = RESYNC_AFTER_RULE_ERROR;
2334 }
2335 break;
2336 case WAITING_FOR_DECL_KEYWORD:
2337 if( ISALPHA(x[0]) ){
2338 psp->declkeyword = x;
2339 psp->declargslot = 0;
2340 psp->decllinenoslot = 0;
2341 psp->insertLineMacro = 1;
2342 psp->state = WAITING_FOR_DECL_ARG;
2343 if( strcmp(x,"name")==0 ){
2344 psp->declargslot = &(psp->gp->name);
2345 psp->insertLineMacro = 0;
2346 }else if( strcmp(x,"include")==0 ){
2347 psp->declargslot = &(psp->gp->include);
2348 }else if( strcmp(x,"code")==0 ){
2349 psp->declargslot = &(psp->gp->extracode);
2350 }else if( strcmp(x,"token_destructor")==0 ){
2351 psp->declargslot = &psp->gp->tokendest;
2352 }else if( strcmp(x,"default_destructor")==0 ){
2353 psp->declargslot = &psp->gp->vardest;
2354 }else if( strcmp(x,"token_prefix")==0 ){
2355 psp->declargslot = &psp->gp->tokenprefix;
2356 psp->insertLineMacro = 0;
2357 }else if( strcmp(x,"syntax_error")==0 ){
2358 psp->declargslot = &(psp->gp->error);
2359 }else if( strcmp(x,"parse_accept")==0 ){
2360 psp->declargslot = &(psp->gp->accept);
2361 }else if( strcmp(x,"parse_failure")==0 ){
2362 psp->declargslot = &(psp->gp->failure);
2363 }else if( strcmp(x,"stack_overflow")==0 ){
2364 psp->declargslot = &(psp->gp->overflow);
2365 }else if( strcmp(x,"extra_argument")==0 ){
2366 psp->declargslot = &(psp->gp->arg);
2367 psp->insertLineMacro = 0;
2368 }else if( strcmp(x,"token_type")==0 ){
2369 psp->declargslot = &(psp->gp->tokentype);
2370 psp->insertLineMacro = 0;
2371 }else if( strcmp(x,"default_type")==0 ){
2372 psp->declargslot = &(psp->gp->vartype);
2373 psp->insertLineMacro = 0;
2374 }else if( strcmp(x,"stack_size")==0 ){
2375 psp->declargslot = &(psp->gp->stacksize);
2376 psp->insertLineMacro = 0;
2377 }else if( strcmp(x,"start_symbol")==0 ){
2378 psp->declargslot = &(psp->gp->start);
2379 psp->insertLineMacro = 0;
2380 }else if( strcmp(x,"left")==0 ){
2381 psp->preccounter++;
2382 psp->declassoc = LEFT;
2383 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2384 }else if( strcmp(x,"right")==0 ){
2385 psp->preccounter++;
2386 psp->declassoc = RIGHT;
2387 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2388 }else if( strcmp(x,"nonassoc")==0 ){
2389 psp->preccounter++;
2390 psp->declassoc = NONE;
2391 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2392 }else if( strcmp(x,"destructor")==0 ){
2393 psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
2394 }else if( strcmp(x,"type")==0 ){
2395 psp->state = WAITING_FOR_DATATYPE_SYMBOL;
2396 }else if( strcmp(x,"fallback")==0 ){
2397 psp->fallback = 0;
2398 psp->state = WAITING_FOR_FALLBACK_ID;
2399 }else if( strcmp(x,"wildcard")==0 ){
2400 psp->state = WAITING_FOR_WILDCARD_ID;
2401 }else if( strcmp(x,"token_class")==0 ){
2402 psp->state = WAITING_FOR_CLASS_ID;
2403 }else{
2404 ErrorMsg(psp->filename,psp->tokenlineno,
2405 "Unknown declaration keyword: \"%%%s\".",x);
2406 psp->errorcnt++;
2407 psp->state = RESYNC_AFTER_DECL_ERROR;
2408 }
2409 }else{
2410 ErrorMsg(psp->filename,psp->tokenlineno,
2411 "Illegal declaration keyword: \"%s\".",x);
2412 psp->errorcnt++;
2413 psp->state = RESYNC_AFTER_DECL_ERROR;
2414 }
2415 break;
2416 case WAITING_FOR_DESTRUCTOR_SYMBOL:
2417 if( !ISALPHA(x[0]) ){
2418 ErrorMsg(psp->filename,psp->tokenlineno,
2419 "Symbol name missing after %%destructor keyword");
2420 psp->errorcnt++;
2421 psp->state = RESYNC_AFTER_DECL_ERROR;
2422 }else{
2423 struct symbol *sp = Symbol_new(x);
2424 psp->declargslot = &sp->destructor;
2425 psp->decllinenoslot = &sp->destLineno;
2426 psp->insertLineMacro = 1;
2427 psp->state = WAITING_FOR_DECL_ARG;
2428 }
2429 break;
2430 case WAITING_FOR_DATATYPE_SYMBOL:
2431 if( !ISALPHA(x[0]) ){
2432 ErrorMsg(psp->filename,psp->tokenlineno,
2433 "Symbol name missing after %%type keyword");
2434 psp->errorcnt++;
2435 psp->state = RESYNC_AFTER_DECL_ERROR;
2436 }else{
2437 struct symbol *sp = Symbol_find(x);
2438 if((sp) && (sp->datatype)){
2439 ErrorMsg(psp->filename,psp->tokenlineno,
2440 "Symbol %%type \"%s\" already defined", x);
2441 psp->errorcnt++;
2442 psp->state = RESYNC_AFTER_DECL_ERROR;
2443 }else{
2444 if (!sp){
2445 sp = Symbol_new(x);
2446 }
2447 psp->declargslot = &sp->datatype;
2448 psp->insertLineMacro = 0;
2449 psp->state = WAITING_FOR_DECL_ARG;
2450 }
2451 }
2452 break;
2453 case WAITING_FOR_PRECEDENCE_SYMBOL:
2454 if( x[0]=='.' ){
2455 psp->state = WAITING_FOR_DECL_OR_RULE;
2456 }else if( ISUPPER(x[0]) ){
2457 struct symbol *sp;
2458 sp = Symbol_new(x);
2459 if( sp->prec>=0 ){
2460 ErrorMsg(psp->filename,psp->tokenlineno,
2461 "Symbol \"%s\" has already be given a precedence.",x);
2462 psp->errorcnt++;
2463 }else{
2464 sp->prec = psp->preccounter;
2465 sp->assoc = psp->declassoc;
2466 }
2467 }else{
2468 ErrorMsg(psp->filename,psp->tokenlineno,
2469 "Can't assign a precedence to \"%s\".",x);
2470 psp->errorcnt++;
2471 }
2472 break;
2473 case WAITING_FOR_DECL_ARG:
2474 if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){
2475 const char *zOld, *zNew;
2476 char *zBuf, *z;
2477 int nOld, n, nLine = 0, nNew, nBack;
2478 int addLineMacro;
2479 char zLine[50];
2480 zNew = x;
2481 if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
2482 nNew = lemonStrlen(zNew);
2483 if( *psp->declargslot ){
2484 zOld = *psp->declargslot;
2485 }else{
2486 zOld = "";
2487 }
2488 nOld = lemonStrlen(zOld);
2489 n = nOld + nNew + 20;
2490 addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro &&
2491 (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
2492 if( addLineMacro ){
2493 for(z=psp->filename, nBack=0; *z; z++){
2494 if( *z=='\\' ) nBack++;
2495 }
2496 lemon_sprintf(zLine, "#line %d ", psp->tokenlineno);
2497 nLine = lemonStrlen(zLine);
2498 n += nLine + lemonStrlen(psp->filename) + nBack;
2499 }
2500 *psp->declargslot = (char *) realloc(*psp->declargslot, n);
2501 zBuf = *psp->declargslot + nOld;
2502 if( addLineMacro ){
2503 if( nOld && zBuf[-1]!='\n' ){
2504 *(zBuf++) = '\n';
2505 }
2506 memcpy(zBuf, zLine, nLine);
2507 zBuf += nLine;
2508 *(zBuf++) = '"';
2509 for(z=psp->filename; *z; z++){
2510 if( *z=='\\' ){
2511 *(zBuf++) = '\\';
2512 }
2513 *(zBuf++) = *z;
2514 }
2515 *(zBuf++) = '"';
2516 *(zBuf++) = '\n';
2517 }
2518 if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){
2519 psp->decllinenoslot[0] = psp->tokenlineno;
2520 }
2521 memcpy(zBuf, zNew, nNew);
2522 zBuf += nNew;
2523 *zBuf = 0;
2524 psp->state = WAITING_FOR_DECL_OR_RULE;
2525 }else{
2526 ErrorMsg(psp->filename,psp->tokenlineno,
2527 "Illegal argument to %%%s: %s",psp->declkeyword,x);
2528 psp->errorcnt++;
2529 psp->state = RESYNC_AFTER_DECL_ERROR;
2530 }
2531 break;
2532 case WAITING_FOR_FALLBACK_ID:
2533 if( x[0]=='.' ){
2534 psp->state = WAITING_FOR_DECL_OR_RULE;
2535 }else if( !ISUPPER(x[0]) ){
2536 ErrorMsg(psp->filename, psp->tokenlineno,
2537 "%%fallback argument \"%s\" should be a token", x);
2538 psp->errorcnt++;
2539 }else{
2540 struct symbol *sp = Symbol_new(x);
2541 if( psp->fallback==0 ){
2542 psp->fallback = sp;
2543 }else if( sp->fallback ){
2544 ErrorMsg(psp->filename, psp->tokenlineno,
2545 "More than one fallback assigned to token %s", x);
2546 psp->errorcnt++;
2547 }else{
2548 sp->fallback = psp->fallback;
2549 psp->gp->has_fallback = 1;
2550 }
2551 }
2552 break;
2553 case WAITING_FOR_WILDCARD_ID:
2554 if( x[0]=='.' ){
2555 psp->state = WAITING_FOR_DECL_OR_RULE;
2556 }else if( !ISUPPER(x[0]) ){
2557 ErrorMsg(psp->filename, psp->tokenlineno,
2558 "%%wildcard argument \"%s\" should be a token", x);
2559 psp->errorcnt++;
2560 }else{
2561 struct symbol *sp = Symbol_new(x);
2562 if( psp->gp->wildcard==0 ){
2563 psp->gp->wildcard = sp;
2564 }else{
2565 ErrorMsg(psp->filename, psp->tokenlineno,
2566 "Extra wildcard to token: %s", x);
2567 psp->errorcnt++;
2568 }
2569 }
2570 break;
2571 case WAITING_FOR_CLASS_ID:
2572 if( !ISLOWER(x[0]) ){
2573 ErrorMsg(psp->filename, psp->tokenlineno,
2574 "%%token_class must be followed by an identifier: ", x);
2575 psp->errorcnt++;
2576 psp->state = RESYNC_AFTER_DECL_ERROR;
2577 }else if( Symbol_find(x) ){
2578 ErrorMsg(psp->filename, psp->tokenlineno,
2579 "Symbol \"%s\" already used", x);
2580 psp->errorcnt++;
2581 psp->state = RESYNC_AFTER_DECL_ERROR;
2582 }else{
2583 psp->tkclass = Symbol_new(x);
2584 psp->tkclass->type = MULTITERMINAL;
2585 psp->state = WAITING_FOR_CLASS_TOKEN;
2586 }
2587 break;
2588 case WAITING_FOR_CLASS_TOKEN:
2589 if( x[0]=='.' ){
2590 psp->state = WAITING_FOR_DECL_OR_RULE;
2591 }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){
2592 struct symbol *msp = psp->tkclass;
2593 msp->nsubsym++;
2594 msp->subsym = (struct symbol **) realloc(msp->subsym,
2595 sizeof(struct symbol*)*msp->nsubsym);
2596 if( !ISUPPER(x[0]) ) x++;
2597 msp->subsym[msp->nsubsym-1] = Symbol_new(x);
2598 }else{
2599 ErrorMsg(psp->filename, psp->tokenlineno,
2600 "%%token_class argument \"%s\" should be a token", x);
2601 psp->errorcnt++;
2602 psp->state = RESYNC_AFTER_DECL_ERROR;
2603 }
2604 break;
2605 case RESYNC_AFTER_RULE_ERROR:
2606 /* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2607 ** break; */
2608 case RESYNC_AFTER_DECL_ERROR:
2609 if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2610 if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
2611 break;
2612 }
2613 }
2614
2615 /* Run the preprocessor over the input file text. The global variables
2616 ** azDefine[0] through azDefine[nDefine-1] contains the names of all defined
2617 ** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and
2618 ** comments them out. Text in between is also commented out as appropriate.
2619 */
2620 static void preprocess_input(char *z){
2621 int i, j, k, n;
2622 int exclude = 0;
2623 int start = 0;
2624 int lineno = 1;
2625 int start_lineno = 1;
2626 for(i=0; z[i]; i++){
2627 if( z[i]=='\n' ) lineno++;
2628 if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue;
2629 if( strncmp(&z[i],"%endif",6)==0 && ISSPACE(z[i+6]) ){
2630 if( exclude ){
2631 exclude--;
2632 if( exclude==0 ){
2633 for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
2634 }
2635 }
2636 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2637 }else if( (strncmp(&z[i],"%ifdef",6)==0 && ISSPACE(z[i+6]))
2638 || (strncmp(&z[i],"%ifndef",7)==0 && ISSPACE(z[i+7])) ){
2639 if( exclude ){
2640 exclude++;
2641 }else{
2642 for(j=i+7; ISSPACE(z[j]); j++){}
2643 for(n=0; z[j+n] && !ISSPACE(z[j+n]); n++){}
2644 exclude = 1;
2645 for(k=0; k<nDefine; k++){
2646 if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){
2647 exclude = 0;
2648 break;
2649 }
2650 }
2651 if( z[i+3]=='n' ) exclude = !exclude;
2652 if( exclude ){
2653 start = i;
2654 start_lineno = lineno;
2655 }
2656 }
2657 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2658 }
2659 }
2660 if( exclude ){
2661 fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
2662 exit(1);
2663 }
2664 }
2665
2666 /* In spite of its name, this function is really a scanner. It read
2667 ** in the entire input file (all at once) then tokenizes it. Each
2668 ** token is passed to the function "parseonetoken" which builds all
2669 ** the appropriate data structures in the global state vector "gp".
2670 */
2671 void Parse(struct lemon *gp)
2672 {
2673 struct pstate ps;
2674 FILE *fp;
2675 char *filebuf;
2676 unsigned int filesize;
2677 int lineno;
2678 int c;
2679 char *cp, *nextcp;
2680 int startline = 0;
2681
2682 memset(&ps, '\0', sizeof(ps));
2683 ps.gp = gp;
2684 ps.filename = gp->filename;
2685 ps.errorcnt = 0;
2686 ps.state = INITIALIZE;
2687
2688 /* Begin by reading the input file */
2689 fp = fopen(ps.filename,"rb");
2690 if( fp==0 ){
2691 ErrorMsg(ps.filename,0,"Can't open this file for reading.");
2692 gp->errorcnt++;
2693 return;
2694 }
2695 fseek(fp,0,2);
2696 filesize = ftell(fp);
2697 rewind(fp);
2698 filebuf = (char *)malloc( filesize+1 );
2699 if( filesize>100000000 || filebuf==0 ){
2700 ErrorMsg(ps.filename,0,"Input file too large.");
2701 gp->errorcnt++;
2702 fclose(fp);
2703 return;
2704 }
2705 if( fread(filebuf,1,filesize,fp)!=filesize ){
2706 ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
2707 filesize);
2708 free(filebuf);
2709 gp->errorcnt++;
2710 fclose(fp);
2711 return;
2712 }
2713 fclose(fp);
2714 filebuf[filesize] = 0;
2715
2716 /* Make an initial pass through the file to handle %ifdef and %ifndef */
2717 preprocess_input(filebuf);
2718
2719 /* Now scan the text of the input file */
2720 lineno = 1;
2721 for(cp=filebuf; (c= *cp)!=0; ){
2722 if( c=='\n' ) lineno++; /* Keep track of the line number */
2723 if( ISSPACE(c) ){ cp++; continue; } /* Skip all white space */
2724 if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
2725 cp+=2;
2726 while( (c= *cp)!=0 && c!='\n' ) cp++;
2727 continue;
2728 }
2729 if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
2730 cp+=2;
2731 while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
2732 if( c=='\n' ) lineno++;
2733 cp++;
2734 }
2735 if( c ) cp++;
2736 continue;
2737 }
2738 ps.tokenstart = cp; /* Mark the beginning of the token */
2739 ps.tokenlineno = lineno; /* Linenumber on which token begins */
2740 if( c=='\"' ){ /* String literals */
2741 cp++;
2742 while( (c= *cp)!=0 && c!='\"' ){
2743 if( c=='\n' ) lineno++;
2744 cp++;
2745 }
2746 if( c==0 ){
2747 ErrorMsg(ps.filename,startline,
2748 "String starting on this line is not terminated before the end of the file.");
2749 ps.errorcnt++;
2750 nextcp = cp;
2751 }else{
2752 nextcp = cp+1;
2753 }
2754 }else if( c=='{' ){ /* A block of C code */
2755 int level;
2756 cp++;
2757 for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
2758 if( c=='\n' ) lineno++;
2759 else if( c=='{' ) level++;
2760 else if( c=='}' ) level--;
2761 else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
2762 int prevc;
2763 cp = &cp[2];
2764 prevc = 0;
2765 while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
2766 if( c=='\n' ) lineno++;
2767 prevc = c;
2768 cp++;
2769 }
2770 }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
2771 cp = &cp[2];
2772 while( (c= *cp)!=0 && c!='\n' ) cp++;
2773 if( c ) lineno++;
2774 }else if( c=='\'' || c=='\"' ){ /* String a character literals */
2775 int startchar, prevc;
2776 startchar = c;
2777 prevc = 0;
2778 for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
2779 if( c=='\n' ) lineno++;
2780 if( prevc=='\\' ) prevc = 0;
2781 else prevc = c;
2782 }
2783 }
2784 }
2785 if( c==0 ){
2786 ErrorMsg(ps.filename,ps.tokenlineno,
2787 "C code starting on this line is not terminated before the end of the file.");
2788 ps.errorcnt++;
2789 nextcp = cp;
2790 }else{
2791 nextcp = cp+1;
2792 }
2793 }else if( ISALNUM(c) ){ /* Identifiers */
2794 while( (c= *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
2795 nextcp = cp;
2796 }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
2797 cp += 3;
2798 nextcp = cp;
2799 }else if( (c=='/' || c=='|') && ISALPHA(cp[1]) ){
2800 cp += 2;
2801 while( (c = *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
2802 nextcp = cp;
2803 }else{ /* All other (one character) operators */
2804 cp++;
2805 nextcp = cp;
2806 }
2807 c = *cp;
2808 *cp = 0; /* Null terminate the token */
2809 parseonetoken(&ps); /* Parse the token */
2810 *cp = (char)c; /* Restore the buffer */
2811 cp = nextcp;
2812 }
2813 free(filebuf); /* Release the buffer after parsing */
2814 gp->rule = ps.firstrule;
2815 gp->errorcnt = ps.errorcnt;
2816 }
2817 /*************************** From the file "plink.c" *********************/
2818 /*
2819 ** Routines processing configuration follow-set propagation links
2820 ** in the LEMON parser generator.
2821 */
2822 static struct plink *plink_freelist = 0;
2823
2824 /* Allocate a new plink */
2825 struct plink *Plink_new(){
2826 struct plink *newlink;
2827
2828 if( plink_freelist==0 ){
2829 int i;
2830 int amt = 100;
2831 plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
2832 if( plink_freelist==0 ){
2833 fprintf(stderr,
2834 "Unable to allocate memory for a new follow-set propagation link.\n");
2835 exit(1);
2836 }
2837 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2838 plink_freelist[amt-1].next = 0;
2839 }
2840 newlink = plink_freelist;
2841 plink_freelist = plink_freelist->next;
2842 return newlink;
2843 }
2844
2845 /* Add a plink to a plink list */
2846 void Plink_add(struct plink **plpp, struct config *cfp)
2847 {
2848 struct plink *newlink;
2849 newlink = Plink_new();
2850 newlink->next = *plpp;
2851 *plpp = newlink;
2852 newlink->cfp = cfp;
2853 }
2854
2855 /* Transfer every plink on the list "from" to the list "to" */
2856 void Plink_copy(struct plink **to, struct plink *from)
2857 {
2858 struct plink *nextpl;
2859 while( from ){
2860 nextpl = from->next;
2861 from->next = *to;
2862 *to = from;
2863 from = nextpl;
2864 }
2865 }
2866
2867 /* Delete every plink on the list */
2868 void Plink_delete(struct plink *plp)
2869 {
2870 struct plink *nextpl;
2871
2872 while( plp ){
2873 nextpl = plp->next;
2874 plp->next = plink_freelist;
2875 plink_freelist = plp;
2876 plp = nextpl;
2877 }
2878 }
2879 /*********************** From the file "report.c" **************************/
2880 /*
2881 ** Procedures for generating reports and tables in the LEMON parser generator.
2882 */
2883
2884 /* Generate a filename with the given suffix. Space to hold the
2885 ** name comes from malloc() and must be freed by the calling
2886 ** function.
2887 */
2888 PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
2889 {
2890 char *name;
2891 char *cp;
2892
2893 name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
2894 if( name==0 ){
2895 fprintf(stderr,"Can't allocate space for a filename.\n");
2896 exit(1);
2897 }
2898 lemon_strcpy(name,lemp->filename);
2899 cp = strrchr(name,'.');
2900 if( cp ) *cp = 0;
2901 lemon_strcat(name,suffix);
2902 return name;
2903 }
2904
2905 /* Open a file with a name based on the name of the input file,
2906 ** but with a different (specified) suffix, and return a pointer
2907 ** to the stream */
2908 PRIVATE FILE *file_open(
2909 struct lemon *lemp,
2910 const char *suffix,
2911 const char *mode
2912 ){
2913 FILE *fp;
2914
2915 if( lemp->outname ) free(lemp->outname);
2916 lemp->outname = file_makename(lemp, suffix);
2917 fp = fopen(lemp->outname,mode);
2918 if( fp==0 && *mode=='w' ){
2919 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
2920 lemp->errorcnt++;
2921 return 0;
2922 }
2923 return fp;
2924 }
2925
2926 /* Duplicate the input file without comments and without actions
2927 ** on rules */
2928 void Reprint(struct lemon *lemp)
2929 {
2930 struct rule *rp;
2931 struct symbol *sp;
2932 int i, j, maxlen, len, ncolumns, skip;
2933 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
2934 maxlen = 10;
2935 for(i=0; i<lemp->nsymbol; i++){
2936 sp = lemp->symbols[i];
2937 len = lemonStrlen(sp->name);
2938 if( len>maxlen ) maxlen = len;
2939 }
2940 ncolumns = 76/(maxlen+5);
2941 if( ncolumns<1 ) ncolumns = 1;
2942 skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
2943 for(i=0; i<skip; i++){
2944 printf("//");
2945 for(j=i; j<lemp->nsymbol; j+=skip){
2946 sp = lemp->symbols[j];
2947 assert( sp->index==j );
2948 printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
2949 }
2950 printf("\n");
2951 }
2952 for(rp=lemp->rule; rp; rp=rp->next){
2953 printf("%s",rp->lhs->name);
2954 /* if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
2955 printf(" ::=");
2956 for(i=0; i<rp->nrhs; i++){
2957 sp = rp->rhs[i];
2958 if( sp->type==MULTITERMINAL ){
2959 printf(" %s", sp->subsym[0]->name);
2960 for(j=1; j<sp->nsubsym; j++){
2961 printf("|%s", sp->subsym[j]->name);
2962 }
2963 }else{
2964 printf(" %s", sp->name);
2965 }
2966 /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
2967 }
2968 printf(".");
2969 if( rp->precsym ) printf(" [%s]",rp->precsym->name);
2970 /* if( rp->code ) printf("\n %s",rp->code); */
2971 printf("\n");
2972 }
2973 }
2974
2975 /* Print a single rule.
2976 */
2977 void RulePrint(FILE *fp, struct rule *rp, int iCursor){
2978 struct symbol *sp;
2979 int i, j;
2980 fprintf(fp,"%s ::=",rp->lhs->name);
2981 for(i=0; i<=rp->nrhs; i++){
2982 if( i==iCursor ) fprintf(fp," *");
2983 if( i==rp->nrhs ) break;
2984 sp = rp->rhs[i];
2985 if( sp->type==MULTITERMINAL ){
2986 fprintf(fp," %s", sp->subsym[0]->name);
2987 for(j=1; j<sp->nsubsym; j++){
2988 fprintf(fp,"|%s",sp->subsym[j]->name);
2989 }
2990 }else{
2991 fprintf(fp," %s", sp->name);
2992 }
2993 }
2994 }
2995
2996 /* Print the rule for a configuration.
2997 */
2998 void ConfigPrint(FILE *fp, struct config *cfp){
2999 RulePrint(fp, cfp->rp, cfp->dot);
3000 }
3001
3002 /* #define TEST */
3003 #if 0
3004 /* Print a set */
3005 PRIVATE void SetPrint(out,set,lemp)
3006 FILE *out;
3007 char *set;
3008 struct lemon *lemp;
3009 {
3010 int i;
3011 char *spacer;
3012 spacer = "";
3013 fprintf(out,"%12s[","");
3014 for(i=0; i<lemp->nterminal; i++){
3015 if( SetFind(set,i) ){
3016 fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
3017 spacer = " ";
3018 }
3019 }
3020 fprintf(out,"]\n");
3021 }
3022
3023 /* Print a plink chain */
3024 PRIVATE void PlinkPrint(out,plp,tag)
3025 FILE *out;
3026 struct plink *plp;
3027 char *tag;
3028 {
3029 while( plp ){
3030 fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum);
3031 ConfigPrint(out,plp->cfp);
3032 fprintf(out,"\n");
3033 plp = plp->next;
3034 }
3035 }
3036 #endif
3037
3038 /* Print an action to the given file descriptor. Return FALSE if
3039 ** nothing was actually printed.
3040 */
3041 int PrintAction(
3042 struct action *ap, /* The action to print */
3043 FILE *fp, /* Print the action here */
3044 int indent /* Indent by this amount */
3045 ){
3046 int result = 1;
3047 switch( ap->type ){
3048 case SHIFT: {
3049 struct state *stp = ap->x.stp;
3050 fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum);
3051 break;
3052 }
3053 case REDUCE: {
3054 struct rule *rp = ap->x.rp;
3055 fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->index);
3056 RulePrint(fp, rp, -1);
3057 break;
3058 }
3059 case SHIFTREDUCE: {
3060 struct rule *rp = ap->x.rp;
3061 fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->index);
3062 RulePrint(fp, rp, -1);
3063 break;
3064 }
3065 case ACCEPT:
3066 fprintf(fp,"%*s accept",indent,ap->sp->name);
3067 break;
3068 case ERROR:
3069 fprintf(fp,"%*s error",indent,ap->sp->name);
3070 break;
3071 case SRCONFLICT:
3072 case RRCONFLICT:
3073 fprintf(fp,"%*s reduce %-7d ** Parsing conflict **",
3074 indent,ap->sp->name,ap->x.rp->index);
3075 break;
3076 case SSCONFLICT:
3077 fprintf(fp,"%*s shift %-7d ** Parsing conflict **",
3078 indent,ap->sp->name,ap->x.stp->statenum);
3079 break;
3080 case SH_RESOLVED:
3081 if( showPrecedenceConflict ){
3082 fprintf(fp,"%*s shift %-7d -- dropped by precedence",
3083 indent,ap->sp->name,ap->x.stp->statenum);
3084 }else{
3085 result = 0;
3086 }
3087 break;
3088 case RD_RESOLVED:
3089 if( showPrecedenceConflict ){
3090 fprintf(fp,"%*s reduce %-7d -- dropped by precedence",
3091 indent,ap->sp->name,ap->x.rp->index);
3092 }else{
3093 result = 0;
3094 }
3095 break;
3096 case NOT_USED:
3097 result = 0;
3098 break;
3099 }
3100 return result;
3101 }
3102
3103 /* Generate the "*.out" log file */
3104 void ReportOutput(struct lemon *lemp)
3105 {
3106 int i;
3107 struct state *stp;
3108 struct config *cfp;
3109 struct action *ap;
3110 FILE *fp;
3111
3112 fp = file_open(lemp,".out","wb");
3113 if( fp==0 ) return;
3114 for(i=0; i<lemp->nxstate; i++){
3115 stp = lemp->sorted[i];
3116 fprintf(fp,"State %d:\n",stp->statenum);
3117 if( lemp->basisflag ) cfp=stp->bp;
3118 else cfp=stp->cfp;
3119 while( cfp ){
3120 char buf[20];
3121 if( cfp->dot==cfp->rp->nrhs ){
3122 lemon_sprintf(buf,"(%d)",cfp->rp->index);
3123 fprintf(fp," %5s ",buf);
3124 }else{
3125 fprintf(fp," ");
3126 }
3127 ConfigPrint(fp,cfp);
3128 fprintf(fp,"\n");
3129 #if 0
3130 SetPrint(fp,cfp->fws,lemp);
3131 PlinkPrint(fp,cfp->fplp,"To ");
3132 PlinkPrint(fp,cfp->bplp,"From");
3133 #endif
3134 if( lemp->basisflag ) cfp=cfp->bp;
3135 else cfp=cfp->next;
3136 }
3137 fprintf(fp,"\n");
3138 for(ap=stp->ap; ap; ap=ap->next){
3139 if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
3140 }
3141 fprintf(fp,"\n");
3142 }
3143 fprintf(fp, "----------------------------------------------------\n");
3144 fprintf(fp, "Symbols:\n");
3145 for(i=0; i<lemp->nsymbol; i++){
3146 int j;
3147 struct symbol *sp;
3148
3149 sp = lemp->symbols[i];
3150 fprintf(fp, " %3d: %s", i, sp->name);
3151 if( sp->type==NONTERMINAL ){
3152 fprintf(fp, ":");
3153 if( sp->lambda ){
3154 fprintf(fp, " <lambda>");
3155 }
3156 for(j=0; j<lemp->nterminal; j++){
3157 if( sp->firstset && SetFind(sp->firstset, j) ){
3158 fprintf(fp, " %s", lemp->symbols[j]->name);
3159 }
3160 }
3161 }
3162 fprintf(fp, "\n");
3163 }
3164 fclose(fp);
3165 return;
3166 }
3167
3168 /* Search for the file "name" which is in the same directory as
3169 ** the exacutable */
3170 PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
3171 {
3172 const char *pathlist;
3173 char *pathbufptr;
3174 char *pathbuf;
3175 char *path,*cp;
3176 char c;
3177
3178 #ifdef __WIN32__
3179 cp = strrchr(argv0,'\\');
3180 #else
3181 cp = strrchr(argv0,'/');
3182 #endif
3183 if( cp ){
3184 c = *cp;
3185 *cp = 0;
3186 path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
3187 if( path ) lemon_sprintf(path,"%s/%s",argv0,name);
3188 *cp = c;
3189 }else{
3190 pathlist = getenv("PATH");
3191 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
3192 pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
3193 path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
3194 if( (pathbuf != 0) && (path!=0) ){
3195 pathbufptr = pathbuf;
3196 lemon_strcpy(pathbuf, pathlist);
3197 while( *pathbuf ){
3198 cp = strchr(pathbuf,':');
3199 if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
3200 c = *cp;
3201 *cp = 0;
3202 lemon_sprintf(path,"%s/%s",pathbuf,name);
3203 *cp = c;
3204 if( c==0 ) pathbuf[0] = 0;
3205 else pathbuf = &cp[1];
3206 if( access(path,modemask)==0 ) break;
3207 }
3208 free(pathbufptr);
3209 }
3210 }
3211 return path;
3212 }
3213
3214 /* Given an action, compute the integer value for that action
3215 ** which is to be put in the action table of the generated machine.
3216 ** Return negative if no action should be generated.
3217 */
3218 PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
3219 {
3220 int act;
3221 switch( ap->type ){
3222 case SHIFT: act = ap->x.stp->statenum; break;
3223 case SHIFTREDUCE: act = ap->x.rp->index + lemp->nstate; break;
3224 case REDUCE: act = ap->x.rp->index + lemp->nstate+lemp->nrule; break;
3225 case ERROR: act = lemp->nstate + lemp->nrule*2; break;
3226 case ACCEPT: act = lemp->nstate + lemp->nrule*2 + 1; break;
3227 default: act = -1; break;
3228 }
3229 return act;
3230 }
3231
3232 #define LINESIZE 1000
3233 /* The next cluster of routines are for reading the template file
3234 ** and writing the results to the generated parser */
3235 /* The first function transfers data from "in" to "out" until
3236 ** a line is seen which begins with "%%". The line number is
3237 ** tracked.
3238 **
3239 ** if name!=0, then any word that begin with "Parse" is changed to
3240 ** begin with *name instead.
3241 */
3242 PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
3243 {
3244 int i, iStart;
3245 char line[LINESIZE];
3246 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
3247 (*lineno)++;
3248 iStart = 0;
3249 if( name ){
3250 for(i=0; line[i]; i++){
3251 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
3252 && (i==0 || !ISALPHA(line[i-1]))
3253 ){
3254 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
3255 fprintf(out,"%s",name);
3256 i += 4;
3257 iStart = i+1;
3258 }
3259 }
3260 }
3261 fprintf(out,"%s",&line[iStart]);
3262 }
3263 }
3264
3265 /* The next function finds the template file and opens it, returning
3266 ** a pointer to the opened file. */
3267 PRIVATE FILE *tplt_open(struct lemon *lemp)
3268 {
3269 static char templatename[] = "lempar.c";
3270 char buf[1000];
3271 FILE *in;
3272 char *tpltname;
3273 char *cp;
3274
3275 /* first, see if user specified a template filename on the command line. */
3276 if (user_templatename != 0) {
3277 if( access(user_templatename,004)==-1 ){
3278 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3279 user_templatename);
3280 lemp->errorcnt++;
3281 return 0;
3282 }
3283 in = fopen(user_templatename,"rb");
3284 if( in==0 ){
3285 fprintf(stderr,"Can't open the template file \"%s\".\n",
3286 user_templatename);
3287 lemp->errorcnt++;
3288 return 0;
3289 }
3290 return in;
3291 }
3292
3293 cp = strrchr(lemp->filename,'.');
3294 if( cp ){
3295 lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
3296 }else{
3297 lemon_sprintf(buf,"%s.lt",lemp->filename);
3298 }
3299 if( access(buf,004)==0 ){
3300 tpltname = buf;
3301 }else if( access(templatename,004)==0 ){
3302 tpltname = templatename;
3303 }else{
3304 tpltname = pathsearch(lemp->argv0,templatename,0);
3305 }
3306 if( tpltname==0 ){
3307 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3308 templatename);
3309 lemp->errorcnt++;
3310 return 0;
3311 }
3312 in = fopen(tpltname,"rb");
3313 if( in==0 ){
3314 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
3315 lemp->errorcnt++;
3316 return 0;
3317 }
3318 return in;
3319 }
3320
3321 /* Print a #line directive line to the output file. */
3322 PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
3323 {
3324 fprintf(out,"#line %d \"",lineno);
3325 while( *filename ){
3326 if( *filename == '\\' ) putc('\\',out);
3327 putc(*filename,out);
3328 filename++;
3329 }
3330 fprintf(out,"\"\n");
3331 }
3332
3333 /* Print a string to the file and keep the linenumber up to date */
3334 PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
3335 {
3336 if( str==0 ) return;
3337 while( *str ){
3338 putc(*str,out);
3339 if( *str=='\n' ) (*lineno)++;
3340 str++;
3341 }
3342 if( str[-1]!='\n' ){
3343 putc('\n',out);
3344 (*lineno)++;
3345 }
3346 if (!lemp->nolinenosflag) {
3347 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3348 }
3349 return;
3350 }
3351
3352 /*
3353 ** The following routine emits code for the destructor for the
3354 ** symbol sp
3355 */
3356 void emit_destructor_code(
3357 FILE *out,
3358 struct symbol *sp,
3359 struct lemon *lemp,
3360 int *lineno
3361 ){
3362 char *cp = 0;
3363
3364 if( sp->type==TERMINAL ){
3365 cp = lemp->tokendest;
3366 if( cp==0 ) return;
3367 fprintf(out,"{\n"); (*lineno)++;
3368 }else if( sp->destructor ){
3369 cp = sp->destructor;
3370 fprintf(out,"{\n"); (*lineno)++;
3371 if( !lemp->nolinenosflag ){
3372 (*lineno)++;
3373 tplt_linedir(out,sp->destLineno,lemp->filename);
3374 }
3375 }else if( lemp->vardest ){
3376 cp = lemp->vardest;
3377 if( cp==0 ) return;
3378 fprintf(out,"{\n"); (*lineno)++;
3379 }else{
3380 assert( 0 ); /* Cannot happen */
3381 }
3382 for(; *cp; cp++){
3383 if( *cp=='$' && cp[1]=='$' ){
3384 fprintf(out,"(yypminor->yy%d)",sp->dtnum);
3385 cp++;
3386 continue;
3387 }
3388 if( *cp=='\n' ) (*lineno)++;
3389 fputc(*cp,out);
3390 }
3391 fprintf(out,"\n"); (*lineno)++;
3392 if (!lemp->nolinenosflag) {
3393 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3394 }
3395 fprintf(out,"}\n"); (*lineno)++;
3396 return;
3397 }
3398
3399 /*
3400 ** Return TRUE (non-zero) if the given symbol has a destructor.
3401 */
3402 int has_destructor(struct symbol *sp, struct lemon *lemp)
3403 {
3404 int ret;
3405 if( sp->type==TERMINAL ){
3406 ret = lemp->tokendest!=0;
3407 }else{
3408 ret = lemp->vardest!=0 || sp->destructor!=0;
3409 }
3410 return ret;
3411 }
3412
3413 /*
3414 ** Append text to a dynamically allocated string. If zText is 0 then
3415 ** reset the string to be empty again. Always return the complete text
3416 ** of the string (which is overwritten with each call).
3417 **
3418 ** n bytes of zText are stored. If n==0 then all of zText up to the first
3419 ** \000 terminator is stored. zText can contain up to two instances of
3420 ** %d. The values of p1 and p2 are written into the first and second
3421 ** %d.
3422 **
3423 ** If n==-1, then the previous character is overwritten.
3424 */
3425 PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
3426 static char empty[1] = { 0 };
3427 static char *z = 0;
3428 static int alloced = 0;
3429 static int used = 0;
3430 int c;
3431 char zInt[40];
3432 if( zText==0 ){
3433 used = 0;
3434 return z;
3435 }
3436 if( n<=0 ){
3437 if( n<0 ){
3438 used += n;
3439 assert( used>=0 );
3440 }
3441 n = lemonStrlen(zText);
3442 }
3443 if( (int) (n+sizeof(zInt)*2+used) >= alloced ){
3444 alloced = n + sizeof(zInt)*2 + used + 200;
3445 z = (char *) realloc(z, alloced);
3446 }
3447 if( z==0 ) return empty;
3448 while( n-- > 0 ){
3449 c = *(zText++);
3450 if( c=='%' && n>0 && zText[0]=='d' ){
3451 lemon_sprintf(zInt, "%d", p1);
3452 p1 = p2;
3453 lemon_strcpy(&z[used], zInt);
3454 used += lemonStrlen(&z[used]);
3455 zText++;
3456 n--;
3457 }else{
3458 z[used++] = (char)c;
3459 }
3460 }
3461 z[used] = 0;
3462 return z;
3463 }
3464
3465 /*
3466 ** zCode is a string that is the action associated with a rule. Expand
3467 ** the symbols in this string so that the refer to elements of the parser
3468 ** stack.
3469 */
3470 PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){
3471 char *cp, *xp;
3472 int i;
3473 char lhsused = 0; /* True if the LHS element has been used */
3474 char used[MAXRHS]; /* True for each RHS element which is used */
3475
3476 for(i=0; i<rp->nrhs; i++) used[i] = 0;
3477 lhsused = 0;
3478
3479 if( rp->code==0 ){
3480 static char newlinestr[2] = { '\n', '\0' };
3481 rp->code = newlinestr;
3482 rp->line = rp->ruleline;
3483 }
3484
3485 append_str(0,0,0,0);
3486
3487 /* This const cast is wrong but harmless, if we're careful. */
3488 for(cp=(char *)rp->code; *cp; cp++){
3489 if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){
3490 char saved;
3491 for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++);
3492 saved = *xp;
3493 *xp = 0;
3494 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
3495 append_str("yygotominor.yy%d",0,rp->lhs->dtnum,0);
3496 cp = xp;
3497 lhsused = 1;
3498 }else{
3499 for(i=0; i<rp->nrhs; i++){
3500 if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
3501 if( cp!=rp->code && cp[-1]=='@' ){
3502 /* If the argument is of the form @X then substituted
3503 ** the token number of X, not the value of X */
3504 append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0);
3505 }else{
3506 struct symbol *sp = rp->rhs[i];
3507 int dtnum;
3508 if( sp->type==MULTITERMINAL ){
3509 dtnum = sp->subsym[0]->dtnum;
3510 }else{
3511 dtnum = sp->dtnum;
3512 }
3513 append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum);
3514 }
3515 cp = xp;
3516 used[i] = 1;
3517 break;
3518 }
3519 }
3520 }
3521 *xp = saved;
3522 }
3523 append_str(cp, 1, 0, 0);
3524 } /* End loop */
3525
3526 /* Check to make sure the LHS has been used */
3527 if( rp->lhsalias && !lhsused ){
3528 ErrorMsg(lemp->filename,rp->ruleline,
3529 "Label \"%s\" for \"%s(%s)\" is never used.",
3530 rp->lhsalias,rp->lhs->name,rp->lhsalias);
3531 lemp->errorcnt++;
3532 }
3533
3534 /* Generate destructor code for RHS symbols which are not used in the
3535 ** reduce code */
3536 for(i=0; i<rp->nrhs; i++){
3537 if( rp->rhsalias[i] && !used[i] ){
3538 ErrorMsg(lemp->filename,rp->ruleline,
3539 "Label %s for \"%s(%s)\" is never used.",
3540 rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
3541 lemp->errorcnt++;
3542 }else if( rp->rhsalias[i]==0 ){
3543 if( has_destructor(rp->rhs[i],lemp) ){
3544 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
3545 rp->rhs[i]->index,i-rp->nrhs+1);
3546 }else{
3547 /* No destructor defined for this term */
3548 }
3549 }
3550 }
3551 if( rp->code ){
3552 cp = append_str(0,0,0,0);
3553 rp->code = Strsafe(cp?cp:"");
3554 }
3555 }
3556
3557 /*
3558 ** Generate code which executes when the rule "rp" is reduced. Write
3559 ** the code to "out". Make sure lineno stays up-to-date.
3560 */
3561 PRIVATE void emit_code(
3562 FILE *out,
3563 struct rule *rp,
3564 struct lemon *lemp,
3565 int *lineno
3566 ){
3567 const char *cp;
3568
3569 /* Generate code to do the reduce action */
3570 if( rp->code ){
3571 if( !lemp->nolinenosflag ){
3572 (*lineno)++;
3573 tplt_linedir(out,rp->line,lemp->filename);
3574 }
3575 fprintf(out,"{%s",rp->code);
3576 for(cp=rp->code; *cp; cp++){
3577 if( *cp=='\n' ) (*lineno)++;
3578 } /* End loop */
3579 fprintf(out,"}\n"); (*lineno)++;
3580 if( !lemp->nolinenosflag ){
3581 (*lineno)++;
3582 tplt_linedir(out,*lineno,lemp->outname);
3583 }
3584 } /* End if( rp->code ) */
3585
3586 return;
3587 }
3588
3589 /*
3590 ** Print the definition of the union used for the parser's data stack.
3591 ** This union contains fields for every possible data type for tokens
3592 ** and nonterminals. In the process of computing and printing this
3593 ** union, also set the ".dtnum" field of every terminal and nonterminal
3594 ** symbol.
3595 */
3596 void print_stack_union(
3597 FILE *out, /* The output stream */
3598 struct lemon *lemp, /* The main info structure for this parser */
3599 int *plineno, /* Pointer to the line number */
3600 int mhflag /* True if generating makeheaders output */
3601 ){
3602 int lineno = *plineno; /* The line number of the output */
3603 char **types; /* A hash table of datatypes */
3604 int arraysize; /* Size of the "types" array */
3605 int maxdtlength; /* Maximum length of any ".datatype" field. */
3606 char *stddt; /* Standardized name for a datatype */
3607 int i,j; /* Loop counters */
3608 unsigned hash; /* For hashing the name of a type */
3609 const char *name; /* Name of the parser */
3610
3611 /* Allocate and initialize types[] and allocate stddt[] */
3612 arraysize = lemp->nsymbol * 2;
3613 types = (char**)calloc( arraysize, sizeof(char*) );
3614 if( types==0 ){
3615 fprintf(stderr,"Out of memory.\n");
3616 exit(1);
3617 }
3618 for(i=0; i<arraysize; i++) types[i] = 0;
3619 maxdtlength = 0;
3620 if( lemp->vartype ){
3621 maxdtlength = lemonStrlen(lemp->vartype);
3622 }
3623 for(i=0; i<lemp->nsymbol; i++){
3624 int len;
3625 struct symbol *sp = lemp->symbols[i];
3626 if( sp->datatype==0 ) continue;
3627 len = lemonStrlen(sp->datatype);
3628 if( len>maxdtlength ) maxdtlength = len;
3629 }
3630 stddt = (char*)malloc( maxdtlength*2 + 1 );
3631 if( stddt==0 ){
3632 fprintf(stderr,"Out of memory.\n");
3633 exit(1);
3634 }
3635
3636 /* Build a hash table of datatypes. The ".dtnum" field of each symbol
3637 ** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
3638 ** used for terminal symbols. If there is no %default_type defined then
3639 ** 0 is also used as the .dtnum value for nonterminals which do not specify
3640 ** a datatype using the %type directive.
3641 */
3642 for(i=0; i<lemp->nsymbol; i++){
3643 struct symbol *sp = lemp->symbols[i];
3644 char *cp;
3645 if( sp==lemp->errsym ){
3646 sp->dtnum = arraysize+1;
3647 continue;
3648 }
3649 if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
3650 sp->dtnum = 0;
3651 continue;
3652 }
3653 cp = sp->datatype;
3654 if( cp==0 ) cp = lemp->vartype;
3655 j = 0;
3656 while( ISSPACE(*cp) ) cp++;
3657 while( *cp ) stddt[j++] = *cp++;
3658 while( j>0 && ISSPACE(stddt[j-1]) ) j--;
3659 stddt[j] = 0;
3660 if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){
3661 sp->dtnum = 0;
3662 continue;
3663 }
3664 hash = 0;
3665 for(j=0; stddt[j]; j++){
3666 hash = hash*53 + stddt[j];
3667 }
3668 hash = (hash & 0x7fffffff)%arraysize;
3669 while( types[hash] ){
3670 if( strcmp(types[hash],stddt)==0 ){
3671 sp->dtnum = hash + 1;
3672 break;
3673 }
3674 hash++;
3675 if( hash>=(unsigned)arraysize ) hash = 0;
3676 }
3677 if( types[hash]==0 ){
3678 sp->dtnum = hash + 1;
3679 types[hash] = (char*)malloc( lemonStrlen(stddt)+1 );
3680 if( types[hash]==0 ){
3681 fprintf(stderr,"Out of memory.\n");
3682 exit(1);
3683 }
3684 lemon_strcpy(types[hash],stddt);
3685 }
3686 }
3687
3688 /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
3689 name = lemp->name ? lemp->name : "Parse";
3690 lineno = *plineno;
3691 if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
3692 fprintf(out,"#define %sTOKENTYPE %s\n",name,
3693 lemp->tokentype?lemp->tokentype:"void*"); lineno++;
3694 if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
3695 fprintf(out,"typedef union {\n"); lineno++;
3696 fprintf(out," int yyinit;\n"); lineno++;
3697 fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
3698 for(i=0; i<arraysize; i++){
3699 if( types[i]==0 ) continue;
3700 fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
3701 free(types[i]);
3702 }
3703 if( lemp->errsym->useCnt ){
3704 fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
3705 }
3706 free(stddt);
3707 free(types);
3708 fprintf(out,"} YYMINORTYPE;\n"); lineno++;
3709 *plineno = lineno;
3710 }
3711
3712 /*
3713 ** Return the name of a C datatype able to represent values between
3714 ** lwr and upr, inclusive. If pnByte!=NULL then also write the sizeof
3715 ** for that type (1, 2, or 4) into *pnByte.
3716 */
3717 static const char *minimum_size_type(int lwr, int upr, int *pnByte){
3718 const char *zType = "int";
3719 int nByte = 4;
3720 if( lwr>=0 ){
3721 if( upr<=255 ){
3722 zType = "unsigned char";
3723 nByte = 1;
3724 }else if( upr<65535 ){
3725 zType = "unsigned short int";
3726 nByte = 2;
3727 }else{
3728 zType = "unsigned int";
3729 nByte = 4;
3730 }
3731 }else if( lwr>=-127 && upr<=127 ){
3732 zType = "signed char";
3733 nByte = 1;
3734 }else if( lwr>=-32767 && upr<32767 ){
3735 zType = "short";
3736 nByte = 2;
3737 }
3738 if( pnByte ) *pnByte = nByte;
3739 return zType;
3740 }
3741
3742 /*
3743 ** Each state contains a set of token transaction and a set of
3744 ** nonterminal transactions. Each of these sets makes an instance
3745 ** of the following structure. An array of these structures is used
3746 ** to order the creation of entries in the yy_action[] table.
3747 */
3748 struct axset {
3749 struct state *stp; /* A pointer to a state */
3750 int isTkn; /* True to use tokens. False for non-terminals */
3751 int nAction; /* Number of actions */
3752 int iOrder; /* Original order of action sets */
3753 };
3754
3755 /*
3756 ** Compare to axset structures for sorting purposes
3757 */
3758 static int axset_compare(const void *a, const void *b){
3759 struct axset *p1 = (struct axset*)a;
3760 struct axset *p2 = (struct axset*)b;
3761 int c;
3762 c = p2->nAction - p1->nAction;
3763 if( c==0 ){
3764 c = p1->iOrder - p2->iOrder;
3765 }
3766 assert( c!=0 || p1==p2 );
3767 return c;
3768 }
3769
3770 /*
3771 ** Write text on "out" that describes the rule "rp".
3772 */
3773 static void writeRuleText(FILE *out, struct rule *rp){
3774 int j;
3775 fprintf(out,"%s ::=", rp->lhs->name);
3776 for(j=0; j<rp->nrhs; j++){
3777 struct symbol *sp = rp->rhs[j];
3778 if( sp->type!=MULTITERMINAL ){
3779 fprintf(out," %s", sp->name);
3780 }else{
3781 int k;
3782 fprintf(out," %s", sp->subsym[0]->name);
3783 for(k=1; k<sp->nsubsym; k++){
3784 fprintf(out,"|%s",sp->subsym[k]->name);
3785 }
3786 }
3787 }
3788 }
3789
3790
3791 /* Generate C source code for the parser */
3792 void ReportTable(
3793 struct lemon *lemp,
3794 int mhflag /* Output in makeheaders format if true */
3795 ){
3796 FILE *out, *in;
3797 char line[LINESIZE];
3798 int lineno;
3799 struct state *stp;
3800 struct action *ap;
3801 struct rule *rp;
3802 struct acttab *pActtab;
3803 int i, j, n, sz;
3804 int szActionType; /* sizeof(YYACTIONTYPE) */
3805 int szCodeType; /* sizeof(YYCODETYPE) */
3806 const char *name;
3807 int mnTknOfst, mxTknOfst;
3808 int mnNtOfst, mxNtOfst;
3809 struct axset *ax;
3810
3811 in = tplt_open(lemp);
3812 if( in==0 ) return;
3813 out = file_open(lemp,".c","wb");
3814 if( out==0 ){
3815 fclose(in);
3816 return;
3817 }
3818 lineno = 1;
3819 tplt_xfer(lemp->name,in,out,&lineno);
3820
3821 /* Generate the include code, if any */
3822 tplt_print(out,lemp,lemp->include,&lineno);
3823 if( mhflag ){
3824 char *incName = file_makename(lemp, ".h");
3825 fprintf(out,"#include \"%s\"\n", incName); lineno++;
3826 free(incName);
3827 }
3828 tplt_xfer(lemp->name,in,out,&lineno);
3829
3830 /* Generate #defines for all tokens */
3831 if( mhflag ){
3832 const char *prefix;
3833 fprintf(out,"#if INTERFACE\n"); lineno++;
3834 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
3835 else prefix = "";
3836 for(i=1; i<lemp->nterminal; i++){
3837 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3838 lineno++;
3839 }
3840 fprintf(out,"#endif\n"); lineno++;
3841 }
3842 tplt_xfer(lemp->name,in,out,&lineno);
3843
3844 /* Generate the defines */
3845 fprintf(out,"#define YYCODETYPE %s\n",
3846 minimum_size_type(0, lemp->nsymbol+1, &szCodeType)); lineno++;
3847 fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++;
3848 fprintf(out,"#define YYACTIONTYPE %s\n",
3849 minimum_size_type(0,lemp->nstate+lemp->nrule*2+5,&szActionType)); lineno++;
3850 if( lemp->wildcard ){
3851 fprintf(out,"#define YYWILDCARD %d\n",
3852 lemp->wildcard->index); lineno++;
3853 }
3854 print_stack_union(out,lemp,&lineno,mhflag);
3855 fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++;
3856 if( lemp->stacksize ){
3857 fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
3858 }else{
3859 fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
3860 }
3861 fprintf(out, "#endif\n"); lineno++;
3862 if( mhflag ){
3863 fprintf(out,"#if INTERFACE\n"); lineno++;
3864 }
3865 name = lemp->name ? lemp->name : "Parse";
3866 if( lemp->arg && lemp->arg[0] ){
3867 i = lemonStrlen(lemp->arg);
3868 while( i>=1 && ISSPACE(lemp->arg[i-1]) ) i--;
3869 while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
3870 fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++;
3871 fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++;
3872 fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n",
3873 name,lemp->arg,&lemp->arg[i]); lineno++;
3874 fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n",
3875 name,&lemp->arg[i],&lemp->arg[i]); lineno++;
3876 }else{
3877 fprintf(out,"#define %sARG_SDECL\n",name); lineno++;
3878 fprintf(out,"#define %sARG_PDECL\n",name); lineno++;
3879 fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
3880 fprintf(out,"#define %sARG_STORE\n",name); lineno++;
3881 }
3882 if( mhflag ){
3883 fprintf(out,"#endif\n"); lineno++;
3884 }
3885 if( lemp->errsym->useCnt ){
3886 fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
3887 fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
3888 }
3889 if( lemp->has_fallback ){
3890 fprintf(out,"#define YYFALLBACK 1\n"); lineno++;
3891 }
3892
3893 /* Compute the action table, but do not output it yet. The action
3894 ** table must be computed before generating the YYNSTATE macro because
3895 ** we need to know how many states can be eliminated.
3896 */
3897 ax = (struct axset *) calloc(lemp->nxstate*2, sizeof(ax[0]));
3898 if( ax==0 ){
3899 fprintf(stderr,"malloc failed\n");
3900 exit(1);
3901 }
3902 for(i=0; i<lemp->nxstate; i++){
3903 stp = lemp->sorted[i];
3904 ax[i*2].stp = stp;
3905 ax[i*2].isTkn = 1;
3906 ax[i*2].nAction = stp->nTknAct;
3907 ax[i*2+1].stp = stp;
3908 ax[i*2+1].isTkn = 0;
3909 ax[i*2+1].nAction = stp->nNtAct;
3910 }
3911 mxTknOfst = mnTknOfst = 0;
3912 mxNtOfst = mnNtOfst = 0;
3913 /* In an effort to minimize the action table size, use the heuristic
3914 ** of placing the largest action sets first */
3915 for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i;
3916 qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare);
3917 pActtab = acttab_alloc();
3918 for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){
3919 stp = ax[i].stp;
3920 if( ax[i].isTkn ){
3921 for(ap=stp->ap; ap; ap=ap->next){
3922 int action;
3923 if( ap->sp->index>=lemp->nterminal ) continue;
3924 action = compute_action(lemp, ap);
3925 if( action<0 ) continue;
3926 acttab_action(pActtab, ap->sp->index, action);
3927 }
3928 stp->iTknOfst = acttab_insert(pActtab);
3929 if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
3930 if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
3931 }else{
3932 for(ap=stp->ap; ap; ap=ap->next){
3933 int action;
3934 if( ap->sp->index<lemp->nterminal ) continue;
3935 if( ap->sp->index==lemp->nsymbol ) continue;
3936 action = compute_action(lemp, ap);
3937 if( action<0 ) continue;
3938 acttab_action(pActtab, ap->sp->index, action);
3939 }
3940 stp->iNtOfst = acttab_insert(pActtab);
3941 if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
3942 if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
3943 }
3944 #if 0 /* Uncomment for a trace of how the yy_action[] table fills out */
3945 { int jj, nn;
3946 for(jj=nn=0; jj<pActtab->nAction; jj++){
3947 if( pActtab->aAction[jj].action<0 ) nn++;
3948 }
3949 printf("%4d: State %3d %s n: %2d size: %5d freespace: %d\n",
3950 i, stp->statenum, ax[i].isTkn ? "Token" : "Var ",
3951 ax[i].nAction, pActtab->nAction, nn);
3952 }
3953 #endif
3954 }
3955 free(ax);
3956
3957 /* Finish rendering the constants now that the action table has
3958 ** been computed */
3959 fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++;
3960 fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
3961 fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++;
3962 fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",lemp->nstate); lineno++;
3963 i = lemp->nstate + lemp->nrule;
3964 fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++;
3965 fprintf(out,"#define YY_MIN_REDUCE %d\n", i); lineno++;
3966 i = lemp->nstate + lemp->nrule*2;
3967 fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++;
3968 fprintf(out,"#define YY_ERROR_ACTION %d\n", i); lineno++;
3969 fprintf(out,"#define YY_ACCEPT_ACTION %d\n", i+1); lineno++;
3970 fprintf(out,"#define YY_NO_ACTION %d\n", i+2); lineno++;
3971 tplt_xfer(lemp->name,in,out,&lineno);
3972
3973 /* Now output the action table and its associates:
3974 **
3975 ** yy_action[] A single table containing all actions.
3976 ** yy_lookahead[] A table containing the lookahead for each entry in
3977 ** yy_action. Used to detect hash collisions.
3978 ** yy_shift_ofst[] For each state, the offset into yy_action for
3979 ** shifting terminals.
3980 ** yy_reduce_ofst[] For each state, the offset into yy_action for
3981 ** shifting non-terminals after a reduce.
3982 ** yy_default[] Default action for each state.
3983 */
3984
3985 /* Output the yy_action table */
3986 lemp->nactiontab = n = acttab_size(pActtab);
3987 lemp->tablesize += n*szActionType;
3988 fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
3989 fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
3990 for(i=j=0; i<n; i++){
3991 int action = acttab_yyaction(pActtab, i);
3992 if( action<0 ) action = lemp->nstate + lemp->nrule + 2;
3993 if( j==0 ) fprintf(out," /* %5d */ ", i);
3994 fprintf(out, " %4d,", action);
3995 if( j==9 || i==n-1 ){
3996 fprintf(out, "\n"); lineno++;
3997 j = 0;
3998 }else{
3999 j++;
4000 }
4001 }
4002 fprintf(out, "};\n"); lineno++;
4003
4004 /* Output the yy_lookahead table */
4005 lemp->tablesize += n*szCodeType;
4006 fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
4007 for(i=j=0; i<n; i++){
4008 int la = acttab_yylookahead(pActtab, i);
4009 if( la<0 ) la = lemp->nsymbol;
4010 if( j==0 ) fprintf(out," /* %5d */ ", i);
4011 fprintf(out, " %4d,", la);
4012 if( j==9 || i==n-1 ){
4013 fprintf(out, "\n"); lineno++;
4014 j = 0;
4015 }else{
4016 j++;
4017 }
4018 }
4019 fprintf(out, "};\n"); lineno++;
4020
4021 /* Output the yy_shift_ofst[] table */
4022 fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++;
4023 n = lemp->nxstate;
4024 while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
4025 fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
4026 fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
4027 fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
4028 fprintf(out, "static const %s yy_shift_ofst[] = {\n",
4029 minimum_size_type(mnTknOfst-1, mxTknOfst, &sz)); lineno++;
4030 lemp->tablesize += n*sz;
4031 for(i=j=0; i<n; i++){
4032 int ofst;
4033 stp = lemp->sorted[i];
4034 ofst = stp->iTknOfst;
4035 if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1;
4036 if( j==0 ) fprintf(out," /* %5d */ ", i);
4037 fprintf(out, " %4d,", ofst);
4038 if( j==9 || i==n-1 ){
4039 fprintf(out, "\n"); lineno++;
4040 j = 0;
4041 }else{
4042 j++;
4043 }
4044 }
4045 fprintf(out, "};\n"); lineno++;
4046
4047 /* Output the yy_reduce_ofst[] table */
4048 fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
4049 n = lemp->nxstate;
4050 while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
4051 fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
4052 fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
4053 fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
4054 fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
4055 minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++;
4056 lemp->tablesize += n*sz;
4057 for(i=j=0; i<n; i++){
4058 int ofst;
4059 stp = lemp->sorted[i];
4060 ofst = stp->iNtOfst;
4061 if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
4062 if( j==0 ) fprintf(out," /* %5d */ ", i);
4063 fprintf(out, " %4d,", ofst);
4064 if( j==9 || i==n-1 ){
4065 fprintf(out, "\n"); lineno++;
4066 j = 0;
4067 }else{
4068 j++;
4069 }
4070 }
4071 fprintf(out, "};\n"); lineno++;
4072
4073 /* Output the default action table */
4074 fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
4075 n = lemp->nxstate;
4076 lemp->tablesize += n*szActionType;
4077 for(i=j=0; i<n; i++){
4078 stp = lemp->sorted[i];
4079 if( j==0 ) fprintf(out," /* %5d */ ", i);
4080 fprintf(out, " %4d,", stp->iDfltReduce+lemp->nstate+lemp->nrule);
4081 if( j==9 || i==n-1 ){
4082 fprintf(out, "\n"); lineno++;
4083 j = 0;
4084 }else{
4085 j++;
4086 }
4087 }
4088 fprintf(out, "};\n"); lineno++;
4089 tplt_xfer(lemp->name,in,out,&lineno);
4090
4091 /* Generate the table of fallback tokens.
4092 */
4093 if( lemp->has_fallback ){
4094 int mx = lemp->nterminal - 1;
4095 while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; }
4096 lemp->tablesize += (mx+1)*szCodeType;
4097 for(i=0; i<=mx; i++){
4098 struct symbol *p = lemp->symbols[i];
4099 if( p->fallback==0 ){
4100 fprintf(out, " 0, /* %10s => nothing */\n", p->name);
4101 }else{
4102 fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index,
4103 p->name, p->fallback->name);
4104 }
4105 lineno++;
4106 }
4107 }
4108 tplt_xfer(lemp->name, in, out, &lineno);
4109
4110 /* Generate a table containing the symbolic name of every symbol
4111 */
4112 for(i=0; i<lemp->nsymbol; i++){
4113 lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name);
4114 fprintf(out," %-15s",line);
4115 if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
4116 }
4117 if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
4118 tplt_xfer(lemp->name,in,out,&lineno);
4119
4120 /* Generate a table containing a text string that describes every
4121 ** rule in the rule set of the grammar. This information is used
4122 ** when tracing REDUCE actions.
4123 */
4124 for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
4125 assert( rp->index==i );
4126 fprintf(out," /* %3d */ \"", i);
4127 writeRuleText(out, rp);
4128 fprintf(out,"\",\n"); lineno++;
4129 }
4130 tplt_xfer(lemp->name,in,out,&lineno);
4131
4132 /* Generate code which executes every time a symbol is popped from
4133 ** the stack while processing errors or while destroying the parser.
4134 ** (In other words, generate the %destructor actions)
4135 */
4136 if( lemp->tokendest ){
4137 int once = 1;
4138 for(i=0; i<lemp->nsymbol; i++){
4139 struct symbol *sp = lemp->symbols[i];
4140 if( sp==0 || sp->type!=TERMINAL ) continue;
4141 if( once ){
4142 fprintf(out, " /* TERMINAL Destructor */\n"); lineno++;
4143 once = 0;
4144 }
4145 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
4146 }
4147 for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
4148 if( i<lemp->nsymbol ){
4149 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4150 fprintf(out," break;\n"); lineno++;
4151 }
4152 }
4153 if( lemp->vardest ){
4154 struct symbol *dflt_sp = 0;
4155 int once = 1;
4156 for(i=0; i<lemp->nsymbol; i++){
4157 struct symbol *sp = lemp->symbols[i];
4158 if( sp==0 || sp->type==TERMINAL ||
4159 sp->index<=0 || sp->destructor!=0 ) continue;
4160 if( once ){
4161 fprintf(out, " /* Default NON-TERMINAL Destructor */\n"); lineno++;
4162 once = 0;
4163 }
4164 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
4165 dflt_sp = sp;
4166 }
4167 if( dflt_sp!=0 ){
4168 emit_destructor_code(out,dflt_sp,lemp,&lineno);
4169 }
4170 fprintf(out," break;\n"); lineno++;
4171 }
4172 for(i=0; i<lemp->nsymbol; i++){
4173 struct symbol *sp = lemp->symbols[i];
4174 if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
4175 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
4176
4177 /* Combine duplicate destructors into a single case */
4178 for(j=i+1; j<lemp->nsymbol; j++){
4179 struct symbol *sp2 = lemp->symbols[j];
4180 if( sp2 && sp2->type!=TERMINAL && sp2->destructor
4181 && sp2->dtnum==sp->dtnum
4182 && strcmp(sp->destructor,sp2->destructor)==0 ){
4183 fprintf(out," case %d: /* %s */\n",
4184 sp2->index, sp2->name); lineno++;
4185 sp2->destructor = 0;
4186 }
4187 }
4188
4189 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4190 fprintf(out," break;\n"); lineno++;
4191 }
4192 tplt_xfer(lemp->name,in,out,&lineno);
4193
4194 /* Generate code which executes whenever the parser stack overflows */
4195 tplt_print(out,lemp,lemp->overflow,&lineno);
4196 tplt_xfer(lemp->name,in,out,&lineno);
4197
4198 /* Generate the table of rule information
4199 **
4200 ** Note: This code depends on the fact that rules are number
4201 ** sequentually beginning with 0.
4202 */
4203 for(rp=lemp->rule; rp; rp=rp->next){
4204 fprintf(out," { %d, %d },\n",rp->lhs->index,rp->nrhs); lineno++;
4205 }
4206 tplt_xfer(lemp->name,in,out,&lineno);
4207
4208 /* Generate code which execution during each REDUCE action */
4209 for(rp=lemp->rule; rp; rp=rp->next){
4210 translate_code(lemp, rp);
4211 }
4212 /* First output rules other than the default: rule */
4213 for(rp=lemp->rule; rp; rp=rp->next){
4214 struct rule *rp2; /* Other rules with the same action */
4215 if( rp->code==0 ) continue;
4216 if( rp->code[0]=='\n' && rp->code[1]==0 ) continue; /* Will be default: */
4217 fprintf(out," case %d: /* ", rp->index);
4218 writeRuleText(out, rp);
4219 fprintf(out, " */\n"); lineno++;
4220 for(rp2=rp->next; rp2; rp2=rp2->next){
4221 if( rp2->code==rp->code ){
4222 fprintf(out," case %d: /* ", rp2->index);
4223 writeRuleText(out, rp2);
4224 fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->index); lineno++;
4225 rp2->code = 0;
4226 }
4227 }
4228 emit_code(out,rp,lemp,&lineno);
4229 fprintf(out," break;\n"); lineno++;
4230 rp->code = 0;
4231 }
4232 /* Finally, output the default: rule. We choose as the default: all
4233 ** empty actions. */
4234 fprintf(out," default:\n"); lineno++;
4235 for(rp=lemp->rule; rp; rp=rp->next){
4236 if( rp->code==0 ) continue;
4237 assert( rp->code[0]=='\n' && rp->code[1]==0 );
4238 fprintf(out," /* (%d) ", rp->index);
4239 writeRuleText(out, rp);
4240 fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->index); lineno++;
4241 }
4242 fprintf(out," break;\n"); lineno++;
4243 tplt_xfer(lemp->name,in,out,&lineno);
4244
4245 /* Generate code which executes if a parse fails */
4246 tplt_print(out,lemp,lemp->failure,&lineno);
4247 tplt_xfer(lemp->name,in,out,&lineno);
4248
4249 /* Generate code which executes when a syntax error occurs */
4250 tplt_print(out,lemp,lemp->error,&lineno);
4251 tplt_xfer(lemp->name,in,out,&lineno);
4252
4253 /* Generate code which executes when the parser accepts its input */
4254 tplt_print(out,lemp,lemp->accept,&lineno);
4255 tplt_xfer(lemp->name,in,out,&lineno);
4256
4257 /* Append any addition code the user desires */
4258 tplt_print(out,lemp,lemp->extracode,&lineno);
4259
4260 fclose(in);
4261 fclose(out);
4262 return;
4263 }
4264
4265 /* Generate a header file for the parser */
4266 void ReportHeader(struct lemon *lemp)
4267 {
4268 FILE *out, *in;
4269 const char *prefix;
4270 char line[LINESIZE];
4271 char pattern[LINESIZE];
4272 int i;
4273
4274 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4275 else prefix = "";
4276 in = file_open(lemp,".h","rb");
4277 if( in ){
4278 int nextChar;
4279 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
4280 lemon_sprintf(pattern,"#define %s%-30s %3d\n",
4281 prefix,lemp->symbols[i]->name,i);
4282 if( strcmp(line,pattern) ) break;
4283 }
4284 nextChar = fgetc(in);
4285 fclose(in);
4286 if( i==lemp->nterminal && nextChar==EOF ){
4287 /* No change in the file. Don't rewrite it. */
4288 return;
4289 }
4290 }
4291 out = file_open(lemp,".h","wb");
4292 if( out ){
4293 for(i=1; i<lemp->nterminal; i++){
4294 fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i);
4295 }
4296 fclose(out);
4297 }
4298 return;
4299 }
4300
4301 /* Reduce the size of the action tables, if possible, by making use
4302 ** of defaults.
4303 **
4304 ** In this version, we take the most frequent REDUCE action and make
4305 ** it the default. Except, there is no default if the wildcard token
4306 ** is a possible look-ahead.
4307 */
4308 void CompressTables(struct lemon *lemp)
4309 {
4310 struct state *stp;
4311 struct action *ap, *ap2;
4312 struct rule *rp, *rp2, *rbest;
4313 int nbest, n;
4314 int i;
4315 int usesWildcard;
4316
4317 for(i=0; i<lemp->nstate; i++){
4318 stp = lemp->sorted[i];
4319 nbest = 0;
4320 rbest = 0;
4321 usesWildcard = 0;
4322
4323 for(ap=stp->ap; ap; ap=ap->next){
4324 if( ap->type==SHIFT && ap->sp==lemp->wildcard ){
4325 usesWildcard = 1;
4326 }
4327 if( ap->type!=REDUCE ) continue;
4328 rp = ap->x.rp;
4329 if( rp->lhsStart ) continue;
4330 if( rp==rbest ) continue;
4331 n = 1;
4332 for(ap2=ap->next; ap2; ap2=ap2->next){
4333 if( ap2->type!=REDUCE ) continue;
4334 rp2 = ap2->x.rp;
4335 if( rp2==rbest ) continue;
4336 if( rp2==rp ) n++;
4337 }
4338 if( n>nbest ){
4339 nbest = n;
4340 rbest = rp;
4341 }
4342 }
4343
4344 /* Do not make a default if the number of rules to default
4345 ** is not at least 1 or if the wildcard token is a possible
4346 ** lookahead.
4347 */
4348 if( nbest<1 || usesWildcard ) continue;
4349
4350
4351 /* Combine matching REDUCE actions into a single default */
4352 for(ap=stp->ap; ap; ap=ap->next){
4353 if( ap->type==REDUCE && ap->x.rp==rbest ) break;
4354 }
4355 assert( ap );
4356 ap->sp = Symbol_new("{default}");
4357 for(ap=ap->next; ap; ap=ap->next){
4358 if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
4359 }
4360 stp->ap = Action_sort(stp->ap);
4361
4362 for(ap=stp->ap; ap; ap=ap->next){
4363 if( ap->type==SHIFT ) break;
4364 if( ap->type==REDUCE && ap->x.rp!=rbest ) break;
4365 }
4366 if( ap==0 ){
4367 stp->autoReduce = 1;
4368 stp->pDfltReduce = rbest;
4369 }
4370 }
4371
4372 /* Make a second pass over all states and actions. Convert
4373 ** every action that is a SHIFT to an autoReduce state into
4374 ** a SHIFTREDUCE action.
4375 */
4376 for(i=0; i<lemp->nstate; i++){
4377 stp = lemp->sorted[i];
4378 for(ap=stp->ap; ap; ap=ap->next){
4379 struct state *pNextState;
4380 if( ap->type!=SHIFT ) continue;
4381 pNextState = ap->x.stp;
4382 if( pNextState->autoReduce && pNextState->pDfltReduce!=0 ){
4383 ap->type = SHIFTREDUCE;
4384 ap->x.rp = pNextState->pDfltReduce;
4385 }
4386 }
4387 }
4388 }
4389
4390
4391 /*
4392 ** Compare two states for sorting purposes. The smaller state is the
4393 ** one with the most non-terminal actions. If they have the same number
4394 ** of non-terminal actions, then the smaller is the one with the most
4395 ** token actions.
4396 */
4397 static int stateResortCompare(const void *a, const void *b){
4398 const struct state *pA = *(const struct state**)a;
4399 const struct state *pB = *(const struct state**)b;
4400 int n;
4401
4402 n = pB->nNtAct - pA->nNtAct;
4403 if( n==0 ){
4404 n = pB->nTknAct - pA->nTknAct;
4405 if( n==0 ){
4406 n = pB->statenum - pA->statenum;
4407 }
4408 }
4409 assert( n!=0 );
4410 return n;
4411 }
4412
4413
4414 /*
4415 ** Renumber and resort states so that states with fewer choices
4416 ** occur at the end. Except, keep state 0 as the first state.
4417 */
4418 void ResortStates(struct lemon *lemp)
4419 {
4420 int i;
4421 struct state *stp;
4422 struct action *ap;
4423
4424 for(i=0; i<lemp->nstate; i++){
4425 stp = lemp->sorted[i];
4426 stp->nTknAct = stp->nNtAct = 0;
4427 stp->iDfltReduce = lemp->nrule; /* Init dflt action to "syntax error" */
4428 stp->iTknOfst = NO_OFFSET;
4429 stp->iNtOfst = NO_OFFSET;
4430 for(ap=stp->ap; ap; ap=ap->next){
4431 int iAction = compute_action(lemp,ap);
4432 if( iAction>=0 ){
4433 if( ap->sp->index<lemp->nterminal ){
4434 stp->nTknAct++;
4435 }else if( ap->sp->index<lemp->nsymbol ){
4436 stp->nNtAct++;
4437 }else{
4438 assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp );
4439 stp->iDfltReduce = iAction - lemp->nstate - lemp->nrule;
4440 }
4441 }
4442 }
4443 }
4444 qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
4445 stateResortCompare);
4446 for(i=0; i<lemp->nstate; i++){
4447 lemp->sorted[i]->statenum = i;
4448 }
4449 lemp->nxstate = lemp->nstate;
4450 while( lemp->nxstate>1 && lemp->sorted[lemp->nxstate-1]->autoReduce ){
4451 lemp->nxstate--;
4452 }
4453 }
4454
4455
4456 /***************** From the file "set.c" ************************************/
4457 /*
4458 ** Set manipulation routines for the LEMON parser generator.
4459 */
4460
4461 static int size = 0;
4462
4463 /* Set the set size */
4464 void SetSize(int n)
4465 {
4466 size = n+1;
4467 }
4468
4469 /* Allocate a new set */
4470 char *SetNew(){
4471 char *s;
4472 s = (char*)calloc( size, 1);
4473 if( s==0 ){
4474 extern void memory_error();
4475 memory_error();
4476 }
4477 return s;
4478 }
4479
4480 /* Deallocate a set */
4481 void SetFree(char *s)
4482 {
4483 free(s);
4484 }
4485
4486 /* Add a new element to the set. Return TRUE if the element was added
4487 ** and FALSE if it was already there. */
4488 int SetAdd(char *s, int e)
4489 {
4490 int rv;
4491 assert( e>=0 && e<size );
4492 rv = s[e];
4493 s[e] = 1;
4494 return !rv;
4495 }
4496
4497 /* Add every element of s2 to s1. Return TRUE if s1 changes. */
4498 int SetUnion(char *s1, char *s2)
4499 {
4500 int i, progress;
4501 progress = 0;
4502 for(i=0; i<size; i++){
4503 if( s2[i]==0 ) continue;
4504 if( s1[i]==0 ){
4505 progress = 1;
4506 s1[i] = 1;
4507 }
4508 }
4509 return progress;
4510 }
4511 /********************** From the file "table.c" ****************************/
4512 /*
4513 ** All code in this file has been automatically generated
4514 ** from a specification in the file
4515 ** "table.q"
4516 ** by the associative array code building program "aagen".
4517 ** Do not edit this file! Instead, edit the specification
4518 ** file, then rerun aagen.
4519 */
4520 /*
4521 ** Code for processing tables in the LEMON parser generator.
4522 */
4523
4524 PRIVATE unsigned strhash(const char *x)
4525 {
4526 unsigned h = 0;
4527 while( *x ) h = h*13 + *(x++);
4528 return h;
4529 }
4530
4531 /* Works like strdup, sort of. Save a string in malloced memory, but
4532 ** keep strings in a table so that the same string is not in more
4533 ** than one place.
4534 */
4535 const char *Strsafe(const char *y)
4536 {
4537 const char *z;
4538 char *cpy;
4539
4540 if( y==0 ) return 0;
4541 z = Strsafe_find(y);
4542 if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
4543 lemon_strcpy(cpy,y);
4544 z = cpy;
4545 Strsafe_insert(z);
4546 }
4547 MemoryCheck(z);
4548 return z;
4549 }
4550
4551 /* There is one instance of the following structure for each
4552 ** associative array of type "x1".
4553 */
4554 struct s_x1 {
4555 int size; /* The number of available slots. */
4556 /* Must be a power of 2 greater than or */
4557 /* equal to 1 */
4558 int count; /* Number of currently slots filled */
4559 struct s_x1node *tbl; /* The data stored here */
4560 struct s_x1node **ht; /* Hash table for lookups */
4561 };
4562
4563 /* There is one instance of this structure for every data element
4564 ** in an associative array of type "x1".
4565 */
4566 typedef struct s_x1node {
4567 const char *data; /* The data */
4568 struct s_x1node *next; /* Next entry with the same hash */
4569 struct s_x1node **from; /* Previous link */
4570 } x1node;
4571
4572 /* There is only one instance of the array, which is the following */
4573 static struct s_x1 *x1a;
4574
4575 /* Allocate a new associative array */
4576 void Strsafe_init(){
4577 if( x1a ) return;
4578 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
4579 if( x1a ){
4580 x1a->size = 1024;
4581 x1a->count = 0;
4582 x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*));
4583 if( x1a->tbl==0 ){
4584 free(x1a);
4585 x1a = 0;
4586 }else{
4587 int i;
4588 x1a->ht = (x1node**)&(x1a->tbl[1024]);
4589 for(i=0; i<1024; i++) x1a->ht[i] = 0;
4590 }
4591 }
4592 }
4593 /* Insert a new record into the array. Return TRUE if successful.
4594 ** Prior data with the same key is NOT overwritten */
4595 int Strsafe_insert(const char *data)
4596 {
4597 x1node *np;
4598 unsigned h;
4599 unsigned ph;
4600
4601 if( x1a==0 ) return 0;
4602 ph = strhash(data);
4603 h = ph & (x1a->size-1);
4604 np = x1a->ht[h];
4605 while( np ){
4606 if( strcmp(np->data,data)==0 ){
4607 /* An existing entry with the same key is found. */
4608 /* Fail because overwrite is not allows. */
4609 return 0;
4610 }
4611 np = np->next;
4612 }
4613 if( x1a->count>=x1a->size ){
4614 /* Need to make the hash table bigger */
4615 int i,arrSize;
4616 struct s_x1 array;
4617 array.size = arrSize = x1a->size*2;
4618 array.count = x1a->count;
4619 array.tbl = (x1node*)calloc(arrSize, sizeof(x1node) + sizeof(x1node*));
4620 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4621 array.ht = (x1node**)&(array.tbl[arrSize]);
4622 for(i=0; i<arrSize; i++) array.ht[i] = 0;
4623 for(i=0; i<x1a->count; i++){
4624 x1node *oldnp, *newnp;
4625 oldnp = &(x1a->tbl[i]);
4626 h = strhash(oldnp->data) & (arrSize-1);
4627 newnp = &(array.tbl[i]);
4628 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4629 newnp->next = array.ht[h];
4630 newnp->data = oldnp->data;
4631 newnp->from = &(array.ht[h]);
4632 array.ht[h] = newnp;
4633 }
4634 free(x1a->tbl);
4635 *x1a = array;
4636 }
4637 /* Insert the new data */
4638 h = ph & (x1a->size-1);
4639 np = &(x1a->tbl[x1a->count++]);
4640 np->data = data;
4641 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
4642 np->next = x1a->ht[h];
4643 x1a->ht[h] = np;
4644 np->from = &(x1a->ht[h]);
4645 return 1;
4646 }
4647
4648 /* Return a pointer to data assigned to the given key. Return NULL
4649 ** if no such key. */
4650 const char *Strsafe_find(const char *key)
4651 {
4652 unsigned h;
4653 x1node *np;
4654
4655 if( x1a==0 ) return 0;
4656 h = strhash(key) & (x1a->size-1);
4657 np = x1a->ht[h];
4658 while( np ){
4659 if( strcmp(np->data,key)==0 ) break;
4660 np = np->next;
4661 }
4662 return np ? np->data : 0;
4663 }
4664
4665 /* Return a pointer to the (terminal or nonterminal) symbol "x".
4666 ** Create a new symbol if this is the first time "x" has been seen.
4667 */
4668 struct symbol *Symbol_new(const char *x)
4669 {
4670 struct symbol *sp;
4671
4672 sp = Symbol_find(x);
4673 if( sp==0 ){
4674 sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
4675 MemoryCheck(sp);
4676 sp->name = Strsafe(x);
4677 sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL;
4678 sp->rule = 0;
4679 sp->fallback = 0;
4680 sp->prec = -1;
4681 sp->assoc = UNK;
4682 sp->firstset = 0;
4683 sp->lambda = LEMON_FALSE;
4684 sp->destructor = 0;
4685 sp->destLineno = 0;
4686 sp->datatype = 0;
4687 sp->useCnt = 0;
4688 Symbol_insert(sp,sp->name);
4689 }
4690 sp->useCnt++;
4691 return sp;
4692 }
4693
4694 /* Compare two symbols for sorting purposes. Return negative,
4695 ** zero, or positive if a is less then, equal to, or greater
4696 ** than b.
4697 **
4698 ** Symbols that begin with upper case letters (terminals or tokens)
4699 ** must sort before symbols that begin with lower case letters
4700 ** (non-terminals). And MULTITERMINAL symbols (created using the
4701 ** %token_class directive) must sort at the very end. Other than
4702 ** that, the order does not matter.
4703 **
4704 ** We find experimentally that leaving the symbols in their original
4705 ** order (the order they appeared in the grammar file) gives the
4706 ** smallest parser tables in SQLite.
4707 */
4708 int Symbolcmpp(const void *_a, const void *_b)
4709 {
4710 const struct symbol *a = *(const struct symbol **) _a;
4711 const struct symbol *b = *(const struct symbol **) _b;
4712 int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1;
4713 int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1;
4714 return i1==i2 ? a->index - b->index : i1 - i2;
4715 }
4716
4717 /* There is one instance of the following structure for each
4718 ** associative array of type "x2".
4719 */
4720 struct s_x2 {
4721 int size; /* The number of available slots. */
4722 /* Must be a power of 2 greater than or */
4723 /* equal to 1 */
4724 int count; /* Number of currently slots filled */
4725 struct s_x2node *tbl; /* The data stored here */
4726 struct s_x2node **ht; /* Hash table for lookups */
4727 };
4728
4729 /* There is one instance of this structure for every data element
4730 ** in an associative array of type "x2".
4731 */
4732 typedef struct s_x2node {
4733 struct symbol *data; /* The data */
4734 const char *key; /* The key */
4735 struct s_x2node *next; /* Next entry with the same hash */
4736 struct s_x2node **from; /* Previous link */
4737 } x2node;
4738
4739 /* There is only one instance of the array, which is the following */
4740 static struct s_x2 *x2a;
4741
4742 /* Allocate a new associative array */
4743 void Symbol_init(){
4744 if( x2a ) return;
4745 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
4746 if( x2a ){
4747 x2a->size = 128;
4748 x2a->count = 0;
4749 x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*));
4750 if( x2a->tbl==0 ){
4751 free(x2a);
4752 x2a = 0;
4753 }else{
4754 int i;
4755 x2a->ht = (x2node**)&(x2a->tbl[128]);
4756 for(i=0; i<128; i++) x2a->ht[i] = 0;
4757 }
4758 }
4759 }
4760 /* Insert a new record into the array. Return TRUE if successful.
4761 ** Prior data with the same key is NOT overwritten */
4762 int Symbol_insert(struct symbol *data, const char *key)
4763 {
4764 x2node *np;
4765 unsigned h;
4766 unsigned ph;
4767
4768 if( x2a==0 ) return 0;
4769 ph = strhash(key);
4770 h = ph & (x2a->size-1);
4771 np = x2a->ht[h];
4772 while( np ){
4773 if( strcmp(np->key,key)==0 ){
4774 /* An existing entry with the same key is found. */
4775 /* Fail because overwrite is not allows. */
4776 return 0;
4777 }
4778 np = np->next;
4779 }
4780 if( x2a->count>=x2a->size ){
4781 /* Need to make the hash table bigger */
4782 int i,arrSize;
4783 struct s_x2 array;
4784 array.size = arrSize = x2a->size*2;
4785 array.count = x2a->count;
4786 array.tbl = (x2node*)calloc(arrSize, sizeof(x2node) + sizeof(x2node*));
4787 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4788 array.ht = (x2node**)&(array.tbl[arrSize]);
4789 for(i=0; i<arrSize; i++) array.ht[i] = 0;
4790 for(i=0; i<x2a->count; i++){
4791 x2node *oldnp, *newnp;
4792 oldnp = &(x2a->tbl[i]);
4793 h = strhash(oldnp->key) & (arrSize-1);
4794 newnp = &(array.tbl[i]);
4795 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4796 newnp->next = array.ht[h];
4797 newnp->key = oldnp->key;
4798 newnp->data = oldnp->data;
4799 newnp->from = &(array.ht[h]);
4800 array.ht[h] = newnp;
4801 }
4802 free(x2a->tbl);
4803 *x2a = array;
4804 }
4805 /* Insert the new data */
4806 h = ph & (x2a->size-1);
4807 np = &(x2a->tbl[x2a->count++]);
4808 np->key = key;
4809 np->data = data;
4810 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
4811 np->next = x2a->ht[h];
4812 x2a->ht[h] = np;
4813 np->from = &(x2a->ht[h]);
4814 return 1;
4815 }
4816
4817 /* Return a pointer to data assigned to the given key. Return NULL
4818 ** if no such key. */
4819 struct symbol *Symbol_find(const char *key)
4820 {
4821 unsigned h;
4822 x2node *np;
4823
4824 if( x2a==0 ) return 0;
4825 h = strhash(key) & (x2a->size-1);
4826 np = x2a->ht[h];
4827 while( np ){
4828 if( strcmp(np->key,key)==0 ) break;
4829 np = np->next;
4830 }
4831 return np ? np->data : 0;
4832 }
4833
4834 /* Return the n-th data. Return NULL if n is out of range. */
4835 struct symbol *Symbol_Nth(int n)
4836 {
4837 struct symbol *data;
4838 if( x2a && n>0 && n<=x2a->count ){
4839 data = x2a->tbl[n-1].data;
4840 }else{
4841 data = 0;
4842 }
4843 return data;
4844 }
4845
4846 /* Return the size of the array */
4847 int Symbol_count()
4848 {
4849 return x2a ? x2a->count : 0;
4850 }
4851
4852 /* Return an array of pointers to all data in the table.
4853 ** The array is obtained from malloc. Return NULL if memory allocation
4854 ** problems, or if the array is empty. */
4855 struct symbol **Symbol_arrayof()
4856 {
4857 struct symbol **array;
4858 int i,arrSize;
4859 if( x2a==0 ) return 0;
4860 arrSize = x2a->count;
4861 array = (struct symbol **)calloc(arrSize, sizeof(struct symbol *));
4862 if( array ){
4863 for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data;
4864 }
4865 return array;
4866 }
4867
4868 /* Compare two configurations */
4869 int Configcmp(const char *_a,const char *_b)
4870 {
4871 const struct config *a = (struct config *) _a;
4872 const struct config *b = (struct config *) _b;
4873 int x;
4874 x = a->rp->index - b->rp->index;
4875 if( x==0 ) x = a->dot - b->dot;
4876 return x;
4877 }
4878
4879 /* Compare two states */
4880 PRIVATE int statecmp(struct config *a, struct config *b)
4881 {
4882 int rc;
4883 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
4884 rc = a->rp->index - b->rp->index;
4885 if( rc==0 ) rc = a->dot - b->dot;
4886 }
4887 if( rc==0 ){
4888 if( a ) rc = 1;
4889 if( b ) rc = -1;
4890 }
4891 return rc;
4892 }
4893
4894 /* Hash a state */
4895 PRIVATE unsigned statehash(struct config *a)
4896 {
4897 unsigned h=0;
4898 while( a ){
4899 h = h*571 + a->rp->index*37 + a->dot;
4900 a = a->bp;
4901 }
4902 return h;
4903 }
4904
4905 /* Allocate a new state structure */
4906 struct state *State_new()
4907 {
4908 struct state *newstate;
4909 newstate = (struct state *)calloc(1, sizeof(struct state) );
4910 MemoryCheck(newstate);
4911 return newstate;
4912 }
4913
4914 /* There is one instance of the following structure for each
4915 ** associative array of type "x3".
4916 */
4917 struct s_x3 {
4918 int size; /* The number of available slots. */
4919 /* Must be a power of 2 greater than or */
4920 /* equal to 1 */
4921 int count; /* Number of currently slots filled */
4922 struct s_x3node *tbl; /* The data stored here */
4923 struct s_x3node **ht; /* Hash table for lookups */
4924 };
4925
4926 /* There is one instance of this structure for every data element
4927 ** in an associative array of type "x3".
4928 */
4929 typedef struct s_x3node {
4930 struct state *data; /* The data */
4931 struct config *key; /* The key */
4932 struct s_x3node *next; /* Next entry with the same hash */
4933 struct s_x3node **from; /* Previous link */
4934 } x3node;
4935
4936 /* There is only one instance of the array, which is the following */
4937 static struct s_x3 *x3a;
4938
4939 /* Allocate a new associative array */
4940 void State_init(){
4941 if( x3a ) return;
4942 x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
4943 if( x3a ){
4944 x3a->size = 128;
4945 x3a->count = 0;
4946 x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*));
4947 if( x3a->tbl==0 ){
4948 free(x3a);
4949 x3a = 0;
4950 }else{
4951 int i;
4952 x3a->ht = (x3node**)&(x3a->tbl[128]);
4953 for(i=0; i<128; i++) x3a->ht[i] = 0;
4954 }
4955 }
4956 }
4957 /* Insert a new record into the array. Return TRUE if successful.
4958 ** Prior data with the same key is NOT overwritten */
4959 int State_insert(struct state *data, struct config *key)
4960 {
4961 x3node *np;
4962 unsigned h;
4963 unsigned ph;
4964
4965 if( x3a==0 ) return 0;
4966 ph = statehash(key);
4967 h = ph & (x3a->size-1);
4968 np = x3a->ht[h];
4969 while( np ){
4970 if( statecmp(np->key,key)==0 ){
4971 /* An existing entry with the same key is found. */
4972 /* Fail because overwrite is not allows. */
4973 return 0;
4974 }
4975 np = np->next;
4976 }
4977 if( x3a->count>=x3a->size ){
4978 /* Need to make the hash table bigger */
4979 int i,arrSize;
4980 struct s_x3 array;
4981 array.size = arrSize = x3a->size*2;
4982 array.count = x3a->count;
4983 array.tbl = (x3node*)calloc(arrSize, sizeof(x3node) + sizeof(x3node*));
4984 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4985 array.ht = (x3node**)&(array.tbl[arrSize]);
4986 for(i=0; i<arrSize; i++) array.ht[i] = 0;
4987 for(i=0; i<x3a->count; i++){
4988 x3node *oldnp, *newnp;
4989 oldnp = &(x3a->tbl[i]);
4990 h = statehash(oldnp->key) & (arrSize-1);
4991 newnp = &(array.tbl[i]);
4992 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4993 newnp->next = array.ht[h];
4994 newnp->key = oldnp->key;
4995 newnp->data = oldnp->data;
4996 newnp->from = &(array.ht[h]);
4997 array.ht[h] = newnp;
4998 }
4999 free(x3a->tbl);
5000 *x3a = array;
5001 }
5002 /* Insert the new data */
5003 h = ph & (x3a->size-1);
5004 np = &(x3a->tbl[x3a->count++]);
5005 np->key = key;
5006 np->data = data;
5007 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
5008 np->next = x3a->ht[h];
5009 x3a->ht[h] = np;
5010 np->from = &(x3a->ht[h]);
5011 return 1;
5012 }
5013
5014 /* Return a pointer to data assigned to the given key. Return NULL
5015 ** if no such key. */
5016 struct state *State_find(struct config *key)
5017 {
5018 unsigned h;
5019 x3node *np;
5020
5021 if( x3a==0 ) return 0;
5022 h = statehash(key) & (x3a->size-1);
5023 np = x3a->ht[h];
5024 while( np ){
5025 if( statecmp(np->key,key)==0 ) break;
5026 np = np->next;
5027 }
5028 return np ? np->data : 0;
5029 }
5030
5031 /* Return an array of pointers to all data in the table.
5032 ** The array is obtained from malloc. Return NULL if memory allocation
5033 ** problems, or if the array is empty. */
5034 struct state **State_arrayof()
5035 {
5036 struct state **array;
5037 int i,arrSize;
5038 if( x3a==0 ) return 0;
5039 arrSize = x3a->count;
5040 array = (struct state **)calloc(arrSize, sizeof(struct state *));
5041 if( array ){
5042 for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data;
5043 }
5044 return array;
5045 }
5046
5047 /* Hash a configuration */
5048 PRIVATE unsigned confighash(struct config *a)
5049 {
5050 unsigned h=0;
5051 h = h*571 + a->rp->index*37 + a->dot;
5052 return h;
5053 }
5054
5055 /* There is one instance of the following structure for each
5056 ** associative array of type "x4".
5057 */
5058 struct s_x4 {
5059 int size; /* The number of available slots. */
5060 /* Must be a power of 2 greater than or */
5061 /* equal to 1 */
5062 int count; /* Number of currently slots filled */
5063 struct s_x4node *tbl; /* The data stored here */
5064 struct s_x4node **ht; /* Hash table for lookups */
5065 };
5066
5067 /* There is one instance of this structure for every data element
5068 ** in an associative array of type "x4".
5069 */
5070 typedef struct s_x4node {
5071 struct config *data; /* The data */
5072 struct s_x4node *next; /* Next entry with the same hash */
5073 struct s_x4node **from; /* Previous link */
5074 } x4node;
5075
5076 /* There is only one instance of the array, which is the following */
5077 static struct s_x4 *x4a;
5078
5079 /* Allocate a new associative array */
5080 void Configtable_init(){
5081 if( x4a ) return;
5082 x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
5083 if( x4a ){
5084 x4a->size = 64;
5085 x4a->count = 0;
5086 x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*));
5087 if( x4a->tbl==0 ){
5088 free(x4a);
5089 x4a = 0;
5090 }else{
5091 int i;
5092 x4a->ht = (x4node**)&(x4a->tbl[64]);
5093 for(i=0; i<64; i++) x4a->ht[i] = 0;
5094 }
5095 }
5096 }
5097 /* Insert a new record into the array. Return TRUE if successful.
5098 ** Prior data with the same key is NOT overwritten */
5099 int Configtable_insert(struct config *data)
5100 {
5101 x4node *np;
5102 unsigned h;
5103 unsigned ph;
5104
5105 if( x4a==0 ) return 0;
5106 ph = confighash(data);
5107 h = ph & (x4a->size-1);
5108 np = x4a->ht[h];
5109 while( np ){
5110 if( Configcmp((const char *) np->data,(const char *) data)==0 ){
5111 /* An existing entry with the same key is found. */
5112 /* Fail because overwrite is not allows. */
5113 return 0;
5114 }
5115 np = np->next;
5116 }
5117 if( x4a->count>=x4a->size ){
5118 /* Need to make the hash table bigger */
5119 int i,arrSize;
5120 struct s_x4 array;
5121 array.size = arrSize = x4a->size*2;
5122 array.count = x4a->count;
5123 array.tbl = (x4node*)calloc(arrSize, sizeof(x4node) + sizeof(x4node*));
5124 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
5125 array.ht = (x4node**)&(array.tbl[arrSize]);
5126 for(i=0; i<arrSize; i++) array.ht[i] = 0;
5127 for(i=0; i<x4a->count; i++){
5128 x4node *oldnp, *newnp;
5129 oldnp = &(x4a->tbl[i]);
5130 h = confighash(oldnp->data) & (arrSize-1);
5131 newnp = &(array.tbl[i]);
5132 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5133 newnp->next = array.ht[h];
5134 newnp->data = oldnp->data;
5135 newnp->from = &(array.ht[h]);
5136 array.ht[h] = newnp;
5137 }
5138 free(x4a->tbl);
5139 *x4a = array;
5140 }
5141 /* Insert the new data */
5142 h = ph & (x4a->size-1);
5143 np = &(x4a->tbl[x4a->count++]);
5144 np->data = data;
5145 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
5146 np->next = x4a->ht[h];
5147 x4a->ht[h] = np;
5148 np->from = &(x4a->ht[h]);
5149 return 1;
5150 }
5151
5152 /* Return a pointer to data assigned to the given key. Return NULL
5153 ** if no such key. */
5154 struct config *Configtable_find(struct config *key)
5155 {
5156 int h;
5157 x4node *np;
5158
5159 if( x4a==0 ) return 0;
5160 h = confighash(key) & (x4a->size-1);
5161 np = x4a->ht[h];
5162 while( np ){
5163 if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
5164 np = np->next;
5165 }
5166 return np ? np->data : 0;
5167 }
5168
5169 /* Remove all data from the table. Pass each data to the function "f"
5170 ** as it is removed. ("f" may be null to avoid this step.) */
5171 void Configtable_clear(int(*f)(struct config *))
5172 {
5173 int i;
5174 if( x4a==0 || x4a->count==0 ) return;
5175 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
5176 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
5177 x4a->count = 0;
5178 return;
5179 }
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3100200/tool/getlock.c ('k') | third_party/sqlite/sqlite-src-3100200/tool/lempar.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698