blob: e0ea780439609d8cd50d6c3d55d93450c64bab6e [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1991, 1994, 1997, 1998 D. Richard Hipp
3**
4** This file contains all sources (including headers) to the LEMON
5** LALR(1) parser generator. The sources have been combined into a
6** single file to make it easy to include LEMON as part of another
7** program.
8**
9** This program is free software; you can redistribute it and/or
10** modify it under the terms of the GNU General Public
11** License as published by the Free Software Foundation; either
12** version 2 of the License, or (at your option) any later version.
13**
14** This program is distributed in the hope that it will be useful,
15** but WITHOUT ANY WARRANTY; without even the implied warranty of
16** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17** General Public License for more details.
18**
19** You should have received a copy of the GNU General Public
20** License along with this library; if not, write to the
21** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22** Boston, MA 02111-1307, USA.
23**
24** Author contact information:
25** drh@acm.org
26** http://www.hwaci.com/drh/
27*/
28#include <stdio.h>
29#include <varargs.h>
30#include <string.h>
31#include <ctype.h>
32
33extern void qsort();
34extern double strtod();
35extern long strtol();
36extern void free();
37extern int access();
38extern int atoi();
39
40#ifndef __WIN32__
41# if defined(_WIN32) || defined(WIN32)
42# define __WIN32__
43# endif
44#endif
45
46/* #define PRIVATE static */
47#define PRIVATE
48
49#ifdef TEST
50#define MAXRHS 5 /* Set low to exercise exception code */
51#else
52#define MAXRHS 1000
53#endif
54
55char *msort();
56extern void *malloc();
57
58/******** From the file "action.h" *************************************/
59struct action *Action_new();
60struct action *Action_sort();
61void Action_add();
62
63/********* From the file "assert.h" ************************************/
64void myassert();
65#ifndef NDEBUG
66# define assert(X) if(!(X))myassert(__FILE__,__LINE__)
67#else
68# define assert(X)
69#endif
70
71/********** From the file "build.h" ************************************/
72void FindRulePrecedences();
73void FindFirstSets();
74void FindStates();
75void FindLinks();
76void FindFollowSets();
77void FindActions();
78
79/********* From the file "configlist.h" *********************************/
80void Configlist_init(/* void */);
81struct config *Configlist_add(/* struct rule *, int */);
82struct config *Configlist_addbasis(/* struct rule *, int */);
83void Configlist_closure(/* void */);
84void Configlist_sort(/* void */);
85void Configlist_sortbasis(/* void */);
86struct config *Configlist_return(/* void */);
87struct config *Configlist_basis(/* void */);
88void Configlist_eat(/* struct config * */);
89void Configlist_reset(/* void */);
90
91/********* From the file "error.h" ***************************************/
92void ErrorMsg( /* char *, int, char *, ... */ );
93
94/****** From the file "option.h" ******************************************/
95struct s_options {
96 enum { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR,
97 OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type;
98 char *label;
99 char *arg;
100 char *message;
101};
drhb0c86772000-06-02 23:21:26 +0000102int OptInit(/* char**,struct s_options*,FILE* */);
103int OptNArgs(/* void */);
104char *OptArg(/* int */);
105void OptErr(/* int */);
106void OptPrint(/* void */);
drh75897232000-05-29 14:26:00 +0000107
108/******** From the file "parse.h" *****************************************/
109void Parse(/* struct lemon *lemp */);
110
111/********* From the file "plink.h" ***************************************/
112struct plink *Plink_new(/* void */);
113void Plink_add(/* struct plink **, struct config * */);
114void Plink_copy(/* struct plink **, struct plink * */);
115void Plink_delete(/* struct plink * */);
116
117/********** From the file "report.h" *************************************/
118void Reprint(/* struct lemon * */);
119void ReportOutput(/* struct lemon * */);
120void ReportTable(/* struct lemon * */);
121void ReportHeader(/* struct lemon * */);
122void CompressTables(/* struct lemon * */);
123
124/********** From the file "set.h" ****************************************/
125void SetSize(/* int N */); /* All sets will be of size N */
126char *SetNew(/* void */); /* A new set for element 0..N */
127void SetFree(/* char* */); /* Deallocate a set */
128
129int SetAdd(/* char*,int */); /* Add element to a set */
130int SetUnion(/* char *A,char *B */); /* A <- A U B, thru element N */
131
132#define SetFind(X,Y) (X[Y]) /* True if Y is in set X */
133
134/********** From the file "struct.h" *************************************/
135/*
136** Principal data structures for the LEMON parser generator.
137*/
138
139typedef enum {FALSE=0, TRUE} Boolean;
140
141/* Symbols (terminals and nonterminals) of the grammar are stored
142** in the following: */
143struct symbol {
144 char *name; /* Name of the symbol */
145 int index; /* Index number for this symbol */
146 enum {
147 TERMINAL,
148 NONTERMINAL
149 } type; /* Symbols are all either TERMINALS or NTs */
150 struct rule *rule; /* Linked list of rules of this (if an NT) */
151 int prec; /* Precedence if defined (-1 otherwise) */
152 enum e_assoc {
153 LEFT,
154 RIGHT,
155 NONE,
156 UNK
157 } assoc; /* Associativity if predecence is defined */
158 char *firstset; /* First-set for all rules of this symbol */
159 Boolean lambda; /* True if NT and can generate an empty string */
160 char *destructor; /* Code which executes whenever this symbol is
161 ** popped from the stack during error processing */
162 int destructorln; /* Line number of destructor code */
163 char *datatype; /* The data type of information held by this
164 ** object. Only used if type==NONTERMINAL */
165 int dtnum; /* The data type number. In the parser, the value
166 ** stack is a union. The .yy%d element of this
167 ** union is the correct data type for this object */
168};
169
170/* Each production rule in the grammar is stored in the following
171** structure. */
172struct rule {
173 struct symbol *lhs; /* Left-hand side of the rule */
174 char *lhsalias; /* Alias for the LHS (NULL if none) */
175 int ruleline; /* Line number for the rule */
176 int nrhs; /* Number of RHS symbols */
177 struct symbol **rhs; /* The RHS symbols */
178 char **rhsalias; /* An alias for each RHS symbol (NULL if none) */
179 int line; /* Line number at which code begins */
180 char *code; /* The code executed when this rule is reduced */
181 struct symbol *precsym; /* Precedence symbol for this rule */
182 int index; /* An index number for this rule */
183 Boolean canReduce; /* True if this rule is ever reduced */
184 struct rule *nextlhs; /* Next rule with the same LHS */
185 struct rule *next; /* Next rule in the global list */
186};
187
188/* A configuration is a production rule of the grammar together with
189** a mark (dot) showing how much of that rule has been processed so far.
190** Configurations also contain a follow-set which is a list of terminal
191** symbols which are allowed to immediately follow the end of the rule.
192** Every configuration is recorded as an instance of the following: */
193struct config {
194 struct rule *rp; /* The rule upon which the configuration is based */
195 int dot; /* The parse point */
196 char *fws; /* Follow-set for this configuration only */
197 struct plink *fplp; /* Follow-set forward propagation links */
198 struct plink *bplp; /* Follow-set backwards propagation links */
199 struct state *stp; /* Pointer to state which contains this */
200 enum {
201 COMPLETE, /* The status is used during followset and */
202 INCOMPLETE /* shift computations */
203 } status;
204 struct config *next; /* Next configuration in the state */
205 struct config *bp; /* The next basis configuration */
206};
207
208/* Every shift or reduce operation is stored as one of the following */
209struct action {
210 struct symbol *sp; /* The look-ahead symbol */
211 enum e_action {
212 SHIFT,
213 ACCEPT,
214 REDUCE,
215 ERROR,
216 CONFLICT, /* Was a reduce, but part of a conflict */
217 SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
218 RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
219 NOT_USED /* Deleted by compression */
220 } type;
221 union {
222 struct state *stp; /* The new state, if a shift */
223 struct rule *rp; /* The rule, if a reduce */
224 } x;
225 struct action *next; /* Next action for this state */
226 struct action *collide; /* Next action with the same hash */
227};
228
229/* Each state of the generated parser's finite state machine
230** is encoded as an instance of the following structure. */
231struct state {
232 struct config *bp; /* The basis configurations for this state */
233 struct config *cfp; /* All configurations in this set */
234 int index; /* Sequencial number for this state */
235 struct action *ap; /* Array of actions for this state */
236 int naction; /* Number of actions for this state */
237 int tabstart; /* First index of the action table */
238 int tabdfltact; /* Default action */
239};
240
241/* A followset propagation link indicates that the contents of one
242** configuration followset should be propagated to another whenever
243** the first changes. */
244struct plink {
245 struct config *cfp; /* The configuration to which linked */
246 struct plink *next; /* The next propagate link */
247};
248
249/* The state vector for the entire parser generator is recorded as
250** follows. (LEMON uses no global variables and makes little use of
251** static variables. Fields in the following structure can be thought
252** of as begin global variables in the program.) */
253struct lemon {
254 struct state **sorted; /* Table of states sorted by state number */
255 struct rule *rule; /* List of all rules */
256 int nstate; /* Number of states */
257 int nrule; /* Number of rules */
258 int nsymbol; /* Number of terminal and nonterminal symbols */
259 int nterminal; /* Number of terminal symbols */
260 struct symbol **symbols; /* Sorted array of pointers to symbols */
261 int errorcnt; /* Number of errors */
262 struct symbol *errsym; /* The error symbol */
263 char *name; /* Name of the generated parser */
264 char *arg; /* Declaration of the 3th argument to parser */
265 char *tokentype; /* Type of terminal symbols in the parser stack */
266 char *start; /* Name of the start symbol for the grammar */
267 char *stacksize; /* Size of the parser stack */
268 char *include; /* Code to put at the start of the C file */
269 int includeln; /* Line number for start of include code */
270 char *error; /* Code to execute when an error is seen */
271 int errorln; /* Line number for start of error code */
272 char *overflow; /* Code to execute on a stack overflow */
273 int overflowln; /* Line number for start of overflow code */
274 char *failure; /* Code to execute on parser failure */
275 int failureln; /* Line number for start of failure code */
276 char *accept; /* Code to execute when the parser excepts */
277 int acceptln; /* Line number for the start of accept code */
278 char *extracode; /* Code appended to the generated file */
279 int extracodeln; /* Line number for the start of the extra code */
280 char *tokendest; /* Code to execute to destroy token data */
281 int tokendestln; /* Line number for token destroyer code */
282 char *filename; /* Name of the input file */
283 char *outname; /* Name of the current output file */
284 char *tokenprefix; /* A prefix added to token names in the .h file */
285 int nconflict; /* Number of parsing conflicts */
286 int tablesize; /* Size of the parse tables */
287 int basisflag; /* Print only basis configurations */
288 char *argv0; /* Name of the program */
289};
290
291#define MemoryCheck(X) if((X)==0){ \
292 extern void memory_error(); \
293 memory_error(); \
294}
295
296/**************** From the file "table.h" *********************************/
297/*
298** All code in this file has been automatically generated
299** from a specification in the file
300** "table.q"
301** by the associative array code building program "aagen".
302** Do not edit this file! Instead, edit the specification
303** file, then rerun aagen.
304*/
305/*
306** Code for processing tables in the LEMON parser generator.
307*/
308
309/* Routines for handling a strings */
310
311char *Strsafe();
312
313void Strsafe_init(/* void */);
314int Strsafe_insert(/* char * */);
315char *Strsafe_find(/* char * */);
316
317/* Routines for handling symbols of the grammar */
318
319struct symbol *Symbol_new();
320int Symbolcmpp(/* struct symbol **, struct symbol ** */);
321void Symbol_init(/* void */);
322int Symbol_insert(/* struct symbol *, char * */);
323struct symbol *Symbol_find(/* char * */);
324struct symbol *Symbol_Nth(/* int */);
325int Symbol_count(/* */);
326struct symbol **Symbol_arrayof(/* */);
327
328/* Routines to manage the state table */
329
330int Configcmp(/* struct config *, struct config * */);
331struct state *State_new();
332void State_init(/* void */);
333int State_insert(/* struct state *, struct config * */);
334struct state *State_find(/* struct config * */);
335struct state **State_arrayof(/* */);
336
337/* Routines used for efficiency in Configlist_add */
338
339void Configtable_init(/* void */);
340int Configtable_insert(/* struct config * */);
341struct config *Configtable_find(/* struct config * */);
342void Configtable_clear(/* int(*)(struct config *) */);
343/****************** From the file "action.c" *******************************/
344/*
345** Routines processing parser actions in the LEMON parser generator.
346*/
347
348/* Allocate a new parser action */
349struct action *Action_new(){
350 static struct action *freelist = 0;
351 struct action *new;
352
353 if( freelist==0 ){
354 int i;
355 int amt = 100;
356 freelist = (struct action *)malloc( sizeof(struct action)*amt );
357 if( freelist==0 ){
358 fprintf(stderr,"Unable to allocate memory for a new parser action.");
359 exit(1);
360 }
361 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
362 freelist[amt-1].next = 0;
363 }
364 new = freelist;
365 freelist = freelist->next;
366 return new;
367}
368
369/* Compare two actions */
370static int actioncmp(ap1,ap2)
371struct action *ap1;
372struct action *ap2;
373{
374 int rc;
375 rc = ap1->sp->index - ap2->sp->index;
376 if( rc==0 ) rc = (int)ap1->type - (int)ap2->type;
377 if( rc==0 ){
drh61bc2722000-08-20 11:42:46 +0000378 assert( ap1->type==REDUCE || ap1->type==RD_RESOLVED || ap1->type==CONFLICT);
379 assert( ap2->type==REDUCE || ap2->type==RD_RESOLVED || ap2->type==CONFLICT);
drh75897232000-05-29 14:26:00 +0000380 rc = ap1->x.rp->index - ap2->x.rp->index;
381 }
382 return rc;
383}
384
385/* Sort parser actions */
386struct action *Action_sort(ap)
387struct action *ap;
388{
389 ap = (struct action *)msort(ap,&ap->next,actioncmp);
390 return ap;
391}
392
393void Action_add(app,type,sp,arg)
394struct action **app;
395enum e_action type;
396struct symbol *sp;
397char *arg;
398{
399 struct action *new;
400 new = Action_new();
401 new->next = *app;
402 *app = new;
403 new->type = type;
404 new->sp = sp;
405 if( type==SHIFT ){
406 new->x.stp = (struct state *)arg;
407 }else{
408 new->x.rp = (struct rule *)arg;
409 }
410}
411/********************** From the file "assert.c" ****************************/
412/*
413** A more efficient way of handling assertions.
414*/
415void myassert(file,line)
416char *file;
417int line;
418{
419 fprintf(stderr,"Assertion failed on line %d of file \"%s\"\n",line,file);
420 exit(1);
421}
422/********************** From the file "build.c" *****************************/
423/*
424** Routines to construction the finite state machine for the LEMON
425** parser generator.
426*/
427
428/* Find a precedence symbol of every rule in the grammar.
429**
430** Those rules which have a precedence symbol coded in the input
431** grammar using the "[symbol]" construct will already have the
432** rp->precsym field filled. Other rules take as their precedence
433** symbol the first RHS symbol with a defined precedence. If there
434** are not RHS symbols with a defined precedence, the precedence
435** symbol field is left blank.
436*/
437void FindRulePrecedences(xp)
438struct lemon *xp;
439{
440 struct rule *rp;
441 for(rp=xp->rule; rp; rp=rp->next){
442 if( rp->precsym==0 ){
443 int i;
444 for(i=0; i<rp->nrhs; i++){
445 if( rp->rhs[i]->prec>=0 ){
446 rp->precsym = rp->rhs[i];
447 break;
448 }
449 }
450 }
451 }
452 return;
453}
454
455/* Find all nonterminals which will generate the empty string.
456** Then go back and compute the first sets of every nonterminal.
457** The first set is the set of all terminal symbols which can begin
458** a string generated by that nonterminal.
459*/
460void FindFirstSets(lemp)
461struct lemon *lemp;
462{
463 int i;
464 struct rule *rp;
465 int progress;
466
467 for(i=0; i<lemp->nsymbol; i++){
468 lemp->symbols[i]->lambda = FALSE;
469 }
470 for(i=lemp->nterminal; i<lemp->nsymbol; i++){
471 lemp->symbols[i]->firstset = SetNew();
472 }
473
474 /* First compute all lambdas */
475 do{
476 progress = 0;
477 for(rp=lemp->rule; rp; rp=rp->next){
478 if( rp->lhs->lambda ) continue;
479 for(i=0; i<rp->nrhs; i++){
480 if( rp->rhs[i]->lambda==FALSE ) break;
481 }
482 if( i==rp->nrhs ){
483 rp->lhs->lambda = TRUE;
484 progress = 1;
485 }
486 }
487 }while( progress );
488
489 /* Now compute all first sets */
490 do{
491 struct symbol *s1, *s2;
492 progress = 0;
493 for(rp=lemp->rule; rp; rp=rp->next){
494 s1 = rp->lhs;
495 for(i=0; i<rp->nrhs; i++){
496 s2 = rp->rhs[i];
497 if( s2->type==TERMINAL ){
498 progress += SetAdd(s1->firstset,s2->index);
499 break;
500 }else if( s1==s2 ){
501 if( s1->lambda==FALSE ) break;
502 }else{
503 progress += SetUnion(s1->firstset,s2->firstset);
504 if( s2->lambda==FALSE ) break;
505 }
506 }
507 }
508 }while( progress );
509 return;
510}
511
512/* Compute all LR(0) states for the grammar. Links
513** are added to between some states so that the LR(1) follow sets
514** can be computed later.
515*/
516PRIVATE struct state *getstate(/* struct lemon * */); /* forward reference */
517void FindStates(lemp)
518struct lemon *lemp;
519{
520 struct symbol *sp;
521 struct rule *rp;
522
523 Configlist_init();
524
525 /* Find the start symbol */
526 if( lemp->start ){
527 sp = Symbol_find(lemp->start);
528 if( sp==0 ){
529 ErrorMsg(lemp->filename,0,
530"The specified start symbol \"%s\" is not \
531in a nonterminal of the grammar. \"%s\" will be used as the start \
532symbol instead.",lemp->start,lemp->rule->lhs->name);
533 lemp->errorcnt++;
534 sp = lemp->rule->lhs;
535 }
536 }else{
537 sp = lemp->rule->lhs;
538 }
539
540 /* Make sure the start symbol doesn't occur on the right-hand side of
541 ** any rule. Report an error if it does. (YACC would generate a new
542 ** start symbol in this case.) */
543 for(rp=lemp->rule; rp; rp=rp->next){
544 int i;
545 for(i=0; i<rp->nrhs; i++){
546 if( rp->rhs[i]==sp ){
547 ErrorMsg(lemp->filename,0,
548"The start symbol \"%s\" occurs on the \
549right-hand side of a rule. This will result in a parser which \
550does not work properly.",sp->name);
551 lemp->errorcnt++;
552 }
553 }
554 }
555
556 /* The basis configuration set for the first state
557 ** is all rules which have the start symbol as their
558 ** left-hand side */
559 for(rp=sp->rule; rp; rp=rp->nextlhs){
560 struct config *newcfp;
561 newcfp = Configlist_addbasis(rp,0);
562 SetAdd(newcfp->fws,0);
563 }
564
565 /* Compute the first state. All other states will be
566 ** computed automatically during the computation of the first one.
567 ** The returned pointer to the first state is not used. */
568 (void)getstate(lemp);
569 return;
570}
571
572/* Return a pointer to a state which is described by the configuration
573** list which has been built from calls to Configlist_add.
574*/
575PRIVATE void buildshifts(/* struct lemon *, struct state * */); /* Forwd ref */
576PRIVATE struct state *getstate(lemp)
577struct lemon *lemp;
578{
579 struct config *cfp, *bp;
580 struct state *stp;
581
582 /* Extract the sorted basis of the new state. The basis was constructed
583 ** by prior calls to "Configlist_addbasis()". */
584 Configlist_sortbasis();
585 bp = Configlist_basis();
586
587 /* Get a state with the same basis */
588 stp = State_find(bp);
589 if( stp ){
590 /* A state with the same basis already exists! Copy all the follow-set
591 ** propagation links from the state under construction into the
592 ** preexisting state, then return a pointer to the preexisting state */
593 struct config *x, *y;
594 for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
595 Plink_copy(&y->bplp,x->bplp);
596 Plink_delete(x->fplp);
597 x->fplp = x->bplp = 0;
598 }
599 cfp = Configlist_return();
600 Configlist_eat(cfp);
601 }else{
602 /* This really is a new state. Construct all the details */
603 Configlist_closure(lemp); /* Compute the configuration closure */
604 Configlist_sort(); /* Sort the configuration closure */
605 cfp = Configlist_return(); /* Get a pointer to the config list */
606 stp = State_new(); /* A new state structure */
607 MemoryCheck(stp);
608 stp->bp = bp; /* Remember the configuration basis */
609 stp->cfp = cfp; /* Remember the configuration closure */
610 stp->index = lemp->nstate++; /* Every state gets a sequence number */
611 stp->ap = 0; /* No actions, yet. */
612 State_insert(stp,stp->bp); /* Add to the state table */
613 buildshifts(lemp,stp); /* Recursively compute successor states */
614 }
615 return stp;
616}
617
618/* Construct all successor states to the given state. A "successor"
619** state is any state which can be reached by a shift action.
620*/
621PRIVATE void buildshifts(lemp,stp)
622struct lemon *lemp;
623struct state *stp; /* The state from which successors are computed */
624{
625 struct config *cfp; /* For looping thru the config closure of "stp" */
626 struct config *bcfp; /* For the inner loop on config closure of "stp" */
627 struct config *new; /* */
628 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
629 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
630 struct state *newstp; /* A pointer to a successor state */
631
632 /* Each configuration becomes complete after it contibutes to a successor
633 ** state. Initially, all configurations are incomplete */
634 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
635
636 /* Loop through all configurations of the state "stp" */
637 for(cfp=stp->cfp; cfp; cfp=cfp->next){
638 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
639 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
640 Configlist_reset(); /* Reset the new config set */
641 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
642
643 /* For every configuration in the state "stp" which has the symbol "sp"
644 ** following its dot, add the same configuration to the basis set under
645 ** construction but with the dot shifted one symbol to the right. */
646 for(bcfp=cfp; bcfp; bcfp=bcfp->next){
647 if( bcfp->status==COMPLETE ) continue; /* Already used */
648 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
649 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
650 if( bsp!=sp ) continue; /* Must be same as for "cfp" */
651 bcfp->status = COMPLETE; /* Mark this config as used */
652 new = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
653 Plink_add(&new->bplp,bcfp);
654 }
655
656 /* Get a pointer to the state described by the basis configuration set
657 ** constructed in the preceding loop */
658 newstp = getstate(lemp);
659
660 /* The state "newstp" is reached from the state "stp" by a shift action
661 ** on the symbol "sp" */
662 Action_add(&stp->ap,SHIFT,sp,newstp);
663 }
664}
665
666/*
667** Construct the propagation links
668*/
669void FindLinks(lemp)
670struct lemon *lemp;
671{
672 int i;
673 struct config *cfp, *other;
674 struct state *stp;
675 struct plink *plp;
676
677 /* Housekeeping detail:
678 ** Add to every propagate link a pointer back to the state to
679 ** which the link is attached. */
680 for(i=0; i<lemp->nstate; i++){
681 stp = lemp->sorted[i];
682 for(cfp=stp->cfp; cfp; cfp=cfp->next){
683 cfp->stp = stp;
684 }
685 }
686
687 /* Convert all backlinks into forward links. Only the forward
688 ** links are used in the follow-set computation. */
689 for(i=0; i<lemp->nstate; i++){
690 stp = lemp->sorted[i];
691 for(cfp=stp->cfp; cfp; cfp=cfp->next){
692 for(plp=cfp->bplp; plp; plp=plp->next){
693 other = plp->cfp;
694 Plink_add(&other->fplp,cfp);
695 }
696 }
697 }
698}
699
700/* Compute all followsets.
701**
702** A followset is the set of all symbols which can come immediately
703** after a configuration.
704*/
705void FindFollowSets(lemp)
706struct lemon *lemp;
707{
708 int i;
709 struct config *cfp;
710 struct plink *plp;
711 int progress;
712 int change;
713
714 for(i=0; i<lemp->nstate; i++){
715 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
716 cfp->status = INCOMPLETE;
717 }
718 }
719
720 do{
721 progress = 0;
722 for(i=0; i<lemp->nstate; i++){
723 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
724 if( cfp->status==COMPLETE ) continue;
725 for(plp=cfp->fplp; plp; plp=plp->next){
726 change = SetUnion(plp->cfp->fws,cfp->fws);
727 if( change ){
728 plp->cfp->status = INCOMPLETE;
729 progress = 1;
730 }
731 }
732 cfp->status = COMPLETE;
733 }
734 }
735 }while( progress );
736}
737
738static int resolve_conflict();
739
740/* Compute the reduce actions, and resolve conflicts.
741*/
742void FindActions(lemp)
743struct lemon *lemp;
744{
745 int i,j;
746 struct config *cfp;
747 struct state *stp;
748 struct symbol *sp;
749 struct rule *rp;
750
751 /* Add all of the reduce actions
752 ** A reduce action is added for each element of the followset of
753 ** a configuration which has its dot at the extreme right.
754 */
755 for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
756 stp = lemp->sorted[i];
757 for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
758 if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
759 for(j=0; j<lemp->nterminal; j++){
760 if( SetFind(cfp->fws,j) ){
761 /* Add a reduce action to the state "stp" which will reduce by the
762 ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
763 Action_add(&stp->ap,REDUCE,lemp->symbols[j],cfp->rp);
764 }
765 }
766 }
767 }
768 }
769
770 /* Add the accepting token */
771 if( lemp->start ){
772 sp = Symbol_find(lemp->start);
773 if( sp==0 ) sp = lemp->rule->lhs;
774 }else{
775 sp = lemp->rule->lhs;
776 }
777 /* Add to the first state (which is always the starting state of the
778 ** finite state machine) an action to ACCEPT if the lookahead is the
779 ** start nonterminal. */
780 Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
781
782 /* Resolve conflicts */
783 for(i=0; i<lemp->nstate; i++){
784 struct action *ap, *nap;
785 struct state *stp;
786 stp = lemp->sorted[i];
787 assert( stp->ap );
788 stp->ap = Action_sort(stp->ap);
789 for(ap=stp->ap; ap && ap->next; ap=nap){
790 for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
791 /* The two actions "ap" and "nap" have the same lookahead.
792 ** Figure out which one should be used */
793 lemp->nconflict += resolve_conflict(ap,nap,lemp->errsym);
794 }
795 }
796 }
797
798 /* Report an error for each rule that can never be reduced. */
799 for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = FALSE;
800 for(i=0; i<lemp->nstate; i++){
801 struct action *ap;
802 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
803 if( ap->type==REDUCE ) ap->x.rp->canReduce = TRUE;
804 }
805 }
806 for(rp=lemp->rule; rp; rp=rp->next){
807 if( rp->canReduce ) continue;
808 ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
809 lemp->errorcnt++;
810 }
811}
812
813/* Resolve a conflict between the two given actions. If the
814** conflict can't be resolve, return non-zero.
815**
816** NO LONGER TRUE:
817** To resolve a conflict, first look to see if either action
818** is on an error rule. In that case, take the action which
819** is not associated with the error rule. If neither or both
820** actions are associated with an error rule, then try to
821** use precedence to resolve the conflict.
822**
823** If either action is a SHIFT, then it must be apx. This
824** function won't work if apx->type==REDUCE and apy->type==SHIFT.
825*/
826static int resolve_conflict(apx,apy,errsym)
827struct action *apx;
828struct action *apy;
829struct symbol *errsym; /* The error symbol (if defined. NULL otherwise) */
830{
831 struct symbol *spx, *spy;
832 int errcnt = 0;
833 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
834 if( apx->type==SHIFT && apy->type==REDUCE ){
835 spx = apx->sp;
836 spy = apy->x.rp->precsym;
837 if( spy==0 || spx->prec<0 || spy->prec<0 ){
838 /* Not enough precedence information. */
839 apy->type = CONFLICT;
840 errcnt++;
841 }else if( spx->prec>spy->prec ){ /* Lower precedence wins */
842 apy->type = RD_RESOLVED;
843 }else if( spx->prec<spy->prec ){
844 apx->type = SH_RESOLVED;
845 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
846 apy->type = RD_RESOLVED; /* associativity */
847 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
848 apx->type = SH_RESOLVED;
849 }else{
850 assert( spx->prec==spy->prec && spx->assoc==NONE );
851 apy->type = CONFLICT;
852 errcnt++;
853 }
854 }else if( apx->type==REDUCE && apy->type==REDUCE ){
855 spx = apx->x.rp->precsym;
856 spy = apy->x.rp->precsym;
857 if( spx==0 || spy==0 || spx->prec<0 ||
858 spy->prec<0 || spx->prec==spy->prec ){
859 apy->type = CONFLICT;
860 errcnt++;
861 }else if( spx->prec>spy->prec ){
862 apy->type = RD_RESOLVED;
863 }else if( spx->prec<spy->prec ){
864 apx->type = RD_RESOLVED;
865 }
866 }else{
867 /* Can't happen. Shifts have to come before Reduces on the
868 ** list because the reduces were added last. Hence, if apx->type==REDUCE
869 ** then it is impossible for apy->type==SHIFT */
870 }
871 return errcnt;
872}
873/********************* From the file "configlist.c" *************************/
874/*
875** Routines to processing a configuration list and building a state
876** in the LEMON parser generator.
877*/
878
879static struct config *freelist = 0; /* List of free configurations */
880static struct config *current = 0; /* Top of list of configurations */
881static struct config **currentend = 0; /* Last on list of configs */
882static struct config *basis = 0; /* Top of list of basis configs */
883static struct config **basisend = 0; /* End of list of basis configs */
884
885/* Return a pointer to a new configuration */
886PRIVATE struct config *newconfig(){
887 struct config *new;
888 if( freelist==0 ){
889 int i;
890 int amt = 3;
891 freelist = (struct config *)malloc( sizeof(struct config)*amt );
892 if( freelist==0 ){
893 fprintf(stderr,"Unable to allocate memory for a new configuration.");
894 exit(1);
895 }
896 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
897 freelist[amt-1].next = 0;
898 }
899 new = freelist;
900 freelist = freelist->next;
901 return new;
902}
903
904/* The configuration "old" is no longer used */
905PRIVATE void deleteconfig(old)
906struct config *old;
907{
908 old->next = freelist;
909 freelist = old;
910}
911
912/* Initialized the configuration list builder */
913void Configlist_init(){
914 current = 0;
915 currentend = &current;
916 basis = 0;
917 basisend = &basis;
918 Configtable_init();
919 return;
920}
921
922/* Initialized the configuration list builder */
923void Configlist_reset(){
924 current = 0;
925 currentend = &current;
926 basis = 0;
927 basisend = &basis;
928 Configtable_clear(0);
929 return;
930}
931
932/* Add another configuration to the configuration list */
933struct config *Configlist_add(rp,dot)
934struct rule *rp; /* The rule */
935int dot; /* Index into the RHS of the rule where the dot goes */
936{
937 struct config *cfp, model;
938
939 assert( currentend!=0 );
940 model.rp = rp;
941 model.dot = dot;
942 cfp = Configtable_find(&model);
943 if( cfp==0 ){
944 cfp = newconfig();
945 cfp->rp = rp;
946 cfp->dot = dot;
947 cfp->fws = SetNew();
948 cfp->stp = 0;
949 cfp->fplp = cfp->bplp = 0;
950 cfp->next = 0;
951 cfp->bp = 0;
952 *currentend = cfp;
953 currentend = &cfp->next;
954 Configtable_insert(cfp);
955 }
956 return cfp;
957}
958
959/* Add a basis configuration to the configuration list */
960struct config *Configlist_addbasis(rp,dot)
961struct rule *rp;
962int dot;
963{
964 struct config *cfp, model;
965
966 assert( basisend!=0 );
967 assert( currentend!=0 );
968 model.rp = rp;
969 model.dot = dot;
970 cfp = Configtable_find(&model);
971 if( cfp==0 ){
972 cfp = newconfig();
973 cfp->rp = rp;
974 cfp->dot = dot;
975 cfp->fws = SetNew();
976 cfp->stp = 0;
977 cfp->fplp = cfp->bplp = 0;
978 cfp->next = 0;
979 cfp->bp = 0;
980 *currentend = cfp;
981 currentend = &cfp->next;
982 *basisend = cfp;
983 basisend = &cfp->bp;
984 Configtable_insert(cfp);
985 }
986 return cfp;
987}
988
989/* Compute the closure of the configuration list */
990void Configlist_closure(lemp)
991struct lemon *lemp;
992{
993 struct config *cfp, *newcfp;
994 struct rule *rp, *newrp;
995 struct symbol *sp, *xsp;
996 int i, dot;
997
998 assert( currentend!=0 );
999 for(cfp=current; cfp; cfp=cfp->next){
1000 rp = cfp->rp;
1001 dot = cfp->dot;
1002 if( dot>=rp->nrhs ) continue;
1003 sp = rp->rhs[dot];
1004 if( sp->type==NONTERMINAL ){
1005 if( sp->rule==0 && sp!=lemp->errsym ){
1006 ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
1007 sp->name);
1008 lemp->errorcnt++;
1009 }
1010 for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
1011 newcfp = Configlist_add(newrp,0);
1012 for(i=dot+1; i<rp->nrhs; i++){
1013 xsp = rp->rhs[i];
1014 if( xsp->type==TERMINAL ){
1015 SetAdd(newcfp->fws,xsp->index);
1016 break;
1017 }else{
1018 SetUnion(newcfp->fws,xsp->firstset);
1019 if( xsp->lambda==FALSE ) break;
1020 }
1021 }
1022 if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
1023 }
1024 }
1025 }
1026 return;
1027}
1028
1029/* Sort the configuration list */
1030void Configlist_sort(){
1031 current = (struct config *)msort(current,&(current->next),Configcmp);
1032 currentend = 0;
1033 return;
1034}
1035
1036/* Sort the basis configuration list */
1037void Configlist_sortbasis(){
1038 basis = (struct config *)msort(current,&(current->bp),Configcmp);
1039 basisend = 0;
1040 return;
1041}
1042
1043/* Return a pointer to the head of the configuration list and
1044** reset the list */
1045struct config *Configlist_return(){
1046 struct config *old;
1047 old = current;
1048 current = 0;
1049 currentend = 0;
1050 return old;
1051}
1052
1053/* Return a pointer to the head of the configuration list and
1054** reset the list */
1055struct config *Configlist_basis(){
1056 struct config *old;
1057 old = basis;
1058 basis = 0;
1059 basisend = 0;
1060 return old;
1061}
1062
1063/* Free all elements of the given configuration list */
1064void Configlist_eat(cfp)
1065struct config *cfp;
1066{
1067 struct config *nextcfp;
1068 for(; cfp; cfp=nextcfp){
1069 nextcfp = cfp->next;
1070 assert( cfp->fplp==0 );
1071 assert( cfp->bplp==0 );
1072 if( cfp->fws ) SetFree(cfp->fws);
1073 deleteconfig(cfp);
1074 }
1075 return;
1076}
1077/***************** From the file "error.c" *********************************/
1078/*
1079** Code for printing error message.
1080*/
1081
1082/* Find a good place to break "msg" so that its length is at least "min"
1083** but no more than "max". Make the point as close to max as possible.
1084*/
1085static int findbreak(msg,min,max)
1086char *msg;
1087int min;
1088int max;
1089{
1090 int i,spot;
1091 char c;
1092 for(i=spot=min; i<=max; i++){
1093 c = msg[i];
1094 if( c=='\t' ) msg[i] = ' ';
1095 if( c=='\n' ){ msg[i] = ' '; spot = i; break; }
1096 if( c==0 ){ spot = i; break; }
1097 if( c=='-' && i<max-1 ) spot = i+1;
1098 if( c==' ' ) spot = i;
1099 }
1100 return spot;
1101}
1102
1103/*
1104** The error message is split across multiple lines if necessary. The
1105** splits occur at a space, if there is a space available near the end
1106** of the line.
1107*/
1108#define ERRMSGSIZE 10000 /* Hope this is big enough. No way to error check */
1109#define LINEWIDTH 79 /* Max width of any output line */
1110#define PREFIXLIMIT 30 /* Max width of the prefix on each line */
1111void ErrorMsg(va_alist)
1112va_dcl
1113{
1114 char *filename;
1115 int lineno;
1116 char *format;
1117 char errmsg[ERRMSGSIZE];
1118 char prefix[PREFIXLIMIT+10];
1119 int errmsgsize;
1120 int prefixsize;
1121 int availablewidth;
1122 va_list ap;
1123 int end, restart, base;
1124
1125 va_start(ap);
1126 filename = va_arg(ap,char*);
1127 lineno = va_arg(ap,int);
1128 format = va_arg(ap,char*);
1129 /* Prepare a prefix to be prepended to every output line */
1130 if( lineno>0 ){
1131 sprintf(prefix,"%.*s:%d: ",PREFIXLIMIT-10,filename,lineno);
1132 }else{
1133 sprintf(prefix,"%.*s: ",PREFIXLIMIT-10,filename);
1134 }
1135 prefixsize = strlen(prefix);
1136 availablewidth = LINEWIDTH - prefixsize;
1137
1138 /* Generate the error message */
1139 vsprintf(errmsg,format,ap);
1140 va_end(ap);
1141 errmsgsize = strlen(errmsg);
1142 /* Remove trailing '\n's from the error message. */
1143 while( errmsgsize>0 && errmsg[errmsgsize-1]=='\n' ){
1144 errmsg[--errmsgsize] = 0;
1145 }
1146
1147 /* Print the error message */
1148 base = 0;
1149 while( errmsg[base]!=0 ){
1150 end = restart = findbreak(&errmsg[base],0,availablewidth);
1151 restart += base;
1152 while( errmsg[restart]==' ' ) restart++;
1153 fprintf(stdout,"%s%.*s\n",prefix,end,&errmsg[base]);
1154 base = restart;
1155 }
1156}
1157/**************** From the file "main.c" ************************************/
1158/*
1159** Main program file for the LEMON parser generator.
1160*/
1161
1162/* Report an out-of-memory condition and abort. This function
1163** is used mostly by the "MemoryCheck" macro in struct.h
1164*/
1165void memory_error(){
1166 fprintf(stderr,"Out of memory. Aborting...\n");
1167 exit(1);
1168}
1169
1170
1171/* The main program. Parse the command line and do it... */
1172int main(argc,argv)
1173int argc;
1174char **argv;
1175{
1176 static int version = 0;
1177 static int rpflag = 0;
1178 static int basisflag = 0;
1179 static int compress = 0;
1180 static int quiet = 0;
1181 static int statistics = 0;
1182 static int mhflag = 0;
1183 static struct s_options options[] = {
1184 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1185 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
1186 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
1187 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file"},
1188 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
1189 {OPT_FLAG, "s", (char*)&statistics, "Print parser stats to standard output."},
1190 {OPT_FLAG, "x", (char*)&version, "Print the version number."},
1191 {OPT_FLAG,0,0,0}
1192 };
1193 int i;
1194 struct lemon lem;
1195
drhb0c86772000-06-02 23:21:26 +00001196 OptInit(argv,options,stderr);
drh75897232000-05-29 14:26:00 +00001197 if( version ){
1198 printf("Lemon version 1.0\n"
1199 "Copyright 1991-1997 by D. Richard Hipp\n"
1200 "Freely distributable under the GNU Public License.\n"
1201 );
1202 exit(0);
1203 }
drhb0c86772000-06-02 23:21:26 +00001204 if( OptNArgs()!=1 ){
drh75897232000-05-29 14:26:00 +00001205 fprintf(stderr,"Exactly one filename argument is required.\n");
1206 exit(1);
1207 }
1208 lem.errorcnt = 0;
1209
1210 /* Initialize the machine */
1211 Strsafe_init();
1212 Symbol_init();
1213 State_init();
1214 lem.argv0 = argv[0];
drhb0c86772000-06-02 23:21:26 +00001215 lem.filename = OptArg(0);
drh75897232000-05-29 14:26:00 +00001216 lem.basisflag = basisflag;
1217 lem.nconflict = 0;
1218 lem.name = lem.include = lem.arg = lem.tokentype = lem.start = 0;
1219 lem.stacksize = 0;
1220 lem.error = lem.overflow = lem.failure = lem.accept = lem.tokendest =
1221 lem.tokenprefix = lem.outname = lem.extracode = 0;
1222 lem.tablesize = 0;
1223 Symbol_new("$");
1224 lem.errsym = Symbol_new("error");
1225
1226 /* Parse the input file */
1227 Parse(&lem);
1228 if( lem.errorcnt ) exit(lem.errorcnt);
1229 if( lem.rule==0 ){
1230 fprintf(stderr,"Empty grammar.\n");
1231 exit(1);
1232 }
1233
1234 /* Count and index the symbols of the grammar */
1235 lem.nsymbol = Symbol_count();
1236 Symbol_new("{default}");
1237 lem.symbols = Symbol_arrayof();
1238 qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*),
1239 (int(*)())Symbolcmpp);
1240 for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
1241 for(i=1; isupper(lem.symbols[i]->name[0]); i++);
1242 lem.nterminal = i;
1243
1244 /* Generate a reprint of the grammar, if requested on the command line */
1245 if( rpflag ){
1246 Reprint(&lem);
1247 }else{
1248 /* Initialize the size for all follow and first sets */
1249 SetSize(lem.nterminal);
1250
1251 /* Find the precedence for every production rule (that has one) */
1252 FindRulePrecedences(&lem);
1253
1254 /* Compute the lambda-nonterminals and the first-sets for every
1255 ** nonterminal */
1256 FindFirstSets(&lem);
1257
1258 /* Compute all LR(0) states. Also record follow-set propagation
1259 ** links so that the follow-set can be computed later */
1260 lem.nstate = 0;
1261 FindStates(&lem);
1262 lem.sorted = State_arrayof();
1263
1264 /* Tie up loose ends on the propagation links */
1265 FindLinks(&lem);
1266
1267 /* Compute the follow set of every reducible configuration */
1268 FindFollowSets(&lem);
1269
1270 /* Compute the action tables */
1271 FindActions(&lem);
1272
1273 /* Compress the action tables */
1274 if( compress==0 ) CompressTables(&lem);
1275
1276 /* Generate a report of the parser generated. (the "y.output" file) */
1277 if( !quiet ) ReportOutput(&lem);
1278
1279 /* Generate the source code for the parser */
1280 ReportTable(&lem, mhflag);
1281
1282 /* Produce a header file for use by the scanner. (This step is
1283 ** omitted if the "-m" option is used because makeheaders will
1284 ** generate the file for us.) */
1285 if( !mhflag ) ReportHeader(&lem);
1286 }
1287 if( statistics ){
1288 printf("Parser statistics: %d terminals, %d nonterminals, %d rules\n",
1289 lem.nterminal, lem.nsymbol - lem.nterminal, lem.nrule);
1290 printf(" %d states, %d parser table entries, %d conflicts\n",
1291 lem.nstate, lem.tablesize, lem.nconflict);
1292 }
1293 if( lem.nconflict ){
1294 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
1295 }
1296 exit(lem.errorcnt + lem.nconflict);
1297}
1298/******************** From the file "msort.c" *******************************/
1299/*
1300** A generic merge-sort program.
1301**
1302** USAGE:
1303** Let "ptr" be a pointer to some structure which is at the head of
1304** a null-terminated list. Then to sort the list call:
1305**
1306** ptr = msort(ptr,&(ptr->next),cmpfnc);
1307**
1308** In the above, "cmpfnc" is a pointer to a function which compares
1309** two instances of the structure and returns an integer, as in
1310** strcmp. The second argument is a pointer to the pointer to the
1311** second element of the linked list. This address is used to compute
1312** the offset to the "next" field within the structure. The offset to
1313** the "next" field must be constant for all structures in the list.
1314**
1315** The function returns a new pointer which is the head of the list
1316** after sorting.
1317**
1318** ALGORITHM:
1319** Merge-sort.
1320*/
1321
1322/*
1323** Return a pointer to the next structure in the linked list.
1324*/
1325#define NEXT(A) (*(char**)(((int)A)+offset))
1326
1327/*
1328** Inputs:
1329** a: A sorted, null-terminated linked list. (May be null).
1330** b: A sorted, null-terminated linked list. (May be null).
1331** cmp: A pointer to the comparison function.
1332** offset: Offset in the structure to the "next" field.
1333**
1334** Return Value:
1335** A pointer to the head of a sorted list containing the elements
1336** of both a and b.
1337**
1338** Side effects:
1339** The "next" pointers for elements in the lists a and b are
1340** changed.
1341*/
1342static char *merge(a,b,cmp,offset)
1343char *a;
1344char *b;
1345int (*cmp)();
1346int offset;
1347{
1348 char *ptr, *head;
1349
1350 if( a==0 ){
1351 head = b;
1352 }else if( b==0 ){
1353 head = a;
1354 }else{
1355 if( (*cmp)(a,b)<0 ){
1356 ptr = a;
1357 a = NEXT(a);
1358 }else{
1359 ptr = b;
1360 b = NEXT(b);
1361 }
1362 head = ptr;
1363 while( a && b ){
1364 if( (*cmp)(a,b)<0 ){
1365 NEXT(ptr) = a;
1366 ptr = a;
1367 a = NEXT(a);
1368 }else{
1369 NEXT(ptr) = b;
1370 ptr = b;
1371 b = NEXT(b);
1372 }
1373 }
1374 if( a ) NEXT(ptr) = a;
1375 else NEXT(ptr) = b;
1376 }
1377 return head;
1378}
1379
1380/*
1381** Inputs:
1382** list: Pointer to a singly-linked list of structures.
1383** next: Pointer to pointer to the second element of the list.
1384** cmp: A comparison function.
1385**
1386** Return Value:
1387** A pointer to the head of a sorted list containing the elements
1388** orginally in list.
1389**
1390** Side effects:
1391** The "next" pointers for elements in list are changed.
1392*/
1393#define LISTSIZE 30
1394char *msort(list,next,cmp)
1395char *list;
1396char **next;
1397int (*cmp)();
1398{
1399 int offset;
1400 char *ep;
1401 char *set[LISTSIZE];
1402 int i;
1403 offset = (int)next - (int)list;
1404 for(i=0; i<LISTSIZE; i++) set[i] = 0;
1405 while( list ){
1406 ep = list;
1407 list = NEXT(list);
1408 NEXT(ep) = 0;
1409 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1410 ep = merge(ep,set[i],cmp,offset);
1411 set[i] = 0;
1412 }
1413 set[i] = ep;
1414 }
1415 ep = 0;
1416 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(ep,set[i],cmp,offset);
1417 return ep;
1418}
1419/************************ From the file "option.c" **************************/
1420static char **argv;
1421static struct s_options *op;
1422static FILE *errstream;
1423
1424#define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1425
1426/*
1427** Print the command line with a carrot pointing to the k-th character
1428** of the n-th field.
1429*/
1430static void errline(n,k,err)
1431int n;
1432int k;
1433FILE *err;
1434{
1435 int spcnt, i;
1436 spcnt = 0;
1437 if( argv[0] ) fprintf(err,"%s",argv[0]);
1438 spcnt = strlen(argv[0]) + 1;
1439 for(i=1; i<n && argv[i]; i++){
1440 fprintf(err," %s",argv[i]);
1441 spcnt += strlen(argv[i]+1);
1442 }
1443 spcnt += k;
1444 for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1445 if( spcnt<20 ){
1446 fprintf(err,"\n%*s^-- here\n",spcnt,"");
1447 }else{
1448 fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1449 }
1450}
1451
1452/*
1453** Return the index of the N-th non-switch argument. Return -1
1454** if N is out of range.
1455*/
1456static int argindex(n)
1457int n;
1458{
1459 int i;
1460 int dashdash = 0;
1461 if( argv!=0 && *argv!=0 ){
1462 for(i=1; argv[i]; i++){
1463 if( dashdash || !ISOPT(argv[i]) ){
1464 if( n==0 ) return i;
1465 n--;
1466 }
1467 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1468 }
1469 }
1470 return -1;
1471}
1472
1473static char emsg[] = "Command line syntax error: ";
1474
1475/*
1476** Process a flag command line argument.
1477*/
1478static int handleflags(i,err)
1479int i;
1480FILE *err;
1481{
1482 int v;
1483 int errcnt = 0;
1484 int j;
1485 for(j=0; op[j].label; j++){
1486 if( strcmp(&argv[i][1],op[j].label)==0 ) break;
1487 }
1488 v = argv[i][0]=='-' ? 1 : 0;
1489 if( op[j].label==0 ){
1490 if( err ){
1491 fprintf(err,"%sundefined option.\n",emsg);
1492 errline(i,1,err);
1493 }
1494 errcnt++;
1495 }else if( op[j].type==OPT_FLAG ){
1496 *((int*)op[j].arg) = v;
1497 }else if( op[j].type==OPT_FFLAG ){
1498 (*(void(*)())(op[j].arg))(v);
1499 }else{
1500 if( err ){
1501 fprintf(err,"%smissing argument on switch.\n",emsg);
1502 errline(i,1,err);
1503 }
1504 errcnt++;
1505 }
1506 return errcnt;
1507}
1508
1509/*
1510** Process a command line switch which has an argument.
1511*/
1512static int handleswitch(i,err)
1513int i;
1514FILE *err;
1515{
1516 int lv = 0;
1517 double dv = 0.0;
1518 char *sv = 0, *end;
1519 char *cp;
1520 int j;
1521 int errcnt = 0;
1522 cp = strchr(argv[i],'=');
1523 *cp = 0;
1524 for(j=0; op[j].label; j++){
1525 if( strcmp(argv[i],op[j].label)==0 ) break;
1526 }
1527 *cp = '=';
1528 if( op[j].label==0 ){
1529 if( err ){
1530 fprintf(err,"%sundefined option.\n",emsg);
1531 errline(i,0,err);
1532 }
1533 errcnt++;
1534 }else{
1535 cp++;
1536 switch( op[j].type ){
1537 case OPT_FLAG:
1538 case OPT_FFLAG:
1539 if( err ){
1540 fprintf(err,"%soption requires an argument.\n",emsg);
1541 errline(i,0,err);
1542 }
1543 errcnt++;
1544 break;
1545 case OPT_DBL:
1546 case OPT_FDBL:
1547 dv = strtod(cp,&end);
1548 if( *end ){
1549 if( err ){
1550 fprintf(err,"%sillegal character in floating-point argument.\n",emsg);
1551 errline(i,((int)end)-(int)argv[i],err);
1552 }
1553 errcnt++;
1554 }
1555 break;
1556 case OPT_INT:
1557 case OPT_FINT:
1558 lv = strtol(cp,&end,0);
1559 if( *end ){
1560 if( err ){
1561 fprintf(err,"%sillegal character in integer argument.\n",emsg);
1562 errline(i,((int)end)-(int)argv[i],err);
1563 }
1564 errcnt++;
1565 }
1566 break;
1567 case OPT_STR:
1568 case OPT_FSTR:
1569 sv = cp;
1570 break;
1571 }
1572 switch( op[j].type ){
1573 case OPT_FLAG:
1574 case OPT_FFLAG:
1575 break;
1576 case OPT_DBL:
1577 *(double*)(op[j].arg) = dv;
1578 break;
1579 case OPT_FDBL:
1580 (*(void(*)())(op[j].arg))(dv);
1581 break;
1582 case OPT_INT:
1583 *(int*)(op[j].arg) = lv;
1584 break;
1585 case OPT_FINT:
1586 (*(void(*)())(op[j].arg))((int)lv);
1587 break;
1588 case OPT_STR:
1589 *(char**)(op[j].arg) = sv;
1590 break;
1591 case OPT_FSTR:
1592 (*(void(*)())(op[j].arg))(sv);
1593 break;
1594 }
1595 }
1596 return errcnt;
1597}
1598
drhb0c86772000-06-02 23:21:26 +00001599int OptInit(a,o,err)
drh75897232000-05-29 14:26:00 +00001600char **a;
1601struct s_options *o;
1602FILE *err;
1603{
1604 int errcnt = 0;
1605 argv = a;
1606 op = o;
1607 errstream = err;
1608 if( argv && *argv && op ){
1609 int i;
1610 for(i=1; argv[i]; i++){
1611 if( argv[i][0]=='+' || argv[i][0]=='-' ){
1612 errcnt += handleflags(i,err);
1613 }else if( strchr(argv[i],'=') ){
1614 errcnt += handleswitch(i,err);
1615 }
1616 }
1617 }
1618 if( errcnt>0 ){
1619 fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
drhb0c86772000-06-02 23:21:26 +00001620 OptPrint();
drh75897232000-05-29 14:26:00 +00001621 exit(1);
1622 }
1623 return 0;
1624}
1625
drhb0c86772000-06-02 23:21:26 +00001626int OptNArgs(){
drh75897232000-05-29 14:26:00 +00001627 int cnt = 0;
1628 int dashdash = 0;
1629 int i;
1630 if( argv!=0 && argv[0]!=0 ){
1631 for(i=1; argv[i]; i++){
1632 if( dashdash || !ISOPT(argv[i]) ) cnt++;
1633 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1634 }
1635 }
1636 return cnt;
1637}
1638
drhb0c86772000-06-02 23:21:26 +00001639char *OptArg(n)
drh75897232000-05-29 14:26:00 +00001640int n;
1641{
1642 int i;
1643 i = argindex(n);
1644 return i>=0 ? argv[i] : 0;
1645}
1646
drhb0c86772000-06-02 23:21:26 +00001647void OptErr(n)
drh75897232000-05-29 14:26:00 +00001648int n;
1649{
1650 int i;
1651 i = argindex(n);
1652 if( i>=0 ) errline(i,0,errstream);
1653}
1654
drhb0c86772000-06-02 23:21:26 +00001655void OptPrint(){
drh75897232000-05-29 14:26:00 +00001656 int i;
1657 int max, len;
1658 max = 0;
1659 for(i=0; op[i].label; i++){
1660 len = strlen(op[i].label) + 1;
1661 switch( op[i].type ){
1662 case OPT_FLAG:
1663 case OPT_FFLAG:
1664 break;
1665 case OPT_INT:
1666 case OPT_FINT:
1667 len += 9; /* length of "<integer>" */
1668 break;
1669 case OPT_DBL:
1670 case OPT_FDBL:
1671 len += 6; /* length of "<real>" */
1672 break;
1673 case OPT_STR:
1674 case OPT_FSTR:
1675 len += 8; /* length of "<string>" */
1676 break;
1677 }
1678 if( len>max ) max = len;
1679 }
1680 for(i=0; op[i].label; i++){
1681 switch( op[i].type ){
1682 case OPT_FLAG:
1683 case OPT_FFLAG:
1684 fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
1685 break;
1686 case OPT_INT:
1687 case OPT_FINT:
1688 fprintf(errstream," %s=<integer>%*s %s\n",op[i].label,
1689 max-strlen(op[i].label)-9,"",op[i].message);
1690 break;
1691 case OPT_DBL:
1692 case OPT_FDBL:
1693 fprintf(errstream," %s=<real>%*s %s\n",op[i].label,
1694 max-strlen(op[i].label)-6,"",op[i].message);
1695 break;
1696 case OPT_STR:
1697 case OPT_FSTR:
1698 fprintf(errstream," %s=<string>%*s %s\n",op[i].label,
1699 max-strlen(op[i].label)-8,"",op[i].message);
1700 break;
1701 }
1702 }
1703}
1704/*********************** From the file "parse.c" ****************************/
1705/*
1706** Input file parser for the LEMON parser generator.
1707*/
1708
1709/* The state of the parser */
1710struct pstate {
1711 char *filename; /* Name of the input file */
1712 int tokenlineno; /* Linenumber at which current token starts */
1713 int errorcnt; /* Number of errors so far */
1714 char *tokenstart; /* Text of current token */
1715 struct lemon *gp; /* Global state vector */
1716 enum e_state {
1717 INITIALIZE,
1718 WAITING_FOR_DECL_OR_RULE,
1719 WAITING_FOR_DECL_KEYWORD,
1720 WAITING_FOR_DECL_ARG,
1721 WAITING_FOR_PRECEDENCE_SYMBOL,
1722 WAITING_FOR_ARROW,
1723 IN_RHS,
1724 LHS_ALIAS_1,
1725 LHS_ALIAS_2,
1726 LHS_ALIAS_3,
1727 RHS_ALIAS_1,
1728 RHS_ALIAS_2,
1729 PRECEDENCE_MARK_1,
1730 PRECEDENCE_MARK_2,
1731 RESYNC_AFTER_RULE_ERROR,
1732 RESYNC_AFTER_DECL_ERROR,
1733 WAITING_FOR_DESTRUCTOR_SYMBOL,
1734 WAITING_FOR_DATATYPE_SYMBOL
1735 } state; /* The state of the parser */
1736 struct symbol *lhs; /* Left-hand side of current rule */
1737 char *lhsalias; /* Alias for the LHS */
1738 int nrhs; /* Number of right-hand side symbols seen */
1739 struct symbol *rhs[MAXRHS]; /* RHS symbols */
1740 char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
1741 struct rule *prevrule; /* Previous rule parsed */
1742 char *declkeyword; /* Keyword of a declaration */
1743 char **declargslot; /* Where the declaration argument should be put */
1744 int *decllnslot; /* Where the declaration linenumber is put */
1745 enum e_assoc declassoc; /* Assign this association to decl arguments */
1746 int preccounter; /* Assign this precedence to decl arguments */
1747 struct rule *firstrule; /* Pointer to first rule in the grammar */
1748 struct rule *lastrule; /* Pointer to the most recently parsed rule */
1749};
1750
1751/* Parse a single token */
1752static void parseonetoken(psp)
1753struct pstate *psp;
1754{
1755 char *x;
1756 x = Strsafe(psp->tokenstart); /* Save the token permanently */
1757#if 0
1758 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
1759 x,psp->state);
1760#endif
1761 switch( psp->state ){
1762 case INITIALIZE:
1763 psp->prevrule = 0;
1764 psp->preccounter = 0;
1765 psp->firstrule = psp->lastrule = 0;
1766 psp->gp->nrule = 0;
1767 /* Fall thru to next case */
1768 case WAITING_FOR_DECL_OR_RULE:
1769 if( x[0]=='%' ){
1770 psp->state = WAITING_FOR_DECL_KEYWORD;
1771 }else if( islower(x[0]) ){
1772 psp->lhs = Symbol_new(x);
1773 psp->nrhs = 0;
1774 psp->lhsalias = 0;
1775 psp->state = WAITING_FOR_ARROW;
1776 }else if( x[0]=='{' ){
1777 if( psp->prevrule==0 ){
1778 ErrorMsg(psp->filename,psp->tokenlineno,
1779"There is not prior rule opon which to attach the code \
1780fragment which begins on this line.");
1781 psp->errorcnt++;
1782 }else if( psp->prevrule->code!=0 ){
1783 ErrorMsg(psp->filename,psp->tokenlineno,
1784"Code fragment beginning on this line is not the first \
1785to follow the previous rule.");
1786 psp->errorcnt++;
1787 }else{
1788 psp->prevrule->line = psp->tokenlineno;
1789 psp->prevrule->code = &x[1];
1790 }
1791 }else if( x[0]=='[' ){
1792 psp->state = PRECEDENCE_MARK_1;
1793 }else{
1794 ErrorMsg(psp->filename,psp->tokenlineno,
1795 "Token \"%s\" should be either \"%%\" or a nonterminal name.",
1796 x);
1797 psp->errorcnt++;
1798 }
1799 break;
1800 case PRECEDENCE_MARK_1:
1801 if( !isupper(x[0]) ){
1802 ErrorMsg(psp->filename,psp->tokenlineno,
1803 "The precedence symbol must be a terminal.");
1804 psp->errorcnt++;
1805 }else if( psp->prevrule==0 ){
1806 ErrorMsg(psp->filename,psp->tokenlineno,
1807 "There is no prior rule to assign precedence \"[%s]\".",x);
1808 psp->errorcnt++;
1809 }else if( psp->prevrule->precsym!=0 ){
1810 ErrorMsg(psp->filename,psp->tokenlineno,
1811"Precedence mark on this line is not the first \
1812to follow the previous rule.");
1813 psp->errorcnt++;
1814 }else{
1815 psp->prevrule->precsym = Symbol_new(x);
1816 }
1817 psp->state = PRECEDENCE_MARK_2;
1818 break;
1819 case PRECEDENCE_MARK_2:
1820 if( x[0]!=']' ){
1821 ErrorMsg(psp->filename,psp->tokenlineno,
1822 "Missing \"]\" on precedence mark.");
1823 psp->errorcnt++;
1824 }
1825 psp->state = WAITING_FOR_DECL_OR_RULE;
1826 break;
1827 case WAITING_FOR_ARROW:
1828 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
1829 psp->state = IN_RHS;
1830 }else if( x[0]=='(' ){
1831 psp->state = LHS_ALIAS_1;
1832 }else{
1833 ErrorMsg(psp->filename,psp->tokenlineno,
1834 "Expected to see a \":\" following the LHS symbol \"%s\".",
1835 psp->lhs->name);
1836 psp->errorcnt++;
1837 psp->state = RESYNC_AFTER_RULE_ERROR;
1838 }
1839 break;
1840 case LHS_ALIAS_1:
1841 if( isalpha(x[0]) ){
1842 psp->lhsalias = x;
1843 psp->state = LHS_ALIAS_2;
1844 }else{
1845 ErrorMsg(psp->filename,psp->tokenlineno,
1846 "\"%s\" is not a valid alias for the LHS \"%s\"\n",
1847 x,psp->lhs->name);
1848 psp->errorcnt++;
1849 psp->state = RESYNC_AFTER_RULE_ERROR;
1850 }
1851 break;
1852 case LHS_ALIAS_2:
1853 if( x[0]==')' ){
1854 psp->state = LHS_ALIAS_3;
1855 }else{
1856 ErrorMsg(psp->filename,psp->tokenlineno,
1857 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
1858 psp->errorcnt++;
1859 psp->state = RESYNC_AFTER_RULE_ERROR;
1860 }
1861 break;
1862 case LHS_ALIAS_3:
1863 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
1864 psp->state = IN_RHS;
1865 }else{
1866 ErrorMsg(psp->filename,psp->tokenlineno,
1867 "Missing \"->\" following: \"%s(%s)\".",
1868 psp->lhs->name,psp->lhsalias);
1869 psp->errorcnt++;
1870 psp->state = RESYNC_AFTER_RULE_ERROR;
1871 }
1872 break;
1873 case IN_RHS:
1874 if( x[0]=='.' ){
1875 struct rule *rp;
1876 rp = (struct rule *)malloc( sizeof(struct rule) +
1877 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs );
1878 if( rp==0 ){
1879 ErrorMsg(psp->filename,psp->tokenlineno,
1880 "Can't allocate enough memory for this rule.");
1881 psp->errorcnt++;
1882 psp->prevrule = 0;
1883 }else{
1884 int i;
1885 rp->ruleline = psp->tokenlineno;
1886 rp->rhs = (struct symbol**)&rp[1];
1887 rp->rhsalias = (char**)&(rp->rhs[psp->nrhs]);
1888 for(i=0; i<psp->nrhs; i++){
1889 rp->rhs[i] = psp->rhs[i];
1890 rp->rhsalias[i] = psp->alias[i];
1891 }
1892 rp->lhs = psp->lhs;
1893 rp->lhsalias = psp->lhsalias;
1894 rp->nrhs = psp->nrhs;
1895 rp->code = 0;
1896 rp->precsym = 0;
1897 rp->index = psp->gp->nrule++;
1898 rp->nextlhs = rp->lhs->rule;
1899 rp->lhs->rule = rp;
1900 rp->next = 0;
1901 if( psp->firstrule==0 ){
1902 psp->firstrule = psp->lastrule = rp;
1903 }else{
1904 psp->lastrule->next = rp;
1905 psp->lastrule = rp;
1906 }
1907 psp->prevrule = rp;
1908 }
1909 psp->state = WAITING_FOR_DECL_OR_RULE;
1910 }else if( isalpha(x[0]) ){
1911 if( psp->nrhs>=MAXRHS ){
1912 ErrorMsg(psp->filename,psp->tokenlineno,
1913 "Too many symbol on RHS or rule beginning at \"%s\".",
1914 x);
1915 psp->errorcnt++;
1916 psp->state = RESYNC_AFTER_RULE_ERROR;
1917 }else{
1918 psp->rhs[psp->nrhs] = Symbol_new(x);
1919 psp->alias[psp->nrhs] = 0;
1920 psp->nrhs++;
1921 }
1922 }else if( x[0]=='(' && psp->nrhs>0 ){
1923 psp->state = RHS_ALIAS_1;
1924 }else{
1925 ErrorMsg(psp->filename,psp->tokenlineno,
1926 "Illegal character on RHS of rule: \"%s\".",x);
1927 psp->errorcnt++;
1928 psp->state = RESYNC_AFTER_RULE_ERROR;
1929 }
1930 break;
1931 case RHS_ALIAS_1:
1932 if( isalpha(x[0]) ){
1933 psp->alias[psp->nrhs-1] = x;
1934 psp->state = RHS_ALIAS_2;
1935 }else{
1936 ErrorMsg(psp->filename,psp->tokenlineno,
1937 "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
1938 x,psp->rhs[psp->nrhs-1]->name);
1939 psp->errorcnt++;
1940 psp->state = RESYNC_AFTER_RULE_ERROR;
1941 }
1942 break;
1943 case RHS_ALIAS_2:
1944 if( x[0]==')' ){
1945 psp->state = IN_RHS;
1946 }else{
1947 ErrorMsg(psp->filename,psp->tokenlineno,
1948 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
1949 psp->errorcnt++;
1950 psp->state = RESYNC_AFTER_RULE_ERROR;
1951 }
1952 break;
1953 case WAITING_FOR_DECL_KEYWORD:
1954 if( isalpha(x[0]) ){
1955 psp->declkeyword = x;
1956 psp->declargslot = 0;
1957 psp->decllnslot = 0;
1958 psp->state = WAITING_FOR_DECL_ARG;
1959 if( strcmp(x,"name")==0 ){
1960 psp->declargslot = &(psp->gp->name);
1961 }else if( strcmp(x,"include")==0 ){
1962 psp->declargslot = &(psp->gp->include);
1963 psp->decllnslot = &psp->gp->includeln;
1964 }else if( strcmp(x,"code")==0 ){
1965 psp->declargslot = &(psp->gp->extracode);
1966 psp->decllnslot = &psp->gp->extracodeln;
1967 }else if( strcmp(x,"token_destructor")==0 ){
1968 psp->declargslot = &psp->gp->tokendest;
1969 psp->decllnslot = &psp->gp->tokendestln;
1970 }else if( strcmp(x,"token_prefix")==0 ){
1971 psp->declargslot = &psp->gp->tokenprefix;
1972 }else if( strcmp(x,"syntax_error")==0 ){
1973 psp->declargslot = &(psp->gp->error);
1974 psp->decllnslot = &psp->gp->errorln;
1975 }else if( strcmp(x,"parse_accept")==0 ){
1976 psp->declargslot = &(psp->gp->accept);
1977 psp->decllnslot = &psp->gp->acceptln;
1978 }else if( strcmp(x,"parse_failure")==0 ){
1979 psp->declargslot = &(psp->gp->failure);
1980 psp->decllnslot = &psp->gp->failureln;
1981 }else if( strcmp(x,"stack_overflow")==0 ){
1982 psp->declargslot = &(psp->gp->overflow);
1983 psp->decllnslot = &psp->gp->overflowln;
1984 }else if( strcmp(x,"extra_argument")==0 ){
1985 psp->declargslot = &(psp->gp->arg);
1986 }else if( strcmp(x,"token_type")==0 ){
1987 psp->declargslot = &(psp->gp->tokentype);
1988 }else if( strcmp(x,"stack_size")==0 ){
1989 psp->declargslot = &(psp->gp->stacksize);
1990 }else if( strcmp(x,"start_symbol")==0 ){
1991 psp->declargslot = &(psp->gp->start);
1992 }else if( strcmp(x,"left")==0 ){
1993 psp->preccounter++;
1994 psp->declassoc = LEFT;
1995 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
1996 }else if( strcmp(x,"right")==0 ){
1997 psp->preccounter++;
1998 psp->declassoc = RIGHT;
1999 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2000 }else if( strcmp(x,"nonassoc")==0 ){
2001 psp->preccounter++;
2002 psp->declassoc = NONE;
2003 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2004 }else if( strcmp(x,"destructor")==0 ){
2005 psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
2006 }else if( strcmp(x,"type")==0 ){
2007 psp->state = WAITING_FOR_DATATYPE_SYMBOL;
2008 }else{
2009 ErrorMsg(psp->filename,psp->tokenlineno,
2010 "Unknown declaration keyword: \"%%%s\".",x);
2011 psp->errorcnt++;
2012 psp->state = RESYNC_AFTER_DECL_ERROR;
2013 }
2014 }else{
2015 ErrorMsg(psp->filename,psp->tokenlineno,
2016 "Illegal declaration keyword: \"%s\".",x);
2017 psp->errorcnt++;
2018 psp->state = RESYNC_AFTER_DECL_ERROR;
2019 }
2020 break;
2021 case WAITING_FOR_DESTRUCTOR_SYMBOL:
2022 if( !isalpha(x[0]) ){
2023 ErrorMsg(psp->filename,psp->tokenlineno,
2024 "Symbol name missing after %destructor keyword");
2025 psp->errorcnt++;
2026 psp->state = RESYNC_AFTER_DECL_ERROR;
2027 }else{
2028 struct symbol *sp = Symbol_new(x);
2029 psp->declargslot = &sp->destructor;
2030 psp->decllnslot = &sp->destructorln;
2031 psp->state = WAITING_FOR_DECL_ARG;
2032 }
2033 break;
2034 case WAITING_FOR_DATATYPE_SYMBOL:
2035 if( !isalpha(x[0]) ){
2036 ErrorMsg(psp->filename,psp->tokenlineno,
2037 "Symbol name missing after %destructor keyword");
2038 psp->errorcnt++;
2039 psp->state = RESYNC_AFTER_DECL_ERROR;
2040 }else{
2041 struct symbol *sp = Symbol_new(x);
2042 psp->declargslot = &sp->datatype;
2043 psp->decllnslot = 0;
2044 psp->state = WAITING_FOR_DECL_ARG;
2045 }
2046 break;
2047 case WAITING_FOR_PRECEDENCE_SYMBOL:
2048 if( x[0]=='.' ){
2049 psp->state = WAITING_FOR_DECL_OR_RULE;
2050 }else if( isupper(x[0]) ){
2051 struct symbol *sp;
2052 sp = Symbol_new(x);
2053 if( sp->prec>=0 ){
2054 ErrorMsg(psp->filename,psp->tokenlineno,
2055 "Symbol \"%s\" has already be given a precedence.",x);
2056 psp->errorcnt++;
2057 }else{
2058 sp->prec = psp->preccounter;
2059 sp->assoc = psp->declassoc;
2060 }
2061 }else{
2062 ErrorMsg(psp->filename,psp->tokenlineno,
2063 "Can't assign a precedence to \"%s\".",x);
2064 psp->errorcnt++;
2065 }
2066 break;
2067 case WAITING_FOR_DECL_ARG:
2068 if( (x[0]=='{' || x[0]=='\"' || isalnum(x[0])) ){
2069 if( *(psp->declargslot)!=0 ){
2070 ErrorMsg(psp->filename,psp->tokenlineno,
2071 "The argument \"%s\" to declaration \"%%%s\" is not the first.",
2072 x[0]=='\"' ? &x[1] : x,psp->declkeyword);
2073 psp->errorcnt++;
2074 psp->state = RESYNC_AFTER_DECL_ERROR;
2075 }else{
2076 *(psp->declargslot) = (x[0]=='\"' || x[0]=='{') ? &x[1] : x;
2077 if( psp->decllnslot ) *psp->decllnslot = psp->tokenlineno;
2078 psp->state = WAITING_FOR_DECL_OR_RULE;
2079 }
2080 }else{
2081 ErrorMsg(psp->filename,psp->tokenlineno,
2082 "Illegal argument to %%%s: %s",psp->declkeyword,x);
2083 psp->errorcnt++;
2084 psp->state = RESYNC_AFTER_DECL_ERROR;
2085 }
2086 break;
2087 case RESYNC_AFTER_RULE_ERROR:
2088/* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2089** break; */
2090 case RESYNC_AFTER_DECL_ERROR:
2091 if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2092 if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
2093 break;
2094 }
2095}
2096
2097/* In spite of its name, this function is really a scanner. It read
2098** in the entire input file (all at once) then tokenizes it. Each
2099** token is passed to the function "parseonetoken" which builds all
2100** the appropriate data structures in the global state vector "gp".
2101*/
2102void Parse(gp)
2103struct lemon *gp;
2104{
2105 struct pstate ps;
2106 FILE *fp;
2107 char *filebuf;
2108 int filesize;
2109 int lineno;
2110 int c;
2111 char *cp, *nextcp;
2112 int startline = 0;
2113
2114 ps.gp = gp;
2115 ps.filename = gp->filename;
2116 ps.errorcnt = 0;
2117 ps.state = INITIALIZE;
2118
2119 /* Begin by reading the input file */
2120 fp = fopen(ps.filename,"rb");
2121 if( fp==0 ){
2122 ErrorMsg(ps.filename,0,"Can't open this file for reading.");
2123 gp->errorcnt++;
2124 return;
2125 }
2126 fseek(fp,0,2);
2127 filesize = ftell(fp);
2128 rewind(fp);
2129 filebuf = (char *)malloc( filesize+1 );
2130 if( filebuf==0 ){
2131 ErrorMsg(ps.filename,0,"Can't allocate %d of memory to hold this file.",
2132 filesize+1);
2133 gp->errorcnt++;
2134 return;
2135 }
2136 if( fread(filebuf,1,filesize,fp)!=filesize ){
2137 ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
2138 filesize);
2139 free(filebuf);
2140 gp->errorcnt++;
2141 return;
2142 }
2143 fclose(fp);
2144 filebuf[filesize] = 0;
2145
2146 /* Now scan the text of the input file */
2147 lineno = 1;
2148 for(cp=filebuf; (c= *cp)!=0; ){
2149 if( c=='\n' ) lineno++; /* Keep track of the line number */
2150 if( isspace(c) ){ cp++; continue; } /* Skip all white space */
2151 if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
2152 cp+=2;
2153 while( (c= *cp)!=0 && c!='\n' ) cp++;
2154 continue;
2155 }
2156 if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
2157 cp+=2;
2158 while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
2159 if( c=='\n' ) lineno++;
2160 cp++;
2161 }
2162 if( c ) cp++;
2163 continue;
2164 }
2165 ps.tokenstart = cp; /* Mark the beginning of the token */
2166 ps.tokenlineno = lineno; /* Linenumber on which token begins */
2167 if( c=='\"' ){ /* String literals */
2168 cp++;
2169 while( (c= *cp)!=0 && c!='\"' ){
2170 if( c=='\n' ) lineno++;
2171 cp++;
2172 }
2173 if( c==0 ){
2174 ErrorMsg(ps.filename,startline,
2175"String starting on this line is not terminated before the end of the file.");
2176 ps.errorcnt++;
2177 nextcp = cp;
2178 }else{
2179 nextcp = cp+1;
2180 }
2181 }else if( c=='{' ){ /* A block of C code */
2182 int level;
2183 cp++;
2184 for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
2185 if( c=='\n' ) lineno++;
2186 else if( c=='{' ) level++;
2187 else if( c=='}' ) level--;
2188 else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
2189 int prevc;
2190 cp = &cp[2];
2191 prevc = 0;
2192 while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
2193 if( c=='\n' ) lineno++;
2194 prevc = c;
2195 cp++;
2196 }
2197 }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
2198 cp = &cp[2];
2199 while( (c= *cp)!=0 && c!='\n' ) cp++;
2200 if( c ) lineno++;
2201 }else if( c=='\'' || c=='\"' ){ /* String a character literals */
2202 int startchar, prevc;
2203 startchar = c;
2204 prevc = 0;
2205 for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
2206 if( c=='\n' ) lineno++;
2207 if( prevc=='\\' ) prevc = 0;
2208 else prevc = c;
2209 }
2210 }
2211 }
2212 if( c==0 ){
2213 ErrorMsg(ps.filename,startline,
2214"C code starting on this line is not terminated before the end of the file.");
2215 ps.errorcnt++;
2216 nextcp = cp;
2217 }else{
2218 nextcp = cp+1;
2219 }
2220 }else if( isalnum(c) ){ /* Identifiers */
2221 while( (c= *cp)!=0 && (isalnum(c) || c=='_') ) cp++;
2222 nextcp = cp;
2223 }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
2224 cp += 3;
2225 nextcp = cp;
2226 }else{ /* All other (one character) operators */
2227 cp++;
2228 nextcp = cp;
2229 }
2230 c = *cp;
2231 *cp = 0; /* Null terminate the token */
2232 parseonetoken(&ps); /* Parse the token */
2233 *cp = c; /* Restore the buffer */
2234 cp = nextcp;
2235 }
2236 free(filebuf); /* Release the buffer after parsing */
2237 gp->rule = ps.firstrule;
2238 gp->errorcnt = ps.errorcnt;
2239}
2240/*************************** From the file "plink.c" *********************/
2241/*
2242** Routines processing configuration follow-set propagation links
2243** in the LEMON parser generator.
2244*/
2245static struct plink *plink_freelist = 0;
2246
2247/* Allocate a new plink */
2248struct plink *Plink_new(){
2249 struct plink *new;
2250
2251 if( plink_freelist==0 ){
2252 int i;
2253 int amt = 100;
2254 plink_freelist = (struct plink *)malloc( sizeof(struct plink)*amt );
2255 if( plink_freelist==0 ){
2256 fprintf(stderr,
2257 "Unable to allocate memory for a new follow-set propagation link.\n");
2258 exit(1);
2259 }
2260 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2261 plink_freelist[amt-1].next = 0;
2262 }
2263 new = plink_freelist;
2264 plink_freelist = plink_freelist->next;
2265 return new;
2266}
2267
2268/* Add a plink to a plink list */
2269void Plink_add(plpp,cfp)
2270struct plink **plpp;
2271struct config *cfp;
2272{
2273 struct plink *new;
2274 new = Plink_new();
2275 new->next = *plpp;
2276 *plpp = new;
2277 new->cfp = cfp;
2278}
2279
2280/* Transfer every plink on the list "from" to the list "to" */
2281void Plink_copy(to,from)
2282struct plink **to;
2283struct plink *from;
2284{
2285 struct plink *nextpl;
2286 while( from ){
2287 nextpl = from->next;
2288 from->next = *to;
2289 *to = from;
2290 from = nextpl;
2291 }
2292}
2293
2294/* Delete every plink on the list */
2295void Plink_delete(plp)
2296struct plink *plp;
2297{
2298 struct plink *nextpl;
2299
2300 while( plp ){
2301 nextpl = plp->next;
2302 plp->next = plink_freelist;
2303 plink_freelist = plp;
2304 plp = nextpl;
2305 }
2306}
2307/*********************** From the file "report.c" **************************/
2308/*
2309** Procedures for generating reports and tables in the LEMON parser generator.
2310*/
2311
2312/* Generate a filename with the given suffix. Space to hold the
2313** name comes from malloc() and must be freed by the calling
2314** function.
2315*/
2316PRIVATE char *file_makename(lemp,suffix)
2317struct lemon *lemp;
2318char *suffix;
2319{
2320 char *name;
2321 char *cp;
2322
2323 name = malloc( strlen(lemp->filename) + strlen(suffix) + 5 );
2324 if( name==0 ){
2325 fprintf(stderr,"Can't allocate space for a filename.\n");
2326 exit(1);
2327 }
2328 strcpy(name,lemp->filename);
2329 cp = strrchr(name,'.');
2330 if( cp ) *cp = 0;
2331 strcat(name,suffix);
2332 return name;
2333}
2334
2335/* Open a file with a name based on the name of the input file,
2336** but with a different (specified) suffix, and return a pointer
2337** to the stream */
2338PRIVATE FILE *file_open(lemp,suffix,mode)
2339struct lemon *lemp;
2340char *suffix;
2341char *mode;
2342{
2343 FILE *fp;
2344
2345 if( lemp->outname ) free(lemp->outname);
2346 lemp->outname = file_makename(lemp, suffix);
2347 fp = fopen(lemp->outname,mode);
2348 if( fp==0 && *mode=='w' ){
2349 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
2350 lemp->errorcnt++;
2351 return 0;
2352 }
2353 return fp;
2354}
2355
2356/* Duplicate the input file without comments and without actions
2357** on rules */
2358void Reprint(lemp)
2359struct lemon *lemp;
2360{
2361 struct rule *rp;
2362 struct symbol *sp;
2363 int i, j, maxlen, len, ncolumns, skip;
2364 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
2365 maxlen = 10;
2366 for(i=0; i<lemp->nsymbol; i++){
2367 sp = lemp->symbols[i];
2368 len = strlen(sp->name);
2369 if( len>maxlen ) maxlen = len;
2370 }
2371 ncolumns = 76/(maxlen+5);
2372 if( ncolumns<1 ) ncolumns = 1;
2373 skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
2374 for(i=0; i<skip; i++){
2375 printf("//");
2376 for(j=i; j<lemp->nsymbol; j+=skip){
2377 sp = lemp->symbols[j];
2378 assert( sp->index==j );
2379 printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
2380 }
2381 printf("\n");
2382 }
2383 for(rp=lemp->rule; rp; rp=rp->next){
2384 printf("%s",rp->lhs->name);
2385/* if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
2386 printf(" ::=");
2387 for(i=0; i<rp->nrhs; i++){
2388 printf(" %s",rp->rhs[i]->name);
2389/* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
2390 }
2391 printf(".");
2392 if( rp->precsym ) printf(" [%s]",rp->precsym->name);
2393/* if( rp->code ) printf("\n %s",rp->code); */
2394 printf("\n");
2395 }
2396}
2397
2398void ConfigPrint(fp,cfp)
2399FILE *fp;
2400struct config *cfp;
2401{
2402 struct rule *rp;
2403 int i;
2404 rp = cfp->rp;
2405 fprintf(fp,"%s ::=",rp->lhs->name);
2406 for(i=0; i<=rp->nrhs; i++){
2407 if( i==cfp->dot ) fprintf(fp," *");
2408 if( i==rp->nrhs ) break;
2409 fprintf(fp," %s",rp->rhs[i]->name);
2410 }
2411}
2412
2413/* #define TEST */
2414#ifdef TEST
2415/* Print a set */
2416PRIVATE void SetPrint(out,set,lemp)
2417FILE *out;
2418char *set;
2419struct lemon *lemp;
2420{
2421 int i;
2422 char *spacer;
2423 spacer = "";
2424 fprintf(out,"%12s[","");
2425 for(i=0; i<lemp->nterminal; i++){
2426 if( SetFind(set,i) ){
2427 fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
2428 spacer = " ";
2429 }
2430 }
2431 fprintf(out,"]\n");
2432}
2433
2434/* Print a plink chain */
2435PRIVATE void PlinkPrint(out,plp,tag)
2436FILE *out;
2437struct plink *plp;
2438char *tag;
2439{
2440 while( plp ){
2441 fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->index);
2442 ConfigPrint(out,plp->cfp);
2443 fprintf(out,"\n");
2444 plp = plp->next;
2445 }
2446}
2447#endif
2448
2449/* Print an action to the given file descriptor. Return FALSE if
2450** nothing was actually printed.
2451*/
2452int PrintAction(struct action *ap, FILE *fp, int indent){
2453 int result = 1;
2454 switch( ap->type ){
2455 case SHIFT:
2456 fprintf(fp,"%*s shift %d",indent,ap->sp->name,ap->x.stp->index);
2457 break;
2458 case REDUCE:
2459 fprintf(fp,"%*s reduce %d",indent,ap->sp->name,ap->x.rp->index);
2460 break;
2461 case ACCEPT:
2462 fprintf(fp,"%*s accept",indent,ap->sp->name);
2463 break;
2464 case ERROR:
2465 fprintf(fp,"%*s error",indent,ap->sp->name);
2466 break;
2467 case CONFLICT:
2468 fprintf(fp,"%*s reduce %-3d ** Parsing conflict **",
2469 indent,ap->sp->name,ap->x.rp->index);
2470 break;
2471 case SH_RESOLVED:
2472 case RD_RESOLVED:
2473 case NOT_USED:
2474 result = 0;
2475 break;
2476 }
2477 return result;
2478}
2479
2480/* Generate the "y.output" log file */
2481void ReportOutput(lemp)
2482struct lemon *lemp;
2483{
2484 int i;
2485 struct state *stp;
2486 struct config *cfp;
2487 struct action *ap;
2488 FILE *fp;
2489
2490 fp = file_open(lemp,".out","w");
2491 if( fp==0 ) return;
2492 fprintf(fp," \b");
2493 for(i=0; i<lemp->nstate; i++){
2494 stp = lemp->sorted[i];
2495 fprintf(fp,"State %d:\n",stp->index);
2496 if( lemp->basisflag ) cfp=stp->bp;
2497 else cfp=stp->cfp;
2498 while( cfp ){
2499 char buf[20];
2500 if( cfp->dot==cfp->rp->nrhs ){
2501 sprintf(buf,"(%d)",cfp->rp->index);
2502 fprintf(fp," %5s ",buf);
2503 }else{
2504 fprintf(fp," ");
2505 }
2506 ConfigPrint(fp,cfp);
2507 fprintf(fp,"\n");
2508#ifdef TEST
2509 SetPrint(fp,cfp->fws,lemp);
2510 PlinkPrint(fp,cfp->fplp,"To ");
2511 PlinkPrint(fp,cfp->bplp,"From");
2512#endif
2513 if( lemp->basisflag ) cfp=cfp->bp;
2514 else cfp=cfp->next;
2515 }
2516 fprintf(fp,"\n");
2517 for(ap=stp->ap; ap; ap=ap->next){
2518 if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
2519 }
2520 fprintf(fp,"\n");
2521 }
2522 fclose(fp);
2523 return;
2524}
2525
2526/* Search for the file "name" which is in the same directory as
2527** the exacutable */
2528PRIVATE char *pathsearch(argv0,name,modemask)
2529char *argv0;
2530char *name;
2531int modemask;
2532{
2533 char *pathlist;
2534 char *path,*cp;
2535 char c;
2536 extern int access();
2537
2538#ifdef __WIN32__
2539 cp = strrchr(argv0,'\\');
2540#else
2541 cp = strrchr(argv0,'/');
2542#endif
2543 if( cp ){
2544 c = *cp;
2545 *cp = 0;
2546 path = (char *)malloc( strlen(argv0) + strlen(name) + 2 );
2547 if( path ) sprintf(path,"%s/%s",argv0,name);
2548 *cp = c;
2549 }else{
2550 extern char *getenv();
2551 pathlist = getenv("PATH");
2552 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
2553 path = (char *)malloc( strlen(pathlist)+strlen(name)+2 );
2554 if( path!=0 ){
2555 while( *pathlist ){
2556 cp = strchr(pathlist,':');
2557 if( cp==0 ) cp = &pathlist[strlen(pathlist)];
2558 c = *cp;
2559 *cp = 0;
2560 sprintf(path,"%s/%s",pathlist,name);
2561 *cp = c;
2562 if( c==0 ) pathlist = "";
2563 else pathlist = &cp[1];
2564 if( access(path,modemask)==0 ) break;
2565 }
2566 }
2567 }
2568 return path;
2569}
2570
2571/* Given an action, compute the integer value for that action
2572** which is to be put in the action table of the generated machine.
2573** Return negative if no action should be generated.
2574*/
2575PRIVATE int compute_action(lemp,ap)
2576struct lemon *lemp;
2577struct action *ap;
2578{
2579 int act;
2580 switch( ap->type ){
2581 case SHIFT: act = ap->x.stp->index; break;
2582 case REDUCE: act = ap->x.rp->index + lemp->nstate; break;
2583 case ERROR: act = lemp->nstate + lemp->nrule; break;
2584 case ACCEPT: act = lemp->nstate + lemp->nrule + 1; break;
2585 default: act = -1; break;
2586 }
2587 return act;
2588}
2589
2590#define LINESIZE 1000
2591/* The next cluster of routines are for reading the template file
2592** and writing the results to the generated parser */
2593/* The first function transfers data from "in" to "out" until
2594** a line is seen which begins with "%%". The line number is
2595** tracked.
2596**
2597** if name!=0, then any word that begin with "Parse" is changed to
2598** begin with *name instead.
2599*/
2600PRIVATE void tplt_xfer(name,in,out,lineno)
2601char *name;
2602FILE *in;
2603FILE *out;
2604int *lineno;
2605{
2606 int i, iStart;
2607 char line[LINESIZE];
2608 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
2609 (*lineno)++;
2610 iStart = 0;
2611 if( name ){
2612 for(i=0; line[i]; i++){
2613 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
2614 && (i==0 || !isalpha(line[i-1]))
2615 ){
2616 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
2617 fprintf(out,"%s",name);
2618 i += 4;
2619 iStart = i+1;
2620 }
2621 }
2622 }
2623 fprintf(out,"%s",&line[iStart]);
2624 }
2625}
2626
2627/* The next function finds the template file and opens it, returning
2628** a pointer to the opened file. */
2629PRIVATE FILE *tplt_open(lemp)
2630struct lemon *lemp;
2631{
2632 static char templatename[] = "lempar.c";
2633 char buf[1000];
2634 FILE *in;
2635 char *tpltname;
2636 char *cp;
2637
2638 cp = strrchr(lemp->filename,'.');
2639 if( cp ){
2640 sprintf(buf,"%.*s.lt",(int)cp-(int)lemp->filename,lemp->filename);
2641 }else{
2642 sprintf(buf,"%s.lt",lemp->filename);
2643 }
2644 if( access(buf,004)==0 ){
2645 tpltname = buf;
2646 }else{
2647 tpltname = pathsearch(lemp->argv0,templatename,0);
2648 }
2649 if( tpltname==0 ){
2650 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
2651 templatename);
2652 lemp->errorcnt++;
2653 return 0;
2654 }
2655 in = fopen(tpltname,"r");
2656 if( in==0 ){
2657 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
2658 lemp->errorcnt++;
2659 return 0;
2660 }
2661 return in;
2662}
2663
2664/* Print a string to the file and keep the linenumber up to date */
2665PRIVATE void tplt_print(out,lemp,str,strln,lineno)
2666FILE *out;
2667struct lemon *lemp;
2668char *str;
2669int strln;
2670int *lineno;
2671{
2672 if( str==0 ) return;
2673 fprintf(out,"#line %d \"%s\"\n",strln,lemp->filename); (*lineno)++;
2674 while( *str ){
2675 if( *str=='\n' ) (*lineno)++;
2676 putc(*str,out);
2677 str++;
2678 }
2679 fprintf(out,"\n#line %d \"%s\"\n",*lineno+2,lemp->outname); (*lineno)+=2;
2680 return;
2681}
2682
2683/*
2684** The following routine emits code for the destructor for the
2685** symbol sp
2686*/
2687void emit_destructor_code(out,sp,lemp,lineno)
2688FILE *out;
2689struct symbol *sp;
2690struct lemon *lemp;
2691int *lineno;
2692{
2693 char *cp;
2694
2695 int linecnt = 0;
2696 if( sp->type==TERMINAL ){
2697 cp = lemp->tokendest;
2698 if( cp==0 ) return;
2699 fprintf(out,"#line %d \"%s\"\n{",lemp->tokendestln,lemp->filename);
2700 }else{
2701 cp = sp->destructor;
2702 if( cp==0 ) return;
2703 fprintf(out,"#line %d \"%s\"\n{",sp->destructorln,lemp->filename);
2704 }
2705 for(; *cp; cp++){
2706 if( *cp=='$' && cp[1]=='$' ){
2707 fprintf(out,"(yypminor->yy%d)",sp->dtnum);
2708 cp++;
2709 continue;
2710 }
2711 if( *cp=='\n' ) linecnt++;
2712 fputc(*cp,out);
2713 }
2714 (*lineno) += 3 + linecnt;
2715 fprintf(out,"}\n#line %d \"%s\"\n",*lineno,lemp->outname);
2716 return;
2717}
2718
2719/*
2720** Return TRUE (non-zero) if the given symbol has a distructor.
2721*/
2722int has_destructor(sp, lemp)
2723struct symbol *sp;
2724struct lemon *lemp;
2725{
2726 int ret;
2727 if( sp->type==TERMINAL ){
2728 ret = lemp->tokendest!=0;
2729 }else{
2730 ret = sp->destructor!=0;
2731 }
2732 return ret;
2733}
2734
2735/*
2736** Generate code which executes when the rule "rp" is reduced. Write
2737** the code to "out". Make sure lineno stays up-to-date.
2738*/
2739PRIVATE void emit_code(out,rp,lemp,lineno)
2740FILE *out;
2741struct rule *rp;
2742struct lemon *lemp;
2743int *lineno;
2744{
2745 char *cp, *xp;
2746 int linecnt = 0;
2747 int i;
2748 char lhsused = 0; /* True if the LHS element has been used */
2749 char used[MAXRHS]; /* True for each RHS element which is used */
2750
2751 for(i=0; i<rp->nrhs; i++) used[i] = 0;
2752 lhsused = 0;
2753
2754 /* Generate code to do the reduce action */
2755 if( rp->code ){
2756 fprintf(out,"#line %d \"%s\"\n{",rp->line,lemp->filename);
2757 for(cp=rp->code; *cp; cp++){
2758 if( isalpha(*cp) && (cp==rp->code || !isalnum(cp[-1])) ){
2759 char saved;
2760 for(xp= &cp[1]; isalnum(*xp); xp++);
2761 saved = *xp;
2762 *xp = 0;
2763 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
2764 fprintf(out,"yygotominor.yy%d",rp->lhs->dtnum);
2765 cp = xp;
2766 lhsused = 1;
2767 }else{
2768 for(i=0; i<rp->nrhs; i++){
2769 if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
2770 fprintf(out,"yymsp[%d].minor.yy%d",i-rp->nrhs+1,rp->rhs[i]->dtnum);
2771 cp = xp;
2772 used[i] = 1;
2773 break;
2774 }
2775 }
2776 }
2777 *xp = saved;
2778 }
2779 if( *cp=='\n' ) linecnt++;
2780 fputc(*cp,out);
2781 } /* End loop */
2782 (*lineno) += 3 + linecnt;
2783 fprintf(out,"}\n#line %d \"%s\"\n",*lineno,lemp->outname);
2784 } /* End if( rp->code ) */
2785
2786 /* Check to make sure the LHS has been used */
2787 if( rp->lhsalias && !lhsused ){
2788 ErrorMsg(lemp->filename,rp->ruleline,
2789 "Label \"%s\" for \"%s(%s)\" is never used.",
2790 rp->lhsalias,rp->lhs->name,rp->lhsalias);
2791 lemp->errorcnt++;
2792 }
2793
2794 /* Generate destructor code for RHS symbols which are not used in the
2795 ** reduce code */
2796 for(i=0; i<rp->nrhs; i++){
2797 if( rp->rhsalias[i] && !used[i] ){
2798 ErrorMsg(lemp->filename,rp->ruleline,
2799 "Label $%s$ for \"%s(%s)\" is never used.",
2800 rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
2801 lemp->errorcnt++;
2802 }else if( rp->rhsalias[i]==0 ){
2803 if( has_destructor(rp->rhs[i],lemp) ){
2804 fprintf(out," yy_destructor(%d,&yymsp[%d].minor);\n",
2805 rp->rhs[i]->index,i-rp->nrhs+1); (*lineno)++;
2806 }else{
2807 fprintf(out," /* No destructor defined for %s */\n",
2808 rp->rhs[i]->name);
2809 (*lineno)++;
2810 }
2811 }
2812 }
2813 return;
2814}
2815
2816/*
2817** Print the definition of the union used for the parser's data stack.
2818** This union contains fields for every possible data type for tokens
2819** and nonterminals. In the process of computing and printing this
2820** union, also set the ".dtnum" field of every terminal and nonterminal
2821** symbol.
2822*/
2823void print_stack_union(out,lemp,plineno,mhflag)
2824FILE *out; /* The output stream */
2825struct lemon *lemp; /* The main info structure for this parser */
2826int *plineno; /* Pointer to the line number */
2827int mhflag; /* True if generating makeheaders output */
2828{
2829 int lineno = *plineno; /* The line number of the output */
2830 char **types; /* A hash table of datatypes */
2831 int arraysize; /* Size of the "types" array */
2832 int maxdtlength; /* Maximum length of any ".datatype" field. */
2833 char *stddt; /* Standardized name for a datatype */
2834 int i,j; /* Loop counters */
2835 int hash; /* For hashing the name of a type */
2836 char *name; /* Name of the parser */
2837
2838 /* Allocate and initialize types[] and allocate stddt[] */
2839 arraysize = lemp->nsymbol * 2;
2840 types = (char**)malloc( arraysize * sizeof(char*) );
2841 for(i=0; i<arraysize; i++) types[i] = 0;
2842 maxdtlength = 0;
2843 for(i=0; i<lemp->nsymbol; i++){
2844 int len;
2845 struct symbol *sp = lemp->symbols[i];
2846 if( sp->datatype==0 ) continue;
2847 len = strlen(sp->datatype);
2848 if( len>maxdtlength ) maxdtlength = len;
2849 }
2850 stddt = (char*)malloc( maxdtlength*2 + 1 );
2851 if( types==0 || stddt==0 ){
2852 fprintf(stderr,"Out of memory.\n");
2853 exit(1);
2854 }
2855
2856 /* Build a hash table of datatypes. The ".dtnum" field of each symbol
2857 ** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
2858 ** used for terminal symbols and for nonterminals which don't specify
2859 ** a datatype using the %type directive. */
2860 for(i=0; i<lemp->nsymbol; i++){
2861 struct symbol *sp = lemp->symbols[i];
2862 char *cp;
2863 if( sp==lemp->errsym ){
2864 sp->dtnum = arraysize+1;
2865 continue;
2866 }
2867 if( sp->type!=NONTERMINAL || sp->datatype==0 ){
2868 sp->dtnum = 0;
2869 continue;
2870 }
2871 cp = sp->datatype;
2872 j = 0;
2873 while( isspace(*cp) ) cp++;
2874 while( *cp ) stddt[j++] = *cp++;
2875 while( j>0 && isspace(stddt[j-1]) ) j--;
2876 stddt[j] = 0;
2877 hash = 0;
2878 for(j=0; stddt[j]; j++){
2879 hash = hash*53 + stddt[j];
2880 }
2881 if( hash<0 ) hash = -hash;
2882 hash = hash%arraysize;
2883 while( types[hash] ){
2884 if( strcmp(types[hash],stddt)==0 ){
2885 sp->dtnum = hash + 1;
2886 break;
2887 }
2888 hash++;
2889 if( hash>=arraysize ) hash = 0;
2890 }
2891 if( types[hash]==0 ){
2892 sp->dtnum = hash + 1;
2893 types[hash] = (char*)malloc( strlen(stddt)+1 );
2894 if( types[hash]==0 ){
2895 fprintf(stderr,"Out of memory.\n");
2896 exit(1);
2897 }
2898 strcpy(types[hash],stddt);
2899 }
2900 }
2901
2902 /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
2903 name = lemp->name ? lemp->name : "Parse";
2904 lineno = *plineno;
2905 if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
2906 fprintf(out,"#define %sTOKENTYPE %s\n",name,
2907 lemp->tokentype?lemp->tokentype:"void*"); lineno++;
2908 if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
2909 fprintf(out,"typedef union {\n"); lineno++;
2910 fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
2911 for(i=0; i<arraysize; i++){
2912 if( types[i]==0 ) continue;
2913 fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
2914 free(types[i]);
2915 }
2916 fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
2917 free(stddt);
2918 free(types);
2919 fprintf(out,"} YYMINORTYPE;\n"); lineno++;
2920 *plineno = lineno;
2921}
2922
2923/* Generate C source code for the parser */
2924void ReportTable(lemp, mhflag)
2925struct lemon *lemp;
2926int mhflag; /* Output in makeheaders format if true */
2927{
2928 FILE *out, *in;
2929 char line[LINESIZE];
2930 int lineno;
2931 struct state *stp;
2932 struct action *ap;
2933 struct rule *rp;
2934 int i;
2935 int tablecnt;
2936 char *name;
2937
2938 in = tplt_open(lemp);
2939 if( in==0 ) return;
2940 out = file_open(lemp,".c","w");
2941 if( out==0 ){
2942 fclose(in);
2943 return;
2944 }
2945 lineno = 1;
2946 tplt_xfer(lemp->name,in,out,&lineno);
2947
2948 /* Generate the include code, if any */
2949 tplt_print(out,lemp,lemp->include,lemp->includeln,&lineno);
2950 if( mhflag ){
2951 char *name = file_makename(lemp, ".h");
2952 fprintf(out,"#include \"%s\"\n", name); lineno++;
2953 free(name);
2954 }
2955 tplt_xfer(lemp->name,in,out,&lineno);
2956
2957 /* Generate #defines for all tokens */
2958 if( mhflag ){
2959 char *prefix;
2960 fprintf(out,"#if INTERFACE\n"); lineno++;
2961 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
2962 else prefix = "";
2963 for(i=1; i<lemp->nterminal; i++){
2964 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
2965 lineno++;
2966 }
2967 fprintf(out,"#endif\n"); lineno++;
2968 }
2969 tplt_xfer(lemp->name,in,out,&lineno);
2970
2971 /* Generate the defines */
2972 fprintf(out,"/* \001 */\n");
2973 fprintf(out,"#define YYCODETYPE %s\n",
2974 lemp->nsymbol>250?"int":"unsigned char"); lineno++;
2975 fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++;
2976 fprintf(out,"#define YYACTIONTYPE %s\n",
2977 lemp->nstate+lemp->nrule>250?"int":"unsigned char"); lineno++;
2978 print_stack_union(out,lemp,&lineno,mhflag);
2979 if( lemp->stacksize ){
2980 if( atoi(lemp->stacksize)<=0 ){
2981 ErrorMsg(lemp->filename,0,
2982"Illegal stack size: [%s]. The stack size should be an integer constant.",
2983 lemp->stacksize);
2984 lemp->errorcnt++;
2985 lemp->stacksize = "100";
2986 }
2987 fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
2988 }else{
2989 fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
2990 }
2991 if( mhflag ){
2992 fprintf(out,"#if INTERFACE\n"); lineno++;
2993 }
2994 name = lemp->name ? lemp->name : "Parse";
2995 if( lemp->arg && lemp->arg[0] ){
2996 int i;
2997 i = strlen(lemp->arg);
drhb1edd012000-06-02 18:52:12 +00002998 while( i>=1 && isspace(lemp->arg[i-1]) ) i--;
2999 while( i>=1 && (isalnum(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
drh75897232000-05-29 14:26:00 +00003000 fprintf(out,"#define %sARGDECL ,%s\n",name,&lemp->arg[i]); lineno++;
3001 fprintf(out,"#define %sXARGDECL %s;\n",name,lemp->arg); lineno++;
3002 fprintf(out,"#define %sANSIARGDECL ,%s\n",name,lemp->arg); lineno++;
3003 }else{
3004 fprintf(out,"#define %sARGDECL\n",name); lineno++;
3005 fprintf(out,"#define %sXARGDECL\n",name); lineno++;
3006 fprintf(out,"#define %sANSIARGDECL\n",name); lineno++;
3007 }
3008 if( mhflag ){
3009 fprintf(out,"#endif\n"); lineno++;
3010 }
3011 fprintf(out,"#define YYNSTATE %d\n",lemp->nstate); lineno++;
3012 fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
3013 fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
3014 fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
3015 tplt_xfer(lemp->name,in,out,&lineno);
3016
3017 /* Generate the action table.
3018 **
3019 ** Each entry in the action table is an element of the following
3020 ** structure:
3021 ** struct yyActionEntry {
3022 ** YYCODETYPE lookahead;
3023 ** YYACTIONTYPE action;
3024 ** struct yyActionEntry *next;
3025 ** }
3026 **
3027 ** The entries are grouped into hash tables, one hash table for each
3028 ** parser state. The hash table has a size which is the smallest
3029 ** power of two needed to hold all entries.
3030 */
3031 tablecnt = 0;
3032
3033 /* Loop over parser states */
3034 for(i=0; i<lemp->nstate; i++){
3035 int tablesize; /* size of the hash table */
3036 int j,k; /* Loop counter */
3037 int collide[2048]; /* The collision chain for the table */
3038 struct action *table[2048]; /* Build the hash table here */
3039
3040 /* Find the number of actions and initialize the hash table */
3041 stp = lemp->sorted[i];
3042 stp->tabstart = tablecnt;
3043 stp->naction = 0;
3044 for(ap=stp->ap; ap; ap=ap->next){
3045 if( ap->sp->index!=lemp->nsymbol && compute_action(lemp,ap)>=0 ){
3046 stp->naction++;
3047 }
3048 }
3049 tablesize = 1;
3050 while( tablesize<stp->naction ) tablesize += tablesize;
3051 assert( tablesize<= sizeof(table)/sizeof(table[0]) );
3052 for(j=0; j<tablesize; j++){
3053 table[j] = 0;
3054 collide[j] = -1;
3055 }
3056
3057 /* Hash the actions into the hash table */
3058 stp->tabdfltact = lemp->nstate + lemp->nrule;
3059 for(ap=stp->ap; ap; ap=ap->next){
3060 int action = compute_action(lemp,ap);
3061 int h;
3062 if( ap->sp->index==lemp->nsymbol ){
3063 stp->tabdfltact = action;
3064 }else if( action>=0 ){
3065 h = ap->sp->index & (tablesize-1);
3066 ap->collide = table[h];
3067 table[h] = ap;
3068 }
3069 }
3070
3071 /* Resolve collisions */
3072 for(j=k=0; j<tablesize; j++){
3073 if( table[j] && table[j]->collide ){
3074 while( table[k] ) k++;
3075 table[k] = table[j]->collide;
3076 collide[j] = k;
3077 table[j]->collide = 0;
3078 if( k<j ) j = k-1;
3079 }
3080 }
3081
3082 /* Print the hash table */
3083 fprintf(out,"/* State %d */\n",stp->index); lineno++;
3084 for(j=0; j<tablesize; j++){
3085 if( table[j]==0 ){
3086 fprintf(out,
3087 " {YYNOCODE,0,0}, /* Unused */\n");
3088 }else{
3089 fprintf(out," {%4d,%4d, ",
3090 table[j]->sp->index,
3091 compute_action(lemp,table[j]));
3092 if( collide[j]>=0 ){
3093 fprintf(out,"&yyActionTable[%4d] }, /* ",
3094 collide[j] + tablecnt);
3095 }else{
3096 fprintf(out,"0 }, /* ");
3097 }
3098 PrintAction(table[j],out,22);
3099 fprintf(out," */\n");
3100 }
3101 lineno++;
3102 }
3103
3104 /* Update the table count */
3105 tablecnt += tablesize;
3106 }
3107 tplt_xfer(lemp->name,in,out,&lineno);
3108 lemp->tablesize = tablecnt;
3109
3110 /* Generate the state table
3111 **
3112 ** Each entry is an element of the following structure:
3113 ** struct yyStateEntry {
3114 ** struct yyActionEntry *hashtbl;
3115 ** int mask;
3116 ** YYACTIONTYPE actionDefault;
3117 ** }
3118 */
3119 for(i=0; i<lemp->nstate; i++){
3120 int tablesize;
3121 stp = lemp->sorted[i];
3122 tablesize = 1;
3123 while( tablesize<stp->naction ) tablesize += tablesize;
3124 fprintf(out," { &yyActionTable[%d], %d, %d},\n",
3125 stp->tabstart,
3126 tablesize - 1,
3127 stp->tabdfltact); lineno++;
3128 }
3129 tplt_xfer(lemp->name,in,out,&lineno);
3130
3131 /* Generate a table containing the symbolic name of every symbol */
3132 for(i=0; i<lemp->nsymbol; i++){
3133 sprintf(line,"\"%s\",",lemp->symbols[i]->name);
3134 fprintf(out," %-15s",line);
3135 if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
3136 }
3137 if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
3138 tplt_xfer(lemp->name,in,out,&lineno);
3139
3140 /* Generate code which executes every time a symbol is popped from
3141 ** the stack while processing errors or while destroying the parser.
3142 ** (In other words, generate the %destructor actions) */
3143 if( lemp->tokendest ){
3144 for(i=0; i<lemp->nsymbol; i++){
3145 struct symbol *sp = lemp->symbols[i];
3146 if( sp==0 || sp->type!=TERMINAL ) continue;
3147 fprintf(out," case %d:\n",sp->index); lineno++;
3148 }
3149 for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
3150 if( i<lemp->nsymbol ){
3151 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
3152 fprintf(out," break;\n"); lineno++;
3153 }
3154 }
3155 for(i=0; i<lemp->nsymbol; i++){
3156 struct symbol *sp = lemp->symbols[i];
3157 if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
3158 fprintf(out," case %d:\n",sp->index); lineno++;
3159 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
3160 fprintf(out," break;\n"); lineno++;
3161 }
3162 tplt_xfer(lemp->name,in,out,&lineno);
3163
3164 /* Generate code which executes whenever the parser stack overflows */
3165 tplt_print(out,lemp,lemp->overflow,lemp->overflowln,&lineno);
3166 tplt_xfer(lemp->name,in,out,&lineno);
3167
3168 /* Generate the table of rule information
3169 **
3170 ** Note: This code depends on the fact that rules are number
3171 ** sequentually beginning with 0.
3172 */
3173 for(rp=lemp->rule; rp; rp=rp->next){
3174 fprintf(out," { %d, %d },\n",rp->lhs->index,rp->nrhs); lineno++;
3175 }
3176 tplt_xfer(lemp->name,in,out,&lineno);
3177
3178 /* Generate code which execution during each REDUCE action */
3179 for(rp=lemp->rule; rp; rp=rp->next){
3180 fprintf(out," case %d:\n",rp->index); lineno++;
3181 fprintf(out," YYTRACE(\"%s ::=",rp->lhs->name);
3182 for(i=0; i<rp->nrhs; i++) fprintf(out," %s",rp->rhs[i]->name);
3183 fprintf(out,"\")\n"); lineno++;
3184 emit_code(out,rp,lemp,&lineno);
3185 fprintf(out," break;\n"); lineno++;
3186 }
3187 tplt_xfer(lemp->name,in,out,&lineno);
3188
3189 /* Generate code which executes if a parse fails */
3190 tplt_print(out,lemp,lemp->failure,lemp->failureln,&lineno);
3191 tplt_xfer(lemp->name,in,out,&lineno);
3192
3193 /* Generate code which executes when a syntax error occurs */
3194 tplt_print(out,lemp,lemp->error,lemp->errorln,&lineno);
3195 tplt_xfer(lemp->name,in,out,&lineno);
3196
3197 /* Generate code which executes when the parser accepts its input */
3198 tplt_print(out,lemp,lemp->accept,lemp->acceptln,&lineno);
3199 tplt_xfer(lemp->name,in,out,&lineno);
3200
3201 /* Append any addition code the user desires */
3202 tplt_print(out,lemp,lemp->extracode,lemp->extracodeln,&lineno);
3203
3204 fclose(in);
3205 fclose(out);
3206 return;
3207}
3208
3209/* Generate a header file for the parser */
3210void ReportHeader(lemp)
3211struct lemon *lemp;
3212{
3213 FILE *out, *in;
3214 char *prefix;
3215 char line[LINESIZE];
3216 char pattern[LINESIZE];
3217 int i;
3218
3219 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
3220 else prefix = "";
3221 in = file_open(lemp,".h","r");
3222 if( in ){
3223 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
3224 sprintf(pattern,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3225 if( strcmp(line,pattern) ) break;
3226 }
3227 fclose(in);
3228 if( i==lemp->nterminal ){
3229 /* No change in the file. Don't rewrite it. */
3230 return;
3231 }
3232 }
3233 out = file_open(lemp,".h","w");
3234 if( out ){
3235 for(i=1; i<lemp->nterminal; i++){
3236 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3237 }
3238 fclose(out);
3239 }
3240 return;
3241}
3242
3243/* Reduce the size of the action tables, if possible, by making use
3244** of defaults.
3245**
3246** In this version, if all REDUCE actions use the same rule, make
3247** them the default. Only default them if there are more than one.
3248*/
3249void CompressTables(lemp)
3250struct lemon *lemp;
3251{
3252 struct state *stp;
3253 struct action *ap;
3254 struct rule *rp;
3255 int i;
3256 int cnt;
3257
3258 for(i=0; i<lemp->nstate; i++){
3259 stp = lemp->sorted[i];
3260
3261 /* Find the first REDUCE action */
3262 for(ap=stp->ap; ap && ap->type!=REDUCE; ap=ap->next);
3263 if( ap==0 ) continue;
3264
3265 /* Remember the rule used */
3266 rp = ap->x.rp;
3267
3268 /* See if all other REDUCE acitons use the same rule */
3269 cnt = 1;
3270 for(ap=ap->next; ap; ap=ap->next){
3271 if( ap->type==REDUCE ){
3272 if( ap->x.rp!=rp ) break;
3273 cnt++;
3274 }
3275 }
3276 if( ap || cnt==1 ) continue;
3277
3278 /* Combine all REDUCE actions into a single default */
3279 for(ap=stp->ap; ap && ap->type!=REDUCE; ap=ap->next);
3280 assert( ap );
3281 ap->sp = Symbol_new("{default}");
3282 for(ap=ap->next; ap; ap=ap->next){
3283 if( ap->type==REDUCE ) ap->type = NOT_USED;
3284 }
3285 stp->ap = Action_sort(stp->ap);
3286 }
3287}
3288/***************** From the file "set.c" ************************************/
3289/*
3290** Set manipulation routines for the LEMON parser generator.
3291*/
3292
3293static int size = 0;
3294
3295/* Set the set size */
3296void SetSize(n)
3297int n;
3298{
3299 size = n+1;
3300}
3301
3302/* Allocate a new set */
3303char *SetNew(){
3304 char *s;
3305 int i;
3306 s = (char*)malloc( size );
3307 if( s==0 ){
3308 extern void memory_error();
3309 memory_error();
3310 }
3311 for(i=0; i<size; i++) s[i] = 0;
3312 return s;
3313}
3314
3315/* Deallocate a set */
3316void SetFree(s)
3317char *s;
3318{
3319 free(s);
3320}
3321
3322/* Add a new element to the set. Return TRUE if the element was added
3323** and FALSE if it was already there. */
3324int SetAdd(s,e)
3325char *s;
3326int e;
3327{
3328 int rv;
3329 rv = s[e];
3330 s[e] = 1;
3331 return !rv;
3332}
3333
3334/* Add every element of s2 to s1. Return TRUE if s1 changes. */
3335int SetUnion(s1,s2)
3336char *s1;
3337char *s2;
3338{
3339 int i, progress;
3340 progress = 0;
3341 for(i=0; i<size; i++){
3342 if( s2[i]==0 ) continue;
3343 if( s1[i]==0 ){
3344 progress = 1;
3345 s1[i] = 1;
3346 }
3347 }
3348 return progress;
3349}
3350/********************** From the file "table.c" ****************************/
3351/*
3352** All code in this file has been automatically generated
3353** from a specification in the file
3354** "table.q"
3355** by the associative array code building program "aagen".
3356** Do not edit this file! Instead, edit the specification
3357** file, then rerun aagen.
3358*/
3359/*
3360** Code for processing tables in the LEMON parser generator.
3361*/
3362
3363PRIVATE int strhash(x)
3364char *x;
3365{
3366 int h = 0;
3367 while( *x) h = h*13 + *(x++);
3368 return h;
3369}
3370
3371/* Works like strdup, sort of. Save a string in malloced memory, but
3372** keep strings in a table so that the same string is not in more
3373** than one place.
3374*/
3375char *Strsafe(y)
3376char *y;
3377{
3378 char *z;
3379
3380 z = Strsafe_find(y);
3381 if( z==0 && (z=malloc( strlen(y)+1 ))!=0 ){
3382 strcpy(z,y);
3383 Strsafe_insert(z);
3384 }
3385 MemoryCheck(z);
3386 return z;
3387}
3388
3389/* There is one instance of the following structure for each
3390** associative array of type "x1".
3391*/
3392struct s_x1 {
3393 int size; /* The number of available slots. */
3394 /* Must be a power of 2 greater than or */
3395 /* equal to 1 */
3396 int count; /* Number of currently slots filled */
3397 struct s_x1node *tbl; /* The data stored here */
3398 struct s_x1node **ht; /* Hash table for lookups */
3399};
3400
3401/* There is one instance of this structure for every data element
3402** in an associative array of type "x1".
3403*/
3404typedef struct s_x1node {
3405 char *data; /* The data */
3406 struct s_x1node *next; /* Next entry with the same hash */
3407 struct s_x1node **from; /* Previous link */
3408} x1node;
3409
3410/* There is only one instance of the array, which is the following */
3411static struct s_x1 *x1a;
3412
3413/* Allocate a new associative array */
3414void Strsafe_init(){
3415 if( x1a ) return;
3416 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
3417 if( x1a ){
3418 x1a->size = 1024;
3419 x1a->count = 0;
3420 x1a->tbl = (x1node*)malloc(
3421 (sizeof(x1node) + sizeof(x1node*))*1024 );
3422 if( x1a->tbl==0 ){
3423 free(x1a);
3424 x1a = 0;
3425 }else{
3426 int i;
3427 x1a->ht = (x1node**)&(x1a->tbl[1024]);
3428 for(i=0; i<1024; i++) x1a->ht[i] = 0;
3429 }
3430 }
3431}
3432/* Insert a new record into the array. Return TRUE if successful.
3433** Prior data with the same key is NOT overwritten */
3434int Strsafe_insert(data)
3435char *data;
3436{
3437 x1node *np;
3438 int h;
3439 int ph;
3440
3441 if( x1a==0 ) return 0;
3442 ph = strhash(data);
3443 h = ph & (x1a->size-1);
3444 np = x1a->ht[h];
3445 while( np ){
3446 if( strcmp(np->data,data)==0 ){
3447 /* An existing entry with the same key is found. */
3448 /* Fail because overwrite is not allows. */
3449 return 0;
3450 }
3451 np = np->next;
3452 }
3453 if( x1a->count>=x1a->size ){
3454 /* Need to make the hash table bigger */
3455 int i,size;
3456 struct s_x1 array;
3457 array.size = size = x1a->size*2;
3458 array.count = x1a->count;
3459 array.tbl = (x1node*)malloc(
3460 (sizeof(x1node) + sizeof(x1node*))*size );
3461 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
3462 array.ht = (x1node**)&(array.tbl[size]);
3463 for(i=0; i<size; i++) array.ht[i] = 0;
3464 for(i=0; i<x1a->count; i++){
3465 x1node *oldnp, *newnp;
3466 oldnp = &(x1a->tbl[i]);
3467 h = strhash(oldnp->data) & (size-1);
3468 newnp = &(array.tbl[i]);
3469 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
3470 newnp->next = array.ht[h];
3471 newnp->data = oldnp->data;
3472 newnp->from = &(array.ht[h]);
3473 array.ht[h] = newnp;
3474 }
3475 free(x1a->tbl);
3476 *x1a = array;
3477 }
3478 /* Insert the new data */
3479 h = ph & (x1a->size-1);
3480 np = &(x1a->tbl[x1a->count++]);
3481 np->data = data;
3482 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
3483 np->next = x1a->ht[h];
3484 x1a->ht[h] = np;
3485 np->from = &(x1a->ht[h]);
3486 return 1;
3487}
3488
3489/* Return a pointer to data assigned to the given key. Return NULL
3490** if no such key. */
3491char *Strsafe_find(key)
3492char *key;
3493{
3494 int h;
3495 x1node *np;
3496
3497 if( x1a==0 ) return 0;
3498 h = strhash(key) & (x1a->size-1);
3499 np = x1a->ht[h];
3500 while( np ){
3501 if( strcmp(np->data,key)==0 ) break;
3502 np = np->next;
3503 }
3504 return np ? np->data : 0;
3505}
3506
3507/* Return a pointer to the (terminal or nonterminal) symbol "x".
3508** Create a new symbol if this is the first time "x" has been seen.
3509*/
3510struct symbol *Symbol_new(x)
3511char *x;
3512{
3513 struct symbol *sp;
3514
3515 sp = Symbol_find(x);
3516 if( sp==0 ){
3517 sp = (struct symbol *)malloc( sizeof(struct symbol) );
3518 MemoryCheck(sp);
3519 sp->name = Strsafe(x);
3520 sp->type = isupper(*x) ? TERMINAL : NONTERMINAL;
3521 sp->rule = 0;
3522 sp->prec = -1;
3523 sp->assoc = UNK;
3524 sp->firstset = 0;
3525 sp->lambda = FALSE;
3526 sp->destructor = 0;
3527 sp->datatype = 0;
3528 Symbol_insert(sp,sp->name);
3529 }
3530 return sp;
3531}
3532
3533/* Compare two symbols */
3534int Symbolcmpp(a,b)
3535struct symbol **a;
3536struct symbol **b;
3537{
3538 return strcmp((**a).name,(**b).name);
3539}
3540
3541/* There is one instance of the following structure for each
3542** associative array of type "x2".
3543*/
3544struct s_x2 {
3545 int size; /* The number of available slots. */
3546 /* Must be a power of 2 greater than or */
3547 /* equal to 1 */
3548 int count; /* Number of currently slots filled */
3549 struct s_x2node *tbl; /* The data stored here */
3550 struct s_x2node **ht; /* Hash table for lookups */
3551};
3552
3553/* There is one instance of this structure for every data element
3554** in an associative array of type "x2".
3555*/
3556typedef struct s_x2node {
3557 struct symbol *data; /* The data */
3558 char *key; /* The key */
3559 struct s_x2node *next; /* Next entry with the same hash */
3560 struct s_x2node **from; /* Previous link */
3561} x2node;
3562
3563/* There is only one instance of the array, which is the following */
3564static struct s_x2 *x2a;
3565
3566/* Allocate a new associative array */
3567void Symbol_init(){
3568 if( x2a ) return;
3569 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
3570 if( x2a ){
3571 x2a->size = 128;
3572 x2a->count = 0;
3573 x2a->tbl = (x2node*)malloc(
3574 (sizeof(x2node) + sizeof(x2node*))*128 );
3575 if( x2a->tbl==0 ){
3576 free(x2a);
3577 x2a = 0;
3578 }else{
3579 int i;
3580 x2a->ht = (x2node**)&(x2a->tbl[128]);
3581 for(i=0; i<128; i++) x2a->ht[i] = 0;
3582 }
3583 }
3584}
3585/* Insert a new record into the array. Return TRUE if successful.
3586** Prior data with the same key is NOT overwritten */
3587int Symbol_insert(data,key)
3588struct symbol *data;
3589char *key;
3590{
3591 x2node *np;
3592 int h;
3593 int ph;
3594
3595 if( x2a==0 ) return 0;
3596 ph = strhash(key);
3597 h = ph & (x2a->size-1);
3598 np = x2a->ht[h];
3599 while( np ){
3600 if( strcmp(np->key,key)==0 ){
3601 /* An existing entry with the same key is found. */
3602 /* Fail because overwrite is not allows. */
3603 return 0;
3604 }
3605 np = np->next;
3606 }
3607 if( x2a->count>=x2a->size ){
3608 /* Need to make the hash table bigger */
3609 int i,size;
3610 struct s_x2 array;
3611 array.size = size = x2a->size*2;
3612 array.count = x2a->count;
3613 array.tbl = (x2node*)malloc(
3614 (sizeof(x2node) + sizeof(x2node*))*size );
3615 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
3616 array.ht = (x2node**)&(array.tbl[size]);
3617 for(i=0; i<size; i++) array.ht[i] = 0;
3618 for(i=0; i<x2a->count; i++){
3619 x2node *oldnp, *newnp;
3620 oldnp = &(x2a->tbl[i]);
3621 h = strhash(oldnp->key) & (size-1);
3622 newnp = &(array.tbl[i]);
3623 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
3624 newnp->next = array.ht[h];
3625 newnp->key = oldnp->key;
3626 newnp->data = oldnp->data;
3627 newnp->from = &(array.ht[h]);
3628 array.ht[h] = newnp;
3629 }
3630 free(x2a->tbl);
3631 *x2a = array;
3632 }
3633 /* Insert the new data */
3634 h = ph & (x2a->size-1);
3635 np = &(x2a->tbl[x2a->count++]);
3636 np->key = key;
3637 np->data = data;
3638 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
3639 np->next = x2a->ht[h];
3640 x2a->ht[h] = np;
3641 np->from = &(x2a->ht[h]);
3642 return 1;
3643}
3644
3645/* Return a pointer to data assigned to the given key. Return NULL
3646** if no such key. */
3647struct symbol *Symbol_find(key)
3648char *key;
3649{
3650 int h;
3651 x2node *np;
3652
3653 if( x2a==0 ) return 0;
3654 h = strhash(key) & (x2a->size-1);
3655 np = x2a->ht[h];
3656 while( np ){
3657 if( strcmp(np->key,key)==0 ) break;
3658 np = np->next;
3659 }
3660 return np ? np->data : 0;
3661}
3662
3663/* Return the n-th data. Return NULL if n is out of range. */
3664struct symbol *Symbol_Nth(n)
3665int n;
3666{
3667 struct symbol *data;
3668 if( x2a && n>0 && n<=x2a->count ){
3669 data = x2a->tbl[n-1].data;
3670 }else{
3671 data = 0;
3672 }
3673 return data;
3674}
3675
3676/* Return the size of the array */
3677int Symbol_count()
3678{
3679 return x2a ? x2a->count : 0;
3680}
3681
3682/* Return an array of pointers to all data in the table.
3683** The array is obtained from malloc. Return NULL if memory allocation
3684** problems, or if the array is empty. */
3685struct symbol **Symbol_arrayof()
3686{
3687 struct symbol **array;
3688 int i,size;
3689 if( x2a==0 ) return 0;
3690 size = x2a->count;
3691 array = (struct symbol **)malloc( sizeof(struct symbol *)*size );
3692 if( array ){
3693 for(i=0; i<size; i++) array[i] = x2a->tbl[i].data;
3694 }
3695 return array;
3696}
3697
3698/* Compare two configurations */
3699int Configcmp(a,b)
3700struct config *a;
3701struct config *b;
3702{
3703 int x;
3704 x = a->rp->index - b->rp->index;
3705 if( x==0 ) x = a->dot - b->dot;
3706 return x;
3707}
3708
3709/* Compare two states */
3710PRIVATE int statecmp(a,b)
3711struct config *a;
3712struct config *b;
3713{
3714 int rc;
3715 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
3716 rc = a->rp->index - b->rp->index;
3717 if( rc==0 ) rc = a->dot - b->dot;
3718 }
3719 if( rc==0 ){
3720 if( a ) rc = 1;
3721 if( b ) rc = -1;
3722 }
3723 return rc;
3724}
3725
3726/* Hash a state */
3727PRIVATE int statehash(a)
3728struct config *a;
3729{
3730 int h=0;
3731 while( a ){
3732 h = h*571 + a->rp->index*37 + a->dot;
3733 a = a->bp;
3734 }
3735 return h;
3736}
3737
3738/* Allocate a new state structure */
3739struct state *State_new()
3740{
3741 struct state *new;
3742 new = (struct state *)malloc( sizeof(struct state) );
3743 MemoryCheck(new);
3744 return new;
3745}
3746
3747/* There is one instance of the following structure for each
3748** associative array of type "x3".
3749*/
3750struct s_x3 {
3751 int size; /* The number of available slots. */
3752 /* Must be a power of 2 greater than or */
3753 /* equal to 1 */
3754 int count; /* Number of currently slots filled */
3755 struct s_x3node *tbl; /* The data stored here */
3756 struct s_x3node **ht; /* Hash table for lookups */
3757};
3758
3759/* There is one instance of this structure for every data element
3760** in an associative array of type "x3".
3761*/
3762typedef struct s_x3node {
3763 struct state *data; /* The data */
3764 struct config *key; /* The key */
3765 struct s_x3node *next; /* Next entry with the same hash */
3766 struct s_x3node **from; /* Previous link */
3767} x3node;
3768
3769/* There is only one instance of the array, which is the following */
3770static struct s_x3 *x3a;
3771
3772/* Allocate a new associative array */
3773void State_init(){
3774 if( x3a ) return;
3775 x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
3776 if( x3a ){
3777 x3a->size = 128;
3778 x3a->count = 0;
3779 x3a->tbl = (x3node*)malloc(
3780 (sizeof(x3node) + sizeof(x3node*))*128 );
3781 if( x3a->tbl==0 ){
3782 free(x3a);
3783 x3a = 0;
3784 }else{
3785 int i;
3786 x3a->ht = (x3node**)&(x3a->tbl[128]);
3787 for(i=0; i<128; i++) x3a->ht[i] = 0;
3788 }
3789 }
3790}
3791/* Insert a new record into the array. Return TRUE if successful.
3792** Prior data with the same key is NOT overwritten */
3793int State_insert(data,key)
3794struct state *data;
3795struct config *key;
3796{
3797 x3node *np;
3798 int h;
3799 int ph;
3800
3801 if( x3a==0 ) return 0;
3802 ph = statehash(key);
3803 h = ph & (x3a->size-1);
3804 np = x3a->ht[h];
3805 while( np ){
3806 if( statecmp(np->key,key)==0 ){
3807 /* An existing entry with the same key is found. */
3808 /* Fail because overwrite is not allows. */
3809 return 0;
3810 }
3811 np = np->next;
3812 }
3813 if( x3a->count>=x3a->size ){
3814 /* Need to make the hash table bigger */
3815 int i,size;
3816 struct s_x3 array;
3817 array.size = size = x3a->size*2;
3818 array.count = x3a->count;
3819 array.tbl = (x3node*)malloc(
3820 (sizeof(x3node) + sizeof(x3node*))*size );
3821 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
3822 array.ht = (x3node**)&(array.tbl[size]);
3823 for(i=0; i<size; i++) array.ht[i] = 0;
3824 for(i=0; i<x3a->count; i++){
3825 x3node *oldnp, *newnp;
3826 oldnp = &(x3a->tbl[i]);
3827 h = statehash(oldnp->key) & (size-1);
3828 newnp = &(array.tbl[i]);
3829 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
3830 newnp->next = array.ht[h];
3831 newnp->key = oldnp->key;
3832 newnp->data = oldnp->data;
3833 newnp->from = &(array.ht[h]);
3834 array.ht[h] = newnp;
3835 }
3836 free(x3a->tbl);
3837 *x3a = array;
3838 }
3839 /* Insert the new data */
3840 h = ph & (x3a->size-1);
3841 np = &(x3a->tbl[x3a->count++]);
3842 np->key = key;
3843 np->data = data;
3844 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
3845 np->next = x3a->ht[h];
3846 x3a->ht[h] = np;
3847 np->from = &(x3a->ht[h]);
3848 return 1;
3849}
3850
3851/* Return a pointer to data assigned to the given key. Return NULL
3852** if no such key. */
3853struct state *State_find(key)
3854struct config *key;
3855{
3856 int h;
3857 x3node *np;
3858
3859 if( x3a==0 ) return 0;
3860 h = statehash(key) & (x3a->size-1);
3861 np = x3a->ht[h];
3862 while( np ){
3863 if( statecmp(np->key,key)==0 ) break;
3864 np = np->next;
3865 }
3866 return np ? np->data : 0;
3867}
3868
3869/* Return an array of pointers to all data in the table.
3870** The array is obtained from malloc. Return NULL if memory allocation
3871** problems, or if the array is empty. */
3872struct state **State_arrayof()
3873{
3874 struct state **array;
3875 int i,size;
3876 if( x3a==0 ) return 0;
3877 size = x3a->count;
3878 array = (struct state **)malloc( sizeof(struct state *)*size );
3879 if( array ){
3880 for(i=0; i<size; i++) array[i] = x3a->tbl[i].data;
3881 }
3882 return array;
3883}
3884
3885/* Hash a configuration */
3886PRIVATE int confighash(a)
3887struct config *a;
3888{
3889 int h=0;
3890 h = h*571 + a->rp->index*37 + a->dot;
3891 return h;
3892}
3893
3894/* There is one instance of the following structure for each
3895** associative array of type "x4".
3896*/
3897struct s_x4 {
3898 int size; /* The number of available slots. */
3899 /* Must be a power of 2 greater than or */
3900 /* equal to 1 */
3901 int count; /* Number of currently slots filled */
3902 struct s_x4node *tbl; /* The data stored here */
3903 struct s_x4node **ht; /* Hash table for lookups */
3904};
3905
3906/* There is one instance of this structure for every data element
3907** in an associative array of type "x4".
3908*/
3909typedef struct s_x4node {
3910 struct config *data; /* The data */
3911 struct s_x4node *next; /* Next entry with the same hash */
3912 struct s_x4node **from; /* Previous link */
3913} x4node;
3914
3915/* There is only one instance of the array, which is the following */
3916static struct s_x4 *x4a;
3917
3918/* Allocate a new associative array */
3919void Configtable_init(){
3920 if( x4a ) return;
3921 x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
3922 if( x4a ){
3923 x4a->size = 64;
3924 x4a->count = 0;
3925 x4a->tbl = (x4node*)malloc(
3926 (sizeof(x4node) + sizeof(x4node*))*64 );
3927 if( x4a->tbl==0 ){
3928 free(x4a);
3929 x4a = 0;
3930 }else{
3931 int i;
3932 x4a->ht = (x4node**)&(x4a->tbl[64]);
3933 for(i=0; i<64; i++) x4a->ht[i] = 0;
3934 }
3935 }
3936}
3937/* Insert a new record into the array. Return TRUE if successful.
3938** Prior data with the same key is NOT overwritten */
3939int Configtable_insert(data)
3940struct config *data;
3941{
3942 x4node *np;
3943 int h;
3944 int ph;
3945
3946 if( x4a==0 ) return 0;
3947 ph = confighash(data);
3948 h = ph & (x4a->size-1);
3949 np = x4a->ht[h];
3950 while( np ){
3951 if( Configcmp(np->data,data)==0 ){
3952 /* An existing entry with the same key is found. */
3953 /* Fail because overwrite is not allows. */
3954 return 0;
3955 }
3956 np = np->next;
3957 }
3958 if( x4a->count>=x4a->size ){
3959 /* Need to make the hash table bigger */
3960 int i,size;
3961 struct s_x4 array;
3962 array.size = size = x4a->size*2;
3963 array.count = x4a->count;
3964 array.tbl = (x4node*)malloc(
3965 (sizeof(x4node) + sizeof(x4node*))*size );
3966 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
3967 array.ht = (x4node**)&(array.tbl[size]);
3968 for(i=0; i<size; i++) array.ht[i] = 0;
3969 for(i=0; i<x4a->count; i++){
3970 x4node *oldnp, *newnp;
3971 oldnp = &(x4a->tbl[i]);
3972 h = confighash(oldnp->data) & (size-1);
3973 newnp = &(array.tbl[i]);
3974 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
3975 newnp->next = array.ht[h];
3976 newnp->data = oldnp->data;
3977 newnp->from = &(array.ht[h]);
3978 array.ht[h] = newnp;
3979 }
3980 free(x4a->tbl);
3981 *x4a = array;
3982 }
3983 /* Insert the new data */
3984 h = ph & (x4a->size-1);
3985 np = &(x4a->tbl[x4a->count++]);
3986 np->data = data;
3987 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
3988 np->next = x4a->ht[h];
3989 x4a->ht[h] = np;
3990 np->from = &(x4a->ht[h]);
3991 return 1;
3992}
3993
3994/* Return a pointer to data assigned to the given key. Return NULL
3995** if no such key. */
3996struct config *Configtable_find(key)
3997struct config *key;
3998{
3999 int h;
4000 x4node *np;
4001
4002 if( x4a==0 ) return 0;
4003 h = confighash(key) & (x4a->size-1);
4004 np = x4a->ht[h];
4005 while( np ){
4006 if( Configcmp(np->data,key)==0 ) break;
4007 np = np->next;
4008 }
4009 return np ? np->data : 0;
4010}
4011
4012/* Remove all data from the table. Pass each data to the function "f"
4013** as it is removed. ("f" may be null to avoid this step.) */
4014void Configtable_clear(f)
4015int(*f)(/* struct config * */);
4016{
4017 int i;
4018 if( x4a==0 || x4a->count==0 ) return;
4019 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
4020 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
4021 x4a->count = 0;
4022 return;
4023}