| /* |
| ** This file contains all sources (including headers) to the LEMON |
| ** LALR(1) parser generator. The sources have been combined into a |
| ** single file to make it easy to include LEMON in the source tree |
| ** and Makefile of another program. |
| ** |
| ** The author of this program disclaims copyright. |
| */ |
| #include <stdio.h> |
| #include <stdarg.h> |
| #include <string.h> |
| #include <ctype.h> |
| #include <stdlib.h> |
| #include <assert.h> |
| |
| #define ISSPACE(X) isspace((unsigned char)(X)) |
| #define ISDIGIT(X) isdigit((unsigned char)(X)) |
| #define ISALNUM(X) isalnum((unsigned char)(X)) |
| #define ISALPHA(X) isalpha((unsigned char)(X)) |
| #define ISUPPER(X) isupper((unsigned char)(X)) |
| #define ISLOWER(X) islower((unsigned char)(X)) |
| |
| |
| #ifndef __WIN32__ |
| # if defined(_WIN32) || defined(WIN32) |
| # define __WIN32__ |
| # endif |
| #endif |
| |
| #ifdef __WIN32__ |
| #ifdef __cplusplus |
| extern "C" { |
| #endif |
| extern int access(const char *path, int mode); |
| #ifdef __cplusplus |
| } |
| #endif |
| #else |
| #include <unistd.h> |
| #endif |
| |
| /* #define PRIVATE static */ |
| #define PRIVATE |
| |
| #ifdef TEST |
| #define MAXRHS 5 /* Set low to exercise exception code */ |
| #else |
| #define MAXRHS 1000 |
| #endif |
| |
| extern void memory_error(); |
| static int showPrecedenceConflict = 0; |
| static char *msort(char*,char**,int(*)(const char*,const char*)); |
| |
| /* |
| ** Compilers are getting increasingly pedantic about type conversions |
| ** as C evolves ever closer to Ada.... To work around the latest problems |
| ** we have to define the following variant of strlen(). |
| */ |
| #define lemonStrlen(X) ((int)strlen(X)) |
| |
| /* |
| ** Compilers are starting to complain about the use of sprintf() and strcpy(), |
| ** saying they are unsafe. So we define our own versions of those routines too. |
| ** |
| ** There are three routines here: lemon_sprintf(), lemon_vsprintf(), and |
| ** lemon_addtext(). The first two are replacements for sprintf() and vsprintf(). |
| ** The third is a helper routine for vsnprintf() that adds texts to the end of a |
| ** buffer, making sure the buffer is always zero-terminated. |
| ** |
| ** The string formatter is a minimal subset of stdlib sprintf() supporting only |
| ** a few simply conversions: |
| ** |
| ** %d |
| ** %s |
| ** %.*s |
| ** |
| */ |
| static void lemon_addtext( |
| char *zBuf, /* The buffer to which text is added */ |
| int *pnUsed, /* Slots of the buffer used so far */ |
| const char *zIn, /* Text to add */ |
| int nIn, /* Bytes of text to add. -1 to use strlen() */ |
| int iWidth /* Field width. Negative to left justify */ |
| ){ |
| if( nIn<0 ) for(nIn=0; zIn[nIn]; nIn++){} |
| while( iWidth>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth--; } |
| if( nIn==0 ) return; |
| memcpy(&zBuf[*pnUsed], zIn, nIn); |
| *pnUsed += nIn; |
| while( (-iWidth)>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth++; } |
| zBuf[*pnUsed] = 0; |
| } |
| static int lemon_vsprintf(char *str, const char *zFormat, va_list ap){ |
| int i, j, k, c; |
| int nUsed = 0; |
| const char *z; |
| char zTemp[50]; |
| str[0] = 0; |
| for(i=j=0; (c = zFormat[i])!=0; i++){ |
| if( c=='%' ){ |
| int iWidth = 0; |
| lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0); |
| c = zFormat[++i]; |
| if( ISDIGIT(c) || (c=='-' && ISDIGIT(zFormat[i+1])) ){ |
| if( c=='-' ) i++; |
| while( ISDIGIT(zFormat[i]) ) iWidth = iWidth*10 + zFormat[i++] - '0'; |
| if( c=='-' ) iWidth = -iWidth; |
| c = zFormat[i]; |
| } |
| if( c=='d' ){ |
| int v = va_arg(ap, int); |
| if( v<0 ){ |
| lemon_addtext(str, &nUsed, "-", 1, iWidth); |
| v = -v; |
| }else if( v==0 ){ |
| lemon_addtext(str, &nUsed, "0", 1, iWidth); |
| } |
| k = 0; |
| while( v>0 ){ |
| k++; |
| zTemp[sizeof(zTemp)-k] = (v%10) + '0'; |
| v /= 10; |
| } |
| lemon_addtext(str, &nUsed, &zTemp[sizeof(zTemp)-k], k, iWidth); |
| }else if( c=='s' ){ |
| z = va_arg(ap, const char*); |
| lemon_addtext(str, &nUsed, z, -1, iWidth); |
| }else if( c=='.' && memcmp(&zFormat[i], ".*s", 3)==0 ){ |
| i += 2; |
| k = va_arg(ap, int); |
| z = va_arg(ap, const char*); |
| lemon_addtext(str, &nUsed, z, k, iWidth); |
| }else if( c=='%' ){ |
| lemon_addtext(str, &nUsed, "%", 1, 0); |
| }else{ |
| fprintf(stderr, "illegal format\n"); |
| exit(1); |
| } |
| j = i+1; |
| } |
| } |
| lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0); |
| return nUsed; |
| } |
| static int lemon_sprintf(char *str, const char *format, ...){ |
| va_list ap; |
| int rc; |
| va_start(ap, format); |
| rc = lemon_vsprintf(str, format, ap); |
| va_end(ap); |
| return rc; |
| } |
| static void lemon_strcpy(char *dest, const char *src){ |
| while( (*(dest++) = *(src++))!=0 ){} |
| } |
| static void lemon_strcat(char *dest, const char *src){ |
| while( *dest ) dest++; |
| lemon_strcpy(dest, src); |
| } |
| |
| |
| /* a few forward declarations... */ |
| struct rule; |
| struct lemon; |
| struct action; |
| |
| static struct action *Action_new(void); |
| static struct action *Action_sort(struct action *); |
| |
| /********** From the file "build.h" ************************************/ |
| void FindRulePrecedences(struct lemon*); |
| void FindFirstSets(struct lemon*); |
| void FindStates(struct lemon*); |
| void FindLinks(struct lemon*); |
| void FindFollowSets(struct lemon*); |
| void FindActions(struct lemon*); |
| |
| /********* From the file "configlist.h" *********************************/ |
| void Configlist_init(void); |
| struct config *Configlist_add(struct rule *, int); |
| struct config *Configlist_addbasis(struct rule *, int); |
| void Configlist_closure(struct lemon *); |
| void Configlist_sort(void); |
| void Configlist_sortbasis(void); |
| struct config *Configlist_return(void); |
| struct config *Configlist_basis(void); |
| void Configlist_eat(struct config *); |
| void Configlist_reset(void); |
| |
| /********* From the file "error.h" ***************************************/ |
| void ErrorMsg(const char *, int,const char *, ...); |
| |
| /****** From the file "option.h" ******************************************/ |
| enum option_type { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR, |
| OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR}; |
| struct s_options { |
| enum option_type type; |
| const char *label; |
| char *arg; |
| const char *message; |
| }; |
| int OptInit(char**,struct s_options*,FILE*); |
| int OptNArgs(void); |
| char *OptArg(int); |
| void OptErr(int); |
| void OptPrint(void); |
| |
| /******** From the file "parse.h" *****************************************/ |
| void Parse(struct lemon *lemp); |
| |
| /********* From the file "plink.h" ***************************************/ |
| struct plink *Plink_new(void); |
| void Plink_add(struct plink **, struct config *); |
| void Plink_copy(struct plink **, struct plink *); |
| void Plink_delete(struct plink *); |
| |
| /********** From the file "report.h" *************************************/ |
| void Reprint(struct lemon *); |
| void ReportOutput(struct lemon *); |
| void ReportTable(struct lemon *, int, int); |
| void ReportHeader(struct lemon *); |
| void CompressTables(struct lemon *); |
| void ResortStates(struct lemon *); |
| |
| /********** From the file "set.h" ****************************************/ |
| void SetSize(int); /* All sets will be of size N */ |
| char *SetNew(void); /* A new set for element 0..N */ |
| void SetFree(char*); /* Deallocate a set */ |
| int SetAdd(char*,int); /* Add element to a set */ |
| int SetUnion(char *,char *); /* A <- A U B, thru element N */ |
| #define SetFind(X,Y) (X[Y]) /* True if Y is in set X */ |
| |
| /********** From the file "struct.h" *************************************/ |
| /* |
| ** Principal data structures for the LEMON parser generator. |
| */ |
| |
| typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean; |
| |
| /* Symbols (terminals and nonterminals) of the grammar are stored |
| ** in the following: */ |
| enum symbol_type { |
| TERMINAL, |
| NONTERMINAL, |
| MULTITERMINAL |
| }; |
| enum e_assoc { |
| LEFT, |
| RIGHT, |
| NONE, |
| UNK |
| }; |
| struct symbol { |
| const char *name; /* Name of the symbol */ |
| int index; /* Index number for this symbol */ |
| enum symbol_type type; /* Symbols are all either TERMINALS or NTs */ |
| struct rule *rule; /* Linked list of rules of this (if an NT) */ |
| struct symbol *fallback; /* fallback token in case this token doesn't parse */ |
| int prec; /* Precedence if defined (-1 otherwise) */ |
| enum e_assoc assoc; /* Associativity if precedence is defined */ |
| char *firstset; /* First-set for all rules of this symbol */ |
| Boolean lambda; /* True if NT and can generate an empty string */ |
| int useCnt; /* Number of times used */ |
| char *destructor; /* Code which executes whenever this symbol is |
| ** popped from the stack during error processing */ |
| int destLineno; /* Line number for start of destructor. Set to |
| ** -1 for duplicate destructors. */ |
| char *datatype; /* The data type of information held by this |
| ** object. Only used if type==NONTERMINAL */ |
| int dtnum; /* The data type number. In the parser, the value |
| ** stack is a union. The .yy%d element of this |
| ** union is the correct data type for this object */ |
| int bContent; /* True if this symbol ever carries content - if |
| ** it is ever more than just syntax */ |
| /* The following fields are used by MULTITERMINALs only */ |
| int nsubsym; /* Number of constituent symbols in the MULTI */ |
| struct symbol **subsym; /* Array of constituent symbols */ |
| }; |
| |
| /* Each production rule in the grammar is stored in the following |
| ** structure. */ |
| struct rule { |
| struct symbol *lhs; /* Left-hand side of the rule */ |
| const char *lhsalias; /* Alias for the LHS (NULL if none) */ |
| int lhsStart; /* True if left-hand side is the start symbol */ |
| int ruleline; /* Line number for the rule */ |
| int nrhs; /* Number of RHS symbols */ |
| struct symbol **rhs; /* The RHS symbols */ |
| const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */ |
| int line; /* Line number at which code begins */ |
| const char *code; /* The code executed when this rule is reduced */ |
| const char *codePrefix; /* Setup code before code[] above */ |
| const char *codeSuffix; /* Breakdown code after code[] above */ |
| struct symbol *precsym; /* Precedence symbol for this rule */ |
| int index; /* An index number for this rule */ |
| int iRule; /* Rule number as used in the generated tables */ |
| Boolean noCode; /* True if this rule has no associated C code */ |
| Boolean codeEmitted; /* True if the code has been emitted already */ |
| Boolean canReduce; /* True if this rule is ever reduced */ |
| Boolean doesReduce; /* Reduce actions occur after optimization */ |
| Boolean neverReduce; /* Reduce is theoretically possible, but prevented |
| ** by actions or other outside implementation */ |
| struct rule *nextlhs; /* Next rule with the same LHS */ |
| struct rule *next; /* Next rule in the global list */ |
| }; |
| |
| /* A configuration is a production rule of the grammar together with |
| ** a mark (dot) showing how much of that rule has been processed so far. |
| ** Configurations also contain a follow-set which is a list of terminal |
| ** symbols which are allowed to immediately follow the end of the rule. |
| ** Every configuration is recorded as an instance of the following: */ |
| enum cfgstatus { |
| COMPLETE, |
| INCOMPLETE |
| }; |
| struct config { |
| struct rule *rp; /* The rule upon which the configuration is based */ |
| int dot; /* The parse point */ |
| char *fws; /* Follow-set for this configuration only */ |
| struct plink *fplp; /* Follow-set forward propagation links */ |
| struct plink *bplp; /* Follow-set backwards propagation links */ |
| struct state *stp; /* Pointer to state which contains this */ |
| enum cfgstatus status; /* used during followset and shift computations */ |
| struct config *next; /* Next configuration in the state */ |
| struct config *bp; /* The next basis configuration */ |
| }; |
| |
| enum e_action { |
| SHIFT, |
| ACCEPT, |
| REDUCE, |
| ERROR, |
| SSCONFLICT, /* A shift/shift conflict */ |
| SRCONFLICT, /* Was a reduce, but part of a conflict */ |
| RRCONFLICT, /* Was a reduce, but part of a conflict */ |
| SH_RESOLVED, /* Was a shift. Precedence resolved conflict */ |
| RD_RESOLVED, /* Was reduce. Precedence resolved conflict */ |
| NOT_USED, /* Deleted by compression */ |
| SHIFTREDUCE /* Shift first, then reduce */ |
| }; |
| |
| /* Every shift or reduce operation is stored as one of the following */ |
| struct action { |
| struct symbol *sp; /* The look-ahead symbol */ |
| enum e_action type; |
| union { |
| struct state *stp; /* The new state, if a shift */ |
| struct rule *rp; /* The rule, if a reduce */ |
| } x; |
| struct symbol *spOpt; /* SHIFTREDUCE optimization to this symbol */ |
| struct action *next; /* Next action for this state */ |
| struct action *collide; /* Next action with the same hash */ |
| }; |
| |
| /* Each state of the generated parser's finite state machine |
| ** is encoded as an instance of the following structure. */ |
| struct state { |
| struct config *bp; /* The basis configurations for this state */ |
| struct config *cfp; /* All configurations in this set */ |
| int statenum; /* Sequential number for this state */ |
| struct action *ap; /* List of actions for this state */ |
| int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */ |
| int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */ |
| int iDfltReduce; /* Default action is to REDUCE by this rule */ |
| struct rule *pDfltReduce;/* The default REDUCE rule. */ |
| int autoReduce; /* True if this is an auto-reduce state */ |
| }; |
| #define NO_OFFSET (-2147483647) |
| |
| /* A followset propagation link indicates that the contents of one |
| ** configuration followset should be propagated to another whenever |
| ** the first changes. */ |
| struct plink { |
| struct config *cfp; /* The configuration to which linked */ |
| struct plink *next; /* The next propagate link */ |
| }; |
| |
| /* The state vector for the entire parser generator is recorded as |
| ** follows. (LEMON uses no global variables and makes little use of |
| ** static variables. Fields in the following structure can be thought |
| ** of as begin global variables in the program.) */ |
| struct lemon { |
| struct state **sorted; /* Table of states sorted by state number */ |
| struct rule *rule; /* List of all rules */ |
| struct rule *startRule; /* First rule */ |
| int nstate; /* Number of states */ |
| int nxstate; /* nstate with tail degenerate states removed */ |
| int nrule; /* Number of rules */ |
| int nruleWithAction; /* Number of rules with actions */ |
| int nsymbol; /* Number of terminal and nonterminal symbols */ |
| int nterminal; /* Number of terminal symbols */ |
| int minShiftReduce; /* Minimum shift-reduce action value */ |
| int errAction; /* Error action value */ |
| int accAction; /* Accept action value */ |
| int noAction; /* No-op action value */ |
| int minReduce; /* Minimum reduce action */ |
| int maxAction; /* Maximum action value of any kind */ |
| struct symbol **symbols; /* Sorted array of pointers to symbols */ |
| int errorcnt; /* Number of errors */ |
| struct symbol *errsym; /* The error symbol */ |
| struct symbol *wildcard; /* Token that matches anything */ |
| char *name; /* Name of the generated parser */ |
| char *arg; /* Declaration of the 3th argument to parser */ |
| char *ctx; /* Declaration of 2nd argument to constructor */ |
| char *tokentype; /* Type of terminal symbols in the parser stack */ |
| char *vartype; /* The default type of non-terminal symbols */ |
| char *start; /* Name of the start symbol for the grammar */ |
| char *stacksize; /* Size of the parser stack */ |
| char *include; /* Code to put at the start of the C file */ |
| char *error; /* Code to execute when an error is seen */ |
| char *overflow; /* Code to execute on a stack overflow */ |
| char *failure; /* Code to execute on parser failure */ |
| char *accept; /* Code to execute when the parser excepts */ |
| char *extracode; /* Code appended to the generated file */ |
| char *tokendest; /* Code to execute to destroy token data */ |
| char *vardest; /* Code for the default non-terminal destructor */ |
| char *filename; /* Name of the input file */ |
| char *outname; /* Name of the current output file */ |
| char *tokenprefix; /* A prefix added to token names in the .h file */ |
| int nconflict; /* Number of parsing conflicts */ |
| int nactiontab; /* Number of entries in the yy_action[] table */ |
| int nlookaheadtab; /* Number of entries in yy_lookahead[] */ |
| int tablesize; /* Total table size of all tables in bytes */ |
| int basisflag; /* Print only basis configurations */ |
| int has_fallback; /* True if any %fallback is seen in the grammar */ |
| int nolinenosflag; /* True if #line statements should not be printed */ |
| char *argv0; /* Name of the program */ |
| }; |
| |
| #define MemoryCheck(X) if((X)==0){ \ |
| extern void memory_error(); \ |
| memory_error(); \ |
| } |
| |
| /**************** From the file "table.h" *********************************/ |
| /* |
| ** All code in this file has been automatically generated |
| ** from a specification in the file |
| ** "table.q" |
| ** by the associative array code building program "aagen". |
| ** Do not edit this file! Instead, edit the specification |
| ** file, then rerun aagen. |
| */ |
| /* |
| ** Code for processing tables in the LEMON parser generator. |
| */ |
| /* Routines for handling a strings */ |
| |
| const char *Strsafe(const char *); |
| |
| void Strsafe_init(void); |
| int Strsafe_insert(const char *); |
| const char *Strsafe_find(const char *); |
| |
| /* Routines for handling symbols of the grammar */ |
| |
| struct symbol *Symbol_new(const char *); |
| int Symbolcmpp(const void *, const void *); |
| void Symbol_init(void); |
| int Symbol_insert(struct symbol *, const char *); |
| struct symbol *Symbol_find(const char *); |
| struct symbol *Symbol_Nth(int); |
| int Symbol_count(void); |
| struct symbol **Symbol_arrayof(void); |
| |
| /* Routines to manage the state table */ |
| |
| int Configcmp(const char *, const char *); |
| struct state *State_new(void); |
| void State_init(void); |
| int State_insert(struct state *, struct config *); |
| struct state *State_find(struct config *); |
| struct state **State_arrayof(void); |
| |
| /* Routines used for efficiency in Configlist_add */ |
| |
| void Configtable_init(void); |
| int Configtable_insert(struct config *); |
| struct config *Configtable_find(struct config *); |
| void Configtable_clear(int(*)(struct config *)); |
| |
| /****************** From the file "action.c" *******************************/ |
| /* |
| ** Routines processing parser actions in the LEMON parser generator. |
| */ |
| |
| /* Allocate a new parser action */ |
| static struct action *Action_new(void){ |
| static struct action *actionfreelist = 0; |
| struct action *newaction; |
| |
| if( actionfreelist==0 ){ |
| int i; |
| int amt = 100; |
| actionfreelist = (struct action *)calloc(amt, sizeof(struct action)); |
| if( actionfreelist==0 ){ |
| fprintf(stderr,"Unable to allocate memory for a new parser action."); |
| exit(1); |
| } |
| for(i=0; i<amt-1; i++) actionfreelist[i].next = &actionfreelist[i+1]; |
| actionfreelist[amt-1].next = 0; |
| } |
| newaction = actionfreelist; |
| actionfreelist = actionfreelist->next; |
| return newaction; |
| } |
| |
| /* Compare two actions for sorting purposes. Return negative, zero, or |
| ** positive if the first action is less than, equal to, or greater than |
| ** the first |
| */ |
| static int actioncmp( |
| struct action *ap1, |
| struct action *ap2 |
| ){ |
| int rc; |
| rc = ap1->sp->index - ap2->sp->index; |
| if( rc==0 ){ |
| rc = (int)ap1->type - (int)ap2->type; |
| } |
| if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){ |
| rc = ap1->x.rp->index - ap2->x.rp->index; |
| } |
| if( rc==0 ){ |
| rc = (int) (ap2 - ap1); |
| } |
| return rc; |
| } |
| |
| /* Sort parser actions */ |
| static struct action *Action_sort( |
| struct action *ap |
| ){ |
| ap = (struct action *)msort((char *)ap,(char **)&ap->next, |
| (int(*)(const char*,const char*))actioncmp); |
| return ap; |
| } |
| |
| void Action_add( |
| struct action **app, |
| enum e_action type, |
| struct symbol *sp, |
| char *arg |
| ){ |
| struct action *newaction; |
| newaction = Action_new(); |
| newaction->next = *app; |
| *app = newaction; |
| newaction->type = type; |
| newaction->sp = sp; |
| newaction->spOpt = 0; |
| if( type==SHIFT ){ |
| newaction->x.stp = (struct state *)arg; |
| }else{ |
| newaction->x.rp = (struct rule *)arg; |
| } |
| } |
| /********************** New code to implement the "acttab" module ***********/ |
| /* |
| ** This module implements routines use to construct the yy_action[] table. |
| */ |
| |
| /* |
| ** The state of the yy_action table under construction is an instance of |
| ** the following structure. |
| ** |
| ** The yy_action table maps the pair (state_number, lookahead) into an |
| ** action_number. The table is an array of integers pairs. The state_number |
| ** determines an initial offset into the yy_action array. The lookahead |
| ** value is then added to this initial offset to get an index X into the |
| ** yy_action array. If the aAction[X].lookahead equals the value of the |
| ** of the lookahead input, then the value of the action_number output is |
| ** aAction[X].action. If the lookaheads do not match then the |
| ** default action for the state_number is returned. |
| ** |
| ** All actions associated with a single state_number are first entered |
| ** into aLookahead[] using multiple calls to acttab_action(). Then the |
| ** actions for that single state_number are placed into the aAction[] |
| ** array with a single call to acttab_insert(). The acttab_insert() call |
| ** also resets the aLookahead[] array in preparation for the next |
| ** state number. |
| */ |
| struct lookahead_action { |
| int lookahead; /* Value of the lookahead token */ |
| int action; /* Action to take on the given lookahead */ |
| }; |
| typedef struct acttab acttab; |
| struct acttab { |
| int nAction; /* Number of used slots in aAction[] */ |
| int nActionAlloc; /* Slots allocated for aAction[] */ |
| struct lookahead_action |
| *aAction, /* The yy_action[] table under construction */ |
| *aLookahead; /* A single new transaction set */ |
| int mnLookahead; /* Minimum aLookahead[].lookahead */ |
| int mnAction; /* Action associated with mnLookahead */ |
| int mxLookahead; /* Maximum aLookahead[].lookahead */ |
| int nLookahead; /* Used slots in aLookahead[] */ |
| int nLookaheadAlloc; /* Slots allocated in aLookahead[] */ |
| int nterminal; /* Number of terminal symbols */ |
| int nsymbol; /* total number of symbols */ |
| }; |
| |
| /* Return the number of entries in the yy_action table */ |
| #define acttab_lookahead_size(X) ((X)->nAction) |
| |
| /* The value for the N-th entry in yy_action */ |
| #define acttab_yyaction(X,N) ((X)->aAction[N].action) |
| |
| /* The value for the N-th entry in yy_lookahead */ |
| #define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead) |
| |
| /* Free all memory associated with the given acttab */ |
| void acttab_free(acttab *p){ |
| free( p->aAction ); |
| free( p->aLookahead ); |
| free( p ); |
| } |
| |
| /* Allocate a new acttab structure */ |
| acttab *acttab_alloc(int nsymbol, int nterminal){ |
| acttab *p = (acttab *) calloc( 1, sizeof(*p) ); |
| if( p==0 ){ |
| fprintf(stderr,"Unable to allocate memory for a new acttab."); |
| exit(1); |
| } |
| memset(p, 0, sizeof(*p)); |
| p->nsymbol = nsymbol; |
| p->nterminal = nterminal; |
| return p; |
| } |
| |
| /* Add a new action to the current transaction set. |
| ** |
| ** This routine is called once for each lookahead for a particular |
| ** state. |
| */ |
| void acttab_action(acttab *p, int lookahead, int action){ |
| if( p->nLookahead>=p->nLookaheadAlloc ){ |
| p->nLookaheadAlloc += 25; |
| p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead, |
| sizeof(p->aLookahead[0])*p->nLookaheadAlloc ); |
| if( p->aLookahead==0 ){ |
| fprintf(stderr,"malloc failed\n"); |
| exit(1); |
| } |
| } |
| if( p->nLookahead==0 ){ |
| p->mxLookahead = lookahead; |
| p->mnLookahead = lookahead; |
| p->mnAction = action; |
| }else{ |
| if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead; |
| if( p->mnLookahead>lookahead ){ |
| p->mnLookahead = lookahead; |
| p->mnAction = action; |
| } |
| } |
| p->aLookahead[p->nLookahead].lookahead = lookahead; |
| p->aLookahead[p->nLookahead].action = action; |
| p->nLookahead++; |
| } |
| |
| /* |
| ** Add the transaction set built up with prior calls to acttab_action() |
| ** into the current action table. Then reset the transaction set back |
| ** to an empty set in preparation for a new round of acttab_action() calls. |
| ** |
| ** Return the offset into the action table of the new transaction. |
| ** |
| ** If the makeItSafe parameter is true, then the offset is chosen so that |
| ** it is impossible to overread the yy_lookaside[] table regardless of |
| ** the lookaside token. This is done for the terminal symbols, as they |
| ** come from external inputs and can contain syntax errors. When makeItSafe |
| ** is false, there is more flexibility in selecting offsets, resulting in |
| ** a smaller table. For non-terminal symbols, which are never syntax errors, |
| ** makeItSafe can be false. |
| */ |
| int acttab_insert(acttab *p, int makeItSafe){ |
| int i, j, k, n, end; |
| assert( p->nLookahead>0 ); |
| |
| /* Make sure we have enough space to hold the expanded action table |
| ** in the worst case. The worst case occurs if the transaction set |
| ** must be appended to the current action table |
| */ |
| n = p->nsymbol + 1; |
| if( p->nAction + n >= p->nActionAlloc ){ |
| int oldAlloc = p->nActionAlloc; |
| p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20; |
| p->aAction = (struct lookahead_action *) realloc( p->aAction, |
| sizeof(p->aAction[0])*p->nActionAlloc); |
| if( p->aAction==0 ){ |
| fprintf(stderr,"malloc failed\n"); |
| exit(1); |
| } |
| for(i=oldAlloc; i<p->nActionAlloc; i++){ |
| p->aAction[i].lookahead = -1; |
| p->aAction[i].action = -1; |
| } |
| } |
| |
| /* Scan the existing action table looking for an offset that is a |
| ** duplicate of the current transaction set. Fall out of the loop |
| ** if and when the duplicate is found. |
| ** |
| ** i is the index in p->aAction[] where p->mnLookahead is inserted. |
| */ |
| end = makeItSafe ? p->mnLookahead : 0; |
| for(i=p->nAction-1; i>=end; i--){ |
| if( p->aAction[i].lookahead==p->mnLookahead ){ |
| /* All lookaheads and actions in the aLookahead[] transaction |
| ** must match against the candidate aAction[i] entry. */ |
| if( p->aAction[i].action!=p->mnAction ) continue; |
| for(j=0; j<p->nLookahead; j++){ |
| k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| if( k<0 || k>=p->nAction ) break; |
| if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break; |
| if( p->aLookahead[j].action!=p->aAction[k].action ) break; |
| } |
| if( j<p->nLookahead ) continue; |
| |
| /* No possible lookahead value that is not in the aLookahead[] |
| ** transaction is allowed to match aAction[i] */ |
| n = 0; |
| for(j=0; j<p->nAction; j++){ |
| if( p->aAction[j].lookahead<0 ) continue; |
| if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++; |
| } |
| if( n==p->nLookahead ){ |
| break; /* An exact match is found at offset i */ |
| } |
| } |
| } |
| |
| /* If no existing offsets exactly match the current transaction, find an |
| ** an empty offset in the aAction[] table in which we can add the |
| ** aLookahead[] transaction. |
| */ |
| if( i<end ){ |
| /* Look for holes in the aAction[] table that fit the current |
| ** aLookahead[] transaction. Leave i set to the offset of the hole. |
| ** If no holes are found, i is left at p->nAction, which means the |
| ** transaction will be appended. */ |
| i = makeItSafe ? p->mnLookahead : 0; |
| for(; i<p->nActionAlloc - p->mxLookahead; i++){ |
| if( p->aAction[i].lookahead<0 ){ |
| for(j=0; j<p->nLookahead; j++){ |
| k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| if( k<0 ) break; |
| if( p->aAction[k].lookahead>=0 ) break; |
| } |
| if( j<p->nLookahead ) continue; |
| for(j=0; j<p->nAction; j++){ |
| if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break; |
| } |
| if( j==p->nAction ){ |
| break; /* Fits in empty slots */ |
| } |
| } |
| } |
| } |
| /* Insert transaction set at index i. */ |
| #if 0 |
| printf("Acttab:"); |
| for(j=0; j<p->nLookahead; j++){ |
| printf(" %d", p->aLookahead[j].lookahead); |
| } |
| printf(" inserted at %d\n", i); |
| #endif |
| for(j=0; j<p->nLookahead; j++){ |
| k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| p->aAction[k] = p->aLookahead[j]; |
| if( k>=p->nAction ) p->nAction = k+1; |
| } |
| if( makeItSafe && i+p->nterminal>=p->nAction ) p->nAction = i+p->nterminal+1; |
| p->nLookahead = 0; |
| |
| /* Return the offset that is added to the lookahead in order to get the |
| ** index into yy_action of the action */ |
| return i - p->mnLookahead; |
| } |
| |
| /* |
| ** Return the size of the action table without the trailing syntax error |
| ** entries. |
| */ |
| int acttab_action_size(acttab *p){ |
| int n = p->nAction; |
| while( n>0 && p->aAction[n-1].lookahead<0 ){ n--; } |
| return n; |
| } |
| |
| /********************** From the file "build.c" *****************************/ |
| /* |
| ** Routines to construction the finite state machine for the LEMON |
| ** parser generator. |
| */ |
| |
| /* Find a precedence symbol of every rule in the grammar. |
| ** |
| ** Those rules which have a precedence symbol coded in the input |
| ** grammar using the "[symbol]" construct will already have the |
| ** rp->precsym field filled. Other rules take as their precedence |
| ** symbol the first RHS symbol with a defined precedence. If there |
| ** are not RHS symbols with a defined precedence, the precedence |
| ** symbol field is left blank. |
| */ |
| void FindRulePrecedences(struct lemon *xp) |
| { |
| struct rule *rp; |
| for(rp=xp->rule; rp; rp=rp->next){ |
| if( rp->precsym==0 ){ |
| int i, j; |
| for(i=0; i<rp->nrhs && rp->precsym==0; i++){ |
| struct symbol *sp = rp->rhs[i]; |
| if( sp->type==MULTITERMINAL ){ |
| for(j=0; j<sp->nsubsym; j++){ |
| if( sp->subsym[j]->prec>=0 ){ |
| rp->precsym = sp->subsym[j]; |
| break; |
| } |
| } |
| }else if( sp->prec>=0 ){ |
| rp->precsym = rp->rhs[i]; |
| } |
| } |
| } |
| } |
| return; |
| } |
| |
| /* Find all nonterminals which will generate the empty string. |
| ** Then go back and compute the first sets of every nonterminal. |
| ** The first set is the set of all terminal symbols which can begin |
| ** a string generated by that nonterminal. |
| */ |
| void FindFirstSets(struct lemon *lemp) |
| { |
| int i, j; |
| struct rule *rp; |
| int progress; |
| |
| for(i=0; i<lemp->nsymbol; i++){ |
| lemp->symbols[i]->lambda = LEMON_FALSE; |
| } |
| for(i=lemp->nterminal; i<lemp->nsymbol; i++){ |
| lemp->symbols[i]->firstset = SetNew(); |
| } |
| |
| /* First compute all lambdas */ |
| do{ |
| progress = 0; |
| for(rp=lemp->rule; rp; rp=rp->next){ |
| if( rp->lhs->lambda ) continue; |
| for(i=0; i<rp->nrhs; i++){ |
| struct symbol *sp = rp->rhs[i]; |
| assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE ); |
| if( sp->lambda==LEMON_FALSE ) break; |
| } |
| if( i==rp->nrhs ){ |
| rp->lhs->lambda = LEMON_TRUE; |
| progress = 1; |
| } |
| } |
| }while( progress ); |
| |
| /* Now compute all first sets */ |
| do{ |
| struct symbol *s1, *s2; |
| progress = 0; |
| for(rp=lemp->rule; rp; rp=rp->next){ |
| s1 = rp->lhs; |
| for(i=0; i<rp->nrhs; i++){ |
| s2 = rp->rhs[i]; |
| if( s2->type==TERMINAL ){ |
| progress += SetAdd(s1->firstset,s2->index); |
| break; |
| }else if( s2->type==MULTITERMINAL ){ |
| for(j=0; j<s2->nsubsym; j++){ |
| progress += SetAdd(s1->firstset,s2->subsym[j]->index); |
| } |
| break; |
| }else if( s1==s2 ){ |
| if( s1->lambda==LEMON_FALSE ) break; |
| }else{ |
| progress += SetUnion(s1->firstset,s2->firstset); |
| if( s2->lambda==LEMON_FALSE ) break; |
| } |
| } |
| } |
| }while( progress ); |
| return; |
| } |
| |
| /* Compute all LR(0) states for the grammar. Links |
| ** are added to between some states so that the LR(1) follow sets |
| ** can be computed later. |
| */ |
| PRIVATE struct state *getstate(struct lemon *); /* forward reference */ |
| void FindStates(struct lemon *lemp) |
| { |
| struct symbol *sp; |
| struct rule *rp; |
| |
| Configlist_init(); |
| |
| /* Find the start symbol */ |
| if( lemp->start ){ |
| sp = Symbol_find(lemp->start); |
| if( sp==0 ){ |
| ErrorMsg(lemp->filename,0, |
| "The specified start symbol \"%s\" is not " |
| "in a nonterminal of the grammar. \"%s\" will be used as the start " |
| "symbol instead.",lemp->start,lemp->startRule->lhs->name); |
| lemp->errorcnt++; |
| sp = lemp->startRule->lhs; |
| } |
| }else{ |
| sp = lemp->startRule->lhs; |
| } |
| |
| /* Make sure the start symbol doesn't occur on the right-hand side of |
| ** any rule. Report an error if it does. (YACC would generate a new |
| ** start symbol in this case.) */ |
| for(rp=lemp->rule; rp; rp=rp->next){ |
| int i; |
| for(i=0; i<rp->nrhs; i++){ |
| if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */ |
| ErrorMsg(lemp->filename,0, |
| "The start symbol \"%s\" occurs on the " |
| "right-hand side of a rule. This will result in a parser which " |
| "does not work properly.",sp->name); |
| lemp->errorcnt++; |
| } |
| } |
| } |
| |
| /* The basis configuration set for the first state |
| ** is all rules which have the start symbol as their |
| ** left-hand side */ |
| for(rp=sp->rule; rp; rp=rp->nextlhs){ |
| struct config *newcfp; |
| rp->lhsStart = 1; |
| newcfp = Configlist_addbasis(rp,0); |
| SetAdd(newcfp->fws,0); |
| } |
| |
| /* Compute the first state. All other states will be |
| ** computed automatically during the computation of the first one. |
| ** The returned pointer to the first state is not used. */ |
| (void)getstate(lemp); |
| return; |
| } |
| |
| /* Return a pointer to a state which is described by the configuration |
| ** list which has been built from calls to Configlist_add. |
| */ |
| PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */ |
| PRIVATE struct state *getstate(struct lemon *lemp) |
| { |
| struct config *cfp, *bp; |
| struct state *stp; |
| |
| /* Extract the sorted basis of the new state. The basis was constructed |
| ** by prior calls to "Configlist_addbasis()". */ |
| Configlist_sortbasis(); |
| bp = Configlist_basis(); |
| |
| /* Get a state with the same basis */ |
| stp = State_find(bp); |
| if( stp ){ |
| /* A state with the same basis already exists! Copy all the follow-set |
| ** propagation links from the state under construction into the |
| ** preexisting state, then return a pointer to the preexisting state */ |
| struct config *x, *y; |
| for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){ |
| Plink_copy(&y->bplp,x->bplp); |
| Plink_delete(x->fplp); |
| x->fplp = x->bplp = 0; |
| } |
| cfp = Configlist_return(); |
| Configlist_eat(cfp); |
| }else{ |
| /* This really is a new state. Construct all the details */ |
| Configlist_closure(lemp); /* Compute the configuration closure */ |
| Configlist_sort(); /* Sort the configuration closure */ |
| cfp = Configlist_return(); /* Get a pointer to the config list */ |
| stp = State_new(); /* A new state structure */ |
| MemoryCheck(stp); |
| stp->bp = bp; /* Remember the configuration basis */ |
| stp->cfp = cfp; /* Remember the configuration closure */ |
| stp->statenum = lemp->nstate++; /* Every state gets a sequence number */ |
| stp->ap = 0; /* No actions, yet. */ |
| State_insert(stp,stp->bp); /* Add to the state table */ |
| buildshifts(lemp,stp); /* Recursively compute successor states */ |
| } |
| return stp; |
| } |
| |
| /* |
| ** Return true if two symbols are the same. |
| */ |
| int same_symbol(struct symbol *a, struct symbol *b) |
| { |
| int i; |
| if( a==b ) return 1; |
| if( a->type!=MULTITERMINAL ) return 0; |
| if( b->type!=MULTITERMINAL ) return 0; |
| if( a->nsubsym!=b->nsubsym ) return 0; |
| for(i=0; i<a->nsubsym; i++){ |
| if( a->subsym[i]!=b->subsym[i] ) return 0; |
| } |
| return 1; |
| } |
| |
| /* Construct all successor states to the given state. A "successor" |
| ** state is any state which can be reached by a shift action. |
| */ |
| PRIVATE void buildshifts(struct lemon *lemp, struct state *stp) |
| { |
| struct config *cfp; /* For looping thru the config closure of "stp" */ |
| struct config *bcfp; /* For the inner loop on config closure of "stp" */ |
| struct config *newcfg; /* */ |
| struct symbol *sp; /* Symbol following the dot in configuration "cfp" */ |
| struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */ |
| struct state *newstp; /* A pointer to a successor state */ |
| |
| /* Each configuration becomes complete after it contibutes to a successor |
| ** state. Initially, all configurations are incomplete */ |
| for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE; |
| |
| /* Loop through all configurations of the state "stp" */ |
| for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */ |
| if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */ |
| Configlist_reset(); /* Reset the new config set */ |
| sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */ |
| |
| /* For every configuration in the state "stp" which has the symbol "sp" |
| ** following its dot, add the same configuration to the basis set under |
| ** construction but with the dot shifted one symbol to the right. */ |
| for(bcfp=cfp; bcfp; bcfp=bcfp->next){ |
| if( bcfp->status==COMPLETE ) continue; /* Already used */ |
| if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */ |
| bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */ |
| if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */ |
| bcfp->status = COMPLETE; /* Mark this config as used */ |
| newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1); |
| Plink_add(&newcfg->bplp,bcfp); |
| } |
| |
| /* Get a pointer to the state described by the basis configuration set |
| ** constructed in the preceding loop */ |
| newstp = getstate(lemp); |
| |
| /* The state "newstp" is reached from the state "stp" by a shift action |
| ** on the symbol "sp" */ |
| if( sp->type==MULTITERMINAL ){ |
| int i; |
| for(i=0; i<sp->nsubsym; i++){ |
| Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp); |
| } |
| }else{ |
| Action_add(&stp->ap,SHIFT,sp,(char *)newstp); |
| } |
| } |
| } |
| |
| /* |
| ** Construct the propagation links |
| */ |
| void FindLinks(struct lemon *lemp) |
| { |
| int i; |
| struct config *cfp, *other; |
| struct state *stp; |
| struct plink *plp; |
| |
| /* Housekeeping detail: |
| ** Add to every propagate link a pointer back to the state to |
| ** which the link is attached. */ |
| for(i=0; i<lemp->nstate; i++){ |
| stp = lemp->sorted[i]; |
| for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| cfp->stp = stp; |
| } |
| } |
| |
| /* Convert all backlinks into forward links. Only the forward |
| ** links are used in the follow-set computation. */ |
| for(i=0; i<lemp->nstate; i++){ |
| stp = lemp->sorted[i]; |
| for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| for(plp=cfp->bplp; plp; plp=plp->next){ |
| other = plp->cfp; |
| Plink_add(&other->fplp,cfp); |
| } |
| } |
| } |
| } |
| |
| /* Compute all followsets. |
| ** |
| ** A followset is the set of all symbols which can come immediately |
| ** after a configuration. |
| */ |
| void FindFollowSets(struct lemon *lemp) |
| { |
| int i; |
| struct config *cfp; |
| struct plink *plp; |
| int progress; |
| int change; |
| |
| for(i=0; i<lemp->nstate; i++){ |
| for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ |
| cfp->status = INCOMPLETE; |
| } |
| } |
| |
| do{ |
| progress = 0; |
| for(i=0; i<lemp->nstate; i++){ |
| for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ |
| if( cfp->status==COMPLETE ) continue; |
| for(plp=cfp->fplp; plp; plp=plp->next){ |
| change = SetUnion(plp->cfp->fws,cfp->fws); |
| if( change ){ |
| plp->cfp->status = INCOMPLETE; |
| progress = 1; |
| } |
| } |
| cfp->status = COMPLETE; |
| } |
| } |
| }while( progress ); |
| } |
| |
| static int resolve_conflict(struct action *,struct action *); |
| |
| /* Compute the reduce actions, and resolve conflicts. |
| */ |
| void FindActions(struct lemon *lemp) |
| { |
| int i,j; |
| struct config *cfp; |
| struct state *stp; |
| struct symbol *sp; |
| struct rule *rp; |
| |
| /* Add all of the reduce actions |
| ** A reduce action is added for each element of the followset of |
| ** a configuration which has its dot at the extreme right. |
| */ |
| for(i=0; i<lemp->nstate; i++){ /* Loop over all states */ |
| stp = lemp->sorted[i]; |
| for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */ |
| if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */ |
| for(j=0; j<lemp->nterminal; j++){ |
| if( SetFind(cfp->fws,j) ){ |
| /* Add a reduce action to the state "stp" which will reduce by the |
| ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */ |
| Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp); |
| } |
| } |
| } |
| } |
| } |
| |
| /* Add the accepting token */ |
| if( lemp->start ){ |
| sp = Symbol_find(lemp->start); |
| if( sp==0 ) sp = lemp->startRule->lhs; |
| }else{ |
| sp = lemp->startRule->lhs; |
| } |
| /* Add to the first state (which is always the starting state of the |
| ** finite state machine) an action to ACCEPT if the lookahead is the |
| ** start nonterminal. */ |
| Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0); |
| |
| /* Resolve conflicts */ |
| for(i=0; i<lemp->nstate; i++){ |
| struct action *ap, *nap; |
| stp = lemp->sorted[i]; |
| /* assert( stp->ap ); */ |
| stp->ap = Action_sort(stp->ap); |
| for(ap=stp->ap; ap && ap->next; ap=ap->next){ |
| for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){ |
| /* The two actions "ap" and "nap" have the same lookahead. |
| ** Figure out which one should be used */ |
| lemp->nconflict += resolve_conflict(ap,nap); |
| } |
| } |
| } |
| |
| /* Report an error for each rule that can never be reduced. */ |
| for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE; |
| for(i=0; i<lemp->nstate; i++){ |
| struct action *ap; |
| for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){ |
| if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE; |
| } |
| } |
| for(rp=lemp->rule; rp; rp=rp->next){ |
| if( rp->canReduce ) continue; |
| ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n"); |
| lemp->errorcnt++; |
| } |
| } |
| |
| /* Resolve a conflict between the two given actions. If the |
| ** conflict can't be resolved, return non-zero. |
| ** |
| ** NO LONGER TRUE: |
| ** To resolve a conflict, first look to see if either action |
| ** is on an error rule. In that case, take the action which |
| ** is not associated with the error rule. If neither or both |
| ** actions are associated with an error rule, then try to |
| ** use precedence to resolve the conflict. |
| ** |
| ** If either action is a SHIFT, then it must be apx. This |
| ** function won't work if apx->type==REDUCE and apy->type==SHIFT. |
| */ |
| static int resolve_conflict( |
| struct action *apx, |
| struct action *apy |
| ){ |
| struct symbol *spx, *spy; |
| int errcnt = 0; |
| assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */ |
| if( apx->type==SHIFT && apy->type==SHIFT ){ |
| apy->type = SSCONFLICT; |
| errcnt++; |
| } |
| if( apx->type==SHIFT && apy->type==REDUCE ){ |
| spx = apx->sp; |
| spy = apy->x.rp->precsym; |
| if( spy==0 || spx->prec<0 || spy->prec<0 ){ |
| /* Not enough precedence information. */ |
| apy->type = SRCONFLICT; |
| errcnt++; |
| }else if( spx->prec>spy->prec ){ /* higher precedence wins */ |
| apy->type = RD_RESOLVED; |
| }else if( spx->prec<spy->prec ){ |
| apx->type = SH_RESOLVED; |
| }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */ |
| apy->type = RD_RESOLVED; /* associativity */ |
| }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */ |
| apx->type = SH_RESOLVED; |
| }else{ |
| assert( spx->prec==spy->prec && spx->assoc==NONE ); |
| apx->type = ERROR; |
| } |
| }else if( apx->type==REDUCE && apy->type==REDUCE ){ |
| spx = apx->x.rp->precsym; |
| spy = apy->x.rp->precsym; |
| if( spx==0 || spy==0 || spx->prec<0 || |
| spy->prec<0 || spx->prec==spy->prec ){ |
| apy->type = RRCONFLICT; |
| errcnt++; |
| }else if( spx->prec>spy->prec ){ |
| apy->type = RD_RESOLVED; |
| }else if( spx->prec<spy->prec ){ |
| apx->type = RD_RESOLVED; |
| } |
| }else{ |
| assert( |
| apx->type==SH_RESOLVED || |
| apx->type==RD_RESOLVED || |
| apx->type==SSCONFLICT || |
| apx->type==SRCONFLICT || |
| apx->type==RRCONFLICT || |
| apy->type==SH_RESOLVED || |
| apy->type==RD_RESOLVED || |
| apy->type==SSCONFLICT || |
| apy->type==SRCONFLICT || |
| apy->type==RRCONFLICT |
| ); |
| /* The REDUCE/SHIFT case cannot happen because SHIFTs come before |
| ** REDUCEs on the list. If we reach this point it must be because |
| ** the parser conflict had already been resolved. */ |
| } |
| return errcnt; |
| } |
| /********************* From the file "configlist.c" *************************/ |
| /* |
| ** Routines to processing a configuration list and building a state |
| ** in the LEMON parser generator. |
| */ |
| |
| static struct config *freelist = 0; /* List of free configurations */ |
| static struct config *current = 0; /* Top of list of configurations */ |
| static struct config **currentend = 0; /* Last on list of configs */ |
| static struct config *basis = 0; /* Top of list of basis configs */ |
| static struct config **basisend = 0; /* End of list of basis configs */ |
| |
| /* Return a pointer to a new configuration */ |
| PRIVATE struct config *newconfig(void){ |
| struct config *newcfg; |
| if( freelist==0 ){ |
| int i; |
| int amt = 3; |
| freelist = (struct config *)calloc( amt, sizeof(struct config) ); |
| if( freelist==0 ){ |
| fprintf(stderr,"Unable to allocate memory for a new configuration."); |
| exit(1); |
| } |
| for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1]; |
| freelist[amt-1].next = 0; |
| } |
| newcfg = freelist; |
| freelist = freelist->next; |
| return newcfg; |
| } |
| |
| /* The configuration "old" is no longer used */ |
| PRIVATE void deleteconfig(struct config *old) |
| { |
| old->next = freelist; |
| freelist = old; |
| } |
| |
| /* Initialized the configuration list builder */ |
| void Configlist_init(void){ |
| current = 0; |
| currentend = ¤t; |
| basis = 0; |
| basisend = &basis; |
| Configtable_init(); |
| return; |
| } |
| |
| /* Initialized the configuration list builder */ |
| void Configlist_reset(void){ |
| current = 0; |
| currentend = ¤t; |
| basis = 0; |
| basisend = &basis; |
| Configtable_clear(0); |
| return; |
| } |
| |
| /* Add another configuration to the configuration list */ |
| struct config *Configlist_add( |
| struct rule *rp, /* The rule */ |
| int dot /* Index into the RHS of the rule where the dot goes */ |
| ){ |
| struct config *cfp, model; |
| |
| assert( currentend!=0 ); |
| model.rp = rp; |
| model.dot = dot; |
| cfp = Configtable_find(&model); |
| if( cfp==0 ){ |
| cfp = newconfig(); |
| cfp->rp = rp; |
| cfp->dot = dot; |
| cfp->fws = SetNew(); |
| cfp->stp = 0; |
| cfp->fplp = cfp->bplp = 0; |
| cfp->next = 0; |
| cfp->bp = 0; |
| *currentend = cfp; |
| currentend = &cfp->next; |
| Configtable_insert(cfp); |
| } |
| return cfp; |
| } |
| |
| /* Add a basis configuration to the configuration list */ |
| struct config *Configlist_addbasis(struct rule *rp, int dot) |
| { |
| struct config *cfp, model; |
| |
| assert( basisend!=0 ); |
| assert( currentend!=0 ); |
| model.rp = rp; |
| model.dot = dot; |
| cfp = Configtable_find(&model); |
| if( cfp==0 ){ |
| cfp = newconfig(); |
| cfp->rp = rp; |
| cfp->dot = dot; |
| cfp->fws = SetNew(); |
| cfp->stp = 0; |
| cfp->fplp = cfp->bplp = 0; |
| cfp->next = 0; |
| cfp->bp = 0; |
| *currentend = cfp; |
| currentend = &cfp->next; |
| *basisend = cfp; |
| basisend = &cfp->bp; |
| Configtable_insert(cfp); |
| } |
| return cfp; |
| } |
| |
| /* Compute the closure of the configuration list */ |
| void Configlist_closure(struct lemon *lemp) |
| { |
| struct config *cfp, *newcfp; |
| struct rule *rp, *newrp; |
| struct symbol *sp, *xsp; |
| int i, dot; |
| |
| assert( currentend!=0 ); |
| for(cfp=current; cfp; cfp=cfp->next){ |
| rp = cfp->rp; |
| dot = cfp->dot; |
| if( dot>=rp->nrhs ) continue; |
| sp = rp->rhs[dot]; |
| if( sp->type==NONTERMINAL ){ |
| if( sp->rule==0 && sp!=lemp->errsym ){ |
| ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.", |
| sp->name); |
| lemp->errorcnt++; |
| } |
| for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){ |
| newcfp = Configlist_add(newrp,0); |
| for(i=dot+1; i<rp->nrhs; i++){ |
| xsp = rp->rhs[i]; |
| if( xsp->type==TERMINAL ){ |
| SetAdd(newcfp->fws,xsp->index); |
| break; |
| }else if( xsp->type==MULTITERMINAL ){ |
| int k; |
| for(k=0; k<xsp->nsubsym; k++){ |
| SetAdd(newcfp->fws, xsp->subsym[k]->index); |
| } |
| break; |
| }else{ |
| SetUnion(newcfp->fws,xsp->firstset); |
| if( xsp->lambda==LEMON_FALSE ) break; |
| } |
| } |
| if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp); |
| } |
| } |
| } |
| return; |
| } |
| |
| /* Sort the configuration list */ |
| void Configlist_sort(void){ |
| current = (struct config*)msort((char*)current,(char**)&(current->next), |
| Configcmp); |
| currentend = 0; |
| return; |
| } |
| |
| /* Sort the basis configuration list */ |
| void Configlist_sortbasis(void){ |
| basis = (struct config*)msort((char*)current,(char**)&(current->bp), |
| Configcmp); |
| basisend = 0; |
| return; |
| } |
| |
| /* Return a pointer to the head of the configuration list and |
| ** reset the list */ |
| struct config *Configlist_return(void){ |
| struct config *old; |
| old = current; |
| current = 0; |
| currentend = 0; |
| return old; |
| } |
| |
| /* Return a pointer to the head of the configuration list and |
| ** reset the list */ |
| struct config *Configlist_basis(void){ |
| struct config *old; |
| old = basis; |
| basis = 0; |
| basisend = 0; |
| return old; |
| } |
| |
| /* Free all elements of the given configuration list */ |
| void Configlist_eat(struct config *cfp) |
| { |
| struct config *nextcfp; |
| for(; cfp; cfp=nextcfp){ |
| nextcfp = cfp->next; |
| assert( cfp->fplp==0 ); |
| assert( cfp->bplp==0 ); |
| if( cfp->fws ) SetFree(cfp->fws); |
| deleteconfig(cfp); |
| } |
| return; |
| } |
| /***************** From the file "error.c" *********************************/ |
| /* |
| ** Code for printing error message. |
| */ |
| |
| void ErrorMsg(const char *filename, int lineno, const char *format, ...){ |
| va_list ap; |
| fprintf(stderr, "%s:%d: ", filename, lineno); |
| va_start(ap, format); |
| vfprintf(stderr,format,ap); |
| va_end(ap); |
| fprintf(stderr, "\n"); |
| } |
| /**************** From the file "main.c" ************************************/ |
| /* |
| ** Main program file for the LEMON parser generator. |
| */ |
| |
| /* Report an out-of-memory condition and abort. This function |
| ** is used mostly by the "MemoryCheck" macro in struct.h |
| */ |
| void memory_error(void){ |
| fprintf(stderr,"Out of memory. Aborting...\n"); |
| exit(1); |
| } |
| |
| static int nDefine = 0; /* Number of -D options on the command line */ |
| static char **azDefine = 0; /* Name of the -D macros */ |
| |
| /* This routine is called with the argument to each -D command-line option. |
| ** Add the macro defined to the azDefine array. |
| */ |
| static void handle_D_option(char *z){ |
| char **paz; |
| nDefine++; |
| azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine); |
| if( azDefine==0 ){ |
| fprintf(stderr,"out of memory\n"); |
| exit(1); |
| } |
| paz = &azDefine[nDefine-1]; |
| *paz = (char *) malloc( lemonStrlen(z)+1 ); |
| if( *paz==0 ){ |
| fprintf(stderr,"out of memory\n"); |
| exit(1); |
| } |
| lemon_strcpy(*paz, z); |
| for(z=*paz; *z && *z!='='; z++){} |
| *z = 0; |
| } |
| |
| /* Rember the name of the output directory |
| */ |
| static char *outputDir = NULL; |
| static void handle_d_option(char *z){ |
| outputDir = (char *) malloc( lemonStrlen(z)+1 ); |
| if( outputDir==0 ){ |
| fprintf(stderr,"out of memory\n"); |
| exit(1); |
| } |
| lemon_strcpy(outputDir, z); |
| } |
| |
| static char *user_templatename = NULL; |
| static void handle_T_option(char *z){ |
| user_templatename = (char *) malloc( lemonStrlen(z)+1 ); |
| if( user_templatename==0 ){ |
| memory_error(); |
| } |
| lemon_strcpy(user_templatename, z); |
| } |
| |
| /* Merge together to lists of rules ordered by rule.iRule */ |
| static struct rule *Rule_merge(struct rule *pA, struct rule *pB){ |
| struct rule *pFirst = 0; |
| struct rule **ppPrev = &pFirst; |
| while( pA && pB ){ |
| if( pA->iRule<pB->iRule ){ |
| *ppPrev = pA; |
| ppPrev = &pA->next; |
| pA = pA->next; |
| }else{ |
| *ppPrev = pB; |
| ppPrev = &pB->next; |
| pB = pB->next; |
| } |
| } |
| if( pA ){ |
| *ppPrev = pA; |
| }else{ |
| *ppPrev = pB; |
| } |
| return pFirst; |
| } |
| |
| /* |
| ** Sort a list of rules in order of increasing iRule value |
| */ |
| static struct rule *Rule_sort(struct rule *rp){ |
| int i; |
| struct rule *pNext; |
| struct rule *x[32]; |
| memset(x, 0, sizeof(x)); |
| while( rp ){ |
| pNext = rp->next; |
| rp->next = 0; |
| for(i=0; i<sizeof(x)/sizeof(x[0]) && x[i]; i++){ |
| rp = Rule_merge(x[i], rp); |
| x[i] = 0; |
| } |
| x[i] = rp; |
| rp = pNext; |
| } |
| rp = 0; |
| for(i=0; i<sizeof(x)/sizeof(x[0]); i++){ |
| rp = Rule_merge(x[i], rp); |
| } |
| return rp; |
| } |
| |
| /* forward reference */ |
| static const char *minimum_size_type(int lwr, int upr, int *pnByte); |
| |
| /* Print a single line of the "Parser Stats" output |
| */ |
| static void stats_line(const char *zLabel, int iValue){ |
| int nLabel = lemonStrlen(zLabel); |
| printf(" %s%.*s %5d\n", zLabel, |
| 35-nLabel, "................................", |
| iValue); |
| } |
| |
| /* The main program. Parse the command line and do it... */ |
| int main(int argc, char **argv) |
| { |
| static int version = 0; |
| static int rpflag = 0; |
| static int basisflag = 0; |
| static int compress = 0; |
| static int quiet = 0; |
| static int statistics = 0; |
| static int mhflag = 0; |
| static int nolinenosflag = 0; |
| static int noResort = 0; |
| static int sqlFlag = 0; |
| |
| static struct s_options options[] = { |
| {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."}, |
| {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."}, |
| {OPT_FSTR, "d", (char*)&handle_d_option, "Output directory. Default '.'"}, |
| {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."}, |
| {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"}, |
| {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."}, |
| {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"}, |
| {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."}, |
| {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."}, |
| {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"}, |
| {OPT_FLAG, "p", (char*)&showPrecedenceConflict, |
| "Show conflicts resolved by precedence rules"}, |
| {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."}, |
| {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"}, |
| {OPT_FLAG, "s", (char*)&statistics, |
| "Print parser stats to standard output."}, |
| {OPT_FLAG, "S", (char*)&sqlFlag, |
| "Generate the *.sql file describing the parser tables."}, |
| {OPT_FLAG, "x", (char*)&version, "Print the version number."}, |
| {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."}, |
| {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"}, |
| {OPT_FLAG,0,0,0} |
| }; |
| int i; |
| int exitcode; |
| struct lemon lem; |
| struct rule *rp; |
| |
| OptInit(argv,options,stderr); |
| if( version ){ |
| printf("Lemon version 1.0\n"); |
| exit(0); |
| } |
| if( OptNArgs()!=1 ){ |
| fprintf(stderr,"Exactly one filename argument is required.\n"); |
| exit(1); |
| } |
| memset(&lem, 0, sizeof(lem)); |
| lem.errorcnt = 0; |
| |
| /* Initialize the machine */ |
| Strsafe_init(); |
| Symbol_init(); |
| State_init(); |
| lem.argv0 = argv[0]; |
| lem.filename = OptArg(0); |
| lem.basisflag = basisflag; |
| lem.nolinenosflag = nolinenosflag; |
| Symbol_new("$"); |
| |
| /* Parse the input file */ |
| Parse(&lem); |
| if( lem.errorcnt ) exit(lem.errorcnt); |
| if( lem.nrule==0 ){ |
| fprintf(stderr,"Empty grammar.\n"); |
| exit(1); |
| } |
| lem.errsym = Symbol_find("error"); |
| |
| /* Count and index the symbols of the grammar */ |
| Symbol_new("{default}"); |
| lem.nsymbol = Symbol_count(); |
| lem.symbols = Symbol_arrayof(); |
| for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i; |
| qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp); |
| for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i; |
| while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; } |
| assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 ); |
| lem.nsymbol = i - 1; |
| for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++); |
| lem.nterminal = i; |
| |
| /* Assign sequential rule numbers. Start with 0. Put rules that have no |
| ** reduce action C-code associated with them last, so that the switch() |
| ** statement that selects reduction actions will have a smaller jump table. |
| */ |
| for(i=0, rp=lem.rule; rp; rp=rp->next){ |
| rp->iRule = rp->code ? i++ : -1; |
| } |
| lem.nruleWithAction = i; |
| for(rp=lem.rule; rp; rp=rp->next){ |
| if( rp->iRule<0 ) rp->iRule = i++; |
| } |
| lem.startRule = lem.rule; |
| lem.rule = Rule_sort(lem.rule); |
| |
| /* Generate a reprint of the grammar, if requested on the command line */ |
| if( rpflag ){ |
| Reprint(&lem); |
| }else{ |
| /* Initialize the size for all follow and first sets */ |
| SetSize(lem.nterminal+1); |
| |
| /* Find the precedence for every production rule (that has one) */ |
| FindRulePrecedences(&lem); |
| |
| /* Compute the lambda-nonterminals and the first-sets for every |
| ** nonterminal */ |
| FindFirstSets(&lem); |
| |
| /* Compute all LR(0) states. Also record follow-set propagation |
| ** links so that the follow-set can be computed later */ |
| lem.nstate = 0; |
| FindStates(&lem); |
| lem.sorted = State_arrayof(); |
| |
| /* Tie up loose ends on the propagation links */ |
| FindLinks(&lem); |
| |
| /* Compute the follow set of every reducible configuration */ |
| FindFollowSets(&lem); |
| |
| /* Compute the action tables */ |
| FindActions(&lem); |
| |
| /* Compress the action tables */ |
| if( compress==0 ) CompressTables(&lem); |
| |
| /* Reorder and renumber the states so that states with fewer choices |
| ** occur at the end. This is an optimization that helps make the |
| ** generated parser tables smaller. */ |
| if( noResort==0 ) ResortStates(&lem); |
| |
| /* Generate a report of the parser generated. (the "y.output" file) */ |
| if( !quiet ) ReportOutput(&lem); |
| |
| /* Generate the source code for the parser */ |
| ReportTable(&lem, mhflag, sqlFlag); |
| |
| /* Produce a header file for use by the scanner. (This step is |
| ** omitted if the "-m" option is used because makeheaders will |
| ** generate the file for us.) */ |
| if( !mhflag ) ReportHeader(&lem); |
| } |
| if( statistics ){ |
| printf("Parser statistics:\n"); |
| stats_line("terminal symbols", lem.nterminal); |
| stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal); |
| stats_line("total symbols", lem.nsymbol); |
| stats_line("rules", lem.nrule); |
| stats_line("states", lem.nxstate); |
| stats_line("conflicts", lem.nconflict); |
| stats_line("action table entries", lem.nactiontab); |
| stats_line("lookahead table entries", lem.nlookaheadtab); |
| stats_line("total table size (bytes)", lem.tablesize); |
| } |
| if( lem.nconflict > 0 ){ |
| fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict); |
| } |
| |
| /* return 0 on success, 1 on failure. */ |
| exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0; |
| exit(exitcode); |
| return (exitcode); |
| } |
| /******************** From the file "msort.c" *******************************/ |
| /* |
| ** A generic merge-sort program. |
| ** |
| ** USAGE: |
| ** Let "ptr" be a pointer to some structure which is at the head of |
| ** a null-terminated list. Then to sort the list call: |
| ** |
| ** ptr = msort(ptr,&(ptr->next),cmpfnc); |
| ** |
| ** In the above, "cmpfnc" is a pointer to a function which compares |
| ** two instances of the structure and returns an integer, as in |
| ** strcmp. The second argument is a pointer to the pointer to the |
| ** second element of the linked list. This address is used to compute |
| ** the offset to the "next" field within the structure. The offset to |
| ** the "next" field must be constant for all structures in the list. |
| ** |
| ** The function returns a new pointer which is the head of the list |
| ** after sorting. |
| ** |
| ** ALGORITHM: |
| ** Merge-sort. |
| */ |
| |
| /* |
| ** Return a pointer to the next structure in the linked list. |
| */ |
| #define NEXT(A) (*(char**)(((char*)A)+offset)) |
| |
| /* |
| ** Inputs: |
| ** a: A sorted, null-terminated linked list. (May be null). |
| ** b: A sorted, null-terminated linked list. (May be null). |
| ** cmp: A pointer to the comparison function. |
| ** offset: Offset in the structure to the "next" field. |
| ** |
| ** Return Value: |
| ** A pointer to the head of a sorted list containing the elements |
| ** of both a and b. |
| ** |
| ** Side effects: |
| ** The "next" pointers for elements in the lists a and b are |
| ** changed. |
| */ |
| static char *merge( |
| char *a, |
| char *b, |
| int (*cmp)(const char*,const char*), |
| int offset |
| ){ |
| char *ptr, *head; |
| |
| if( a==0 ){ |
| head = b; |
| }else if( b==0 ){ |
| head = a; |
| }else{ |
| if( (*cmp)(a,b)<=0 ){ |
| ptr = a; |
| a = NEXT(a); |
| }else{ |
| ptr = b; |
| b = NEXT(b); |
| } |
| head = ptr; |
| while( a && b ){ |
| if( (*cmp)(a,b)<=0 ){ |
| NEXT(ptr) = a; |
| ptr = a; |
| a = NEXT(a); |
| }else{ |
| NEXT(ptr) = b; |
| ptr = b; |
| b = NEXT(b); |
| } |
| } |
| if( a ) NEXT(ptr) = a; |
| else NEXT(ptr) = b; |
| } |
| return head; |
| } |
| |
| /* |
| ** Inputs: |
| ** list: Pointer to a singly-linked list of structures. |
| ** next: Pointer to pointer to the second element of the list. |
| ** cmp: A comparison function. |
| ** |
| ** Return Value: |
| ** A pointer to the head of a sorted list containing the elements |
| ** orginally in list. |
| ** |
| ** Side effects: |
| ** The "next" pointers for elements in list are changed. |
| */ |
| #define LISTSIZE 30 |
| static char *msort( |
| char *list, |
| char **next, |
| int (*cmp)(const char*,const char*) |
| ){ |
| unsigned long offset; |
| char *ep; |
| char *set[LISTSIZE]; |
| int i; |
| offset = (unsigned long)((char*)next - (char*)list); |
| for(i=0; i<LISTSIZE; i++) set[i] = 0; |
| while( list ){ |
| ep = list; |
| list = NEXT(list); |
| NEXT(ep) = 0; |
| for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){ |
| ep = merge(ep,set[i],cmp,offset); |
| set[i] = 0; |
| } |
| set[i] = ep; |
| } |
| ep = 0; |
| for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset); |
| return ep; |
| } |
| /************************ From the file "option.c" **************************/ |
| static char **g_argv; |
| static struct s_options *op; |
| static FILE *errstream; |
| |
| #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0) |
| |
| /* |
| ** Print the command line with a carrot pointing to the k-th character |
| ** of the n-th field. |
| */ |
| static void errline(int n, int k, FILE *err) |
| { |
| int spcnt, i; |
| if( g_argv[0] ) fprintf(err,"%s",g_argv[0]); |
| spcnt = lemonStrlen(g_argv[0]) + 1; |
| for(i=1; i<n && g_argv[i]; i++){ |
| fprintf(err," %s",g_argv[i]); |
| spcnt += lemonStrlen(g_argv[i])+1; |
| } |
| spcnt += k; |
| for(; g_argv[i]; i++) fprintf(err," %s",g_argv[i]); |
| if( spcnt<20 ){ |
| fprintf(err,"\n%*s^-- here\n",spcnt,""); |
| }else{ |
| fprintf(err,"\n%*shere --^\n",spcnt-7,""); |
| } |
| } |
| |
| /* |
| ** Return the index of the N-th non-switch argument. Return -1 |
| ** if N is out of range. |
| */ |
| static int argindex(int n) |
| { |
| int i; |
| int dashdash = 0; |
| if( g_argv!=0 && *g_argv!=0 ){ |
| for(i=1; g_argv[i]; i++){ |
| if( dashdash || !ISOPT(g_argv[i]) ){ |
| if( n==0 ) return i; |
| n--; |
| } |
| if( strcmp(g_argv[i],"--")==0 ) dashdash = 1; |
| } |
| } |
| return -1; |
| } |
| |
| static char emsg[] = "Command line syntax error: "; |
| |
| /* |
| ** Process a flag command line argument. |
| */ |
| static int handleflags(int i, FILE *err) |
| { |
| int v; |
| int errcnt = 0; |
| int j; |
| for(j=0; op[j].label; j++){ |
| if( strncmp(&g_argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break; |
| } |
| v = g_argv[i][0]=='-' ? 1 : 0; |
| if( op[j].label==0 ){ |
| if( err ){ |
| fprintf(err,"%sundefined option.\n",emsg); |
| errline(i,1,err); |
| } |
| errcnt++; |
| }else if( op[j].arg==0 ){ |
| /* Ignore this option */ |
| }else if( op[j].type==OPT_FLAG ){ |
| *((int*)op[j].arg) = v; |
| }else if( op[j].type==OPT_FFLAG ){ |
| (*(void(*)(int))(op[j].arg))(v); |
| }else if( op[j].type==OPT_FSTR ){ |
| (*(void(*)(char *))(op[j].arg))(&g_argv[i][2]); |
| }else{ |
| if( err ){ |
| fprintf(err,"%smissing argument on switch.\n",emsg); |
| errline(i,1,err); |
| } |
| errcnt++; |
| } |
| return errcnt; |
| } |
| |
| /* |
| ** Process a command line switch which has an argument. |
| */ |
| static int handleswitch(int i, FILE *err) |
| { |
| int lv = 0; |
| double dv = 0.0; |
| char *sv = 0, *end; |
| char *cp; |
| int j; |
| int errcnt = 0; |
| cp = strchr(g_argv[i],'='); |
| assert( cp!=0 ); |
| *cp = 0; |
| for(j=0; op[j].label; j++){ |
| if( strcmp(g_argv[i],op[j].label)==0 ) break; |
| } |
| *cp = '='; |
| if( op[j].label==0 ){ |
| if( err ){ |
| fprintf(err,"%sundefined option.\n",emsg); |
| errline(i,0,err); |
| } |
| errcnt++; |
| }else{ |
| cp++; |
| switch( op[j].type ){ |
| case OPT_FLAG: |
| case OPT_FFLAG: |
| if( err ){ |
| fprintf(err,"%soption requires an argument.\n",emsg); |
| errline(i,0,err); |
| } |
| errcnt++; |
| break; |
| case OPT_DBL: |
| case OPT_FDBL: |
| dv = strtod(cp,&end); |
| if( *end ){ |
| if( err ){ |
| fprintf(err, |
| "%sillegal character in floating-point argument.\n",emsg); |
| errline(i,(int)((char*)end-(char*)g_argv[i]),err); |
| } |
| errcnt++; |
| } |
| break; |
| case OPT_INT: |
| case OPT_FINT: |
| lv = strtol(cp,&end,0); |
| if( *end ){ |
| if( err ){ |
| fprintf(err,"%sillegal character in integer argument.\n",emsg); |
| errline(i,(int)((char*)end-(char*)g_argv[i]),err); |
| } |
| errcnt++; |
| } |
| break; |
| case OPT_STR: |
| case OPT_FSTR: |
| sv = cp; |
| break; |
| } |
| switch( op[j].type ){ |
| case OPT_FLAG: |
| case OPT_FFLAG: |
| break; |
| case OPT_DBL: |
| *(double*)(op[j].arg) = dv; |
| break; |
| case OPT_FDBL: |
| (*(void(*)(double))(op[j].arg))(dv); |
| break; |
| case OPT_INT: |
| *(int*)(op[j].arg) = lv; |
| break; |
| case OPT_FINT: |
| (*(void(*)(int))(op[j].arg))((int)lv); |
| break; |
| case OPT_STR: |
| *(char**)(op[j].arg) = sv; |
| break; |
| case OPT_FSTR: |
| (*(void(*)(char *))(op[j].arg))(sv); |
| break; |
| } |
| } |
| return errcnt; |
| } |
| |
| int OptInit(char **a, struct s_options *o, FILE *err) |
| { |
| int errcnt = 0; |
| g_argv = a; |
| op = o; |
| errstream = err; |
| if( g_argv && *g_argv && op ){ |
| int i; |
| for(i=1; g_argv[i]; i++){ |
| if( g_argv[i][0]=='+' || g_argv[i][0]=='-' ){ |
| errcnt += handleflags(i,err); |
| }else if( strchr(g_argv[i],'=') ){ |
| errcnt += handleswitch(i,err); |
| } |
| } |
| } |
| if( errcnt>0 ){ |
| fprintf(err,"Valid command line options for \"%s\" are:\n",*a); |
| OptPrint(); |
| exit(1); |
| } |
| return 0; |
| } |
| |
| int OptNArgs(void){ |
| int cnt = 0; |
| int dashdash = 0; |
| int i; |
| if( g_argv!=0 && g_argv[0]!=0 ){ |
| for(i=1; g_argv[i]; i++){ |
| if( dashdash || !ISOPT(g_argv[i]) ) cnt++; |
| if( strcmp(g_argv[i],"--")==0 ) dashdash = 1; |
| } |
| } |
| return cnt; |
| } |
| |
| char *OptArg(int n) |
| { |
| int i; |
| i = argindex(n); |
| return i>=0 ? g_argv[i] : 0; |
| } |
| |
| void OptErr(int n) |
| { |
| int i; |
| i = argindex(n); |
| if( i>=0 ) errline(i,0,errstream); |
| } |
| |
| void OptPrint(void){ |
| int i; |
| int max, len; |
| max = 0; |
| for(i=0; op[i].label; i++){ |
| len = lemonStrlen(op[i].label) + 1; |
| switch( op[i].type ){ |
| case OPT_FLAG: |
| case OPT_FFLAG: |
| break; |
| case OPT_INT: |
| case OPT_FINT: |
| len += 9; /* length of "<integer>" */ |
| break; |
| case OPT_DBL: |
| case OPT_FDBL: |
| len += 6; /* length of "<real>" */ |
| break; |
| case OPT_STR: |
| case OPT_FSTR: |
| len += 8; /* length of "<string>" */ |
| break; |
| } |
| if( len>max ) max = len; |
| } |
| for(i=0; op[i].label; i++){ |
| switch( op[i].type ){ |
| case OPT_FLAG: |
| case OPT_FFLAG: |
| fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message); |
| break; |
| case OPT_INT: |
| case OPT_FINT: |
| fprintf(errstream," -%s<integer>%*s %s\n",op[i].label, |
| (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message); |
| break; |
| case OPT_DBL: |
| case OPT_FDBL: |
| fprintf(errstream," -%s<real>%*s %s\n",op[i].label, |
| (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message); |
| break; |
| case OPT_STR: |
| case OPT_FSTR: |
| fprintf(errstream," -%s<string>%*s %s\n",op[i].label, |
| (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message); |
| break; |
| } |
| } |
| } |
| /*********************** From the file "parse.c" ****************************/ |
| /* |
| ** Input file parser for the LEMON parser generator. |
| */ |
| |
| /* The state of the parser */ |
| enum e_state { |
| INITIALIZE, |
| WAITING_FOR_DECL_OR_RULE, |
| WAITING_FOR_DECL_KEYWORD, |
| WAITING_FOR_DECL_ARG, |
| WAITING_FOR_PRECEDENCE_SYMBOL, |
| WAITING_FOR_ARROW, |
| IN_RHS, |
| LHS_ALIAS_1, |
| LHS_ALIAS_2, |
| LHS_ALIAS_3, |
| RHS_ALIAS_1, |
| RHS_ALIAS_2, |
| PRECEDENCE_MARK_1, |
| PRECEDENCE_MARK_2, |
| RESYNC_AFTER_RULE_ERROR, |
| RESYNC_AFTER_DECL_ERROR, |
| WAITING_FOR_DESTRUCTOR_SYMBOL, |
| WAITING_FOR_DATATYPE_SYMBOL, |
| WAITING_FOR_FALLBACK_ID, |
| WAITING_FOR_WILDCARD_ID, |
| WAITING_FOR_CLASS_ID, |
| WAITING_FOR_CLASS_TOKEN, |
| WAITING_FOR_TOKEN_NAME |
| }; |
| struct pstate { |
| char *filename; /* Name of the input file */ |
| int tokenlineno; /* Linenumber at which current token starts */ |
| int errorcnt; /* Number of errors so far */ |
| char *tokenstart; /* Text of current token */ |
| struct lemon *gp; /* Global state vector */ |
| enum e_state state; /* The state of the parser */ |
| struct symbol *fallback; /* The fallback token */ |
| struct symbol *tkclass; /* Token class symbol */ |
| struct symbol *lhs; /* Left-hand side of current rule */ |
| const char *lhsalias; /* Alias for the LHS */ |
| int nrhs; /* Number of right-hand side symbols seen */ |
| struct symbol *rhs[MAXRHS]; /* RHS symbols */ |
| const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */ |
| struct rule *prevrule; /* Previous rule parsed */ |
| const char *declkeyword; /* Keyword of a declaration */ |
| char **declargslot; /* Where the declaration argument should be put */ |
| int insertLineMacro; /* Add #line before declaration insert */ |
| int *decllinenoslot; /* Where to write declaration line number */ |
| enum e_assoc declassoc; /* Assign this association to decl arguments */ |
| int preccounter; /* Assign this precedence to decl arguments */ |
| struct rule *firstrule; /* Pointer to first rule in the grammar */ |
| struct rule *lastrule; /* Pointer to the most recently parsed rule */ |
| }; |
| |
| /* Parse a single token */ |
| static void parseonetoken(struct pstate *psp) |
| { |
| const char *x; |
| x = Strsafe(psp->tokenstart); /* Save the token permanently */ |
| #if 0 |
| printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno, |
| x,psp->state); |
| #endif |
| switch( psp->state ){ |
| case INITIALIZE: |
| psp->prevrule = 0; |
| psp->preccounter = 0; |
| psp->firstrule = psp->lastrule = 0; |
| psp->gp->nrule = 0; |
| /* Fall thru to next case */ |
| case WAITING_FOR_DECL_OR_RULE: |
| if( x[0]=='%' ){ |
| psp->state = WAITING_FOR_DECL_KEYWORD; |
| }else if( ISLOWER(x[0]) ){ |
| psp->lhs = Symbol_new(x); |
| psp->nrhs = 0; |
| psp->lhsalias = 0; |
| psp->state = WAITING_FOR_ARROW; |
| }else if( x[0]=='{' ){ |
| if( psp->prevrule==0 ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "There is no prior rule upon which to attach the code " |
| "fragment which begins on this line."); |
| psp->errorcnt++; |
| }else if( psp->prevrule->code!=0 ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Code fragment beginning on this line is not the first " |
| "to follow the previous rule."); |
| psp->errorcnt++; |
| }else if( strcmp(x, "{NEVER-REDUCE")==0 ){ |
| psp->prevrule->neverReduce = 1; |
| }else{ |
| psp->prevrule->line = psp->tokenlineno; |
| psp->prevrule->code = &x[1]; |
| psp->prevrule->noCode = 0; |
| } |
| }else if( x[0]=='[' ){ |
| psp->state = PRECEDENCE_MARK_1; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Token \"%s\" should be either \"%%\" or a nonterminal name.", |
| x); |
| psp->errorcnt++; |
| } |
| break; |
| case PRECEDENCE_MARK_1: |
| if( !ISUPPER(x[0]) ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "The precedence symbol must be a terminal."); |
| psp->errorcnt++; |
| }else if( psp->prevrule==0 ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "There is no prior rule to assign precedence \"[%s]\".",x); |
| psp->errorcnt++; |
| }else if( psp->prevrule->precsym!=0 ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Precedence mark on this line is not the first " |
| "to follow the previous rule."); |
| psp->errorcnt++; |
| }else{ |
| psp->prevrule->precsym = Symbol_new(x); |
| } |
| psp->state = PRECEDENCE_MARK_2; |
| break; |
| case PRECEDENCE_MARK_2: |
| if( x[0]!=']' ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Missing \"]\" on precedence mark."); |
| psp->errorcnt++; |
| } |
| psp->state = WAITING_FOR_DECL_OR_RULE; |
| break; |
| case WAITING_FOR_ARROW: |
| if( x[0]==':' && x[1]==':' && x[2]=='=' ){ |
| psp->state = IN_RHS; |
| }else if( x[0]=='(' ){ |
| psp->state = LHS_ALIAS_1; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Expected to see a \":\" following the LHS symbol \"%s\".", |
| psp->lhs->name); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_RULE_ERROR; |
| } |
| break; |
| case LHS_ALIAS_1: |
| if( ISALPHA(x[0]) ){ |
| psp->lhsalias = x; |
| psp->state = LHS_ALIAS_2; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "\"%s\" is not a valid alias for the LHS \"%s\"\n", |
| x,psp->lhs->name); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_RULE_ERROR; |
| } |
| break; |
| case LHS_ALIAS_2: |
| if( x[0]==')' ){ |
| psp->state = LHS_ALIAS_3; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_RULE_ERROR; |
| } |
| break; |
| case LHS_ALIAS_3: |
| if( x[0]==':' && x[1]==':' && x[2]=='=' ){ |
| psp->state = IN_RHS; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Missing \"->\" following: \"%s(%s)\".", |
| psp->lhs->name,psp->lhsalias); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_RULE_ERROR; |
| } |
| break; |
| case IN_RHS: |
| if( x[0]=='.' ){ |
| struct rule *rp; |
| rp = (struct rule *)calloc( sizeof(struct rule) + |
| sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1); |
| if( rp==0 ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Can't allocate enough memory for this rule."); |
| psp->errorcnt++; |
| psp->prevrule = 0; |
| }else{ |
| int i; |
| rp->ruleline = psp->tokenlineno; |
| rp->rhs = (struct symbol**)&rp[1]; |
| rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]); |
| for(i=0; i<psp->nrhs; i++){ |
| rp->rhs[i] = psp->rhs[i]; |
| rp->rhsalias[i] = psp->alias[i]; |
| if( rp->rhsalias[i]!=0 ){ rp->rhs[i]->bContent = 1; } |
| } |
| rp->lhs = psp->lhs; |
| rp->lhsalias = psp->lhsalias; |
| rp->nrhs = psp->nrhs; |
| rp->code = 0; |
| rp->noCode = 1; |
| rp->precsym = 0; |
| rp->index = psp->gp->nrule++; |
| rp->nextlhs = rp->lhs->rule; |
| rp->lhs->rule = rp; |
| rp->next = 0; |
| if( psp->firstrule==0 ){ |
| psp->firstrule = psp->lastrule = rp; |
| }else{ |
| psp->lastrule->next = rp; |
| psp->lastrule = rp; |
| } |
| psp->prevrule = rp; |
| } |
| psp->state = WAITING_FOR_DECL_OR_RULE; |
| }else if( ISALPHA(x[0]) ){ |
| if( psp->nrhs>=MAXRHS ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Too many symbols on RHS of rule beginning at \"%s\".", |
| x); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_RULE_ERROR; |
| }else{ |
| psp->rhs[psp->nrhs] = Symbol_new(x); |
| psp->alias[psp->nrhs] = 0; |
| psp->nrhs++; |
| } |
| }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){ |
| struct symbol *msp = psp->rhs[psp->nrhs-1]; |
| if( msp->type!=MULTITERMINAL ){ |
| struct symbol *origsp = msp; |
| msp = (struct symbol *) calloc(1,sizeof(*msp)); |
| memset(msp, 0, sizeof(*msp)); |
| msp->type = MULTITERMINAL; |
| msp->nsubsym = 1; |
| msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*)); |
| msp->subsym[0] = origsp; |
| msp->name = origsp->name; |
| psp->rhs[psp->nrhs-1] = msp; |
| } |
| msp->nsubsym++; |
| msp->subsym = (struct symbol **) realloc(msp->subsym, |
| sizeof(struct symbol*)*msp->nsubsym); |
| msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]); |
| if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Cannot form a compound containing a non-terminal"); |
| psp->errorcnt++; |
| } |
| }else if( x[0]=='(' && psp->nrhs>0 ){ |
| psp->state = RHS_ALIAS_1; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Illegal character on RHS of rule: \"%s\".",x); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_RULE_ERROR; |
| } |
| break; |
| case RHS_ALIAS_1: |
| if( ISALPHA(x[0]) ){ |
| psp->alias[psp->nrhs-1] = x; |
| psp->state = RHS_ALIAS_2; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n", |
| x,psp->rhs[psp->nrhs-1]->name); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_RULE_ERROR; |
| } |
| break; |
| case RHS_ALIAS_2: |
| if( x[0]==')' ){ |
| psp->state = IN_RHS; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_RULE_ERROR; |
| } |
| break; |
| case WAITING_FOR_DECL_KEYWORD: |
| if( ISALPHA(x[0]) ){ |
| psp->declkeyword = x; |
| psp->declargslot = 0; |
| psp->decllinenoslot = 0; |
| psp->insertLineMacro = 1; |
| psp->state = WAITING_FOR_DECL_ARG; |
| if( strcmp(x,"name")==0 ){ |
| psp->declargslot = &(psp->gp->name); |
| psp->insertLineMacro = 0; |
| }else if( strcmp(x,"include")==0 ){ |
| psp->declargslot = &(psp->gp->include); |
| }else if( strcmp(x,"code")==0 ){ |
| psp->declargslot = &(psp->gp->extracode); |
| }else if( strcmp(x,"token_destructor")==0 ){ |
| psp->declargslot = &psp->gp->tokendest; |
| }else if( strcmp(x,"default_destructor")==0 ){ |
| psp->declargslot = &psp->gp->vardest; |
| }else if( strcmp(x,"token_prefix")==0 ){ |
| psp->declargslot = &psp->gp->tokenprefix; |
| psp->insertLineMacro = 0; |
| }else if( strcmp(x,"syntax_error")==0 ){ |
| psp->declargslot = &(psp->gp->error); |
| }else if( strcmp(x,"parse_accept")==0 ){ |
| psp->declargslot = &(psp->gp->accept); |
| }else if( strcmp(x,"parse_failure")==0 ){ |
| psp->declargslot = &(psp->gp->failure); |
| }else if( strcmp(x,"stack_overflow")==0 ){ |
| psp->declargslot = &(psp->gp->overflow); |
| }else if( strcmp(x,"extra_argument")==0 ){ |
| psp->declargslot = &(psp->gp->arg); |
| psp->insertLineMacro = 0; |
| }else if( strcmp(x,"extra_context")==0 ){ |
| psp->declargslot = &(psp->gp->ctx); |
| psp->insertLineMacro = 0; |
| }else if( strcmp(x,"token_type")==0 ){ |
| psp->declargslot = &(psp->gp->tokentype); |
| psp->insertLineMacro = 0; |
| }else if( strcmp(x,"default_type")==0 ){ |
| psp->declargslot = &(psp->gp->vartype); |
| psp->insertLineMacro = 0; |
| }else if( strcmp(x,"stack_size")==0 ){ |
| psp->declargslot = &(psp->gp->stacksize); |
| psp->insertLineMacro = 0; |
| }else if( strcmp(x,"start_symbol")==0 ){ |
| psp->declargslot = &(psp->gp->start); |
| psp->insertLineMacro = 0; |
| }else if( strcmp(x,"left")==0 ){ |
| psp->preccounter++; |
| psp->declassoc = LEFT; |
| psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
| }else if( strcmp(x,"right")==0 ){ |
| psp->preccounter++; |
| psp->declassoc = RIGHT; |
| psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
| }else if( strcmp(x,"nonassoc")==0 ){ |
| psp->preccounter++; |
| psp->declassoc = NONE; |
| psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
| }else if( strcmp(x,"destructor")==0 ){ |
| psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL; |
| }else if( strcmp(x,"type")==0 ){ |
| psp->state = WAITING_FOR_DATATYPE_SYMBOL; |
| }else if( strcmp(x,"fallback")==0 ){ |
| psp->fallback = 0; |
| psp->state = WAITING_FOR_FALLBACK_ID; |
| }else if( strcmp(x,"token")==0 ){ |
| psp->state = WAITING_FOR_TOKEN_NAME; |
| }else if( strcmp(x,"wildcard")==0 ){ |
| psp->state = WAITING_FOR_WILDCARD_ID; |
| }else if( strcmp(x,"token_class")==0 ){ |
| psp->state = WAITING_FOR_CLASS_ID; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Unknown declaration keyword: \"%%%s\".",x); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_DECL_ERROR; |
| } |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Illegal declaration keyword: \"%s\".",x); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_DECL_ERROR; |
| } |
| break; |
| case WAITING_FOR_DESTRUCTOR_SYMBOL: |
| if( !ISALPHA(x[0]) ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Symbol name missing after %%destructor keyword"); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_DECL_ERROR; |
| }else{ |
| struct symbol *sp = Symbol_new(x); |
| psp->declargslot = &sp->destructor; |
| psp->decllinenoslot = &sp->destLineno; |
| psp->insertLineMacro = 1; |
| psp->state = WAITING_FOR_DECL_ARG; |
| } |
| break; |
| case WAITING_FOR_DATATYPE_SYMBOL: |
| if( !ISALPHA(x[0]) ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Symbol name missing after %%type keyword"); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_DECL_ERROR; |
| }else{ |
| struct symbol *sp = Symbol_find(x); |
| if((sp) && (sp->datatype)){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Symbol %%type \"%s\" already defined", x); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_DECL_ERROR; |
| }else{ |
| if (!sp){ |
| sp = Symbol_new(x); |
| } |
| psp->declargslot = &sp->datatype; |
| psp->insertLineMacro = 0; |
| psp->state = WAITING_FOR_DECL_ARG; |
| } |
| } |
| break; |
| case WAITING_FOR_PRECEDENCE_SYMBOL: |
| if( x[0]=='.' ){ |
| psp->state = WAITING_FOR_DECL_OR_RULE; |
| }else if( ISUPPER(x[0]) ){ |
| struct symbol *sp; |
| sp = Symbol_new(x); |
| if( sp->prec>=0 ){ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Symbol \"%s\" has already be given a precedence.",x); |
| psp->errorcnt++; |
| }else{ |
| sp->prec = psp->preccounter; |
| sp->assoc = psp->declassoc; |
| } |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Can't assign a precedence to \"%s\".",x); |
| psp->errorcnt++; |
| } |
| break; |
| case WAITING_FOR_DECL_ARG: |
| if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){ |
| const char *zOld, *zNew; |
| char *zBuf, *z; |
| int nOld, n, nLine = 0, nNew, nBack; |
| int addLineMacro; |
| char zLine[50]; |
| zNew = x; |
| if( zNew[0]=='"' || zNew[0]=='{' ) zNew++; |
| nNew = lemonStrlen(zNew); |
| if( *psp->declargslot ){ |
| zOld = *psp->declargslot; |
| }else{ |
| zOld = ""; |
| } |
| nOld = lemonStrlen(zOld); |
| n = nOld + nNew + 20; |
| addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro && |
| (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0); |
| if( addLineMacro ){ |
| for(z=psp->filename, nBack=0; *z; z++){ |
| if( *z=='\\' ) nBack++; |
| } |
| lemon_sprintf(zLine, "#line %d ", psp->tokenlineno); |
| nLine = lemonStrlen(zLine); |
| n += nLine + lemonStrlen(psp->filename) + nBack; |
| } |
| *psp->declargslot = (char *) realloc(*psp->declargslot, n); |
| zBuf = *psp->declargslot + nOld; |
| if( addLineMacro ){ |
| if( nOld && zBuf[-1]!='\n' ){ |
| *(zBuf++) = '\n'; |
| } |
| memcpy(zBuf, zLine, nLine); |
| zBuf += nLine; |
| *(zBuf++) = '"'; |
| for(z=psp->filename; *z; z++){ |
| if( *z=='\\' ){ |
| *(zBuf++) = '\\'; |
| } |
| *(zBuf++) = *z; |
| } |
| *(zBuf++) = '"'; |
| *(zBuf++) = '\n'; |
| } |
| if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){ |
| psp->decllinenoslot[0] = psp->tokenlineno; |
| } |
| memcpy(zBuf, zNew, nNew); |
| zBuf += nNew; |
| *zBuf = 0; |
| psp->state = WAITING_FOR_DECL_OR_RULE; |
| }else{ |
| ErrorMsg(psp->filename,psp->tokenlineno, |
| "Illegal argument to %%%s: %s",psp->declkeyword,x); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_DECL_ERROR; |
| } |
| break; |
| case WAITING_FOR_FALLBACK_ID: |
| if( x[0]=='.' ){ |
| psp->state = WAITING_FOR_DECL_OR_RULE; |
| }else if( !ISUPPER(x[0]) ){ |
| ErrorMsg(psp->filename, psp->tokenlineno, |
| "%%fallback argument \"%s\" should be a token", x); |
| psp->errorcnt++; |
| }else{ |
| struct symbol *sp = Symbol_new(x); |
| if( psp->fallback==0 ){ |
| psp->fallback = sp; |
| }else if( sp->fallback ){ |
| ErrorMsg(psp->filename, psp->tokenlineno, |
| "More than one fallback assigned to token %s", x); |
| psp->errorcnt++; |
| }else{ |
| sp->fallback = psp->fallback; |
| psp->gp->has_fallback = 1; |
| } |
| } |
| break; |
| case WAITING_FOR_TOKEN_NAME: |
| /* Tokens do not have to be declared before use. But they can be |
| ** in order to control their assigned integer number. The number for |
| ** each token is assigned when it is first seen. So by including |
| ** |
| ** %token ONE TWO THREE |
| ** |
| ** early in the grammar file, that assigns small consecutive values |
| ** to each of the tokens ONE TWO and THREE. |
| */ |
| if( x[0]=='.' ){ |
| psp->state = WAITING_FOR_DECL_OR_RULE; |
| }else if( !ISUPPER(x[0]) ){ |
| ErrorMsg(psp->filename, psp->tokenlineno, |
| "%%token argument \"%s\" should be a token", x); |
| psp->errorcnt++; |
| }else{ |
| (void)Symbol_new(x); |
| } |
| break; |
| case WAITING_FOR_WILDCARD_ID: |
| if( x[0]=='.' ){ |
| psp->state = WAITING_FOR_DECL_OR_RULE; |
| }else if( !ISUPPER(x[0]) ){ |
| ErrorMsg(psp->filename, psp->tokenlineno, |
| "%%wildcard argument \"%s\" should be a token", x); |
| psp->errorcnt++; |
| }else{ |
| struct symbol *sp = Symbol_new(x); |
| if( psp->gp->wildcard==0 ){ |
| psp->gp->wildcard = sp; |
| }else{ |
| ErrorMsg(psp->filename, psp->tokenlineno, |
| "Extra wildcard to token: %s", x); |
| psp->errorcnt++; |
| } |
| } |
| break; |
| case WAITING_FOR_CLASS_ID: |
| if( !ISLOWER(x[0]) ){ |
| ErrorMsg(psp->filename, psp->tokenlineno, |
| "%%token_class must be followed by an identifier: %s", x); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_DECL_ERROR; |
| }else if( Symbol_find(x) ){ |
| ErrorMsg(psp->filename, psp->tokenlineno, |
| "Symbol \"%s\" already used", x); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_DECL_ERROR; |
| }else{ |
| psp->tkclass = Symbol_new(x); |
| psp->tkclass->type = MULTITERMINAL; |
| psp->state = WAITING_FOR_CLASS_TOKEN; |
| } |
| break; |
| case WAITING_FOR_CLASS_TOKEN: |
| if( x[0]=='.' ){ |
| psp->state = WAITING_FOR_DECL_OR_RULE; |
| }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){ |
| struct symbol *msp = psp->tkclass; |
| msp->nsubsym++; |
| msp->subsym = (struct symbol **) realloc(msp->subsym, |
| sizeof(struct symbol*)*msp->nsubsym); |
| if( !ISUPPER(x[0]) ) x++; |
| msp->subsym[msp->nsubsym-1] = Symbol_new(x); |
| }else{ |
| ErrorMsg(psp->filename, psp->tokenlineno, |
| "%%token_class argument \"%s\" should be a token", x); |
| psp->errorcnt++; |
| psp->state = RESYNC_AFTER_DECL_ERROR; |
| } |
| break; |
| case RESYNC_AFTER_RULE_ERROR: |
| /* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE; |
| ** break; */ |
| case RESYNC_AFTER_DECL_ERROR: |
| if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE; |
| if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD; |
| break; |
| } |
| } |
| |
| /* Run the preprocessor over the input file text. The global variables |
| ** azDefine[0] through azDefine[nDefine-1] contains the names of all defined |
| ** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and |
| ** comments them out. Text in between is also commented out as appropriate. |
| */ |
| static void preprocess_input(char *z){ |
| int i, j, k, n; |
| int exclude = 0; |
| int start = 0; |
| int lineno |