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