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