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 *); |
| 221 | void ReportTable(struct lemon *, int); |
| 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 | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 295 | int noCode; /* True if this rule has no associated C code */ |
| 296 | int codeEmitted; /* True if the code has been emitted already */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 297 | struct symbol *precsym; /* Precedence symbol for this rule */ |
| 298 | int index; /* An index number for this rule */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 299 | int iRule; /* Rule number as used in the generated tables */ |
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 | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 302 | struct rule *nextlhs; /* Next rule with the same LHS */ |
| 303 | struct rule *next; /* Next rule in the global list */ |
| 304 | }; |
| 305 | |
| 306 | /* A configuration is a production rule of the grammar together with |
| 307 | ** a mark (dot) showing how much of that rule has been processed so far. |
| 308 | ** Configurations also contain a follow-set which is a list of terminal |
| 309 | ** symbols which are allowed to immediately follow the end of the rule. |
| 310 | ** Every configuration is recorded as an instance of the following: */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 311 | enum cfgstatus { |
| 312 | COMPLETE, |
| 313 | INCOMPLETE |
| 314 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 315 | struct config { |
| 316 | struct rule *rp; /* The rule upon which the configuration is based */ |
| 317 | int dot; /* The parse point */ |
| 318 | char *fws; /* Follow-set for this configuration only */ |
| 319 | struct plink *fplp; /* Follow-set forward propagation links */ |
| 320 | struct plink *bplp; /* Follow-set backwards propagation links */ |
| 321 | struct state *stp; /* Pointer to state which contains this */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 322 | enum cfgstatus status; /* used during followset and shift computations */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 323 | struct config *next; /* Next configuration in the state */ |
| 324 | struct config *bp; /* The next basis configuration */ |
| 325 | }; |
| 326 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 327 | enum e_action { |
| 328 | SHIFT, |
| 329 | ACCEPT, |
| 330 | REDUCE, |
| 331 | ERROR, |
| 332 | SSCONFLICT, /* A shift/shift conflict */ |
| 333 | SRCONFLICT, /* Was a reduce, but part of a conflict */ |
| 334 | RRCONFLICT, /* Was a reduce, but part of a conflict */ |
| 335 | SH_RESOLVED, /* Was a shift. Precedence resolved conflict */ |
| 336 | RD_RESOLVED, /* Was reduce. Precedence resolved conflict */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 337 | NOT_USED, /* Deleted by compression */ |
| 338 | SHIFTREDUCE /* Shift first, then reduce */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 339 | }; |
| 340 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 341 | /* Every shift or reduce operation is stored as one of the following */ |
| 342 | struct action { |
| 343 | struct symbol *sp; /* The look-ahead symbol */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 344 | enum e_action type; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 345 | union { |
| 346 | struct state *stp; /* The new state, if a shift */ |
| 347 | struct rule *rp; /* The rule, if a reduce */ |
| 348 | } x; |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 349 | struct symbol *spOpt; /* SHIFTREDUCE optimization to this symbol */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 350 | struct action *next; /* Next action for this state */ |
| 351 | struct action *collide; /* Next action with the same hash */ |
| 352 | }; |
| 353 | |
| 354 | /* Each state of the generated parser's finite state machine |
| 355 | ** is encoded as an instance of the following structure. */ |
| 356 | struct state { |
| 357 | struct config *bp; /* The basis configurations for this state */ |
| 358 | struct config *cfp; /* All configurations in this set */ |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 359 | int statenum; /* Sequential number for this state */ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 360 | struct action *ap; /* List of actions for this state */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 361 | int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */ |
| 362 | int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 363 | int iDfltReduce; /* Default action is to REDUCE by this rule */ |
| 364 | struct rule *pDfltReduce;/* The default REDUCE rule. */ |
| 365 | int autoReduce; /* True if this is an auto-reduce state */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 366 | }; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 367 | #define NO_OFFSET (-2147483647) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 368 | |
| 369 | /* A followset propagation link indicates that the contents of one |
| 370 | ** configuration followset should be propagated to another whenever |
| 371 | ** the first changes. */ |
| 372 | struct plink { |
| 373 | struct config *cfp; /* The configuration to which linked */ |
| 374 | struct plink *next; /* The next propagate link */ |
| 375 | }; |
| 376 | |
| 377 | /* The state vector for the entire parser generator is recorded as |
| 378 | ** follows. (LEMON uses no global variables and makes little use of |
| 379 | ** static variables. Fields in the following structure can be thought |
| 380 | ** of as begin global variables in the program.) */ |
| 381 | struct lemon { |
| 382 | struct state **sorted; /* Table of states sorted by state number */ |
| 383 | struct rule *rule; /* List of all rules */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 384 | struct rule *startRule; /* First rule */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 385 | int nstate; /* Number of states */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 386 | int nxstate; /* nstate with tail degenerate states removed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 387 | int nrule; /* Number of rules */ |
| 388 | int nsymbol; /* Number of terminal and nonterminal symbols */ |
| 389 | int nterminal; /* Number of terminal symbols */ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 390 | int minShiftReduce; /* Minimum shift-reduce action value */ |
| 391 | int errAction; /* Error action value */ |
| 392 | int accAction; /* Accept action value */ |
| 393 | int noAction; /* No-op action value */ |
| 394 | int minReduce; /* Minimum reduce action */ |
| 395 | int maxAction; /* Maximum action value of any kind */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 396 | struct symbol **symbols; /* Sorted array of pointers to symbols */ |
| 397 | int errorcnt; /* Number of errors */ |
| 398 | struct symbol *errsym; /* The error symbol */ |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 399 | struct symbol *wildcard; /* Token that matches anything */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 400 | char *name; /* Name of the generated parser */ |
| 401 | char *arg; /* Declaration of the 3th argument to parser */ |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 402 | char *ctx; /* Declaration of 2nd argument to constructor */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 403 | char *tokentype; /* Type of terminal symbols in the parser stack */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 404 | char *vartype; /* The default type of non-terminal symbols */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 405 | char *start; /* Name of the start symbol for the grammar */ |
| 406 | char *stacksize; /* Size of the parser stack */ |
| 407 | char *include; /* Code to put at the start of the C file */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 408 | char *error; /* Code to execute when an error is seen */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 409 | char *overflow; /* Code to execute on a stack overflow */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 410 | char *failure; /* Code to execute on parser failure */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 411 | char *accept; /* Code to execute when the parser excepts */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 412 | char *extracode; /* Code appended to the generated file */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 413 | char *tokendest; /* Code to execute to destroy token data */ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 414 | char *vardest; /* Code for the default non-terminal destructor */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 415 | char *filename; /* Name of the input file */ |
| 416 | char *outname; /* Name of the current output file */ |
| 417 | char *tokenprefix; /* A prefix added to token names in the .h file */ |
| 418 | int nconflict; /* Number of parsing conflicts */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 419 | int nactiontab; /* Number of entries in the yy_action[] table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 420 | int nlookaheadtab; /* Number of entries in yy_lookahead[] */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 421 | int tablesize; /* Total table size of all tables in bytes */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 422 | int basisflag; /* Print only basis configurations */ |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 423 | int has_fallback; /* True if any %fallback is seen in the grammar */ |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 424 | int nolinenosflag; /* True if #line statements should not be printed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 425 | char *argv0; /* Name of the program */ |
| 426 | }; |
| 427 | |
| 428 | #define MemoryCheck(X) if((X)==0){ \ |
| 429 | extern void memory_error(); \ |
| 430 | memory_error(); \ |
| 431 | } |
| 432 | |
| 433 | /**************** From the file "table.h" *********************************/ |
| 434 | /* |
| 435 | ** All code in this file has been automatically generated |
| 436 | ** from a specification in the file |
| 437 | ** "table.q" |
| 438 | ** by the associative array code building program "aagen". |
| 439 | ** Do not edit this file! Instead, edit the specification |
| 440 | ** file, then rerun aagen. |
| 441 | */ |
| 442 | /* |
| 443 | ** Code for processing tables in the LEMON parser generator. |
| 444 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 445 | /* Routines for handling a strings */ |
| 446 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 447 | const char *Strsafe(const char *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 448 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 449 | void Strsafe_init(void); |
| 450 | int Strsafe_insert(const char *); |
| 451 | const char *Strsafe_find(const char *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 452 | |
| 453 | /* Routines for handling symbols of the grammar */ |
| 454 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 455 | struct symbol *Symbol_new(const char *); |
| 456 | int Symbolcmpp(const void *, const void *); |
| 457 | void Symbol_init(void); |
| 458 | int Symbol_insert(struct symbol *, const char *); |
| 459 | struct symbol *Symbol_find(const char *); |
| 460 | struct symbol *Symbol_Nth(int); |
| 461 | int Symbol_count(void); |
| 462 | struct symbol **Symbol_arrayof(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 463 | |
| 464 | /* Routines to manage the state table */ |
| 465 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 466 | int Configcmp(const char *, const char *); |
| 467 | struct state *State_new(void); |
| 468 | void State_init(void); |
| 469 | int State_insert(struct state *, struct config *); |
| 470 | struct state *State_find(struct config *); |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 471 | struct state **State_arrayof(void); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 472 | |
| 473 | /* Routines used for efficiency in Configlist_add */ |
| 474 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 475 | void Configtable_init(void); |
| 476 | int Configtable_insert(struct config *); |
| 477 | struct config *Configtable_find(struct config *); |
| 478 | void Configtable_clear(int(*)(struct config *)); |
| 479 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 480 | /****************** From the file "action.c" *******************************/ |
| 481 | /* |
| 482 | ** Routines processing parser actions in the LEMON parser generator. |
| 483 | */ |
| 484 | |
| 485 | /* Allocate a new parser action */ |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 486 | static struct action *Action_new(void){ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 487 | static struct action *actionfreelist = 0; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 488 | struct action *newaction; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 489 | |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 490 | if( actionfreelist==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 491 | int i; |
| 492 | int amt = 100; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 493 | actionfreelist = (struct action *)calloc(amt, sizeof(struct action)); |
| 494 | if( actionfreelist==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 495 | fprintf(stderr,"Unable to allocate memory for a new parser action."); |
| 496 | exit(1); |
| 497 | } |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 498 | for(i=0; i<amt-1; i++) actionfreelist[i].next = &actionfreelist[i+1]; |
| 499 | actionfreelist[amt-1].next = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 500 | } |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 501 | newaction = actionfreelist; |
| 502 | actionfreelist = actionfreelist->next; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 503 | return newaction; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 504 | } |
| 505 | |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 506 | /* Compare two actions for sorting purposes. Return negative, zero, or |
| 507 | ** positive if the first action is less than, equal to, or greater than |
| 508 | ** the first |
| 509 | */ |
| 510 | static int actioncmp( |
| 511 | struct action *ap1, |
| 512 | struct action *ap2 |
| 513 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 514 | int rc; |
| 515 | rc = ap1->sp->index - ap2->sp->index; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 516 | if( rc==0 ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 517 | rc = (int)ap1->type - (int)ap2->type; |
| 518 | } |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 519 | if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 520 | rc = ap1->x.rp->index - ap2->x.rp->index; |
| 521 | } |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 522 | if( rc==0 ){ |
icculus | 7b429aa | 2010-03-03 17:09:01 +0000 | [diff] [blame] | 523 | rc = (int) (ap2 - ap1); |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 524 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 525 | return rc; |
| 526 | } |
| 527 | |
| 528 | /* Sort parser actions */ |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 529 | static struct action *Action_sort( |
| 530 | struct action *ap |
| 531 | ){ |
| 532 | ap = (struct action *)msort((char *)ap,(char **)&ap->next, |
| 533 | (int(*)(const char*,const char*))actioncmp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 534 | return ap; |
| 535 | } |
| 536 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 537 | void Action_add( |
| 538 | struct action **app, |
| 539 | enum e_action type, |
| 540 | struct symbol *sp, |
| 541 | char *arg |
| 542 | ){ |
| 543 | struct action *newaction; |
| 544 | newaction = Action_new(); |
| 545 | newaction->next = *app; |
| 546 | *app = newaction; |
| 547 | newaction->type = type; |
| 548 | newaction->sp = sp; |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 549 | newaction->spOpt = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 550 | if( type==SHIFT ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 551 | newaction->x.stp = (struct state *)arg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 552 | }else{ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 553 | newaction->x.rp = (struct rule *)arg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 556 | /********************** New code to implement the "acttab" module ***********/ |
| 557 | /* |
| 558 | ** This module implements routines use to construct the yy_action[] table. |
| 559 | */ |
| 560 | |
| 561 | /* |
| 562 | ** The state of the yy_action table under construction is an instance of |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 563 | ** the following structure. |
| 564 | ** |
| 565 | ** The yy_action table maps the pair (state_number, lookahead) into an |
| 566 | ** action_number. The table is an array of integers pairs. The state_number |
| 567 | ** determines an initial offset into the yy_action array. The lookahead |
| 568 | ** value is then added to this initial offset to get an index X into the |
| 569 | ** yy_action array. If the aAction[X].lookahead equals the value of the |
| 570 | ** of the lookahead input, then the value of the action_number output is |
| 571 | ** aAction[X].action. If the lookaheads do not match then the |
| 572 | ** default action for the state_number is returned. |
| 573 | ** |
| 574 | ** All actions associated with a single state_number are first entered |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 575 | ** into aLookahead[] using multiple calls to acttab_action(). Then the |
| 576 | ** actions for that single state_number are placed into the aAction[] |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 577 | ** array with a single call to acttab_insert(). The acttab_insert() call |
| 578 | ** also resets the aLookahead[] array in preparation for the next |
| 579 | ** state number. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 580 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 581 | struct lookahead_action { |
| 582 | int lookahead; /* Value of the lookahead token */ |
| 583 | int action; /* Action to take on the given lookahead */ |
| 584 | }; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 585 | typedef struct acttab acttab; |
| 586 | struct acttab { |
| 587 | int nAction; /* Number of used slots in aAction[] */ |
| 588 | int nActionAlloc; /* Slots allocated for aAction[] */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 589 | struct lookahead_action |
| 590 | *aAction, /* The yy_action[] table under construction */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 591 | *aLookahead; /* A single new transaction set */ |
| 592 | int mnLookahead; /* Minimum aLookahead[].lookahead */ |
| 593 | int mnAction; /* Action associated with mnLookahead */ |
| 594 | int mxLookahead; /* Maximum aLookahead[].lookahead */ |
| 595 | int nLookahead; /* Used slots in aLookahead[] */ |
| 596 | int nLookaheadAlloc; /* Slots allocated in aLookahead[] */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 597 | int nterminal; /* Number of terminal symbols */ |
| 598 | int nsymbol; /* total number of symbols */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | /* Return the number of entries in the yy_action table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 602 | #define acttab_lookahead_size(X) ((X)->nAction) |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 603 | |
| 604 | /* The value for the N-th entry in yy_action */ |
| 605 | #define acttab_yyaction(X,N) ((X)->aAction[N].action) |
| 606 | |
| 607 | /* The value for the N-th entry in yy_lookahead */ |
| 608 | #define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead) |
| 609 | |
| 610 | /* Free all memory associated with the given acttab */ |
| 611 | void acttab_free(acttab *p){ |
| 612 | free( p->aAction ); |
| 613 | free( p->aLookahead ); |
| 614 | free( p ); |
| 615 | } |
| 616 | |
| 617 | /* Allocate a new acttab structure */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 618 | acttab *acttab_alloc(int nsymbol, int nterminal){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 619 | acttab *p = (acttab *) calloc( 1, sizeof(*p) ); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 620 | if( p==0 ){ |
| 621 | fprintf(stderr,"Unable to allocate memory for a new acttab."); |
| 622 | exit(1); |
| 623 | } |
| 624 | memset(p, 0, sizeof(*p)); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 625 | p->nsymbol = nsymbol; |
| 626 | p->nterminal = nterminal; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 627 | return p; |
| 628 | } |
| 629 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 630 | /* Add a new action to the current transaction set. |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 631 | ** |
| 632 | ** This routine is called once for each lookahead for a particular |
| 633 | ** state. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 634 | */ |
| 635 | void acttab_action(acttab *p, int lookahead, int action){ |
| 636 | if( p->nLookahead>=p->nLookaheadAlloc ){ |
| 637 | p->nLookaheadAlloc += 25; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 638 | p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead, |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 639 | sizeof(p->aLookahead[0])*p->nLookaheadAlloc ); |
| 640 | if( p->aLookahead==0 ){ |
| 641 | fprintf(stderr,"malloc failed\n"); |
| 642 | exit(1); |
| 643 | } |
| 644 | } |
| 645 | if( p->nLookahead==0 ){ |
| 646 | p->mxLookahead = lookahead; |
| 647 | p->mnLookahead = lookahead; |
| 648 | p->mnAction = action; |
| 649 | }else{ |
| 650 | if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead; |
| 651 | if( p->mnLookahead>lookahead ){ |
| 652 | p->mnLookahead = lookahead; |
| 653 | p->mnAction = action; |
| 654 | } |
| 655 | } |
| 656 | p->aLookahead[p->nLookahead].lookahead = lookahead; |
| 657 | p->aLookahead[p->nLookahead].action = action; |
| 658 | p->nLookahead++; |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | ** Add the transaction set built up with prior calls to acttab_action() |
| 663 | ** into the current action table. Then reset the transaction set back |
| 664 | ** to an empty set in preparation for a new round of acttab_action() calls. |
| 665 | ** |
| 666 | ** Return the offset into the action table of the new transaction. |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 667 | ** |
| 668 | ** If the makeItSafe parameter is true, then the offset is chosen so that |
| 669 | ** it is impossible to overread the yy_lookaside[] table regardless of |
| 670 | ** the lookaside token. This is done for the terminal symbols, as they |
| 671 | ** come from external inputs and can contain syntax errors. When makeItSafe |
| 672 | ** is false, there is more flexibility in selecting offsets, resulting in |
| 673 | ** a smaller table. For non-terminal symbols, which are never syntax errors, |
| 674 | ** makeItSafe can be false. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 675 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 676 | int acttab_insert(acttab *p, int makeItSafe){ |
| 677 | int i, j, k, n, end; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 678 | assert( p->nLookahead>0 ); |
| 679 | |
| 680 | /* Make sure we have enough space to hold the expanded action table |
| 681 | ** in the worst case. The worst case occurs if the transaction set |
| 682 | ** must be appended to the current action table |
| 683 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 684 | n = p->nsymbol + 1; |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 685 | if( p->nAction + n >= p->nActionAlloc ){ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 686 | int oldAlloc = p->nActionAlloc; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 687 | p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 688 | p->aAction = (struct lookahead_action *) realloc( p->aAction, |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 689 | sizeof(p->aAction[0])*p->nActionAlloc); |
| 690 | if( p->aAction==0 ){ |
| 691 | fprintf(stderr,"malloc failed\n"); |
| 692 | exit(1); |
| 693 | } |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 694 | for(i=oldAlloc; i<p->nActionAlloc; i++){ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 695 | p->aAction[i].lookahead = -1; |
| 696 | p->aAction[i].action = -1; |
| 697 | } |
| 698 | } |
| 699 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 700 | /* Scan the existing action table looking for an offset that is a |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 701 | ** duplicate of the current transaction set. Fall out of the loop |
| 702 | ** if and when the duplicate is found. |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 703 | ** |
| 704 | ** i is the index in p->aAction[] where p->mnLookahead is inserted. |
| 705 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 706 | end = makeItSafe ? p->mnLookahead : 0; |
| 707 | for(i=p->nAction-1; i>=end; i--){ |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 708 | if( p->aAction[i].lookahead==p->mnLookahead ){ |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 709 | /* All lookaheads and actions in the aLookahead[] transaction |
| 710 | ** must match against the candidate aAction[i] entry. */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 711 | if( p->aAction[i].action!=p->mnAction ) continue; |
| 712 | for(j=0; j<p->nLookahead; j++){ |
| 713 | k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| 714 | if( k<0 || k>=p->nAction ) break; |
| 715 | if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break; |
| 716 | if( p->aLookahead[j].action!=p->aAction[k].action ) break; |
| 717 | } |
| 718 | if( j<p->nLookahead ) continue; |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 719 | |
| 720 | /* No possible lookahead value that is not in the aLookahead[] |
| 721 | ** transaction is allowed to match aAction[i] */ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 722 | n = 0; |
| 723 | for(j=0; j<p->nAction; j++){ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 724 | if( p->aAction[j].lookahead<0 ) continue; |
| 725 | if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 726 | } |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 727 | if( n==p->nLookahead ){ |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 728 | break; /* An exact match is found at offset i */ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 729 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 732 | |
| 733 | /* If no existing offsets exactly match the current transaction, find an |
| 734 | ** an empty offset in the aAction[] table in which we can add the |
| 735 | ** aLookahead[] transaction. |
| 736 | */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 737 | if( i<end ){ |
drh | 8dc3e8f | 2010-01-07 03:53:03 +0000 | [diff] [blame] | 738 | /* Look for holes in the aAction[] table that fit the current |
| 739 | ** aLookahead[] transaction. Leave i set to the offset of the hole. |
| 740 | ** If no holes are found, i is left at p->nAction, which means the |
| 741 | ** transaction will be appended. */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 742 | i = makeItSafe ? p->mnLookahead : 0; |
| 743 | for(; i<p->nActionAlloc - p->mxLookahead; i++){ |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 744 | if( p->aAction[i].lookahead<0 ){ |
| 745 | for(j=0; j<p->nLookahead; j++){ |
| 746 | k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| 747 | if( k<0 ) break; |
| 748 | if( p->aAction[k].lookahead>=0 ) break; |
| 749 | } |
| 750 | if( j<p->nLookahead ) continue; |
| 751 | for(j=0; j<p->nAction; j++){ |
| 752 | if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break; |
| 753 | } |
| 754 | if( j==p->nAction ){ |
| 755 | break; /* Fits in empty slots */ |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 760 | /* Insert transaction set at index i. */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 761 | #if 0 |
| 762 | printf("Acttab:"); |
| 763 | for(j=0; j<p->nLookahead; j++){ |
| 764 | printf(" %d", p->aLookahead[j].lookahead); |
| 765 | } |
| 766 | printf(" inserted at %d\n", i); |
| 767 | #endif |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 768 | for(j=0; j<p->nLookahead; j++){ |
| 769 | k = p->aLookahead[j].lookahead - p->mnLookahead + i; |
| 770 | p->aAction[k] = p->aLookahead[j]; |
| 771 | if( k>=p->nAction ) p->nAction = k+1; |
| 772 | } |
drh | 4396c61 | 2017-12-27 15:21:16 +0000 | [diff] [blame] | 773 | if( makeItSafe && i+p->nterminal>=p->nAction ) p->nAction = i+p->nterminal+1; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 774 | p->nLookahead = 0; |
| 775 | |
| 776 | /* Return the offset that is added to the lookahead in order to get the |
| 777 | ** index into yy_action of the action */ |
| 778 | return i - p->mnLookahead; |
| 779 | } |
| 780 | |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 781 | /* |
| 782 | ** Return the size of the action table without the trailing syntax error |
| 783 | ** entries. |
| 784 | */ |
| 785 | int acttab_action_size(acttab *p){ |
| 786 | int n = p->nAction; |
| 787 | while( n>0 && p->aAction[n-1].lookahead<0 ){ n--; } |
| 788 | return n; |
| 789 | } |
| 790 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 791 | /********************** From the file "build.c" *****************************/ |
| 792 | /* |
| 793 | ** Routines to construction the finite state machine for the LEMON |
| 794 | ** parser generator. |
| 795 | */ |
| 796 | |
| 797 | /* Find a precedence symbol of every rule in the grammar. |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 798 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 799 | ** Those rules which have a precedence symbol coded in the input |
| 800 | ** grammar using the "[symbol]" construct will already have the |
| 801 | ** rp->precsym field filled. Other rules take as their precedence |
| 802 | ** symbol the first RHS symbol with a defined precedence. If there |
| 803 | ** are not RHS symbols with a defined precedence, the precedence |
| 804 | ** symbol field is left blank. |
| 805 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 806 | void FindRulePrecedences(struct lemon *xp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 807 | { |
| 808 | struct rule *rp; |
| 809 | for(rp=xp->rule; rp; rp=rp->next){ |
| 810 | if( rp->precsym==0 ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 811 | int i, j; |
| 812 | for(i=0; i<rp->nrhs && rp->precsym==0; i++){ |
| 813 | struct symbol *sp = rp->rhs[i]; |
| 814 | if( sp->type==MULTITERMINAL ){ |
| 815 | for(j=0; j<sp->nsubsym; j++){ |
| 816 | if( sp->subsym[j]->prec>=0 ){ |
| 817 | rp->precsym = sp->subsym[j]; |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | }else if( sp->prec>=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 822 | rp->precsym = rp->rhs[i]; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 823 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | } |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | /* Find all nonterminals which will generate the empty string. |
| 831 | ** Then go back and compute the first sets of every nonterminal. |
| 832 | ** The first set is the set of all terminal symbols which can begin |
| 833 | ** a string generated by that nonterminal. |
| 834 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 835 | void FindFirstSets(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 836 | { |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 837 | int i, j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 838 | struct rule *rp; |
| 839 | int progress; |
| 840 | |
| 841 | for(i=0; i<lemp->nsymbol; i++){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 842 | lemp->symbols[i]->lambda = LEMON_FALSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 843 | } |
| 844 | for(i=lemp->nterminal; i<lemp->nsymbol; i++){ |
| 845 | lemp->symbols[i]->firstset = SetNew(); |
| 846 | } |
| 847 | |
| 848 | /* First compute all lambdas */ |
| 849 | do{ |
| 850 | progress = 0; |
| 851 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 852 | if( rp->lhs->lambda ) continue; |
| 853 | for(i=0; i<rp->nrhs; i++){ |
drh | 7dd1ac6 | 2012-01-07 15:17:18 +0000 | [diff] [blame] | 854 | struct symbol *sp = rp->rhs[i]; |
| 855 | assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE ); |
| 856 | if( sp->lambda==LEMON_FALSE ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 857 | } |
| 858 | if( i==rp->nrhs ){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 859 | rp->lhs->lambda = LEMON_TRUE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 860 | progress = 1; |
| 861 | } |
| 862 | } |
| 863 | }while( progress ); |
| 864 | |
| 865 | /* Now compute all first sets */ |
| 866 | do{ |
| 867 | struct symbol *s1, *s2; |
| 868 | progress = 0; |
| 869 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 870 | s1 = rp->lhs; |
| 871 | for(i=0; i<rp->nrhs; i++){ |
| 872 | s2 = rp->rhs[i]; |
| 873 | if( s2->type==TERMINAL ){ |
| 874 | progress += SetAdd(s1->firstset,s2->index); |
| 875 | break; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 876 | }else if( s2->type==MULTITERMINAL ){ |
| 877 | for(j=0; j<s2->nsubsym; j++){ |
| 878 | progress += SetAdd(s1->firstset,s2->subsym[j]->index); |
| 879 | } |
| 880 | break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 881 | }else if( s1==s2 ){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 882 | if( s1->lambda==LEMON_FALSE ) break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 883 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 884 | progress += SetUnion(s1->firstset,s2->firstset); |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 885 | if( s2->lambda==LEMON_FALSE ) break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 886 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | }while( progress ); |
| 890 | return; |
| 891 | } |
| 892 | |
| 893 | /* Compute all LR(0) states for the grammar. Links |
| 894 | ** are added to between some states so that the LR(1) follow sets |
| 895 | ** can be computed later. |
| 896 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 897 | PRIVATE struct state *getstate(struct lemon *); /* forward reference */ |
| 898 | void FindStates(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 899 | { |
| 900 | struct symbol *sp; |
| 901 | struct rule *rp; |
| 902 | |
| 903 | Configlist_init(); |
| 904 | |
| 905 | /* Find the start symbol */ |
| 906 | if( lemp->start ){ |
| 907 | sp = Symbol_find(lemp->start); |
| 908 | if( sp==0 ){ |
| 909 | ErrorMsg(lemp->filename,0, |
| 910 | "The specified start symbol \"%s\" is not \ |
| 911 | in a nonterminal of the grammar. \"%s\" will be used as the start \ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 912 | symbol instead.",lemp->start,lemp->startRule->lhs->name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 913 | lemp->errorcnt++; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 914 | sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 915 | } |
| 916 | }else{ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 917 | sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /* Make sure the start symbol doesn't occur on the right-hand side of |
| 921 | ** any rule. Report an error if it does. (YACC would generate a new |
| 922 | ** start symbol in this case.) */ |
| 923 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 924 | int i; |
| 925 | for(i=0; i<rp->nrhs; i++){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 926 | if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 927 | ErrorMsg(lemp->filename,0, |
| 928 | "The start symbol \"%s\" occurs on the \ |
| 929 | right-hand side of a rule. This will result in a parser which \ |
| 930 | does not work properly.",sp->name); |
| 931 | lemp->errorcnt++; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | /* The basis configuration set for the first state |
| 937 | ** is all rules which have the start symbol as their |
| 938 | ** left-hand side */ |
| 939 | for(rp=sp->rule; rp; rp=rp->nextlhs){ |
| 940 | struct config *newcfp; |
drh | b496099 | 2007-10-05 16:16:36 +0000 | [diff] [blame] | 941 | rp->lhsStart = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 942 | newcfp = Configlist_addbasis(rp,0); |
| 943 | SetAdd(newcfp->fws,0); |
| 944 | } |
| 945 | |
| 946 | /* Compute the first state. All other states will be |
| 947 | ** computed automatically during the computation of the first one. |
| 948 | ** The returned pointer to the first state is not used. */ |
| 949 | (void)getstate(lemp); |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | /* Return a pointer to a state which is described by the configuration |
| 954 | ** list which has been built from calls to Configlist_add. |
| 955 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 956 | PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */ |
| 957 | PRIVATE struct state *getstate(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 958 | { |
| 959 | struct config *cfp, *bp; |
| 960 | struct state *stp; |
| 961 | |
| 962 | /* Extract the sorted basis of the new state. The basis was constructed |
| 963 | ** by prior calls to "Configlist_addbasis()". */ |
| 964 | Configlist_sortbasis(); |
| 965 | bp = Configlist_basis(); |
| 966 | |
| 967 | /* Get a state with the same basis */ |
| 968 | stp = State_find(bp); |
| 969 | if( stp ){ |
| 970 | /* A state with the same basis already exists! Copy all the follow-set |
| 971 | ** propagation links from the state under construction into the |
| 972 | ** preexisting state, then return a pointer to the preexisting state */ |
| 973 | struct config *x, *y; |
| 974 | for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){ |
| 975 | Plink_copy(&y->bplp,x->bplp); |
| 976 | Plink_delete(x->fplp); |
| 977 | x->fplp = x->bplp = 0; |
| 978 | } |
| 979 | cfp = Configlist_return(); |
| 980 | Configlist_eat(cfp); |
| 981 | }else{ |
| 982 | /* This really is a new state. Construct all the details */ |
| 983 | Configlist_closure(lemp); /* Compute the configuration closure */ |
| 984 | Configlist_sort(); /* Sort the configuration closure */ |
| 985 | cfp = Configlist_return(); /* Get a pointer to the config list */ |
| 986 | stp = State_new(); /* A new state structure */ |
| 987 | MemoryCheck(stp); |
| 988 | stp->bp = bp; /* Remember the configuration basis */ |
| 989 | stp->cfp = cfp; /* Remember the configuration closure */ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 990 | stp->statenum = lemp->nstate++; /* Every state gets a sequence number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 991 | stp->ap = 0; /* No actions, yet. */ |
| 992 | State_insert(stp,stp->bp); /* Add to the state table */ |
| 993 | buildshifts(lemp,stp); /* Recursively compute successor states */ |
| 994 | } |
| 995 | return stp; |
| 996 | } |
| 997 | |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 998 | /* |
| 999 | ** Return true if two symbols are the same. |
| 1000 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1001 | int same_symbol(struct symbol *a, struct symbol *b) |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1002 | { |
| 1003 | int i; |
| 1004 | if( a==b ) return 1; |
| 1005 | if( a->type!=MULTITERMINAL ) return 0; |
| 1006 | if( b->type!=MULTITERMINAL ) return 0; |
| 1007 | if( a->nsubsym!=b->nsubsym ) return 0; |
| 1008 | for(i=0; i<a->nsubsym; i++){ |
| 1009 | if( a->subsym[i]!=b->subsym[i] ) return 0; |
| 1010 | } |
| 1011 | return 1; |
| 1012 | } |
| 1013 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1014 | /* Construct all successor states to the given state. A "successor" |
| 1015 | ** state is any state which can be reached by a shift action. |
| 1016 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1017 | PRIVATE void buildshifts(struct lemon *lemp, struct state *stp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1018 | { |
| 1019 | struct config *cfp; /* For looping thru the config closure of "stp" */ |
| 1020 | struct config *bcfp; /* For the inner loop on config closure of "stp" */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1021 | struct config *newcfg; /* */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1022 | struct symbol *sp; /* Symbol following the dot in configuration "cfp" */ |
| 1023 | struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */ |
| 1024 | struct state *newstp; /* A pointer to a successor state */ |
| 1025 | |
| 1026 | /* Each configuration becomes complete after it contibutes to a successor |
| 1027 | ** state. Initially, all configurations are incomplete */ |
| 1028 | for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE; |
| 1029 | |
| 1030 | /* Loop through all configurations of the state "stp" */ |
| 1031 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| 1032 | if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */ |
| 1033 | if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */ |
| 1034 | Configlist_reset(); /* Reset the new config set */ |
| 1035 | sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */ |
| 1036 | |
| 1037 | /* For every configuration in the state "stp" which has the symbol "sp" |
| 1038 | ** following its dot, add the same configuration to the basis set under |
| 1039 | ** construction but with the dot shifted one symbol to the right. */ |
| 1040 | for(bcfp=cfp; bcfp; bcfp=bcfp->next){ |
| 1041 | if( bcfp->status==COMPLETE ) continue; /* Already used */ |
| 1042 | if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */ |
| 1043 | bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1044 | if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1045 | bcfp->status = COMPLETE; /* Mark this config as used */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1046 | newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1); |
| 1047 | Plink_add(&newcfg->bplp,bcfp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /* Get a pointer to the state described by the basis configuration set |
| 1051 | ** constructed in the preceding loop */ |
| 1052 | newstp = getstate(lemp); |
| 1053 | |
| 1054 | /* The state "newstp" is reached from the state "stp" by a shift action |
| 1055 | ** on the symbol "sp" */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1056 | if( sp->type==MULTITERMINAL ){ |
| 1057 | int i; |
| 1058 | for(i=0; i<sp->nsubsym; i++){ |
| 1059 | Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp); |
| 1060 | } |
| 1061 | }else{ |
| 1062 | Action_add(&stp->ap,SHIFT,sp,(char *)newstp); |
| 1063 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | ** Construct the propagation links |
| 1069 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1070 | void FindLinks(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1071 | { |
| 1072 | int i; |
| 1073 | struct config *cfp, *other; |
| 1074 | struct state *stp; |
| 1075 | struct plink *plp; |
| 1076 | |
| 1077 | /* Housekeeping detail: |
| 1078 | ** Add to every propagate link a pointer back to the state to |
| 1079 | ** which the link is attached. */ |
| 1080 | for(i=0; i<lemp->nstate; i++){ |
| 1081 | stp = lemp->sorted[i]; |
| 1082 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| 1083 | cfp->stp = stp; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | /* Convert all backlinks into forward links. Only the forward |
| 1088 | ** links are used in the follow-set computation. */ |
| 1089 | for(i=0; i<lemp->nstate; i++){ |
| 1090 | stp = lemp->sorted[i]; |
| 1091 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ |
| 1092 | for(plp=cfp->bplp; plp; plp=plp->next){ |
| 1093 | other = plp->cfp; |
| 1094 | Plink_add(&other->fplp,cfp); |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | /* Compute all followsets. |
| 1101 | ** |
| 1102 | ** A followset is the set of all symbols which can come immediately |
| 1103 | ** after a configuration. |
| 1104 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1105 | void FindFollowSets(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1106 | { |
| 1107 | int i; |
| 1108 | struct config *cfp; |
| 1109 | struct plink *plp; |
| 1110 | int progress; |
| 1111 | int change; |
| 1112 | |
| 1113 | for(i=0; i<lemp->nstate; i++){ |
| 1114 | for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ |
| 1115 | cfp->status = INCOMPLETE; |
| 1116 | } |
| 1117 | } |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1118 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1119 | do{ |
| 1120 | progress = 0; |
| 1121 | for(i=0; i<lemp->nstate; i++){ |
| 1122 | for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ |
| 1123 | if( cfp->status==COMPLETE ) continue; |
| 1124 | for(plp=cfp->fplp; plp; plp=plp->next){ |
| 1125 | change = SetUnion(plp->cfp->fws,cfp->fws); |
| 1126 | if( change ){ |
| 1127 | plp->cfp->status = INCOMPLETE; |
| 1128 | progress = 1; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1129 | } |
| 1130 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1131 | cfp->status = COMPLETE; |
| 1132 | } |
| 1133 | } |
| 1134 | }while( progress ); |
| 1135 | } |
| 1136 | |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 1137 | static int resolve_conflict(struct action *,struct action *); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1138 | |
| 1139 | /* Compute the reduce actions, and resolve conflicts. |
| 1140 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1141 | void FindActions(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1142 | { |
| 1143 | int i,j; |
| 1144 | struct config *cfp; |
| 1145 | struct state *stp; |
| 1146 | struct symbol *sp; |
| 1147 | struct rule *rp; |
| 1148 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1149 | /* Add all of the reduce actions |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1150 | ** A reduce action is added for each element of the followset of |
| 1151 | ** a configuration which has its dot at the extreme right. |
| 1152 | */ |
| 1153 | for(i=0; i<lemp->nstate; i++){ /* Loop over all states */ |
| 1154 | stp = lemp->sorted[i]; |
| 1155 | for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */ |
| 1156 | if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */ |
| 1157 | for(j=0; j<lemp->nterminal; j++){ |
| 1158 | if( SetFind(cfp->fws,j) ){ |
| 1159 | /* Add a reduce action to the state "stp" which will reduce by the |
| 1160 | ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */ |
drh | 218dc69 | 2004-05-31 23:13:45 +0000 | [diff] [blame] | 1161 | Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1162 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1163 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | /* Add the accepting token */ |
| 1169 | if( lemp->start ){ |
| 1170 | sp = Symbol_find(lemp->start); |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1171 | if( sp==0 ) sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1172 | }else{ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1173 | sp = lemp->startRule->lhs; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1174 | } |
| 1175 | /* Add to the first state (which is always the starting state of the |
| 1176 | ** finite state machine) an action to ACCEPT if the lookahead is the |
| 1177 | ** start nonterminal. */ |
| 1178 | Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0); |
| 1179 | |
| 1180 | /* Resolve conflicts */ |
| 1181 | for(i=0; i<lemp->nstate; i++){ |
| 1182 | struct action *ap, *nap; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1183 | stp = lemp->sorted[i]; |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 1184 | /* assert( stp->ap ); */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1185 | stp->ap = Action_sort(stp->ap); |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1186 | for(ap=stp->ap; ap && ap->next; ap=ap->next){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1187 | for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){ |
| 1188 | /* The two actions "ap" and "nap" have the same lookahead. |
| 1189 | ** Figure out which one should be used */ |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 1190 | lemp->nconflict += resolve_conflict(ap,nap); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | /* Report an error for each rule that can never be reduced. */ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 1196 | for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1197 | for(i=0; i<lemp->nstate; i++){ |
| 1198 | struct action *ap; |
| 1199 | for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){ |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 1200 | if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 1204 | if( rp->canReduce ) continue; |
| 1205 | ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n"); |
| 1206 | lemp->errorcnt++; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | /* Resolve a conflict between the two given actions. If the |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 1211 | ** conflict can't be resolved, return non-zero. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1212 | ** |
| 1213 | ** NO LONGER TRUE: |
| 1214 | ** To resolve a conflict, first look to see if either action |
| 1215 | ** is on an error rule. In that case, take the action which |
| 1216 | ** is not associated with the error rule. If neither or both |
| 1217 | ** actions are associated with an error rule, then try to |
| 1218 | ** use precedence to resolve the conflict. |
| 1219 | ** |
| 1220 | ** If either action is a SHIFT, then it must be apx. This |
| 1221 | ** function won't work if apx->type==REDUCE and apy->type==SHIFT. |
| 1222 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1223 | static int resolve_conflict( |
| 1224 | struct action *apx, |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 1225 | struct action *apy |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1226 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1227 | struct symbol *spx, *spy; |
| 1228 | int errcnt = 0; |
| 1229 | assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */ |
drh | f0fa1c1 | 2006-12-14 01:06:22 +0000 | [diff] [blame] | 1230 | if( apx->type==SHIFT && apy->type==SHIFT ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1231 | apy->type = SSCONFLICT; |
drh | f0fa1c1 | 2006-12-14 01:06:22 +0000 | [diff] [blame] | 1232 | errcnt++; |
| 1233 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1234 | if( apx->type==SHIFT && apy->type==REDUCE ){ |
| 1235 | spx = apx->sp; |
| 1236 | spy = apy->x.rp->precsym; |
| 1237 | if( spy==0 || spx->prec<0 || spy->prec<0 ){ |
| 1238 | /* Not enough precedence information. */ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1239 | apy->type = SRCONFLICT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1240 | errcnt++; |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1241 | }else if( spx->prec>spy->prec ){ /* higher precedence wins */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1242 | apy->type = RD_RESOLVED; |
| 1243 | }else if( spx->prec<spy->prec ){ |
| 1244 | apx->type = SH_RESOLVED; |
| 1245 | }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */ |
| 1246 | apy->type = RD_RESOLVED; /* associativity */ |
| 1247 | }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */ |
| 1248 | apx->type = SH_RESOLVED; |
| 1249 | }else{ |
| 1250 | assert( spx->prec==spy->prec && spx->assoc==NONE ); |
drh | 62a223e | 2014-06-09 13:11:40 +0000 | [diff] [blame] | 1251 | apx->type = ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1252 | } |
| 1253 | }else if( apx->type==REDUCE && apy->type==REDUCE ){ |
| 1254 | spx = apx->x.rp->precsym; |
| 1255 | spy = apy->x.rp->precsym; |
| 1256 | if( spx==0 || spy==0 || spx->prec<0 || |
| 1257 | spy->prec<0 || spx->prec==spy->prec ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1258 | apy->type = RRCONFLICT; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1259 | errcnt++; |
| 1260 | }else if( spx->prec>spy->prec ){ |
| 1261 | apy->type = RD_RESOLVED; |
| 1262 | }else if( spx->prec<spy->prec ){ |
| 1263 | apx->type = RD_RESOLVED; |
| 1264 | } |
| 1265 | }else{ |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1266 | assert( |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1267 | apx->type==SH_RESOLVED || |
| 1268 | apx->type==RD_RESOLVED || |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1269 | apx->type==SSCONFLICT || |
| 1270 | apx->type==SRCONFLICT || |
| 1271 | apx->type==RRCONFLICT || |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1272 | apy->type==SH_RESOLVED || |
| 1273 | apy->type==RD_RESOLVED || |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1274 | apy->type==SSCONFLICT || |
| 1275 | apy->type==SRCONFLICT || |
| 1276 | apy->type==RRCONFLICT |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 1277 | ); |
| 1278 | /* The REDUCE/SHIFT case cannot happen because SHIFTs come before |
| 1279 | ** REDUCEs on the list. If we reach this point it must be because |
| 1280 | ** the parser conflict had already been resolved. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1281 | } |
| 1282 | return errcnt; |
| 1283 | } |
| 1284 | /********************* From the file "configlist.c" *************************/ |
| 1285 | /* |
| 1286 | ** Routines to processing a configuration list and building a state |
| 1287 | ** in the LEMON parser generator. |
| 1288 | */ |
| 1289 | |
| 1290 | static struct config *freelist = 0; /* List of free configurations */ |
| 1291 | static struct config *current = 0; /* Top of list of configurations */ |
| 1292 | static struct config **currentend = 0; /* Last on list of configs */ |
| 1293 | static struct config *basis = 0; /* Top of list of basis configs */ |
| 1294 | static struct config **basisend = 0; /* End of list of basis configs */ |
| 1295 | |
| 1296 | /* Return a pointer to a new configuration */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1297 | PRIVATE struct config *newconfig(void){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1298 | struct config *newcfg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1299 | if( freelist==0 ){ |
| 1300 | int i; |
| 1301 | int amt = 3; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1302 | freelist = (struct config *)calloc( amt, sizeof(struct config) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1303 | if( freelist==0 ){ |
| 1304 | fprintf(stderr,"Unable to allocate memory for a new configuration."); |
| 1305 | exit(1); |
| 1306 | } |
| 1307 | for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1]; |
| 1308 | freelist[amt-1].next = 0; |
| 1309 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1310 | newcfg = freelist; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1311 | freelist = freelist->next; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1312 | return newcfg; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | /* The configuration "old" is no longer used */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1316 | PRIVATE void deleteconfig(struct config *old) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1317 | { |
| 1318 | old->next = freelist; |
| 1319 | freelist = old; |
| 1320 | } |
| 1321 | |
| 1322 | /* Initialized the configuration list builder */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1323 | void Configlist_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1324 | current = 0; |
| 1325 | currentend = ¤t; |
| 1326 | basis = 0; |
| 1327 | basisend = &basis; |
| 1328 | Configtable_init(); |
| 1329 | return; |
| 1330 | } |
| 1331 | |
| 1332 | /* Initialized the configuration list builder */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1333 | void Configlist_reset(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1334 | current = 0; |
| 1335 | currentend = ¤t; |
| 1336 | basis = 0; |
| 1337 | basisend = &basis; |
| 1338 | Configtable_clear(0); |
| 1339 | return; |
| 1340 | } |
| 1341 | |
| 1342 | /* Add another configuration to the configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1343 | struct config *Configlist_add( |
| 1344 | struct rule *rp, /* The rule */ |
| 1345 | int dot /* Index into the RHS of the rule where the dot goes */ |
| 1346 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1347 | struct config *cfp, model; |
| 1348 | |
| 1349 | assert( currentend!=0 ); |
| 1350 | model.rp = rp; |
| 1351 | model.dot = dot; |
| 1352 | cfp = Configtable_find(&model); |
| 1353 | if( cfp==0 ){ |
| 1354 | cfp = newconfig(); |
| 1355 | cfp->rp = rp; |
| 1356 | cfp->dot = dot; |
| 1357 | cfp->fws = SetNew(); |
| 1358 | cfp->stp = 0; |
| 1359 | cfp->fplp = cfp->bplp = 0; |
| 1360 | cfp->next = 0; |
| 1361 | cfp->bp = 0; |
| 1362 | *currentend = cfp; |
| 1363 | currentend = &cfp->next; |
| 1364 | Configtable_insert(cfp); |
| 1365 | } |
| 1366 | return cfp; |
| 1367 | } |
| 1368 | |
| 1369 | /* Add a basis configuration to the configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1370 | struct config *Configlist_addbasis(struct rule *rp, int dot) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1371 | { |
| 1372 | struct config *cfp, model; |
| 1373 | |
| 1374 | assert( basisend!=0 ); |
| 1375 | assert( currentend!=0 ); |
| 1376 | model.rp = rp; |
| 1377 | model.dot = dot; |
| 1378 | cfp = Configtable_find(&model); |
| 1379 | if( cfp==0 ){ |
| 1380 | cfp = newconfig(); |
| 1381 | cfp->rp = rp; |
| 1382 | cfp->dot = dot; |
| 1383 | cfp->fws = SetNew(); |
| 1384 | cfp->stp = 0; |
| 1385 | cfp->fplp = cfp->bplp = 0; |
| 1386 | cfp->next = 0; |
| 1387 | cfp->bp = 0; |
| 1388 | *currentend = cfp; |
| 1389 | currentend = &cfp->next; |
| 1390 | *basisend = cfp; |
| 1391 | basisend = &cfp->bp; |
| 1392 | Configtable_insert(cfp); |
| 1393 | } |
| 1394 | return cfp; |
| 1395 | } |
| 1396 | |
| 1397 | /* Compute the closure of the configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1398 | void Configlist_closure(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1399 | { |
| 1400 | struct config *cfp, *newcfp; |
| 1401 | struct rule *rp, *newrp; |
| 1402 | struct symbol *sp, *xsp; |
| 1403 | int i, dot; |
| 1404 | |
| 1405 | assert( currentend!=0 ); |
| 1406 | for(cfp=current; cfp; cfp=cfp->next){ |
| 1407 | rp = cfp->rp; |
| 1408 | dot = cfp->dot; |
| 1409 | if( dot>=rp->nrhs ) continue; |
| 1410 | sp = rp->rhs[dot]; |
| 1411 | if( sp->type==NONTERMINAL ){ |
| 1412 | if( sp->rule==0 && sp!=lemp->errsym ){ |
| 1413 | ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.", |
| 1414 | sp->name); |
| 1415 | lemp->errorcnt++; |
| 1416 | } |
| 1417 | for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){ |
| 1418 | newcfp = Configlist_add(newrp,0); |
| 1419 | for(i=dot+1; i<rp->nrhs; i++){ |
| 1420 | xsp = rp->rhs[i]; |
| 1421 | if( xsp->type==TERMINAL ){ |
| 1422 | SetAdd(newcfp->fws,xsp->index); |
| 1423 | break; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 1424 | }else if( xsp->type==MULTITERMINAL ){ |
| 1425 | int k; |
| 1426 | for(k=0; k<xsp->nsubsym; k++){ |
| 1427 | SetAdd(newcfp->fws, xsp->subsym[k]->index); |
| 1428 | } |
| 1429 | break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1430 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1431 | SetUnion(newcfp->fws,xsp->firstset); |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 1432 | if( xsp->lambda==LEMON_FALSE ) break; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1433 | } |
| 1434 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1435 | if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp); |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | return; |
| 1440 | } |
| 1441 | |
| 1442 | /* Sort the configuration list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1443 | void Configlist_sort(void){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 1444 | current = (struct config*)msort((char*)current,(char**)&(current->next), |
| 1445 | Configcmp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1446 | currentend = 0; |
| 1447 | return; |
| 1448 | } |
| 1449 | |
| 1450 | /* Sort the basis configuration list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1451 | void Configlist_sortbasis(void){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 1452 | basis = (struct config*)msort((char*)current,(char**)&(current->bp), |
| 1453 | Configcmp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1454 | basisend = 0; |
| 1455 | return; |
| 1456 | } |
| 1457 | |
| 1458 | /* Return a pointer to the head of the configuration list and |
| 1459 | ** reset the list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1460 | struct config *Configlist_return(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1461 | struct config *old; |
| 1462 | old = current; |
| 1463 | current = 0; |
| 1464 | currentend = 0; |
| 1465 | return old; |
| 1466 | } |
| 1467 | |
| 1468 | /* Return a pointer to the head of the configuration list and |
| 1469 | ** reset the list */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1470 | struct config *Configlist_basis(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1471 | struct config *old; |
| 1472 | old = basis; |
| 1473 | basis = 0; |
| 1474 | basisend = 0; |
| 1475 | return old; |
| 1476 | } |
| 1477 | |
| 1478 | /* Free all elements of the given configuration list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1479 | void Configlist_eat(struct config *cfp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1480 | { |
| 1481 | struct config *nextcfp; |
| 1482 | for(; cfp; cfp=nextcfp){ |
| 1483 | nextcfp = cfp->next; |
| 1484 | assert( cfp->fplp==0 ); |
| 1485 | assert( cfp->bplp==0 ); |
| 1486 | if( cfp->fws ) SetFree(cfp->fws); |
| 1487 | deleteconfig(cfp); |
| 1488 | } |
| 1489 | return; |
| 1490 | } |
| 1491 | /***************** From the file "error.c" *********************************/ |
| 1492 | /* |
| 1493 | ** Code for printing error message. |
| 1494 | */ |
| 1495 | |
drh | f9a2e7b | 2003-04-15 01:49:48 +0000 | [diff] [blame] | 1496 | void ErrorMsg(const char *filename, int lineno, const char *format, ...){ |
icculus | 15a2cec | 2010-02-16 16:07:28 +0000 | [diff] [blame] | 1497 | va_list ap; |
icculus | 1c11f74 | 2010-02-15 00:01:04 +0000 | [diff] [blame] | 1498 | fprintf(stderr, "%s:%d: ", filename, lineno); |
| 1499 | va_start(ap, format); |
| 1500 | vfprintf(stderr,format,ap); |
| 1501 | va_end(ap); |
| 1502 | fprintf(stderr, "\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1503 | } |
| 1504 | /**************** From the file "main.c" ************************************/ |
| 1505 | /* |
| 1506 | ** Main program file for the LEMON parser generator. |
| 1507 | */ |
| 1508 | |
| 1509 | /* Report an out-of-memory condition and abort. This function |
| 1510 | ** is used mostly by the "MemoryCheck" macro in struct.h |
| 1511 | */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 1512 | void memory_error(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1513 | fprintf(stderr,"Out of memory. Aborting...\n"); |
| 1514 | exit(1); |
| 1515 | } |
| 1516 | |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1517 | static int nDefine = 0; /* Number of -D options on the command line */ |
| 1518 | static char **azDefine = 0; /* Name of the -D macros */ |
| 1519 | |
| 1520 | /* This routine is called with the argument to each -D command-line option. |
| 1521 | ** Add the macro defined to the azDefine array. |
| 1522 | */ |
| 1523 | static void handle_D_option(char *z){ |
| 1524 | char **paz; |
| 1525 | nDefine++; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1526 | azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1527 | if( azDefine==0 ){ |
| 1528 | fprintf(stderr,"out of memory\n"); |
| 1529 | exit(1); |
| 1530 | } |
| 1531 | paz = &azDefine[nDefine-1]; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1532 | *paz = (char *) malloc( lemonStrlen(z)+1 ); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1533 | if( *paz==0 ){ |
| 1534 | fprintf(stderr,"out of memory\n"); |
| 1535 | exit(1); |
| 1536 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 1537 | lemon_strcpy(*paz, z); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1538 | for(z=*paz; *z && *z!='='; z++){} |
| 1539 | *z = 0; |
| 1540 | } |
| 1541 | |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 1542 | /* Rember the name of the output directory |
| 1543 | */ |
| 1544 | static char *outputDir = NULL; |
| 1545 | static void handle_d_option(char *z){ |
| 1546 | outputDir = (char *) malloc( lemonStrlen(z)+1 ); |
| 1547 | if( outputDir==0 ){ |
| 1548 | fprintf(stderr,"out of memory\n"); |
| 1549 | exit(1); |
| 1550 | } |
| 1551 | lemon_strcpy(outputDir, z); |
| 1552 | } |
| 1553 | |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 1554 | static char *user_templatename = NULL; |
| 1555 | static void handle_T_option(char *z){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1556 | user_templatename = (char *) malloc( lemonStrlen(z)+1 ); |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 1557 | if( user_templatename==0 ){ |
| 1558 | memory_error(); |
| 1559 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 1560 | lemon_strcpy(user_templatename, z); |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 1561 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1562 | |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 1563 | /* Merge together to lists of rules ordered by rule.iRule */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1564 | static struct rule *Rule_merge(struct rule *pA, struct rule *pB){ |
| 1565 | struct rule *pFirst = 0; |
| 1566 | struct rule **ppPrev = &pFirst; |
| 1567 | while( pA && pB ){ |
| 1568 | if( pA->iRule<pB->iRule ){ |
| 1569 | *ppPrev = pA; |
| 1570 | ppPrev = &pA->next; |
| 1571 | pA = pA->next; |
| 1572 | }else{ |
| 1573 | *ppPrev = pB; |
| 1574 | ppPrev = &pB->next; |
| 1575 | pB = pB->next; |
| 1576 | } |
| 1577 | } |
| 1578 | if( pA ){ |
| 1579 | *ppPrev = pA; |
| 1580 | }else{ |
| 1581 | *ppPrev = pB; |
| 1582 | } |
| 1583 | return pFirst; |
| 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | ** Sort a list of rules in order of increasing iRule value |
| 1588 | */ |
| 1589 | static struct rule *Rule_sort(struct rule *rp){ |
| 1590 | int i; |
| 1591 | struct rule *pNext; |
| 1592 | struct rule *x[32]; |
| 1593 | memset(x, 0, sizeof(x)); |
| 1594 | while( rp ){ |
| 1595 | pNext = rp->next; |
| 1596 | rp->next = 0; |
| 1597 | for(i=0; i<sizeof(x)/sizeof(x[0]) && x[i]; i++){ |
| 1598 | rp = Rule_merge(x[i], rp); |
| 1599 | x[i] = 0; |
| 1600 | } |
| 1601 | x[i] = rp; |
| 1602 | rp = pNext; |
| 1603 | } |
| 1604 | rp = 0; |
| 1605 | for(i=0; i<sizeof(x)/sizeof(x[0]); i++){ |
| 1606 | rp = Rule_merge(x[i], rp); |
| 1607 | } |
| 1608 | return rp; |
| 1609 | } |
| 1610 | |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1611 | /* forward reference */ |
| 1612 | static const char *minimum_size_type(int lwr, int upr, int *pnByte); |
| 1613 | |
| 1614 | /* Print a single line of the "Parser Stats" output |
| 1615 | */ |
| 1616 | static void stats_line(const char *zLabel, int iValue){ |
| 1617 | int nLabel = lemonStrlen(zLabel); |
| 1618 | printf(" %s%.*s %5d\n", zLabel, |
| 1619 | 35-nLabel, "................................", |
| 1620 | iValue); |
| 1621 | } |
| 1622 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1623 | /* The main program. Parse the command line and do it... */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1624 | int main(int argc, char **argv) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1625 | { |
| 1626 | static int version = 0; |
| 1627 | static int rpflag = 0; |
| 1628 | static int basisflag = 0; |
| 1629 | static int compress = 0; |
| 1630 | static int quiet = 0; |
| 1631 | static int statistics = 0; |
| 1632 | static int mhflag = 0; |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 1633 | static int nolinenosflag = 0; |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1634 | static int noResort = 0; |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 1635 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1636 | static struct s_options options[] = { |
| 1637 | {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."}, |
| 1638 | {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."}, |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 1639 | {OPT_FSTR, "d", (char*)&handle_d_option, "Output directory. Default '.'"}, |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1640 | {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1641 | {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1642 | {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1643 | {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"}, |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 1644 | {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."}, |
| 1645 | {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1646 | {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"}, |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 1647 | {OPT_FLAG, "p", (char*)&showPrecedenceConflict, |
| 1648 | "Show conflicts resolved by precedence rules"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1649 | {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."}, |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1650 | {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"}, |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1651 | {OPT_FLAG, "s", (char*)&statistics, |
| 1652 | "Print parser stats to standard output."}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1653 | {OPT_FLAG, "x", (char*)&version, "Print the version number."}, |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1654 | {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."}, |
| 1655 | {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"}, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1656 | {OPT_FLAG,0,0,0} |
| 1657 | }; |
| 1658 | int i; |
icculus | 42585cf | 2010-02-14 05:19:56 +0000 | [diff] [blame] | 1659 | int exitcode; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1660 | struct lemon lem; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1661 | struct rule *rp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1662 | |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 1663 | OptInit(argv,options,stderr); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1664 | if( version ){ |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1665 | printf("Lemon version 1.0\n"); |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 1666 | exit(0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1667 | } |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 1668 | if( OptNArgs()!=1 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1669 | fprintf(stderr,"Exactly one filename argument is required.\n"); |
| 1670 | exit(1); |
| 1671 | } |
drh | 954f6b4 | 2006-06-13 13:27:46 +0000 | [diff] [blame] | 1672 | memset(&lem, 0, sizeof(lem)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1673 | lem.errorcnt = 0; |
| 1674 | |
| 1675 | /* Initialize the machine */ |
| 1676 | Strsafe_init(); |
| 1677 | Symbol_init(); |
| 1678 | State_init(); |
| 1679 | lem.argv0 = argv[0]; |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 1680 | lem.filename = OptArg(0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1681 | lem.basisflag = basisflag; |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 1682 | lem.nolinenosflag = nolinenosflag; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1683 | Symbol_new("$"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1684 | |
| 1685 | /* Parse the input file */ |
| 1686 | Parse(&lem); |
| 1687 | if( lem.errorcnt ) exit(lem.errorcnt); |
drh | 954f6b4 | 2006-06-13 13:27:46 +0000 | [diff] [blame] | 1688 | if( lem.nrule==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1689 | fprintf(stderr,"Empty grammar.\n"); |
| 1690 | exit(1); |
| 1691 | } |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 1692 | lem.errsym = Symbol_find("error"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1693 | |
| 1694 | /* Count and index the symbols of the grammar */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1695 | Symbol_new("{default}"); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 1696 | lem.nsymbol = Symbol_count(); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1697 | lem.symbols = Symbol_arrayof(); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 1698 | for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i; |
| 1699 | qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp); |
| 1700 | for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i; |
| 1701 | while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; } |
| 1702 | assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 ); |
| 1703 | lem.nsymbol = i - 1; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 1704 | for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1705 | lem.nterminal = i; |
| 1706 | |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 1707 | /* Assign sequential rule numbers. Start with 0. Put rules that have no |
| 1708 | ** reduce action C-code associated with them last, so that the switch() |
| 1709 | ** statement that selects reduction actions will have a smaller jump table. |
| 1710 | */ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 1711 | for(i=0, rp=lem.rule; rp; rp=rp->next){ |
| 1712 | rp->iRule = rp->code ? i++ : -1; |
| 1713 | } |
| 1714 | for(rp=lem.rule; rp; rp=rp->next){ |
| 1715 | if( rp->iRule<0 ) rp->iRule = i++; |
| 1716 | } |
| 1717 | lem.startRule = lem.rule; |
| 1718 | lem.rule = Rule_sort(lem.rule); |
| 1719 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1720 | /* Generate a reprint of the grammar, if requested on the command line */ |
| 1721 | if( rpflag ){ |
| 1722 | Reprint(&lem); |
| 1723 | }else{ |
| 1724 | /* Initialize the size for all follow and first sets */ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 1725 | SetSize(lem.nterminal+1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1726 | |
| 1727 | /* Find the precedence for every production rule (that has one) */ |
| 1728 | FindRulePrecedences(&lem); |
| 1729 | |
| 1730 | /* Compute the lambda-nonterminals and the first-sets for every |
| 1731 | ** nonterminal */ |
| 1732 | FindFirstSets(&lem); |
| 1733 | |
| 1734 | /* Compute all LR(0) states. Also record follow-set propagation |
| 1735 | ** links so that the follow-set can be computed later */ |
| 1736 | lem.nstate = 0; |
| 1737 | FindStates(&lem); |
| 1738 | lem.sorted = State_arrayof(); |
| 1739 | |
| 1740 | /* Tie up loose ends on the propagation links */ |
| 1741 | FindLinks(&lem); |
| 1742 | |
| 1743 | /* Compute the follow set of every reducible configuration */ |
| 1744 | FindFollowSets(&lem); |
| 1745 | |
| 1746 | /* Compute the action tables */ |
| 1747 | FindActions(&lem); |
| 1748 | |
| 1749 | /* Compress the action tables */ |
| 1750 | if( compress==0 ) CompressTables(&lem); |
| 1751 | |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 1752 | /* Reorder and renumber the states so that states with fewer choices |
drh | dd7e9db | 2010-07-19 01:52:07 +0000 | [diff] [blame] | 1753 | ** occur at the end. This is an optimization that helps make the |
| 1754 | ** generated parser tables smaller. */ |
| 1755 | if( noResort==0 ) ResortStates(&lem); |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 1756 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1757 | /* Generate a report of the parser generated. (the "y.output" file) */ |
| 1758 | if( !quiet ) ReportOutput(&lem); |
| 1759 | |
| 1760 | /* Generate the source code for the parser */ |
| 1761 | ReportTable(&lem, mhflag); |
| 1762 | |
| 1763 | /* Produce a header file for use by the scanner. (This step is |
| 1764 | ** omitted if the "-m" option is used because makeheaders will |
| 1765 | ** generate the file for us.) */ |
| 1766 | if( !mhflag ) ReportHeader(&lem); |
| 1767 | } |
| 1768 | if( statistics ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1769 | printf("Parser statistics:\n"); |
| 1770 | stats_line("terminal symbols", lem.nterminal); |
| 1771 | stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal); |
| 1772 | stats_line("total symbols", lem.nsymbol); |
| 1773 | stats_line("rules", lem.nrule); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 1774 | stats_line("states", lem.nxstate); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1775 | stats_line("conflicts", lem.nconflict); |
| 1776 | stats_line("action table entries", lem.nactiontab); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 1777 | stats_line("lookahead table entries", lem.nlookaheadtab); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 1778 | stats_line("total table size (bytes)", lem.tablesize); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1779 | } |
icculus | 8e15802 | 2010-02-16 16:09:03 +0000 | [diff] [blame] | 1780 | if( lem.nconflict > 0 ){ |
| 1781 | fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict); |
icculus | 42585cf | 2010-02-14 05:19:56 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | /* return 0 on success, 1 on failure. */ |
icculus | 8e15802 | 2010-02-16 16:09:03 +0000 | [diff] [blame] | 1785 | exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0; |
icculus | 42585cf | 2010-02-14 05:19:56 +0000 | [diff] [blame] | 1786 | exit(exitcode); |
| 1787 | return (exitcode); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1788 | } |
| 1789 | /******************** From the file "msort.c" *******************************/ |
| 1790 | /* |
| 1791 | ** A generic merge-sort program. |
| 1792 | ** |
| 1793 | ** USAGE: |
| 1794 | ** Let "ptr" be a pointer to some structure which is at the head of |
| 1795 | ** a null-terminated list. Then to sort the list call: |
| 1796 | ** |
| 1797 | ** ptr = msort(ptr,&(ptr->next),cmpfnc); |
| 1798 | ** |
| 1799 | ** In the above, "cmpfnc" is a pointer to a function which compares |
| 1800 | ** two instances of the structure and returns an integer, as in |
| 1801 | ** strcmp. The second argument is a pointer to the pointer to the |
| 1802 | ** second element of the linked list. This address is used to compute |
| 1803 | ** the offset to the "next" field within the structure. The offset to |
| 1804 | ** the "next" field must be constant for all structures in the list. |
| 1805 | ** |
| 1806 | ** The function returns a new pointer which is the head of the list |
| 1807 | ** after sorting. |
| 1808 | ** |
| 1809 | ** ALGORITHM: |
| 1810 | ** Merge-sort. |
| 1811 | */ |
| 1812 | |
| 1813 | /* |
| 1814 | ** Return a pointer to the next structure in the linked list. |
| 1815 | */ |
drh | d25d692 | 2012-04-18 09:59:56 +0000 | [diff] [blame] | 1816 | #define NEXT(A) (*(char**)(((char*)A)+offset)) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1817 | |
| 1818 | /* |
| 1819 | ** Inputs: |
| 1820 | ** a: A sorted, null-terminated linked list. (May be null). |
| 1821 | ** b: A sorted, null-terminated linked list. (May be null). |
| 1822 | ** cmp: A pointer to the comparison function. |
| 1823 | ** offset: Offset in the structure to the "next" field. |
| 1824 | ** |
| 1825 | ** Return Value: |
| 1826 | ** A pointer to the head of a sorted list containing the elements |
| 1827 | ** of both a and b. |
| 1828 | ** |
| 1829 | ** Side effects: |
| 1830 | ** The "next" pointers for elements in the lists a and b are |
| 1831 | ** changed. |
| 1832 | */ |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 1833 | static char *merge( |
| 1834 | char *a, |
| 1835 | char *b, |
| 1836 | int (*cmp)(const char*,const char*), |
| 1837 | int offset |
| 1838 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1839 | char *ptr, *head; |
| 1840 | |
| 1841 | if( a==0 ){ |
| 1842 | head = b; |
| 1843 | }else if( b==0 ){ |
| 1844 | head = a; |
| 1845 | }else{ |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 1846 | if( (*cmp)(a,b)<=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1847 | ptr = a; |
| 1848 | a = NEXT(a); |
| 1849 | }else{ |
| 1850 | ptr = b; |
| 1851 | b = NEXT(b); |
| 1852 | } |
| 1853 | head = ptr; |
| 1854 | while( a && b ){ |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 1855 | if( (*cmp)(a,b)<=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1856 | NEXT(ptr) = a; |
| 1857 | ptr = a; |
| 1858 | a = NEXT(a); |
| 1859 | }else{ |
| 1860 | NEXT(ptr) = b; |
| 1861 | ptr = b; |
| 1862 | b = NEXT(b); |
| 1863 | } |
| 1864 | } |
| 1865 | if( a ) NEXT(ptr) = a; |
| 1866 | else NEXT(ptr) = b; |
| 1867 | } |
| 1868 | return head; |
| 1869 | } |
| 1870 | |
| 1871 | /* |
| 1872 | ** Inputs: |
| 1873 | ** list: Pointer to a singly-linked list of structures. |
| 1874 | ** next: Pointer to pointer to the second element of the list. |
| 1875 | ** cmp: A comparison function. |
| 1876 | ** |
| 1877 | ** Return Value: |
| 1878 | ** A pointer to the head of a sorted list containing the elements |
| 1879 | ** orginally in list. |
| 1880 | ** |
| 1881 | ** Side effects: |
| 1882 | ** The "next" pointers for elements in list are changed. |
| 1883 | */ |
| 1884 | #define LISTSIZE 30 |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 1885 | static char *msort( |
| 1886 | char *list, |
| 1887 | char **next, |
| 1888 | int (*cmp)(const char*,const char*) |
| 1889 | ){ |
drh | ba99af5 | 2001-10-25 20:37:16 +0000 | [diff] [blame] | 1890 | unsigned long offset; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1891 | char *ep; |
| 1892 | char *set[LISTSIZE]; |
| 1893 | int i; |
drh | 1cc0d11 | 2015-03-31 15:15:48 +0000 | [diff] [blame] | 1894 | offset = (unsigned long)((char*)next - (char*)list); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1895 | for(i=0; i<LISTSIZE; i++) set[i] = 0; |
| 1896 | while( list ){ |
| 1897 | ep = list; |
| 1898 | list = NEXT(list); |
| 1899 | NEXT(ep) = 0; |
| 1900 | for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){ |
| 1901 | ep = merge(ep,set[i],cmp,offset); |
| 1902 | set[i] = 0; |
| 1903 | } |
| 1904 | set[i] = ep; |
| 1905 | } |
| 1906 | ep = 0; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 1907 | 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] | 1908 | return ep; |
| 1909 | } |
| 1910 | /************************ From the file "option.c" **************************/ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1911 | static char **g_argv; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1912 | static struct s_options *op; |
| 1913 | static FILE *errstream; |
| 1914 | |
| 1915 | #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0) |
| 1916 | |
| 1917 | /* |
| 1918 | ** Print the command line with a carrot pointing to the k-th character |
| 1919 | ** of the n-th field. |
| 1920 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1921 | static void errline(int n, int k, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1922 | { |
| 1923 | int spcnt, i; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1924 | if( g_argv[0] ) fprintf(err,"%s",g_argv[0]); |
| 1925 | spcnt = lemonStrlen(g_argv[0]) + 1; |
| 1926 | for(i=1; i<n && g_argv[i]; i++){ |
| 1927 | fprintf(err," %s",g_argv[i]); |
| 1928 | spcnt += lemonStrlen(g_argv[i])+1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1929 | } |
| 1930 | spcnt += k; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1931 | for(; g_argv[i]; i++) fprintf(err," %s",g_argv[i]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1932 | if( spcnt<20 ){ |
| 1933 | fprintf(err,"\n%*s^-- here\n",spcnt,""); |
| 1934 | }else{ |
| 1935 | fprintf(err,"\n%*shere --^\n",spcnt-7,""); |
| 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | /* |
| 1940 | ** Return the index of the N-th non-switch argument. Return -1 |
| 1941 | ** if N is out of range. |
| 1942 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1943 | static int argindex(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1944 | { |
| 1945 | int i; |
| 1946 | int dashdash = 0; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1947 | if( g_argv!=0 && *g_argv!=0 ){ |
| 1948 | for(i=1; g_argv[i]; i++){ |
| 1949 | if( dashdash || !ISOPT(g_argv[i]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1950 | if( n==0 ) return i; |
| 1951 | n--; |
| 1952 | } |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1953 | if( strcmp(g_argv[i],"--")==0 ) dashdash = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | return -1; |
| 1957 | } |
| 1958 | |
| 1959 | static char emsg[] = "Command line syntax error: "; |
| 1960 | |
| 1961 | /* |
| 1962 | ** Process a flag command line argument. |
| 1963 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1964 | static int handleflags(int i, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1965 | { |
| 1966 | int v; |
| 1967 | int errcnt = 0; |
| 1968 | int j; |
| 1969 | for(j=0; op[j].label; j++){ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1970 | 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] | 1971 | } |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1972 | v = g_argv[i][0]=='-' ? 1 : 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1973 | if( op[j].label==0 ){ |
| 1974 | if( err ){ |
| 1975 | fprintf(err,"%sundefined option.\n",emsg); |
| 1976 | errline(i,1,err); |
| 1977 | } |
| 1978 | errcnt++; |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 1979 | }else if( op[j].arg==0 ){ |
| 1980 | /* Ignore this option */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1981 | }else if( op[j].type==OPT_FLAG ){ |
| 1982 | *((int*)op[j].arg) = v; |
| 1983 | }else if( op[j].type==OPT_FFLAG ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 1984 | (*(void(*)(int))(op[j].arg))(v); |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 1985 | }else if( op[j].type==OPT_FSTR ){ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 1986 | (*(void(*)(char *))(op[j].arg))(&g_argv[i][2]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1987 | }else{ |
| 1988 | if( err ){ |
| 1989 | fprintf(err,"%smissing argument on switch.\n",emsg); |
| 1990 | errline(i,1,err); |
| 1991 | } |
| 1992 | errcnt++; |
| 1993 | } |
| 1994 | return errcnt; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | ** Process a command line switch which has an argument. |
| 1999 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2000 | static int handleswitch(int i, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2001 | { |
| 2002 | int lv = 0; |
| 2003 | double dv = 0.0; |
| 2004 | char *sv = 0, *end; |
| 2005 | char *cp; |
| 2006 | int j; |
| 2007 | int errcnt = 0; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2008 | cp = strchr(g_argv[i],'='); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 2009 | assert( cp!=0 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2010 | *cp = 0; |
| 2011 | for(j=0; op[j].label; j++){ |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2012 | if( strcmp(g_argv[i],op[j].label)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2013 | } |
| 2014 | *cp = '='; |
| 2015 | if( op[j].label==0 ){ |
| 2016 | if( err ){ |
| 2017 | fprintf(err,"%sundefined option.\n",emsg); |
| 2018 | errline(i,0,err); |
| 2019 | } |
| 2020 | errcnt++; |
| 2021 | }else{ |
| 2022 | cp++; |
| 2023 | switch( op[j].type ){ |
| 2024 | case OPT_FLAG: |
| 2025 | case OPT_FFLAG: |
| 2026 | if( err ){ |
| 2027 | fprintf(err,"%soption requires an argument.\n",emsg); |
| 2028 | errline(i,0,err); |
| 2029 | } |
| 2030 | errcnt++; |
| 2031 | break; |
| 2032 | case OPT_DBL: |
| 2033 | case OPT_FDBL: |
| 2034 | dv = strtod(cp,&end); |
| 2035 | if( *end ){ |
| 2036 | if( err ){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 2037 | fprintf(err, |
| 2038 | "%sillegal character in floating-point argument.\n",emsg); |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2039 | errline(i,(int)((char*)end-(char*)g_argv[i]),err); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2040 | } |
| 2041 | errcnt++; |
| 2042 | } |
| 2043 | break; |
| 2044 | case OPT_INT: |
| 2045 | case OPT_FINT: |
| 2046 | lv = strtol(cp,&end,0); |
| 2047 | if( *end ){ |
| 2048 | if( err ){ |
| 2049 | fprintf(err,"%sillegal character in integer 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_STR: |
| 2056 | case OPT_FSTR: |
| 2057 | sv = cp; |
| 2058 | break; |
| 2059 | } |
| 2060 | switch( op[j].type ){ |
| 2061 | case OPT_FLAG: |
| 2062 | case OPT_FFLAG: |
| 2063 | break; |
| 2064 | case OPT_DBL: |
| 2065 | *(double*)(op[j].arg) = dv; |
| 2066 | break; |
| 2067 | case OPT_FDBL: |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2068 | (*(void(*)(double))(op[j].arg))(dv); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | case OPT_INT: |
| 2071 | *(int*)(op[j].arg) = lv; |
| 2072 | break; |
| 2073 | case OPT_FINT: |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2074 | (*(void(*)(int))(op[j].arg))((int)lv); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2075 | break; |
| 2076 | case OPT_STR: |
| 2077 | *(char**)(op[j].arg) = sv; |
| 2078 | break; |
| 2079 | case OPT_FSTR: |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2080 | (*(void(*)(char *))(op[j].arg))(sv); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2081 | break; |
| 2082 | } |
| 2083 | } |
| 2084 | return errcnt; |
| 2085 | } |
| 2086 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2087 | int OptInit(char **a, struct s_options *o, FILE *err) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2088 | { |
| 2089 | int errcnt = 0; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2090 | g_argv = a; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2091 | op = o; |
| 2092 | errstream = err; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2093 | if( g_argv && *g_argv && op ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2094 | int i; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2095 | for(i=1; g_argv[i]; i++){ |
| 2096 | if( g_argv[i][0]=='+' || g_argv[i][0]=='-' ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2097 | errcnt += handleflags(i,err); |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2098 | }else if( strchr(g_argv[i],'=') ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2099 | errcnt += handleswitch(i,err); |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | if( errcnt>0 ){ |
| 2104 | fprintf(err,"Valid command line options for \"%s\" are:\n",*a); |
drh | b0c8677 | 2000-06-02 23:21:26 +0000 | [diff] [blame] | 2105 | OptPrint(); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2106 | exit(1); |
| 2107 | } |
| 2108 | return 0; |
| 2109 | } |
| 2110 | |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 2111 | int OptNArgs(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2112 | int cnt = 0; |
| 2113 | int dashdash = 0; |
| 2114 | int i; |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2115 | if( g_argv!=0 && g_argv[0]!=0 ){ |
| 2116 | for(i=1; g_argv[i]; i++){ |
| 2117 | if( dashdash || !ISOPT(g_argv[i]) ) cnt++; |
| 2118 | if( strcmp(g_argv[i],"--")==0 ) dashdash = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | return cnt; |
| 2122 | } |
| 2123 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2124 | char *OptArg(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2125 | { |
| 2126 | int i; |
| 2127 | i = argindex(n); |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2128 | return i>=0 ? g_argv[i] : 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2131 | void OptErr(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2132 | { |
| 2133 | int i; |
| 2134 | i = argindex(n); |
| 2135 | if( i>=0 ) errline(i,0,errstream); |
| 2136 | } |
| 2137 | |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 2138 | void OptPrint(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2139 | int i; |
| 2140 | int max, len; |
| 2141 | max = 0; |
| 2142 | for(i=0; op[i].label; i++){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2143 | len = lemonStrlen(op[i].label) + 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2144 | switch( op[i].type ){ |
| 2145 | case OPT_FLAG: |
| 2146 | case OPT_FFLAG: |
| 2147 | break; |
| 2148 | case OPT_INT: |
| 2149 | case OPT_FINT: |
| 2150 | len += 9; /* length of "<integer>" */ |
| 2151 | break; |
| 2152 | case OPT_DBL: |
| 2153 | case OPT_FDBL: |
| 2154 | len += 6; /* length of "<real>" */ |
| 2155 | break; |
| 2156 | case OPT_STR: |
| 2157 | case OPT_FSTR: |
| 2158 | len += 8; /* length of "<string>" */ |
| 2159 | break; |
| 2160 | } |
| 2161 | if( len>max ) max = len; |
| 2162 | } |
| 2163 | for(i=0; op[i].label; i++){ |
| 2164 | switch( op[i].type ){ |
| 2165 | case OPT_FLAG: |
| 2166 | case OPT_FFLAG: |
| 2167 | fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message); |
| 2168 | break; |
| 2169 | case OPT_INT: |
| 2170 | case OPT_FINT: |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 2171 | fprintf(errstream," -%s<integer>%*s %s\n",op[i].label, |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2172 | (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2173 | break; |
| 2174 | case OPT_DBL: |
| 2175 | case OPT_FDBL: |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 2176 | fprintf(errstream," -%s<real>%*s %s\n",op[i].label, |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2177 | (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2178 | break; |
| 2179 | case OPT_STR: |
| 2180 | case OPT_FSTR: |
drh | 0325d39 | 2015-01-01 19:11:22 +0000 | [diff] [blame] | 2181 | fprintf(errstream," -%s<string>%*s %s\n",op[i].label, |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2182 | (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2183 | break; |
| 2184 | } |
| 2185 | } |
| 2186 | } |
| 2187 | /*********************** From the file "parse.c" ****************************/ |
| 2188 | /* |
| 2189 | ** Input file parser for the LEMON parser generator. |
| 2190 | */ |
| 2191 | |
| 2192 | /* The state of the parser */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2193 | enum e_state { |
| 2194 | INITIALIZE, |
| 2195 | WAITING_FOR_DECL_OR_RULE, |
| 2196 | WAITING_FOR_DECL_KEYWORD, |
| 2197 | WAITING_FOR_DECL_ARG, |
| 2198 | WAITING_FOR_PRECEDENCE_SYMBOL, |
| 2199 | WAITING_FOR_ARROW, |
| 2200 | IN_RHS, |
| 2201 | LHS_ALIAS_1, |
| 2202 | LHS_ALIAS_2, |
| 2203 | LHS_ALIAS_3, |
| 2204 | RHS_ALIAS_1, |
| 2205 | RHS_ALIAS_2, |
| 2206 | PRECEDENCE_MARK_1, |
| 2207 | PRECEDENCE_MARK_2, |
| 2208 | RESYNC_AFTER_RULE_ERROR, |
| 2209 | RESYNC_AFTER_DECL_ERROR, |
| 2210 | WAITING_FOR_DESTRUCTOR_SYMBOL, |
| 2211 | WAITING_FOR_DATATYPE_SYMBOL, |
| 2212 | WAITING_FOR_FALLBACK_ID, |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2213 | WAITING_FOR_WILDCARD_ID, |
| 2214 | WAITING_FOR_CLASS_ID, |
drh | 59c435a | 2017-08-02 03:21:11 +0000 | [diff] [blame] | 2215 | WAITING_FOR_CLASS_TOKEN, |
| 2216 | WAITING_FOR_TOKEN_NAME |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2217 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2218 | struct pstate { |
| 2219 | char *filename; /* Name of the input file */ |
| 2220 | int tokenlineno; /* Linenumber at which current token starts */ |
| 2221 | int errorcnt; /* Number of errors so far */ |
| 2222 | char *tokenstart; /* Text of current token */ |
| 2223 | struct lemon *gp; /* Global state vector */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2224 | enum e_state state; /* The state of the parser */ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2225 | struct symbol *fallback; /* The fallback token */ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2226 | struct symbol *tkclass; /* Token class symbol */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2227 | struct symbol *lhs; /* Left-hand side of current rule */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2228 | const char *lhsalias; /* Alias for the LHS */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2229 | int nrhs; /* Number of right-hand side symbols seen */ |
| 2230 | struct symbol *rhs[MAXRHS]; /* RHS symbols */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2231 | const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2232 | struct rule *prevrule; /* Previous rule parsed */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2233 | const char *declkeyword; /* Keyword of a declaration */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2234 | char **declargslot; /* Where the declaration argument should be put */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2235 | int insertLineMacro; /* Add #line before declaration insert */ |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 2236 | int *decllinenoslot; /* Where to write declaration line number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2237 | enum e_assoc declassoc; /* Assign this association to decl arguments */ |
| 2238 | int preccounter; /* Assign this precedence to decl arguments */ |
| 2239 | struct rule *firstrule; /* Pointer to first rule in the grammar */ |
| 2240 | struct rule *lastrule; /* Pointer to the most recently parsed rule */ |
| 2241 | }; |
| 2242 | |
| 2243 | /* Parse a single token */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2244 | static void parseonetoken(struct pstate *psp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2245 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2246 | const char *x; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2247 | x = Strsafe(psp->tokenstart); /* Save the token permanently */ |
| 2248 | #if 0 |
| 2249 | printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno, |
| 2250 | x,psp->state); |
| 2251 | #endif |
| 2252 | switch( psp->state ){ |
| 2253 | case INITIALIZE: |
| 2254 | psp->prevrule = 0; |
| 2255 | psp->preccounter = 0; |
| 2256 | psp->firstrule = psp->lastrule = 0; |
| 2257 | psp->gp->nrule = 0; |
| 2258 | /* Fall thru to next case */ |
| 2259 | case WAITING_FOR_DECL_OR_RULE: |
| 2260 | if( x[0]=='%' ){ |
| 2261 | psp->state = WAITING_FOR_DECL_KEYWORD; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2262 | }else if( ISLOWER(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2263 | psp->lhs = Symbol_new(x); |
| 2264 | psp->nrhs = 0; |
| 2265 | psp->lhsalias = 0; |
| 2266 | psp->state = WAITING_FOR_ARROW; |
| 2267 | }else if( x[0]=='{' ){ |
| 2268 | if( psp->prevrule==0 ){ |
| 2269 | ErrorMsg(psp->filename,psp->tokenlineno, |
drh | 3cb2f6e | 2012-01-09 14:19:05 +0000 | [diff] [blame] | 2270 | "There is no prior rule upon which to attach the code \ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2271 | fragment which begins on this line."); |
| 2272 | psp->errorcnt++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2273 | }else if( psp->prevrule->code!=0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2274 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2275 | "Code fragment beginning on this line is not the first \ |
| 2276 | to follow the previous rule."); |
| 2277 | psp->errorcnt++; |
| 2278 | }else{ |
| 2279 | psp->prevrule->line = psp->tokenlineno; |
| 2280 | psp->prevrule->code = &x[1]; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 2281 | psp->prevrule->noCode = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2282 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2283 | }else if( x[0]=='[' ){ |
| 2284 | psp->state = PRECEDENCE_MARK_1; |
| 2285 | }else{ |
| 2286 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2287 | "Token \"%s\" should be either \"%%\" or a nonterminal name.", |
| 2288 | x); |
| 2289 | psp->errorcnt++; |
| 2290 | } |
| 2291 | break; |
| 2292 | case PRECEDENCE_MARK_1: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2293 | if( !ISUPPER(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2294 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2295 | "The precedence symbol must be a terminal."); |
| 2296 | psp->errorcnt++; |
| 2297 | }else if( psp->prevrule==0 ){ |
| 2298 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2299 | "There is no prior rule to assign precedence \"[%s]\".",x); |
| 2300 | psp->errorcnt++; |
| 2301 | }else if( psp->prevrule->precsym!=0 ){ |
| 2302 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2303 | "Precedence mark on this line is not the first \ |
| 2304 | to follow the previous rule."); |
| 2305 | psp->errorcnt++; |
| 2306 | }else{ |
| 2307 | psp->prevrule->precsym = Symbol_new(x); |
| 2308 | } |
| 2309 | psp->state = PRECEDENCE_MARK_2; |
| 2310 | break; |
| 2311 | case PRECEDENCE_MARK_2: |
| 2312 | if( x[0]!=']' ){ |
| 2313 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2314 | "Missing \"]\" on precedence mark."); |
| 2315 | psp->errorcnt++; |
| 2316 | } |
| 2317 | psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2318 | break; |
| 2319 | case WAITING_FOR_ARROW: |
| 2320 | if( x[0]==':' && x[1]==':' && x[2]=='=' ){ |
| 2321 | psp->state = IN_RHS; |
| 2322 | }else if( x[0]=='(' ){ |
| 2323 | psp->state = LHS_ALIAS_1; |
| 2324 | }else{ |
| 2325 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2326 | "Expected to see a \":\" following the LHS symbol \"%s\".", |
| 2327 | psp->lhs->name); |
| 2328 | psp->errorcnt++; |
| 2329 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2330 | } |
| 2331 | break; |
| 2332 | case LHS_ALIAS_1: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2333 | if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2334 | psp->lhsalias = x; |
| 2335 | psp->state = LHS_ALIAS_2; |
| 2336 | }else{ |
| 2337 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2338 | "\"%s\" is not a valid alias for the LHS \"%s\"\n", |
| 2339 | x,psp->lhs->name); |
| 2340 | psp->errorcnt++; |
| 2341 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2342 | } |
| 2343 | break; |
| 2344 | case LHS_ALIAS_2: |
| 2345 | if( x[0]==')' ){ |
| 2346 | psp->state = LHS_ALIAS_3; |
| 2347 | }else{ |
| 2348 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2349 | "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias); |
| 2350 | psp->errorcnt++; |
| 2351 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2352 | } |
| 2353 | break; |
| 2354 | case LHS_ALIAS_3: |
| 2355 | if( x[0]==':' && x[1]==':' && x[2]=='=' ){ |
| 2356 | psp->state = IN_RHS; |
| 2357 | }else{ |
| 2358 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2359 | "Missing \"->\" following: \"%s(%s)\".", |
| 2360 | psp->lhs->name,psp->lhsalias); |
| 2361 | psp->errorcnt++; |
| 2362 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2363 | } |
| 2364 | break; |
| 2365 | case IN_RHS: |
| 2366 | if( x[0]=='.' ){ |
| 2367 | struct rule *rp; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 2368 | rp = (struct rule *)calloc( sizeof(struct rule) + |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 2369 | sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2370 | if( rp==0 ){ |
| 2371 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2372 | "Can't allocate enough memory for this rule."); |
| 2373 | psp->errorcnt++; |
| 2374 | psp->prevrule = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2375 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2376 | int i; |
| 2377 | rp->ruleline = psp->tokenlineno; |
| 2378 | rp->rhs = (struct symbol**)&rp[1]; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2379 | rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2380 | for(i=0; i<psp->nrhs; i++){ |
| 2381 | rp->rhs[i] = psp->rhs[i]; |
| 2382 | rp->rhsalias[i] = psp->alias[i]; |
drh | 539e741 | 2018-04-21 20:24:19 +0000 | [diff] [blame] | 2383 | if( rp->rhsalias[i]!=0 ){ rp->rhs[i]->bContent = 1; } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2384 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2385 | rp->lhs = psp->lhs; |
| 2386 | rp->lhsalias = psp->lhsalias; |
| 2387 | rp->nrhs = psp->nrhs; |
| 2388 | rp->code = 0; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 2389 | rp->noCode = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2390 | rp->precsym = 0; |
| 2391 | rp->index = psp->gp->nrule++; |
| 2392 | rp->nextlhs = rp->lhs->rule; |
| 2393 | rp->lhs->rule = rp; |
| 2394 | rp->next = 0; |
| 2395 | if( psp->firstrule==0 ){ |
| 2396 | psp->firstrule = psp->lastrule = rp; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2397 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2398 | psp->lastrule->next = rp; |
| 2399 | psp->lastrule = rp; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2400 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2401 | psp->prevrule = rp; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2402 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2403 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2404 | }else if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2405 | if( psp->nrhs>=MAXRHS ){ |
| 2406 | ErrorMsg(psp->filename,psp->tokenlineno, |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 2407 | "Too many symbols on RHS of rule beginning at \"%s\".", |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2408 | x); |
| 2409 | psp->errorcnt++; |
| 2410 | psp->state = RESYNC_AFTER_RULE_ERROR; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2411 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2412 | psp->rhs[psp->nrhs] = Symbol_new(x); |
| 2413 | psp->alias[psp->nrhs] = 0; |
| 2414 | psp->nrhs++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2415 | } |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2416 | }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){ |
| 2417 | struct symbol *msp = psp->rhs[psp->nrhs-1]; |
| 2418 | if( msp->type!=MULTITERMINAL ){ |
| 2419 | struct symbol *origsp = msp; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2420 | msp = (struct symbol *) calloc(1,sizeof(*msp)); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2421 | memset(msp, 0, sizeof(*msp)); |
| 2422 | msp->type = MULTITERMINAL; |
| 2423 | msp->nsubsym = 1; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2424 | msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*)); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2425 | msp->subsym[0] = origsp; |
| 2426 | msp->name = origsp->name; |
| 2427 | psp->rhs[psp->nrhs-1] = msp; |
| 2428 | } |
| 2429 | msp->nsubsym++; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2430 | msp->subsym = (struct symbol **) realloc(msp->subsym, |
| 2431 | sizeof(struct symbol*)*msp->nsubsym); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2432 | msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2433 | if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2434 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2435 | "Cannot form a compound containing a non-terminal"); |
| 2436 | psp->errorcnt++; |
| 2437 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2438 | }else if( x[0]=='(' && psp->nrhs>0 ){ |
| 2439 | psp->state = RHS_ALIAS_1; |
| 2440 | }else{ |
| 2441 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2442 | "Illegal character on RHS of rule: \"%s\".",x); |
| 2443 | psp->errorcnt++; |
| 2444 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2445 | } |
| 2446 | break; |
| 2447 | case RHS_ALIAS_1: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2448 | if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2449 | psp->alias[psp->nrhs-1] = x; |
| 2450 | psp->state = RHS_ALIAS_2; |
| 2451 | }else{ |
| 2452 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2453 | "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n", |
| 2454 | x,psp->rhs[psp->nrhs-1]->name); |
| 2455 | psp->errorcnt++; |
| 2456 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2457 | } |
| 2458 | break; |
| 2459 | case RHS_ALIAS_2: |
| 2460 | if( x[0]==')' ){ |
| 2461 | psp->state = IN_RHS; |
| 2462 | }else{ |
| 2463 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2464 | "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias); |
| 2465 | psp->errorcnt++; |
| 2466 | psp->state = RESYNC_AFTER_RULE_ERROR; |
| 2467 | } |
| 2468 | break; |
| 2469 | case WAITING_FOR_DECL_KEYWORD: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2470 | if( ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2471 | psp->declkeyword = x; |
| 2472 | psp->declargslot = 0; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 2473 | psp->decllinenoslot = 0; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2474 | psp->insertLineMacro = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2475 | psp->state = WAITING_FOR_DECL_ARG; |
| 2476 | if( strcmp(x,"name")==0 ){ |
| 2477 | psp->declargslot = &(psp->gp->name); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2478 | psp->insertLineMacro = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2479 | }else if( strcmp(x,"include")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2480 | psp->declargslot = &(psp->gp->include); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2481 | }else if( strcmp(x,"code")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2482 | psp->declargslot = &(psp->gp->extracode); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2483 | }else if( strcmp(x,"token_destructor")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2484 | psp->declargslot = &psp->gp->tokendest; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2485 | }else if( strcmp(x,"default_destructor")==0 ){ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2486 | psp->declargslot = &psp->gp->vardest; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2487 | }else if( strcmp(x,"token_prefix")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2488 | psp->declargslot = &psp->gp->tokenprefix; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2489 | psp->insertLineMacro = 0; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2490 | }else if( strcmp(x,"syntax_error")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2491 | psp->declargslot = &(psp->gp->error); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2492 | }else if( strcmp(x,"parse_accept")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2493 | psp->declargslot = &(psp->gp->accept); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2494 | }else if( strcmp(x,"parse_failure")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2495 | psp->declargslot = &(psp->gp->failure); |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2496 | }else if( strcmp(x,"stack_overflow")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2497 | psp->declargslot = &(psp->gp->overflow); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2498 | }else if( strcmp(x,"extra_argument")==0 ){ |
| 2499 | psp->declargslot = &(psp->gp->arg); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2500 | psp->insertLineMacro = 0; |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 2501 | }else if( strcmp(x,"extra_context")==0 ){ |
| 2502 | psp->declargslot = &(psp->gp->ctx); |
| 2503 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2504 | }else if( strcmp(x,"token_type")==0 ){ |
| 2505 | psp->declargslot = &(psp->gp->tokentype); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2506 | psp->insertLineMacro = 0; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2507 | }else if( strcmp(x,"default_type")==0 ){ |
| 2508 | psp->declargslot = &(psp->gp->vartype); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2509 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2510 | }else if( strcmp(x,"stack_size")==0 ){ |
| 2511 | psp->declargslot = &(psp->gp->stacksize); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2512 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2513 | }else if( strcmp(x,"start_symbol")==0 ){ |
| 2514 | psp->declargslot = &(psp->gp->start); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2515 | psp->insertLineMacro = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2516 | }else if( strcmp(x,"left")==0 ){ |
| 2517 | psp->preccounter++; |
| 2518 | psp->declassoc = LEFT; |
| 2519 | psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
| 2520 | }else if( strcmp(x,"right")==0 ){ |
| 2521 | psp->preccounter++; |
| 2522 | psp->declassoc = RIGHT; |
| 2523 | psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
| 2524 | }else if( strcmp(x,"nonassoc")==0 ){ |
| 2525 | psp->preccounter++; |
| 2526 | psp->declassoc = NONE; |
| 2527 | psp->state = WAITING_FOR_PRECEDENCE_SYMBOL; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2528 | }else if( strcmp(x,"destructor")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2529 | psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2530 | }else if( strcmp(x,"type")==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2531 | psp->state = WAITING_FOR_DATATYPE_SYMBOL; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2532 | }else if( strcmp(x,"fallback")==0 ){ |
| 2533 | psp->fallback = 0; |
| 2534 | psp->state = WAITING_FOR_FALLBACK_ID; |
drh | 59c435a | 2017-08-02 03:21:11 +0000 | [diff] [blame] | 2535 | }else if( strcmp(x,"token")==0 ){ |
| 2536 | psp->state = WAITING_FOR_TOKEN_NAME; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 2537 | }else if( strcmp(x,"wildcard")==0 ){ |
| 2538 | psp->state = WAITING_FOR_WILDCARD_ID; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2539 | }else if( strcmp(x,"token_class")==0 ){ |
| 2540 | psp->state = WAITING_FOR_CLASS_ID; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2541 | }else{ |
| 2542 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2543 | "Unknown declaration keyword: \"%%%s\".",x); |
| 2544 | psp->errorcnt++; |
| 2545 | psp->state = RESYNC_AFTER_DECL_ERROR; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2546 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2547 | }else{ |
| 2548 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2549 | "Illegal declaration keyword: \"%s\".",x); |
| 2550 | psp->errorcnt++; |
| 2551 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2552 | } |
| 2553 | break; |
| 2554 | case WAITING_FOR_DESTRUCTOR_SYMBOL: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2555 | if( !ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2556 | ErrorMsg(psp->filename,psp->tokenlineno, |
icculus | d0d97b0 | 2010-02-17 20:22:10 +0000 | [diff] [blame] | 2557 | "Symbol name missing after %%destructor keyword"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2558 | psp->errorcnt++; |
| 2559 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2560 | }else{ |
icculus | d286fa6 | 2010-03-03 17:06:32 +0000 | [diff] [blame] | 2561 | struct symbol *sp = Symbol_new(x); |
| 2562 | psp->declargslot = &sp->destructor; |
| 2563 | psp->decllinenoslot = &sp->destLineno; |
| 2564 | psp->insertLineMacro = 1; |
| 2565 | psp->state = WAITING_FOR_DECL_ARG; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2566 | } |
| 2567 | break; |
| 2568 | case WAITING_FOR_DATATYPE_SYMBOL: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2569 | if( !ISALPHA(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2570 | ErrorMsg(psp->filename,psp->tokenlineno, |
icculus | d0d97b0 | 2010-02-17 20:22:10 +0000 | [diff] [blame] | 2571 | "Symbol name missing after %%type keyword"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2572 | psp->errorcnt++; |
| 2573 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2574 | }else{ |
icculus | 866bf1e | 2010-02-17 20:31:32 +0000 | [diff] [blame] | 2575 | struct symbol *sp = Symbol_find(x); |
| 2576 | if((sp) && (sp->datatype)){ |
| 2577 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2578 | "Symbol %%type \"%s\" already defined", x); |
| 2579 | psp->errorcnt++; |
| 2580 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2581 | }else{ |
| 2582 | if (!sp){ |
| 2583 | sp = Symbol_new(x); |
| 2584 | } |
| 2585 | psp->declargslot = &sp->datatype; |
| 2586 | psp->insertLineMacro = 0; |
| 2587 | psp->state = WAITING_FOR_DECL_ARG; |
| 2588 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2589 | } |
| 2590 | break; |
| 2591 | case WAITING_FOR_PRECEDENCE_SYMBOL: |
| 2592 | if( x[0]=='.' ){ |
| 2593 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2594 | }else if( ISUPPER(x[0]) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2595 | struct symbol *sp; |
| 2596 | sp = Symbol_new(x); |
| 2597 | if( sp->prec>=0 ){ |
| 2598 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2599 | "Symbol \"%s\" has already be given a precedence.",x); |
| 2600 | psp->errorcnt++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2601 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2602 | sp->prec = psp->preccounter; |
| 2603 | sp->assoc = psp->declassoc; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2604 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2605 | }else{ |
| 2606 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2607 | "Can't assign a precedence to \"%s\".",x); |
| 2608 | psp->errorcnt++; |
| 2609 | } |
| 2610 | break; |
| 2611 | case WAITING_FOR_DECL_ARG: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2612 | if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2613 | const char *zOld, *zNew; |
| 2614 | char *zBuf, *z; |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 2615 | int nOld, n, nLine = 0, nNew, nBack; |
drh | b5bd49e | 2008-07-14 12:21:08 +0000 | [diff] [blame] | 2616 | int addLineMacro; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2617 | char zLine[50]; |
| 2618 | zNew = x; |
| 2619 | if( zNew[0]=='"' || zNew[0]=='{' ) zNew++; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2620 | nNew = lemonStrlen(zNew); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2621 | if( *psp->declargslot ){ |
| 2622 | zOld = *psp->declargslot; |
| 2623 | }else{ |
| 2624 | zOld = ""; |
| 2625 | } |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2626 | nOld = lemonStrlen(zOld); |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2627 | n = nOld + nNew + 20; |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 2628 | addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro && |
drh | b5bd49e | 2008-07-14 12:21:08 +0000 | [diff] [blame] | 2629 | (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0); |
| 2630 | if( addLineMacro ){ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2631 | for(z=psp->filename, nBack=0; *z; z++){ |
| 2632 | if( *z=='\\' ) nBack++; |
| 2633 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 2634 | lemon_sprintf(zLine, "#line %d ", psp->tokenlineno); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2635 | nLine = lemonStrlen(zLine); |
| 2636 | n += nLine + lemonStrlen(psp->filename) + nBack; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2637 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2638 | *psp->declargslot = (char *) realloc(*psp->declargslot, n); |
| 2639 | zBuf = *psp->declargslot + nOld; |
drh | b5bd49e | 2008-07-14 12:21:08 +0000 | [diff] [blame] | 2640 | if( addLineMacro ){ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2641 | if( nOld && zBuf[-1]!='\n' ){ |
| 2642 | *(zBuf++) = '\n'; |
| 2643 | } |
| 2644 | memcpy(zBuf, zLine, nLine); |
| 2645 | zBuf += nLine; |
| 2646 | *(zBuf++) = '"'; |
| 2647 | for(z=psp->filename; *z; z++){ |
| 2648 | if( *z=='\\' ){ |
| 2649 | *(zBuf++) = '\\'; |
| 2650 | } |
| 2651 | *(zBuf++) = *z; |
| 2652 | } |
| 2653 | *(zBuf++) = '"'; |
| 2654 | *(zBuf++) = '\n'; |
| 2655 | } |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 2656 | if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){ |
| 2657 | psp->decllinenoslot[0] = psp->tokenlineno; |
| 2658 | } |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 2659 | memcpy(zBuf, zNew, nNew); |
| 2660 | zBuf += nNew; |
| 2661 | *zBuf = 0; |
| 2662 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2663 | }else{ |
| 2664 | ErrorMsg(psp->filename,psp->tokenlineno, |
| 2665 | "Illegal argument to %%%s: %s",psp->declkeyword,x); |
| 2666 | psp->errorcnt++; |
| 2667 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2668 | } |
| 2669 | break; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2670 | case WAITING_FOR_FALLBACK_ID: |
| 2671 | if( x[0]=='.' ){ |
| 2672 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2673 | }else if( !ISUPPER(x[0]) ){ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 2674 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2675 | "%%fallback argument \"%s\" should be a token", x); |
| 2676 | psp->errorcnt++; |
| 2677 | }else{ |
| 2678 | struct symbol *sp = Symbol_new(x); |
| 2679 | if( psp->fallback==0 ){ |
| 2680 | psp->fallback = sp; |
| 2681 | }else if( sp->fallback ){ |
| 2682 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2683 | "More than one fallback assigned to token %s", x); |
| 2684 | psp->errorcnt++; |
| 2685 | }else{ |
| 2686 | sp->fallback = psp->fallback; |
| 2687 | psp->gp->has_fallback = 1; |
| 2688 | } |
| 2689 | } |
| 2690 | break; |
drh | 59c435a | 2017-08-02 03:21:11 +0000 | [diff] [blame] | 2691 | case WAITING_FOR_TOKEN_NAME: |
| 2692 | /* Tokens do not have to be declared before use. But they can be |
| 2693 | ** in order to control their assigned integer number. The number for |
| 2694 | ** each token is assigned when it is first seen. So by including |
| 2695 | ** |
| 2696 | ** %token ONE TWO THREE |
| 2697 | ** |
| 2698 | ** early in the grammar file, that assigns small consecutive values |
| 2699 | ** to each of the tokens ONE TWO and THREE. |
| 2700 | */ |
| 2701 | if( x[0]=='.' ){ |
| 2702 | psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2703 | }else if( !ISUPPER(x[0]) ){ |
| 2704 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2705 | "%%token argument \"%s\" should be a token", x); |
| 2706 | psp->errorcnt++; |
| 2707 | }else{ |
| 2708 | (void)Symbol_new(x); |
| 2709 | } |
| 2710 | break; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 2711 | case WAITING_FOR_WILDCARD_ID: |
| 2712 | if( x[0]=='.' ){ |
| 2713 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2714 | }else if( !ISUPPER(x[0]) ){ |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 2715 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2716 | "%%wildcard argument \"%s\" should be a token", x); |
| 2717 | psp->errorcnt++; |
| 2718 | }else{ |
| 2719 | struct symbol *sp = Symbol_new(x); |
| 2720 | if( psp->gp->wildcard==0 ){ |
| 2721 | psp->gp->wildcard = sp; |
| 2722 | }else{ |
| 2723 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2724 | "Extra wildcard to token: %s", x); |
| 2725 | psp->errorcnt++; |
| 2726 | } |
| 2727 | } |
| 2728 | break; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2729 | case WAITING_FOR_CLASS_ID: |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2730 | if( !ISLOWER(x[0]) ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2731 | ErrorMsg(psp->filename, psp->tokenlineno, |
mistachkin | d9bc6e8 | 2019-05-10 16:16:19 +0000 | [diff] [blame] | 2732 | "%%token_class must be followed by an identifier: %s", x); |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2733 | psp->errorcnt++; |
| 2734 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2735 | }else if( Symbol_find(x) ){ |
| 2736 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2737 | "Symbol \"%s\" already used", x); |
| 2738 | psp->errorcnt++; |
| 2739 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2740 | }else{ |
| 2741 | psp->tkclass = Symbol_new(x); |
| 2742 | psp->tkclass->type = MULTITERMINAL; |
| 2743 | psp->state = WAITING_FOR_CLASS_TOKEN; |
| 2744 | } |
| 2745 | break; |
| 2746 | case WAITING_FOR_CLASS_TOKEN: |
| 2747 | if( x[0]=='.' ){ |
| 2748 | psp->state = WAITING_FOR_DECL_OR_RULE; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2749 | }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2750 | struct symbol *msp = psp->tkclass; |
| 2751 | msp->nsubsym++; |
| 2752 | msp->subsym = (struct symbol **) realloc(msp->subsym, |
| 2753 | sizeof(struct symbol*)*msp->nsubsym); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2754 | if( !ISUPPER(x[0]) ) x++; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 2755 | msp->subsym[msp->nsubsym-1] = Symbol_new(x); |
| 2756 | }else{ |
| 2757 | ErrorMsg(psp->filename, psp->tokenlineno, |
| 2758 | "%%token_class argument \"%s\" should be a token", x); |
| 2759 | psp->errorcnt++; |
| 2760 | psp->state = RESYNC_AFTER_DECL_ERROR; |
| 2761 | } |
| 2762 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2763 | case RESYNC_AFTER_RULE_ERROR: |
| 2764 | /* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2765 | ** break; */ |
| 2766 | case RESYNC_AFTER_DECL_ERROR: |
| 2767 | if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE; |
| 2768 | if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD; |
| 2769 | break; |
| 2770 | } |
| 2771 | } |
| 2772 | |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 2773 | /* Run the preprocessor over the input file text. The global variables |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2774 | ** azDefine[0] through azDefine[nDefine-1] contains the names of all defined |
| 2775 | ** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and |
| 2776 | ** comments them out. Text in between is also commented out as appropriate. |
| 2777 | */ |
danielk1977 | 940fac9 | 2005-01-23 22:41:37 +0000 | [diff] [blame] | 2778 | static void preprocess_input(char *z){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2779 | int i, j, k, n; |
| 2780 | int exclude = 0; |
rse | 38514a9 | 2007-09-20 11:34:17 +0000 | [diff] [blame] | 2781 | int start = 0; |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2782 | int lineno = 1; |
rse | 38514a9 | 2007-09-20 11:34:17 +0000 | [diff] [blame] | 2783 | int start_lineno = 1; |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2784 | for(i=0; z[i]; i++){ |
| 2785 | if( z[i]=='\n' ) lineno++; |
| 2786 | if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2787 | if( strncmp(&z[i],"%endif",6)==0 && ISSPACE(z[i+6]) ){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2788 | if( exclude ){ |
| 2789 | exclude--; |
| 2790 | if( exclude==0 ){ |
| 2791 | for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' '; |
| 2792 | } |
| 2793 | } |
| 2794 | for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' '; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2795 | }else if( (strncmp(&z[i],"%ifdef",6)==0 && ISSPACE(z[i+6])) |
| 2796 | || (strncmp(&z[i],"%ifndef",7)==0 && ISSPACE(z[i+7])) ){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2797 | if( exclude ){ |
| 2798 | exclude++; |
| 2799 | }else{ |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2800 | for(j=i+7; ISSPACE(z[j]); j++){} |
| 2801 | for(n=0; z[j+n] && !ISSPACE(z[j+n]); n++){} |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2802 | exclude = 1; |
| 2803 | for(k=0; k<nDefine; k++){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 2804 | if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2805 | exclude = 0; |
| 2806 | break; |
| 2807 | } |
| 2808 | } |
| 2809 | if( z[i+3]=='n' ) exclude = !exclude; |
| 2810 | if( exclude ){ |
| 2811 | start = i; |
| 2812 | start_lineno = lineno; |
| 2813 | } |
| 2814 | } |
| 2815 | for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' '; |
| 2816 | } |
| 2817 | } |
| 2818 | if( exclude ){ |
| 2819 | fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno); |
| 2820 | exit(1); |
| 2821 | } |
| 2822 | } |
| 2823 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2824 | /* In spite of its name, this function is really a scanner. It read |
| 2825 | ** in the entire input file (all at once) then tokenizes it. Each |
| 2826 | ** token is passed to the function "parseonetoken" which builds all |
| 2827 | ** the appropriate data structures in the global state vector "gp". |
| 2828 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2829 | void Parse(struct lemon *gp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2830 | { |
| 2831 | struct pstate ps; |
| 2832 | FILE *fp; |
| 2833 | char *filebuf; |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 2834 | unsigned int filesize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2835 | int lineno; |
| 2836 | int c; |
| 2837 | char *cp, *nextcp; |
| 2838 | int startline = 0; |
| 2839 | |
rse | 38514a9 | 2007-09-20 11:34:17 +0000 | [diff] [blame] | 2840 | memset(&ps, '\0', sizeof(ps)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2841 | ps.gp = gp; |
| 2842 | ps.filename = gp->filename; |
| 2843 | ps.errorcnt = 0; |
| 2844 | ps.state = INITIALIZE; |
| 2845 | |
| 2846 | /* Begin by reading the input file */ |
| 2847 | fp = fopen(ps.filename,"rb"); |
| 2848 | if( fp==0 ){ |
| 2849 | ErrorMsg(ps.filename,0,"Can't open this file for reading."); |
| 2850 | gp->errorcnt++; |
| 2851 | return; |
| 2852 | } |
| 2853 | fseek(fp,0,2); |
| 2854 | filesize = ftell(fp); |
| 2855 | rewind(fp); |
| 2856 | filebuf = (char *)malloc( filesize+1 ); |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 2857 | if( filesize>100000000 || filebuf==0 ){ |
| 2858 | ErrorMsg(ps.filename,0,"Input file too large."); |
mistachkin | c93c614 | 2018-09-08 16:55:18 +0000 | [diff] [blame] | 2859 | free(filebuf); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2860 | gp->errorcnt++; |
drh | e0a59cf | 2011-08-30 00:58:58 +0000 | [diff] [blame] | 2861 | fclose(fp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2862 | return; |
| 2863 | } |
| 2864 | if( fread(filebuf,1,filesize,fp)!=filesize ){ |
| 2865 | ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.", |
| 2866 | filesize); |
| 2867 | free(filebuf); |
| 2868 | gp->errorcnt++; |
drh | e0a59cf | 2011-08-30 00:58:58 +0000 | [diff] [blame] | 2869 | fclose(fp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2870 | return; |
| 2871 | } |
| 2872 | fclose(fp); |
| 2873 | filebuf[filesize] = 0; |
| 2874 | |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 2875 | /* Make an initial pass through the file to handle %ifdef and %ifndef */ |
| 2876 | preprocess_input(filebuf); |
| 2877 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2878 | /* Now scan the text of the input file */ |
| 2879 | lineno = 1; |
| 2880 | for(cp=filebuf; (c= *cp)!=0; ){ |
| 2881 | if( c=='\n' ) lineno++; /* Keep track of the line number */ |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2882 | if( ISSPACE(c) ){ cp++; continue; } /* Skip all white space */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2883 | if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */ |
| 2884 | cp+=2; |
| 2885 | while( (c= *cp)!=0 && c!='\n' ) cp++; |
| 2886 | continue; |
| 2887 | } |
| 2888 | if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */ |
| 2889 | cp+=2; |
| 2890 | while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){ |
| 2891 | if( c=='\n' ) lineno++; |
| 2892 | cp++; |
| 2893 | } |
| 2894 | if( c ) cp++; |
| 2895 | continue; |
| 2896 | } |
| 2897 | ps.tokenstart = cp; /* Mark the beginning of the token */ |
| 2898 | ps.tokenlineno = lineno; /* Linenumber on which token begins */ |
| 2899 | if( c=='\"' ){ /* String literals */ |
| 2900 | cp++; |
| 2901 | while( (c= *cp)!=0 && c!='\"' ){ |
| 2902 | if( c=='\n' ) lineno++; |
| 2903 | cp++; |
| 2904 | } |
| 2905 | if( c==0 ){ |
| 2906 | ErrorMsg(ps.filename,startline, |
| 2907 | "String starting on this line is not terminated before the end of the file."); |
| 2908 | ps.errorcnt++; |
| 2909 | nextcp = cp; |
| 2910 | }else{ |
| 2911 | nextcp = cp+1; |
| 2912 | } |
| 2913 | }else if( c=='{' ){ /* A block of C code */ |
| 2914 | int level; |
| 2915 | cp++; |
| 2916 | for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){ |
| 2917 | if( c=='\n' ) lineno++; |
| 2918 | else if( c=='{' ) level++; |
| 2919 | else if( c=='}' ) level--; |
| 2920 | else if( c=='/' && cp[1]=='*' ){ /* Skip comments */ |
| 2921 | int prevc; |
| 2922 | cp = &cp[2]; |
| 2923 | prevc = 0; |
| 2924 | while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){ |
| 2925 | if( c=='\n' ) lineno++; |
| 2926 | prevc = c; |
| 2927 | cp++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2928 | } |
| 2929 | }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2930 | cp = &cp[2]; |
| 2931 | while( (c= *cp)!=0 && c!='\n' ) cp++; |
| 2932 | if( c ) lineno++; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2933 | }else if( c=='\'' || c=='\"' ){ /* String a character literals */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2934 | int startchar, prevc; |
| 2935 | startchar = c; |
| 2936 | prevc = 0; |
| 2937 | for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){ |
| 2938 | if( c=='\n' ) lineno++; |
| 2939 | if( prevc=='\\' ) prevc = 0; |
| 2940 | else prevc = c; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2941 | } |
| 2942 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2943 | } |
| 2944 | if( c==0 ){ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 2945 | ErrorMsg(ps.filename,ps.tokenlineno, |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2946 | "C code starting on this line is not terminated before the end of the file."); |
| 2947 | ps.errorcnt++; |
| 2948 | nextcp = cp; |
| 2949 | }else{ |
| 2950 | nextcp = cp+1; |
| 2951 | } |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2952 | }else if( ISALNUM(c) ){ /* Identifiers */ |
| 2953 | while( (c= *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2954 | nextcp = cp; |
| 2955 | }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */ |
| 2956 | cp += 3; |
| 2957 | nextcp = cp; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2958 | }else if( (c=='/' || c=='|') && ISALPHA(cp[1]) ){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2959 | cp += 2; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 2960 | while( (c = *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 2961 | nextcp = cp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2962 | }else{ /* All other (one character) operators */ |
| 2963 | cp++; |
| 2964 | nextcp = cp; |
| 2965 | } |
| 2966 | c = *cp; |
| 2967 | *cp = 0; /* Null terminate the token */ |
| 2968 | parseonetoken(&ps); /* Parse the token */ |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 2969 | *cp = (char)c; /* Restore the buffer */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2970 | cp = nextcp; |
| 2971 | } |
| 2972 | free(filebuf); /* Release the buffer after parsing */ |
| 2973 | gp->rule = ps.firstrule; |
| 2974 | gp->errorcnt = ps.errorcnt; |
| 2975 | } |
| 2976 | /*************************** From the file "plink.c" *********************/ |
| 2977 | /* |
| 2978 | ** Routines processing configuration follow-set propagation links |
| 2979 | ** in the LEMON parser generator. |
| 2980 | */ |
| 2981 | static struct plink *plink_freelist = 0; |
| 2982 | |
| 2983 | /* Allocate a new plink */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 2984 | struct plink *Plink_new(void){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2985 | struct plink *newlink; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2986 | |
| 2987 | if( plink_freelist==0 ){ |
| 2988 | int i; |
| 2989 | int amt = 100; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 2990 | plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2991 | if( plink_freelist==0 ){ |
| 2992 | fprintf(stderr, |
| 2993 | "Unable to allocate memory for a new follow-set propagation link.\n"); |
| 2994 | exit(1); |
| 2995 | } |
| 2996 | for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1]; |
| 2997 | plink_freelist[amt-1].next = 0; |
| 2998 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 2999 | newlink = plink_freelist; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3000 | plink_freelist = plink_freelist->next; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3001 | return newlink; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | /* Add a plink to a plink list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3005 | void Plink_add(struct plink **plpp, struct config *cfp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3006 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3007 | struct plink *newlink; |
| 3008 | newlink = Plink_new(); |
| 3009 | newlink->next = *plpp; |
| 3010 | *plpp = newlink; |
| 3011 | newlink->cfp = cfp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | /* Transfer every plink on the list "from" to the list "to" */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3015 | void Plink_copy(struct plink **to, struct plink *from) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3016 | { |
| 3017 | struct plink *nextpl; |
| 3018 | while( from ){ |
| 3019 | nextpl = from->next; |
| 3020 | from->next = *to; |
| 3021 | *to = from; |
| 3022 | from = nextpl; |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | /* Delete every plink on the list */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3027 | void Plink_delete(struct plink *plp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3028 | { |
| 3029 | struct plink *nextpl; |
| 3030 | |
| 3031 | while( plp ){ |
| 3032 | nextpl = plp->next; |
| 3033 | plp->next = plink_freelist; |
| 3034 | plink_freelist = plp; |
| 3035 | plp = nextpl; |
| 3036 | } |
| 3037 | } |
| 3038 | /*********************** From the file "report.c" **************************/ |
| 3039 | /* |
| 3040 | ** Procedures for generating reports and tables in the LEMON parser generator. |
| 3041 | */ |
| 3042 | |
| 3043 | /* Generate a filename with the given suffix. Space to hold the |
| 3044 | ** name comes from malloc() and must be freed by the calling |
| 3045 | ** function. |
| 3046 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3047 | PRIVATE char *file_makename(struct lemon *lemp, const char *suffix) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3048 | { |
| 3049 | char *name; |
| 3050 | char *cp; |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 3051 | char *filename = lemp->filename; |
| 3052 | int sz; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3053 | |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 3054 | if( outputDir ){ |
| 3055 | cp = strrchr(filename, '/'); |
| 3056 | if( cp ) filename = cp + 1; |
| 3057 | } |
| 3058 | sz = lemonStrlen(filename); |
| 3059 | sz += lemonStrlen(suffix); |
| 3060 | if( outputDir ) sz += lemonStrlen(outputDir) + 1; |
| 3061 | sz += 5; |
| 3062 | name = (char*)malloc( sz ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3063 | if( name==0 ){ |
| 3064 | fprintf(stderr,"Can't allocate space for a filename.\n"); |
| 3065 | exit(1); |
| 3066 | } |
drh | 9f88e6d | 2018-04-20 20:47:49 +0000 | [diff] [blame] | 3067 | name[0] = 0; |
| 3068 | if( outputDir ){ |
| 3069 | lemon_strcpy(name, outputDir); |
| 3070 | lemon_strcat(name, "/"); |
| 3071 | } |
| 3072 | lemon_strcat(name,filename); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3073 | cp = strrchr(name,'.'); |
| 3074 | if( cp ) *cp = 0; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3075 | lemon_strcat(name,suffix); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3076 | return name; |
| 3077 | } |
| 3078 | |
| 3079 | /* Open a file with a name based on the name of the input file, |
| 3080 | ** but with a different (specified) suffix, and return a pointer |
| 3081 | ** to the stream */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3082 | PRIVATE FILE *file_open( |
| 3083 | struct lemon *lemp, |
| 3084 | const char *suffix, |
| 3085 | const char *mode |
| 3086 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3087 | FILE *fp; |
| 3088 | |
| 3089 | if( lemp->outname ) free(lemp->outname); |
| 3090 | lemp->outname = file_makename(lemp, suffix); |
| 3091 | fp = fopen(lemp->outname,mode); |
| 3092 | if( fp==0 && *mode=='w' ){ |
| 3093 | fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname); |
| 3094 | lemp->errorcnt++; |
| 3095 | return 0; |
| 3096 | } |
| 3097 | return fp; |
| 3098 | } |
| 3099 | |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3100 | /* Print the text of a rule |
| 3101 | */ |
| 3102 | void rule_print(FILE *out, struct rule *rp){ |
| 3103 | int i, j; |
| 3104 | fprintf(out, "%s",rp->lhs->name); |
| 3105 | /* if( rp->lhsalias ) fprintf(out,"(%s)",rp->lhsalias); */ |
| 3106 | fprintf(out," ::="); |
| 3107 | for(i=0; i<rp->nrhs; i++){ |
| 3108 | struct symbol *sp = rp->rhs[i]; |
| 3109 | if( sp->type==MULTITERMINAL ){ |
| 3110 | fprintf(out," %s", sp->subsym[0]->name); |
| 3111 | for(j=1; j<sp->nsubsym; j++){ |
| 3112 | fprintf(out,"|%s", sp->subsym[j]->name); |
| 3113 | } |
| 3114 | }else{ |
| 3115 | fprintf(out," %s", sp->name); |
| 3116 | } |
| 3117 | /* if( rp->rhsalias[i] ) fprintf(out,"(%s)",rp->rhsalias[i]); */ |
| 3118 | } |
| 3119 | } |
| 3120 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3121 | /* Duplicate the input file without comments and without actions |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3122 | ** on rules */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3123 | void Reprint(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3124 | { |
| 3125 | struct rule *rp; |
| 3126 | struct symbol *sp; |
| 3127 | int i, j, maxlen, len, ncolumns, skip; |
| 3128 | printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename); |
| 3129 | maxlen = 10; |
| 3130 | for(i=0; i<lemp->nsymbol; i++){ |
| 3131 | sp = lemp->symbols[i]; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3132 | len = lemonStrlen(sp->name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3133 | if( len>maxlen ) maxlen = len; |
| 3134 | } |
| 3135 | ncolumns = 76/(maxlen+5); |
| 3136 | if( ncolumns<1 ) ncolumns = 1; |
| 3137 | skip = (lemp->nsymbol + ncolumns - 1)/ncolumns; |
| 3138 | for(i=0; i<skip; i++){ |
| 3139 | printf("//"); |
| 3140 | for(j=i; j<lemp->nsymbol; j+=skip){ |
| 3141 | sp = lemp->symbols[j]; |
| 3142 | assert( sp->index==j ); |
| 3143 | printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name); |
| 3144 | } |
| 3145 | printf("\n"); |
| 3146 | } |
| 3147 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3148 | rule_print(stdout, rp); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3149 | printf("."); |
| 3150 | if( rp->precsym ) printf(" [%s]",rp->precsym->name); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3151 | /* if( rp->code ) printf("\n %s",rp->code); */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3152 | printf("\n"); |
| 3153 | } |
| 3154 | } |
| 3155 | |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3156 | /* Print a single rule. |
| 3157 | */ |
| 3158 | void RulePrint(FILE *fp, struct rule *rp, int iCursor){ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3159 | struct symbol *sp; |
| 3160 | int i, j; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3161 | fprintf(fp,"%s ::=",rp->lhs->name); |
| 3162 | for(i=0; i<=rp->nrhs; i++){ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3163 | if( i==iCursor ) fprintf(fp," *"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3164 | if( i==rp->nrhs ) break; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3165 | sp = rp->rhs[i]; |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3166 | if( sp->type==MULTITERMINAL ){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 3167 | fprintf(fp," %s", sp->subsym[0]->name); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3168 | for(j=1; j<sp->nsubsym; j++){ |
| 3169 | fprintf(fp,"|%s",sp->subsym[j]->name); |
| 3170 | } |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 3171 | }else{ |
| 3172 | fprintf(fp," %s", sp->name); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3173 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3174 | } |
| 3175 | } |
| 3176 | |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3177 | /* Print the rule for a configuration. |
| 3178 | */ |
| 3179 | void ConfigPrint(FILE *fp, struct config *cfp){ |
| 3180 | RulePrint(fp, cfp->rp, cfp->dot); |
| 3181 | } |
| 3182 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3183 | /* #define TEST */ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3184 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3185 | /* Print a set */ |
| 3186 | PRIVATE void SetPrint(out,set,lemp) |
| 3187 | FILE *out; |
| 3188 | char *set; |
| 3189 | struct lemon *lemp; |
| 3190 | { |
| 3191 | int i; |
| 3192 | char *spacer; |
| 3193 | spacer = ""; |
| 3194 | fprintf(out,"%12s[",""); |
| 3195 | for(i=0; i<lemp->nterminal; i++){ |
| 3196 | if( SetFind(set,i) ){ |
| 3197 | fprintf(out,"%s%s",spacer,lemp->symbols[i]->name); |
| 3198 | spacer = " "; |
| 3199 | } |
| 3200 | } |
| 3201 | fprintf(out,"]\n"); |
| 3202 | } |
| 3203 | |
| 3204 | /* Print a plink chain */ |
| 3205 | PRIVATE void PlinkPrint(out,plp,tag) |
| 3206 | FILE *out; |
| 3207 | struct plink *plp; |
| 3208 | char *tag; |
| 3209 | { |
| 3210 | while( plp ){ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 3211 | fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3212 | ConfigPrint(out,plp->cfp); |
| 3213 | fprintf(out,"\n"); |
| 3214 | plp = plp->next; |
| 3215 | } |
| 3216 | } |
| 3217 | #endif |
| 3218 | |
| 3219 | /* Print an action to the given file descriptor. Return FALSE if |
| 3220 | ** nothing was actually printed. |
| 3221 | */ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3222 | int PrintAction( |
| 3223 | struct action *ap, /* The action to print */ |
| 3224 | FILE *fp, /* Print the action here */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3225 | int indent /* Indent by this amount */ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3226 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3227 | int result = 1; |
| 3228 | switch( ap->type ){ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3229 | case SHIFT: { |
| 3230 | struct state *stp = ap->x.stp; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3231 | fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3232 | break; |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3233 | } |
| 3234 | case REDUCE: { |
| 3235 | struct rule *rp = ap->x.rp; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3236 | fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->iRule); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3237 | RulePrint(fp, rp, -1); |
| 3238 | break; |
| 3239 | } |
| 3240 | case SHIFTREDUCE: { |
| 3241 | struct rule *rp = ap->x.rp; |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3242 | fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->iRule); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3243 | RulePrint(fp, rp, -1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3244 | break; |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3245 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3246 | case ACCEPT: |
| 3247 | fprintf(fp,"%*s accept",indent,ap->sp->name); |
| 3248 | break; |
| 3249 | case ERROR: |
| 3250 | fprintf(fp,"%*s error",indent,ap->sp->name); |
| 3251 | break; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3252 | case SRCONFLICT: |
| 3253 | case RRCONFLICT: |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3254 | fprintf(fp,"%*s reduce %-7d ** Parsing conflict **", |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3255 | indent,ap->sp->name,ap->x.rp->iRule); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3256 | break; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3257 | case SSCONFLICT: |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3258 | fprintf(fp,"%*s shift %-7d ** Parsing conflict **", |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3259 | indent,ap->sp->name,ap->x.stp->statenum); |
| 3260 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3261 | case SH_RESOLVED: |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3262 | if( showPrecedenceConflict ){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3263 | fprintf(fp,"%*s shift %-7d -- dropped by precedence", |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3264 | indent,ap->sp->name,ap->x.stp->statenum); |
| 3265 | }else{ |
| 3266 | result = 0; |
| 3267 | } |
| 3268 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3269 | case RD_RESOLVED: |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3270 | if( showPrecedenceConflict ){ |
drh | 7e698e9 | 2015-09-07 14:22:24 +0000 | [diff] [blame] | 3271 | fprintf(fp,"%*s reduce %-7d -- dropped by precedence", |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3272 | indent,ap->sp->name,ap->x.rp->iRule); |
drh | f5c4e0f | 2010-07-18 11:35:53 +0000 | [diff] [blame] | 3273 | }else{ |
| 3274 | result = 0; |
| 3275 | } |
| 3276 | break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3277 | case NOT_USED: |
| 3278 | result = 0; |
| 3279 | break; |
| 3280 | } |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 3281 | if( result && ap->spOpt ){ |
| 3282 | fprintf(fp," /* because %s==%s */", ap->sp->name, ap->spOpt->name); |
| 3283 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3284 | return result; |
| 3285 | } |
| 3286 | |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3287 | /* Generate the "*.out" log file */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3288 | void ReportOutput(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3289 | { |
drh | 539e741 | 2018-04-21 20:24:19 +0000 | [diff] [blame] | 3290 | int i, n; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3291 | struct state *stp; |
| 3292 | struct config *cfp; |
| 3293 | struct action *ap; |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3294 | struct rule *rp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3295 | FILE *fp; |
| 3296 | |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 3297 | fp = file_open(lemp,".out","wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3298 | if( fp==0 ) return; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3299 | for(i=0; i<lemp->nxstate; i++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3300 | stp = lemp->sorted[i]; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 3301 | fprintf(fp,"State %d:\n",stp->statenum); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3302 | if( lemp->basisflag ) cfp=stp->bp; |
| 3303 | else cfp=stp->cfp; |
| 3304 | while( cfp ){ |
| 3305 | char buf[20]; |
| 3306 | if( cfp->dot==cfp->rp->nrhs ){ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 3307 | lemon_sprintf(buf,"(%d)",cfp->rp->iRule); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3308 | fprintf(fp," %5s ",buf); |
| 3309 | }else{ |
| 3310 | fprintf(fp," "); |
| 3311 | } |
| 3312 | ConfigPrint(fp,cfp); |
| 3313 | fprintf(fp,"\n"); |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3314 | #if 0 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3315 | SetPrint(fp,cfp->fws,lemp); |
| 3316 | PlinkPrint(fp,cfp->fplp,"To "); |
| 3317 | PlinkPrint(fp,cfp->bplp,"From"); |
| 3318 | #endif |
| 3319 | if( lemp->basisflag ) cfp=cfp->bp; |
| 3320 | else cfp=cfp->next; |
| 3321 | } |
| 3322 | fprintf(fp,"\n"); |
| 3323 | for(ap=stp->ap; ap; ap=ap->next){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3324 | if( PrintAction(ap,fp,30) ) fprintf(fp,"\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3325 | } |
| 3326 | fprintf(fp,"\n"); |
| 3327 | } |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 3328 | fprintf(fp, "----------------------------------------------------\n"); |
| 3329 | fprintf(fp, "Symbols:\n"); |
drh | 539e741 | 2018-04-21 20:24:19 +0000 | [diff] [blame] | 3330 | 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] | 3331 | for(i=0; i<lemp->nsymbol; i++){ |
| 3332 | int j; |
| 3333 | struct symbol *sp; |
| 3334 | |
| 3335 | sp = lemp->symbols[i]; |
| 3336 | fprintf(fp, " %3d: %s", i, sp->name); |
| 3337 | if( sp->type==NONTERMINAL ){ |
| 3338 | fprintf(fp, ":"); |
| 3339 | if( sp->lambda ){ |
| 3340 | fprintf(fp, " <lambda>"); |
| 3341 | } |
| 3342 | for(j=0; j<lemp->nterminal; j++){ |
| 3343 | if( sp->firstset && SetFind(sp->firstset, j) ){ |
| 3344 | fprintf(fp, " %s", lemp->symbols[j]->name); |
| 3345 | } |
| 3346 | } |
| 3347 | } |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3348 | if( sp->prec>=0 ) fprintf(fp," (precedence=%d)", sp->prec); |
drh | e927818 | 2007-07-18 18:16:29 +0000 | [diff] [blame] | 3349 | fprintf(fp, "\n"); |
| 3350 | } |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3351 | fprintf(fp, "----------------------------------------------------\n"); |
drh | 539e741 | 2018-04-21 20:24:19 +0000 | [diff] [blame] | 3352 | fprintf(fp, "Syntax-only Symbols:\n"); |
| 3353 | fprintf(fp, "The following symbols never carry semantic content.\n\n"); |
| 3354 | for(i=n=0; i<lemp->nsymbol; i++){ |
| 3355 | int w; |
| 3356 | struct symbol *sp = lemp->symbols[i]; |
| 3357 | if( sp->bContent ) continue; |
| 3358 | w = (int)strlen(sp->name); |
| 3359 | if( n>0 && n+w>75 ){ |
| 3360 | fprintf(fp,"\n"); |
| 3361 | n = 0; |
| 3362 | } |
| 3363 | if( n>0 ){ |
| 3364 | fprintf(fp, " "); |
| 3365 | n++; |
| 3366 | } |
| 3367 | fprintf(fp, "%s", sp->name); |
| 3368 | n += w; |
| 3369 | } |
| 3370 | if( n>0 ) fprintf(fp, "\n"); |
| 3371 | fprintf(fp, "----------------------------------------------------\n"); |
drh | 12f6839 | 2018-04-06 19:12:55 +0000 | [diff] [blame] | 3372 | fprintf(fp, "Rules:\n"); |
| 3373 | for(rp=lemp->rule; rp; rp=rp->next){ |
| 3374 | fprintf(fp, "%4d: ", rp->iRule); |
| 3375 | rule_print(fp, rp); |
| 3376 | fprintf(fp,"."); |
| 3377 | if( rp->precsym ){ |
| 3378 | fprintf(fp," [%s precedence=%d]", |
| 3379 | rp->precsym->name, rp->precsym->prec); |
| 3380 | } |
| 3381 | fprintf(fp,"\n"); |
| 3382 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3383 | fclose(fp); |
| 3384 | return; |
| 3385 | } |
| 3386 | |
| 3387 | /* Search for the file "name" which is in the same directory as |
| 3388 | ** the exacutable */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3389 | PRIVATE char *pathsearch(char *argv0, char *name, int modemask) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3390 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3391 | const char *pathlist; |
| 3392 | char *pathbufptr; |
| 3393 | char *pathbuf; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3394 | char *path,*cp; |
| 3395 | char c; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3396 | |
| 3397 | #ifdef __WIN32__ |
| 3398 | cp = strrchr(argv0,'\\'); |
| 3399 | #else |
| 3400 | cp = strrchr(argv0,'/'); |
| 3401 | #endif |
| 3402 | if( cp ){ |
| 3403 | c = *cp; |
| 3404 | *cp = 0; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3405 | path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 ); |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3406 | if( path ) lemon_sprintf(path,"%s/%s",argv0,name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3407 | *cp = c; |
| 3408 | }else{ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3409 | pathlist = getenv("PATH"); |
| 3410 | if( pathlist==0 ) pathlist = ".:/bin:/usr/bin"; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3411 | pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 ); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3412 | path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 ); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3413 | if( (pathbuf != 0) && (path!=0) ){ |
| 3414 | pathbufptr = pathbuf; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3415 | lemon_strcpy(pathbuf, pathlist); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3416 | while( *pathbuf ){ |
| 3417 | cp = strchr(pathbuf,':'); |
| 3418 | if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3419 | c = *cp; |
| 3420 | *cp = 0; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3421 | lemon_sprintf(path,"%s/%s",pathbuf,name); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3422 | *cp = c; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3423 | if( c==0 ) pathbuf[0] = 0; |
| 3424 | else pathbuf = &cp[1]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3425 | if( access(path,modemask)==0 ) break; |
| 3426 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3427 | free(pathbufptr); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3428 | } |
| 3429 | } |
| 3430 | return path; |
| 3431 | } |
| 3432 | |
| 3433 | /* Given an action, compute the integer value for that action |
| 3434 | ** which is to be put in the action table of the generated machine. |
| 3435 | ** Return negative if no action should be generated. |
| 3436 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3437 | PRIVATE int compute_action(struct lemon *lemp, struct action *ap) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3438 | { |
| 3439 | int act; |
| 3440 | switch( ap->type ){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 3441 | case SHIFT: act = ap->x.stp->statenum; break; |
drh | bd8fcc1 | 2017-06-28 11:56:18 +0000 | [diff] [blame] | 3442 | case SHIFTREDUCE: { |
drh | bd8fcc1 | 2017-06-28 11:56:18 +0000 | [diff] [blame] | 3443 | /* Since a SHIFT is inherient after a prior REDUCE, convert any |
| 3444 | ** SHIFTREDUCE action with a nonterminal on the LHS into a simple |
| 3445 | ** REDUCE action: */ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3446 | if( ap->sp->index>=lemp->nterminal ){ |
| 3447 | act = lemp->minReduce + ap->x.rp->iRule; |
| 3448 | }else{ |
| 3449 | act = lemp->minShiftReduce + ap->x.rp->iRule; |
| 3450 | } |
drh | bd8fcc1 | 2017-06-28 11:56:18 +0000 | [diff] [blame] | 3451 | break; |
| 3452 | } |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 3453 | case REDUCE: act = lemp->minReduce + ap->x.rp->iRule; break; |
| 3454 | case ERROR: act = lemp->errAction; break; |
| 3455 | case ACCEPT: act = lemp->accAction; break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3456 | default: act = -1; break; |
| 3457 | } |
| 3458 | return act; |
| 3459 | } |
| 3460 | |
| 3461 | #define LINESIZE 1000 |
| 3462 | /* The next cluster of routines are for reading the template file |
| 3463 | ** and writing the results to the generated parser */ |
| 3464 | /* The first function transfers data from "in" to "out" until |
| 3465 | ** a line is seen which begins with "%%". The line number is |
| 3466 | ** tracked. |
| 3467 | ** |
| 3468 | ** if name!=0, then any word that begin with "Parse" is changed to |
| 3469 | ** begin with *name instead. |
| 3470 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3471 | PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3472 | { |
| 3473 | int i, iStart; |
| 3474 | char line[LINESIZE]; |
| 3475 | while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){ |
| 3476 | (*lineno)++; |
| 3477 | iStart = 0; |
| 3478 | if( name ){ |
| 3479 | for(i=0; line[i]; i++){ |
| 3480 | if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0 |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3481 | && (i==0 || !ISALPHA(line[i-1])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3482 | ){ |
| 3483 | if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]); |
| 3484 | fprintf(out,"%s",name); |
| 3485 | i += 4; |
| 3486 | iStart = i+1; |
| 3487 | } |
| 3488 | } |
| 3489 | } |
| 3490 | fprintf(out,"%s",&line[iStart]); |
| 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | /* The next function finds the template file and opens it, returning |
| 3495 | ** a pointer to the opened file. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3496 | PRIVATE FILE *tplt_open(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3497 | { |
| 3498 | static char templatename[] = "lempar.c"; |
| 3499 | char buf[1000]; |
| 3500 | FILE *in; |
| 3501 | char *tpltname; |
| 3502 | char *cp; |
| 3503 | |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 3504 | /* first, see if user specified a template filename on the command line. */ |
| 3505 | if (user_templatename != 0) { |
| 3506 | if( access(user_templatename,004)==-1 ){ |
| 3507 | fprintf(stderr,"Can't find the parser driver template file \"%s\".\n", |
| 3508 | user_templatename); |
| 3509 | lemp->errorcnt++; |
| 3510 | return 0; |
| 3511 | } |
| 3512 | in = fopen(user_templatename,"rb"); |
| 3513 | if( in==0 ){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3514 | fprintf(stderr,"Can't open the template file \"%s\".\n", |
| 3515 | user_templatename); |
icculus | 3e143bd | 2010-02-14 00:48:49 +0000 | [diff] [blame] | 3516 | lemp->errorcnt++; |
| 3517 | return 0; |
| 3518 | } |
| 3519 | return in; |
| 3520 | } |
| 3521 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3522 | cp = strrchr(lemp->filename,'.'); |
| 3523 | if( cp ){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3524 | lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3525 | }else{ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3526 | lemon_sprintf(buf,"%s.lt",lemp->filename); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3527 | } |
| 3528 | if( access(buf,004)==0 ){ |
| 3529 | tpltname = buf; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3530 | }else if( access(templatename,004)==0 ){ |
| 3531 | tpltname = templatename; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3532 | }else{ |
| 3533 | tpltname = pathsearch(lemp->argv0,templatename,0); |
| 3534 | } |
| 3535 | if( tpltname==0 ){ |
| 3536 | fprintf(stderr,"Can't find the parser driver template file \"%s\".\n", |
| 3537 | templatename); |
| 3538 | lemp->errorcnt++; |
| 3539 | return 0; |
| 3540 | } |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 3541 | in = fopen(tpltname,"rb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3542 | if( in==0 ){ |
| 3543 | fprintf(stderr,"Can't open the template file \"%s\".\n",templatename); |
| 3544 | lemp->errorcnt++; |
| 3545 | return 0; |
| 3546 | } |
| 3547 | return in; |
| 3548 | } |
| 3549 | |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3550 | /* Print a #line directive line to the output file. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3551 | PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename) |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3552 | { |
| 3553 | fprintf(out,"#line %d \"",lineno); |
| 3554 | while( *filename ){ |
| 3555 | if( *filename == '\\' ) putc('\\',out); |
| 3556 | putc(*filename,out); |
| 3557 | filename++; |
| 3558 | } |
| 3559 | fprintf(out,"\"\n"); |
| 3560 | } |
| 3561 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3562 | /* Print a string to the file and keep the linenumber up to date */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3563 | PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3564 | { |
| 3565 | if( str==0 ) return; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3566 | while( *str ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3567 | putc(*str,out); |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3568 | if( *str=='\n' ) (*lineno)++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3569 | str++; |
| 3570 | } |
drh | 9db55df | 2004-09-09 14:01:21 +0000 | [diff] [blame] | 3571 | if( str[-1]!='\n' ){ |
| 3572 | putc('\n',out); |
| 3573 | (*lineno)++; |
| 3574 | } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3575 | if (!lemp->nolinenosflag) { |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3576 | (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3577 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3578 | return; |
| 3579 | } |
| 3580 | |
| 3581 | /* |
| 3582 | ** The following routine emits code for the destructor for the |
| 3583 | ** symbol sp |
| 3584 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3585 | void emit_destructor_code( |
| 3586 | FILE *out, |
| 3587 | struct symbol *sp, |
| 3588 | struct lemon *lemp, |
| 3589 | int *lineno |
| 3590 | ){ |
drh | cc83b6e | 2004-04-23 23:38:42 +0000 | [diff] [blame] | 3591 | char *cp = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3592 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3593 | if( sp->type==TERMINAL ){ |
| 3594 | cp = lemp->tokendest; |
| 3595 | if( cp==0 ) return; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 3596 | fprintf(out,"{\n"); (*lineno)++; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3597 | }else if( sp->destructor ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3598 | cp = sp->destructor; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 3599 | fprintf(out,"{\n"); (*lineno)++; |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3600 | if( !lemp->nolinenosflag ){ |
| 3601 | (*lineno)++; |
| 3602 | tplt_linedir(out,sp->destLineno,lemp->filename); |
| 3603 | } |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3604 | }else if( lemp->vardest ){ |
| 3605 | cp = lemp->vardest; |
| 3606 | if( cp==0 ) return; |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 3607 | fprintf(out,"{\n"); (*lineno)++; |
drh | cc83b6e | 2004-04-23 23:38:42 +0000 | [diff] [blame] | 3608 | }else{ |
| 3609 | assert( 0 ); /* Cannot happen */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3610 | } |
| 3611 | for(; *cp; cp++){ |
| 3612 | if( *cp=='$' && cp[1]=='$' ){ |
| 3613 | fprintf(out,"(yypminor->yy%d)",sp->dtnum); |
| 3614 | cp++; |
| 3615 | continue; |
| 3616 | } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3617 | if( *cp=='\n' ) (*lineno)++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3618 | fputc(*cp,out); |
| 3619 | } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3620 | fprintf(out,"\n"); (*lineno)++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3621 | if (!lemp->nolinenosflag) { |
| 3622 | (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3623 | } |
| 3624 | fprintf(out,"}\n"); (*lineno)++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3625 | return; |
| 3626 | } |
| 3627 | |
| 3628 | /* |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3629 | ** Return TRUE (non-zero) if the given symbol has a destructor. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3630 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3631 | int has_destructor(struct symbol *sp, struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3632 | { |
| 3633 | int ret; |
| 3634 | if( sp->type==TERMINAL ){ |
| 3635 | ret = lemp->tokendest!=0; |
| 3636 | }else{ |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3637 | ret = lemp->vardest!=0 || sp->destructor!=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3638 | } |
| 3639 | return ret; |
| 3640 | } |
| 3641 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3642 | /* |
| 3643 | ** Append text to a dynamically allocated string. If zText is 0 then |
| 3644 | ** reset the string to be empty again. Always return the complete text |
| 3645 | ** of the string (which is overwritten with each call). |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3646 | ** |
| 3647 | ** n bytes of zText are stored. If n==0 then all of zText up to the first |
| 3648 | ** \000 terminator is stored. zText can contain up to two instances of |
| 3649 | ** %d. The values of p1 and p2 are written into the first and second |
| 3650 | ** %d. |
| 3651 | ** |
| 3652 | ** If n==-1, then the previous character is overwritten. |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3653 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3654 | PRIVATE char *append_str(const char *zText, int n, int p1, int p2){ |
| 3655 | static char empty[1] = { 0 }; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3656 | static char *z = 0; |
| 3657 | static int alloced = 0; |
| 3658 | static int used = 0; |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3659 | int c; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3660 | char zInt[40]; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3661 | if( zText==0 ){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3662 | if( used==0 && z!=0 ) z[0] = 0; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3663 | used = 0; |
| 3664 | return z; |
| 3665 | } |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3666 | if( n<=0 ){ |
| 3667 | if( n<0 ){ |
| 3668 | used += n; |
| 3669 | assert( used>=0 ); |
| 3670 | } |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3671 | n = lemonStrlen(zText); |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3672 | } |
drh | df60971 | 2010-11-23 20:55:27 +0000 | [diff] [blame] | 3673 | if( (int) (n+sizeof(zInt)*2+used) >= alloced ){ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3674 | alloced = n + sizeof(zInt)*2 + used + 200; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3675 | z = (char *) realloc(z, alloced); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3676 | } |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3677 | if( z==0 ) return empty; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3678 | while( n-- > 0 ){ |
| 3679 | c = *(zText++); |
drh | 5048962 | 2006-10-13 12:25:29 +0000 | [diff] [blame] | 3680 | if( c=='%' && n>0 && zText[0]=='d' ){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3681 | lemon_sprintf(zInt, "%d", p1); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3682 | p1 = p2; |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 3683 | lemon_strcpy(&z[used], zInt); |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3684 | used += lemonStrlen(&z[used]); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3685 | zText++; |
| 3686 | n--; |
| 3687 | }else{ |
mistachkin | 2318d33 | 2015-01-12 18:02:52 +0000 | [diff] [blame] | 3688 | z[used++] = (char)c; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3689 | } |
| 3690 | } |
| 3691 | z[used] = 0; |
| 3692 | return z; |
| 3693 | } |
| 3694 | |
| 3695 | /* |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3696 | ** Write and transform the rp->code string so that symbols are expanded. |
| 3697 | ** Populate the rp->codePrefix and rp->codeSuffix strings, as appropriate. |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3698 | ** |
| 3699 | ** Return 1 if the expanded code requires that "yylhsminor" local variable |
| 3700 | ** to be defined. |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3701 | */ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3702 | PRIVATE int translate_code(struct lemon *lemp, struct rule *rp){ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3703 | char *cp, *xp; |
| 3704 | int i; |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3705 | int rc = 0; /* True if yylhsminor is used */ |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 3706 | int dontUseRhs0 = 0; /* If true, use of left-most RHS label is illegal */ |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3707 | const char *zSkip = 0; /* The zOvwrt comment within rp->code, or NULL */ |
| 3708 | char lhsused = 0; /* True if the LHS element has been used */ |
| 3709 | char lhsdirect; /* True if LHS writes directly into stack */ |
| 3710 | char used[MAXRHS]; /* True for each RHS element which is used */ |
| 3711 | char zLhs[50]; /* Convert the LHS symbol into this string */ |
| 3712 | char zOvwrt[900]; /* Comment that to allow LHS to overwrite RHS */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3713 | |
| 3714 | for(i=0; i<rp->nrhs; i++) used[i] = 0; |
| 3715 | lhsused = 0; |
| 3716 | |
drh | 19c9e56 | 2007-03-29 20:13:53 +0000 | [diff] [blame] | 3717 | if( rp->code==0 ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3718 | static char newlinestr[2] = { '\n', '\0' }; |
| 3719 | rp->code = newlinestr; |
drh | 19c9e56 | 2007-03-29 20:13:53 +0000 | [diff] [blame] | 3720 | rp->line = rp->ruleline; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3721 | rp->noCode = 1; |
| 3722 | }else{ |
| 3723 | rp->noCode = 0; |
drh | 19c9e56 | 2007-03-29 20:13:53 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3726 | |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 3727 | if( rp->nrhs==0 ){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3728 | /* If there are no RHS symbols, then writing directly to the LHS is ok */ |
| 3729 | lhsdirect = 1; |
| 3730 | }else if( rp->rhsalias[0]==0 ){ |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 3731 | /* The left-most RHS symbol has no value. LHS direct is ok. But |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3732 | ** we have to call the distructor on the RHS symbol first. */ |
| 3733 | lhsdirect = 1; |
| 3734 | if( has_destructor(rp->rhs[0],lemp) ){ |
| 3735 | append_str(0,0,0,0); |
| 3736 | append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0, |
| 3737 | rp->rhs[0]->index,1-rp->nrhs); |
| 3738 | rp->codePrefix = Strsafe(append_str(0,0,0,0)); |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3739 | rp->noCode = 0; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3740 | } |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 3741 | }else if( rp->lhsalias==0 ){ |
| 3742 | /* There is no LHS value symbol. */ |
| 3743 | lhsdirect = 1; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3744 | }else if( strcmp(rp->lhsalias,rp->rhsalias[0])==0 ){ |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3745 | /* The LHS symbol and the left-most RHS symbol are the same, so |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3746 | ** direct writing is allowed */ |
| 3747 | lhsdirect = 1; |
| 3748 | lhsused = 1; |
| 3749 | used[0] = 1; |
| 3750 | if( rp->lhs->dtnum!=rp->rhs[0]->dtnum ){ |
| 3751 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3752 | "%s(%s) and %s(%s) share the same label but have " |
| 3753 | "different datatypes.", |
| 3754 | rp->lhs->name, rp->lhsalias, rp->rhs[0]->name, rp->rhsalias[0]); |
| 3755 | lemp->errorcnt++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3756 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3757 | }else{ |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3758 | lemon_sprintf(zOvwrt, "/*%s-overwrites-%s*/", |
| 3759 | rp->lhsalias, rp->rhsalias[0]); |
| 3760 | zSkip = strstr(rp->code, zOvwrt); |
| 3761 | if( zSkip!=0 ){ |
| 3762 | /* The code contains a special comment that indicates that it is safe |
| 3763 | ** for the LHS label to overwrite left-most RHS label. */ |
| 3764 | lhsdirect = 1; |
| 3765 | }else{ |
| 3766 | lhsdirect = 0; |
| 3767 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3768 | } |
| 3769 | if( lhsdirect ){ |
| 3770 | sprintf(zLhs, "yymsp[%d].minor.yy%d",1-rp->nrhs,rp->lhs->dtnum); |
| 3771 | }else{ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3772 | rc = 1; |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3773 | sprintf(zLhs, "yylhsminor.yy%d",rp->lhs->dtnum); |
| 3774 | } |
| 3775 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3776 | append_str(0,0,0,0); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3777 | |
| 3778 | /* This const cast is wrong but harmless, if we're careful. */ |
| 3779 | for(cp=(char *)rp->code; *cp; cp++){ |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3780 | if( cp==zSkip ){ |
| 3781 | append_str(zOvwrt,0,0,0); |
| 3782 | cp += lemonStrlen(zOvwrt)-1; |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 3783 | dontUseRhs0 = 1; |
drh | cf82f0d | 2016-02-17 04:33:10 +0000 | [diff] [blame] | 3784 | continue; |
| 3785 | } |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3786 | if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3787 | char saved; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 3788 | for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3789 | saved = *xp; |
| 3790 | *xp = 0; |
| 3791 | if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3792 | append_str(zLhs,0,0,0); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3793 | cp = xp; |
| 3794 | lhsused = 1; |
| 3795 | }else{ |
| 3796 | for(i=0; i<rp->nrhs; i++){ |
| 3797 | if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){ |
drh | 43303de | 2016-02-17 12:34:03 +0000 | [diff] [blame] | 3798 | if( i==0 && dontUseRhs0 ){ |
| 3799 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3800 | "Label %s used after '%s'.", |
| 3801 | rp->rhsalias[0], zOvwrt); |
| 3802 | lemp->errorcnt++; |
| 3803 | }else if( cp!=rp->code && cp[-1]=='@' ){ |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3804 | /* If the argument is of the form @X then substituted |
| 3805 | ** the token number of X, not the value of X */ |
| 3806 | append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0); |
| 3807 | }else{ |
drh | fd40531 | 2005-11-06 04:06:59 +0000 | [diff] [blame] | 3808 | struct symbol *sp = rp->rhs[i]; |
| 3809 | int dtnum; |
| 3810 | if( sp->type==MULTITERMINAL ){ |
| 3811 | dtnum = sp->subsym[0]->dtnum; |
| 3812 | }else{ |
| 3813 | dtnum = sp->dtnum; |
| 3814 | } |
| 3815 | append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum); |
drh | 7ac25c7 | 2004-08-19 15:12:26 +0000 | [diff] [blame] | 3816 | } |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3817 | cp = xp; |
| 3818 | used[i] = 1; |
| 3819 | break; |
| 3820 | } |
| 3821 | } |
| 3822 | } |
| 3823 | *xp = saved; |
| 3824 | } |
| 3825 | append_str(cp, 1, 0, 0); |
| 3826 | } /* End loop */ |
| 3827 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3828 | /* Main code generation completed */ |
| 3829 | cp = append_str(0,0,0,0); |
| 3830 | if( cp && cp[0] ) rp->code = Strsafe(cp); |
| 3831 | append_str(0,0,0,0); |
| 3832 | |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3833 | /* Check to make sure the LHS has been used */ |
| 3834 | if( rp->lhsalias && !lhsused ){ |
| 3835 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3836 | "Label \"%s\" for \"%s(%s)\" is never used.", |
| 3837 | rp->lhsalias,rp->lhs->name,rp->lhsalias); |
| 3838 | lemp->errorcnt++; |
| 3839 | } |
| 3840 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3841 | /* Generate destructor code for RHS minor values which are not referenced. |
| 3842 | ** Generate error messages for unused labels and duplicate labels. |
| 3843 | */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3844 | for(i=0; i<rp->nrhs; i++){ |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3845 | if( rp->rhsalias[i] ){ |
| 3846 | if( i>0 ){ |
| 3847 | int j; |
| 3848 | if( rp->lhsalias && strcmp(rp->lhsalias,rp->rhsalias[i])==0 ){ |
| 3849 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3850 | "%s(%s) has the same label as the LHS but is not the left-most " |
| 3851 | "symbol on the RHS.", |
drh | f135cb7 | 2019-04-30 14:26:31 +0000 | [diff] [blame] | 3852 | rp->rhs[i]->name, rp->rhsalias[i]); |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3853 | lemp->errorcnt++; |
| 3854 | } |
| 3855 | for(j=0; j<i; j++){ |
| 3856 | if( rp->rhsalias[j] && strcmp(rp->rhsalias[j],rp->rhsalias[i])==0 ){ |
| 3857 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3858 | "Label %s used for multiple symbols on the RHS of a rule.", |
| 3859 | rp->rhsalias[i]); |
| 3860 | lemp->errorcnt++; |
| 3861 | break; |
| 3862 | } |
| 3863 | } |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3864 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3865 | if( !used[i] ){ |
| 3866 | ErrorMsg(lemp->filename,rp->ruleline, |
| 3867 | "Label %s for \"%s(%s)\" is never used.", |
| 3868 | rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]); |
| 3869 | lemp->errorcnt++; |
| 3870 | } |
| 3871 | }else if( i>0 && has_destructor(rp->rhs[i],lemp) ){ |
| 3872 | append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0, |
| 3873 | rp->rhs[i]->index,i-rp->nrhs+1); |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3874 | } |
| 3875 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3876 | |
| 3877 | /* If unable to write LHS values directly into the stack, write the |
| 3878 | ** saved LHS value now. */ |
| 3879 | if( lhsdirect==0 ){ |
| 3880 | append_str(" yymsp[%d].minor.yy%d = ", 0, 1-rp->nrhs, rp->lhs->dtnum); |
| 3881 | append_str(zLhs, 0, 0, 0); |
| 3882 | append_str(";\n", 0, 0, 0); |
drh | 61e339a | 2007-01-16 03:09:02 +0000 | [diff] [blame] | 3883 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3884 | |
| 3885 | /* Suffix code generation complete */ |
| 3886 | cp = append_str(0,0,0,0); |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 3887 | if( cp && cp[0] ){ |
| 3888 | rp->codeSuffix = Strsafe(cp); |
| 3889 | rp->noCode = 0; |
| 3890 | } |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 3891 | |
| 3892 | return rc; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 3895 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3896 | ** Generate code which executes when the rule "rp" is reduced. Write |
| 3897 | ** the code to "out". Make sure lineno stays up-to-date. |
| 3898 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3899 | PRIVATE void emit_code( |
| 3900 | FILE *out, |
| 3901 | struct rule *rp, |
| 3902 | struct lemon *lemp, |
| 3903 | int *lineno |
| 3904 | ){ |
| 3905 | const char *cp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3906 | |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3907 | /* Setup code prior to the #line directive */ |
| 3908 | if( rp->codePrefix && rp->codePrefix[0] ){ |
| 3909 | fprintf(out, "{%s", rp->codePrefix); |
| 3910 | for(cp=rp->codePrefix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } |
| 3911 | } |
| 3912 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3913 | /* Generate code to do the reduce action */ |
| 3914 | if( rp->code ){ |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3915 | if( !lemp->nolinenosflag ){ |
| 3916 | (*lineno)++; |
| 3917 | tplt_linedir(out,rp->line,lemp->filename); |
| 3918 | } |
drh | af805ca | 2004-09-07 11:28:25 +0000 | [diff] [blame] | 3919 | fprintf(out,"{%s",rp->code); |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3920 | for(cp=rp->code; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } |
shane | 5854393 | 2008-12-10 20:10:04 +0000 | [diff] [blame] | 3921 | fprintf(out,"}\n"); (*lineno)++; |
drh | 2547336 | 2015-09-04 18:03:45 +0000 | [diff] [blame] | 3922 | if( !lemp->nolinenosflag ){ |
| 3923 | (*lineno)++; |
| 3924 | tplt_linedir(out,*lineno,lemp->outname); |
| 3925 | } |
drh | 4dd0d3f | 2016-02-17 01:18:33 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | /* Generate breakdown code that occurs after the #line directive */ |
| 3929 | if( rp->codeSuffix && rp->codeSuffix[0] ){ |
| 3930 | fprintf(out, "%s", rp->codeSuffix); |
| 3931 | for(cp=rp->codeSuffix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; } |
| 3932 | } |
| 3933 | |
| 3934 | if( rp->codePrefix ){ |
| 3935 | fprintf(out, "}\n"); (*lineno)++; |
| 3936 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3937 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3938 | return; |
| 3939 | } |
| 3940 | |
| 3941 | /* |
| 3942 | ** Print the definition of the union used for the parser's data stack. |
| 3943 | ** This union contains fields for every possible data type for tokens |
| 3944 | ** and nonterminals. In the process of computing and printing this |
| 3945 | ** union, also set the ".dtnum" field of every terminal and nonterminal |
| 3946 | ** symbol. |
| 3947 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3948 | void print_stack_union( |
| 3949 | FILE *out, /* The output stream */ |
| 3950 | struct lemon *lemp, /* The main info structure for this parser */ |
| 3951 | int *plineno, /* Pointer to the line number */ |
| 3952 | int mhflag /* True if generating makeheaders output */ |
| 3953 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3954 | int lineno = *plineno; /* The line number of the output */ |
| 3955 | char **types; /* A hash table of datatypes */ |
| 3956 | int arraysize; /* Size of the "types" array */ |
| 3957 | int maxdtlength; /* Maximum length of any ".datatype" field. */ |
| 3958 | char *stddt; /* Standardized name for a datatype */ |
| 3959 | int i,j; /* Loop counters */ |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 3960 | unsigned hash; /* For hashing the name of a type */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 3961 | const char *name; /* Name of the parser */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3962 | |
| 3963 | /* Allocate and initialize types[] and allocate stddt[] */ |
| 3964 | arraysize = lemp->nsymbol * 2; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 3965 | types = (char**)calloc( arraysize, sizeof(char*) ); |
drh | 070d422 | 2011-06-02 15:48:51 +0000 | [diff] [blame] | 3966 | if( types==0 ){ |
| 3967 | fprintf(stderr,"Out of memory.\n"); |
| 3968 | exit(1); |
| 3969 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3970 | for(i=0; i<arraysize; i++) types[i] = 0; |
| 3971 | maxdtlength = 0; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3972 | if( lemp->vartype ){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3973 | maxdtlength = lemonStrlen(lemp->vartype); |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 3974 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3975 | for(i=0; i<lemp->nsymbol; i++){ |
| 3976 | int len; |
| 3977 | struct symbol *sp = lemp->symbols[i]; |
| 3978 | if( sp->datatype==0 ) continue; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 3979 | len = lemonStrlen(sp->datatype); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3980 | if( len>maxdtlength ) maxdtlength = len; |
| 3981 | } |
| 3982 | stddt = (char*)malloc( maxdtlength*2 + 1 ); |
drh | 070d422 | 2011-06-02 15:48:51 +0000 | [diff] [blame] | 3983 | if( stddt==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3984 | fprintf(stderr,"Out of memory.\n"); |
| 3985 | exit(1); |
| 3986 | } |
| 3987 | |
| 3988 | /* Build a hash table of datatypes. The ".dtnum" field of each symbol |
| 3989 | ** 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] | 3990 | ** used for terminal symbols. If there is no %default_type defined then |
| 3991 | ** 0 is also used as the .dtnum value for nonterminals which do not specify |
| 3992 | ** a datatype using the %type directive. |
| 3993 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3994 | for(i=0; i<lemp->nsymbol; i++){ |
| 3995 | struct symbol *sp = lemp->symbols[i]; |
| 3996 | char *cp; |
| 3997 | if( sp==lemp->errsym ){ |
| 3998 | sp->dtnum = arraysize+1; |
| 3999 | continue; |
| 4000 | } |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 4001 | if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4002 | sp->dtnum = 0; |
| 4003 | continue; |
| 4004 | } |
| 4005 | cp = sp->datatype; |
drh | 960e8c6 | 2001-04-03 16:53:21 +0000 | [diff] [blame] | 4006 | if( cp==0 ) cp = lemp->vartype; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4007 | j = 0; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 4008 | while( ISSPACE(*cp) ) cp++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4009 | while( *cp ) stddt[j++] = *cp++; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 4010 | while( j>0 && ISSPACE(stddt[j-1]) ) j--; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4011 | stddt[j] = 0; |
drh | 02368c9 | 2009-04-05 15:18:02 +0000 | [diff] [blame] | 4012 | if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){ |
drh | 32c4d74 | 2008-07-01 16:34:49 +0000 | [diff] [blame] | 4013 | sp->dtnum = 0; |
| 4014 | continue; |
| 4015 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4016 | hash = 0; |
| 4017 | for(j=0; stddt[j]; j++){ |
| 4018 | hash = hash*53 + stddt[j]; |
| 4019 | } |
drh | 3b2129c | 2003-05-13 00:34:21 +0000 | [diff] [blame] | 4020 | hash = (hash & 0x7fffffff)%arraysize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4021 | while( types[hash] ){ |
| 4022 | if( strcmp(types[hash],stddt)==0 ){ |
| 4023 | sp->dtnum = hash + 1; |
| 4024 | break; |
| 4025 | } |
| 4026 | hash++; |
drh | 2b51f21 | 2013-10-11 23:01:02 +0000 | [diff] [blame] | 4027 | if( hash>=(unsigned)arraysize ) hash = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4028 | } |
| 4029 | if( types[hash]==0 ){ |
| 4030 | sp->dtnum = hash + 1; |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 4031 | types[hash] = (char*)malloc( lemonStrlen(stddt)+1 ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4032 | if( types[hash]==0 ){ |
| 4033 | fprintf(stderr,"Out of memory.\n"); |
| 4034 | exit(1); |
| 4035 | } |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 4036 | lemon_strcpy(types[hash],stddt); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */ |
| 4041 | name = lemp->name ? lemp->name : "Parse"; |
| 4042 | lineno = *plineno; |
| 4043 | if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; } |
| 4044 | fprintf(out,"#define %sTOKENTYPE %s\n",name, |
| 4045 | lemp->tokentype?lemp->tokentype:"void*"); lineno++; |
| 4046 | if( mhflag ){ fprintf(out,"#endif\n"); lineno++; } |
| 4047 | fprintf(out,"typedef union {\n"); lineno++; |
drh | 15b024c | 2008-12-11 02:20:43 +0000 | [diff] [blame] | 4048 | fprintf(out," int yyinit;\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4049 | fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++; |
| 4050 | for(i=0; i<arraysize; i++){ |
| 4051 | if( types[i]==0 ) continue; |
| 4052 | fprintf(out," %s yy%d;\n",types[i],i+1); lineno++; |
| 4053 | free(types[i]); |
| 4054 | } |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 4055 | if( lemp->errsym && lemp->errsym->useCnt ){ |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4056 | fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++; |
| 4057 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4058 | free(stddt); |
| 4059 | free(types); |
| 4060 | fprintf(out,"} YYMINORTYPE;\n"); lineno++; |
| 4061 | *plineno = lineno; |
| 4062 | } |
| 4063 | |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4064 | /* |
| 4065 | ** Return the name of a C datatype able to represent values between |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4066 | ** lwr and upr, inclusive. If pnByte!=NULL then also write the sizeof |
| 4067 | ** for that type (1, 2, or 4) into *pnByte. |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4068 | */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4069 | static const char *minimum_size_type(int lwr, int upr, int *pnByte){ |
| 4070 | const char *zType = "int"; |
| 4071 | int nByte = 4; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4072 | if( lwr>=0 ){ |
| 4073 | if( upr<=255 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4074 | zType = "unsigned char"; |
| 4075 | nByte = 1; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4076 | }else if( upr<65535 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4077 | zType = "unsigned short int"; |
| 4078 | nByte = 2; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4079 | }else{ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4080 | zType = "unsigned int"; |
| 4081 | nByte = 4; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4082 | } |
| 4083 | }else if( lwr>=-127 && upr<=127 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4084 | zType = "signed char"; |
| 4085 | nByte = 1; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4086 | }else if( lwr>=-32767 && upr<32767 ){ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4087 | zType = "short"; |
| 4088 | nByte = 2; |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4089 | } |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4090 | if( pnByte ) *pnByte = nByte; |
| 4091 | return zType; |
drh | b29b0a5 | 2002-02-23 19:39:46 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4094 | /* |
| 4095 | ** Each state contains a set of token transaction and a set of |
| 4096 | ** nonterminal transactions. Each of these sets makes an instance |
| 4097 | ** of the following structure. An array of these structures is used |
| 4098 | ** to order the creation of entries in the yy_action[] table. |
| 4099 | */ |
| 4100 | struct axset { |
| 4101 | struct state *stp; /* A pointer to a state */ |
| 4102 | int isTkn; /* True to use tokens. False for non-terminals */ |
| 4103 | int nAction; /* Number of actions */ |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4104 | int iOrder; /* Original order of action sets */ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4105 | }; |
| 4106 | |
| 4107 | /* |
| 4108 | ** Compare to axset structures for sorting purposes |
| 4109 | */ |
| 4110 | static int axset_compare(const void *a, const void *b){ |
| 4111 | struct axset *p1 = (struct axset*)a; |
| 4112 | struct axset *p2 = (struct axset*)b; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4113 | int c; |
| 4114 | c = p2->nAction - p1->nAction; |
| 4115 | if( c==0 ){ |
drh | 337cd0d | 2015-09-07 23:40:42 +0000 | [diff] [blame] | 4116 | c = p1->iOrder - p2->iOrder; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4117 | } |
| 4118 | assert( c!=0 || p1==p2 ); |
| 4119 | return c; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4122 | /* |
| 4123 | ** Write text on "out" that describes the rule "rp". |
| 4124 | */ |
| 4125 | static void writeRuleText(FILE *out, struct rule *rp){ |
| 4126 | int j; |
| 4127 | fprintf(out,"%s ::=", rp->lhs->name); |
| 4128 | for(j=0; j<rp->nrhs; j++){ |
| 4129 | struct symbol *sp = rp->rhs[j]; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4130 | if( sp->type!=MULTITERMINAL ){ |
| 4131 | fprintf(out," %s", sp->name); |
| 4132 | }else{ |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4133 | int k; |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4134 | fprintf(out," %s", sp->subsym[0]->name); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4135 | for(k=1; k<sp->nsubsym; k++){ |
| 4136 | fprintf(out,"|%s",sp->subsym[k]->name); |
| 4137 | } |
| 4138 | } |
| 4139 | } |
| 4140 | } |
| 4141 | |
| 4142 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4143 | /* Generate C source code for the parser */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4144 | void ReportTable( |
| 4145 | struct lemon *lemp, |
| 4146 | int mhflag /* Output in makeheaders format if true */ |
| 4147 | ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4148 | FILE *out, *in; |
| 4149 | char line[LINESIZE]; |
| 4150 | int lineno; |
| 4151 | struct state *stp; |
| 4152 | struct action *ap; |
| 4153 | struct rule *rp; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4154 | struct acttab *pActtab; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4155 | int i, j, n, sz; |
drh | 2e51716 | 2019-08-28 02:09:47 +0000 | [diff] [blame] | 4156 | int nLookAhead; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4157 | int szActionType; /* sizeof(YYACTIONTYPE) */ |
| 4158 | int szCodeType; /* sizeof(YYCODETYPE) */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4159 | const char *name; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4160 | int mnTknOfst, mxTknOfst; |
| 4161 | int mnNtOfst, mxNtOfst; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4162 | struct axset *ax; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4163 | |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4164 | lemp->minShiftReduce = lemp->nstate; |
| 4165 | lemp->errAction = lemp->minShiftReduce + lemp->nrule; |
| 4166 | lemp->accAction = lemp->errAction + 1; |
| 4167 | lemp->noAction = lemp->accAction + 1; |
| 4168 | lemp->minReduce = lemp->noAction + 1; |
| 4169 | lemp->maxAction = lemp->minReduce + lemp->nrule; |
| 4170 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4171 | in = tplt_open(lemp); |
| 4172 | if( in==0 ) return; |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 4173 | out = file_open(lemp,".c","wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4174 | if( out==0 ){ |
| 4175 | fclose(in); |
| 4176 | return; |
| 4177 | } |
| 4178 | lineno = 1; |
| 4179 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4180 | |
| 4181 | /* Generate the include code, if any */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4182 | tplt_print(out,lemp,lemp->include,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4183 | if( mhflag ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 4184 | char *incName = file_makename(lemp, ".h"); |
| 4185 | fprintf(out,"#include \"%s\"\n", incName); lineno++; |
| 4186 | free(incName); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4187 | } |
| 4188 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4189 | |
| 4190 | /* Generate #defines for all tokens */ |
| 4191 | if( mhflag ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4192 | const char *prefix; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4193 | fprintf(out,"#if INTERFACE\n"); lineno++; |
| 4194 | if( lemp->tokenprefix ) prefix = lemp->tokenprefix; |
| 4195 | else prefix = ""; |
| 4196 | for(i=1; i<lemp->nterminal; i++){ |
| 4197 | fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i); |
| 4198 | lineno++; |
| 4199 | } |
| 4200 | fprintf(out,"#endif\n"); lineno++; |
| 4201 | } |
| 4202 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4203 | |
| 4204 | /* Generate the defines */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4205 | fprintf(out,"#define YYCODETYPE %s\n", |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 4206 | minimum_size_type(0, lemp->nsymbol, &szCodeType)); lineno++; |
| 4207 | fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4208 | fprintf(out,"#define YYACTIONTYPE %s\n", |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4209 | minimum_size_type(0,lemp->maxAction,&szActionType)); lineno++; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4210 | if( lemp->wildcard ){ |
| 4211 | fprintf(out,"#define YYWILDCARD %d\n", |
| 4212 | lemp->wildcard->index); lineno++; |
| 4213 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4214 | print_stack_union(out,lemp,&lineno,mhflag); |
drh | ca44b5a | 2007-02-22 23:06:58 +0000 | [diff] [blame] | 4215 | fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4216 | if( lemp->stacksize ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4217 | fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++; |
| 4218 | }else{ |
| 4219 | fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++; |
| 4220 | } |
drh | ca44b5a | 2007-02-22 23:06:58 +0000 | [diff] [blame] | 4221 | fprintf(out, "#endif\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4222 | if( mhflag ){ |
| 4223 | fprintf(out,"#if INTERFACE\n"); lineno++; |
| 4224 | } |
| 4225 | name = lemp->name ? lemp->name : "Parse"; |
| 4226 | if( lemp->arg && lemp->arg[0] ){ |
drh | 87cf137 | 2008-08-13 20:09:06 +0000 | [diff] [blame] | 4227 | i = lemonStrlen(lemp->arg); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 4228 | while( i>=1 && ISSPACE(lemp->arg[i-1]) ) i--; |
| 4229 | while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4230 | fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++; |
| 4231 | fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++; |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 4232 | fprintf(out,"#define %sARG_PARAM ,%s\n",name,&lemp->arg[i]); lineno++; |
| 4233 | fprintf(out,"#define %sARG_FETCH %s=yypParser->%s;\n", |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4234 | name,lemp->arg,&lemp->arg[i]); lineno++; |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 4235 | fprintf(out,"#define %sARG_STORE yypParser->%s=%s;\n", |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4236 | name,&lemp->arg[i],&lemp->arg[i]); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4237 | }else{ |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 4238 | fprintf(out,"#define %sARG_SDECL\n",name); lineno++; |
| 4239 | fprintf(out,"#define %sARG_PDECL\n",name); lineno++; |
| 4240 | fprintf(out,"#define %sARG_PARAM\n",name); lineno++; |
drh | 1f245e4 | 2002-03-11 13:55:50 +0000 | [diff] [blame] | 4241 | fprintf(out,"#define %sARG_FETCH\n",name); lineno++; |
| 4242 | fprintf(out,"#define %sARG_STORE\n",name); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4243 | } |
drh | fb32c44 | 2018-04-21 13:51:42 +0000 | [diff] [blame] | 4244 | if( lemp->ctx && lemp->ctx[0] ){ |
| 4245 | i = lemonStrlen(lemp->ctx); |
| 4246 | while( i>=1 && ISSPACE(lemp->ctx[i-1]) ) i--; |
| 4247 | while( i>=1 && (ISALNUM(lemp->ctx[i-1]) || lemp->ctx[i-1]=='_') ) i--; |
| 4248 | fprintf(out,"#define %sCTX_SDECL %s;\n",name,lemp->ctx); lineno++; |
| 4249 | fprintf(out,"#define %sCTX_PDECL ,%s\n",name,lemp->ctx); lineno++; |
| 4250 | fprintf(out,"#define %sCTX_PARAM ,%s\n",name,&lemp->ctx[i]); lineno++; |
| 4251 | fprintf(out,"#define %sCTX_FETCH %s=yypParser->%s;\n", |
| 4252 | name,lemp->ctx,&lemp->ctx[i]); lineno++; |
| 4253 | fprintf(out,"#define %sCTX_STORE yypParser->%s=%s;\n", |
| 4254 | name,&lemp->ctx[i],&lemp->ctx[i]); lineno++; |
| 4255 | }else{ |
| 4256 | fprintf(out,"#define %sCTX_SDECL\n",name); lineno++; |
| 4257 | fprintf(out,"#define %sCTX_PDECL\n",name); lineno++; |
| 4258 | fprintf(out,"#define %sCTX_PARAM\n",name); lineno++; |
| 4259 | fprintf(out,"#define %sCTX_FETCH\n",name); lineno++; |
| 4260 | fprintf(out,"#define %sCTX_STORE\n",name); lineno++; |
| 4261 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4262 | if( mhflag ){ |
| 4263 | fprintf(out,"#endif\n"); lineno++; |
| 4264 | } |
drh | ed0c15b | 2018-04-16 14:31:34 +0000 | [diff] [blame] | 4265 | if( lemp->errsym && lemp->errsym->useCnt ){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4266 | fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++; |
| 4267 | fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++; |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4268 | } |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4269 | if( lemp->has_fallback ){ |
| 4270 | fprintf(out,"#define YYFALLBACK 1\n"); lineno++; |
| 4271 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4272 | |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4273 | /* Compute the action table, but do not output it yet. The action |
| 4274 | ** table must be computed before generating the YYNSTATE macro because |
| 4275 | ** we need to know how many states can be eliminated. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4276 | */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4277 | ax = (struct axset *) calloc(lemp->nxstate*2, sizeof(ax[0])); |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4278 | if( ax==0 ){ |
| 4279 | fprintf(stderr,"malloc failed\n"); |
| 4280 | exit(1); |
| 4281 | } |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4282 | for(i=0; i<lemp->nxstate; i++){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4283 | stp = lemp->sorted[i]; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4284 | ax[i*2].stp = stp; |
| 4285 | ax[i*2].isTkn = 1; |
| 4286 | ax[i*2].nAction = stp->nTknAct; |
| 4287 | ax[i*2+1].stp = stp; |
| 4288 | ax[i*2+1].isTkn = 0; |
| 4289 | ax[i*2+1].nAction = stp->nNtAct; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4290 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4291 | mxTknOfst = mnTknOfst = 0; |
| 4292 | mxNtOfst = mnNtOfst = 0; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4293 | /* In an effort to minimize the action table size, use the heuristic |
| 4294 | ** of placing the largest action sets first */ |
| 4295 | for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i; |
| 4296 | qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4297 | pActtab = acttab_alloc(lemp->nsymbol, lemp->nterminal); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4298 | for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){ |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4299 | stp = ax[i].stp; |
| 4300 | if( ax[i].isTkn ){ |
| 4301 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4302 | int action; |
| 4303 | if( ap->sp->index>=lemp->nterminal ) continue; |
| 4304 | action = compute_action(lemp, ap); |
| 4305 | if( action<0 ) continue; |
| 4306 | acttab_action(pActtab, ap->sp->index, action); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4307 | } |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4308 | stp->iTknOfst = acttab_insert(pActtab, 1); |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4309 | if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst; |
| 4310 | if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst; |
| 4311 | }else{ |
| 4312 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4313 | int action; |
| 4314 | if( ap->sp->index<lemp->nterminal ) continue; |
| 4315 | if( ap->sp->index==lemp->nsymbol ) continue; |
| 4316 | action = compute_action(lemp, ap); |
| 4317 | if( action<0 ) continue; |
| 4318 | acttab_action(pActtab, ap->sp->index, action); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4319 | } |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4320 | stp->iNtOfst = acttab_insert(pActtab, 0); |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4321 | if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst; |
| 4322 | if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4323 | } |
drh | 337cd0d | 2015-09-07 23:40:42 +0000 | [diff] [blame] | 4324 | #if 0 /* Uncomment for a trace of how the yy_action[] table fills out */ |
| 4325 | { int jj, nn; |
| 4326 | for(jj=nn=0; jj<pActtab->nAction; jj++){ |
| 4327 | if( pActtab->aAction[jj].action<0 ) nn++; |
| 4328 | } |
| 4329 | printf("%4d: State %3d %s n: %2d size: %5d freespace: %d\n", |
| 4330 | i, stp->statenum, ax[i].isTkn ? "Token" : "Var ", |
| 4331 | ax[i].nAction, pActtab->nAction, nn); |
| 4332 | } |
| 4333 | #endif |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4334 | } |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4335 | free(ax); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4336 | |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4337 | /* Mark rules that are actually used for reduce actions after all |
| 4338 | ** optimizations have been applied |
| 4339 | */ |
| 4340 | for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE; |
| 4341 | for(i=0; i<lemp->nxstate; i++){ |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4342 | for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){ |
| 4343 | if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){ |
drh | 69bfa55 | 2017-04-26 04:32:17 +0000 | [diff] [blame] | 4344 | ap->x.rp->doesReduce = 1; |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4345 | } |
| 4346 | } |
| 4347 | } |
| 4348 | |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4349 | /* Finish rendering the constants now that the action table has |
| 4350 | ** been computed */ |
| 4351 | fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++; |
| 4352 | fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++; |
drh | 0d9de99 | 2017-12-26 18:04:23 +0000 | [diff] [blame] | 4353 | fprintf(out,"#define YYNTOKEN %d\n",lemp->nterminal); lineno++; |
drh | 337cd0d | 2015-09-07 23:40:42 +0000 | [diff] [blame] | 4354 | fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++; |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4355 | i = lemp->minShiftReduce; |
| 4356 | fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",i); lineno++; |
| 4357 | i += lemp->nrule; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4358 | fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++; |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4359 | fprintf(out,"#define YY_ERROR_ACTION %d\n", lemp->errAction); lineno++; |
| 4360 | fprintf(out,"#define YY_ACCEPT_ACTION %d\n", lemp->accAction); lineno++; |
| 4361 | fprintf(out,"#define YY_NO_ACTION %d\n", lemp->noAction); lineno++; |
| 4362 | fprintf(out,"#define YY_MIN_REDUCE %d\n", lemp->minReduce); lineno++; |
| 4363 | i = lemp->minReduce + lemp->nrule; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4364 | fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4365 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4366 | |
| 4367 | /* Now output the action table and its associates: |
| 4368 | ** |
| 4369 | ** yy_action[] A single table containing all actions. |
| 4370 | ** yy_lookahead[] A table containing the lookahead for each entry in |
| 4371 | ** yy_action. Used to detect hash collisions. |
| 4372 | ** yy_shift_ofst[] For each state, the offset into yy_action for |
| 4373 | ** shifting terminals. |
| 4374 | ** yy_reduce_ofst[] For each state, the offset into yy_action for |
| 4375 | ** shifting non-terminals after a reduce. |
| 4376 | ** yy_default[] Default action for each state. |
| 4377 | */ |
| 4378 | |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4379 | /* Output the yy_action table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4380 | lemp->nactiontab = n = acttab_action_size(pActtab); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4381 | lemp->tablesize += n*szActionType; |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 4382 | fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++; |
| 4383 | fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4384 | for(i=j=0; i<n; i++){ |
| 4385 | int action = acttab_yyaction(pActtab, i); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4386 | if( action<0 ) action = lemp->noAction; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4387 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4388 | fprintf(out, " %4d,", action); |
| 4389 | if( j==9 || i==n-1 ){ |
| 4390 | fprintf(out, "\n"); lineno++; |
| 4391 | j = 0; |
| 4392 | }else{ |
| 4393 | j++; |
| 4394 | } |
| 4395 | } |
| 4396 | fprintf(out, "};\n"); lineno++; |
| 4397 | |
| 4398 | /* Output the yy_lookahead table */ |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4399 | lemp->nlookaheadtab = n = acttab_lookahead_size(pActtab); |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4400 | lemp->tablesize += n*szCodeType; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 4401 | fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4402 | for(i=j=0; i<n; i++){ |
| 4403 | int la = acttab_yylookahead(pActtab, i); |
| 4404 | if( la<0 ) la = lemp->nsymbol; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4405 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4406 | fprintf(out, " %4d,", la); |
drh | 2e51716 | 2019-08-28 02:09:47 +0000 | [diff] [blame] | 4407 | if( j==9 ){ |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4408 | fprintf(out, "\n"); lineno++; |
| 4409 | j = 0; |
| 4410 | }else{ |
| 4411 | j++; |
| 4412 | } |
| 4413 | } |
drh | 2e51716 | 2019-08-28 02:09:47 +0000 | [diff] [blame] | 4414 | /* Add extra entries to the end of the yy_lookahead[] table so that |
| 4415 | ** yy_shift_ofst[]+iToken will always be a valid index into the array, |
| 4416 | ** even for the largest possible value of yy_shift_ofst[] and iToken. */ |
| 4417 | nLookAhead = lemp->nterminal + lemp->nactiontab; |
| 4418 | while( i<nLookAhead ){ |
| 4419 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
| 4420 | fprintf(out, " %4d,", lemp->nterminal); |
| 4421 | if( j==9 ){ |
| 4422 | fprintf(out, "\n"); lineno++; |
| 4423 | j = 0; |
| 4424 | }else{ |
| 4425 | j++; |
| 4426 | } |
| 4427 | i++; |
| 4428 | } |
| 4429 | if( j>0 ) fprintf(out, "\n"); lineno++; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4430 | fprintf(out, "};\n"); lineno++; |
| 4431 | |
| 4432 | /* Output the yy_shift_ofst[] table */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4433 | n = lemp->nxstate; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4434 | while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--; |
drh | 701b688 | 2016-08-10 13:30:43 +0000 | [diff] [blame] | 4435 | fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++; |
| 4436 | fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++; |
| 4437 | fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4438 | fprintf(out, "static const %s yy_shift_ofst[] = {\n", |
drh | 701b688 | 2016-08-10 13:30:43 +0000 | [diff] [blame] | 4439 | minimum_size_type(mnTknOfst, lemp->nterminal+lemp->nactiontab, &sz)); |
| 4440 | lineno++; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4441 | lemp->tablesize += n*sz; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4442 | for(i=j=0; i<n; i++){ |
| 4443 | int ofst; |
| 4444 | stp = lemp->sorted[i]; |
| 4445 | ofst = stp->iTknOfst; |
drh | 701b688 | 2016-08-10 13:30:43 +0000 | [diff] [blame] | 4446 | if( ofst==NO_OFFSET ) ofst = lemp->nactiontab; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4447 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4448 | fprintf(out, " %4d,", ofst); |
| 4449 | if( j==9 || i==n-1 ){ |
| 4450 | fprintf(out, "\n"); lineno++; |
| 4451 | j = 0; |
| 4452 | }else{ |
| 4453 | j++; |
| 4454 | } |
| 4455 | } |
| 4456 | fprintf(out, "};\n"); lineno++; |
| 4457 | |
| 4458 | /* Output the yy_reduce_ofst[] table */ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4459 | n = lemp->nxstate; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4460 | while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--; |
drh | f16371d | 2009-11-03 19:18:31 +0000 | [diff] [blame] | 4461 | fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++; |
| 4462 | fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++; |
| 4463 | fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++; |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4464 | fprintf(out, "static const %s yy_reduce_ofst[] = {\n", |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4465 | minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++; |
| 4466 | lemp->tablesize += n*sz; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4467 | for(i=j=0; i<n; i++){ |
| 4468 | int ofst; |
| 4469 | stp = lemp->sorted[i]; |
| 4470 | ofst = stp->iNtOfst; |
| 4471 | if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4472 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4473 | fprintf(out, " %4d,", ofst); |
| 4474 | if( j==9 || i==n-1 ){ |
| 4475 | fprintf(out, "\n"); lineno++; |
| 4476 | j = 0; |
| 4477 | }else{ |
| 4478 | j++; |
| 4479 | } |
| 4480 | } |
| 4481 | fprintf(out, "};\n"); lineno++; |
| 4482 | |
| 4483 | /* Output the default action table */ |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 4484 | fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++; |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4485 | n = lemp->nxstate; |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4486 | lemp->tablesize += n*szActionType; |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4487 | for(i=j=0; i<n; i++){ |
| 4488 | stp = lemp->sorted[i]; |
drh | fdbf928 | 2003-10-21 16:34:41 +0000 | [diff] [blame] | 4489 | if( j==0 ) fprintf(out," /* %5d */ ", i); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4490 | if( stp->iDfltReduce<0 ){ |
| 4491 | fprintf(out, " %4d,", lemp->errAction); |
| 4492 | }else{ |
| 4493 | fprintf(out, " %4d,", stp->iDfltReduce + lemp->minReduce); |
| 4494 | } |
drh | 8b58201 | 2003-10-21 13:16:03 +0000 | [diff] [blame] | 4495 | if( j==9 || i==n-1 ){ |
| 4496 | fprintf(out, "\n"); lineno++; |
| 4497 | j = 0; |
| 4498 | }else{ |
| 4499 | j++; |
| 4500 | } |
| 4501 | } |
| 4502 | fprintf(out, "};\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4503 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4504 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4505 | /* Generate the table of fallback tokens. |
| 4506 | */ |
| 4507 | if( lemp->has_fallback ){ |
drh | 1441f3e | 2009-06-12 12:50:50 +0000 | [diff] [blame] | 4508 | int mx = lemp->nterminal - 1; |
drh | 010bdb4 | 2019-08-28 11:31:11 +0000 | [diff] [blame^] | 4509 | /* 2019-08-28: Generate fallback entries for every token to avoid |
| 4510 | ** having to do a range check on the index */ |
| 4511 | /* while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; } */ |
drh | c75e016 | 2015-09-07 02:23:02 +0000 | [diff] [blame] | 4512 | lemp->tablesize += (mx+1)*szCodeType; |
drh | 1441f3e | 2009-06-12 12:50:50 +0000 | [diff] [blame] | 4513 | for(i=0; i<=mx; i++){ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4514 | struct symbol *p = lemp->symbols[i]; |
| 4515 | if( p->fallback==0 ){ |
| 4516 | fprintf(out, " 0, /* %10s => nothing */\n", p->name); |
| 4517 | }else{ |
| 4518 | fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index, |
| 4519 | p->name, p->fallback->name); |
| 4520 | } |
| 4521 | lineno++; |
| 4522 | } |
| 4523 | } |
| 4524 | tplt_xfer(lemp->name, in, out, &lineno); |
| 4525 | |
| 4526 | /* Generate a table containing the symbolic name of every symbol |
| 4527 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4528 | for(i=0; i<lemp->nsymbol; i++){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 4529 | lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name); |
drh | 3a9d6c7 | 2017-12-25 04:15:38 +0000 | [diff] [blame] | 4530 | fprintf(out," /* %4d */ \"%s\",\n",i, lemp->symbols[i]->name); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4531 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4532 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4533 | |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4534 | /* Generate a table containing a text string that describes every |
drh | 34ff57b | 2008-07-14 12:27:51 +0000 | [diff] [blame] | 4535 | ** rule in the rule set of the grammar. This information is used |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4536 | ** when tracing REDUCE actions. |
| 4537 | */ |
| 4538 | for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4539 | assert( rp->iRule==i ); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4540 | fprintf(out," /* %3d */ \"", i); |
| 4541 | writeRuleText(out, rp); |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4542 | fprintf(out,"\",\n"); lineno++; |
| 4543 | } |
| 4544 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4545 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4546 | /* Generate code which executes every time a symbol is popped from |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4547 | ** the stack while processing errors or while destroying the parser. |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 4548 | ** (In other words, generate the %destructor actions) |
| 4549 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4550 | if( lemp->tokendest ){ |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4551 | int once = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4552 | for(i=0; i<lemp->nsymbol; i++){ |
| 4553 | struct symbol *sp = lemp->symbols[i]; |
| 4554 | if( sp==0 || sp->type!=TERMINAL ) continue; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4555 | if( once ){ |
| 4556 | fprintf(out, " /* TERMINAL Destructor */\n"); lineno++; |
| 4557 | once = 0; |
| 4558 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4559 | fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4560 | } |
| 4561 | for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++); |
| 4562 | if( i<lemp->nsymbol ){ |
| 4563 | emit_destructor_code(out,lemp->symbols[i],lemp,&lineno); |
| 4564 | fprintf(out," break;\n"); lineno++; |
| 4565 | } |
| 4566 | } |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4567 | if( lemp->vardest ){ |
| 4568 | struct symbol *dflt_sp = 0; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4569 | int once = 1; |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4570 | for(i=0; i<lemp->nsymbol; i++){ |
| 4571 | struct symbol *sp = lemp->symbols[i]; |
| 4572 | if( sp==0 || sp->type==TERMINAL || |
| 4573 | sp->index<=0 || sp->destructor!=0 ) continue; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4574 | if( once ){ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4575 | fprintf(out, " /* Default NON-TERMINAL Destructor */\n");lineno++; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4576 | once = 0; |
| 4577 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4578 | fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4579 | dflt_sp = sp; |
| 4580 | } |
| 4581 | if( dflt_sp!=0 ){ |
| 4582 | emit_destructor_code(out,dflt_sp,lemp,&lineno); |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4583 | } |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 4584 | fprintf(out," break;\n"); lineno++; |
drh | 8d65973 | 2005-01-13 23:54:06 +0000 | [diff] [blame] | 4585 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4586 | for(i=0; i<lemp->nsymbol; i++){ |
| 4587 | struct symbol *sp = lemp->symbols[i]; |
| 4588 | if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue; |
drh | 0f832dd | 2016-08-16 16:46:40 +0000 | [diff] [blame] | 4589 | if( sp->destLineno<0 ) continue; /* Already emitted */ |
drh | 7501301 | 2009-06-12 15:47:34 +0000 | [diff] [blame] | 4590 | fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4591 | |
| 4592 | /* Combine duplicate destructors into a single case */ |
| 4593 | for(j=i+1; j<lemp->nsymbol; j++){ |
| 4594 | struct symbol *sp2 = lemp->symbols[j]; |
| 4595 | if( sp2 && sp2->type!=TERMINAL && sp2->destructor |
| 4596 | && sp2->dtnum==sp->dtnum |
| 4597 | && strcmp(sp->destructor,sp2->destructor)==0 ){ |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4598 | fprintf(out," case %d: /* %s */\n", |
| 4599 | sp2->index, sp2->name); lineno++; |
drh | 0f832dd | 2016-08-16 16:46:40 +0000 | [diff] [blame] | 4600 | sp2->destLineno = -1; /* Avoid emitting this destructor again */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4601 | } |
| 4602 | } |
| 4603 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4604 | emit_destructor_code(out,lemp->symbols[i],lemp,&lineno); |
| 4605 | fprintf(out," break;\n"); lineno++; |
| 4606 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4607 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4608 | |
| 4609 | /* Generate code which executes whenever the parser stack overflows */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4610 | tplt_print(out,lemp,lemp->overflow,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4611 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4612 | |
drh | cfc45b1 | 2018-12-03 23:57:27 +0000 | [diff] [blame] | 4613 | /* Generate the tables of rule information. yyRuleInfoLhs[] and |
| 4614 | ** yyRuleInfoNRhs[]. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4615 | ** |
| 4616 | ** Note: This code depends on the fact that rules are number |
| 4617 | ** sequentually beginning with 0. |
| 4618 | */ |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4619 | for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ |
drh | cfc45b1 | 2018-12-03 23:57:27 +0000 | [diff] [blame] | 4620 | fprintf(out," %4d, /* (%d) ", rp->lhs->index, i); |
| 4621 | rule_print(out, rp); |
| 4622 | fprintf(out," */\n"); lineno++; |
| 4623 | } |
| 4624 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4625 | for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){ |
| 4626 | fprintf(out," %3d, /* (%d) ", -rp->nrhs, i); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4627 | rule_print(out, rp); |
| 4628 | fprintf(out," */\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4629 | } |
| 4630 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4631 | |
| 4632 | /* Generate code which execution during each REDUCE action */ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 4633 | i = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4634 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | dabd04c | 2016-02-17 01:46:19 +0000 | [diff] [blame] | 4635 | i += translate_code(lemp, rp); |
| 4636 | } |
| 4637 | if( i ){ |
| 4638 | fprintf(out," YYMINORTYPE yylhsminor;\n"); lineno++; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4639 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4640 | /* First output rules other than the default: rule */ |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4641 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4642 | struct rule *rp2; /* Other rules with the same action */ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4643 | if( rp->codeEmitted ) continue; |
| 4644 | if( rp->noCode ){ |
| 4645 | /* No C code actions, so this will be part of the "default:" rule */ |
drh | 2e55b04 | 2016-04-30 17:19:30 +0000 | [diff] [blame] | 4646 | continue; |
| 4647 | } |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4648 | fprintf(out," case %d: /* ", rp->iRule); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4649 | writeRuleText(out, rp); |
| 4650 | fprintf(out, " */\n"); lineno++; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4651 | for(rp2=rp->next; rp2; rp2=rp2->next){ |
drh | afb8cd9 | 2016-04-29 11:28:35 +0000 | [diff] [blame] | 4652 | if( rp2->code==rp->code && rp2->codePrefix==rp->codePrefix |
| 4653 | && rp2->codeSuffix==rp->codeSuffix ){ |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4654 | fprintf(out," case %d: /* ", rp2->iRule); |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 4655 | writeRuleText(out, rp2); |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4656 | fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->iRule); lineno++; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4657 | rp2->codeEmitted = 1; |
drh | 0bb132b | 2004-07-20 14:06:51 +0000 | [diff] [blame] | 4658 | } |
| 4659 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4660 | emit_code(out,rp,lemp,&lineno); |
| 4661 | fprintf(out," break;\n"); lineno++; |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4662 | rp->codeEmitted = 1; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4663 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4664 | /* Finally, output the default: rule. We choose as the default: all |
| 4665 | ** empty actions. */ |
| 4666 | fprintf(out," default:\n"); lineno++; |
| 4667 | for(rp=lemp->rule; rp; rp=rp->next){ |
drh | 711c981 | 2016-05-23 14:24:31 +0000 | [diff] [blame] | 4668 | if( rp->codeEmitted ) continue; |
| 4669 | assert( rp->noCode ); |
drh | 4ef0770 | 2016-03-16 19:45:54 +0000 | [diff] [blame] | 4670 | fprintf(out," /* (%d) ", rp->iRule); |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4671 | writeRuleText(out, rp); |
drh | 756b41e | 2016-05-24 18:55:08 +0000 | [diff] [blame] | 4672 | if( rp->doesReduce ){ |
| 4673 | fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->iRule); lineno++; |
| 4674 | }else{ |
| 4675 | fprintf(out, " (OPTIMIZED OUT) */ assert(yyruleno!=%d);\n", |
| 4676 | rp->iRule); lineno++; |
| 4677 | } |
drh | c53eed1 | 2009-06-12 17:46:19 +0000 | [diff] [blame] | 4678 | } |
| 4679 | fprintf(out," break;\n"); lineno++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4680 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4681 | |
| 4682 | /* Generate code which executes if a parse fails */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4683 | tplt_print(out,lemp,lemp->failure,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4684 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4685 | |
| 4686 | /* Generate code which executes when a syntax error occurs */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4687 | tplt_print(out,lemp,lemp->error,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4688 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4689 | |
| 4690 | /* Generate code which executes when the parser accepts its input */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4691 | tplt_print(out,lemp,lemp->accept,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4692 | tplt_xfer(lemp->name,in,out,&lineno); |
| 4693 | |
| 4694 | /* Append any addition code the user desires */ |
drh | a5808f3 | 2008-04-27 22:19:44 +0000 | [diff] [blame] | 4695 | tplt_print(out,lemp,lemp->extracode,&lineno); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4696 | |
drh | e2dcc42 | 2019-01-15 14:44:23 +0000 | [diff] [blame] | 4697 | acttab_free(pActtab); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4698 | fclose(in); |
| 4699 | fclose(out); |
| 4700 | return; |
| 4701 | } |
| 4702 | |
| 4703 | /* Generate a header file for the parser */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4704 | void ReportHeader(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4705 | { |
| 4706 | FILE *out, *in; |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4707 | const char *prefix; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4708 | char line[LINESIZE]; |
| 4709 | char pattern[LINESIZE]; |
| 4710 | int i; |
| 4711 | |
| 4712 | if( lemp->tokenprefix ) prefix = lemp->tokenprefix; |
| 4713 | else prefix = ""; |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 4714 | in = file_open(lemp,".h","rb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4715 | if( in ){ |
drh | 8ba0d1c | 2012-06-16 15:26:31 +0000 | [diff] [blame] | 4716 | int nextChar; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4717 | for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4718 | lemon_sprintf(pattern,"#define %s%-30s %3d\n", |
| 4719 | prefix,lemp->symbols[i]->name,i); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4720 | if( strcmp(line,pattern) ) break; |
| 4721 | } |
drh | 8ba0d1c | 2012-06-16 15:26:31 +0000 | [diff] [blame] | 4722 | nextChar = fgetc(in); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4723 | fclose(in); |
drh | 8ba0d1c | 2012-06-16 15:26:31 +0000 | [diff] [blame] | 4724 | if( i==lemp->nterminal && nextChar==EOF ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4725 | /* No change in the file. Don't rewrite it. */ |
| 4726 | return; |
| 4727 | } |
| 4728 | } |
drh | 2aa6ca4 | 2004-09-10 00:14:04 +0000 | [diff] [blame] | 4729 | out = file_open(lemp,".h","wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4730 | if( out ){ |
| 4731 | for(i=1; i<lemp->nterminal; i++){ |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 4732 | fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4733 | } |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4734 | fclose(out); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4735 | } |
| 4736 | return; |
| 4737 | } |
| 4738 | |
| 4739 | /* Reduce the size of the action tables, if possible, by making use |
| 4740 | ** of defaults. |
| 4741 | ** |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4742 | ** In this version, we take the most frequent REDUCE action and make |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4743 | ** it the default. Except, there is no default if the wildcard token |
| 4744 | ** is a possible look-ahead. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4745 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4746 | void CompressTables(struct lemon *lemp) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4747 | { |
| 4748 | struct state *stp; |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 4749 | struct action *ap, *ap2, *nextap; |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4750 | struct rule *rp, *rp2, *rbest; |
drh | 0c6dfaa | 2015-09-08 21:16:46 +0000 | [diff] [blame] | 4751 | int nbest, n; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4752 | int i; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4753 | int usesWildcard; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4754 | |
| 4755 | for(i=0; i<lemp->nstate; i++){ |
| 4756 | stp = lemp->sorted[i]; |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4757 | nbest = 0; |
| 4758 | rbest = 0; |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4759 | usesWildcard = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4760 | |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4761 | for(ap=stp->ap; ap; ap=ap->next){ |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4762 | if( ap->type==SHIFT && ap->sp==lemp->wildcard ){ |
| 4763 | usesWildcard = 1; |
| 4764 | } |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4765 | if( ap->type!=REDUCE ) continue; |
| 4766 | rp = ap->x.rp; |
drh | b496099 | 2007-10-05 16:16:36 +0000 | [diff] [blame] | 4767 | if( rp->lhsStart ) continue; |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4768 | if( rp==rbest ) continue; |
| 4769 | n = 1; |
| 4770 | for(ap2=ap->next; ap2; ap2=ap2->next){ |
| 4771 | if( ap2->type!=REDUCE ) continue; |
| 4772 | rp2 = ap2->x.rp; |
| 4773 | if( rp2==rbest ) continue; |
| 4774 | if( rp2==rp ) n++; |
| 4775 | } |
| 4776 | if( n>nbest ){ |
| 4777 | nbest = n; |
| 4778 | rbest = rp; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4779 | } |
| 4780 | } |
drh | 06f60d8 | 2017-04-14 19:46:12 +0000 | [diff] [blame] | 4781 | |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4782 | /* Do not make a default if the number of rules to default |
drh | e09daa9 | 2006-06-10 13:29:31 +0000 | [diff] [blame] | 4783 | ** is not at least 1 or if the wildcard token is a possible |
| 4784 | ** lookahead. |
| 4785 | */ |
| 4786 | if( nbest<1 || usesWildcard ) continue; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4787 | |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4788 | |
| 4789 | /* Combine matching REDUCE actions into a single default */ |
| 4790 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4791 | if( ap->type==REDUCE && ap->x.rp==rbest ) break; |
| 4792 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4793 | assert( ap ); |
| 4794 | ap->sp = Symbol_new("{default}"); |
| 4795 | for(ap=ap->next; ap; ap=ap->next){ |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4796 | if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4797 | } |
| 4798 | stp->ap = Action_sort(stp->ap); |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4799 | |
| 4800 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4801 | if( ap->type==SHIFT ) break; |
| 4802 | if( ap->type==REDUCE && ap->x.rp!=rbest ) break; |
| 4803 | } |
| 4804 | if( ap==0 ){ |
| 4805 | stp->autoReduce = 1; |
| 4806 | stp->pDfltReduce = rbest; |
| 4807 | } |
| 4808 | } |
| 4809 | |
| 4810 | /* Make a second pass over all states and actions. Convert |
| 4811 | ** every action that is a SHIFT to an autoReduce state into |
| 4812 | ** a SHIFTREDUCE action. |
| 4813 | */ |
| 4814 | for(i=0; i<lemp->nstate; i++){ |
| 4815 | stp = lemp->sorted[i]; |
| 4816 | for(ap=stp->ap; ap; ap=ap->next){ |
| 4817 | struct state *pNextState; |
| 4818 | if( ap->type!=SHIFT ) continue; |
| 4819 | pNextState = ap->x.stp; |
| 4820 | if( pNextState->autoReduce && pNextState->pDfltReduce!=0 ){ |
| 4821 | ap->type = SHIFTREDUCE; |
| 4822 | ap->x.rp = pNextState->pDfltReduce; |
| 4823 | } |
| 4824 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4825 | } |
drh | c173ad8 | 2016-05-23 16:15:02 +0000 | [diff] [blame] | 4826 | |
| 4827 | /* If a SHIFTREDUCE action specifies a rule that has a single RHS term |
| 4828 | ** (meaning that the SHIFTREDUCE will land back in the state where it |
| 4829 | ** started) and if there is no C-code associated with the reduce action, |
| 4830 | ** then we can go ahead and convert the action to be the same as the |
| 4831 | ** action for the RHS of the rule. |
| 4832 | */ |
| 4833 | for(i=0; i<lemp->nstate; i++){ |
| 4834 | stp = lemp->sorted[i]; |
| 4835 | for(ap=stp->ap; ap; ap=nextap){ |
| 4836 | nextap = ap->next; |
| 4837 | if( ap->type!=SHIFTREDUCE ) continue; |
| 4838 | rp = ap->x.rp; |
| 4839 | if( rp->noCode==0 ) continue; |
| 4840 | if( rp->nrhs!=1 ) continue; |
| 4841 | #if 1 |
| 4842 | /* Only apply this optimization to non-terminals. It would be OK to |
| 4843 | ** apply it to terminal symbols too, but that makes the parser tables |
| 4844 | ** larger. */ |
| 4845 | if( ap->sp->index<lemp->nterminal ) continue; |
| 4846 | #endif |
| 4847 | /* If we reach this point, it means the optimization can be applied */ |
| 4848 | nextap = ap; |
| 4849 | for(ap2=stp->ap; ap2 && (ap2==ap || ap2->sp!=rp->lhs); ap2=ap2->next){} |
| 4850 | assert( ap2!=0 ); |
| 4851 | ap->spOpt = ap2->sp; |
| 4852 | ap->type = ap2->type; |
| 4853 | ap->x = ap2->x; |
| 4854 | } |
| 4855 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4856 | } |
drh | b59499c | 2002-02-23 18:45:13 +0000 | [diff] [blame] | 4857 | |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4858 | |
| 4859 | /* |
| 4860 | ** Compare two states for sorting purposes. The smaller state is the |
| 4861 | ** one with the most non-terminal actions. If they have the same number |
| 4862 | ** of non-terminal actions, then the smaller is the one with the most |
| 4863 | ** token actions. |
| 4864 | */ |
| 4865 | static int stateResortCompare(const void *a, const void *b){ |
| 4866 | const struct state *pA = *(const struct state**)a; |
| 4867 | const struct state *pB = *(const struct state**)b; |
| 4868 | int n; |
| 4869 | |
| 4870 | n = pB->nNtAct - pA->nNtAct; |
| 4871 | if( n==0 ){ |
| 4872 | n = pB->nTknAct - pA->nTknAct; |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4873 | if( n==0 ){ |
| 4874 | n = pB->statenum - pA->statenum; |
| 4875 | } |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4876 | } |
drh | e594bc3 | 2009-11-03 13:02:25 +0000 | [diff] [blame] | 4877 | assert( n!=0 ); |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4878 | return n; |
| 4879 | } |
| 4880 | |
| 4881 | |
| 4882 | /* |
| 4883 | ** Renumber and resort states so that states with fewer choices |
| 4884 | ** occur at the end. Except, keep state 0 as the first state. |
| 4885 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4886 | void ResortStates(struct lemon *lemp) |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4887 | { |
| 4888 | int i; |
| 4889 | struct state *stp; |
| 4890 | struct action *ap; |
| 4891 | |
| 4892 | for(i=0; i<lemp->nstate; i++){ |
| 4893 | stp = lemp->sorted[i]; |
| 4894 | stp->nTknAct = stp->nNtAct = 0; |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4895 | stp->iDfltReduce = -1; /* Init dflt action to "syntax error" */ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4896 | stp->iTknOfst = NO_OFFSET; |
| 4897 | stp->iNtOfst = NO_OFFSET; |
| 4898 | for(ap=stp->ap; ap; ap=ap->next){ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4899 | int iAction = compute_action(lemp,ap); |
| 4900 | if( iAction>=0 ){ |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4901 | if( ap->sp->index<lemp->nterminal ){ |
| 4902 | stp->nTknAct++; |
| 4903 | }else if( ap->sp->index<lemp->nsymbol ){ |
| 4904 | stp->nNtAct++; |
| 4905 | }else{ |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4906 | assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp ); |
drh | 5c8241b | 2017-12-24 23:38:10 +0000 | [diff] [blame] | 4907 | stp->iDfltReduce = iAction; |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4908 | } |
| 4909 | } |
| 4910 | } |
| 4911 | } |
| 4912 | qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]), |
| 4913 | stateResortCompare); |
| 4914 | for(i=0; i<lemp->nstate; i++){ |
| 4915 | lemp->sorted[i]->statenum = i; |
| 4916 | } |
drh | 3bd48ab | 2015-09-07 18:23:37 +0000 | [diff] [blame] | 4917 | lemp->nxstate = lemp->nstate; |
| 4918 | while( lemp->nxstate>1 && lemp->sorted[lemp->nxstate-1]->autoReduce ){ |
| 4919 | lemp->nxstate--; |
| 4920 | } |
drh | ada354d | 2005-11-05 15:03:59 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4924 | /***************** From the file "set.c" ************************************/ |
| 4925 | /* |
| 4926 | ** Set manipulation routines for the LEMON parser generator. |
| 4927 | */ |
| 4928 | |
| 4929 | static int size = 0; |
| 4930 | |
| 4931 | /* Set the set size */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4932 | void SetSize(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4933 | { |
| 4934 | size = n+1; |
| 4935 | } |
| 4936 | |
| 4937 | /* Allocate a new set */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 4938 | char *SetNew(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4939 | char *s; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 4940 | s = (char*)calloc( size, 1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4941 | if( s==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4942 | memory_error(); |
| 4943 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4944 | return s; |
| 4945 | } |
| 4946 | |
| 4947 | /* Deallocate a set */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4948 | void SetFree(char *s) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4949 | { |
| 4950 | free(s); |
| 4951 | } |
| 4952 | |
| 4953 | /* Add a new element to the set. Return TRUE if the element was added |
| 4954 | ** and FALSE if it was already there. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4955 | int SetAdd(char *s, int e) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4956 | { |
| 4957 | int rv; |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 4958 | assert( e>=0 && e<size ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4959 | rv = s[e]; |
| 4960 | s[e] = 1; |
| 4961 | return !rv; |
| 4962 | } |
| 4963 | |
| 4964 | /* Add every element of s2 to s1. Return TRUE if s1 changes. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 4965 | int SetUnion(char *s1, char *s2) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4966 | { |
| 4967 | int i, progress; |
| 4968 | progress = 0; |
| 4969 | for(i=0; i<size; i++){ |
| 4970 | if( s2[i]==0 ) continue; |
| 4971 | if( s1[i]==0 ){ |
| 4972 | progress = 1; |
| 4973 | s1[i] = 1; |
| 4974 | } |
| 4975 | } |
| 4976 | return progress; |
| 4977 | } |
| 4978 | /********************** From the file "table.c" ****************************/ |
| 4979 | /* |
| 4980 | ** All code in this file has been automatically generated |
| 4981 | ** from a specification in the file |
| 4982 | ** "table.q" |
| 4983 | ** by the associative array code building program "aagen". |
| 4984 | ** Do not edit this file! Instead, edit the specification |
| 4985 | ** file, then rerun aagen. |
| 4986 | */ |
| 4987 | /* |
| 4988 | ** Code for processing tables in the LEMON parser generator. |
| 4989 | */ |
| 4990 | |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 4991 | PRIVATE unsigned strhash(const char *x) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4992 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 4993 | unsigned h = 0; |
| 4994 | while( *x ) h = h*13 + *(x++); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4995 | return h; |
| 4996 | } |
| 4997 | |
| 4998 | /* Works like strdup, sort of. Save a string in malloced memory, but |
| 4999 | ** keep strings in a table so that the same string is not in more |
| 5000 | ** than one place. |
| 5001 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5002 | const char *Strsafe(const char *y) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5003 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5004 | const char *z; |
| 5005 | char *cpy; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5006 | |
drh | 916f75f | 2006-07-17 00:19:39 +0000 | [diff] [blame] | 5007 | if( y==0 ) return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5008 | z = Strsafe_find(y); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5009 | if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){ |
drh | 898799f | 2014-01-10 23:21:00 +0000 | [diff] [blame] | 5010 | lemon_strcpy(cpy,y); |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5011 | z = cpy; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5012 | Strsafe_insert(z); |
| 5013 | } |
| 5014 | MemoryCheck(z); |
| 5015 | return z; |
| 5016 | } |
| 5017 | |
| 5018 | /* There is one instance of the following structure for each |
| 5019 | ** associative array of type "x1". |
| 5020 | */ |
| 5021 | struct s_x1 { |
| 5022 | int size; /* The number of available slots. */ |
| 5023 | /* Must be a power of 2 greater than or */ |
| 5024 | /* equal to 1 */ |
| 5025 | int count; /* Number of currently slots filled */ |
| 5026 | struct s_x1node *tbl; /* The data stored here */ |
| 5027 | struct s_x1node **ht; /* Hash table for lookups */ |
| 5028 | }; |
| 5029 | |
| 5030 | /* There is one instance of this structure for every data element |
| 5031 | ** in an associative array of type "x1". |
| 5032 | */ |
| 5033 | typedef struct s_x1node { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5034 | const char *data; /* The data */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5035 | struct s_x1node *next; /* Next entry with the same hash */ |
| 5036 | struct s_x1node **from; /* Previous link */ |
| 5037 | } x1node; |
| 5038 | |
| 5039 | /* There is only one instance of the array, which is the following */ |
| 5040 | static struct s_x1 *x1a; |
| 5041 | |
| 5042 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5043 | void Strsafe_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5044 | if( x1a ) return; |
| 5045 | x1a = (struct s_x1*)malloc( sizeof(struct s_x1) ); |
| 5046 | if( x1a ){ |
| 5047 | x1a->size = 1024; |
| 5048 | x1a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5049 | x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5050 | if( x1a->tbl==0 ){ |
| 5051 | free(x1a); |
| 5052 | x1a = 0; |
| 5053 | }else{ |
| 5054 | int i; |
| 5055 | x1a->ht = (x1node**)&(x1a->tbl[1024]); |
| 5056 | for(i=0; i<1024; i++) x1a->ht[i] = 0; |
| 5057 | } |
| 5058 | } |
| 5059 | } |
| 5060 | /* Insert a new record into the array. Return TRUE if successful. |
| 5061 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5062 | int Strsafe_insert(const char *data) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5063 | { |
| 5064 | x1node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5065 | unsigned h; |
| 5066 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5067 | |
| 5068 | if( x1a==0 ) return 0; |
| 5069 | ph = strhash(data); |
| 5070 | h = ph & (x1a->size-1); |
| 5071 | np = x1a->ht[h]; |
| 5072 | while( np ){ |
| 5073 | if( strcmp(np->data,data)==0 ){ |
| 5074 | /* An existing entry with the same key is found. */ |
| 5075 | /* Fail because overwrite is not allows. */ |
| 5076 | return 0; |
| 5077 | } |
| 5078 | np = np->next; |
| 5079 | } |
| 5080 | if( x1a->count>=x1a->size ){ |
| 5081 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5082 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5083 | struct s_x1 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5084 | array.size = arrSize = x1a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5085 | array.count = x1a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5086 | array.tbl = (x1node*)calloc(arrSize, sizeof(x1node) + sizeof(x1node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5087 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5088 | array.ht = (x1node**)&(array.tbl[arrSize]); |
| 5089 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5090 | for(i=0; i<x1a->count; i++){ |
| 5091 | x1node *oldnp, *newnp; |
| 5092 | oldnp = &(x1a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5093 | h = strhash(oldnp->data) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5094 | newnp = &(array.tbl[i]); |
| 5095 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5096 | newnp->next = array.ht[h]; |
| 5097 | newnp->data = oldnp->data; |
| 5098 | newnp->from = &(array.ht[h]); |
| 5099 | array.ht[h] = newnp; |
| 5100 | } |
| 5101 | free(x1a->tbl); |
| 5102 | *x1a = array; |
| 5103 | } |
| 5104 | /* Insert the new data */ |
| 5105 | h = ph & (x1a->size-1); |
| 5106 | np = &(x1a->tbl[x1a->count++]); |
| 5107 | np->data = data; |
| 5108 | if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next); |
| 5109 | np->next = x1a->ht[h]; |
| 5110 | x1a->ht[h] = np; |
| 5111 | np->from = &(x1a->ht[h]); |
| 5112 | return 1; |
| 5113 | } |
| 5114 | |
| 5115 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5116 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5117 | const char *Strsafe_find(const char *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5118 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5119 | unsigned h; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5120 | x1node *np; |
| 5121 | |
| 5122 | if( x1a==0 ) return 0; |
| 5123 | h = strhash(key) & (x1a->size-1); |
| 5124 | np = x1a->ht[h]; |
| 5125 | while( np ){ |
| 5126 | if( strcmp(np->data,key)==0 ) break; |
| 5127 | np = np->next; |
| 5128 | } |
| 5129 | return np ? np->data : 0; |
| 5130 | } |
| 5131 | |
| 5132 | /* Return a pointer to the (terminal or nonterminal) symbol "x". |
| 5133 | ** Create a new symbol if this is the first time "x" has been seen. |
| 5134 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5135 | struct symbol *Symbol_new(const char *x) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5136 | { |
| 5137 | struct symbol *sp; |
| 5138 | |
| 5139 | sp = Symbol_find(x); |
| 5140 | if( sp==0 ){ |
drh | 9892c5d | 2007-12-21 00:02:11 +0000 | [diff] [blame] | 5141 | sp = (struct symbol *)calloc(1, sizeof(struct symbol) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5142 | MemoryCheck(sp); |
| 5143 | sp->name = Strsafe(x); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 5144 | sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5145 | sp->rule = 0; |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 5146 | sp->fallback = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5147 | sp->prec = -1; |
| 5148 | sp->assoc = UNK; |
| 5149 | sp->firstset = 0; |
drh | aa9f112 | 2007-08-23 02:50:56 +0000 | [diff] [blame] | 5150 | sp->lambda = LEMON_FALSE; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5151 | sp->destructor = 0; |
drh | 4dc8ef5 | 2008-07-01 17:13:57 +0000 | [diff] [blame] | 5152 | sp->destLineno = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5153 | sp->datatype = 0; |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 5154 | sp->useCnt = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5155 | Symbol_insert(sp,sp->name); |
| 5156 | } |
drh | c4dd3fd | 2008-01-22 01:48:05 +0000 | [diff] [blame] | 5157 | sp->useCnt++; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5158 | return sp; |
| 5159 | } |
| 5160 | |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 5161 | /* Compare two symbols for sorting purposes. Return negative, |
| 5162 | ** zero, or positive if a is less then, equal to, or greater |
| 5163 | ** than b. |
drh | 60d3165 | 2004-02-22 00:08:04 +0000 | [diff] [blame] | 5164 | ** |
| 5165 | ** Symbols that begin with upper case letters (terminals or tokens) |
| 5166 | ** must sort before symbols that begin with lower case letters |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 5167 | ** (non-terminals). And MULTITERMINAL symbols (created using the |
| 5168 | ** %token_class directive) must sort at the very end. Other than |
| 5169 | ** that, the order does not matter. |
drh | 60d3165 | 2004-02-22 00:08:04 +0000 | [diff] [blame] | 5170 | ** |
| 5171 | ** We find experimentally that leaving the symbols in their original |
| 5172 | ** order (the order they appeared in the grammar file) gives the |
| 5173 | ** smallest parser tables in SQLite. |
| 5174 | */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5175 | int Symbolcmpp(const void *_a, const void *_b) |
| 5176 | { |
drh | 61f92cd | 2014-01-11 03:06:18 +0000 | [diff] [blame] | 5177 | const struct symbol *a = *(const struct symbol **) _a; |
| 5178 | const struct symbol *b = *(const struct symbol **) _b; |
| 5179 | int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1; |
| 5180 | int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1; |
| 5181 | return i1==i2 ? a->index - b->index : i1 - i2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | /* There is one instance of the following structure for each |
| 5185 | ** associative array of type "x2". |
| 5186 | */ |
| 5187 | struct s_x2 { |
| 5188 | int size; /* The number of available slots. */ |
| 5189 | /* Must be a power of 2 greater than or */ |
| 5190 | /* equal to 1 */ |
| 5191 | int count; /* Number of currently slots filled */ |
| 5192 | struct s_x2node *tbl; /* The data stored here */ |
| 5193 | struct s_x2node **ht; /* Hash table for lookups */ |
| 5194 | }; |
| 5195 | |
| 5196 | /* There is one instance of this structure for every data element |
| 5197 | ** in an associative array of type "x2". |
| 5198 | */ |
| 5199 | typedef struct s_x2node { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5200 | struct symbol *data; /* The data */ |
| 5201 | const char *key; /* The key */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5202 | struct s_x2node *next; /* Next entry with the same hash */ |
| 5203 | struct s_x2node **from; /* Previous link */ |
| 5204 | } x2node; |
| 5205 | |
| 5206 | /* There is only one instance of the array, which is the following */ |
| 5207 | static struct s_x2 *x2a; |
| 5208 | |
| 5209 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5210 | void Symbol_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5211 | if( x2a ) return; |
| 5212 | x2a = (struct s_x2*)malloc( sizeof(struct s_x2) ); |
| 5213 | if( x2a ){ |
| 5214 | x2a->size = 128; |
| 5215 | x2a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5216 | x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5217 | if( x2a->tbl==0 ){ |
| 5218 | free(x2a); |
| 5219 | x2a = 0; |
| 5220 | }else{ |
| 5221 | int i; |
| 5222 | x2a->ht = (x2node**)&(x2a->tbl[128]); |
| 5223 | for(i=0; i<128; i++) x2a->ht[i] = 0; |
| 5224 | } |
| 5225 | } |
| 5226 | } |
| 5227 | /* Insert a new record into the array. Return TRUE if successful. |
| 5228 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5229 | int Symbol_insert(struct symbol *data, const char *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5230 | { |
| 5231 | x2node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5232 | unsigned h; |
| 5233 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5234 | |
| 5235 | if( x2a==0 ) return 0; |
| 5236 | ph = strhash(key); |
| 5237 | h = ph & (x2a->size-1); |
| 5238 | np = x2a->ht[h]; |
| 5239 | while( np ){ |
| 5240 | if( strcmp(np->key,key)==0 ){ |
| 5241 | /* An existing entry with the same key is found. */ |
| 5242 | /* Fail because overwrite is not allows. */ |
| 5243 | return 0; |
| 5244 | } |
| 5245 | np = np->next; |
| 5246 | } |
| 5247 | if( x2a->count>=x2a->size ){ |
| 5248 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5249 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5250 | struct s_x2 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5251 | array.size = arrSize = x2a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5252 | array.count = x2a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5253 | array.tbl = (x2node*)calloc(arrSize, sizeof(x2node) + sizeof(x2node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5254 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5255 | array.ht = (x2node**)&(array.tbl[arrSize]); |
| 5256 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5257 | for(i=0; i<x2a->count; i++){ |
| 5258 | x2node *oldnp, *newnp; |
| 5259 | oldnp = &(x2a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5260 | h = strhash(oldnp->key) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5261 | newnp = &(array.tbl[i]); |
| 5262 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5263 | newnp->next = array.ht[h]; |
| 5264 | newnp->key = oldnp->key; |
| 5265 | newnp->data = oldnp->data; |
| 5266 | newnp->from = &(array.ht[h]); |
| 5267 | array.ht[h] = newnp; |
| 5268 | } |
| 5269 | free(x2a->tbl); |
| 5270 | *x2a = array; |
| 5271 | } |
| 5272 | /* Insert the new data */ |
| 5273 | h = ph & (x2a->size-1); |
| 5274 | np = &(x2a->tbl[x2a->count++]); |
| 5275 | np->key = key; |
| 5276 | np->data = data; |
| 5277 | if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next); |
| 5278 | np->next = x2a->ht[h]; |
| 5279 | x2a->ht[h] = np; |
| 5280 | np->from = &(x2a->ht[h]); |
| 5281 | return 1; |
| 5282 | } |
| 5283 | |
| 5284 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5285 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5286 | struct symbol *Symbol_find(const char *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5287 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5288 | unsigned h; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5289 | x2node *np; |
| 5290 | |
| 5291 | if( x2a==0 ) return 0; |
| 5292 | h = strhash(key) & (x2a->size-1); |
| 5293 | np = x2a->ht[h]; |
| 5294 | while( np ){ |
| 5295 | if( strcmp(np->key,key)==0 ) break; |
| 5296 | np = np->next; |
| 5297 | } |
| 5298 | return np ? np->data : 0; |
| 5299 | } |
| 5300 | |
| 5301 | /* Return the n-th data. Return NULL if n is out of range. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5302 | struct symbol *Symbol_Nth(int n) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5303 | { |
| 5304 | struct symbol *data; |
| 5305 | if( x2a && n>0 && n<=x2a->count ){ |
| 5306 | data = x2a->tbl[n-1].data; |
| 5307 | }else{ |
| 5308 | data = 0; |
| 5309 | } |
| 5310 | return data; |
| 5311 | } |
| 5312 | |
| 5313 | /* Return the size of the array */ |
| 5314 | int Symbol_count() |
| 5315 | { |
| 5316 | return x2a ? x2a->count : 0; |
| 5317 | } |
| 5318 | |
| 5319 | /* Return an array of pointers to all data in the table. |
| 5320 | ** The array is obtained from malloc. Return NULL if memory allocation |
| 5321 | ** problems, or if the array is empty. */ |
| 5322 | struct symbol **Symbol_arrayof() |
| 5323 | { |
| 5324 | struct symbol **array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5325 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5326 | if( x2a==0 ) return 0; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5327 | arrSize = x2a->count; |
| 5328 | array = (struct symbol **)calloc(arrSize, sizeof(struct symbol *)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5329 | if( array ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5330 | for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5331 | } |
| 5332 | return array; |
| 5333 | } |
| 5334 | |
| 5335 | /* Compare two configurations */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5336 | int Configcmp(const char *_a,const char *_b) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5337 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5338 | const struct config *a = (struct config *) _a; |
| 5339 | const struct config *b = (struct config *) _b; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5340 | int x; |
| 5341 | x = a->rp->index - b->rp->index; |
| 5342 | if( x==0 ) x = a->dot - b->dot; |
| 5343 | return x; |
| 5344 | } |
| 5345 | |
| 5346 | /* Compare two states */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5347 | PRIVATE int statecmp(struct config *a, struct config *b) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5348 | { |
| 5349 | int rc; |
| 5350 | for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){ |
| 5351 | rc = a->rp->index - b->rp->index; |
| 5352 | if( rc==0 ) rc = a->dot - b->dot; |
| 5353 | } |
| 5354 | if( rc==0 ){ |
| 5355 | if( a ) rc = 1; |
| 5356 | if( b ) rc = -1; |
| 5357 | } |
| 5358 | return rc; |
| 5359 | } |
| 5360 | |
| 5361 | /* Hash a state */ |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5362 | PRIVATE unsigned statehash(struct config *a) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5363 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5364 | unsigned h=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5365 | while( a ){ |
| 5366 | h = h*571 + a->rp->index*37 + a->dot; |
| 5367 | a = a->bp; |
| 5368 | } |
| 5369 | return h; |
| 5370 | } |
| 5371 | |
| 5372 | /* Allocate a new state structure */ |
| 5373 | struct state *State_new() |
| 5374 | { |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5375 | struct state *newstate; |
| 5376 | newstate = (struct state *)calloc(1, sizeof(struct state) ); |
| 5377 | MemoryCheck(newstate); |
| 5378 | return newstate; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | /* There is one instance of the following structure for each |
| 5382 | ** associative array of type "x3". |
| 5383 | */ |
| 5384 | struct s_x3 { |
| 5385 | int size; /* The number of available slots. */ |
| 5386 | /* Must be a power of 2 greater than or */ |
| 5387 | /* equal to 1 */ |
| 5388 | int count; /* Number of currently slots filled */ |
| 5389 | struct s_x3node *tbl; /* The data stored here */ |
| 5390 | struct s_x3node **ht; /* Hash table for lookups */ |
| 5391 | }; |
| 5392 | |
| 5393 | /* There is one instance of this structure for every data element |
| 5394 | ** in an associative array of type "x3". |
| 5395 | */ |
| 5396 | typedef struct s_x3node { |
| 5397 | struct state *data; /* The data */ |
| 5398 | struct config *key; /* The key */ |
| 5399 | struct s_x3node *next; /* Next entry with the same hash */ |
| 5400 | struct s_x3node **from; /* Previous link */ |
| 5401 | } x3node; |
| 5402 | |
| 5403 | /* There is only one instance of the array, which is the following */ |
| 5404 | static struct s_x3 *x3a; |
| 5405 | |
| 5406 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5407 | void State_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5408 | if( x3a ) return; |
| 5409 | x3a = (struct s_x3*)malloc( sizeof(struct s_x3) ); |
| 5410 | if( x3a ){ |
| 5411 | x3a->size = 128; |
| 5412 | x3a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5413 | x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5414 | if( x3a->tbl==0 ){ |
| 5415 | free(x3a); |
| 5416 | x3a = 0; |
| 5417 | }else{ |
| 5418 | int i; |
| 5419 | x3a->ht = (x3node**)&(x3a->tbl[128]); |
| 5420 | for(i=0; i<128; i++) x3a->ht[i] = 0; |
| 5421 | } |
| 5422 | } |
| 5423 | } |
| 5424 | /* Insert a new record into the array. Return TRUE if successful. |
| 5425 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5426 | int State_insert(struct state *data, struct config *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5427 | { |
| 5428 | x3node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5429 | unsigned h; |
| 5430 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5431 | |
| 5432 | if( x3a==0 ) return 0; |
| 5433 | ph = statehash(key); |
| 5434 | h = ph & (x3a->size-1); |
| 5435 | np = x3a->ht[h]; |
| 5436 | while( np ){ |
| 5437 | if( statecmp(np->key,key)==0 ){ |
| 5438 | /* An existing entry with the same key is found. */ |
| 5439 | /* Fail because overwrite is not allows. */ |
| 5440 | return 0; |
| 5441 | } |
| 5442 | np = np->next; |
| 5443 | } |
| 5444 | if( x3a->count>=x3a->size ){ |
| 5445 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5446 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5447 | struct s_x3 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5448 | array.size = arrSize = x3a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5449 | array.count = x3a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5450 | array.tbl = (x3node*)calloc(arrSize, sizeof(x3node) + sizeof(x3node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5451 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5452 | array.ht = (x3node**)&(array.tbl[arrSize]); |
| 5453 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5454 | for(i=0; i<x3a->count; i++){ |
| 5455 | x3node *oldnp, *newnp; |
| 5456 | oldnp = &(x3a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5457 | h = statehash(oldnp->key) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5458 | newnp = &(array.tbl[i]); |
| 5459 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5460 | newnp->next = array.ht[h]; |
| 5461 | newnp->key = oldnp->key; |
| 5462 | newnp->data = oldnp->data; |
| 5463 | newnp->from = &(array.ht[h]); |
| 5464 | array.ht[h] = newnp; |
| 5465 | } |
| 5466 | free(x3a->tbl); |
| 5467 | *x3a = array; |
| 5468 | } |
| 5469 | /* Insert the new data */ |
| 5470 | h = ph & (x3a->size-1); |
| 5471 | np = &(x3a->tbl[x3a->count++]); |
| 5472 | np->key = key; |
| 5473 | np->data = data; |
| 5474 | if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next); |
| 5475 | np->next = x3a->ht[h]; |
| 5476 | x3a->ht[h] = np; |
| 5477 | np->from = &(x3a->ht[h]); |
| 5478 | return 1; |
| 5479 | } |
| 5480 | |
| 5481 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5482 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5483 | struct state *State_find(struct config *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5484 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5485 | unsigned h; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5486 | x3node *np; |
| 5487 | |
| 5488 | if( x3a==0 ) return 0; |
| 5489 | h = statehash(key) & (x3a->size-1); |
| 5490 | np = x3a->ht[h]; |
| 5491 | while( np ){ |
| 5492 | if( statecmp(np->key,key)==0 ) break; |
| 5493 | np = np->next; |
| 5494 | } |
| 5495 | return np ? np->data : 0; |
| 5496 | } |
| 5497 | |
| 5498 | /* Return an array of pointers to all data in the table. |
| 5499 | ** The array is obtained from malloc. Return NULL if memory allocation |
| 5500 | ** problems, or if the array is empty. */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5501 | struct state **State_arrayof(void) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5502 | { |
| 5503 | struct state **array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5504 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5505 | if( x3a==0 ) return 0; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5506 | arrSize = x3a->count; |
| 5507 | array = (struct state **)calloc(arrSize, sizeof(struct state *)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5508 | if( array ){ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5509 | for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5510 | } |
| 5511 | return array; |
| 5512 | } |
| 5513 | |
| 5514 | /* Hash a configuration */ |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5515 | PRIVATE unsigned confighash(struct config *a) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5516 | { |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5517 | unsigned h=0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5518 | h = h*571 + a->rp->index*37 + a->dot; |
| 5519 | return h; |
| 5520 | } |
| 5521 | |
| 5522 | /* There is one instance of the following structure for each |
| 5523 | ** associative array of type "x4". |
| 5524 | */ |
| 5525 | struct s_x4 { |
| 5526 | int size; /* The number of available slots. */ |
| 5527 | /* Must be a power of 2 greater than or */ |
| 5528 | /* equal to 1 */ |
| 5529 | int count; /* Number of currently slots filled */ |
| 5530 | struct s_x4node *tbl; /* The data stored here */ |
| 5531 | struct s_x4node **ht; /* Hash table for lookups */ |
| 5532 | }; |
| 5533 | |
| 5534 | /* There is one instance of this structure for every data element |
| 5535 | ** in an associative array of type "x4". |
| 5536 | */ |
| 5537 | typedef struct s_x4node { |
| 5538 | struct config *data; /* The data */ |
| 5539 | struct s_x4node *next; /* Next entry with the same hash */ |
| 5540 | struct s_x4node **from; /* Previous link */ |
| 5541 | } x4node; |
| 5542 | |
| 5543 | /* There is only one instance of the array, which is the following */ |
| 5544 | static struct s_x4 *x4a; |
| 5545 | |
| 5546 | /* Allocate a new associative array */ |
drh | 14d8855 | 2017-04-14 19:44:15 +0000 | [diff] [blame] | 5547 | void Configtable_init(void){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5548 | if( x4a ) return; |
| 5549 | x4a = (struct s_x4*)malloc( sizeof(struct s_x4) ); |
| 5550 | if( x4a ){ |
| 5551 | x4a->size = 64; |
| 5552 | x4a->count = 0; |
drh | 03e1b1f | 2014-01-11 12:52:25 +0000 | [diff] [blame] | 5553 | x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5554 | if( x4a->tbl==0 ){ |
| 5555 | free(x4a); |
| 5556 | x4a = 0; |
| 5557 | }else{ |
| 5558 | int i; |
| 5559 | x4a->ht = (x4node**)&(x4a->tbl[64]); |
| 5560 | for(i=0; i<64; i++) x4a->ht[i] = 0; |
| 5561 | } |
| 5562 | } |
| 5563 | } |
| 5564 | /* Insert a new record into the array. Return TRUE if successful. |
| 5565 | ** Prior data with the same key is NOT overwritten */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5566 | int Configtable_insert(struct config *data) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5567 | { |
| 5568 | x4node *np; |
drh | 01f75f2 | 2013-10-02 20:46:30 +0000 | [diff] [blame] | 5569 | unsigned h; |
| 5570 | unsigned ph; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5571 | |
| 5572 | if( x4a==0 ) return 0; |
| 5573 | ph = confighash(data); |
| 5574 | h = ph & (x4a->size-1); |
| 5575 | np = x4a->ht[h]; |
| 5576 | while( np ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5577 | if( Configcmp((const char *) np->data,(const char *) data)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5578 | /* An existing entry with the same key is found. */ |
| 5579 | /* Fail because overwrite is not allows. */ |
| 5580 | return 0; |
| 5581 | } |
| 5582 | np = np->next; |
| 5583 | } |
| 5584 | if( x4a->count>=x4a->size ){ |
| 5585 | /* Need to make the hash table bigger */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5586 | int i,arrSize; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5587 | struct s_x4 array; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5588 | array.size = arrSize = x4a->size*2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5589 | array.count = x4a->count; |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5590 | array.tbl = (x4node*)calloc(arrSize, sizeof(x4node) + sizeof(x4node*)); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5591 | if( array.tbl==0 ) return 0; /* Fail due to malloc failure */ |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5592 | array.ht = (x4node**)&(array.tbl[arrSize]); |
| 5593 | for(i=0; i<arrSize; i++) array.ht[i] = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5594 | for(i=0; i<x4a->count; i++){ |
| 5595 | x4node *oldnp, *newnp; |
| 5596 | oldnp = &(x4a->tbl[i]); |
mistachkin | 8e18922 | 2015-04-19 21:43:16 +0000 | [diff] [blame] | 5597 | h = confighash(oldnp->data) & (arrSize-1); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5598 | newnp = &(array.tbl[i]); |
| 5599 | if( array.ht[h] ) array.ht[h]->from = &(newnp->next); |
| 5600 | newnp->next = array.ht[h]; |
| 5601 | newnp->data = oldnp->data; |
| 5602 | newnp->from = &(array.ht[h]); |
| 5603 | array.ht[h] = newnp; |
| 5604 | } |
| 5605 | free(x4a->tbl); |
| 5606 | *x4a = array; |
| 5607 | } |
| 5608 | /* Insert the new data */ |
| 5609 | h = ph & (x4a->size-1); |
| 5610 | np = &(x4a->tbl[x4a->count++]); |
| 5611 | np->data = data; |
| 5612 | if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next); |
| 5613 | np->next = x4a->ht[h]; |
| 5614 | x4a->ht[h] = np; |
| 5615 | np->from = &(x4a->ht[h]); |
| 5616 | return 1; |
| 5617 | } |
| 5618 | |
| 5619 | /* Return a pointer to data assigned to the given key. Return NULL |
| 5620 | ** if no such key. */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5621 | struct config *Configtable_find(struct config *key) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5622 | { |
| 5623 | int h; |
| 5624 | x4node *np; |
| 5625 | |
| 5626 | if( x4a==0 ) return 0; |
| 5627 | h = confighash(key) & (x4a->size-1); |
| 5628 | np = x4a->ht[h]; |
| 5629 | while( np ){ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5630 | if( Configcmp((const char *) np->data,(const char *) key)==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5631 | np = np->next; |
| 5632 | } |
| 5633 | return np ? np->data : 0; |
| 5634 | } |
| 5635 | |
| 5636 | /* Remove all data from the table. Pass each data to the function "f" |
| 5637 | ** as it is removed. ("f" may be null to avoid this step.) */ |
icculus | 9e44cf1 | 2010-02-14 17:14:22 +0000 | [diff] [blame] | 5638 | void Configtable_clear(int(*f)(struct config *)) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 5639 | { |
| 5640 | int i; |
| 5641 | if( x4a==0 || x4a->count==0 ) return; |
| 5642 | if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data); |
| 5643 | for(i=0; i<x4a->size; i++) x4a->ht[i] = 0; |
| 5644 | x4a->count = 0; |
| 5645 | return; |
| 5646 | } |