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