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