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