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 | |
mistachkin | 17df761 | 2019-06-03 15:09:25 +0000 | [diff] [blame] | 51 | extern void memory_error(); |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 52 | static int showPrecedenceConflict = 0; |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 53 | static char *msort(char*,char**,int(*)(const char*,const char*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 54 | |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 55 | /* |
| 56 | ** Compilers are getting increasingly pedantic about type conversions |
| 57 | ** as C evolves ever closer to Ada.... To work around the latest problems |
| 58 | ** we have to define the following variant of strlen(). |
| 59 | */ |
| 60 | #define lemonStrlen(X) ((int)strlen(X)) |
| 61 | |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 62 | /* |
| 63 | ** Compilers are starting to complain about the use of sprintf() and strcpy(), |
| 64 | ** saying they are unsafe. So we define our own versions of those routines too. |
| 65 | ** |
| 66 | ** There are three routines here: lemon_sprintf(), lemon_vsprintf(), and |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 67 | ** lemon_addtext(). The first two are replacements for sprintf() and vsprintf(). |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 68 | ** The third is a helper routine for vsnprintf() that adds texts to the end of a |
| 69 | ** buffer, making sure the buffer is always zero-terminated. |
| 70 | ** |
| 71 | ** The string formatter is a minimal subset of stdlib sprintf() supporting only |
| 72 | ** a few simply conversions: |
| 73 | ** |
| 74 | ** %d |
| 75 | ** %s |
| 76 | ** %.*s |
| 77 | ** |
| 78 | */ |
| 79 | static void lemon_addtext( |
| 80 | char *zBuf, /* The buffer to which text is added */ |
| 81 | int *pnUsed, /* Slots of the buffer used so far */ |
| 82 | const char *zIn, /* Text to add */ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 83 | int nIn, /* Bytes of text to add. -1 to use strlen() */ |
| 84 | int iWidth /* Field width. Negative to left justify */ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 85 | ){ |
| 86 | if( nIn<0 ) for(nIn=0; zIn[nIn]; nIn++){} |
drh | ecaa9d3 | 2014-01-11 03:27:37 +0000 | [diff] [blame] | 87 | while( iWidth>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth--; } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 88 | if( nIn==0 ) return; |
| 89 | memcpy(&zBuf[*pnUsed], zIn, nIn); |
| 90 | *pnUsed += nIn; |
drh | ecaa9d3 | 2014-01-11 03:27:37 +0000 | [diff] [blame] | 91 | while( (-iWidth)>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth++; } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 92 | zBuf[*pnUsed] = 0; |
| 93 | } |
| 94 | static int lemon_vsprintf(char *str, const char *zFormat, va_list ap){ |
mistachkin | 7a42965 | 2014-01-14 10:17:21 +0000 | [diff] [blame] | 95 | int i, j, k, c; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 96 | int nUsed = 0; |
| 97 | const char *z; |
| 98 | char zTemp[50]; |
| 99 | str[0] = 0; |
| 100 | for(i=j=0; (c = zFormat[i])!=0; i++){ |
| 101 | if( c=='%' ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 102 | int iWidth = 0; |
| 103 | lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 104 | c = zFormat[++i]; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 105 | if( ISDIGIT(c) || (c=='-' && ISDIGIT(zFormat[i+1])) ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 106 | if( c=='-' ) i++; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 107 | while( ISDIGIT(zFormat[i]) ) iWidth = iWidth*10 + zFormat[i++] - '0'; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 108 | if( c=='-' ) iWidth = -iWidth; |
| 109 | c = zFormat[i]; |
| 110 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 111 | if( c=='d' ){ |
| 112 | int v = va_arg(ap, int); |
| 113 | if( v<0 ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 114 | lemon_addtext(str, &nUsed, "-", 1, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 115 | v = -v; |
| 116 | }else if( v==0 ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 117 | lemon_addtext(str, &nUsed, "0", 1, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 118 | } |
| 119 | k = 0; |
| 120 | while( v>0 ){ |
| 121 | k++; |
| 122 | zTemp[sizeof(zTemp)-k] = (v%10) + '0'; |
| 123 | v /= 10; |
| 124 | } |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 125 | lemon_addtext(str, &nUsed, &zTemp[sizeof(zTemp)-k], k, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 126 | }else if( c=='s' ){ |
| 127 | z = va_arg(ap, const char*); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 128 | lemon_addtext(str, &nUsed, z, -1, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 129 | }else if( c=='.' && memcmp(&zFormat[i], ".*s", 3)==0 ){ |
| 130 | i += 2; |
| 131 | k = va_arg(ap, int); |
| 132 | z = va_arg(ap, const char*); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 133 | lemon_addtext(str, &nUsed, z, k, iWidth); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 134 | }else if( c=='%' ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 135 | lemon_addtext(str, &nUsed, "%", 1, 0); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 136 | }else{ |
| 137 | fprintf(stderr, "illegal format\n"); |
| 138 | exit(1); |
| 139 | } |
| 140 | j = i+1; |
| 141 | } |
| 142 | } |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 143 | lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 144 | return nUsed; |
| 145 | } |
| 146 | static int lemon_sprintf(char *str, const char *format, ...){ |
| 147 | va_list ap; |
| 148 | int rc; |
| 149 | va_start(ap, format); |
| 150 | rc = lemon_vsprintf(str, format, ap); |
| 151 | va_end(ap); |
| 152 | return rc; |
| 153 | } |
| 154 | static void lemon_strcpy(char *dest, const char *src){ |
| 155 | while( (*(dest++) = *(src++))!=0 ){} |
| 156 | } |
| 157 | static void lemon_strcat(char *dest, const char *src){ |
| 158 | while( *dest ) dest++; |
| 159 | lemon_strcpy(dest, src); |
| 160 | } |
| 161 | |
| 162 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 163 | /* a few forward declarations... */ |
| 164 | struct rule; |
| 165 | struct lemon; |
| 166 | struct action; |
| 167 | |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 168 | static struct action *Action_new(void); |
| 169 | static struct action *Action_sort(struct action *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 170 | |
| 171 | /********** From the file "build.h" ************************************/ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 172 | void FindRulePrecedences(struct lemon*); |
| 173 | void FindFirstSets(struct lemon*); |
| 174 | void FindStates(struct lemon*); |
| 175 | void FindLinks(struct lemon*); |
| 176 | void FindFollowSets(struct lemon*); |
| 177 | void FindActions(struct lemon*); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 178 | |
| 179 | /********* From the file "configlist.h" *********************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 180 | void Configlist_init(void); |
| 181 | struct config *Configlist_add(struct rule *, int); |
| 182 | struct config *Configlist_addbasis(struct rule *, int); |
| 183 | void Configlist_closure(struct lemon *); |
| 184 | void Configlist_sort(void); |
| 185 | void Configlist_sortbasis(void); |
| 186 | struct config *Configlist_return(void); |
| 187 | struct config *Configlist_basis(void); |
| 188 | void Configlist_eat(struct config *); |
| 189 | void Configlist_reset(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 190 | |
| 191 | /********* From the file "error.h" ***************************************/ |
drh | f9a2e7b | 2003-04-15 01:49:48 +0000 | [diff] [blame] | 192 | void ErrorMsg(const char *, int,const char *, ...); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 193 | |
| 194 | /****** From the file "option.h" ******************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 195 | enum option_type { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR, |
| 196 | OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR}; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 197 | struct s_options { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 198 | enum option_type type; |
| 199 | const char *label; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 200 | char *arg; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 201 | const char *message; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 202 | }; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 203 | int OptInit(char**,struct s_options*,FILE*); |
| 204 | int OptNArgs(void); |
| 205 | char *OptArg(int); |
| 206 | void OptErr(int); |
| 207 | void OptPrint(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 208 | |
| 209 | /******** From the file "parse.h" *****************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 210 | void Parse(struct lemon *lemp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 211 | |
| 212 | /********* From the file "plink.h" ***************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 213 | struct plink *Plink_new(void); |
| 214 | void Plink_add(struct plink **, struct config *); |
| 215 | void Plink_copy(struct plink **, struct plink *); |
| 216 | void Plink_delete(struct plink *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 217 | |
| 218 | /********** From the file "report.h" *************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 219 | void Reprint(struct lemon *); |
| 220 | void ReportOutput(struct lemon *); |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 221 | void ReportTable(struct lemon *, int, int); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 222 | void ReportHeader(struct lemon *); |
| 223 | void CompressTables(struct lemon *); |
| 224 | void ResortStates(struct lemon *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 225 | |
| 226 | /********** From the file "set.h" ****************************************/ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 227 | void SetSize(int); /* All sets will be of size N */ |
| 228 | char *SetNew(void); /* A new set for element 0..N */ |
| 229 | void SetFree(char*); /* Deallocate a set */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 230 | int SetAdd(char*,int); /* Add element to a set */ |
| 231 | int SetUnion(char *,char *); /* A <- A U B, thru element N */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 232 | #define SetFind(X,Y) (X[Y]) /* True if Y is in set X */ |
| 233 | |
| 234 | /********** From the file "struct.h" *************************************/ |
| 235 | /* |
| 236 | ** Principal data structures for the LEMON parser generator. |
| 237 | */ |
| 238 | |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 239 | typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 240 | |
| 241 | /* Symbols (terminals and nonterminals) of the grammar are stored |
| 242 | ** in the following: */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 243 | enum symbol_type { |
| 244 | TERMINAL, |
| 245 | NONTERMINAL, |
| 246 | MULTITERMINAL |
| 247 | }; |
| 248 | enum e_assoc { |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 249 | LEFT, |
| 250 | RIGHT, |
| 251 | NONE, |
| 252 | UNK |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 253 | }; |
| 254 | struct symbol { |
| 255 | const char *name; /* Name of the symbol */ |
| 256 | int index; /* Index number for this symbol */ |
| 257 | enum symbol_type type; /* Symbols are all either TERMINALS or NTs */ |
| 258 | struct rule *rule; /* Linked list of rules of this (if an NT) */ |
| 259 | struct symbol *fallback; /* fallback token in case this token doesn't parse */ |
| 260 | int prec; /* Precedence if defined (-1 otherwise) */ |
| 261 | enum e_assoc assoc; /* Associativity if precedence is defined */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 262 | char *firstset; /* First-set for all rules of this symbol */ |
| 263 | Boolean lambda; /* True if NT and can generate an empty string */ |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 264 | int useCnt; /* Number of times used */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 265 | char *destructor; /* Code which executes whenever this symbol is |
| 266 | ** popped from the stack during error processing */ |
drh | 0f832dd | 2016-08-16 16:46:40 +0000 | [diff] [blame] | 267 | int destLineno; /* Line number for start of destructor. Set to |
| 268 | ** -1 for duplicate destructors. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 269 | char *datatype; /* The data type of information held by this |
| 270 | ** object. Only used if type==NONTERMINAL */ |
| 271 | int dtnum; /* The data type number. In the parser, the value |
| 272 | ** stack is a union. The .yy%d element of this |
| 273 | ** union is the correct data type for this object */ |
drh | 539e741 | 2018-04-21 20:24:19 +0000 | [diff] [blame] | 274 | int bContent; /* True if this symbol ever carries content - if |
| 275 | ** it is ever more than just syntax */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 276 | /* The following fields are used by MULTITERMINALs only */ |
| 277 | int nsubsym; /* Number of constituent symbols in the MULTI */ |
| 278 | struct symbol **subsym; /* Array of constituent symbols */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | /* Each production rule in the grammar is stored in the following |
| 282 | ** structure. */ |
| 283 | struct rule { |
| 284 | struct symbol *lhs; /* Left-hand side of the rule */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 285 | const char *lhsalias; /* Alias for the LHS (NULL if none) */ |
drh | b496099 | 2007-10-05 16:16:36 +0000 | [diff] [blame] | 286 | int lhsStart; /* True if left-hand side is the start symbol */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 287 | int ruleline; /* Line number for the rule */ |
| 288 | int nrhs; /* Number of RHS symbols */ |
| 289 | struct symbol **rhs; /* The RHS symbols */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 290 | const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 291 | int line; /* Line number at which code begins */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 292 | const char *code; /* The code executed when this rule is reduced */ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 293 | const char *codePrefix; /* Setup code before code[] above */ |
| 294 | const char *codeSuffix; /* Breakdown code after code[] above */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 295 | struct symbol *precsym; /* Precedence symbol for this rule */ |
| 296 | int index; /* An index number for this rule */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 297 | int iRule; /* Rule number as used in the generated tables */ |
drh | e94006e | 2019-12-10 20:41:48 +0000 | [diff] [blame] | 298 | Boolean noCode; /* True if this rule has no associated C code */ |
| 299 | Boolean codeEmitted; /* True if the code has been emitted already */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 300 | Boolean canReduce; /* True if this rule is ever reduced */ |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 301 | Boolean doesReduce; /* Reduce actions occur after optimization */ |
drh | e94006e | 2019-12-10 20:41:48 +0000 | [diff] [blame] | 302 | Boolean neverReduce; /* Reduce is theoretically possible, but prevented |
| 303 | ** by actions or other outside implementation */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | struct rule *nextlhs; /* Next rule with the same LHS */ |
| 305 | struct rule *next; /* Next rule in the global list */ |
| 306 | }; |
| 307 | |
| 308 | /* A configuration is a production rule of the grammar together with |
| 309 | ** a mark (dot) showing how much of that rule has been processed so far. |
| 310 | ** Configurations also contain a follow-set which is a list of terminal |
| 311 | ** symbols which are allowed to immediately follow the end of the rule. |
| 312 | ** Every configuration is recorded as an instance of the following: */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 313 | enum cfgstatus { |
| 314 | COMPLETE, |
| 315 | INCOMPLETE |
| 316 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 317 | struct config { |
| 318 | struct rule *rp; /* The rule upon which the configuration is based */ |
| 319 | int dot; /* The parse point */ |
| 320 | char *fws; /* Follow-set for this configuration only */ |
| 321 | struct plink *fplp; /* Follow-set forward propagation links */ |
| 322 | struct plink *bplp; /* Follow-set backwards propagation links */ |
| 323 | struct state *stp; /* Pointer to state which contains this */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 324 | enum cfgstatus status; /* used during followset and shift computations */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 325 | struct config *next; /* Next configuration in the state */ |
| 326 | struct config *bp; /* The next basis configuration */ |
| 327 | }; |
| 328 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 329 | enum e_action { |
| 330 | SHIFT, |
| 331 | ACCEPT, |
| 332 | REDUCE, |
| 333 | ERROR, |
| 334 | SSCONFLICT, /* A shift/shift conflict */ |
| 335 | SRCONFLICT, /* Was a reduce, but part of a conflict */ |
| 336 | RRCONFLICT, /* Was a reduce, but part of a conflict */ |
| 337 | SH_RESOLVED, /* Was a shift. Precedence resolved conflict */ |
| 338 | RD_RESOLVED, /* Was reduce. Precedence resolved conflict */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 339 | NOT_USED, /* Deleted by compression */ |
| 340 | SHIFTREDUCE /* Shift first, then reduce */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 341 | }; |
| 342 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 343 | /* Every shift or reduce operation is stored as one of the following */ |
| 344 | struct action { |
| 345 | struct symbol *sp; /* The look-ahead symbol */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 346 | enum e_action type; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 347 | union { |
| 348 | struct state *stp; /* The new state, if a shift */ |
| 349 | struct rule *rp; /* The rule, if a reduce */ |
| 350 | } x; |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 351 | struct symbol *spOpt; /* SHIFTREDUCE optimization to this symbol */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 352 | struct action *next; /* Next action for this state */ |
| 353 | struct action *collide; /* Next action with the same hash */ |
| 354 | }; |
| 355 | |
| 356 | /* Each state of the generated parser's finite state machine |
| 357 | ** is encoded as an instance of the following structure. */ |
| 358 | struct state { |
| 359 | struct config *bp; /* The basis configurations for this state */ |
| 360 | struct config *cfp; /* All configurations in this set */ |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 361 | int statenum; /* Sequential number for this state */ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 362 | struct action *ap; /* List of actions for this state */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 363 | int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */ |
| 364 | int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 365 | int iDfltReduce; /* Default action is to REDUCE by this rule */ |
| 366 | struct rule *pDfltReduce;/* The default REDUCE rule. */ |
| 367 | int autoReduce; /* True if this is an auto-reduce state */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 368 | }; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 369 | #define NO_OFFSET (-2147483647) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 370 | |
| 371 | /* A followset propagation link indicates that the contents of one |
| 372 | ** configuration followset should be propagated to another whenever |
| 373 | ** the first changes. */ |
| 374 | struct plink { |
| 375 | struct config *cfp; /* The configuration to which linked */ |
| 376 | struct plink *next; /* The next propagate link */ |
| 377 | }; |
| 378 | |
| 379 | /* The state vector for the entire parser generator is recorded as |
| 380 | ** follows. (LEMON uses no global variables and makes little use of |
| 381 | ** static variables. Fields in the following structure can be thought |
| 382 | ** of as begin global variables in the program.) */ |
| 383 | struct lemon { |
| 384 | struct state **sorted; /* Table of states sorted by state number */ |
| 385 | struct rule *rule; /* List of all rules */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 386 | struct rule *startRule; /* First rule */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 387 | int nstate; /* Number of states */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 388 | int nxstate; /* nstate with tail degenerate states removed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 389 | int nrule; /* Number of rules */ |
drh | ce678c2 | 2019-12-11 18:53:51 +0000 | [diff] [blame] | 390 | int nruleWithAction; /* Number of rules with actions */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 391 | int nsymbol; /* Number of terminal and nonterminal symbols */ |
| 392 | int nterminal; /* Number of terminal symbols */ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 393 | int minShiftReduce; /* Minimum shift-reduce action value */ |
| 394 | int errAction; /* Error action value */ |
| 395 | int accAction; /* Accept action value */ |
| 396 | int noAction; /* No-op action value */ |
| 397 | int minReduce; /* Minimum reduce action */ |
| 398 | int maxAction; /* Maximum action value of any kind */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 399 | struct symbol **symbols; /* Sorted array of pointers to symbols */ |
| 400 | int errorcnt; /* Number of errors */ |
| 401 | struct symbol *errsym; /* The error symbol */ |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 402 | struct symbol *wildcard; /* Token that matches anything */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 403 | char *name; /* Name of the generated parser */ |
| 404 | char *arg; /* Declaration of the 3th argument to parser */ |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 405 | char *ctx; /* Declaration of 2nd argument to constructor */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 406 | char *tokentype; /* Type of terminal symbols in the parser stack */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 407 | char *vartype; /* The default type of non-terminal symbols */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 408 | char *start; /* Name of the start symbol for the grammar */ |
| 409 | char *stacksize; /* Size of the parser stack */ |
| 410 | char *include; /* Code to put at the start of the C file */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 411 | char *error; /* Code to execute when an error is seen */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 412 | char *overflow; /* Code to execute on a stack overflow */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 413 | char *failure; /* Code to execute on parser failure */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 414 | char *accept; /* Code to execute when the parser excepts */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 415 | char *extracode; /* Code appended to the generated file */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 416 | char *tokendest; /* Code to execute to destroy token data */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 417 | char *vardest; /* Code for the default non-terminal destructor */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 418 | char *filename; /* Name of the input file */ |
| 419 | char *outname; /* Name of the current output file */ |
| 420 | char *tokenprefix; /* A prefix added to token names in the .h file */ |
| 421 | int nconflict; /* Number of parsing conflicts */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 422 | int nactiontab; /* Number of entries in the yy_action[] table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 423 | int nlookaheadtab; /* Number of entries in yy_lookahead[] */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 424 | int tablesize; /* Total table size of all tables in bytes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 425 | int basisflag; /* Print only basis configurations */ |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 426 | int printPreprocessed; /* Show preprocessor output on stdout */ |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 427 | int has_fallback; /* True if any %fallback is seen in the grammar */ |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 428 | int nolinenosflag; /* True if #line statements should not be printed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 429 | char *argv0; /* Name of the program */ |
| 430 | }; |
| 431 | |
| 432 | #define MemoryCheck(X) if((X)==0){ \ |
| 433 | extern void memory_error(); \ |
| 434 | memory_error(); \ |
| 435 | } |
| 436 | |
| 437 | /**************** From the file "table.h" *********************************/ |
| 438 | /* |
| 439 | ** All code in this file has been automatically generated |
| 440 | ** from a specification in the file |
| 441 | ** "table.q" |
| 442 | ** by the associative array code building program "aagen". |
| 443 | ** Do not edit this file! Instead, edit the specification |
| 444 | ** file, then rerun aagen. |
| 445 | */ |
| 446 | /* |
| 447 | ** Code for processing tables in the LEMON parser generator. |
| 448 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 449 | /* Routines for handling a strings */ |
| 450 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 451 | const char *Strsafe(const char *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 452 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 453 | void Strsafe_init(void); |
| 454 | int Strsafe_insert(const char *); |
| 455 | const char *Strsafe_find(const char *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 456 | |
| 457 | /* Routines for handling symbols of the grammar */ |
| 458 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 459 | struct symbol *Symbol_new(const char *); |
| 460 | int Symbolcmpp(const void *, const void *); |
| 461 | void Symbol_init(void); |
| 462 | int Symbol_insert(struct symbol *, const char *); |
| 463 | struct symbol *Symbol_find(const char *); |
| 464 | struct symbol *Symbol_Nth(int); |
| 465 | int Symbol_count(void); |
| 466 | struct symbol **Symbol_arrayof(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 467 | |
| 468 | /* Routines to manage the state table */ |
| 469 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 470 | int Configcmp(const char *, const char *); |
| 471 | struct state *State_new(void); |
| 472 | void State_init(void); |
| 473 | int State_insert(struct state *, struct config *); |
| 474 | struct state *State_find(struct config *); |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 475 | struct state **State_arrayof(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 476 | |
| 477 | /* Routines used for efficiency in Configlist_add */ |
| 478 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 479 | void Configtable_init(void); |
| 480 | int Configtable_insert(struct config *); |
| 481 | struct config *Configtable_find(struct config *); |
| 482 | void Configtable_clear(int(*)(struct config *)); |
| 483 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 484 | /****************** From the file "action.c" *******************************/ |
| 485 | /* |
| 486 | ** Routines processing parser actions in the LEMON parser generator. |
| 487 | */ |
| 488 | |
| 489 | /* Allocate a new parser action */ |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 490 | static struct action *Action_new(void){ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 491 | static struct action *actionfreelist = 0; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 492 | struct action *newaction; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 493 | |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 494 | if( actionfreelist==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 495 | int i; |
| 496 | int amt = 100; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 497 | actionfreelist = (struct action *)calloc(amt, sizeof(struct action)); |
| 498 | if( actionfreelist==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 499 | fprintf(stderr,"Unable to allocate memory for a new parser action."); |
| 500 | exit(1); |
| 501 | } |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 502 | for(i=0; i<amt-1; i++) actionfreelist[i].next = &actionfreelist[i+1]; |
| 503 | actionfreelist[amt-1].next = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 504 | } |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 505 | newaction = actionfreelist; |
| 506 | actionfreelist = actionfreelist->next; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 507 | return newaction; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 508 | } |
| 509 | |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 510 | /* Compare two actions for sorting purposes. Return negative, zero, or |
| 511 | ** positive if the first action is less than, equal to, or greater than |
| 512 | ** the first |
| 513 | */ |
| 514 | static int actioncmp( |
| 515 | struct action *ap1, |
| 516 | struct action *ap2 |
| 517 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 518 | int rc; |
| 519 | rc = ap1->sp->index - ap2->sp->index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 520 | if( rc==0 ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 521 | rc = (int)ap1->type - (int)ap2->type; |
| 522 | } |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 523 | if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 524 | rc = ap1->x.rp->index - ap2->x.rp->index; |
| 525 | } |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 526 | if( rc==0 ){ |
icculus | 7b429aa | 2010-03-03 17:09:01 +0000 | [diff] [blame] | 527 | rc = (int) (ap2 - ap1); |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 528 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 529 | return rc; |
| 530 | } |
| 531 | |
| 532 | /* Sort parser actions */ |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 533 | static struct action *Action_sort( |
| 534 | struct action *ap |
| 535 | ){ |
| 536 | ap = (struct action *)msort((char *)ap,(char **)&ap->next, |
| 537 | (int(*)(const char*,const char*))actioncmp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 538 | return ap; |
| 539 | } |
| 540 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 541 | void Action_add( |
| 542 | struct action **app, |
| 543 | enum e_action type, |
| 544 | struct symbol *sp, |
| 545 | char *arg |
| 546 | ){ |
| 547 | struct action *newaction; |
| 548 | newaction = Action_new(); |
| 549 | newaction->next = *app; |
| 550 | *app = newaction; |
| 551 | newaction->type = type; |
| 552 | newaction->sp = sp; |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 553 | newaction->spOpt = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 554 | if( type==SHIFT ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 555 | newaction->x.stp = (struct state *)arg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 556 | }else{ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 557 | newaction->x.rp = (struct rule *)arg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 558 | } |
| 559 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 560 | /********************** New code to implement the "acttab" module ***********/ |
| 561 | /* |
| 562 | ** This module implements routines use to construct the yy_action[] table. |
| 563 | */ |
| 564 | |
| 565 | /* |
| 566 | ** The state of the yy_action table under construction is an instance of |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 567 | ** the following structure. |
| 568 | ** |
| 569 | ** The yy_action table maps the pair (state_number, lookahead) into an |
| 570 | ** action_number. The table is an array of integers pairs. The state_number |
| 571 | ** determines an initial offset into the yy_action array. The lookahead |
| 572 | ** value is then added to this initial offset to get an index X into the |
| 573 | ** yy_action array. If the aAction[X].lookahead equals the value of the |
| 574 | ** of the lookahead input, then the value of the action_number output is |
| 575 | ** aAction[X].action. If the lookaheads do not match then the |
| 576 | ** default action for the state_number is returned. |
| 577 | ** |
| 578 | ** All actions associated with a single state_number are first entered |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 579 | ** into aLookahead[] using multiple calls to acttab_action(). Then the |
| 580 | ** actions for that single state_number are placed into the aAction[] |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 581 | ** array with a single call to acttab_insert(). The acttab_insert() call |
| 582 | ** also resets the aLookahead[] array in preparation for the next |
| 583 | ** state number. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 584 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 585 | struct lookahead_action { |
| 586 | int lookahead; /* Value of the lookahead token */ |
| 587 | int action; /* Action to take on the given lookahead */ |
| 588 | }; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 589 | typedef struct acttab acttab; |
| 590 | struct acttab { |
| 591 | int nAction; /* Number of used slots in aAction[] */ |
| 592 | int nActionAlloc; /* Slots allocated for aAction[] */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 593 | struct lookahead_action |
| 594 | *aAction, /* The yy_action[] table under construction */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 595 | *aLookahead; /* A single new transaction set */ |
| 596 | int mnLookahead; /* Minimum aLookahead[].lookahead */ |
| 597 | int mnAction; /* Action associated with mnLookahead */ |
| 598 | int mxLookahead; /* Maximum aLookahead[].lookahead */ |
| 599 | int nLookahead; /* Used slots in aLookahead[] */ |
| 600 | int nLookaheadAlloc; /* Slots allocated in aLookahead[] */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 601 | int nterminal; /* Number of terminal symbols */ |
| 602 | int nsymbol; /* total number of symbols */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 603 | }; |
| 604 | |
| 605 | /* Return the number of entries in the yy_action table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 606 | #define acttab_lookahead_size(X) ((X)->nAction) |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 607 | |
| 608 | /* The value for the N-th entry in yy_action */ |
| 609 | #define acttab_yyaction(X,N) ((X)->aAction[N].action) |
| 610 | |
| 611 | /* The value for the N-th entry in yy_lookahead */ |
| 612 | #define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead) |
| 613 | |
| 614 | /* Free all memory associated with the given acttab */ |
| 615 | void acttab_free(acttab *p){ |
| 616 | free( p->aAction ); |
| 617 | free( p->aLookahead ); |
| 618 | free( p ); |
| 619 | } |
| 620 | |
| 621 | /* Allocate a new acttab structure */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 622 | acttab *acttab_alloc(int nsymbol, int nterminal){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 623 | acttab *p = (acttab *) calloc( 1, sizeof(*p) ); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 624 | if( p==0 ){ |
| 625 | fprintf(stderr,"Unable to allocate memory for a new acttab."); |
| 626 | exit(1); |
| 627 | } |
| 628 | memset(p, 0, sizeof(*p)); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 629 | p->nsymbol = nsymbol; |
| 630 | p->nterminal = nterminal; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 631 | return p; |
| 632 | } |
| 633 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 634 | /* Add a new action to the current transaction set. |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 635 | ** |
| 636 | ** This routine is called once for each lookahead for a particular |
| 637 | ** state. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 638 | */ |
| 639 | void acttab_action(acttab *p, int lookahead, int action){ |
| 640 | if( p->nLookahead>=p->nLookaheadAlloc ){ |
| 641 | p->nLookaheadAlloc += 25; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 642 | p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead, |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 643 | sizeof(p->aLookahead[0])*p->nLookaheadAlloc ); |
| 644 | if( p->aLookahead==0 ){ |
| 645 | fprintf(stderr,"malloc failed\n"); |
| 646 | exit(1); |
| 647 | } |
| 648 | } |
| 649 | if( p->nLookahead==0 ){ |
| 650 | p->mxLookahead = lookahead; |
| 651 | p->mnLookahead = lookahead; |
| 652 | p->mnAction = action; |
| 653 | }else{ |
| 654 | if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead; |
| 655 | if( p->mnLookahead>lookahead ){ |
| 656 | p->mnLookahead = lookahead; |
| 657 | p->mnAction = action; |
| 658 | } |
| 659 | } |
| 660 | p->aLookahead[p->nLookahead].lookahead = lookahead; |
| 661 | p->aLookahead[p->nLookahead].action = action; |
| 662 | p->nLookahead++; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | ** Add the transaction set built up with prior calls to acttab_action() |
| 667 | ** into the current action table. Then reset the transaction set back |
| 668 | ** to an empty set in preparation for a new round of acttab_action() calls. |
| 669 | ** |
| 670 | ** Return the offset into the action table of the new transaction. |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 671 | ** |
| 672 | ** If the makeItSafe parameter is true, then the offset is chosen so that |
| 673 | ** it is impossible to overread the yy_lookaside[] table regardless of |
| 674 | ** the lookaside token. This is done for the terminal symbols, as they |
| 675 | ** come from external inputs and can contain syntax errors. When makeItSafe |
| 676 | ** is false, there is more flexibility in selecting offsets, resulting in |
| 677 | ** a smaller table. For non-terminal symbols, which are never syntax errors, |
| 678 | ** makeItSafe can be false. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 679 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 680 | int acttab_insert(acttab *p, int makeItSafe){ |
| 681 | int i, j, k, n, end; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 682 | assert( p->nLookahead>0 ); |
| 683 | |
| 684 | /* Make sure we have enough space to hold the expanded action table |
| 685 | ** in the worst case. The worst case occurs if the transaction set |
| 686 | ** must be appended to the current action table |
| 687 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 688 | n = p->nsymbol + 1; |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 689 | if( p->nAction + n >= p->nActionAlloc ){ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 690 | int oldAlloc = p->nActionAlloc; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 691 | p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 692 | p->aAction = (struct lookahead_action *) realloc( p->aAction, |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 693 | sizeof(p->aAction[0])*p->nActionAlloc); |
| 694 | if( p->aAction==0 ){ |
| 695 | fprintf(stderr,"malloc failed\n"); |
| 696 | exit(1); |
| 697 | } |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 698 | for(i=oldAlloc; i<p->nActionAlloc; i++){ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 699 | p->aAction[i].lookahead = -1; |
| 700 | p->aAction[i].action = -1; |
| 701 | } |
| 702 | } |
| 703 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 704 | /* Scan the existing action table looking for an offset that is a |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 705 | ** duplicate of the current transaction set. Fall out of the loop |
| 706 | ** if and when the duplicate is found. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 707 | ** |
| 708 | ** i is the index in p->aAction[] where p->mnLookahead is inserted. |
| 709 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 710 | end = makeItSafe ? p->mnLookahead : 0; |
| 711 | for(i=p->nAction-1; i>=end; i--){ |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 712 | if( p->aAction[i].lookahead==p->mnLookahead ){ |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 713 | /* All lookaheads and actions in the aLookahead[] transaction |
| 714 | ** must match against the candidate aAction[i] entry. */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 715 | if( p->aAction[i].action!=p->mnAction ) continue; |
| 716 | for(j=0; j<p->nLookahead; j++){ |
| 717 | k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| 718 | if( k<0 || k>=p->nAction ) break; |
| 719 | if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break; |
| 720 | if( p->aLookahead[j].action!=p->aAction[k].action ) break; |
| 721 | } |
| 722 | if( j<p->nLookahead ) continue; |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 723 | |
| 724 | /* No possible lookahead value that is not in the aLookahead[] |
| 725 | ** transaction is allowed to match aAction[i] */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 726 | n = 0; |
| 727 | for(j=0; j<p->nAction; j++){ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 728 | if( p->aAction[j].lookahead<0 ) continue; |
| 729 | if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 730 | } |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 731 | if( n==p->nLookahead ){ |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 732 | break; /* An exact match is found at offset i */ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 733 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 736 | |
| 737 | /* If no existing offsets exactly match the current transaction, find an |
| 738 | ** an empty offset in the aAction[] table in which we can add the |
| 739 | ** aLookahead[] transaction. |
| 740 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 741 | if( i<end ){ |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 742 | /* Look for holes in the aAction[] table that fit the current |
| 743 | ** aLookahead[] transaction. Leave i set to the offset of the hole. |
| 744 | ** If no holes are found, i is left at p->nAction, which means the |
| 745 | ** transaction will be appended. */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 746 | i = makeItSafe ? p->mnLookahead : 0; |
| 747 | for(; i<p->nActionAlloc - p->mxLookahead; i++){ |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 748 | if( p->aAction[i].lookahead<0 ){ |
| 749 | for(j=0; j<p->nLookahead; j++){ |
| 750 | k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| 751 | if( k<0 ) break; |
| 752 | if( p->aAction[k].lookahead>=0 ) break; |
| 753 | } |
| 754 | if( j<p->nLookahead ) continue; |
| 755 | for(j=0; j<p->nAction; j++){ |
| 756 | if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break; |
| 757 | } |
| 758 | if( j==p->nAction ){ |
| 759 | break; /* Fits in empty slots */ |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 764 | /* Insert transaction set at index i. */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 765 | #if 0 |
| 766 | printf("Acttab:"); |
| 767 | for(j=0; j<p->nLookahead; j++){ |
| 768 | printf(" %d", p->aLookahead[j].lookahead); |
| 769 | } |
| 770 | printf(" inserted at %d\n", i); |
| 771 | #endif |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 772 | for(j=0; j<p->nLookahead; j++){ |
| 773 | k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| 774 | p->aAction[k] = p->aLookahead[j]; |
| 775 | if( k>=p->nAction ) p->nAction = k+1; |
| 776 | } |
drh | 4396c61 | 2017-12-27 15:21:16 +0000 | [diff] [blame] | 777 | if( makeItSafe && i+p->nterminal>=p->nAction ) p->nAction = i+p->nterminal+1; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 778 | p->nLookahead = 0; |
| 779 | |
| 780 | /* Return the offset that is added to the lookahead in order to get the |
| 781 | ** index into yy_action of the action */ |
| 782 | return i - p->mnLookahead; |
| 783 | } |
| 784 | |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 785 | /* |
| 786 | ** Return the size of the action table without the trailing syntax error |
| 787 | ** entries. |
| 788 | */ |
| 789 | int acttab_action_size(acttab *p){ |
| 790 | int n = p->nAction; |
| 791 | while( n>0 && p->aAction[n-1].lookahead<0 ){ n--; } |
| 792 | return n; |
| 793 | } |
| 794 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 795 | /********************** From the file "build.c" *****************************/ |
| 796 | /* |
| 797 | ** Routines to construction the finite state machine for the LEMON |
| 798 | ** parser generator. |
| 799 | */ |
| 800 | |
| 801 | /* Find a precedence symbol of every rule in the grammar. |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 802 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 803 | ** Those rules which have a precedence symbol coded in the input |
| 804 | ** grammar using the "[symbol]" construct will already have the |
| 805 | ** rp->precsym field filled. Other rules take as their precedence |
| 806 | ** symbol the first RHS symbol with a defined precedence. If there |
| 807 | ** are not RHS symbols with a defined precedence, the precedence |
| 808 | ** symbol field is left blank. |
| 809 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 810 | void FindRulePrecedences(struct lemon *xp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 811 | { |
| 812 | struct rule *rp; |
| 813 | for(rp=xp->rule; rp; rp=rp->next){ |
| 814 | if( rp->precsym==0 ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 815 | int i, j; |
| 816 | for(i=0; i<rp->nrhs && rp->precsym==0; i++){ |
| 817 | struct symbol *sp = rp->rhs[i]; |
| 818 | if( sp->type==MULTITERMINAL ){ |
| 819 | for(j=0; j<sp->nsubsym; j++){ |
| 820 | if( sp->subsym[j]->prec>=0 ){ |
| 821 | rp->precsym = sp->subsym[j]; |
| 822 | break; |
| 823 | } |
| 824 | } |
| 825 | }else if( sp->prec>=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 826 | rp->precsym = rp->rhs[i]; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 827 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | /* Find all nonterminals which will generate the empty string. |
| 835 | ** Then go back and compute the first sets of every nonterminal. |
| 836 | ** The first set is the set of all terminal symbols which can begin |
| 837 | ** a string generated by that nonterminal. |
| 838 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 839 | void FindFirstSets(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 840 | { |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 841 | int i, j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 842 | struct rule *rp; |
| 843 | int progress; |
| 844 | |
| 845 | for(i=0; i<lemp->nsymbol; i++){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 846 | lemp->symbols[i]->lambda = LEMON_FALSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 847 | } |
| 848 | for(i=lemp->nterminal; i<lemp->nsymbol; i++){ |
| 849 | lemp->symbols[i]->firstset = SetNew(); |
| 850 | } |
| 851 | |
| 852 | /* First compute all lambdas */ |
| 853 | do{ |
| 854 | progress = 0; |
| 855 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 856 | if( rp->lhs->lambda ) continue; |
| 857 | for(i=0; i<rp->nrhs; i++){ |
drh | 7dd1ac6 | 2012-01-07 15:17:18 +0000 | [diff] [blame] | 858 | struct symbol *sp = rp->rhs[i]; |
| 859 | assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE ); |
| 860 | if( sp->lambda==LEMON_FALSE ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 861 | } |
| 862 | if( i==rp->nrhs ){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 863 | rp->lhs->lambda = LEMON_TRUE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 864 | progress = 1; |
| 865 | } |
| 866 | } |
| 867 | }while( progress ); |
| 868 | |
| 869 | /* Now compute all first sets */ |
| 870 | do{ |
| 871 | struct symbol *s1, *s2; |
| 872 | progress = 0; |
| 873 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 874 | s1 = rp->lhs; |
| 875 | for(i=0; i<rp->nrhs; i++){ |
| 876 | s2 = rp->rhs[i]; |
| 877 | if( s2->type==TERMINAL ){ |
| 878 | progress += SetAdd(s1->firstset,s2->index); |
| 879 | break; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 880 | }else if( s2->type==MULTITERMINAL ){ |
| 881 | for(j=0; j<s2->nsubsym; j++){ |
| 882 | progress += SetAdd(s1->firstset,s2->subsym[j]->index); |
| 883 | } |
| 884 | break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 885 | }else if( s1==s2 ){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 886 | if( s1->lambda==LEMON_FALSE ) break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 887 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 888 | progress += SetUnion(s1->firstset,s2->firstset); |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 889 | if( s2->lambda==LEMON_FALSE ) break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 890 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | }while( progress ); |
| 894 | return; |
| 895 | } |
| 896 | |
| 897 | /* Compute all LR(0) states for the grammar. Links |
| 898 | ** are added to between some states so that the LR(1) follow sets |
| 899 | ** can be computed later. |
| 900 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 901 | PRIVATE struct state *getstate(struct lemon *); /* forward reference */ |
| 902 | void FindStates(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 903 | { |
| 904 | struct symbol *sp; |
| 905 | struct rule *rp; |
| 906 | |
| 907 | Configlist_init(); |
| 908 | |
| 909 | /* Find the start symbol */ |
| 910 | if( lemp->start ){ |
| 911 | sp = Symbol_find(lemp->start); |
| 912 | if( sp==0 ){ |
| 913 | ErrorMsg(lemp->filename,0, |
drh | 3ecc05b | 2019-12-12 00:20:40 +0000 | [diff] [blame] | 914 | "The specified start symbol \"%s\" is not " |
| 915 | "in a nonterminal of the grammar. \"%s\" will be used as the start " |
| 916 | "symbol instead.",lemp->start,lemp->startRule->lhs->name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 917 | lemp->errorcnt++; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 918 | sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 919 | } |
| 920 | }else{ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 921 | sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /* Make sure the start symbol doesn't occur on the right-hand side of |
| 925 | ** any rule. Report an error if it does. (YACC would generate a new |
| 926 | ** start symbol in this case.) */ |
| 927 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 928 | int i; |
| 929 | for(i=0; i<rp->nrhs; i++){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 930 | if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 931 | ErrorMsg(lemp->filename,0, |
drh | 3ecc05b | 2019-12-12 00:20:40 +0000 | [diff] [blame] | 932 | "The start symbol \"%s\" occurs on the " |
| 933 | "right-hand side of a rule. This will result in a parser which " |
| 934 | "does not work properly.",sp->name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 935 | lemp->errorcnt++; |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /* The basis configuration set for the first state |
| 941 | ** is all rules which have the start symbol as their |
| 942 | ** left-hand side */ |
| 943 | for(rp=sp->rule; rp; rp=rp->nextlhs){ |
| 944 | struct config *newcfp; |
drh | b496099 | 2007-10-05 16:16:36 +0000 | [diff] [blame] | 945 | rp->lhsStart = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 946 | newcfp = Configlist_addbasis(rp,0); |
| 947 | SetAdd(newcfp->fws,0); |
| 948 | } |
| 949 | |
| 950 | /* Compute the first state. All other states will be |
| 951 | ** computed automatically during the computation of the first one. |
| 952 | ** The returned pointer to the first state is not used. */ |
| 953 | (void)getstate(lemp); |
| 954 | return; |
| 955 | } |
| 956 | |
| 957 | /* Return a pointer to a state which is described by the configuration |
| 958 | ** list which has been built from calls to Configlist_add. |
| 959 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 960 | PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */ |
| 961 | PRIVATE struct state *getstate(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 962 | { |
| 963 | struct config *cfp, *bp; |
| 964 | struct state *stp; |
| 965 | |
| 966 | /* Extract the sorted basis of the new state. The basis was constructed |
| 967 | ** by prior calls to "Configlist_addbasis()". */ |
| 968 | Configlist_sortbasis(); |
| 969 | bp = Configlist_basis(); |
| 970 | |
| 971 | /* Get a state with the same basis */ |
| 972 | stp = State_find(bp); |
| 973 | if( stp ){ |
| 974 | /* A state with the same basis already exists! Copy all the follow-set |
| 975 | ** propagation links from the state under construction into the |
| 976 | ** preexisting state, then return a pointer to the preexisting state */ |
| 977 | struct config *x, *y; |
| 978 | for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){ |
| 979 | Plink_copy(&y->bplp,x->bplp); |
| 980 | Plink_delete(x->fplp); |
| 981 | x->fplp = x->bplp = 0; |
| 982 | } |
| 983 | cfp = Configlist_return(); |
| 984 | Configlist_eat(cfp); |
| 985 | }else{ |
| 986 | /* This really is a new state. Construct all the details */ |
| 987 | Configlist_closure(lemp); /* Compute the configuration closure */ |
| 988 | Configlist_sort(); /* Sort the configuration closure */ |
| 989 | cfp = Configlist_return(); /* Get a pointer to the config list */ |
| 990 | stp = State_new(); /* A new state structure */ |
| 991 | MemoryCheck(stp); |
| 992 | stp->bp = bp; /* Remember the configuration basis */ |
| 993 | stp->cfp = cfp; /* Remember the configuration closure */ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 994 | stp->statenum = lemp->nstate++; /* Every state gets a sequence number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 995 | stp->ap = 0; /* No actions, yet. */ |
| 996 | State_insert(stp,stp->bp); /* Add to the state table */ |
| 997 | buildshifts(lemp,stp); /* Recursively compute successor states */ |
| 998 | } |
| 999 | return stp; |
| 1000 | } |
| 1001 | |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1002 | /* |
| 1003 | ** Return true if two symbols are the same. |
| 1004 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1005 | int same_symbol(struct symbol *a, struct symbol *b) |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1006 | { |
| 1007 | int i; |
| 1008 | if( a==b ) return 1; |
| 1009 | if( a->type!=MULTITERMINAL ) return 0; |
| 1010 | if( b->type!=MULTITERMINAL ) return 0; |
| 1011 | if( a->nsubsym!=b->nsubsym ) return 0; |
| 1012 | for(i=0; i<a->nsubsym; i++){ |
| 1013 | if( a->subsym[i]!=b->subsym[i] ) return 0; |
| 1014 | } |
| 1015 | return 1; |
| 1016 | } |
| 1017 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1018 | /* Construct all successor states to the given state. A "successor" |
| 1019 | ** state is any state which can be reached by a shift action. |
| 1020 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1021 | PRIVATE void buildshifts(struct lemon *lemp, struct state *stp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1022 | { |
| 1023 | struct config *cfp; /* For looping thru the config closure of "stp" */ |
| 1024 | struct config *bcfp; /* For the inner loop on config closure of "stp" */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1025 | struct config *newcfg; /* */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1026 | struct symbol *sp; /* Symbol following the dot in configuration "cfp" */ |
| 1027 | struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */ |
| 1028 | struct state *newstp; /* A pointer to a successor state */ |
| 1029 | |
| 1030 | /* Each configuration becomes complete after it contibutes to a successor |
| 1031 | ** state. Initially, all configurations are incomplete */ |
| 1032 | for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE; |
| 1033 | |
| 1034 | /* Loop through all configurations of the state "stp" */ |
| 1035 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| 1036 | if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */ |
| 1037 | if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */ |
| 1038 | Configlist_reset(); /* Reset the new config set */ |
| 1039 | sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */ |
| 1040 | |
| 1041 | /* For every configuration in the state "stp" which has the symbol "sp" |
| 1042 | ** following its dot, add the same configuration to the basis set under |
| 1043 | ** construction but with the dot shifted one symbol to the right. */ |
| 1044 | for(bcfp=cfp; bcfp; bcfp=bcfp->next){ |
| 1045 | if( bcfp->status==COMPLETE ) continue; /* Already used */ |
| 1046 | if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */ |
| 1047 | bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1048 | if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1049 | bcfp->status = COMPLETE; /* Mark this config as used */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1050 | newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1); |
| 1051 | Plink_add(&newcfg->bplp,bcfp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* Get a pointer to the state described by the basis configuration set |
| 1055 | ** constructed in the preceding loop */ |
| 1056 | newstp = getstate(lemp); |
| 1057 | |
| 1058 | /* The state "newstp" is reached from the state "stp" by a shift action |
| 1059 | ** on the symbol "sp" */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1060 | if( sp->type==MULTITERMINAL ){ |
| 1061 | int i; |
| 1062 | for(i=0; i<sp->nsubsym; i++){ |
| 1063 | Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp); |
| 1064 | } |
| 1065 | }else{ |
| 1066 | Action_add(&stp->ap,SHIFT,sp,(char *)newstp); |
| 1067 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | ** Construct the propagation links |
| 1073 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1074 | void FindLinks(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1075 | { |
| 1076 | int i; |
| 1077 | struct config *cfp, *other; |
| 1078 | struct state *stp; |
| 1079 | struct plink *plp; |
| 1080 | |
| 1081 | /* Housekeeping detail: |
| 1082 | ** Add to every propagate link a pointer back to the state to |
| 1083 | ** which the link is attached. */ |
| 1084 | for(i=0; i<lemp->nstate; i++){ |
| 1085 | stp = lemp->sorted[i]; |
| 1086 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| 1087 | cfp->stp = stp; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /* Convert all backlinks into forward links. Only the forward |
| 1092 | ** links are used in the follow-set computation. */ |
| 1093 | for(i=0; i<lemp->nstate; i++){ |
| 1094 | stp = lemp->sorted[i]; |
| 1095 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| 1096 | for(plp=cfp->bplp; plp; plp=plp->next){ |
| 1097 | other = plp->cfp; |
| 1098 | Plink_add(&other->fplp,cfp); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /* Compute all followsets. |
| 1105 | ** |
| 1106 | ** A followset is the set of all symbols which can come immediately |
| 1107 | ** after a configuration. |
| 1108 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1109 | void FindFollowSets(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1110 | { |
| 1111 | int i; |
| 1112 | struct config *cfp; |
| 1113 | struct plink *plp; |
| 1114 | int progress; |
| 1115 | int change; |
| 1116 | |
| 1117 | for(i=0; i<lemp->nstate; i++){ |
| 1118 | for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ |
| 1119 | cfp->status = INCOMPLETE; |
| 1120 | } |
| 1121 | } |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1122 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1123 | do{ |
| 1124 | progress = 0; |
| 1125 | for(i=0; i<lemp->nstate; i++){ |
| 1126 | for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ |
| 1127 | if( cfp->status==COMPLETE ) continue; |
| 1128 | for(plp=cfp->fplp; plp; plp=plp->next){ |
| 1129 | change = SetUnion(plp->cfp->fws,cfp->fws); |
| 1130 | if( change ){ |
| 1131 | plp->cfp->status = INCOMPLETE; |
| 1132 | progress = 1; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1133 | } |
| 1134 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1135 | cfp->status = COMPLETE; |
| 1136 | } |
| 1137 | } |
| 1138 | }while( progress ); |
| 1139 | } |
| 1140 | |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 1141 | static int resolve_conflict(struct action *,struct action *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1142 | |
| 1143 | /* Compute the reduce actions, and resolve conflicts. |
| 1144 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1145 | void FindActions(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1146 | { |
| 1147 | int i,j; |
| 1148 | struct config *cfp; |
| 1149 | struct state *stp; |
| 1150 | struct symbol *sp; |
| 1151 | struct rule *rp; |
| 1152 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1153 | /* Add all of the reduce actions |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1154 | ** A reduce action is added for each element of the followset of |
| 1155 | ** a configuration which has its dot at the extreme right. |
| 1156 | */ |
| 1157 | for(i=0; i<lemp->nstate; i++){ /* Loop over all states */ |
| 1158 | stp = lemp->sorted[i]; |
| 1159 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */ |
| 1160 | if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */ |
| 1161 | for(j=0; j<lemp->nterminal; j++){ |
| 1162 | if( SetFind(cfp->fws,j) ){ |
| 1163 | /* Add a reduce action to the state "stp" which will reduce by the |
| 1164 | ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */ |
drh | 218dc69 | 2004-05-31 23:13:45 +0000 | [diff] [blame] | 1165 | Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1166 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1167 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | /* Add the accepting token */ |
| 1173 | if( lemp->start ){ |
| 1174 | sp = Symbol_find(lemp->start); |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1175 | if( sp==0 ) sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1176 | }else{ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1177 | sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1178 | } |
| 1179 | /* Add to the first state (which is always the starting state of the |
| 1180 | ** finite state machine) an action to ACCEPT if the lookahead is the |
| 1181 | ** start nonterminal. */ |
| 1182 | Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0); |
| 1183 | |
| 1184 | /* Resolve conflicts */ |
| 1185 | for(i=0; i<lemp->nstate; i++){ |
| 1186 | struct action *ap, *nap; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1187 | stp = lemp->sorted[i]; |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 1188 | /* assert( stp->ap ); */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1189 | stp->ap = Action_sort(stp->ap); |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1190 | for(ap=stp->ap; ap && ap->next; ap=ap->next){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1191 | for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){ |
| 1192 | /* The two actions "ap" and "nap" have the same lookahead. |
| 1193 | ** Figure out which one should be used */ |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 1194 | lemp->nconflict += resolve_conflict(ap,nap); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | /* Report an error for each rule that can never be reduced. */ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 1200 | for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1201 | for(i=0; i<lemp->nstate; i++){ |
| 1202 | struct action *ap; |
| 1203 | for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 1204 | if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 1208 | if( rp->canReduce ) continue; |
| 1209 | ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n"); |
| 1210 | lemp->errorcnt++; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | /* Resolve a conflict between the two given actions. If the |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 1215 | ** conflict can't be resolved, return non-zero. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1216 | ** |
| 1217 | ** NO LONGER TRUE: |
| 1218 | ** To resolve a conflict, first look to see if either action |
| 1219 | ** is on an error rule. In that case, take the action which |
| 1220 | ** is not associated with the error rule. If neither or both |
| 1221 | ** actions are associated with an error rule, then try to |
| 1222 | ** use precedence to resolve the conflict. |
| 1223 | ** |
| 1224 | ** If either action is a SHIFT, then it must be apx. This |
| 1225 | ** function won't work if apx->type==REDUCE and apy->type==SHIFT. |
| 1226 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1227 | static int resolve_conflict( |
| 1228 | struct action *apx, |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 1229 | struct action *apy |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1230 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1231 | struct symbol *spx, *spy; |
| 1232 | int errcnt = 0; |
| 1233 | assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */ |
drh | f0fa1c1 | 2006-12-14 01:06:22 +0000 | [diff] [blame] | 1234 | if( apx->type==SHIFT && apy->type==SHIFT ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1235 | apy->type = SSCONFLICT; |
drh | f0fa1c1 | 2006-12-14 01:06:22 +0000 | [diff] [blame] | 1236 | errcnt++; |
| 1237 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1238 | if( apx->type==SHIFT && apy->type==REDUCE ){ |
| 1239 | spx = apx->sp; |
| 1240 | spy = apy->x.rp->precsym; |
| 1241 | if( spy==0 || spx->prec<0 || spy->prec<0 ){ |
| 1242 | /* Not enough precedence information. */ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1243 | apy->type = SRCONFLICT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1244 | errcnt++; |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1245 | }else if( spx->prec>spy->prec ){ /* higher precedence wins */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1246 | apy->type = RD_RESOLVED; |
| 1247 | }else if( spx->prec<spy->prec ){ |
| 1248 | apx->type = SH_RESOLVED; |
| 1249 | }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */ |
| 1250 | apy->type = RD_RESOLVED; /* associativity */ |
| 1251 | }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */ |
| 1252 | apx->type = SH_RESOLVED; |
| 1253 | }else{ |
| 1254 | assert( spx->prec==spy->prec && spx->assoc==NONE ); |
drh | 62a223e | 2014-06-09 13:11:40 +0000 | [diff] [blame] | 1255 | apx->type = ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1256 | } |
| 1257 | }else if( apx->type==REDUCE && apy->type==REDUCE ){ |
| 1258 | spx = apx->x.rp->precsym; |
| 1259 | spy = apy->x.rp->precsym; |
| 1260 | if( spx==0 || spy==0 || spx->prec<0 || |
| 1261 | spy->prec<0 || spx->prec==spy->prec ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1262 | apy->type = RRCONFLICT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1263 | errcnt++; |
| 1264 | }else if( spx->prec>spy->prec ){ |
| 1265 | apy->type = RD_RESOLVED; |
| 1266 | }else if( spx->prec<spy->prec ){ |
| 1267 | apx->type = RD_RESOLVED; |
| 1268 | } |
| 1269 | }else{ |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1270 | assert( |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1271 | apx->type==SH_RESOLVED || |
| 1272 | apx->type==RD_RESOLVED || |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1273 | apx->type==SSCONFLICT || |
| 1274 | apx->type==SRCONFLICT || |
| 1275 | apx->type==RRCONFLICT || |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1276 | apy->type==SH_RESOLVED || |
| 1277 | apy->type==RD_RESOLVED || |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1278 | apy->type==SSCONFLICT || |
| 1279 | apy->type==SRCONFLICT || |
| 1280 | apy->type==RRCONFLICT |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1281 | ); |
| 1282 | /* The REDUCE/SHIFT case cannot happen because SHIFTs come before |
| 1283 | ** REDUCEs on the list. If we reach this point it must be because |
| 1284 | ** the parser conflict had already been resolved. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1285 | } |
| 1286 | return errcnt; |
| 1287 | } |
| 1288 | /********************* From the file "configlist.c" *************************/ |
| 1289 | /* |
| 1290 | ** Routines to processing a configuration list and building a state |
| 1291 | ** in the LEMON parser generator. |
| 1292 | */ |
| 1293 | |
| 1294 | static struct config *freelist = 0; /* List of free configurations */ |
| 1295 | static struct config *current = 0; /* Top of list of configurations */ |
| 1296 | static struct config **currentend = 0; /* Last on list of configs */ |
| 1297 | static struct config *basis = 0; /* Top of list of basis configs */ |
| 1298 | static struct config **basisend = 0; /* End of list of basis configs */ |
| 1299 | |
| 1300 | /* Return a pointer to a new configuration */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1301 | PRIVATE struct config *newconfig(void){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1302 | struct config *newcfg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1303 | if( freelist==0 ){ |
| 1304 | int i; |
| 1305 | int amt = 3; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1306 | freelist = (struct config *)calloc( amt, sizeof(struct config) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1307 | if( freelist==0 ){ |
| 1308 | fprintf(stderr,"Unable to allocate memory for a new configuration."); |
| 1309 | exit(1); |
| 1310 | } |
| 1311 | for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1]; |
| 1312 | freelist[amt-1].next = 0; |
| 1313 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1314 | newcfg = freelist; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1315 | freelist = freelist->next; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1316 | return newcfg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | /* The configuration "old" is no longer used */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1320 | PRIVATE void deleteconfig(struct config *old) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1321 | { |
| 1322 | old->next = freelist; |
| 1323 | freelist = old; |
| 1324 | } |
| 1325 | |
| 1326 | /* Initialized the configuration list builder */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1327 | void Configlist_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1328 | current = 0; |
| 1329 | currentend = ¤t; |
| 1330 | basis = 0; |
| 1331 | basisend = &basis; |
| 1332 | Configtable_init(); |
| 1333 | return; |
| 1334 | } |
| 1335 | |
| 1336 | /* Initialized the configuration list builder */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1337 | void Configlist_reset(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1338 | current = 0; |
| 1339 | currentend = ¤t; |
| 1340 | basis = 0; |
| 1341 | basisend = &basis; |
| 1342 | Configtable_clear(0); |
| 1343 | return; |
| 1344 | } |
| 1345 | |
| 1346 | /* Add another configuration to the configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1347 | struct config *Configlist_add( |
| 1348 | struct rule *rp, /* The rule */ |
| 1349 | int dot /* Index into the RHS of the rule where the dot goes */ |
| 1350 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1351 | struct config *cfp, model; |
| 1352 | |
| 1353 | assert( currentend!=0 ); |
| 1354 | model.rp = rp; |
| 1355 | model.dot = dot; |
| 1356 | cfp = Configtable_find(&model); |
| 1357 | if( cfp==0 ){ |
| 1358 | cfp = newconfig(); |
| 1359 | cfp->rp = rp; |
| 1360 | cfp->dot = dot; |
| 1361 | cfp->fws = SetNew(); |
| 1362 | cfp->stp = 0; |
| 1363 | cfp->fplp = cfp->bplp = 0; |
| 1364 | cfp->next = 0; |
| 1365 | cfp->bp = 0; |
| 1366 | *currentend = cfp; |
| 1367 | currentend = &cfp->next; |
| 1368 | Configtable_insert(cfp); |
| 1369 | } |
| 1370 | return cfp; |
| 1371 | } |
| 1372 | |
| 1373 | /* Add a basis configuration to the configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1374 | struct config *Configlist_addbasis(struct rule *rp, int dot) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1375 | { |
| 1376 | struct config *cfp, model; |
| 1377 | |
| 1378 | assert( basisend!=0 ); |
| 1379 | assert( currentend!=0 ); |
| 1380 | model.rp = rp; |
| 1381 | model.dot = dot; |
| 1382 | cfp = Configtable_find(&model); |
| 1383 | if( cfp==0 ){ |
| 1384 | cfp = newconfig(); |
| 1385 | cfp->rp = rp; |
| 1386 | cfp->dot = dot; |
| 1387 | cfp->fws = SetNew(); |
| 1388 | cfp->stp = 0; |
| 1389 | cfp->fplp = cfp->bplp = 0; |
| 1390 | cfp->next = 0; |
| 1391 | cfp->bp = 0; |
| 1392 | *currentend = cfp; |
| 1393 | currentend = &cfp->next; |
| 1394 | *basisend = cfp; |
| 1395 | basisend = &cfp->bp; |
| 1396 | Configtable_insert(cfp); |
| 1397 | } |
| 1398 | return cfp; |
| 1399 | } |
| 1400 | |
| 1401 | /* Compute the closure of the configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1402 | void Configlist_closure(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1403 | { |
| 1404 | struct config *cfp, *newcfp; |
| 1405 | struct rule *rp, *newrp; |
| 1406 | struct symbol *sp, *xsp; |
| 1407 | int i, dot; |
| 1408 | |
| 1409 | assert( currentend!=0 ); |
| 1410 | for(cfp=current; cfp; cfp=cfp->next){ |
| 1411 | rp = cfp->rp; |
| 1412 | dot = cfp->dot; |
| 1413 | if( dot>=rp->nrhs ) continue; |
| 1414 | sp = rp->rhs[dot]; |
| 1415 | if( sp->type==NONTERMINAL ){ |
| 1416 | if( sp->rule==0 && sp!=lemp->errsym ){ |
| 1417 | ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.", |
| 1418 | sp->name); |
| 1419 | lemp->errorcnt++; |
| 1420 | } |
| 1421 | for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){ |
| 1422 | newcfp = Configlist_add(newrp,0); |
| 1423 | for(i=dot+1; i<rp->nrhs; i++){ |
| 1424 | xsp = rp->rhs[i]; |
| 1425 | if( xsp->type==TERMINAL ){ |
| 1426 | SetAdd(newcfp->fws,xsp->index); |
| 1427 | break; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1428 | }else if( xsp->type==MULTITERMINAL ){ |
| 1429 | int k; |
| 1430 | for(k=0; k<xsp->nsubsym; k++){ |
| 1431 | SetAdd(newcfp->fws, xsp->subsym[k]->index); |
| 1432 | } |
| 1433 | break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1434 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1435 | SetUnion(newcfp->fws,xsp->firstset); |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 1436 | if( xsp->lambda==LEMON_FALSE ) break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1439 | if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp); |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | return; |
| 1444 | } |
| 1445 | |
| 1446 | /* Sort the configuration list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1447 | void Configlist_sort(void){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 1448 | current = (struct config*)msort((char*)current,(char**)&(current->next), |
| 1449 | Configcmp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1450 | currentend = 0; |
| 1451 | return; |
| 1452 | } |
| 1453 | |
| 1454 | /* Sort the basis configuration list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1455 | void Configlist_sortbasis(void){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 1456 | basis = (struct config*)msort((char*)current,(char**)&(current->bp), |
| 1457 | Configcmp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1458 | basisend = 0; |
| 1459 | return; |
| 1460 | } |
| 1461 | |
| 1462 | /* Return a pointer to the head of the configuration list and |
| 1463 | ** reset the list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1464 | struct config *Configlist_return(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1465 | struct config *old; |
| 1466 | old = current; |
| 1467 | current = 0; |
| 1468 | currentend = 0; |
| 1469 | return old; |
| 1470 | } |
| 1471 | |
| 1472 | /* Return a pointer to the head of the configuration list and |
| 1473 | ** reset the list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1474 | struct config *Configlist_basis(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1475 | struct config *old; |
| 1476 | old = basis; |
| 1477 | basis = 0; |
| 1478 | basisend = 0; |
| 1479 | return old; |
| 1480 | } |
| 1481 | |
| 1482 | /* Free all elements of the given configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1483 | void Configlist_eat(struct config *cfp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1484 | { |
| 1485 | struct config *nextcfp; |
| 1486 | for(; cfp; cfp=nextcfp){ |
| 1487 | nextcfp = cfp->next; |
| 1488 | assert( cfp->fplp==0 ); |
| 1489 | assert( cfp->bplp==0 ); |
| 1490 | if( cfp->fws ) SetFree(cfp->fws); |
| 1491 | deleteconfig(cfp); |
| 1492 | } |
| 1493 | return; |
| 1494 | } |
| 1495 | /***************** From the file "error.c" *********************************/ |
| 1496 | /* |
| 1497 | ** Code for printing error message. |
| 1498 | */ |
| 1499 | |
drh | f9a2e7b | 2003-04-15 01:49:48 +0000 | [diff] [blame] | 1500 | void ErrorMsg(const char *filename, int lineno, const char *format, ...){ |
icculus | 15a2cec | 2010-02-16 16:07:28 +0000 | [diff] [blame] | 1501 | va_list ap; |
icculus | 1c11f74 | 2010-02-15 00:01:04 +0000 | [diff] [blame] | 1502 | fprintf(stderr, "%s:%d: ", filename, lineno); |
| 1503 | va_start(ap, format); |
| 1504 | vfprintf(stderr,format,ap); |
| 1505 | va_end(ap); |
| 1506 | fprintf(stderr, "\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1507 | } |
| 1508 | /**************** From the file "main.c" ************************************/ |
| 1509 | /* |
| 1510 | ** Main program file for the LEMON parser generator. |
| 1511 | */ |
| 1512 | |
| 1513 | /* Report an out-of-memory condition and abort. This function |
| 1514 | ** is used mostly by the "MemoryCheck" macro in struct.h |
| 1515 | */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1516 | void memory_error(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1517 | fprintf(stderr,"Out of memory. Aborting...\n"); |
| 1518 | exit(1); |
| 1519 | } |
| 1520 | |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1521 | static int nDefine = 0; /* Number of -D options on the command line */ |
| 1522 | static char **azDefine = 0; /* Name of the -D macros */ |
| 1523 | |
| 1524 | /* This routine is called with the argument to each -D command-line option. |
| 1525 | ** Add the macro defined to the azDefine array. |
| 1526 | */ |
| 1527 | static void handle_D_option(char *z){ |
| 1528 | char **paz; |
| 1529 | nDefine++; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1530 | azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1531 | if( azDefine==0 ){ |
| 1532 | fprintf(stderr,"out of memory\n"); |
| 1533 | exit(1); |
| 1534 | } |
| 1535 | paz = &azDefine[nDefine-1]; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1536 | *paz = (char *) malloc( lemonStrlen(z)+1 ); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1537 | if( *paz==0 ){ |
| 1538 | fprintf(stderr,"out of memory\n"); |
| 1539 | exit(1); |
| 1540 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 1541 | lemon_strcpy(*paz, z); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1542 | for(z=*paz; *z && *z!='='; z++){} |
| 1543 | *z = 0; |
| 1544 | } |
| 1545 | |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 1546 | /* Rember the name of the output directory |
| 1547 | */ |
| 1548 | static char *outputDir = NULL; |
| 1549 | static void handle_d_option(char *z){ |
| 1550 | outputDir = (char *) malloc( lemonStrlen(z)+1 ); |
| 1551 | if( outputDir==0 ){ |
| 1552 | fprintf(stderr,"out of memory\n"); |
| 1553 | exit(1); |
| 1554 | } |
| 1555 | lemon_strcpy(outputDir, z); |
| 1556 | } |
| 1557 | |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 1558 | static char *user_templatename = NULL; |
| 1559 | static void handle_T_option(char *z){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1560 | user_templatename = (char *) malloc( lemonStrlen(z)+1 ); |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 1561 | if( user_templatename==0 ){ |
| 1562 | memory_error(); |
| 1563 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 1564 | lemon_strcpy(user_templatename, z); |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 1565 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1566 | |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 1567 | /* Merge together to lists of rules ordered by rule.iRule */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1568 | static struct rule *Rule_merge(struct rule *pA, struct rule *pB){ |
| 1569 | struct rule *pFirst = 0; |
| 1570 | struct rule **ppPrev = &pFirst; |
| 1571 | while( pA && pB ){ |
| 1572 | if( pA->iRule<pB->iRule ){ |
| 1573 | *ppPrev = pA; |
| 1574 | ppPrev = &pA->next; |
| 1575 | pA = pA->next; |
| 1576 | }else{ |
| 1577 | *ppPrev = pB; |
| 1578 | ppPrev = &pB->next; |
| 1579 | pB = pB->next; |
| 1580 | } |
| 1581 | } |
| 1582 | if( pA ){ |
| 1583 | *ppPrev = pA; |
| 1584 | }else{ |
| 1585 | *ppPrev = pB; |
| 1586 | } |
| 1587 | return pFirst; |
| 1588 | } |
| 1589 | |
| 1590 | /* |
| 1591 | ** Sort a list of rules in order of increasing iRule value |
| 1592 | */ |
| 1593 | static struct rule *Rule_sort(struct rule *rp){ |
drh | 18ef40e | 2020-09-16 16:55:56 +0000 | [diff] [blame^] | 1594 | unsigned int i; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1595 | struct rule *pNext; |
| 1596 | struct rule *x[32]; |
| 1597 | memset(x, 0, sizeof(x)); |
| 1598 | while( rp ){ |
| 1599 | pNext = rp->next; |
| 1600 | rp->next = 0; |
| 1601 | for(i=0; i<sizeof(x)/sizeof(x[0]) && x[i]; i++){ |
| 1602 | rp = Rule_merge(x[i], rp); |
| 1603 | x[i] = 0; |
| 1604 | } |
| 1605 | x[i] = rp; |
| 1606 | rp = pNext; |
| 1607 | } |
| 1608 | rp = 0; |
| 1609 | for(i=0; i<sizeof(x)/sizeof(x[0]); i++){ |
| 1610 | rp = Rule_merge(x[i], rp); |
| 1611 | } |
| 1612 | return rp; |
| 1613 | } |
| 1614 | |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1615 | /* forward reference */ |
| 1616 | static const char *minimum_size_type(int lwr, int upr, int *pnByte); |
| 1617 | |
| 1618 | /* Print a single line of the "Parser Stats" output |
| 1619 | */ |
| 1620 | static void stats_line(const char *zLabel, int iValue){ |
| 1621 | int nLabel = lemonStrlen(zLabel); |
| 1622 | printf(" %s%.*s %5d\n", zLabel, |
| 1623 | 35-nLabel, "................................", |
| 1624 | iValue); |
| 1625 | } |
| 1626 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1627 | /* The main program. Parse the command line and do it... */ |
drh | 18ef40e | 2020-09-16 16:55:56 +0000 | [diff] [blame^] | 1628 | int main(int argc, char **argv){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1629 | static int version = 0; |
| 1630 | static int rpflag = 0; |
| 1631 | static int basisflag = 0; |
| 1632 | static int compress = 0; |
| 1633 | static int quiet = 0; |
| 1634 | static int statistics = 0; |
| 1635 | static int mhflag = 0; |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 1636 | static int nolinenosflag = 0; |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1637 | static int noResort = 0; |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 1638 | static int sqlFlag = 0; |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 1639 | static int printPP = 0; |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 1640 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1641 | static struct s_options options[] = { |
| 1642 | {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."}, |
| 1643 | {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."}, |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 1644 | {OPT_FSTR, "d", (char*)&handle_d_option, "Output directory. Default '.'"}, |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1645 | {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."}, |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 1646 | {OPT_FLAG, "E", (char*)&printPP, "Print input file after preprocessing."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1647 | {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1648 | {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1649 | {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"}, |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 1650 | {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."}, |
| 1651 | {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1652 | {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"}, |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 1653 | {OPT_FLAG, "p", (char*)&showPrecedenceConflict, |
| 1654 | "Show conflicts resolved by precedence rules"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1655 | {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."}, |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1656 | {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"}, |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1657 | {OPT_FLAG, "s", (char*)&statistics, |
| 1658 | "Print parser stats to standard output."}, |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 1659 | {OPT_FLAG, "S", (char*)&sqlFlag, |
| 1660 | "Generate the *.sql file describing the parser tables."}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1661 | {OPT_FLAG, "x", (char*)&version, "Print the version number."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1662 | {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."}, |
| 1663 | {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1664 | {OPT_FLAG,0,0,0} |
| 1665 | }; |
| 1666 | int i; |
icculus | 42585cf | 2010-02-14 05:19:56 +0000 | [diff] [blame] | 1667 | int exitcode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1668 | struct lemon lem; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1669 | struct rule *rp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1670 | |
drh | 18ef40e | 2020-09-16 16:55:56 +0000 | [diff] [blame^] | 1671 | (void)argc; |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 1672 | OptInit(argv,options,stderr); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1673 | if( version ){ |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1674 | printf("Lemon version 1.0\n"); |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1675 | exit(0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1676 | } |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 1677 | if( OptNArgs()!=1 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1678 | fprintf(stderr,"Exactly one filename argument is required.\n"); |
| 1679 | exit(1); |
| 1680 | } |
drh | 954f6b4 | 2006-06-13 13:27:46 +0000 | [diff] [blame] | 1681 | memset(&lem, 0, sizeof(lem)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1682 | lem.errorcnt = 0; |
| 1683 | |
| 1684 | /* Initialize the machine */ |
| 1685 | Strsafe_init(); |
| 1686 | Symbol_init(); |
| 1687 | State_init(); |
| 1688 | lem.argv0 = argv[0]; |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 1689 | lem.filename = OptArg(0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1690 | lem.basisflag = basisflag; |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 1691 | lem.nolinenosflag = nolinenosflag; |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 1692 | lem.printPreprocessed = printPP; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1693 | Symbol_new("$"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1694 | |
| 1695 | /* Parse the input file */ |
| 1696 | Parse(&lem); |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 1697 | if( lem.printPreprocessed || lem.errorcnt ) exit(lem.errorcnt); |
drh | 954f6b4 | 2006-06-13 13:27:46 +0000 | [diff] [blame] | 1698 | if( lem.nrule==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1699 | fprintf(stderr,"Empty grammar.\n"); |
| 1700 | exit(1); |
| 1701 | } |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 1702 | lem.errsym = Symbol_find("error"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1703 | |
| 1704 | /* Count and index the symbols of the grammar */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1705 | Symbol_new("{default}"); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 1706 | lem.nsymbol = Symbol_count(); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1707 | lem.symbols = Symbol_arrayof(); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 1708 | for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i; |
| 1709 | qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp); |
| 1710 | for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i; |
| 1711 | while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; } |
| 1712 | assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 ); |
| 1713 | lem.nsymbol = i - 1; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 1714 | for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1715 | lem.nterminal = i; |
| 1716 | |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 1717 | /* Assign sequential rule numbers. Start with 0. Put rules that have no |
| 1718 | ** reduce action C-code associated with them last, so that the switch() |
| 1719 | ** statement that selects reduction actions will have a smaller jump table. |
| 1720 | */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1721 | for(i=0, rp=lem.rule; rp; rp=rp->next){ |
| 1722 | rp->iRule = rp->code ? i++ : -1; |
| 1723 | } |
drh | ce678c2 | 2019-12-11 18:53:51 +0000 | [diff] [blame] | 1724 | lem.nruleWithAction = i; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1725 | for(rp=lem.rule; rp; rp=rp->next){ |
| 1726 | if( rp->iRule<0 ) rp->iRule = i++; |
| 1727 | } |
| 1728 | lem.startRule = lem.rule; |
| 1729 | lem.rule = Rule_sort(lem.rule); |
| 1730 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1731 | /* Generate a reprint of the grammar, if requested on the command line */ |
| 1732 | if( rpflag ){ |
| 1733 | Reprint(&lem); |
| 1734 | }else{ |
| 1735 | /* Initialize the size for all follow and first sets */ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1736 | SetSize(lem.nterminal+1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1737 | |
| 1738 | /* Find the precedence for every production rule (that has one) */ |
| 1739 | FindRulePrecedences(&lem); |
| 1740 | |
| 1741 | /* Compute the lambda-nonterminals and the first-sets for every |
| 1742 | ** nonterminal */ |
| 1743 | FindFirstSets(&lem); |
| 1744 | |
| 1745 | /* Compute all LR(0) states. Also record follow-set propagation |
| 1746 | ** links so that the follow-set can be computed later */ |
| 1747 | lem.nstate = 0; |
| 1748 | FindStates(&lem); |
| 1749 | lem.sorted = State_arrayof(); |
| 1750 | |
| 1751 | /* Tie up loose ends on the propagation links */ |
| 1752 | FindLinks(&lem); |
| 1753 | |
| 1754 | /* Compute the follow set of every reducible configuration */ |
| 1755 | FindFollowSets(&lem); |
| 1756 | |
| 1757 | /* Compute the action tables */ |
| 1758 | FindActions(&lem); |
| 1759 | |
| 1760 | /* Compress the action tables */ |
| 1761 | if( compress==0 ) CompressTables(&lem); |
| 1762 | |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 1763 | /* Reorder and renumber the states so that states with fewer choices |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1764 | ** occur at the end. This is an optimization that helps make the |
| 1765 | ** generated parser tables smaller. */ |
| 1766 | if( noResort==0 ) ResortStates(&lem); |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 1767 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1768 | /* Generate a report of the parser generated. (the "y.output" file) */ |
| 1769 | if( !quiet ) ReportOutput(&lem); |
| 1770 | |
| 1771 | /* Generate the source code for the parser */ |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 1772 | ReportTable(&lem, mhflag, sqlFlag); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1773 | |
| 1774 | /* Produce a header file for use by the scanner. (This step is |
| 1775 | ** omitted if the "-m" option is used because makeheaders will |
| 1776 | ** generate the file for us.) */ |
| 1777 | if( !mhflag ) ReportHeader(&lem); |
| 1778 | } |
| 1779 | if( statistics ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1780 | printf("Parser statistics:\n"); |
| 1781 | stats_line("terminal symbols", lem.nterminal); |
| 1782 | stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal); |
| 1783 | stats_line("total symbols", lem.nsymbol); |
| 1784 | stats_line("rules", lem.nrule); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 1785 | stats_line("states", lem.nxstate); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1786 | stats_line("conflicts", lem.nconflict); |
| 1787 | stats_line("action table entries", lem.nactiontab); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 1788 | stats_line("lookahead table entries", lem.nlookaheadtab); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1789 | stats_line("total table size (bytes)", lem.tablesize); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1790 | } |
icculus | 8e15802 | 2010-02-16 16:09:03 +0000 | [diff] [blame] | 1791 | if( lem.nconflict > 0 ){ |
| 1792 | fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict); |
icculus | 42585cf | 2010-02-14 05:19:56 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | /* return 0 on success, 1 on failure. */ |
icculus | 8e15802 | 2010-02-16 16:09:03 +0000 | [diff] [blame] | 1796 | exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0; |
icculus | 42585cf | 2010-02-14 05:19:56 +0000 | [diff] [blame] | 1797 | exit(exitcode); |
| 1798 | return (exitcode); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1799 | } |
| 1800 | /******************** From the file "msort.c" *******************************/ |
| 1801 | /* |
| 1802 | ** A generic merge-sort program. |
| 1803 | ** |
| 1804 | ** USAGE: |
| 1805 | ** Let "ptr" be a pointer to some structure which is at the head of |
| 1806 | ** a null-terminated list. Then to sort the list call: |
| 1807 | ** |
| 1808 | ** ptr = msort(ptr,&(ptr->next),cmpfnc); |
| 1809 | ** |
| 1810 | ** In the above, "cmpfnc" is a pointer to a function which compares |
| 1811 | ** two instances of the structure and returns an integer, as in |
| 1812 | ** strcmp. The second argument is a pointer to the pointer to the |
| 1813 | ** second element of the linked list. This address is used to compute |
| 1814 | ** the offset to the "next" field within the structure. The offset to |
| 1815 | ** the "next" field must be constant for all structures in the list. |
| 1816 | ** |
| 1817 | ** The function returns a new pointer which is the head of the list |
| 1818 | ** after sorting. |
| 1819 | ** |
| 1820 | ** ALGORITHM: |
| 1821 | ** Merge-sort. |
| 1822 | */ |
| 1823 | |
| 1824 | /* |
| 1825 | ** Return a pointer to the next structure in the linked list. |
| 1826 | */ |
drh | d25d692 | 2012-04-18 09:59:56 +0000 | [diff] [blame] | 1827 | #define NEXT(A) (*(char**)(((char*)A)+offset)) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1828 | |
| 1829 | /* |
| 1830 | ** Inputs: |
| 1831 | ** a: A sorted, null-terminated linked list. (May be null). |
| 1832 | ** b: A sorted, null-terminated linked list. (May be null). |
| 1833 | ** cmp: A pointer to the comparison function. |
| 1834 | ** offset: Offset in the structure to the "next" field. |
| 1835 | ** |
| 1836 | ** Return Value: |
| 1837 | ** A pointer to the head of a sorted list containing the elements |
| 1838 | ** of both a and b. |
| 1839 | ** |
| 1840 | ** Side effects: |
| 1841 | ** The "next" pointers for elements in the lists a and b are |
| 1842 | ** changed. |
| 1843 | */ |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 1844 | static char *merge( |
| 1845 | char *a, |
| 1846 | char *b, |
| 1847 | int (*cmp)(const char*,const char*), |
| 1848 | int offset |
| 1849 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1850 | char *ptr, *head; |
| 1851 | |
| 1852 | if( a==0 ){ |
| 1853 | head = b; |
| 1854 | }else if( b==0 ){ |
| 1855 | head = a; |
| 1856 | }else{ |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 1857 | if( (*cmp)(a,b)<=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1858 | ptr = a; |
| 1859 | a = NEXT(a); |
| 1860 | }else{ |
| 1861 | ptr = b; |
| 1862 | b = NEXT(b); |
| 1863 | } |
| 1864 | head = ptr; |
| 1865 | while( a && b ){ |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 1866 | if( (*cmp)(a,b)<=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1867 | NEXT(ptr) = a; |
| 1868 | ptr = a; |
| 1869 | a = NEXT(a); |
| 1870 | }else{ |
| 1871 | NEXT(ptr) = b; |
| 1872 | ptr = b; |
| 1873 | b = NEXT(b); |
| 1874 | } |
| 1875 | } |
| 1876 | if( a ) NEXT(ptr) = a; |
| 1877 | else NEXT(ptr) = b; |
| 1878 | } |
| 1879 | return head; |
| 1880 | } |
| 1881 | |
| 1882 | /* |
| 1883 | ** Inputs: |
| 1884 | ** list: Pointer to a singly-linked list of structures. |
| 1885 | ** next: Pointer to pointer to the second element of the list. |
| 1886 | ** cmp: A comparison function. |
| 1887 | ** |
| 1888 | ** Return Value: |
| 1889 | ** A pointer to the head of a sorted list containing the elements |
| 1890 | ** orginally in list. |
| 1891 | ** |
| 1892 | ** Side effects: |
| 1893 | ** The "next" pointers for elements in list are changed. |
| 1894 | */ |
| 1895 | #define LISTSIZE 30 |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 1896 | static char *msort( |
| 1897 | char *list, |
| 1898 | char **next, |
| 1899 | int (*cmp)(const char*,const char*) |
| 1900 | ){ |
drh | ba99af5 | 2001-10-25 20:37:16 +0000 | [diff] [blame] | 1901 | unsigned long offset; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1902 | char *ep; |
| 1903 | char *set[LISTSIZE]; |
| 1904 | int i; |
drh | 1cc0d11 | 2015-03-31 15:15:48 +0000 | [diff] [blame] | 1905 | offset = (unsigned long)((char*)next - (char*)list); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1906 | for(i=0; i<LISTSIZE; i++) set[i] = 0; |
| 1907 | while( list ){ |
| 1908 | ep = list; |
| 1909 | list = NEXT(list); |
| 1910 | NEXT(ep) = 0; |
| 1911 | for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){ |
| 1912 | ep = merge(ep,set[i],cmp,offset); |
| 1913 | set[i] = 0; |
| 1914 | } |
| 1915 | set[i] = ep; |
| 1916 | } |
| 1917 | ep = 0; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 1918 | 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] | 1919 | return ep; |
| 1920 | } |
| 1921 | /************************ From the file "option.c" **************************/ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1922 | static char **g_argv; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1923 | static struct s_options *op; |
| 1924 | static FILE *errstream; |
| 1925 | |
| 1926 | #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0) |
| 1927 | |
| 1928 | /* |
| 1929 | ** Print the command line with a carrot pointing to the k-th character |
| 1930 | ** of the n-th field. |
| 1931 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1932 | static void errline(int n, int k, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1933 | { |
| 1934 | int spcnt, i; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1935 | if( g_argv[0] ) fprintf(err,"%s",g_argv[0]); |
| 1936 | spcnt = lemonStrlen(g_argv[0]) + 1; |
| 1937 | for(i=1; i<n && g_argv[i]; i++){ |
| 1938 | fprintf(err," %s",g_argv[i]); |
| 1939 | spcnt += lemonStrlen(g_argv[i])+1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1940 | } |
| 1941 | spcnt += k; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1942 | for(; g_argv[i]; i++) fprintf(err," %s",g_argv[i]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1943 | if( spcnt<20 ){ |
| 1944 | fprintf(err,"\n%*s^-- here\n",spcnt,""); |
| 1945 | }else{ |
| 1946 | fprintf(err,"\n%*shere --^\n",spcnt-7,""); |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | /* |
| 1951 | ** Return the index of the N-th non-switch argument. Return -1 |
| 1952 | ** if N is out of range. |
| 1953 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1954 | static int argindex(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1955 | { |
| 1956 | int i; |
| 1957 | int dashdash = 0; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1958 | if( g_argv!=0 && *g_argv!=0 ){ |
| 1959 | for(i=1; g_argv[i]; i++){ |
| 1960 | if( dashdash || !ISOPT(g_argv[i]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1961 | if( n==0 ) return i; |
| 1962 | n--; |
| 1963 | } |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1964 | if( strcmp(g_argv[i],"--")==0 ) dashdash = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1965 | } |
| 1966 | } |
| 1967 | return -1; |
| 1968 | } |
| 1969 | |
| 1970 | static char emsg[] = "Command line syntax error: "; |
| 1971 | |
| 1972 | /* |
| 1973 | ** Process a flag command line argument. |
| 1974 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1975 | static int handleflags(int i, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1976 | { |
| 1977 | int v; |
| 1978 | int errcnt = 0; |
| 1979 | int j; |
| 1980 | for(j=0; op[j].label; j++){ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1981 | if( strncmp(&g_argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1982 | } |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1983 | v = g_argv[i][0]=='-' ? 1 : 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1984 | if( op[j].label==0 ){ |
| 1985 | if( err ){ |
| 1986 | fprintf(err,"%sundefined option.\n",emsg); |
| 1987 | errline(i,1,err); |
| 1988 | } |
| 1989 | errcnt++; |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1990 | }else if( op[j].arg==0 ){ |
| 1991 | /* Ignore this option */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1992 | }else if( op[j].type==OPT_FLAG ){ |
| 1993 | *((int*)op[j].arg) = v; |
| 1994 | }else if( op[j].type==OPT_FFLAG ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1995 | (*(void(*)(int))(op[j].arg))(v); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1996 | }else if( op[j].type==OPT_FSTR ){ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1997 | (*(void(*)(char *))(op[j].arg))(&g_argv[i][2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1998 | }else{ |
| 1999 | if( err ){ |
| 2000 | fprintf(err,"%smissing argument on switch.\n",emsg); |
| 2001 | errline(i,1,err); |
| 2002 | } |
| 2003 | errcnt++; |
| 2004 | } |
| 2005 | return errcnt; |
| 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | ** Process a command line switch which has an argument. |
| 2010 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2011 | static int handleswitch(int i, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2012 | { |
| 2013 | int lv = 0; |
| 2014 | double dv = 0.0; |
| 2015 | char *sv = 0, *end; |
| 2016 | char *cp; |
| 2017 | int j; |
| 2018 | int errcnt = 0; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2019 | cp = strchr(g_argv[i],'='); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 2020 | assert( cp!=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2021 | *cp = 0; |
| 2022 | for(j=0; op[j].label; j++){ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2023 | if( strcmp(g_argv[i],op[j].label)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2024 | } |
| 2025 | *cp = '='; |
| 2026 | if( op[j].label==0 ){ |
| 2027 | if( err ){ |
| 2028 | fprintf(err,"%sundefined option.\n",emsg); |
| 2029 | errline(i,0,err); |
| 2030 | } |
| 2031 | errcnt++; |
| 2032 | }else{ |
| 2033 | cp++; |
| 2034 | switch( op[j].type ){ |
| 2035 | case OPT_FLAG: |
| 2036 | case OPT_FFLAG: |
| 2037 | if( err ){ |
| 2038 | fprintf(err,"%soption requires an argument.\n",emsg); |
| 2039 | errline(i,0,err); |
| 2040 | } |
| 2041 | errcnt++; |
| 2042 | break; |
| 2043 | case OPT_DBL: |
| 2044 | case OPT_FDBL: |
| 2045 | dv = strtod(cp,&end); |
| 2046 | if( *end ){ |
| 2047 | if( err ){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 2048 | fprintf(err, |
| 2049 | "%sillegal character in floating-point argument.\n",emsg); |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2050 | errline(i,(int)((char*)end-(char*)g_argv[i]),err); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2051 | } |
| 2052 | errcnt++; |
| 2053 | } |
| 2054 | break; |
| 2055 | case OPT_INT: |
| 2056 | case OPT_FINT: |
| 2057 | lv = strtol(cp,&end,0); |
| 2058 | if( *end ){ |
| 2059 | if( err ){ |
| 2060 | fprintf(err,"%sillegal character in integer argument.\n",emsg); |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2061 | errline(i,(int)((char*)end-(char*)g_argv[i]),err); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2062 | } |
| 2063 | errcnt++; |
| 2064 | } |
| 2065 | break; |
| 2066 | case OPT_STR: |
| 2067 | case OPT_FSTR: |
| 2068 | sv = cp; |
| 2069 | break; |
| 2070 | } |
| 2071 | switch( op[j].type ){ |
| 2072 | case OPT_FLAG: |
| 2073 | case OPT_FFLAG: |
| 2074 | break; |
| 2075 | case OPT_DBL: |
| 2076 | *(double*)(op[j].arg) = dv; |
| 2077 | break; |
| 2078 | case OPT_FDBL: |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2079 | (*(void(*)(double))(op[j].arg))(dv); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2080 | break; |
| 2081 | case OPT_INT: |
| 2082 | *(int*)(op[j].arg) = lv; |
| 2083 | break; |
| 2084 | case OPT_FINT: |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2085 | (*(void(*)(int))(op[j].arg))((int)lv); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2086 | break; |
| 2087 | case OPT_STR: |
| 2088 | *(char**)(op[j].arg) = sv; |
| 2089 | break; |
| 2090 | case OPT_FSTR: |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2091 | (*(void(*)(char *))(op[j].arg))(sv); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2092 | break; |
| 2093 | } |
| 2094 | } |
| 2095 | return errcnt; |
| 2096 | } |
| 2097 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2098 | int OptInit(char **a, struct s_options *o, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2099 | { |
| 2100 | int errcnt = 0; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2101 | g_argv = a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2102 | op = o; |
| 2103 | errstream = err; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2104 | if( g_argv && *g_argv && op ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2105 | int i; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2106 | for(i=1; g_argv[i]; i++){ |
| 2107 | if( g_argv[i][0]=='+' || g_argv[i][0]=='-' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2108 | errcnt += handleflags(i,err); |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2109 | }else if( strchr(g_argv[i],'=') ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2110 | errcnt += handleswitch(i,err); |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | if( errcnt>0 ){ |
| 2115 | fprintf(err,"Valid command line options for \"%s\" are:\n",*a); |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 2116 | OptPrint(); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2117 | exit(1); |
| 2118 | } |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 2122 | int OptNArgs(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2123 | int cnt = 0; |
| 2124 | int dashdash = 0; |
| 2125 | int i; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2126 | if( g_argv!=0 && g_argv[0]!=0 ){ |
| 2127 | for(i=1; g_argv[i]; i++){ |
| 2128 | if( dashdash || !ISOPT(g_argv[i]) ) cnt++; |
| 2129 | if( strcmp(g_argv[i],"--")==0 ) dashdash = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | return cnt; |
| 2133 | } |
| 2134 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2135 | char *OptArg(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2136 | { |
| 2137 | int i; |
| 2138 | i = argindex(n); |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2139 | return i>=0 ? g_argv[i] : 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2142 | void OptErr(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2143 | { |
| 2144 | int i; |
| 2145 | i = argindex(n); |
| 2146 | if( i>=0 ) errline(i,0,errstream); |
| 2147 | } |
| 2148 | |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 2149 | void OptPrint(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2150 | int i; |
| 2151 | int max, len; |
| 2152 | max = 0; |
| 2153 | for(i=0; op[i].label; i++){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2154 | len = lemonStrlen(op[i].label) + 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2155 | switch( op[i].type ){ |
| 2156 | case OPT_FLAG: |
| 2157 | case OPT_FFLAG: |
| 2158 | break; |
| 2159 | case OPT_INT: |
| 2160 | case OPT_FINT: |
| 2161 | len += 9; /* length of "<integer>" */ |
| 2162 | break; |
| 2163 | case OPT_DBL: |
| 2164 | case OPT_FDBL: |
| 2165 | len += 6; /* length of "<real>" */ |
| 2166 | break; |
| 2167 | case OPT_STR: |
| 2168 | case OPT_FSTR: |
| 2169 | len += 8; /* length of "<string>" */ |
| 2170 | break; |
| 2171 | } |
| 2172 | if( len>max ) max = len; |
| 2173 | } |
| 2174 | for(i=0; op[i].label; i++){ |
| 2175 | switch( op[i].type ){ |
| 2176 | case OPT_FLAG: |
| 2177 | case OPT_FFLAG: |
| 2178 | fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message); |
| 2179 | break; |
| 2180 | case OPT_INT: |
| 2181 | case OPT_FINT: |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 2182 | fprintf(errstream," -%s<integer>%*s %s\n",op[i].label, |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2183 | (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2184 | break; |
| 2185 | case OPT_DBL: |
| 2186 | case OPT_FDBL: |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 2187 | fprintf(errstream," -%s<real>%*s %s\n",op[i].label, |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2188 | (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2189 | break; |
| 2190 | case OPT_STR: |
| 2191 | case OPT_FSTR: |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 2192 | fprintf(errstream," -%s<string>%*s %s\n",op[i].label, |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2193 | (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2194 | break; |
| 2195 | } |
| 2196 | } |
| 2197 | } |
| 2198 | /*********************** From the file "parse.c" ****************************/ |
| 2199 | /* |
| 2200 | ** Input file parser for the LEMON parser generator. |
| 2201 | */ |
| 2202 | |
| 2203 | /* The state of the parser */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2204 | enum e_state { |
| 2205 | INITIALIZE, |
| 2206 | WAITING_FOR_DECL_OR_RULE, |
| 2207 | WAITING_FOR_DECL_KEYWORD, |
| 2208 | WAITING_FOR_DECL_ARG, |
| 2209 | WAITING_FOR_PRECEDENCE_SYMBOL, |
| 2210 | WAITING_FOR_ARROW, |
| 2211 | IN_RHS, |
| 2212 | LHS_ALIAS_1, |
| 2213 | LHS_ALIAS_2, |
| 2214 | LHS_ALIAS_3, |
| 2215 | RHS_ALIAS_1, |
| 2216 | RHS_ALIAS_2, |
| 2217 | PRECEDENCE_MARK_1, |
| 2218 | PRECEDENCE_MARK_2, |
| 2219 | RESYNC_AFTER_RULE_ERROR, |
| 2220 | RESYNC_AFTER_DECL_ERROR, |
| 2221 | WAITING_FOR_DESTRUCTOR_SYMBOL, |
| 2222 | WAITING_FOR_DATATYPE_SYMBOL, |
| 2223 | WAITING_FOR_FALLBACK_ID, |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2224 | WAITING_FOR_WILDCARD_ID, |
| 2225 | WAITING_FOR_CLASS_ID, |
drh | 59c435a | 2017-08-02 03:21:11 +0000 | [diff] [blame] | 2226 | WAITING_FOR_CLASS_TOKEN, |
| 2227 | WAITING_FOR_TOKEN_NAME |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2228 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2229 | struct pstate { |
| 2230 | char *filename; /* Name of the input file */ |
| 2231 | int tokenlineno; /* Linenumber at which current token starts */ |
| 2232 | int errorcnt; /* Number of errors so far */ |
| 2233 | char *tokenstart; /* Text of current token */ |
| 2234 | struct lemon *gp; /* Global state vector */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2235 | enum e_state state; /* The state of the parser */ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2236 | struct symbol *fallback; /* The fallback token */ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2237 | struct symbol *tkclass; /* Token class symbol */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2238 | struct symbol *lhs; /* Left-hand side of current rule */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2239 | const char *lhsalias; /* Alias for the LHS */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2240 | int nrhs; /* Number of right-hand side symbols seen */ |
| 2241 | struct symbol *rhs[MAXRHS]; /* RHS symbols */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2242 | const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2243 | struct rule *prevrule; /* Previous rule parsed */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2244 | const char *declkeyword; /* Keyword of a declaration */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2245 | char **declargslot; /* Where the declaration argument should be put */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2246 | int insertLineMacro; /* Add #line before declaration insert */ |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 2247 | int *decllinenoslot; /* Where to write declaration line number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2248 | enum e_assoc declassoc; /* Assign this association to decl arguments */ |
| 2249 | int preccounter; /* Assign this precedence to decl arguments */ |
| 2250 | struct rule *firstrule; /* Pointer to first rule in the grammar */ |
| 2251 | struct rule *lastrule; /* Pointer to the most recently parsed rule */ |
| 2252 | }; |
| 2253 | |
| 2254 | /* Parse a single token */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2255 | static void parseonetoken(struct pstate *psp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2256 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2257 | const char *x; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2258 | x = Strsafe(psp->tokenstart); /* Save the token permanently */ |
| 2259 | #if 0 |
| 2260 | printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno, |
| 2261 | x,psp->state); |
| 2262 | #endif |
| 2263 | switch( psp->state ){ |
| 2264 | case INITIALIZE: |
| 2265 | psp->prevrule = 0; |
| 2266 | psp->preccounter = 0; |
| 2267 | psp->firstrule = psp->lastrule = 0; |
| 2268 | psp->gp->nrule = 0; |
drh | 18ef40e | 2020-09-16 16:55:56 +0000 | [diff] [blame^] | 2269 | /* fall through */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2270 | case WAITING_FOR_DECL_OR_RULE: |
| 2271 | if( x[0]=='%' ){ |
| 2272 | psp->state = WAITING_FOR_DECL_KEYWORD; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2273 | }else if( ISLOWER(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2274 | psp->lhs = Symbol_new(x); |
| 2275 | psp->nrhs = 0; |
| 2276 | psp->lhsalias = 0; |
| 2277 | psp->state = WAITING_FOR_ARROW; |
| 2278 | }else if( x[0]=='{' ){ |
| 2279 | if( psp->prevrule==0 ){ |
| 2280 | ErrorMsg(psp->filename,psp->tokenlineno, |
drh | 3ecc05b | 2019-12-12 00:20:40 +0000 | [diff] [blame] | 2281 | "There is no prior rule upon which to attach the code " |
| 2282 | "fragment which begins on this line."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2283 | psp->errorcnt++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2284 | }else if( psp->prevrule->code!=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2285 | ErrorMsg(psp->filename,psp->tokenlineno, |
drh | 3ecc05b | 2019-12-12 00:20:40 +0000 | [diff] [blame] | 2286 | "Code fragment beginning on this line is not the first " |
| 2287 | "to follow the previous rule."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2288 | psp->errorcnt++; |
drh | e94006e | 2019-12-10 20:41:48 +0000 | [diff] [blame] | 2289 | }else if( strcmp(x, "{NEVER-REDUCE")==0 ){ |
| 2290 | psp->prevrule->neverReduce = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2291 | }else{ |
| 2292 | psp->prevrule->line = psp->tokenlineno; |
| 2293 | psp->prevrule->code = &x[1]; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 2294 | psp->prevrule->noCode = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2295 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2296 | }else if( x[0]=='[' ){ |
| 2297 | psp->state = PRECEDENCE_MARK_1; |
| 2298 | }else{ |
| 2299 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2300 | "Token \"%s\" should be either \"%%\" or a nonterminal name.", |
| 2301 | x); |
| 2302 | psp->errorcnt++; |
| 2303 | } |
| 2304 | break; |
| 2305 | case PRECEDENCE_MARK_1: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2306 | if( !ISUPPER(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2307 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2308 | "The precedence symbol must be a terminal."); |
| 2309 | psp->errorcnt++; |
| 2310 | }else if( psp->prevrule==0 ){ |
| 2311 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2312 | "There is no prior rule to assign precedence \"[%s]\".",x); |
| 2313 | psp->errorcnt++; |
| 2314 | }else if( psp->prevrule->precsym!=0 ){ |
| 2315 | ErrorMsg(psp->filename,psp->tokenlineno, |
drh | 3ecc05b | 2019-12-12 00:20:40 +0000 | [diff] [blame] | 2316 | "Precedence mark on this line is not the first " |
| 2317 | "to follow the previous rule."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2318 | psp->errorcnt++; |
| 2319 | }else{ |
| 2320 | psp->prevrule->precsym = Symbol_new(x); |
| 2321 | } |
| 2322 | psp->state = PRECEDENCE_MARK_2; |
| 2323 | break; |
| 2324 | case PRECEDENCE_MARK_2: |
| 2325 | if( x[0]!=']' ){ |
| 2326 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2327 | "Missing \"]\" on precedence mark."); |
| 2328 | psp->errorcnt++; |
| 2329 | } |
| 2330 | psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2331 | break; |
| 2332 | case WAITING_FOR_ARROW: |
| 2333 | if( x[0]==':' && x[1]==':' && x[2]=='=' ){ |
| 2334 | psp->state = IN_RHS; |
| 2335 | }else if( x[0]=='(' ){ |
| 2336 | psp->state = LHS_ALIAS_1; |
| 2337 | }else{ |
| 2338 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2339 | "Expected to see a \":\" following the LHS symbol \"%s\".", |
| 2340 | psp->lhs->name); |
| 2341 | psp->errorcnt++; |
| 2342 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2343 | } |
| 2344 | break; |
| 2345 | case LHS_ALIAS_1: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2346 | if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2347 | psp->lhsalias = x; |
| 2348 | psp->state = LHS_ALIAS_2; |
| 2349 | }else{ |
| 2350 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2351 | "\"%s\" is not a valid alias for the LHS \"%s\"\n", |
| 2352 | x,psp->lhs->name); |
| 2353 | psp->errorcnt++; |
| 2354 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2355 | } |
| 2356 | break; |
| 2357 | case LHS_ALIAS_2: |
| 2358 | if( x[0]==')' ){ |
| 2359 | psp->state = LHS_ALIAS_3; |
| 2360 | }else{ |
| 2361 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2362 | "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias); |
| 2363 | psp->errorcnt++; |
| 2364 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2365 | } |
| 2366 | break; |
| 2367 | case LHS_ALIAS_3: |
| 2368 | if( x[0]==':' && x[1]==':' && x[2]=='=' ){ |
| 2369 | psp->state = IN_RHS; |
| 2370 | }else{ |
| 2371 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2372 | "Missing \"->\" following: \"%s(%s)\".", |
| 2373 | psp->lhs->name,psp->lhsalias); |
| 2374 | psp->errorcnt++; |
| 2375 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2376 | } |
| 2377 | break; |
| 2378 | case IN_RHS: |
| 2379 | if( x[0]=='.' ){ |
| 2380 | struct rule *rp; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 2381 | rp = (struct rule *)calloc( sizeof(struct rule) + |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 2382 | sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2383 | if( rp==0 ){ |
| 2384 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2385 | "Can't allocate enough memory for this rule."); |
| 2386 | psp->errorcnt++; |
| 2387 | psp->prevrule = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2388 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2389 | int i; |
| 2390 | rp->ruleline = psp->tokenlineno; |
| 2391 | rp->rhs = (struct symbol**)&rp[1]; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2392 | rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2393 | for(i=0; i<psp->nrhs; i++){ |
| 2394 | rp->rhs[i] = psp->rhs[i]; |
| 2395 | rp->rhsalias[i] = psp->alias[i]; |
drh | 539e741 | 2018-04-21 20:24:19 +0000 | [diff] [blame] | 2396 | if( rp->rhsalias[i]!=0 ){ rp->rhs[i]->bContent = 1; } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2397 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2398 | rp->lhs = psp->lhs; |
| 2399 | rp->lhsalias = psp->lhsalias; |
| 2400 | rp->nrhs = psp->nrhs; |
| 2401 | rp->code = 0; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 2402 | rp->noCode = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2403 | rp->precsym = 0; |
| 2404 | rp->index = psp->gp->nrule++; |
| 2405 | rp->nextlhs = rp->lhs->rule; |
| 2406 | rp->lhs->rule = rp; |
| 2407 | rp->next = 0; |
| 2408 | if( psp->firstrule==0 ){ |
| 2409 | psp->firstrule = psp->lastrule = rp; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2410 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2411 | psp->lastrule->next = rp; |
| 2412 | psp->lastrule = rp; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2413 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2414 | psp->prevrule = rp; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2415 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2416 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2417 | }else if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2418 | if( psp->nrhs>=MAXRHS ){ |
| 2419 | ErrorMsg(psp->filename,psp->tokenlineno, |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 2420 | "Too many symbols on RHS of rule beginning at \"%s\".", |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2421 | x); |
| 2422 | psp->errorcnt++; |
| 2423 | psp->state = RESYNC_AFTER_RULE_ERROR; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2424 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2425 | psp->rhs[psp->nrhs] = Symbol_new(x); |
| 2426 | psp->alias[psp->nrhs] = 0; |
| 2427 | psp->nrhs++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2428 | } |
drh | 2b75d3b | 2020-09-05 06:21:54 +0000 | [diff] [blame] | 2429 | }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 && ISUPPER(x[1]) ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2430 | struct symbol *msp = psp->rhs[psp->nrhs-1]; |
| 2431 | if( msp->type!=MULTITERMINAL ){ |
| 2432 | struct symbol *origsp = msp; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2433 | msp = (struct symbol *) calloc(1,sizeof(*msp)); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2434 | memset(msp, 0, sizeof(*msp)); |
| 2435 | msp->type = MULTITERMINAL; |
| 2436 | msp->nsubsym = 1; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2437 | msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*)); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2438 | msp->subsym[0] = origsp; |
| 2439 | msp->name = origsp->name; |
| 2440 | psp->rhs[psp->nrhs-1] = msp; |
| 2441 | } |
| 2442 | msp->nsubsym++; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2443 | msp->subsym = (struct symbol **) realloc(msp->subsym, |
| 2444 | sizeof(struct symbol*)*msp->nsubsym); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2445 | msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2446 | if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2447 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2448 | "Cannot form a compound containing a non-terminal"); |
| 2449 | psp->errorcnt++; |
| 2450 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2451 | }else if( x[0]=='(' && psp->nrhs>0 ){ |
| 2452 | psp->state = RHS_ALIAS_1; |
| 2453 | }else{ |
| 2454 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2455 | "Illegal character on RHS of rule: \"%s\".",x); |
| 2456 | psp->errorcnt++; |
| 2457 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2458 | } |
| 2459 | break; |
| 2460 | case RHS_ALIAS_1: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2461 | if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2462 | psp->alias[psp->nrhs-1] = x; |
| 2463 | psp->state = RHS_ALIAS_2; |
| 2464 | }else{ |
| 2465 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2466 | "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n", |
| 2467 | x,psp->rhs[psp->nrhs-1]->name); |
| 2468 | psp->errorcnt++; |
| 2469 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2470 | } |
| 2471 | break; |
| 2472 | case RHS_ALIAS_2: |
| 2473 | if( x[0]==')' ){ |
| 2474 | psp->state = IN_RHS; |
| 2475 | }else{ |
| 2476 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2477 | "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias); |
| 2478 | psp->errorcnt++; |
| 2479 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2480 | } |
| 2481 | break; |
| 2482 | case WAITING_FOR_DECL_KEYWORD: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2483 | if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2484 | psp->declkeyword = x; |
| 2485 | psp->declargslot = 0; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 2486 | psp->decllinenoslot = 0; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2487 | psp->insertLineMacro = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2488 | psp->state = WAITING_FOR_DECL_ARG; |
| 2489 | if( strcmp(x,"name")==0 ){ |
| 2490 | psp->declargslot = &(psp->gp->name); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2491 | psp->insertLineMacro = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2492 | }else if( strcmp(x,"include")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2493 | psp->declargslot = &(psp->gp->include); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2494 | }else if( strcmp(x,"code")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2495 | psp->declargslot = &(psp->gp->extracode); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2496 | }else if( strcmp(x,"token_destructor")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2497 | psp->declargslot = &psp->gp->tokendest; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2498 | }else if( strcmp(x,"default_destructor")==0 ){ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2499 | psp->declargslot = &psp->gp->vardest; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2500 | }else if( strcmp(x,"token_prefix")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2501 | psp->declargslot = &psp->gp->tokenprefix; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2502 | psp->insertLineMacro = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2503 | }else if( strcmp(x,"syntax_error")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2504 | psp->declargslot = &(psp->gp->error); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2505 | }else if( strcmp(x,"parse_accept")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2506 | psp->declargslot = &(psp->gp->accept); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2507 | }else if( strcmp(x,"parse_failure")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2508 | psp->declargslot = &(psp->gp->failure); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2509 | }else if( strcmp(x,"stack_overflow")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2510 | psp->declargslot = &(psp->gp->overflow); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2511 | }else if( strcmp(x,"extra_argument")==0 ){ |
| 2512 | psp->declargslot = &(psp->gp->arg); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2513 | psp->insertLineMacro = 0; |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 2514 | }else if( strcmp(x,"extra_context")==0 ){ |
| 2515 | psp->declargslot = &(psp->gp->ctx); |
| 2516 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2517 | }else if( strcmp(x,"token_type")==0 ){ |
| 2518 | psp->declargslot = &(psp->gp->tokentype); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2519 | psp->insertLineMacro = 0; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2520 | }else if( strcmp(x,"default_type")==0 ){ |
| 2521 | psp->declargslot = &(psp->gp->vartype); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2522 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2523 | }else if( strcmp(x,"stack_size")==0 ){ |
| 2524 | psp->declargslot = &(psp->gp->stacksize); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2525 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2526 | }else if( strcmp(x,"start_symbol")==0 ){ |
| 2527 | psp->declargslot = &(psp->gp->start); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2528 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2529 | }else if( strcmp(x,"left")==0 ){ |
| 2530 | psp->preccounter++; |
| 2531 | psp->declassoc = LEFT; |
| 2532 | psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
| 2533 | }else if( strcmp(x,"right")==0 ){ |
| 2534 | psp->preccounter++; |
| 2535 | psp->declassoc = RIGHT; |
| 2536 | psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
| 2537 | }else if( strcmp(x,"nonassoc")==0 ){ |
| 2538 | psp->preccounter++; |
| 2539 | psp->declassoc = NONE; |
| 2540 | psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2541 | }else if( strcmp(x,"destructor")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2542 | psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2543 | }else if( strcmp(x,"type")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2544 | psp->state = WAITING_FOR_DATATYPE_SYMBOL; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2545 | }else if( strcmp(x,"fallback")==0 ){ |
| 2546 | psp->fallback = 0; |
| 2547 | psp->state = WAITING_FOR_FALLBACK_ID; |
drh | 59c435a | 2017-08-02 03:21:11 +0000 | [diff] [blame] | 2548 | }else if( strcmp(x,"token")==0 ){ |
| 2549 | psp->state = WAITING_FOR_TOKEN_NAME; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 2550 | }else if( strcmp(x,"wildcard")==0 ){ |
| 2551 | psp->state = WAITING_FOR_WILDCARD_ID; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2552 | }else if( strcmp(x,"token_class")==0 ){ |
| 2553 | psp->state = WAITING_FOR_CLASS_ID; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2554 | }else{ |
| 2555 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2556 | "Unknown declaration keyword: \"%%%s\".",x); |
| 2557 | psp->errorcnt++; |
| 2558 | psp->state = RESYNC_AFTER_DECL_ERROR; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2559 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2560 | }else{ |
| 2561 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2562 | "Illegal declaration keyword: \"%s\".",x); |
| 2563 | psp->errorcnt++; |
| 2564 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2565 | } |
| 2566 | break; |
| 2567 | case WAITING_FOR_DESTRUCTOR_SYMBOL: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2568 | if( !ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2569 | ErrorMsg(psp->filename,psp->tokenlineno, |
icculus | d0d97b0 | 2010-02-17 20:22:10 +0000 | [diff] [blame] | 2570 | "Symbol name missing after %%destructor keyword"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2571 | psp->errorcnt++; |
| 2572 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2573 | }else{ |
icculus | d286fa6 | 2010-03-03 17:06:32 +0000 | [diff] [blame] | 2574 | struct symbol *sp = Symbol_new(x); |
| 2575 | psp->declargslot = &sp->destructor; |
| 2576 | psp->decllinenoslot = &sp->destLineno; |
| 2577 | psp->insertLineMacro = 1; |
| 2578 | psp->state = WAITING_FOR_DECL_ARG; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2579 | } |
| 2580 | break; |
| 2581 | case WAITING_FOR_DATATYPE_SYMBOL: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2582 | if( !ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2583 | ErrorMsg(psp->filename,psp->tokenlineno, |
icculus | d0d97b0 | 2010-02-17 20:22:10 +0000 | [diff] [blame] | 2584 | "Symbol name missing after %%type keyword"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2585 | psp->errorcnt++; |
| 2586 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2587 | }else{ |
icculus | 866bf1e | 2010-02-17 20:31:32 +0000 | [diff] [blame] | 2588 | struct symbol *sp = Symbol_find(x); |
| 2589 | if((sp) && (sp->datatype)){ |
| 2590 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2591 | "Symbol %%type \"%s\" already defined", x); |
| 2592 | psp->errorcnt++; |
| 2593 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2594 | }else{ |
| 2595 | if (!sp){ |
| 2596 | sp = Symbol_new(x); |
| 2597 | } |
| 2598 | psp->declargslot = &sp->datatype; |
| 2599 | psp->insertLineMacro = 0; |
| 2600 | psp->state = WAITING_FOR_DECL_ARG; |
| 2601 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2602 | } |
| 2603 | break; |
| 2604 | case WAITING_FOR_PRECEDENCE_SYMBOL: |
| 2605 | if( x[0]=='.' ){ |
| 2606 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2607 | }else if( ISUPPER(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2608 | struct symbol *sp; |
| 2609 | sp = Symbol_new(x); |
| 2610 | if( sp->prec>=0 ){ |
| 2611 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2612 | "Symbol \"%s\" has already be given a precedence.",x); |
| 2613 | psp->errorcnt++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2614 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2615 | sp->prec = psp->preccounter; |
| 2616 | sp->assoc = psp->declassoc; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2617 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2618 | }else{ |
| 2619 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2620 | "Can't assign a precedence to \"%s\".",x); |
| 2621 | psp->errorcnt++; |
| 2622 | } |
| 2623 | break; |
| 2624 | case WAITING_FOR_DECL_ARG: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2625 | if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2626 | const char *zOld, *zNew; |
| 2627 | char *zBuf, *z; |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 2628 | int nOld, n, nLine = 0, nNew, nBack; |
drh | b5bd49e | 2008-07-14 12:21:08 +0000 | [diff] [blame] | 2629 | int addLineMacro; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2630 | char zLine[50]; |
| 2631 | zNew = x; |
| 2632 | if( zNew[0]=='"' || zNew[0]=='{' ) zNew++; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2633 | nNew = lemonStrlen(zNew); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2634 | if( *psp->declargslot ){ |
| 2635 | zOld = *psp->declargslot; |
| 2636 | }else{ |
| 2637 | zOld = ""; |
| 2638 | } |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2639 | nOld = lemonStrlen(zOld); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2640 | n = nOld + nNew + 20; |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 2641 | addLineMacro = !psp->gp->nolinenosflag |
| 2642 | && psp->insertLineMacro |
| 2643 | && psp->tokenlineno>1 |
| 2644 | && (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0); |
drh | b5bd49e | 2008-07-14 12:21:08 +0000 | [diff] [blame] | 2645 | if( addLineMacro ){ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2646 | for(z=psp->filename, nBack=0; *z; z++){ |
| 2647 | if( *z=='\\' ) nBack++; |
| 2648 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 2649 | lemon_sprintf(zLine, "#line %d ", psp->tokenlineno); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2650 | nLine = lemonStrlen(zLine); |
| 2651 | n += nLine + lemonStrlen(psp->filename) + nBack; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2652 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2653 | *psp->declargslot = (char *) realloc(*psp->declargslot, n); |
| 2654 | zBuf = *psp->declargslot + nOld; |
drh | b5bd49e | 2008-07-14 12:21:08 +0000 | [diff] [blame] | 2655 | if( addLineMacro ){ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2656 | if( nOld && zBuf[-1]!='\n' ){ |
| 2657 | *(zBuf++) = '\n'; |
| 2658 | } |
| 2659 | memcpy(zBuf, zLine, nLine); |
| 2660 | zBuf += nLine; |
| 2661 | *(zBuf++) = '"'; |
| 2662 | for(z=psp->filename; *z; z++){ |
| 2663 | if( *z=='\\' ){ |
| 2664 | *(zBuf++) = '\\'; |
| 2665 | } |
| 2666 | *(zBuf++) = *z; |
| 2667 | } |
| 2668 | *(zBuf++) = '"'; |
| 2669 | *(zBuf++) = '\n'; |
| 2670 | } |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 2671 | if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){ |
| 2672 | psp->decllinenoslot[0] = psp->tokenlineno; |
| 2673 | } |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2674 | memcpy(zBuf, zNew, nNew); |
| 2675 | zBuf += nNew; |
| 2676 | *zBuf = 0; |
| 2677 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2678 | }else{ |
| 2679 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2680 | "Illegal argument to %%%s: %s",psp->declkeyword,x); |
| 2681 | psp->errorcnt++; |
| 2682 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2683 | } |
| 2684 | break; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2685 | case WAITING_FOR_FALLBACK_ID: |
| 2686 | if( x[0]=='.' ){ |
| 2687 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2688 | }else if( !ISUPPER(x[0]) ){ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2689 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2690 | "%%fallback argument \"%s\" should be a token", x); |
| 2691 | psp->errorcnt++; |
| 2692 | }else{ |
| 2693 | struct symbol *sp = Symbol_new(x); |
| 2694 | if( psp->fallback==0 ){ |
| 2695 | psp->fallback = sp; |
| 2696 | }else if( sp->fallback ){ |
| 2697 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2698 | "More than one fallback assigned to token %s", x); |
| 2699 | psp->errorcnt++; |
| 2700 | }else{ |
| 2701 | sp->fallback = psp->fallback; |
| 2702 | psp->gp->has_fallback = 1; |
| 2703 | } |
| 2704 | } |
| 2705 | break; |
drh | 59c435a | 2017-08-02 03:21:11 +0000 | [diff] [blame] | 2706 | case WAITING_FOR_TOKEN_NAME: |
| 2707 | /* Tokens do not have to be declared before use. But they can be |
| 2708 | ** in order to control their assigned integer number. The number for |
| 2709 | ** each token is assigned when it is first seen. So by including |
| 2710 | ** |
| 2711 | ** %token ONE TWO THREE |
| 2712 | ** |
| 2713 | ** early in the grammar file, that assigns small consecutive values |
| 2714 | ** to each of the tokens ONE TWO and THREE. |
| 2715 | */ |
| 2716 | if( x[0]=='.' ){ |
| 2717 | psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2718 | }else if( !ISUPPER(x[0]) ){ |
| 2719 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2720 | "%%token argument \"%s\" should be a token", x); |
| 2721 | psp->errorcnt++; |
| 2722 | }else{ |
| 2723 | (void)Symbol_new(x); |
| 2724 | } |
| 2725 | break; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 2726 | case WAITING_FOR_WILDCARD_ID: |
| 2727 | if( x[0]=='.' ){ |
| 2728 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2729 | }else if( !ISUPPER(x[0]) ){ |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 2730 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2731 | "%%wildcard argument \"%s\" should be a token", x); |
| 2732 | psp->errorcnt++; |
| 2733 | }else{ |
| 2734 | struct symbol *sp = Symbol_new(x); |
| 2735 | if( psp->gp->wildcard==0 ){ |
| 2736 | psp->gp->wildcard = sp; |
| 2737 | }else{ |
| 2738 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2739 | "Extra wildcard to token: %s", x); |
| 2740 | psp->errorcnt++; |
| 2741 | } |
| 2742 | } |
| 2743 | break; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2744 | case WAITING_FOR_CLASS_ID: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2745 | if( !ISLOWER(x[0]) ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2746 | ErrorMsg(psp->filename, psp->tokenlineno, |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2747 | "%%token_class must be followed by an identifier: %s", x); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2748 | psp->errorcnt++; |
| 2749 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2750 | }else if( Symbol_find(x) ){ |
| 2751 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2752 | "Symbol \"%s\" already used", x); |
| 2753 | psp->errorcnt++; |
| 2754 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2755 | }else{ |
| 2756 | psp->tkclass = Symbol_new(x); |
| 2757 | psp->tkclass->type = MULTITERMINAL; |
| 2758 | psp->state = WAITING_FOR_CLASS_TOKEN; |
| 2759 | } |
| 2760 | break; |
| 2761 | case WAITING_FOR_CLASS_TOKEN: |
| 2762 | if( x[0]=='.' ){ |
| 2763 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2764 | }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2765 | struct symbol *msp = psp->tkclass; |
| 2766 | msp->nsubsym++; |
| 2767 | msp->subsym = (struct symbol **) realloc(msp->subsym, |
| 2768 | sizeof(struct symbol*)*msp->nsubsym); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2769 | if( !ISUPPER(x[0]) ) x++; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2770 | msp->subsym[msp->nsubsym-1] = Symbol_new(x); |
| 2771 | }else{ |
| 2772 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2773 | "%%token_class argument \"%s\" should be a token", x); |
| 2774 | psp->errorcnt++; |
| 2775 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2776 | } |
| 2777 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2778 | case RESYNC_AFTER_RULE_ERROR: |
| 2779 | /* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2780 | ** break; */ |
| 2781 | case RESYNC_AFTER_DECL_ERROR: |
| 2782 | if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2783 | if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD; |
| 2784 | break; |
| 2785 | } |
| 2786 | } |
| 2787 | |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 2788 | /* The text in the input is part of the argument to an %ifdef or %ifndef. |
| 2789 | ** Evaluate the text as a boolean expression. Return true or false. |
| 2790 | */ |
| 2791 | static int eval_preprocessor_boolean(char *z, int lineno){ |
| 2792 | int neg = 0; |
| 2793 | int res = 0; |
| 2794 | int okTerm = 1; |
| 2795 | int i; |
| 2796 | for(i=0; z[i]!=0; i++){ |
| 2797 | if( ISSPACE(z[i]) ) continue; |
| 2798 | if( z[i]=='!' ){ |
| 2799 | if( !okTerm ) goto pp_syntax_error; |
| 2800 | neg = !neg; |
| 2801 | continue; |
| 2802 | } |
| 2803 | if( z[i]=='|' && z[i+1]=='|' ){ |
| 2804 | if( okTerm ) goto pp_syntax_error; |
| 2805 | if( res ) return 1; |
| 2806 | i++; |
| 2807 | okTerm = 1; |
| 2808 | continue; |
| 2809 | } |
| 2810 | if( z[i]=='&' && z[i+1]=='&' ){ |
| 2811 | if( okTerm ) goto pp_syntax_error; |
| 2812 | if( !res ) return 0; |
| 2813 | i++; |
| 2814 | okTerm = 1; |
| 2815 | continue; |
| 2816 | } |
| 2817 | if( z[i]=='(' ){ |
| 2818 | int k; |
| 2819 | int n = 1; |
| 2820 | if( !okTerm ) goto pp_syntax_error; |
| 2821 | for(k=i+1; z[k]; k++){ |
| 2822 | if( z[k]==')' ){ |
| 2823 | n--; |
| 2824 | if( n==0 ){ |
| 2825 | z[k] = 0; |
| 2826 | res = eval_preprocessor_boolean(&z[i+1], -1); |
| 2827 | z[k] = ')'; |
| 2828 | if( res<0 ){ |
| 2829 | i = i-res; |
| 2830 | goto pp_syntax_error; |
| 2831 | } |
| 2832 | i = k; |
| 2833 | break; |
| 2834 | } |
| 2835 | }else if( z[k]=='(' ){ |
| 2836 | n++; |
| 2837 | }else if( z[k]==0 ){ |
| 2838 | i = k; |
| 2839 | goto pp_syntax_error; |
| 2840 | } |
| 2841 | } |
| 2842 | if( neg ){ |
| 2843 | res = !res; |
| 2844 | neg = 0; |
| 2845 | } |
| 2846 | okTerm = 0; |
| 2847 | continue; |
| 2848 | } |
| 2849 | if( ISALPHA(z[i]) ){ |
| 2850 | int j, k, n; |
| 2851 | if( !okTerm ) goto pp_syntax_error; |
| 2852 | for(k=i+1; ISALNUM(z[k]) || z[k]=='_'; k++){} |
| 2853 | n = k - i; |
| 2854 | res = 0; |
| 2855 | for(j=0; j<nDefine; j++){ |
| 2856 | if( strncmp(azDefine[j],&z[i],n)==0 && azDefine[j][n]==0 ){ |
| 2857 | res = 1; |
| 2858 | break; |
| 2859 | } |
| 2860 | } |
| 2861 | i = k-1; |
| 2862 | if( neg ){ |
| 2863 | res = !res; |
| 2864 | neg = 0; |
| 2865 | } |
| 2866 | okTerm = 0; |
| 2867 | continue; |
| 2868 | } |
| 2869 | goto pp_syntax_error; |
| 2870 | } |
| 2871 | return res; |
| 2872 | |
| 2873 | pp_syntax_error: |
| 2874 | if( lineno>0 ){ |
| 2875 | fprintf(stderr, "%%if syntax error on line %d.\n", lineno); |
| 2876 | fprintf(stderr, " %.*s <-- syntax error here\n", i+1, z); |
| 2877 | exit(1); |
| 2878 | }else{ |
| 2879 | return -(i+1); |
| 2880 | } |
| 2881 | } |
| 2882 | |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 2883 | /* Run the preprocessor over the input file text. The global variables |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2884 | ** azDefine[0] through azDefine[nDefine-1] contains the names of all defined |
| 2885 | ** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and |
| 2886 | ** comments them out. Text in between is also commented out as appropriate. |
| 2887 | */ |
danielk1977 | 940fac9 | 2005-01-23 22:41:37 +0000 | [diff] [blame] | 2888 | static void preprocess_input(char *z){ |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 2889 | int i, j, k; |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2890 | int exclude = 0; |
rse | 38514a9 | 2007-09-20 11:34:17 +0000 | [diff] [blame] | 2891 | int start = 0; |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2892 | int lineno = 1; |
rse | 38514a9 | 2007-09-20 11:34:17 +0000 | [diff] [blame] | 2893 | int start_lineno = 1; |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2894 | for(i=0; z[i]; i++){ |
| 2895 | if( z[i]=='\n' ) lineno++; |
| 2896 | if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2897 | if( strncmp(&z[i],"%endif",6)==0 && ISSPACE(z[i+6]) ){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2898 | if( exclude ){ |
| 2899 | exclude--; |
| 2900 | if( exclude==0 ){ |
| 2901 | for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' '; |
| 2902 | } |
| 2903 | } |
| 2904 | for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' '; |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 2905 | }else if( strncmp(&z[i],"%else",5)==0 && ISSPACE(z[i+5]) ){ |
| 2906 | if( exclude==1){ |
| 2907 | exclude = 0; |
| 2908 | for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' '; |
| 2909 | }else if( exclude==0 ){ |
| 2910 | exclude = 1; |
| 2911 | start = i; |
| 2912 | start_lineno = lineno; |
| 2913 | } |
| 2914 | for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' '; |
| 2915 | }else if( strncmp(&z[i],"%ifdef ",7)==0 |
| 2916 | || strncmp(&z[i],"%if ",4)==0 |
| 2917 | || strncmp(&z[i],"%ifndef ",8)==0 ){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2918 | if( exclude ){ |
| 2919 | exclude++; |
| 2920 | }else{ |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 2921 | int isNot; |
| 2922 | int iBool; |
| 2923 | for(j=i; z[j] && !ISSPACE(z[j]); j++){} |
| 2924 | iBool = j; |
| 2925 | isNot = (j==i+7); |
| 2926 | while( z[j] && z[j]!='\n' ){ j++; } |
| 2927 | k = z[j]; |
| 2928 | z[j] = 0; |
| 2929 | exclude = eval_preprocessor_boolean(&z[iBool], lineno); |
| 2930 | z[j] = k; |
| 2931 | if( !isNot ) exclude = !exclude; |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2932 | if( exclude ){ |
| 2933 | start = i; |
| 2934 | start_lineno = lineno; |
| 2935 | } |
| 2936 | } |
| 2937 | for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' '; |
| 2938 | } |
| 2939 | } |
| 2940 | if( exclude ){ |
| 2941 | fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno); |
| 2942 | exit(1); |
| 2943 | } |
| 2944 | } |
| 2945 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2946 | /* In spite of its name, this function is really a scanner. It read |
| 2947 | ** in the entire input file (all at once) then tokenizes it. Each |
| 2948 | ** token is passed to the function "parseonetoken" which builds all |
| 2949 | ** the appropriate data structures in the global state vector "gp". |
| 2950 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2951 | void Parse(struct lemon *gp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2952 | { |
| 2953 | struct pstate ps; |
| 2954 | FILE *fp; |
| 2955 | char *filebuf; |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 2956 | unsigned int filesize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2957 | int lineno; |
| 2958 | int c; |
| 2959 | char *cp, *nextcp; |
| 2960 | int startline = 0; |
| 2961 | |
rse | 38514a9 | 2007-09-20 11:34:17 +0000 | [diff] [blame] | 2962 | memset(&ps, '\0', sizeof(ps)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2963 | ps.gp = gp; |
| 2964 | ps.filename = gp->filename; |
| 2965 | ps.errorcnt = 0; |
| 2966 | ps.state = INITIALIZE; |
| 2967 | |
| 2968 | /* Begin by reading the input file */ |
| 2969 | fp = fopen(ps.filename,"rb"); |
| 2970 | if( fp==0 ){ |
| 2971 | ErrorMsg(ps.filename,0,"Can't open this file for reading."); |
| 2972 | gp->errorcnt++; |
| 2973 | return; |
| 2974 | } |
| 2975 | fseek(fp,0,2); |
| 2976 | filesize = ftell(fp); |
| 2977 | rewind(fp); |
| 2978 | filebuf = (char *)malloc( filesize+1 ); |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 2979 | if( filesize>100000000 || filebuf==0 ){ |
| 2980 | ErrorMsg(ps.filename,0,"Input file too large."); |
mistachkin | c93c614 | 2018-09-08 16:55:18 +0000 | [diff] [blame] | 2981 | free(filebuf); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2982 | gp->errorcnt++; |
drh | e0a59cf | 2011-08-30 00:58:58 +0000 | [diff] [blame] | 2983 | fclose(fp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2984 | return; |
| 2985 | } |
| 2986 | if( fread(filebuf,1,filesize,fp)!=filesize ){ |
| 2987 | ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.", |
| 2988 | filesize); |
| 2989 | free(filebuf); |
| 2990 | gp->errorcnt++; |
drh | e0a59cf | 2011-08-30 00:58:58 +0000 | [diff] [blame] | 2991 | fclose(fp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2992 | return; |
| 2993 | } |
| 2994 | fclose(fp); |
| 2995 | filebuf[filesize] = 0; |
| 2996 | |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2997 | /* Make an initial pass through the file to handle %ifdef and %ifndef */ |
| 2998 | preprocess_input(filebuf); |
drh | 0a34cf5 | 2020-07-03 15:41:08 +0000 | [diff] [blame] | 2999 | if( gp->printPreprocessed ){ |
| 3000 | printf("%s\n", filebuf); |
| 3001 | return; |
| 3002 | } |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 3003 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3004 | /* Now scan the text of the input file */ |
| 3005 | lineno = 1; |
| 3006 | for(cp=filebuf; (c= *cp)!=0; ){ |
| 3007 | if( c=='\n' ) lineno++; /* Keep track of the line number */ |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3008 | if( ISSPACE(c) ){ cp++; continue; } /* Skip all white space */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3009 | if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */ |
| 3010 | cp+=2; |
| 3011 | while( (c= *cp)!=0 && c!='\n' ) cp++; |
| 3012 | continue; |
| 3013 | } |
| 3014 | if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */ |
| 3015 | cp+=2; |
| 3016 | while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){ |
| 3017 | if( c=='\n' ) lineno++; |
| 3018 | cp++; |
| 3019 | } |
| 3020 | if( c ) cp++; |
| 3021 | continue; |
| 3022 | } |
| 3023 | ps.tokenstart = cp; /* Mark the beginning of the token */ |
| 3024 | ps.tokenlineno = lineno; /* Linenumber on which token begins */ |
| 3025 | if( c=='\"' ){ /* String literals */ |
| 3026 | cp++; |
| 3027 | while( (c= *cp)!=0 && c!='\"' ){ |
| 3028 | if( c=='\n' ) lineno++; |
| 3029 | cp++; |
| 3030 | } |
| 3031 | if( c==0 ){ |
| 3032 | ErrorMsg(ps.filename,startline, |
drh | 3ecc05b | 2019-12-12 00:20:40 +0000 | [diff] [blame] | 3033 | "String starting on this line is not terminated before " |
| 3034 | "the end of the file."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3035 | ps.errorcnt++; |
| 3036 | nextcp = cp; |
| 3037 | }else{ |
| 3038 | nextcp = cp+1; |
| 3039 | } |
| 3040 | }else if( c=='{' ){ /* A block of C code */ |
| 3041 | int level; |
| 3042 | cp++; |
| 3043 | for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){ |
| 3044 | if( c=='\n' ) lineno++; |
| 3045 | else if( c=='{' ) level++; |
| 3046 | else if( c=='}' ) level--; |
| 3047 | else if( c=='/' && cp[1]=='*' ){ /* Skip comments */ |
| 3048 | int prevc; |
| 3049 | cp = &cp[2]; |
| 3050 | prevc = 0; |
| 3051 | while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){ |
| 3052 | if( c=='\n' ) lineno++; |
| 3053 | prevc = c; |
| 3054 | cp++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 3055 | } |
| 3056 | }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3057 | cp = &cp[2]; |
| 3058 | while( (c= *cp)!=0 && c!='\n' ) cp++; |
| 3059 | if( c ) lineno++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 3060 | }else if( c=='\'' || c=='\"' ){ /* String a character literals */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3061 | int startchar, prevc; |
| 3062 | startchar = c; |
| 3063 | prevc = 0; |
| 3064 | for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){ |
| 3065 | if( c=='\n' ) lineno++; |
| 3066 | if( prevc=='\\' ) prevc = 0; |
| 3067 | else prevc = c; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 3068 | } |
| 3069 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3070 | } |
| 3071 | if( c==0 ){ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3072 | ErrorMsg(ps.filename,ps.tokenlineno, |
drh | 3ecc05b | 2019-12-12 00:20:40 +0000 | [diff] [blame] | 3073 | "C code starting on this line is not terminated before " |
| 3074 | "the end of the file."); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3075 | ps.errorcnt++; |
| 3076 | nextcp = cp; |
| 3077 | }else{ |
| 3078 | nextcp = cp+1; |
| 3079 | } |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3080 | }else if( ISALNUM(c) ){ /* Identifiers */ |
| 3081 | while( (c= *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3082 | nextcp = cp; |
| 3083 | }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */ |
| 3084 | cp += 3; |
| 3085 | nextcp = cp; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3086 | }else if( (c=='/' || c=='|') && ISALPHA(cp[1]) ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3087 | cp += 2; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3088 | while( (c = *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3089 | nextcp = cp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3090 | }else{ /* All other (one character) operators */ |
| 3091 | cp++; |
| 3092 | nextcp = cp; |
| 3093 | } |
| 3094 | c = *cp; |
| 3095 | *cp = 0; /* Null terminate the token */ |
| 3096 | parseonetoken(&ps); /* Parse the token */ |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 3097 | *cp = (char)c; /* Restore the buffer */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3098 | cp = nextcp; |
| 3099 | } |
| 3100 | free(filebuf); /* Release the buffer after parsing */ |
| 3101 | gp->rule = ps.firstrule; |
| 3102 | gp->errorcnt = ps.errorcnt; |
| 3103 | } |
| 3104 | /*************************** From the file "plink.c" *********************/ |
| 3105 | /* |
| 3106 | ** Routines processing configuration follow-set propagation links |
| 3107 | ** in the LEMON parser generator. |
| 3108 | */ |
| 3109 | static struct plink *plink_freelist = 0; |
| 3110 | |
| 3111 | /* Allocate a new plink */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 3112 | struct plink *Plink_new(void){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3113 | struct plink *newlink; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3114 | |
| 3115 | if( plink_freelist==0 ){ |
| 3116 | int i; |
| 3117 | int amt = 100; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3118 | plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3119 | if( plink_freelist==0 ){ |
| 3120 | fprintf(stderr, |
| 3121 | "Unable to allocate memory for a new follow-set propagation link.\n"); |
| 3122 | exit(1); |
| 3123 | } |
| 3124 | for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1]; |
| 3125 | plink_freelist[amt-1].next = 0; |
| 3126 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3127 | newlink = plink_freelist; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3128 | plink_freelist = plink_freelist->next; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3129 | return newlink; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | /* Add a plink to a plink list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3133 | void Plink_add(struct plink **plpp, struct config *cfp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3134 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3135 | struct plink *newlink; |
| 3136 | newlink = Plink_new(); |
| 3137 | newlink->next = *plpp; |
| 3138 | *plpp = newlink; |
| 3139 | newlink->cfp = cfp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | /* Transfer every plink on the list "from" to the list "to" */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3143 | void Plink_copy(struct plink **to, struct plink *from) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3144 | { |
| 3145 | struct plink *nextpl; |
| 3146 | while( from ){ |
| 3147 | nextpl = from->next; |
| 3148 | from->next = *to; |
| 3149 | *to = from; |
| 3150 | from = nextpl; |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | /* Delete every plink on the list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3155 | void Plink_delete(struct plink *plp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3156 | { |
| 3157 | struct plink *nextpl; |
| 3158 | |
| 3159 | while( plp ){ |
| 3160 | nextpl = plp->next; |
| 3161 | plp->next = plink_freelist; |
| 3162 | plink_freelist = plp; |
| 3163 | plp = nextpl; |
| 3164 | } |
| 3165 | } |
| 3166 | /*********************** From the file "report.c" **************************/ |
| 3167 | /* |
| 3168 | ** Procedures for generating reports and tables in the LEMON parser generator. |
| 3169 | */ |
| 3170 | |
| 3171 | /* Generate a filename with the given suffix. Space to hold the |
| 3172 | ** name comes from malloc() and must be freed by the calling |
| 3173 | ** function. |
| 3174 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3175 | PRIVATE char *file_makename(struct lemon *lemp, const char *suffix) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3176 | { |
| 3177 | char *name; |
| 3178 | char *cp; |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 3179 | char *filename = lemp->filename; |
| 3180 | int sz; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3181 | |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 3182 | if( outputDir ){ |
| 3183 | cp = strrchr(filename, '/'); |
| 3184 | if( cp ) filename = cp + 1; |
| 3185 | } |
| 3186 | sz = lemonStrlen(filename); |
| 3187 | sz += lemonStrlen(suffix); |
| 3188 | if( outputDir ) sz += lemonStrlen(outputDir) + 1; |
| 3189 | sz += 5; |
| 3190 | name = (char*)malloc( sz ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3191 | if( name==0 ){ |
| 3192 | fprintf(stderr,"Can't allocate space for a filename.\n"); |
| 3193 | exit(1); |
| 3194 | } |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 3195 | name[0] = 0; |
| 3196 | if( outputDir ){ |
| 3197 | lemon_strcpy(name, outputDir); |
| 3198 | lemon_strcat(name, "/"); |
| 3199 | } |
| 3200 | lemon_strcat(name,filename); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3201 | cp = strrchr(name,'.'); |
| 3202 | if( cp ) *cp = 0; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3203 | lemon_strcat(name,suffix); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3204 | return name; |
| 3205 | } |
| 3206 | |
| 3207 | /* Open a file with a name based on the name of the input file, |
| 3208 | ** but with a different (specified) suffix, and return a pointer |
| 3209 | ** to the stream */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3210 | PRIVATE FILE *file_open( |
| 3211 | struct lemon *lemp, |
| 3212 | const char *suffix, |
| 3213 | const char *mode |
| 3214 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3215 | FILE *fp; |
| 3216 | |
| 3217 | if( lemp->outname ) free(lemp->outname); |
| 3218 | lemp->outname = file_makename(lemp, suffix); |
| 3219 | fp = fopen(lemp->outname,mode); |
| 3220 | if( fp==0 && *mode=='w' ){ |
| 3221 | fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname); |
| 3222 | lemp->errorcnt++; |
| 3223 | return 0; |
| 3224 | } |
| 3225 | return fp; |
| 3226 | } |
| 3227 | |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3228 | /* Print the text of a rule |
| 3229 | */ |
| 3230 | void rule_print(FILE *out, struct rule *rp){ |
| 3231 | int i, j; |
| 3232 | fprintf(out, "%s",rp->lhs->name); |
| 3233 | /* if( rp->lhsalias ) fprintf(out,"(%s)",rp->lhsalias); */ |
| 3234 | fprintf(out," ::="); |
| 3235 | for(i=0; i<rp->nrhs; i++){ |
| 3236 | struct symbol *sp = rp->rhs[i]; |
| 3237 | if( sp->type==MULTITERMINAL ){ |
| 3238 | fprintf(out," %s", sp->subsym[0]->name); |
| 3239 | for(j=1; j<sp->nsubsym; j++){ |
| 3240 | fprintf(out,"|%s", sp->subsym[j]->name); |
| 3241 | } |
| 3242 | }else{ |
| 3243 | fprintf(out," %s", sp->name); |
| 3244 | } |
| 3245 | /* if( rp->rhsalias[i] ) fprintf(out,"(%s)",rp->rhsalias[i]); */ |
| 3246 | } |
| 3247 | } |
| 3248 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3249 | /* Duplicate the input file without comments and without actions |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3250 | ** on rules */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3251 | void Reprint(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3252 | { |
| 3253 | struct rule *rp; |
| 3254 | struct symbol *sp; |
| 3255 | int i, j, maxlen, len, ncolumns, skip; |
| 3256 | printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename); |
| 3257 | maxlen = 10; |
| 3258 | for(i=0; i<lemp->nsymbol; i++){ |
| 3259 | sp = lemp->symbols[i]; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3260 | len = lemonStrlen(sp->name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3261 | if( len>maxlen ) maxlen = len; |
| 3262 | } |
| 3263 | ncolumns = 76/(maxlen+5); |
| 3264 | if( ncolumns<1 ) ncolumns = 1; |
| 3265 | skip = (lemp->nsymbol + ncolumns - 1)/ncolumns; |
| 3266 | for(i=0; i<skip; i++){ |
| 3267 | printf("//"); |
| 3268 | for(j=i; j<lemp->nsymbol; j+=skip){ |
| 3269 | sp = lemp->symbols[j]; |
| 3270 | assert( sp->index==j ); |
| 3271 | printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name); |
| 3272 | } |
| 3273 | printf("\n"); |
| 3274 | } |
| 3275 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3276 | rule_print(stdout, rp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3277 | printf("."); |
| 3278 | if( rp->precsym ) printf(" [%s]",rp->precsym->name); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3279 | /* if( rp->code ) printf("\n %s",rp->code); */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3280 | printf("\n"); |
| 3281 | } |
| 3282 | } |
| 3283 | |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3284 | /* Print a single rule. |
| 3285 | */ |
| 3286 | void RulePrint(FILE *fp, struct rule *rp, int iCursor){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3287 | struct symbol *sp; |
| 3288 | int i, j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3289 | fprintf(fp,"%s ::=",rp->lhs->name); |
| 3290 | for(i=0; i<=rp->nrhs; i++){ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3291 | if( i==iCursor ) fprintf(fp," *"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3292 | if( i==rp->nrhs ) break; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3293 | sp = rp->rhs[i]; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3294 | if( sp->type==MULTITERMINAL ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 3295 | fprintf(fp," %s", sp->subsym[0]->name); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3296 | for(j=1; j<sp->nsubsym; j++){ |
| 3297 | fprintf(fp,"|%s",sp->subsym[j]->name); |
| 3298 | } |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 3299 | }else{ |
| 3300 | fprintf(fp," %s", sp->name); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3301 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3302 | } |
| 3303 | } |
| 3304 | |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3305 | /* Print the rule for a configuration. |
| 3306 | */ |
| 3307 | void ConfigPrint(FILE *fp, struct config *cfp){ |
| 3308 | RulePrint(fp, cfp->rp, cfp->dot); |
| 3309 | } |
| 3310 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3311 | /* #define TEST */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3312 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3313 | /* Print a set */ |
| 3314 | PRIVATE void SetPrint(out,set,lemp) |
| 3315 | FILE *out; |
| 3316 | char *set; |
| 3317 | struct lemon *lemp; |
| 3318 | { |
| 3319 | int i; |
| 3320 | char *spacer; |
| 3321 | spacer = ""; |
| 3322 | fprintf(out,"%12s[",""); |
| 3323 | for(i=0; i<lemp->nterminal; i++){ |
| 3324 | if( SetFind(set,i) ){ |
| 3325 | fprintf(out,"%s%s",spacer,lemp->symbols[i]->name); |
| 3326 | spacer = " "; |
| 3327 | } |
| 3328 | } |
| 3329 | fprintf(out,"]\n"); |
| 3330 | } |
| 3331 | |
| 3332 | /* Print a plink chain */ |
| 3333 | PRIVATE void PlinkPrint(out,plp,tag) |
| 3334 | FILE *out; |
| 3335 | struct plink *plp; |
| 3336 | char *tag; |
| 3337 | { |
| 3338 | while( plp ){ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 3339 | fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3340 | ConfigPrint(out,plp->cfp); |
| 3341 | fprintf(out,"\n"); |
| 3342 | plp = plp->next; |
| 3343 | } |
| 3344 | } |
| 3345 | #endif |
| 3346 | |
| 3347 | /* Print an action to the given file descriptor. Return FALSE if |
| 3348 | ** nothing was actually printed. |
| 3349 | */ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3350 | int PrintAction( |
| 3351 | struct action *ap, /* The action to print */ |
| 3352 | FILE *fp, /* Print the action here */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3353 | int indent /* Indent by this amount */ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3354 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3355 | int result = 1; |
| 3356 | switch( ap->type ){ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3357 | case SHIFT: { |
| 3358 | struct state *stp = ap->x.stp; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3359 | fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3360 | break; |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3361 | } |
| 3362 | case REDUCE: { |
| 3363 | struct rule *rp = ap->x.rp; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3364 | fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->iRule); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3365 | RulePrint(fp, rp, -1); |
| 3366 | break; |
| 3367 | } |
| 3368 | case SHIFTREDUCE: { |
| 3369 | struct rule *rp = ap->x.rp; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3370 | fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->iRule); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3371 | RulePrint(fp, rp, -1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3372 | break; |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3373 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3374 | case ACCEPT: |
| 3375 | fprintf(fp,"%*s accept",indent,ap->sp->name); |
| 3376 | break; |
| 3377 | case ERROR: |
| 3378 | fprintf(fp,"%*s error",indent,ap->sp->name); |
| 3379 | break; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3380 | case SRCONFLICT: |
| 3381 | case RRCONFLICT: |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3382 | fprintf(fp,"%*s reduce %-7d ** Parsing conflict **", |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3383 | indent,ap->sp->name,ap->x.rp->iRule); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3384 | break; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3385 | case SSCONFLICT: |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3386 | fprintf(fp,"%*s shift %-7d ** Parsing conflict **", |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3387 | indent,ap->sp->name,ap->x.stp->statenum); |
| 3388 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3389 | case SH_RESOLVED: |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3390 | if( showPrecedenceConflict ){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3391 | fprintf(fp,"%*s shift %-7d -- dropped by precedence", |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3392 | indent,ap->sp->name,ap->x.stp->statenum); |
| 3393 | }else{ |
| 3394 | result = 0; |
| 3395 | } |
| 3396 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3397 | case RD_RESOLVED: |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3398 | if( showPrecedenceConflict ){ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3399 | fprintf(fp,"%*s reduce %-7d -- dropped by precedence", |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3400 | indent,ap->sp->name,ap->x.rp->iRule); |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3401 | }else{ |
| 3402 | result = 0; |
| 3403 | } |
| 3404 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3405 | case NOT_USED: |
| 3406 | result = 0; |
| 3407 | break; |
| 3408 | } |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 3409 | if( result && ap->spOpt ){ |
| 3410 | fprintf(fp," /* because %s==%s */", ap->sp->name, ap->spOpt->name); |
| 3411 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3412 | return result; |
| 3413 | } |
| 3414 | |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3415 | /* Generate the "*.out" log file */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3416 | void ReportOutput(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3417 | { |
drh | 539e741 | 2018-04-21 20:24:19 +0000 | [diff] [blame] | 3418 | int i, n; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3419 | struct state *stp; |
| 3420 | struct config *cfp; |
| 3421 | struct action *ap; |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3422 | struct rule *rp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3423 | FILE *fp; |
| 3424 | |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 3425 | fp = file_open(lemp,".out","wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3426 | if( fp==0 ) return; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3427 | for(i=0; i<lemp->nxstate; i++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3428 | stp = lemp->sorted[i]; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 3429 | fprintf(fp,"State %d:\n",stp->statenum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3430 | if( lemp->basisflag ) cfp=stp->bp; |
| 3431 | else cfp=stp->cfp; |
| 3432 | while( cfp ){ |
| 3433 | char buf[20]; |
| 3434 | if( cfp->dot==cfp->rp->nrhs ){ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3435 | lemon_sprintf(buf,"(%d)",cfp->rp->iRule); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3436 | fprintf(fp," %5s ",buf); |
| 3437 | }else{ |
| 3438 | fprintf(fp," "); |
| 3439 | } |
| 3440 | ConfigPrint(fp,cfp); |
| 3441 | fprintf(fp,"\n"); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3442 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3443 | SetPrint(fp,cfp->fws,lemp); |
| 3444 | PlinkPrint(fp,cfp->fplp,"To "); |
| 3445 | PlinkPrint(fp,cfp->bplp,"From"); |
| 3446 | #endif |
| 3447 | if( lemp->basisflag ) cfp=cfp->bp; |
| 3448 | else cfp=cfp->next; |
| 3449 | } |
| 3450 | fprintf(fp,"\n"); |
| 3451 | for(ap=stp->ap; ap; ap=ap->next){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3452 | if( PrintAction(ap,fp,30) ) fprintf(fp,"\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3453 | } |
| 3454 | fprintf(fp,"\n"); |
| 3455 | } |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 3456 | fprintf(fp, "----------------------------------------------------\n"); |
| 3457 | fprintf(fp, "Symbols:\n"); |
drh | 539e741 | 2018-04-21 20:24:19 +0000 | [diff] [blame] | 3458 | fprintf(fp, "The first-set of non-terminals is shown after the name.\n\n"); |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 3459 | for(i=0; i<lemp->nsymbol; i++){ |
| 3460 | int j; |
| 3461 | struct symbol *sp; |
| 3462 | |
| 3463 | sp = lemp->symbols[i]; |
| 3464 | fprintf(fp, " %3d: %s", i, sp->name); |
| 3465 | if( sp->type==NONTERMINAL ){ |
| 3466 | fprintf(fp, ":"); |
| 3467 | if( sp->lambda ){ |
| 3468 | fprintf(fp, " <lambda>"); |
| 3469 | } |
| 3470 | for(j=0; j<lemp->nterminal; j++){ |
| 3471 | if( sp->firstset && SetFind(sp->firstset, j) ){ |
| 3472 | fprintf(fp, " %s", lemp->symbols[j]->name); |
| 3473 | } |
| 3474 | } |
| 3475 | } |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3476 | if( sp->prec>=0 ) fprintf(fp," (precedence=%d)", sp->prec); |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 3477 | fprintf(fp, "\n"); |
| 3478 | } |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3479 | fprintf(fp, "----------------------------------------------------\n"); |
drh | 539e741 | 2018-04-21 20:24:19 +0000 | [diff] [blame] | 3480 | fprintf(fp, "Syntax-only Symbols:\n"); |
| 3481 | fprintf(fp, "The following symbols never carry semantic content.\n\n"); |
| 3482 | for(i=n=0; i<lemp->nsymbol; i++){ |
| 3483 | int w; |
| 3484 | struct symbol *sp = lemp->symbols[i]; |
| 3485 | if( sp->bContent ) continue; |
| 3486 | w = (int)strlen(sp->name); |
| 3487 | if( n>0 && n+w>75 ){ |
| 3488 | fprintf(fp,"\n"); |
| 3489 | n = 0; |
| 3490 | } |
| 3491 | if( n>0 ){ |
| 3492 | fprintf(fp, " "); |
| 3493 | n++; |
| 3494 | } |
| 3495 | fprintf(fp, "%s", sp->name); |
| 3496 | n += w; |
| 3497 | } |
| 3498 | if( n>0 ) fprintf(fp, "\n"); |
| 3499 | fprintf(fp, "----------------------------------------------------\n"); |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3500 | fprintf(fp, "Rules:\n"); |
| 3501 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 3502 | fprintf(fp, "%4d: ", rp->iRule); |
| 3503 | rule_print(fp, rp); |
| 3504 | fprintf(fp,"."); |
| 3505 | if( rp->precsym ){ |
| 3506 | fprintf(fp," [%s precedence=%d]", |
| 3507 | rp->precsym->name, rp->precsym->prec); |
| 3508 | } |
| 3509 | fprintf(fp,"\n"); |
| 3510 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3511 | fclose(fp); |
| 3512 | return; |
| 3513 | } |
| 3514 | |
| 3515 | /* Search for the file "name" which is in the same directory as |
| 3516 | ** the exacutable */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3517 | PRIVATE char *pathsearch(char *argv0, char *name, int modemask) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3518 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3519 | const char *pathlist; |
| 3520 | char *pathbufptr; |
| 3521 | char *pathbuf; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3522 | char *path,*cp; |
| 3523 | char c; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3524 | |
| 3525 | #ifdef __WIN32__ |
| 3526 | cp = strrchr(argv0,'\\'); |
| 3527 | #else |
| 3528 | cp = strrchr(argv0,'/'); |
| 3529 | #endif |
| 3530 | if( cp ){ |
| 3531 | c = *cp; |
| 3532 | *cp = 0; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3533 | path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 ); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3534 | if( path ) lemon_sprintf(path,"%s/%s",argv0,name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3535 | *cp = c; |
| 3536 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3537 | pathlist = getenv("PATH"); |
| 3538 | if( pathlist==0 ) pathlist = ".:/bin:/usr/bin"; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3539 | pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 ); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3540 | path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 ); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3541 | if( (pathbuf != 0) && (path!=0) ){ |
| 3542 | pathbufptr = pathbuf; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3543 | lemon_strcpy(pathbuf, pathlist); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3544 | while( *pathbuf ){ |
| 3545 | cp = strchr(pathbuf,':'); |
| 3546 | if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3547 | c = *cp; |
| 3548 | *cp = 0; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3549 | lemon_sprintf(path,"%s/%s",pathbuf,name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3550 | *cp = c; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3551 | if( c==0 ) pathbuf[0] = 0; |
| 3552 | else pathbuf = &cp[1]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3553 | if( access(path,modemask)==0 ) break; |
| 3554 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3555 | free(pathbufptr); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3556 | } |
| 3557 | } |
| 3558 | return path; |
| 3559 | } |
| 3560 | |
| 3561 | /* Given an action, compute the integer value for that action |
| 3562 | ** which is to be put in the action table of the generated machine. |
| 3563 | ** Return negative if no action should be generated. |
| 3564 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3565 | PRIVATE int compute_action(struct lemon *lemp, struct action *ap) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3566 | { |
| 3567 | int act; |
| 3568 | switch( ap->type ){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3569 | case SHIFT: act = ap->x.stp->statenum; break; |
drh | bd8fcc1 | 2017-06-28 11:56:18 +0000 | [diff] [blame] | 3570 | case SHIFTREDUCE: { |
drh | bd8fcc1 | 2017-06-28 11:56:18 +0000 | [diff] [blame] | 3571 | /* Since a SHIFT is inherient after a prior REDUCE, convert any |
| 3572 | ** SHIFTREDUCE action with a nonterminal on the LHS into a simple |
| 3573 | ** REDUCE action: */ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3574 | if( ap->sp->index>=lemp->nterminal ){ |
| 3575 | act = lemp->minReduce + ap->x.rp->iRule; |
| 3576 | }else{ |
| 3577 | act = lemp->minShiftReduce + ap->x.rp->iRule; |
| 3578 | } |
drh | bd8fcc1 | 2017-06-28 11:56:18 +0000 | [diff] [blame] | 3579 | break; |
| 3580 | } |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3581 | case REDUCE: act = lemp->minReduce + ap->x.rp->iRule; break; |
| 3582 | case ERROR: act = lemp->errAction; break; |
| 3583 | case ACCEPT: act = lemp->accAction; break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3584 | default: act = -1; break; |
| 3585 | } |
| 3586 | return act; |
| 3587 | } |
| 3588 | |
| 3589 | #define LINESIZE 1000 |
| 3590 | /* The next cluster of routines are for reading the template file |
| 3591 | ** and writing the results to the generated parser */ |
| 3592 | /* The first function transfers data from "in" to "out" until |
| 3593 | ** a line is seen which begins with "%%". The line number is |
| 3594 | ** tracked. |
| 3595 | ** |
| 3596 | ** if name!=0, then any word that begin with "Parse" is changed to |
| 3597 | ** begin with *name instead. |
| 3598 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3599 | PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3600 | { |
| 3601 | int i, iStart; |
| 3602 | char line[LINESIZE]; |
| 3603 | while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){ |
| 3604 | (*lineno)++; |
| 3605 | iStart = 0; |
| 3606 | if( name ){ |
| 3607 | for(i=0; line[i]; i++){ |
| 3608 | if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0 |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3609 | && (i==0 || !ISALPHA(line[i-1])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3610 | ){ |
| 3611 | if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]); |
| 3612 | fprintf(out,"%s",name); |
| 3613 | i += 4; |
| 3614 | iStart = i+1; |
| 3615 | } |
| 3616 | } |
| 3617 | } |
| 3618 | fprintf(out,"%s",&line[iStart]); |
| 3619 | } |
| 3620 | } |
| 3621 | |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 3622 | /* Skip forward past the header of the template file to the first "%%" |
| 3623 | */ |
| 3624 | PRIVATE void tplt_skip_header(FILE *in, int *lineno) |
| 3625 | { |
| 3626 | char line[LINESIZE]; |
| 3627 | while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){ |
| 3628 | (*lineno)++; |
| 3629 | } |
| 3630 | } |
| 3631 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3632 | /* The next function finds the template file and opens it, returning |
| 3633 | ** a pointer to the opened file. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3634 | PRIVATE FILE *tplt_open(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3635 | { |
| 3636 | static char templatename[] = "lempar.c"; |
| 3637 | char buf[1000]; |
| 3638 | FILE *in; |
| 3639 | char *tpltname; |
| 3640 | char *cp; |
| 3641 | |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 3642 | /* first, see if user specified a template filename on the command line. */ |
| 3643 | if (user_templatename != 0) { |
| 3644 | if( access(user_templatename,004)==-1 ){ |
| 3645 | fprintf(stderr,"Can't find the parser driver template file \"%s\".\n", |
| 3646 | user_templatename); |
| 3647 | lemp->errorcnt++; |
| 3648 | return 0; |
| 3649 | } |
| 3650 | in = fopen(user_templatename,"rb"); |
| 3651 | if( in==0 ){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3652 | fprintf(stderr,"Can't open the template file \"%s\".\n", |
| 3653 | user_templatename); |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 3654 | lemp->errorcnt++; |
| 3655 | return 0; |
| 3656 | } |
| 3657 | return in; |
| 3658 | } |
| 3659 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3660 | cp = strrchr(lemp->filename,'.'); |
| 3661 | if( cp ){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3662 | lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3663 | }else{ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3664 | lemon_sprintf(buf,"%s.lt",lemp->filename); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3665 | } |
| 3666 | if( access(buf,004)==0 ){ |
| 3667 | tpltname = buf; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3668 | }else if( access(templatename,004)==0 ){ |
| 3669 | tpltname = templatename; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3670 | }else{ |
| 3671 | tpltname = pathsearch(lemp->argv0,templatename,0); |
| 3672 | } |
| 3673 | if( tpltname==0 ){ |
| 3674 | fprintf(stderr,"Can't find the parser driver template file \"%s\".\n", |
| 3675 | templatename); |
| 3676 | lemp->errorcnt++; |
| 3677 | return 0; |
| 3678 | } |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 3679 | in = fopen(tpltname,"rb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3680 | if( in==0 ){ |
| 3681 | fprintf(stderr,"Can't open the template file \"%s\".\n",templatename); |
| 3682 | lemp->errorcnt++; |
| 3683 | return 0; |
| 3684 | } |
| 3685 | return in; |
| 3686 | } |
| 3687 | |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3688 | /* Print a #line directive line to the output file. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3689 | PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename) |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3690 | { |
| 3691 | fprintf(out,"#line %d \"",lineno); |
| 3692 | while( *filename ){ |
| 3693 | if( *filename == '\\' ) putc('\\',out); |
| 3694 | putc(*filename,out); |
| 3695 | filename++; |
| 3696 | } |
| 3697 | fprintf(out,"\"\n"); |
| 3698 | } |
| 3699 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3700 | /* Print a string to the file and keep the linenumber up to date */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3701 | PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3702 | { |
| 3703 | if( str==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3704 | while( *str ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3705 | putc(*str,out); |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3706 | if( *str=='\n' ) (*lineno)++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3707 | str++; |
| 3708 | } |
drh | 9db55df | 2004-09-09 14:01:21 +0000 | [diff] [blame] | 3709 | if( str[-1]!='\n' ){ |
| 3710 | putc('\n',out); |
| 3711 | (*lineno)++; |
| 3712 | } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3713 | if (!lemp->nolinenosflag) { |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3714 | (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3715 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3716 | return; |
| 3717 | } |
| 3718 | |
| 3719 | /* |
| 3720 | ** The following routine emits code for the destructor for the |
| 3721 | ** symbol sp |
| 3722 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3723 | void emit_destructor_code( |
| 3724 | FILE *out, |
| 3725 | struct symbol *sp, |
| 3726 | struct lemon *lemp, |
| 3727 | int *lineno |
| 3728 | ){ |
drh | cc83b6e | 2004-04-23 23:38:42 +0000 | [diff] [blame] | 3729 | char *cp = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3730 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3731 | if( sp->type==TERMINAL ){ |
| 3732 | cp = lemp->tokendest; |
| 3733 | if( cp==0 ) return; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 3734 | fprintf(out,"{\n"); (*lineno)++; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3735 | }else if( sp->destructor ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3736 | cp = sp->destructor; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 3737 | fprintf(out,"{\n"); (*lineno)++; |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3738 | if( !lemp->nolinenosflag ){ |
| 3739 | (*lineno)++; |
| 3740 | tplt_linedir(out,sp->destLineno,lemp->filename); |
| 3741 | } |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3742 | }else if( lemp->vardest ){ |
| 3743 | cp = lemp->vardest; |
| 3744 | if( cp==0 ) return; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 3745 | fprintf(out,"{\n"); (*lineno)++; |
drh | cc83b6e | 2004-04-23 23:38:42 +0000 | [diff] [blame] | 3746 | }else{ |
| 3747 | assert( 0 ); /* Cannot happen */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3748 | } |
| 3749 | for(; *cp; cp++){ |
| 3750 | if( *cp=='$' && cp[1]=='$' ){ |
| 3751 | fprintf(out,"(yypminor->yy%d)",sp->dtnum); |
| 3752 | cp++; |
| 3753 | continue; |
| 3754 | } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3755 | if( *cp=='\n' ) (*lineno)++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3756 | fputc(*cp,out); |
| 3757 | } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3758 | fprintf(out,"\n"); (*lineno)++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3759 | if (!lemp->nolinenosflag) { |
| 3760 | (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3761 | } |
| 3762 | fprintf(out,"}\n"); (*lineno)++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3763 | return; |
| 3764 | } |
| 3765 | |
| 3766 | /* |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3767 | ** Return TRUE (non-zero) if the given symbol has a destructor. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3768 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3769 | int has_destructor(struct symbol *sp, struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3770 | { |
| 3771 | int ret; |
| 3772 | if( sp->type==TERMINAL ){ |
| 3773 | ret = lemp->tokendest!=0; |
| 3774 | }else{ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3775 | ret = lemp->vardest!=0 || sp->destructor!=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3776 | } |
| 3777 | return ret; |
| 3778 | } |
| 3779 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3780 | /* |
| 3781 | ** Append text to a dynamically allocated string. If zText is 0 then |
| 3782 | ** reset the string to be empty again. Always return the complete text |
| 3783 | ** of the string (which is overwritten with each call). |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3784 | ** |
| 3785 | ** n bytes of zText are stored. If n==0 then all of zText up to the first |
| 3786 | ** \000 terminator is stored. zText can contain up to two instances of |
| 3787 | ** %d. The values of p1 and p2 are written into the first and second |
| 3788 | ** %d. |
| 3789 | ** |
| 3790 | ** If n==-1, then the previous character is overwritten. |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3791 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3792 | PRIVATE char *append_str(const char *zText, int n, int p1, int p2){ |
| 3793 | static char empty[1] = { 0 }; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3794 | static char *z = 0; |
| 3795 | static int alloced = 0; |
| 3796 | static int used = 0; |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3797 | int c; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3798 | char zInt[40]; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3799 | if( zText==0 ){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3800 | if( used==0 && z!=0 ) z[0] = 0; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3801 | used = 0; |
| 3802 | return z; |
| 3803 | } |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3804 | if( n<=0 ){ |
| 3805 | if( n<0 ){ |
| 3806 | used += n; |
| 3807 | assert( used>=0 ); |
| 3808 | } |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3809 | n = lemonStrlen(zText); |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3810 | } |
drh | df60971 | 2010-11-23 20:55:27 +0000 | [diff] [blame] | 3811 | if( (int) (n+sizeof(zInt)*2+used) >= alloced ){ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3812 | alloced = n + sizeof(zInt)*2 + used + 200; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3813 | z = (char *) realloc(z, alloced); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3814 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3815 | if( z==0 ) return empty; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3816 | while( n-- > 0 ){ |
| 3817 | c = *(zText++); |
drh | 5048962 | 2006-10-13 12:25:29 +0000 | [diff] [blame] | 3818 | if( c=='%' && n>0 && zText[0]=='d' ){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3819 | lemon_sprintf(zInt, "%d", p1); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3820 | p1 = p2; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3821 | lemon_strcpy(&z[used], zInt); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3822 | used += lemonStrlen(&z[used]); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3823 | zText++; |
| 3824 | n--; |
| 3825 | }else{ |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 3826 | z[used++] = (char)c; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3827 | } |
| 3828 | } |
| 3829 | z[used] = 0; |
| 3830 | return z; |
| 3831 | } |
| 3832 | |
| 3833 | /* |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3834 | ** Write and transform the rp->code string so that symbols are expanded. |
| 3835 | ** Populate the rp->codePrefix and rp->codeSuffix strings, as appropriate. |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3836 | ** |
| 3837 | ** Return 1 if the expanded code requires that "yylhsminor" local variable |
| 3838 | ** to be defined. |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3839 | */ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3840 | PRIVATE int translate_code(struct lemon *lemp, struct rule *rp){ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3841 | char *cp, *xp; |
| 3842 | int i; |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3843 | int rc = 0; /* True if yylhsminor is used */ |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 3844 | int dontUseRhs0 = 0; /* If true, use of left-most RHS label is illegal */ |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3845 | const char *zSkip = 0; /* The zOvwrt comment within rp->code, or NULL */ |
| 3846 | char lhsused = 0; /* True if the LHS element has been used */ |
| 3847 | char lhsdirect; /* True if LHS writes directly into stack */ |
| 3848 | char used[MAXRHS]; /* True for each RHS element which is used */ |
| 3849 | char zLhs[50]; /* Convert the LHS symbol into this string */ |
| 3850 | char zOvwrt[900]; /* Comment that to allow LHS to overwrite RHS */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3851 | |
| 3852 | for(i=0; i<rp->nrhs; i++) used[i] = 0; |
| 3853 | lhsused = 0; |
| 3854 | |
drh | 19c9e56 | 2007-03-29 20:13:53 +0000 | [diff] [blame] | 3855 | if( rp->code==0 ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3856 | static char newlinestr[2] = { '\n', '\0' }; |
| 3857 | rp->code = newlinestr; |
drh | 19c9e56 | 2007-03-29 20:13:53 +0000 | [diff] [blame] | 3858 | rp->line = rp->ruleline; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3859 | rp->noCode = 1; |
| 3860 | }else{ |
| 3861 | rp->noCode = 0; |
drh | 19c9e56 | 2007-03-29 20:13:53 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3864 | |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 3865 | if( rp->nrhs==0 ){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3866 | /* If there are no RHS symbols, then writing directly to the LHS is ok */ |
| 3867 | lhsdirect = 1; |
| 3868 | }else if( rp->rhsalias[0]==0 ){ |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 3869 | /* The left-most RHS symbol has no value. LHS direct is ok. But |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3870 | ** we have to call the distructor on the RHS symbol first. */ |
| 3871 | lhsdirect = 1; |
| 3872 | if( has_destructor(rp->rhs[0],lemp) ){ |
| 3873 | append_str(0,0,0,0); |
| 3874 | append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0, |
| 3875 | rp->rhs[0]->index,1-rp->nrhs); |
| 3876 | rp->codePrefix = Strsafe(append_str(0,0,0,0)); |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3877 | rp->noCode = 0; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3878 | } |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 3879 | }else if( rp->lhsalias==0 ){ |
| 3880 | /* There is no LHS value symbol. */ |
| 3881 | lhsdirect = 1; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3882 | }else if( strcmp(rp->lhsalias,rp->rhsalias[0])==0 ){ |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3883 | /* The LHS symbol and the left-most RHS symbol are the same, so |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3884 | ** direct writing is allowed */ |
| 3885 | lhsdirect = 1; |
| 3886 | lhsused = 1; |
| 3887 | used[0] = 1; |
| 3888 | if( rp->lhs->dtnum!=rp->rhs[0]->dtnum ){ |
| 3889 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3890 | "%s(%s) and %s(%s) share the same label but have " |
| 3891 | "different datatypes.", |
| 3892 | rp->lhs->name, rp->lhsalias, rp->rhs[0]->name, rp->rhsalias[0]); |
| 3893 | lemp->errorcnt++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3894 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3895 | }else{ |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3896 | lemon_sprintf(zOvwrt, "/*%s-overwrites-%s*/", |
| 3897 | rp->lhsalias, rp->rhsalias[0]); |
| 3898 | zSkip = strstr(rp->code, zOvwrt); |
| 3899 | if( zSkip!=0 ){ |
| 3900 | /* The code contains a special comment that indicates that it is safe |
| 3901 | ** for the LHS label to overwrite left-most RHS label. */ |
| 3902 | lhsdirect = 1; |
| 3903 | }else{ |
| 3904 | lhsdirect = 0; |
| 3905 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3906 | } |
| 3907 | if( lhsdirect ){ |
| 3908 | sprintf(zLhs, "yymsp[%d].minor.yy%d",1-rp->nrhs,rp->lhs->dtnum); |
| 3909 | }else{ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3910 | rc = 1; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3911 | sprintf(zLhs, "yylhsminor.yy%d",rp->lhs->dtnum); |
| 3912 | } |
| 3913 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3914 | append_str(0,0,0,0); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3915 | |
| 3916 | /* This const cast is wrong but harmless, if we're careful. */ |
| 3917 | for(cp=(char *)rp->code; *cp; cp++){ |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3918 | if( cp==zSkip ){ |
| 3919 | append_str(zOvwrt,0,0,0); |
| 3920 | cp += lemonStrlen(zOvwrt)-1; |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 3921 | dontUseRhs0 = 1; |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3922 | continue; |
| 3923 | } |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3924 | if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3925 | char saved; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3926 | for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3927 | saved = *xp; |
| 3928 | *xp = 0; |
| 3929 | if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3930 | append_str(zLhs,0,0,0); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3931 | cp = xp; |
| 3932 | lhsused = 1; |
| 3933 | }else{ |
| 3934 | for(i=0; i<rp->nrhs; i++){ |
| 3935 | if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){ |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 3936 | if( i==0 && dontUseRhs0 ){ |
| 3937 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3938 | "Label %s used after '%s'.", |
| 3939 | rp->rhsalias[0], zOvwrt); |
| 3940 | lemp->errorcnt++; |
| 3941 | }else if( cp!=rp->code && cp[-1]=='@' ){ |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3942 | /* If the argument is of the form @X then substituted |
| 3943 | ** the token number of X, not the value of X */ |
| 3944 | append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0); |
| 3945 | }else{ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3946 | struct symbol *sp = rp->rhs[i]; |
| 3947 | int dtnum; |
| 3948 | if( sp->type==MULTITERMINAL ){ |
| 3949 | dtnum = sp->subsym[0]->dtnum; |
| 3950 | }else{ |
| 3951 | dtnum = sp->dtnum; |
| 3952 | } |
| 3953 | append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum); |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3954 | } |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3955 | cp = xp; |
| 3956 | used[i] = 1; |
| 3957 | break; |
| 3958 | } |
| 3959 | } |
| 3960 | } |
| 3961 | *xp = saved; |
| 3962 | } |
| 3963 | append_str(cp, 1, 0, 0); |
| 3964 | } /* End loop */ |
| 3965 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3966 | /* Main code generation completed */ |
| 3967 | cp = append_str(0,0,0,0); |
| 3968 | if( cp && cp[0] ) rp->code = Strsafe(cp); |
| 3969 | append_str(0,0,0,0); |
| 3970 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3971 | /* Check to make sure the LHS has been used */ |
| 3972 | if( rp->lhsalias && !lhsused ){ |
| 3973 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3974 | "Label \"%s\" for \"%s(%s)\" is never used.", |
| 3975 | rp->lhsalias,rp->lhs->name,rp->lhsalias); |
| 3976 | lemp->errorcnt++; |
| 3977 | } |
| 3978 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3979 | /* Generate destructor code for RHS minor values which are not referenced. |
| 3980 | ** Generate error messages for unused labels and duplicate labels. |
| 3981 | */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3982 | for(i=0; i<rp->nrhs; i++){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3983 | if( rp->rhsalias[i] ){ |
| 3984 | if( i>0 ){ |
| 3985 | int j; |
| 3986 | if( rp->lhsalias && strcmp(rp->lhsalias,rp->rhsalias[i])==0 ){ |
| 3987 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3988 | "%s(%s) has the same label as the LHS but is not the left-most " |
| 3989 | "symbol on the RHS.", |
drh | f135cb7 | 2019-04-30 14:26:31 +0000 | [diff] [blame] | 3990 | rp->rhs[i]->name, rp->rhsalias[i]); |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3991 | lemp->errorcnt++; |
| 3992 | } |
| 3993 | for(j=0; j<i; j++){ |
| 3994 | if( rp->rhsalias[j] && strcmp(rp->rhsalias[j],rp->rhsalias[i])==0 ){ |
| 3995 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3996 | "Label %s used for multiple symbols on the RHS of a rule.", |
| 3997 | rp->rhsalias[i]); |
| 3998 | lemp->errorcnt++; |
| 3999 | break; |
| 4000 | } |
| 4001 | } |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4002 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 4003 | if( !used[i] ){ |
| 4004 | ErrorMsg(lemp->filename,rp->ruleline, |
| 4005 | "Label %s for \"%s(%s)\" is never used.", |
| 4006 | rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]); |
| 4007 | lemp->errorcnt++; |
| 4008 | } |
| 4009 | }else if( i>0 && has_destructor(rp->rhs[i],lemp) ){ |
| 4010 | append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0, |
| 4011 | rp->rhs[i]->index,i-rp->nrhs+1); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4012 | } |
| 4013 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 4014 | |
| 4015 | /* If unable to write LHS values directly into the stack, write the |
| 4016 | ** saved LHS value now. */ |
| 4017 | if( lhsdirect==0 ){ |
| 4018 | append_str(" yymsp[%d].minor.yy%d = ", 0, 1-rp->nrhs, rp->lhs->dtnum); |
| 4019 | append_str(zLhs, 0, 0, 0); |
| 4020 | append_str(";\n", 0, 0, 0); |
drh | 61e339a | 2007-01-16 03:09:02 +0000 | [diff] [blame] | 4021 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 4022 | |
| 4023 | /* Suffix code generation complete */ |
| 4024 | cp = append_str(0,0,0,0); |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4025 | if( cp && cp[0] ){ |
| 4026 | rp->codeSuffix = Strsafe(cp); |
| 4027 | rp->noCode = 0; |
| 4028 | } |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 4029 | |
| 4030 | return rc; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4031 | } |
| 4032 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4033 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4034 | ** Generate code which executes when the rule "rp" is reduced. Write |
| 4035 | ** the code to "out". Make sure lineno stays up-to-date. |
| 4036 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4037 | PRIVATE void emit_code( |
| 4038 | FILE *out, |
| 4039 | struct rule *rp, |
| 4040 | struct lemon *lemp, |
| 4041 | int *lineno |
| 4042 | ){ |
| 4043 | const char *cp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4044 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 4045 | /* Setup code prior to the #line directive */ |
| 4046 | if( rp->codePrefix && rp->codePrefix[0] ){ |
| 4047 | fprintf(out, "{%s", rp->codePrefix); |
| 4048 | for(cp=rp->codePrefix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } |
| 4049 | } |
| 4050 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4051 | /* Generate code to do the reduce action */ |
| 4052 | if( rp->code ){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 4053 | if( !lemp->nolinenosflag ){ |
| 4054 | (*lineno)++; |
| 4055 | tplt_linedir(out,rp->line,lemp->filename); |
| 4056 | } |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 4057 | fprintf(out,"{%s",rp->code); |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 4058 | for(cp=rp->code; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 4059 | fprintf(out,"}\n"); (*lineno)++; |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 4060 | if( !lemp->nolinenosflag ){ |
| 4061 | (*lineno)++; |
| 4062 | tplt_linedir(out,*lineno,lemp->outname); |
| 4063 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | /* Generate breakdown code that occurs after the #line directive */ |
| 4067 | if( rp->codeSuffix && rp->codeSuffix[0] ){ |
| 4068 | fprintf(out, "%s", rp->codeSuffix); |
| 4069 | for(cp=rp->codeSuffix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } |
| 4070 | } |
| 4071 | |
| 4072 | if( rp->codePrefix ){ |
| 4073 | fprintf(out, "}\n"); (*lineno)++; |
| 4074 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4075 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4076 | return; |
| 4077 | } |
| 4078 | |
| 4079 | /* |
| 4080 | ** Print the definition of the union used for the parser's data stack. |
| 4081 | ** This union contains fields for every possible data type for tokens |
| 4082 | ** and nonterminals. In the process of computing and printing this |
| 4083 | ** union, also set the ".dtnum" field of every terminal and nonterminal |
| 4084 | ** symbol. |
| 4085 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4086 | void print_stack_union( |
| 4087 | FILE *out, /* The output stream */ |
| 4088 | struct lemon *lemp, /* The main info structure for this parser */ |
| 4089 | int *plineno, /* Pointer to the line number */ |
| 4090 | int mhflag /* True if generating makeheaders output */ |
| 4091 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4092 | int lineno = *plineno; /* The line number of the output */ |
| 4093 | char **types; /* A hash table of datatypes */ |
| 4094 | int arraysize; /* Size of the "types" array */ |
| 4095 | int maxdtlength; /* Maximum length of any ".datatype" field. */ |
| 4096 | char *stddt; /* Standardized name for a datatype */ |
| 4097 | int i,j; /* Loop counters */ |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 4098 | unsigned hash; /* For hashing the name of a type */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4099 | const char *name; /* Name of the parser */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4100 | |
| 4101 | /* Allocate and initialize types[] and allocate stddt[] */ |
| 4102 | arraysize = lemp->nsymbol * 2; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 4103 | types = (char**)calloc( arraysize, sizeof(char*) ); |
drh | 070d422 | 2011-06-02 15:48:51 +0000 | [diff] [blame] | 4104 | if( types==0 ){ |
| 4105 | fprintf(stderr,"Out of memory.\n"); |
| 4106 | exit(1); |
| 4107 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4108 | for(i=0; i<arraysize; i++) types[i] = 0; |
| 4109 | maxdtlength = 0; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 4110 | if( lemp->vartype ){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 4111 | maxdtlength = lemonStrlen(lemp->vartype); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 4112 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4113 | for(i=0; i<lemp->nsymbol; i++){ |
| 4114 | int len; |
| 4115 | struct symbol *sp = lemp->symbols[i]; |
| 4116 | if( sp->datatype==0 ) continue; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 4117 | len = lemonStrlen(sp->datatype); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4118 | if( len>maxdtlength ) maxdtlength = len; |
| 4119 | } |
| 4120 | stddt = (char*)malloc( maxdtlength*2 + 1 ); |
drh | 070d422 | 2011-06-02 15:48:51 +0000 | [diff] [blame] | 4121 | if( stddt==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4122 | fprintf(stderr,"Out of memory.\n"); |
| 4123 | exit(1); |
| 4124 | } |
| 4125 | |
| 4126 | /* Build a hash table of datatypes. The ".dtnum" field of each symbol |
| 4127 | ** 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] | 4128 | ** used for terminal symbols. If there is no %default_type defined then |
| 4129 | ** 0 is also used as the .dtnum value for nonterminals which do not specify |
| 4130 | ** a datatype using the %type directive. |
| 4131 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4132 | for(i=0; i<lemp->nsymbol; i++){ |
| 4133 | struct symbol *sp = lemp->symbols[i]; |
| 4134 | char *cp; |
| 4135 | if( sp==lemp->errsym ){ |
| 4136 | sp->dtnum = arraysize+1; |
| 4137 | continue; |
| 4138 | } |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 4139 | if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4140 | sp->dtnum = 0; |
| 4141 | continue; |
| 4142 | } |
| 4143 | cp = sp->datatype; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 4144 | if( cp==0 ) cp = lemp->vartype; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4145 | j = 0; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 4146 | while( ISSPACE(*cp) ) cp++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4147 | while( *cp ) stddt[j++] = *cp++; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 4148 | while( j>0 && ISSPACE(stddt[j-1]) ) j--; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4149 | stddt[j] = 0; |
drh | 02368c9 | 2009-04-05 15:18:02 +0000 | [diff] [blame] | 4150 | if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){ |
drh | 32c4d74 | 2008-07-01 16:34:49 +0000 | [diff] [blame] | 4151 | sp->dtnum = 0; |
| 4152 | continue; |
| 4153 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4154 | hash = 0; |
| 4155 | for(j=0; stddt[j]; j++){ |
| 4156 | hash = hash*53 + stddt[j]; |
| 4157 | } |
drh | 3b2129c | 2003-05-13 00:34:21 +0000 | [diff] [blame] | 4158 | hash = (hash & 0x7fffffff)%arraysize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4159 | while( types[hash] ){ |
| 4160 | if( strcmp(types[hash],stddt)==0 ){ |
| 4161 | sp->dtnum = hash + 1; |
| 4162 | break; |
| 4163 | } |
| 4164 | hash++; |
drh | 2b51f21 | 2013-10-11 23:01:02 +0000 | [diff] [blame] | 4165 | if( hash>=(unsigned)arraysize ) hash = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4166 | } |
| 4167 | if( types[hash]==0 ){ |
| 4168 | sp->dtnum = hash + 1; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 4169 | types[hash] = (char*)malloc( lemonStrlen(stddt)+1 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4170 | if( types[hash]==0 ){ |
| 4171 | fprintf(stderr,"Out of memory.\n"); |
| 4172 | exit(1); |
| 4173 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 4174 | lemon_strcpy(types[hash],stddt); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | |
| 4178 | /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */ |
| 4179 | name = lemp->name ? lemp->name : "Parse"; |
| 4180 | lineno = *plineno; |
| 4181 | if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; } |
| 4182 | fprintf(out,"#define %sTOKENTYPE %s\n",name, |
| 4183 | lemp->tokentype?lemp->tokentype:"void*"); lineno++; |
| 4184 | if( mhflag ){ fprintf(out,"#endif\n"); lineno++; } |
| 4185 | fprintf(out,"typedef union {\n"); lineno++; |
drh | 15b024c | 2008-12-11 02:20:43 +0000 | [diff] [blame] | 4186 | fprintf(out," int yyinit;\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4187 | fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++; |
| 4188 | for(i=0; i<arraysize; i++){ |
| 4189 | if( types[i]==0 ) continue; |
| 4190 | fprintf(out," %s yy%d;\n",types[i],i+1); lineno++; |
| 4191 | free(types[i]); |
| 4192 | } |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 4193 | if( lemp->errsym && lemp->errsym->useCnt ){ |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4194 | fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++; |
| 4195 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4196 | free(stddt); |
| 4197 | free(types); |
| 4198 | fprintf(out,"} YYMINORTYPE;\n"); lineno++; |
| 4199 | *plineno = lineno; |
| 4200 | } |
| 4201 | |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4202 | /* |
| 4203 | ** Return the name of a C datatype able to represent values between |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4204 | ** lwr and upr, inclusive. If pnByte!=NULL then also write the sizeof |
| 4205 | ** for that type (1, 2, or 4) into *pnByte. |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4206 | */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4207 | static const char *minimum_size_type(int lwr, int upr, int *pnByte){ |
| 4208 | const char *zType = "int"; |
| 4209 | int nByte = 4; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4210 | if( lwr>=0 ){ |
| 4211 | if( upr<=255 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4212 | zType = "unsigned char"; |
| 4213 | nByte = 1; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4214 | }else if( upr<65535 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4215 | zType = "unsigned short int"; |
| 4216 | nByte = 2; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4217 | }else{ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4218 | zType = "unsigned int"; |
| 4219 | nByte = 4; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4220 | } |
| 4221 | }else if( lwr>=-127 && upr<=127 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4222 | zType = "signed char"; |
| 4223 | nByte = 1; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4224 | }else if( lwr>=-32767 && upr<32767 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4225 | zType = "short"; |
| 4226 | nByte = 2; |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4227 | } |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4228 | if( pnByte ) *pnByte = nByte; |
| 4229 | return zType; |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4232 | /* |
| 4233 | ** Each state contains a set of token transaction and a set of |
| 4234 | ** nonterminal transactions. Each of these sets makes an instance |
| 4235 | ** of the following structure. An array of these structures is used |
| 4236 | ** to order the creation of entries in the yy_action[] table. |
| 4237 | */ |
| 4238 | struct axset { |
| 4239 | struct state *stp; /* A pointer to a state */ |
| 4240 | int isTkn; /* True to use tokens. False for non-terminals */ |
| 4241 | int nAction; /* Number of actions */ |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4242 | int iOrder; /* Original order of action sets */ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4243 | }; |
| 4244 | |
| 4245 | /* |
| 4246 | ** Compare to axset structures for sorting purposes |
| 4247 | */ |
| 4248 | static int axset_compare(const void *a, const void *b){ |
| 4249 | struct axset *p1 = (struct axset*)a; |
| 4250 | struct axset *p2 = (struct axset*)b; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4251 | int c; |
| 4252 | c = p2->nAction - p1->nAction; |
| 4253 | if( c==0 ){ |
drh | 337cd0d | 2015-09-07 23:40:42 +0000 | [diff] [blame] | 4254 | c = p1->iOrder - p2->iOrder; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4255 | } |
| 4256 | assert( c!=0 || p1==p2 ); |
| 4257 | return c; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4260 | /* |
| 4261 | ** Write text on "out" that describes the rule "rp". |
| 4262 | */ |
| 4263 | static void writeRuleText(FILE *out, struct rule *rp){ |
| 4264 | int j; |
| 4265 | fprintf(out,"%s ::=", rp->lhs->name); |
| 4266 | for(j=0; j<rp->nrhs; j++){ |
| 4267 | struct symbol *sp = rp->rhs[j]; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4268 | if( sp->type!=MULTITERMINAL ){ |
| 4269 | fprintf(out," %s", sp->name); |
| 4270 | }else{ |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4271 | int k; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4272 | fprintf(out," %s", sp->subsym[0]->name); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4273 | for(k=1; k<sp->nsubsym; k++){ |
| 4274 | fprintf(out,"|%s",sp->subsym[k]->name); |
| 4275 | } |
| 4276 | } |
| 4277 | } |
| 4278 | } |
| 4279 | |
| 4280 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4281 | /* Generate C source code for the parser */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4282 | void ReportTable( |
| 4283 | struct lemon *lemp, |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4284 | int mhflag, /* Output in makeheaders format if true */ |
| 4285 | int sqlFlag /* Generate the *.sql file too */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4286 | ){ |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4287 | FILE *out, *in, *sql; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4288 | char line[LINESIZE]; |
| 4289 | int lineno; |
| 4290 | struct state *stp; |
| 4291 | struct action *ap; |
| 4292 | struct rule *rp; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4293 | struct acttab *pActtab; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4294 | int i, j, n, sz; |
drh | 2e51716 | 2019-08-28 02:09:47 +0000 | [diff] [blame] | 4295 | int nLookAhead; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4296 | int szActionType; /* sizeof(YYACTIONTYPE) */ |
| 4297 | int szCodeType; /* sizeof(YYCODETYPE) */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4298 | const char *name; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4299 | int mnTknOfst, mxTknOfst; |
| 4300 | int mnNtOfst, mxNtOfst; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4301 | struct axset *ax; |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 4302 | char *prefix; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4303 | |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4304 | lemp->minShiftReduce = lemp->nstate; |
| 4305 | lemp->errAction = lemp->minShiftReduce + lemp->nrule; |
| 4306 | lemp->accAction = lemp->errAction + 1; |
| 4307 | lemp->noAction = lemp->accAction + 1; |
| 4308 | lemp->minReduce = lemp->noAction + 1; |
| 4309 | lemp->maxAction = lemp->minReduce + lemp->nrule; |
| 4310 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4311 | in = tplt_open(lemp); |
| 4312 | if( in==0 ) return; |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 4313 | out = file_open(lemp,".c","wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4314 | if( out==0 ){ |
| 4315 | fclose(in); |
| 4316 | return; |
| 4317 | } |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4318 | if( sqlFlag==0 ){ |
| 4319 | sql = 0; |
| 4320 | }else{ |
| 4321 | sql = file_open(lemp, ".sql", "wb"); |
| 4322 | if( sql==0 ){ |
| 4323 | fclose(in); |
| 4324 | fclose(out); |
| 4325 | return; |
| 4326 | } |
| 4327 | fprintf(sql, |
drh | 1417c2f | 2019-11-29 12:51:00 +0000 | [diff] [blame] | 4328 | "BEGIN;\n" |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4329 | "CREATE TABLE symbol(\n" |
| 4330 | " id INTEGER PRIMARY KEY,\n" |
| 4331 | " name TEXT NOT NULL,\n" |
| 4332 | " isTerminal BOOLEAN NOT NULL,\n" |
drh | 1417c2f | 2019-11-29 12:51:00 +0000 | [diff] [blame] | 4333 | " fallback INTEGER REFERENCES symbol" |
| 4334 | " DEFERRABLE INITIALLY DEFERRED\n" |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4335 | ");\n" |
| 4336 | ); |
| 4337 | for(i=0; i<lemp->nsymbol; i++){ |
| 4338 | fprintf(sql, |
| 4339 | "INSERT INTO symbol(id,name,isTerminal,fallback)" |
| 4340 | "VALUES(%d,'%s',%s", |
| 4341 | i, lemp->symbols[i]->name, |
| 4342 | i<lemp->nterminal ? "TRUE" : "FALSE" |
| 4343 | ); |
| 4344 | if( lemp->symbols[i]->fallback ){ |
| 4345 | fprintf(sql, ",%d);\n", lemp->symbols[i]->fallback->index); |
| 4346 | }else{ |
| 4347 | fprintf(sql, ",NULL);\n"); |
| 4348 | } |
| 4349 | } |
| 4350 | fprintf(sql, |
| 4351 | "CREATE TABLE rule(\n" |
| 4352 | " ruleid INTEGER PRIMARY KEY,\n" |
drh | 3e5f7fe | 2019-12-19 12:29:31 +0000 | [diff] [blame] | 4353 | " lhs INTEGER REFERENCES symbol(id),\n" |
| 4354 | " txt TEXT\n" |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4355 | ");\n" |
| 4356 | "CREATE TABLE rulerhs(\n" |
| 4357 | " ruleid INTEGER REFERENCES rule(ruleid),\n" |
| 4358 | " pos INTEGER,\n" |
| 4359 | " sym INTEGER REFERENCES symbol(id)\n" |
| 4360 | ");\n" |
| 4361 | ); |
| 4362 | for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ |
| 4363 | assert( i==rp->iRule ); |
| 4364 | fprintf(sql, |
drh | 59c5679 | 2019-12-19 13:17:07 +0000 | [diff] [blame] | 4365 | "INSERT INTO rule(ruleid,lhs,txt)VALUES(%d,%d,'", |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4366 | rp->iRule, rp->lhs->index |
| 4367 | ); |
drh | 3e5f7fe | 2019-12-19 12:29:31 +0000 | [diff] [blame] | 4368 | writeRuleText(sql, rp); |
| 4369 | fprintf(sql,"');\n"); |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4370 | for(j=0; j<rp->nrhs; j++){ |
| 4371 | struct symbol *sp = rp->rhs[j]; |
| 4372 | if( sp->type!=MULTITERMINAL ){ |
| 4373 | fprintf(sql, |
| 4374 | "INSERT INTO rulerhs(ruleid,pos,sym)VALUES(%d,%d,%d);\n", |
| 4375 | i,j,sp->index |
| 4376 | ); |
| 4377 | }else{ |
| 4378 | int k; |
| 4379 | for(k=0; k<sp->nsubsym; k++){ |
| 4380 | fprintf(sql, |
| 4381 | "INSERT INTO rulerhs(ruleid,pos,sym)VALUES(%d,%d,%d);\n", |
| 4382 | i,j,sp->subsym[k]->index |
| 4383 | ); |
| 4384 | } |
| 4385 | } |
| 4386 | } |
| 4387 | } |
drh | 1417c2f | 2019-11-29 12:51:00 +0000 | [diff] [blame] | 4388 | fprintf(sql, "COMMIT;\n"); |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4389 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4390 | lineno = 1; |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 4391 | |
drh | 512aa78 | 2020-09-01 12:26:55 +0000 | [diff] [blame] | 4392 | fprintf(out, |
| 4393 | "/* This file is automatically generated by Lemon from input grammar\n" |
| 4394 | "** source file \"%s\". */\n", lemp->filename); lineno += 2; |
| 4395 | |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 4396 | /* The first %include directive begins with a C-language comment, |
| 4397 | ** then skip over the header comment of the template file |
| 4398 | */ |
| 4399 | if( lemp->include==0 ) lemp->include = ""; |
| 4400 | for(i=0; ISSPACE(lemp->include[i]); i++){ |
| 4401 | if( lemp->include[i]=='\n' ){ |
| 4402 | lemp->include += i+1; |
| 4403 | i = -1; |
| 4404 | } |
| 4405 | } |
| 4406 | if( lemp->include[0]=='/' ){ |
| 4407 | tplt_skip_header(in,&lineno); |
| 4408 | }else{ |
| 4409 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4410 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4411 | |
| 4412 | /* Generate the include code, if any */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4413 | tplt_print(out,lemp,lemp->include,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4414 | if( mhflag ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4415 | char *incName = file_makename(lemp, ".h"); |
| 4416 | fprintf(out,"#include \"%s\"\n", incName); lineno++; |
| 4417 | free(incName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4418 | } |
| 4419 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4420 | |
| 4421 | /* Generate #defines for all tokens */ |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 4422 | if( lemp->tokenprefix ) prefix = lemp->tokenprefix; |
| 4423 | else prefix = ""; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4424 | if( mhflag ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4425 | fprintf(out,"#if INTERFACE\n"); lineno++; |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 4426 | }else{ |
| 4427 | fprintf(out,"#ifndef %s%s\n", prefix, lemp->symbols[1]->name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4428 | } |
drh | 60c71b0 | 2020-09-01 11:20:03 +0000 | [diff] [blame] | 4429 | for(i=1; i<lemp->nterminal; i++){ |
| 4430 | fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i); |
| 4431 | lineno++; |
| 4432 | } |
| 4433 | fprintf(out,"#endif\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4434 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4435 | |
| 4436 | /* Generate the defines */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4437 | fprintf(out,"#define YYCODETYPE %s\n", |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 4438 | minimum_size_type(0, lemp->nsymbol, &szCodeType)); lineno++; |
| 4439 | fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4440 | fprintf(out,"#define YYACTIONTYPE %s\n", |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4441 | minimum_size_type(0,lemp->maxAction,&szActionType)); lineno++; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4442 | if( lemp->wildcard ){ |
| 4443 | fprintf(out,"#define YYWILDCARD %d\n", |
| 4444 | lemp->wildcard->index); lineno++; |
| 4445 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4446 | print_stack_union(out,lemp,&lineno,mhflag); |
drh | ca44b5a | 2007-02-22 23:06:58 +0000 | [diff] [blame] | 4447 | fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4448 | if( lemp->stacksize ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4449 | fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++; |
| 4450 | }else{ |
| 4451 | fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++; |
| 4452 | } |
drh | ca44b5a | 2007-02-22 23:06:58 +0000 | [diff] [blame] | 4453 | fprintf(out, "#endif\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4454 | if( mhflag ){ |
| 4455 | fprintf(out,"#if INTERFACE\n"); lineno++; |
| 4456 | } |
| 4457 | name = lemp->name ? lemp->name : "Parse"; |
| 4458 | if( lemp->arg && lemp->arg[0] ){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 4459 | i = lemonStrlen(lemp->arg); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 4460 | while( i>=1 && ISSPACE(lemp->arg[i-1]) ) i--; |
| 4461 | while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4462 | fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++; |
| 4463 | fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++; |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 4464 | fprintf(out,"#define %sARG_PARAM ,%s\n",name,&lemp->arg[i]); lineno++; |
| 4465 | fprintf(out,"#define %sARG_FETCH %s=yypParser->%s;\n", |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4466 | name,lemp->arg,&lemp->arg[i]); lineno++; |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 4467 | fprintf(out,"#define %sARG_STORE yypParser->%s=%s;\n", |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4468 | name,&lemp->arg[i],&lemp->arg[i]); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4469 | }else{ |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 4470 | fprintf(out,"#define %sARG_SDECL\n",name); lineno++; |
| 4471 | fprintf(out,"#define %sARG_PDECL\n",name); lineno++; |
| 4472 | fprintf(out,"#define %sARG_PARAM\n",name); lineno++; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4473 | fprintf(out,"#define %sARG_FETCH\n",name); lineno++; |
| 4474 | fprintf(out,"#define %sARG_STORE\n",name); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4475 | } |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 4476 | if( lemp->ctx && lemp->ctx[0] ){ |
| 4477 | i = lemonStrlen(lemp->ctx); |
| 4478 | while( i>=1 && ISSPACE(lemp->ctx[i-1]) ) i--; |
| 4479 | while( i>=1 && (ISALNUM(lemp->ctx[i-1]) || lemp->ctx[i-1]=='_') ) i--; |
| 4480 | fprintf(out,"#define %sCTX_SDECL %s;\n",name,lemp->ctx); lineno++; |
| 4481 | fprintf(out,"#define %sCTX_PDECL ,%s\n",name,lemp->ctx); lineno++; |
| 4482 | fprintf(out,"#define %sCTX_PARAM ,%s\n",name,&lemp->ctx[i]); lineno++; |
| 4483 | fprintf(out,"#define %sCTX_FETCH %s=yypParser->%s;\n", |
| 4484 | name,lemp->ctx,&lemp->ctx[i]); lineno++; |
| 4485 | fprintf(out,"#define %sCTX_STORE yypParser->%s=%s;\n", |
| 4486 | name,&lemp->ctx[i],&lemp->ctx[i]); lineno++; |
| 4487 | }else{ |
| 4488 | fprintf(out,"#define %sCTX_SDECL\n",name); lineno++; |
| 4489 | fprintf(out,"#define %sCTX_PDECL\n",name); lineno++; |
| 4490 | fprintf(out,"#define %sCTX_PARAM\n",name); lineno++; |
| 4491 | fprintf(out,"#define %sCTX_FETCH\n",name); lineno++; |
| 4492 | fprintf(out,"#define %sCTX_STORE\n",name); lineno++; |
| 4493 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4494 | if( mhflag ){ |
| 4495 | fprintf(out,"#endif\n"); lineno++; |
| 4496 | } |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 4497 | if( lemp->errsym && lemp->errsym->useCnt ){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4498 | fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++; |
| 4499 | fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++; |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4500 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4501 | if( lemp->has_fallback ){ |
| 4502 | fprintf(out,"#define YYFALLBACK 1\n"); lineno++; |
| 4503 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4504 | |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4505 | /* Compute the action table, but do not output it yet. The action |
| 4506 | ** table must be computed before generating the YYNSTATE macro because |
| 4507 | ** we need to know how many states can be eliminated. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4508 | */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4509 | ax = (struct axset *) calloc(lemp->nxstate*2, sizeof(ax[0])); |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4510 | if( ax==0 ){ |
| 4511 | fprintf(stderr,"malloc failed\n"); |
| 4512 | exit(1); |
| 4513 | } |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4514 | for(i=0; i<lemp->nxstate; i++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4515 | stp = lemp->sorted[i]; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4516 | ax[i*2].stp = stp; |
| 4517 | ax[i*2].isTkn = 1; |
| 4518 | ax[i*2].nAction = stp->nTknAct; |
| 4519 | ax[i*2+1].stp = stp; |
| 4520 | ax[i*2+1].isTkn = 0; |
| 4521 | ax[i*2+1].nAction = stp->nNtAct; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4522 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4523 | mxTknOfst = mnTknOfst = 0; |
| 4524 | mxNtOfst = mnNtOfst = 0; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4525 | /* In an effort to minimize the action table size, use the heuristic |
| 4526 | ** of placing the largest action sets first */ |
| 4527 | for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i; |
| 4528 | qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4529 | pActtab = acttab_alloc(lemp->nsymbol, lemp->nterminal); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4530 | for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4531 | stp = ax[i].stp; |
| 4532 | if( ax[i].isTkn ){ |
| 4533 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4534 | int action; |
| 4535 | if( ap->sp->index>=lemp->nterminal ) continue; |
| 4536 | action = compute_action(lemp, ap); |
| 4537 | if( action<0 ) continue; |
| 4538 | acttab_action(pActtab, ap->sp->index, action); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4539 | } |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4540 | stp->iTknOfst = acttab_insert(pActtab, 1); |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4541 | if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst; |
| 4542 | if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst; |
| 4543 | }else{ |
| 4544 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4545 | int action; |
| 4546 | if( ap->sp->index<lemp->nterminal ) continue; |
| 4547 | if( ap->sp->index==lemp->nsymbol ) continue; |
| 4548 | action = compute_action(lemp, ap); |
| 4549 | if( action<0 ) continue; |
| 4550 | acttab_action(pActtab, ap->sp->index, action); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4551 | } |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4552 | stp->iNtOfst = acttab_insert(pActtab, 0); |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4553 | if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst; |
| 4554 | if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4555 | } |
drh | 337cd0d | 2015-09-07 23:40:42 +0000 | [diff] [blame] | 4556 | #if 0 /* Uncomment for a trace of how the yy_action[] table fills out */ |
| 4557 | { int jj, nn; |
| 4558 | for(jj=nn=0; jj<pActtab->nAction; jj++){ |
| 4559 | if( pActtab->aAction[jj].action<0 ) nn++; |
| 4560 | } |
| 4561 | printf("%4d: State %3d %s n: %2d size: %5d freespace: %d\n", |
| 4562 | i, stp->statenum, ax[i].isTkn ? "Token" : "Var ", |
| 4563 | ax[i].nAction, pActtab->nAction, nn); |
| 4564 | } |
| 4565 | #endif |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4566 | } |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4567 | free(ax); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4568 | |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4569 | /* Mark rules that are actually used for reduce actions after all |
| 4570 | ** optimizations have been applied |
| 4571 | */ |
| 4572 | for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE; |
| 4573 | for(i=0; i<lemp->nxstate; i++){ |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4574 | for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){ |
| 4575 | if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){ |
drh | 69bfa55 | 2017-04-26 04:32:17 +0000 | [diff] [blame] | 4576 | ap->x.rp->doesReduce = 1; |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4577 | } |
| 4578 | } |
| 4579 | } |
| 4580 | |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4581 | /* Finish rendering the constants now that the action table has |
| 4582 | ** been computed */ |
| 4583 | fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++; |
| 4584 | fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++; |
drh | ce678c2 | 2019-12-11 18:53:51 +0000 | [diff] [blame] | 4585 | fprintf(out,"#define YYNRULE_WITH_ACTION %d\n",lemp->nruleWithAction); |
| 4586 | lineno++; |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 4587 | fprintf(out,"#define YYNTOKEN %d\n",lemp->nterminal); lineno++; |
drh | 337cd0d | 2015-09-07 23:40:42 +0000 | [diff] [blame] | 4588 | fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++; |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4589 | i = lemp->minShiftReduce; |
| 4590 | fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",i); lineno++; |
| 4591 | i += lemp->nrule; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4592 | fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++; |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4593 | fprintf(out,"#define YY_ERROR_ACTION %d\n", lemp->errAction); lineno++; |
| 4594 | fprintf(out,"#define YY_ACCEPT_ACTION %d\n", lemp->accAction); lineno++; |
| 4595 | fprintf(out,"#define YY_NO_ACTION %d\n", lemp->noAction); lineno++; |
| 4596 | fprintf(out,"#define YY_MIN_REDUCE %d\n", lemp->minReduce); lineno++; |
| 4597 | i = lemp->minReduce + lemp->nrule; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4598 | fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4599 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4600 | |
| 4601 | /* Now output the action table and its associates: |
| 4602 | ** |
| 4603 | ** yy_action[] A single table containing all actions. |
| 4604 | ** yy_lookahead[] A table containing the lookahead for each entry in |
| 4605 | ** yy_action. Used to detect hash collisions. |
| 4606 | ** yy_shift_ofst[] For each state, the offset into yy_action for |
| 4607 | ** shifting terminals. |
| 4608 | ** yy_reduce_ofst[] For each state, the offset into yy_action for |
| 4609 | ** shifting non-terminals after a reduce. |
| 4610 | ** yy_default[] Default action for each state. |
| 4611 | */ |
| 4612 | |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4613 | /* Output the yy_action table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4614 | lemp->nactiontab = n = acttab_action_size(pActtab); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4615 | lemp->tablesize += n*szActionType; |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 4616 | fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++; |
| 4617 | fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4618 | for(i=j=0; i<n; i++){ |
| 4619 | int action = acttab_yyaction(pActtab, i); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4620 | if( action<0 ) action = lemp->noAction; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4621 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4622 | fprintf(out, " %4d,", action); |
| 4623 | if( j==9 || i==n-1 ){ |
| 4624 | fprintf(out, "\n"); lineno++; |
| 4625 | j = 0; |
| 4626 | }else{ |
| 4627 | j++; |
| 4628 | } |
| 4629 | } |
| 4630 | fprintf(out, "};\n"); lineno++; |
| 4631 | |
| 4632 | /* Output the yy_lookahead table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4633 | lemp->nlookaheadtab = n = acttab_lookahead_size(pActtab); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4634 | lemp->tablesize += n*szCodeType; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 4635 | fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4636 | for(i=j=0; i<n; i++){ |
| 4637 | int la = acttab_yylookahead(pActtab, i); |
| 4638 | if( la<0 ) la = lemp->nsymbol; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4639 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4640 | fprintf(out, " %4d,", la); |
drh | 2e51716 | 2019-08-28 02:09:47 +0000 | [diff] [blame] | 4641 | if( j==9 ){ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4642 | fprintf(out, "\n"); lineno++; |
| 4643 | j = 0; |
| 4644 | }else{ |
| 4645 | j++; |
| 4646 | } |
| 4647 | } |
drh | 2e51716 | 2019-08-28 02:09:47 +0000 | [diff] [blame] | 4648 | /* Add extra entries to the end of the yy_lookahead[] table so that |
| 4649 | ** yy_shift_ofst[]+iToken will always be a valid index into the array, |
| 4650 | ** even for the largest possible value of yy_shift_ofst[] and iToken. */ |
| 4651 | nLookAhead = lemp->nterminal + lemp->nactiontab; |
| 4652 | while( i<nLookAhead ){ |
| 4653 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
| 4654 | fprintf(out, " %4d,", lemp->nterminal); |
| 4655 | if( j==9 ){ |
| 4656 | fprintf(out, "\n"); lineno++; |
| 4657 | j = 0; |
| 4658 | }else{ |
| 4659 | j++; |
| 4660 | } |
| 4661 | i++; |
| 4662 | } |
mistachkin | acf6e08 | 2019-09-11 15:25:26 +0000 | [diff] [blame] | 4663 | if( j>0 ){ fprintf(out, "\n"); lineno++; } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4664 | fprintf(out, "};\n"); lineno++; |
| 4665 | |
| 4666 | /* Output the yy_shift_ofst[] table */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4667 | n = lemp->nxstate; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4668 | while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--; |
drh | 701b688 | 2016-08-10 13:30:43 +0000 | [diff] [blame] | 4669 | fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++; |
| 4670 | fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++; |
| 4671 | fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4672 | fprintf(out, "static const %s yy_shift_ofst[] = {\n", |
drh | 701b688 | 2016-08-10 13:30:43 +0000 | [diff] [blame] | 4673 | minimum_size_type(mnTknOfst, lemp->nterminal+lemp->nactiontab, &sz)); |
| 4674 | lineno++; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4675 | lemp->tablesize += n*sz; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4676 | for(i=j=0; i<n; i++){ |
| 4677 | int ofst; |
| 4678 | stp = lemp->sorted[i]; |
| 4679 | ofst = stp->iTknOfst; |
drh | 701b688 | 2016-08-10 13:30:43 +0000 | [diff] [blame] | 4680 | if( ofst==NO_OFFSET ) ofst = lemp->nactiontab; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4681 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4682 | fprintf(out, " %4d,", ofst); |
| 4683 | if( j==9 || i==n-1 ){ |
| 4684 | fprintf(out, "\n"); lineno++; |
| 4685 | j = 0; |
| 4686 | }else{ |
| 4687 | j++; |
| 4688 | } |
| 4689 | } |
| 4690 | fprintf(out, "};\n"); lineno++; |
| 4691 | |
| 4692 | /* Output the yy_reduce_ofst[] table */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4693 | n = lemp->nxstate; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4694 | while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--; |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 4695 | fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++; |
| 4696 | fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++; |
| 4697 | fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4698 | fprintf(out, "static const %s yy_reduce_ofst[] = {\n", |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4699 | minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++; |
| 4700 | lemp->tablesize += n*sz; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4701 | for(i=j=0; i<n; i++){ |
| 4702 | int ofst; |
| 4703 | stp = lemp->sorted[i]; |
| 4704 | ofst = stp->iNtOfst; |
| 4705 | if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4706 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4707 | fprintf(out, " %4d,", ofst); |
| 4708 | if( j==9 || i==n-1 ){ |
| 4709 | fprintf(out, "\n"); lineno++; |
| 4710 | j = 0; |
| 4711 | }else{ |
| 4712 | j++; |
| 4713 | } |
| 4714 | } |
| 4715 | fprintf(out, "};\n"); lineno++; |
| 4716 | |
| 4717 | /* Output the default action table */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 4718 | fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4719 | n = lemp->nxstate; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4720 | lemp->tablesize += n*szActionType; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4721 | for(i=j=0; i<n; i++){ |
| 4722 | stp = lemp->sorted[i]; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4723 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4724 | if( stp->iDfltReduce<0 ){ |
| 4725 | fprintf(out, " %4d,", lemp->errAction); |
| 4726 | }else{ |
| 4727 | fprintf(out, " %4d,", stp->iDfltReduce + lemp->minReduce); |
| 4728 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4729 | if( j==9 || i==n-1 ){ |
| 4730 | fprintf(out, "\n"); lineno++; |
| 4731 | j = 0; |
| 4732 | }else{ |
| 4733 | j++; |
| 4734 | } |
| 4735 | } |
| 4736 | fprintf(out, "};\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4737 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4738 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4739 | /* Generate the table of fallback tokens. |
| 4740 | */ |
| 4741 | if( lemp->has_fallback ){ |
drh | 1441f3e | 2009-06-12 12:50:50 +0000 | [diff] [blame] | 4742 | int mx = lemp->nterminal - 1; |
drh | 010bdb4 | 2019-08-28 11:31:11 +0000 | [diff] [blame] | 4743 | /* 2019-08-28: Generate fallback entries for every token to avoid |
| 4744 | ** having to do a range check on the index */ |
| 4745 | /* while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; } */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4746 | lemp->tablesize += (mx+1)*szCodeType; |
drh | 1441f3e | 2009-06-12 12:50:50 +0000 | [diff] [blame] | 4747 | for(i=0; i<=mx; i++){ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4748 | struct symbol *p = lemp->symbols[i]; |
| 4749 | if( p->fallback==0 ){ |
| 4750 | fprintf(out, " 0, /* %10s => nothing */\n", p->name); |
| 4751 | }else{ |
| 4752 | fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index, |
| 4753 | p->name, p->fallback->name); |
| 4754 | } |
| 4755 | lineno++; |
| 4756 | } |
| 4757 | } |
| 4758 | tplt_xfer(lemp->name, in, out, &lineno); |
| 4759 | |
| 4760 | /* Generate a table containing the symbolic name of every symbol |
| 4761 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4762 | for(i=0; i<lemp->nsymbol; i++){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 4763 | lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4764 | fprintf(out," /* %4d */ \"%s\",\n",i, lemp->symbols[i]->name); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4765 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4766 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4767 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4768 | /* Generate a table containing a text string that describes every |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 4769 | ** rule in the rule set of the grammar. This information is used |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4770 | ** when tracing REDUCE actions. |
| 4771 | */ |
| 4772 | for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4773 | assert( rp->iRule==i ); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4774 | fprintf(out," /* %3d */ \"", i); |
| 4775 | writeRuleText(out, rp); |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4776 | fprintf(out,"\",\n"); lineno++; |
| 4777 | } |
| 4778 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4779 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4780 | /* Generate code which executes every time a symbol is popped from |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4781 | ** the stack while processing errors or while destroying the parser. |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4782 | ** (In other words, generate the %destructor actions) |
| 4783 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4784 | if( lemp->tokendest ){ |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4785 | int once = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4786 | for(i=0; i<lemp->nsymbol; i++){ |
| 4787 | struct symbol *sp = lemp->symbols[i]; |
| 4788 | if( sp==0 || sp->type!=TERMINAL ) continue; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4789 | if( once ){ |
| 4790 | fprintf(out, " /* TERMINAL Destructor */\n"); lineno++; |
| 4791 | once = 0; |
| 4792 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4793 | fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4794 | } |
| 4795 | for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++); |
| 4796 | if( i<lemp->nsymbol ){ |
| 4797 | emit_destructor_code(out,lemp->symbols[i],lemp,&lineno); |
| 4798 | fprintf(out," break;\n"); lineno++; |
| 4799 | } |
| 4800 | } |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4801 | if( lemp->vardest ){ |
| 4802 | struct symbol *dflt_sp = 0; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4803 | int once = 1; |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4804 | for(i=0; i<lemp->nsymbol; i++){ |
| 4805 | struct symbol *sp = lemp->symbols[i]; |
| 4806 | if( sp==0 || sp->type==TERMINAL || |
| 4807 | sp->index<=0 || sp->destructor!=0 ) continue; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4808 | if( once ){ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4809 | fprintf(out, " /* Default NON-TERMINAL Destructor */\n");lineno++; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4810 | once = 0; |
| 4811 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4812 | fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4813 | dflt_sp = sp; |
| 4814 | } |
| 4815 | if( dflt_sp!=0 ){ |
| 4816 | emit_destructor_code(out,dflt_sp,lemp,&lineno); |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4817 | } |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4818 | fprintf(out," break;\n"); lineno++; |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4819 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4820 | for(i=0; i<lemp->nsymbol; i++){ |
| 4821 | struct symbol *sp = lemp->symbols[i]; |
| 4822 | if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue; |
drh | 0f832dd | 2016-08-16 16:46:40 +0000 | [diff] [blame] | 4823 | if( sp->destLineno<0 ) continue; /* Already emitted */ |
drh | 7501301 | 2009-06-12 15:47:34 +0000 | [diff] [blame] | 4824 | fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4825 | |
| 4826 | /* Combine duplicate destructors into a single case */ |
| 4827 | for(j=i+1; j<lemp->nsymbol; j++){ |
| 4828 | struct symbol *sp2 = lemp->symbols[j]; |
| 4829 | if( sp2 && sp2->type!=TERMINAL && sp2->destructor |
| 4830 | && sp2->dtnum==sp->dtnum |
| 4831 | && strcmp(sp->destructor,sp2->destructor)==0 ){ |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4832 | fprintf(out," case %d: /* %s */\n", |
| 4833 | sp2->index, sp2->name); lineno++; |
drh | 0f832dd | 2016-08-16 16:46:40 +0000 | [diff] [blame] | 4834 | sp2->destLineno = -1; /* Avoid emitting this destructor again */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4835 | } |
| 4836 | } |
| 4837 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4838 | emit_destructor_code(out,lemp->symbols[i],lemp,&lineno); |
| 4839 | fprintf(out," break;\n"); lineno++; |
| 4840 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4841 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4842 | |
| 4843 | /* Generate code which executes whenever the parser stack overflows */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4844 | tplt_print(out,lemp,lemp->overflow,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4845 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4846 | |
drh | cfc45b1 | 2018-12-03 23:57:27 +0000 | [diff] [blame] | 4847 | /* Generate the tables of rule information. yyRuleInfoLhs[] and |
| 4848 | ** yyRuleInfoNRhs[]. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4849 | ** |
| 4850 | ** Note: This code depends on the fact that rules are number |
| 4851 | ** sequentually beginning with 0. |
| 4852 | */ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4853 | for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ |
drh | cfc45b1 | 2018-12-03 23:57:27 +0000 | [diff] [blame] | 4854 | fprintf(out," %4d, /* (%d) ", rp->lhs->index, i); |
| 4855 | rule_print(out, rp); |
| 4856 | fprintf(out," */\n"); lineno++; |
| 4857 | } |
| 4858 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4859 | for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ |
| 4860 | fprintf(out," %3d, /* (%d) ", -rp->nrhs, i); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4861 | rule_print(out, rp); |
| 4862 | fprintf(out," */\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4863 | } |
| 4864 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4865 | |
| 4866 | /* Generate code which execution during each REDUCE action */ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 4867 | i = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4868 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 4869 | i += translate_code(lemp, rp); |
| 4870 | } |
| 4871 | if( i ){ |
| 4872 | fprintf(out," YYMINORTYPE yylhsminor;\n"); lineno++; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4873 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4874 | /* First output rules other than the default: rule */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4875 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4876 | struct rule *rp2; /* Other rules with the same action */ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4877 | if( rp->codeEmitted ) continue; |
| 4878 | if( rp->noCode ){ |
| 4879 | /* No C code actions, so this will be part of the "default:" rule */ |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 4880 | continue; |
| 4881 | } |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4882 | fprintf(out," case %d: /* ", rp->iRule); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4883 | writeRuleText(out, rp); |
| 4884 | fprintf(out, " */\n"); lineno++; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4885 | for(rp2=rp->next; rp2; rp2=rp2->next){ |
drh | afb8cd9 | 2016-04-29 11:28:35 +0000 | [diff] [blame] | 4886 | if( rp2->code==rp->code && rp2->codePrefix==rp->codePrefix |
| 4887 | && rp2->codeSuffix==rp->codeSuffix ){ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4888 | fprintf(out," case %d: /* ", rp2->iRule); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4889 | writeRuleText(out, rp2); |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4890 | fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->iRule); lineno++; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4891 | rp2->codeEmitted = 1; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4892 | } |
| 4893 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4894 | emit_code(out,rp,lemp,&lineno); |
| 4895 | fprintf(out," break;\n"); lineno++; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4896 | rp->codeEmitted = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4897 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4898 | /* Finally, output the default: rule. We choose as the default: all |
| 4899 | ** empty actions. */ |
| 4900 | fprintf(out," default:\n"); lineno++; |
| 4901 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4902 | if( rp->codeEmitted ) continue; |
| 4903 | assert( rp->noCode ); |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4904 | fprintf(out," /* (%d) ", rp->iRule); |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4905 | writeRuleText(out, rp); |
drh | e94006e | 2019-12-10 20:41:48 +0000 | [diff] [blame] | 4906 | if( rp->neverReduce ){ |
| 4907 | fprintf(out, " (NEVER REDUCES) */ assert(yyruleno!=%d);\n", |
| 4908 | rp->iRule); lineno++; |
| 4909 | }else if( rp->doesReduce ){ |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4910 | fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->iRule); lineno++; |
| 4911 | }else{ |
| 4912 | fprintf(out, " (OPTIMIZED OUT) */ assert(yyruleno!=%d);\n", |
| 4913 | rp->iRule); lineno++; |
| 4914 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4915 | } |
| 4916 | fprintf(out," break;\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4917 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4918 | |
| 4919 | /* Generate code which executes if a parse fails */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4920 | tplt_print(out,lemp,lemp->failure,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4921 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4922 | |
| 4923 | /* Generate code which executes when a syntax error occurs */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4924 | tplt_print(out,lemp,lemp->error,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4925 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4926 | |
| 4927 | /* Generate code which executes when the parser accepts its input */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4928 | tplt_print(out,lemp,lemp->accept,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4929 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4930 | |
| 4931 | /* Append any addition code the user desires */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4932 | tplt_print(out,lemp,lemp->extracode,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4933 | |
drh | e2dcc42 | 2019-01-15 14:44:23 +0000 | [diff] [blame] | 4934 | acttab_free(pActtab); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4935 | fclose(in); |
| 4936 | fclose(out); |
drh | fe03dac | 2019-11-26 02:22:39 +0000 | [diff] [blame] | 4937 | if( sql ) fclose(sql); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4938 | return; |
| 4939 | } |
| 4940 | |
| 4941 | /* Generate a header file for the parser */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4942 | void ReportHeader(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4943 | { |
| 4944 | FILE *out, *in; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4945 | const char *prefix; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4946 | char line[LINESIZE]; |
| 4947 | char pattern[LINESIZE]; |
| 4948 | int i; |
| 4949 | |
| 4950 | if( lemp->tokenprefix ) prefix = lemp->tokenprefix; |
| 4951 | else prefix = ""; |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 4952 | in = file_open(lemp,".h","rb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4953 | if( in ){ |
drh | 8ba0d1c | 2012-06-16 15:26:31 +0000 | [diff] [blame] | 4954 | int nextChar; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4955 | for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4956 | lemon_sprintf(pattern,"#define %s%-30s %3d\n", |
| 4957 | prefix,lemp->symbols[i]->name,i); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4958 | if( strcmp(line,pattern) ) break; |
| 4959 | } |
drh | 8ba0d1c | 2012-06-16 15:26:31 +0000 | [diff] [blame] | 4960 | nextChar = fgetc(in); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4961 | fclose(in); |
drh | 8ba0d1c | 2012-06-16 15:26:31 +0000 | [diff] [blame] | 4962 | if( i==lemp->nterminal && nextChar==EOF ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4963 | /* No change in the file. Don't rewrite it. */ |
| 4964 | return; |
| 4965 | } |
| 4966 | } |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 4967 | out = file_open(lemp,".h","wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4968 | if( out ){ |
| 4969 | for(i=1; i<lemp->nterminal; i++){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4970 | fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4971 | } |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4972 | fclose(out); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4973 | } |
| 4974 | return; |
| 4975 | } |
| 4976 | |
| 4977 | /* Reduce the size of the action tables, if possible, by making use |
| 4978 | ** of defaults. |
| 4979 | ** |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4980 | ** In this version, we take the most frequent REDUCE action and make |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4981 | ** it the default. Except, there is no default if the wildcard token |
| 4982 | ** is a possible look-ahead. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4983 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4984 | void CompressTables(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4985 | { |
| 4986 | struct state *stp; |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 4987 | struct action *ap, *ap2, *nextap; |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4988 | struct rule *rp, *rp2, *rbest; |
drh | 0c6dfaa | 2015-09-08 21:16:46 +0000 | [diff] [blame] | 4989 | int nbest, n; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4990 | int i; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4991 | int usesWildcard; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4992 | |
| 4993 | for(i=0; i<lemp->nstate; i++){ |
| 4994 | stp = lemp->sorted[i]; |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4995 | nbest = 0; |
| 4996 | rbest = 0; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4997 | usesWildcard = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4998 | |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4999 | for(ap=stp->ap; ap; ap=ap->next){ |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 5000 | if( ap->type==SHIFT && ap->sp==lemp->wildcard ){ |
| 5001 | usesWildcard = 1; |
| 5002 | } |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 5003 | if( ap->type!=REDUCE ) continue; |
| 5004 | rp = ap->x.rp; |
drh | b496099 | 2007-10-05 16:16:36 +0000 | [diff] [blame] | 5005 | if( rp->lhsStart ) continue; |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 5006 | if( rp==rbest ) continue; |
| 5007 | n = 1; |
| 5008 | for(ap2=ap->next; ap2; ap2=ap2->next){ |
| 5009 | if( ap2->type!=REDUCE ) continue; |
| 5010 | rp2 = ap2->x.rp; |
| 5011 | if( rp2==rbest ) continue; |
| 5012 | if( rp2==rp ) n++; |
| 5013 | } |
| 5014 | if( n>nbest ){ |
| 5015 | nbest = n; |
| 5016 | rbest = rp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5017 | } |
| 5018 | } |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 5019 | |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 5020 | /* Do not make a default if the number of rules to default |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 5021 | ** is not at least 1 or if the wildcard token is a possible |
| 5022 | ** lookahead. |
| 5023 | */ |
| 5024 | if( nbest<1 || usesWildcard ) continue; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5025 | |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 5026 | |
| 5027 | /* Combine matching REDUCE actions into a single default */ |
| 5028 | for(ap=stp->ap; ap; ap=ap->next){ |
| 5029 | if( ap->type==REDUCE && ap->x.rp==rbest ) break; |
| 5030 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5031 | assert( ap ); |
| 5032 | ap->sp = Symbol_new("{default}"); |
| 5033 | for(ap=ap->next; ap; ap=ap->next){ |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 5034 | if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5035 | } |
| 5036 | stp->ap = Action_sort(stp->ap); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 5037 | |
| 5038 | for(ap=stp->ap; ap; ap=ap->next){ |
| 5039 | if( ap->type==SHIFT ) break; |
| 5040 | if( ap->type==REDUCE && ap->x.rp!=rbest ) break; |
| 5041 | } |
| 5042 | if( ap==0 ){ |
| 5043 | stp->autoReduce = 1; |
| 5044 | stp->pDfltReduce = rbest; |
| 5045 | } |
| 5046 | } |
| 5047 | |
| 5048 | /* Make a second pass over all states and actions. Convert |
| 5049 | ** every action that is a SHIFT to an autoReduce state into |
| 5050 | ** a SHIFTREDUCE action. |
| 5051 | */ |
| 5052 | for(i=0; i<lemp->nstate; i++){ |
| 5053 | stp = lemp->sorted[i]; |
| 5054 | for(ap=stp->ap; ap; ap=ap->next){ |
| 5055 | struct state *pNextState; |
| 5056 | if( ap->type!=SHIFT ) continue; |
| 5057 | pNextState = ap->x.stp; |
| 5058 | if( pNextState->autoReduce && pNextState->pDfltReduce!=0 ){ |
| 5059 | ap->type = SHIFTREDUCE; |
| 5060 | ap->x.rp = pNextState->pDfltReduce; |
| 5061 | } |
| 5062 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5063 | } |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 5064 | |
| 5065 | /* If a SHIFTREDUCE action specifies a rule that has a single RHS term |
| 5066 | ** (meaning that the SHIFTREDUCE will land back in the state where it |
| 5067 | ** started) and if there is no C-code associated with the reduce action, |
| 5068 | ** then we can go ahead and convert the action to be the same as the |
| 5069 | ** action for the RHS of the rule. |
| 5070 | */ |
| 5071 | for(i=0; i<lemp->nstate; i++){ |
| 5072 | stp = lemp->sorted[i]; |
| 5073 | for(ap=stp->ap; ap; ap=nextap){ |
| 5074 | nextap = ap->next; |
| 5075 | if( ap->type!=SHIFTREDUCE ) continue; |
| 5076 | rp = ap->x.rp; |
| 5077 | if( rp->noCode==0 ) continue; |
| 5078 | if( rp->nrhs!=1 ) continue; |
| 5079 | #if 1 |
| 5080 | /* Only apply this optimization to non-terminals. It would be OK to |
| 5081 | ** apply it to terminal symbols too, but that makes the parser tables |
| 5082 | ** larger. */ |
| 5083 | if( ap->sp->index<lemp->nterminal ) continue; |
| 5084 | #endif |
| 5085 | /* If we reach this point, it means the optimization can be applied */ |
| 5086 | nextap = ap; |
| 5087 | for(ap2=stp->ap; ap2 && (ap2==ap || ap2->sp!=rp->lhs); ap2=ap2->next){} |
| 5088 | assert( ap2!=0 ); |
| 5089 | ap->spOpt = ap2->sp; |
| 5090 | ap->type = ap2->type; |
| 5091 | ap->x = ap2->x; |
| 5092 | } |
| 5093 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5094 | } |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 5095 | |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 5096 | |
| 5097 | /* |
| 5098 | ** Compare two states for sorting purposes. The smaller state is the |
| 5099 | ** one with the most non-terminal actions. If they have the same number |
| 5100 | ** of non-terminal actions, then the smaller is the one with the most |
| 5101 | ** token actions. |
| 5102 | */ |
| 5103 | static int stateResortCompare(const void *a, const void *b){ |
| 5104 | const struct state *pA = *(const struct state**)a; |
| 5105 | const struct state *pB = *(const struct state**)b; |
| 5106 | int n; |
| 5107 | |
| 5108 | n = pB->nNtAct - pA->nNtAct; |
| 5109 | if( n==0 ){ |
| 5110 | n = pB->nTknAct - pA->nTknAct; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 5111 | if( n==0 ){ |
| 5112 | n = pB->statenum - pA->statenum; |
| 5113 | } |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 5114 | } |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 5115 | assert( n!=0 ); |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 5116 | return n; |
| 5117 | } |
| 5118 | |
| 5119 | |
| 5120 | /* |
| 5121 | ** Renumber and resort states so that states with fewer choices |
| 5122 | ** occur at the end. Except, keep state 0 as the first state. |
| 5123 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5124 | void ResortStates(struct lemon *lemp) |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 5125 | { |
| 5126 | int i; |
| 5127 | struct state *stp; |
| 5128 | struct action *ap; |
| 5129 | |
| 5130 | for(i=0; i<lemp->nstate; i++){ |
| 5131 | stp = lemp->sorted[i]; |
| 5132 | stp->nTknAct = stp->nNtAct = 0; |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 5133 | stp->iDfltReduce = -1; /* Init dflt action to "syntax error" */ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 5134 | stp->iTknOfst = NO_OFFSET; |
| 5135 | stp->iNtOfst = NO_OFFSET; |
| 5136 | for(ap=stp->ap; ap; ap=ap->next){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 5137 | int iAction = compute_action(lemp,ap); |
| 5138 | if( iAction>=0 ){ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 5139 | if( ap->sp->index<lemp->nterminal ){ |
| 5140 | stp->nTknAct++; |
| 5141 | }else if( ap->sp->index<lemp->nsymbol ){ |
| 5142 | stp->nNtAct++; |
| 5143 | }else{ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 5144 | assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp ); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 5145 | stp->iDfltReduce = iAction; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 5146 | } |
| 5147 | } |
| 5148 | } |
| 5149 | } |
| 5150 | qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]), |
| 5151 | stateResortCompare); |
| 5152 | for(i=0; i<lemp->nstate; i++){ |
| 5153 | lemp->sorted[i]->statenum = i; |
| 5154 | } |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 5155 | lemp->nxstate = lemp->nstate; |
| 5156 | while( lemp->nxstate>1 && lemp->sorted[lemp->nxstate-1]->autoReduce ){ |
| 5157 | lemp->nxstate--; |
| 5158 | } |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
| 5161 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5162 | /***************** From the file "set.c" ************************************/ |
| 5163 | /* |
| 5164 | ** Set manipulation routines for the LEMON parser generator. |
| 5165 | */ |
| 5166 | |
| 5167 | static int size = 0; |
| 5168 | |
| 5169 | /* Set the set size */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5170 | void SetSize(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5171 | { |
| 5172 | size = n+1; |
| 5173 | } |
| 5174 | |
| 5175 | /* Allocate a new set */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5176 | char *SetNew(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5177 | char *s; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 5178 | s = (char*)calloc( size, 1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5179 | if( s==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5180 | memory_error(); |
| 5181 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5182 | return s; |
| 5183 | } |
| 5184 | |
| 5185 | /* Deallocate a set */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5186 | void SetFree(char *s) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5187 | { |
| 5188 | free(s); |
| 5189 | } |
| 5190 | |
| 5191 | /* Add a new element to the set. Return TRUE if the element was added |
| 5192 | ** and FALSE if it was already there. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5193 | int SetAdd(char *s, int e) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5194 | { |
| 5195 | int rv; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 5196 | assert( e>=0 && e<size ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5197 | rv = s[e]; |
| 5198 | s[e] = 1; |
| 5199 | return !rv; |
| 5200 | } |
| 5201 | |
| 5202 | /* Add every element of s2 to s1. Return TRUE if s1 changes. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5203 | int SetUnion(char *s1, char *s2) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5204 | { |
| 5205 | int i, progress; |
| 5206 | progress = 0; |
| 5207 | for(i=0; i<size; i++){ |
| 5208 | if( s2[i]==0 ) continue; |
| 5209 | if( s1[i]==0 ){ |
| 5210 | progress = 1; |
| 5211 | s1[i] = 1; |
| 5212 | } |
| 5213 | } |
| 5214 | return progress; |
| 5215 | } |
| 5216 | /********************** From the file "table.c" ****************************/ |
| 5217 | /* |
| 5218 | ** All code in this file has been automatically generated |
| 5219 | ** from a specification in the file |
| 5220 | ** "table.q" |
| 5221 | ** by the associative array code building program "aagen". |
| 5222 | ** Do not edit this file! Instead, edit the specification |
| 5223 | ** file, then rerun aagen. |
| 5224 | */ |
| 5225 | /* |
| 5226 | ** Code for processing tables in the LEMON parser generator. |
| 5227 | */ |
| 5228 | |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5229 | PRIVATE unsigned strhash(const char *x) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5230 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5231 | unsigned h = 0; |
| 5232 | while( *x ) h = h*13 + *(x++); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5233 | return h; |
| 5234 | } |
| 5235 | |
| 5236 | /* Works like strdup, sort of. Save a string in malloced memory, but |
| 5237 | ** keep strings in a table so that the same string is not in more |
| 5238 | ** than one place. |
| 5239 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5240 | const char *Strsafe(const char *y) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5241 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5242 | const char *z; |
| 5243 | char *cpy; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5244 | |
drh | 916f75f | 2006-07-17 00:19:39 +0000 | [diff] [blame] | 5245 | if( y==0 ) return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5246 | z = Strsafe_find(y); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5247 | if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 5248 | lemon_strcpy(cpy,y); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5249 | z = cpy; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5250 | Strsafe_insert(z); |
| 5251 | } |
| 5252 | MemoryCheck(z); |
| 5253 | return z; |
| 5254 | } |
| 5255 | |
| 5256 | /* There is one instance of the following structure for each |
| 5257 | ** associative array of type "x1". |
| 5258 | */ |
| 5259 | struct s_x1 { |
| 5260 | int size; /* The number of available slots. */ |
| 5261 | /* Must be a power of 2 greater than or */ |
| 5262 | /* equal to 1 */ |
| 5263 | int count; /* Number of currently slots filled */ |
| 5264 | struct s_x1node *tbl; /* The data stored here */ |
| 5265 | struct s_x1node **ht; /* Hash table for lookups */ |
| 5266 | }; |
| 5267 | |
| 5268 | /* There is one instance of this structure for every data element |
| 5269 | ** in an associative array of type "x1". |
| 5270 | */ |
| 5271 | typedef struct s_x1node { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5272 | const char *data; /* The data */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5273 | struct s_x1node *next; /* Next entry with the same hash */ |
| 5274 | struct s_x1node **from; /* Previous link */ |
| 5275 | } x1node; |
| 5276 | |
| 5277 | /* There is only one instance of the array, which is the following */ |
| 5278 | static struct s_x1 *x1a; |
| 5279 | |
| 5280 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5281 | void Strsafe_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5282 | if( x1a ) return; |
| 5283 | x1a = (struct s_x1*)malloc( sizeof(struct s_x1) ); |
| 5284 | if( x1a ){ |
| 5285 | x1a->size = 1024; |
| 5286 | x1a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5287 | x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5288 | if( x1a->tbl==0 ){ |
| 5289 | free(x1a); |
| 5290 | x1a = 0; |
| 5291 | }else{ |
| 5292 | int i; |
| 5293 | x1a->ht = (x1node**)&(x1a->tbl[1024]); |
| 5294 | for(i=0; i<1024; i++) x1a->ht[i] = 0; |
| 5295 | } |
| 5296 | } |
| 5297 | } |
| 5298 | /* Insert a new record into the array. Return TRUE if successful. |
| 5299 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5300 | int Strsafe_insert(const char *data) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5301 | { |
| 5302 | x1node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5303 | unsigned h; |
| 5304 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5305 | |
| 5306 | if( x1a==0 ) return 0; |
| 5307 | ph = strhash(data); |
| 5308 | h = ph & (x1a->size-1); |
| 5309 | np = x1a->ht[h]; |
| 5310 | while( np ){ |
| 5311 | if( strcmp(np->data,data)==0 ){ |
| 5312 | /* An existing entry with the same key is found. */ |
| 5313 | /* Fail because overwrite is not allows. */ |
| 5314 | return 0; |
| 5315 | } |
| 5316 | np = np->next; |
| 5317 | } |
| 5318 | if( x1a->count>=x1a->size ){ |
| 5319 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5320 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5321 | struct s_x1 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5322 | array.size = arrSize = x1a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5323 | array.count = x1a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5324 | array.tbl = (x1node*)calloc(arrSize, sizeof(x1node) + sizeof(x1node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5325 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5326 | array.ht = (x1node**)&(array.tbl[arrSize]); |
| 5327 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5328 | for(i=0; i<x1a->count; i++){ |
| 5329 | x1node *oldnp, *newnp; |
| 5330 | oldnp = &(x1a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5331 | h = strhash(oldnp->data) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5332 | newnp = &(array.tbl[i]); |
| 5333 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5334 | newnp->next = array.ht[h]; |
| 5335 | newnp->data = oldnp->data; |
| 5336 | newnp->from = &(array.ht[h]); |
| 5337 | array.ht[h] = newnp; |
| 5338 | } |
| 5339 | free(x1a->tbl); |
| 5340 | *x1a = array; |
| 5341 | } |
| 5342 | /* Insert the new data */ |
| 5343 | h = ph & (x1a->size-1); |
| 5344 | np = &(x1a->tbl[x1a->count++]); |
| 5345 | np->data = data; |
| 5346 | if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next); |
| 5347 | np->next = x1a->ht[h]; |
| 5348 | x1a->ht[h] = np; |
| 5349 | np->from = &(x1a->ht[h]); |
| 5350 | return 1; |
| 5351 | } |
| 5352 | |
| 5353 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5354 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5355 | const char *Strsafe_find(const char *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5356 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5357 | unsigned h; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5358 | x1node *np; |
| 5359 | |
| 5360 | if( x1a==0 ) return 0; |
| 5361 | h = strhash(key) & (x1a->size-1); |
| 5362 | np = x1a->ht[h]; |
| 5363 | while( np ){ |
| 5364 | if( strcmp(np->data,key)==0 ) break; |
| 5365 | np = np->next; |
| 5366 | } |
| 5367 | return np ? np->data : 0; |
| 5368 | } |
| 5369 | |
| 5370 | /* Return a pointer to the (terminal or nonterminal) symbol "x". |
| 5371 | ** Create a new symbol if this is the first time "x" has been seen. |
| 5372 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5373 | struct symbol *Symbol_new(const char *x) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5374 | { |
| 5375 | struct symbol *sp; |
| 5376 | |
| 5377 | sp = Symbol_find(x); |
| 5378 | if( sp==0 ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 5379 | sp = (struct symbol *)calloc(1, sizeof(struct symbol) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5380 | MemoryCheck(sp); |
| 5381 | sp->name = Strsafe(x); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 5382 | sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5383 | sp->rule = 0; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 5384 | sp->fallback = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5385 | sp->prec = -1; |
| 5386 | sp->assoc = UNK; |
| 5387 | sp->firstset = 0; |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 5388 | sp->lambda = LEMON_FALSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5389 | sp->destructor = 0; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 5390 | sp->destLineno = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5391 | sp->datatype = 0; |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 5392 | sp->useCnt = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5393 | Symbol_insert(sp,sp->name); |
| 5394 | } |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 5395 | sp->useCnt++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5396 | return sp; |
| 5397 | } |
| 5398 | |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 5399 | /* Compare two symbols for sorting purposes. Return negative, |
| 5400 | ** zero, or positive if a is less then, equal to, or greater |
| 5401 | ** than b. |
drh | 60d3165 | 2004-02-22 00:08:04 +0000 | [diff] [blame] | 5402 | ** |
| 5403 | ** Symbols that begin with upper case letters (terminals or tokens) |
| 5404 | ** must sort before symbols that begin with lower case letters |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 5405 | ** (non-terminals). And MULTITERMINAL symbols (created using the |
| 5406 | ** %token_class directive) must sort at the very end. Other than |
| 5407 | ** that, the order does not matter. |
drh | 60d3165 | 2004-02-22 00:08:04 +0000 | [diff] [blame] | 5408 | ** |
| 5409 | ** We find experimentally that leaving the symbols in their original |
| 5410 | ** order (the order they appeared in the grammar file) gives the |
| 5411 | ** smallest parser tables in SQLite. |
| 5412 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5413 | int Symbolcmpp(const void *_a, const void *_b) |
| 5414 | { |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 5415 | const struct symbol *a = *(const struct symbol **) _a; |
| 5416 | const struct symbol *b = *(const struct symbol **) _b; |
| 5417 | int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1; |
| 5418 | int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1; |
| 5419 | return i1==i2 ? a->index - b->index : i1 - i2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
| 5422 | /* There is one instance of the following structure for each |
| 5423 | ** associative array of type "x2". |
| 5424 | */ |
| 5425 | struct s_x2 { |
| 5426 | int size; /* The number of available slots. */ |
| 5427 | /* Must be a power of 2 greater than or */ |
| 5428 | /* equal to 1 */ |
| 5429 | int count; /* Number of currently slots filled */ |
| 5430 | struct s_x2node *tbl; /* The data stored here */ |
| 5431 | struct s_x2node **ht; /* Hash table for lookups */ |
| 5432 | }; |
| 5433 | |
| 5434 | /* There is one instance of this structure for every data element |
| 5435 | ** in an associative array of type "x2". |
| 5436 | */ |
| 5437 | typedef struct s_x2node { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5438 | struct symbol *data; /* The data */ |
| 5439 | const char *key; /* The key */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5440 | struct s_x2node *next; /* Next entry with the same hash */ |
| 5441 | struct s_x2node **from; /* Previous link */ |
| 5442 | } x2node; |
| 5443 | |
| 5444 | /* There is only one instance of the array, which is the following */ |
| 5445 | static struct s_x2 *x2a; |
| 5446 | |
| 5447 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5448 | void Symbol_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5449 | if( x2a ) return; |
| 5450 | x2a = (struct s_x2*)malloc( sizeof(struct s_x2) ); |
| 5451 | if( x2a ){ |
| 5452 | x2a->size = 128; |
| 5453 | x2a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5454 | x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5455 | if( x2a->tbl==0 ){ |
| 5456 | free(x2a); |
| 5457 | x2a = 0; |
| 5458 | }else{ |
| 5459 | int i; |
| 5460 | x2a->ht = (x2node**)&(x2a->tbl[128]); |
| 5461 | for(i=0; i<128; i++) x2a->ht[i] = 0; |
| 5462 | } |
| 5463 | } |
| 5464 | } |
| 5465 | /* Insert a new record into the array. Return TRUE if successful. |
| 5466 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5467 | int Symbol_insert(struct symbol *data, const char *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5468 | { |
| 5469 | x2node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5470 | unsigned h; |
| 5471 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5472 | |
| 5473 | if( x2a==0 ) return 0; |
| 5474 | ph = strhash(key); |
| 5475 | h = ph & (x2a->size-1); |
| 5476 | np = x2a->ht[h]; |
| 5477 | while( np ){ |
| 5478 | if( strcmp(np->key,key)==0 ){ |
| 5479 | /* An existing entry with the same key is found. */ |
| 5480 | /* Fail because overwrite is not allows. */ |
| 5481 | return 0; |
| 5482 | } |
| 5483 | np = np->next; |
| 5484 | } |
| 5485 | if( x2a->count>=x2a->size ){ |
| 5486 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5487 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5488 | struct s_x2 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5489 | array.size = arrSize = x2a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5490 | array.count = x2a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5491 | array.tbl = (x2node*)calloc(arrSize, sizeof(x2node) + sizeof(x2node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5492 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5493 | array.ht = (x2node**)&(array.tbl[arrSize]); |
| 5494 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5495 | for(i=0; i<x2a->count; i++){ |
| 5496 | x2node *oldnp, *newnp; |
| 5497 | oldnp = &(x2a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5498 | h = strhash(oldnp->key) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5499 | newnp = &(array.tbl[i]); |
| 5500 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5501 | newnp->next = array.ht[h]; |
| 5502 | newnp->key = oldnp->key; |
| 5503 | newnp->data = oldnp->data; |
| 5504 | newnp->from = &(array.ht[h]); |
| 5505 | array.ht[h] = newnp; |
| 5506 | } |
| 5507 | free(x2a->tbl); |
| 5508 | *x2a = array; |
| 5509 | } |
| 5510 | /* Insert the new data */ |
| 5511 | h = ph & (x2a->size-1); |
| 5512 | np = &(x2a->tbl[x2a->count++]); |
| 5513 | np->key = key; |
| 5514 | np->data = data; |
| 5515 | if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next); |
| 5516 | np->next = x2a->ht[h]; |
| 5517 | x2a->ht[h] = np; |
| 5518 | np->from = &(x2a->ht[h]); |
| 5519 | return 1; |
| 5520 | } |
| 5521 | |
| 5522 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5523 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5524 | struct symbol *Symbol_find(const char *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5525 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5526 | unsigned h; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5527 | x2node *np; |
| 5528 | |
| 5529 | if( x2a==0 ) return 0; |
| 5530 | h = strhash(key) & (x2a->size-1); |
| 5531 | np = x2a->ht[h]; |
| 5532 | while( np ){ |
| 5533 | if( strcmp(np->key,key)==0 ) break; |
| 5534 | np = np->next; |
| 5535 | } |
| 5536 | return np ? np->data : 0; |
| 5537 | } |
| 5538 | |
| 5539 | /* Return the n-th data. Return NULL if n is out of range. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5540 | struct symbol *Symbol_Nth(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5541 | { |
| 5542 | struct symbol *data; |
| 5543 | if( x2a && n>0 && n<=x2a->count ){ |
| 5544 | data = x2a->tbl[n-1].data; |
| 5545 | }else{ |
| 5546 | data = 0; |
| 5547 | } |
| 5548 | return data; |
| 5549 | } |
| 5550 | |
| 5551 | /* Return the size of the array */ |
| 5552 | int Symbol_count() |
| 5553 | { |
| 5554 | return x2a ? x2a->count : 0; |
| 5555 | } |
| 5556 | |
| 5557 | /* Return an array of pointers to all data in the table. |
| 5558 | ** The array is obtained from malloc. Return NULL if memory allocation |
| 5559 | ** problems, or if the array is empty. */ |
| 5560 | struct symbol **Symbol_arrayof() |
| 5561 | { |
| 5562 | struct symbol **array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5563 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5564 | if( x2a==0 ) return 0; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5565 | arrSize = x2a->count; |
| 5566 | array = (struct symbol **)calloc(arrSize, sizeof(struct symbol *)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5567 | if( array ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5568 | for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5569 | } |
| 5570 | return array; |
| 5571 | } |
| 5572 | |
| 5573 | /* Compare two configurations */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5574 | int Configcmp(const char *_a,const char *_b) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5575 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5576 | const struct config *a = (struct config *) _a; |
| 5577 | const struct config *b = (struct config *) _b; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5578 | int x; |
| 5579 | x = a->rp->index - b->rp->index; |
| 5580 | if( x==0 ) x = a->dot - b->dot; |
| 5581 | return x; |
| 5582 | } |
| 5583 | |
| 5584 | /* Compare two states */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5585 | PRIVATE int statecmp(struct config *a, struct config *b) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5586 | { |
| 5587 | int rc; |
| 5588 | for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){ |
| 5589 | rc = a->rp->index - b->rp->index; |
| 5590 | if( rc==0 ) rc = a->dot - b->dot; |
| 5591 | } |
| 5592 | if( rc==0 ){ |
| 5593 | if( a ) rc = 1; |
| 5594 | if( b ) rc = -1; |
| 5595 | } |
| 5596 | return rc; |
| 5597 | } |
| 5598 | |
| 5599 | /* Hash a state */ |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5600 | PRIVATE unsigned statehash(struct config *a) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5601 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5602 | unsigned h=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5603 | while( a ){ |
| 5604 | h = h*571 + a->rp->index*37 + a->dot; |
| 5605 | a = a->bp; |
| 5606 | } |
| 5607 | return h; |
| 5608 | } |
| 5609 | |
| 5610 | /* Allocate a new state structure */ |
| 5611 | struct state *State_new() |
| 5612 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5613 | struct state *newstate; |
| 5614 | newstate = (struct state *)calloc(1, sizeof(struct state) ); |
| 5615 | MemoryCheck(newstate); |
| 5616 | return newstate; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5617 | } |
| 5618 | |
| 5619 | /* There is one instance of the following structure for each |
| 5620 | ** associative array of type "x3". |
| 5621 | */ |
| 5622 | struct s_x3 { |
| 5623 | int size; /* The number of available slots. */ |
| 5624 | /* Must be a power of 2 greater than or */ |
| 5625 | /* equal to 1 */ |
| 5626 | int count; /* Number of currently slots filled */ |
| 5627 | struct s_x3node *tbl; /* The data stored here */ |
| 5628 | struct s_x3node **ht; /* Hash table for lookups */ |
| 5629 | }; |
| 5630 | |
| 5631 | /* There is one instance of this structure for every data element |
| 5632 | ** in an associative array of type "x3". |
| 5633 | */ |
| 5634 | typedef struct s_x3node { |
| 5635 | struct state *data; /* The data */ |
| 5636 | struct config *key; /* The key */ |
| 5637 | struct s_x3node *next; /* Next entry with the same hash */ |
| 5638 | struct s_x3node **from; /* Previous link */ |
| 5639 | } x3node; |
| 5640 | |
| 5641 | /* There is only one instance of the array, which is the following */ |
| 5642 | static struct s_x3 *x3a; |
| 5643 | |
| 5644 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5645 | void State_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5646 | if( x3a ) return; |
| 5647 | x3a = (struct s_x3*)malloc( sizeof(struct s_x3) ); |
| 5648 | if( x3a ){ |
| 5649 | x3a->size = 128; |
| 5650 | x3a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5651 | x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5652 | if( x3a->tbl==0 ){ |
| 5653 | free(x3a); |
| 5654 | x3a = 0; |
| 5655 | }else{ |
| 5656 | int i; |
| 5657 | x3a->ht = (x3node**)&(x3a->tbl[128]); |
| 5658 | for(i=0; i<128; i++) x3a->ht[i] = 0; |
| 5659 | } |
| 5660 | } |
| 5661 | } |
| 5662 | /* Insert a new record into the array. Return TRUE if successful. |
| 5663 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5664 | int State_insert(struct state *data, struct config *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5665 | { |
| 5666 | x3node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5667 | unsigned h; |
| 5668 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5669 | |
| 5670 | if( x3a==0 ) return 0; |
| 5671 | ph = statehash(key); |
| 5672 | h = ph & (x3a->size-1); |
| 5673 | np = x3a->ht[h]; |
| 5674 | while( np ){ |
| 5675 | if( statecmp(np->key,key)==0 ){ |
| 5676 | /* An existing entry with the same key is found. */ |
| 5677 | /* Fail because overwrite is not allows. */ |
| 5678 | return 0; |
| 5679 | } |
| 5680 | np = np->next; |
| 5681 | } |
| 5682 | if( x3a->count>=x3a->size ){ |
| 5683 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5684 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5685 | struct s_x3 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5686 | array.size = arrSize = x3a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5687 | array.count = x3a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5688 | array.tbl = (x3node*)calloc(arrSize, sizeof(x3node) + sizeof(x3node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5689 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5690 | array.ht = (x3node**)&(array.tbl[arrSize]); |
| 5691 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5692 | for(i=0; i<x3a->count; i++){ |
| 5693 | x3node *oldnp, *newnp; |
| 5694 | oldnp = &(x3a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5695 | h = statehash(oldnp->key) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5696 | newnp = &(array.tbl[i]); |
| 5697 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5698 | newnp->next = array.ht[h]; |
| 5699 | newnp->key = oldnp->key; |
| 5700 | newnp->data = oldnp->data; |
| 5701 | newnp->from = &(array.ht[h]); |
| 5702 | array.ht[h] = newnp; |
| 5703 | } |
| 5704 | free(x3a->tbl); |
| 5705 | *x3a = array; |
| 5706 | } |
| 5707 | /* Insert the new data */ |
| 5708 | h = ph & (x3a->size-1); |
| 5709 | np = &(x3a->tbl[x3a->count++]); |
| 5710 | np->key = key; |
| 5711 | np->data = data; |
| 5712 | if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next); |
| 5713 | np->next = x3a->ht[h]; |
| 5714 | x3a->ht[h] = np; |
| 5715 | np->from = &(x3a->ht[h]); |
| 5716 | return 1; |
| 5717 | } |
| 5718 | |
| 5719 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5720 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5721 | struct state *State_find(struct config *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5722 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5723 | unsigned h; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5724 | x3node *np; |
| 5725 | |
| 5726 | if( x3a==0 ) return 0; |
| 5727 | h = statehash(key) & (x3a->size-1); |
| 5728 | np = x3a->ht[h]; |
| 5729 | while( np ){ |
| 5730 | if( statecmp(np->key,key)==0 ) break; |
| 5731 | np = np->next; |
| 5732 | } |
| 5733 | return np ? np->data : 0; |
| 5734 | } |
| 5735 | |
| 5736 | /* Return an array of pointers to all data in the table. |
| 5737 | ** The array is obtained from malloc. Return NULL if memory allocation |
| 5738 | ** problems, or if the array is empty. */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5739 | struct state **State_arrayof(void) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5740 | { |
| 5741 | struct state **array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5742 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5743 | if( x3a==0 ) return 0; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5744 | arrSize = x3a->count; |
| 5745 | array = (struct state **)calloc(arrSize, sizeof(struct state *)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5746 | if( array ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5747 | for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5748 | } |
| 5749 | return array; |
| 5750 | } |
| 5751 | |
| 5752 | /* Hash a configuration */ |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5753 | PRIVATE unsigned confighash(struct config *a) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5754 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5755 | unsigned h=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5756 | h = h*571 + a->rp->index*37 + a->dot; |
| 5757 | return h; |
| 5758 | } |
| 5759 | |
| 5760 | /* There is one instance of the following structure for each |
| 5761 | ** associative array of type "x4". |
| 5762 | */ |
| 5763 | struct s_x4 { |
| 5764 | int size; /* The number of available slots. */ |
| 5765 | /* Must be a power of 2 greater than or */ |
| 5766 | /* equal to 1 */ |
| 5767 | int count; /* Number of currently slots filled */ |
| 5768 | struct s_x4node *tbl; /* The data stored here */ |
| 5769 | struct s_x4node **ht; /* Hash table for lookups */ |
| 5770 | }; |
| 5771 | |
| 5772 | /* There is one instance of this structure for every data element |
| 5773 | ** in an associative array of type "x4". |
| 5774 | */ |
| 5775 | typedef struct s_x4node { |
| 5776 | struct config *data; /* The data */ |
| 5777 | struct s_x4node *next; /* Next entry with the same hash */ |
| 5778 | struct s_x4node **from; /* Previous link */ |
| 5779 | } x4node; |
| 5780 | |
| 5781 | /* There is only one instance of the array, which is the following */ |
| 5782 | static struct s_x4 *x4a; |
| 5783 | |
| 5784 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5785 | void Configtable_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5786 | if( x4a ) return; |
| 5787 | x4a = (struct s_x4*)malloc( sizeof(struct s_x4) ); |
| 5788 | if( x4a ){ |
| 5789 | x4a->size = 64; |
| 5790 | x4a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5791 | x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5792 | if( x4a->tbl==0 ){ |
| 5793 | free(x4a); |
| 5794 | x4a = 0; |
| 5795 | }else{ |
| 5796 | int i; |
| 5797 | x4a->ht = (x4node**)&(x4a->tbl[64]); |
| 5798 | for(i=0; i<64; i++) x4a->ht[i] = 0; |
| 5799 | } |
| 5800 | } |
| 5801 | } |
| 5802 | /* Insert a new record into the array. Return TRUE if successful. |
| 5803 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5804 | int Configtable_insert(struct config *data) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5805 | { |
| 5806 | x4node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5807 | unsigned h; |
| 5808 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5809 | |
| 5810 | if( x4a==0 ) return 0; |
| 5811 | ph = confighash(data); |
| 5812 | h = ph & (x4a->size-1); |
| 5813 | np = x4a->ht[h]; |
| 5814 | while( np ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5815 | if( Configcmp((const char *) np->data,(const char *) data)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5816 | /* An existing entry with the same key is found. */ |
| 5817 | /* Fail because overwrite is not allows. */ |
| 5818 | return 0; |
| 5819 | } |
| 5820 | np = np->next; |
| 5821 | } |
| 5822 | if( x4a->count>=x4a->size ){ |
| 5823 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5824 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5825 | struct s_x4 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5826 | array.size = arrSize = x4a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5827 | array.count = x4a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5828 | array.tbl = (x4node*)calloc(arrSize, sizeof(x4node) + sizeof(x4node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5829 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5830 | array.ht = (x4node**)&(array.tbl[arrSize]); |
| 5831 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5832 | for(i=0; i<x4a->count; i++){ |
| 5833 | x4node *oldnp, *newnp; |
| 5834 | oldnp = &(x4a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5835 | h = confighash(oldnp->data) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5836 | newnp = &(array.tbl[i]); |
| 5837 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5838 | newnp->next = array.ht[h]; |
| 5839 | newnp->data = oldnp->data; |
| 5840 | newnp->from = &(array.ht[h]); |
| 5841 | array.ht[h] = newnp; |
| 5842 | } |
| 5843 | free(x4a->tbl); |
| 5844 | *x4a = array; |
| 5845 | } |
| 5846 | /* Insert the new data */ |
| 5847 | h = ph & (x4a->size-1); |
| 5848 | np = &(x4a->tbl[x4a->count++]); |
| 5849 | np->data = data; |
| 5850 | if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next); |
| 5851 | np->next = x4a->ht[h]; |
| 5852 | x4a->ht[h] = np; |
| 5853 | np->from = &(x4a->ht[h]); |
| 5854 | return 1; |
| 5855 | } |
| 5856 | |
| 5857 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5858 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5859 | struct config *Configtable_find(struct config *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5860 | { |
| 5861 | int h; |
| 5862 | x4node *np; |
| 5863 | |
| 5864 | if( x4a==0 ) return 0; |
| 5865 | h = confighash(key) & (x4a->size-1); |
| 5866 | np = x4a->ht[h]; |
| 5867 | while( np ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5868 | if( Configcmp((const char *) np->data,(const char *) key)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5869 | np = np->next; |
| 5870 | } |
| 5871 | return np ? np->data : 0; |
| 5872 | } |
| 5873 | |
| 5874 | /* Remove all data from the table. Pass each data to the function "f" |
| 5875 | ** as it is removed. ("f" may be null to avoid this step.) */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5876 | void Configtable_clear(int(*f)(struct config *)) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5877 | { |
| 5878 | int i; |
| 5879 | if( x4a==0 || x4a->count==0 ) return; |
| 5880 | if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data); |
| 5881 | for(i=0; i<x4a->size; i++) x4a->ht[i] = 0; |
| 5882 | x4a->count = 0; |
| 5883 | return; |
| 5884 | } |