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