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 | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 16 | #define ISSPACE(X) isspace((unsigned char)(X)) |
| 17 | #define ISDIGIT(X) isdigit((unsigned char)(X)) |
| 18 | #define ISALNUM(X) isalnum((unsigned char)(X)) |
| 19 | #define ISALPHA(X) isalpha((unsigned char)(X)) |
| 20 | #define ISUPPER(X) isupper((unsigned char)(X)) |
| 21 | #define ISLOWER(X) islower((unsigned char)(X)) |
| 22 | |
| 23 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 24 | #ifndef __WIN32__ |
| 25 | # if defined(_WIN32) || defined(WIN32) |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 26 | # define __WIN32__ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 27 | # endif |
| 28 | #endif |
| 29 | |
rse | 8f30448 | 2007-07-30 18:31:53 +0000 | [diff] [blame] | 30 | #ifdef __WIN32__ |
drh | df60971 | 2010-11-23 20:55:27 +0000 | [diff] [blame] | 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | extern int access(const char *path, int mode); |
| 35 | #ifdef __cplusplus |
| 36 | } |
| 37 | #endif |
rse | 8f30448 | 2007-07-30 18:31:53 +0000 | [diff] [blame] | 38 | #else |
| 39 | #include <unistd.h> |
| 40 | #endif |
| 41 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 42 | /* #define PRIVATE static */ |
| 43 | #define PRIVATE |
| 44 | |
| 45 | #ifdef TEST |
| 46 | #define MAXRHS 5 /* Set low to exercise exception code */ |
| 47 | #else |
| 48 | #define MAXRHS 1000 |
| 49 | #endif |
| 50 | |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 51 | static int showPrecedenceConflict = 0; |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 52 | static char *msort(char*,char**,int(*)(const char*,const char*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 53 | |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 54 | /* |
| 55 | ** Compilers are getting increasingly pedantic about type conversions |
| 56 | ** as C evolves ever closer to Ada.... To work around the latest problems |
| 57 | ** we have to define the following variant of strlen(). |
| 58 | */ |
| 59 | #define lemonStrlen(X) ((int)strlen(X)) |
| 60 | |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 61 | /* |
| 62 | ** Compilers are starting to complain about the use of sprintf() and strcpy(), |
| 63 | ** saying they are unsafe. So we define our own versions of those routines too. |
| 64 | ** |
| 65 | ** There are three routines here: lemon_sprintf(), lemon_vsprintf(), and |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 66 | ** lemon_addtext(). The first two are replacements for sprintf() and vsprintf(). |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 67 | ** The third is a helper routine for vsnprintf() that adds texts to the end of a |
| 68 | ** buffer, making sure the buffer is always zero-terminated. |
| 69 | ** |
| 70 | ** The string formatter is a minimal subset of stdlib sprintf() supporting only |
| 71 | ** a few simply conversions: |
| 72 | ** |
| 73 | ** %d |
| 74 | ** %s |
| 75 | ** %.*s |
| 76 | ** |
| 77 | */ |
| 78 | static void lemon_addtext( |
| 79 | char *zBuf, /* The buffer to which text is added */ |
| 80 | int *pnUsed, /* Slots of the buffer used so far */ |
| 81 | const char *zIn, /* Text to add */ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 82 | int nIn, /* Bytes of text to add. -1 to use strlen() */ |
| 83 | int iWidth /* Field width. Negative to left justify */ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 84 | ){ |
| 85 | if( nIn<0 ) for(nIn=0; zIn[nIn]; nIn++){} |
drh | ecaa9d3 | 2014-01-11 03:27:37 +0000 | [diff] [blame] | 86 | while( iWidth>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth--; } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 87 | if( nIn==0 ) return; |
| 88 | memcpy(&zBuf[*pnUsed], zIn, nIn); |
| 89 | *pnUsed += nIn; |
drh | ecaa9d3 | 2014-01-11 03:27:37 +0000 | [diff] [blame] | 90 | while( (-iWidth)>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth++; } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 91 | zBuf[*pnUsed] = 0; |
| 92 | } |
| 93 | static int lemon_vsprintf(char *str, const char *zFormat, va_list ap){ |
mistachkin | 7a42965 | 2014-01-14 10:17:21 +0000 | [diff] [blame] | 94 | int i, j, k, c; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 95 | int nUsed = 0; |
| 96 | const char *z; |
| 97 | char zTemp[50]; |
| 98 | str[0] = 0; |
| 99 | for(i=j=0; (c = zFormat[i])!=0; i++){ |
| 100 | if( c=='%' ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 101 | int iWidth = 0; |
| 102 | lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 103 | c = zFormat[++i]; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 104 | if( ISDIGIT(c) || (c=='-' && ISDIGIT(zFormat[i+1])) ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 105 | if( c=='-' ) i++; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 106 | while( ISDIGIT(zFormat[i]) ) iWidth = iWidth*10 + zFormat[i++] - '0'; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 107 | if( c=='-' ) iWidth = -iWidth; |
| 108 | c = zFormat[i]; |
| 109 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 110 | if( c=='d' ){ |
| 111 | int v = va_arg(ap, int); |
| 112 | if( v<0 ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 113 | lemon_addtext(str, &nUsed, "-", 1, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 114 | v = -v; |
| 115 | }else if( v==0 ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 116 | lemon_addtext(str, &nUsed, "0", 1, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 117 | } |
| 118 | k = 0; |
| 119 | while( v>0 ){ |
| 120 | k++; |
| 121 | zTemp[sizeof(zTemp)-k] = (v%10) + '0'; |
| 122 | v /= 10; |
| 123 | } |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 124 | lemon_addtext(str, &nUsed, &zTemp[sizeof(zTemp)-k], k, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 125 | }else if( c=='s' ){ |
| 126 | z = va_arg(ap, const char*); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 127 | lemon_addtext(str, &nUsed, z, -1, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 128 | }else if( c=='.' && memcmp(&zFormat[i], ".*s", 3)==0 ){ |
| 129 | i += 2; |
| 130 | k = va_arg(ap, int); |
| 131 | z = va_arg(ap, const char*); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 132 | lemon_addtext(str, &nUsed, z, k, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 133 | }else if( c=='%' ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 134 | lemon_addtext(str, &nUsed, "%", 1, 0); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 135 | }else{ |
| 136 | fprintf(stderr, "illegal format\n"); |
| 137 | exit(1); |
| 138 | } |
| 139 | j = i+1; |
| 140 | } |
| 141 | } |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 142 | lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 143 | return nUsed; |
| 144 | } |
| 145 | static int lemon_sprintf(char *str, const char *format, ...){ |
| 146 | va_list ap; |
| 147 | int rc; |
| 148 | va_start(ap, format); |
| 149 | rc = lemon_vsprintf(str, format, ap); |
| 150 | va_end(ap); |
| 151 | return rc; |
| 152 | } |
| 153 | static void lemon_strcpy(char *dest, const char *src){ |
| 154 | while( (*(dest++) = *(src++))!=0 ){} |
| 155 | } |
| 156 | static void lemon_strcat(char *dest, const char *src){ |
| 157 | while( *dest ) dest++; |
| 158 | lemon_strcpy(dest, src); |
| 159 | } |
| 160 | |
| 161 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 162 | /* a few forward declarations... */ |
| 163 | struct rule; |
| 164 | struct lemon; |
| 165 | struct action; |
| 166 | |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 167 | static struct action *Action_new(void); |
| 168 | static struct action *Action_sort(struct action *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 169 | |
| 170 | /********** From the file "build.h" ************************************/ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 171 | void FindRulePrecedences(struct lemon*); |
| 172 | void FindFirstSets(struct lemon*); |
| 173 | void FindStates(struct lemon*); |
| 174 | void FindLinks(struct lemon*); |
| 175 | void FindFollowSets(struct lemon*); |
| 176 | void FindActions(struct lemon*); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 177 | |
| 178 | /********* From the file "configlist.h" *********************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 179 | void Configlist_init(void); |
| 180 | struct config *Configlist_add(struct rule *, int); |
| 181 | struct config *Configlist_addbasis(struct rule *, int); |
| 182 | void Configlist_closure(struct lemon *); |
| 183 | void Configlist_sort(void); |
| 184 | void Configlist_sortbasis(void); |
| 185 | struct config *Configlist_return(void); |
| 186 | struct config *Configlist_basis(void); |
| 187 | void Configlist_eat(struct config *); |
| 188 | void Configlist_reset(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 189 | |
| 190 | /********* From the file "error.h" ***************************************/ |
drh | f9a2e7b | 2003-04-15 01:49:48 +0000 | [diff] [blame] | 191 | void ErrorMsg(const char *, int,const char *, ...); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 192 | |
| 193 | /****** From the file "option.h" ******************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 194 | enum option_type { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR, |
| 195 | OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR}; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 196 | struct s_options { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 197 | enum option_type type; |
| 198 | const char *label; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 199 | char *arg; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 200 | const char *message; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 201 | }; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 202 | int OptInit(char**,struct s_options*,FILE*); |
| 203 | int OptNArgs(void); |
| 204 | char *OptArg(int); |
| 205 | void OptErr(int); |
| 206 | void OptPrint(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 207 | |
| 208 | /******** From the file "parse.h" *****************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 209 | void Parse(struct lemon *lemp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 210 | |
| 211 | /********* From the file "plink.h" ***************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 212 | struct plink *Plink_new(void); |
| 213 | void Plink_add(struct plink **, struct config *); |
| 214 | void Plink_copy(struct plink **, struct plink *); |
| 215 | void Plink_delete(struct plink *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 216 | |
| 217 | /********** From the file "report.h" *************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 218 | void Reprint(struct lemon *); |
| 219 | void ReportOutput(struct lemon *); |
| 220 | void ReportTable(struct lemon *, int); |
| 221 | void ReportHeader(struct lemon *); |
| 222 | void CompressTables(struct lemon *); |
| 223 | void ResortStates(struct lemon *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 224 | |
| 225 | /********** From the file "set.h" ****************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 226 | void SetSize(int); /* All sets will be of size N */ |
| 227 | char *SetNew(void); /* A new set for element 0..N */ |
| 228 | void SetFree(char*); /* Deallocate a set */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 229 | int SetAdd(char*,int); /* Add element to a set */ |
| 230 | int SetUnion(char *,char *); /* A <- A U B, thru element N */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 231 | #define SetFind(X,Y) (X[Y]) /* True if Y is in set X */ |
| 232 | |
| 233 | /********** From the file "struct.h" *************************************/ |
| 234 | /* |
| 235 | ** Principal data structures for the LEMON parser generator. |
| 236 | */ |
| 237 | |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 238 | typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 239 | |
| 240 | /* Symbols (terminals and nonterminals) of the grammar are stored |
| 241 | ** in the following: */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 242 | enum symbol_type { |
| 243 | TERMINAL, |
| 244 | NONTERMINAL, |
| 245 | MULTITERMINAL |
| 246 | }; |
| 247 | enum e_assoc { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 248 | LEFT, |
| 249 | RIGHT, |
| 250 | NONE, |
| 251 | UNK |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 252 | }; |
| 253 | struct symbol { |
| 254 | const char *name; /* Name of the symbol */ |
| 255 | int index; /* Index number for this symbol */ |
| 256 | enum symbol_type type; /* Symbols are all either TERMINALS or NTs */ |
| 257 | struct rule *rule; /* Linked list of rules of this (if an NT) */ |
| 258 | struct symbol *fallback; /* fallback token in case this token doesn't parse */ |
| 259 | int prec; /* Precedence if defined (-1 otherwise) */ |
| 260 | enum e_assoc assoc; /* Associativity if precedence is defined */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 261 | char *firstset; /* First-set for all rules of this symbol */ |
| 262 | Boolean lambda; /* True if NT and can generate an empty string */ |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 263 | int useCnt; /* Number of times used */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 264 | char *destructor; /* Code which executes whenever this symbol is |
| 265 | ** popped from the stack during error processing */ |
drh | 0f832dd | 2016-08-16 16:46:40 +0000 | [diff] [blame] | 266 | int destLineno; /* Line number for start of destructor. Set to |
| 267 | ** -1 for duplicate destructors. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 268 | char *datatype; /* The data type of information held by this |
| 269 | ** object. Only used if type==NONTERMINAL */ |
| 270 | int dtnum; /* The data type number. In the parser, the value |
| 271 | ** stack is a union. The .yy%d element of this |
| 272 | ** union is the correct data type for this object */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 273 | /* The following fields are used by MULTITERMINALs only */ |
| 274 | int nsubsym; /* Number of constituent symbols in the MULTI */ |
| 275 | struct symbol **subsym; /* Array of constituent symbols */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | /* Each production rule in the grammar is stored in the following |
| 279 | ** structure. */ |
| 280 | struct rule { |
| 281 | struct symbol *lhs; /* Left-hand side of the rule */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 282 | const char *lhsalias; /* Alias for the LHS (NULL if none) */ |
drh | b496099 | 2007-10-05 16:16:36 +0000 | [diff] [blame] | 283 | int lhsStart; /* True if left-hand side is the start symbol */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 284 | int ruleline; /* Line number for the rule */ |
| 285 | int nrhs; /* Number of RHS symbols */ |
| 286 | struct symbol **rhs; /* The RHS symbols */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 287 | const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 288 | int line; /* Line number at which code begins */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 289 | const char *code; /* The code executed when this rule is reduced */ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 290 | const char *codePrefix; /* Setup code before code[] above */ |
| 291 | const char *codeSuffix; /* Breakdown code after code[] above */ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 292 | int noCode; /* True if this rule has no associated C code */ |
| 293 | int codeEmitted; /* True if the code has been emitted already */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 294 | struct symbol *precsym; /* Precedence symbol for this rule */ |
| 295 | int index; /* An index number for this rule */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 296 | int iRule; /* Rule number as used in the generated tables */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 297 | Boolean canReduce; /* True if this rule is ever reduced */ |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 298 | Boolean doesReduce; /* Reduce actions occur after optimization */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 299 | struct rule *nextlhs; /* Next rule with the same LHS */ |
| 300 | struct rule *next; /* Next rule in the global list */ |
| 301 | }; |
| 302 | |
| 303 | /* A configuration is a production rule of the grammar together with |
| 304 | ** a mark (dot) showing how much of that rule has been processed so far. |
| 305 | ** Configurations also contain a follow-set which is a list of terminal |
| 306 | ** symbols which are allowed to immediately follow the end of the rule. |
| 307 | ** Every configuration is recorded as an instance of the following: */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 308 | enum cfgstatus { |
| 309 | COMPLETE, |
| 310 | INCOMPLETE |
| 311 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 312 | struct config { |
| 313 | struct rule *rp; /* The rule upon which the configuration is based */ |
| 314 | int dot; /* The parse point */ |
| 315 | char *fws; /* Follow-set for this configuration only */ |
| 316 | struct plink *fplp; /* Follow-set forward propagation links */ |
| 317 | struct plink *bplp; /* Follow-set backwards propagation links */ |
| 318 | struct state *stp; /* Pointer to state which contains this */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 319 | enum cfgstatus status; /* used during followset and shift computations */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 320 | struct config *next; /* Next configuration in the state */ |
| 321 | struct config *bp; /* The next basis configuration */ |
| 322 | }; |
| 323 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 324 | enum e_action { |
| 325 | SHIFT, |
| 326 | ACCEPT, |
| 327 | REDUCE, |
| 328 | ERROR, |
| 329 | SSCONFLICT, /* A shift/shift conflict */ |
| 330 | SRCONFLICT, /* Was a reduce, but part of a conflict */ |
| 331 | RRCONFLICT, /* Was a reduce, but part of a conflict */ |
| 332 | SH_RESOLVED, /* Was a shift. Precedence resolved conflict */ |
| 333 | RD_RESOLVED, /* Was reduce. Precedence resolved conflict */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 334 | NOT_USED, /* Deleted by compression */ |
| 335 | SHIFTREDUCE /* Shift first, then reduce */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 336 | }; |
| 337 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 338 | /* Every shift or reduce operation is stored as one of the following */ |
| 339 | struct action { |
| 340 | struct symbol *sp; /* The look-ahead symbol */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 341 | enum e_action type; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 342 | union { |
| 343 | struct state *stp; /* The new state, if a shift */ |
| 344 | struct rule *rp; /* The rule, if a reduce */ |
| 345 | } x; |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 346 | struct symbol *spOpt; /* SHIFTREDUCE optimization to this symbol */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 347 | struct action *next; /* Next action for this state */ |
| 348 | struct action *collide; /* Next action with the same hash */ |
| 349 | }; |
| 350 | |
| 351 | /* Each state of the generated parser's finite state machine |
| 352 | ** is encoded as an instance of the following structure. */ |
| 353 | struct state { |
| 354 | struct config *bp; /* The basis configurations for this state */ |
| 355 | struct config *cfp; /* All configurations in this set */ |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 356 | int statenum; /* Sequential number for this state */ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 357 | struct action *ap; /* List of actions for this state */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 358 | int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */ |
| 359 | int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 360 | int iDfltReduce; /* Default action is to REDUCE by this rule */ |
| 361 | struct rule *pDfltReduce;/* The default REDUCE rule. */ |
| 362 | int autoReduce; /* True if this is an auto-reduce state */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 363 | }; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 364 | #define NO_OFFSET (-2147483647) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 365 | |
| 366 | /* A followset propagation link indicates that the contents of one |
| 367 | ** configuration followset should be propagated to another whenever |
| 368 | ** the first changes. */ |
| 369 | struct plink { |
| 370 | struct config *cfp; /* The configuration to which linked */ |
| 371 | struct plink *next; /* The next propagate link */ |
| 372 | }; |
| 373 | |
| 374 | /* The state vector for the entire parser generator is recorded as |
| 375 | ** follows. (LEMON uses no global variables and makes little use of |
| 376 | ** static variables. Fields in the following structure can be thought |
| 377 | ** of as begin global variables in the program.) */ |
| 378 | struct lemon { |
| 379 | struct state **sorted; /* Table of states sorted by state number */ |
| 380 | struct rule *rule; /* List of all rules */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 381 | struct rule *startRule; /* First rule */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 382 | int nstate; /* Number of states */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 383 | int nxstate; /* nstate with tail degenerate states removed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 384 | int nrule; /* Number of rules */ |
| 385 | int nsymbol; /* Number of terminal and nonterminal symbols */ |
| 386 | int nterminal; /* Number of terminal symbols */ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 387 | int minShiftReduce; /* Minimum shift-reduce action value */ |
| 388 | int errAction; /* Error action value */ |
| 389 | int accAction; /* Accept action value */ |
| 390 | int noAction; /* No-op action value */ |
| 391 | int minReduce; /* Minimum reduce action */ |
| 392 | int maxAction; /* Maximum action value of any kind */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 393 | struct symbol **symbols; /* Sorted array of pointers to symbols */ |
| 394 | int errorcnt; /* Number of errors */ |
| 395 | struct symbol *errsym; /* The error symbol */ |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 396 | struct symbol *wildcard; /* Token that matches anything */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 397 | char *name; /* Name of the generated parser */ |
| 398 | char *arg; /* Declaration of the 3th argument to parser */ |
| 399 | char *tokentype; /* Type of terminal symbols in the parser stack */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 400 | char *vartype; /* The default type of non-terminal symbols */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 401 | char *start; /* Name of the start symbol for the grammar */ |
| 402 | char *stacksize; /* Size of the parser stack */ |
| 403 | char *include; /* Code to put at the start of the C file */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 404 | char *error; /* Code to execute when an error is seen */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 405 | char *overflow; /* Code to execute on a stack overflow */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 406 | char *failure; /* Code to execute on parser failure */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 407 | char *accept; /* Code to execute when the parser excepts */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 408 | char *extracode; /* Code appended to the generated file */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 409 | char *tokendest; /* Code to execute to destroy token data */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 410 | char *vardest; /* Code for the default non-terminal destructor */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 411 | char *filename; /* Name of the input file */ |
| 412 | char *outname; /* Name of the current output file */ |
| 413 | char *tokenprefix; /* A prefix added to token names in the .h file */ |
| 414 | int nconflict; /* Number of parsing conflicts */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 415 | int nactiontab; /* Number of entries in the yy_action[] table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 416 | int nlookaheadtab; /* Number of entries in yy_lookahead[] */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 417 | int tablesize; /* Total table size of all tables in bytes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 418 | int basisflag; /* Print only basis configurations */ |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 419 | int has_fallback; /* True if any %fallback is seen in the grammar */ |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 420 | int nolinenosflag; /* True if #line statements should not be printed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 421 | char *argv0; /* Name of the program */ |
| 422 | }; |
| 423 | |
| 424 | #define MemoryCheck(X) if((X)==0){ \ |
| 425 | extern void memory_error(); \ |
| 426 | memory_error(); \ |
| 427 | } |
| 428 | |
| 429 | /**************** From the file "table.h" *********************************/ |
| 430 | /* |
| 431 | ** All code in this file has been automatically generated |
| 432 | ** from a specification in the file |
| 433 | ** "table.q" |
| 434 | ** by the associative array code building program "aagen". |
| 435 | ** Do not edit this file! Instead, edit the specification |
| 436 | ** file, then rerun aagen. |
| 437 | */ |
| 438 | /* |
| 439 | ** Code for processing tables in the LEMON parser generator. |
| 440 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 441 | /* Routines for handling a strings */ |
| 442 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 443 | const char *Strsafe(const char *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 444 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 445 | void Strsafe_init(void); |
| 446 | int Strsafe_insert(const char *); |
| 447 | const char *Strsafe_find(const char *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 448 | |
| 449 | /* Routines for handling symbols of the grammar */ |
| 450 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 451 | struct symbol *Symbol_new(const char *); |
| 452 | int Symbolcmpp(const void *, const void *); |
| 453 | void Symbol_init(void); |
| 454 | int Symbol_insert(struct symbol *, const char *); |
| 455 | struct symbol *Symbol_find(const char *); |
| 456 | struct symbol *Symbol_Nth(int); |
| 457 | int Symbol_count(void); |
| 458 | struct symbol **Symbol_arrayof(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 459 | |
| 460 | /* Routines to manage the state table */ |
| 461 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 462 | int Configcmp(const char *, const char *); |
| 463 | struct state *State_new(void); |
| 464 | void State_init(void); |
| 465 | int State_insert(struct state *, struct config *); |
| 466 | struct state *State_find(struct config *); |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 467 | struct state **State_arrayof(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 468 | |
| 469 | /* Routines used for efficiency in Configlist_add */ |
| 470 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 471 | void Configtable_init(void); |
| 472 | int Configtable_insert(struct config *); |
| 473 | struct config *Configtable_find(struct config *); |
| 474 | void Configtable_clear(int(*)(struct config *)); |
| 475 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 476 | /****************** From the file "action.c" *******************************/ |
| 477 | /* |
| 478 | ** Routines processing parser actions in the LEMON parser generator. |
| 479 | */ |
| 480 | |
| 481 | /* Allocate a new parser action */ |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 482 | static struct action *Action_new(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 483 | static struct action *freelist = 0; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 484 | struct action *newaction; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 485 | |
| 486 | if( freelist==0 ){ |
| 487 | int i; |
| 488 | int amt = 100; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 489 | freelist = (struct action *)calloc(amt, sizeof(struct action)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 490 | if( freelist==0 ){ |
| 491 | fprintf(stderr,"Unable to allocate memory for a new parser action."); |
| 492 | exit(1); |
| 493 | } |
| 494 | for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1]; |
| 495 | freelist[amt-1].next = 0; |
| 496 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 497 | newaction = freelist; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 498 | freelist = freelist->next; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 499 | return newaction; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 500 | } |
| 501 | |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 502 | /* Compare two actions for sorting purposes. Return negative, zero, or |
| 503 | ** positive if the first action is less than, equal to, or greater than |
| 504 | ** the first |
| 505 | */ |
| 506 | static int actioncmp( |
| 507 | struct action *ap1, |
| 508 | struct action *ap2 |
| 509 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 510 | int rc; |
| 511 | rc = ap1->sp->index - ap2->sp->index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 512 | if( rc==0 ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 513 | rc = (int)ap1->type - (int)ap2->type; |
| 514 | } |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 515 | if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 516 | rc = ap1->x.rp->index - ap2->x.rp->index; |
| 517 | } |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 518 | if( rc==0 ){ |
icculus | 7b429aa | 2010-03-03 17:09:01 +0000 | [diff] [blame] | 519 | rc = (int) (ap2 - ap1); |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 520 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 521 | return rc; |
| 522 | } |
| 523 | |
| 524 | /* Sort parser actions */ |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 525 | static struct action *Action_sort( |
| 526 | struct action *ap |
| 527 | ){ |
| 528 | ap = (struct action *)msort((char *)ap,(char **)&ap->next, |
| 529 | (int(*)(const char*,const char*))actioncmp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 530 | return ap; |
| 531 | } |
| 532 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 533 | void Action_add( |
| 534 | struct action **app, |
| 535 | enum e_action type, |
| 536 | struct symbol *sp, |
| 537 | char *arg |
| 538 | ){ |
| 539 | struct action *newaction; |
| 540 | newaction = Action_new(); |
| 541 | newaction->next = *app; |
| 542 | *app = newaction; |
| 543 | newaction->type = type; |
| 544 | newaction->sp = sp; |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 545 | newaction->spOpt = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 546 | if( type==SHIFT ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 547 | newaction->x.stp = (struct state *)arg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 548 | }else{ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 549 | newaction->x.rp = (struct rule *)arg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 552 | /********************** New code to implement the "acttab" module ***********/ |
| 553 | /* |
| 554 | ** This module implements routines use to construct the yy_action[] table. |
| 555 | */ |
| 556 | |
| 557 | /* |
| 558 | ** The state of the yy_action table under construction is an instance of |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 559 | ** the following structure. |
| 560 | ** |
| 561 | ** The yy_action table maps the pair (state_number, lookahead) into an |
| 562 | ** action_number. The table is an array of integers pairs. The state_number |
| 563 | ** determines an initial offset into the yy_action array. The lookahead |
| 564 | ** value is then added to this initial offset to get an index X into the |
| 565 | ** yy_action array. If the aAction[X].lookahead equals the value of the |
| 566 | ** of the lookahead input, then the value of the action_number output is |
| 567 | ** aAction[X].action. If the lookaheads do not match then the |
| 568 | ** default action for the state_number is returned. |
| 569 | ** |
| 570 | ** All actions associated with a single state_number are first entered |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 571 | ** into aLookahead[] using multiple calls to acttab_action(). Then the |
| 572 | ** actions for that single state_number are placed into the aAction[] |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 573 | ** array with a single call to acttab_insert(). The acttab_insert() call |
| 574 | ** also resets the aLookahead[] array in preparation for the next |
| 575 | ** state number. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 576 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 577 | struct lookahead_action { |
| 578 | int lookahead; /* Value of the lookahead token */ |
| 579 | int action; /* Action to take on the given lookahead */ |
| 580 | }; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 581 | typedef struct acttab acttab; |
| 582 | struct acttab { |
| 583 | int nAction; /* Number of used slots in aAction[] */ |
| 584 | int nActionAlloc; /* Slots allocated for aAction[] */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 585 | struct lookahead_action |
| 586 | *aAction, /* The yy_action[] table under construction */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 587 | *aLookahead; /* A single new transaction set */ |
| 588 | int mnLookahead; /* Minimum aLookahead[].lookahead */ |
| 589 | int mnAction; /* Action associated with mnLookahead */ |
| 590 | int mxLookahead; /* Maximum aLookahead[].lookahead */ |
| 591 | int nLookahead; /* Used slots in aLookahead[] */ |
| 592 | int nLookaheadAlloc; /* Slots allocated in aLookahead[] */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 593 | int nterminal; /* Number of terminal symbols */ |
| 594 | int nsymbol; /* total number of symbols */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 595 | }; |
| 596 | |
| 597 | /* Return the number of entries in the yy_action table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 598 | #define acttab_lookahead_size(X) ((X)->nAction) |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 599 | |
| 600 | /* The value for the N-th entry in yy_action */ |
| 601 | #define acttab_yyaction(X,N) ((X)->aAction[N].action) |
| 602 | |
| 603 | /* The value for the N-th entry in yy_lookahead */ |
| 604 | #define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead) |
| 605 | |
| 606 | /* Free all memory associated with the given acttab */ |
| 607 | void acttab_free(acttab *p){ |
| 608 | free( p->aAction ); |
| 609 | free( p->aLookahead ); |
| 610 | free( p ); |
| 611 | } |
| 612 | |
| 613 | /* Allocate a new acttab structure */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 614 | acttab *acttab_alloc(int nsymbol, int nterminal){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 615 | acttab *p = (acttab *) calloc( 1, sizeof(*p) ); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 616 | if( p==0 ){ |
| 617 | fprintf(stderr,"Unable to allocate memory for a new acttab."); |
| 618 | exit(1); |
| 619 | } |
| 620 | memset(p, 0, sizeof(*p)); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 621 | p->nsymbol = nsymbol; |
| 622 | p->nterminal = nterminal; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 623 | return p; |
| 624 | } |
| 625 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 626 | /* Add a new action to the current transaction set. |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 627 | ** |
| 628 | ** This routine is called once for each lookahead for a particular |
| 629 | ** state. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 630 | */ |
| 631 | void acttab_action(acttab *p, int lookahead, int action){ |
| 632 | if( p->nLookahead>=p->nLookaheadAlloc ){ |
| 633 | p->nLookaheadAlloc += 25; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 634 | p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead, |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 635 | sizeof(p->aLookahead[0])*p->nLookaheadAlloc ); |
| 636 | if( p->aLookahead==0 ){ |
| 637 | fprintf(stderr,"malloc failed\n"); |
| 638 | exit(1); |
| 639 | } |
| 640 | } |
| 641 | if( p->nLookahead==0 ){ |
| 642 | p->mxLookahead = lookahead; |
| 643 | p->mnLookahead = lookahead; |
| 644 | p->mnAction = action; |
| 645 | }else{ |
| 646 | if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead; |
| 647 | if( p->mnLookahead>lookahead ){ |
| 648 | p->mnLookahead = lookahead; |
| 649 | p->mnAction = action; |
| 650 | } |
| 651 | } |
| 652 | p->aLookahead[p->nLookahead].lookahead = lookahead; |
| 653 | p->aLookahead[p->nLookahead].action = action; |
| 654 | p->nLookahead++; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | ** Add the transaction set built up with prior calls to acttab_action() |
| 659 | ** into the current action table. Then reset the transaction set back |
| 660 | ** to an empty set in preparation for a new round of acttab_action() calls. |
| 661 | ** |
| 662 | ** Return the offset into the action table of the new transaction. |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 663 | ** |
| 664 | ** If the makeItSafe parameter is true, then the offset is chosen so that |
| 665 | ** it is impossible to overread the yy_lookaside[] table regardless of |
| 666 | ** the lookaside token. This is done for the terminal symbols, as they |
| 667 | ** come from external inputs and can contain syntax errors. When makeItSafe |
| 668 | ** is false, there is more flexibility in selecting offsets, resulting in |
| 669 | ** a smaller table. For non-terminal symbols, which are never syntax errors, |
| 670 | ** makeItSafe can be false. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 671 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 672 | int acttab_insert(acttab *p, int makeItSafe){ |
| 673 | int i, j, k, n, end; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 674 | assert( p->nLookahead>0 ); |
| 675 | |
| 676 | /* Make sure we have enough space to hold the expanded action table |
| 677 | ** in the worst case. The worst case occurs if the transaction set |
| 678 | ** must be appended to the current action table |
| 679 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 680 | n = p->nsymbol + 1; |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 681 | if( p->nAction + n >= p->nActionAlloc ){ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 682 | int oldAlloc = p->nActionAlloc; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 683 | p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 684 | p->aAction = (struct lookahead_action *) realloc( p->aAction, |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 685 | sizeof(p->aAction[0])*p->nActionAlloc); |
| 686 | if( p->aAction==0 ){ |
| 687 | fprintf(stderr,"malloc failed\n"); |
| 688 | exit(1); |
| 689 | } |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 690 | for(i=oldAlloc; i<p->nActionAlloc; i++){ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 691 | p->aAction[i].lookahead = -1; |
| 692 | p->aAction[i].action = -1; |
| 693 | } |
| 694 | } |
| 695 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 696 | /* Scan the existing action table looking for an offset that is a |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 697 | ** duplicate of the current transaction set. Fall out of the loop |
| 698 | ** if and when the duplicate is found. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 699 | ** |
| 700 | ** i is the index in p->aAction[] where p->mnLookahead is inserted. |
| 701 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 702 | end = makeItSafe ? p->mnLookahead : 0; |
| 703 | for(i=p->nAction-1; i>=end; i--){ |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 704 | if( p->aAction[i].lookahead==p->mnLookahead ){ |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 705 | /* All lookaheads and actions in the aLookahead[] transaction |
| 706 | ** must match against the candidate aAction[i] entry. */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 707 | if( p->aAction[i].action!=p->mnAction ) continue; |
| 708 | for(j=0; j<p->nLookahead; j++){ |
| 709 | k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| 710 | if( k<0 || k>=p->nAction ) break; |
| 711 | if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break; |
| 712 | if( p->aLookahead[j].action!=p->aAction[k].action ) break; |
| 713 | } |
| 714 | if( j<p->nLookahead ) continue; |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 715 | |
| 716 | /* No possible lookahead value that is not in the aLookahead[] |
| 717 | ** transaction is allowed to match aAction[i] */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 718 | n = 0; |
| 719 | for(j=0; j<p->nAction; j++){ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 720 | if( p->aAction[j].lookahead<0 ) continue; |
| 721 | if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 722 | } |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 723 | if( n==p->nLookahead ){ |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 724 | break; /* An exact match is found at offset i */ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 725 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 728 | |
| 729 | /* If no existing offsets exactly match the current transaction, find an |
| 730 | ** an empty offset in the aAction[] table in which we can add the |
| 731 | ** aLookahead[] transaction. |
| 732 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 733 | if( i<end ){ |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 734 | /* Look for holes in the aAction[] table that fit the current |
| 735 | ** aLookahead[] transaction. Leave i set to the offset of the hole. |
| 736 | ** If no holes are found, i is left at p->nAction, which means the |
| 737 | ** transaction will be appended. */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 738 | i = makeItSafe ? p->mnLookahead : 0; |
| 739 | for(; i<p->nActionAlloc - p->mxLookahead; i++){ |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 740 | if( p->aAction[i].lookahead<0 ){ |
| 741 | for(j=0; j<p->nLookahead; j++){ |
| 742 | k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| 743 | if( k<0 ) break; |
| 744 | if( p->aAction[k].lookahead>=0 ) break; |
| 745 | } |
| 746 | if( j<p->nLookahead ) continue; |
| 747 | for(j=0; j<p->nAction; j++){ |
| 748 | if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break; |
| 749 | } |
| 750 | if( j==p->nAction ){ |
| 751 | break; /* Fits in empty slots */ |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 756 | /* Insert transaction set at index i. */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 757 | #if 0 |
| 758 | printf("Acttab:"); |
| 759 | for(j=0; j<p->nLookahead; j++){ |
| 760 | printf(" %d", p->aLookahead[j].lookahead); |
| 761 | } |
| 762 | printf(" inserted at %d\n", i); |
| 763 | #endif |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 764 | for(j=0; j<p->nLookahead; j++){ |
| 765 | k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| 766 | p->aAction[k] = p->aLookahead[j]; |
| 767 | if( k>=p->nAction ) p->nAction = k+1; |
| 768 | } |
drh | 4396c61 | 2017-12-27 15:21:16 +0000 | [diff] [blame] | 769 | if( makeItSafe && i+p->nterminal>=p->nAction ) p->nAction = i+p->nterminal+1; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 770 | p->nLookahead = 0; |
| 771 | |
| 772 | /* Return the offset that is added to the lookahead in order to get the |
| 773 | ** index into yy_action of the action */ |
| 774 | return i - p->mnLookahead; |
| 775 | } |
| 776 | |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 777 | /* |
| 778 | ** Return the size of the action table without the trailing syntax error |
| 779 | ** entries. |
| 780 | */ |
| 781 | int acttab_action_size(acttab *p){ |
| 782 | int n = p->nAction; |
| 783 | while( n>0 && p->aAction[n-1].lookahead<0 ){ n--; } |
| 784 | return n; |
| 785 | } |
| 786 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 787 | /********************** From the file "build.c" *****************************/ |
| 788 | /* |
| 789 | ** Routines to construction the finite state machine for the LEMON |
| 790 | ** parser generator. |
| 791 | */ |
| 792 | |
| 793 | /* Find a precedence symbol of every rule in the grammar. |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 794 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 795 | ** Those rules which have a precedence symbol coded in the input |
| 796 | ** grammar using the "[symbol]" construct will already have the |
| 797 | ** rp->precsym field filled. Other rules take as their precedence |
| 798 | ** symbol the first RHS symbol with a defined precedence. If there |
| 799 | ** are not RHS symbols with a defined precedence, the precedence |
| 800 | ** symbol field is left blank. |
| 801 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 802 | void FindRulePrecedences(struct lemon *xp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 803 | { |
| 804 | struct rule *rp; |
| 805 | for(rp=xp->rule; rp; rp=rp->next){ |
| 806 | if( rp->precsym==0 ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 807 | int i, j; |
| 808 | for(i=0; i<rp->nrhs && rp->precsym==0; i++){ |
| 809 | struct symbol *sp = rp->rhs[i]; |
| 810 | if( sp->type==MULTITERMINAL ){ |
| 811 | for(j=0; j<sp->nsubsym; j++){ |
| 812 | if( sp->subsym[j]->prec>=0 ){ |
| 813 | rp->precsym = sp->subsym[j]; |
| 814 | break; |
| 815 | } |
| 816 | } |
| 817 | }else if( sp->prec>=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 818 | rp->precsym = rp->rhs[i]; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 819 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | } |
| 823 | return; |
| 824 | } |
| 825 | |
| 826 | /* Find all nonterminals which will generate the empty string. |
| 827 | ** Then go back and compute the first sets of every nonterminal. |
| 828 | ** The first set is the set of all terminal symbols which can begin |
| 829 | ** a string generated by that nonterminal. |
| 830 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 831 | void FindFirstSets(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 832 | { |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 833 | int i, j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 834 | struct rule *rp; |
| 835 | int progress; |
| 836 | |
| 837 | for(i=0; i<lemp->nsymbol; i++){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 838 | lemp->symbols[i]->lambda = LEMON_FALSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 839 | } |
| 840 | for(i=lemp->nterminal; i<lemp->nsymbol; i++){ |
| 841 | lemp->symbols[i]->firstset = SetNew(); |
| 842 | } |
| 843 | |
| 844 | /* First compute all lambdas */ |
| 845 | do{ |
| 846 | progress = 0; |
| 847 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 848 | if( rp->lhs->lambda ) continue; |
| 849 | for(i=0; i<rp->nrhs; i++){ |
drh | 7dd1ac6 | 2012-01-07 15:17:18 +0000 | [diff] [blame] | 850 | struct symbol *sp = rp->rhs[i]; |
| 851 | assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE ); |
| 852 | if( sp->lambda==LEMON_FALSE ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 853 | } |
| 854 | if( i==rp->nrhs ){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 855 | rp->lhs->lambda = LEMON_TRUE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 856 | progress = 1; |
| 857 | } |
| 858 | } |
| 859 | }while( progress ); |
| 860 | |
| 861 | /* Now compute all first sets */ |
| 862 | do{ |
| 863 | struct symbol *s1, *s2; |
| 864 | progress = 0; |
| 865 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 866 | s1 = rp->lhs; |
| 867 | for(i=0; i<rp->nrhs; i++){ |
| 868 | s2 = rp->rhs[i]; |
| 869 | if( s2->type==TERMINAL ){ |
| 870 | progress += SetAdd(s1->firstset,s2->index); |
| 871 | break; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 872 | }else if( s2->type==MULTITERMINAL ){ |
| 873 | for(j=0; j<s2->nsubsym; j++){ |
| 874 | progress += SetAdd(s1->firstset,s2->subsym[j]->index); |
| 875 | } |
| 876 | break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 877 | }else if( s1==s2 ){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 878 | if( s1->lambda==LEMON_FALSE ) break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 879 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 880 | progress += SetUnion(s1->firstset,s2->firstset); |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 881 | if( s2->lambda==LEMON_FALSE ) break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 882 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | }while( progress ); |
| 886 | return; |
| 887 | } |
| 888 | |
| 889 | /* Compute all LR(0) states for the grammar. Links |
| 890 | ** are added to between some states so that the LR(1) follow sets |
| 891 | ** can be computed later. |
| 892 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 893 | PRIVATE struct state *getstate(struct lemon *); /* forward reference */ |
| 894 | void FindStates(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 895 | { |
| 896 | struct symbol *sp; |
| 897 | struct rule *rp; |
| 898 | |
| 899 | Configlist_init(); |
| 900 | |
| 901 | /* Find the start symbol */ |
| 902 | if( lemp->start ){ |
| 903 | sp = Symbol_find(lemp->start); |
| 904 | if( sp==0 ){ |
| 905 | ErrorMsg(lemp->filename,0, |
| 906 | "The specified start symbol \"%s\" is not \ |
| 907 | in a nonterminal of the grammar. \"%s\" will be used as the start \ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 908 | symbol instead.",lemp->start,lemp->startRule->lhs->name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 909 | lemp->errorcnt++; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 910 | sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 911 | } |
| 912 | }else{ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 913 | sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* Make sure the start symbol doesn't occur on the right-hand side of |
| 917 | ** any rule. Report an error if it does. (YACC would generate a new |
| 918 | ** start symbol in this case.) */ |
| 919 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 920 | int i; |
| 921 | for(i=0; i<rp->nrhs; i++){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 922 | if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 923 | ErrorMsg(lemp->filename,0, |
| 924 | "The start symbol \"%s\" occurs on the \ |
| 925 | right-hand side of a rule. This will result in a parser which \ |
| 926 | does not work properly.",sp->name); |
| 927 | lemp->errorcnt++; |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /* The basis configuration set for the first state |
| 933 | ** is all rules which have the start symbol as their |
| 934 | ** left-hand side */ |
| 935 | for(rp=sp->rule; rp; rp=rp->nextlhs){ |
| 936 | struct config *newcfp; |
drh | b496099 | 2007-10-05 16:16:36 +0000 | [diff] [blame] | 937 | rp->lhsStart = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 938 | newcfp = Configlist_addbasis(rp,0); |
| 939 | SetAdd(newcfp->fws,0); |
| 940 | } |
| 941 | |
| 942 | /* Compute the first state. All other states will be |
| 943 | ** computed automatically during the computation of the first one. |
| 944 | ** The returned pointer to the first state is not used. */ |
| 945 | (void)getstate(lemp); |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | /* Return a pointer to a state which is described by the configuration |
| 950 | ** list which has been built from calls to Configlist_add. |
| 951 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 952 | PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */ |
| 953 | PRIVATE struct state *getstate(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 954 | { |
| 955 | struct config *cfp, *bp; |
| 956 | struct state *stp; |
| 957 | |
| 958 | /* Extract the sorted basis of the new state. The basis was constructed |
| 959 | ** by prior calls to "Configlist_addbasis()". */ |
| 960 | Configlist_sortbasis(); |
| 961 | bp = Configlist_basis(); |
| 962 | |
| 963 | /* Get a state with the same basis */ |
| 964 | stp = State_find(bp); |
| 965 | if( stp ){ |
| 966 | /* A state with the same basis already exists! Copy all the follow-set |
| 967 | ** propagation links from the state under construction into the |
| 968 | ** preexisting state, then return a pointer to the preexisting state */ |
| 969 | struct config *x, *y; |
| 970 | for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){ |
| 971 | Plink_copy(&y->bplp,x->bplp); |
| 972 | Plink_delete(x->fplp); |
| 973 | x->fplp = x->bplp = 0; |
| 974 | } |
| 975 | cfp = Configlist_return(); |
| 976 | Configlist_eat(cfp); |
| 977 | }else{ |
| 978 | /* This really is a new state. Construct all the details */ |
| 979 | Configlist_closure(lemp); /* Compute the configuration closure */ |
| 980 | Configlist_sort(); /* Sort the configuration closure */ |
| 981 | cfp = Configlist_return(); /* Get a pointer to the config list */ |
| 982 | stp = State_new(); /* A new state structure */ |
| 983 | MemoryCheck(stp); |
| 984 | stp->bp = bp; /* Remember the configuration basis */ |
| 985 | stp->cfp = cfp; /* Remember the configuration closure */ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 986 | stp->statenum = lemp->nstate++; /* Every state gets a sequence number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 987 | stp->ap = 0; /* No actions, yet. */ |
| 988 | State_insert(stp,stp->bp); /* Add to the state table */ |
| 989 | buildshifts(lemp,stp); /* Recursively compute successor states */ |
| 990 | } |
| 991 | return stp; |
| 992 | } |
| 993 | |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 994 | /* |
| 995 | ** Return true if two symbols are the same. |
| 996 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 997 | int same_symbol(struct symbol *a, struct symbol *b) |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 998 | { |
| 999 | int i; |
| 1000 | if( a==b ) return 1; |
| 1001 | if( a->type!=MULTITERMINAL ) return 0; |
| 1002 | if( b->type!=MULTITERMINAL ) return 0; |
| 1003 | if( a->nsubsym!=b->nsubsym ) return 0; |
| 1004 | for(i=0; i<a->nsubsym; i++){ |
| 1005 | if( a->subsym[i]!=b->subsym[i] ) return 0; |
| 1006 | } |
| 1007 | return 1; |
| 1008 | } |
| 1009 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1010 | /* Construct all successor states to the given state. A "successor" |
| 1011 | ** state is any state which can be reached by a shift action. |
| 1012 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1013 | PRIVATE void buildshifts(struct lemon *lemp, struct state *stp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1014 | { |
| 1015 | struct config *cfp; /* For looping thru the config closure of "stp" */ |
| 1016 | struct config *bcfp; /* For the inner loop on config closure of "stp" */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1017 | struct config *newcfg; /* */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1018 | struct symbol *sp; /* Symbol following the dot in configuration "cfp" */ |
| 1019 | struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */ |
| 1020 | struct state *newstp; /* A pointer to a successor state */ |
| 1021 | |
| 1022 | /* Each configuration becomes complete after it contibutes to a successor |
| 1023 | ** state. Initially, all configurations are incomplete */ |
| 1024 | for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE; |
| 1025 | |
| 1026 | /* Loop through all configurations of the state "stp" */ |
| 1027 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| 1028 | if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */ |
| 1029 | if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */ |
| 1030 | Configlist_reset(); /* Reset the new config set */ |
| 1031 | sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */ |
| 1032 | |
| 1033 | /* For every configuration in the state "stp" which has the symbol "sp" |
| 1034 | ** following its dot, add the same configuration to the basis set under |
| 1035 | ** construction but with the dot shifted one symbol to the right. */ |
| 1036 | for(bcfp=cfp; bcfp; bcfp=bcfp->next){ |
| 1037 | if( bcfp->status==COMPLETE ) continue; /* Already used */ |
| 1038 | if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */ |
| 1039 | bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1040 | if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1041 | bcfp->status = COMPLETE; /* Mark this config as used */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1042 | newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1); |
| 1043 | Plink_add(&newcfg->bplp,bcfp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | /* Get a pointer to the state described by the basis configuration set |
| 1047 | ** constructed in the preceding loop */ |
| 1048 | newstp = getstate(lemp); |
| 1049 | |
| 1050 | /* The state "newstp" is reached from the state "stp" by a shift action |
| 1051 | ** on the symbol "sp" */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1052 | if( sp->type==MULTITERMINAL ){ |
| 1053 | int i; |
| 1054 | for(i=0; i<sp->nsubsym; i++){ |
| 1055 | Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp); |
| 1056 | } |
| 1057 | }else{ |
| 1058 | Action_add(&stp->ap,SHIFT,sp,(char *)newstp); |
| 1059 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | ** Construct the propagation links |
| 1065 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1066 | void FindLinks(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1067 | { |
| 1068 | int i; |
| 1069 | struct config *cfp, *other; |
| 1070 | struct state *stp; |
| 1071 | struct plink *plp; |
| 1072 | |
| 1073 | /* Housekeeping detail: |
| 1074 | ** Add to every propagate link a pointer back to the state to |
| 1075 | ** which the link is attached. */ |
| 1076 | for(i=0; i<lemp->nstate; i++){ |
| 1077 | stp = lemp->sorted[i]; |
| 1078 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| 1079 | cfp->stp = stp; |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /* Convert all backlinks into forward links. Only the forward |
| 1084 | ** links are used in the follow-set computation. */ |
| 1085 | for(i=0; i<lemp->nstate; i++){ |
| 1086 | stp = lemp->sorted[i]; |
| 1087 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| 1088 | for(plp=cfp->bplp; plp; plp=plp->next){ |
| 1089 | other = plp->cfp; |
| 1090 | Plink_add(&other->fplp,cfp); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | /* Compute all followsets. |
| 1097 | ** |
| 1098 | ** A followset is the set of all symbols which can come immediately |
| 1099 | ** after a configuration. |
| 1100 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1101 | void FindFollowSets(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1102 | { |
| 1103 | int i; |
| 1104 | struct config *cfp; |
| 1105 | struct plink *plp; |
| 1106 | int progress; |
| 1107 | int change; |
| 1108 | |
| 1109 | for(i=0; i<lemp->nstate; i++){ |
| 1110 | for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ |
| 1111 | cfp->status = INCOMPLETE; |
| 1112 | } |
| 1113 | } |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1114 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1115 | do{ |
| 1116 | progress = 0; |
| 1117 | for(i=0; i<lemp->nstate; i++){ |
| 1118 | for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ |
| 1119 | if( cfp->status==COMPLETE ) continue; |
| 1120 | for(plp=cfp->fplp; plp; plp=plp->next){ |
| 1121 | change = SetUnion(plp->cfp->fws,cfp->fws); |
| 1122 | if( change ){ |
| 1123 | plp->cfp->status = INCOMPLETE; |
| 1124 | progress = 1; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1125 | } |
| 1126 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1127 | cfp->status = COMPLETE; |
| 1128 | } |
| 1129 | } |
| 1130 | }while( progress ); |
| 1131 | } |
| 1132 | |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 1133 | static int resolve_conflict(struct action *,struct action *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1134 | |
| 1135 | /* Compute the reduce actions, and resolve conflicts. |
| 1136 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1137 | void FindActions(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1138 | { |
| 1139 | int i,j; |
| 1140 | struct config *cfp; |
| 1141 | struct state *stp; |
| 1142 | struct symbol *sp; |
| 1143 | struct rule *rp; |
| 1144 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1145 | /* Add all of the reduce actions |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1146 | ** A reduce action is added for each element of the followset of |
| 1147 | ** a configuration which has its dot at the extreme right. |
| 1148 | */ |
| 1149 | for(i=0; i<lemp->nstate; i++){ /* Loop over all states */ |
| 1150 | stp = lemp->sorted[i]; |
| 1151 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */ |
| 1152 | if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */ |
| 1153 | for(j=0; j<lemp->nterminal; j++){ |
| 1154 | if( SetFind(cfp->fws,j) ){ |
| 1155 | /* Add a reduce action to the state "stp" which will reduce by the |
| 1156 | ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */ |
drh | 218dc69 | 2004-05-31 23:13:45 +0000 | [diff] [blame] | 1157 | Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1158 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1159 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | /* Add the accepting token */ |
| 1165 | if( lemp->start ){ |
| 1166 | sp = Symbol_find(lemp->start); |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1167 | if( sp==0 ) sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1168 | }else{ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1169 | sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1170 | } |
| 1171 | /* Add to the first state (which is always the starting state of the |
| 1172 | ** finite state machine) an action to ACCEPT if the lookahead is the |
| 1173 | ** start nonterminal. */ |
| 1174 | Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0); |
| 1175 | |
| 1176 | /* Resolve conflicts */ |
| 1177 | for(i=0; i<lemp->nstate; i++){ |
| 1178 | struct action *ap, *nap; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1179 | stp = lemp->sorted[i]; |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 1180 | /* assert( stp->ap ); */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1181 | stp->ap = Action_sort(stp->ap); |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1182 | for(ap=stp->ap; ap && ap->next; ap=ap->next){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1183 | for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){ |
| 1184 | /* The two actions "ap" and "nap" have the same lookahead. |
| 1185 | ** Figure out which one should be used */ |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 1186 | lemp->nconflict += resolve_conflict(ap,nap); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | /* Report an error for each rule that can never be reduced. */ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 1192 | for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1193 | for(i=0; i<lemp->nstate; i++){ |
| 1194 | struct action *ap; |
| 1195 | for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 1196 | if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 1200 | if( rp->canReduce ) continue; |
| 1201 | ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n"); |
| 1202 | lemp->errorcnt++; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /* Resolve a conflict between the two given actions. If the |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 1207 | ** conflict can't be resolved, return non-zero. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1208 | ** |
| 1209 | ** NO LONGER TRUE: |
| 1210 | ** To resolve a conflict, first look to see if either action |
| 1211 | ** is on an error rule. In that case, take the action which |
| 1212 | ** is not associated with the error rule. If neither or both |
| 1213 | ** actions are associated with an error rule, then try to |
| 1214 | ** use precedence to resolve the conflict. |
| 1215 | ** |
| 1216 | ** If either action is a SHIFT, then it must be apx. This |
| 1217 | ** function won't work if apx->type==REDUCE and apy->type==SHIFT. |
| 1218 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1219 | static int resolve_conflict( |
| 1220 | struct action *apx, |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 1221 | struct action *apy |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1222 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1223 | struct symbol *spx, *spy; |
| 1224 | int errcnt = 0; |
| 1225 | assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */ |
drh | f0fa1c1 | 2006-12-14 01:06:22 +0000 | [diff] [blame] | 1226 | if( apx->type==SHIFT && apy->type==SHIFT ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1227 | apy->type = SSCONFLICT; |
drh | f0fa1c1 | 2006-12-14 01:06:22 +0000 | [diff] [blame] | 1228 | errcnt++; |
| 1229 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1230 | if( apx->type==SHIFT && apy->type==REDUCE ){ |
| 1231 | spx = apx->sp; |
| 1232 | spy = apy->x.rp->precsym; |
| 1233 | if( spy==0 || spx->prec<0 || spy->prec<0 ){ |
| 1234 | /* Not enough precedence information. */ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1235 | apy->type = SRCONFLICT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1236 | errcnt++; |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1237 | }else if( spx->prec>spy->prec ){ /* higher precedence wins */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1238 | apy->type = RD_RESOLVED; |
| 1239 | }else if( spx->prec<spy->prec ){ |
| 1240 | apx->type = SH_RESOLVED; |
| 1241 | }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */ |
| 1242 | apy->type = RD_RESOLVED; /* associativity */ |
| 1243 | }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */ |
| 1244 | apx->type = SH_RESOLVED; |
| 1245 | }else{ |
| 1246 | assert( spx->prec==spy->prec && spx->assoc==NONE ); |
drh | 62a223e | 2014-06-09 13:11:40 +0000 | [diff] [blame] | 1247 | apx->type = ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1248 | } |
| 1249 | }else if( apx->type==REDUCE && apy->type==REDUCE ){ |
| 1250 | spx = apx->x.rp->precsym; |
| 1251 | spy = apy->x.rp->precsym; |
| 1252 | if( spx==0 || spy==0 || spx->prec<0 || |
| 1253 | spy->prec<0 || spx->prec==spy->prec ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1254 | apy->type = RRCONFLICT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1255 | errcnt++; |
| 1256 | }else if( spx->prec>spy->prec ){ |
| 1257 | apy->type = RD_RESOLVED; |
| 1258 | }else if( spx->prec<spy->prec ){ |
| 1259 | apx->type = RD_RESOLVED; |
| 1260 | } |
| 1261 | }else{ |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1262 | assert( |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1263 | apx->type==SH_RESOLVED || |
| 1264 | apx->type==RD_RESOLVED || |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1265 | apx->type==SSCONFLICT || |
| 1266 | apx->type==SRCONFLICT || |
| 1267 | apx->type==RRCONFLICT || |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1268 | apy->type==SH_RESOLVED || |
| 1269 | apy->type==RD_RESOLVED || |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1270 | apy->type==SSCONFLICT || |
| 1271 | apy->type==SRCONFLICT || |
| 1272 | apy->type==RRCONFLICT |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1273 | ); |
| 1274 | /* The REDUCE/SHIFT case cannot happen because SHIFTs come before |
| 1275 | ** REDUCEs on the list. If we reach this point it must be because |
| 1276 | ** the parser conflict had already been resolved. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1277 | } |
| 1278 | return errcnt; |
| 1279 | } |
| 1280 | /********************* From the file "configlist.c" *************************/ |
| 1281 | /* |
| 1282 | ** Routines to processing a configuration list and building a state |
| 1283 | ** in the LEMON parser generator. |
| 1284 | */ |
| 1285 | |
| 1286 | static struct config *freelist = 0; /* List of free configurations */ |
| 1287 | static struct config *current = 0; /* Top of list of configurations */ |
| 1288 | static struct config **currentend = 0; /* Last on list of configs */ |
| 1289 | static struct config *basis = 0; /* Top of list of basis configs */ |
| 1290 | static struct config **basisend = 0; /* End of list of basis configs */ |
| 1291 | |
| 1292 | /* Return a pointer to a new configuration */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1293 | PRIVATE struct config *newconfig(void){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1294 | struct config *newcfg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1295 | if( freelist==0 ){ |
| 1296 | int i; |
| 1297 | int amt = 3; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1298 | freelist = (struct config *)calloc( amt, sizeof(struct config) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1299 | if( freelist==0 ){ |
| 1300 | fprintf(stderr,"Unable to allocate memory for a new configuration."); |
| 1301 | exit(1); |
| 1302 | } |
| 1303 | for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1]; |
| 1304 | freelist[amt-1].next = 0; |
| 1305 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1306 | newcfg = freelist; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1307 | freelist = freelist->next; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1308 | return newcfg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /* The configuration "old" is no longer used */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1312 | PRIVATE void deleteconfig(struct config *old) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1313 | { |
| 1314 | old->next = freelist; |
| 1315 | freelist = old; |
| 1316 | } |
| 1317 | |
| 1318 | /* Initialized the configuration list builder */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1319 | void Configlist_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1320 | current = 0; |
| 1321 | currentend = ¤t; |
| 1322 | basis = 0; |
| 1323 | basisend = &basis; |
| 1324 | Configtable_init(); |
| 1325 | return; |
| 1326 | } |
| 1327 | |
| 1328 | /* Initialized the configuration list builder */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1329 | void Configlist_reset(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1330 | current = 0; |
| 1331 | currentend = ¤t; |
| 1332 | basis = 0; |
| 1333 | basisend = &basis; |
| 1334 | Configtable_clear(0); |
| 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | /* Add another configuration to the configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1339 | struct config *Configlist_add( |
| 1340 | struct rule *rp, /* The rule */ |
| 1341 | int dot /* Index into the RHS of the rule where the dot goes */ |
| 1342 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1343 | struct config *cfp, model; |
| 1344 | |
| 1345 | assert( currentend!=0 ); |
| 1346 | model.rp = rp; |
| 1347 | model.dot = dot; |
| 1348 | cfp = Configtable_find(&model); |
| 1349 | if( cfp==0 ){ |
| 1350 | cfp = newconfig(); |
| 1351 | cfp->rp = rp; |
| 1352 | cfp->dot = dot; |
| 1353 | cfp->fws = SetNew(); |
| 1354 | cfp->stp = 0; |
| 1355 | cfp->fplp = cfp->bplp = 0; |
| 1356 | cfp->next = 0; |
| 1357 | cfp->bp = 0; |
| 1358 | *currentend = cfp; |
| 1359 | currentend = &cfp->next; |
| 1360 | Configtable_insert(cfp); |
| 1361 | } |
| 1362 | return cfp; |
| 1363 | } |
| 1364 | |
| 1365 | /* Add a basis configuration to the configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1366 | struct config *Configlist_addbasis(struct rule *rp, int dot) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1367 | { |
| 1368 | struct config *cfp, model; |
| 1369 | |
| 1370 | assert( basisend!=0 ); |
| 1371 | assert( currentend!=0 ); |
| 1372 | model.rp = rp; |
| 1373 | model.dot = dot; |
| 1374 | cfp = Configtable_find(&model); |
| 1375 | if( cfp==0 ){ |
| 1376 | cfp = newconfig(); |
| 1377 | cfp->rp = rp; |
| 1378 | cfp->dot = dot; |
| 1379 | cfp->fws = SetNew(); |
| 1380 | cfp->stp = 0; |
| 1381 | cfp->fplp = cfp->bplp = 0; |
| 1382 | cfp->next = 0; |
| 1383 | cfp->bp = 0; |
| 1384 | *currentend = cfp; |
| 1385 | currentend = &cfp->next; |
| 1386 | *basisend = cfp; |
| 1387 | basisend = &cfp->bp; |
| 1388 | Configtable_insert(cfp); |
| 1389 | } |
| 1390 | return cfp; |
| 1391 | } |
| 1392 | |
| 1393 | /* Compute the closure of the configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1394 | void Configlist_closure(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1395 | { |
| 1396 | struct config *cfp, *newcfp; |
| 1397 | struct rule *rp, *newrp; |
| 1398 | struct symbol *sp, *xsp; |
| 1399 | int i, dot; |
| 1400 | |
| 1401 | assert( currentend!=0 ); |
| 1402 | for(cfp=current; cfp; cfp=cfp->next){ |
| 1403 | rp = cfp->rp; |
| 1404 | dot = cfp->dot; |
| 1405 | if( dot>=rp->nrhs ) continue; |
| 1406 | sp = rp->rhs[dot]; |
| 1407 | if( sp->type==NONTERMINAL ){ |
| 1408 | if( sp->rule==0 && sp!=lemp->errsym ){ |
| 1409 | ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.", |
| 1410 | sp->name); |
| 1411 | lemp->errorcnt++; |
| 1412 | } |
| 1413 | for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){ |
| 1414 | newcfp = Configlist_add(newrp,0); |
| 1415 | for(i=dot+1; i<rp->nrhs; i++){ |
| 1416 | xsp = rp->rhs[i]; |
| 1417 | if( xsp->type==TERMINAL ){ |
| 1418 | SetAdd(newcfp->fws,xsp->index); |
| 1419 | break; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1420 | }else if( xsp->type==MULTITERMINAL ){ |
| 1421 | int k; |
| 1422 | for(k=0; k<xsp->nsubsym; k++){ |
| 1423 | SetAdd(newcfp->fws, xsp->subsym[k]->index); |
| 1424 | } |
| 1425 | break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1426 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1427 | SetUnion(newcfp->fws,xsp->firstset); |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 1428 | if( xsp->lambda==LEMON_FALSE ) break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1431 | if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp); |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | return; |
| 1436 | } |
| 1437 | |
| 1438 | /* Sort the configuration list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1439 | void Configlist_sort(void){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 1440 | current = (struct config*)msort((char*)current,(char**)&(current->next), |
| 1441 | Configcmp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1442 | currentend = 0; |
| 1443 | return; |
| 1444 | } |
| 1445 | |
| 1446 | /* Sort the basis configuration list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1447 | void Configlist_sortbasis(void){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 1448 | basis = (struct config*)msort((char*)current,(char**)&(current->bp), |
| 1449 | Configcmp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1450 | basisend = 0; |
| 1451 | return; |
| 1452 | } |
| 1453 | |
| 1454 | /* Return a pointer to the head of the configuration list and |
| 1455 | ** reset the list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1456 | struct config *Configlist_return(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1457 | struct config *old; |
| 1458 | old = current; |
| 1459 | current = 0; |
| 1460 | currentend = 0; |
| 1461 | return old; |
| 1462 | } |
| 1463 | |
| 1464 | /* Return a pointer to the head of the configuration list and |
| 1465 | ** reset the list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1466 | struct config *Configlist_basis(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1467 | struct config *old; |
| 1468 | old = basis; |
| 1469 | basis = 0; |
| 1470 | basisend = 0; |
| 1471 | return old; |
| 1472 | } |
| 1473 | |
| 1474 | /* Free all elements of the given configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1475 | void Configlist_eat(struct config *cfp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1476 | { |
| 1477 | struct config *nextcfp; |
| 1478 | for(; cfp; cfp=nextcfp){ |
| 1479 | nextcfp = cfp->next; |
| 1480 | assert( cfp->fplp==0 ); |
| 1481 | assert( cfp->bplp==0 ); |
| 1482 | if( cfp->fws ) SetFree(cfp->fws); |
| 1483 | deleteconfig(cfp); |
| 1484 | } |
| 1485 | return; |
| 1486 | } |
| 1487 | /***************** From the file "error.c" *********************************/ |
| 1488 | /* |
| 1489 | ** Code for printing error message. |
| 1490 | */ |
| 1491 | |
drh | f9a2e7b | 2003-04-15 01:49:48 +0000 | [diff] [blame] | 1492 | void ErrorMsg(const char *filename, int lineno, const char *format, ...){ |
icculus | 15a2cec | 2010-02-16 16:07:28 +0000 | [diff] [blame] | 1493 | va_list ap; |
icculus | 1c11f74 | 2010-02-15 00:01:04 +0000 | [diff] [blame] | 1494 | fprintf(stderr, "%s:%d: ", filename, lineno); |
| 1495 | va_start(ap, format); |
| 1496 | vfprintf(stderr,format,ap); |
| 1497 | va_end(ap); |
| 1498 | fprintf(stderr, "\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1499 | } |
| 1500 | /**************** From the file "main.c" ************************************/ |
| 1501 | /* |
| 1502 | ** Main program file for the LEMON parser generator. |
| 1503 | */ |
| 1504 | |
| 1505 | /* Report an out-of-memory condition and abort. This function |
| 1506 | ** is used mostly by the "MemoryCheck" macro in struct.h |
| 1507 | */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1508 | void memory_error(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1509 | fprintf(stderr,"Out of memory. Aborting...\n"); |
| 1510 | exit(1); |
| 1511 | } |
| 1512 | |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1513 | static int nDefine = 0; /* Number of -D options on the command line */ |
| 1514 | static char **azDefine = 0; /* Name of the -D macros */ |
| 1515 | |
| 1516 | /* This routine is called with the argument to each -D command-line option. |
| 1517 | ** Add the macro defined to the azDefine array. |
| 1518 | */ |
| 1519 | static void handle_D_option(char *z){ |
| 1520 | char **paz; |
| 1521 | nDefine++; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1522 | azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1523 | if( azDefine==0 ){ |
| 1524 | fprintf(stderr,"out of memory\n"); |
| 1525 | exit(1); |
| 1526 | } |
| 1527 | paz = &azDefine[nDefine-1]; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1528 | *paz = (char *) malloc( lemonStrlen(z)+1 ); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1529 | if( *paz==0 ){ |
| 1530 | fprintf(stderr,"out of memory\n"); |
| 1531 | exit(1); |
| 1532 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 1533 | lemon_strcpy(*paz, z); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1534 | for(z=*paz; *z && *z!='='; z++){} |
| 1535 | *z = 0; |
| 1536 | } |
| 1537 | |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 1538 | /* Rember the name of the output directory |
| 1539 | */ |
| 1540 | static char *outputDir = NULL; |
| 1541 | static void handle_d_option(char *z){ |
| 1542 | outputDir = (char *) malloc( lemonStrlen(z)+1 ); |
| 1543 | if( outputDir==0 ){ |
| 1544 | fprintf(stderr,"out of memory\n"); |
| 1545 | exit(1); |
| 1546 | } |
| 1547 | lemon_strcpy(outputDir, z); |
| 1548 | } |
| 1549 | |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 1550 | static char *user_templatename = NULL; |
| 1551 | static void handle_T_option(char *z){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1552 | user_templatename = (char *) malloc( lemonStrlen(z)+1 ); |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 1553 | if( user_templatename==0 ){ |
| 1554 | memory_error(); |
| 1555 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 1556 | lemon_strcpy(user_templatename, z); |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 1557 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1558 | |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 1559 | /* Merge together to lists of rules ordered by rule.iRule */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1560 | static struct rule *Rule_merge(struct rule *pA, struct rule *pB){ |
| 1561 | struct rule *pFirst = 0; |
| 1562 | struct rule **ppPrev = &pFirst; |
| 1563 | while( pA && pB ){ |
| 1564 | if( pA->iRule<pB->iRule ){ |
| 1565 | *ppPrev = pA; |
| 1566 | ppPrev = &pA->next; |
| 1567 | pA = pA->next; |
| 1568 | }else{ |
| 1569 | *ppPrev = pB; |
| 1570 | ppPrev = &pB->next; |
| 1571 | pB = pB->next; |
| 1572 | } |
| 1573 | } |
| 1574 | if( pA ){ |
| 1575 | *ppPrev = pA; |
| 1576 | }else{ |
| 1577 | *ppPrev = pB; |
| 1578 | } |
| 1579 | return pFirst; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | ** Sort a list of rules in order of increasing iRule value |
| 1584 | */ |
| 1585 | static struct rule *Rule_sort(struct rule *rp){ |
| 1586 | int i; |
| 1587 | struct rule *pNext; |
| 1588 | struct rule *x[32]; |
| 1589 | memset(x, 0, sizeof(x)); |
| 1590 | while( rp ){ |
| 1591 | pNext = rp->next; |
| 1592 | rp->next = 0; |
| 1593 | for(i=0; i<sizeof(x)/sizeof(x[0]) && x[i]; i++){ |
| 1594 | rp = Rule_merge(x[i], rp); |
| 1595 | x[i] = 0; |
| 1596 | } |
| 1597 | x[i] = rp; |
| 1598 | rp = pNext; |
| 1599 | } |
| 1600 | rp = 0; |
| 1601 | for(i=0; i<sizeof(x)/sizeof(x[0]); i++){ |
| 1602 | rp = Rule_merge(x[i], rp); |
| 1603 | } |
| 1604 | return rp; |
| 1605 | } |
| 1606 | |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1607 | /* forward reference */ |
| 1608 | static const char *minimum_size_type(int lwr, int upr, int *pnByte); |
| 1609 | |
| 1610 | /* Print a single line of the "Parser Stats" output |
| 1611 | */ |
| 1612 | static void stats_line(const char *zLabel, int iValue){ |
| 1613 | int nLabel = lemonStrlen(zLabel); |
| 1614 | printf(" %s%.*s %5d\n", zLabel, |
| 1615 | 35-nLabel, "................................", |
| 1616 | iValue); |
| 1617 | } |
| 1618 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1619 | /* The main program. Parse the command line and do it... */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1620 | int main(int argc, char **argv) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1621 | { |
| 1622 | static int version = 0; |
| 1623 | static int rpflag = 0; |
| 1624 | static int basisflag = 0; |
| 1625 | static int compress = 0; |
| 1626 | static int quiet = 0; |
| 1627 | static int statistics = 0; |
| 1628 | static int mhflag = 0; |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 1629 | static int nolinenosflag = 0; |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1630 | static int noResort = 0; |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 1631 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1632 | static struct s_options options[] = { |
| 1633 | {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."}, |
| 1634 | {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."}, |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 1635 | {OPT_FSTR, "d", (char*)&handle_d_option, "Output directory. Default '.'"}, |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1636 | {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1637 | {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1638 | {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1639 | {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"}, |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 1640 | {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."}, |
| 1641 | {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1642 | {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"}, |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 1643 | {OPT_FLAG, "p", (char*)&showPrecedenceConflict, |
| 1644 | "Show conflicts resolved by precedence rules"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1645 | {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."}, |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1646 | {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"}, |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1647 | {OPT_FLAG, "s", (char*)&statistics, |
| 1648 | "Print parser stats to standard output."}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1649 | {OPT_FLAG, "x", (char*)&version, "Print the version number."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1650 | {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."}, |
| 1651 | {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1652 | {OPT_FLAG,0,0,0} |
| 1653 | }; |
| 1654 | int i; |
icculus | 42585cf | 2010-02-14 05:19:56 +0000 | [diff] [blame] | 1655 | int exitcode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1656 | struct lemon lem; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1657 | struct rule *rp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1658 | |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 1659 | OptInit(argv,options,stderr); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1660 | if( version ){ |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1661 | printf("Lemon version 1.0\n"); |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1662 | exit(0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1663 | } |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 1664 | if( OptNArgs()!=1 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1665 | fprintf(stderr,"Exactly one filename argument is required.\n"); |
| 1666 | exit(1); |
| 1667 | } |
drh | 954f6b4 | 2006-06-13 13:27:46 +0000 | [diff] [blame] | 1668 | memset(&lem, 0, sizeof(lem)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1669 | lem.errorcnt = 0; |
| 1670 | |
| 1671 | /* Initialize the machine */ |
| 1672 | Strsafe_init(); |
| 1673 | Symbol_init(); |
| 1674 | State_init(); |
| 1675 | lem.argv0 = argv[0]; |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 1676 | lem.filename = OptArg(0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1677 | lem.basisflag = basisflag; |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 1678 | lem.nolinenosflag = nolinenosflag; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1679 | Symbol_new("$"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1680 | |
| 1681 | /* Parse the input file */ |
| 1682 | Parse(&lem); |
| 1683 | if( lem.errorcnt ) exit(lem.errorcnt); |
drh | 954f6b4 | 2006-06-13 13:27:46 +0000 | [diff] [blame] | 1684 | if( lem.nrule==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1685 | fprintf(stderr,"Empty grammar.\n"); |
| 1686 | exit(1); |
| 1687 | } |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 1688 | lem.errsym = Symbol_find("error"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1689 | |
| 1690 | /* Count and index the symbols of the grammar */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1691 | Symbol_new("{default}"); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 1692 | lem.nsymbol = Symbol_count(); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1693 | lem.symbols = Symbol_arrayof(); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 1694 | for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i; |
| 1695 | qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp); |
| 1696 | for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i; |
| 1697 | while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; } |
| 1698 | assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 ); |
| 1699 | lem.nsymbol = i - 1; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 1700 | for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1701 | lem.nterminal = i; |
| 1702 | |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 1703 | /* Assign sequential rule numbers. Start with 0. Put rules that have no |
| 1704 | ** reduce action C-code associated with them last, so that the switch() |
| 1705 | ** statement that selects reduction actions will have a smaller jump table. |
| 1706 | */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1707 | for(i=0, rp=lem.rule; rp; rp=rp->next){ |
| 1708 | rp->iRule = rp->code ? i++ : -1; |
| 1709 | } |
| 1710 | for(rp=lem.rule; rp; rp=rp->next){ |
| 1711 | if( rp->iRule<0 ) rp->iRule = i++; |
| 1712 | } |
| 1713 | lem.startRule = lem.rule; |
| 1714 | lem.rule = Rule_sort(lem.rule); |
| 1715 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1716 | /* Generate a reprint of the grammar, if requested on the command line */ |
| 1717 | if( rpflag ){ |
| 1718 | Reprint(&lem); |
| 1719 | }else{ |
| 1720 | /* Initialize the size for all follow and first sets */ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1721 | SetSize(lem.nterminal+1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1722 | |
| 1723 | /* Find the precedence for every production rule (that has one) */ |
| 1724 | FindRulePrecedences(&lem); |
| 1725 | |
| 1726 | /* Compute the lambda-nonterminals and the first-sets for every |
| 1727 | ** nonterminal */ |
| 1728 | FindFirstSets(&lem); |
| 1729 | |
| 1730 | /* Compute all LR(0) states. Also record follow-set propagation |
| 1731 | ** links so that the follow-set can be computed later */ |
| 1732 | lem.nstate = 0; |
| 1733 | FindStates(&lem); |
| 1734 | lem.sorted = State_arrayof(); |
| 1735 | |
| 1736 | /* Tie up loose ends on the propagation links */ |
| 1737 | FindLinks(&lem); |
| 1738 | |
| 1739 | /* Compute the follow set of every reducible configuration */ |
| 1740 | FindFollowSets(&lem); |
| 1741 | |
| 1742 | /* Compute the action tables */ |
| 1743 | FindActions(&lem); |
| 1744 | |
| 1745 | /* Compress the action tables */ |
| 1746 | if( compress==0 ) CompressTables(&lem); |
| 1747 | |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 1748 | /* Reorder and renumber the states so that states with fewer choices |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1749 | ** occur at the end. This is an optimization that helps make the |
| 1750 | ** generated parser tables smaller. */ |
| 1751 | if( noResort==0 ) ResortStates(&lem); |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 1752 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1753 | /* Generate a report of the parser generated. (the "y.output" file) */ |
| 1754 | if( !quiet ) ReportOutput(&lem); |
| 1755 | |
| 1756 | /* Generate the source code for the parser */ |
| 1757 | ReportTable(&lem, mhflag); |
| 1758 | |
| 1759 | /* Produce a header file for use by the scanner. (This step is |
| 1760 | ** omitted if the "-m" option is used because makeheaders will |
| 1761 | ** generate the file for us.) */ |
| 1762 | if( !mhflag ) ReportHeader(&lem); |
| 1763 | } |
| 1764 | if( statistics ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1765 | printf("Parser statistics:\n"); |
| 1766 | stats_line("terminal symbols", lem.nterminal); |
| 1767 | stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal); |
| 1768 | stats_line("total symbols", lem.nsymbol); |
| 1769 | stats_line("rules", lem.nrule); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 1770 | stats_line("states", lem.nxstate); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1771 | stats_line("conflicts", lem.nconflict); |
| 1772 | stats_line("action table entries", lem.nactiontab); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 1773 | stats_line("lookahead table entries", lem.nlookaheadtab); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1774 | stats_line("total table size (bytes)", lem.tablesize); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1775 | } |
icculus | 8e15802 | 2010-02-16 16:09:03 +0000 | [diff] [blame] | 1776 | if( lem.nconflict > 0 ){ |
| 1777 | fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict); |
icculus | 42585cf | 2010-02-14 05:19:56 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | /* return 0 on success, 1 on failure. */ |
icculus | 8e15802 | 2010-02-16 16:09:03 +0000 | [diff] [blame] | 1781 | exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0; |
icculus | 42585cf | 2010-02-14 05:19:56 +0000 | [diff] [blame] | 1782 | exit(exitcode); |
| 1783 | return (exitcode); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1784 | } |
| 1785 | /******************** From the file "msort.c" *******************************/ |
| 1786 | /* |
| 1787 | ** A generic merge-sort program. |
| 1788 | ** |
| 1789 | ** USAGE: |
| 1790 | ** Let "ptr" be a pointer to some structure which is at the head of |
| 1791 | ** a null-terminated list. Then to sort the list call: |
| 1792 | ** |
| 1793 | ** ptr = msort(ptr,&(ptr->next),cmpfnc); |
| 1794 | ** |
| 1795 | ** In the above, "cmpfnc" is a pointer to a function which compares |
| 1796 | ** two instances of the structure and returns an integer, as in |
| 1797 | ** strcmp. The second argument is a pointer to the pointer to the |
| 1798 | ** second element of the linked list. This address is used to compute |
| 1799 | ** the offset to the "next" field within the structure. The offset to |
| 1800 | ** the "next" field must be constant for all structures in the list. |
| 1801 | ** |
| 1802 | ** The function returns a new pointer which is the head of the list |
| 1803 | ** after sorting. |
| 1804 | ** |
| 1805 | ** ALGORITHM: |
| 1806 | ** Merge-sort. |
| 1807 | */ |
| 1808 | |
| 1809 | /* |
| 1810 | ** Return a pointer to the next structure in the linked list. |
| 1811 | */ |
drh | d25d692 | 2012-04-18 09:59:56 +0000 | [diff] [blame] | 1812 | #define NEXT(A) (*(char**)(((char*)A)+offset)) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1813 | |
| 1814 | /* |
| 1815 | ** Inputs: |
| 1816 | ** a: A sorted, null-terminated linked list. (May be null). |
| 1817 | ** b: A sorted, null-terminated linked list. (May be null). |
| 1818 | ** cmp: A pointer to the comparison function. |
| 1819 | ** offset: Offset in the structure to the "next" field. |
| 1820 | ** |
| 1821 | ** Return Value: |
| 1822 | ** A pointer to the head of a sorted list containing the elements |
| 1823 | ** of both a and b. |
| 1824 | ** |
| 1825 | ** Side effects: |
| 1826 | ** The "next" pointers for elements in the lists a and b are |
| 1827 | ** changed. |
| 1828 | */ |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 1829 | static char *merge( |
| 1830 | char *a, |
| 1831 | char *b, |
| 1832 | int (*cmp)(const char*,const char*), |
| 1833 | int offset |
| 1834 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1835 | char *ptr, *head; |
| 1836 | |
| 1837 | if( a==0 ){ |
| 1838 | head = b; |
| 1839 | }else if( b==0 ){ |
| 1840 | head = a; |
| 1841 | }else{ |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 1842 | if( (*cmp)(a,b)<=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1843 | ptr = a; |
| 1844 | a = NEXT(a); |
| 1845 | }else{ |
| 1846 | ptr = b; |
| 1847 | b = NEXT(b); |
| 1848 | } |
| 1849 | head = ptr; |
| 1850 | while( a && b ){ |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 1851 | if( (*cmp)(a,b)<=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1852 | NEXT(ptr) = a; |
| 1853 | ptr = a; |
| 1854 | a = NEXT(a); |
| 1855 | }else{ |
| 1856 | NEXT(ptr) = b; |
| 1857 | ptr = b; |
| 1858 | b = NEXT(b); |
| 1859 | } |
| 1860 | } |
| 1861 | if( a ) NEXT(ptr) = a; |
| 1862 | else NEXT(ptr) = b; |
| 1863 | } |
| 1864 | return head; |
| 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | ** Inputs: |
| 1869 | ** list: Pointer to a singly-linked list of structures. |
| 1870 | ** next: Pointer to pointer to the second element of the list. |
| 1871 | ** cmp: A comparison function. |
| 1872 | ** |
| 1873 | ** Return Value: |
| 1874 | ** A pointer to the head of a sorted list containing the elements |
| 1875 | ** orginally in list. |
| 1876 | ** |
| 1877 | ** Side effects: |
| 1878 | ** The "next" pointers for elements in list are changed. |
| 1879 | */ |
| 1880 | #define LISTSIZE 30 |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 1881 | static char *msort( |
| 1882 | char *list, |
| 1883 | char **next, |
| 1884 | int (*cmp)(const char*,const char*) |
| 1885 | ){ |
drh | ba99af5 | 2001-10-25 20:37:16 +0000 | [diff] [blame] | 1886 | unsigned long offset; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1887 | char *ep; |
| 1888 | char *set[LISTSIZE]; |
| 1889 | int i; |
drh | 1cc0d11 | 2015-03-31 15:15:48 +0000 | [diff] [blame] | 1890 | offset = (unsigned long)((char*)next - (char*)list); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1891 | for(i=0; i<LISTSIZE; i++) set[i] = 0; |
| 1892 | while( list ){ |
| 1893 | ep = list; |
| 1894 | list = NEXT(list); |
| 1895 | NEXT(ep) = 0; |
| 1896 | for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){ |
| 1897 | ep = merge(ep,set[i],cmp,offset); |
| 1898 | set[i] = 0; |
| 1899 | } |
| 1900 | set[i] = ep; |
| 1901 | } |
| 1902 | ep = 0; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 1903 | 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] | 1904 | return ep; |
| 1905 | } |
| 1906 | /************************ From the file "option.c" **************************/ |
| 1907 | static char **argv; |
| 1908 | static struct s_options *op; |
| 1909 | static FILE *errstream; |
| 1910 | |
| 1911 | #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0) |
| 1912 | |
| 1913 | /* |
| 1914 | ** Print the command line with a carrot pointing to the k-th character |
| 1915 | ** of the n-th field. |
| 1916 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1917 | static void errline(int n, int k, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1918 | { |
| 1919 | int spcnt, i; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1920 | if( argv[0] ) fprintf(err,"%s",argv[0]); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 1921 | spcnt = lemonStrlen(argv[0]) + 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1922 | for(i=1; i<n && argv[i]; i++){ |
| 1923 | fprintf(err," %s",argv[i]); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 1924 | spcnt += lemonStrlen(argv[i])+1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1925 | } |
| 1926 | spcnt += k; |
| 1927 | for(; argv[i]; i++) fprintf(err," %s",argv[i]); |
| 1928 | if( spcnt<20 ){ |
| 1929 | fprintf(err,"\n%*s^-- here\n",spcnt,""); |
| 1930 | }else{ |
| 1931 | fprintf(err,"\n%*shere --^\n",spcnt-7,""); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | /* |
| 1936 | ** Return the index of the N-th non-switch argument. Return -1 |
| 1937 | ** if N is out of range. |
| 1938 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1939 | static int argindex(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1940 | { |
| 1941 | int i; |
| 1942 | int dashdash = 0; |
| 1943 | if( argv!=0 && *argv!=0 ){ |
| 1944 | for(i=1; argv[i]; i++){ |
| 1945 | if( dashdash || !ISOPT(argv[i]) ){ |
| 1946 | if( n==0 ) return i; |
| 1947 | n--; |
| 1948 | } |
| 1949 | if( strcmp(argv[i],"--")==0 ) dashdash = 1; |
| 1950 | } |
| 1951 | } |
| 1952 | return -1; |
| 1953 | } |
| 1954 | |
| 1955 | static char emsg[] = "Command line syntax error: "; |
| 1956 | |
| 1957 | /* |
| 1958 | ** Process a flag command line argument. |
| 1959 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1960 | static int handleflags(int i, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1961 | { |
| 1962 | int v; |
| 1963 | int errcnt = 0; |
| 1964 | int j; |
| 1965 | for(j=0; op[j].label; j++){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 1966 | 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] | 1967 | } |
| 1968 | v = argv[i][0]=='-' ? 1 : 0; |
| 1969 | if( op[j].label==0 ){ |
| 1970 | if( err ){ |
| 1971 | fprintf(err,"%sundefined option.\n",emsg); |
| 1972 | errline(i,1,err); |
| 1973 | } |
| 1974 | errcnt++; |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1975 | }else if( op[j].arg==0 ){ |
| 1976 | /* Ignore this option */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1977 | }else if( op[j].type==OPT_FLAG ){ |
| 1978 | *((int*)op[j].arg) = v; |
| 1979 | }else if( op[j].type==OPT_FFLAG ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1980 | (*(void(*)(int))(op[j].arg))(v); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1981 | }else if( op[j].type==OPT_FSTR ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1982 | (*(void(*)(char *))(op[j].arg))(&argv[i][2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1983 | }else{ |
| 1984 | if( err ){ |
| 1985 | fprintf(err,"%smissing argument on switch.\n",emsg); |
| 1986 | errline(i,1,err); |
| 1987 | } |
| 1988 | errcnt++; |
| 1989 | } |
| 1990 | return errcnt; |
| 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | ** Process a command line switch which has an argument. |
| 1995 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1996 | static int handleswitch(int i, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1997 | { |
| 1998 | int lv = 0; |
| 1999 | double dv = 0.0; |
| 2000 | char *sv = 0, *end; |
| 2001 | char *cp; |
| 2002 | int j; |
| 2003 | int errcnt = 0; |
| 2004 | cp = strchr(argv[i],'='); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 2005 | assert( cp!=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2006 | *cp = 0; |
| 2007 | for(j=0; op[j].label; j++){ |
| 2008 | if( strcmp(argv[i],op[j].label)==0 ) break; |
| 2009 | } |
| 2010 | *cp = '='; |
| 2011 | if( op[j].label==0 ){ |
| 2012 | if( err ){ |
| 2013 | fprintf(err,"%sundefined option.\n",emsg); |
| 2014 | errline(i,0,err); |
| 2015 | } |
| 2016 | errcnt++; |
| 2017 | }else{ |
| 2018 | cp++; |
| 2019 | switch( op[j].type ){ |
| 2020 | case OPT_FLAG: |
| 2021 | case OPT_FFLAG: |
| 2022 | if( err ){ |
| 2023 | fprintf(err,"%soption requires an argument.\n",emsg); |
| 2024 | errline(i,0,err); |
| 2025 | } |
| 2026 | errcnt++; |
| 2027 | break; |
| 2028 | case OPT_DBL: |
| 2029 | case OPT_FDBL: |
| 2030 | dv = strtod(cp,&end); |
| 2031 | if( *end ){ |
| 2032 | if( err ){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 2033 | fprintf(err, |
| 2034 | "%sillegal character in floating-point argument.\n",emsg); |
drh | 1cc0d11 | 2015-03-31 15:15:48 +0000 | [diff] [blame] | 2035 | errline(i,(int)((char*)end-(char*)argv[i]),err); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2036 | } |
| 2037 | errcnt++; |
| 2038 | } |
| 2039 | break; |
| 2040 | case OPT_INT: |
| 2041 | case OPT_FINT: |
| 2042 | lv = strtol(cp,&end,0); |
| 2043 | if( *end ){ |
| 2044 | if( err ){ |
| 2045 | fprintf(err,"%sillegal character in integer argument.\n",emsg); |
drh | 1cc0d11 | 2015-03-31 15:15:48 +0000 | [diff] [blame] | 2046 | errline(i,(int)((char*)end-(char*)argv[i]),err); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2047 | } |
| 2048 | errcnt++; |
| 2049 | } |
| 2050 | break; |
| 2051 | case OPT_STR: |
| 2052 | case OPT_FSTR: |
| 2053 | sv = cp; |
| 2054 | break; |
| 2055 | } |
| 2056 | switch( op[j].type ){ |
| 2057 | case OPT_FLAG: |
| 2058 | case OPT_FFLAG: |
| 2059 | break; |
| 2060 | case OPT_DBL: |
| 2061 | *(double*)(op[j].arg) = dv; |
| 2062 | break; |
| 2063 | case OPT_FDBL: |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2064 | (*(void(*)(double))(op[j].arg))(dv); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2065 | break; |
| 2066 | case OPT_INT: |
| 2067 | *(int*)(op[j].arg) = lv; |
| 2068 | break; |
| 2069 | case OPT_FINT: |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2070 | (*(void(*)(int))(op[j].arg))((int)lv); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2071 | break; |
| 2072 | case OPT_STR: |
| 2073 | *(char**)(op[j].arg) = sv; |
| 2074 | break; |
| 2075 | case OPT_FSTR: |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2076 | (*(void(*)(char *))(op[j].arg))(sv); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2077 | break; |
| 2078 | } |
| 2079 | } |
| 2080 | return errcnt; |
| 2081 | } |
| 2082 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2083 | int OptInit(char **a, struct s_options *o, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2084 | { |
| 2085 | int errcnt = 0; |
| 2086 | argv = a; |
| 2087 | op = o; |
| 2088 | errstream = err; |
| 2089 | if( argv && *argv && op ){ |
| 2090 | int i; |
| 2091 | for(i=1; argv[i]; i++){ |
| 2092 | if( argv[i][0]=='+' || argv[i][0]=='-' ){ |
| 2093 | errcnt += handleflags(i,err); |
| 2094 | }else if( strchr(argv[i],'=') ){ |
| 2095 | errcnt += handleswitch(i,err); |
| 2096 | } |
| 2097 | } |
| 2098 | } |
| 2099 | if( errcnt>0 ){ |
| 2100 | fprintf(err,"Valid command line options for \"%s\" are:\n",*a); |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 2101 | OptPrint(); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2102 | exit(1); |
| 2103 | } |
| 2104 | return 0; |
| 2105 | } |
| 2106 | |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 2107 | int OptNArgs(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2108 | int cnt = 0; |
| 2109 | int dashdash = 0; |
| 2110 | int i; |
| 2111 | if( argv!=0 && argv[0]!=0 ){ |
| 2112 | for(i=1; argv[i]; i++){ |
| 2113 | if( dashdash || !ISOPT(argv[i]) ) cnt++; |
| 2114 | if( strcmp(argv[i],"--")==0 ) dashdash = 1; |
| 2115 | } |
| 2116 | } |
| 2117 | return cnt; |
| 2118 | } |
| 2119 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2120 | char *OptArg(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2121 | { |
| 2122 | int i; |
| 2123 | i = argindex(n); |
| 2124 | return i>=0 ? argv[i] : 0; |
| 2125 | } |
| 2126 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2127 | void OptErr(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2128 | { |
| 2129 | int i; |
| 2130 | i = argindex(n); |
| 2131 | if( i>=0 ) errline(i,0,errstream); |
| 2132 | } |
| 2133 | |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 2134 | void OptPrint(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2135 | int i; |
| 2136 | int max, len; |
| 2137 | max = 0; |
| 2138 | for(i=0; op[i].label; i++){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2139 | len = lemonStrlen(op[i].label) + 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2140 | switch( op[i].type ){ |
| 2141 | case OPT_FLAG: |
| 2142 | case OPT_FFLAG: |
| 2143 | break; |
| 2144 | case OPT_INT: |
| 2145 | case OPT_FINT: |
| 2146 | len += 9; /* length of "<integer>" */ |
| 2147 | break; |
| 2148 | case OPT_DBL: |
| 2149 | case OPT_FDBL: |
| 2150 | len += 6; /* length of "<real>" */ |
| 2151 | break; |
| 2152 | case OPT_STR: |
| 2153 | case OPT_FSTR: |
| 2154 | len += 8; /* length of "<string>" */ |
| 2155 | break; |
| 2156 | } |
| 2157 | if( len>max ) max = len; |
| 2158 | } |
| 2159 | for(i=0; op[i].label; i++){ |
| 2160 | switch( op[i].type ){ |
| 2161 | case OPT_FLAG: |
| 2162 | case OPT_FFLAG: |
| 2163 | fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message); |
| 2164 | break; |
| 2165 | case OPT_INT: |
| 2166 | case OPT_FINT: |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 2167 | fprintf(errstream," -%s<integer>%*s %s\n",op[i].label, |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2168 | (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2169 | break; |
| 2170 | case OPT_DBL: |
| 2171 | case OPT_FDBL: |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 2172 | fprintf(errstream," -%s<real>%*s %s\n",op[i].label, |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2173 | (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2174 | break; |
| 2175 | case OPT_STR: |
| 2176 | case OPT_FSTR: |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 2177 | fprintf(errstream," -%s<string>%*s %s\n",op[i].label, |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2178 | (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2179 | break; |
| 2180 | } |
| 2181 | } |
| 2182 | } |
| 2183 | /*********************** From the file "parse.c" ****************************/ |
| 2184 | /* |
| 2185 | ** Input file parser for the LEMON parser generator. |
| 2186 | */ |
| 2187 | |
| 2188 | /* The state of the parser */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2189 | enum e_state { |
| 2190 | INITIALIZE, |
| 2191 | WAITING_FOR_DECL_OR_RULE, |
| 2192 | WAITING_FOR_DECL_KEYWORD, |
| 2193 | WAITING_FOR_DECL_ARG, |
| 2194 | WAITING_FOR_PRECEDENCE_SYMBOL, |
| 2195 | WAITING_FOR_ARROW, |
| 2196 | IN_RHS, |
| 2197 | LHS_ALIAS_1, |
| 2198 | LHS_ALIAS_2, |
| 2199 | LHS_ALIAS_3, |
| 2200 | RHS_ALIAS_1, |
| 2201 | RHS_ALIAS_2, |
| 2202 | PRECEDENCE_MARK_1, |
| 2203 | PRECEDENCE_MARK_2, |
| 2204 | RESYNC_AFTER_RULE_ERROR, |
| 2205 | RESYNC_AFTER_DECL_ERROR, |
| 2206 | WAITING_FOR_DESTRUCTOR_SYMBOL, |
| 2207 | WAITING_FOR_DATATYPE_SYMBOL, |
| 2208 | WAITING_FOR_FALLBACK_ID, |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2209 | WAITING_FOR_WILDCARD_ID, |
| 2210 | WAITING_FOR_CLASS_ID, |
drh | 59c435a | 2017-08-02 03:21:11 +0000 | [diff] [blame] | 2211 | WAITING_FOR_CLASS_TOKEN, |
| 2212 | WAITING_FOR_TOKEN_NAME |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2213 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2214 | struct pstate { |
| 2215 | char *filename; /* Name of the input file */ |
| 2216 | int tokenlineno; /* Linenumber at which current token starts */ |
| 2217 | int errorcnt; /* Number of errors so far */ |
| 2218 | char *tokenstart; /* Text of current token */ |
| 2219 | struct lemon *gp; /* Global state vector */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2220 | enum e_state state; /* The state of the parser */ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2221 | struct symbol *fallback; /* The fallback token */ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2222 | struct symbol *tkclass; /* Token class symbol */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2223 | struct symbol *lhs; /* Left-hand side of current rule */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2224 | const char *lhsalias; /* Alias for the LHS */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2225 | int nrhs; /* Number of right-hand side symbols seen */ |
| 2226 | struct symbol *rhs[MAXRHS]; /* RHS symbols */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2227 | const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2228 | struct rule *prevrule; /* Previous rule parsed */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2229 | const char *declkeyword; /* Keyword of a declaration */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2230 | char **declargslot; /* Where the declaration argument should be put */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2231 | int insertLineMacro; /* Add #line before declaration insert */ |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 2232 | int *decllinenoslot; /* Where to write declaration line number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2233 | enum e_assoc declassoc; /* Assign this association to decl arguments */ |
| 2234 | int preccounter; /* Assign this precedence to decl arguments */ |
| 2235 | struct rule *firstrule; /* Pointer to first rule in the grammar */ |
| 2236 | struct rule *lastrule; /* Pointer to the most recently parsed rule */ |
| 2237 | }; |
| 2238 | |
| 2239 | /* Parse a single token */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2240 | static void parseonetoken(struct pstate *psp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2241 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2242 | const char *x; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2243 | x = Strsafe(psp->tokenstart); /* Save the token permanently */ |
| 2244 | #if 0 |
| 2245 | printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno, |
| 2246 | x,psp->state); |
| 2247 | #endif |
| 2248 | switch( psp->state ){ |
| 2249 | case INITIALIZE: |
| 2250 | psp->prevrule = 0; |
| 2251 | psp->preccounter = 0; |
| 2252 | psp->firstrule = psp->lastrule = 0; |
| 2253 | psp->gp->nrule = 0; |
| 2254 | /* Fall thru to next case */ |
| 2255 | case WAITING_FOR_DECL_OR_RULE: |
| 2256 | if( x[0]=='%' ){ |
| 2257 | psp->state = WAITING_FOR_DECL_KEYWORD; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2258 | }else if( ISLOWER(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2259 | psp->lhs = Symbol_new(x); |
| 2260 | psp->nrhs = 0; |
| 2261 | psp->lhsalias = 0; |
| 2262 | psp->state = WAITING_FOR_ARROW; |
| 2263 | }else if( x[0]=='{' ){ |
| 2264 | if( psp->prevrule==0 ){ |
| 2265 | ErrorMsg(psp->filename,psp->tokenlineno, |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 2266 | "There is no prior rule upon which to attach the code \ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2267 | fragment which begins on this line."); |
| 2268 | psp->errorcnt++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2269 | }else if( psp->prevrule->code!=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2270 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2271 | "Code fragment beginning on this line is not the first \ |
| 2272 | to follow the previous rule."); |
| 2273 | psp->errorcnt++; |
| 2274 | }else{ |
| 2275 | psp->prevrule->line = psp->tokenlineno; |
| 2276 | psp->prevrule->code = &x[1]; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 2277 | psp->prevrule->noCode = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2278 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2279 | }else if( x[0]=='[' ){ |
| 2280 | psp->state = PRECEDENCE_MARK_1; |
| 2281 | }else{ |
| 2282 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2283 | "Token \"%s\" should be either \"%%\" or a nonterminal name.", |
| 2284 | x); |
| 2285 | psp->errorcnt++; |
| 2286 | } |
| 2287 | break; |
| 2288 | case PRECEDENCE_MARK_1: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2289 | if( !ISUPPER(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2290 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2291 | "The precedence symbol must be a terminal."); |
| 2292 | psp->errorcnt++; |
| 2293 | }else if( psp->prevrule==0 ){ |
| 2294 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2295 | "There is no prior rule to assign precedence \"[%s]\".",x); |
| 2296 | psp->errorcnt++; |
| 2297 | }else if( psp->prevrule->precsym!=0 ){ |
| 2298 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2299 | "Precedence mark on this line is not the first \ |
| 2300 | to follow the previous rule."); |
| 2301 | psp->errorcnt++; |
| 2302 | }else{ |
| 2303 | psp->prevrule->precsym = Symbol_new(x); |
| 2304 | } |
| 2305 | psp->state = PRECEDENCE_MARK_2; |
| 2306 | break; |
| 2307 | case PRECEDENCE_MARK_2: |
| 2308 | if( x[0]!=']' ){ |
| 2309 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2310 | "Missing \"]\" on precedence mark."); |
| 2311 | psp->errorcnt++; |
| 2312 | } |
| 2313 | psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2314 | break; |
| 2315 | case WAITING_FOR_ARROW: |
| 2316 | if( x[0]==':' && x[1]==':' && x[2]=='=' ){ |
| 2317 | psp->state = IN_RHS; |
| 2318 | }else if( x[0]=='(' ){ |
| 2319 | psp->state = LHS_ALIAS_1; |
| 2320 | }else{ |
| 2321 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2322 | "Expected to see a \":\" following the LHS symbol \"%s\".", |
| 2323 | psp->lhs->name); |
| 2324 | psp->errorcnt++; |
| 2325 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2326 | } |
| 2327 | break; |
| 2328 | case LHS_ALIAS_1: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2329 | if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2330 | psp->lhsalias = x; |
| 2331 | psp->state = LHS_ALIAS_2; |
| 2332 | }else{ |
| 2333 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2334 | "\"%s\" is not a valid alias for the LHS \"%s\"\n", |
| 2335 | x,psp->lhs->name); |
| 2336 | psp->errorcnt++; |
| 2337 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2338 | } |
| 2339 | break; |
| 2340 | case LHS_ALIAS_2: |
| 2341 | if( x[0]==')' ){ |
| 2342 | psp->state = LHS_ALIAS_3; |
| 2343 | }else{ |
| 2344 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2345 | "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias); |
| 2346 | psp->errorcnt++; |
| 2347 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2348 | } |
| 2349 | break; |
| 2350 | case LHS_ALIAS_3: |
| 2351 | if( x[0]==':' && x[1]==':' && x[2]=='=' ){ |
| 2352 | psp->state = IN_RHS; |
| 2353 | }else{ |
| 2354 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2355 | "Missing \"->\" following: \"%s(%s)\".", |
| 2356 | psp->lhs->name,psp->lhsalias); |
| 2357 | psp->errorcnt++; |
| 2358 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2359 | } |
| 2360 | break; |
| 2361 | case IN_RHS: |
| 2362 | if( x[0]=='.' ){ |
| 2363 | struct rule *rp; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 2364 | rp = (struct rule *)calloc( sizeof(struct rule) + |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 2365 | sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2366 | if( rp==0 ){ |
| 2367 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2368 | "Can't allocate enough memory for this rule."); |
| 2369 | psp->errorcnt++; |
| 2370 | psp->prevrule = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2371 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2372 | int i; |
| 2373 | rp->ruleline = psp->tokenlineno; |
| 2374 | rp->rhs = (struct symbol**)&rp[1]; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2375 | rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2376 | for(i=0; i<psp->nrhs; i++){ |
| 2377 | rp->rhs[i] = psp->rhs[i]; |
| 2378 | rp->rhsalias[i] = psp->alias[i]; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2379 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2380 | rp->lhs = psp->lhs; |
| 2381 | rp->lhsalias = psp->lhsalias; |
| 2382 | rp->nrhs = psp->nrhs; |
| 2383 | rp->code = 0; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 2384 | rp->noCode = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2385 | rp->precsym = 0; |
| 2386 | rp->index = psp->gp->nrule++; |
| 2387 | rp->nextlhs = rp->lhs->rule; |
| 2388 | rp->lhs->rule = rp; |
| 2389 | rp->next = 0; |
| 2390 | if( psp->firstrule==0 ){ |
| 2391 | psp->firstrule = psp->lastrule = rp; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2392 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2393 | psp->lastrule->next = rp; |
| 2394 | psp->lastrule = rp; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2395 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2396 | psp->prevrule = rp; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2397 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2398 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2399 | }else if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2400 | if( psp->nrhs>=MAXRHS ){ |
| 2401 | ErrorMsg(psp->filename,psp->tokenlineno, |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 2402 | "Too many symbols on RHS of rule beginning at \"%s\".", |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2403 | x); |
| 2404 | psp->errorcnt++; |
| 2405 | psp->state = RESYNC_AFTER_RULE_ERROR; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2406 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2407 | psp->rhs[psp->nrhs] = Symbol_new(x); |
| 2408 | psp->alias[psp->nrhs] = 0; |
| 2409 | psp->nrhs++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2410 | } |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2411 | }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){ |
| 2412 | struct symbol *msp = psp->rhs[psp->nrhs-1]; |
| 2413 | if( msp->type!=MULTITERMINAL ){ |
| 2414 | struct symbol *origsp = msp; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2415 | msp = (struct symbol *) calloc(1,sizeof(*msp)); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2416 | memset(msp, 0, sizeof(*msp)); |
| 2417 | msp->type = MULTITERMINAL; |
| 2418 | msp->nsubsym = 1; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2419 | msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*)); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2420 | msp->subsym[0] = origsp; |
| 2421 | msp->name = origsp->name; |
| 2422 | psp->rhs[psp->nrhs-1] = msp; |
| 2423 | } |
| 2424 | msp->nsubsym++; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2425 | msp->subsym = (struct symbol **) realloc(msp->subsym, |
| 2426 | sizeof(struct symbol*)*msp->nsubsym); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2427 | msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2428 | if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2429 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2430 | "Cannot form a compound containing a non-terminal"); |
| 2431 | psp->errorcnt++; |
| 2432 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2433 | }else if( x[0]=='(' && psp->nrhs>0 ){ |
| 2434 | psp->state = RHS_ALIAS_1; |
| 2435 | }else{ |
| 2436 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2437 | "Illegal character on RHS of rule: \"%s\".",x); |
| 2438 | psp->errorcnt++; |
| 2439 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2440 | } |
| 2441 | break; |
| 2442 | case RHS_ALIAS_1: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2443 | if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2444 | psp->alias[psp->nrhs-1] = x; |
| 2445 | psp->state = RHS_ALIAS_2; |
| 2446 | }else{ |
| 2447 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2448 | "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n", |
| 2449 | x,psp->rhs[psp->nrhs-1]->name); |
| 2450 | psp->errorcnt++; |
| 2451 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2452 | } |
| 2453 | break; |
| 2454 | case RHS_ALIAS_2: |
| 2455 | if( x[0]==')' ){ |
| 2456 | psp->state = IN_RHS; |
| 2457 | }else{ |
| 2458 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2459 | "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias); |
| 2460 | psp->errorcnt++; |
| 2461 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2462 | } |
| 2463 | break; |
| 2464 | case WAITING_FOR_DECL_KEYWORD: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2465 | if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2466 | psp->declkeyword = x; |
| 2467 | psp->declargslot = 0; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 2468 | psp->decllinenoslot = 0; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2469 | psp->insertLineMacro = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2470 | psp->state = WAITING_FOR_DECL_ARG; |
| 2471 | if( strcmp(x,"name")==0 ){ |
| 2472 | psp->declargslot = &(psp->gp->name); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2473 | psp->insertLineMacro = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2474 | }else if( strcmp(x,"include")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2475 | psp->declargslot = &(psp->gp->include); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2476 | }else if( strcmp(x,"code")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2477 | psp->declargslot = &(psp->gp->extracode); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2478 | }else if( strcmp(x,"token_destructor")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2479 | psp->declargslot = &psp->gp->tokendest; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2480 | }else if( strcmp(x,"default_destructor")==0 ){ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2481 | psp->declargslot = &psp->gp->vardest; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2482 | }else if( strcmp(x,"token_prefix")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2483 | psp->declargslot = &psp->gp->tokenprefix; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2484 | psp->insertLineMacro = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2485 | }else if( strcmp(x,"syntax_error")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2486 | psp->declargslot = &(psp->gp->error); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2487 | }else if( strcmp(x,"parse_accept")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2488 | psp->declargslot = &(psp->gp->accept); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2489 | }else if( strcmp(x,"parse_failure")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2490 | psp->declargslot = &(psp->gp->failure); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2491 | }else if( strcmp(x,"stack_overflow")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2492 | psp->declargslot = &(psp->gp->overflow); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2493 | }else if( strcmp(x,"extra_argument")==0 ){ |
| 2494 | psp->declargslot = &(psp->gp->arg); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2495 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2496 | }else if( strcmp(x,"token_type")==0 ){ |
| 2497 | psp->declargslot = &(psp->gp->tokentype); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2498 | psp->insertLineMacro = 0; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2499 | }else if( strcmp(x,"default_type")==0 ){ |
| 2500 | psp->declargslot = &(psp->gp->vartype); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2501 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2502 | }else if( strcmp(x,"stack_size")==0 ){ |
| 2503 | psp->declargslot = &(psp->gp->stacksize); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2504 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2505 | }else if( strcmp(x,"start_symbol")==0 ){ |
| 2506 | psp->declargslot = &(psp->gp->start); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2507 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2508 | }else if( strcmp(x,"left")==0 ){ |
| 2509 | psp->preccounter++; |
| 2510 | psp->declassoc = LEFT; |
| 2511 | psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
| 2512 | }else if( strcmp(x,"right")==0 ){ |
| 2513 | psp->preccounter++; |
| 2514 | psp->declassoc = RIGHT; |
| 2515 | psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
| 2516 | }else if( strcmp(x,"nonassoc")==0 ){ |
| 2517 | psp->preccounter++; |
| 2518 | psp->declassoc = NONE; |
| 2519 | psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2520 | }else if( strcmp(x,"destructor")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2521 | psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2522 | }else if( strcmp(x,"type")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2523 | psp->state = WAITING_FOR_DATATYPE_SYMBOL; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2524 | }else if( strcmp(x,"fallback")==0 ){ |
| 2525 | psp->fallback = 0; |
| 2526 | psp->state = WAITING_FOR_FALLBACK_ID; |
drh | 59c435a | 2017-08-02 03:21:11 +0000 | [diff] [blame] | 2527 | }else if( strcmp(x,"token")==0 ){ |
| 2528 | psp->state = WAITING_FOR_TOKEN_NAME; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 2529 | }else if( strcmp(x,"wildcard")==0 ){ |
| 2530 | psp->state = WAITING_FOR_WILDCARD_ID; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2531 | }else if( strcmp(x,"token_class")==0 ){ |
| 2532 | psp->state = WAITING_FOR_CLASS_ID; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2533 | }else{ |
| 2534 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2535 | "Unknown declaration keyword: \"%%%s\".",x); |
| 2536 | psp->errorcnt++; |
| 2537 | psp->state = RESYNC_AFTER_DECL_ERROR; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2538 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2539 | }else{ |
| 2540 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2541 | "Illegal declaration keyword: \"%s\".",x); |
| 2542 | psp->errorcnt++; |
| 2543 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2544 | } |
| 2545 | break; |
| 2546 | case WAITING_FOR_DESTRUCTOR_SYMBOL: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2547 | if( !ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2548 | ErrorMsg(psp->filename,psp->tokenlineno, |
icculus | d0d97b0 | 2010-02-17 20:22:10 +0000 | [diff] [blame] | 2549 | "Symbol name missing after %%destructor keyword"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2550 | psp->errorcnt++; |
| 2551 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2552 | }else{ |
icculus | d286fa6 | 2010-03-03 17:06:32 +0000 | [diff] [blame] | 2553 | struct symbol *sp = Symbol_new(x); |
| 2554 | psp->declargslot = &sp->destructor; |
| 2555 | psp->decllinenoslot = &sp->destLineno; |
| 2556 | psp->insertLineMacro = 1; |
| 2557 | psp->state = WAITING_FOR_DECL_ARG; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2558 | } |
| 2559 | break; |
| 2560 | case WAITING_FOR_DATATYPE_SYMBOL: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2561 | if( !ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2562 | ErrorMsg(psp->filename,psp->tokenlineno, |
icculus | d0d97b0 | 2010-02-17 20:22:10 +0000 | [diff] [blame] | 2563 | "Symbol name missing after %%type keyword"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2564 | psp->errorcnt++; |
| 2565 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2566 | }else{ |
icculus | 866bf1e | 2010-02-17 20:31:32 +0000 | [diff] [blame] | 2567 | struct symbol *sp = Symbol_find(x); |
| 2568 | if((sp) && (sp->datatype)){ |
| 2569 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2570 | "Symbol %%type \"%s\" already defined", x); |
| 2571 | psp->errorcnt++; |
| 2572 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2573 | }else{ |
| 2574 | if (!sp){ |
| 2575 | sp = Symbol_new(x); |
| 2576 | } |
| 2577 | psp->declargslot = &sp->datatype; |
| 2578 | psp->insertLineMacro = 0; |
| 2579 | psp->state = WAITING_FOR_DECL_ARG; |
| 2580 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2581 | } |
| 2582 | break; |
| 2583 | case WAITING_FOR_PRECEDENCE_SYMBOL: |
| 2584 | if( x[0]=='.' ){ |
| 2585 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2586 | }else if( ISUPPER(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2587 | struct symbol *sp; |
| 2588 | sp = Symbol_new(x); |
| 2589 | if( sp->prec>=0 ){ |
| 2590 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2591 | "Symbol \"%s\" has already be given a precedence.",x); |
| 2592 | psp->errorcnt++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2593 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2594 | sp->prec = psp->preccounter; |
| 2595 | sp->assoc = psp->declassoc; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2596 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2597 | }else{ |
| 2598 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2599 | "Can't assign a precedence to \"%s\".",x); |
| 2600 | psp->errorcnt++; |
| 2601 | } |
| 2602 | break; |
| 2603 | case WAITING_FOR_DECL_ARG: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2604 | if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2605 | const char *zOld, *zNew; |
| 2606 | char *zBuf, *z; |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 2607 | int nOld, n, nLine = 0, nNew, nBack; |
drh | b5bd49e | 2008-07-14 12:21:08 +0000 | [diff] [blame] | 2608 | int addLineMacro; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2609 | char zLine[50]; |
| 2610 | zNew = x; |
| 2611 | if( zNew[0]=='"' || zNew[0]=='{' ) zNew++; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2612 | nNew = lemonStrlen(zNew); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2613 | if( *psp->declargslot ){ |
| 2614 | zOld = *psp->declargslot; |
| 2615 | }else{ |
| 2616 | zOld = ""; |
| 2617 | } |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2618 | nOld = lemonStrlen(zOld); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2619 | n = nOld + nNew + 20; |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 2620 | addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro && |
drh | b5bd49e | 2008-07-14 12:21:08 +0000 | [diff] [blame] | 2621 | (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0); |
| 2622 | if( addLineMacro ){ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2623 | for(z=psp->filename, nBack=0; *z; z++){ |
| 2624 | if( *z=='\\' ) nBack++; |
| 2625 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 2626 | lemon_sprintf(zLine, "#line %d ", psp->tokenlineno); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2627 | nLine = lemonStrlen(zLine); |
| 2628 | n += nLine + lemonStrlen(psp->filename) + nBack; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2629 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2630 | *psp->declargslot = (char *) realloc(*psp->declargslot, n); |
| 2631 | zBuf = *psp->declargslot + nOld; |
drh | b5bd49e | 2008-07-14 12:21:08 +0000 | [diff] [blame] | 2632 | if( addLineMacro ){ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2633 | if( nOld && zBuf[-1]!='\n' ){ |
| 2634 | *(zBuf++) = '\n'; |
| 2635 | } |
| 2636 | memcpy(zBuf, zLine, nLine); |
| 2637 | zBuf += nLine; |
| 2638 | *(zBuf++) = '"'; |
| 2639 | for(z=psp->filename; *z; z++){ |
| 2640 | if( *z=='\\' ){ |
| 2641 | *(zBuf++) = '\\'; |
| 2642 | } |
| 2643 | *(zBuf++) = *z; |
| 2644 | } |
| 2645 | *(zBuf++) = '"'; |
| 2646 | *(zBuf++) = '\n'; |
| 2647 | } |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 2648 | if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){ |
| 2649 | psp->decllinenoslot[0] = psp->tokenlineno; |
| 2650 | } |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2651 | memcpy(zBuf, zNew, nNew); |
| 2652 | zBuf += nNew; |
| 2653 | *zBuf = 0; |
| 2654 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2655 | }else{ |
| 2656 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2657 | "Illegal argument to %%%s: %s",psp->declkeyword,x); |
| 2658 | psp->errorcnt++; |
| 2659 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2660 | } |
| 2661 | break; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2662 | case WAITING_FOR_FALLBACK_ID: |
| 2663 | if( x[0]=='.' ){ |
| 2664 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2665 | }else if( !ISUPPER(x[0]) ){ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2666 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2667 | "%%fallback argument \"%s\" should be a token", x); |
| 2668 | psp->errorcnt++; |
| 2669 | }else{ |
| 2670 | struct symbol *sp = Symbol_new(x); |
| 2671 | if( psp->fallback==0 ){ |
| 2672 | psp->fallback = sp; |
| 2673 | }else if( sp->fallback ){ |
| 2674 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2675 | "More than one fallback assigned to token %s", x); |
| 2676 | psp->errorcnt++; |
| 2677 | }else{ |
| 2678 | sp->fallback = psp->fallback; |
| 2679 | psp->gp->has_fallback = 1; |
| 2680 | } |
| 2681 | } |
| 2682 | break; |
drh | 59c435a | 2017-08-02 03:21:11 +0000 | [diff] [blame] | 2683 | case WAITING_FOR_TOKEN_NAME: |
| 2684 | /* Tokens do not have to be declared before use. But they can be |
| 2685 | ** in order to control their assigned integer number. The number for |
| 2686 | ** each token is assigned when it is first seen. So by including |
| 2687 | ** |
| 2688 | ** %token ONE TWO THREE |
| 2689 | ** |
| 2690 | ** early in the grammar file, that assigns small consecutive values |
| 2691 | ** to each of the tokens ONE TWO and THREE. |
| 2692 | */ |
| 2693 | if( x[0]=='.' ){ |
| 2694 | psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2695 | }else if( !ISUPPER(x[0]) ){ |
| 2696 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2697 | "%%token argument \"%s\" should be a token", x); |
| 2698 | psp->errorcnt++; |
| 2699 | }else{ |
| 2700 | (void)Symbol_new(x); |
| 2701 | } |
| 2702 | break; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 2703 | case WAITING_FOR_WILDCARD_ID: |
| 2704 | if( x[0]=='.' ){ |
| 2705 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2706 | }else if( !ISUPPER(x[0]) ){ |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 2707 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2708 | "%%wildcard argument \"%s\" should be a token", x); |
| 2709 | psp->errorcnt++; |
| 2710 | }else{ |
| 2711 | struct symbol *sp = Symbol_new(x); |
| 2712 | if( psp->gp->wildcard==0 ){ |
| 2713 | psp->gp->wildcard = sp; |
| 2714 | }else{ |
| 2715 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2716 | "Extra wildcard to token: %s", x); |
| 2717 | psp->errorcnt++; |
| 2718 | } |
| 2719 | } |
| 2720 | break; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2721 | case WAITING_FOR_CLASS_ID: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2722 | if( !ISLOWER(x[0]) ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2723 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2724 | "%%token_class must be followed by an identifier: ", x); |
| 2725 | psp->errorcnt++; |
| 2726 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2727 | }else if( Symbol_find(x) ){ |
| 2728 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2729 | "Symbol \"%s\" already used", x); |
| 2730 | psp->errorcnt++; |
| 2731 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2732 | }else{ |
| 2733 | psp->tkclass = Symbol_new(x); |
| 2734 | psp->tkclass->type = MULTITERMINAL; |
| 2735 | psp->state = WAITING_FOR_CLASS_TOKEN; |
| 2736 | } |
| 2737 | break; |
| 2738 | case WAITING_FOR_CLASS_TOKEN: |
| 2739 | if( x[0]=='.' ){ |
| 2740 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2741 | }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2742 | struct symbol *msp = psp->tkclass; |
| 2743 | msp->nsubsym++; |
| 2744 | msp->subsym = (struct symbol **) realloc(msp->subsym, |
| 2745 | sizeof(struct symbol*)*msp->nsubsym); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2746 | if( !ISUPPER(x[0]) ) x++; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2747 | msp->subsym[msp->nsubsym-1] = Symbol_new(x); |
| 2748 | }else{ |
| 2749 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2750 | "%%token_class argument \"%s\" should be a token", x); |
| 2751 | psp->errorcnt++; |
| 2752 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2753 | } |
| 2754 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2755 | case RESYNC_AFTER_RULE_ERROR: |
| 2756 | /* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2757 | ** break; */ |
| 2758 | case RESYNC_AFTER_DECL_ERROR: |
| 2759 | if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2760 | if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD; |
| 2761 | break; |
| 2762 | } |
| 2763 | } |
| 2764 | |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 2765 | /* Run the preprocessor over the input file text. The global variables |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2766 | ** azDefine[0] through azDefine[nDefine-1] contains the names of all defined |
| 2767 | ** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and |
| 2768 | ** comments them out. Text in between is also commented out as appropriate. |
| 2769 | */ |
danielk1977 | 940fac9 | 2005-01-23 22:41:37 +0000 | [diff] [blame] | 2770 | static void preprocess_input(char *z){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2771 | int i, j, k, n; |
| 2772 | int exclude = 0; |
rse | 38514a9 | 2007-09-20 11:34:17 +0000 | [diff] [blame] | 2773 | int start = 0; |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2774 | int lineno = 1; |
rse | 38514a9 | 2007-09-20 11:34:17 +0000 | [diff] [blame] | 2775 | int start_lineno = 1; |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2776 | for(i=0; z[i]; i++){ |
| 2777 | if( z[i]=='\n' ) lineno++; |
| 2778 | if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2779 | if( strncmp(&z[i],"%endif",6)==0 && ISSPACE(z[i+6]) ){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2780 | if( exclude ){ |
| 2781 | exclude--; |
| 2782 | if( exclude==0 ){ |
| 2783 | for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' '; |
| 2784 | } |
| 2785 | } |
| 2786 | for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' '; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2787 | }else if( (strncmp(&z[i],"%ifdef",6)==0 && ISSPACE(z[i+6])) |
| 2788 | || (strncmp(&z[i],"%ifndef",7)==0 && ISSPACE(z[i+7])) ){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2789 | if( exclude ){ |
| 2790 | exclude++; |
| 2791 | }else{ |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2792 | for(j=i+7; ISSPACE(z[j]); j++){} |
| 2793 | for(n=0; z[j+n] && !ISSPACE(z[j+n]); n++){} |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2794 | exclude = 1; |
| 2795 | for(k=0; k<nDefine; k++){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2796 | if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2797 | exclude = 0; |
| 2798 | break; |
| 2799 | } |
| 2800 | } |
| 2801 | if( z[i+3]=='n' ) exclude = !exclude; |
| 2802 | if( exclude ){ |
| 2803 | start = i; |
| 2804 | start_lineno = lineno; |
| 2805 | } |
| 2806 | } |
| 2807 | for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' '; |
| 2808 | } |
| 2809 | } |
| 2810 | if( exclude ){ |
| 2811 | fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno); |
| 2812 | exit(1); |
| 2813 | } |
| 2814 | } |
| 2815 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2816 | /* In spite of its name, this function is really a scanner. It read |
| 2817 | ** in the entire input file (all at once) then tokenizes it. Each |
| 2818 | ** token is passed to the function "parseonetoken" which builds all |
| 2819 | ** the appropriate data structures in the global state vector "gp". |
| 2820 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2821 | void Parse(struct lemon *gp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2822 | { |
| 2823 | struct pstate ps; |
| 2824 | FILE *fp; |
| 2825 | char *filebuf; |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 2826 | unsigned int filesize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2827 | int lineno; |
| 2828 | int c; |
| 2829 | char *cp, *nextcp; |
| 2830 | int startline = 0; |
| 2831 | |
rse | 38514a9 | 2007-09-20 11:34:17 +0000 | [diff] [blame] | 2832 | memset(&ps, '\0', sizeof(ps)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2833 | ps.gp = gp; |
| 2834 | ps.filename = gp->filename; |
| 2835 | ps.errorcnt = 0; |
| 2836 | ps.state = INITIALIZE; |
| 2837 | |
| 2838 | /* Begin by reading the input file */ |
| 2839 | fp = fopen(ps.filename,"rb"); |
| 2840 | if( fp==0 ){ |
| 2841 | ErrorMsg(ps.filename,0,"Can't open this file for reading."); |
| 2842 | gp->errorcnt++; |
| 2843 | return; |
| 2844 | } |
| 2845 | fseek(fp,0,2); |
| 2846 | filesize = ftell(fp); |
| 2847 | rewind(fp); |
| 2848 | filebuf = (char *)malloc( filesize+1 ); |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 2849 | if( filesize>100000000 || filebuf==0 ){ |
| 2850 | ErrorMsg(ps.filename,0,"Input file too large."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2851 | gp->errorcnt++; |
drh | e0a59cf | 2011-08-30 00:58:58 +0000 | [diff] [blame] | 2852 | fclose(fp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2853 | return; |
| 2854 | } |
| 2855 | if( fread(filebuf,1,filesize,fp)!=filesize ){ |
| 2856 | ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.", |
| 2857 | filesize); |
| 2858 | free(filebuf); |
| 2859 | gp->errorcnt++; |
drh | e0a59cf | 2011-08-30 00:58:58 +0000 | [diff] [blame] | 2860 | fclose(fp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2861 | return; |
| 2862 | } |
| 2863 | fclose(fp); |
| 2864 | filebuf[filesize] = 0; |
| 2865 | |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2866 | /* Make an initial pass through the file to handle %ifdef and %ifndef */ |
| 2867 | preprocess_input(filebuf); |
| 2868 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2869 | /* Now scan the text of the input file */ |
| 2870 | lineno = 1; |
| 2871 | for(cp=filebuf; (c= *cp)!=0; ){ |
| 2872 | if( c=='\n' ) lineno++; /* Keep track of the line number */ |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2873 | if( ISSPACE(c) ){ cp++; continue; } /* Skip all white space */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2874 | if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */ |
| 2875 | cp+=2; |
| 2876 | while( (c= *cp)!=0 && c!='\n' ) cp++; |
| 2877 | continue; |
| 2878 | } |
| 2879 | if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */ |
| 2880 | cp+=2; |
| 2881 | while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){ |
| 2882 | if( c=='\n' ) lineno++; |
| 2883 | cp++; |
| 2884 | } |
| 2885 | if( c ) cp++; |
| 2886 | continue; |
| 2887 | } |
| 2888 | ps.tokenstart = cp; /* Mark the beginning of the token */ |
| 2889 | ps.tokenlineno = lineno; /* Linenumber on which token begins */ |
| 2890 | if( c=='\"' ){ /* String literals */ |
| 2891 | cp++; |
| 2892 | while( (c= *cp)!=0 && c!='\"' ){ |
| 2893 | if( c=='\n' ) lineno++; |
| 2894 | cp++; |
| 2895 | } |
| 2896 | if( c==0 ){ |
| 2897 | ErrorMsg(ps.filename,startline, |
| 2898 | "String starting on this line is not terminated before the end of the file."); |
| 2899 | ps.errorcnt++; |
| 2900 | nextcp = cp; |
| 2901 | }else{ |
| 2902 | nextcp = cp+1; |
| 2903 | } |
| 2904 | }else if( c=='{' ){ /* A block of C code */ |
| 2905 | int level; |
| 2906 | cp++; |
| 2907 | for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){ |
| 2908 | if( c=='\n' ) lineno++; |
| 2909 | else if( c=='{' ) level++; |
| 2910 | else if( c=='}' ) level--; |
| 2911 | else if( c=='/' && cp[1]=='*' ){ /* Skip comments */ |
| 2912 | int prevc; |
| 2913 | cp = &cp[2]; |
| 2914 | prevc = 0; |
| 2915 | while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){ |
| 2916 | if( c=='\n' ) lineno++; |
| 2917 | prevc = c; |
| 2918 | cp++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2919 | } |
| 2920 | }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2921 | cp = &cp[2]; |
| 2922 | while( (c= *cp)!=0 && c!='\n' ) cp++; |
| 2923 | if( c ) lineno++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2924 | }else if( c=='\'' || c=='\"' ){ /* String a character literals */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2925 | int startchar, prevc; |
| 2926 | startchar = c; |
| 2927 | prevc = 0; |
| 2928 | for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){ |
| 2929 | if( c=='\n' ) lineno++; |
| 2930 | if( prevc=='\\' ) prevc = 0; |
| 2931 | else prevc = c; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2932 | } |
| 2933 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2934 | } |
| 2935 | if( c==0 ){ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2936 | ErrorMsg(ps.filename,ps.tokenlineno, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2937 | "C code starting on this line is not terminated before the end of the file."); |
| 2938 | ps.errorcnt++; |
| 2939 | nextcp = cp; |
| 2940 | }else{ |
| 2941 | nextcp = cp+1; |
| 2942 | } |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2943 | }else if( ISALNUM(c) ){ /* Identifiers */ |
| 2944 | while( (c= *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2945 | nextcp = cp; |
| 2946 | }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */ |
| 2947 | cp += 3; |
| 2948 | nextcp = cp; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2949 | }else if( (c=='/' || c=='|') && ISALPHA(cp[1]) ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2950 | cp += 2; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2951 | while( (c = *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2952 | nextcp = cp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2953 | }else{ /* All other (one character) operators */ |
| 2954 | cp++; |
| 2955 | nextcp = cp; |
| 2956 | } |
| 2957 | c = *cp; |
| 2958 | *cp = 0; /* Null terminate the token */ |
| 2959 | parseonetoken(&ps); /* Parse the token */ |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 2960 | *cp = (char)c; /* Restore the buffer */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2961 | cp = nextcp; |
| 2962 | } |
| 2963 | free(filebuf); /* Release the buffer after parsing */ |
| 2964 | gp->rule = ps.firstrule; |
| 2965 | gp->errorcnt = ps.errorcnt; |
| 2966 | } |
| 2967 | /*************************** From the file "plink.c" *********************/ |
| 2968 | /* |
| 2969 | ** Routines processing configuration follow-set propagation links |
| 2970 | ** in the LEMON parser generator. |
| 2971 | */ |
| 2972 | static struct plink *plink_freelist = 0; |
| 2973 | |
| 2974 | /* Allocate a new plink */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 2975 | struct plink *Plink_new(void){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2976 | struct plink *newlink; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2977 | |
| 2978 | if( plink_freelist==0 ){ |
| 2979 | int i; |
| 2980 | int amt = 100; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 2981 | plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2982 | if( plink_freelist==0 ){ |
| 2983 | fprintf(stderr, |
| 2984 | "Unable to allocate memory for a new follow-set propagation link.\n"); |
| 2985 | exit(1); |
| 2986 | } |
| 2987 | for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1]; |
| 2988 | plink_freelist[amt-1].next = 0; |
| 2989 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2990 | newlink = plink_freelist; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2991 | plink_freelist = plink_freelist->next; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2992 | return newlink; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | /* Add a plink to a plink list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2996 | void Plink_add(struct plink **plpp, struct config *cfp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2997 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2998 | struct plink *newlink; |
| 2999 | newlink = Plink_new(); |
| 3000 | newlink->next = *plpp; |
| 3001 | *plpp = newlink; |
| 3002 | newlink->cfp = cfp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | /* Transfer every plink on the list "from" to the list "to" */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3006 | void Plink_copy(struct plink **to, struct plink *from) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3007 | { |
| 3008 | struct plink *nextpl; |
| 3009 | while( from ){ |
| 3010 | nextpl = from->next; |
| 3011 | from->next = *to; |
| 3012 | *to = from; |
| 3013 | from = nextpl; |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | /* Delete every plink on the list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3018 | void Plink_delete(struct plink *plp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3019 | { |
| 3020 | struct plink *nextpl; |
| 3021 | |
| 3022 | while( plp ){ |
| 3023 | nextpl = plp->next; |
| 3024 | plp->next = plink_freelist; |
| 3025 | plink_freelist = plp; |
| 3026 | plp = nextpl; |
| 3027 | } |
| 3028 | } |
| 3029 | /*********************** From the file "report.c" **************************/ |
| 3030 | /* |
| 3031 | ** Procedures for generating reports and tables in the LEMON parser generator. |
| 3032 | */ |
| 3033 | |
| 3034 | /* Generate a filename with the given suffix. Space to hold the |
| 3035 | ** name comes from malloc() and must be freed by the calling |
| 3036 | ** function. |
| 3037 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3038 | PRIVATE char *file_makename(struct lemon *lemp, const char *suffix) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3039 | { |
| 3040 | char *name; |
| 3041 | char *cp; |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 3042 | char *filename = lemp->filename; |
| 3043 | int sz; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3044 | |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 3045 | if( outputDir ){ |
| 3046 | cp = strrchr(filename, '/'); |
| 3047 | if( cp ) filename = cp + 1; |
| 3048 | } |
| 3049 | sz = lemonStrlen(filename); |
| 3050 | sz += lemonStrlen(suffix); |
| 3051 | if( outputDir ) sz += lemonStrlen(outputDir) + 1; |
| 3052 | sz += 5; |
| 3053 | name = (char*)malloc( sz ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3054 | if( name==0 ){ |
| 3055 | fprintf(stderr,"Can't allocate space for a filename.\n"); |
| 3056 | exit(1); |
| 3057 | } |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 3058 | name[0] = 0; |
| 3059 | if( outputDir ){ |
| 3060 | lemon_strcpy(name, outputDir); |
| 3061 | lemon_strcat(name, "/"); |
| 3062 | } |
| 3063 | lemon_strcat(name,filename); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3064 | cp = strrchr(name,'.'); |
| 3065 | if( cp ) *cp = 0; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3066 | lemon_strcat(name,suffix); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3067 | return name; |
| 3068 | } |
| 3069 | |
| 3070 | /* Open a file with a name based on the name of the input file, |
| 3071 | ** but with a different (specified) suffix, and return a pointer |
| 3072 | ** to the stream */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3073 | PRIVATE FILE *file_open( |
| 3074 | struct lemon *lemp, |
| 3075 | const char *suffix, |
| 3076 | const char *mode |
| 3077 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3078 | FILE *fp; |
| 3079 | |
| 3080 | if( lemp->outname ) free(lemp->outname); |
| 3081 | lemp->outname = file_makename(lemp, suffix); |
| 3082 | fp = fopen(lemp->outname,mode); |
| 3083 | if( fp==0 && *mode=='w' ){ |
| 3084 | fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname); |
| 3085 | lemp->errorcnt++; |
| 3086 | return 0; |
| 3087 | } |
| 3088 | return fp; |
| 3089 | } |
| 3090 | |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3091 | /* Print the text of a rule |
| 3092 | */ |
| 3093 | void rule_print(FILE *out, struct rule *rp){ |
| 3094 | int i, j; |
| 3095 | fprintf(out, "%s",rp->lhs->name); |
| 3096 | /* if( rp->lhsalias ) fprintf(out,"(%s)",rp->lhsalias); */ |
| 3097 | fprintf(out," ::="); |
| 3098 | for(i=0; i<rp->nrhs; i++){ |
| 3099 | struct symbol *sp = rp->rhs[i]; |
| 3100 | if( sp->type==MULTITERMINAL ){ |
| 3101 | fprintf(out," %s", sp->subsym[0]->name); |
| 3102 | for(j=1; j<sp->nsubsym; j++){ |
| 3103 | fprintf(out,"|%s", sp->subsym[j]->name); |
| 3104 | } |
| 3105 | }else{ |
| 3106 | fprintf(out," %s", sp->name); |
| 3107 | } |
| 3108 | /* if( rp->rhsalias[i] ) fprintf(out,"(%s)",rp->rhsalias[i]); */ |
| 3109 | } |
| 3110 | } |
| 3111 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3112 | /* Duplicate the input file without comments and without actions |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3113 | ** on rules */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3114 | void Reprint(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3115 | { |
| 3116 | struct rule *rp; |
| 3117 | struct symbol *sp; |
| 3118 | int i, j, maxlen, len, ncolumns, skip; |
| 3119 | printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename); |
| 3120 | maxlen = 10; |
| 3121 | for(i=0; i<lemp->nsymbol; i++){ |
| 3122 | sp = lemp->symbols[i]; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3123 | len = lemonStrlen(sp->name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3124 | if( len>maxlen ) maxlen = len; |
| 3125 | } |
| 3126 | ncolumns = 76/(maxlen+5); |
| 3127 | if( ncolumns<1 ) ncolumns = 1; |
| 3128 | skip = (lemp->nsymbol + ncolumns - 1)/ncolumns; |
| 3129 | for(i=0; i<skip; i++){ |
| 3130 | printf("//"); |
| 3131 | for(j=i; j<lemp->nsymbol; j+=skip){ |
| 3132 | sp = lemp->symbols[j]; |
| 3133 | assert( sp->index==j ); |
| 3134 | printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name); |
| 3135 | } |
| 3136 | printf("\n"); |
| 3137 | } |
| 3138 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3139 | rule_print(stdout, rp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3140 | printf("."); |
| 3141 | if( rp->precsym ) printf(" [%s]",rp->precsym->name); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3142 | /* if( rp->code ) printf("\n %s",rp->code); */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3143 | printf("\n"); |
| 3144 | } |
| 3145 | } |
| 3146 | |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3147 | /* Print a single rule. |
| 3148 | */ |
| 3149 | void RulePrint(FILE *fp, struct rule *rp, int iCursor){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3150 | struct symbol *sp; |
| 3151 | int i, j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3152 | fprintf(fp,"%s ::=",rp->lhs->name); |
| 3153 | for(i=0; i<=rp->nrhs; i++){ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3154 | if( i==iCursor ) fprintf(fp," *"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3155 | if( i==rp->nrhs ) break; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3156 | sp = rp->rhs[i]; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3157 | if( sp->type==MULTITERMINAL ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 3158 | fprintf(fp," %s", sp->subsym[0]->name); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3159 | for(j=1; j<sp->nsubsym; j++){ |
| 3160 | fprintf(fp,"|%s",sp->subsym[j]->name); |
| 3161 | } |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 3162 | }else{ |
| 3163 | fprintf(fp," %s", sp->name); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3164 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3165 | } |
| 3166 | } |
| 3167 | |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3168 | /* Print the rule for a configuration. |
| 3169 | */ |
| 3170 | void ConfigPrint(FILE *fp, struct config *cfp){ |
| 3171 | RulePrint(fp, cfp->rp, cfp->dot); |
| 3172 | } |
| 3173 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3174 | /* #define TEST */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3175 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3176 | /* Print a set */ |
| 3177 | PRIVATE void SetPrint(out,set,lemp) |
| 3178 | FILE *out; |
| 3179 | char *set; |
| 3180 | struct lemon *lemp; |
| 3181 | { |
| 3182 | int i; |
| 3183 | char *spacer; |
| 3184 | spacer = ""; |
| 3185 | fprintf(out,"%12s[",""); |
| 3186 | for(i=0; i<lemp->nterminal; i++){ |
| 3187 | if( SetFind(set,i) ){ |
| 3188 | fprintf(out,"%s%s",spacer,lemp->symbols[i]->name); |
| 3189 | spacer = " "; |
| 3190 | } |
| 3191 | } |
| 3192 | fprintf(out,"]\n"); |
| 3193 | } |
| 3194 | |
| 3195 | /* Print a plink chain */ |
| 3196 | PRIVATE void PlinkPrint(out,plp,tag) |
| 3197 | FILE *out; |
| 3198 | struct plink *plp; |
| 3199 | char *tag; |
| 3200 | { |
| 3201 | while( plp ){ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 3202 | fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3203 | ConfigPrint(out,plp->cfp); |
| 3204 | fprintf(out,"\n"); |
| 3205 | plp = plp->next; |
| 3206 | } |
| 3207 | } |
| 3208 | #endif |
| 3209 | |
| 3210 | /* Print an action to the given file descriptor. Return FALSE if |
| 3211 | ** nothing was actually printed. |
| 3212 | */ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3213 | int PrintAction( |
| 3214 | struct action *ap, /* The action to print */ |
| 3215 | FILE *fp, /* Print the action here */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3216 | int indent /* Indent by this amount */ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3217 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3218 | int result = 1; |
| 3219 | switch( ap->type ){ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3220 | case SHIFT: { |
| 3221 | struct state *stp = ap->x.stp; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3222 | fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3223 | break; |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3224 | } |
| 3225 | case REDUCE: { |
| 3226 | struct rule *rp = ap->x.rp; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3227 | fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->iRule); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3228 | RulePrint(fp, rp, -1); |
| 3229 | break; |
| 3230 | } |
| 3231 | case SHIFTREDUCE: { |
| 3232 | struct rule *rp = ap->x.rp; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3233 | fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->iRule); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3234 | RulePrint(fp, rp, -1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3235 | break; |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3236 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3237 | case ACCEPT: |
| 3238 | fprintf(fp,"%*s accept",indent,ap->sp->name); |
| 3239 | break; |
| 3240 | case ERROR: |
| 3241 | fprintf(fp,"%*s error",indent,ap->sp->name); |
| 3242 | break; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3243 | case SRCONFLICT: |
| 3244 | case RRCONFLICT: |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3245 | fprintf(fp,"%*s reduce %-7d ** Parsing conflict **", |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3246 | indent,ap->sp->name,ap->x.rp->iRule); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3247 | break; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3248 | case SSCONFLICT: |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3249 | fprintf(fp,"%*s shift %-7d ** Parsing conflict **", |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3250 | indent,ap->sp->name,ap->x.stp->statenum); |
| 3251 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3252 | case SH_RESOLVED: |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3253 | if( showPrecedenceConflict ){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3254 | fprintf(fp,"%*s shift %-7d -- dropped by precedence", |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3255 | indent,ap->sp->name,ap->x.stp->statenum); |
| 3256 | }else{ |
| 3257 | result = 0; |
| 3258 | } |
| 3259 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3260 | case RD_RESOLVED: |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3261 | if( showPrecedenceConflict ){ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3262 | fprintf(fp,"%*s reduce %-7d -- dropped by precedence", |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3263 | indent,ap->sp->name,ap->x.rp->iRule); |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3264 | }else{ |
| 3265 | result = 0; |
| 3266 | } |
| 3267 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3268 | case NOT_USED: |
| 3269 | result = 0; |
| 3270 | break; |
| 3271 | } |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 3272 | if( result && ap->spOpt ){ |
| 3273 | fprintf(fp," /* because %s==%s */", ap->sp->name, ap->spOpt->name); |
| 3274 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3275 | return result; |
| 3276 | } |
| 3277 | |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3278 | /* Generate the "*.out" log file */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3279 | void ReportOutput(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3280 | { |
| 3281 | int i; |
| 3282 | struct state *stp; |
| 3283 | struct config *cfp; |
| 3284 | struct action *ap; |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3285 | struct rule *rp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3286 | FILE *fp; |
| 3287 | |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 3288 | fp = file_open(lemp,".out","wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3289 | if( fp==0 ) return; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3290 | for(i=0; i<lemp->nxstate; i++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3291 | stp = lemp->sorted[i]; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 3292 | fprintf(fp,"State %d:\n",stp->statenum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3293 | if( lemp->basisflag ) cfp=stp->bp; |
| 3294 | else cfp=stp->cfp; |
| 3295 | while( cfp ){ |
| 3296 | char buf[20]; |
| 3297 | if( cfp->dot==cfp->rp->nrhs ){ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3298 | lemon_sprintf(buf,"(%d)",cfp->rp->iRule); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3299 | fprintf(fp," %5s ",buf); |
| 3300 | }else{ |
| 3301 | fprintf(fp," "); |
| 3302 | } |
| 3303 | ConfigPrint(fp,cfp); |
| 3304 | fprintf(fp,"\n"); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3305 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3306 | SetPrint(fp,cfp->fws,lemp); |
| 3307 | PlinkPrint(fp,cfp->fplp,"To "); |
| 3308 | PlinkPrint(fp,cfp->bplp,"From"); |
| 3309 | #endif |
| 3310 | if( lemp->basisflag ) cfp=cfp->bp; |
| 3311 | else cfp=cfp->next; |
| 3312 | } |
| 3313 | fprintf(fp,"\n"); |
| 3314 | for(ap=stp->ap; ap; ap=ap->next){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3315 | if( PrintAction(ap,fp,30) ) fprintf(fp,"\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3316 | } |
| 3317 | fprintf(fp,"\n"); |
| 3318 | } |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 3319 | fprintf(fp, "----------------------------------------------------\n"); |
| 3320 | fprintf(fp, "Symbols:\n"); |
| 3321 | for(i=0; i<lemp->nsymbol; i++){ |
| 3322 | int j; |
| 3323 | struct symbol *sp; |
| 3324 | |
| 3325 | sp = lemp->symbols[i]; |
| 3326 | fprintf(fp, " %3d: %s", i, sp->name); |
| 3327 | if( sp->type==NONTERMINAL ){ |
| 3328 | fprintf(fp, ":"); |
| 3329 | if( sp->lambda ){ |
| 3330 | fprintf(fp, " <lambda>"); |
| 3331 | } |
| 3332 | for(j=0; j<lemp->nterminal; j++){ |
| 3333 | if( sp->firstset && SetFind(sp->firstset, j) ){ |
| 3334 | fprintf(fp, " %s", lemp->symbols[j]->name); |
| 3335 | } |
| 3336 | } |
| 3337 | } |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3338 | if( sp->prec>=0 ) fprintf(fp," (precedence=%d)", sp->prec); |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 3339 | fprintf(fp, "\n"); |
| 3340 | } |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3341 | fprintf(fp, "----------------------------------------------------\n"); |
| 3342 | fprintf(fp, "Rules:\n"); |
| 3343 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 3344 | fprintf(fp, "%4d: ", rp->iRule); |
| 3345 | rule_print(fp, rp); |
| 3346 | fprintf(fp,"."); |
| 3347 | if( rp->precsym ){ |
| 3348 | fprintf(fp," [%s precedence=%d]", |
| 3349 | rp->precsym->name, rp->precsym->prec); |
| 3350 | } |
| 3351 | fprintf(fp,"\n"); |
| 3352 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3353 | fclose(fp); |
| 3354 | return; |
| 3355 | } |
| 3356 | |
| 3357 | /* Search for the file "name" which is in the same directory as |
| 3358 | ** the exacutable */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3359 | PRIVATE char *pathsearch(char *argv0, char *name, int modemask) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3360 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3361 | const char *pathlist; |
| 3362 | char *pathbufptr; |
| 3363 | char *pathbuf; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3364 | char *path,*cp; |
| 3365 | char c; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3366 | |
| 3367 | #ifdef __WIN32__ |
| 3368 | cp = strrchr(argv0,'\\'); |
| 3369 | #else |
| 3370 | cp = strrchr(argv0,'/'); |
| 3371 | #endif |
| 3372 | if( cp ){ |
| 3373 | c = *cp; |
| 3374 | *cp = 0; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3375 | path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 ); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3376 | if( path ) lemon_sprintf(path,"%s/%s",argv0,name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3377 | *cp = c; |
| 3378 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3379 | pathlist = getenv("PATH"); |
| 3380 | if( pathlist==0 ) pathlist = ".:/bin:/usr/bin"; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3381 | pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 ); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3382 | path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 ); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3383 | if( (pathbuf != 0) && (path!=0) ){ |
| 3384 | pathbufptr = pathbuf; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3385 | lemon_strcpy(pathbuf, pathlist); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3386 | while( *pathbuf ){ |
| 3387 | cp = strchr(pathbuf,':'); |
| 3388 | if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3389 | c = *cp; |
| 3390 | *cp = 0; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3391 | lemon_sprintf(path,"%s/%s",pathbuf,name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3392 | *cp = c; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3393 | if( c==0 ) pathbuf[0] = 0; |
| 3394 | else pathbuf = &cp[1]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3395 | if( access(path,modemask)==0 ) break; |
| 3396 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3397 | free(pathbufptr); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3398 | } |
| 3399 | } |
| 3400 | return path; |
| 3401 | } |
| 3402 | |
| 3403 | /* Given an action, compute the integer value for that action |
| 3404 | ** which is to be put in the action table of the generated machine. |
| 3405 | ** Return negative if no action should be generated. |
| 3406 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3407 | PRIVATE int compute_action(struct lemon *lemp, struct action *ap) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3408 | { |
| 3409 | int act; |
| 3410 | switch( ap->type ){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3411 | case SHIFT: act = ap->x.stp->statenum; break; |
drh | bd8fcc1 | 2017-06-28 11:56:18 +0000 | [diff] [blame] | 3412 | case SHIFTREDUCE: { |
drh | bd8fcc1 | 2017-06-28 11:56:18 +0000 | [diff] [blame] | 3413 | /* Since a SHIFT is inherient after a prior REDUCE, convert any |
| 3414 | ** SHIFTREDUCE action with a nonterminal on the LHS into a simple |
| 3415 | ** REDUCE action: */ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3416 | if( ap->sp->index>=lemp->nterminal ){ |
| 3417 | act = lemp->minReduce + ap->x.rp->iRule; |
| 3418 | }else{ |
| 3419 | act = lemp->minShiftReduce + ap->x.rp->iRule; |
| 3420 | } |
drh | bd8fcc1 | 2017-06-28 11:56:18 +0000 | [diff] [blame] | 3421 | break; |
| 3422 | } |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3423 | case REDUCE: act = lemp->minReduce + ap->x.rp->iRule; break; |
| 3424 | case ERROR: act = lemp->errAction; break; |
| 3425 | case ACCEPT: act = lemp->accAction; break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3426 | default: act = -1; break; |
| 3427 | } |
| 3428 | return act; |
| 3429 | } |
| 3430 | |
| 3431 | #define LINESIZE 1000 |
| 3432 | /* The next cluster of routines are for reading the template file |
| 3433 | ** and writing the results to the generated parser */ |
| 3434 | /* The first function transfers data from "in" to "out" until |
| 3435 | ** a line is seen which begins with "%%". The line number is |
| 3436 | ** tracked. |
| 3437 | ** |
| 3438 | ** if name!=0, then any word that begin with "Parse" is changed to |
| 3439 | ** begin with *name instead. |
| 3440 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3441 | PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3442 | { |
| 3443 | int i, iStart; |
| 3444 | char line[LINESIZE]; |
| 3445 | while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){ |
| 3446 | (*lineno)++; |
| 3447 | iStart = 0; |
| 3448 | if( name ){ |
| 3449 | for(i=0; line[i]; i++){ |
| 3450 | if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0 |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3451 | && (i==0 || !ISALPHA(line[i-1])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3452 | ){ |
| 3453 | if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]); |
| 3454 | fprintf(out,"%s",name); |
| 3455 | i += 4; |
| 3456 | iStart = i+1; |
| 3457 | } |
| 3458 | } |
| 3459 | } |
| 3460 | fprintf(out,"%s",&line[iStart]); |
| 3461 | } |
| 3462 | } |
| 3463 | |
| 3464 | /* The next function finds the template file and opens it, returning |
| 3465 | ** a pointer to the opened file. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3466 | PRIVATE FILE *tplt_open(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3467 | { |
| 3468 | static char templatename[] = "lempar.c"; |
| 3469 | char buf[1000]; |
| 3470 | FILE *in; |
| 3471 | char *tpltname; |
| 3472 | char *cp; |
| 3473 | |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 3474 | /* first, see if user specified a template filename on the command line. */ |
| 3475 | if (user_templatename != 0) { |
| 3476 | if( access(user_templatename,004)==-1 ){ |
| 3477 | fprintf(stderr,"Can't find the parser driver template file \"%s\".\n", |
| 3478 | user_templatename); |
| 3479 | lemp->errorcnt++; |
| 3480 | return 0; |
| 3481 | } |
| 3482 | in = fopen(user_templatename,"rb"); |
| 3483 | if( in==0 ){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3484 | fprintf(stderr,"Can't open the template file \"%s\".\n", |
| 3485 | user_templatename); |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 3486 | lemp->errorcnt++; |
| 3487 | return 0; |
| 3488 | } |
| 3489 | return in; |
| 3490 | } |
| 3491 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3492 | cp = strrchr(lemp->filename,'.'); |
| 3493 | if( cp ){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3494 | lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3495 | }else{ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3496 | lemon_sprintf(buf,"%s.lt",lemp->filename); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3497 | } |
| 3498 | if( access(buf,004)==0 ){ |
| 3499 | tpltname = buf; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3500 | }else if( access(templatename,004)==0 ){ |
| 3501 | tpltname = templatename; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3502 | }else{ |
| 3503 | tpltname = pathsearch(lemp->argv0,templatename,0); |
| 3504 | } |
| 3505 | if( tpltname==0 ){ |
| 3506 | fprintf(stderr,"Can't find the parser driver template file \"%s\".\n", |
| 3507 | templatename); |
| 3508 | lemp->errorcnt++; |
| 3509 | return 0; |
| 3510 | } |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 3511 | in = fopen(tpltname,"rb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3512 | if( in==0 ){ |
| 3513 | fprintf(stderr,"Can't open the template file \"%s\".\n",templatename); |
| 3514 | lemp->errorcnt++; |
| 3515 | return 0; |
| 3516 | } |
| 3517 | return in; |
| 3518 | } |
| 3519 | |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3520 | /* Print a #line directive line to the output file. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3521 | PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename) |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3522 | { |
| 3523 | fprintf(out,"#line %d \"",lineno); |
| 3524 | while( *filename ){ |
| 3525 | if( *filename == '\\' ) putc('\\',out); |
| 3526 | putc(*filename,out); |
| 3527 | filename++; |
| 3528 | } |
| 3529 | fprintf(out,"\"\n"); |
| 3530 | } |
| 3531 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3532 | /* Print a string to the file and keep the linenumber up to date */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3533 | PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3534 | { |
| 3535 | if( str==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3536 | while( *str ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3537 | putc(*str,out); |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3538 | if( *str=='\n' ) (*lineno)++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3539 | str++; |
| 3540 | } |
drh | 9db55df | 2004-09-09 14:01:21 +0000 | [diff] [blame] | 3541 | if( str[-1]!='\n' ){ |
| 3542 | putc('\n',out); |
| 3543 | (*lineno)++; |
| 3544 | } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3545 | if (!lemp->nolinenosflag) { |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3546 | (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3547 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3548 | return; |
| 3549 | } |
| 3550 | |
| 3551 | /* |
| 3552 | ** The following routine emits code for the destructor for the |
| 3553 | ** symbol sp |
| 3554 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3555 | void emit_destructor_code( |
| 3556 | FILE *out, |
| 3557 | struct symbol *sp, |
| 3558 | struct lemon *lemp, |
| 3559 | int *lineno |
| 3560 | ){ |
drh | cc83b6e | 2004-04-23 23:38:42 +0000 | [diff] [blame] | 3561 | char *cp = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3562 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3563 | if( sp->type==TERMINAL ){ |
| 3564 | cp = lemp->tokendest; |
| 3565 | if( cp==0 ) return; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 3566 | fprintf(out,"{\n"); (*lineno)++; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3567 | }else if( sp->destructor ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3568 | cp = sp->destructor; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 3569 | fprintf(out,"{\n"); (*lineno)++; |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3570 | if( !lemp->nolinenosflag ){ |
| 3571 | (*lineno)++; |
| 3572 | tplt_linedir(out,sp->destLineno,lemp->filename); |
| 3573 | } |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3574 | }else if( lemp->vardest ){ |
| 3575 | cp = lemp->vardest; |
| 3576 | if( cp==0 ) return; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 3577 | fprintf(out,"{\n"); (*lineno)++; |
drh | cc83b6e | 2004-04-23 23:38:42 +0000 | [diff] [blame] | 3578 | }else{ |
| 3579 | assert( 0 ); /* Cannot happen */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3580 | } |
| 3581 | for(; *cp; cp++){ |
| 3582 | if( *cp=='$' && cp[1]=='$' ){ |
| 3583 | fprintf(out,"(yypminor->yy%d)",sp->dtnum); |
| 3584 | cp++; |
| 3585 | continue; |
| 3586 | } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3587 | if( *cp=='\n' ) (*lineno)++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3588 | fputc(*cp,out); |
| 3589 | } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3590 | fprintf(out,"\n"); (*lineno)++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3591 | if (!lemp->nolinenosflag) { |
| 3592 | (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3593 | } |
| 3594 | fprintf(out,"}\n"); (*lineno)++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3595 | return; |
| 3596 | } |
| 3597 | |
| 3598 | /* |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3599 | ** Return TRUE (non-zero) if the given symbol has a destructor. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3600 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3601 | int has_destructor(struct symbol *sp, struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3602 | { |
| 3603 | int ret; |
| 3604 | if( sp->type==TERMINAL ){ |
| 3605 | ret = lemp->tokendest!=0; |
| 3606 | }else{ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3607 | ret = lemp->vardest!=0 || sp->destructor!=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3608 | } |
| 3609 | return ret; |
| 3610 | } |
| 3611 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3612 | /* |
| 3613 | ** Append text to a dynamically allocated string. If zText is 0 then |
| 3614 | ** reset the string to be empty again. Always return the complete text |
| 3615 | ** of the string (which is overwritten with each call). |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3616 | ** |
| 3617 | ** n bytes of zText are stored. If n==0 then all of zText up to the first |
| 3618 | ** \000 terminator is stored. zText can contain up to two instances of |
| 3619 | ** %d. The values of p1 and p2 are written into the first and second |
| 3620 | ** %d. |
| 3621 | ** |
| 3622 | ** If n==-1, then the previous character is overwritten. |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3623 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3624 | PRIVATE char *append_str(const char *zText, int n, int p1, int p2){ |
| 3625 | static char empty[1] = { 0 }; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3626 | static char *z = 0; |
| 3627 | static int alloced = 0; |
| 3628 | static int used = 0; |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3629 | int c; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3630 | char zInt[40]; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3631 | if( zText==0 ){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3632 | if( used==0 && z!=0 ) z[0] = 0; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3633 | used = 0; |
| 3634 | return z; |
| 3635 | } |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3636 | if( n<=0 ){ |
| 3637 | if( n<0 ){ |
| 3638 | used += n; |
| 3639 | assert( used>=0 ); |
| 3640 | } |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3641 | n = lemonStrlen(zText); |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3642 | } |
drh | df60971 | 2010-11-23 20:55:27 +0000 | [diff] [blame] | 3643 | if( (int) (n+sizeof(zInt)*2+used) >= alloced ){ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3644 | alloced = n + sizeof(zInt)*2 + used + 200; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3645 | z = (char *) realloc(z, alloced); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3646 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3647 | if( z==0 ) return empty; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3648 | while( n-- > 0 ){ |
| 3649 | c = *(zText++); |
drh | 5048962 | 2006-10-13 12:25:29 +0000 | [diff] [blame] | 3650 | if( c=='%' && n>0 && zText[0]=='d' ){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3651 | lemon_sprintf(zInt, "%d", p1); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3652 | p1 = p2; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3653 | lemon_strcpy(&z[used], zInt); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3654 | used += lemonStrlen(&z[used]); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3655 | zText++; |
| 3656 | n--; |
| 3657 | }else{ |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 3658 | z[used++] = (char)c; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3659 | } |
| 3660 | } |
| 3661 | z[used] = 0; |
| 3662 | return z; |
| 3663 | } |
| 3664 | |
| 3665 | /* |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3666 | ** Write and transform the rp->code string so that symbols are expanded. |
| 3667 | ** Populate the rp->codePrefix and rp->codeSuffix strings, as appropriate. |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3668 | ** |
| 3669 | ** Return 1 if the expanded code requires that "yylhsminor" local variable |
| 3670 | ** to be defined. |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3671 | */ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3672 | PRIVATE int translate_code(struct lemon *lemp, struct rule *rp){ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3673 | char *cp, *xp; |
| 3674 | int i; |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3675 | int rc = 0; /* True if yylhsminor is used */ |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 3676 | int dontUseRhs0 = 0; /* If true, use of left-most RHS label is illegal */ |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3677 | const char *zSkip = 0; /* The zOvwrt comment within rp->code, or NULL */ |
| 3678 | char lhsused = 0; /* True if the LHS element has been used */ |
| 3679 | char lhsdirect; /* True if LHS writes directly into stack */ |
| 3680 | char used[MAXRHS]; /* True for each RHS element which is used */ |
| 3681 | char zLhs[50]; /* Convert the LHS symbol into this string */ |
| 3682 | char zOvwrt[900]; /* Comment that to allow LHS to overwrite RHS */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3683 | |
| 3684 | for(i=0; i<rp->nrhs; i++) used[i] = 0; |
| 3685 | lhsused = 0; |
| 3686 | |
drh | 19c9e56 | 2007-03-29 20:13:53 +0000 | [diff] [blame] | 3687 | if( rp->code==0 ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3688 | static char newlinestr[2] = { '\n', '\0' }; |
| 3689 | rp->code = newlinestr; |
drh | 19c9e56 | 2007-03-29 20:13:53 +0000 | [diff] [blame] | 3690 | rp->line = rp->ruleline; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3691 | rp->noCode = 1; |
| 3692 | }else{ |
| 3693 | rp->noCode = 0; |
drh | 19c9e56 | 2007-03-29 20:13:53 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3696 | |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 3697 | if( rp->nrhs==0 ){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3698 | /* If there are no RHS symbols, then writing directly to the LHS is ok */ |
| 3699 | lhsdirect = 1; |
| 3700 | }else if( rp->rhsalias[0]==0 ){ |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 3701 | /* The left-most RHS symbol has no value. LHS direct is ok. But |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3702 | ** we have to call the distructor on the RHS symbol first. */ |
| 3703 | lhsdirect = 1; |
| 3704 | if( has_destructor(rp->rhs[0],lemp) ){ |
| 3705 | append_str(0,0,0,0); |
| 3706 | append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0, |
| 3707 | rp->rhs[0]->index,1-rp->nrhs); |
| 3708 | rp->codePrefix = Strsafe(append_str(0,0,0,0)); |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3709 | rp->noCode = 0; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3710 | } |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 3711 | }else if( rp->lhsalias==0 ){ |
| 3712 | /* There is no LHS value symbol. */ |
| 3713 | lhsdirect = 1; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3714 | }else if( strcmp(rp->lhsalias,rp->rhsalias[0])==0 ){ |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3715 | /* The LHS symbol and the left-most RHS symbol are the same, so |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3716 | ** direct writing is allowed */ |
| 3717 | lhsdirect = 1; |
| 3718 | lhsused = 1; |
| 3719 | used[0] = 1; |
| 3720 | if( rp->lhs->dtnum!=rp->rhs[0]->dtnum ){ |
| 3721 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3722 | "%s(%s) and %s(%s) share the same label but have " |
| 3723 | "different datatypes.", |
| 3724 | rp->lhs->name, rp->lhsalias, rp->rhs[0]->name, rp->rhsalias[0]); |
| 3725 | lemp->errorcnt++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3726 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3727 | }else{ |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3728 | lemon_sprintf(zOvwrt, "/*%s-overwrites-%s*/", |
| 3729 | rp->lhsalias, rp->rhsalias[0]); |
| 3730 | zSkip = strstr(rp->code, zOvwrt); |
| 3731 | if( zSkip!=0 ){ |
| 3732 | /* The code contains a special comment that indicates that it is safe |
| 3733 | ** for the LHS label to overwrite left-most RHS label. */ |
| 3734 | lhsdirect = 1; |
| 3735 | }else{ |
| 3736 | lhsdirect = 0; |
| 3737 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3738 | } |
| 3739 | if( lhsdirect ){ |
| 3740 | sprintf(zLhs, "yymsp[%d].minor.yy%d",1-rp->nrhs,rp->lhs->dtnum); |
| 3741 | }else{ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3742 | rc = 1; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3743 | sprintf(zLhs, "yylhsminor.yy%d",rp->lhs->dtnum); |
| 3744 | } |
| 3745 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3746 | append_str(0,0,0,0); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3747 | |
| 3748 | /* This const cast is wrong but harmless, if we're careful. */ |
| 3749 | for(cp=(char *)rp->code; *cp; cp++){ |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3750 | if( cp==zSkip ){ |
| 3751 | append_str(zOvwrt,0,0,0); |
| 3752 | cp += lemonStrlen(zOvwrt)-1; |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 3753 | dontUseRhs0 = 1; |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3754 | continue; |
| 3755 | } |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3756 | if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3757 | char saved; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3758 | for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3759 | saved = *xp; |
| 3760 | *xp = 0; |
| 3761 | if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3762 | append_str(zLhs,0,0,0); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3763 | cp = xp; |
| 3764 | lhsused = 1; |
| 3765 | }else{ |
| 3766 | for(i=0; i<rp->nrhs; i++){ |
| 3767 | if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){ |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 3768 | if( i==0 && dontUseRhs0 ){ |
| 3769 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3770 | "Label %s used after '%s'.", |
| 3771 | rp->rhsalias[0], zOvwrt); |
| 3772 | lemp->errorcnt++; |
| 3773 | }else if( cp!=rp->code && cp[-1]=='@' ){ |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3774 | /* If the argument is of the form @X then substituted |
| 3775 | ** the token number of X, not the value of X */ |
| 3776 | append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0); |
| 3777 | }else{ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3778 | struct symbol *sp = rp->rhs[i]; |
| 3779 | int dtnum; |
| 3780 | if( sp->type==MULTITERMINAL ){ |
| 3781 | dtnum = sp->subsym[0]->dtnum; |
| 3782 | }else{ |
| 3783 | dtnum = sp->dtnum; |
| 3784 | } |
| 3785 | append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum); |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3786 | } |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3787 | cp = xp; |
| 3788 | used[i] = 1; |
| 3789 | break; |
| 3790 | } |
| 3791 | } |
| 3792 | } |
| 3793 | *xp = saved; |
| 3794 | } |
| 3795 | append_str(cp, 1, 0, 0); |
| 3796 | } /* End loop */ |
| 3797 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3798 | /* Main code generation completed */ |
| 3799 | cp = append_str(0,0,0,0); |
| 3800 | if( cp && cp[0] ) rp->code = Strsafe(cp); |
| 3801 | append_str(0,0,0,0); |
| 3802 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3803 | /* Check to make sure the LHS has been used */ |
| 3804 | if( rp->lhsalias && !lhsused ){ |
| 3805 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3806 | "Label \"%s\" for \"%s(%s)\" is never used.", |
| 3807 | rp->lhsalias,rp->lhs->name,rp->lhsalias); |
| 3808 | lemp->errorcnt++; |
| 3809 | } |
| 3810 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3811 | /* Generate destructor code for RHS minor values which are not referenced. |
| 3812 | ** Generate error messages for unused labels and duplicate labels. |
| 3813 | */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3814 | for(i=0; i<rp->nrhs; i++){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3815 | if( rp->rhsalias[i] ){ |
| 3816 | if( i>0 ){ |
| 3817 | int j; |
| 3818 | if( rp->lhsalias && strcmp(rp->lhsalias,rp->rhsalias[i])==0 ){ |
| 3819 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3820 | "%s(%s) has the same label as the LHS but is not the left-most " |
| 3821 | "symbol on the RHS.", |
| 3822 | rp->rhs[i]->name, rp->rhsalias); |
| 3823 | lemp->errorcnt++; |
| 3824 | } |
| 3825 | for(j=0; j<i; j++){ |
| 3826 | if( rp->rhsalias[j] && strcmp(rp->rhsalias[j],rp->rhsalias[i])==0 ){ |
| 3827 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3828 | "Label %s used for multiple symbols on the RHS of a rule.", |
| 3829 | rp->rhsalias[i]); |
| 3830 | lemp->errorcnt++; |
| 3831 | break; |
| 3832 | } |
| 3833 | } |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3834 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3835 | if( !used[i] ){ |
| 3836 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3837 | "Label %s for \"%s(%s)\" is never used.", |
| 3838 | rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]); |
| 3839 | lemp->errorcnt++; |
| 3840 | } |
| 3841 | }else if( i>0 && has_destructor(rp->rhs[i],lemp) ){ |
| 3842 | append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0, |
| 3843 | rp->rhs[i]->index,i-rp->nrhs+1); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3844 | } |
| 3845 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3846 | |
| 3847 | /* If unable to write LHS values directly into the stack, write the |
| 3848 | ** saved LHS value now. */ |
| 3849 | if( lhsdirect==0 ){ |
| 3850 | append_str(" yymsp[%d].minor.yy%d = ", 0, 1-rp->nrhs, rp->lhs->dtnum); |
| 3851 | append_str(zLhs, 0, 0, 0); |
| 3852 | append_str(";\n", 0, 0, 0); |
drh | 61e339a | 2007-01-16 03:09:02 +0000 | [diff] [blame] | 3853 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3854 | |
| 3855 | /* Suffix code generation complete */ |
| 3856 | cp = append_str(0,0,0,0); |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3857 | if( cp && cp[0] ){ |
| 3858 | rp->codeSuffix = Strsafe(cp); |
| 3859 | rp->noCode = 0; |
| 3860 | } |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3861 | |
| 3862 | return rc; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3865 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3866 | ** Generate code which executes when the rule "rp" is reduced. Write |
| 3867 | ** the code to "out". Make sure lineno stays up-to-date. |
| 3868 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3869 | PRIVATE void emit_code( |
| 3870 | FILE *out, |
| 3871 | struct rule *rp, |
| 3872 | struct lemon *lemp, |
| 3873 | int *lineno |
| 3874 | ){ |
| 3875 | const char *cp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3876 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3877 | /* Setup code prior to the #line directive */ |
| 3878 | if( rp->codePrefix && rp->codePrefix[0] ){ |
| 3879 | fprintf(out, "{%s", rp->codePrefix); |
| 3880 | for(cp=rp->codePrefix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } |
| 3881 | } |
| 3882 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3883 | /* Generate code to do the reduce action */ |
| 3884 | if( rp->code ){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3885 | if( !lemp->nolinenosflag ){ |
| 3886 | (*lineno)++; |
| 3887 | tplt_linedir(out,rp->line,lemp->filename); |
| 3888 | } |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3889 | fprintf(out,"{%s",rp->code); |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3890 | for(cp=rp->code; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3891 | fprintf(out,"}\n"); (*lineno)++; |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3892 | if( !lemp->nolinenosflag ){ |
| 3893 | (*lineno)++; |
| 3894 | tplt_linedir(out,*lineno,lemp->outname); |
| 3895 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
| 3898 | /* Generate breakdown code that occurs after the #line directive */ |
| 3899 | if( rp->codeSuffix && rp->codeSuffix[0] ){ |
| 3900 | fprintf(out, "%s", rp->codeSuffix); |
| 3901 | for(cp=rp->codeSuffix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } |
| 3902 | } |
| 3903 | |
| 3904 | if( rp->codePrefix ){ |
| 3905 | fprintf(out, "}\n"); (*lineno)++; |
| 3906 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3907 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3908 | return; |
| 3909 | } |
| 3910 | |
| 3911 | /* |
| 3912 | ** Print the definition of the union used for the parser's data stack. |
| 3913 | ** This union contains fields for every possible data type for tokens |
| 3914 | ** and nonterminals. In the process of computing and printing this |
| 3915 | ** union, also set the ".dtnum" field of every terminal and nonterminal |
| 3916 | ** symbol. |
| 3917 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3918 | void print_stack_union( |
| 3919 | FILE *out, /* The output stream */ |
| 3920 | struct lemon *lemp, /* The main info structure for this parser */ |
| 3921 | int *plineno, /* Pointer to the line number */ |
| 3922 | int mhflag /* True if generating makeheaders output */ |
| 3923 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3924 | int lineno = *plineno; /* The line number of the output */ |
| 3925 | char **types; /* A hash table of datatypes */ |
| 3926 | int arraysize; /* Size of the "types" array */ |
| 3927 | int maxdtlength; /* Maximum length of any ".datatype" field. */ |
| 3928 | char *stddt; /* Standardized name for a datatype */ |
| 3929 | int i,j; /* Loop counters */ |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 3930 | unsigned hash; /* For hashing the name of a type */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3931 | const char *name; /* Name of the parser */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3932 | |
| 3933 | /* Allocate and initialize types[] and allocate stddt[] */ |
| 3934 | arraysize = lemp->nsymbol * 2; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3935 | types = (char**)calloc( arraysize, sizeof(char*) ); |
drh | 070d422 | 2011-06-02 15:48:51 +0000 | [diff] [blame] | 3936 | if( types==0 ){ |
| 3937 | fprintf(stderr,"Out of memory.\n"); |
| 3938 | exit(1); |
| 3939 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3940 | for(i=0; i<arraysize; i++) types[i] = 0; |
| 3941 | maxdtlength = 0; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3942 | if( lemp->vartype ){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3943 | maxdtlength = lemonStrlen(lemp->vartype); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3944 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3945 | for(i=0; i<lemp->nsymbol; i++){ |
| 3946 | int len; |
| 3947 | struct symbol *sp = lemp->symbols[i]; |
| 3948 | if( sp->datatype==0 ) continue; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3949 | len = lemonStrlen(sp->datatype); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3950 | if( len>maxdtlength ) maxdtlength = len; |
| 3951 | } |
| 3952 | stddt = (char*)malloc( maxdtlength*2 + 1 ); |
drh | 070d422 | 2011-06-02 15:48:51 +0000 | [diff] [blame] | 3953 | if( stddt==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3954 | fprintf(stderr,"Out of memory.\n"); |
| 3955 | exit(1); |
| 3956 | } |
| 3957 | |
| 3958 | /* Build a hash table of datatypes. The ".dtnum" field of each symbol |
| 3959 | ** 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] | 3960 | ** used for terminal symbols. If there is no %default_type defined then |
| 3961 | ** 0 is also used as the .dtnum value for nonterminals which do not specify |
| 3962 | ** a datatype using the %type directive. |
| 3963 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3964 | for(i=0; i<lemp->nsymbol; i++){ |
| 3965 | struct symbol *sp = lemp->symbols[i]; |
| 3966 | char *cp; |
| 3967 | if( sp==lemp->errsym ){ |
| 3968 | sp->dtnum = arraysize+1; |
| 3969 | continue; |
| 3970 | } |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3971 | if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3972 | sp->dtnum = 0; |
| 3973 | continue; |
| 3974 | } |
| 3975 | cp = sp->datatype; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3976 | if( cp==0 ) cp = lemp->vartype; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3977 | j = 0; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3978 | while( ISSPACE(*cp) ) cp++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3979 | while( *cp ) stddt[j++] = *cp++; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3980 | while( j>0 && ISSPACE(stddt[j-1]) ) j--; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3981 | stddt[j] = 0; |
drh | 02368c9 | 2009-04-05 15:18:02 +0000 | [diff] [blame] | 3982 | if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){ |
drh | 32c4d74 | 2008-07-01 16:34:49 +0000 | [diff] [blame] | 3983 | sp->dtnum = 0; |
| 3984 | continue; |
| 3985 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3986 | hash = 0; |
| 3987 | for(j=0; stddt[j]; j++){ |
| 3988 | hash = hash*53 + stddt[j]; |
| 3989 | } |
drh | 3b2129c | 2003-05-13 00:34:21 +0000 | [diff] [blame] | 3990 | hash = (hash & 0x7fffffff)%arraysize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3991 | while( types[hash] ){ |
| 3992 | if( strcmp(types[hash],stddt)==0 ){ |
| 3993 | sp->dtnum = hash + 1; |
| 3994 | break; |
| 3995 | } |
| 3996 | hash++; |
drh | 2b51f21 | 2013-10-11 23:01:02 +0000 | [diff] [blame] | 3997 | if( hash>=(unsigned)arraysize ) hash = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3998 | } |
| 3999 | if( types[hash]==0 ){ |
| 4000 | sp->dtnum = hash + 1; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 4001 | types[hash] = (char*)malloc( lemonStrlen(stddt)+1 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4002 | if( types[hash]==0 ){ |
| 4003 | fprintf(stderr,"Out of memory.\n"); |
| 4004 | exit(1); |
| 4005 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 4006 | lemon_strcpy(types[hash],stddt); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */ |
| 4011 | name = lemp->name ? lemp->name : "Parse"; |
| 4012 | lineno = *plineno; |
| 4013 | if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; } |
| 4014 | fprintf(out,"#define %sTOKENTYPE %s\n",name, |
| 4015 | lemp->tokentype?lemp->tokentype:"void*"); lineno++; |
| 4016 | if( mhflag ){ fprintf(out,"#endif\n"); lineno++; } |
| 4017 | fprintf(out,"typedef union {\n"); lineno++; |
drh | 15b024c | 2008-12-11 02:20:43 +0000 | [diff] [blame] | 4018 | fprintf(out," int yyinit;\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4019 | fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++; |
| 4020 | for(i=0; i<arraysize; i++){ |
| 4021 | if( types[i]==0 ) continue; |
| 4022 | fprintf(out," %s yy%d;\n",types[i],i+1); lineno++; |
| 4023 | free(types[i]); |
| 4024 | } |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 4025 | if( lemp->errsym && lemp->errsym->useCnt ){ |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4026 | fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++; |
| 4027 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4028 | free(stddt); |
| 4029 | free(types); |
| 4030 | fprintf(out,"} YYMINORTYPE;\n"); lineno++; |
| 4031 | *plineno = lineno; |
| 4032 | } |
| 4033 | |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4034 | /* |
| 4035 | ** Return the name of a C datatype able to represent values between |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4036 | ** lwr and upr, inclusive. If pnByte!=NULL then also write the sizeof |
| 4037 | ** for that type (1, 2, or 4) into *pnByte. |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4038 | */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4039 | static const char *minimum_size_type(int lwr, int upr, int *pnByte){ |
| 4040 | const char *zType = "int"; |
| 4041 | int nByte = 4; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4042 | if( lwr>=0 ){ |
| 4043 | if( upr<=255 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4044 | zType = "unsigned char"; |
| 4045 | nByte = 1; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4046 | }else if( upr<65535 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4047 | zType = "unsigned short int"; |
| 4048 | nByte = 2; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4049 | }else{ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4050 | zType = "unsigned int"; |
| 4051 | nByte = 4; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4052 | } |
| 4053 | }else if( lwr>=-127 && upr<=127 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4054 | zType = "signed char"; |
| 4055 | nByte = 1; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4056 | }else if( lwr>=-32767 && upr<32767 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4057 | zType = "short"; |
| 4058 | nByte = 2; |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4059 | } |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4060 | if( pnByte ) *pnByte = nByte; |
| 4061 | return zType; |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4064 | /* |
| 4065 | ** Each state contains a set of token transaction and a set of |
| 4066 | ** nonterminal transactions. Each of these sets makes an instance |
| 4067 | ** of the following structure. An array of these structures is used |
| 4068 | ** to order the creation of entries in the yy_action[] table. |
| 4069 | */ |
| 4070 | struct axset { |
| 4071 | struct state *stp; /* A pointer to a state */ |
| 4072 | int isTkn; /* True to use tokens. False for non-terminals */ |
| 4073 | int nAction; /* Number of actions */ |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4074 | int iOrder; /* Original order of action sets */ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4075 | }; |
| 4076 | |
| 4077 | /* |
| 4078 | ** Compare to axset structures for sorting purposes |
| 4079 | */ |
| 4080 | static int axset_compare(const void *a, const void *b){ |
| 4081 | struct axset *p1 = (struct axset*)a; |
| 4082 | struct axset *p2 = (struct axset*)b; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4083 | int c; |
| 4084 | c = p2->nAction - p1->nAction; |
| 4085 | if( c==0 ){ |
drh | 337cd0d | 2015-09-07 23:40:42 +0000 | [diff] [blame] | 4086 | c = p1->iOrder - p2->iOrder; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4087 | } |
| 4088 | assert( c!=0 || p1==p2 ); |
| 4089 | return c; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4092 | /* |
| 4093 | ** Write text on "out" that describes the rule "rp". |
| 4094 | */ |
| 4095 | static void writeRuleText(FILE *out, struct rule *rp){ |
| 4096 | int j; |
| 4097 | fprintf(out,"%s ::=", rp->lhs->name); |
| 4098 | for(j=0; j<rp->nrhs; j++){ |
| 4099 | struct symbol *sp = rp->rhs[j]; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4100 | if( sp->type!=MULTITERMINAL ){ |
| 4101 | fprintf(out," %s", sp->name); |
| 4102 | }else{ |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4103 | int k; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4104 | fprintf(out," %s", sp->subsym[0]->name); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4105 | for(k=1; k<sp->nsubsym; k++){ |
| 4106 | fprintf(out,"|%s",sp->subsym[k]->name); |
| 4107 | } |
| 4108 | } |
| 4109 | } |
| 4110 | } |
| 4111 | |
| 4112 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4113 | /* Generate C source code for the parser */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4114 | void ReportTable( |
| 4115 | struct lemon *lemp, |
| 4116 | int mhflag /* Output in makeheaders format if true */ |
| 4117 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4118 | FILE *out, *in; |
| 4119 | char line[LINESIZE]; |
| 4120 | int lineno; |
| 4121 | struct state *stp; |
| 4122 | struct action *ap; |
| 4123 | struct rule *rp; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4124 | struct acttab *pActtab; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4125 | int i, j, n, sz; |
| 4126 | int szActionType; /* sizeof(YYACTIONTYPE) */ |
| 4127 | int szCodeType; /* sizeof(YYCODETYPE) */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4128 | const char *name; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4129 | int mnTknOfst, mxTknOfst; |
| 4130 | int mnNtOfst, mxNtOfst; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4131 | struct axset *ax; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4132 | |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4133 | lemp->minShiftReduce = lemp->nstate; |
| 4134 | lemp->errAction = lemp->minShiftReduce + lemp->nrule; |
| 4135 | lemp->accAction = lemp->errAction + 1; |
| 4136 | lemp->noAction = lemp->accAction + 1; |
| 4137 | lemp->minReduce = lemp->noAction + 1; |
| 4138 | lemp->maxAction = lemp->minReduce + lemp->nrule; |
| 4139 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4140 | in = tplt_open(lemp); |
| 4141 | if( in==0 ) return; |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 4142 | out = file_open(lemp,".c","wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4143 | if( out==0 ){ |
| 4144 | fclose(in); |
| 4145 | return; |
| 4146 | } |
| 4147 | lineno = 1; |
| 4148 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4149 | |
| 4150 | /* Generate the include code, if any */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4151 | tplt_print(out,lemp,lemp->include,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4152 | if( mhflag ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4153 | char *incName = file_makename(lemp, ".h"); |
| 4154 | fprintf(out,"#include \"%s\"\n", incName); lineno++; |
| 4155 | free(incName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4156 | } |
| 4157 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4158 | |
| 4159 | /* Generate #defines for all tokens */ |
| 4160 | if( mhflag ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4161 | const char *prefix; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4162 | fprintf(out,"#if INTERFACE\n"); lineno++; |
| 4163 | if( lemp->tokenprefix ) prefix = lemp->tokenprefix; |
| 4164 | else prefix = ""; |
| 4165 | for(i=1; i<lemp->nterminal; i++){ |
| 4166 | fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i); |
| 4167 | lineno++; |
| 4168 | } |
| 4169 | fprintf(out,"#endif\n"); lineno++; |
| 4170 | } |
| 4171 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4172 | |
| 4173 | /* Generate the defines */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4174 | fprintf(out,"#define YYCODETYPE %s\n", |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 4175 | minimum_size_type(0, lemp->nsymbol, &szCodeType)); lineno++; |
| 4176 | fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4177 | fprintf(out,"#define YYACTIONTYPE %s\n", |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4178 | minimum_size_type(0,lemp->maxAction,&szActionType)); lineno++; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4179 | if( lemp->wildcard ){ |
| 4180 | fprintf(out,"#define YYWILDCARD %d\n", |
| 4181 | lemp->wildcard->index); lineno++; |
| 4182 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4183 | print_stack_union(out,lemp,&lineno,mhflag); |
drh | ca44b5a | 2007-02-22 23:06:58 +0000 | [diff] [blame] | 4184 | fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4185 | if( lemp->stacksize ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4186 | fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++; |
| 4187 | }else{ |
| 4188 | fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++; |
| 4189 | } |
drh | ca44b5a | 2007-02-22 23:06:58 +0000 | [diff] [blame] | 4190 | fprintf(out, "#endif\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4191 | if( mhflag ){ |
| 4192 | fprintf(out,"#if INTERFACE\n"); lineno++; |
| 4193 | } |
| 4194 | name = lemp->name ? lemp->name : "Parse"; |
| 4195 | if( lemp->arg && lemp->arg[0] ){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 4196 | i = lemonStrlen(lemp->arg); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 4197 | while( i>=1 && ISSPACE(lemp->arg[i-1]) ) i--; |
| 4198 | while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4199 | fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++; |
| 4200 | fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++; |
| 4201 | fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n", |
| 4202 | name,lemp->arg,&lemp->arg[i]); lineno++; |
| 4203 | fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n", |
| 4204 | name,&lemp->arg[i],&lemp->arg[i]); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4205 | }else{ |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4206 | fprintf(out,"#define %sARG_SDECL\n",name); lineno++; |
| 4207 | fprintf(out,"#define %sARG_PDECL\n",name); lineno++; |
| 4208 | fprintf(out,"#define %sARG_FETCH\n",name); lineno++; |
| 4209 | fprintf(out,"#define %sARG_STORE\n",name); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4210 | } |
| 4211 | if( mhflag ){ |
| 4212 | fprintf(out,"#endif\n"); lineno++; |
| 4213 | } |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 4214 | if( lemp->errsym && lemp->errsym->useCnt ){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4215 | fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++; |
| 4216 | fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++; |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4217 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4218 | if( lemp->has_fallback ){ |
| 4219 | fprintf(out,"#define YYFALLBACK 1\n"); lineno++; |
| 4220 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4221 | |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4222 | /* Compute the action table, but do not output it yet. The action |
| 4223 | ** table must be computed before generating the YYNSTATE macro because |
| 4224 | ** we need to know how many states can be eliminated. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4225 | */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4226 | ax = (struct axset *) calloc(lemp->nxstate*2, sizeof(ax[0])); |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4227 | if( ax==0 ){ |
| 4228 | fprintf(stderr,"malloc failed\n"); |
| 4229 | exit(1); |
| 4230 | } |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4231 | for(i=0; i<lemp->nxstate; i++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4232 | stp = lemp->sorted[i]; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4233 | ax[i*2].stp = stp; |
| 4234 | ax[i*2].isTkn = 1; |
| 4235 | ax[i*2].nAction = stp->nTknAct; |
| 4236 | ax[i*2+1].stp = stp; |
| 4237 | ax[i*2+1].isTkn = 0; |
| 4238 | ax[i*2+1].nAction = stp->nNtAct; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4239 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4240 | mxTknOfst = mnTknOfst = 0; |
| 4241 | mxNtOfst = mnNtOfst = 0; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4242 | /* In an effort to minimize the action table size, use the heuristic |
| 4243 | ** of placing the largest action sets first */ |
| 4244 | for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i; |
| 4245 | qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4246 | pActtab = acttab_alloc(lemp->nsymbol, lemp->nterminal); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4247 | for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4248 | stp = ax[i].stp; |
| 4249 | if( ax[i].isTkn ){ |
| 4250 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4251 | int action; |
| 4252 | if( ap->sp->index>=lemp->nterminal ) continue; |
| 4253 | action = compute_action(lemp, ap); |
| 4254 | if( action<0 ) continue; |
| 4255 | acttab_action(pActtab, ap->sp->index, action); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4256 | } |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4257 | stp->iTknOfst = acttab_insert(pActtab, 1); |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4258 | if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst; |
| 4259 | if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst; |
| 4260 | }else{ |
| 4261 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4262 | int action; |
| 4263 | if( ap->sp->index<lemp->nterminal ) continue; |
| 4264 | if( ap->sp->index==lemp->nsymbol ) continue; |
| 4265 | action = compute_action(lemp, ap); |
| 4266 | if( action<0 ) continue; |
| 4267 | acttab_action(pActtab, ap->sp->index, action); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4268 | } |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4269 | stp->iNtOfst = acttab_insert(pActtab, 0); |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4270 | if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst; |
| 4271 | if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4272 | } |
drh | 337cd0d | 2015-09-07 23:40:42 +0000 | [diff] [blame] | 4273 | #if 0 /* Uncomment for a trace of how the yy_action[] table fills out */ |
| 4274 | { int jj, nn; |
| 4275 | for(jj=nn=0; jj<pActtab->nAction; jj++){ |
| 4276 | if( pActtab->aAction[jj].action<0 ) nn++; |
| 4277 | } |
| 4278 | printf("%4d: State %3d %s n: %2d size: %5d freespace: %d\n", |
| 4279 | i, stp->statenum, ax[i].isTkn ? "Token" : "Var ", |
| 4280 | ax[i].nAction, pActtab->nAction, nn); |
| 4281 | } |
| 4282 | #endif |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4283 | } |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4284 | free(ax); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4285 | |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4286 | /* Mark rules that are actually used for reduce actions after all |
| 4287 | ** optimizations have been applied |
| 4288 | */ |
| 4289 | for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE; |
| 4290 | for(i=0; i<lemp->nxstate; i++){ |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4291 | for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){ |
| 4292 | if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){ |
drh | 69bfa55 | 2017-04-26 04:32:17 +0000 | [diff] [blame] | 4293 | ap->x.rp->doesReduce = 1; |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | } |
| 4297 | |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4298 | /* Finish rendering the constants now that the action table has |
| 4299 | ** been computed */ |
| 4300 | fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++; |
| 4301 | fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++; |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 4302 | fprintf(out,"#define YYNTOKEN %d\n",lemp->nterminal); lineno++; |
drh | 337cd0d | 2015-09-07 23:40:42 +0000 | [diff] [blame] | 4303 | fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++; |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4304 | i = lemp->minShiftReduce; |
| 4305 | fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",i); lineno++; |
| 4306 | i += lemp->nrule; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4307 | fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++; |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4308 | fprintf(out,"#define YY_ERROR_ACTION %d\n", lemp->errAction); lineno++; |
| 4309 | fprintf(out,"#define YY_ACCEPT_ACTION %d\n", lemp->accAction); lineno++; |
| 4310 | fprintf(out,"#define YY_NO_ACTION %d\n", lemp->noAction); lineno++; |
| 4311 | fprintf(out,"#define YY_MIN_REDUCE %d\n", lemp->minReduce); lineno++; |
| 4312 | i = lemp->minReduce + lemp->nrule; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4313 | fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4314 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4315 | |
| 4316 | /* Now output the action table and its associates: |
| 4317 | ** |
| 4318 | ** yy_action[] A single table containing all actions. |
| 4319 | ** yy_lookahead[] A table containing the lookahead for each entry in |
| 4320 | ** yy_action. Used to detect hash collisions. |
| 4321 | ** yy_shift_ofst[] For each state, the offset into yy_action for |
| 4322 | ** shifting terminals. |
| 4323 | ** yy_reduce_ofst[] For each state, the offset into yy_action for |
| 4324 | ** shifting non-terminals after a reduce. |
| 4325 | ** yy_default[] Default action for each state. |
| 4326 | */ |
| 4327 | |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4328 | /* Output the yy_action table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4329 | lemp->nactiontab = n = acttab_action_size(pActtab); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4330 | lemp->tablesize += n*szActionType; |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 4331 | fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++; |
| 4332 | fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4333 | for(i=j=0; i<n; i++){ |
| 4334 | int action = acttab_yyaction(pActtab, i); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4335 | if( action<0 ) action = lemp->noAction; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4336 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4337 | fprintf(out, " %4d,", action); |
| 4338 | if( j==9 || i==n-1 ){ |
| 4339 | fprintf(out, "\n"); lineno++; |
| 4340 | j = 0; |
| 4341 | }else{ |
| 4342 | j++; |
| 4343 | } |
| 4344 | } |
| 4345 | fprintf(out, "};\n"); lineno++; |
| 4346 | |
| 4347 | /* Output the yy_lookahead table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4348 | lemp->nlookaheadtab = n = acttab_lookahead_size(pActtab); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4349 | lemp->tablesize += n*szCodeType; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 4350 | fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4351 | for(i=j=0; i<n; i++){ |
| 4352 | int la = acttab_yylookahead(pActtab, i); |
| 4353 | if( la<0 ) la = lemp->nsymbol; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4354 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4355 | fprintf(out, " %4d,", la); |
| 4356 | if( j==9 || i==n-1 ){ |
| 4357 | fprintf(out, "\n"); lineno++; |
| 4358 | j = 0; |
| 4359 | }else{ |
| 4360 | j++; |
| 4361 | } |
| 4362 | } |
| 4363 | fprintf(out, "};\n"); lineno++; |
| 4364 | |
| 4365 | /* Output the yy_shift_ofst[] table */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4366 | n = lemp->nxstate; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4367 | while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--; |
drh | 701b688 | 2016-08-10 13:30:43 +0000 | [diff] [blame] | 4368 | fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++; |
| 4369 | fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++; |
| 4370 | fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4371 | fprintf(out, "static const %s yy_shift_ofst[] = {\n", |
drh | 701b688 | 2016-08-10 13:30:43 +0000 | [diff] [blame] | 4372 | minimum_size_type(mnTknOfst, lemp->nterminal+lemp->nactiontab, &sz)); |
| 4373 | lineno++; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4374 | lemp->tablesize += n*sz; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4375 | for(i=j=0; i<n; i++){ |
| 4376 | int ofst; |
| 4377 | stp = lemp->sorted[i]; |
| 4378 | ofst = stp->iTknOfst; |
drh | 701b688 | 2016-08-10 13:30:43 +0000 | [diff] [blame] | 4379 | if( ofst==NO_OFFSET ) ofst = lemp->nactiontab; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4380 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4381 | fprintf(out, " %4d,", ofst); |
| 4382 | if( j==9 || i==n-1 ){ |
| 4383 | fprintf(out, "\n"); lineno++; |
| 4384 | j = 0; |
| 4385 | }else{ |
| 4386 | j++; |
| 4387 | } |
| 4388 | } |
| 4389 | fprintf(out, "};\n"); lineno++; |
| 4390 | |
| 4391 | /* Output the yy_reduce_ofst[] table */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4392 | n = lemp->nxstate; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4393 | while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--; |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 4394 | fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++; |
| 4395 | fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++; |
| 4396 | fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4397 | fprintf(out, "static const %s yy_reduce_ofst[] = {\n", |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4398 | minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++; |
| 4399 | lemp->tablesize += n*sz; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4400 | for(i=j=0; i<n; i++){ |
| 4401 | int ofst; |
| 4402 | stp = lemp->sorted[i]; |
| 4403 | ofst = stp->iNtOfst; |
| 4404 | if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4405 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4406 | fprintf(out, " %4d,", ofst); |
| 4407 | if( j==9 || i==n-1 ){ |
| 4408 | fprintf(out, "\n"); lineno++; |
| 4409 | j = 0; |
| 4410 | }else{ |
| 4411 | j++; |
| 4412 | } |
| 4413 | } |
| 4414 | fprintf(out, "};\n"); lineno++; |
| 4415 | |
| 4416 | /* Output the default action table */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 4417 | fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4418 | n = lemp->nxstate; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4419 | lemp->tablesize += n*szActionType; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4420 | for(i=j=0; i<n; i++){ |
| 4421 | stp = lemp->sorted[i]; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4422 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4423 | if( stp->iDfltReduce<0 ){ |
| 4424 | fprintf(out, " %4d,", lemp->errAction); |
| 4425 | }else{ |
| 4426 | fprintf(out, " %4d,", stp->iDfltReduce + lemp->minReduce); |
| 4427 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4428 | if( j==9 || i==n-1 ){ |
| 4429 | fprintf(out, "\n"); lineno++; |
| 4430 | j = 0; |
| 4431 | }else{ |
| 4432 | j++; |
| 4433 | } |
| 4434 | } |
| 4435 | fprintf(out, "};\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4436 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4437 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4438 | /* Generate the table of fallback tokens. |
| 4439 | */ |
| 4440 | if( lemp->has_fallback ){ |
drh | 1441f3e | 2009-06-12 12:50:50 +0000 | [diff] [blame] | 4441 | int mx = lemp->nterminal - 1; |
| 4442 | while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; } |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4443 | lemp->tablesize += (mx+1)*szCodeType; |
drh | 1441f3e | 2009-06-12 12:50:50 +0000 | [diff] [blame] | 4444 | for(i=0; i<=mx; i++){ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4445 | struct symbol *p = lemp->symbols[i]; |
| 4446 | if( p->fallback==0 ){ |
| 4447 | fprintf(out, " 0, /* %10s => nothing */\n", p->name); |
| 4448 | }else{ |
| 4449 | fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index, |
| 4450 | p->name, p->fallback->name); |
| 4451 | } |
| 4452 | lineno++; |
| 4453 | } |
| 4454 | } |
| 4455 | tplt_xfer(lemp->name, in, out, &lineno); |
| 4456 | |
| 4457 | /* Generate a table containing the symbolic name of every symbol |
| 4458 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4459 | for(i=0; i<lemp->nsymbol; i++){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 4460 | lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4461 | fprintf(out," /* %4d */ \"%s\",\n",i, lemp->symbols[i]->name); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4462 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4463 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4464 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4465 | /* Generate a table containing a text string that describes every |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 4466 | ** rule in the rule set of the grammar. This information is used |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4467 | ** when tracing REDUCE actions. |
| 4468 | */ |
| 4469 | for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4470 | assert( rp->iRule==i ); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4471 | fprintf(out," /* %3d */ \"", i); |
| 4472 | writeRuleText(out, rp); |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4473 | fprintf(out,"\",\n"); lineno++; |
| 4474 | } |
| 4475 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4476 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4477 | /* Generate code which executes every time a symbol is popped from |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4478 | ** the stack while processing errors or while destroying the parser. |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4479 | ** (In other words, generate the %destructor actions) |
| 4480 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4481 | if( lemp->tokendest ){ |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4482 | int once = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4483 | for(i=0; i<lemp->nsymbol; i++){ |
| 4484 | struct symbol *sp = lemp->symbols[i]; |
| 4485 | if( sp==0 || sp->type!=TERMINAL ) continue; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4486 | if( once ){ |
| 4487 | fprintf(out, " /* TERMINAL Destructor */\n"); lineno++; |
| 4488 | once = 0; |
| 4489 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4490 | fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4491 | } |
| 4492 | for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++); |
| 4493 | if( i<lemp->nsymbol ){ |
| 4494 | emit_destructor_code(out,lemp->symbols[i],lemp,&lineno); |
| 4495 | fprintf(out," break;\n"); lineno++; |
| 4496 | } |
| 4497 | } |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4498 | if( lemp->vardest ){ |
| 4499 | struct symbol *dflt_sp = 0; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4500 | int once = 1; |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4501 | for(i=0; i<lemp->nsymbol; i++){ |
| 4502 | struct symbol *sp = lemp->symbols[i]; |
| 4503 | if( sp==0 || sp->type==TERMINAL || |
| 4504 | sp->index<=0 || sp->destructor!=0 ) continue; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4505 | if( once ){ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4506 | fprintf(out, " /* Default NON-TERMINAL Destructor */\n");lineno++; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4507 | once = 0; |
| 4508 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4509 | fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4510 | dflt_sp = sp; |
| 4511 | } |
| 4512 | if( dflt_sp!=0 ){ |
| 4513 | emit_destructor_code(out,dflt_sp,lemp,&lineno); |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4514 | } |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4515 | fprintf(out," break;\n"); lineno++; |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4516 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4517 | for(i=0; i<lemp->nsymbol; i++){ |
| 4518 | struct symbol *sp = lemp->symbols[i]; |
| 4519 | if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue; |
drh | 0f832dd | 2016-08-16 16:46:40 +0000 | [diff] [blame] | 4520 | if( sp->destLineno<0 ) continue; /* Already emitted */ |
drh | 7501301 | 2009-06-12 15:47:34 +0000 | [diff] [blame] | 4521 | fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4522 | |
| 4523 | /* Combine duplicate destructors into a single case */ |
| 4524 | for(j=i+1; j<lemp->nsymbol; j++){ |
| 4525 | struct symbol *sp2 = lemp->symbols[j]; |
| 4526 | if( sp2 && sp2->type!=TERMINAL && sp2->destructor |
| 4527 | && sp2->dtnum==sp->dtnum |
| 4528 | && strcmp(sp->destructor,sp2->destructor)==0 ){ |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4529 | fprintf(out," case %d: /* %s */\n", |
| 4530 | sp2->index, sp2->name); lineno++; |
drh | 0f832dd | 2016-08-16 16:46:40 +0000 | [diff] [blame] | 4531 | sp2->destLineno = -1; /* Avoid emitting this destructor again */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4532 | } |
| 4533 | } |
| 4534 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4535 | emit_destructor_code(out,lemp->symbols[i],lemp,&lineno); |
| 4536 | fprintf(out," break;\n"); lineno++; |
| 4537 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4538 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4539 | |
| 4540 | /* Generate code which executes whenever the parser stack overflows */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4541 | tplt_print(out,lemp,lemp->overflow,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4542 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4543 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4544 | /* Generate the table of rule information |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4545 | ** |
| 4546 | ** Note: This code depends on the fact that rules are number |
| 4547 | ** sequentually beginning with 0. |
| 4548 | */ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4549 | for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ |
| 4550 | fprintf(out," { %4d, %4d }, /* (%d) ",rp->lhs->index,-rp->nrhs,i); |
| 4551 | rule_print(out, rp); |
| 4552 | fprintf(out," */\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4553 | } |
| 4554 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4555 | |
| 4556 | /* Generate code which execution during each REDUCE action */ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 4557 | i = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4558 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 4559 | i += translate_code(lemp, rp); |
| 4560 | } |
| 4561 | if( i ){ |
| 4562 | fprintf(out," YYMINORTYPE yylhsminor;\n"); lineno++; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4563 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4564 | /* First output rules other than the default: rule */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4565 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4566 | struct rule *rp2; /* Other rules with the same action */ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4567 | if( rp->codeEmitted ) continue; |
| 4568 | if( rp->noCode ){ |
| 4569 | /* No C code actions, so this will be part of the "default:" rule */ |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 4570 | continue; |
| 4571 | } |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4572 | fprintf(out," case %d: /* ", rp->iRule); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4573 | writeRuleText(out, rp); |
| 4574 | fprintf(out, " */\n"); lineno++; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4575 | for(rp2=rp->next; rp2; rp2=rp2->next){ |
drh | afb8cd9 | 2016-04-29 11:28:35 +0000 | [diff] [blame] | 4576 | if( rp2->code==rp->code && rp2->codePrefix==rp->codePrefix |
| 4577 | && rp2->codeSuffix==rp->codeSuffix ){ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4578 | fprintf(out," case %d: /* ", rp2->iRule); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4579 | writeRuleText(out, rp2); |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4580 | fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->iRule); lineno++; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4581 | rp2->codeEmitted = 1; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4582 | } |
| 4583 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4584 | emit_code(out,rp,lemp,&lineno); |
| 4585 | fprintf(out," break;\n"); lineno++; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4586 | rp->codeEmitted = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4587 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4588 | /* Finally, output the default: rule. We choose as the default: all |
| 4589 | ** empty actions. */ |
| 4590 | fprintf(out," default:\n"); lineno++; |
| 4591 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4592 | if( rp->codeEmitted ) continue; |
| 4593 | assert( rp->noCode ); |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4594 | fprintf(out," /* (%d) ", rp->iRule); |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4595 | writeRuleText(out, rp); |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4596 | if( rp->doesReduce ){ |
| 4597 | fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->iRule); lineno++; |
| 4598 | }else{ |
| 4599 | fprintf(out, " (OPTIMIZED OUT) */ assert(yyruleno!=%d);\n", |
| 4600 | rp->iRule); lineno++; |
| 4601 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4602 | } |
| 4603 | fprintf(out," break;\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4604 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4605 | |
| 4606 | /* Generate code which executes if a parse fails */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4607 | tplt_print(out,lemp,lemp->failure,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4608 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4609 | |
| 4610 | /* Generate code which executes when a syntax error occurs */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4611 | tplt_print(out,lemp,lemp->error,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4612 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4613 | |
| 4614 | /* Generate code which executes when the parser accepts its input */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4615 | tplt_print(out,lemp,lemp->accept,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4616 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4617 | |
| 4618 | /* Append any addition code the user desires */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4619 | tplt_print(out,lemp,lemp->extracode,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4620 | |
| 4621 | fclose(in); |
| 4622 | fclose(out); |
| 4623 | return; |
| 4624 | } |
| 4625 | |
| 4626 | /* Generate a header file for the parser */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4627 | void ReportHeader(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4628 | { |
| 4629 | FILE *out, *in; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4630 | const char *prefix; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4631 | char line[LINESIZE]; |
| 4632 | char pattern[LINESIZE]; |
| 4633 | int i; |
| 4634 | |
| 4635 | if( lemp->tokenprefix ) prefix = lemp->tokenprefix; |
| 4636 | else prefix = ""; |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 4637 | in = file_open(lemp,".h","rb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4638 | if( in ){ |
drh | 8ba0d1c | 2012-06-16 15:26:31 +0000 | [diff] [blame] | 4639 | int nextChar; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4640 | for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4641 | lemon_sprintf(pattern,"#define %s%-30s %3d\n", |
| 4642 | prefix,lemp->symbols[i]->name,i); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4643 | if( strcmp(line,pattern) ) break; |
| 4644 | } |
drh | 8ba0d1c | 2012-06-16 15:26:31 +0000 | [diff] [blame] | 4645 | nextChar = fgetc(in); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4646 | fclose(in); |
drh | 8ba0d1c | 2012-06-16 15:26:31 +0000 | [diff] [blame] | 4647 | if( i==lemp->nterminal && nextChar==EOF ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4648 | /* No change in the file. Don't rewrite it. */ |
| 4649 | return; |
| 4650 | } |
| 4651 | } |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 4652 | out = file_open(lemp,".h","wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4653 | if( out ){ |
| 4654 | for(i=1; i<lemp->nterminal; i++){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4655 | fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4656 | } |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4657 | fclose(out); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4658 | } |
| 4659 | return; |
| 4660 | } |
| 4661 | |
| 4662 | /* Reduce the size of the action tables, if possible, by making use |
| 4663 | ** of defaults. |
| 4664 | ** |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4665 | ** In this version, we take the most frequent REDUCE action and make |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4666 | ** it the default. Except, there is no default if the wildcard token |
| 4667 | ** is a possible look-ahead. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4668 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4669 | void CompressTables(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4670 | { |
| 4671 | struct state *stp; |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 4672 | struct action *ap, *ap2, *nextap; |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4673 | struct rule *rp, *rp2, *rbest; |
drh | 0c6dfaa | 2015-09-08 21:16:46 +0000 | [diff] [blame] | 4674 | int nbest, n; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4675 | int i; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4676 | int usesWildcard; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4677 | |
| 4678 | for(i=0; i<lemp->nstate; i++){ |
| 4679 | stp = lemp->sorted[i]; |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4680 | nbest = 0; |
| 4681 | rbest = 0; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4682 | usesWildcard = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4683 | |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4684 | for(ap=stp->ap; ap; ap=ap->next){ |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4685 | if( ap->type==SHIFT && ap->sp==lemp->wildcard ){ |
| 4686 | usesWildcard = 1; |
| 4687 | } |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4688 | if( ap->type!=REDUCE ) continue; |
| 4689 | rp = ap->x.rp; |
drh | b496099 | 2007-10-05 16:16:36 +0000 | [diff] [blame] | 4690 | if( rp->lhsStart ) continue; |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4691 | if( rp==rbest ) continue; |
| 4692 | n = 1; |
| 4693 | for(ap2=ap->next; ap2; ap2=ap2->next){ |
| 4694 | if( ap2->type!=REDUCE ) continue; |
| 4695 | rp2 = ap2->x.rp; |
| 4696 | if( rp2==rbest ) continue; |
| 4697 | if( rp2==rp ) n++; |
| 4698 | } |
| 4699 | if( n>nbest ){ |
| 4700 | nbest = n; |
| 4701 | rbest = rp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4702 | } |
| 4703 | } |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4704 | |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4705 | /* Do not make a default if the number of rules to default |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4706 | ** is not at least 1 or if the wildcard token is a possible |
| 4707 | ** lookahead. |
| 4708 | */ |
| 4709 | if( nbest<1 || usesWildcard ) continue; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4710 | |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4711 | |
| 4712 | /* Combine matching REDUCE actions into a single default */ |
| 4713 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4714 | if( ap->type==REDUCE && ap->x.rp==rbest ) break; |
| 4715 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4716 | assert( ap ); |
| 4717 | ap->sp = Symbol_new("{default}"); |
| 4718 | for(ap=ap->next; ap; ap=ap->next){ |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4719 | if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4720 | } |
| 4721 | stp->ap = Action_sort(stp->ap); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4722 | |
| 4723 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4724 | if( ap->type==SHIFT ) break; |
| 4725 | if( ap->type==REDUCE && ap->x.rp!=rbest ) break; |
| 4726 | } |
| 4727 | if( ap==0 ){ |
| 4728 | stp->autoReduce = 1; |
| 4729 | stp->pDfltReduce = rbest; |
| 4730 | } |
| 4731 | } |
| 4732 | |
| 4733 | /* Make a second pass over all states and actions. Convert |
| 4734 | ** every action that is a SHIFT to an autoReduce state into |
| 4735 | ** a SHIFTREDUCE action. |
| 4736 | */ |
| 4737 | for(i=0; i<lemp->nstate; i++){ |
| 4738 | stp = lemp->sorted[i]; |
| 4739 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4740 | struct state *pNextState; |
| 4741 | if( ap->type!=SHIFT ) continue; |
| 4742 | pNextState = ap->x.stp; |
| 4743 | if( pNextState->autoReduce && pNextState->pDfltReduce!=0 ){ |
| 4744 | ap->type = SHIFTREDUCE; |
| 4745 | ap->x.rp = pNextState->pDfltReduce; |
| 4746 | } |
| 4747 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4748 | } |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 4749 | |
| 4750 | /* If a SHIFTREDUCE action specifies a rule that has a single RHS term |
| 4751 | ** (meaning that the SHIFTREDUCE will land back in the state where it |
| 4752 | ** started) and if there is no C-code associated with the reduce action, |
| 4753 | ** then we can go ahead and convert the action to be the same as the |
| 4754 | ** action for the RHS of the rule. |
| 4755 | */ |
| 4756 | for(i=0; i<lemp->nstate; i++){ |
| 4757 | stp = lemp->sorted[i]; |
| 4758 | for(ap=stp->ap; ap; ap=nextap){ |
| 4759 | nextap = ap->next; |
| 4760 | if( ap->type!=SHIFTREDUCE ) continue; |
| 4761 | rp = ap->x.rp; |
| 4762 | if( rp->noCode==0 ) continue; |
| 4763 | if( rp->nrhs!=1 ) continue; |
| 4764 | #if 1 |
| 4765 | /* Only apply this optimization to non-terminals. It would be OK to |
| 4766 | ** apply it to terminal symbols too, but that makes the parser tables |
| 4767 | ** larger. */ |
| 4768 | if( ap->sp->index<lemp->nterminal ) continue; |
| 4769 | #endif |
| 4770 | /* If we reach this point, it means the optimization can be applied */ |
| 4771 | nextap = ap; |
| 4772 | for(ap2=stp->ap; ap2 && (ap2==ap || ap2->sp!=rp->lhs); ap2=ap2->next){} |
| 4773 | assert( ap2!=0 ); |
| 4774 | ap->spOpt = ap2->sp; |
| 4775 | ap->type = ap2->type; |
| 4776 | ap->x = ap2->x; |
| 4777 | } |
| 4778 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4779 | } |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4780 | |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4781 | |
| 4782 | /* |
| 4783 | ** Compare two states for sorting purposes. The smaller state is the |
| 4784 | ** one with the most non-terminal actions. If they have the same number |
| 4785 | ** of non-terminal actions, then the smaller is the one with the most |
| 4786 | ** token actions. |
| 4787 | */ |
| 4788 | static int stateResortCompare(const void *a, const void *b){ |
| 4789 | const struct state *pA = *(const struct state**)a; |
| 4790 | const struct state *pB = *(const struct state**)b; |
| 4791 | int n; |
| 4792 | |
| 4793 | n = pB->nNtAct - pA->nNtAct; |
| 4794 | if( n==0 ){ |
| 4795 | n = pB->nTknAct - pA->nTknAct; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4796 | if( n==0 ){ |
| 4797 | n = pB->statenum - pA->statenum; |
| 4798 | } |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4799 | } |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4800 | assert( n!=0 ); |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4801 | return n; |
| 4802 | } |
| 4803 | |
| 4804 | |
| 4805 | /* |
| 4806 | ** Renumber and resort states so that states with fewer choices |
| 4807 | ** occur at the end. Except, keep state 0 as the first state. |
| 4808 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4809 | void ResortStates(struct lemon *lemp) |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4810 | { |
| 4811 | int i; |
| 4812 | struct state *stp; |
| 4813 | struct action *ap; |
| 4814 | |
| 4815 | for(i=0; i<lemp->nstate; i++){ |
| 4816 | stp = lemp->sorted[i]; |
| 4817 | stp->nTknAct = stp->nNtAct = 0; |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4818 | stp->iDfltReduce = -1; /* Init dflt action to "syntax error" */ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4819 | stp->iTknOfst = NO_OFFSET; |
| 4820 | stp->iNtOfst = NO_OFFSET; |
| 4821 | for(ap=stp->ap; ap; ap=ap->next){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4822 | int iAction = compute_action(lemp,ap); |
| 4823 | if( iAction>=0 ){ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4824 | if( ap->sp->index<lemp->nterminal ){ |
| 4825 | stp->nTknAct++; |
| 4826 | }else if( ap->sp->index<lemp->nsymbol ){ |
| 4827 | stp->nNtAct++; |
| 4828 | }else{ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4829 | assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp ); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4830 | stp->iDfltReduce = iAction; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4831 | } |
| 4832 | } |
| 4833 | } |
| 4834 | } |
| 4835 | qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]), |
| 4836 | stateResortCompare); |
| 4837 | for(i=0; i<lemp->nstate; i++){ |
| 4838 | lemp->sorted[i]->statenum = i; |
| 4839 | } |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4840 | lemp->nxstate = lemp->nstate; |
| 4841 | while( lemp->nxstate>1 && lemp->sorted[lemp->nxstate-1]->autoReduce ){ |
| 4842 | lemp->nxstate--; |
| 4843 | } |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4844 | } |
| 4845 | |
| 4846 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4847 | /***************** From the file "set.c" ************************************/ |
| 4848 | /* |
| 4849 | ** Set manipulation routines for the LEMON parser generator. |
| 4850 | */ |
| 4851 | |
| 4852 | static int size = 0; |
| 4853 | |
| 4854 | /* Set the set size */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4855 | void SetSize(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4856 | { |
| 4857 | size = n+1; |
| 4858 | } |
| 4859 | |
| 4860 | /* Allocate a new set */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 4861 | char *SetNew(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4862 | char *s; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 4863 | s = (char*)calloc( size, 1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4864 | if( s==0 ){ |
| 4865 | extern void memory_error(); |
| 4866 | memory_error(); |
| 4867 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4868 | return s; |
| 4869 | } |
| 4870 | |
| 4871 | /* Deallocate a set */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4872 | void SetFree(char *s) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4873 | { |
| 4874 | free(s); |
| 4875 | } |
| 4876 | |
| 4877 | /* Add a new element to the set. Return TRUE if the element was added |
| 4878 | ** and FALSE if it was already there. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4879 | int SetAdd(char *s, int e) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4880 | { |
| 4881 | int rv; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 4882 | assert( e>=0 && e<size ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4883 | rv = s[e]; |
| 4884 | s[e] = 1; |
| 4885 | return !rv; |
| 4886 | } |
| 4887 | |
| 4888 | /* Add every element of s2 to s1. Return TRUE if s1 changes. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4889 | int SetUnion(char *s1, char *s2) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4890 | { |
| 4891 | int i, progress; |
| 4892 | progress = 0; |
| 4893 | for(i=0; i<size; i++){ |
| 4894 | if( s2[i]==0 ) continue; |
| 4895 | if( s1[i]==0 ){ |
| 4896 | progress = 1; |
| 4897 | s1[i] = 1; |
| 4898 | } |
| 4899 | } |
| 4900 | return progress; |
| 4901 | } |
| 4902 | /********************** From the file "table.c" ****************************/ |
| 4903 | /* |
| 4904 | ** All code in this file has been automatically generated |
| 4905 | ** from a specification in the file |
| 4906 | ** "table.q" |
| 4907 | ** by the associative array code building program "aagen". |
| 4908 | ** Do not edit this file! Instead, edit the specification |
| 4909 | ** file, then rerun aagen. |
| 4910 | */ |
| 4911 | /* |
| 4912 | ** Code for processing tables in the LEMON parser generator. |
| 4913 | */ |
| 4914 | |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 4915 | PRIVATE unsigned strhash(const char *x) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4916 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 4917 | unsigned h = 0; |
| 4918 | while( *x ) h = h*13 + *(x++); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4919 | return h; |
| 4920 | } |
| 4921 | |
| 4922 | /* Works like strdup, sort of. Save a string in malloced memory, but |
| 4923 | ** keep strings in a table so that the same string is not in more |
| 4924 | ** than one place. |
| 4925 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4926 | const char *Strsafe(const char *y) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4927 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4928 | const char *z; |
| 4929 | char *cpy; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4930 | |
drh | 916f75f | 2006-07-17 00:19:39 +0000 | [diff] [blame] | 4931 | if( y==0 ) return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4932 | z = Strsafe_find(y); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4933 | if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 4934 | lemon_strcpy(cpy,y); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4935 | z = cpy; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4936 | Strsafe_insert(z); |
| 4937 | } |
| 4938 | MemoryCheck(z); |
| 4939 | return z; |
| 4940 | } |
| 4941 | |
| 4942 | /* There is one instance of the following structure for each |
| 4943 | ** associative array of type "x1". |
| 4944 | */ |
| 4945 | struct s_x1 { |
| 4946 | int size; /* The number of available slots. */ |
| 4947 | /* Must be a power of 2 greater than or */ |
| 4948 | /* equal to 1 */ |
| 4949 | int count; /* Number of currently slots filled */ |
| 4950 | struct s_x1node *tbl; /* The data stored here */ |
| 4951 | struct s_x1node **ht; /* Hash table for lookups */ |
| 4952 | }; |
| 4953 | |
| 4954 | /* There is one instance of this structure for every data element |
| 4955 | ** in an associative array of type "x1". |
| 4956 | */ |
| 4957 | typedef struct s_x1node { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4958 | const char *data; /* The data */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4959 | struct s_x1node *next; /* Next entry with the same hash */ |
| 4960 | struct s_x1node **from; /* Previous link */ |
| 4961 | } x1node; |
| 4962 | |
| 4963 | /* There is only one instance of the array, which is the following */ |
| 4964 | static struct s_x1 *x1a; |
| 4965 | |
| 4966 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 4967 | void Strsafe_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4968 | if( x1a ) return; |
| 4969 | x1a = (struct s_x1*)malloc( sizeof(struct s_x1) ); |
| 4970 | if( x1a ){ |
| 4971 | x1a->size = 1024; |
| 4972 | x1a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 4973 | x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4974 | if( x1a->tbl==0 ){ |
| 4975 | free(x1a); |
| 4976 | x1a = 0; |
| 4977 | }else{ |
| 4978 | int i; |
| 4979 | x1a->ht = (x1node**)&(x1a->tbl[1024]); |
| 4980 | for(i=0; i<1024; i++) x1a->ht[i] = 0; |
| 4981 | } |
| 4982 | } |
| 4983 | } |
| 4984 | /* Insert a new record into the array. Return TRUE if successful. |
| 4985 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4986 | int Strsafe_insert(const char *data) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4987 | { |
| 4988 | x1node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 4989 | unsigned h; |
| 4990 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4991 | |
| 4992 | if( x1a==0 ) return 0; |
| 4993 | ph = strhash(data); |
| 4994 | h = ph & (x1a->size-1); |
| 4995 | np = x1a->ht[h]; |
| 4996 | while( np ){ |
| 4997 | if( strcmp(np->data,data)==0 ){ |
| 4998 | /* An existing entry with the same key is found. */ |
| 4999 | /* Fail because overwrite is not allows. */ |
| 5000 | return 0; |
| 5001 | } |
| 5002 | np = np->next; |
| 5003 | } |
| 5004 | if( x1a->count>=x1a->size ){ |
| 5005 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5006 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5007 | struct s_x1 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5008 | array.size = arrSize = x1a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5009 | array.count = x1a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5010 | array.tbl = (x1node*)calloc(arrSize, sizeof(x1node) + sizeof(x1node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5011 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5012 | array.ht = (x1node**)&(array.tbl[arrSize]); |
| 5013 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5014 | for(i=0; i<x1a->count; i++){ |
| 5015 | x1node *oldnp, *newnp; |
| 5016 | oldnp = &(x1a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5017 | h = strhash(oldnp->data) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5018 | newnp = &(array.tbl[i]); |
| 5019 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5020 | newnp->next = array.ht[h]; |
| 5021 | newnp->data = oldnp->data; |
| 5022 | newnp->from = &(array.ht[h]); |
| 5023 | array.ht[h] = newnp; |
| 5024 | } |
| 5025 | free(x1a->tbl); |
| 5026 | *x1a = array; |
| 5027 | } |
| 5028 | /* Insert the new data */ |
| 5029 | h = ph & (x1a->size-1); |
| 5030 | np = &(x1a->tbl[x1a->count++]); |
| 5031 | np->data = data; |
| 5032 | if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next); |
| 5033 | np->next = x1a->ht[h]; |
| 5034 | x1a->ht[h] = np; |
| 5035 | np->from = &(x1a->ht[h]); |
| 5036 | return 1; |
| 5037 | } |
| 5038 | |
| 5039 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5040 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5041 | const char *Strsafe_find(const char *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5042 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5043 | unsigned h; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5044 | x1node *np; |
| 5045 | |
| 5046 | if( x1a==0 ) return 0; |
| 5047 | h = strhash(key) & (x1a->size-1); |
| 5048 | np = x1a->ht[h]; |
| 5049 | while( np ){ |
| 5050 | if( strcmp(np->data,key)==0 ) break; |
| 5051 | np = np->next; |
| 5052 | } |
| 5053 | return np ? np->data : 0; |
| 5054 | } |
| 5055 | |
| 5056 | /* Return a pointer to the (terminal or nonterminal) symbol "x". |
| 5057 | ** Create a new symbol if this is the first time "x" has been seen. |
| 5058 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5059 | struct symbol *Symbol_new(const char *x) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5060 | { |
| 5061 | struct symbol *sp; |
| 5062 | |
| 5063 | sp = Symbol_find(x); |
| 5064 | if( sp==0 ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 5065 | sp = (struct symbol *)calloc(1, sizeof(struct symbol) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5066 | MemoryCheck(sp); |
| 5067 | sp->name = Strsafe(x); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 5068 | sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5069 | sp->rule = 0; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 5070 | sp->fallback = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5071 | sp->prec = -1; |
| 5072 | sp->assoc = UNK; |
| 5073 | sp->firstset = 0; |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 5074 | sp->lambda = LEMON_FALSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5075 | sp->destructor = 0; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 5076 | sp->destLineno = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5077 | sp->datatype = 0; |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 5078 | sp->useCnt = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5079 | Symbol_insert(sp,sp->name); |
| 5080 | } |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 5081 | sp->useCnt++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5082 | return sp; |
| 5083 | } |
| 5084 | |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 5085 | /* Compare two symbols for sorting purposes. Return negative, |
| 5086 | ** zero, or positive if a is less then, equal to, or greater |
| 5087 | ** than b. |
drh | 60d3165 | 2004-02-22 00:08:04 +0000 | [diff] [blame] | 5088 | ** |
| 5089 | ** Symbols that begin with upper case letters (terminals or tokens) |
| 5090 | ** must sort before symbols that begin with lower case letters |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 5091 | ** (non-terminals). And MULTITERMINAL symbols (created using the |
| 5092 | ** %token_class directive) must sort at the very end. Other than |
| 5093 | ** that, the order does not matter. |
drh | 60d3165 | 2004-02-22 00:08:04 +0000 | [diff] [blame] | 5094 | ** |
| 5095 | ** We find experimentally that leaving the symbols in their original |
| 5096 | ** order (the order they appeared in the grammar file) gives the |
| 5097 | ** smallest parser tables in SQLite. |
| 5098 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5099 | int Symbolcmpp(const void *_a, const void *_b) |
| 5100 | { |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 5101 | const struct symbol *a = *(const struct symbol **) _a; |
| 5102 | const struct symbol *b = *(const struct symbol **) _b; |
| 5103 | int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1; |
| 5104 | int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1; |
| 5105 | return i1==i2 ? a->index - b->index : i1 - i2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | /* There is one instance of the following structure for each |
| 5109 | ** associative array of type "x2". |
| 5110 | */ |
| 5111 | struct s_x2 { |
| 5112 | int size; /* The number of available slots. */ |
| 5113 | /* Must be a power of 2 greater than or */ |
| 5114 | /* equal to 1 */ |
| 5115 | int count; /* Number of currently slots filled */ |
| 5116 | struct s_x2node *tbl; /* The data stored here */ |
| 5117 | struct s_x2node **ht; /* Hash table for lookups */ |
| 5118 | }; |
| 5119 | |
| 5120 | /* There is one instance of this structure for every data element |
| 5121 | ** in an associative array of type "x2". |
| 5122 | */ |
| 5123 | typedef struct s_x2node { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5124 | struct symbol *data; /* The data */ |
| 5125 | const char *key; /* The key */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5126 | struct s_x2node *next; /* Next entry with the same hash */ |
| 5127 | struct s_x2node **from; /* Previous link */ |
| 5128 | } x2node; |
| 5129 | |
| 5130 | /* There is only one instance of the array, which is the following */ |
| 5131 | static struct s_x2 *x2a; |
| 5132 | |
| 5133 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5134 | void Symbol_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5135 | if( x2a ) return; |
| 5136 | x2a = (struct s_x2*)malloc( sizeof(struct s_x2) ); |
| 5137 | if( x2a ){ |
| 5138 | x2a->size = 128; |
| 5139 | x2a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5140 | x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5141 | if( x2a->tbl==0 ){ |
| 5142 | free(x2a); |
| 5143 | x2a = 0; |
| 5144 | }else{ |
| 5145 | int i; |
| 5146 | x2a->ht = (x2node**)&(x2a->tbl[128]); |
| 5147 | for(i=0; i<128; i++) x2a->ht[i] = 0; |
| 5148 | } |
| 5149 | } |
| 5150 | } |
| 5151 | /* Insert a new record into the array. Return TRUE if successful. |
| 5152 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5153 | int Symbol_insert(struct symbol *data, const char *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5154 | { |
| 5155 | x2node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5156 | unsigned h; |
| 5157 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5158 | |
| 5159 | if( x2a==0 ) return 0; |
| 5160 | ph = strhash(key); |
| 5161 | h = ph & (x2a->size-1); |
| 5162 | np = x2a->ht[h]; |
| 5163 | while( np ){ |
| 5164 | if( strcmp(np->key,key)==0 ){ |
| 5165 | /* An existing entry with the same key is found. */ |
| 5166 | /* Fail because overwrite is not allows. */ |
| 5167 | return 0; |
| 5168 | } |
| 5169 | np = np->next; |
| 5170 | } |
| 5171 | if( x2a->count>=x2a->size ){ |
| 5172 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5173 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5174 | struct s_x2 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5175 | array.size = arrSize = x2a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5176 | array.count = x2a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5177 | array.tbl = (x2node*)calloc(arrSize, sizeof(x2node) + sizeof(x2node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5178 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5179 | array.ht = (x2node**)&(array.tbl[arrSize]); |
| 5180 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5181 | for(i=0; i<x2a->count; i++){ |
| 5182 | x2node *oldnp, *newnp; |
| 5183 | oldnp = &(x2a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5184 | h = strhash(oldnp->key) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5185 | newnp = &(array.tbl[i]); |
| 5186 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5187 | newnp->next = array.ht[h]; |
| 5188 | newnp->key = oldnp->key; |
| 5189 | newnp->data = oldnp->data; |
| 5190 | newnp->from = &(array.ht[h]); |
| 5191 | array.ht[h] = newnp; |
| 5192 | } |
| 5193 | free(x2a->tbl); |
| 5194 | *x2a = array; |
| 5195 | } |
| 5196 | /* Insert the new data */ |
| 5197 | h = ph & (x2a->size-1); |
| 5198 | np = &(x2a->tbl[x2a->count++]); |
| 5199 | np->key = key; |
| 5200 | np->data = data; |
| 5201 | if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next); |
| 5202 | np->next = x2a->ht[h]; |
| 5203 | x2a->ht[h] = np; |
| 5204 | np->from = &(x2a->ht[h]); |
| 5205 | return 1; |
| 5206 | } |
| 5207 | |
| 5208 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5209 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5210 | struct symbol *Symbol_find(const char *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5211 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5212 | unsigned h; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5213 | x2node *np; |
| 5214 | |
| 5215 | if( x2a==0 ) return 0; |
| 5216 | h = strhash(key) & (x2a->size-1); |
| 5217 | np = x2a->ht[h]; |
| 5218 | while( np ){ |
| 5219 | if( strcmp(np->key,key)==0 ) break; |
| 5220 | np = np->next; |
| 5221 | } |
| 5222 | return np ? np->data : 0; |
| 5223 | } |
| 5224 | |
| 5225 | /* Return the n-th data. Return NULL if n is out of range. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5226 | struct symbol *Symbol_Nth(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5227 | { |
| 5228 | struct symbol *data; |
| 5229 | if( x2a && n>0 && n<=x2a->count ){ |
| 5230 | data = x2a->tbl[n-1].data; |
| 5231 | }else{ |
| 5232 | data = 0; |
| 5233 | } |
| 5234 | return data; |
| 5235 | } |
| 5236 | |
| 5237 | /* Return the size of the array */ |
| 5238 | int Symbol_count() |
| 5239 | { |
| 5240 | return x2a ? x2a->count : 0; |
| 5241 | } |
| 5242 | |
| 5243 | /* Return an array of pointers to all data in the table. |
| 5244 | ** The array is obtained from malloc. Return NULL if memory allocation |
| 5245 | ** problems, or if the array is empty. */ |
| 5246 | struct symbol **Symbol_arrayof() |
| 5247 | { |
| 5248 | struct symbol **array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5249 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5250 | if( x2a==0 ) return 0; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5251 | arrSize = x2a->count; |
| 5252 | array = (struct symbol **)calloc(arrSize, sizeof(struct symbol *)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5253 | if( array ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5254 | for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5255 | } |
| 5256 | return array; |
| 5257 | } |
| 5258 | |
| 5259 | /* Compare two configurations */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5260 | int Configcmp(const char *_a,const char *_b) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5261 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5262 | const struct config *a = (struct config *) _a; |
| 5263 | const struct config *b = (struct config *) _b; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5264 | int x; |
| 5265 | x = a->rp->index - b->rp->index; |
| 5266 | if( x==0 ) x = a->dot - b->dot; |
| 5267 | return x; |
| 5268 | } |
| 5269 | |
| 5270 | /* Compare two states */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5271 | PRIVATE int statecmp(struct config *a, struct config *b) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5272 | { |
| 5273 | int rc; |
| 5274 | for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){ |
| 5275 | rc = a->rp->index - b->rp->index; |
| 5276 | if( rc==0 ) rc = a->dot - b->dot; |
| 5277 | } |
| 5278 | if( rc==0 ){ |
| 5279 | if( a ) rc = 1; |
| 5280 | if( b ) rc = -1; |
| 5281 | } |
| 5282 | return rc; |
| 5283 | } |
| 5284 | |
| 5285 | /* Hash a state */ |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5286 | PRIVATE unsigned statehash(struct config *a) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5287 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5288 | unsigned h=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5289 | while( a ){ |
| 5290 | h = h*571 + a->rp->index*37 + a->dot; |
| 5291 | a = a->bp; |
| 5292 | } |
| 5293 | return h; |
| 5294 | } |
| 5295 | |
| 5296 | /* Allocate a new state structure */ |
| 5297 | struct state *State_new() |
| 5298 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5299 | struct state *newstate; |
| 5300 | newstate = (struct state *)calloc(1, sizeof(struct state) ); |
| 5301 | MemoryCheck(newstate); |
| 5302 | return newstate; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | /* There is one instance of the following structure for each |
| 5306 | ** associative array of type "x3". |
| 5307 | */ |
| 5308 | struct s_x3 { |
| 5309 | int size; /* The number of available slots. */ |
| 5310 | /* Must be a power of 2 greater than or */ |
| 5311 | /* equal to 1 */ |
| 5312 | int count; /* Number of currently slots filled */ |
| 5313 | struct s_x3node *tbl; /* The data stored here */ |
| 5314 | struct s_x3node **ht; /* Hash table for lookups */ |
| 5315 | }; |
| 5316 | |
| 5317 | /* There is one instance of this structure for every data element |
| 5318 | ** in an associative array of type "x3". |
| 5319 | */ |
| 5320 | typedef struct s_x3node { |
| 5321 | struct state *data; /* The data */ |
| 5322 | struct config *key; /* The key */ |
| 5323 | struct s_x3node *next; /* Next entry with the same hash */ |
| 5324 | struct s_x3node **from; /* Previous link */ |
| 5325 | } x3node; |
| 5326 | |
| 5327 | /* There is only one instance of the array, which is the following */ |
| 5328 | static struct s_x3 *x3a; |
| 5329 | |
| 5330 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5331 | void State_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5332 | if( x3a ) return; |
| 5333 | x3a = (struct s_x3*)malloc( sizeof(struct s_x3) ); |
| 5334 | if( x3a ){ |
| 5335 | x3a->size = 128; |
| 5336 | x3a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5337 | x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5338 | if( x3a->tbl==0 ){ |
| 5339 | free(x3a); |
| 5340 | x3a = 0; |
| 5341 | }else{ |
| 5342 | int i; |
| 5343 | x3a->ht = (x3node**)&(x3a->tbl[128]); |
| 5344 | for(i=0; i<128; i++) x3a->ht[i] = 0; |
| 5345 | } |
| 5346 | } |
| 5347 | } |
| 5348 | /* Insert a new record into the array. Return TRUE if successful. |
| 5349 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5350 | int State_insert(struct state *data, struct config *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5351 | { |
| 5352 | x3node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5353 | unsigned h; |
| 5354 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5355 | |
| 5356 | if( x3a==0 ) return 0; |
| 5357 | ph = statehash(key); |
| 5358 | h = ph & (x3a->size-1); |
| 5359 | np = x3a->ht[h]; |
| 5360 | while( np ){ |
| 5361 | if( statecmp(np->key,key)==0 ){ |
| 5362 | /* An existing entry with the same key is found. */ |
| 5363 | /* Fail because overwrite is not allows. */ |
| 5364 | return 0; |
| 5365 | } |
| 5366 | np = np->next; |
| 5367 | } |
| 5368 | if( x3a->count>=x3a->size ){ |
| 5369 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5370 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5371 | struct s_x3 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5372 | array.size = arrSize = x3a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5373 | array.count = x3a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5374 | array.tbl = (x3node*)calloc(arrSize, sizeof(x3node) + sizeof(x3node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5375 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5376 | array.ht = (x3node**)&(array.tbl[arrSize]); |
| 5377 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5378 | for(i=0; i<x3a->count; i++){ |
| 5379 | x3node *oldnp, *newnp; |
| 5380 | oldnp = &(x3a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5381 | h = statehash(oldnp->key) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5382 | newnp = &(array.tbl[i]); |
| 5383 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5384 | newnp->next = array.ht[h]; |
| 5385 | newnp->key = oldnp->key; |
| 5386 | newnp->data = oldnp->data; |
| 5387 | newnp->from = &(array.ht[h]); |
| 5388 | array.ht[h] = newnp; |
| 5389 | } |
| 5390 | free(x3a->tbl); |
| 5391 | *x3a = array; |
| 5392 | } |
| 5393 | /* Insert the new data */ |
| 5394 | h = ph & (x3a->size-1); |
| 5395 | np = &(x3a->tbl[x3a->count++]); |
| 5396 | np->key = key; |
| 5397 | np->data = data; |
| 5398 | if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next); |
| 5399 | np->next = x3a->ht[h]; |
| 5400 | x3a->ht[h] = np; |
| 5401 | np->from = &(x3a->ht[h]); |
| 5402 | return 1; |
| 5403 | } |
| 5404 | |
| 5405 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5406 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5407 | struct state *State_find(struct config *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5408 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5409 | unsigned h; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5410 | x3node *np; |
| 5411 | |
| 5412 | if( x3a==0 ) return 0; |
| 5413 | h = statehash(key) & (x3a->size-1); |
| 5414 | np = x3a->ht[h]; |
| 5415 | while( np ){ |
| 5416 | if( statecmp(np->key,key)==0 ) break; |
| 5417 | np = np->next; |
| 5418 | } |
| 5419 | return np ? np->data : 0; |
| 5420 | } |
| 5421 | |
| 5422 | /* Return an array of pointers to all data in the table. |
| 5423 | ** The array is obtained from malloc. Return NULL if memory allocation |
| 5424 | ** problems, or if the array is empty. */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5425 | struct state **State_arrayof(void) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5426 | { |
| 5427 | struct state **array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5428 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5429 | if( x3a==0 ) return 0; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5430 | arrSize = x3a->count; |
| 5431 | array = (struct state **)calloc(arrSize, sizeof(struct state *)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5432 | if( array ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5433 | for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5434 | } |
| 5435 | return array; |
| 5436 | } |
| 5437 | |
| 5438 | /* Hash a configuration */ |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5439 | PRIVATE unsigned confighash(struct config *a) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5440 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5441 | unsigned h=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5442 | h = h*571 + a->rp->index*37 + a->dot; |
| 5443 | return h; |
| 5444 | } |
| 5445 | |
| 5446 | /* There is one instance of the following structure for each |
| 5447 | ** associative array of type "x4". |
| 5448 | */ |
| 5449 | struct s_x4 { |
| 5450 | int size; /* The number of available slots. */ |
| 5451 | /* Must be a power of 2 greater than or */ |
| 5452 | /* equal to 1 */ |
| 5453 | int count; /* Number of currently slots filled */ |
| 5454 | struct s_x4node *tbl; /* The data stored here */ |
| 5455 | struct s_x4node **ht; /* Hash table for lookups */ |
| 5456 | }; |
| 5457 | |
| 5458 | /* There is one instance of this structure for every data element |
| 5459 | ** in an associative array of type "x4". |
| 5460 | */ |
| 5461 | typedef struct s_x4node { |
| 5462 | struct config *data; /* The data */ |
| 5463 | struct s_x4node *next; /* Next entry with the same hash */ |
| 5464 | struct s_x4node **from; /* Previous link */ |
| 5465 | } x4node; |
| 5466 | |
| 5467 | /* There is only one instance of the array, which is the following */ |
| 5468 | static struct s_x4 *x4a; |
| 5469 | |
| 5470 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5471 | void Configtable_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5472 | if( x4a ) return; |
| 5473 | x4a = (struct s_x4*)malloc( sizeof(struct s_x4) ); |
| 5474 | if( x4a ){ |
| 5475 | x4a->size = 64; |
| 5476 | x4a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5477 | x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5478 | if( x4a->tbl==0 ){ |
| 5479 | free(x4a); |
| 5480 | x4a = 0; |
| 5481 | }else{ |
| 5482 | int i; |
| 5483 | x4a->ht = (x4node**)&(x4a->tbl[64]); |
| 5484 | for(i=0; i<64; i++) x4a->ht[i] = 0; |
| 5485 | } |
| 5486 | } |
| 5487 | } |
| 5488 | /* Insert a new record into the array. Return TRUE if successful. |
| 5489 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5490 | int Configtable_insert(struct config *data) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5491 | { |
| 5492 | x4node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5493 | unsigned h; |
| 5494 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5495 | |
| 5496 | if( x4a==0 ) return 0; |
| 5497 | ph = confighash(data); |
| 5498 | h = ph & (x4a->size-1); |
| 5499 | np = x4a->ht[h]; |
| 5500 | while( np ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5501 | if( Configcmp((const char *) np->data,(const char *) data)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5502 | /* An existing entry with the same key is found. */ |
| 5503 | /* Fail because overwrite is not allows. */ |
| 5504 | return 0; |
| 5505 | } |
| 5506 | np = np->next; |
| 5507 | } |
| 5508 | if( x4a->count>=x4a->size ){ |
| 5509 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5510 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5511 | struct s_x4 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5512 | array.size = arrSize = x4a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5513 | array.count = x4a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5514 | array.tbl = (x4node*)calloc(arrSize, sizeof(x4node) + sizeof(x4node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5515 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5516 | array.ht = (x4node**)&(array.tbl[arrSize]); |
| 5517 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5518 | for(i=0; i<x4a->count; i++){ |
| 5519 | x4node *oldnp, *newnp; |
| 5520 | oldnp = &(x4a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5521 | h = confighash(oldnp->data) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5522 | newnp = &(array.tbl[i]); |
| 5523 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5524 | newnp->next = array.ht[h]; |
| 5525 | newnp->data = oldnp->data; |
| 5526 | newnp->from = &(array.ht[h]); |
| 5527 | array.ht[h] = newnp; |
| 5528 | } |
| 5529 | free(x4a->tbl); |
| 5530 | *x4a = array; |
| 5531 | } |
| 5532 | /* Insert the new data */ |
| 5533 | h = ph & (x4a->size-1); |
| 5534 | np = &(x4a->tbl[x4a->count++]); |
| 5535 | np->data = data; |
| 5536 | if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next); |
| 5537 | np->next = x4a->ht[h]; |
| 5538 | x4a->ht[h] = np; |
| 5539 | np->from = &(x4a->ht[h]); |
| 5540 | return 1; |
| 5541 | } |
| 5542 | |
| 5543 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5544 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5545 | struct config *Configtable_find(struct config *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5546 | { |
| 5547 | int h; |
| 5548 | x4node *np; |
| 5549 | |
| 5550 | if( x4a==0 ) return 0; |
| 5551 | h = confighash(key) & (x4a->size-1); |
| 5552 | np = x4a->ht[h]; |
| 5553 | while( np ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5554 | if( Configcmp((const char *) np->data,(const char *) key)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5555 | np = np->next; |
| 5556 | } |
| 5557 | return np ? np->data : 0; |
| 5558 | } |
| 5559 | |
| 5560 | /* Remove all data from the table. Pass each data to the function "f" |
| 5561 | ** as it is removed. ("f" may be null to avoid this step.) */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5562 | void Configtable_clear(int(*f)(struct config *)) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5563 | { |
| 5564 | int i; |
| 5565 | if( x4a==0 || x4a->count==0 ) return; |
| 5566 | if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data); |
| 5567 | for(i=0; i<x4a->size; i++) x4a->ht[i] = 0; |
| 5568 | x4a->count = 0; |
| 5569 | return; |
| 5570 | } |