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