Annotation of embedaddon/lighttpd/src/lemon.c, revision 1.1.1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>