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