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