2
** This file contains all sources (including headers) to the LEMON
3
** LALR(1) parser generator. The sources have been combined into a
4
** single file to make it easy to include LEMON in the source tree
5
** and Makefile of another program.
7
** The author of this program disclaims copyright.
16
# if defined(_WIN32) || defined(WIN32)
21
/* #define PRIVATE static */
25
#define MAXRHS 5 /* Set low to exercise exception code */
31
extern void *malloc();
33
/******** From the file "action.h" *************************************/
34
struct action *Action_new();
35
struct action *Action_sort();
37
/********* From the file "assert.h" ************************************/
40
# define assert(X) if(!(X))myassert(__FILE__,__LINE__)
45
/********** From the file "build.h" ************************************/
46
void FindRulePrecedences();
50
void FindFollowSets();
53
/********* From the file "configlist.h" *********************************/
54
void Configlist_init(/* void */);
55
struct config *Configlist_add(/* struct rule *, int */);
56
struct config *Configlist_addbasis(/* struct rule *, int */);
57
void Configlist_closure(/* void */);
58
void Configlist_sort(/* void */);
59
void Configlist_sortbasis(/* void */);
60
struct config *Configlist_return(/* void */);
61
struct config *Configlist_basis(/* void */);
62
void Configlist_eat(/* struct config * */);
63
void Configlist_reset(/* void */);
65
/********* From the file "error.h" ***************************************/
66
void ErrorMsg(const char *, int,const char *, ...);
68
/****** From the file "option.h" ******************************************/
70
enum { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR,
71
OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type;
76
int OptInit(/* char**,struct s_options*,FILE* */);
77
int OptNArgs(/* void */);
78
char *OptArg(/* int */);
79
void OptErr(/* int */);
80
void OptPrint(/* void */);
82
/******** From the file "parse.h" *****************************************/
83
void Parse(/* struct lemon *lemp */);
85
/********* From the file "plink.h" ***************************************/
86
struct plink *Plink_new(/* void */);
87
void Plink_add(/* struct plink **, struct config * */);
88
void Plink_copy(/* struct plink **, struct plink * */);
89
void Plink_delete(/* struct plink * */);
91
/********** From the file "report.h" *************************************/
92
void Reprint(/* struct lemon * */);
93
void ReportOutput(/* struct lemon * */);
94
void ReportTable(/* struct lemon * */);
95
void ReportHeader(/* struct lemon * */);
96
void CompressTables(/* struct lemon * */);
97
void ResortStates(/* struct lemon * */);
99
/********** From the file "set.h" ****************************************/
100
void SetSize(/* int N */); /* All sets will be of size N */
101
char *SetNew(/* void */); /* A new set for element 0..N */
102
void SetFree(/* char* */); /* Deallocate a set */
104
int SetAdd(/* char*,int */); /* Add element to a set */
105
int SetUnion(/* char *A,char *B */); /* A <- A U B, thru element N */
107
#define SetFind(X,Y) (X[Y]) /* True if Y is in set X */
109
/********** From the file "struct.h" *************************************/
111
** Principal data structures for the LEMON parser generator.
114
typedef enum {B_FALSE=0, B_TRUE} Boolean;
116
/* Symbols (terminals and nonterminals) of the grammar are stored
117
** in the following: */
119
char *name; /* Name of the symbol */
120
int index; /* Index number for this symbol */
125
} type; /* Symbols are all either TERMINALS or NTs */
126
struct rule *rule; /* Linked list of rules of this (if an NT) */
127
struct symbol *fallback; /* fallback token in case this token doesn't parse */
128
int prec; /* Precedence if defined (-1 otherwise) */
134
} assoc; /* Associativity if predecence is defined */
135
char *firstset; /* First-set for all rules of this symbol */
136
Boolean lambda; /* True if NT and can generate an empty string */
137
char *destructor; /* Code which executes whenever this symbol is
138
** popped from the stack during error processing */
139
int destructorln; /* Line number of destructor code */
140
char *datatype; /* The data type of information held by this
141
** object. Only used if type==NONTERMINAL */
142
int dtnum; /* The data type number. In the parser, the value
143
** stack is a union. The .yy%d element of this
144
** union is the correct data type for this object */
145
/* The following fields are used by MULTITERMINALs only */
146
int nsubsym; /* Number of constituent symbols in the MULTI */
147
struct symbol **subsym; /* Array of constituent symbols */
150
/* Each production rule in the grammar is stored in the following
153
struct symbol *lhs; /* Left-hand side of the rule */
154
char *lhsalias; /* Alias for the LHS (NULL if none) */
155
int ruleline; /* Line number for the rule */
156
int nrhs; /* Number of RHS symbols */
157
struct symbol **rhs; /* The RHS symbols */
158
char **rhsalias; /* An alias for each RHS symbol (NULL if none) */
159
int line; /* Line number at which code begins */
160
char *code; /* The code executed when this rule is reduced */
161
struct symbol *precsym; /* Precedence symbol for this rule */
162
int index; /* An index number for this rule */
163
Boolean canReduce; /* True if this rule is ever reduced */
164
struct rule *nextlhs; /* Next rule with the same LHS */
165
struct rule *next; /* Next rule in the global list */
168
/* A configuration is a production rule of the grammar together with
169
** a mark (dot) showing how much of that rule has been processed so far.
170
** Configurations also contain a follow-set which is a list of terminal
171
** symbols which are allowed to immediately follow the end of the rule.
172
** Every configuration is recorded as an instance of the following: */
174
struct rule *rp; /* The rule upon which the configuration is based */
175
int dot; /* The parse point */
176
char *fws; /* Follow-set for this configuration only */
177
struct plink *fplp; /* Follow-set forward propagation links */
178
struct plink *bplp; /* Follow-set backwards propagation links */
179
struct state *stp; /* Pointer to state which contains this */
181
COMPLETE, /* The status is used during followset and */
182
INCOMPLETE /* shift computations */
184
struct config *next; /* Next configuration in the state */
185
struct config *bp; /* The next basis configuration */
188
/* Every shift or reduce operation is stored as one of the following */
190
struct symbol *sp; /* The look-ahead symbol */
196
CONFLICT, /* Was a reduce, but part of a conflict */
197
SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
198
RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
199
NOT_USED /* Deleted by compression */
202
struct state *stp; /* The new state, if a shift */
203
struct rule *rp; /* The rule, if a reduce */
205
struct action *next; /* Next action for this state */
206
struct action *collide; /* Next action with the same hash */
209
/* Each state of the generated parser's finite state machine
210
** is encoded as an instance of the following structure. */
212
struct config *bp; /* The basis configurations for this state */
213
struct config *cfp; /* All configurations in this set */
214
int statenum; /* Sequencial number for this state */
215
struct action *ap; /* Array of actions for this state */
216
int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */
217
int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */
218
int iDflt; /* Default action */
220
#define NO_OFFSET (-2147483647)
222
/* A followset propagation link indicates that the contents of one
223
** configuration followset should be propagated to another whenever
224
** the first changes. */
226
struct config *cfp; /* The configuration to which linked */
227
struct plink *next; /* The next propagate link */
230
/* The state vector for the entire parser generator is recorded as
231
** follows. (LEMON uses no global variables and makes little use of
232
** static variables. Fields in the following structure can be thought
233
** of as begin global variables in the program.) */
235
struct state **sorted; /* Table of states sorted by state number */
236
struct rule *rule; /* List of all rules */
237
int nstate; /* Number of states */
238
int nrule; /* Number of rules */
239
int nsymbol; /* Number of terminal and nonterminal symbols */
240
int nterminal; /* Number of terminal symbols */
241
struct symbol **symbols; /* Sorted array of pointers to symbols */
242
int errorcnt; /* Number of errors */
243
struct symbol *errsym; /* The error symbol */
244
char *name; /* Name of the generated parser */
245
char *arg; /* Declaration of the 3th argument to parser */
246
char *tokentype; /* Type of terminal symbols in the parser stack */
247
char *vartype; /* The default type of non-terminal symbols */
248
char *start; /* Name of the start symbol for the grammar */
249
char *stacksize; /* Size of the parser stack */
250
char *include; /* Code to put at the start of the C file */
251
int includeln; /* Line number for start of include code */
252
char *error; /* Code to execute when an error is seen */
253
int errorln; /* Line number for start of error code */
254
char *overflow; /* Code to execute on a stack overflow */
255
int overflowln; /* Line number for start of overflow code */
256
char *failure; /* Code to execute on parser failure */
257
int failureln; /* Line number for start of failure code */
258
char *accept; /* Code to execute when the parser excepts */
259
int acceptln; /* Line number for the start of accept code */
260
char *extracode; /* Code appended to the generated file */
261
int extracodeln; /* Line number for the start of the extra code */
262
char *tokendest; /* Code to execute to destroy token data */
263
int tokendestln; /* Line number for token destroyer code */
264
char *vardest; /* Code for the default non-terminal destructor */
265
int vardestln; /* Line number for default non-term destructor code*/
266
char *filename; /* Name of the input file */
267
char *outname; /* Name of the current output file */
268
char *tokenprefix; /* A prefix added to token names in the .h file */
269
int nconflict; /* Number of parsing conflicts */
270
int tablesize; /* Size of the parse tables */
271
int basisflag; /* Print only basis configurations */
272
int has_fallback; /* True if any %fallback is seen in the grammer */
273
char *argv0; /* Name of the program */
276
#define MemoryCheck(X) if((X)==0){ \
277
extern void memory_error(); \
281
/**************** From the file "table.h" *********************************/
283
** All code in this file has been automatically generated
284
** from a specification in the file
286
** by the associative array code building program "aagen".
287
** Do not edit this file! Instead, edit the specification
288
** file, then rerun aagen.
291
** Code for processing tables in the LEMON parser generator.
294
/* Routines for handling a strings */
298
void Strsafe_init(/* void */);
299
int Strsafe_insert(/* char * */);
300
char *Strsafe_find(/* char * */);
302
/* Routines for handling symbols of the grammar */
304
struct symbol *Symbol_new();
305
int Symbolcmpp(/* struct symbol **, struct symbol ** */);
306
void Symbol_init(/* void */);
307
int Symbol_insert(/* struct symbol *, char * */);
308
struct symbol *Symbol_find(/* char * */);
309
struct symbol *Symbol_Nth(/* int */);
310
int Symbol_count(/* */);
311
struct symbol **Symbol_arrayof(/* */);
313
/* Routines to manage the state table */
315
int Configcmp(/* struct config *, struct config * */);
316
struct state *State_new();
317
void State_init(/* void */);
318
int State_insert(/* struct state *, struct config * */);
319
struct state *State_find(/* struct config * */);
320
struct state **State_arrayof(/* */);
322
/* Routines used for efficiency in Configlist_add */
324
void Configtable_init(/* void */);
325
int Configtable_insert(/* struct config * */);
326
struct config *Configtable_find(/* struct config * */);
327
void Configtable_clear(/* int(*)(struct config *) */);
328
/****************** From the file "action.c" *******************************/
330
** Routines processing parser actions in the LEMON parser generator.
333
/* Allocate a new parser action */
334
struct action *Action_new(){
335
static struct action *freelist = 0;
341
freelist = (struct action *)malloc( sizeof(struct action)*amt );
343
fprintf(stderr,"Unable to allocate memory for a new parser action.");
346
for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
347
freelist[amt-1].next = 0;
350
freelist = freelist->next;
354
/* Compare two actions */
355
static int actioncmp(ap1,ap2)
360
rc = ap1->sp->index - ap2->sp->index;
361
if( rc==0 ) rc = (int)ap1->type - (int)ap2->type;
363
assert( ap1->type==REDUCE || ap1->type==RD_RESOLVED || ap1->type==CONFLICT);
364
assert( ap2->type==REDUCE || ap2->type==RD_RESOLVED || ap2->type==CONFLICT);
365
rc = ap1->x.rp->index - ap2->x.rp->index;
370
/* Sort parser actions */
371
struct action *Action_sort(ap)
374
ap = (struct action *)msort((char *)ap,(char **)&ap->next,actioncmp);
378
void Action_add(app,type,sp,arg)
391
new->x.stp = (struct state *)arg;
393
new->x.rp = (struct rule *)arg;
396
/********************** New code to implement the "acttab" module ***********/
398
** This module implements routines use to construct the yy_action[] table.
402
** The state of the yy_action table under construction is an instance of
403
** the following structure
405
typedef struct acttab acttab;
407
int nAction; /* Number of used slots in aAction[] */
408
int nActionAlloc; /* Slots allocated for aAction[] */
410
int lookahead; /* Value of the lookahead token */
411
int action; /* Action to take on the given lookahead */
412
} *aAction, /* The yy_action[] table under construction */
413
*aLookahead; /* A single new transaction set */
414
int mnLookahead; /* Minimum aLookahead[].lookahead */
415
int mnAction; /* Action associated with mnLookahead */
416
int mxLookahead; /* Maximum aLookahead[].lookahead */
417
int nLookahead; /* Used slots in aLookahead[] */
418
int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
421
/* Return the number of entries in the yy_action table */
422
#define acttab_size(X) ((X)->nAction)
424
/* The value for the N-th entry in yy_action */
425
#define acttab_yyaction(X,N) ((X)->aAction[N].action)
427
/* The value for the N-th entry in yy_lookahead */
428
#define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
430
/* Free all memory associated with the given acttab */
431
void acttab_free(acttab *p){
433
free( p->aLookahead );
437
/* Allocate a new acttab structure */
438
acttab *acttab_alloc(void){
439
acttab *p = malloc( sizeof(*p) );
441
fprintf(stderr,"Unable to allocate memory for a new acttab.");
444
memset(p, 0, sizeof(*p));
448
/* Add a new action to the current transaction set
450
void acttab_action(acttab *p, int lookahead, int action){
451
if( p->nLookahead>=p->nLookaheadAlloc ){
452
p->nLookaheadAlloc += 25;
453
p->aLookahead = realloc( p->aLookahead,
454
sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
455
if( p->aLookahead==0 ){
456
fprintf(stderr,"malloc failed\n");
460
if( p->nLookahead==0 ){
461
p->mxLookahead = lookahead;
462
p->mnLookahead = lookahead;
463
p->mnAction = action;
465
if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
466
if( p->mnLookahead>lookahead ){
467
p->mnLookahead = lookahead;
468
p->mnAction = action;
471
p->aLookahead[p->nLookahead].lookahead = lookahead;
472
p->aLookahead[p->nLookahead].action = action;
477
** Add the transaction set built up with prior calls to acttab_action()
478
** into the current action table. Then reset the transaction set back
479
** to an empty set in preparation for a new round of acttab_action() calls.
481
** Return the offset into the action table of the new transaction.
483
int acttab_insert(acttab *p){
485
assert( p->nLookahead>0 );
487
/* Make sure we have enough space to hold the expanded action table
488
** in the worst case. The worst case occurs if the transaction set
489
** must be appended to the current action table
491
n = p->mxLookahead + 1;
492
if( p->nAction + n >= p->nActionAlloc ){
493
int oldAlloc = p->nActionAlloc;
494
p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
495
p->aAction = realloc( p->aAction,
496
sizeof(p->aAction[0])*p->nActionAlloc);
498
fprintf(stderr,"malloc failed\n");
501
for(i=oldAlloc; i<p->nActionAlloc; i++){
502
p->aAction[i].lookahead = -1;
503
p->aAction[i].action = -1;
507
/* Scan the existing action table looking for an offset where we can
508
** insert the current transaction set. Fall out of the loop when that
509
** offset is found. In the worst case, we fall out of the loop when
510
** i reaches p->nAction, which means we append the new transaction set.
512
** i is the index in p->aAction[] where p->mnLookahead is inserted.
514
for(i=0; i<p->nAction+p->mnLookahead; i++){
515
if( p->aAction[i].lookahead<0 ){
516
for(j=0; j<p->nLookahead; j++){
517
k = p->aLookahead[j].lookahead - p->mnLookahead + i;
519
if( p->aAction[k].lookahead>=0 ) break;
521
if( j<p->nLookahead ) continue;
522
for(j=0; j<p->nAction; j++){
523
if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
526
break; /* Fits in empty slots */
528
}else if( p->aAction[i].lookahead==p->mnLookahead ){
529
if( p->aAction[i].action!=p->mnAction ) continue;
530
for(j=0; j<p->nLookahead; j++){
531
k = p->aLookahead[j].lookahead - p->mnLookahead + i;
532
if( k<0 || k>=p->nAction ) break;
533
if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
534
if( p->aLookahead[j].action!=p->aAction[k].action ) break;
536
if( j<p->nLookahead ) continue;
538
for(j=0; j<p->nAction; j++){
539
if( p->aAction[j].lookahead<0 ) continue;
540
if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
542
if( n==p->nLookahead ){
543
break; /* Same as a prior transaction set */
547
/* Insert transaction set at index i. */
548
for(j=0; j<p->nLookahead; j++){
549
k = p->aLookahead[j].lookahead - p->mnLookahead + i;
550
p->aAction[k] = p->aLookahead[j];
551
if( k>=p->nAction ) p->nAction = k+1;
555
/* Return the offset that is added to the lookahead in order to get the
556
** index into yy_action of the action */
557
return i - p->mnLookahead;
560
/********************** From the file "assert.c" ****************************/
562
** A more efficient way of handling assertions.
564
void myassert(file,line)
568
fprintf(stderr,"Assertion failed on line %d of file \"%s\"\n",line,file);
571
/********************** From the file "build.c" *****************************/
573
** Routines to construction the finite state machine for the LEMON
577
/* Find a precedence symbol of every rule in the grammar.
579
** Those rules which have a precedence symbol coded in the input
580
** grammar using the "[symbol]" construct will already have the
581
** rp->precsym field filled. Other rules take as their precedence
582
** symbol the first RHS symbol with a defined precedence. If there
583
** are not RHS symbols with a defined precedence, the precedence
584
** symbol field is left blank.
586
void FindRulePrecedences(xp)
590
for(rp=xp->rule; rp; rp=rp->next){
591
if( rp->precsym==0 ){
593
for(i=0; i<rp->nrhs && rp->precsym==0; i++){
594
struct symbol *sp = rp->rhs[i];
595
if( sp->type==MULTITERMINAL ){
596
for(j=0; j<sp->nsubsym; j++){
597
if( sp->subsym[j]->prec>=0 ){
598
rp->precsym = sp->subsym[j];
602
}else if( sp->prec>=0 ){
603
rp->precsym = rp->rhs[i];
611
/* Find all nonterminals which will generate the empty string.
612
** Then go back and compute the first sets of every nonterminal.
613
** The first set is the set of all terminal symbols which can begin
614
** a string generated by that nonterminal.
616
void FindFirstSets(lemp)
623
for(i=0; i<lemp->nsymbol; i++){
624
lemp->symbols[i]->lambda = B_FALSE;
626
for(i=lemp->nterminal; i<lemp->nsymbol; i++){
627
lemp->symbols[i]->firstset = SetNew();
630
/* First compute all lambdas */
633
for(rp=lemp->rule; rp; rp=rp->next){
634
if( rp->lhs->lambda ) continue;
635
for(i=0; i<rp->nrhs; i++){
636
struct symbol *sp = rp->rhs[i];
637
if( sp->type!=TERMINAL || sp->lambda==B_FALSE ) break;
640
rp->lhs->lambda = B_TRUE;
646
/* Now compute all first sets */
648
struct symbol *s1, *s2;
650
for(rp=lemp->rule; rp; rp=rp->next){
652
for(i=0; i<rp->nrhs; i++){
654
if( s2->type==TERMINAL ){
655
progress += SetAdd(s1->firstset,s2->index);
657
}else if( s2->type==MULTITERMINAL ){
658
for(j=0; j<s2->nsubsym; j++){
659
progress += SetAdd(s1->firstset,s2->subsym[j]->index);
663
if( s1->lambda==B_FALSE ) break;
665
progress += SetUnion(s1->firstset,s2->firstset);
666
if( s2->lambda==B_FALSE ) break;
674
/* Compute all LR(0) states for the grammar. Links
675
** are added to between some states so that the LR(1) follow sets
676
** can be computed later.
678
PRIVATE struct state *getstate(/* struct lemon * */); /* forward reference */
679
void FindStates(lemp)
687
/* Find the start symbol */
689
sp = Symbol_find(lemp->start);
691
ErrorMsg(lemp->filename,0,
692
"The specified start symbol \"%s\" is not \
693
in a nonterminal of the grammar. \"%s\" will be used as the start \
694
symbol instead.",lemp->start,lemp->rule->lhs->name);
696
sp = lemp->rule->lhs;
699
sp = lemp->rule->lhs;
702
/* Make sure the start symbol doesn't occur on the right-hand side of
703
** any rule. Report an error if it does. (YACC would generate a new
704
** start symbol in this case.) */
705
for(rp=lemp->rule; rp; rp=rp->next){
707
for(i=0; i<rp->nrhs; i++){
708
if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */
709
ErrorMsg(lemp->filename,0,
710
"The start symbol \"%s\" occurs on the \
711
right-hand side of a rule. This will result in a parser which \
712
does not work properly.",sp->name);
718
/* The basis configuration set for the first state
719
** is all rules which have the start symbol as their
721
for(rp=sp->rule; rp; rp=rp->nextlhs){
722
struct config *newcfp;
723
newcfp = Configlist_addbasis(rp,0);
724
SetAdd(newcfp->fws,0);
727
/* Compute the first state. All other states will be
728
** computed automatically during the computation of the first one.
729
** The returned pointer to the first state is not used. */
730
(void)getstate(lemp);
734
/* Return a pointer to a state which is described by the configuration
735
** list which has been built from calls to Configlist_add.
737
PRIVATE void buildshifts(/* struct lemon *, struct state * */); /* Forwd ref */
738
PRIVATE struct state *getstate(lemp)
741
struct config *cfp, *bp;
744
/* Extract the sorted basis of the new state. The basis was constructed
745
** by prior calls to "Configlist_addbasis()". */
746
Configlist_sortbasis();
747
bp = Configlist_basis();
749
/* Get a state with the same basis */
750
stp = State_find(bp);
752
/* A state with the same basis already exists! Copy all the follow-set
753
** propagation links from the state under construction into the
754
** preexisting state, then return a pointer to the preexisting state */
755
struct config *x, *y;
756
for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
757
Plink_copy(&y->bplp,x->bplp);
758
Plink_delete(x->fplp);
759
x->fplp = x->bplp = 0;
761
cfp = Configlist_return();
764
/* This really is a new state. Construct all the details */
765
Configlist_closure(lemp); /* Compute the configuration closure */
766
Configlist_sort(); /* Sort the configuration closure */
767
cfp = Configlist_return(); /* Get a pointer to the config list */
768
stp = State_new(); /* A new state structure */
770
stp->bp = bp; /* Remember the configuration basis */
771
stp->cfp = cfp; /* Remember the configuration closure */
772
stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
773
stp->ap = 0; /* No actions, yet. */
774
State_insert(stp,stp->bp); /* Add to the state table */
775
buildshifts(lemp,stp); /* Recursively compute successor states */
781
** Return true if two symbols are the same.
789
if( a->type!=MULTITERMINAL ) return 0;
790
if( b->type!=MULTITERMINAL ) return 0;
791
if( a->nsubsym!=b->nsubsym ) return 0;
792
for(i=0; i<a->nsubsym; i++){
793
if( a->subsym[i]!=b->subsym[i] ) return 0;
798
/* Construct all successor states to the given state. A "successor"
799
** state is any state which can be reached by a shift action.
801
PRIVATE void buildshifts(lemp,stp)
803
struct state *stp; /* The state from which successors are computed */
805
struct config *cfp; /* For looping thru the config closure of "stp" */
806
struct config *bcfp; /* For the inner loop on config closure of "stp" */
807
struct config *new; /* */
808
struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
809
struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
810
struct state *newstp; /* A pointer to a successor state */
812
/* Each configuration becomes complete after it contibutes to a successor
813
** state. Initially, all configurations are incomplete */
814
for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
816
/* Loop through all configurations of the state "stp" */
817
for(cfp=stp->cfp; cfp; cfp=cfp->next){
818
if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
819
if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
820
Configlist_reset(); /* Reset the new config set */
821
sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
823
/* For every configuration in the state "stp" which has the symbol "sp"
824
** following its dot, add the same configuration to the basis set under
825
** construction but with the dot shifted one symbol to the right. */
826
for(bcfp=cfp; bcfp; bcfp=bcfp->next){
827
if( bcfp->status==COMPLETE ) continue; /* Already used */
828
if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
829
bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
830
if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
831
bcfp->status = COMPLETE; /* Mark this config as used */
832
new = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
833
Plink_add(&new->bplp,bcfp);
836
/* Get a pointer to the state described by the basis configuration set
837
** constructed in the preceding loop */
838
newstp = getstate(lemp);
840
/* The state "newstp" is reached from the state "stp" by a shift action
841
** on the symbol "sp" */
842
if( sp->type==MULTITERMINAL ){
844
for(i=0; i<sp->nsubsym; i++){
845
Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
848
Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
854
** Construct the propagation links
860
struct config *cfp, *other;
864
/* Housekeeping detail:
865
** Add to every propagate link a pointer back to the state to
866
** which the link is attached. */
867
for(i=0; i<lemp->nstate; i++){
868
stp = lemp->sorted[i];
869
for(cfp=stp->cfp; cfp; cfp=cfp->next){
874
/* Convert all backlinks into forward links. Only the forward
875
** links are used in the follow-set computation. */
876
for(i=0; i<lemp->nstate; i++){
877
stp = lemp->sorted[i];
878
for(cfp=stp->cfp; cfp; cfp=cfp->next){
879
for(plp=cfp->bplp; plp; plp=plp->next){
881
Plink_add(&other->fplp,cfp);
887
/* Compute all followsets.
889
** A followset is the set of all symbols which can come immediately
890
** after a configuration.
892
void FindFollowSets(lemp)
901
for(i=0; i<lemp->nstate; i++){
902
for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
903
cfp->status = INCOMPLETE;
909
for(i=0; i<lemp->nstate; i++){
910
for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
911
if( cfp->status==COMPLETE ) continue;
912
for(plp=cfp->fplp; plp; plp=plp->next){
913
change = SetUnion(plp->cfp->fws,cfp->fws);
915
plp->cfp->status = INCOMPLETE;
919
cfp->status = COMPLETE;
925
static int resolve_conflict();
927
/* Compute the reduce actions, and resolve conflicts.
929
void FindActions(lemp)
938
/* Add all of the reduce actions
939
** A reduce action is added for each element of the followset of
940
** a configuration which has its dot at the extreme right.
942
for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
943
stp = lemp->sorted[i];
944
for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
945
if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
946
for(j=0; j<lemp->nterminal; j++){
947
if( SetFind(cfp->fws,j) ){
948
/* Add a reduce action to the state "stp" which will reduce by the
949
** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
950
Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
957
/* Add the accepting token */
959
sp = Symbol_find(lemp->start);
960
if( sp==0 ) sp = lemp->rule->lhs;
962
sp = lemp->rule->lhs;
964
/* Add to the first state (which is always the starting state of the
965
** finite state machine) an action to ACCEPT if the lookahead is the
966
** start nonterminal. */
967
Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
969
/* Resolve conflicts */
970
for(i=0; i<lemp->nstate; i++){
971
struct action *ap, *nap;
973
stp = lemp->sorted[i];
975
stp->ap = Action_sort(stp->ap);
976
for(ap=stp->ap; ap && ap->next; ap=ap->next){
977
for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
978
/* The two actions "ap" and "nap" have the same lookahead.
979
** Figure out which one should be used */
980
lemp->nconflict += resolve_conflict(ap,nap,lemp->errsym);
985
/* Report an error for each rule that can never be reduced. */
986
for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = B_FALSE;
987
for(i=0; i<lemp->nstate; i++){
989
for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
990
if( ap->type==REDUCE ) ap->x.rp->canReduce = B_TRUE;
993
for(rp=lemp->rule; rp; rp=rp->next){
994
if( rp->canReduce ) continue;
995
ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
1000
/* Resolve a conflict between the two given actions. If the
1001
** conflict can't be resolve, return non-zero.
1004
** To resolve a conflict, first look to see if either action
1005
** is on an error rule. In that case, take the action which
1006
** is not associated with the error rule. If neither or both
1007
** actions are associated with an error rule, then try to
1008
** use precedence to resolve the conflict.
1010
** If either action is a SHIFT, then it must be apx. This
1011
** function won't work if apx->type==REDUCE and apy->type==SHIFT.
1013
static int resolve_conflict(apx,apy,errsym)
1016
struct symbol *errsym; /* The error symbol (if defined. NULL otherwise) */
1018
struct symbol *spx, *spy;
1020
assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
1021
if( apx->type==SHIFT && apy->type==REDUCE ){
1023
spy = apy->x.rp->precsym;
1024
if( spy==0 || spx->prec<0 || spy->prec<0 ){
1025
/* Not enough precedence information. */
1026
apy->type = CONFLICT;
1028
}else if( spx->prec>spy->prec ){ /* Lower precedence wins */
1029
apy->type = RD_RESOLVED;
1030
}else if( spx->prec<spy->prec ){
1031
apx->type = SH_RESOLVED;
1032
}else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
1033
apy->type = RD_RESOLVED; /* associativity */
1034
}else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
1035
apx->type = SH_RESOLVED;
1037
assert( spx->prec==spy->prec && spx->assoc==NONE );
1038
apy->type = CONFLICT;
1041
}else if( apx->type==REDUCE && apy->type==REDUCE ){
1042
spx = apx->x.rp->precsym;
1043
spy = apy->x.rp->precsym;
1044
if( spx==0 || spy==0 || spx->prec<0 ||
1045
spy->prec<0 || spx->prec==spy->prec ){
1046
apy->type = CONFLICT;
1048
}else if( spx->prec>spy->prec ){
1049
apy->type = RD_RESOLVED;
1050
}else if( spx->prec<spy->prec ){
1051
apx->type = RD_RESOLVED;
1055
apx->type==SH_RESOLVED ||
1056
apx->type==RD_RESOLVED ||
1057
apx->type==CONFLICT ||
1058
apy->type==SH_RESOLVED ||
1059
apy->type==RD_RESOLVED ||
1062
/* The REDUCE/SHIFT case cannot happen because SHIFTs come before
1063
** REDUCEs on the list. If we reach this point it must be because
1064
** the parser conflict had already been resolved. */
1068
/********************* From the file "configlist.c" *************************/
1070
** Routines to processing a configuration list and building a state
1071
** in the LEMON parser generator.
1074
static struct config *freelist = 0; /* List of free configurations */
1075
static struct config *current = 0; /* Top of list of configurations */
1076
static struct config **currentend = 0; /* Last on list of configs */
1077
static struct config *basis = 0; /* Top of list of basis configs */
1078
static struct config **basisend = 0; /* End of list of basis configs */
1080
/* Return a pointer to a new configuration */
1081
PRIVATE struct config *newconfig(){
1086
freelist = (struct config *)malloc( sizeof(struct config)*amt );
1088
fprintf(stderr,"Unable to allocate memory for a new configuration.");
1091
for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
1092
freelist[amt-1].next = 0;
1095
freelist = freelist->next;
1099
/* The configuration "old" is no longer used */
1100
PRIVATE void deleteconfig(old)
1103
old->next = freelist;
1107
/* Initialized the configuration list builder */
1108
void Configlist_init(){
1110
currentend = ¤t;
1117
/* Initialized the configuration list builder */
1118
void Configlist_reset(){
1120
currentend = ¤t;
1123
Configtable_clear(0);
1127
/* Add another configuration to the configuration list */
1128
struct config *Configlist_add(rp,dot)
1129
struct rule *rp; /* The rule */
1130
int dot; /* Index into the RHS of the rule where the dot goes */
1132
struct config *cfp, model;
1134
assert( currentend!=0 );
1137
cfp = Configtable_find(&model);
1142
cfp->fws = SetNew();
1144
cfp->fplp = cfp->bplp = 0;
1148
currentend = &cfp->next;
1149
Configtable_insert(cfp);
1154
/* Add a basis configuration to the configuration list */
1155
struct config *Configlist_addbasis(rp,dot)
1159
struct config *cfp, model;
1161
assert( basisend!=0 );
1162
assert( currentend!=0 );
1165
cfp = Configtable_find(&model);
1170
cfp->fws = SetNew();
1172
cfp->fplp = cfp->bplp = 0;
1176
currentend = &cfp->next;
1178
basisend = &cfp->bp;
1179
Configtable_insert(cfp);
1184
/* Compute the closure of the configuration list */
1185
void Configlist_closure(lemp)
1188
struct config *cfp, *newcfp;
1189
struct rule *rp, *newrp;
1190
struct symbol *sp, *xsp;
1193
assert( currentend!=0 );
1194
for(cfp=current; cfp; cfp=cfp->next){
1197
if( dot>=rp->nrhs ) continue;
1199
if( sp->type==NONTERMINAL ){
1200
if( sp->rule==0 && sp!=lemp->errsym ){
1201
ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
1205
for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
1206
newcfp = Configlist_add(newrp,0);
1207
for(i=dot+1; i<rp->nrhs; i++){
1209
if( xsp->type==TERMINAL ){
1210
SetAdd(newcfp->fws,xsp->index);
1212
}else if( xsp->type==MULTITERMINAL ){
1214
for(k=0; k<xsp->nsubsym; k++){
1215
SetAdd(newcfp->fws, xsp->subsym[k]->index);
1219
SetUnion(newcfp->fws,xsp->firstset);
1220
if( xsp->lambda==B_FALSE ) break;
1223
if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
1230
/* Sort the configuration list */
1231
void Configlist_sort(){
1232
current = (struct config *)msort((char *)current,(char **)&(current->next),Configcmp);
1237
/* Sort the basis configuration list */
1238
void Configlist_sortbasis(){
1239
basis = (struct config *)msort((char *)current,(char **)&(current->bp),Configcmp);
1244
/* Return a pointer to the head of the configuration list and
1245
** reset the list */
1246
struct config *Configlist_return(){
1254
/* Return a pointer to the head of the configuration list and
1255
** reset the list */
1256
struct config *Configlist_basis(){
1264
/* Free all elements of the given configuration list */
1265
void Configlist_eat(cfp)
1268
struct config *nextcfp;
1269
for(; cfp; cfp=nextcfp){
1270
nextcfp = cfp->next;
1271
assert( cfp->fplp==0 );
1272
assert( cfp->bplp==0 );
1273
if( cfp->fws ) SetFree(cfp->fws);
1278
/***************** From the file "error.c" *********************************/
1280
** Code for printing error message.
1283
/* Find a good place to break "msg" so that its length is at least "min"
1284
** but no more than "max". Make the point as close to max as possible.
1286
static int findbreak(msg,min,max)
1293
for(i=spot=min; i<=max; i++){
1295
if( c=='\t' ) msg[i] = ' ';
1296
if( c=='\n' ){ msg[i] = ' '; spot = i; break; }
1297
if( c==0 ){ spot = i; break; }
1298
if( c=='-' && i<max-1 ) spot = i+1;
1299
if( c==' ' ) spot = i;
1305
** The error message is split across multiple lines if necessary. The
1306
** splits occur at a space, if there is a space available near the end
1309
#define ERRMSGSIZE 10000 /* Hope this is big enough. No way to error check */
1310
#define LINEWIDTH 79 /* Max width of any output line */
1311
#define PREFIXLIMIT 30 /* Max width of the prefix on each line */
1312
void ErrorMsg(const char *filename, int lineno, const char *format, ...){
1313
char errmsg[ERRMSGSIZE];
1314
char prefix[PREFIXLIMIT+10];
1319
int end, restart, base;
1321
va_start(ap, format);
1322
/* Prepare a prefix to be prepended to every output line */
1324
sprintf(prefix,"%.*s:%d: ",PREFIXLIMIT-10,filename,lineno);
1326
sprintf(prefix,"%.*s: ",PREFIXLIMIT-10,filename);
1328
prefixsize = strlen(prefix);
1329
availablewidth = LINEWIDTH - prefixsize;
1331
/* Generate the error message */
1332
vsprintf(errmsg,format,ap);
1334
errmsgsize = strlen(errmsg);
1335
/* Remove trailing '\n's from the error message. */
1336
while( errmsgsize>0 && errmsg[errmsgsize-1]=='\n' ){
1337
errmsg[--errmsgsize] = 0;
1340
/* Print the error message */
1342
while( errmsg[base]!=0 ){
1343
end = restart = findbreak(&errmsg[base],0,availablewidth);
1345
while( errmsg[restart]==' ' ) restart++;
1346
fprintf(stdout,"%s%.*s\n",prefix,end,&errmsg[base]);
1350
/**************** From the file "main.c" ************************************/
1352
** Main program file for the LEMON parser generator.
1355
/* Report an out-of-memory condition and abort. This function
1356
** is used mostly by the "MemoryCheck" macro in struct.h
1358
void memory_error(){
1359
fprintf(stderr,"Out of memory. Aborting...\n");
1363
static int nDefine = 0; /* Number of -D options on the command line */
1364
static char **azDefine = 0; /* Name of the -D macros */
1366
/* This routine is called with the argument to each -D command-line option.
1367
** Add the macro defined to the azDefine array.
1369
static void handle_D_option(char *z){
1372
azDefine = realloc(azDefine, sizeof(azDefine[0])*nDefine);
1374
fprintf(stderr,"out of memory\n");
1377
paz = &azDefine[nDefine-1];
1378
*paz = malloc( strlen(z)+1 );
1380
fprintf(stderr,"out of memory\n");
1384
for(z=*paz; *z && *z!='='; z++){}
1389
/* The main program. Parse the command line and do it... */
1394
static int version = 0;
1395
static int rpflag = 0;
1396
static int basisflag = 0;
1397
static int compress = 0;
1398
static int quiet = 0;
1399
static int statistics = 0;
1400
static int mhflag = 0;
1401
static struct s_options options[] = {
1402
{OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1403
{OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
1404
{OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
1405
{OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
1406
{OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file"},
1407
{OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
1408
{OPT_FLAG, "s", (char*)&statistics,
1409
"Print parser stats to standard output."},
1410
{OPT_FLAG, "x", (char*)&version, "Print the version number."},
1416
OptInit(argv,options,stderr);
1418
printf("Lemon version 1.0\n");
1421
if( OptNArgs()!=1 ){
1422
fprintf(stderr,"Exactly one filename argument is required.\n");
1427
/* Initialize the machine */
1431
lem.argv0 = argv[0];
1432
lem.filename = OptArg(0);
1433
lem.basisflag = basisflag;
1434
lem.has_fallback = 0;
1436
lem.name = lem.include = lem.arg = lem.tokentype = lem.start = 0;
1439
lem.error = lem.overflow = lem.failure = lem.accept = lem.tokendest =
1440
lem.tokenprefix = lem.outname = lem.extracode = 0;
1444
lem.errsym = Symbol_new("error");
1446
/* Parse the input file */
1448
if( lem.errorcnt ) exit(lem.errorcnt);
1450
fprintf(stderr,"Empty grammar.\n");
1454
/* Count and index the symbols of the grammar */
1455
lem.nsymbol = Symbol_count();
1456
Symbol_new("{default}");
1457
lem.symbols = Symbol_arrayof();
1458
for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
1459
qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*),
1460
(int(*)())Symbolcmpp);
1461
for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
1462
for(i=1; isupper(lem.symbols[i]->name[0]); i++);
1465
/* Generate a reprint of the grammar, if requested on the command line */
1469
/* Initialize the size for all follow and first sets */
1470
SetSize(lem.nterminal);
1472
/* Find the precedence for every production rule (that has one) */
1473
FindRulePrecedences(&lem);
1475
/* Compute the lambda-nonterminals and the first-sets for every
1477
FindFirstSets(&lem);
1479
/* Compute all LR(0) states. Also record follow-set propagation
1480
** links so that the follow-set can be computed later */
1483
lem.sorted = State_arrayof();
1485
/* Tie up loose ends on the propagation links */
1488
/* Compute the follow set of every reducible configuration */
1489
FindFollowSets(&lem);
1491
/* Compute the action tables */
1494
/* Compress the action tables */
1495
if( compress==0 ) CompressTables(&lem);
1497
/* Reorder and renumber the states so that states with fewer choices
1498
** occur at the end. */
1501
/* Generate a report of the parser generated. (the "y.output" file) */
1502
if( !quiet ) ReportOutput(&lem);
1504
/* Generate the source code for the parser */
1505
ReportTable(&lem, mhflag);
1507
/* Produce a header file for use by the scanner. (This step is
1508
** omitted if the "-m" option is used because makeheaders will
1509
** generate the file for us.) */
1510
if( !mhflag ) ReportHeader(&lem);
1513
printf("Parser statistics: %d terminals, %d nonterminals, %d rules\n",
1514
lem.nterminal, lem.nsymbol - lem.nterminal, lem.nrule);
1515
printf(" %d states, %d parser table entries, %d conflicts\n",
1516
lem.nstate, lem.tablesize, lem.nconflict);
1518
if( lem.nconflict ){
1519
fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
1521
exit(lem.errorcnt + lem.nconflict);
1522
return (lem.errorcnt + lem.nconflict);
1524
/******************** From the file "msort.c" *******************************/
1526
** A generic merge-sort program.
1529
** Let "ptr" be a pointer to some structure which is at the head of
1530
** a null-terminated list. Then to sort the list call:
1532
** ptr = msort(ptr,&(ptr->next),cmpfnc);
1534
** In the above, "cmpfnc" is a pointer to a function which compares
1535
** two instances of the structure and returns an integer, as in
1536
** strcmp. The second argument is a pointer to the pointer to the
1537
** second element of the linked list. This address is used to compute
1538
** the offset to the "next" field within the structure. The offset to
1539
** the "next" field must be constant for all structures in the list.
1541
** The function returns a new pointer which is the head of the list
1549
** Return a pointer to the next structure in the linked list.
1551
#define NEXT(A) (*(char**)(((unsigned long)A)+offset))
1555
** a: A sorted, null-terminated linked list. (May be null).
1556
** b: A sorted, null-terminated linked list. (May be null).
1557
** cmp: A pointer to the comparison function.
1558
** offset: Offset in the structure to the "next" field.
1561
** A pointer to the head of a sorted list containing the elements
1565
** The "next" pointers for elements in the lists a and b are
1568
static char *merge(a,b,cmp,offset)
1581
if( (*cmp)(a,b)<0 ){
1590
if( (*cmp)(a,b)<0 ){
1600
if( a ) NEXT(ptr) = a;
1608
** list: Pointer to a singly-linked list of structures.
1609
** next: Pointer to pointer to the second element of the list.
1610
** cmp: A comparison function.
1613
** A pointer to the head of a sorted list containing the elements
1614
** orginally in list.
1617
** The "next" pointers for elements in list are changed.
1620
char *msort(list,next,cmp)
1625
unsigned long offset;
1627
char *set[LISTSIZE];
1629
offset = (unsigned long)next - (unsigned long)list;
1630
for(i=0; i<LISTSIZE; i++) set[i] = 0;
1635
for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1636
ep = merge(ep,set[i],cmp,offset);
1642
for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(ep,set[i],cmp,offset);
1645
/************************ From the file "option.c" **************************/
1647
static struct s_options *op;
1648
static FILE *errstream;
1650
#define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1653
** Print the command line with a carrot pointing to the k-th character
1654
** of the n-th field.
1656
static void errline(n,k,err)
1662
if( argv[0] ) fprintf(err,"%s",argv[0]);
1663
spcnt = strlen(argv[0]) + 1;
1664
for(i=1; i<n && argv[i]; i++){
1665
fprintf(err," %s",argv[i]);
1666
spcnt += strlen(argv[i])+1;
1669
for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1671
fprintf(err,"\n%*s^-- here\n",spcnt,"");
1673
fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1678
** Return the index of the N-th non-switch argument. Return -1
1679
** if N is out of range.
1681
static int argindex(n)
1686
if( argv!=0 && *argv!=0 ){
1687
for(i=1; argv[i]; i++){
1688
if( dashdash || !ISOPT(argv[i]) ){
1689
if( n==0 ) return i;
1692
if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1698
static char emsg[] = "Command line syntax error: ";
1701
** Process a flag command line argument.
1703
static int handleflags(i,err)
1710
for(j=0; op[j].label; j++){
1711
if( strncmp(&argv[i][1],op[j].label,strlen(op[j].label))==0 ) break;
1713
v = argv[i][0]=='-' ? 1 : 0;
1714
if( op[j].label==0 ){
1716
fprintf(err,"%sundefined option.\n",emsg);
1720
}else if( op[j].type==OPT_FLAG ){
1721
*((int*)op[j].arg) = v;
1722
}else if( op[j].type==OPT_FFLAG ){
1723
(*(void(*)())(op[j].arg))(v);
1724
}else if( op[j].type==OPT_FSTR ){
1725
(*(void(*)())(op[j].arg))(&argv[i][2]);
1728
fprintf(err,"%smissing argument on switch.\n",emsg);
1737
** Process a command line switch which has an argument.
1739
static int handleswitch(i,err)
1749
cp = strchr(argv[i],'=');
1752
for(j=0; op[j].label; j++){
1753
if( strcmp(argv[i],op[j].label)==0 ) break;
1756
if( op[j].label==0 ){
1758
fprintf(err,"%sundefined option.\n",emsg);
1764
switch( op[j].type ){
1768
fprintf(err,"%soption requires an argument.\n",emsg);
1775
dv = strtod(cp,&end);
1778
fprintf(err,"%sillegal character in floating-point argument.\n",emsg);
1779
errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
1786
lv = strtol(cp,&end,0);
1789
fprintf(err,"%sillegal character in integer argument.\n",emsg);
1790
errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
1800
switch( op[j].type ){
1805
*(double*)(op[j].arg) = dv;
1808
(*(void(*)())(op[j].arg))(dv);
1811
*(int*)(op[j].arg) = lv;
1814
(*(void(*)())(op[j].arg))((int)lv);
1817
*(char**)(op[j].arg) = sv;
1820
(*(void(*)())(op[j].arg))(sv);
1827
int OptInit(a,o,err)
1829
struct s_options *o;
1836
if( argv && *argv && op ){
1838
for(i=1; argv[i]; i++){
1839
if( argv[i][0]=='+' || argv[i][0]=='-' ){
1840
errcnt += handleflags(i,err);
1841
}else if( strchr(argv[i],'=') ){
1842
errcnt += handleswitch(i,err);
1847
fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
1858
if( argv!=0 && argv[0]!=0 ){
1859
for(i=1; argv[i]; i++){
1860
if( dashdash || !ISOPT(argv[i]) ) cnt++;
1861
if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1872
return i>=0 ? argv[i] : 0;
1880
if( i>=0 ) errline(i,0,errstream);
1887
for(i=0; op[i].label; i++){
1888
len = strlen(op[i].label) + 1;
1889
switch( op[i].type ){
1895
len += 9; /* length of "<integer>" */
1899
len += 6; /* length of "<real>" */
1903
len += 8; /* length of "<string>" */
1906
if( len>max ) max = len;
1908
for(i=0; op[i].label; i++){
1909
switch( op[i].type ){
1912
fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
1916
fprintf(errstream," %s=<integer>%*s %s\n",op[i].label,
1917
(int)(max-strlen(op[i].label)-9),"",op[i].message);
1921
fprintf(errstream," %s=<real>%*s %s\n",op[i].label,
1922
(int)(max-strlen(op[i].label)-6),"",op[i].message);
1926
fprintf(errstream," %s=<string>%*s %s\n",op[i].label,
1927
(int)(max-strlen(op[i].label)-8),"",op[i].message);
1932
/*********************** From the file "parse.c" ****************************/
1934
** Input file parser for the LEMON parser generator.
1937
/* The state of the parser */
1939
char *filename; /* Name of the input file */
1940
int tokenlineno; /* Linenumber at which current token starts */
1941
int errorcnt; /* Number of errors so far */
1942
char *tokenstart; /* Text of current token */
1943
struct lemon *gp; /* Global state vector */
1946
WAITING_FOR_DECL_OR_RULE,
1947
WAITING_FOR_DECL_KEYWORD,
1948
WAITING_FOR_DECL_ARG,
1949
WAITING_FOR_PRECEDENCE_SYMBOL,
1959
RESYNC_AFTER_RULE_ERROR,
1960
RESYNC_AFTER_DECL_ERROR,
1961
WAITING_FOR_DESTRUCTOR_SYMBOL,
1962
WAITING_FOR_DATATYPE_SYMBOL,
1963
WAITING_FOR_FALLBACK_ID
1964
} state; /* The state of the parser */
1965
struct symbol *fallback; /* The fallback token */
1966
struct symbol *lhs; /* Left-hand side of current rule */
1967
char *lhsalias; /* Alias for the LHS */
1968
int nrhs; /* Number of right-hand side symbols seen */
1969
struct symbol *rhs[MAXRHS]; /* RHS symbols */
1970
char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
1971
struct rule *prevrule; /* Previous rule parsed */
1972
char *declkeyword; /* Keyword of a declaration */
1973
char **declargslot; /* Where the declaration argument should be put */
1974
int *decllnslot; /* Where the declaration linenumber is put */
1975
enum e_assoc declassoc; /* Assign this association to decl arguments */
1976
int preccounter; /* Assign this precedence to decl arguments */
1977
struct rule *firstrule; /* Pointer to first rule in the grammar */
1978
struct rule *lastrule; /* Pointer to the most recently parsed rule */
1981
/* Parse a single token */
1982
static void parseonetoken(psp)
1986
x = Strsafe(psp->tokenstart); /* Save the token permanently */
1988
printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
1991
switch( psp->state ){
1994
psp->preccounter = 0;
1995
psp->firstrule = psp->lastrule = 0;
1997
/* Fall thru to next case */
1998
case WAITING_FOR_DECL_OR_RULE:
2000
psp->state = WAITING_FOR_DECL_KEYWORD;
2001
}else if( islower(x[0]) ){
2002
psp->lhs = Symbol_new(x);
2005
psp->state = WAITING_FOR_ARROW;
2006
}else if( x[0]=='{' ){
2007
if( psp->prevrule==0 ){
2008
ErrorMsg(psp->filename,psp->tokenlineno,
2009
"There is not prior rule opon which to attach the code \
2010
fragment which begins on this line.");
2012
}else if( psp->prevrule->code!=0 ){
2013
ErrorMsg(psp->filename,psp->tokenlineno,
2014
"Code fragment beginning on this line is not the first \
2015
to follow the previous rule.");
2018
psp->prevrule->line = psp->tokenlineno;
2019
psp->prevrule->code = &x[1];
2021
}else if( x[0]=='[' ){
2022
psp->state = PRECEDENCE_MARK_1;
2024
ErrorMsg(psp->filename,psp->tokenlineno,
2025
"Token \"%s\" should be either \"%%\" or a nonterminal name.",
2030
case PRECEDENCE_MARK_1:
2031
if( !isupper(x[0]) ){
2032
ErrorMsg(psp->filename,psp->tokenlineno,
2033
"The precedence symbol must be a terminal.");
2035
}else if( psp->prevrule==0 ){
2036
ErrorMsg(psp->filename,psp->tokenlineno,
2037
"There is no prior rule to assign precedence \"[%s]\".",x);
2039
}else if( psp->prevrule->precsym!=0 ){
2040
ErrorMsg(psp->filename,psp->tokenlineno,
2041
"Precedence mark on this line is not the first \
2042
to follow the previous rule.");
2045
psp->prevrule->precsym = Symbol_new(x);
2047
psp->state = PRECEDENCE_MARK_2;
2049
case PRECEDENCE_MARK_2:
2051
ErrorMsg(psp->filename,psp->tokenlineno,
2052
"Missing \"]\" on precedence mark.");
2055
psp->state = WAITING_FOR_DECL_OR_RULE;
2057
case WAITING_FOR_ARROW:
2058
if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2059
psp->state = IN_RHS;
2060
}else if( x[0]=='(' ){
2061
psp->state = LHS_ALIAS_1;
2063
ErrorMsg(psp->filename,psp->tokenlineno,
2064
"Expected to see a \":\" following the LHS symbol \"%s\".",
2067
psp->state = RESYNC_AFTER_RULE_ERROR;
2071
if( isalpha(x[0]) ){
2073
psp->state = LHS_ALIAS_2;
2075
ErrorMsg(psp->filename,psp->tokenlineno,
2076
"\"%s\" is not a valid alias for the LHS \"%s\"\n",
2079
psp->state = RESYNC_AFTER_RULE_ERROR;
2084
psp->state = LHS_ALIAS_3;
2086
ErrorMsg(psp->filename,psp->tokenlineno,
2087
"Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2089
psp->state = RESYNC_AFTER_RULE_ERROR;
2093
if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2094
psp->state = IN_RHS;
2096
ErrorMsg(psp->filename,psp->tokenlineno,
2097
"Missing \"->\" following: \"%s(%s)\".",
2098
psp->lhs->name,psp->lhsalias);
2100
psp->state = RESYNC_AFTER_RULE_ERROR;
2106
rp = (struct rule *)malloc( sizeof(struct rule) +
2107
sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs );
2109
ErrorMsg(psp->filename,psp->tokenlineno,
2110
"Can't allocate enough memory for this rule.");
2115
rp->ruleline = psp->tokenlineno;
2116
rp->rhs = (struct symbol**)&rp[1];
2117
rp->rhsalias = (char**)&(rp->rhs[psp->nrhs]);
2118
for(i=0; i<psp->nrhs; i++){
2119
rp->rhs[i] = psp->rhs[i];
2120
rp->rhsalias[i] = psp->alias[i];
2123
rp->lhsalias = psp->lhsalias;
2124
rp->nrhs = psp->nrhs;
2127
rp->index = psp->gp->nrule++;
2128
rp->nextlhs = rp->lhs->rule;
2131
if( psp->firstrule==0 ){
2132
psp->firstrule = psp->lastrule = rp;
2134
psp->lastrule->next = rp;
2139
psp->state = WAITING_FOR_DECL_OR_RULE;
2140
}else if( isalpha(x[0]) ){
2141
if( psp->nrhs>=MAXRHS ){
2142
ErrorMsg(psp->filename,psp->tokenlineno,
2143
"Too many symbols on RHS or rule beginning at \"%s\".",
2146
psp->state = RESYNC_AFTER_RULE_ERROR;
2148
psp->rhs[psp->nrhs] = Symbol_new(x);
2149
psp->alias[psp->nrhs] = 0;
2152
}else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
2153
struct symbol *msp = psp->rhs[psp->nrhs-1];
2154
if( msp->type!=MULTITERMINAL ){
2155
struct symbol *origsp = msp;
2156
msp = malloc(sizeof(*msp));
2157
memset(msp, 0, sizeof(*msp));
2158
msp->type = MULTITERMINAL;
2160
msp->subsym = malloc(sizeof(struct symbol*));
2161
msp->subsym[0] = origsp;
2162
msp->name = origsp->name;
2163
psp->rhs[psp->nrhs-1] = msp;
2166
msp->subsym = realloc(msp->subsym, sizeof(struct symbol*)*msp->nsubsym);
2167
msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
2168
if( islower(x[1]) || islower(msp->subsym[0]->name[0]) ){
2169
ErrorMsg(psp->filename,psp->tokenlineno,
2170
"Cannot form a compound containing a non-terminal");
2173
}else if( x[0]=='(' && psp->nrhs>0 ){
2174
psp->state = RHS_ALIAS_1;
2176
ErrorMsg(psp->filename,psp->tokenlineno,
2177
"Illegal character on RHS of rule: \"%s\".",x);
2179
psp->state = RESYNC_AFTER_RULE_ERROR;
2183
if( isalpha(x[0]) ){
2184
psp->alias[psp->nrhs-1] = x;
2185
psp->state = RHS_ALIAS_2;
2187
ErrorMsg(psp->filename,psp->tokenlineno,
2188
"\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
2189
x,psp->rhs[psp->nrhs-1]->name);
2191
psp->state = RESYNC_AFTER_RULE_ERROR;
2196
psp->state = IN_RHS;
2198
ErrorMsg(psp->filename,psp->tokenlineno,
2199
"Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2201
psp->state = RESYNC_AFTER_RULE_ERROR;
2204
case WAITING_FOR_DECL_KEYWORD:
2205
if( isalpha(x[0]) ){
2206
psp->declkeyword = x;
2207
psp->declargslot = 0;
2208
psp->decllnslot = 0;
2209
psp->state = WAITING_FOR_DECL_ARG;
2210
if( strcmp(x,"name")==0 ){
2211
psp->declargslot = &(psp->gp->name);
2212
}else if( strcmp(x,"include")==0 ){
2213
psp->declargslot = &(psp->gp->include);
2214
psp->decllnslot = &psp->gp->includeln;
2215
}else if( strcmp(x,"code")==0 ){
2216
psp->declargslot = &(psp->gp->extracode);
2217
psp->decllnslot = &psp->gp->extracodeln;
2218
}else if( strcmp(x,"token_destructor")==0 ){
2219
psp->declargslot = &psp->gp->tokendest;
2220
psp->decllnslot = &psp->gp->tokendestln;
2221
}else if( strcmp(x,"default_destructor")==0 ){
2222
psp->declargslot = &psp->gp->vardest;
2223
psp->decllnslot = &psp->gp->vardestln;
2224
}else if( strcmp(x,"token_prefix")==0 ){
2225
psp->declargslot = &psp->gp->tokenprefix;
2226
}else if( strcmp(x,"syntax_error")==0 ){
2227
psp->declargslot = &(psp->gp->error);
2228
psp->decllnslot = &psp->gp->errorln;
2229
}else if( strcmp(x,"parse_accept")==0 ){
2230
psp->declargslot = &(psp->gp->accept);
2231
psp->decllnslot = &psp->gp->acceptln;
2232
}else if( strcmp(x,"parse_failure")==0 ){
2233
psp->declargslot = &(psp->gp->failure);
2234
psp->decllnslot = &psp->gp->failureln;
2235
}else if( strcmp(x,"stack_overflow")==0 ){
2236
psp->declargslot = &(psp->gp->overflow);
2237
psp->decllnslot = &psp->gp->overflowln;
2238
}else if( strcmp(x,"extra_argument")==0 ){
2239
psp->declargslot = &(psp->gp->arg);
2240
}else if( strcmp(x,"token_type")==0 ){
2241
psp->declargslot = &(psp->gp->tokentype);
2242
}else if( strcmp(x,"default_type")==0 ){
2243
psp->declargslot = &(psp->gp->vartype);
2244
}else if( strcmp(x,"stack_size")==0 ){
2245
psp->declargslot = &(psp->gp->stacksize);
2246
}else if( strcmp(x,"start_symbol")==0 ){
2247
psp->declargslot = &(psp->gp->start);
2248
}else if( strcmp(x,"left")==0 ){
2250
psp->declassoc = LEFT;
2251
psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2252
}else if( strcmp(x,"right")==0 ){
2254
psp->declassoc = RIGHT;
2255
psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2256
}else if( strcmp(x,"nonassoc")==0 ){
2258
psp->declassoc = NONE;
2259
psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2260
}else if( strcmp(x,"destructor")==0 ){
2261
psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
2262
}else if( strcmp(x,"type")==0 ){
2263
psp->state = WAITING_FOR_DATATYPE_SYMBOL;
2264
}else if( strcmp(x,"fallback")==0 ){
2266
psp->state = WAITING_FOR_FALLBACK_ID;
2268
ErrorMsg(psp->filename,psp->tokenlineno,
2269
"Unknown declaration keyword: \"%%%s\".",x);
2271
psp->state = RESYNC_AFTER_DECL_ERROR;
2274
ErrorMsg(psp->filename,psp->tokenlineno,
2275
"Illegal declaration keyword: \"%s\".",x);
2277
psp->state = RESYNC_AFTER_DECL_ERROR;
2280
case WAITING_FOR_DESTRUCTOR_SYMBOL:
2281
if( !isalpha(x[0]) ){
2282
ErrorMsg(psp->filename,psp->tokenlineno,
2283
"Symbol name missing after %destructor keyword");
2285
psp->state = RESYNC_AFTER_DECL_ERROR;
2287
struct symbol *sp = Symbol_new(x);
2288
psp->declargslot = &sp->destructor;
2289
psp->decllnslot = &sp->destructorln;
2290
psp->state = WAITING_FOR_DECL_ARG;
2293
case WAITING_FOR_DATATYPE_SYMBOL:
2294
if( !isalpha(x[0]) ){
2295
ErrorMsg(psp->filename,psp->tokenlineno,
2296
"Symbol name missing after %destructor keyword");
2298
psp->state = RESYNC_AFTER_DECL_ERROR;
2300
struct symbol *sp = Symbol_new(x);
2301
psp->declargslot = &sp->datatype;
2302
psp->decllnslot = 0;
2303
psp->state = WAITING_FOR_DECL_ARG;
2306
case WAITING_FOR_PRECEDENCE_SYMBOL:
2308
psp->state = WAITING_FOR_DECL_OR_RULE;
2309
}else if( isupper(x[0]) ){
2313
ErrorMsg(psp->filename,psp->tokenlineno,
2314
"Symbol \"%s\" has already be given a precedence.",x);
2317
sp->prec = psp->preccounter;
2318
sp->assoc = psp->declassoc;
2321
ErrorMsg(psp->filename,psp->tokenlineno,
2322
"Can't assign a precedence to \"%s\".",x);
2326
case WAITING_FOR_DECL_ARG:
2327
if( (x[0]=='{' || x[0]=='\"' || isalnum(x[0])) ){
2328
if( *(psp->declargslot)!=0 ){
2329
ErrorMsg(psp->filename,psp->tokenlineno,
2330
"The argument \"%s\" to declaration \"%%%s\" is not the first.",
2331
x[0]=='\"' ? &x[1] : x,psp->declkeyword);
2333
psp->state = RESYNC_AFTER_DECL_ERROR;
2335
*(psp->declargslot) = (x[0]=='\"' || x[0]=='{') ? &x[1] : x;
2336
if( psp->decllnslot ) *psp->decllnslot = psp->tokenlineno;
2337
psp->state = WAITING_FOR_DECL_OR_RULE;
2340
ErrorMsg(psp->filename,psp->tokenlineno,
2341
"Illegal argument to %%%s: %s",psp->declkeyword,x);
2343
psp->state = RESYNC_AFTER_DECL_ERROR;
2346
case WAITING_FOR_FALLBACK_ID:
2348
psp->state = WAITING_FOR_DECL_OR_RULE;
2349
}else if( !isupper(x[0]) ){
2350
ErrorMsg(psp->filename, psp->tokenlineno,
2351
"%%fallback argument \"%s\" should be a token", x);
2354
struct symbol *sp = Symbol_new(x);
2355
if( psp->fallback==0 ){
2357
}else if( sp->fallback ){
2358
ErrorMsg(psp->filename, psp->tokenlineno,
2359
"More than one fallback assigned to token %s", x);
2362
sp->fallback = psp->fallback;
2363
psp->gp->has_fallback = 1;
2367
case RESYNC_AFTER_RULE_ERROR:
2368
/* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2370
case RESYNC_AFTER_DECL_ERROR:
2371
if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2372
if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
2377
/* Run the proprocessor over the input file text. The global variables
2378
** azDefine[0] through azDefine[nDefine-1] contains the names of all defined
2379
** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and
2380
** comments them out. Text in between is also commented out as appropriate.
2382
static void preprocess_input(char *z){
2388
for(i=0; z[i]; i++){
2389
if( z[i]=='\n' ) lineno++;
2390
if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue;
2391
if( strncmp(&z[i],"%endif",6)==0 && isspace(z[i+6]) ){
2395
for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
2398
for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2399
}else if( (strncmp(&z[i],"%ifdef",6)==0 && isspace(z[i+6]))
2400
|| (strncmp(&z[i],"%ifndef",7)==0 && isspace(z[i+7])) ){
2404
for(j=i+7; isspace(z[j]); j++){}
2405
for(n=0; z[j+n] && !isspace(z[j+n]); n++){}
2407
for(k=0; k<nDefine; k++){
2408
if( strncmp(azDefine[k],&z[j],n)==0 && strlen(azDefine[k])==n ){
2413
if( z[i+3]=='n' ) exclude = !exclude;
2416
start_lineno = lineno;
2419
for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2423
fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
2428
/* In spite of its name, this function is really a scanner. It read
2429
** in the entire input file (all at once) then tokenizes it. Each
2430
** token is passed to the function "parseonetoken" which builds all
2431
** the appropriate data structures in the global state vector "gp".
2446
ps.filename = gp->filename;
2448
ps.state = INITIALIZE;
2450
/* Begin by reading the input file */
2451
fp = fopen(ps.filename,"rb");
2453
ErrorMsg(ps.filename,0,"Can't open this file for reading.");
2458
filesize = ftell(fp);
2460
filebuf = (char *)malloc( filesize+1 );
2462
ErrorMsg(ps.filename,0,"Can't allocate %d of memory to hold this file.",
2467
if( fread(filebuf,1,filesize,fp)!=filesize ){
2468
ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
2475
filebuf[filesize] = 0;
2477
/* Make an initial pass through the file to handle %ifdef and %ifndef */
2478
preprocess_input(filebuf);
2480
/* Now scan the text of the input file */
2482
for(cp=filebuf; (c= *cp)!=0; ){
2483
if( c=='\n' ) lineno++; /* Keep track of the line number */
2484
if( isspace(c) ){ cp++; continue; } /* Skip all white space */
2485
if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
2487
while( (c= *cp)!=0 && c!='\n' ) cp++;
2490
if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
2492
while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
2493
if( c=='\n' ) lineno++;
2499
ps.tokenstart = cp; /* Mark the beginning of the token */
2500
ps.tokenlineno = lineno; /* Linenumber on which token begins */
2501
if( c=='\"' ){ /* String literals */
2503
while( (c= *cp)!=0 && c!='\"' ){
2504
if( c=='\n' ) lineno++;
2508
ErrorMsg(ps.filename,startline,
2509
"String starting on this line is not terminated before the end of the file.");
2515
}else if( c=='{' ){ /* A block of C code */
2518
for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
2519
if( c=='\n' ) lineno++;
2520
else if( c=='{' ) level++;
2521
else if( c=='}' ) level--;
2522
else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
2526
while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
2527
if( c=='\n' ) lineno++;
2531
}else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
2533
while( (c= *cp)!=0 && c!='\n' ) cp++;
2535
}else if( c=='\'' || c=='\"' ){ /* String a character literals */
2536
int startchar, prevc;
2539
for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
2540
if( c=='\n' ) lineno++;
2541
if( prevc=='\\' ) prevc = 0;
2547
ErrorMsg(ps.filename,ps.tokenlineno,
2548
"C code starting on this line is not terminated before the end of the file.");
2554
}else if( isalnum(c) ){ /* Identifiers */
2555
while( (c= *cp)!=0 && (isalnum(c) || c=='_') ) cp++;
2557
}else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
2560
}else if( (c=='/' || c=='|') && isalpha(cp[1]) ){
2562
while( (c = *cp)!=0 && (isalnum(c) || c=='_') ) cp++;
2564
}else{ /* All other (one character) operators */
2569
*cp = 0; /* Null terminate the token */
2570
parseonetoken(&ps); /* Parse the token */
2571
*cp = c; /* Restore the buffer */
2574
free(filebuf); /* Release the buffer after parsing */
2575
gp->rule = ps.firstrule;
2576
gp->errorcnt = ps.errorcnt;
2578
/*************************** From the file "plink.c" *********************/
2580
** Routines processing configuration follow-set propagation links
2581
** in the LEMON parser generator.
2583
static struct plink *plink_freelist = 0;
2585
/* Allocate a new plink */
2586
struct plink *Plink_new(){
2589
if( plink_freelist==0 ){
2592
plink_freelist = (struct plink *)malloc( sizeof(struct plink)*amt );
2593
if( plink_freelist==0 ){
2595
"Unable to allocate memory for a new follow-set propagation link.\n");
2598
for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2599
plink_freelist[amt-1].next = 0;
2601
new = plink_freelist;
2602
plink_freelist = plink_freelist->next;
2606
/* Add a plink to a plink list */
2607
void Plink_add(plpp,cfp)
2608
struct plink **plpp;
2618
/* Transfer every plink on the list "from" to the list "to" */
2619
void Plink_copy(to,from)
2623
struct plink *nextpl;
2625
nextpl = from->next;
2632
/* Delete every plink on the list */
2633
void Plink_delete(plp)
2636
struct plink *nextpl;
2640
plp->next = plink_freelist;
2641
plink_freelist = plp;
2645
/*********************** From the file "report.c" **************************/
2647
** Procedures for generating reports and tables in the LEMON parser generator.
2650
/* Generate a filename with the given suffix. Space to hold the
2651
** name comes from malloc() and must be freed by the calling
2654
PRIVATE char *file_makename(lemp,suffix)
2661
name = malloc( strlen(lemp->filename) + strlen(suffix) + 5 );
2663
fprintf(stderr,"Can't allocate space for a filename.\n");
2666
strcpy(name,lemp->filename);
2667
cp = strrchr(name,'.');
2669
strcat(name,suffix);
2673
/* Open a file with a name based on the name of the input file,
2674
** but with a different (specified) suffix, and return a pointer
2676
PRIVATE FILE *file_open(lemp,suffix,mode)
2683
if( lemp->outname ) free(lemp->outname);
2684
lemp->outname = file_makename(lemp, suffix);
2685
fp = fopen(lemp->outname,mode);
2686
if( fp==0 && *mode=='w' ){
2687
fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
2694
/* Duplicate the input file without comments and without actions
2701
int i, j, maxlen, len, ncolumns, skip;
2702
printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
2704
for(i=0; i<lemp->nsymbol; i++){
2705
sp = lemp->symbols[i];
2706
len = strlen(sp->name);
2707
if( len>maxlen ) maxlen = len;
2709
ncolumns = 76/(maxlen+5);
2710
if( ncolumns<1 ) ncolumns = 1;
2711
skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
2712
for(i=0; i<skip; i++){
2714
for(j=i; j<lemp->nsymbol; j+=skip){
2715
sp = lemp->symbols[j];
2716
assert( sp->index==j );
2717
printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
2721
for(rp=lemp->rule; rp; rp=rp->next){
2722
printf("%s",rp->lhs->name);
2723
/* if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
2725
for(i=0; i<rp->nrhs; i++){
2727
printf(" %s", sp->name);
2728
if( sp->type==MULTITERMINAL ){
2729
for(j=1; j<sp->nsubsym; j++){
2730
printf("|%s", sp->subsym[j]->name);
2733
/* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
2736
if( rp->precsym ) printf(" [%s]",rp->precsym->name);
2737
/* if( rp->code ) printf("\n %s",rp->code); */
2742
void ConfigPrint(fp,cfp)
2750
fprintf(fp,"%s ::=",rp->lhs->name);
2751
for(i=0; i<=rp->nrhs; i++){
2752
if( i==cfp->dot ) fprintf(fp," *");
2753
if( i==rp->nrhs ) break;
2755
fprintf(fp," %s", sp->name);
2756
if( sp->type==MULTITERMINAL ){
2757
for(j=1; j<sp->nsubsym; j++){
2758
fprintf(fp,"|%s",sp->subsym[j]->name);
2767
PRIVATE void SetPrint(out,set,lemp)
2775
fprintf(out,"%12s[","");
2776
for(i=0; i<lemp->nterminal; i++){
2777
if( SetFind(set,i) ){
2778
fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
2785
/* Print a plink chain */
2786
PRIVATE void PlinkPrint(out,plp,tag)
2792
fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum);
2793
ConfigPrint(out,plp->cfp);
2800
/* Print an action to the given file descriptor. Return FALSE if
2801
** nothing was actually printed.
2803
int PrintAction(struct action *ap, FILE *fp, int indent){
2807
fprintf(fp,"%*s shift %d",indent,ap->sp->name,ap->x.stp->statenum);
2810
fprintf(fp,"%*s reduce %d",indent,ap->sp->name,ap->x.rp->index);
2813
fprintf(fp,"%*s accept",indent,ap->sp->name);
2816
fprintf(fp,"%*s error",indent,ap->sp->name);
2819
fprintf(fp,"%*s reduce %-3d ** Parsing conflict **",
2820
indent,ap->sp->name,ap->x.rp->index);
2831
/* Generate the "y.output" log file */
2832
void ReportOutput(lemp)
2841
fp = file_open(lemp,".out","wb");
2844
for(i=0; i<lemp->nstate; i++){
2845
stp = lemp->sorted[i];
2846
fprintf(fp,"State %d:\n",stp->statenum);
2847
if( lemp->basisflag ) cfp=stp->bp;
2851
if( cfp->dot==cfp->rp->nrhs ){
2852
sprintf(buf,"(%d)",cfp->rp->index);
2853
fprintf(fp," %5s ",buf);
2857
ConfigPrint(fp,cfp);
2860
SetPrint(fp,cfp->fws,lemp);
2861
PlinkPrint(fp,cfp->fplp,"To ");
2862
PlinkPrint(fp,cfp->bplp,"From");
2864
if( lemp->basisflag ) cfp=cfp->bp;
2868
for(ap=stp->ap; ap; ap=ap->next){
2869
if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
2877
/* Search for the file "name" which is in the same directory as
2878
** the exacutable */
2879
PRIVATE char *pathsearch(argv0,name,modemask)
2887
extern int access();
2890
cp = strrchr(argv0,'\\');
2892
cp = strrchr(argv0,'/');
2897
path = (char *)malloc( strlen(argv0) + strlen(name) + 2 );
2898
if( path ) sprintf(path,"%s/%s",argv0,name);
2901
extern char *getenv();
2902
pathlist = getenv("PATH");
2903
if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
2904
path = (char *)malloc( strlen(pathlist)+strlen(name)+2 );
2907
cp = strchr(pathlist,':');
2908
if( cp==0 ) cp = &pathlist[strlen(pathlist)];
2911
sprintf(path,"%s/%s",pathlist,name);
2913
if( c==0 ) pathlist = "";
2914
else pathlist = &cp[1];
2915
if( access(path,modemask)==0 ) break;
2922
/* Given an action, compute the integer value for that action
2923
** which is to be put in the action table of the generated machine.
2924
** Return negative if no action should be generated.
2926
PRIVATE int compute_action(lemp,ap)
2932
case SHIFT: act = ap->x.stp->statenum; break;
2933
case REDUCE: act = ap->x.rp->index + lemp->nstate; break;
2934
case ERROR: act = lemp->nstate + lemp->nrule; break;
2935
case ACCEPT: act = lemp->nstate + lemp->nrule + 1; break;
2936
default: act = -1; break;
2941
#define LINESIZE 1000
2942
/* The next cluster of routines are for reading the template file
2943
** and writing the results to the generated parser */
2944
/* The first function transfers data from "in" to "out" until
2945
** a line is seen which begins with "%%". The line number is
2948
** if name!=0, then any word that begin with "Parse" is changed to
2949
** begin with *name instead.
2951
PRIVATE void tplt_xfer(name,in,out,lineno)
2958
char line[LINESIZE];
2959
while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
2963
for(i=0; line[i]; i++){
2964
if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
2965
&& (i==0 || !isalpha(line[i-1]))
2967
if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
2968
fprintf(out,"%s",name);
2974
fprintf(out,"%s",&line[iStart]);
2978
/* The next function finds the template file and opens it, returning
2979
** a pointer to the opened file. */
2980
PRIVATE FILE *tplt_open(lemp)
2983
static char templatename[] = "lempar.c";
2989
cp = strrchr(lemp->filename,'.');
2991
sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
2993
sprintf(buf,"%s.lt",lemp->filename);
2995
if( access(buf,004)==0 ){
2997
}else if( access(templatename,004)==0 ){
2998
tpltname = templatename;
3000
tpltname = pathsearch(lemp->argv0,templatename,0);
3003
fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3008
in = fopen(tpltname,"rb");
3010
fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
3017
/* Print a #line directive line to the output file. */
3018
PRIVATE void tplt_linedir(out,lineno,filename)
3023
fprintf(out,"#line %d \"",lineno);
3025
if( *filename == '\\' ) putc('\\',out);
3026
putc(*filename,out);
3029
fprintf(out,"\"\n");
3032
/* Print a string to the file and keep the linenumber up to date */
3033
PRIVATE void tplt_print(out,lemp,str,strln,lineno)
3040
if( str==0 ) return;
3041
tplt_linedir(out,strln,lemp->filename);
3044
if( *str=='\n' ) (*lineno)++;
3048
if( str[-1]!='\n' ){
3052
tplt_linedir(out,*lineno+2,lemp->outname);
3058
** The following routine emits code for the destructor for the
3061
void emit_destructor_code(out,sp,lemp,lineno)
3070
if( sp->type==TERMINAL ){
3071
cp = lemp->tokendest;
3073
tplt_linedir(out,lemp->tokendestln,lemp->filename);
3075
}else if( sp->destructor ){
3076
cp = sp->destructor;
3077
tplt_linedir(out,sp->destructorln,lemp->filename);
3079
}else if( lemp->vardest ){
3082
tplt_linedir(out,lemp->vardestln,lemp->filename);
3085
assert( 0 ); /* Cannot happen */
3088
if( *cp=='$' && cp[1]=='$' ){
3089
fprintf(out,"(yypminor->yy%d)",sp->dtnum);
3093
if( *cp=='\n' ) linecnt++;
3096
(*lineno) += 3 + linecnt;
3098
tplt_linedir(out,*lineno,lemp->outname);
3103
** Return TRUE (non-zero) if the given symbol has a destructor.
3105
int has_destructor(sp, lemp)
3110
if( sp->type==TERMINAL ){
3111
ret = lemp->tokendest!=0;
3113
ret = lemp->vardest!=0 || sp->destructor!=0;
3119
** Append text to a dynamically allocated string. If zText is 0 then
3120
** reset the string to be empty again. Always return the complete text
3121
** of the string (which is overwritten with each call).
3123
** n bytes of zText are stored. If n==0 then all of zText up to the first
3124
** \000 terminator is stored. zText can contain up to two instances of
3125
** %d. The values of p1 and p2 are written into the first and second
3128
** If n==-1, then the previous character is overwritten.
3130
PRIVATE char *append_str(char *zText, int n, int p1, int p2){
3132
static int alloced = 0;
3133
static int used = 0;
3148
if( n+sizeof(zInt)*2+used >= alloced ){
3149
alloced = n + sizeof(zInt)*2 + used + 200;
3150
z = realloc(z, alloced);
3152
if( z==0 ) return "";
3155
if( c=='%' && zText[0]=='d' ){
3156
sprintf(zInt, "%d", p1);
3158
strcpy(&z[used], zInt);
3159
used += strlen(&z[used]);
3171
** zCode is a string that is the action associated with a rule. Expand
3172
** the symbols in this string so that the refer to elements of the parser
3175
PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){
3178
char lhsused = 0; /* True if the LHS element has been used */
3179
char used[MAXRHS]; /* True for each RHS element which is used */
3181
for(i=0; i<rp->nrhs; i++) used[i] = 0;
3184
append_str(0,0,0,0);
3185
for(cp=rp->code; *cp; cp++){
3186
if( isalpha(*cp) && (cp==rp->code || (!isalnum(cp[-1]) && cp[-1]!='_')) ){
3188
for(xp= &cp[1]; isalnum(*xp) || *xp=='_'; xp++);
3191
if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
3192
append_str("yygotominor.yy%d",0,rp->lhs->dtnum,0);
3196
for(i=0; i<rp->nrhs; i++){
3197
if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
3198
if( cp!=rp->code && cp[-1]=='@' ){
3199
/* If the argument is of the form @X then substituted
3200
** the token number of X, not the value of X */
3201
append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0);
3203
struct symbol *sp = rp->rhs[i];
3205
if( sp->type==MULTITERMINAL ){
3206
dtnum = sp->subsym[0]->dtnum;
3210
append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum);
3220
append_str(cp, 1, 0, 0);
3223
/* Check to make sure the LHS has been used */
3224
if( rp->lhsalias && !lhsused ){
3225
ErrorMsg(lemp->filename,rp->ruleline,
3226
"Label \"%s\" for \"%s(%s)\" is never used.",
3227
rp->lhsalias,rp->lhs->name,rp->lhsalias);
3231
/* Generate destructor code for RHS symbols which are not used in the
3233
for(i=0; i<rp->nrhs; i++){
3234
if( rp->rhsalias[i] && !used[i] ){
3235
ErrorMsg(lemp->filename,rp->ruleline,
3236
"Label %s for \"%s(%s)\" is never used.",
3237
rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
3239
}else if( rp->rhsalias[i]==0 ){
3240
if( has_destructor(rp->rhs[i],lemp) ){
3241
append_str(" yy_destructor(%d,&yymsp[%d].minor);\n", 0,
3242
rp->rhs[i]->index,i-rp->nrhs+1);
3244
/* No destructor defined for this term */
3248
cp = append_str(0,0,0,0);
3249
rp->code = Strsafe(cp);
3253
** Generate code which executes when the rule "rp" is reduced. Write
3254
** the code to "out". Make sure lineno stays up-to-date.
3256
PRIVATE void emit_code(out,rp,lemp,lineno)
3265
/* Generate code to do the reduce action */
3267
tplt_linedir(out,rp->line,lemp->filename);
3268
fprintf(out,"{%s",rp->code);
3269
for(cp=rp->code; *cp; cp++){
3270
if( *cp=='\n' ) linecnt++;
3272
(*lineno) += 3 + linecnt;
3274
tplt_linedir(out,*lineno,lemp->outname);
3275
} /* End if( rp->code ) */
3281
** Print the definition of the union used for the parser's data stack.
3282
** This union contains fields for every possible data type for tokens
3283
** and nonterminals. In the process of computing and printing this
3284
** union, also set the ".dtnum" field of every terminal and nonterminal
3287
void print_stack_union(out,lemp,plineno,mhflag)
3288
FILE *out; /* The output stream */
3289
struct lemon *lemp; /* The main info structure for this parser */
3290
int *plineno; /* Pointer to the line number */
3291
int mhflag; /* True if generating makeheaders output */
3293
int lineno = *plineno; /* The line number of the output */
3294
char **types; /* A hash table of datatypes */
3295
int arraysize; /* Size of the "types" array */
3296
int maxdtlength; /* Maximum length of any ".datatype" field. */
3297
char *stddt; /* Standardized name for a datatype */
3298
int i,j; /* Loop counters */
3299
int hash; /* For hashing the name of a type */
3300
char *name; /* Name of the parser */
3302
/* Allocate and initialize types[] and allocate stddt[] */
3303
arraysize = lemp->nsymbol * 2;
3304
types = (char**)malloc( arraysize * sizeof(char*) );
3305
for(i=0; i<arraysize; i++) types[i] = 0;
3307
if( lemp->vartype ){
3308
maxdtlength = strlen(lemp->vartype);
3310
for(i=0; i<lemp->nsymbol; i++){
3312
struct symbol *sp = lemp->symbols[i];
3313
if( sp->datatype==0 ) continue;
3314
len = strlen(sp->datatype);
3315
if( len>maxdtlength ) maxdtlength = len;
3317
stddt = (char*)malloc( maxdtlength*2 + 1 );
3318
if( types==0 || stddt==0 ){
3319
fprintf(stderr,"Out of memory.\n");
3323
/* Build a hash table of datatypes. The ".dtnum" field of each symbol
3324
** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
3325
** used for terminal symbols. If there is no %default_type defined then
3326
** 0 is also used as the .dtnum value for nonterminals which do not specify
3327
** a datatype using the %type directive.
3329
for(i=0; i<lemp->nsymbol; i++){
3330
struct symbol *sp = lemp->symbols[i];
3332
if( sp==lemp->errsym ){
3333
sp->dtnum = arraysize+1;
3336
if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
3341
if( cp==0 ) cp = lemp->vartype;
3343
while( isspace(*cp) ) cp++;
3344
while( *cp ) stddt[j++] = *cp++;
3345
while( j>0 && isspace(stddt[j-1]) ) j--;
3348
for(j=0; stddt[j]; j++){
3349
hash = hash*53 + stddt[j];
3351
hash = (hash & 0x7fffffff)%arraysize;
3352
while( types[hash] ){
3353
if( strcmp(types[hash],stddt)==0 ){
3354
sp->dtnum = hash + 1;
3358
if( hash>=arraysize ) hash = 0;
3360
if( types[hash]==0 ){
3361
sp->dtnum = hash + 1;
3362
types[hash] = (char*)malloc( strlen(stddt)+1 );
3363
if( types[hash]==0 ){
3364
fprintf(stderr,"Out of memory.\n");
3367
strcpy(types[hash],stddt);
3371
/* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
3372
name = lemp->name ? lemp->name : "Parse";
3374
if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
3375
fprintf(out,"#define %sTOKENTYPE %s\n",name,
3376
lemp->tokentype?lemp->tokentype:"void*"); lineno++;
3377
if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
3378
fprintf(out,"typedef union {\n"); lineno++;
3379
fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
3380
for(i=0; i<arraysize; i++){
3381
if( types[i]==0 ) continue;
3382
fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
3385
fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
3388
fprintf(out,"} YYMINORTYPE;\n"); lineno++;
3393
** Return the name of a C datatype able to represent values between
3394
** lwr and upr, inclusive.
3396
static const char *minimum_size_type(int lwr, int upr){
3399
return "unsigned char";
3400
}else if( upr<65535 ){
3401
return "unsigned short int";
3403
return "unsigned int";
3405
}else if( lwr>=-127 && upr<=127 ){
3406
return "signed char";
3407
}else if( lwr>=-32767 && upr<32767 ){
3415
** Each state contains a set of token transaction and a set of
3416
** nonterminal transactions. Each of these sets makes an instance
3417
** of the following structure. An array of these structures is used
3418
** to order the creation of entries in the yy_action[] table.
3421
struct state *stp; /* A pointer to a state */
3422
int isTkn; /* True to use tokens. False for non-terminals */
3423
int nAction; /* Number of actions */
3427
** Compare to axset structures for sorting purposes
3429
static int axset_compare(const void *a, const void *b){
3430
struct axset *p1 = (struct axset*)a;
3431
struct axset *p2 = (struct axset*)b;
3432
return p2->nAction - p1->nAction;
3435
/* Generate C source code for the parser */
3436
void ReportTable(lemp, mhflag)
3438
int mhflag; /* Output in makeheaders format if true */
3441
char line[LINESIZE];
3446
struct acttab *pActtab;
3449
int mnTknOfst, mxTknOfst;
3450
int mnNtOfst, mxNtOfst;
3453
in = tplt_open(lemp);
3455
out = file_open(lemp,".c","wb");
3461
tplt_xfer(lemp->name,in,out,&lineno);
3463
/* Generate the include code, if any */
3464
tplt_print(out,lemp,lemp->include,lemp->includeln,&lineno);
3466
char *name = file_makename(lemp, ".h");
3467
fprintf(out,"#include \"%s\"\n", name); lineno++;
3470
tplt_xfer(lemp->name,in,out,&lineno);
3472
/* Generate #defines for all tokens */
3475
fprintf(out,"#if INTERFACE\n"); lineno++;
3476
if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
3478
for(i=1; i<lemp->nterminal; i++){
3479
fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3482
fprintf(out,"#endif\n"); lineno++;
3484
tplt_xfer(lemp->name,in,out,&lineno);
3486
/* Generate the defines */
3487
fprintf(out,"#define YYCODETYPE %s\n",
3488
minimum_size_type(0, lemp->nsymbol+5)); lineno++;
3489
fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++;
3490
fprintf(out,"#define YYACTIONTYPE %s\n",
3491
minimum_size_type(0, lemp->nstate+lemp->nrule+5)); lineno++;
3492
print_stack_union(out,lemp,&lineno,mhflag);
3493
if( lemp->stacksize ){
3494
if( atoi(lemp->stacksize)<=0 ){
3495
ErrorMsg(lemp->filename,0,
3496
"Illegal stack size: [%s]. The stack size should be an integer constant.",
3499
lemp->stacksize = "100";
3501
fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
3503
fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
3506
fprintf(out,"#if INTERFACE\n"); lineno++;
3508
name = lemp->name ? lemp->name : "Parse";
3509
if( lemp->arg && lemp->arg[0] ){
3511
i = strlen(lemp->arg);
3512
while( i>=1 && isspace(lemp->arg[i-1]) ) i--;
3513
while( i>=1 && (isalnum(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
3514
fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++;
3515
fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++;
3516
fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n",
3517
name,lemp->arg,&lemp->arg[i]); lineno++;
3518
fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n",
3519
name,&lemp->arg[i],&lemp->arg[i]); lineno++;
3521
fprintf(out,"#define %sARG_SDECL\n",name); lineno++;
3522
fprintf(out,"#define %sARG_PDECL\n",name); lineno++;
3523
fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
3524
fprintf(out,"#define %sARG_STORE\n",name); lineno++;
3527
fprintf(out,"#endif\n"); lineno++;
3529
fprintf(out,"#define YYNSTATE %d\n",lemp->nstate); lineno++;
3530
fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
3531
fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
3532
fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
3533
if( lemp->has_fallback ){
3534
fprintf(out,"#define YYFALLBACK 1\n"); lineno++;
3536
tplt_xfer(lemp->name,in,out,&lineno);
3538
/* Generate the action table and its associates:
3540
** yy_action[] A single table containing all actions.
3541
** yy_lookahead[] A table containing the lookahead for each entry in
3542
** yy_action. Used to detect hash collisions.
3543
** yy_shift_ofst[] For each state, the offset into yy_action for
3544
** shifting terminals.
3545
** yy_reduce_ofst[] For each state, the offset into yy_action for
3546
** shifting non-terminals after a reduce.
3547
** yy_default[] Default action for each state.
3550
/* Compute the actions on all states and count them up */
3551
ax = malloc( sizeof(ax[0])*lemp->nstate*2 );
3553
fprintf(stderr,"malloc failed\n");
3556
for(i=0; i<lemp->nstate; i++){
3557
stp = lemp->sorted[i];
3560
ax[i*2].nAction = stp->nTknAct;
3561
ax[i*2+1].stp = stp;
3562
ax[i*2+1].isTkn = 0;
3563
ax[i*2+1].nAction = stp->nNtAct;
3565
mxTknOfst = mnTknOfst = 0;
3566
mxNtOfst = mnNtOfst = 0;
3568
/* Compute the action table. In order to try to keep the size of the
3569
** action table to a minimum, the heuristic of placing the largest action
3570
** sets first is used.
3572
qsort(ax, lemp->nstate*2, sizeof(ax[0]), axset_compare);
3573
pActtab = acttab_alloc();
3574
for(i=0; i<lemp->nstate*2 && ax[i].nAction>0; i++){
3577
for(ap=stp->ap; ap; ap=ap->next){
3579
if( ap->sp->index>=lemp->nterminal ) continue;
3580
action = compute_action(lemp, ap);
3581
if( action<0 ) continue;
3582
acttab_action(pActtab, ap->sp->index, action);
3584
stp->iTknOfst = acttab_insert(pActtab);
3585
if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
3586
if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
3588
for(ap=stp->ap; ap; ap=ap->next){
3590
if( ap->sp->index<lemp->nterminal ) continue;
3591
if( ap->sp->index==lemp->nsymbol ) continue;
3592
action = compute_action(lemp, ap);
3593
if( action<0 ) continue;
3594
acttab_action(pActtab, ap->sp->index, action);
3596
stp->iNtOfst = acttab_insert(pActtab);
3597
if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
3598
if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
3603
/* Output the yy_action table */
3604
fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
3605
n = acttab_size(pActtab);
3606
for(i=j=0; i<n; i++){
3607
int action = acttab_yyaction(pActtab, i);
3608
if( action<0 ) action = lemp->nsymbol + lemp->nrule + 2;
3609
if( j==0 ) fprintf(out," /* %5d */ ", i);
3610
fprintf(out, " %4d,", action);
3611
if( j==9 || i==n-1 ){
3612
fprintf(out, "\n"); lineno++;
3618
fprintf(out, "};\n"); lineno++;
3620
/* Output the yy_lookahead table */
3621
fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
3622
for(i=j=0; i<n; i++){
3623
int la = acttab_yylookahead(pActtab, i);
3624
if( la<0 ) la = lemp->nsymbol;
3625
if( j==0 ) fprintf(out," /* %5d */ ", i);
3626
fprintf(out, " %4d,", la);
3627
if( j==9 || i==n-1 ){
3628
fprintf(out, "\n"); lineno++;
3634
fprintf(out, "};\n"); lineno++;
3636
/* Output the yy_shift_ofst[] table */
3637
fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++;
3639
while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
3640
fprintf(out, "#define YY_SHIFT_MAX %d\n", n-1); lineno++;
3641
fprintf(out, "static const %s yy_shift_ofst[] = {\n",
3642
minimum_size_type(mnTknOfst-1, mxTknOfst)); lineno++;
3643
for(i=j=0; i<n; i++){
3645
stp = lemp->sorted[i];
3646
ofst = stp->iTknOfst;
3647
if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1;
3648
if( j==0 ) fprintf(out," /* %5d */ ", i);
3649
fprintf(out, " %4d,", ofst);
3650
if( j==9 || i==n-1 ){
3651
fprintf(out, "\n"); lineno++;
3657
fprintf(out, "};\n"); lineno++;
3659
/* Output the yy_reduce_ofst[] table */
3660
fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
3662
while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
3663
fprintf(out, "#define YY_REDUCE_MAX %d\n", n-1); lineno++;
3664
fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
3665
minimum_size_type(mnNtOfst-1, mxNtOfst)); lineno++;
3666
for(i=j=0; i<n; i++){
3668
stp = lemp->sorted[i];
3669
ofst = stp->iNtOfst;
3670
if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
3671
if( j==0 ) fprintf(out," /* %5d */ ", i);
3672
fprintf(out, " %4d,", ofst);
3673
if( j==9 || i==n-1 ){
3674
fprintf(out, "\n"); lineno++;
3680
fprintf(out, "};\n"); lineno++;
3682
/* Output the default action table */
3683
fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
3685
for(i=j=0; i<n; i++){
3686
stp = lemp->sorted[i];
3687
if( j==0 ) fprintf(out," /* %5d */ ", i);
3688
fprintf(out, " %4d,", stp->iDflt);
3689
if( j==9 || i==n-1 ){
3690
fprintf(out, "\n"); lineno++;
3696
fprintf(out, "};\n"); lineno++;
3697
tplt_xfer(lemp->name,in,out,&lineno);
3699
/* Generate the table of fallback tokens.
3701
if( lemp->has_fallback ){
3702
for(i=0; i<lemp->nterminal; i++){
3703
struct symbol *p = lemp->symbols[i];
3704
if( p->fallback==0 ){
3705
fprintf(out, " 0, /* %10s => nothing */\n", p->name);
3707
fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index,
3708
p->name, p->fallback->name);
3713
tplt_xfer(lemp->name, in, out, &lineno);
3715
/* Generate a table containing the symbolic name of every symbol
3717
for(i=0; i<lemp->nsymbol; i++){
3718
sprintf(line,"\"%s\",",lemp->symbols[i]->name);
3719
fprintf(out," %-15s",line);
3720
if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
3722
if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
3723
tplt_xfer(lemp->name,in,out,&lineno);
3725
/* Generate a table containing a text string that describes every
3726
** rule in the rule set of the grammer. This information is used
3727
** when tracing REDUCE actions.
3729
for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
3730
assert( rp->index==i );
3731
fprintf(out," /* %3d */ \"%s ::=", i, rp->lhs->name);
3732
for(j=0; j<rp->nrhs; j++){
3733
struct symbol *sp = rp->rhs[j];
3734
fprintf(out," %s", sp->name);
3735
if( sp->type==MULTITERMINAL ){
3737
for(k=1; k<sp->nsubsym; k++){
3738
fprintf(out,"|%s",sp->subsym[k]->name);
3742
fprintf(out,"\",\n"); lineno++;
3744
tplt_xfer(lemp->name,in,out,&lineno);
3746
/* Generate code which executes every time a symbol is popped from
3747
** the stack while processing errors or while destroying the parser.
3748
** (In other words, generate the %destructor actions)
3750
if( lemp->tokendest ){
3751
for(i=0; i<lemp->nsymbol; i++){
3752
struct symbol *sp = lemp->symbols[i];
3753
if( sp==0 || sp->type!=TERMINAL ) continue;
3754
fprintf(out," case %d:\n",sp->index); lineno++;
3756
for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
3757
if( i<lemp->nsymbol ){
3758
emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
3759
fprintf(out," break;\n"); lineno++;
3762
if( lemp->vardest ){
3763
struct symbol *dflt_sp = 0;
3764
for(i=0; i<lemp->nsymbol; i++){
3765
struct symbol *sp = lemp->symbols[i];
3766
if( sp==0 || sp->type==TERMINAL ||
3767
sp->index<=0 || sp->destructor!=0 ) continue;
3768
fprintf(out," case %d:\n",sp->index); lineno++;
3772
emit_destructor_code(out,dflt_sp,lemp,&lineno);
3773
fprintf(out," break;\n"); lineno++;
3776
for(i=0; i<lemp->nsymbol; i++){
3777
struct symbol *sp = lemp->symbols[i];
3778
if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
3779
fprintf(out," case %d:\n",sp->index); lineno++;
3781
/* Combine duplicate destructors into a single case */
3782
for(j=i+1; j<lemp->nsymbol; j++){
3783
struct symbol *sp2 = lemp->symbols[j];
3784
if( sp2 && sp2->type!=TERMINAL && sp2->destructor
3785
&& sp2->dtnum==sp->dtnum
3786
&& strcmp(sp->destructor,sp2->destructor)==0 ){
3787
fprintf(out," case %d:\n",sp2->index); lineno++;
3788
sp2->destructor = 0;
3792
emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
3793
fprintf(out," break;\n"); lineno++;
3795
tplt_xfer(lemp->name,in,out,&lineno);
3797
/* Generate code which executes whenever the parser stack overflows */
3798
tplt_print(out,lemp,lemp->overflow,lemp->overflowln,&lineno);
3799
tplt_xfer(lemp->name,in,out,&lineno);
3801
/* Generate the table of rule information
3803
** Note: This code depends on the fact that rules are number
3804
** sequentually beginning with 0.
3806
for(rp=lemp->rule; rp; rp=rp->next){
3807
fprintf(out," { %d, %d },\n",rp->lhs->index,rp->nrhs); lineno++;
3809
tplt_xfer(lemp->name,in,out,&lineno);
3811
/* Generate code which execution during each REDUCE action */
3812
for(rp=lemp->rule; rp; rp=rp->next){
3813
if( rp->code ) translate_code(lemp, rp);
3815
for(rp=lemp->rule; rp; rp=rp->next){
3817
if( rp->code==0 ) continue;
3818
fprintf(out," case %d:\n",rp->index); lineno++;
3819
for(rp2=rp->next; rp2; rp2=rp2->next){
3820
if( rp2->code==rp->code ){
3821
fprintf(out," case %d:\n",rp2->index); lineno++;
3825
emit_code(out,rp,lemp,&lineno);
3826
fprintf(out," break;\n"); lineno++;
3828
tplt_xfer(lemp->name,in,out,&lineno);
3830
/* Generate code which executes if a parse fails */
3831
tplt_print(out,lemp,lemp->failure,lemp->failureln,&lineno);
3832
tplt_xfer(lemp->name,in,out,&lineno);
3834
/* Generate code which executes when a syntax error occurs */
3835
tplt_print(out,lemp,lemp->error,lemp->errorln,&lineno);
3836
tplt_xfer(lemp->name,in,out,&lineno);
3838
/* Generate code which executes when the parser accepts its input */
3839
tplt_print(out,lemp,lemp->accept,lemp->acceptln,&lineno);
3840
tplt_xfer(lemp->name,in,out,&lineno);
3842
/* Append any addition code the user desires */
3843
tplt_print(out,lemp,lemp->extracode,lemp->extracodeln,&lineno);
3850
/* Generate a header file for the parser */
3851
void ReportHeader(lemp)
3856
char line[LINESIZE];
3857
char pattern[LINESIZE];
3860
if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
3862
in = file_open(lemp,".h","rb");
3864
for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
3865
sprintf(pattern,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3866
if( strcmp(line,pattern) ) break;
3869
if( i==lemp->nterminal ){
3870
/* No change in the file. Don't rewrite it. */
3874
out = file_open(lemp,".h","wb");
3876
for(i=1; i<lemp->nterminal; i++){
3877
fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3884
/* Reduce the size of the action tables, if possible, by making use
3887
** In this version, we take the most frequent REDUCE action and make
3890
void CompressTables(lemp)
3894
struct action *ap, *ap2;
3895
struct rule *rp, *rp2, *rbest;
3899
for(i=0; i<lemp->nstate; i++){
3900
stp = lemp->sorted[i];
3904
for(ap=stp->ap; ap; ap=ap->next){
3905
if( ap->type!=REDUCE ) continue;
3907
if( rp==rbest ) continue;
3909
for(ap2=ap->next; ap2; ap2=ap2->next){
3910
if( ap2->type!=REDUCE ) continue;
3912
if( rp2==rbest ) continue;
3921
/* Do not make a default if the number of rules to default
3922
** is not at least 1 */
3923
if( nbest<1 ) continue;
3926
/* Combine matching REDUCE actions into a single default */
3927
for(ap=stp->ap; ap; ap=ap->next){
3928
if( ap->type==REDUCE && ap->x.rp==rbest ) break;
3931
ap->sp = Symbol_new("{default}");
3932
for(ap=ap->next; ap; ap=ap->next){
3933
if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
3935
stp->ap = Action_sort(stp->ap);
3941
** Compare two states for sorting purposes. The smaller state is the
3942
** one with the most non-terminal actions. If they have the same number
3943
** of non-terminal actions, then the smaller is the one with the most
3946
static int stateResortCompare(const void *a, const void *b){
3947
const struct state *pA = *(const struct state**)a;
3948
const struct state *pB = *(const struct state**)b;
3951
n = pB->nNtAct - pA->nNtAct;
3953
n = pB->nTknAct - pA->nTknAct;
3960
** Renumber and resort states so that states with fewer choices
3961
** occur at the end. Except, keep state 0 as the first state.
3963
void ResortStates(lemp)
3970
for(i=0; i<lemp->nstate; i++){
3971
stp = lemp->sorted[i];
3972
stp->nTknAct = stp->nNtAct = 0;
3973
stp->iDflt = lemp->nstate + lemp->nrule;
3974
stp->iTknOfst = NO_OFFSET;
3975
stp->iNtOfst = NO_OFFSET;
3976
for(ap=stp->ap; ap; ap=ap->next){
3977
if( compute_action(lemp,ap)>=0 ){
3978
if( ap->sp->index<lemp->nterminal ){
3980
}else if( ap->sp->index<lemp->nsymbol ){
3983
stp->iDflt = compute_action(lemp, ap);
3988
qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
3989
stateResortCompare);
3990
for(i=0; i<lemp->nstate; i++){
3991
lemp->sorted[i]->statenum = i;
3996
/***************** From the file "set.c" ************************************/
3998
** Set manipulation routines for the LEMON parser generator.
4001
static int size = 0;
4003
/* Set the set size */
4010
/* Allocate a new set */
4014
s = (char*)malloc( size );
4016
extern void memory_error();
4019
for(i=0; i<size; i++) s[i] = 0;
4023
/* Deallocate a set */
4030
/* Add a new element to the set. Return TRUE if the element was added
4031
** and FALSE if it was already there. */
4042
/* Add every element of s2 to s1. Return TRUE if s1 changes. */
4049
for(i=0; i<size; i++){
4050
if( s2[i]==0 ) continue;
4058
/********************** From the file "table.c" ****************************/
4060
** All code in this file has been automatically generated
4061
** from a specification in the file
4063
** by the associative array code building program "aagen".
4064
** Do not edit this file! Instead, edit the specification
4065
** file, then rerun aagen.
4068
** Code for processing tables in the LEMON parser generator.
4071
PRIVATE int strhash(x)
4075
while( *x) h = h*13 + *(x++);
4079
/* Works like strdup, sort of. Save a string in malloced memory, but
4080
** keep strings in a table so that the same string is not in more
4088
z = Strsafe_find(y);
4089
if( z==0 && (z=malloc( strlen(y)+1 ))!=0 ){
4097
/* There is one instance of the following structure for each
4098
** associative array of type "x1".
4101
int size; /* The number of available slots. */
4102
/* Must be a power of 2 greater than or */
4104
int count; /* Number of currently slots filled */
4105
struct s_x1node *tbl; /* The data stored here */
4106
struct s_x1node **ht; /* Hash table for lookups */
4109
/* There is one instance of this structure for every data element
4110
** in an associative array of type "x1".
4112
typedef struct s_x1node {
4113
char *data; /* The data */
4114
struct s_x1node *next; /* Next entry with the same hash */
4115
struct s_x1node **from; /* Previous link */
4118
/* There is only one instance of the array, which is the following */
4119
static struct s_x1 *x1a;
4121
/* Allocate a new associative array */
4122
void Strsafe_init(){
4124
x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
4128
x1a->tbl = (x1node*)malloc(
4129
(sizeof(x1node) + sizeof(x1node*))*1024 );
4135
x1a->ht = (x1node**)&(x1a->tbl[1024]);
4136
for(i=0; i<1024; i++) x1a->ht[i] = 0;
4140
/* Insert a new record into the array. Return TRUE if successful.
4141
** Prior data with the same key is NOT overwritten */
4142
int Strsafe_insert(data)
4149
if( x1a==0 ) return 0;
4151
h = ph & (x1a->size-1);
4154
if( strcmp(np->data,data)==0 ){
4155
/* An existing entry with the same key is found. */
4156
/* Fail because overwrite is not allows. */
4161
if( x1a->count>=x1a->size ){
4162
/* Need to make the hash table bigger */
4165
array.size = size = x1a->size*2;
4166
array.count = x1a->count;
4167
array.tbl = (x1node*)malloc(
4168
(sizeof(x1node) + sizeof(x1node*))*size );
4169
if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4170
array.ht = (x1node**)&(array.tbl[size]);
4171
for(i=0; i<size; i++) array.ht[i] = 0;
4172
for(i=0; i<x1a->count; i++){
4173
x1node *oldnp, *newnp;
4174
oldnp = &(x1a->tbl[i]);
4175
h = strhash(oldnp->data) & (size-1);
4176
newnp = &(array.tbl[i]);
4177
if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4178
newnp->next = array.ht[h];
4179
newnp->data = oldnp->data;
4180
newnp->from = &(array.ht[h]);
4181
array.ht[h] = newnp;
4186
/* Insert the new data */
4187
h = ph & (x1a->size-1);
4188
np = &(x1a->tbl[x1a->count++]);
4190
if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
4191
np->next = x1a->ht[h];
4193
np->from = &(x1a->ht[h]);
4197
/* Return a pointer to data assigned to the given key. Return NULL
4198
** if no such key. */
4199
char *Strsafe_find(key)
4205
if( x1a==0 ) return 0;
4206
h = strhash(key) & (x1a->size-1);
4209
if( strcmp(np->data,key)==0 ) break;
4212
return np ? np->data : 0;
4215
/* Return a pointer to the (terminal or nonterminal) symbol "x".
4216
** Create a new symbol if this is the first time "x" has been seen.
4218
struct symbol *Symbol_new(x)
4223
sp = Symbol_find(x);
4225
sp = (struct symbol *)malloc( sizeof(struct symbol) );
4227
sp->name = Strsafe(x);
4228
sp->type = isupper(*x) ? TERMINAL : NONTERMINAL;
4234
sp->lambda = B_FALSE;
4237
Symbol_insert(sp,sp->name);
4242
/* Compare two symbols for working purposes
4244
** Symbols that begin with upper case letters (terminals or tokens)
4245
** must sort before symbols that begin with lower case letters
4246
** (non-terminals). Other than that, the order does not matter.
4248
** We find experimentally that leaving the symbols in their original
4249
** order (the order they appeared in the grammar file) gives the
4250
** smallest parser tables in SQLite.
4252
int Symbolcmpp(struct symbol **a, struct symbol **b){
4253
int i1 = (**a).index + 10000000*((**a).name[0]>'Z');
4254
int i2 = (**b).index + 10000000*((**b).name[0]>'Z');
4258
/* There is one instance of the following structure for each
4259
** associative array of type "x2".
4262
int size; /* The number of available slots. */
4263
/* Must be a power of 2 greater than or */
4265
int count; /* Number of currently slots filled */
4266
struct s_x2node *tbl; /* The data stored here */
4267
struct s_x2node **ht; /* Hash table for lookups */
4270
/* There is one instance of this structure for every data element
4271
** in an associative array of type "x2".
4273
typedef struct s_x2node {
4274
struct symbol *data; /* The data */
4275
char *key; /* The key */
4276
struct s_x2node *next; /* Next entry with the same hash */
4277
struct s_x2node **from; /* Previous link */
4280
/* There is only one instance of the array, which is the following */
4281
static struct s_x2 *x2a;
4283
/* Allocate a new associative array */
4286
x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
4290
x2a->tbl = (x2node*)malloc(
4291
(sizeof(x2node) + sizeof(x2node*))*128 );
4297
x2a->ht = (x2node**)&(x2a->tbl[128]);
4298
for(i=0; i<128; i++) x2a->ht[i] = 0;
4302
/* Insert a new record into the array. Return TRUE if successful.
4303
** Prior data with the same key is NOT overwritten */
4304
int Symbol_insert(data,key)
4305
struct symbol *data;
4312
if( x2a==0 ) return 0;
4314
h = ph & (x2a->size-1);
4317
if( strcmp(np->key,key)==0 ){
4318
/* An existing entry with the same key is found. */
4319
/* Fail because overwrite is not allows. */
4324
if( x2a->count>=x2a->size ){
4325
/* Need to make the hash table bigger */
4328
array.size = size = x2a->size*2;
4329
array.count = x2a->count;
4330
array.tbl = (x2node*)malloc(
4331
(sizeof(x2node) + sizeof(x2node*))*size );
4332
if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4333
array.ht = (x2node**)&(array.tbl[size]);
4334
for(i=0; i<size; i++) array.ht[i] = 0;
4335
for(i=0; i<x2a->count; i++){
4336
x2node *oldnp, *newnp;
4337
oldnp = &(x2a->tbl[i]);
4338
h = strhash(oldnp->key) & (size-1);
4339
newnp = &(array.tbl[i]);
4340
if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4341
newnp->next = array.ht[h];
4342
newnp->key = oldnp->key;
4343
newnp->data = oldnp->data;
4344
newnp->from = &(array.ht[h]);
4345
array.ht[h] = newnp;
4350
/* Insert the new data */
4351
h = ph & (x2a->size-1);
4352
np = &(x2a->tbl[x2a->count++]);
4355
if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
4356
np->next = x2a->ht[h];
4358
np->from = &(x2a->ht[h]);
4362
/* Return a pointer to data assigned to the given key. Return NULL
4363
** if no such key. */
4364
struct symbol *Symbol_find(key)
4370
if( x2a==0 ) return 0;
4371
h = strhash(key) & (x2a->size-1);
4374
if( strcmp(np->key,key)==0 ) break;
4377
return np ? np->data : 0;
4380
/* Return the n-th data. Return NULL if n is out of range. */
4381
struct symbol *Symbol_Nth(n)
4384
struct symbol *data;
4385
if( x2a && n>0 && n<=x2a->count ){
4386
data = x2a->tbl[n-1].data;
4393
/* Return the size of the array */
4396
return x2a ? x2a->count : 0;
4399
/* Return an array of pointers to all data in the table.
4400
** The array is obtained from malloc. Return NULL if memory allocation
4401
** problems, or if the array is empty. */
4402
struct symbol **Symbol_arrayof()
4404
struct symbol **array;
4406
if( x2a==0 ) return 0;
4408
array = (struct symbol **)malloc( sizeof(struct symbol *)*size );
4410
for(i=0; i<size; i++) array[i] = x2a->tbl[i].data;
4415
/* Compare two configurations */
4421
x = a->rp->index - b->rp->index;
4422
if( x==0 ) x = a->dot - b->dot;
4426
/* Compare two states */
4427
PRIVATE int statecmp(a,b)
4432
for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
4433
rc = a->rp->index - b->rp->index;
4434
if( rc==0 ) rc = a->dot - b->dot;
4444
PRIVATE int statehash(a)
4449
h = h*571 + a->rp->index*37 + a->dot;
4455
/* Allocate a new state structure */
4456
struct state *State_new()
4459
new = (struct state *)malloc( sizeof(struct state) );
4464
/* There is one instance of the following structure for each
4465
** associative array of type "x3".
4468
int size; /* The number of available slots. */
4469
/* Must be a power of 2 greater than or */
4471
int count; /* Number of currently slots filled */
4472
struct s_x3node *tbl; /* The data stored here */
4473
struct s_x3node **ht; /* Hash table for lookups */
4476
/* There is one instance of this structure for every data element
4477
** in an associative array of type "x3".
4479
typedef struct s_x3node {
4480
struct state *data; /* The data */
4481
struct config *key; /* The key */
4482
struct s_x3node *next; /* Next entry with the same hash */
4483
struct s_x3node **from; /* Previous link */
4486
/* There is only one instance of the array, which is the following */
4487
static struct s_x3 *x3a;
4489
/* Allocate a new associative array */
4492
x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
4496
x3a->tbl = (x3node*)malloc(
4497
(sizeof(x3node) + sizeof(x3node*))*128 );
4503
x3a->ht = (x3node**)&(x3a->tbl[128]);
4504
for(i=0; i<128; i++) x3a->ht[i] = 0;
4508
/* Insert a new record into the array. Return TRUE if successful.
4509
** Prior data with the same key is NOT overwritten */
4510
int State_insert(data,key)
4518
if( x3a==0 ) return 0;
4519
ph = statehash(key);
4520
h = ph & (x3a->size-1);
4523
if( statecmp(np->key,key)==0 ){
4524
/* An existing entry with the same key is found. */
4525
/* Fail because overwrite is not allows. */
4530
if( x3a->count>=x3a->size ){
4531
/* Need to make the hash table bigger */
4534
array.size = size = x3a->size*2;
4535
array.count = x3a->count;
4536
array.tbl = (x3node*)malloc(
4537
(sizeof(x3node) + sizeof(x3node*))*size );
4538
if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4539
array.ht = (x3node**)&(array.tbl[size]);
4540
for(i=0; i<size; i++) array.ht[i] = 0;
4541
for(i=0; i<x3a->count; i++){
4542
x3node *oldnp, *newnp;
4543
oldnp = &(x3a->tbl[i]);
4544
h = statehash(oldnp->key) & (size-1);
4545
newnp = &(array.tbl[i]);
4546
if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4547
newnp->next = array.ht[h];
4548
newnp->key = oldnp->key;
4549
newnp->data = oldnp->data;
4550
newnp->from = &(array.ht[h]);
4551
array.ht[h] = newnp;
4556
/* Insert the new data */
4557
h = ph & (x3a->size-1);
4558
np = &(x3a->tbl[x3a->count++]);
4561
if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
4562
np->next = x3a->ht[h];
4564
np->from = &(x3a->ht[h]);
4568
/* Return a pointer to data assigned to the given key. Return NULL
4569
** if no such key. */
4570
struct state *State_find(key)
4576
if( x3a==0 ) return 0;
4577
h = statehash(key) & (x3a->size-1);
4580
if( statecmp(np->key,key)==0 ) break;
4583
return np ? np->data : 0;
4586
/* Return an array of pointers to all data in the table.
4587
** The array is obtained from malloc. Return NULL if memory allocation
4588
** problems, or if the array is empty. */
4589
struct state **State_arrayof()
4591
struct state **array;
4593
if( x3a==0 ) return 0;
4595
array = (struct state **)malloc( sizeof(struct state *)*size );
4597
for(i=0; i<size; i++) array[i] = x3a->tbl[i].data;
4602
/* Hash a configuration */
4603
PRIVATE int confighash(a)
4607
h = h*571 + a->rp->index*37 + a->dot;
4611
/* There is one instance of the following structure for each
4612
** associative array of type "x4".
4615
int size; /* The number of available slots. */
4616
/* Must be a power of 2 greater than or */
4618
int count; /* Number of currently slots filled */
4619
struct s_x4node *tbl; /* The data stored here */
4620
struct s_x4node **ht; /* Hash table for lookups */
4623
/* There is one instance of this structure for every data element
4624
** in an associative array of type "x4".
4626
typedef struct s_x4node {
4627
struct config *data; /* The data */
4628
struct s_x4node *next; /* Next entry with the same hash */
4629
struct s_x4node **from; /* Previous link */
4632
/* There is only one instance of the array, which is the following */
4633
static struct s_x4 *x4a;
4635
/* Allocate a new associative array */
4636
void Configtable_init(){
4638
x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
4642
x4a->tbl = (x4node*)malloc(
4643
(sizeof(x4node) + sizeof(x4node*))*64 );
4649
x4a->ht = (x4node**)&(x4a->tbl[64]);
4650
for(i=0; i<64; i++) x4a->ht[i] = 0;
4654
/* Insert a new record into the array. Return TRUE if successful.
4655
** Prior data with the same key is NOT overwritten */
4656
int Configtable_insert(data)
4657
struct config *data;
4663
if( x4a==0 ) return 0;
4664
ph = confighash(data);
4665
h = ph & (x4a->size-1);
4668
if( Configcmp(np->data,data)==0 ){
4669
/* An existing entry with the same key is found. */
4670
/* Fail because overwrite is not allows. */
4675
if( x4a->count>=x4a->size ){
4676
/* Need to make the hash table bigger */
4679
array.size = size = x4a->size*2;
4680
array.count = x4a->count;
4681
array.tbl = (x4node*)malloc(
4682
(sizeof(x4node) + sizeof(x4node*))*size );
4683
if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4684
array.ht = (x4node**)&(array.tbl[size]);
4685
for(i=0; i<size; i++) array.ht[i] = 0;
4686
for(i=0; i<x4a->count; i++){
4687
x4node *oldnp, *newnp;
4688
oldnp = &(x4a->tbl[i]);
4689
h = confighash(oldnp->data) & (size-1);
4690
newnp = &(array.tbl[i]);
4691
if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4692
newnp->next = array.ht[h];
4693
newnp->data = oldnp->data;
4694
newnp->from = &(array.ht[h]);
4695
array.ht[h] = newnp;
4700
/* Insert the new data */
4701
h = ph & (x4a->size-1);
4702
np = &(x4a->tbl[x4a->count++]);
4704
if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
4705
np->next = x4a->ht[h];
4707
np->from = &(x4a->ht[h]);
4711
/* Return a pointer to data assigned to the given key. Return NULL
4712
** if no such key. */
4713
struct config *Configtable_find(key)
4719
if( x4a==0 ) return 0;
4720
h = confighash(key) & (x4a->size-1);
4723
if( Configcmp(np->data,key)==0 ) break;
4726
return np ? np->data : 0;
4729
/* Remove all data from the table. Pass each data to the function "f"
4730
** as it is removed. ("f" may be null to avoid this step.) */
4731
void Configtable_clear(f)
4732
int(*f)(/* struct config * */);
4735
if( x4a==0 || x4a->count==0 ) return;
4736
if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
4737
for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;