blob: 96bbed747386b3f50f65a48a943c02f1bbc6307d [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drh75897232000-05-29 14:26:00 +00002** This file contains all sources (including headers) to the LEMON
3** LALR(1) parser generator. The sources have been combined into a
drh960e8c62001-04-03 16:53:21 +00004** single file to make it easy to include LEMON in the source tree
5** and Makefile of another program.
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** The author of this program disclaims copyright.
drh75897232000-05-29 14:26:00 +00008*/
9#include <stdio.h>
drhf9a2e7b2003-04-15 01:49:48 +000010#include <stdarg.h>
drh75897232000-05-29 14:26:00 +000011#include <string.h>
12#include <ctype.h>
drh8b582012003-10-21 13:16:03 +000013#include <stdlib.h>
drhe9278182007-07-18 18:16:29 +000014#include <assert.h>
drh75897232000-05-29 14:26:00 +000015
drhc56fac72015-10-29 13:48:15 +000016#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
drh75897232000-05-29 14:26:00 +000024#ifndef __WIN32__
25# if defined(_WIN32) || defined(WIN32)
drhf2f105d2012-08-20 15:53:54 +000026# define __WIN32__
drh75897232000-05-29 14:26:00 +000027# endif
28#endif
29
rse8f304482007-07-30 18:31:53 +000030#ifdef __WIN32__
drhdf609712010-11-23 20:55:27 +000031#ifdef __cplusplus
32extern "C" {
33#endif
34extern int access(const char *path, int mode);
35#ifdef __cplusplus
36}
37#endif
rse8f304482007-07-30 18:31:53 +000038#else
39#include <unistd.h>
40#endif
41
drh75897232000-05-29 14:26:00 +000042/* #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
drhf5c4e0f2010-07-18 11:35:53 +000051static int showPrecedenceConflict = 0;
drhe9278182007-07-18 18:16:29 +000052static char *msort(char*,char**,int(*)(const char*,const char*));
drh75897232000-05-29 14:26:00 +000053
drh87cf1372008-08-13 20:09:06 +000054/*
55** Compilers are getting increasingly pedantic about type conversions
56** as C evolves ever closer to Ada.... To work around the latest problems
57** we have to define the following variant of strlen().
58*/
59#define lemonStrlen(X) ((int)strlen(X))
60
drh898799f2014-01-10 23:21:00 +000061/*
62** Compilers are starting to complain about the use of sprintf() and strcpy(),
63** saying they are unsafe. So we define our own versions of those routines too.
64**
65** There are three routines here: lemon_sprintf(), lemon_vsprintf(), and
drh25473362015-09-04 18:03:45 +000066** lemon_addtext(). The first two are replacements for sprintf() and vsprintf().
drh898799f2014-01-10 23:21:00 +000067** The third is a helper routine for vsnprintf() that adds texts to the end of a
68** buffer, making sure the buffer is always zero-terminated.
69**
70** The string formatter is a minimal subset of stdlib sprintf() supporting only
71** a few simply conversions:
72**
73** %d
74** %s
75** %.*s
76**
77*/
78static void lemon_addtext(
79 char *zBuf, /* The buffer to which text is added */
80 int *pnUsed, /* Slots of the buffer used so far */
81 const char *zIn, /* Text to add */
drh61f92cd2014-01-11 03:06:18 +000082 int nIn, /* Bytes of text to add. -1 to use strlen() */
83 int iWidth /* Field width. Negative to left justify */
drh898799f2014-01-10 23:21:00 +000084){
85 if( nIn<0 ) for(nIn=0; zIn[nIn]; nIn++){}
drhecaa9d32014-01-11 03:27:37 +000086 while( iWidth>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth--; }
drh898799f2014-01-10 23:21:00 +000087 if( nIn==0 ) return;
88 memcpy(&zBuf[*pnUsed], zIn, nIn);
89 *pnUsed += nIn;
drhecaa9d32014-01-11 03:27:37 +000090 while( (-iWidth)>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth++; }
drh898799f2014-01-10 23:21:00 +000091 zBuf[*pnUsed] = 0;
92}
93static int lemon_vsprintf(char *str, const char *zFormat, va_list ap){
mistachkin7a429652014-01-14 10:17:21 +000094 int i, j, k, c;
drh898799f2014-01-10 23:21:00 +000095 int nUsed = 0;
96 const char *z;
97 char zTemp[50];
98 str[0] = 0;
99 for(i=j=0; (c = zFormat[i])!=0; i++){
100 if( c=='%' ){
drh61f92cd2014-01-11 03:06:18 +0000101 int iWidth = 0;
102 lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0);
drh898799f2014-01-10 23:21:00 +0000103 c = zFormat[++i];
drhc56fac72015-10-29 13:48:15 +0000104 if( ISDIGIT(c) || (c=='-' && ISDIGIT(zFormat[i+1])) ){
drh61f92cd2014-01-11 03:06:18 +0000105 if( c=='-' ) i++;
drhc56fac72015-10-29 13:48:15 +0000106 while( ISDIGIT(zFormat[i]) ) iWidth = iWidth*10 + zFormat[i++] - '0';
drh61f92cd2014-01-11 03:06:18 +0000107 if( c=='-' ) iWidth = -iWidth;
108 c = zFormat[i];
109 }
drh898799f2014-01-10 23:21:00 +0000110 if( c=='d' ){
111 int v = va_arg(ap, int);
112 if( v<0 ){
drh61f92cd2014-01-11 03:06:18 +0000113 lemon_addtext(str, &nUsed, "-", 1, iWidth);
drh898799f2014-01-10 23:21:00 +0000114 v = -v;
115 }else if( v==0 ){
drh61f92cd2014-01-11 03:06:18 +0000116 lemon_addtext(str, &nUsed, "0", 1, iWidth);
drh898799f2014-01-10 23:21:00 +0000117 }
118 k = 0;
119 while( v>0 ){
120 k++;
121 zTemp[sizeof(zTemp)-k] = (v%10) + '0';
122 v /= 10;
123 }
drh61f92cd2014-01-11 03:06:18 +0000124 lemon_addtext(str, &nUsed, &zTemp[sizeof(zTemp)-k], k, iWidth);
drh898799f2014-01-10 23:21:00 +0000125 }else if( c=='s' ){
126 z = va_arg(ap, const char*);
drh61f92cd2014-01-11 03:06:18 +0000127 lemon_addtext(str, &nUsed, z, -1, iWidth);
drh898799f2014-01-10 23:21:00 +0000128 }else if( c=='.' && memcmp(&zFormat[i], ".*s", 3)==0 ){
129 i += 2;
130 k = va_arg(ap, int);
131 z = va_arg(ap, const char*);
drh61f92cd2014-01-11 03:06:18 +0000132 lemon_addtext(str, &nUsed, z, k, iWidth);
drh898799f2014-01-10 23:21:00 +0000133 }else if( c=='%' ){
drh61f92cd2014-01-11 03:06:18 +0000134 lemon_addtext(str, &nUsed, "%", 1, 0);
drh898799f2014-01-10 23:21:00 +0000135 }else{
136 fprintf(stderr, "illegal format\n");
137 exit(1);
138 }
139 j = i+1;
140 }
141 }
drh61f92cd2014-01-11 03:06:18 +0000142 lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0);
drh898799f2014-01-10 23:21:00 +0000143 return nUsed;
144}
145static int lemon_sprintf(char *str, const char *format, ...){
146 va_list ap;
147 int rc;
148 va_start(ap, format);
149 rc = lemon_vsprintf(str, format, ap);
150 va_end(ap);
151 return rc;
152}
153static void lemon_strcpy(char *dest, const char *src){
154 while( (*(dest++) = *(src++))!=0 ){}
155}
156static void lemon_strcat(char *dest, const char *src){
157 while( *dest ) dest++;
158 lemon_strcpy(dest, src);
159}
160
161
icculus9e44cf12010-02-14 17:14:22 +0000162/* a few forward declarations... */
163struct rule;
164struct lemon;
165struct action;
166
drhe9278182007-07-18 18:16:29 +0000167static struct action *Action_new(void);
168static struct action *Action_sort(struct action *);
drh75897232000-05-29 14:26:00 +0000169
170/********** From the file "build.h" ************************************/
drh14d88552017-04-14 19:44:15 +0000171void FindRulePrecedences(struct lemon*);
172void FindFirstSets(struct lemon*);
173void FindStates(struct lemon*);
174void FindLinks(struct lemon*);
175void FindFollowSets(struct lemon*);
176void FindActions(struct lemon*);
drh75897232000-05-29 14:26:00 +0000177
178/********* From the file "configlist.h" *********************************/
icculus9e44cf12010-02-14 17:14:22 +0000179void Configlist_init(void);
180struct config *Configlist_add(struct rule *, int);
181struct config *Configlist_addbasis(struct rule *, int);
182void Configlist_closure(struct lemon *);
183void Configlist_sort(void);
184void Configlist_sortbasis(void);
185struct config *Configlist_return(void);
186struct config *Configlist_basis(void);
187void Configlist_eat(struct config *);
188void Configlist_reset(void);
drh75897232000-05-29 14:26:00 +0000189
190/********* From the file "error.h" ***************************************/
drhf9a2e7b2003-04-15 01:49:48 +0000191void ErrorMsg(const char *, int,const char *, ...);
drh75897232000-05-29 14:26:00 +0000192
193/****** From the file "option.h" ******************************************/
icculus9e44cf12010-02-14 17:14:22 +0000194enum option_type { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR,
195 OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR};
drh75897232000-05-29 14:26:00 +0000196struct s_options {
icculus9e44cf12010-02-14 17:14:22 +0000197 enum option_type type;
198 const char *label;
drh75897232000-05-29 14:26:00 +0000199 char *arg;
icculus9e44cf12010-02-14 17:14:22 +0000200 const char *message;
drh75897232000-05-29 14:26:00 +0000201};
icculus9e44cf12010-02-14 17:14:22 +0000202int OptInit(char**,struct s_options*,FILE*);
203int OptNArgs(void);
204char *OptArg(int);
205void OptErr(int);
206void OptPrint(void);
drh75897232000-05-29 14:26:00 +0000207
208/******** From the file "parse.h" *****************************************/
icculus9e44cf12010-02-14 17:14:22 +0000209void Parse(struct lemon *lemp);
drh75897232000-05-29 14:26:00 +0000210
211/********* From the file "plink.h" ***************************************/
icculus9e44cf12010-02-14 17:14:22 +0000212struct plink *Plink_new(void);
213void Plink_add(struct plink **, struct config *);
214void Plink_copy(struct plink **, struct plink *);
215void Plink_delete(struct plink *);
drh75897232000-05-29 14:26:00 +0000216
217/********** From the file "report.h" *************************************/
icculus9e44cf12010-02-14 17:14:22 +0000218void Reprint(struct lemon *);
219void ReportOutput(struct lemon *);
220void ReportTable(struct lemon *, int);
221void ReportHeader(struct lemon *);
222void CompressTables(struct lemon *);
223void ResortStates(struct lemon *);
drh75897232000-05-29 14:26:00 +0000224
225/********** From the file "set.h" ****************************************/
icculus9e44cf12010-02-14 17:14:22 +0000226void SetSize(int); /* All sets will be of size N */
227char *SetNew(void); /* A new set for element 0..N */
228void SetFree(char*); /* Deallocate a set */
icculus9e44cf12010-02-14 17:14:22 +0000229int SetAdd(char*,int); /* Add element to a set */
230int SetUnion(char *,char *); /* A <- A U B, thru element N */
drh75897232000-05-29 14:26:00 +0000231#define SetFind(X,Y) (X[Y]) /* True if Y is in set X */
232
233/********** From the file "struct.h" *************************************/
234/*
235** Principal data structures for the LEMON parser generator.
236*/
237
drhaa9f1122007-08-23 02:50:56 +0000238typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean;
drh75897232000-05-29 14:26:00 +0000239
240/* Symbols (terminals and nonterminals) of the grammar are stored
241** in the following: */
icculus9e44cf12010-02-14 17:14:22 +0000242enum symbol_type {
243 TERMINAL,
244 NONTERMINAL,
245 MULTITERMINAL
246};
247enum e_assoc {
drh75897232000-05-29 14:26:00 +0000248 LEFT,
249 RIGHT,
250 NONE,
251 UNK
icculus9e44cf12010-02-14 17:14:22 +0000252};
253struct symbol {
254 const char *name; /* Name of the symbol */
255 int index; /* Index number for this symbol */
256 enum symbol_type type; /* Symbols are all either TERMINALS or NTs */
257 struct rule *rule; /* Linked list of rules of this (if an NT) */
258 struct symbol *fallback; /* fallback token in case this token doesn't parse */
259 int prec; /* Precedence if defined (-1 otherwise) */
260 enum e_assoc assoc; /* Associativity if precedence is defined */
drh75897232000-05-29 14:26:00 +0000261 char *firstset; /* First-set for all rules of this symbol */
262 Boolean lambda; /* True if NT and can generate an empty string */
drhc4dd3fd2008-01-22 01:48:05 +0000263 int useCnt; /* Number of times used */
drh75897232000-05-29 14:26:00 +0000264 char *destructor; /* Code which executes whenever this symbol is
265 ** popped from the stack during error processing */
drh0f832dd2016-08-16 16:46:40 +0000266 int destLineno; /* Line number for start of destructor. Set to
267 ** -1 for duplicate destructors. */
drh75897232000-05-29 14:26:00 +0000268 char *datatype; /* The data type of information held by this
269 ** object. Only used if type==NONTERMINAL */
270 int dtnum; /* The data type number. In the parser, the value
271 ** stack is a union. The .yy%d element of this
272 ** union is the correct data type for this object */
drhfd405312005-11-06 04:06:59 +0000273 /* The following fields are used by MULTITERMINALs only */
274 int nsubsym; /* Number of constituent symbols in the MULTI */
275 struct symbol **subsym; /* Array of constituent symbols */
drh75897232000-05-29 14:26:00 +0000276};
277
278/* Each production rule in the grammar is stored in the following
279** structure. */
280struct rule {
281 struct symbol *lhs; /* Left-hand side of the rule */
icculus9e44cf12010-02-14 17:14:22 +0000282 const char *lhsalias; /* Alias for the LHS (NULL if none) */
drhb4960992007-10-05 16:16:36 +0000283 int lhsStart; /* True if left-hand side is the start symbol */
drh75897232000-05-29 14:26:00 +0000284 int ruleline; /* Line number for the rule */
285 int nrhs; /* Number of RHS symbols */
286 struct symbol **rhs; /* The RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +0000287 const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */
drh75897232000-05-29 14:26:00 +0000288 int line; /* Line number at which code begins */
icculus9e44cf12010-02-14 17:14:22 +0000289 const char *code; /* The code executed when this rule is reduced */
drh4dd0d3f2016-02-17 01:18:33 +0000290 const char *codePrefix; /* Setup code before code[] above */
291 const char *codeSuffix; /* Breakdown code after code[] above */
drh711c9812016-05-23 14:24:31 +0000292 int noCode; /* True if this rule has no associated C code */
293 int codeEmitted; /* True if the code has been emitted already */
drh75897232000-05-29 14:26:00 +0000294 struct symbol *precsym; /* Precedence symbol for this rule */
295 int index; /* An index number for this rule */
drh4ef07702016-03-16 19:45:54 +0000296 int iRule; /* Rule number as used in the generated tables */
drh75897232000-05-29 14:26:00 +0000297 Boolean canReduce; /* True if this rule is ever reduced */
drh756b41e2016-05-24 18:55:08 +0000298 Boolean doesReduce; /* Reduce actions occur after optimization */
drh75897232000-05-29 14:26:00 +0000299 struct rule *nextlhs; /* Next rule with the same LHS */
300 struct rule *next; /* Next rule in the global list */
301};
302
303/* A configuration is a production rule of the grammar together with
304** a mark (dot) showing how much of that rule has been processed so far.
305** Configurations also contain a follow-set which is a list of terminal
306** symbols which are allowed to immediately follow the end of the rule.
307** Every configuration is recorded as an instance of the following: */
icculus9e44cf12010-02-14 17:14:22 +0000308enum cfgstatus {
309 COMPLETE,
310 INCOMPLETE
311};
drh75897232000-05-29 14:26:00 +0000312struct config {
313 struct rule *rp; /* The rule upon which the configuration is based */
314 int dot; /* The parse point */
315 char *fws; /* Follow-set for this configuration only */
316 struct plink *fplp; /* Follow-set forward propagation links */
317 struct plink *bplp; /* Follow-set backwards propagation links */
318 struct state *stp; /* Pointer to state which contains this */
icculus9e44cf12010-02-14 17:14:22 +0000319 enum cfgstatus status; /* used during followset and shift computations */
drh75897232000-05-29 14:26:00 +0000320 struct config *next; /* Next configuration in the state */
321 struct config *bp; /* The next basis configuration */
322};
323
icculus9e44cf12010-02-14 17:14:22 +0000324enum e_action {
325 SHIFT,
326 ACCEPT,
327 REDUCE,
328 ERROR,
329 SSCONFLICT, /* A shift/shift conflict */
330 SRCONFLICT, /* Was a reduce, but part of a conflict */
331 RRCONFLICT, /* Was a reduce, but part of a conflict */
332 SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
333 RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
drh3bd48ab2015-09-07 18:23:37 +0000334 NOT_USED, /* Deleted by compression */
335 SHIFTREDUCE /* Shift first, then reduce */
icculus9e44cf12010-02-14 17:14:22 +0000336};
337
drh75897232000-05-29 14:26:00 +0000338/* Every shift or reduce operation is stored as one of the following */
339struct action {
340 struct symbol *sp; /* The look-ahead symbol */
icculus9e44cf12010-02-14 17:14:22 +0000341 enum e_action type;
drh75897232000-05-29 14:26:00 +0000342 union {
343 struct state *stp; /* The new state, if a shift */
344 struct rule *rp; /* The rule, if a reduce */
345 } x;
drhc173ad82016-05-23 16:15:02 +0000346 struct symbol *spOpt; /* SHIFTREDUCE optimization to this symbol */
drh75897232000-05-29 14:26:00 +0000347 struct action *next; /* Next action for this state */
348 struct action *collide; /* Next action with the same hash */
349};
350
351/* Each state of the generated parser's finite state machine
352** is encoded as an instance of the following structure. */
353struct state {
354 struct config *bp; /* The basis configurations for this state */
355 struct config *cfp; /* All configurations in this set */
drh34ff57b2008-07-14 12:27:51 +0000356 int statenum; /* Sequential number for this state */
drh711c9812016-05-23 14:24:31 +0000357 struct action *ap; /* List of actions for this state */
drh8b582012003-10-21 13:16:03 +0000358 int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */
359 int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */
drh3bd48ab2015-09-07 18:23:37 +0000360 int iDfltReduce; /* Default action is to REDUCE by this rule */
361 struct rule *pDfltReduce;/* The default REDUCE rule. */
362 int autoReduce; /* True if this is an auto-reduce state */
drh75897232000-05-29 14:26:00 +0000363};
drh8b582012003-10-21 13:16:03 +0000364#define NO_OFFSET (-2147483647)
drh75897232000-05-29 14:26:00 +0000365
366/* A followset propagation link indicates that the contents of one
367** configuration followset should be propagated to another whenever
368** the first changes. */
369struct plink {
370 struct config *cfp; /* The configuration to which linked */
371 struct plink *next; /* The next propagate link */
372};
373
374/* The state vector for the entire parser generator is recorded as
375** follows. (LEMON uses no global variables and makes little use of
376** static variables. Fields in the following structure can be thought
377** of as begin global variables in the program.) */
378struct lemon {
379 struct state **sorted; /* Table of states sorted by state number */
380 struct rule *rule; /* List of all rules */
drh4ef07702016-03-16 19:45:54 +0000381 struct rule *startRule; /* First rule */
drh75897232000-05-29 14:26:00 +0000382 int nstate; /* Number of states */
drh3bd48ab2015-09-07 18:23:37 +0000383 int nxstate; /* nstate with tail degenerate states removed */
drh75897232000-05-29 14:26:00 +0000384 int nrule; /* Number of rules */
385 int nsymbol; /* Number of terminal and nonterminal symbols */
386 int nterminal; /* Number of terminal symbols */
drh5c8241b2017-12-24 23:38:10 +0000387 int minShiftReduce; /* Minimum shift-reduce action value */
388 int errAction; /* Error action value */
389 int accAction; /* Accept action value */
390 int noAction; /* No-op action value */
391 int minReduce; /* Minimum reduce action */
392 int maxAction; /* Maximum action value of any kind */
drh75897232000-05-29 14:26:00 +0000393 struct symbol **symbols; /* Sorted array of pointers to symbols */
394 int errorcnt; /* Number of errors */
395 struct symbol *errsym; /* The error symbol */
drhe09daa92006-06-10 13:29:31 +0000396 struct symbol *wildcard; /* Token that matches anything */
drh75897232000-05-29 14:26:00 +0000397 char *name; /* Name of the generated parser */
398 char *arg; /* Declaration of the 3th argument to parser */
399 char *tokentype; /* Type of terminal symbols in the parser stack */
drh960e8c62001-04-03 16:53:21 +0000400 char *vartype; /* The default type of non-terminal symbols */
drh75897232000-05-29 14:26:00 +0000401 char *start; /* Name of the start symbol for the grammar */
402 char *stacksize; /* Size of the parser stack */
403 char *include; /* Code to put at the start of the C file */
drh75897232000-05-29 14:26:00 +0000404 char *error; /* Code to execute when an error is seen */
drh75897232000-05-29 14:26:00 +0000405 char *overflow; /* Code to execute on a stack overflow */
drh75897232000-05-29 14:26:00 +0000406 char *failure; /* Code to execute on parser failure */
drh75897232000-05-29 14:26:00 +0000407 char *accept; /* Code to execute when the parser excepts */
drh75897232000-05-29 14:26:00 +0000408 char *extracode; /* Code appended to the generated file */
drh75897232000-05-29 14:26:00 +0000409 char *tokendest; /* Code to execute to destroy token data */
drh960e8c62001-04-03 16:53:21 +0000410 char *vardest; /* Code for the default non-terminal destructor */
drh75897232000-05-29 14:26:00 +0000411 char *filename; /* Name of the input file */
412 char *outname; /* Name of the current output file */
413 char *tokenprefix; /* A prefix added to token names in the .h file */
414 int nconflict; /* Number of parsing conflicts */
drhc75e0162015-09-07 02:23:02 +0000415 int nactiontab; /* Number of entries in the yy_action[] table */
drh3a9d6c72017-12-25 04:15:38 +0000416 int nlookaheadtab; /* Number of entries in yy_lookahead[] */
drhc75e0162015-09-07 02:23:02 +0000417 int tablesize; /* Total table size of all tables in bytes */
drh75897232000-05-29 14:26:00 +0000418 int basisflag; /* Print only basis configurations */
drh34ff57b2008-07-14 12:27:51 +0000419 int has_fallback; /* True if any %fallback is seen in the grammar */
shane58543932008-12-10 20:10:04 +0000420 int nolinenosflag; /* True if #line statements should not be printed */
drh75897232000-05-29 14:26:00 +0000421 char *argv0; /* Name of the program */
422};
423
424#define MemoryCheck(X) if((X)==0){ \
425 extern void memory_error(); \
426 memory_error(); \
427}
428
429/**************** From the file "table.h" *********************************/
430/*
431** All code in this file has been automatically generated
432** from a specification in the file
433** "table.q"
434** by the associative array code building program "aagen".
435** Do not edit this file! Instead, edit the specification
436** file, then rerun aagen.
437*/
438/*
439** Code for processing tables in the LEMON parser generator.
440*/
drh75897232000-05-29 14:26:00 +0000441/* Routines for handling a strings */
442
icculus9e44cf12010-02-14 17:14:22 +0000443const char *Strsafe(const char *);
drh75897232000-05-29 14:26:00 +0000444
icculus9e44cf12010-02-14 17:14:22 +0000445void Strsafe_init(void);
446int Strsafe_insert(const char *);
447const char *Strsafe_find(const char *);
drh75897232000-05-29 14:26:00 +0000448
449/* Routines for handling symbols of the grammar */
450
icculus9e44cf12010-02-14 17:14:22 +0000451struct symbol *Symbol_new(const char *);
452int Symbolcmpp(const void *, const void *);
453void Symbol_init(void);
454int Symbol_insert(struct symbol *, const char *);
455struct symbol *Symbol_find(const char *);
456struct symbol *Symbol_Nth(int);
457int Symbol_count(void);
458struct symbol **Symbol_arrayof(void);
drh75897232000-05-29 14:26:00 +0000459
460/* Routines to manage the state table */
461
icculus9e44cf12010-02-14 17:14:22 +0000462int Configcmp(const char *, const char *);
463struct state *State_new(void);
464void State_init(void);
465int State_insert(struct state *, struct config *);
466struct state *State_find(struct config *);
drh14d88552017-04-14 19:44:15 +0000467struct state **State_arrayof(void);
drh75897232000-05-29 14:26:00 +0000468
469/* Routines used for efficiency in Configlist_add */
470
icculus9e44cf12010-02-14 17:14:22 +0000471void Configtable_init(void);
472int Configtable_insert(struct config *);
473struct config *Configtable_find(struct config *);
474void Configtable_clear(int(*)(struct config *));
475
drh75897232000-05-29 14:26:00 +0000476/****************** From the file "action.c" *******************************/
477/*
478** Routines processing parser actions in the LEMON parser generator.
479*/
480
481/* Allocate a new parser action */
drhe9278182007-07-18 18:16:29 +0000482static struct action *Action_new(void){
drh75897232000-05-29 14:26:00 +0000483 static struct action *freelist = 0;
icculus9e44cf12010-02-14 17:14:22 +0000484 struct action *newaction;
drh75897232000-05-29 14:26:00 +0000485
486 if( freelist==0 ){
487 int i;
488 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +0000489 freelist = (struct action *)calloc(amt, sizeof(struct action));
drh75897232000-05-29 14:26:00 +0000490 if( freelist==0 ){
491 fprintf(stderr,"Unable to allocate memory for a new parser action.");
492 exit(1);
493 }
494 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
495 freelist[amt-1].next = 0;
496 }
icculus9e44cf12010-02-14 17:14:22 +0000497 newaction = freelist;
drh75897232000-05-29 14:26:00 +0000498 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +0000499 return newaction;
drh75897232000-05-29 14:26:00 +0000500}
501
drhe9278182007-07-18 18:16:29 +0000502/* Compare two actions for sorting purposes. Return negative, zero, or
503** positive if the first action is less than, equal to, or greater than
504** the first
505*/
506static int actioncmp(
507 struct action *ap1,
508 struct action *ap2
509){
drh75897232000-05-29 14:26:00 +0000510 int rc;
511 rc = ap1->sp->index - ap2->sp->index;
drh75897232000-05-29 14:26:00 +0000512 if( rc==0 ){
drh9892c5d2007-12-21 00:02:11 +0000513 rc = (int)ap1->type - (int)ap2->type;
514 }
drh3bd48ab2015-09-07 18:23:37 +0000515 if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){
drh75897232000-05-29 14:26:00 +0000516 rc = ap1->x.rp->index - ap2->x.rp->index;
517 }
drhe594bc32009-11-03 13:02:25 +0000518 if( rc==0 ){
icculus7b429aa2010-03-03 17:09:01 +0000519 rc = (int) (ap2 - ap1);
drhe594bc32009-11-03 13:02:25 +0000520 }
drh75897232000-05-29 14:26:00 +0000521 return rc;
522}
523
524/* Sort parser actions */
drhe9278182007-07-18 18:16:29 +0000525static struct action *Action_sort(
526 struct action *ap
527){
528 ap = (struct action *)msort((char *)ap,(char **)&ap->next,
529 (int(*)(const char*,const char*))actioncmp);
drh75897232000-05-29 14:26:00 +0000530 return ap;
531}
532
icculus9e44cf12010-02-14 17:14:22 +0000533void Action_add(
534 struct action **app,
535 enum e_action type,
536 struct symbol *sp,
537 char *arg
538){
539 struct action *newaction;
540 newaction = Action_new();
541 newaction->next = *app;
542 *app = newaction;
543 newaction->type = type;
544 newaction->sp = sp;
drhc173ad82016-05-23 16:15:02 +0000545 newaction->spOpt = 0;
drh75897232000-05-29 14:26:00 +0000546 if( type==SHIFT ){
icculus9e44cf12010-02-14 17:14:22 +0000547 newaction->x.stp = (struct state *)arg;
drh75897232000-05-29 14:26:00 +0000548 }else{
icculus9e44cf12010-02-14 17:14:22 +0000549 newaction->x.rp = (struct rule *)arg;
drh75897232000-05-29 14:26:00 +0000550 }
551}
drh8b582012003-10-21 13:16:03 +0000552/********************** New code to implement the "acttab" module ***********/
553/*
554** This module implements routines use to construct the yy_action[] table.
555*/
556
557/*
558** The state of the yy_action table under construction is an instance of
drh8dc3e8f2010-01-07 03:53:03 +0000559** the following structure.
560**
561** The yy_action table maps the pair (state_number, lookahead) into an
562** action_number. The table is an array of integers pairs. The state_number
563** determines an initial offset into the yy_action array. The lookahead
564** value is then added to this initial offset to get an index X into the
565** yy_action array. If the aAction[X].lookahead equals the value of the
566** of the lookahead input, then the value of the action_number output is
567** aAction[X].action. If the lookaheads do not match then the
568** default action for the state_number is returned.
569**
570** All actions associated with a single state_number are first entered
drh06f60d82017-04-14 19:46:12 +0000571** into aLookahead[] using multiple calls to acttab_action(). Then the
572** actions for that single state_number are placed into the aAction[]
drh8dc3e8f2010-01-07 03:53:03 +0000573** array with a single call to acttab_insert(). The acttab_insert() call
574** also resets the aLookahead[] array in preparation for the next
575** state number.
drh8b582012003-10-21 13:16:03 +0000576*/
icculus9e44cf12010-02-14 17:14:22 +0000577struct lookahead_action {
578 int lookahead; /* Value of the lookahead token */
579 int action; /* Action to take on the given lookahead */
580};
drh8b582012003-10-21 13:16:03 +0000581typedef struct acttab acttab;
582struct acttab {
583 int nAction; /* Number of used slots in aAction[] */
584 int nActionAlloc; /* Slots allocated for aAction[] */
icculus9e44cf12010-02-14 17:14:22 +0000585 struct lookahead_action
586 *aAction, /* The yy_action[] table under construction */
drh8b582012003-10-21 13:16:03 +0000587 *aLookahead; /* A single new transaction set */
588 int mnLookahead; /* Minimum aLookahead[].lookahead */
589 int mnAction; /* Action associated with mnLookahead */
590 int mxLookahead; /* Maximum aLookahead[].lookahead */
591 int nLookahead; /* Used slots in aLookahead[] */
592 int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
drh3a9d6c72017-12-25 04:15:38 +0000593 int nterminal; /* Number of terminal symbols */
594 int nsymbol; /* total number of symbols */
drh8b582012003-10-21 13:16:03 +0000595};
596
597/* Return the number of entries in the yy_action table */
drh3a9d6c72017-12-25 04:15:38 +0000598#define acttab_lookahead_size(X) ((X)->nAction)
drh8b582012003-10-21 13:16:03 +0000599
600/* The value for the N-th entry in yy_action */
601#define acttab_yyaction(X,N) ((X)->aAction[N].action)
602
603/* The value for the N-th entry in yy_lookahead */
604#define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
605
606/* Free all memory associated with the given acttab */
607void acttab_free(acttab *p){
608 free( p->aAction );
609 free( p->aLookahead );
610 free( p );
611}
612
613/* Allocate a new acttab structure */
drh3a9d6c72017-12-25 04:15:38 +0000614acttab *acttab_alloc(int nsymbol, int nterminal){
icculus9e44cf12010-02-14 17:14:22 +0000615 acttab *p = (acttab *) calloc( 1, sizeof(*p) );
drh8b582012003-10-21 13:16:03 +0000616 if( p==0 ){
617 fprintf(stderr,"Unable to allocate memory for a new acttab.");
618 exit(1);
619 }
620 memset(p, 0, sizeof(*p));
drh3a9d6c72017-12-25 04:15:38 +0000621 p->nsymbol = nsymbol;
622 p->nterminal = nterminal;
drh8b582012003-10-21 13:16:03 +0000623 return p;
624}
625
drh06f60d82017-04-14 19:46:12 +0000626/* Add a new action to the current transaction set.
drh8dc3e8f2010-01-07 03:53:03 +0000627**
628** This routine is called once for each lookahead for a particular
629** state.
drh8b582012003-10-21 13:16:03 +0000630*/
631void acttab_action(acttab *p, int lookahead, int action){
632 if( p->nLookahead>=p->nLookaheadAlloc ){
633 p->nLookaheadAlloc += 25;
icculus9e44cf12010-02-14 17:14:22 +0000634 p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
drh8b582012003-10-21 13:16:03 +0000635 sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
636 if( p->aLookahead==0 ){
637 fprintf(stderr,"malloc failed\n");
638 exit(1);
639 }
640 }
641 if( p->nLookahead==0 ){
642 p->mxLookahead = lookahead;
643 p->mnLookahead = lookahead;
644 p->mnAction = action;
645 }else{
646 if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
647 if( p->mnLookahead>lookahead ){
648 p->mnLookahead = lookahead;
649 p->mnAction = action;
650 }
651 }
652 p->aLookahead[p->nLookahead].lookahead = lookahead;
653 p->aLookahead[p->nLookahead].action = action;
654 p->nLookahead++;
655}
656
657/*
658** Add the transaction set built up with prior calls to acttab_action()
659** into the current action table. Then reset the transaction set back
660** to an empty set in preparation for a new round of acttab_action() calls.
661**
662** Return the offset into the action table of the new transaction.
drh3a9d6c72017-12-25 04:15:38 +0000663**
664** If the makeItSafe parameter is true, then the offset is chosen so that
665** it is impossible to overread the yy_lookaside[] table regardless of
666** the lookaside token. This is done for the terminal symbols, as they
667** come from external inputs and can contain syntax errors. When makeItSafe
668** is false, there is more flexibility in selecting offsets, resulting in
669** a smaller table. For non-terminal symbols, which are never syntax errors,
670** makeItSafe can be false.
drh8b582012003-10-21 13:16:03 +0000671*/
drh3a9d6c72017-12-25 04:15:38 +0000672int acttab_insert(acttab *p, int makeItSafe){
673 int i, j, k, n, end;
drh8b582012003-10-21 13:16:03 +0000674 assert( p->nLookahead>0 );
675
676 /* Make sure we have enough space to hold the expanded action table
677 ** in the worst case. The worst case occurs if the transaction set
678 ** must be appended to the current action table
679 */
drh3a9d6c72017-12-25 04:15:38 +0000680 n = p->nsymbol + 1;
drh8dc3e8f2010-01-07 03:53:03 +0000681 if( p->nAction + n >= p->nActionAlloc ){
drhfdbf9282003-10-21 16:34:41 +0000682 int oldAlloc = p->nActionAlloc;
drh8b582012003-10-21 13:16:03 +0000683 p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
icculus9e44cf12010-02-14 17:14:22 +0000684 p->aAction = (struct lookahead_action *) realloc( p->aAction,
drh8b582012003-10-21 13:16:03 +0000685 sizeof(p->aAction[0])*p->nActionAlloc);
686 if( p->aAction==0 ){
687 fprintf(stderr,"malloc failed\n");
688 exit(1);
689 }
drhfdbf9282003-10-21 16:34:41 +0000690 for(i=oldAlloc; i<p->nActionAlloc; i++){
drh8b582012003-10-21 13:16:03 +0000691 p->aAction[i].lookahead = -1;
692 p->aAction[i].action = -1;
693 }
694 }
695
drh06f60d82017-04-14 19:46:12 +0000696 /* Scan the existing action table looking for an offset that is a
drh8dc3e8f2010-01-07 03:53:03 +0000697 ** duplicate of the current transaction set. Fall out of the loop
698 ** if and when the duplicate is found.
drh8b582012003-10-21 13:16:03 +0000699 **
700 ** i is the index in p->aAction[] where p->mnLookahead is inserted.
701 */
drh3a9d6c72017-12-25 04:15:38 +0000702 end = makeItSafe ? p->mnLookahead : 0;
703 for(i=p->nAction-1; i>=end; i--){
drhf16371d2009-11-03 19:18:31 +0000704 if( p->aAction[i].lookahead==p->mnLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000705 /* All lookaheads and actions in the aLookahead[] transaction
706 ** must match against the candidate aAction[i] entry. */
drh8b582012003-10-21 13:16:03 +0000707 if( p->aAction[i].action!=p->mnAction ) continue;
708 for(j=0; j<p->nLookahead; j++){
709 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
710 if( k<0 || k>=p->nAction ) break;
711 if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
712 if( p->aLookahead[j].action!=p->aAction[k].action ) break;
713 }
714 if( j<p->nLookahead ) continue;
drh8dc3e8f2010-01-07 03:53:03 +0000715
716 /* No possible lookahead value that is not in the aLookahead[]
717 ** transaction is allowed to match aAction[i] */
drh8b582012003-10-21 13:16:03 +0000718 n = 0;
719 for(j=0; j<p->nAction; j++){
drhfdbf9282003-10-21 16:34:41 +0000720 if( p->aAction[j].lookahead<0 ) continue;
721 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
drh8b582012003-10-21 13:16:03 +0000722 }
drhfdbf9282003-10-21 16:34:41 +0000723 if( n==p->nLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000724 break; /* An exact match is found at offset i */
drhfdbf9282003-10-21 16:34:41 +0000725 }
drh8b582012003-10-21 13:16:03 +0000726 }
727 }
drh8dc3e8f2010-01-07 03:53:03 +0000728
729 /* If no existing offsets exactly match the current transaction, find an
730 ** an empty offset in the aAction[] table in which we can add the
731 ** aLookahead[] transaction.
732 */
drh3a9d6c72017-12-25 04:15:38 +0000733 if( i<end ){
drh8dc3e8f2010-01-07 03:53:03 +0000734 /* Look for holes in the aAction[] table that fit the current
735 ** aLookahead[] transaction. Leave i set to the offset of the hole.
736 ** If no holes are found, i is left at p->nAction, which means the
737 ** transaction will be appended. */
drh3a9d6c72017-12-25 04:15:38 +0000738 i = makeItSafe ? p->mnLookahead : 0;
739 for(; i<p->nActionAlloc - p->mxLookahead; i++){
drhf16371d2009-11-03 19:18:31 +0000740 if( p->aAction[i].lookahead<0 ){
741 for(j=0; j<p->nLookahead; j++){
742 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
743 if( k<0 ) break;
744 if( p->aAction[k].lookahead>=0 ) break;
745 }
746 if( j<p->nLookahead ) continue;
747 for(j=0; j<p->nAction; j++){
748 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
749 }
750 if( j==p->nAction ){
751 break; /* Fits in empty slots */
752 }
753 }
754 }
755 }
drh8b582012003-10-21 13:16:03 +0000756 /* Insert transaction set at index i. */
drh3a9d6c72017-12-25 04:15:38 +0000757#if 0
758 printf("Acttab:");
759 for(j=0; j<p->nLookahead; j++){
760 printf(" %d", p->aLookahead[j].lookahead);
761 }
762 printf(" inserted at %d\n", i);
763#endif
drh8b582012003-10-21 13:16:03 +0000764 for(j=0; j<p->nLookahead; j++){
765 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
766 p->aAction[k] = p->aLookahead[j];
767 if( k>=p->nAction ) p->nAction = k+1;
768 }
drh4396c612017-12-27 15:21:16 +0000769 if( makeItSafe && i+p->nterminal>=p->nAction ) p->nAction = i+p->nterminal+1;
drh8b582012003-10-21 13:16:03 +0000770 p->nLookahead = 0;
771
772 /* Return the offset that is added to the lookahead in order to get the
773 ** index into yy_action of the action */
774 return i - p->mnLookahead;
775}
776
drh3a9d6c72017-12-25 04:15:38 +0000777/*
778** Return the size of the action table without the trailing syntax error
779** entries.
780*/
781int acttab_action_size(acttab *p){
782 int n = p->nAction;
783 while( n>0 && p->aAction[n-1].lookahead<0 ){ n--; }
784 return n;
785}
786
drh75897232000-05-29 14:26:00 +0000787/********************** From the file "build.c" *****************************/
788/*
789** Routines to construction the finite state machine for the LEMON
790** parser generator.
791*/
792
793/* Find a precedence symbol of every rule in the grammar.
drh06f60d82017-04-14 19:46:12 +0000794**
drh75897232000-05-29 14:26:00 +0000795** Those rules which have a precedence symbol coded in the input
796** grammar using the "[symbol]" construct will already have the
797** rp->precsym field filled. Other rules take as their precedence
798** symbol the first RHS symbol with a defined precedence. If there
799** are not RHS symbols with a defined precedence, the precedence
800** symbol field is left blank.
801*/
icculus9e44cf12010-02-14 17:14:22 +0000802void FindRulePrecedences(struct lemon *xp)
drh75897232000-05-29 14:26:00 +0000803{
804 struct rule *rp;
805 for(rp=xp->rule; rp; rp=rp->next){
806 if( rp->precsym==0 ){
drhfd405312005-11-06 04:06:59 +0000807 int i, j;
808 for(i=0; i<rp->nrhs && rp->precsym==0; i++){
809 struct symbol *sp = rp->rhs[i];
810 if( sp->type==MULTITERMINAL ){
811 for(j=0; j<sp->nsubsym; j++){
812 if( sp->subsym[j]->prec>=0 ){
813 rp->precsym = sp->subsym[j];
814 break;
815 }
816 }
817 }else if( sp->prec>=0 ){
drh75897232000-05-29 14:26:00 +0000818 rp->precsym = rp->rhs[i];
drhf2f105d2012-08-20 15:53:54 +0000819 }
drh75897232000-05-29 14:26:00 +0000820 }
821 }
822 }
823 return;
824}
825
826/* Find all nonterminals which will generate the empty string.
827** Then go back and compute the first sets of every nonterminal.
828** The first set is the set of all terminal symbols which can begin
829** a string generated by that nonterminal.
830*/
icculus9e44cf12010-02-14 17:14:22 +0000831void FindFirstSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000832{
drhfd405312005-11-06 04:06:59 +0000833 int i, j;
drh75897232000-05-29 14:26:00 +0000834 struct rule *rp;
835 int progress;
836
837 for(i=0; i<lemp->nsymbol; i++){
drhaa9f1122007-08-23 02:50:56 +0000838 lemp->symbols[i]->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +0000839 }
840 for(i=lemp->nterminal; i<lemp->nsymbol; i++){
841 lemp->symbols[i]->firstset = SetNew();
842 }
843
844 /* First compute all lambdas */
845 do{
846 progress = 0;
847 for(rp=lemp->rule; rp; rp=rp->next){
848 if( rp->lhs->lambda ) continue;
849 for(i=0; i<rp->nrhs; i++){
drh7dd1ac62012-01-07 15:17:18 +0000850 struct symbol *sp = rp->rhs[i];
851 assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE );
852 if( sp->lambda==LEMON_FALSE ) break;
drh75897232000-05-29 14:26:00 +0000853 }
854 if( i==rp->nrhs ){
drhaa9f1122007-08-23 02:50:56 +0000855 rp->lhs->lambda = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +0000856 progress = 1;
857 }
858 }
859 }while( progress );
860
861 /* Now compute all first sets */
862 do{
863 struct symbol *s1, *s2;
864 progress = 0;
865 for(rp=lemp->rule; rp; rp=rp->next){
866 s1 = rp->lhs;
867 for(i=0; i<rp->nrhs; i++){
868 s2 = rp->rhs[i];
869 if( s2->type==TERMINAL ){
870 progress += SetAdd(s1->firstset,s2->index);
871 break;
drhfd405312005-11-06 04:06:59 +0000872 }else if( s2->type==MULTITERMINAL ){
873 for(j=0; j<s2->nsubsym; j++){
874 progress += SetAdd(s1->firstset,s2->subsym[j]->index);
875 }
876 break;
drhf2f105d2012-08-20 15:53:54 +0000877 }else if( s1==s2 ){
drhaa9f1122007-08-23 02:50:56 +0000878 if( s1->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000879 }else{
drh75897232000-05-29 14:26:00 +0000880 progress += SetUnion(s1->firstset,s2->firstset);
drhaa9f1122007-08-23 02:50:56 +0000881 if( s2->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000882 }
drh75897232000-05-29 14:26:00 +0000883 }
884 }
885 }while( progress );
886 return;
887}
888
889/* Compute all LR(0) states for the grammar. Links
890** are added to between some states so that the LR(1) follow sets
891** can be computed later.
892*/
icculus9e44cf12010-02-14 17:14:22 +0000893PRIVATE struct state *getstate(struct lemon *); /* forward reference */
894void FindStates(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000895{
896 struct symbol *sp;
897 struct rule *rp;
898
899 Configlist_init();
900
901 /* Find the start symbol */
902 if( lemp->start ){
903 sp = Symbol_find(lemp->start);
904 if( sp==0 ){
905 ErrorMsg(lemp->filename,0,
906"The specified start symbol \"%s\" is not \
907in a nonterminal of the grammar. \"%s\" will be used as the start \
drh4ef07702016-03-16 19:45:54 +0000908symbol instead.",lemp->start,lemp->startRule->lhs->name);
drh75897232000-05-29 14:26:00 +0000909 lemp->errorcnt++;
drh4ef07702016-03-16 19:45:54 +0000910 sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +0000911 }
912 }else{
drh4ef07702016-03-16 19:45:54 +0000913 sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +0000914 }
915
916 /* Make sure the start symbol doesn't occur on the right-hand side of
917 ** any rule. Report an error if it does. (YACC would generate a new
918 ** start symbol in this case.) */
919 for(rp=lemp->rule; rp; rp=rp->next){
920 int i;
921 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +0000922 if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */
drh75897232000-05-29 14:26:00 +0000923 ErrorMsg(lemp->filename,0,
924"The start symbol \"%s\" occurs on the \
925right-hand side of a rule. This will result in a parser which \
926does not work properly.",sp->name);
927 lemp->errorcnt++;
928 }
929 }
930 }
931
932 /* The basis configuration set for the first state
933 ** is all rules which have the start symbol as their
934 ** left-hand side */
935 for(rp=sp->rule; rp; rp=rp->nextlhs){
936 struct config *newcfp;
drhb4960992007-10-05 16:16:36 +0000937 rp->lhsStart = 1;
drh75897232000-05-29 14:26:00 +0000938 newcfp = Configlist_addbasis(rp,0);
939 SetAdd(newcfp->fws,0);
940 }
941
942 /* Compute the first state. All other states will be
943 ** computed automatically during the computation of the first one.
944 ** The returned pointer to the first state is not used. */
945 (void)getstate(lemp);
946 return;
947}
948
949/* Return a pointer to a state which is described by the configuration
950** list which has been built from calls to Configlist_add.
951*/
icculus9e44cf12010-02-14 17:14:22 +0000952PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
953PRIVATE struct state *getstate(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000954{
955 struct config *cfp, *bp;
956 struct state *stp;
957
958 /* Extract the sorted basis of the new state. The basis was constructed
959 ** by prior calls to "Configlist_addbasis()". */
960 Configlist_sortbasis();
961 bp = Configlist_basis();
962
963 /* Get a state with the same basis */
964 stp = State_find(bp);
965 if( stp ){
966 /* A state with the same basis already exists! Copy all the follow-set
967 ** propagation links from the state under construction into the
968 ** preexisting state, then return a pointer to the preexisting state */
969 struct config *x, *y;
970 for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
971 Plink_copy(&y->bplp,x->bplp);
972 Plink_delete(x->fplp);
973 x->fplp = x->bplp = 0;
974 }
975 cfp = Configlist_return();
976 Configlist_eat(cfp);
977 }else{
978 /* This really is a new state. Construct all the details */
979 Configlist_closure(lemp); /* Compute the configuration closure */
980 Configlist_sort(); /* Sort the configuration closure */
981 cfp = Configlist_return(); /* Get a pointer to the config list */
982 stp = State_new(); /* A new state structure */
983 MemoryCheck(stp);
984 stp->bp = bp; /* Remember the configuration basis */
985 stp->cfp = cfp; /* Remember the configuration closure */
drhada354d2005-11-05 15:03:59 +0000986 stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
drh75897232000-05-29 14:26:00 +0000987 stp->ap = 0; /* No actions, yet. */
988 State_insert(stp,stp->bp); /* Add to the state table */
989 buildshifts(lemp,stp); /* Recursively compute successor states */
990 }
991 return stp;
992}
993
drhfd405312005-11-06 04:06:59 +0000994/*
995** Return true if two symbols are the same.
996*/
icculus9e44cf12010-02-14 17:14:22 +0000997int same_symbol(struct symbol *a, struct symbol *b)
drhfd405312005-11-06 04:06:59 +0000998{
999 int i;
1000 if( a==b ) return 1;
1001 if( a->type!=MULTITERMINAL ) return 0;
1002 if( b->type!=MULTITERMINAL ) return 0;
1003 if( a->nsubsym!=b->nsubsym ) return 0;
1004 for(i=0; i<a->nsubsym; i++){
1005 if( a->subsym[i]!=b->subsym[i] ) return 0;
1006 }
1007 return 1;
1008}
1009
drh75897232000-05-29 14:26:00 +00001010/* Construct all successor states to the given state. A "successor"
1011** state is any state which can be reached by a shift action.
1012*/
icculus9e44cf12010-02-14 17:14:22 +00001013PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
drh75897232000-05-29 14:26:00 +00001014{
1015 struct config *cfp; /* For looping thru the config closure of "stp" */
1016 struct config *bcfp; /* For the inner loop on config closure of "stp" */
icculus9e44cf12010-02-14 17:14:22 +00001017 struct config *newcfg; /* */
drh75897232000-05-29 14:26:00 +00001018 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
1019 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
1020 struct state *newstp; /* A pointer to a successor state */
1021
1022 /* Each configuration becomes complete after it contibutes to a successor
1023 ** state. Initially, all configurations are incomplete */
1024 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
1025
1026 /* Loop through all configurations of the state "stp" */
1027 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1028 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
1029 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
1030 Configlist_reset(); /* Reset the new config set */
1031 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
1032
1033 /* For every configuration in the state "stp" which has the symbol "sp"
1034 ** following its dot, add the same configuration to the basis set under
1035 ** construction but with the dot shifted one symbol to the right. */
1036 for(bcfp=cfp; bcfp; bcfp=bcfp->next){
1037 if( bcfp->status==COMPLETE ) continue; /* Already used */
1038 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
1039 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
drhfd405312005-11-06 04:06:59 +00001040 if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
drh75897232000-05-29 14:26:00 +00001041 bcfp->status = COMPLETE; /* Mark this config as used */
icculus9e44cf12010-02-14 17:14:22 +00001042 newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
1043 Plink_add(&newcfg->bplp,bcfp);
drh75897232000-05-29 14:26:00 +00001044 }
1045
1046 /* Get a pointer to the state described by the basis configuration set
1047 ** constructed in the preceding loop */
1048 newstp = getstate(lemp);
1049
1050 /* The state "newstp" is reached from the state "stp" by a shift action
1051 ** on the symbol "sp" */
drhfd405312005-11-06 04:06:59 +00001052 if( sp->type==MULTITERMINAL ){
1053 int i;
1054 for(i=0; i<sp->nsubsym; i++){
1055 Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
1056 }
1057 }else{
1058 Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
1059 }
drh75897232000-05-29 14:26:00 +00001060 }
1061}
1062
1063/*
1064** Construct the propagation links
1065*/
icculus9e44cf12010-02-14 17:14:22 +00001066void FindLinks(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001067{
1068 int i;
1069 struct config *cfp, *other;
1070 struct state *stp;
1071 struct plink *plp;
1072
1073 /* Housekeeping detail:
1074 ** Add to every propagate link a pointer back to the state to
1075 ** which the link is attached. */
1076 for(i=0; i<lemp->nstate; i++){
1077 stp = lemp->sorted[i];
1078 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1079 cfp->stp = stp;
1080 }
1081 }
1082
1083 /* Convert all backlinks into forward links. Only the forward
1084 ** links are used in the follow-set computation. */
1085 for(i=0; i<lemp->nstate; i++){
1086 stp = lemp->sorted[i];
1087 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1088 for(plp=cfp->bplp; plp; plp=plp->next){
1089 other = plp->cfp;
1090 Plink_add(&other->fplp,cfp);
1091 }
1092 }
1093 }
1094}
1095
1096/* Compute all followsets.
1097**
1098** A followset is the set of all symbols which can come immediately
1099** after a configuration.
1100*/
icculus9e44cf12010-02-14 17:14:22 +00001101void FindFollowSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001102{
1103 int i;
1104 struct config *cfp;
1105 struct plink *plp;
1106 int progress;
1107 int change;
1108
1109 for(i=0; i<lemp->nstate; i++){
1110 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1111 cfp->status = INCOMPLETE;
1112 }
1113 }
drh06f60d82017-04-14 19:46:12 +00001114
drh75897232000-05-29 14:26:00 +00001115 do{
1116 progress = 0;
1117 for(i=0; i<lemp->nstate; i++){
1118 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1119 if( cfp->status==COMPLETE ) continue;
1120 for(plp=cfp->fplp; plp; plp=plp->next){
1121 change = SetUnion(plp->cfp->fws,cfp->fws);
1122 if( change ){
1123 plp->cfp->status = INCOMPLETE;
1124 progress = 1;
drhf2f105d2012-08-20 15:53:54 +00001125 }
1126 }
drh75897232000-05-29 14:26:00 +00001127 cfp->status = COMPLETE;
1128 }
1129 }
1130 }while( progress );
1131}
1132
drh3cb2f6e2012-01-09 14:19:05 +00001133static int resolve_conflict(struct action *,struct action *);
drh75897232000-05-29 14:26:00 +00001134
1135/* Compute the reduce actions, and resolve conflicts.
1136*/
icculus9e44cf12010-02-14 17:14:22 +00001137void FindActions(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001138{
1139 int i,j;
1140 struct config *cfp;
1141 struct state *stp;
1142 struct symbol *sp;
1143 struct rule *rp;
1144
drh06f60d82017-04-14 19:46:12 +00001145 /* Add all of the reduce actions
drh75897232000-05-29 14:26:00 +00001146 ** A reduce action is added for each element of the followset of
1147 ** a configuration which has its dot at the extreme right.
1148 */
1149 for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
1150 stp = lemp->sorted[i];
1151 for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
1152 if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
1153 for(j=0; j<lemp->nterminal; j++){
1154 if( SetFind(cfp->fws,j) ){
1155 /* Add a reduce action to the state "stp" which will reduce by the
1156 ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
drh218dc692004-05-31 23:13:45 +00001157 Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
drh75897232000-05-29 14:26:00 +00001158 }
drhf2f105d2012-08-20 15:53:54 +00001159 }
drh75897232000-05-29 14:26:00 +00001160 }
1161 }
1162 }
1163
1164 /* Add the accepting token */
1165 if( lemp->start ){
1166 sp = Symbol_find(lemp->start);
drh4ef07702016-03-16 19:45:54 +00001167 if( sp==0 ) sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +00001168 }else{
drh4ef07702016-03-16 19:45:54 +00001169 sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +00001170 }
1171 /* Add to the first state (which is always the starting state of the
1172 ** finite state machine) an action to ACCEPT if the lookahead is the
1173 ** start nonterminal. */
1174 Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
1175
1176 /* Resolve conflicts */
1177 for(i=0; i<lemp->nstate; i++){
1178 struct action *ap, *nap;
drh75897232000-05-29 14:26:00 +00001179 stp = lemp->sorted[i];
drhe9278182007-07-18 18:16:29 +00001180 /* assert( stp->ap ); */
drh75897232000-05-29 14:26:00 +00001181 stp->ap = Action_sort(stp->ap);
drhb59499c2002-02-23 18:45:13 +00001182 for(ap=stp->ap; ap && ap->next; ap=ap->next){
drh75897232000-05-29 14:26:00 +00001183 for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
1184 /* The two actions "ap" and "nap" have the same lookahead.
1185 ** Figure out which one should be used */
drh3cb2f6e2012-01-09 14:19:05 +00001186 lemp->nconflict += resolve_conflict(ap,nap);
drh75897232000-05-29 14:26:00 +00001187 }
1188 }
1189 }
1190
1191 /* Report an error for each rule that can never be reduced. */
drhaa9f1122007-08-23 02:50:56 +00001192 for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00001193 for(i=0; i<lemp->nstate; i++){
1194 struct action *ap;
1195 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
drhaa9f1122007-08-23 02:50:56 +00001196 if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +00001197 }
1198 }
1199 for(rp=lemp->rule; rp; rp=rp->next){
1200 if( rp->canReduce ) continue;
1201 ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
1202 lemp->errorcnt++;
1203 }
1204}
1205
1206/* Resolve a conflict between the two given actions. If the
drh34ff57b2008-07-14 12:27:51 +00001207** conflict can't be resolved, return non-zero.
drh75897232000-05-29 14:26:00 +00001208**
1209** NO LONGER TRUE:
1210** To resolve a conflict, first look to see if either action
1211** is on an error rule. In that case, take the action which
1212** is not associated with the error rule. If neither or both
1213** actions are associated with an error rule, then try to
1214** use precedence to resolve the conflict.
1215**
1216** If either action is a SHIFT, then it must be apx. This
1217** function won't work if apx->type==REDUCE and apy->type==SHIFT.
1218*/
icculus9e44cf12010-02-14 17:14:22 +00001219static int resolve_conflict(
1220 struct action *apx,
drh3cb2f6e2012-01-09 14:19:05 +00001221 struct action *apy
icculus9e44cf12010-02-14 17:14:22 +00001222){
drh75897232000-05-29 14:26:00 +00001223 struct symbol *spx, *spy;
1224 int errcnt = 0;
1225 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
drhf0fa1c12006-12-14 01:06:22 +00001226 if( apx->type==SHIFT && apy->type==SHIFT ){
drh9892c5d2007-12-21 00:02:11 +00001227 apy->type = SSCONFLICT;
drhf0fa1c12006-12-14 01:06:22 +00001228 errcnt++;
1229 }
drh75897232000-05-29 14:26:00 +00001230 if( apx->type==SHIFT && apy->type==REDUCE ){
1231 spx = apx->sp;
1232 spy = apy->x.rp->precsym;
1233 if( spy==0 || spx->prec<0 || spy->prec<0 ){
1234 /* Not enough precedence information. */
drh9892c5d2007-12-21 00:02:11 +00001235 apy->type = SRCONFLICT;
drh75897232000-05-29 14:26:00 +00001236 errcnt++;
drhdd7e9db2010-07-19 01:52:07 +00001237 }else if( spx->prec>spy->prec ){ /* higher precedence wins */
drh75897232000-05-29 14:26:00 +00001238 apy->type = RD_RESOLVED;
1239 }else if( spx->prec<spy->prec ){
1240 apx->type = SH_RESOLVED;
1241 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
1242 apy->type = RD_RESOLVED; /* associativity */
1243 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
1244 apx->type = SH_RESOLVED;
1245 }else{
1246 assert( spx->prec==spy->prec && spx->assoc==NONE );
drh62a223e2014-06-09 13:11:40 +00001247 apx->type = ERROR;
drh75897232000-05-29 14:26:00 +00001248 }
1249 }else if( apx->type==REDUCE && apy->type==REDUCE ){
1250 spx = apx->x.rp->precsym;
1251 spy = apy->x.rp->precsym;
1252 if( spx==0 || spy==0 || spx->prec<0 ||
1253 spy->prec<0 || spx->prec==spy->prec ){
drh9892c5d2007-12-21 00:02:11 +00001254 apy->type = RRCONFLICT;
drh75897232000-05-29 14:26:00 +00001255 errcnt++;
1256 }else if( spx->prec>spy->prec ){
1257 apy->type = RD_RESOLVED;
1258 }else if( spx->prec<spy->prec ){
1259 apx->type = RD_RESOLVED;
1260 }
1261 }else{
drh06f60d82017-04-14 19:46:12 +00001262 assert(
drhb59499c2002-02-23 18:45:13 +00001263 apx->type==SH_RESOLVED ||
1264 apx->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001265 apx->type==SSCONFLICT ||
1266 apx->type==SRCONFLICT ||
1267 apx->type==RRCONFLICT ||
drhb59499c2002-02-23 18:45:13 +00001268 apy->type==SH_RESOLVED ||
1269 apy->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001270 apy->type==SSCONFLICT ||
1271 apy->type==SRCONFLICT ||
1272 apy->type==RRCONFLICT
drhb59499c2002-02-23 18:45:13 +00001273 );
1274 /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
1275 ** REDUCEs on the list. If we reach this point it must be because
1276 ** the parser conflict had already been resolved. */
drh75897232000-05-29 14:26:00 +00001277 }
1278 return errcnt;
1279}
1280/********************* From the file "configlist.c" *************************/
1281/*
1282** Routines to processing a configuration list and building a state
1283** in the LEMON parser generator.
1284*/
1285
1286static struct config *freelist = 0; /* List of free configurations */
1287static struct config *current = 0; /* Top of list of configurations */
1288static struct config **currentend = 0; /* Last on list of configs */
1289static struct config *basis = 0; /* Top of list of basis configs */
1290static struct config **basisend = 0; /* End of list of basis configs */
1291
1292/* Return a pointer to a new configuration */
drh14d88552017-04-14 19:44:15 +00001293PRIVATE struct config *newconfig(void){
icculus9e44cf12010-02-14 17:14:22 +00001294 struct config *newcfg;
drh75897232000-05-29 14:26:00 +00001295 if( freelist==0 ){
1296 int i;
1297 int amt = 3;
drh9892c5d2007-12-21 00:02:11 +00001298 freelist = (struct config *)calloc( amt, sizeof(struct config) );
drh75897232000-05-29 14:26:00 +00001299 if( freelist==0 ){
1300 fprintf(stderr,"Unable to allocate memory for a new configuration.");
1301 exit(1);
1302 }
1303 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
1304 freelist[amt-1].next = 0;
1305 }
icculus9e44cf12010-02-14 17:14:22 +00001306 newcfg = freelist;
drh75897232000-05-29 14:26:00 +00001307 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00001308 return newcfg;
drh75897232000-05-29 14:26:00 +00001309}
1310
1311/* The configuration "old" is no longer used */
icculus9e44cf12010-02-14 17:14:22 +00001312PRIVATE void deleteconfig(struct config *old)
drh75897232000-05-29 14:26:00 +00001313{
1314 old->next = freelist;
1315 freelist = old;
1316}
1317
1318/* Initialized the configuration list builder */
drh14d88552017-04-14 19:44:15 +00001319void Configlist_init(void){
drh75897232000-05-29 14:26:00 +00001320 current = 0;
1321 currentend = &current;
1322 basis = 0;
1323 basisend = &basis;
1324 Configtable_init();
1325 return;
1326}
1327
1328/* Initialized the configuration list builder */
drh14d88552017-04-14 19:44:15 +00001329void Configlist_reset(void){
drh75897232000-05-29 14:26:00 +00001330 current = 0;
1331 currentend = &current;
1332 basis = 0;
1333 basisend = &basis;
1334 Configtable_clear(0);
1335 return;
1336}
1337
1338/* Add another configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001339struct config *Configlist_add(
1340 struct rule *rp, /* The rule */
1341 int dot /* Index into the RHS of the rule where the dot goes */
1342){
drh75897232000-05-29 14:26:00 +00001343 struct config *cfp, model;
1344
1345 assert( currentend!=0 );
1346 model.rp = rp;
1347 model.dot = dot;
1348 cfp = Configtable_find(&model);
1349 if( cfp==0 ){
1350 cfp = newconfig();
1351 cfp->rp = rp;
1352 cfp->dot = dot;
1353 cfp->fws = SetNew();
1354 cfp->stp = 0;
1355 cfp->fplp = cfp->bplp = 0;
1356 cfp->next = 0;
1357 cfp->bp = 0;
1358 *currentend = cfp;
1359 currentend = &cfp->next;
1360 Configtable_insert(cfp);
1361 }
1362 return cfp;
1363}
1364
1365/* Add a basis configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001366struct config *Configlist_addbasis(struct rule *rp, int dot)
drh75897232000-05-29 14:26:00 +00001367{
1368 struct config *cfp, model;
1369
1370 assert( basisend!=0 );
1371 assert( currentend!=0 );
1372 model.rp = rp;
1373 model.dot = dot;
1374 cfp = Configtable_find(&model);
1375 if( cfp==0 ){
1376 cfp = newconfig();
1377 cfp->rp = rp;
1378 cfp->dot = dot;
1379 cfp->fws = SetNew();
1380 cfp->stp = 0;
1381 cfp->fplp = cfp->bplp = 0;
1382 cfp->next = 0;
1383 cfp->bp = 0;
1384 *currentend = cfp;
1385 currentend = &cfp->next;
1386 *basisend = cfp;
1387 basisend = &cfp->bp;
1388 Configtable_insert(cfp);
1389 }
1390 return cfp;
1391}
1392
1393/* Compute the closure of the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001394void Configlist_closure(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001395{
1396 struct config *cfp, *newcfp;
1397 struct rule *rp, *newrp;
1398 struct symbol *sp, *xsp;
1399 int i, dot;
1400
1401 assert( currentend!=0 );
1402 for(cfp=current; cfp; cfp=cfp->next){
1403 rp = cfp->rp;
1404 dot = cfp->dot;
1405 if( dot>=rp->nrhs ) continue;
1406 sp = rp->rhs[dot];
1407 if( sp->type==NONTERMINAL ){
1408 if( sp->rule==0 && sp!=lemp->errsym ){
1409 ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
1410 sp->name);
1411 lemp->errorcnt++;
1412 }
1413 for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
1414 newcfp = Configlist_add(newrp,0);
1415 for(i=dot+1; i<rp->nrhs; i++){
1416 xsp = rp->rhs[i];
1417 if( xsp->type==TERMINAL ){
1418 SetAdd(newcfp->fws,xsp->index);
1419 break;
drhfd405312005-11-06 04:06:59 +00001420 }else if( xsp->type==MULTITERMINAL ){
1421 int k;
1422 for(k=0; k<xsp->nsubsym; k++){
1423 SetAdd(newcfp->fws, xsp->subsym[k]->index);
1424 }
1425 break;
drhf2f105d2012-08-20 15:53:54 +00001426 }else{
drh75897232000-05-29 14:26:00 +00001427 SetUnion(newcfp->fws,xsp->firstset);
drhaa9f1122007-08-23 02:50:56 +00001428 if( xsp->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +00001429 }
1430 }
drh75897232000-05-29 14:26:00 +00001431 if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
1432 }
1433 }
1434 }
1435 return;
1436}
1437
1438/* Sort the configuration list */
drh14d88552017-04-14 19:44:15 +00001439void Configlist_sort(void){
drh25473362015-09-04 18:03:45 +00001440 current = (struct config*)msort((char*)current,(char**)&(current->next),
1441 Configcmp);
drh75897232000-05-29 14:26:00 +00001442 currentend = 0;
1443 return;
1444}
1445
1446/* Sort the basis configuration list */
drh14d88552017-04-14 19:44:15 +00001447void Configlist_sortbasis(void){
drh25473362015-09-04 18:03:45 +00001448 basis = (struct config*)msort((char*)current,(char**)&(current->bp),
1449 Configcmp);
drh75897232000-05-29 14:26:00 +00001450 basisend = 0;
1451 return;
1452}
1453
1454/* Return a pointer to the head of the configuration list and
1455** reset the list */
drh14d88552017-04-14 19:44:15 +00001456struct config *Configlist_return(void){
drh75897232000-05-29 14:26:00 +00001457 struct config *old;
1458 old = current;
1459 current = 0;
1460 currentend = 0;
1461 return old;
1462}
1463
1464/* Return a pointer to the head of the configuration list and
1465** reset the list */
drh14d88552017-04-14 19:44:15 +00001466struct config *Configlist_basis(void){
drh75897232000-05-29 14:26:00 +00001467 struct config *old;
1468 old = basis;
1469 basis = 0;
1470 basisend = 0;
1471 return old;
1472}
1473
1474/* Free all elements of the given configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001475void Configlist_eat(struct config *cfp)
drh75897232000-05-29 14:26:00 +00001476{
1477 struct config *nextcfp;
1478 for(; cfp; cfp=nextcfp){
1479 nextcfp = cfp->next;
1480 assert( cfp->fplp==0 );
1481 assert( cfp->bplp==0 );
1482 if( cfp->fws ) SetFree(cfp->fws);
1483 deleteconfig(cfp);
1484 }
1485 return;
1486}
1487/***************** From the file "error.c" *********************************/
1488/*
1489** Code for printing error message.
1490*/
1491
drhf9a2e7b2003-04-15 01:49:48 +00001492void ErrorMsg(const char *filename, int lineno, const char *format, ...){
icculus15a2cec2010-02-16 16:07:28 +00001493 va_list ap;
icculus1c11f742010-02-15 00:01:04 +00001494 fprintf(stderr, "%s:%d: ", filename, lineno);
1495 va_start(ap, format);
1496 vfprintf(stderr,format,ap);
1497 va_end(ap);
1498 fprintf(stderr, "\n");
drh75897232000-05-29 14:26:00 +00001499}
1500/**************** From the file "main.c" ************************************/
1501/*
1502** Main program file for the LEMON parser generator.
1503*/
1504
1505/* Report an out-of-memory condition and abort. This function
1506** is used mostly by the "MemoryCheck" macro in struct.h
1507*/
drh14d88552017-04-14 19:44:15 +00001508void memory_error(void){
drh75897232000-05-29 14:26:00 +00001509 fprintf(stderr,"Out of memory. Aborting...\n");
1510 exit(1);
1511}
1512
drh6d08b4d2004-07-20 12:45:22 +00001513static int nDefine = 0; /* Number of -D options on the command line */
1514static char **azDefine = 0; /* Name of the -D macros */
1515
1516/* This routine is called with the argument to each -D command-line option.
1517** Add the macro defined to the azDefine array.
1518*/
1519static void handle_D_option(char *z){
1520 char **paz;
1521 nDefine++;
icculus9e44cf12010-02-14 17:14:22 +00001522 azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine);
drh6d08b4d2004-07-20 12:45:22 +00001523 if( azDefine==0 ){
1524 fprintf(stderr,"out of memory\n");
1525 exit(1);
1526 }
1527 paz = &azDefine[nDefine-1];
icculus9e44cf12010-02-14 17:14:22 +00001528 *paz = (char *) malloc( lemonStrlen(z)+1 );
drh6d08b4d2004-07-20 12:45:22 +00001529 if( *paz==0 ){
1530 fprintf(stderr,"out of memory\n");
1531 exit(1);
1532 }
drh898799f2014-01-10 23:21:00 +00001533 lemon_strcpy(*paz, z);
drh6d08b4d2004-07-20 12:45:22 +00001534 for(z=*paz; *z && *z!='='; z++){}
1535 *z = 0;
1536}
1537
icculus3e143bd2010-02-14 00:48:49 +00001538static char *user_templatename = NULL;
1539static void handle_T_option(char *z){
icculus9e44cf12010-02-14 17:14:22 +00001540 user_templatename = (char *) malloc( lemonStrlen(z)+1 );
icculus3e143bd2010-02-14 00:48:49 +00001541 if( user_templatename==0 ){
1542 memory_error();
1543 }
drh898799f2014-01-10 23:21:00 +00001544 lemon_strcpy(user_templatename, z);
icculus3e143bd2010-02-14 00:48:49 +00001545}
drh75897232000-05-29 14:26:00 +00001546
drh711c9812016-05-23 14:24:31 +00001547/* Merge together to lists of rules ordered by rule.iRule */
drh4ef07702016-03-16 19:45:54 +00001548static struct rule *Rule_merge(struct rule *pA, struct rule *pB){
1549 struct rule *pFirst = 0;
1550 struct rule **ppPrev = &pFirst;
1551 while( pA && pB ){
1552 if( pA->iRule<pB->iRule ){
1553 *ppPrev = pA;
1554 ppPrev = &pA->next;
1555 pA = pA->next;
1556 }else{
1557 *ppPrev = pB;
1558 ppPrev = &pB->next;
1559 pB = pB->next;
1560 }
1561 }
1562 if( pA ){
1563 *ppPrev = pA;
1564 }else{
1565 *ppPrev = pB;
1566 }
1567 return pFirst;
1568}
1569
1570/*
1571** Sort a list of rules in order of increasing iRule value
1572*/
1573static struct rule *Rule_sort(struct rule *rp){
1574 int i;
1575 struct rule *pNext;
1576 struct rule *x[32];
1577 memset(x, 0, sizeof(x));
1578 while( rp ){
1579 pNext = rp->next;
1580 rp->next = 0;
1581 for(i=0; i<sizeof(x)/sizeof(x[0]) && x[i]; i++){
1582 rp = Rule_merge(x[i], rp);
1583 x[i] = 0;
1584 }
1585 x[i] = rp;
1586 rp = pNext;
1587 }
1588 rp = 0;
1589 for(i=0; i<sizeof(x)/sizeof(x[0]); i++){
1590 rp = Rule_merge(x[i], rp);
1591 }
1592 return rp;
1593}
1594
drhc75e0162015-09-07 02:23:02 +00001595/* forward reference */
1596static const char *minimum_size_type(int lwr, int upr, int *pnByte);
1597
1598/* Print a single line of the "Parser Stats" output
1599*/
1600static void stats_line(const char *zLabel, int iValue){
1601 int nLabel = lemonStrlen(zLabel);
1602 printf(" %s%.*s %5d\n", zLabel,
1603 35-nLabel, "................................",
1604 iValue);
1605}
1606
drh75897232000-05-29 14:26:00 +00001607/* The main program. Parse the command line and do it... */
icculus9e44cf12010-02-14 17:14:22 +00001608int main(int argc, char **argv)
drh75897232000-05-29 14:26:00 +00001609{
1610 static int version = 0;
1611 static int rpflag = 0;
1612 static int basisflag = 0;
1613 static int compress = 0;
1614 static int quiet = 0;
1615 static int statistics = 0;
1616 static int mhflag = 0;
shane58543932008-12-10 20:10:04 +00001617 static int nolinenosflag = 0;
drhdd7e9db2010-07-19 01:52:07 +00001618 static int noResort = 0;
drh75897232000-05-29 14:26:00 +00001619 static struct s_options options[] = {
1620 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1621 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
drh6d08b4d2004-07-20 12:45:22 +00001622 {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
drh0325d392015-01-01 19:11:22 +00001623 {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"},
drh75897232000-05-29 14:26:00 +00001624 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
drh0325d392015-01-01 19:11:22 +00001625 {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"},
shane58543932008-12-10 20:10:04 +00001626 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
1627 {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
drh0325d392015-01-01 19:11:22 +00001628 {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"},
drhf5c4e0f2010-07-18 11:35:53 +00001629 {OPT_FLAG, "p", (char*)&showPrecedenceConflict,
1630 "Show conflicts resolved by precedence rules"},
drh75897232000-05-29 14:26:00 +00001631 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
drhdd7e9db2010-07-19 01:52:07 +00001632 {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"},
drh6d08b4d2004-07-20 12:45:22 +00001633 {OPT_FLAG, "s", (char*)&statistics,
1634 "Print parser stats to standard output."},
drh75897232000-05-29 14:26:00 +00001635 {OPT_FLAG, "x", (char*)&version, "Print the version number."},
drh0325d392015-01-01 19:11:22 +00001636 {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
1637 {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"},
drh75897232000-05-29 14:26:00 +00001638 {OPT_FLAG,0,0,0}
1639 };
1640 int i;
icculus42585cf2010-02-14 05:19:56 +00001641 int exitcode;
drh75897232000-05-29 14:26:00 +00001642 struct lemon lem;
drh4ef07702016-03-16 19:45:54 +00001643 struct rule *rp;
drh75897232000-05-29 14:26:00 +00001644
drhb0c86772000-06-02 23:21:26 +00001645 OptInit(argv,options,stderr);
drh75897232000-05-29 14:26:00 +00001646 if( version ){
drhb19a2bc2001-09-16 00:13:26 +00001647 printf("Lemon version 1.0\n");
drh06f60d82017-04-14 19:46:12 +00001648 exit(0);
drh75897232000-05-29 14:26:00 +00001649 }
drhb0c86772000-06-02 23:21:26 +00001650 if( OptNArgs()!=1 ){
drh75897232000-05-29 14:26:00 +00001651 fprintf(stderr,"Exactly one filename argument is required.\n");
1652 exit(1);
1653 }
drh954f6b42006-06-13 13:27:46 +00001654 memset(&lem, 0, sizeof(lem));
drh75897232000-05-29 14:26:00 +00001655 lem.errorcnt = 0;
1656
1657 /* Initialize the machine */
1658 Strsafe_init();
1659 Symbol_init();
1660 State_init();
1661 lem.argv0 = argv[0];
drhb0c86772000-06-02 23:21:26 +00001662 lem.filename = OptArg(0);
drh75897232000-05-29 14:26:00 +00001663 lem.basisflag = basisflag;
shane58543932008-12-10 20:10:04 +00001664 lem.nolinenosflag = nolinenosflag;
drh75897232000-05-29 14:26:00 +00001665 Symbol_new("$");
1666 lem.errsym = Symbol_new("error");
drhc4dd3fd2008-01-22 01:48:05 +00001667 lem.errsym->useCnt = 0;
drh75897232000-05-29 14:26:00 +00001668
1669 /* Parse the input file */
1670 Parse(&lem);
1671 if( lem.errorcnt ) exit(lem.errorcnt);
drh954f6b42006-06-13 13:27:46 +00001672 if( lem.nrule==0 ){
drh75897232000-05-29 14:26:00 +00001673 fprintf(stderr,"Empty grammar.\n");
1674 exit(1);
1675 }
1676
1677 /* Count and index the symbols of the grammar */
drh75897232000-05-29 14:26:00 +00001678 Symbol_new("{default}");
drh61f92cd2014-01-11 03:06:18 +00001679 lem.nsymbol = Symbol_count();
drh75897232000-05-29 14:26:00 +00001680 lem.symbols = Symbol_arrayof();
drh61f92cd2014-01-11 03:06:18 +00001681 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1682 qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp);
1683 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1684 while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; }
1685 assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 );
1686 lem.nsymbol = i - 1;
drhc56fac72015-10-29 13:48:15 +00001687 for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++);
drh75897232000-05-29 14:26:00 +00001688 lem.nterminal = i;
1689
drh711c9812016-05-23 14:24:31 +00001690 /* Assign sequential rule numbers. Start with 0. Put rules that have no
1691 ** reduce action C-code associated with them last, so that the switch()
1692 ** statement that selects reduction actions will have a smaller jump table.
1693 */
drh4ef07702016-03-16 19:45:54 +00001694 for(i=0, rp=lem.rule; rp; rp=rp->next){
1695 rp->iRule = rp->code ? i++ : -1;
1696 }
1697 for(rp=lem.rule; rp; rp=rp->next){
1698 if( rp->iRule<0 ) rp->iRule = i++;
1699 }
1700 lem.startRule = lem.rule;
1701 lem.rule = Rule_sort(lem.rule);
1702
drh75897232000-05-29 14:26:00 +00001703 /* Generate a reprint of the grammar, if requested on the command line */
1704 if( rpflag ){
1705 Reprint(&lem);
1706 }else{
1707 /* Initialize the size for all follow and first sets */
drh9892c5d2007-12-21 00:02:11 +00001708 SetSize(lem.nterminal+1);
drh75897232000-05-29 14:26:00 +00001709
1710 /* Find the precedence for every production rule (that has one) */
1711 FindRulePrecedences(&lem);
1712
1713 /* Compute the lambda-nonterminals and the first-sets for every
1714 ** nonterminal */
1715 FindFirstSets(&lem);
1716
1717 /* Compute all LR(0) states. Also record follow-set propagation
1718 ** links so that the follow-set can be computed later */
1719 lem.nstate = 0;
1720 FindStates(&lem);
1721 lem.sorted = State_arrayof();
1722
1723 /* Tie up loose ends on the propagation links */
1724 FindLinks(&lem);
1725
1726 /* Compute the follow set of every reducible configuration */
1727 FindFollowSets(&lem);
1728
1729 /* Compute the action tables */
1730 FindActions(&lem);
1731
1732 /* Compress the action tables */
1733 if( compress==0 ) CompressTables(&lem);
1734
drhada354d2005-11-05 15:03:59 +00001735 /* Reorder and renumber the states so that states with fewer choices
drhdd7e9db2010-07-19 01:52:07 +00001736 ** occur at the end. This is an optimization that helps make the
1737 ** generated parser tables smaller. */
1738 if( noResort==0 ) ResortStates(&lem);
drhada354d2005-11-05 15:03:59 +00001739
drh75897232000-05-29 14:26:00 +00001740 /* Generate a report of the parser generated. (the "y.output" file) */
1741 if( !quiet ) ReportOutput(&lem);
1742
1743 /* Generate the source code for the parser */
1744 ReportTable(&lem, mhflag);
1745
1746 /* Produce a header file for use by the scanner. (This step is
1747 ** omitted if the "-m" option is used because makeheaders will
1748 ** generate the file for us.) */
1749 if( !mhflag ) ReportHeader(&lem);
1750 }
1751 if( statistics ){
drhc75e0162015-09-07 02:23:02 +00001752 printf("Parser statistics:\n");
1753 stats_line("terminal symbols", lem.nterminal);
1754 stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal);
1755 stats_line("total symbols", lem.nsymbol);
1756 stats_line("rules", lem.nrule);
drh3bd48ab2015-09-07 18:23:37 +00001757 stats_line("states", lem.nxstate);
drhc75e0162015-09-07 02:23:02 +00001758 stats_line("conflicts", lem.nconflict);
1759 stats_line("action table entries", lem.nactiontab);
drh3a9d6c72017-12-25 04:15:38 +00001760 stats_line("lookahead table entries", lem.nlookaheadtab);
drhc75e0162015-09-07 02:23:02 +00001761 stats_line("total table size (bytes)", lem.tablesize);
drh75897232000-05-29 14:26:00 +00001762 }
icculus8e158022010-02-16 16:09:03 +00001763 if( lem.nconflict > 0 ){
1764 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
icculus42585cf2010-02-14 05:19:56 +00001765 }
1766
1767 /* return 0 on success, 1 on failure. */
icculus8e158022010-02-16 16:09:03 +00001768 exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
icculus42585cf2010-02-14 05:19:56 +00001769 exit(exitcode);
1770 return (exitcode);
drh75897232000-05-29 14:26:00 +00001771}
1772/******************** From the file "msort.c" *******************************/
1773/*
1774** A generic merge-sort program.
1775**
1776** USAGE:
1777** Let "ptr" be a pointer to some structure which is at the head of
1778** a null-terminated list. Then to sort the list call:
1779**
1780** ptr = msort(ptr,&(ptr->next),cmpfnc);
1781**
1782** In the above, "cmpfnc" is a pointer to a function which compares
1783** two instances of the structure and returns an integer, as in
1784** strcmp. The second argument is a pointer to the pointer to the
1785** second element of the linked list. This address is used to compute
1786** the offset to the "next" field within the structure. The offset to
1787** the "next" field must be constant for all structures in the list.
1788**
1789** The function returns a new pointer which is the head of the list
1790** after sorting.
1791**
1792** ALGORITHM:
1793** Merge-sort.
1794*/
1795
1796/*
1797** Return a pointer to the next structure in the linked list.
1798*/
drhd25d6922012-04-18 09:59:56 +00001799#define NEXT(A) (*(char**)(((char*)A)+offset))
drh75897232000-05-29 14:26:00 +00001800
1801/*
1802** Inputs:
1803** a: A sorted, null-terminated linked list. (May be null).
1804** b: A sorted, null-terminated linked list. (May be null).
1805** cmp: A pointer to the comparison function.
1806** offset: Offset in the structure to the "next" field.
1807**
1808** Return Value:
1809** A pointer to the head of a sorted list containing the elements
1810** of both a and b.
1811**
1812** Side effects:
1813** The "next" pointers for elements in the lists a and b are
1814** changed.
1815*/
drhe9278182007-07-18 18:16:29 +00001816static char *merge(
1817 char *a,
1818 char *b,
1819 int (*cmp)(const char*,const char*),
1820 int offset
1821){
drh75897232000-05-29 14:26:00 +00001822 char *ptr, *head;
1823
1824 if( a==0 ){
1825 head = b;
1826 }else if( b==0 ){
1827 head = a;
1828 }else{
drhe594bc32009-11-03 13:02:25 +00001829 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001830 ptr = a;
1831 a = NEXT(a);
1832 }else{
1833 ptr = b;
1834 b = NEXT(b);
1835 }
1836 head = ptr;
1837 while( a && b ){
drhe594bc32009-11-03 13:02:25 +00001838 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001839 NEXT(ptr) = a;
1840 ptr = a;
1841 a = NEXT(a);
1842 }else{
1843 NEXT(ptr) = b;
1844 ptr = b;
1845 b = NEXT(b);
1846 }
1847 }
1848 if( a ) NEXT(ptr) = a;
1849 else NEXT(ptr) = b;
1850 }
1851 return head;
1852}
1853
1854/*
1855** Inputs:
1856** list: Pointer to a singly-linked list of structures.
1857** next: Pointer to pointer to the second element of the list.
1858** cmp: A comparison function.
1859**
1860** Return Value:
1861** A pointer to the head of a sorted list containing the elements
1862** orginally in list.
1863**
1864** Side effects:
1865** The "next" pointers for elements in list are changed.
1866*/
1867#define LISTSIZE 30
drhe9278182007-07-18 18:16:29 +00001868static char *msort(
1869 char *list,
1870 char **next,
1871 int (*cmp)(const char*,const char*)
1872){
drhba99af52001-10-25 20:37:16 +00001873 unsigned long offset;
drh75897232000-05-29 14:26:00 +00001874 char *ep;
1875 char *set[LISTSIZE];
1876 int i;
drh1cc0d112015-03-31 15:15:48 +00001877 offset = (unsigned long)((char*)next - (char*)list);
drh75897232000-05-29 14:26:00 +00001878 for(i=0; i<LISTSIZE; i++) set[i] = 0;
1879 while( list ){
1880 ep = list;
1881 list = NEXT(list);
1882 NEXT(ep) = 0;
1883 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1884 ep = merge(ep,set[i],cmp,offset);
1885 set[i] = 0;
1886 }
1887 set[i] = ep;
1888 }
1889 ep = 0;
drhe594bc32009-11-03 13:02:25 +00001890 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset);
drh75897232000-05-29 14:26:00 +00001891 return ep;
1892}
1893/************************ From the file "option.c" **************************/
1894static char **argv;
1895static struct s_options *op;
1896static FILE *errstream;
1897
1898#define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1899
1900/*
1901** Print the command line with a carrot pointing to the k-th character
1902** of the n-th field.
1903*/
icculus9e44cf12010-02-14 17:14:22 +00001904static void errline(int n, int k, FILE *err)
drh75897232000-05-29 14:26:00 +00001905{
1906 int spcnt, i;
drh75897232000-05-29 14:26:00 +00001907 if( argv[0] ) fprintf(err,"%s",argv[0]);
drh87cf1372008-08-13 20:09:06 +00001908 spcnt = lemonStrlen(argv[0]) + 1;
drh75897232000-05-29 14:26:00 +00001909 for(i=1; i<n && argv[i]; i++){
1910 fprintf(err," %s",argv[i]);
drh87cf1372008-08-13 20:09:06 +00001911 spcnt += lemonStrlen(argv[i])+1;
drh75897232000-05-29 14:26:00 +00001912 }
1913 spcnt += k;
1914 for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1915 if( spcnt<20 ){
1916 fprintf(err,"\n%*s^-- here\n",spcnt,"");
1917 }else{
1918 fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1919 }
1920}
1921
1922/*
1923** Return the index of the N-th non-switch argument. Return -1
1924** if N is out of range.
1925*/
icculus9e44cf12010-02-14 17:14:22 +00001926static int argindex(int n)
drh75897232000-05-29 14:26:00 +00001927{
1928 int i;
1929 int dashdash = 0;
1930 if( argv!=0 && *argv!=0 ){
1931 for(i=1; argv[i]; i++){
1932 if( dashdash || !ISOPT(argv[i]) ){
1933 if( n==0 ) return i;
1934 n--;
1935 }
1936 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1937 }
1938 }
1939 return -1;
1940}
1941
1942static char emsg[] = "Command line syntax error: ";
1943
1944/*
1945** Process a flag command line argument.
1946*/
icculus9e44cf12010-02-14 17:14:22 +00001947static int handleflags(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001948{
1949 int v;
1950 int errcnt = 0;
1951 int j;
1952 for(j=0; op[j].label; j++){
drh87cf1372008-08-13 20:09:06 +00001953 if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
drh75897232000-05-29 14:26:00 +00001954 }
1955 v = argv[i][0]=='-' ? 1 : 0;
1956 if( op[j].label==0 ){
1957 if( err ){
1958 fprintf(err,"%sundefined option.\n",emsg);
1959 errline(i,1,err);
1960 }
1961 errcnt++;
drh0325d392015-01-01 19:11:22 +00001962 }else if( op[j].arg==0 ){
1963 /* Ignore this option */
drh75897232000-05-29 14:26:00 +00001964 }else if( op[j].type==OPT_FLAG ){
1965 *((int*)op[j].arg) = v;
1966 }else if( op[j].type==OPT_FFLAG ){
icculus9e44cf12010-02-14 17:14:22 +00001967 (*(void(*)(int))(op[j].arg))(v);
drh6d08b4d2004-07-20 12:45:22 +00001968 }else if( op[j].type==OPT_FSTR ){
icculus9e44cf12010-02-14 17:14:22 +00001969 (*(void(*)(char *))(op[j].arg))(&argv[i][2]);
drh75897232000-05-29 14:26:00 +00001970 }else{
1971 if( err ){
1972 fprintf(err,"%smissing argument on switch.\n",emsg);
1973 errline(i,1,err);
1974 }
1975 errcnt++;
1976 }
1977 return errcnt;
1978}
1979
1980/*
1981** Process a command line switch which has an argument.
1982*/
icculus9e44cf12010-02-14 17:14:22 +00001983static int handleswitch(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001984{
1985 int lv = 0;
1986 double dv = 0.0;
1987 char *sv = 0, *end;
1988 char *cp;
1989 int j;
1990 int errcnt = 0;
1991 cp = strchr(argv[i],'=');
drh43617e92006-03-06 20:55:46 +00001992 assert( cp!=0 );
drh75897232000-05-29 14:26:00 +00001993 *cp = 0;
1994 for(j=0; op[j].label; j++){
1995 if( strcmp(argv[i],op[j].label)==0 ) break;
1996 }
1997 *cp = '=';
1998 if( op[j].label==0 ){
1999 if( err ){
2000 fprintf(err,"%sundefined option.\n",emsg);
2001 errline(i,0,err);
2002 }
2003 errcnt++;
2004 }else{
2005 cp++;
2006 switch( op[j].type ){
2007 case OPT_FLAG:
2008 case OPT_FFLAG:
2009 if( err ){
2010 fprintf(err,"%soption requires an argument.\n",emsg);
2011 errline(i,0,err);
2012 }
2013 errcnt++;
2014 break;
2015 case OPT_DBL:
2016 case OPT_FDBL:
2017 dv = strtod(cp,&end);
2018 if( *end ){
2019 if( err ){
drh25473362015-09-04 18:03:45 +00002020 fprintf(err,
2021 "%sillegal character in floating-point argument.\n",emsg);
drh1cc0d112015-03-31 15:15:48 +00002022 errline(i,(int)((char*)end-(char*)argv[i]),err);
drh75897232000-05-29 14:26:00 +00002023 }
2024 errcnt++;
2025 }
2026 break;
2027 case OPT_INT:
2028 case OPT_FINT:
2029 lv = strtol(cp,&end,0);
2030 if( *end ){
2031 if( err ){
2032 fprintf(err,"%sillegal character in integer argument.\n",emsg);
drh1cc0d112015-03-31 15:15:48 +00002033 errline(i,(int)((char*)end-(char*)argv[i]),err);
drh75897232000-05-29 14:26:00 +00002034 }
2035 errcnt++;
2036 }
2037 break;
2038 case OPT_STR:
2039 case OPT_FSTR:
2040 sv = cp;
2041 break;
2042 }
2043 switch( op[j].type ){
2044 case OPT_FLAG:
2045 case OPT_FFLAG:
2046 break;
2047 case OPT_DBL:
2048 *(double*)(op[j].arg) = dv;
2049 break;
2050 case OPT_FDBL:
icculus9e44cf12010-02-14 17:14:22 +00002051 (*(void(*)(double))(op[j].arg))(dv);
drh75897232000-05-29 14:26:00 +00002052 break;
2053 case OPT_INT:
2054 *(int*)(op[j].arg) = lv;
2055 break;
2056 case OPT_FINT:
icculus9e44cf12010-02-14 17:14:22 +00002057 (*(void(*)(int))(op[j].arg))((int)lv);
drh75897232000-05-29 14:26:00 +00002058 break;
2059 case OPT_STR:
2060 *(char**)(op[j].arg) = sv;
2061 break;
2062 case OPT_FSTR:
icculus9e44cf12010-02-14 17:14:22 +00002063 (*(void(*)(char *))(op[j].arg))(sv);
drh75897232000-05-29 14:26:00 +00002064 break;
2065 }
2066 }
2067 return errcnt;
2068}
2069
icculus9e44cf12010-02-14 17:14:22 +00002070int OptInit(char **a, struct s_options *o, FILE *err)
drh75897232000-05-29 14:26:00 +00002071{
2072 int errcnt = 0;
2073 argv = a;
2074 op = o;
2075 errstream = err;
2076 if( argv && *argv && op ){
2077 int i;
2078 for(i=1; argv[i]; i++){
2079 if( argv[i][0]=='+' || argv[i][0]=='-' ){
2080 errcnt += handleflags(i,err);
2081 }else if( strchr(argv[i],'=') ){
2082 errcnt += handleswitch(i,err);
2083 }
2084 }
2085 }
2086 if( errcnt>0 ){
2087 fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
drhb0c86772000-06-02 23:21:26 +00002088 OptPrint();
drh75897232000-05-29 14:26:00 +00002089 exit(1);
2090 }
2091 return 0;
2092}
2093
drh14d88552017-04-14 19:44:15 +00002094int OptNArgs(void){
drh75897232000-05-29 14:26:00 +00002095 int cnt = 0;
2096 int dashdash = 0;
2097 int i;
2098 if( argv!=0 && argv[0]!=0 ){
2099 for(i=1; argv[i]; i++){
2100 if( dashdash || !ISOPT(argv[i]) ) cnt++;
2101 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
2102 }
2103 }
2104 return cnt;
2105}
2106
icculus9e44cf12010-02-14 17:14:22 +00002107char *OptArg(int n)
drh75897232000-05-29 14:26:00 +00002108{
2109 int i;
2110 i = argindex(n);
2111 return i>=0 ? argv[i] : 0;
2112}
2113
icculus9e44cf12010-02-14 17:14:22 +00002114void OptErr(int n)
drh75897232000-05-29 14:26:00 +00002115{
2116 int i;
2117 i = argindex(n);
2118 if( i>=0 ) errline(i,0,errstream);
2119}
2120
drh14d88552017-04-14 19:44:15 +00002121void OptPrint(void){
drh75897232000-05-29 14:26:00 +00002122 int i;
2123 int max, len;
2124 max = 0;
2125 for(i=0; op[i].label; i++){
drh87cf1372008-08-13 20:09:06 +00002126 len = lemonStrlen(op[i].label) + 1;
drh75897232000-05-29 14:26:00 +00002127 switch( op[i].type ){
2128 case OPT_FLAG:
2129 case OPT_FFLAG:
2130 break;
2131 case OPT_INT:
2132 case OPT_FINT:
2133 len += 9; /* length of "<integer>" */
2134 break;
2135 case OPT_DBL:
2136 case OPT_FDBL:
2137 len += 6; /* length of "<real>" */
2138 break;
2139 case OPT_STR:
2140 case OPT_FSTR:
2141 len += 8; /* length of "<string>" */
2142 break;
2143 }
2144 if( len>max ) max = len;
2145 }
2146 for(i=0; op[i].label; i++){
2147 switch( op[i].type ){
2148 case OPT_FLAG:
2149 case OPT_FFLAG:
2150 fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
2151 break;
2152 case OPT_INT:
2153 case OPT_FINT:
drh0325d392015-01-01 19:11:22 +00002154 fprintf(errstream," -%s<integer>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002155 (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002156 break;
2157 case OPT_DBL:
2158 case OPT_FDBL:
drh0325d392015-01-01 19:11:22 +00002159 fprintf(errstream," -%s<real>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002160 (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002161 break;
2162 case OPT_STR:
2163 case OPT_FSTR:
drh0325d392015-01-01 19:11:22 +00002164 fprintf(errstream," -%s<string>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002165 (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002166 break;
2167 }
2168 }
2169}
2170/*********************** From the file "parse.c" ****************************/
2171/*
2172** Input file parser for the LEMON parser generator.
2173*/
2174
2175/* The state of the parser */
icculus9e44cf12010-02-14 17:14:22 +00002176enum e_state {
2177 INITIALIZE,
2178 WAITING_FOR_DECL_OR_RULE,
2179 WAITING_FOR_DECL_KEYWORD,
2180 WAITING_FOR_DECL_ARG,
2181 WAITING_FOR_PRECEDENCE_SYMBOL,
2182 WAITING_FOR_ARROW,
2183 IN_RHS,
2184 LHS_ALIAS_1,
2185 LHS_ALIAS_2,
2186 LHS_ALIAS_3,
2187 RHS_ALIAS_1,
2188 RHS_ALIAS_2,
2189 PRECEDENCE_MARK_1,
2190 PRECEDENCE_MARK_2,
2191 RESYNC_AFTER_RULE_ERROR,
2192 RESYNC_AFTER_DECL_ERROR,
2193 WAITING_FOR_DESTRUCTOR_SYMBOL,
2194 WAITING_FOR_DATATYPE_SYMBOL,
2195 WAITING_FOR_FALLBACK_ID,
drh61f92cd2014-01-11 03:06:18 +00002196 WAITING_FOR_WILDCARD_ID,
2197 WAITING_FOR_CLASS_ID,
drh59c435a2017-08-02 03:21:11 +00002198 WAITING_FOR_CLASS_TOKEN,
2199 WAITING_FOR_TOKEN_NAME
icculus9e44cf12010-02-14 17:14:22 +00002200};
drh75897232000-05-29 14:26:00 +00002201struct pstate {
2202 char *filename; /* Name of the input file */
2203 int tokenlineno; /* Linenumber at which current token starts */
2204 int errorcnt; /* Number of errors so far */
2205 char *tokenstart; /* Text of current token */
2206 struct lemon *gp; /* Global state vector */
icculus9e44cf12010-02-14 17:14:22 +00002207 enum e_state state; /* The state of the parser */
drh0bd1f4e2002-06-06 18:54:39 +00002208 struct symbol *fallback; /* The fallback token */
drh61f92cd2014-01-11 03:06:18 +00002209 struct symbol *tkclass; /* Token class symbol */
drh75897232000-05-29 14:26:00 +00002210 struct symbol *lhs; /* Left-hand side of current rule */
icculus9e44cf12010-02-14 17:14:22 +00002211 const char *lhsalias; /* Alias for the LHS */
drh75897232000-05-29 14:26:00 +00002212 int nrhs; /* Number of right-hand side symbols seen */
2213 struct symbol *rhs[MAXRHS]; /* RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +00002214 const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
drh75897232000-05-29 14:26:00 +00002215 struct rule *prevrule; /* Previous rule parsed */
icculus9e44cf12010-02-14 17:14:22 +00002216 const char *declkeyword; /* Keyword of a declaration */
drh75897232000-05-29 14:26:00 +00002217 char **declargslot; /* Where the declaration argument should be put */
drha5808f32008-04-27 22:19:44 +00002218 int insertLineMacro; /* Add #line before declaration insert */
drh4dc8ef52008-07-01 17:13:57 +00002219 int *decllinenoslot; /* Where to write declaration line number */
drh75897232000-05-29 14:26:00 +00002220 enum e_assoc declassoc; /* Assign this association to decl arguments */
2221 int preccounter; /* Assign this precedence to decl arguments */
2222 struct rule *firstrule; /* Pointer to first rule in the grammar */
2223 struct rule *lastrule; /* Pointer to the most recently parsed rule */
2224};
2225
2226/* Parse a single token */
icculus9e44cf12010-02-14 17:14:22 +00002227static void parseonetoken(struct pstate *psp)
drh75897232000-05-29 14:26:00 +00002228{
icculus9e44cf12010-02-14 17:14:22 +00002229 const char *x;
drh75897232000-05-29 14:26:00 +00002230 x = Strsafe(psp->tokenstart); /* Save the token permanently */
2231#if 0
2232 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
2233 x,psp->state);
2234#endif
2235 switch( psp->state ){
2236 case INITIALIZE:
2237 psp->prevrule = 0;
2238 psp->preccounter = 0;
2239 psp->firstrule = psp->lastrule = 0;
2240 psp->gp->nrule = 0;
2241 /* Fall thru to next case */
2242 case WAITING_FOR_DECL_OR_RULE:
2243 if( x[0]=='%' ){
2244 psp->state = WAITING_FOR_DECL_KEYWORD;
drhc56fac72015-10-29 13:48:15 +00002245 }else if( ISLOWER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002246 psp->lhs = Symbol_new(x);
2247 psp->nrhs = 0;
2248 psp->lhsalias = 0;
2249 psp->state = WAITING_FOR_ARROW;
2250 }else if( x[0]=='{' ){
2251 if( psp->prevrule==0 ){
2252 ErrorMsg(psp->filename,psp->tokenlineno,
drh3cb2f6e2012-01-09 14:19:05 +00002253"There is no prior rule upon which to attach the code \
drh75897232000-05-29 14:26:00 +00002254fragment which begins on this line.");
2255 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002256 }else if( psp->prevrule->code!=0 ){
drh75897232000-05-29 14:26:00 +00002257 ErrorMsg(psp->filename,psp->tokenlineno,
2258"Code fragment beginning on this line is not the first \
2259to follow the previous rule.");
2260 psp->errorcnt++;
2261 }else{
2262 psp->prevrule->line = psp->tokenlineno;
2263 psp->prevrule->code = &x[1];
drh711c9812016-05-23 14:24:31 +00002264 psp->prevrule->noCode = 0;
drhf2f105d2012-08-20 15:53:54 +00002265 }
drh75897232000-05-29 14:26:00 +00002266 }else if( x[0]=='[' ){
2267 psp->state = PRECEDENCE_MARK_1;
2268 }else{
2269 ErrorMsg(psp->filename,psp->tokenlineno,
2270 "Token \"%s\" should be either \"%%\" or a nonterminal name.",
2271 x);
2272 psp->errorcnt++;
2273 }
2274 break;
2275 case PRECEDENCE_MARK_1:
drhc56fac72015-10-29 13:48:15 +00002276 if( !ISUPPER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002277 ErrorMsg(psp->filename,psp->tokenlineno,
2278 "The precedence symbol must be a terminal.");
2279 psp->errorcnt++;
2280 }else if( psp->prevrule==0 ){
2281 ErrorMsg(psp->filename,psp->tokenlineno,
2282 "There is no prior rule to assign precedence \"[%s]\".",x);
2283 psp->errorcnt++;
2284 }else if( psp->prevrule->precsym!=0 ){
2285 ErrorMsg(psp->filename,psp->tokenlineno,
2286"Precedence mark on this line is not the first \
2287to follow the previous rule.");
2288 psp->errorcnt++;
2289 }else{
2290 psp->prevrule->precsym = Symbol_new(x);
2291 }
2292 psp->state = PRECEDENCE_MARK_2;
2293 break;
2294 case PRECEDENCE_MARK_2:
2295 if( x[0]!=']' ){
2296 ErrorMsg(psp->filename,psp->tokenlineno,
2297 "Missing \"]\" on precedence mark.");
2298 psp->errorcnt++;
2299 }
2300 psp->state = WAITING_FOR_DECL_OR_RULE;
2301 break;
2302 case WAITING_FOR_ARROW:
2303 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2304 psp->state = IN_RHS;
2305 }else if( x[0]=='(' ){
2306 psp->state = LHS_ALIAS_1;
2307 }else{
2308 ErrorMsg(psp->filename,psp->tokenlineno,
2309 "Expected to see a \":\" following the LHS symbol \"%s\".",
2310 psp->lhs->name);
2311 psp->errorcnt++;
2312 psp->state = RESYNC_AFTER_RULE_ERROR;
2313 }
2314 break;
2315 case LHS_ALIAS_1:
drhc56fac72015-10-29 13:48:15 +00002316 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002317 psp->lhsalias = x;
2318 psp->state = LHS_ALIAS_2;
2319 }else{
2320 ErrorMsg(psp->filename,psp->tokenlineno,
2321 "\"%s\" is not a valid alias for the LHS \"%s\"\n",
2322 x,psp->lhs->name);
2323 psp->errorcnt++;
2324 psp->state = RESYNC_AFTER_RULE_ERROR;
2325 }
2326 break;
2327 case LHS_ALIAS_2:
2328 if( x[0]==')' ){
2329 psp->state = LHS_ALIAS_3;
2330 }else{
2331 ErrorMsg(psp->filename,psp->tokenlineno,
2332 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2333 psp->errorcnt++;
2334 psp->state = RESYNC_AFTER_RULE_ERROR;
2335 }
2336 break;
2337 case LHS_ALIAS_3:
2338 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2339 psp->state = IN_RHS;
2340 }else{
2341 ErrorMsg(psp->filename,psp->tokenlineno,
2342 "Missing \"->\" following: \"%s(%s)\".",
2343 psp->lhs->name,psp->lhsalias);
2344 psp->errorcnt++;
2345 psp->state = RESYNC_AFTER_RULE_ERROR;
2346 }
2347 break;
2348 case IN_RHS:
2349 if( x[0]=='.' ){
2350 struct rule *rp;
drh06f60d82017-04-14 19:46:12 +00002351 rp = (struct rule *)calloc( sizeof(struct rule) +
drh9892c5d2007-12-21 00:02:11 +00002352 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
drh75897232000-05-29 14:26:00 +00002353 if( rp==0 ){
2354 ErrorMsg(psp->filename,psp->tokenlineno,
2355 "Can't allocate enough memory for this rule.");
2356 psp->errorcnt++;
2357 psp->prevrule = 0;
drhf2f105d2012-08-20 15:53:54 +00002358 }else{
drh75897232000-05-29 14:26:00 +00002359 int i;
2360 rp->ruleline = psp->tokenlineno;
2361 rp->rhs = (struct symbol**)&rp[1];
icculus9e44cf12010-02-14 17:14:22 +00002362 rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
drh75897232000-05-29 14:26:00 +00002363 for(i=0; i<psp->nrhs; i++){
2364 rp->rhs[i] = psp->rhs[i];
2365 rp->rhsalias[i] = psp->alias[i];
drhf2f105d2012-08-20 15:53:54 +00002366 }
drh75897232000-05-29 14:26:00 +00002367 rp->lhs = psp->lhs;
2368 rp->lhsalias = psp->lhsalias;
2369 rp->nrhs = psp->nrhs;
2370 rp->code = 0;
drh711c9812016-05-23 14:24:31 +00002371 rp->noCode = 1;
drh75897232000-05-29 14:26:00 +00002372 rp->precsym = 0;
2373 rp->index = psp->gp->nrule++;
2374 rp->nextlhs = rp->lhs->rule;
2375 rp->lhs->rule = rp;
2376 rp->next = 0;
2377 if( psp->firstrule==0 ){
2378 psp->firstrule = psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002379 }else{
drh75897232000-05-29 14:26:00 +00002380 psp->lastrule->next = rp;
2381 psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002382 }
drh75897232000-05-29 14:26:00 +00002383 psp->prevrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002384 }
drh75897232000-05-29 14:26:00 +00002385 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002386 }else if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002387 if( psp->nrhs>=MAXRHS ){
2388 ErrorMsg(psp->filename,psp->tokenlineno,
drhc4dd3fd2008-01-22 01:48:05 +00002389 "Too many symbols on RHS of rule beginning at \"%s\".",
drh75897232000-05-29 14:26:00 +00002390 x);
2391 psp->errorcnt++;
2392 psp->state = RESYNC_AFTER_RULE_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002393 }else{
drh75897232000-05-29 14:26:00 +00002394 psp->rhs[psp->nrhs] = Symbol_new(x);
2395 psp->alias[psp->nrhs] = 0;
2396 psp->nrhs++;
drhf2f105d2012-08-20 15:53:54 +00002397 }
drhfd405312005-11-06 04:06:59 +00002398 }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
2399 struct symbol *msp = psp->rhs[psp->nrhs-1];
2400 if( msp->type!=MULTITERMINAL ){
2401 struct symbol *origsp = msp;
icculus9e44cf12010-02-14 17:14:22 +00002402 msp = (struct symbol *) calloc(1,sizeof(*msp));
drhfd405312005-11-06 04:06:59 +00002403 memset(msp, 0, sizeof(*msp));
2404 msp->type = MULTITERMINAL;
2405 msp->nsubsym = 1;
icculus9e44cf12010-02-14 17:14:22 +00002406 msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*));
drhfd405312005-11-06 04:06:59 +00002407 msp->subsym[0] = origsp;
2408 msp->name = origsp->name;
2409 psp->rhs[psp->nrhs-1] = msp;
2410 }
2411 msp->nsubsym++;
icculus9e44cf12010-02-14 17:14:22 +00002412 msp->subsym = (struct symbol **) realloc(msp->subsym,
2413 sizeof(struct symbol*)*msp->nsubsym);
drhfd405312005-11-06 04:06:59 +00002414 msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
drhc56fac72015-10-29 13:48:15 +00002415 if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){
drhfd405312005-11-06 04:06:59 +00002416 ErrorMsg(psp->filename,psp->tokenlineno,
2417 "Cannot form a compound containing a non-terminal");
2418 psp->errorcnt++;
2419 }
drh75897232000-05-29 14:26:00 +00002420 }else if( x[0]=='(' && psp->nrhs>0 ){
2421 psp->state = RHS_ALIAS_1;
2422 }else{
2423 ErrorMsg(psp->filename,psp->tokenlineno,
2424 "Illegal character on RHS of rule: \"%s\".",x);
2425 psp->errorcnt++;
2426 psp->state = RESYNC_AFTER_RULE_ERROR;
2427 }
2428 break;
2429 case RHS_ALIAS_1:
drhc56fac72015-10-29 13:48:15 +00002430 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002431 psp->alias[psp->nrhs-1] = x;
2432 psp->state = RHS_ALIAS_2;
2433 }else{
2434 ErrorMsg(psp->filename,psp->tokenlineno,
2435 "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
2436 x,psp->rhs[psp->nrhs-1]->name);
2437 psp->errorcnt++;
2438 psp->state = RESYNC_AFTER_RULE_ERROR;
2439 }
2440 break;
2441 case RHS_ALIAS_2:
2442 if( x[0]==')' ){
2443 psp->state = IN_RHS;
2444 }else{
2445 ErrorMsg(psp->filename,psp->tokenlineno,
2446 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2447 psp->errorcnt++;
2448 psp->state = RESYNC_AFTER_RULE_ERROR;
2449 }
2450 break;
2451 case WAITING_FOR_DECL_KEYWORD:
drhc56fac72015-10-29 13:48:15 +00002452 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002453 psp->declkeyword = x;
2454 psp->declargslot = 0;
drh4dc8ef52008-07-01 17:13:57 +00002455 psp->decllinenoslot = 0;
drha5808f32008-04-27 22:19:44 +00002456 psp->insertLineMacro = 1;
drh75897232000-05-29 14:26:00 +00002457 psp->state = WAITING_FOR_DECL_ARG;
2458 if( strcmp(x,"name")==0 ){
2459 psp->declargslot = &(psp->gp->name);
drha5808f32008-04-27 22:19:44 +00002460 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002461 }else if( strcmp(x,"include")==0 ){
drh75897232000-05-29 14:26:00 +00002462 psp->declargslot = &(psp->gp->include);
drhf2f105d2012-08-20 15:53:54 +00002463 }else if( strcmp(x,"code")==0 ){
drh75897232000-05-29 14:26:00 +00002464 psp->declargslot = &(psp->gp->extracode);
drhf2f105d2012-08-20 15:53:54 +00002465 }else if( strcmp(x,"token_destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002466 psp->declargslot = &psp->gp->tokendest;
drhf2f105d2012-08-20 15:53:54 +00002467 }else if( strcmp(x,"default_destructor")==0 ){
drh960e8c62001-04-03 16:53:21 +00002468 psp->declargslot = &psp->gp->vardest;
drhf2f105d2012-08-20 15:53:54 +00002469 }else if( strcmp(x,"token_prefix")==0 ){
drh75897232000-05-29 14:26:00 +00002470 psp->declargslot = &psp->gp->tokenprefix;
drha5808f32008-04-27 22:19:44 +00002471 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002472 }else if( strcmp(x,"syntax_error")==0 ){
drh75897232000-05-29 14:26:00 +00002473 psp->declargslot = &(psp->gp->error);
drhf2f105d2012-08-20 15:53:54 +00002474 }else if( strcmp(x,"parse_accept")==0 ){
drh75897232000-05-29 14:26:00 +00002475 psp->declargslot = &(psp->gp->accept);
drhf2f105d2012-08-20 15:53:54 +00002476 }else if( strcmp(x,"parse_failure")==0 ){
drh75897232000-05-29 14:26:00 +00002477 psp->declargslot = &(psp->gp->failure);
drhf2f105d2012-08-20 15:53:54 +00002478 }else if( strcmp(x,"stack_overflow")==0 ){
drh75897232000-05-29 14:26:00 +00002479 psp->declargslot = &(psp->gp->overflow);
drh75897232000-05-29 14:26:00 +00002480 }else if( strcmp(x,"extra_argument")==0 ){
2481 psp->declargslot = &(psp->gp->arg);
drha5808f32008-04-27 22:19:44 +00002482 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002483 }else if( strcmp(x,"token_type")==0 ){
2484 psp->declargslot = &(psp->gp->tokentype);
drha5808f32008-04-27 22:19:44 +00002485 psp->insertLineMacro = 0;
drh960e8c62001-04-03 16:53:21 +00002486 }else if( strcmp(x,"default_type")==0 ){
2487 psp->declargslot = &(psp->gp->vartype);
drha5808f32008-04-27 22:19:44 +00002488 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002489 }else if( strcmp(x,"stack_size")==0 ){
2490 psp->declargslot = &(psp->gp->stacksize);
drha5808f32008-04-27 22:19:44 +00002491 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002492 }else if( strcmp(x,"start_symbol")==0 ){
2493 psp->declargslot = &(psp->gp->start);
drha5808f32008-04-27 22:19:44 +00002494 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002495 }else if( strcmp(x,"left")==0 ){
2496 psp->preccounter++;
2497 psp->declassoc = LEFT;
2498 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2499 }else if( strcmp(x,"right")==0 ){
2500 psp->preccounter++;
2501 psp->declassoc = RIGHT;
2502 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2503 }else if( strcmp(x,"nonassoc")==0 ){
2504 psp->preccounter++;
2505 psp->declassoc = NONE;
2506 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002507 }else if( strcmp(x,"destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002508 psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002509 }else if( strcmp(x,"type")==0 ){
drh75897232000-05-29 14:26:00 +00002510 psp->state = WAITING_FOR_DATATYPE_SYMBOL;
drh0bd1f4e2002-06-06 18:54:39 +00002511 }else if( strcmp(x,"fallback")==0 ){
2512 psp->fallback = 0;
2513 psp->state = WAITING_FOR_FALLBACK_ID;
drh59c435a2017-08-02 03:21:11 +00002514 }else if( strcmp(x,"token")==0 ){
2515 psp->state = WAITING_FOR_TOKEN_NAME;
drhe09daa92006-06-10 13:29:31 +00002516 }else if( strcmp(x,"wildcard")==0 ){
2517 psp->state = WAITING_FOR_WILDCARD_ID;
drh61f92cd2014-01-11 03:06:18 +00002518 }else if( strcmp(x,"token_class")==0 ){
2519 psp->state = WAITING_FOR_CLASS_ID;
drh75897232000-05-29 14:26:00 +00002520 }else{
2521 ErrorMsg(psp->filename,psp->tokenlineno,
2522 "Unknown declaration keyword: \"%%%s\".",x);
2523 psp->errorcnt++;
2524 psp->state = RESYNC_AFTER_DECL_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002525 }
drh75897232000-05-29 14:26:00 +00002526 }else{
2527 ErrorMsg(psp->filename,psp->tokenlineno,
2528 "Illegal declaration keyword: \"%s\".",x);
2529 psp->errorcnt++;
2530 psp->state = RESYNC_AFTER_DECL_ERROR;
2531 }
2532 break;
2533 case WAITING_FOR_DESTRUCTOR_SYMBOL:
drhc56fac72015-10-29 13:48:15 +00002534 if( !ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002535 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002536 "Symbol name missing after %%destructor keyword");
drh75897232000-05-29 14:26:00 +00002537 psp->errorcnt++;
2538 psp->state = RESYNC_AFTER_DECL_ERROR;
2539 }else{
icculusd286fa62010-03-03 17:06:32 +00002540 struct symbol *sp = Symbol_new(x);
2541 psp->declargslot = &sp->destructor;
2542 psp->decllinenoslot = &sp->destLineno;
2543 psp->insertLineMacro = 1;
2544 psp->state = WAITING_FOR_DECL_ARG;
drh75897232000-05-29 14:26:00 +00002545 }
2546 break;
2547 case WAITING_FOR_DATATYPE_SYMBOL:
drhc56fac72015-10-29 13:48:15 +00002548 if( !ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002549 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002550 "Symbol name missing after %%type keyword");
drh75897232000-05-29 14:26:00 +00002551 psp->errorcnt++;
2552 psp->state = RESYNC_AFTER_DECL_ERROR;
2553 }else{
icculus866bf1e2010-02-17 20:31:32 +00002554 struct symbol *sp = Symbol_find(x);
2555 if((sp) && (sp->datatype)){
2556 ErrorMsg(psp->filename,psp->tokenlineno,
2557 "Symbol %%type \"%s\" already defined", x);
2558 psp->errorcnt++;
2559 psp->state = RESYNC_AFTER_DECL_ERROR;
2560 }else{
2561 if (!sp){
2562 sp = Symbol_new(x);
2563 }
2564 psp->declargslot = &sp->datatype;
2565 psp->insertLineMacro = 0;
2566 psp->state = WAITING_FOR_DECL_ARG;
2567 }
drh75897232000-05-29 14:26:00 +00002568 }
2569 break;
2570 case WAITING_FOR_PRECEDENCE_SYMBOL:
2571 if( x[0]=='.' ){
2572 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002573 }else if( ISUPPER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002574 struct symbol *sp;
2575 sp = Symbol_new(x);
2576 if( sp->prec>=0 ){
2577 ErrorMsg(psp->filename,psp->tokenlineno,
2578 "Symbol \"%s\" has already be given a precedence.",x);
2579 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002580 }else{
drh75897232000-05-29 14:26:00 +00002581 sp->prec = psp->preccounter;
2582 sp->assoc = psp->declassoc;
drhf2f105d2012-08-20 15:53:54 +00002583 }
drh75897232000-05-29 14:26:00 +00002584 }else{
2585 ErrorMsg(psp->filename,psp->tokenlineno,
2586 "Can't assign a precedence to \"%s\".",x);
2587 psp->errorcnt++;
2588 }
2589 break;
2590 case WAITING_FOR_DECL_ARG:
drhc56fac72015-10-29 13:48:15 +00002591 if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){
icculus9e44cf12010-02-14 17:14:22 +00002592 const char *zOld, *zNew;
2593 char *zBuf, *z;
mistachkin2318d332015-01-12 18:02:52 +00002594 int nOld, n, nLine = 0, nNew, nBack;
drhb5bd49e2008-07-14 12:21:08 +00002595 int addLineMacro;
drha5808f32008-04-27 22:19:44 +00002596 char zLine[50];
2597 zNew = x;
2598 if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
drh87cf1372008-08-13 20:09:06 +00002599 nNew = lemonStrlen(zNew);
drha5808f32008-04-27 22:19:44 +00002600 if( *psp->declargslot ){
2601 zOld = *psp->declargslot;
2602 }else{
2603 zOld = "";
2604 }
drh87cf1372008-08-13 20:09:06 +00002605 nOld = lemonStrlen(zOld);
drha5808f32008-04-27 22:19:44 +00002606 n = nOld + nNew + 20;
shane58543932008-12-10 20:10:04 +00002607 addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro &&
drhb5bd49e2008-07-14 12:21:08 +00002608 (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
2609 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002610 for(z=psp->filename, nBack=0; *z; z++){
2611 if( *z=='\\' ) nBack++;
2612 }
drh898799f2014-01-10 23:21:00 +00002613 lemon_sprintf(zLine, "#line %d ", psp->tokenlineno);
drh87cf1372008-08-13 20:09:06 +00002614 nLine = lemonStrlen(zLine);
2615 n += nLine + lemonStrlen(psp->filename) + nBack;
drha5808f32008-04-27 22:19:44 +00002616 }
icculus9e44cf12010-02-14 17:14:22 +00002617 *psp->declargslot = (char *) realloc(*psp->declargslot, n);
2618 zBuf = *psp->declargslot + nOld;
drhb5bd49e2008-07-14 12:21:08 +00002619 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002620 if( nOld && zBuf[-1]!='\n' ){
2621 *(zBuf++) = '\n';
2622 }
2623 memcpy(zBuf, zLine, nLine);
2624 zBuf += nLine;
2625 *(zBuf++) = '"';
2626 for(z=psp->filename; *z; z++){
2627 if( *z=='\\' ){
2628 *(zBuf++) = '\\';
2629 }
2630 *(zBuf++) = *z;
2631 }
2632 *(zBuf++) = '"';
2633 *(zBuf++) = '\n';
2634 }
drh4dc8ef52008-07-01 17:13:57 +00002635 if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){
2636 psp->decllinenoslot[0] = psp->tokenlineno;
2637 }
drha5808f32008-04-27 22:19:44 +00002638 memcpy(zBuf, zNew, nNew);
2639 zBuf += nNew;
2640 *zBuf = 0;
2641 psp->state = WAITING_FOR_DECL_OR_RULE;
drh75897232000-05-29 14:26:00 +00002642 }else{
2643 ErrorMsg(psp->filename,psp->tokenlineno,
2644 "Illegal argument to %%%s: %s",psp->declkeyword,x);
2645 psp->errorcnt++;
2646 psp->state = RESYNC_AFTER_DECL_ERROR;
2647 }
2648 break;
drh0bd1f4e2002-06-06 18:54:39 +00002649 case WAITING_FOR_FALLBACK_ID:
2650 if( x[0]=='.' ){
2651 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002652 }else if( !ISUPPER(x[0]) ){
drh0bd1f4e2002-06-06 18:54:39 +00002653 ErrorMsg(psp->filename, psp->tokenlineno,
2654 "%%fallback argument \"%s\" should be a token", x);
2655 psp->errorcnt++;
2656 }else{
2657 struct symbol *sp = Symbol_new(x);
2658 if( psp->fallback==0 ){
2659 psp->fallback = sp;
2660 }else if( sp->fallback ){
2661 ErrorMsg(psp->filename, psp->tokenlineno,
2662 "More than one fallback assigned to token %s", x);
2663 psp->errorcnt++;
2664 }else{
2665 sp->fallback = psp->fallback;
2666 psp->gp->has_fallback = 1;
2667 }
2668 }
2669 break;
drh59c435a2017-08-02 03:21:11 +00002670 case WAITING_FOR_TOKEN_NAME:
2671 /* Tokens do not have to be declared before use. But they can be
2672 ** in order to control their assigned integer number. The number for
2673 ** each token is assigned when it is first seen. So by including
2674 **
2675 ** %token ONE TWO THREE
2676 **
2677 ** early in the grammar file, that assigns small consecutive values
2678 ** to each of the tokens ONE TWO and THREE.
2679 */
2680 if( x[0]=='.' ){
2681 psp->state = WAITING_FOR_DECL_OR_RULE;
2682 }else if( !ISUPPER(x[0]) ){
2683 ErrorMsg(psp->filename, psp->tokenlineno,
2684 "%%token argument \"%s\" should be a token", x);
2685 psp->errorcnt++;
2686 }else{
2687 (void)Symbol_new(x);
2688 }
2689 break;
drhe09daa92006-06-10 13:29:31 +00002690 case WAITING_FOR_WILDCARD_ID:
2691 if( x[0]=='.' ){
2692 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002693 }else if( !ISUPPER(x[0]) ){
drhe09daa92006-06-10 13:29:31 +00002694 ErrorMsg(psp->filename, psp->tokenlineno,
2695 "%%wildcard argument \"%s\" should be a token", x);
2696 psp->errorcnt++;
2697 }else{
2698 struct symbol *sp = Symbol_new(x);
2699 if( psp->gp->wildcard==0 ){
2700 psp->gp->wildcard = sp;
2701 }else{
2702 ErrorMsg(psp->filename, psp->tokenlineno,
2703 "Extra wildcard to token: %s", x);
2704 psp->errorcnt++;
2705 }
2706 }
2707 break;
drh61f92cd2014-01-11 03:06:18 +00002708 case WAITING_FOR_CLASS_ID:
drhc56fac72015-10-29 13:48:15 +00002709 if( !ISLOWER(x[0]) ){
drh61f92cd2014-01-11 03:06:18 +00002710 ErrorMsg(psp->filename, psp->tokenlineno,
2711 "%%token_class must be followed by an identifier: ", x);
2712 psp->errorcnt++;
2713 psp->state = RESYNC_AFTER_DECL_ERROR;
2714 }else if( Symbol_find(x) ){
2715 ErrorMsg(psp->filename, psp->tokenlineno,
2716 "Symbol \"%s\" already used", x);
2717 psp->errorcnt++;
2718 psp->state = RESYNC_AFTER_DECL_ERROR;
2719 }else{
2720 psp->tkclass = Symbol_new(x);
2721 psp->tkclass->type = MULTITERMINAL;
2722 psp->state = WAITING_FOR_CLASS_TOKEN;
2723 }
2724 break;
2725 case WAITING_FOR_CLASS_TOKEN:
2726 if( x[0]=='.' ){
2727 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002728 }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){
drh61f92cd2014-01-11 03:06:18 +00002729 struct symbol *msp = psp->tkclass;
2730 msp->nsubsym++;
2731 msp->subsym = (struct symbol **) realloc(msp->subsym,
2732 sizeof(struct symbol*)*msp->nsubsym);
drhc56fac72015-10-29 13:48:15 +00002733 if( !ISUPPER(x[0]) ) x++;
drh61f92cd2014-01-11 03:06:18 +00002734 msp->subsym[msp->nsubsym-1] = Symbol_new(x);
2735 }else{
2736 ErrorMsg(psp->filename, psp->tokenlineno,
2737 "%%token_class argument \"%s\" should be a token", x);
2738 psp->errorcnt++;
2739 psp->state = RESYNC_AFTER_DECL_ERROR;
2740 }
2741 break;
drh75897232000-05-29 14:26:00 +00002742 case RESYNC_AFTER_RULE_ERROR:
2743/* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2744** break; */
2745 case RESYNC_AFTER_DECL_ERROR:
2746 if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2747 if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
2748 break;
2749 }
2750}
2751
drh34ff57b2008-07-14 12:27:51 +00002752/* Run the preprocessor over the input file text. The global variables
drh6d08b4d2004-07-20 12:45:22 +00002753** azDefine[0] through azDefine[nDefine-1] contains the names of all defined
2754** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and
2755** comments them out. Text in between is also commented out as appropriate.
2756*/
danielk1977940fac92005-01-23 22:41:37 +00002757static void preprocess_input(char *z){
drh6d08b4d2004-07-20 12:45:22 +00002758 int i, j, k, n;
2759 int exclude = 0;
rse38514a92007-09-20 11:34:17 +00002760 int start = 0;
drh6d08b4d2004-07-20 12:45:22 +00002761 int lineno = 1;
rse38514a92007-09-20 11:34:17 +00002762 int start_lineno = 1;
drh6d08b4d2004-07-20 12:45:22 +00002763 for(i=0; z[i]; i++){
2764 if( z[i]=='\n' ) lineno++;
2765 if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue;
drhc56fac72015-10-29 13:48:15 +00002766 if( strncmp(&z[i],"%endif",6)==0 && ISSPACE(z[i+6]) ){
drh6d08b4d2004-07-20 12:45:22 +00002767 if( exclude ){
2768 exclude--;
2769 if( exclude==0 ){
2770 for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
2771 }
2772 }
2773 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
drhc56fac72015-10-29 13:48:15 +00002774 }else if( (strncmp(&z[i],"%ifdef",6)==0 && ISSPACE(z[i+6]))
2775 || (strncmp(&z[i],"%ifndef",7)==0 && ISSPACE(z[i+7])) ){
drh6d08b4d2004-07-20 12:45:22 +00002776 if( exclude ){
2777 exclude++;
2778 }else{
drhc56fac72015-10-29 13:48:15 +00002779 for(j=i+7; ISSPACE(z[j]); j++){}
2780 for(n=0; z[j+n] && !ISSPACE(z[j+n]); n++){}
drh6d08b4d2004-07-20 12:45:22 +00002781 exclude = 1;
2782 for(k=0; k<nDefine; k++){
drh87cf1372008-08-13 20:09:06 +00002783 if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){
drh6d08b4d2004-07-20 12:45:22 +00002784 exclude = 0;
2785 break;
2786 }
2787 }
2788 if( z[i+3]=='n' ) exclude = !exclude;
2789 if( exclude ){
2790 start = i;
2791 start_lineno = lineno;
2792 }
2793 }
2794 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2795 }
2796 }
2797 if( exclude ){
2798 fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
2799 exit(1);
2800 }
2801}
2802
drh75897232000-05-29 14:26:00 +00002803/* In spite of its name, this function is really a scanner. It read
2804** in the entire input file (all at once) then tokenizes it. Each
2805** token is passed to the function "parseonetoken" which builds all
2806** the appropriate data structures in the global state vector "gp".
2807*/
icculus9e44cf12010-02-14 17:14:22 +00002808void Parse(struct lemon *gp)
drh75897232000-05-29 14:26:00 +00002809{
2810 struct pstate ps;
2811 FILE *fp;
2812 char *filebuf;
mistachkin2318d332015-01-12 18:02:52 +00002813 unsigned int filesize;
drh75897232000-05-29 14:26:00 +00002814 int lineno;
2815 int c;
2816 char *cp, *nextcp;
2817 int startline = 0;
2818
rse38514a92007-09-20 11:34:17 +00002819 memset(&ps, '\0', sizeof(ps));
drh75897232000-05-29 14:26:00 +00002820 ps.gp = gp;
2821 ps.filename = gp->filename;
2822 ps.errorcnt = 0;
2823 ps.state = INITIALIZE;
2824
2825 /* Begin by reading the input file */
2826 fp = fopen(ps.filename,"rb");
2827 if( fp==0 ){
2828 ErrorMsg(ps.filename,0,"Can't open this file for reading.");
2829 gp->errorcnt++;
2830 return;
2831 }
2832 fseek(fp,0,2);
2833 filesize = ftell(fp);
2834 rewind(fp);
2835 filebuf = (char *)malloc( filesize+1 );
drh03e1b1f2014-01-11 12:52:25 +00002836 if( filesize>100000000 || filebuf==0 ){
2837 ErrorMsg(ps.filename,0,"Input file too large.");
drh75897232000-05-29 14:26:00 +00002838 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002839 fclose(fp);
drh75897232000-05-29 14:26:00 +00002840 return;
2841 }
2842 if( fread(filebuf,1,filesize,fp)!=filesize ){
2843 ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
2844 filesize);
2845 free(filebuf);
2846 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002847 fclose(fp);
drh75897232000-05-29 14:26:00 +00002848 return;
2849 }
2850 fclose(fp);
2851 filebuf[filesize] = 0;
2852
drh6d08b4d2004-07-20 12:45:22 +00002853 /* Make an initial pass through the file to handle %ifdef and %ifndef */
2854 preprocess_input(filebuf);
2855
drh75897232000-05-29 14:26:00 +00002856 /* Now scan the text of the input file */
2857 lineno = 1;
2858 for(cp=filebuf; (c= *cp)!=0; ){
2859 if( c=='\n' ) lineno++; /* Keep track of the line number */
drhc56fac72015-10-29 13:48:15 +00002860 if( ISSPACE(c) ){ cp++; continue; } /* Skip all white space */
drh75897232000-05-29 14:26:00 +00002861 if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
2862 cp+=2;
2863 while( (c= *cp)!=0 && c!='\n' ) cp++;
2864 continue;
2865 }
2866 if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
2867 cp+=2;
2868 while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
2869 if( c=='\n' ) lineno++;
2870 cp++;
2871 }
2872 if( c ) cp++;
2873 continue;
2874 }
2875 ps.tokenstart = cp; /* Mark the beginning of the token */
2876 ps.tokenlineno = lineno; /* Linenumber on which token begins */
2877 if( c=='\"' ){ /* String literals */
2878 cp++;
2879 while( (c= *cp)!=0 && c!='\"' ){
2880 if( c=='\n' ) lineno++;
2881 cp++;
2882 }
2883 if( c==0 ){
2884 ErrorMsg(ps.filename,startline,
2885"String starting on this line is not terminated before the end of the file.");
2886 ps.errorcnt++;
2887 nextcp = cp;
2888 }else{
2889 nextcp = cp+1;
2890 }
2891 }else if( c=='{' ){ /* A block of C code */
2892 int level;
2893 cp++;
2894 for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
2895 if( c=='\n' ) lineno++;
2896 else if( c=='{' ) level++;
2897 else if( c=='}' ) level--;
2898 else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
2899 int prevc;
2900 cp = &cp[2];
2901 prevc = 0;
2902 while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
2903 if( c=='\n' ) lineno++;
2904 prevc = c;
2905 cp++;
drhf2f105d2012-08-20 15:53:54 +00002906 }
2907 }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
drh75897232000-05-29 14:26:00 +00002908 cp = &cp[2];
2909 while( (c= *cp)!=0 && c!='\n' ) cp++;
2910 if( c ) lineno++;
drhf2f105d2012-08-20 15:53:54 +00002911 }else if( c=='\'' || c=='\"' ){ /* String a character literals */
drh75897232000-05-29 14:26:00 +00002912 int startchar, prevc;
2913 startchar = c;
2914 prevc = 0;
2915 for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
2916 if( c=='\n' ) lineno++;
2917 if( prevc=='\\' ) prevc = 0;
2918 else prevc = c;
drhf2f105d2012-08-20 15:53:54 +00002919 }
2920 }
drh75897232000-05-29 14:26:00 +00002921 }
2922 if( c==0 ){
drh960e8c62001-04-03 16:53:21 +00002923 ErrorMsg(ps.filename,ps.tokenlineno,
drh75897232000-05-29 14:26:00 +00002924"C code starting on this line is not terminated before the end of the file.");
2925 ps.errorcnt++;
2926 nextcp = cp;
2927 }else{
2928 nextcp = cp+1;
2929 }
drhc56fac72015-10-29 13:48:15 +00002930 }else if( ISALNUM(c) ){ /* Identifiers */
2931 while( (c= *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
drh75897232000-05-29 14:26:00 +00002932 nextcp = cp;
2933 }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
2934 cp += 3;
2935 nextcp = cp;
drhc56fac72015-10-29 13:48:15 +00002936 }else if( (c=='/' || c=='|') && ISALPHA(cp[1]) ){
drhfd405312005-11-06 04:06:59 +00002937 cp += 2;
drhc56fac72015-10-29 13:48:15 +00002938 while( (c = *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
drhfd405312005-11-06 04:06:59 +00002939 nextcp = cp;
drh75897232000-05-29 14:26:00 +00002940 }else{ /* All other (one character) operators */
2941 cp++;
2942 nextcp = cp;
2943 }
2944 c = *cp;
2945 *cp = 0; /* Null terminate the token */
2946 parseonetoken(&ps); /* Parse the token */
mistachkin2318d332015-01-12 18:02:52 +00002947 *cp = (char)c; /* Restore the buffer */
drh75897232000-05-29 14:26:00 +00002948 cp = nextcp;
2949 }
2950 free(filebuf); /* Release the buffer after parsing */
2951 gp->rule = ps.firstrule;
2952 gp->errorcnt = ps.errorcnt;
2953}
2954/*************************** From the file "plink.c" *********************/
2955/*
2956** Routines processing configuration follow-set propagation links
2957** in the LEMON parser generator.
2958*/
2959static struct plink *plink_freelist = 0;
2960
2961/* Allocate a new plink */
drh14d88552017-04-14 19:44:15 +00002962struct plink *Plink_new(void){
icculus9e44cf12010-02-14 17:14:22 +00002963 struct plink *newlink;
drh75897232000-05-29 14:26:00 +00002964
2965 if( plink_freelist==0 ){
2966 int i;
2967 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +00002968 plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
drh75897232000-05-29 14:26:00 +00002969 if( plink_freelist==0 ){
2970 fprintf(stderr,
2971 "Unable to allocate memory for a new follow-set propagation link.\n");
2972 exit(1);
2973 }
2974 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2975 plink_freelist[amt-1].next = 0;
2976 }
icculus9e44cf12010-02-14 17:14:22 +00002977 newlink = plink_freelist;
drh75897232000-05-29 14:26:00 +00002978 plink_freelist = plink_freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00002979 return newlink;
drh75897232000-05-29 14:26:00 +00002980}
2981
2982/* Add a plink to a plink list */
icculus9e44cf12010-02-14 17:14:22 +00002983void Plink_add(struct plink **plpp, struct config *cfp)
drh75897232000-05-29 14:26:00 +00002984{
icculus9e44cf12010-02-14 17:14:22 +00002985 struct plink *newlink;
2986 newlink = Plink_new();
2987 newlink->next = *plpp;
2988 *plpp = newlink;
2989 newlink->cfp = cfp;
drh75897232000-05-29 14:26:00 +00002990}
2991
2992/* Transfer every plink on the list "from" to the list "to" */
icculus9e44cf12010-02-14 17:14:22 +00002993void Plink_copy(struct plink **to, struct plink *from)
drh75897232000-05-29 14:26:00 +00002994{
2995 struct plink *nextpl;
2996 while( from ){
2997 nextpl = from->next;
2998 from->next = *to;
2999 *to = from;
3000 from = nextpl;
3001 }
3002}
3003
3004/* Delete every plink on the list */
icculus9e44cf12010-02-14 17:14:22 +00003005void Plink_delete(struct plink *plp)
drh75897232000-05-29 14:26:00 +00003006{
3007 struct plink *nextpl;
3008
3009 while( plp ){
3010 nextpl = plp->next;
3011 plp->next = plink_freelist;
3012 plink_freelist = plp;
3013 plp = nextpl;
3014 }
3015}
3016/*********************** From the file "report.c" **************************/
3017/*
3018** Procedures for generating reports and tables in the LEMON parser generator.
3019*/
3020
3021/* Generate a filename with the given suffix. Space to hold the
3022** name comes from malloc() and must be freed by the calling
3023** function.
3024*/
icculus9e44cf12010-02-14 17:14:22 +00003025PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
drh75897232000-05-29 14:26:00 +00003026{
3027 char *name;
3028 char *cp;
3029
icculus9e44cf12010-02-14 17:14:22 +00003030 name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
drh75897232000-05-29 14:26:00 +00003031 if( name==0 ){
3032 fprintf(stderr,"Can't allocate space for a filename.\n");
3033 exit(1);
3034 }
drh898799f2014-01-10 23:21:00 +00003035 lemon_strcpy(name,lemp->filename);
drh75897232000-05-29 14:26:00 +00003036 cp = strrchr(name,'.');
3037 if( cp ) *cp = 0;
drh898799f2014-01-10 23:21:00 +00003038 lemon_strcat(name,suffix);
drh75897232000-05-29 14:26:00 +00003039 return name;
3040}
3041
3042/* Open a file with a name based on the name of the input file,
3043** but with a different (specified) suffix, and return a pointer
3044** to the stream */
icculus9e44cf12010-02-14 17:14:22 +00003045PRIVATE FILE *file_open(
3046 struct lemon *lemp,
3047 const char *suffix,
3048 const char *mode
3049){
drh75897232000-05-29 14:26:00 +00003050 FILE *fp;
3051
3052 if( lemp->outname ) free(lemp->outname);
3053 lemp->outname = file_makename(lemp, suffix);
3054 fp = fopen(lemp->outname,mode);
3055 if( fp==0 && *mode=='w' ){
3056 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
3057 lemp->errorcnt++;
3058 return 0;
3059 }
3060 return fp;
3061}
3062
drh5c8241b2017-12-24 23:38:10 +00003063/* Print the text of a rule
3064*/
3065void rule_print(FILE *out, struct rule *rp){
3066 int i, j;
3067 fprintf(out, "%s",rp->lhs->name);
3068 /* if( rp->lhsalias ) fprintf(out,"(%s)",rp->lhsalias); */
3069 fprintf(out," ::=");
3070 for(i=0; i<rp->nrhs; i++){
3071 struct symbol *sp = rp->rhs[i];
3072 if( sp->type==MULTITERMINAL ){
3073 fprintf(out," %s", sp->subsym[0]->name);
3074 for(j=1; j<sp->nsubsym; j++){
3075 fprintf(out,"|%s", sp->subsym[j]->name);
3076 }
3077 }else{
3078 fprintf(out," %s", sp->name);
3079 }
3080 /* if( rp->rhsalias[i] ) fprintf(out,"(%s)",rp->rhsalias[i]); */
3081 }
3082}
3083
drh06f60d82017-04-14 19:46:12 +00003084/* Duplicate the input file without comments and without actions
drh75897232000-05-29 14:26:00 +00003085** on rules */
icculus9e44cf12010-02-14 17:14:22 +00003086void Reprint(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003087{
3088 struct rule *rp;
3089 struct symbol *sp;
3090 int i, j, maxlen, len, ncolumns, skip;
3091 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
3092 maxlen = 10;
3093 for(i=0; i<lemp->nsymbol; i++){
3094 sp = lemp->symbols[i];
drh87cf1372008-08-13 20:09:06 +00003095 len = lemonStrlen(sp->name);
drh75897232000-05-29 14:26:00 +00003096 if( len>maxlen ) maxlen = len;
3097 }
3098 ncolumns = 76/(maxlen+5);
3099 if( ncolumns<1 ) ncolumns = 1;
3100 skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
3101 for(i=0; i<skip; i++){
3102 printf("//");
3103 for(j=i; j<lemp->nsymbol; j+=skip){
3104 sp = lemp->symbols[j];
3105 assert( sp->index==j );
3106 printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
3107 }
3108 printf("\n");
3109 }
3110 for(rp=lemp->rule; rp; rp=rp->next){
drh5c8241b2017-12-24 23:38:10 +00003111 rule_print(stdout, rp);
drh75897232000-05-29 14:26:00 +00003112 printf(".");
3113 if( rp->precsym ) printf(" [%s]",rp->precsym->name);
drhfd405312005-11-06 04:06:59 +00003114 /* if( rp->code ) printf("\n %s",rp->code); */
drh75897232000-05-29 14:26:00 +00003115 printf("\n");
3116 }
3117}
3118
drh7e698e92015-09-07 14:22:24 +00003119/* Print a single rule.
3120*/
3121void RulePrint(FILE *fp, struct rule *rp, int iCursor){
drhfd405312005-11-06 04:06:59 +00003122 struct symbol *sp;
3123 int i, j;
drh75897232000-05-29 14:26:00 +00003124 fprintf(fp,"%s ::=",rp->lhs->name);
3125 for(i=0; i<=rp->nrhs; i++){
drh7e698e92015-09-07 14:22:24 +00003126 if( i==iCursor ) fprintf(fp," *");
drh75897232000-05-29 14:26:00 +00003127 if( i==rp->nrhs ) break;
drhfd405312005-11-06 04:06:59 +00003128 sp = rp->rhs[i];
drhfd405312005-11-06 04:06:59 +00003129 if( sp->type==MULTITERMINAL ){
drh61f92cd2014-01-11 03:06:18 +00003130 fprintf(fp," %s", sp->subsym[0]->name);
drhfd405312005-11-06 04:06:59 +00003131 for(j=1; j<sp->nsubsym; j++){
3132 fprintf(fp,"|%s",sp->subsym[j]->name);
3133 }
drh61f92cd2014-01-11 03:06:18 +00003134 }else{
3135 fprintf(fp," %s", sp->name);
drhfd405312005-11-06 04:06:59 +00003136 }
drh75897232000-05-29 14:26:00 +00003137 }
3138}
3139
drh7e698e92015-09-07 14:22:24 +00003140/* Print the rule for a configuration.
3141*/
3142void ConfigPrint(FILE *fp, struct config *cfp){
3143 RulePrint(fp, cfp->rp, cfp->dot);
3144}
3145
drh75897232000-05-29 14:26:00 +00003146/* #define TEST */
drhfd405312005-11-06 04:06:59 +00003147#if 0
drh75897232000-05-29 14:26:00 +00003148/* Print a set */
3149PRIVATE void SetPrint(out,set,lemp)
3150FILE *out;
3151char *set;
3152struct lemon *lemp;
3153{
3154 int i;
3155 char *spacer;
3156 spacer = "";
3157 fprintf(out,"%12s[","");
3158 for(i=0; i<lemp->nterminal; i++){
3159 if( SetFind(set,i) ){
3160 fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
3161 spacer = " ";
3162 }
3163 }
3164 fprintf(out,"]\n");
3165}
3166
3167/* Print a plink chain */
3168PRIVATE void PlinkPrint(out,plp,tag)
3169FILE *out;
3170struct plink *plp;
3171char *tag;
3172{
3173 while( plp ){
drhada354d2005-11-05 15:03:59 +00003174 fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum);
drh75897232000-05-29 14:26:00 +00003175 ConfigPrint(out,plp->cfp);
3176 fprintf(out,"\n");
3177 plp = plp->next;
3178 }
3179}
3180#endif
3181
3182/* Print an action to the given file descriptor. Return FALSE if
3183** nothing was actually printed.
3184*/
drh7e698e92015-09-07 14:22:24 +00003185int PrintAction(
3186 struct action *ap, /* The action to print */
3187 FILE *fp, /* Print the action here */
drh3bd48ab2015-09-07 18:23:37 +00003188 int indent /* Indent by this amount */
drh7e698e92015-09-07 14:22:24 +00003189){
drh75897232000-05-29 14:26:00 +00003190 int result = 1;
3191 switch( ap->type ){
drh7e698e92015-09-07 14:22:24 +00003192 case SHIFT: {
3193 struct state *stp = ap->x.stp;
drh3bd48ab2015-09-07 18:23:37 +00003194 fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum);
drh75897232000-05-29 14:26:00 +00003195 break;
drh7e698e92015-09-07 14:22:24 +00003196 }
3197 case REDUCE: {
3198 struct rule *rp = ap->x.rp;
drh4ef07702016-03-16 19:45:54 +00003199 fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->iRule);
drh3bd48ab2015-09-07 18:23:37 +00003200 RulePrint(fp, rp, -1);
3201 break;
3202 }
3203 case SHIFTREDUCE: {
3204 struct rule *rp = ap->x.rp;
drh4ef07702016-03-16 19:45:54 +00003205 fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->iRule);
drh3bd48ab2015-09-07 18:23:37 +00003206 RulePrint(fp, rp, -1);
drh75897232000-05-29 14:26:00 +00003207 break;
drh7e698e92015-09-07 14:22:24 +00003208 }
drh75897232000-05-29 14:26:00 +00003209 case ACCEPT:
3210 fprintf(fp,"%*s accept",indent,ap->sp->name);
3211 break;
3212 case ERROR:
3213 fprintf(fp,"%*s error",indent,ap->sp->name);
3214 break;
drh9892c5d2007-12-21 00:02:11 +00003215 case SRCONFLICT:
3216 case RRCONFLICT:
drh3bd48ab2015-09-07 18:23:37 +00003217 fprintf(fp,"%*s reduce %-7d ** Parsing conflict **",
drh4ef07702016-03-16 19:45:54 +00003218 indent,ap->sp->name,ap->x.rp->iRule);
drh75897232000-05-29 14:26:00 +00003219 break;
drh9892c5d2007-12-21 00:02:11 +00003220 case SSCONFLICT:
drh06f60d82017-04-14 19:46:12 +00003221 fprintf(fp,"%*s shift %-7d ** Parsing conflict **",
drh9892c5d2007-12-21 00:02:11 +00003222 indent,ap->sp->name,ap->x.stp->statenum);
3223 break;
drh75897232000-05-29 14:26:00 +00003224 case SH_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00003225 if( showPrecedenceConflict ){
drh3bd48ab2015-09-07 18:23:37 +00003226 fprintf(fp,"%*s shift %-7d -- dropped by precedence",
drhf5c4e0f2010-07-18 11:35:53 +00003227 indent,ap->sp->name,ap->x.stp->statenum);
3228 }else{
3229 result = 0;
3230 }
3231 break;
drh75897232000-05-29 14:26:00 +00003232 case RD_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00003233 if( showPrecedenceConflict ){
drh7e698e92015-09-07 14:22:24 +00003234 fprintf(fp,"%*s reduce %-7d -- dropped by precedence",
drh4ef07702016-03-16 19:45:54 +00003235 indent,ap->sp->name,ap->x.rp->iRule);
drhf5c4e0f2010-07-18 11:35:53 +00003236 }else{
3237 result = 0;
3238 }
3239 break;
drh75897232000-05-29 14:26:00 +00003240 case NOT_USED:
3241 result = 0;
3242 break;
3243 }
drhc173ad82016-05-23 16:15:02 +00003244 if( result && ap->spOpt ){
3245 fprintf(fp," /* because %s==%s */", ap->sp->name, ap->spOpt->name);
3246 }
drh75897232000-05-29 14:26:00 +00003247 return result;
3248}
3249
drh3bd48ab2015-09-07 18:23:37 +00003250/* Generate the "*.out" log file */
icculus9e44cf12010-02-14 17:14:22 +00003251void ReportOutput(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003252{
3253 int i;
3254 struct state *stp;
3255 struct config *cfp;
3256 struct action *ap;
3257 FILE *fp;
3258
drh2aa6ca42004-09-10 00:14:04 +00003259 fp = file_open(lemp,".out","wb");
drh75897232000-05-29 14:26:00 +00003260 if( fp==0 ) return;
drh3bd48ab2015-09-07 18:23:37 +00003261 for(i=0; i<lemp->nxstate; i++){
drh75897232000-05-29 14:26:00 +00003262 stp = lemp->sorted[i];
drhada354d2005-11-05 15:03:59 +00003263 fprintf(fp,"State %d:\n",stp->statenum);
drh75897232000-05-29 14:26:00 +00003264 if( lemp->basisflag ) cfp=stp->bp;
3265 else cfp=stp->cfp;
3266 while( cfp ){
3267 char buf[20];
3268 if( cfp->dot==cfp->rp->nrhs ){
drh4ef07702016-03-16 19:45:54 +00003269 lemon_sprintf(buf,"(%d)",cfp->rp->iRule);
drh75897232000-05-29 14:26:00 +00003270 fprintf(fp," %5s ",buf);
3271 }else{
3272 fprintf(fp," ");
3273 }
3274 ConfigPrint(fp,cfp);
3275 fprintf(fp,"\n");
drhfd405312005-11-06 04:06:59 +00003276#if 0
drh75897232000-05-29 14:26:00 +00003277 SetPrint(fp,cfp->fws,lemp);
3278 PlinkPrint(fp,cfp->fplp,"To ");
3279 PlinkPrint(fp,cfp->bplp,"From");
3280#endif
3281 if( lemp->basisflag ) cfp=cfp->bp;
3282 else cfp=cfp->next;
3283 }
3284 fprintf(fp,"\n");
3285 for(ap=stp->ap; ap; ap=ap->next){
drh3bd48ab2015-09-07 18:23:37 +00003286 if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
drh75897232000-05-29 14:26:00 +00003287 }
3288 fprintf(fp,"\n");
3289 }
drhe9278182007-07-18 18:16:29 +00003290 fprintf(fp, "----------------------------------------------------\n");
3291 fprintf(fp, "Symbols:\n");
3292 for(i=0; i<lemp->nsymbol; i++){
3293 int j;
3294 struct symbol *sp;
3295
3296 sp = lemp->symbols[i];
3297 fprintf(fp, " %3d: %s", i, sp->name);
3298 if( sp->type==NONTERMINAL ){
3299 fprintf(fp, ":");
3300 if( sp->lambda ){
3301 fprintf(fp, " <lambda>");
3302 }
3303 for(j=0; j<lemp->nterminal; j++){
3304 if( sp->firstset && SetFind(sp->firstset, j) ){
3305 fprintf(fp, " %s", lemp->symbols[j]->name);
3306 }
3307 }
3308 }
3309 fprintf(fp, "\n");
3310 }
drh75897232000-05-29 14:26:00 +00003311 fclose(fp);
3312 return;
3313}
3314
3315/* Search for the file "name" which is in the same directory as
3316** the exacutable */
icculus9e44cf12010-02-14 17:14:22 +00003317PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
drh75897232000-05-29 14:26:00 +00003318{
icculus9e44cf12010-02-14 17:14:22 +00003319 const char *pathlist;
3320 char *pathbufptr;
3321 char *pathbuf;
drh75897232000-05-29 14:26:00 +00003322 char *path,*cp;
3323 char c;
drh75897232000-05-29 14:26:00 +00003324
3325#ifdef __WIN32__
3326 cp = strrchr(argv0,'\\');
3327#else
3328 cp = strrchr(argv0,'/');
3329#endif
3330 if( cp ){
3331 c = *cp;
3332 *cp = 0;
drh87cf1372008-08-13 20:09:06 +00003333 path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
drh898799f2014-01-10 23:21:00 +00003334 if( path ) lemon_sprintf(path,"%s/%s",argv0,name);
drh75897232000-05-29 14:26:00 +00003335 *cp = c;
3336 }else{
drh75897232000-05-29 14:26:00 +00003337 pathlist = getenv("PATH");
3338 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
icculus9e44cf12010-02-14 17:14:22 +00003339 pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
drh87cf1372008-08-13 20:09:06 +00003340 path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
icculus9e44cf12010-02-14 17:14:22 +00003341 if( (pathbuf != 0) && (path!=0) ){
3342 pathbufptr = pathbuf;
drh898799f2014-01-10 23:21:00 +00003343 lemon_strcpy(pathbuf, pathlist);
icculus9e44cf12010-02-14 17:14:22 +00003344 while( *pathbuf ){
3345 cp = strchr(pathbuf,':');
3346 if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
drh75897232000-05-29 14:26:00 +00003347 c = *cp;
3348 *cp = 0;
drh898799f2014-01-10 23:21:00 +00003349 lemon_sprintf(path,"%s/%s",pathbuf,name);
drh75897232000-05-29 14:26:00 +00003350 *cp = c;
icculus9e44cf12010-02-14 17:14:22 +00003351 if( c==0 ) pathbuf[0] = 0;
3352 else pathbuf = &cp[1];
drh75897232000-05-29 14:26:00 +00003353 if( access(path,modemask)==0 ) break;
3354 }
icculus9e44cf12010-02-14 17:14:22 +00003355 free(pathbufptr);
drh75897232000-05-29 14:26:00 +00003356 }
3357 }
3358 return path;
3359}
3360
3361/* Given an action, compute the integer value for that action
3362** which is to be put in the action table of the generated machine.
3363** Return negative if no action should be generated.
3364*/
icculus9e44cf12010-02-14 17:14:22 +00003365PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
drh75897232000-05-29 14:26:00 +00003366{
3367 int act;
3368 switch( ap->type ){
drh3bd48ab2015-09-07 18:23:37 +00003369 case SHIFT: act = ap->x.stp->statenum; break;
drhbd8fcc12017-06-28 11:56:18 +00003370 case SHIFTREDUCE: {
drhbd8fcc12017-06-28 11:56:18 +00003371 /* Since a SHIFT is inherient after a prior REDUCE, convert any
3372 ** SHIFTREDUCE action with a nonterminal on the LHS into a simple
3373 ** REDUCE action: */
drh5c8241b2017-12-24 23:38:10 +00003374 if( ap->sp->index>=lemp->nterminal ){
3375 act = lemp->minReduce + ap->x.rp->iRule;
3376 }else{
3377 act = lemp->minShiftReduce + ap->x.rp->iRule;
3378 }
drhbd8fcc12017-06-28 11:56:18 +00003379 break;
3380 }
drh5c8241b2017-12-24 23:38:10 +00003381 case REDUCE: act = lemp->minReduce + ap->x.rp->iRule; break;
3382 case ERROR: act = lemp->errAction; break;
3383 case ACCEPT: act = lemp->accAction; break;
drh75897232000-05-29 14:26:00 +00003384 default: act = -1; break;
3385 }
3386 return act;
3387}
3388
3389#define LINESIZE 1000
3390/* The next cluster of routines are for reading the template file
3391** and writing the results to the generated parser */
3392/* The first function transfers data from "in" to "out" until
3393** a line is seen which begins with "%%". The line number is
3394** tracked.
3395**
3396** if name!=0, then any word that begin with "Parse" is changed to
3397** begin with *name instead.
3398*/
icculus9e44cf12010-02-14 17:14:22 +00003399PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
drh75897232000-05-29 14:26:00 +00003400{
3401 int i, iStart;
3402 char line[LINESIZE];
3403 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
3404 (*lineno)++;
3405 iStart = 0;
3406 if( name ){
3407 for(i=0; line[i]; i++){
3408 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
drhc56fac72015-10-29 13:48:15 +00003409 && (i==0 || !ISALPHA(line[i-1]))
drh75897232000-05-29 14:26:00 +00003410 ){
3411 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
3412 fprintf(out,"%s",name);
3413 i += 4;
3414 iStart = i+1;
3415 }
3416 }
3417 }
3418 fprintf(out,"%s",&line[iStart]);
3419 }
3420}
3421
3422/* The next function finds the template file and opens it, returning
3423** a pointer to the opened file. */
icculus9e44cf12010-02-14 17:14:22 +00003424PRIVATE FILE *tplt_open(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003425{
3426 static char templatename[] = "lempar.c";
3427 char buf[1000];
3428 FILE *in;
3429 char *tpltname;
3430 char *cp;
3431
icculus3e143bd2010-02-14 00:48:49 +00003432 /* first, see if user specified a template filename on the command line. */
3433 if (user_templatename != 0) {
3434 if( access(user_templatename,004)==-1 ){
3435 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3436 user_templatename);
3437 lemp->errorcnt++;
3438 return 0;
3439 }
3440 in = fopen(user_templatename,"rb");
3441 if( in==0 ){
drh25473362015-09-04 18:03:45 +00003442 fprintf(stderr,"Can't open the template file \"%s\".\n",
3443 user_templatename);
icculus3e143bd2010-02-14 00:48:49 +00003444 lemp->errorcnt++;
3445 return 0;
3446 }
3447 return in;
3448 }
3449
drh75897232000-05-29 14:26:00 +00003450 cp = strrchr(lemp->filename,'.');
3451 if( cp ){
drh898799f2014-01-10 23:21:00 +00003452 lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
drh75897232000-05-29 14:26:00 +00003453 }else{
drh898799f2014-01-10 23:21:00 +00003454 lemon_sprintf(buf,"%s.lt",lemp->filename);
drh75897232000-05-29 14:26:00 +00003455 }
3456 if( access(buf,004)==0 ){
3457 tpltname = buf;
drh960e8c62001-04-03 16:53:21 +00003458 }else if( access(templatename,004)==0 ){
3459 tpltname = templatename;
drh75897232000-05-29 14:26:00 +00003460 }else{
3461 tpltname = pathsearch(lemp->argv0,templatename,0);
3462 }
3463 if( tpltname==0 ){
3464 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3465 templatename);
3466 lemp->errorcnt++;
3467 return 0;
3468 }
drh2aa6ca42004-09-10 00:14:04 +00003469 in = fopen(tpltname,"rb");
drh75897232000-05-29 14:26:00 +00003470 if( in==0 ){
3471 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
3472 lemp->errorcnt++;
3473 return 0;
3474 }
3475 return in;
3476}
3477
drhaf805ca2004-09-07 11:28:25 +00003478/* Print a #line directive line to the output file. */
icculus9e44cf12010-02-14 17:14:22 +00003479PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
drhaf805ca2004-09-07 11:28:25 +00003480{
3481 fprintf(out,"#line %d \"",lineno);
3482 while( *filename ){
3483 if( *filename == '\\' ) putc('\\',out);
3484 putc(*filename,out);
3485 filename++;
3486 }
3487 fprintf(out,"\"\n");
3488}
3489
drh75897232000-05-29 14:26:00 +00003490/* Print a string to the file and keep the linenumber up to date */
icculus9e44cf12010-02-14 17:14:22 +00003491PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
drh75897232000-05-29 14:26:00 +00003492{
3493 if( str==0 ) return;
drh75897232000-05-29 14:26:00 +00003494 while( *str ){
drh75897232000-05-29 14:26:00 +00003495 putc(*str,out);
shane58543932008-12-10 20:10:04 +00003496 if( *str=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003497 str++;
3498 }
drh9db55df2004-09-09 14:01:21 +00003499 if( str[-1]!='\n' ){
3500 putc('\n',out);
3501 (*lineno)++;
3502 }
shane58543932008-12-10 20:10:04 +00003503 if (!lemp->nolinenosflag) {
drh06f60d82017-04-14 19:46:12 +00003504 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
shane58543932008-12-10 20:10:04 +00003505 }
drh75897232000-05-29 14:26:00 +00003506 return;
3507}
3508
3509/*
3510** The following routine emits code for the destructor for the
3511** symbol sp
3512*/
icculus9e44cf12010-02-14 17:14:22 +00003513void emit_destructor_code(
3514 FILE *out,
3515 struct symbol *sp,
3516 struct lemon *lemp,
3517 int *lineno
3518){
drhcc83b6e2004-04-23 23:38:42 +00003519 char *cp = 0;
drh75897232000-05-29 14:26:00 +00003520
drh75897232000-05-29 14:26:00 +00003521 if( sp->type==TERMINAL ){
3522 cp = lemp->tokendest;
3523 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003524 fprintf(out,"{\n"); (*lineno)++;
drh960e8c62001-04-03 16:53:21 +00003525 }else if( sp->destructor ){
drh75897232000-05-29 14:26:00 +00003526 cp = sp->destructor;
drha5808f32008-04-27 22:19:44 +00003527 fprintf(out,"{\n"); (*lineno)++;
drh25473362015-09-04 18:03:45 +00003528 if( !lemp->nolinenosflag ){
3529 (*lineno)++;
3530 tplt_linedir(out,sp->destLineno,lemp->filename);
3531 }
drh960e8c62001-04-03 16:53:21 +00003532 }else if( lemp->vardest ){
3533 cp = lemp->vardest;
3534 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003535 fprintf(out,"{\n"); (*lineno)++;
drhcc83b6e2004-04-23 23:38:42 +00003536 }else{
3537 assert( 0 ); /* Cannot happen */
drh75897232000-05-29 14:26:00 +00003538 }
3539 for(; *cp; cp++){
3540 if( *cp=='$' && cp[1]=='$' ){
3541 fprintf(out,"(yypminor->yy%d)",sp->dtnum);
3542 cp++;
3543 continue;
3544 }
shane58543932008-12-10 20:10:04 +00003545 if( *cp=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003546 fputc(*cp,out);
3547 }
shane58543932008-12-10 20:10:04 +00003548 fprintf(out,"\n"); (*lineno)++;
drh06f60d82017-04-14 19:46:12 +00003549 if (!lemp->nolinenosflag) {
3550 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
shane58543932008-12-10 20:10:04 +00003551 }
3552 fprintf(out,"}\n"); (*lineno)++;
drh75897232000-05-29 14:26:00 +00003553 return;
3554}
3555
3556/*
drh960e8c62001-04-03 16:53:21 +00003557** Return TRUE (non-zero) if the given symbol has a destructor.
drh75897232000-05-29 14:26:00 +00003558*/
icculus9e44cf12010-02-14 17:14:22 +00003559int has_destructor(struct symbol *sp, struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003560{
3561 int ret;
3562 if( sp->type==TERMINAL ){
3563 ret = lemp->tokendest!=0;
3564 }else{
drh960e8c62001-04-03 16:53:21 +00003565 ret = lemp->vardest!=0 || sp->destructor!=0;
drh75897232000-05-29 14:26:00 +00003566 }
3567 return ret;
3568}
3569
drh0bb132b2004-07-20 14:06:51 +00003570/*
3571** Append text to a dynamically allocated string. If zText is 0 then
3572** reset the string to be empty again. Always return the complete text
3573** of the string (which is overwritten with each call).
drh7ac25c72004-08-19 15:12:26 +00003574**
3575** n bytes of zText are stored. If n==0 then all of zText up to the first
3576** \000 terminator is stored. zText can contain up to two instances of
3577** %d. The values of p1 and p2 are written into the first and second
3578** %d.
3579**
3580** If n==-1, then the previous character is overwritten.
drh0bb132b2004-07-20 14:06:51 +00003581*/
icculus9e44cf12010-02-14 17:14:22 +00003582PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
3583 static char empty[1] = { 0 };
drh0bb132b2004-07-20 14:06:51 +00003584 static char *z = 0;
3585 static int alloced = 0;
3586 static int used = 0;
drhaf805ca2004-09-07 11:28:25 +00003587 int c;
drh0bb132b2004-07-20 14:06:51 +00003588 char zInt[40];
drh0bb132b2004-07-20 14:06:51 +00003589 if( zText==0 ){
drh4dd0d3f2016-02-17 01:18:33 +00003590 if( used==0 && z!=0 ) z[0] = 0;
drh0bb132b2004-07-20 14:06:51 +00003591 used = 0;
3592 return z;
3593 }
drh7ac25c72004-08-19 15:12:26 +00003594 if( n<=0 ){
3595 if( n<0 ){
3596 used += n;
3597 assert( used>=0 );
3598 }
drh87cf1372008-08-13 20:09:06 +00003599 n = lemonStrlen(zText);
drh7ac25c72004-08-19 15:12:26 +00003600 }
drhdf609712010-11-23 20:55:27 +00003601 if( (int) (n+sizeof(zInt)*2+used) >= alloced ){
drh0bb132b2004-07-20 14:06:51 +00003602 alloced = n + sizeof(zInt)*2 + used + 200;
icculus9e44cf12010-02-14 17:14:22 +00003603 z = (char *) realloc(z, alloced);
drh0bb132b2004-07-20 14:06:51 +00003604 }
icculus9e44cf12010-02-14 17:14:22 +00003605 if( z==0 ) return empty;
drh0bb132b2004-07-20 14:06:51 +00003606 while( n-- > 0 ){
3607 c = *(zText++);
drh50489622006-10-13 12:25:29 +00003608 if( c=='%' && n>0 && zText[0]=='d' ){
drh898799f2014-01-10 23:21:00 +00003609 lemon_sprintf(zInt, "%d", p1);
drh0bb132b2004-07-20 14:06:51 +00003610 p1 = p2;
drh898799f2014-01-10 23:21:00 +00003611 lemon_strcpy(&z[used], zInt);
drh87cf1372008-08-13 20:09:06 +00003612 used += lemonStrlen(&z[used]);
drh0bb132b2004-07-20 14:06:51 +00003613 zText++;
3614 n--;
3615 }else{
mistachkin2318d332015-01-12 18:02:52 +00003616 z[used++] = (char)c;
drh0bb132b2004-07-20 14:06:51 +00003617 }
3618 }
3619 z[used] = 0;
3620 return z;
3621}
3622
3623/*
drh711c9812016-05-23 14:24:31 +00003624** Write and transform the rp->code string so that symbols are expanded.
3625** Populate the rp->codePrefix and rp->codeSuffix strings, as appropriate.
drhdabd04c2016-02-17 01:46:19 +00003626**
3627** Return 1 if the expanded code requires that "yylhsminor" local variable
3628** to be defined.
drh0bb132b2004-07-20 14:06:51 +00003629*/
drhdabd04c2016-02-17 01:46:19 +00003630PRIVATE int translate_code(struct lemon *lemp, struct rule *rp){
drh0bb132b2004-07-20 14:06:51 +00003631 char *cp, *xp;
3632 int i;
drhcf82f0d2016-02-17 04:33:10 +00003633 int rc = 0; /* True if yylhsminor is used */
drh43303de2016-02-17 12:34:03 +00003634 int dontUseRhs0 = 0; /* If true, use of left-most RHS label is illegal */
drhcf82f0d2016-02-17 04:33:10 +00003635 const char *zSkip = 0; /* The zOvwrt comment within rp->code, or NULL */
3636 char lhsused = 0; /* True if the LHS element has been used */
3637 char lhsdirect; /* True if LHS writes directly into stack */
3638 char used[MAXRHS]; /* True for each RHS element which is used */
3639 char zLhs[50]; /* Convert the LHS symbol into this string */
3640 char zOvwrt[900]; /* Comment that to allow LHS to overwrite RHS */
drh0bb132b2004-07-20 14:06:51 +00003641
3642 for(i=0; i<rp->nrhs; i++) used[i] = 0;
3643 lhsused = 0;
3644
drh19c9e562007-03-29 20:13:53 +00003645 if( rp->code==0 ){
icculus9e44cf12010-02-14 17:14:22 +00003646 static char newlinestr[2] = { '\n', '\0' };
3647 rp->code = newlinestr;
drh19c9e562007-03-29 20:13:53 +00003648 rp->line = rp->ruleline;
drh711c9812016-05-23 14:24:31 +00003649 rp->noCode = 1;
3650 }else{
3651 rp->noCode = 0;
drh19c9e562007-03-29 20:13:53 +00003652 }
3653
drh4dd0d3f2016-02-17 01:18:33 +00003654
drh2e55b042016-04-30 17:19:30 +00003655 if( rp->nrhs==0 ){
drh4dd0d3f2016-02-17 01:18:33 +00003656 /* If there are no RHS symbols, then writing directly to the LHS is ok */
3657 lhsdirect = 1;
3658 }else if( rp->rhsalias[0]==0 ){
drh2e55b042016-04-30 17:19:30 +00003659 /* The left-most RHS symbol has no value. LHS direct is ok. But
drh4dd0d3f2016-02-17 01:18:33 +00003660 ** we have to call the distructor on the RHS symbol first. */
3661 lhsdirect = 1;
3662 if( has_destructor(rp->rhs[0],lemp) ){
3663 append_str(0,0,0,0);
3664 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
3665 rp->rhs[0]->index,1-rp->nrhs);
3666 rp->codePrefix = Strsafe(append_str(0,0,0,0));
drh711c9812016-05-23 14:24:31 +00003667 rp->noCode = 0;
drh4dd0d3f2016-02-17 01:18:33 +00003668 }
drh2e55b042016-04-30 17:19:30 +00003669 }else if( rp->lhsalias==0 ){
3670 /* There is no LHS value symbol. */
3671 lhsdirect = 1;
drh4dd0d3f2016-02-17 01:18:33 +00003672 }else if( strcmp(rp->lhsalias,rp->rhsalias[0])==0 ){
drh06f60d82017-04-14 19:46:12 +00003673 /* The LHS symbol and the left-most RHS symbol are the same, so
drh4dd0d3f2016-02-17 01:18:33 +00003674 ** direct writing is allowed */
3675 lhsdirect = 1;
3676 lhsused = 1;
3677 used[0] = 1;
3678 if( rp->lhs->dtnum!=rp->rhs[0]->dtnum ){
3679 ErrorMsg(lemp->filename,rp->ruleline,
3680 "%s(%s) and %s(%s) share the same label but have "
3681 "different datatypes.",
3682 rp->lhs->name, rp->lhsalias, rp->rhs[0]->name, rp->rhsalias[0]);
3683 lemp->errorcnt++;
drh06f60d82017-04-14 19:46:12 +00003684 }
drh4dd0d3f2016-02-17 01:18:33 +00003685 }else{
drhcf82f0d2016-02-17 04:33:10 +00003686 lemon_sprintf(zOvwrt, "/*%s-overwrites-%s*/",
3687 rp->lhsalias, rp->rhsalias[0]);
3688 zSkip = strstr(rp->code, zOvwrt);
3689 if( zSkip!=0 ){
3690 /* The code contains a special comment that indicates that it is safe
3691 ** for the LHS label to overwrite left-most RHS label. */
3692 lhsdirect = 1;
3693 }else{
3694 lhsdirect = 0;
3695 }
drh4dd0d3f2016-02-17 01:18:33 +00003696 }
3697 if( lhsdirect ){
3698 sprintf(zLhs, "yymsp[%d].minor.yy%d",1-rp->nrhs,rp->lhs->dtnum);
3699 }else{
drhdabd04c2016-02-17 01:46:19 +00003700 rc = 1;
drh4dd0d3f2016-02-17 01:18:33 +00003701 sprintf(zLhs, "yylhsminor.yy%d",rp->lhs->dtnum);
3702 }
3703
drh0bb132b2004-07-20 14:06:51 +00003704 append_str(0,0,0,0);
icculus9e44cf12010-02-14 17:14:22 +00003705
3706 /* This const cast is wrong but harmless, if we're careful. */
3707 for(cp=(char *)rp->code; *cp; cp++){
drhcf82f0d2016-02-17 04:33:10 +00003708 if( cp==zSkip ){
3709 append_str(zOvwrt,0,0,0);
3710 cp += lemonStrlen(zOvwrt)-1;
drh43303de2016-02-17 12:34:03 +00003711 dontUseRhs0 = 1;
drhcf82f0d2016-02-17 04:33:10 +00003712 continue;
3713 }
drhc56fac72015-10-29 13:48:15 +00003714 if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){
drh0bb132b2004-07-20 14:06:51 +00003715 char saved;
drhc56fac72015-10-29 13:48:15 +00003716 for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++);
drh0bb132b2004-07-20 14:06:51 +00003717 saved = *xp;
3718 *xp = 0;
3719 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
drh4dd0d3f2016-02-17 01:18:33 +00003720 append_str(zLhs,0,0,0);
drh0bb132b2004-07-20 14:06:51 +00003721 cp = xp;
3722 lhsused = 1;
3723 }else{
3724 for(i=0; i<rp->nrhs; i++){
3725 if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
drh43303de2016-02-17 12:34:03 +00003726 if( i==0 && dontUseRhs0 ){
3727 ErrorMsg(lemp->filename,rp->ruleline,
3728 "Label %s used after '%s'.",
3729 rp->rhsalias[0], zOvwrt);
3730 lemp->errorcnt++;
3731 }else if( cp!=rp->code && cp[-1]=='@' ){
drh7ac25c72004-08-19 15:12:26 +00003732 /* If the argument is of the form @X then substituted
3733 ** the token number of X, not the value of X */
3734 append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0);
3735 }else{
drhfd405312005-11-06 04:06:59 +00003736 struct symbol *sp = rp->rhs[i];
3737 int dtnum;
3738 if( sp->type==MULTITERMINAL ){
3739 dtnum = sp->subsym[0]->dtnum;
3740 }else{
3741 dtnum = sp->dtnum;
3742 }
3743 append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum);
drh7ac25c72004-08-19 15:12:26 +00003744 }
drh0bb132b2004-07-20 14:06:51 +00003745 cp = xp;
3746 used[i] = 1;
3747 break;
3748 }
3749 }
3750 }
3751 *xp = saved;
3752 }
3753 append_str(cp, 1, 0, 0);
3754 } /* End loop */
3755
drh4dd0d3f2016-02-17 01:18:33 +00003756 /* Main code generation completed */
3757 cp = append_str(0,0,0,0);
3758 if( cp && cp[0] ) rp->code = Strsafe(cp);
3759 append_str(0,0,0,0);
3760
drh0bb132b2004-07-20 14:06:51 +00003761 /* Check to make sure the LHS has been used */
3762 if( rp->lhsalias && !lhsused ){
3763 ErrorMsg(lemp->filename,rp->ruleline,
3764 "Label \"%s\" for \"%s(%s)\" is never used.",
3765 rp->lhsalias,rp->lhs->name,rp->lhsalias);
3766 lemp->errorcnt++;
3767 }
3768
drh4dd0d3f2016-02-17 01:18:33 +00003769 /* Generate destructor code for RHS minor values which are not referenced.
3770 ** Generate error messages for unused labels and duplicate labels.
3771 */
drh0bb132b2004-07-20 14:06:51 +00003772 for(i=0; i<rp->nrhs; i++){
drh4dd0d3f2016-02-17 01:18:33 +00003773 if( rp->rhsalias[i] ){
3774 if( i>0 ){
3775 int j;
3776 if( rp->lhsalias && strcmp(rp->lhsalias,rp->rhsalias[i])==0 ){
3777 ErrorMsg(lemp->filename,rp->ruleline,
3778 "%s(%s) has the same label as the LHS but is not the left-most "
3779 "symbol on the RHS.",
3780 rp->rhs[i]->name, rp->rhsalias);
3781 lemp->errorcnt++;
3782 }
3783 for(j=0; j<i; j++){
3784 if( rp->rhsalias[j] && strcmp(rp->rhsalias[j],rp->rhsalias[i])==0 ){
3785 ErrorMsg(lemp->filename,rp->ruleline,
3786 "Label %s used for multiple symbols on the RHS of a rule.",
3787 rp->rhsalias[i]);
3788 lemp->errorcnt++;
3789 break;
3790 }
3791 }
drh0bb132b2004-07-20 14:06:51 +00003792 }
drh4dd0d3f2016-02-17 01:18:33 +00003793 if( !used[i] ){
3794 ErrorMsg(lemp->filename,rp->ruleline,
3795 "Label %s for \"%s(%s)\" is never used.",
3796 rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
3797 lemp->errorcnt++;
3798 }
3799 }else if( i>0 && has_destructor(rp->rhs[i],lemp) ){
3800 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
3801 rp->rhs[i]->index,i-rp->nrhs+1);
drh0bb132b2004-07-20 14:06:51 +00003802 }
3803 }
drh4dd0d3f2016-02-17 01:18:33 +00003804
3805 /* If unable to write LHS values directly into the stack, write the
3806 ** saved LHS value now. */
3807 if( lhsdirect==0 ){
3808 append_str(" yymsp[%d].minor.yy%d = ", 0, 1-rp->nrhs, rp->lhs->dtnum);
3809 append_str(zLhs, 0, 0, 0);
3810 append_str(";\n", 0, 0, 0);
drh61e339a2007-01-16 03:09:02 +00003811 }
drh4dd0d3f2016-02-17 01:18:33 +00003812
3813 /* Suffix code generation complete */
3814 cp = append_str(0,0,0,0);
drh711c9812016-05-23 14:24:31 +00003815 if( cp && cp[0] ){
3816 rp->codeSuffix = Strsafe(cp);
3817 rp->noCode = 0;
3818 }
drhdabd04c2016-02-17 01:46:19 +00003819
3820 return rc;
drh0bb132b2004-07-20 14:06:51 +00003821}
3822
drh06f60d82017-04-14 19:46:12 +00003823/*
drh75897232000-05-29 14:26:00 +00003824** Generate code which executes when the rule "rp" is reduced. Write
3825** the code to "out". Make sure lineno stays up-to-date.
3826*/
icculus9e44cf12010-02-14 17:14:22 +00003827PRIVATE void emit_code(
3828 FILE *out,
3829 struct rule *rp,
3830 struct lemon *lemp,
3831 int *lineno
3832){
3833 const char *cp;
drh75897232000-05-29 14:26:00 +00003834
drh4dd0d3f2016-02-17 01:18:33 +00003835 /* Setup code prior to the #line directive */
3836 if( rp->codePrefix && rp->codePrefix[0] ){
3837 fprintf(out, "{%s", rp->codePrefix);
3838 for(cp=rp->codePrefix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
3839 }
3840
drh75897232000-05-29 14:26:00 +00003841 /* Generate code to do the reduce action */
3842 if( rp->code ){
drh25473362015-09-04 18:03:45 +00003843 if( !lemp->nolinenosflag ){
3844 (*lineno)++;
3845 tplt_linedir(out,rp->line,lemp->filename);
3846 }
drhaf805ca2004-09-07 11:28:25 +00003847 fprintf(out,"{%s",rp->code);
drh4dd0d3f2016-02-17 01:18:33 +00003848 for(cp=rp->code; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
shane58543932008-12-10 20:10:04 +00003849 fprintf(out,"}\n"); (*lineno)++;
drh25473362015-09-04 18:03:45 +00003850 if( !lemp->nolinenosflag ){
3851 (*lineno)++;
3852 tplt_linedir(out,*lineno,lemp->outname);
3853 }
drh4dd0d3f2016-02-17 01:18:33 +00003854 }
3855
3856 /* Generate breakdown code that occurs after the #line directive */
3857 if( rp->codeSuffix && rp->codeSuffix[0] ){
3858 fprintf(out, "%s", rp->codeSuffix);
3859 for(cp=rp->codeSuffix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
3860 }
3861
3862 if( rp->codePrefix ){
3863 fprintf(out, "}\n"); (*lineno)++;
3864 }
drh75897232000-05-29 14:26:00 +00003865
drh75897232000-05-29 14:26:00 +00003866 return;
3867}
3868
3869/*
3870** Print the definition of the union used for the parser's data stack.
3871** This union contains fields for every possible data type for tokens
3872** and nonterminals. In the process of computing and printing this
3873** union, also set the ".dtnum" field of every terminal and nonterminal
3874** symbol.
3875*/
icculus9e44cf12010-02-14 17:14:22 +00003876void print_stack_union(
3877 FILE *out, /* The output stream */
3878 struct lemon *lemp, /* The main info structure for this parser */
3879 int *plineno, /* Pointer to the line number */
3880 int mhflag /* True if generating makeheaders output */
3881){
drh75897232000-05-29 14:26:00 +00003882 int lineno = *plineno; /* The line number of the output */
3883 char **types; /* A hash table of datatypes */
3884 int arraysize; /* Size of the "types" array */
3885 int maxdtlength; /* Maximum length of any ".datatype" field. */
3886 char *stddt; /* Standardized name for a datatype */
3887 int i,j; /* Loop counters */
drh01f75f22013-10-02 20:46:30 +00003888 unsigned hash; /* For hashing the name of a type */
icculus9e44cf12010-02-14 17:14:22 +00003889 const char *name; /* Name of the parser */
drh75897232000-05-29 14:26:00 +00003890
3891 /* Allocate and initialize types[] and allocate stddt[] */
3892 arraysize = lemp->nsymbol * 2;
drh9892c5d2007-12-21 00:02:11 +00003893 types = (char**)calloc( arraysize, sizeof(char*) );
drh070d4222011-06-02 15:48:51 +00003894 if( types==0 ){
3895 fprintf(stderr,"Out of memory.\n");
3896 exit(1);
3897 }
drh75897232000-05-29 14:26:00 +00003898 for(i=0; i<arraysize; i++) types[i] = 0;
3899 maxdtlength = 0;
drh960e8c62001-04-03 16:53:21 +00003900 if( lemp->vartype ){
drh87cf1372008-08-13 20:09:06 +00003901 maxdtlength = lemonStrlen(lemp->vartype);
drh960e8c62001-04-03 16:53:21 +00003902 }
drh75897232000-05-29 14:26:00 +00003903 for(i=0; i<lemp->nsymbol; i++){
3904 int len;
3905 struct symbol *sp = lemp->symbols[i];
3906 if( sp->datatype==0 ) continue;
drh87cf1372008-08-13 20:09:06 +00003907 len = lemonStrlen(sp->datatype);
drh75897232000-05-29 14:26:00 +00003908 if( len>maxdtlength ) maxdtlength = len;
3909 }
3910 stddt = (char*)malloc( maxdtlength*2 + 1 );
drh070d4222011-06-02 15:48:51 +00003911 if( stddt==0 ){
drh75897232000-05-29 14:26:00 +00003912 fprintf(stderr,"Out of memory.\n");
3913 exit(1);
3914 }
3915
3916 /* Build a hash table of datatypes. The ".dtnum" field of each symbol
3917 ** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
drh960e8c62001-04-03 16:53:21 +00003918 ** used for terminal symbols. If there is no %default_type defined then
3919 ** 0 is also used as the .dtnum value for nonterminals which do not specify
3920 ** a datatype using the %type directive.
3921 */
drh75897232000-05-29 14:26:00 +00003922 for(i=0; i<lemp->nsymbol; i++){
3923 struct symbol *sp = lemp->symbols[i];
3924 char *cp;
3925 if( sp==lemp->errsym ){
3926 sp->dtnum = arraysize+1;
3927 continue;
3928 }
drh960e8c62001-04-03 16:53:21 +00003929 if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
drh75897232000-05-29 14:26:00 +00003930 sp->dtnum = 0;
3931 continue;
3932 }
3933 cp = sp->datatype;
drh960e8c62001-04-03 16:53:21 +00003934 if( cp==0 ) cp = lemp->vartype;
drh75897232000-05-29 14:26:00 +00003935 j = 0;
drhc56fac72015-10-29 13:48:15 +00003936 while( ISSPACE(*cp) ) cp++;
drh75897232000-05-29 14:26:00 +00003937 while( *cp ) stddt[j++] = *cp++;
drhc56fac72015-10-29 13:48:15 +00003938 while( j>0 && ISSPACE(stddt[j-1]) ) j--;
drh75897232000-05-29 14:26:00 +00003939 stddt[j] = 0;
drh02368c92009-04-05 15:18:02 +00003940 if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){
drh32c4d742008-07-01 16:34:49 +00003941 sp->dtnum = 0;
3942 continue;
3943 }
drh75897232000-05-29 14:26:00 +00003944 hash = 0;
3945 for(j=0; stddt[j]; j++){
3946 hash = hash*53 + stddt[j];
3947 }
drh3b2129c2003-05-13 00:34:21 +00003948 hash = (hash & 0x7fffffff)%arraysize;
drh75897232000-05-29 14:26:00 +00003949 while( types[hash] ){
3950 if( strcmp(types[hash],stddt)==0 ){
3951 sp->dtnum = hash + 1;
3952 break;
3953 }
3954 hash++;
drh2b51f212013-10-11 23:01:02 +00003955 if( hash>=(unsigned)arraysize ) hash = 0;
drh75897232000-05-29 14:26:00 +00003956 }
3957 if( types[hash]==0 ){
3958 sp->dtnum = hash + 1;
drh87cf1372008-08-13 20:09:06 +00003959 types[hash] = (char*)malloc( lemonStrlen(stddt)+1 );
drh75897232000-05-29 14:26:00 +00003960 if( types[hash]==0 ){
3961 fprintf(stderr,"Out of memory.\n");
3962 exit(1);
3963 }
drh898799f2014-01-10 23:21:00 +00003964 lemon_strcpy(types[hash],stddt);
drh75897232000-05-29 14:26:00 +00003965 }
3966 }
3967
3968 /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
3969 name = lemp->name ? lemp->name : "Parse";
3970 lineno = *plineno;
3971 if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
3972 fprintf(out,"#define %sTOKENTYPE %s\n",name,
3973 lemp->tokentype?lemp->tokentype:"void*"); lineno++;
3974 if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
3975 fprintf(out,"typedef union {\n"); lineno++;
drh15b024c2008-12-11 02:20:43 +00003976 fprintf(out," int yyinit;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003977 fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
3978 for(i=0; i<arraysize; i++){
3979 if( types[i]==0 ) continue;
3980 fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
3981 free(types[i]);
3982 }
drhc4dd3fd2008-01-22 01:48:05 +00003983 if( lemp->errsym->useCnt ){
3984 fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
3985 }
drh75897232000-05-29 14:26:00 +00003986 free(stddt);
3987 free(types);
3988 fprintf(out,"} YYMINORTYPE;\n"); lineno++;
3989 *plineno = lineno;
3990}
3991
drhb29b0a52002-02-23 19:39:46 +00003992/*
3993** Return the name of a C datatype able to represent values between
drhc75e0162015-09-07 02:23:02 +00003994** lwr and upr, inclusive. If pnByte!=NULL then also write the sizeof
3995** for that type (1, 2, or 4) into *pnByte.
drhb29b0a52002-02-23 19:39:46 +00003996*/
drhc75e0162015-09-07 02:23:02 +00003997static const char *minimum_size_type(int lwr, int upr, int *pnByte){
3998 const char *zType = "int";
3999 int nByte = 4;
drh8b582012003-10-21 13:16:03 +00004000 if( lwr>=0 ){
4001 if( upr<=255 ){
drhc75e0162015-09-07 02:23:02 +00004002 zType = "unsigned char";
4003 nByte = 1;
drh8b582012003-10-21 13:16:03 +00004004 }else if( upr<65535 ){
drhc75e0162015-09-07 02:23:02 +00004005 zType = "unsigned short int";
4006 nByte = 2;
drh8b582012003-10-21 13:16:03 +00004007 }else{
drhc75e0162015-09-07 02:23:02 +00004008 zType = "unsigned int";
4009 nByte = 4;
drh8b582012003-10-21 13:16:03 +00004010 }
4011 }else if( lwr>=-127 && upr<=127 ){
drhc75e0162015-09-07 02:23:02 +00004012 zType = "signed char";
4013 nByte = 1;
drh8b582012003-10-21 13:16:03 +00004014 }else if( lwr>=-32767 && upr<32767 ){
drhc75e0162015-09-07 02:23:02 +00004015 zType = "short";
4016 nByte = 2;
drhb29b0a52002-02-23 19:39:46 +00004017 }
drhc75e0162015-09-07 02:23:02 +00004018 if( pnByte ) *pnByte = nByte;
4019 return zType;
drhb29b0a52002-02-23 19:39:46 +00004020}
4021
drhfdbf9282003-10-21 16:34:41 +00004022/*
4023** Each state contains a set of token transaction and a set of
4024** nonterminal transactions. Each of these sets makes an instance
4025** of the following structure. An array of these structures is used
4026** to order the creation of entries in the yy_action[] table.
4027*/
4028struct axset {
4029 struct state *stp; /* A pointer to a state */
4030 int isTkn; /* True to use tokens. False for non-terminals */
4031 int nAction; /* Number of actions */
drhe594bc32009-11-03 13:02:25 +00004032 int iOrder; /* Original order of action sets */
drhfdbf9282003-10-21 16:34:41 +00004033};
4034
4035/*
4036** Compare to axset structures for sorting purposes
4037*/
4038static int axset_compare(const void *a, const void *b){
4039 struct axset *p1 = (struct axset*)a;
4040 struct axset *p2 = (struct axset*)b;
drhe594bc32009-11-03 13:02:25 +00004041 int c;
4042 c = p2->nAction - p1->nAction;
4043 if( c==0 ){
drh337cd0d2015-09-07 23:40:42 +00004044 c = p1->iOrder - p2->iOrder;
drhe594bc32009-11-03 13:02:25 +00004045 }
4046 assert( c!=0 || p1==p2 );
4047 return c;
drhfdbf9282003-10-21 16:34:41 +00004048}
4049
drhc4dd3fd2008-01-22 01:48:05 +00004050/*
4051** Write text on "out" that describes the rule "rp".
4052*/
4053static void writeRuleText(FILE *out, struct rule *rp){
4054 int j;
4055 fprintf(out,"%s ::=", rp->lhs->name);
4056 for(j=0; j<rp->nrhs; j++){
4057 struct symbol *sp = rp->rhs[j];
drh61f92cd2014-01-11 03:06:18 +00004058 if( sp->type!=MULTITERMINAL ){
4059 fprintf(out," %s", sp->name);
4060 }else{
drhc4dd3fd2008-01-22 01:48:05 +00004061 int k;
drh61f92cd2014-01-11 03:06:18 +00004062 fprintf(out," %s", sp->subsym[0]->name);
drhc4dd3fd2008-01-22 01:48:05 +00004063 for(k=1; k<sp->nsubsym; k++){
4064 fprintf(out,"|%s",sp->subsym[k]->name);
4065 }
4066 }
4067 }
4068}
4069
4070
drh75897232000-05-29 14:26:00 +00004071/* Generate C source code for the parser */
icculus9e44cf12010-02-14 17:14:22 +00004072void ReportTable(
4073 struct lemon *lemp,
4074 int mhflag /* Output in makeheaders format if true */
4075){
drh75897232000-05-29 14:26:00 +00004076 FILE *out, *in;
4077 char line[LINESIZE];
4078 int lineno;
4079 struct state *stp;
4080 struct action *ap;
4081 struct rule *rp;
drh8b582012003-10-21 13:16:03 +00004082 struct acttab *pActtab;
drhc75e0162015-09-07 02:23:02 +00004083 int i, j, n, sz;
4084 int szActionType; /* sizeof(YYACTIONTYPE) */
4085 int szCodeType; /* sizeof(YYCODETYPE) */
icculus9e44cf12010-02-14 17:14:22 +00004086 const char *name;
drh8b582012003-10-21 13:16:03 +00004087 int mnTknOfst, mxTknOfst;
4088 int mnNtOfst, mxNtOfst;
drhfdbf9282003-10-21 16:34:41 +00004089 struct axset *ax;
drh75897232000-05-29 14:26:00 +00004090
drh5c8241b2017-12-24 23:38:10 +00004091 lemp->minShiftReduce = lemp->nstate;
4092 lemp->errAction = lemp->minShiftReduce + lemp->nrule;
4093 lemp->accAction = lemp->errAction + 1;
4094 lemp->noAction = lemp->accAction + 1;
4095 lemp->minReduce = lemp->noAction + 1;
4096 lemp->maxAction = lemp->minReduce + lemp->nrule;
4097
drh75897232000-05-29 14:26:00 +00004098 in = tplt_open(lemp);
4099 if( in==0 ) return;
drh2aa6ca42004-09-10 00:14:04 +00004100 out = file_open(lemp,".c","wb");
drh75897232000-05-29 14:26:00 +00004101 if( out==0 ){
4102 fclose(in);
4103 return;
4104 }
4105 lineno = 1;
4106 tplt_xfer(lemp->name,in,out,&lineno);
4107
4108 /* Generate the include code, if any */
drha5808f32008-04-27 22:19:44 +00004109 tplt_print(out,lemp,lemp->include,&lineno);
drh75897232000-05-29 14:26:00 +00004110 if( mhflag ){
mistachkin8e189222015-04-19 21:43:16 +00004111 char *incName = file_makename(lemp, ".h");
4112 fprintf(out,"#include \"%s\"\n", incName); lineno++;
4113 free(incName);
drh75897232000-05-29 14:26:00 +00004114 }
4115 tplt_xfer(lemp->name,in,out,&lineno);
4116
4117 /* Generate #defines for all tokens */
4118 if( mhflag ){
icculus9e44cf12010-02-14 17:14:22 +00004119 const char *prefix;
drh75897232000-05-29 14:26:00 +00004120 fprintf(out,"#if INTERFACE\n"); lineno++;
4121 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4122 else prefix = "";
4123 for(i=1; i<lemp->nterminal; i++){
4124 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
4125 lineno++;
4126 }
4127 fprintf(out,"#endif\n"); lineno++;
4128 }
4129 tplt_xfer(lemp->name,in,out,&lineno);
4130
4131 /* Generate the defines */
drh75897232000-05-29 14:26:00 +00004132 fprintf(out,"#define YYCODETYPE %s\n",
drhc75e0162015-09-07 02:23:02 +00004133 minimum_size_type(0, lemp->nsymbol+1, &szCodeType)); lineno++;
drh75897232000-05-29 14:26:00 +00004134 fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++;
4135 fprintf(out,"#define YYACTIONTYPE %s\n",
drh5c8241b2017-12-24 23:38:10 +00004136 minimum_size_type(0,lemp->maxAction,&szActionType)); lineno++;
drhe09daa92006-06-10 13:29:31 +00004137 if( lemp->wildcard ){
4138 fprintf(out,"#define YYWILDCARD %d\n",
4139 lemp->wildcard->index); lineno++;
4140 }
drh75897232000-05-29 14:26:00 +00004141 print_stack_union(out,lemp,&lineno,mhflag);
drhca44b5a2007-02-22 23:06:58 +00004142 fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004143 if( lemp->stacksize ){
drh75897232000-05-29 14:26:00 +00004144 fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
4145 }else{
4146 fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
4147 }
drhca44b5a2007-02-22 23:06:58 +00004148 fprintf(out, "#endif\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004149 if( mhflag ){
4150 fprintf(out,"#if INTERFACE\n"); lineno++;
4151 }
4152 name = lemp->name ? lemp->name : "Parse";
4153 if( lemp->arg && lemp->arg[0] ){
drh87cf1372008-08-13 20:09:06 +00004154 i = lemonStrlen(lemp->arg);
drhc56fac72015-10-29 13:48:15 +00004155 while( i>=1 && ISSPACE(lemp->arg[i-1]) ) i--;
4156 while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
drh1f245e42002-03-11 13:55:50 +00004157 fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++;
4158 fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++;
4159 fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n",
4160 name,lemp->arg,&lemp->arg[i]); lineno++;
4161 fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n",
4162 name,&lemp->arg[i],&lemp->arg[i]); lineno++;
drh75897232000-05-29 14:26:00 +00004163 }else{
drh1f245e42002-03-11 13:55:50 +00004164 fprintf(out,"#define %sARG_SDECL\n",name); lineno++;
4165 fprintf(out,"#define %sARG_PDECL\n",name); lineno++;
4166 fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
4167 fprintf(out,"#define %sARG_STORE\n",name); lineno++;
drh75897232000-05-29 14:26:00 +00004168 }
4169 if( mhflag ){
4170 fprintf(out,"#endif\n"); lineno++;
4171 }
drhc4dd3fd2008-01-22 01:48:05 +00004172 if( lemp->errsym->useCnt ){
drh3bd48ab2015-09-07 18:23:37 +00004173 fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
4174 fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
drhc4dd3fd2008-01-22 01:48:05 +00004175 }
drh0bd1f4e2002-06-06 18:54:39 +00004176 if( lemp->has_fallback ){
4177 fprintf(out,"#define YYFALLBACK 1\n"); lineno++;
4178 }
drh75897232000-05-29 14:26:00 +00004179
drh3bd48ab2015-09-07 18:23:37 +00004180 /* Compute the action table, but do not output it yet. The action
4181 ** table must be computed before generating the YYNSTATE macro because
4182 ** we need to know how many states can be eliminated.
drh75897232000-05-29 14:26:00 +00004183 */
drh3bd48ab2015-09-07 18:23:37 +00004184 ax = (struct axset *) calloc(lemp->nxstate*2, sizeof(ax[0]));
drhfdbf9282003-10-21 16:34:41 +00004185 if( ax==0 ){
4186 fprintf(stderr,"malloc failed\n");
4187 exit(1);
4188 }
drh3bd48ab2015-09-07 18:23:37 +00004189 for(i=0; i<lemp->nxstate; i++){
drh75897232000-05-29 14:26:00 +00004190 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00004191 ax[i*2].stp = stp;
4192 ax[i*2].isTkn = 1;
4193 ax[i*2].nAction = stp->nTknAct;
4194 ax[i*2+1].stp = stp;
4195 ax[i*2+1].isTkn = 0;
4196 ax[i*2+1].nAction = stp->nNtAct;
drh75897232000-05-29 14:26:00 +00004197 }
drh8b582012003-10-21 13:16:03 +00004198 mxTknOfst = mnTknOfst = 0;
4199 mxNtOfst = mnNtOfst = 0;
drh3bd48ab2015-09-07 18:23:37 +00004200 /* In an effort to minimize the action table size, use the heuristic
4201 ** of placing the largest action sets first */
4202 for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i;
4203 qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare);
drh3a9d6c72017-12-25 04:15:38 +00004204 pActtab = acttab_alloc(lemp->nsymbol, lemp->nterminal);
drh3bd48ab2015-09-07 18:23:37 +00004205 for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){
drhfdbf9282003-10-21 16:34:41 +00004206 stp = ax[i].stp;
4207 if( ax[i].isTkn ){
4208 for(ap=stp->ap; ap; ap=ap->next){
4209 int action;
4210 if( ap->sp->index>=lemp->nterminal ) continue;
4211 action = compute_action(lemp, ap);
4212 if( action<0 ) continue;
4213 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00004214 }
drh3a9d6c72017-12-25 04:15:38 +00004215 stp->iTknOfst = acttab_insert(pActtab, 1);
drhfdbf9282003-10-21 16:34:41 +00004216 if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
4217 if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
4218 }else{
4219 for(ap=stp->ap; ap; ap=ap->next){
4220 int action;
4221 if( ap->sp->index<lemp->nterminal ) continue;
4222 if( ap->sp->index==lemp->nsymbol ) continue;
4223 action = compute_action(lemp, ap);
4224 if( action<0 ) continue;
4225 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00004226 }
drh3a9d6c72017-12-25 04:15:38 +00004227 stp->iNtOfst = acttab_insert(pActtab, 0);
drhfdbf9282003-10-21 16:34:41 +00004228 if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
4229 if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
drh8b582012003-10-21 13:16:03 +00004230 }
drh337cd0d2015-09-07 23:40:42 +00004231#if 0 /* Uncomment for a trace of how the yy_action[] table fills out */
4232 { int jj, nn;
4233 for(jj=nn=0; jj<pActtab->nAction; jj++){
4234 if( pActtab->aAction[jj].action<0 ) nn++;
4235 }
4236 printf("%4d: State %3d %s n: %2d size: %5d freespace: %d\n",
4237 i, stp->statenum, ax[i].isTkn ? "Token" : "Var ",
4238 ax[i].nAction, pActtab->nAction, nn);
4239 }
4240#endif
drh8b582012003-10-21 13:16:03 +00004241 }
drhfdbf9282003-10-21 16:34:41 +00004242 free(ax);
drh8b582012003-10-21 13:16:03 +00004243
drh756b41e2016-05-24 18:55:08 +00004244 /* Mark rules that are actually used for reduce actions after all
4245 ** optimizations have been applied
4246 */
4247 for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE;
4248 for(i=0; i<lemp->nxstate; i++){
drh756b41e2016-05-24 18:55:08 +00004249 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
4250 if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){
drh69bfa552017-04-26 04:32:17 +00004251 ap->x.rp->doesReduce = 1;
drh756b41e2016-05-24 18:55:08 +00004252 }
4253 }
4254 }
4255
drh3bd48ab2015-09-07 18:23:37 +00004256 /* Finish rendering the constants now that the action table has
4257 ** been computed */
4258 fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++;
4259 fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
drh0d9de992017-12-26 18:04:23 +00004260 fprintf(out,"#define YYNTOKEN %d\n",lemp->nterminal); lineno++;
drh337cd0d2015-09-07 23:40:42 +00004261 fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++;
drh5c8241b2017-12-24 23:38:10 +00004262 i = lemp->minShiftReduce;
4263 fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",i); lineno++;
4264 i += lemp->nrule;
drh3bd48ab2015-09-07 18:23:37 +00004265 fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++;
drh5c8241b2017-12-24 23:38:10 +00004266 fprintf(out,"#define YY_ERROR_ACTION %d\n", lemp->errAction); lineno++;
4267 fprintf(out,"#define YY_ACCEPT_ACTION %d\n", lemp->accAction); lineno++;
4268 fprintf(out,"#define YY_NO_ACTION %d\n", lemp->noAction); lineno++;
4269 fprintf(out,"#define YY_MIN_REDUCE %d\n", lemp->minReduce); lineno++;
4270 i = lemp->minReduce + lemp->nrule;
drh3bd48ab2015-09-07 18:23:37 +00004271 fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004272 tplt_xfer(lemp->name,in,out,&lineno);
4273
4274 /* Now output the action table and its associates:
4275 **
4276 ** yy_action[] A single table containing all actions.
4277 ** yy_lookahead[] A table containing the lookahead for each entry in
4278 ** yy_action. Used to detect hash collisions.
4279 ** yy_shift_ofst[] For each state, the offset into yy_action for
4280 ** shifting terminals.
4281 ** yy_reduce_ofst[] For each state, the offset into yy_action for
4282 ** shifting non-terminals after a reduce.
4283 ** yy_default[] Default action for each state.
4284 */
4285
drh8b582012003-10-21 13:16:03 +00004286 /* Output the yy_action table */
drh3a9d6c72017-12-25 04:15:38 +00004287 lemp->nactiontab = n = acttab_action_size(pActtab);
drhc75e0162015-09-07 02:23:02 +00004288 lemp->tablesize += n*szActionType;
drhf16371d2009-11-03 19:18:31 +00004289 fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
4290 fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00004291 for(i=j=0; i<n; i++){
4292 int action = acttab_yyaction(pActtab, i);
drh5c8241b2017-12-24 23:38:10 +00004293 if( action<0 ) action = lemp->noAction;
drhfdbf9282003-10-21 16:34:41 +00004294 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004295 fprintf(out, " %4d,", action);
4296 if( j==9 || i==n-1 ){
4297 fprintf(out, "\n"); lineno++;
4298 j = 0;
4299 }else{
4300 j++;
4301 }
4302 }
4303 fprintf(out, "};\n"); lineno++;
4304
4305 /* Output the yy_lookahead table */
drh3a9d6c72017-12-25 04:15:38 +00004306 lemp->nlookaheadtab = n = acttab_lookahead_size(pActtab);
drhc75e0162015-09-07 02:23:02 +00004307 lemp->tablesize += n*szCodeType;
drh57196282004-10-06 15:41:16 +00004308 fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00004309 for(i=j=0; i<n; i++){
4310 int la = acttab_yylookahead(pActtab, i);
4311 if( la<0 ) la = lemp->nsymbol;
drhfdbf9282003-10-21 16:34:41 +00004312 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004313 fprintf(out, " %4d,", la);
4314 if( j==9 || i==n-1 ){
4315 fprintf(out, "\n"); lineno++;
4316 j = 0;
4317 }else{
4318 j++;
4319 }
4320 }
4321 fprintf(out, "};\n"); lineno++;
4322
4323 /* Output the yy_shift_ofst[] table */
drh3bd48ab2015-09-07 18:23:37 +00004324 n = lemp->nxstate;
drhada354d2005-11-05 15:03:59 +00004325 while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
drh701b6882016-08-10 13:30:43 +00004326 fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
4327 fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
4328 fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
drh06f60d82017-04-14 19:46:12 +00004329 fprintf(out, "static const %s yy_shift_ofst[] = {\n",
drh701b6882016-08-10 13:30:43 +00004330 minimum_size_type(mnTknOfst, lemp->nterminal+lemp->nactiontab, &sz));
4331 lineno++;
drhc75e0162015-09-07 02:23:02 +00004332 lemp->tablesize += n*sz;
drh8b582012003-10-21 13:16:03 +00004333 for(i=j=0; i<n; i++){
4334 int ofst;
4335 stp = lemp->sorted[i];
4336 ofst = stp->iTknOfst;
drh701b6882016-08-10 13:30:43 +00004337 if( ofst==NO_OFFSET ) ofst = lemp->nactiontab;
drhfdbf9282003-10-21 16:34:41 +00004338 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004339 fprintf(out, " %4d,", ofst);
4340 if( j==9 || i==n-1 ){
4341 fprintf(out, "\n"); lineno++;
4342 j = 0;
4343 }else{
4344 j++;
4345 }
4346 }
4347 fprintf(out, "};\n"); lineno++;
4348
4349 /* Output the yy_reduce_ofst[] table */
drh3bd48ab2015-09-07 18:23:37 +00004350 n = lemp->nxstate;
drhada354d2005-11-05 15:03:59 +00004351 while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00004352 fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
4353 fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
4354 fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
drh06f60d82017-04-14 19:46:12 +00004355 fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
drhc75e0162015-09-07 02:23:02 +00004356 minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++;
4357 lemp->tablesize += n*sz;
drh8b582012003-10-21 13:16:03 +00004358 for(i=j=0; i<n; i++){
4359 int ofst;
4360 stp = lemp->sorted[i];
4361 ofst = stp->iNtOfst;
4362 if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00004363 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004364 fprintf(out, " %4d,", ofst);
4365 if( j==9 || i==n-1 ){
4366 fprintf(out, "\n"); lineno++;
4367 j = 0;
4368 }else{
4369 j++;
4370 }
4371 }
4372 fprintf(out, "};\n"); lineno++;
4373
4374 /* Output the default action table */
drh57196282004-10-06 15:41:16 +00004375 fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004376 n = lemp->nxstate;
drhc75e0162015-09-07 02:23:02 +00004377 lemp->tablesize += n*szActionType;
drh8b582012003-10-21 13:16:03 +00004378 for(i=j=0; i<n; i++){
4379 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00004380 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh5c8241b2017-12-24 23:38:10 +00004381 if( stp->iDfltReduce<0 ){
4382 fprintf(out, " %4d,", lemp->errAction);
4383 }else{
4384 fprintf(out, " %4d,", stp->iDfltReduce + lemp->minReduce);
4385 }
drh8b582012003-10-21 13:16:03 +00004386 if( j==9 || i==n-1 ){
4387 fprintf(out, "\n"); lineno++;
4388 j = 0;
4389 }else{
4390 j++;
4391 }
4392 }
4393 fprintf(out, "};\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004394 tplt_xfer(lemp->name,in,out,&lineno);
4395
drh0bd1f4e2002-06-06 18:54:39 +00004396 /* Generate the table of fallback tokens.
4397 */
4398 if( lemp->has_fallback ){
drh1441f3e2009-06-12 12:50:50 +00004399 int mx = lemp->nterminal - 1;
4400 while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; }
drhc75e0162015-09-07 02:23:02 +00004401 lemp->tablesize += (mx+1)*szCodeType;
drh1441f3e2009-06-12 12:50:50 +00004402 for(i=0; i<=mx; i++){
drh0bd1f4e2002-06-06 18:54:39 +00004403 struct symbol *p = lemp->symbols[i];
4404 if( p->fallback==0 ){
4405 fprintf(out, " 0, /* %10s => nothing */\n", p->name);
4406 }else{
4407 fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index,
4408 p->name, p->fallback->name);
4409 }
4410 lineno++;
4411 }
4412 }
4413 tplt_xfer(lemp->name, in, out, &lineno);
4414
4415 /* Generate a table containing the symbolic name of every symbol
4416 */
drh75897232000-05-29 14:26:00 +00004417 for(i=0; i<lemp->nsymbol; i++){
drh898799f2014-01-10 23:21:00 +00004418 lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name);
drh3a9d6c72017-12-25 04:15:38 +00004419 fprintf(out," /* %4d */ \"%s\",\n",i, lemp->symbols[i]->name); lineno++;
drh75897232000-05-29 14:26:00 +00004420 }
drh75897232000-05-29 14:26:00 +00004421 tplt_xfer(lemp->name,in,out,&lineno);
4422
drh0bd1f4e2002-06-06 18:54:39 +00004423 /* Generate a table containing a text string that describes every
drh34ff57b2008-07-14 12:27:51 +00004424 ** rule in the rule set of the grammar. This information is used
drh0bd1f4e2002-06-06 18:54:39 +00004425 ** when tracing REDUCE actions.
4426 */
4427 for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
drh4ef07702016-03-16 19:45:54 +00004428 assert( rp->iRule==i );
drhc4dd3fd2008-01-22 01:48:05 +00004429 fprintf(out," /* %3d */ \"", i);
4430 writeRuleText(out, rp);
drh0bd1f4e2002-06-06 18:54:39 +00004431 fprintf(out,"\",\n"); lineno++;
4432 }
4433 tplt_xfer(lemp->name,in,out,&lineno);
4434
drh75897232000-05-29 14:26:00 +00004435 /* Generate code which executes every time a symbol is popped from
drh06f60d82017-04-14 19:46:12 +00004436 ** the stack while processing errors or while destroying the parser.
drh0bd1f4e2002-06-06 18:54:39 +00004437 ** (In other words, generate the %destructor actions)
4438 */
drh75897232000-05-29 14:26:00 +00004439 if( lemp->tokendest ){
drh4dc8ef52008-07-01 17:13:57 +00004440 int once = 1;
drh75897232000-05-29 14:26:00 +00004441 for(i=0; i<lemp->nsymbol; i++){
4442 struct symbol *sp = lemp->symbols[i];
4443 if( sp==0 || sp->type!=TERMINAL ) continue;
drh4dc8ef52008-07-01 17:13:57 +00004444 if( once ){
4445 fprintf(out, " /* TERMINAL Destructor */\n"); lineno++;
4446 once = 0;
4447 }
drhc53eed12009-06-12 17:46:19 +00004448 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh75897232000-05-29 14:26:00 +00004449 }
4450 for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
4451 if( i<lemp->nsymbol ){
4452 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4453 fprintf(out," break;\n"); lineno++;
4454 }
4455 }
drh8d659732005-01-13 23:54:06 +00004456 if( lemp->vardest ){
4457 struct symbol *dflt_sp = 0;
drh4dc8ef52008-07-01 17:13:57 +00004458 int once = 1;
drh8d659732005-01-13 23:54:06 +00004459 for(i=0; i<lemp->nsymbol; i++){
4460 struct symbol *sp = lemp->symbols[i];
4461 if( sp==0 || sp->type==TERMINAL ||
4462 sp->index<=0 || sp->destructor!=0 ) continue;
drh4dc8ef52008-07-01 17:13:57 +00004463 if( once ){
drh5c8241b2017-12-24 23:38:10 +00004464 fprintf(out, " /* Default NON-TERMINAL Destructor */\n");lineno++;
drh4dc8ef52008-07-01 17:13:57 +00004465 once = 0;
4466 }
drhc53eed12009-06-12 17:46:19 +00004467 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh8d659732005-01-13 23:54:06 +00004468 dflt_sp = sp;
4469 }
4470 if( dflt_sp!=0 ){
4471 emit_destructor_code(out,dflt_sp,lemp,&lineno);
drh8d659732005-01-13 23:54:06 +00004472 }
drh4dc8ef52008-07-01 17:13:57 +00004473 fprintf(out," break;\n"); lineno++;
drh8d659732005-01-13 23:54:06 +00004474 }
drh75897232000-05-29 14:26:00 +00004475 for(i=0; i<lemp->nsymbol; i++){
4476 struct symbol *sp = lemp->symbols[i];
4477 if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
drh0f832dd2016-08-16 16:46:40 +00004478 if( sp->destLineno<0 ) continue; /* Already emitted */
drh75013012009-06-12 15:47:34 +00004479 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004480
4481 /* Combine duplicate destructors into a single case */
4482 for(j=i+1; j<lemp->nsymbol; j++){
4483 struct symbol *sp2 = lemp->symbols[j];
4484 if( sp2 && sp2->type!=TERMINAL && sp2->destructor
4485 && sp2->dtnum==sp->dtnum
4486 && strcmp(sp->destructor,sp2->destructor)==0 ){
drhc53eed12009-06-12 17:46:19 +00004487 fprintf(out," case %d: /* %s */\n",
4488 sp2->index, sp2->name); lineno++;
drh0f832dd2016-08-16 16:46:40 +00004489 sp2->destLineno = -1; /* Avoid emitting this destructor again */
drh0bb132b2004-07-20 14:06:51 +00004490 }
4491 }
4492
drh75897232000-05-29 14:26:00 +00004493 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4494 fprintf(out," break;\n"); lineno++;
4495 }
drh75897232000-05-29 14:26:00 +00004496 tplt_xfer(lemp->name,in,out,&lineno);
4497
4498 /* Generate code which executes whenever the parser stack overflows */
drha5808f32008-04-27 22:19:44 +00004499 tplt_print(out,lemp,lemp->overflow,&lineno);
drh75897232000-05-29 14:26:00 +00004500 tplt_xfer(lemp->name,in,out,&lineno);
4501
drh06f60d82017-04-14 19:46:12 +00004502 /* Generate the table of rule information
drh75897232000-05-29 14:26:00 +00004503 **
4504 ** Note: This code depends on the fact that rules are number
4505 ** sequentually beginning with 0.
4506 */
drh5c8241b2017-12-24 23:38:10 +00004507 for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
4508 fprintf(out," { %4d, %4d }, /* (%d) ",rp->lhs->index,-rp->nrhs,i);
4509 rule_print(out, rp);
4510 fprintf(out," */\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004511 }
4512 tplt_xfer(lemp->name,in,out,&lineno);
4513
4514 /* Generate code which execution during each REDUCE action */
drhdabd04c2016-02-17 01:46:19 +00004515 i = 0;
drh75897232000-05-29 14:26:00 +00004516 for(rp=lemp->rule; rp; rp=rp->next){
drhdabd04c2016-02-17 01:46:19 +00004517 i += translate_code(lemp, rp);
4518 }
4519 if( i ){
4520 fprintf(out," YYMINORTYPE yylhsminor;\n"); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004521 }
drhc53eed12009-06-12 17:46:19 +00004522 /* First output rules other than the default: rule */
drh0bb132b2004-07-20 14:06:51 +00004523 for(rp=lemp->rule; rp; rp=rp->next){
drhc53eed12009-06-12 17:46:19 +00004524 struct rule *rp2; /* Other rules with the same action */
drh711c9812016-05-23 14:24:31 +00004525 if( rp->codeEmitted ) continue;
4526 if( rp->noCode ){
4527 /* No C code actions, so this will be part of the "default:" rule */
drh2e55b042016-04-30 17:19:30 +00004528 continue;
4529 }
drh4ef07702016-03-16 19:45:54 +00004530 fprintf(out," case %d: /* ", rp->iRule);
drhc4dd3fd2008-01-22 01:48:05 +00004531 writeRuleText(out, rp);
4532 fprintf(out, " */\n"); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004533 for(rp2=rp->next; rp2; rp2=rp2->next){
drhafb8cd92016-04-29 11:28:35 +00004534 if( rp2->code==rp->code && rp2->codePrefix==rp->codePrefix
4535 && rp2->codeSuffix==rp->codeSuffix ){
drh4ef07702016-03-16 19:45:54 +00004536 fprintf(out," case %d: /* ", rp2->iRule);
drhc4dd3fd2008-01-22 01:48:05 +00004537 writeRuleText(out, rp2);
drh4ef07702016-03-16 19:45:54 +00004538 fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->iRule); lineno++;
drh711c9812016-05-23 14:24:31 +00004539 rp2->codeEmitted = 1;
drh0bb132b2004-07-20 14:06:51 +00004540 }
4541 }
drh75897232000-05-29 14:26:00 +00004542 emit_code(out,rp,lemp,&lineno);
4543 fprintf(out," break;\n"); lineno++;
drh711c9812016-05-23 14:24:31 +00004544 rp->codeEmitted = 1;
drh75897232000-05-29 14:26:00 +00004545 }
drhc53eed12009-06-12 17:46:19 +00004546 /* Finally, output the default: rule. We choose as the default: all
4547 ** empty actions. */
4548 fprintf(out," default:\n"); lineno++;
4549 for(rp=lemp->rule; rp; rp=rp->next){
drh711c9812016-05-23 14:24:31 +00004550 if( rp->codeEmitted ) continue;
4551 assert( rp->noCode );
drh4ef07702016-03-16 19:45:54 +00004552 fprintf(out," /* (%d) ", rp->iRule);
drhc53eed12009-06-12 17:46:19 +00004553 writeRuleText(out, rp);
drh756b41e2016-05-24 18:55:08 +00004554 if( rp->doesReduce ){
4555 fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->iRule); lineno++;
4556 }else{
4557 fprintf(out, " (OPTIMIZED OUT) */ assert(yyruleno!=%d);\n",
4558 rp->iRule); lineno++;
4559 }
drhc53eed12009-06-12 17:46:19 +00004560 }
4561 fprintf(out," break;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004562 tplt_xfer(lemp->name,in,out,&lineno);
4563
4564 /* Generate code which executes if a parse fails */
drha5808f32008-04-27 22:19:44 +00004565 tplt_print(out,lemp,lemp->failure,&lineno);
drh75897232000-05-29 14:26:00 +00004566 tplt_xfer(lemp->name,in,out,&lineno);
4567
4568 /* Generate code which executes when a syntax error occurs */
drha5808f32008-04-27 22:19:44 +00004569 tplt_print(out,lemp,lemp->error,&lineno);
drh75897232000-05-29 14:26:00 +00004570 tplt_xfer(lemp->name,in,out,&lineno);
4571
4572 /* Generate code which executes when the parser accepts its input */
drha5808f32008-04-27 22:19:44 +00004573 tplt_print(out,lemp,lemp->accept,&lineno);
drh75897232000-05-29 14:26:00 +00004574 tplt_xfer(lemp->name,in,out,&lineno);
4575
4576 /* Append any addition code the user desires */
drha5808f32008-04-27 22:19:44 +00004577 tplt_print(out,lemp,lemp->extracode,&lineno);
drh75897232000-05-29 14:26:00 +00004578
4579 fclose(in);
4580 fclose(out);
4581 return;
4582}
4583
4584/* Generate a header file for the parser */
icculus9e44cf12010-02-14 17:14:22 +00004585void ReportHeader(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004586{
4587 FILE *out, *in;
icculus9e44cf12010-02-14 17:14:22 +00004588 const char *prefix;
drh75897232000-05-29 14:26:00 +00004589 char line[LINESIZE];
4590 char pattern[LINESIZE];
4591 int i;
4592
4593 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4594 else prefix = "";
drh2aa6ca42004-09-10 00:14:04 +00004595 in = file_open(lemp,".h","rb");
drh75897232000-05-29 14:26:00 +00004596 if( in ){
drh8ba0d1c2012-06-16 15:26:31 +00004597 int nextChar;
drh75897232000-05-29 14:26:00 +00004598 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
drh61f92cd2014-01-11 03:06:18 +00004599 lemon_sprintf(pattern,"#define %s%-30s %3d\n",
4600 prefix,lemp->symbols[i]->name,i);
drh75897232000-05-29 14:26:00 +00004601 if( strcmp(line,pattern) ) break;
4602 }
drh8ba0d1c2012-06-16 15:26:31 +00004603 nextChar = fgetc(in);
drh75897232000-05-29 14:26:00 +00004604 fclose(in);
drh8ba0d1c2012-06-16 15:26:31 +00004605 if( i==lemp->nterminal && nextChar==EOF ){
drh75897232000-05-29 14:26:00 +00004606 /* No change in the file. Don't rewrite it. */
4607 return;
4608 }
4609 }
drh2aa6ca42004-09-10 00:14:04 +00004610 out = file_open(lemp,".h","wb");
drh75897232000-05-29 14:26:00 +00004611 if( out ){
4612 for(i=1; i<lemp->nterminal; i++){
drh61f92cd2014-01-11 03:06:18 +00004613 fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i);
drh75897232000-05-29 14:26:00 +00004614 }
drh06f60d82017-04-14 19:46:12 +00004615 fclose(out);
drh75897232000-05-29 14:26:00 +00004616 }
4617 return;
4618}
4619
4620/* Reduce the size of the action tables, if possible, by making use
4621** of defaults.
4622**
drhb59499c2002-02-23 18:45:13 +00004623** In this version, we take the most frequent REDUCE action and make
drhe09daa92006-06-10 13:29:31 +00004624** it the default. Except, there is no default if the wildcard token
4625** is a possible look-ahead.
drh75897232000-05-29 14:26:00 +00004626*/
icculus9e44cf12010-02-14 17:14:22 +00004627void CompressTables(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004628{
4629 struct state *stp;
drhc173ad82016-05-23 16:15:02 +00004630 struct action *ap, *ap2, *nextap;
drhb59499c2002-02-23 18:45:13 +00004631 struct rule *rp, *rp2, *rbest;
drh0c6dfaa2015-09-08 21:16:46 +00004632 int nbest, n;
drh75897232000-05-29 14:26:00 +00004633 int i;
drhe09daa92006-06-10 13:29:31 +00004634 int usesWildcard;
drh75897232000-05-29 14:26:00 +00004635
4636 for(i=0; i<lemp->nstate; i++){
4637 stp = lemp->sorted[i];
drhb59499c2002-02-23 18:45:13 +00004638 nbest = 0;
4639 rbest = 0;
drhe09daa92006-06-10 13:29:31 +00004640 usesWildcard = 0;
drh75897232000-05-29 14:26:00 +00004641
drhb59499c2002-02-23 18:45:13 +00004642 for(ap=stp->ap; ap; ap=ap->next){
drhe09daa92006-06-10 13:29:31 +00004643 if( ap->type==SHIFT && ap->sp==lemp->wildcard ){
4644 usesWildcard = 1;
4645 }
drhb59499c2002-02-23 18:45:13 +00004646 if( ap->type!=REDUCE ) continue;
4647 rp = ap->x.rp;
drhb4960992007-10-05 16:16:36 +00004648 if( rp->lhsStart ) continue;
drhb59499c2002-02-23 18:45:13 +00004649 if( rp==rbest ) continue;
4650 n = 1;
4651 for(ap2=ap->next; ap2; ap2=ap2->next){
4652 if( ap2->type!=REDUCE ) continue;
4653 rp2 = ap2->x.rp;
4654 if( rp2==rbest ) continue;
4655 if( rp2==rp ) n++;
4656 }
4657 if( n>nbest ){
4658 nbest = n;
4659 rbest = rp;
drh75897232000-05-29 14:26:00 +00004660 }
4661 }
drh06f60d82017-04-14 19:46:12 +00004662
drhb59499c2002-02-23 18:45:13 +00004663 /* Do not make a default if the number of rules to default
drhe09daa92006-06-10 13:29:31 +00004664 ** is not at least 1 or if the wildcard token is a possible
4665 ** lookahead.
4666 */
4667 if( nbest<1 || usesWildcard ) continue;
drh75897232000-05-29 14:26:00 +00004668
drhb59499c2002-02-23 18:45:13 +00004669
4670 /* Combine matching REDUCE actions into a single default */
4671 for(ap=stp->ap; ap; ap=ap->next){
4672 if( ap->type==REDUCE && ap->x.rp==rbest ) break;
4673 }
drh75897232000-05-29 14:26:00 +00004674 assert( ap );
4675 ap->sp = Symbol_new("{default}");
4676 for(ap=ap->next; ap; ap=ap->next){
drhb59499c2002-02-23 18:45:13 +00004677 if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
drh75897232000-05-29 14:26:00 +00004678 }
4679 stp->ap = Action_sort(stp->ap);
drh3bd48ab2015-09-07 18:23:37 +00004680
4681 for(ap=stp->ap; ap; ap=ap->next){
4682 if( ap->type==SHIFT ) break;
4683 if( ap->type==REDUCE && ap->x.rp!=rbest ) break;
4684 }
4685 if( ap==0 ){
4686 stp->autoReduce = 1;
4687 stp->pDfltReduce = rbest;
4688 }
4689 }
4690
4691 /* Make a second pass over all states and actions. Convert
4692 ** every action that is a SHIFT to an autoReduce state into
4693 ** a SHIFTREDUCE action.
4694 */
4695 for(i=0; i<lemp->nstate; i++){
4696 stp = lemp->sorted[i];
4697 for(ap=stp->ap; ap; ap=ap->next){
4698 struct state *pNextState;
4699 if( ap->type!=SHIFT ) continue;
4700 pNextState = ap->x.stp;
4701 if( pNextState->autoReduce && pNextState->pDfltReduce!=0 ){
4702 ap->type = SHIFTREDUCE;
4703 ap->x.rp = pNextState->pDfltReduce;
4704 }
4705 }
drh75897232000-05-29 14:26:00 +00004706 }
drhc173ad82016-05-23 16:15:02 +00004707
4708 /* If a SHIFTREDUCE action specifies a rule that has a single RHS term
4709 ** (meaning that the SHIFTREDUCE will land back in the state where it
4710 ** started) and if there is no C-code associated with the reduce action,
4711 ** then we can go ahead and convert the action to be the same as the
4712 ** action for the RHS of the rule.
4713 */
4714 for(i=0; i<lemp->nstate; i++){
4715 stp = lemp->sorted[i];
4716 for(ap=stp->ap; ap; ap=nextap){
4717 nextap = ap->next;
4718 if( ap->type!=SHIFTREDUCE ) continue;
4719 rp = ap->x.rp;
4720 if( rp->noCode==0 ) continue;
4721 if( rp->nrhs!=1 ) continue;
4722#if 1
4723 /* Only apply this optimization to non-terminals. It would be OK to
4724 ** apply it to terminal symbols too, but that makes the parser tables
4725 ** larger. */
4726 if( ap->sp->index<lemp->nterminal ) continue;
4727#endif
4728 /* If we reach this point, it means the optimization can be applied */
4729 nextap = ap;
4730 for(ap2=stp->ap; ap2 && (ap2==ap || ap2->sp!=rp->lhs); ap2=ap2->next){}
4731 assert( ap2!=0 );
4732 ap->spOpt = ap2->sp;
4733 ap->type = ap2->type;
4734 ap->x = ap2->x;
4735 }
4736 }
drh75897232000-05-29 14:26:00 +00004737}
drhb59499c2002-02-23 18:45:13 +00004738
drhada354d2005-11-05 15:03:59 +00004739
4740/*
4741** Compare two states for sorting purposes. The smaller state is the
4742** one with the most non-terminal actions. If they have the same number
4743** of non-terminal actions, then the smaller is the one with the most
4744** token actions.
4745*/
4746static int stateResortCompare(const void *a, const void *b){
4747 const struct state *pA = *(const struct state**)a;
4748 const struct state *pB = *(const struct state**)b;
4749 int n;
4750
4751 n = pB->nNtAct - pA->nNtAct;
4752 if( n==0 ){
4753 n = pB->nTknAct - pA->nTknAct;
drhe594bc32009-11-03 13:02:25 +00004754 if( n==0 ){
4755 n = pB->statenum - pA->statenum;
4756 }
drhada354d2005-11-05 15:03:59 +00004757 }
drhe594bc32009-11-03 13:02:25 +00004758 assert( n!=0 );
drhada354d2005-11-05 15:03:59 +00004759 return n;
4760}
4761
4762
4763/*
4764** Renumber and resort states so that states with fewer choices
4765** occur at the end. Except, keep state 0 as the first state.
4766*/
icculus9e44cf12010-02-14 17:14:22 +00004767void ResortStates(struct lemon *lemp)
drhada354d2005-11-05 15:03:59 +00004768{
4769 int i;
4770 struct state *stp;
4771 struct action *ap;
4772
4773 for(i=0; i<lemp->nstate; i++){
4774 stp = lemp->sorted[i];
4775 stp->nTknAct = stp->nNtAct = 0;
drh5c8241b2017-12-24 23:38:10 +00004776 stp->iDfltReduce = -1; /* Init dflt action to "syntax error" */
drhada354d2005-11-05 15:03:59 +00004777 stp->iTknOfst = NO_OFFSET;
4778 stp->iNtOfst = NO_OFFSET;
4779 for(ap=stp->ap; ap; ap=ap->next){
drh3bd48ab2015-09-07 18:23:37 +00004780 int iAction = compute_action(lemp,ap);
4781 if( iAction>=0 ){
drhada354d2005-11-05 15:03:59 +00004782 if( ap->sp->index<lemp->nterminal ){
4783 stp->nTknAct++;
4784 }else if( ap->sp->index<lemp->nsymbol ){
4785 stp->nNtAct++;
4786 }else{
drh3bd48ab2015-09-07 18:23:37 +00004787 assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp );
drh5c8241b2017-12-24 23:38:10 +00004788 stp->iDfltReduce = iAction;
drhada354d2005-11-05 15:03:59 +00004789 }
4790 }
4791 }
4792 }
4793 qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
4794 stateResortCompare);
4795 for(i=0; i<lemp->nstate; i++){
4796 lemp->sorted[i]->statenum = i;
4797 }
drh3bd48ab2015-09-07 18:23:37 +00004798 lemp->nxstate = lemp->nstate;
4799 while( lemp->nxstate>1 && lemp->sorted[lemp->nxstate-1]->autoReduce ){
4800 lemp->nxstate--;
4801 }
drhada354d2005-11-05 15:03:59 +00004802}
4803
4804
drh75897232000-05-29 14:26:00 +00004805/***************** From the file "set.c" ************************************/
4806/*
4807** Set manipulation routines for the LEMON parser generator.
4808*/
4809
4810static int size = 0;
4811
4812/* Set the set size */
icculus9e44cf12010-02-14 17:14:22 +00004813void SetSize(int n)
drh75897232000-05-29 14:26:00 +00004814{
4815 size = n+1;
4816}
4817
4818/* Allocate a new set */
drh14d88552017-04-14 19:44:15 +00004819char *SetNew(void){
drh75897232000-05-29 14:26:00 +00004820 char *s;
drh9892c5d2007-12-21 00:02:11 +00004821 s = (char*)calloc( size, 1);
drh75897232000-05-29 14:26:00 +00004822 if( s==0 ){
4823 extern void memory_error();
4824 memory_error();
4825 }
drh75897232000-05-29 14:26:00 +00004826 return s;
4827}
4828
4829/* Deallocate a set */
icculus9e44cf12010-02-14 17:14:22 +00004830void SetFree(char *s)
drh75897232000-05-29 14:26:00 +00004831{
4832 free(s);
4833}
4834
4835/* Add a new element to the set. Return TRUE if the element was added
4836** and FALSE if it was already there. */
icculus9e44cf12010-02-14 17:14:22 +00004837int SetAdd(char *s, int e)
drh75897232000-05-29 14:26:00 +00004838{
4839 int rv;
drh9892c5d2007-12-21 00:02:11 +00004840 assert( e>=0 && e<size );
drh75897232000-05-29 14:26:00 +00004841 rv = s[e];
4842 s[e] = 1;
4843 return !rv;
4844}
4845
4846/* Add every element of s2 to s1. Return TRUE if s1 changes. */
icculus9e44cf12010-02-14 17:14:22 +00004847int SetUnion(char *s1, char *s2)
drh75897232000-05-29 14:26:00 +00004848{
4849 int i, progress;
4850 progress = 0;
4851 for(i=0; i<size; i++){
4852 if( s2[i]==0 ) continue;
4853 if( s1[i]==0 ){
4854 progress = 1;
4855 s1[i] = 1;
4856 }
4857 }
4858 return progress;
4859}
4860/********************** From the file "table.c" ****************************/
4861/*
4862** All code in this file has been automatically generated
4863** from a specification in the file
4864** "table.q"
4865** by the associative array code building program "aagen".
4866** Do not edit this file! Instead, edit the specification
4867** file, then rerun aagen.
4868*/
4869/*
4870** Code for processing tables in the LEMON parser generator.
4871*/
4872
drh01f75f22013-10-02 20:46:30 +00004873PRIVATE unsigned strhash(const char *x)
drh75897232000-05-29 14:26:00 +00004874{
drh01f75f22013-10-02 20:46:30 +00004875 unsigned h = 0;
4876 while( *x ) h = h*13 + *(x++);
drh75897232000-05-29 14:26:00 +00004877 return h;
4878}
4879
4880/* Works like strdup, sort of. Save a string in malloced memory, but
4881** keep strings in a table so that the same string is not in more
4882** than one place.
4883*/
icculus9e44cf12010-02-14 17:14:22 +00004884const char *Strsafe(const char *y)
drh75897232000-05-29 14:26:00 +00004885{
icculus9e44cf12010-02-14 17:14:22 +00004886 const char *z;
4887 char *cpy;
drh75897232000-05-29 14:26:00 +00004888
drh916f75f2006-07-17 00:19:39 +00004889 if( y==0 ) return 0;
drh75897232000-05-29 14:26:00 +00004890 z = Strsafe_find(y);
icculus9e44cf12010-02-14 17:14:22 +00004891 if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
drh898799f2014-01-10 23:21:00 +00004892 lemon_strcpy(cpy,y);
icculus9e44cf12010-02-14 17:14:22 +00004893 z = cpy;
drh75897232000-05-29 14:26:00 +00004894 Strsafe_insert(z);
4895 }
4896 MemoryCheck(z);
4897 return z;
4898}
4899
4900/* There is one instance of the following structure for each
4901** associative array of type "x1".
4902*/
4903struct s_x1 {
4904 int size; /* The number of available slots. */
4905 /* Must be a power of 2 greater than or */
4906 /* equal to 1 */
4907 int count; /* Number of currently slots filled */
4908 struct s_x1node *tbl; /* The data stored here */
4909 struct s_x1node **ht; /* Hash table for lookups */
4910};
4911
4912/* There is one instance of this structure for every data element
4913** in an associative array of type "x1".
4914*/
4915typedef struct s_x1node {
icculus9e44cf12010-02-14 17:14:22 +00004916 const char *data; /* The data */
drh75897232000-05-29 14:26:00 +00004917 struct s_x1node *next; /* Next entry with the same hash */
4918 struct s_x1node **from; /* Previous link */
4919} x1node;
4920
4921/* There is only one instance of the array, which is the following */
4922static struct s_x1 *x1a;
4923
4924/* Allocate a new associative array */
drh14d88552017-04-14 19:44:15 +00004925void Strsafe_init(void){
drh75897232000-05-29 14:26:00 +00004926 if( x1a ) return;
4927 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
4928 if( x1a ){
4929 x1a->size = 1024;
4930 x1a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00004931 x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*));
drh75897232000-05-29 14:26:00 +00004932 if( x1a->tbl==0 ){
4933 free(x1a);
4934 x1a = 0;
4935 }else{
4936 int i;
4937 x1a->ht = (x1node**)&(x1a->tbl[1024]);
4938 for(i=0; i<1024; i++) x1a->ht[i] = 0;
4939 }
4940 }
4941}
4942/* Insert a new record into the array. Return TRUE if successful.
4943** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004944int Strsafe_insert(const char *data)
drh75897232000-05-29 14:26:00 +00004945{
4946 x1node *np;
drh01f75f22013-10-02 20:46:30 +00004947 unsigned h;
4948 unsigned ph;
drh75897232000-05-29 14:26:00 +00004949
4950 if( x1a==0 ) return 0;
4951 ph = strhash(data);
4952 h = ph & (x1a->size-1);
4953 np = x1a->ht[h];
4954 while( np ){
4955 if( strcmp(np->data,data)==0 ){
4956 /* An existing entry with the same key is found. */
4957 /* Fail because overwrite is not allows. */
4958 return 0;
4959 }
4960 np = np->next;
4961 }
4962 if( x1a->count>=x1a->size ){
4963 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00004964 int i,arrSize;
drh75897232000-05-29 14:26:00 +00004965 struct s_x1 array;
mistachkin8e189222015-04-19 21:43:16 +00004966 array.size = arrSize = x1a->size*2;
drh75897232000-05-29 14:26:00 +00004967 array.count = x1a->count;
mistachkin8e189222015-04-19 21:43:16 +00004968 array.tbl = (x1node*)calloc(arrSize, sizeof(x1node) + sizeof(x1node*));
drh75897232000-05-29 14:26:00 +00004969 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00004970 array.ht = (x1node**)&(array.tbl[arrSize]);
4971 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00004972 for(i=0; i<x1a->count; i++){
4973 x1node *oldnp, *newnp;
4974 oldnp = &(x1a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00004975 h = strhash(oldnp->data) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00004976 newnp = &(array.tbl[i]);
4977 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4978 newnp->next = array.ht[h];
4979 newnp->data = oldnp->data;
4980 newnp->from = &(array.ht[h]);
4981 array.ht[h] = newnp;
4982 }
4983 free(x1a->tbl);
4984 *x1a = array;
4985 }
4986 /* Insert the new data */
4987 h = ph & (x1a->size-1);
4988 np = &(x1a->tbl[x1a->count++]);
4989 np->data = data;
4990 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
4991 np->next = x1a->ht[h];
4992 x1a->ht[h] = np;
4993 np->from = &(x1a->ht[h]);
4994 return 1;
4995}
4996
4997/* Return a pointer to data assigned to the given key. Return NULL
4998** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004999const char *Strsafe_find(const char *key)
drh75897232000-05-29 14:26:00 +00005000{
drh01f75f22013-10-02 20:46:30 +00005001 unsigned h;
drh75897232000-05-29 14:26:00 +00005002 x1node *np;
5003
5004 if( x1a==0 ) return 0;
5005 h = strhash(key) & (x1a->size-1);
5006 np = x1a->ht[h];
5007 while( np ){
5008 if( strcmp(np->data,key)==0 ) break;
5009 np = np->next;
5010 }
5011 return np ? np->data : 0;
5012}
5013
5014/* Return a pointer to the (terminal or nonterminal) symbol "x".
5015** Create a new symbol if this is the first time "x" has been seen.
5016*/
icculus9e44cf12010-02-14 17:14:22 +00005017struct symbol *Symbol_new(const char *x)
drh75897232000-05-29 14:26:00 +00005018{
5019 struct symbol *sp;
5020
5021 sp = Symbol_find(x);
5022 if( sp==0 ){
drh9892c5d2007-12-21 00:02:11 +00005023 sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
drh75897232000-05-29 14:26:00 +00005024 MemoryCheck(sp);
5025 sp->name = Strsafe(x);
drhc56fac72015-10-29 13:48:15 +00005026 sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL;
drh75897232000-05-29 14:26:00 +00005027 sp->rule = 0;
drh0bd1f4e2002-06-06 18:54:39 +00005028 sp->fallback = 0;
drh75897232000-05-29 14:26:00 +00005029 sp->prec = -1;
5030 sp->assoc = UNK;
5031 sp->firstset = 0;
drhaa9f1122007-08-23 02:50:56 +00005032 sp->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00005033 sp->destructor = 0;
drh4dc8ef52008-07-01 17:13:57 +00005034 sp->destLineno = 0;
drh75897232000-05-29 14:26:00 +00005035 sp->datatype = 0;
drhc4dd3fd2008-01-22 01:48:05 +00005036 sp->useCnt = 0;
drh75897232000-05-29 14:26:00 +00005037 Symbol_insert(sp,sp->name);
5038 }
drhc4dd3fd2008-01-22 01:48:05 +00005039 sp->useCnt++;
drh75897232000-05-29 14:26:00 +00005040 return sp;
5041}
5042
drh61f92cd2014-01-11 03:06:18 +00005043/* Compare two symbols for sorting purposes. Return negative,
5044** zero, or positive if a is less then, equal to, or greater
5045** than b.
drh60d31652004-02-22 00:08:04 +00005046**
5047** Symbols that begin with upper case letters (terminals or tokens)
5048** must sort before symbols that begin with lower case letters
drh61f92cd2014-01-11 03:06:18 +00005049** (non-terminals). And MULTITERMINAL symbols (created using the
5050** %token_class directive) must sort at the very end. Other than
5051** that, the order does not matter.
drh60d31652004-02-22 00:08:04 +00005052**
5053** We find experimentally that leaving the symbols in their original
5054** order (the order they appeared in the grammar file) gives the
5055** smallest parser tables in SQLite.
5056*/
icculus9e44cf12010-02-14 17:14:22 +00005057int Symbolcmpp(const void *_a, const void *_b)
5058{
drh61f92cd2014-01-11 03:06:18 +00005059 const struct symbol *a = *(const struct symbol **) _a;
5060 const struct symbol *b = *(const struct symbol **) _b;
5061 int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1;
5062 int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1;
5063 return i1==i2 ? a->index - b->index : i1 - i2;
drh75897232000-05-29 14:26:00 +00005064}
5065
5066/* There is one instance of the following structure for each
5067** associative array of type "x2".
5068*/
5069struct s_x2 {
5070 int size; /* The number of available slots. */
5071 /* Must be a power of 2 greater than or */
5072 /* equal to 1 */
5073 int count; /* Number of currently slots filled */
5074 struct s_x2node *tbl; /* The data stored here */
5075 struct s_x2node **ht; /* Hash table for lookups */
5076};
5077
5078/* There is one instance of this structure for every data element
5079** in an associative array of type "x2".
5080*/
5081typedef struct s_x2node {
icculus9e44cf12010-02-14 17:14:22 +00005082 struct symbol *data; /* The data */
5083 const char *key; /* The key */
drh75897232000-05-29 14:26:00 +00005084 struct s_x2node *next; /* Next entry with the same hash */
5085 struct s_x2node **from; /* Previous link */
5086} x2node;
5087
5088/* There is only one instance of the array, which is the following */
5089static struct s_x2 *x2a;
5090
5091/* Allocate a new associative array */
drh14d88552017-04-14 19:44:15 +00005092void Symbol_init(void){
drh75897232000-05-29 14:26:00 +00005093 if( x2a ) return;
5094 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
5095 if( x2a ){
5096 x2a->size = 128;
5097 x2a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005098 x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*));
drh75897232000-05-29 14:26:00 +00005099 if( x2a->tbl==0 ){
5100 free(x2a);
5101 x2a = 0;
5102 }else{
5103 int i;
5104 x2a->ht = (x2node**)&(x2a->tbl[128]);
5105 for(i=0; i<128; i++) x2a->ht[i] = 0;
5106 }
5107 }
5108}
5109/* Insert a new record into the array. Return TRUE if successful.
5110** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005111int Symbol_insert(struct symbol *data, const char *key)
drh75897232000-05-29 14:26:00 +00005112{
5113 x2node *np;
drh01f75f22013-10-02 20:46:30 +00005114 unsigned h;
5115 unsigned ph;
drh75897232000-05-29 14:26:00 +00005116
5117 if( x2a==0 ) return 0;
5118 ph = strhash(key);
5119 h = ph & (x2a->size-1);
5120 np = x2a->ht[h];
5121 while( np ){
5122 if( strcmp(np->key,key)==0 ){
5123 /* An existing entry with the same key is found. */
5124 /* Fail because overwrite is not allows. */
5125 return 0;
5126 }
5127 np = np->next;
5128 }
5129 if( x2a->count>=x2a->size ){
5130 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005131 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005132 struct s_x2 array;
mistachkin8e189222015-04-19 21:43:16 +00005133 array.size = arrSize = x2a->size*2;
drh75897232000-05-29 14:26:00 +00005134 array.count = x2a->count;
mistachkin8e189222015-04-19 21:43:16 +00005135 array.tbl = (x2node*)calloc(arrSize, sizeof(x2node) + sizeof(x2node*));
drh75897232000-05-29 14:26:00 +00005136 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005137 array.ht = (x2node**)&(array.tbl[arrSize]);
5138 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005139 for(i=0; i<x2a->count; i++){
5140 x2node *oldnp, *newnp;
5141 oldnp = &(x2a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005142 h = strhash(oldnp->key) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005143 newnp = &(array.tbl[i]);
5144 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5145 newnp->next = array.ht[h];
5146 newnp->key = oldnp->key;
5147 newnp->data = oldnp->data;
5148 newnp->from = &(array.ht[h]);
5149 array.ht[h] = newnp;
5150 }
5151 free(x2a->tbl);
5152 *x2a = array;
5153 }
5154 /* Insert the new data */
5155 h = ph & (x2a->size-1);
5156 np = &(x2a->tbl[x2a->count++]);
5157 np->key = key;
5158 np->data = data;
5159 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
5160 np->next = x2a->ht[h];
5161 x2a->ht[h] = np;
5162 np->from = &(x2a->ht[h]);
5163 return 1;
5164}
5165
5166/* Return a pointer to data assigned to the given key. Return NULL
5167** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005168struct symbol *Symbol_find(const char *key)
drh75897232000-05-29 14:26:00 +00005169{
drh01f75f22013-10-02 20:46:30 +00005170 unsigned h;
drh75897232000-05-29 14:26:00 +00005171 x2node *np;
5172
5173 if( x2a==0 ) return 0;
5174 h = strhash(key) & (x2a->size-1);
5175 np = x2a->ht[h];
5176 while( np ){
5177 if( strcmp(np->key,key)==0 ) break;
5178 np = np->next;
5179 }
5180 return np ? np->data : 0;
5181}
5182
5183/* Return the n-th data. Return NULL if n is out of range. */
icculus9e44cf12010-02-14 17:14:22 +00005184struct symbol *Symbol_Nth(int n)
drh75897232000-05-29 14:26:00 +00005185{
5186 struct symbol *data;
5187 if( x2a && n>0 && n<=x2a->count ){
5188 data = x2a->tbl[n-1].data;
5189 }else{
5190 data = 0;
5191 }
5192 return data;
5193}
5194
5195/* Return the size of the array */
5196int Symbol_count()
5197{
5198 return x2a ? x2a->count : 0;
5199}
5200
5201/* Return an array of pointers to all data in the table.
5202** The array is obtained from malloc. Return NULL if memory allocation
5203** problems, or if the array is empty. */
5204struct symbol **Symbol_arrayof()
5205{
5206 struct symbol **array;
mistachkin8e189222015-04-19 21:43:16 +00005207 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005208 if( x2a==0 ) return 0;
mistachkin8e189222015-04-19 21:43:16 +00005209 arrSize = x2a->count;
5210 array = (struct symbol **)calloc(arrSize, sizeof(struct symbol *));
drh75897232000-05-29 14:26:00 +00005211 if( array ){
mistachkin8e189222015-04-19 21:43:16 +00005212 for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data;
drh75897232000-05-29 14:26:00 +00005213 }
5214 return array;
5215}
5216
5217/* Compare two configurations */
icculus9e44cf12010-02-14 17:14:22 +00005218int Configcmp(const char *_a,const char *_b)
drh75897232000-05-29 14:26:00 +00005219{
icculus9e44cf12010-02-14 17:14:22 +00005220 const struct config *a = (struct config *) _a;
5221 const struct config *b = (struct config *) _b;
drh75897232000-05-29 14:26:00 +00005222 int x;
5223 x = a->rp->index - b->rp->index;
5224 if( x==0 ) x = a->dot - b->dot;
5225 return x;
5226}
5227
5228/* Compare two states */
icculus9e44cf12010-02-14 17:14:22 +00005229PRIVATE int statecmp(struct config *a, struct config *b)
drh75897232000-05-29 14:26:00 +00005230{
5231 int rc;
5232 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
5233 rc = a->rp->index - b->rp->index;
5234 if( rc==0 ) rc = a->dot - b->dot;
5235 }
5236 if( rc==0 ){
5237 if( a ) rc = 1;
5238 if( b ) rc = -1;
5239 }
5240 return rc;
5241}
5242
5243/* Hash a state */
drh01f75f22013-10-02 20:46:30 +00005244PRIVATE unsigned statehash(struct config *a)
drh75897232000-05-29 14:26:00 +00005245{
drh01f75f22013-10-02 20:46:30 +00005246 unsigned h=0;
drh75897232000-05-29 14:26:00 +00005247 while( a ){
5248 h = h*571 + a->rp->index*37 + a->dot;
5249 a = a->bp;
5250 }
5251 return h;
5252}
5253
5254/* Allocate a new state structure */
5255struct state *State_new()
5256{
icculus9e44cf12010-02-14 17:14:22 +00005257 struct state *newstate;
5258 newstate = (struct state *)calloc(1, sizeof(struct state) );
5259 MemoryCheck(newstate);
5260 return newstate;
drh75897232000-05-29 14:26:00 +00005261}
5262
5263/* There is one instance of the following structure for each
5264** associative array of type "x3".
5265*/
5266struct s_x3 {
5267 int size; /* The number of available slots. */
5268 /* Must be a power of 2 greater than or */
5269 /* equal to 1 */
5270 int count; /* Number of currently slots filled */
5271 struct s_x3node *tbl; /* The data stored here */
5272 struct s_x3node **ht; /* Hash table for lookups */
5273};
5274
5275/* There is one instance of this structure for every data element
5276** in an associative array of type "x3".
5277*/
5278typedef struct s_x3node {
5279 struct state *data; /* The data */
5280 struct config *key; /* The key */
5281 struct s_x3node *next; /* Next entry with the same hash */
5282 struct s_x3node **from; /* Previous link */
5283} x3node;
5284
5285/* There is only one instance of the array, which is the following */
5286static struct s_x3 *x3a;
5287
5288/* Allocate a new associative array */
drh14d88552017-04-14 19:44:15 +00005289void State_init(void){
drh75897232000-05-29 14:26:00 +00005290 if( x3a ) return;
5291 x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
5292 if( x3a ){
5293 x3a->size = 128;
5294 x3a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005295 x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*));
drh75897232000-05-29 14:26:00 +00005296 if( x3a->tbl==0 ){
5297 free(x3a);
5298 x3a = 0;
5299 }else{
5300 int i;
5301 x3a->ht = (x3node**)&(x3a->tbl[128]);
5302 for(i=0; i<128; i++) x3a->ht[i] = 0;
5303 }
5304 }
5305}
5306/* Insert a new record into the array. Return TRUE if successful.
5307** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005308int State_insert(struct state *data, struct config *key)
drh75897232000-05-29 14:26:00 +00005309{
5310 x3node *np;
drh01f75f22013-10-02 20:46:30 +00005311 unsigned h;
5312 unsigned ph;
drh75897232000-05-29 14:26:00 +00005313
5314 if( x3a==0 ) return 0;
5315 ph = statehash(key);
5316 h = ph & (x3a->size-1);
5317 np = x3a->ht[h];
5318 while( np ){
5319 if( statecmp(np->key,key)==0 ){
5320 /* An existing entry with the same key is found. */
5321 /* Fail because overwrite is not allows. */
5322 return 0;
5323 }
5324 np = np->next;
5325 }
5326 if( x3a->count>=x3a->size ){
5327 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005328 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005329 struct s_x3 array;
mistachkin8e189222015-04-19 21:43:16 +00005330 array.size = arrSize = x3a->size*2;
drh75897232000-05-29 14:26:00 +00005331 array.count = x3a->count;
mistachkin8e189222015-04-19 21:43:16 +00005332 array.tbl = (x3node*)calloc(arrSize, sizeof(x3node) + sizeof(x3node*));
drh75897232000-05-29 14:26:00 +00005333 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005334 array.ht = (x3node**)&(array.tbl[arrSize]);
5335 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005336 for(i=0; i<x3a->count; i++){
5337 x3node *oldnp, *newnp;
5338 oldnp = &(x3a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005339 h = statehash(oldnp->key) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005340 newnp = &(array.tbl[i]);
5341 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5342 newnp->next = array.ht[h];
5343 newnp->key = oldnp->key;
5344 newnp->data = oldnp->data;
5345 newnp->from = &(array.ht[h]);
5346 array.ht[h] = newnp;
5347 }
5348 free(x3a->tbl);
5349 *x3a = array;
5350 }
5351 /* Insert the new data */
5352 h = ph & (x3a->size-1);
5353 np = &(x3a->tbl[x3a->count++]);
5354 np->key = key;
5355 np->data = data;
5356 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
5357 np->next = x3a->ht[h];
5358 x3a->ht[h] = np;
5359 np->from = &(x3a->ht[h]);
5360 return 1;
5361}
5362
5363/* Return a pointer to data assigned to the given key. Return NULL
5364** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005365struct state *State_find(struct config *key)
drh75897232000-05-29 14:26:00 +00005366{
drh01f75f22013-10-02 20:46:30 +00005367 unsigned h;
drh75897232000-05-29 14:26:00 +00005368 x3node *np;
5369
5370 if( x3a==0 ) return 0;
5371 h = statehash(key) & (x3a->size-1);
5372 np = x3a->ht[h];
5373 while( np ){
5374 if( statecmp(np->key,key)==0 ) break;
5375 np = np->next;
5376 }
5377 return np ? np->data : 0;
5378}
5379
5380/* Return an array of pointers to all data in the table.
5381** The array is obtained from malloc. Return NULL if memory allocation
5382** problems, or if the array is empty. */
drh14d88552017-04-14 19:44:15 +00005383struct state **State_arrayof(void)
drh75897232000-05-29 14:26:00 +00005384{
5385 struct state **array;
mistachkin8e189222015-04-19 21:43:16 +00005386 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005387 if( x3a==0 ) return 0;
mistachkin8e189222015-04-19 21:43:16 +00005388 arrSize = x3a->count;
5389 array = (struct state **)calloc(arrSize, sizeof(struct state *));
drh75897232000-05-29 14:26:00 +00005390 if( array ){
mistachkin8e189222015-04-19 21:43:16 +00005391 for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data;
drh75897232000-05-29 14:26:00 +00005392 }
5393 return array;
5394}
5395
5396/* Hash a configuration */
drh01f75f22013-10-02 20:46:30 +00005397PRIVATE unsigned confighash(struct config *a)
drh75897232000-05-29 14:26:00 +00005398{
drh01f75f22013-10-02 20:46:30 +00005399 unsigned h=0;
drh75897232000-05-29 14:26:00 +00005400 h = h*571 + a->rp->index*37 + a->dot;
5401 return h;
5402}
5403
5404/* There is one instance of the following structure for each
5405** associative array of type "x4".
5406*/
5407struct s_x4 {
5408 int size; /* The number of available slots. */
5409 /* Must be a power of 2 greater than or */
5410 /* equal to 1 */
5411 int count; /* Number of currently slots filled */
5412 struct s_x4node *tbl; /* The data stored here */
5413 struct s_x4node **ht; /* Hash table for lookups */
5414};
5415
5416/* There is one instance of this structure for every data element
5417** in an associative array of type "x4".
5418*/
5419typedef struct s_x4node {
5420 struct config *data; /* The data */
5421 struct s_x4node *next; /* Next entry with the same hash */
5422 struct s_x4node **from; /* Previous link */
5423} x4node;
5424
5425/* There is only one instance of the array, which is the following */
5426static struct s_x4 *x4a;
5427
5428/* Allocate a new associative array */
drh14d88552017-04-14 19:44:15 +00005429void Configtable_init(void){
drh75897232000-05-29 14:26:00 +00005430 if( x4a ) return;
5431 x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
5432 if( x4a ){
5433 x4a->size = 64;
5434 x4a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005435 x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*));
drh75897232000-05-29 14:26:00 +00005436 if( x4a->tbl==0 ){
5437 free(x4a);
5438 x4a = 0;
5439 }else{
5440 int i;
5441 x4a->ht = (x4node**)&(x4a->tbl[64]);
5442 for(i=0; i<64; i++) x4a->ht[i] = 0;
5443 }
5444 }
5445}
5446/* Insert a new record into the array. Return TRUE if successful.
5447** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005448int Configtable_insert(struct config *data)
drh75897232000-05-29 14:26:00 +00005449{
5450 x4node *np;
drh01f75f22013-10-02 20:46:30 +00005451 unsigned h;
5452 unsigned ph;
drh75897232000-05-29 14:26:00 +00005453
5454 if( x4a==0 ) return 0;
5455 ph = confighash(data);
5456 h = ph & (x4a->size-1);
5457 np = x4a->ht[h];
5458 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00005459 if( Configcmp((const char *) np->data,(const char *) data)==0 ){
drh75897232000-05-29 14:26:00 +00005460 /* An existing entry with the same key is found. */
5461 /* Fail because overwrite is not allows. */
5462 return 0;
5463 }
5464 np = np->next;
5465 }
5466 if( x4a->count>=x4a->size ){
5467 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005468 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005469 struct s_x4 array;
mistachkin8e189222015-04-19 21:43:16 +00005470 array.size = arrSize = x4a->size*2;
drh75897232000-05-29 14:26:00 +00005471 array.count = x4a->count;
mistachkin8e189222015-04-19 21:43:16 +00005472 array.tbl = (x4node*)calloc(arrSize, sizeof(x4node) + sizeof(x4node*));
drh75897232000-05-29 14:26:00 +00005473 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005474 array.ht = (x4node**)&(array.tbl[arrSize]);
5475 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005476 for(i=0; i<x4a->count; i++){
5477 x4node *oldnp, *newnp;
5478 oldnp = &(x4a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005479 h = confighash(oldnp->data) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005480 newnp = &(array.tbl[i]);
5481 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5482 newnp->next = array.ht[h];
5483 newnp->data = oldnp->data;
5484 newnp->from = &(array.ht[h]);
5485 array.ht[h] = newnp;
5486 }
5487 free(x4a->tbl);
5488 *x4a = array;
5489 }
5490 /* Insert the new data */
5491 h = ph & (x4a->size-1);
5492 np = &(x4a->tbl[x4a->count++]);
5493 np->data = data;
5494 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
5495 np->next = x4a->ht[h];
5496 x4a->ht[h] = np;
5497 np->from = &(x4a->ht[h]);
5498 return 1;
5499}
5500
5501/* Return a pointer to data assigned to the given key. Return NULL
5502** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005503struct config *Configtable_find(struct config *key)
drh75897232000-05-29 14:26:00 +00005504{
5505 int h;
5506 x4node *np;
5507
5508 if( x4a==0 ) return 0;
5509 h = confighash(key) & (x4a->size-1);
5510 np = x4a->ht[h];
5511 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00005512 if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
drh75897232000-05-29 14:26:00 +00005513 np = np->next;
5514 }
5515 return np ? np->data : 0;
5516}
5517
5518/* Remove all data from the table. Pass each data to the function "f"
5519** as it is removed. ("f" may be null to avoid this step.) */
icculus9e44cf12010-02-14 17:14:22 +00005520void Configtable_clear(int(*f)(struct config *))
drh75897232000-05-29 14:26:00 +00005521{
5522 int i;
5523 if( x4a==0 || x4a->count==0 ) return;
5524 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
5525 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
5526 x4a->count = 0;
5527 return;
5528}