blob: acc5450c993d9481a387416f24bd6d43e1126a8e [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 */
387 struct symbol **symbols; /* Sorted array of pointers to symbols */
388 int errorcnt; /* Number of errors */
389 struct symbol *errsym; /* The error symbol */
drhe09daa92006-06-10 13:29:31 +0000390 struct symbol *wildcard; /* Token that matches anything */
drh75897232000-05-29 14:26:00 +0000391 char *name; /* Name of the generated parser */
392 char *arg; /* Declaration of the 3th argument to parser */
393 char *tokentype; /* Type of terminal symbols in the parser stack */
drh960e8c62001-04-03 16:53:21 +0000394 char *vartype; /* The default type of non-terminal symbols */
drh75897232000-05-29 14:26:00 +0000395 char *start; /* Name of the start symbol for the grammar */
396 char *stacksize; /* Size of the parser stack */
397 char *include; /* Code to put at the start of the C file */
drh75897232000-05-29 14:26:00 +0000398 char *error; /* Code to execute when an error is seen */
drh75897232000-05-29 14:26:00 +0000399 char *overflow; /* Code to execute on a stack overflow */
drh75897232000-05-29 14:26:00 +0000400 char *failure; /* Code to execute on parser failure */
drh75897232000-05-29 14:26:00 +0000401 char *accept; /* Code to execute when the parser excepts */
drh75897232000-05-29 14:26:00 +0000402 char *extracode; /* Code appended to the generated file */
drh75897232000-05-29 14:26:00 +0000403 char *tokendest; /* Code to execute to destroy token data */
drh960e8c62001-04-03 16:53:21 +0000404 char *vardest; /* Code for the default non-terminal destructor */
drh75897232000-05-29 14:26:00 +0000405 char *filename; /* Name of the input file */
406 char *outname; /* Name of the current output file */
407 char *tokenprefix; /* A prefix added to token names in the .h file */
408 int nconflict; /* Number of parsing conflicts */
drhc75e0162015-09-07 02:23:02 +0000409 int nactiontab; /* Number of entries in the yy_action[] table */
410 int tablesize; /* Total table size of all tables in bytes */
drh75897232000-05-29 14:26:00 +0000411 int basisflag; /* Print only basis configurations */
drh34ff57b2008-07-14 12:27:51 +0000412 int has_fallback; /* True if any %fallback is seen in the grammar */
shane58543932008-12-10 20:10:04 +0000413 int nolinenosflag; /* True if #line statements should not be printed */
drh75897232000-05-29 14:26:00 +0000414 char *argv0; /* Name of the program */
415};
416
417#define MemoryCheck(X) if((X)==0){ \
418 extern void memory_error(); \
419 memory_error(); \
420}
421
422/**************** From the file "table.h" *********************************/
423/*
424** All code in this file has been automatically generated
425** from a specification in the file
426** "table.q"
427** by the associative array code building program "aagen".
428** Do not edit this file! Instead, edit the specification
429** file, then rerun aagen.
430*/
431/*
432** Code for processing tables in the LEMON parser generator.
433*/
drh75897232000-05-29 14:26:00 +0000434/* Routines for handling a strings */
435
icculus9e44cf12010-02-14 17:14:22 +0000436const char *Strsafe(const char *);
drh75897232000-05-29 14:26:00 +0000437
icculus9e44cf12010-02-14 17:14:22 +0000438void Strsafe_init(void);
439int Strsafe_insert(const char *);
440const char *Strsafe_find(const char *);
drh75897232000-05-29 14:26:00 +0000441
442/* Routines for handling symbols of the grammar */
443
icculus9e44cf12010-02-14 17:14:22 +0000444struct symbol *Symbol_new(const char *);
445int Symbolcmpp(const void *, const void *);
446void Symbol_init(void);
447int Symbol_insert(struct symbol *, const char *);
448struct symbol *Symbol_find(const char *);
449struct symbol *Symbol_Nth(int);
450int Symbol_count(void);
451struct symbol **Symbol_arrayof(void);
drh75897232000-05-29 14:26:00 +0000452
453/* Routines to manage the state table */
454
icculus9e44cf12010-02-14 17:14:22 +0000455int Configcmp(const char *, const char *);
456struct state *State_new(void);
457void State_init(void);
458int State_insert(struct state *, struct config *);
459struct state *State_find(struct config *);
drh14d88552017-04-14 19:44:15 +0000460struct state **State_arrayof(void);
drh75897232000-05-29 14:26:00 +0000461
462/* Routines used for efficiency in Configlist_add */
463
icculus9e44cf12010-02-14 17:14:22 +0000464void Configtable_init(void);
465int Configtable_insert(struct config *);
466struct config *Configtable_find(struct config *);
467void Configtable_clear(int(*)(struct config *));
468
drh75897232000-05-29 14:26:00 +0000469/****************** From the file "action.c" *******************************/
470/*
471** Routines processing parser actions in the LEMON parser generator.
472*/
473
474/* Allocate a new parser action */
drhe9278182007-07-18 18:16:29 +0000475static struct action *Action_new(void){
drh75897232000-05-29 14:26:00 +0000476 static struct action *freelist = 0;
icculus9e44cf12010-02-14 17:14:22 +0000477 struct action *newaction;
drh75897232000-05-29 14:26:00 +0000478
479 if( freelist==0 ){
480 int i;
481 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +0000482 freelist = (struct action *)calloc(amt, sizeof(struct action));
drh75897232000-05-29 14:26:00 +0000483 if( freelist==0 ){
484 fprintf(stderr,"Unable to allocate memory for a new parser action.");
485 exit(1);
486 }
487 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
488 freelist[amt-1].next = 0;
489 }
icculus9e44cf12010-02-14 17:14:22 +0000490 newaction = freelist;
drh75897232000-05-29 14:26:00 +0000491 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +0000492 return newaction;
drh75897232000-05-29 14:26:00 +0000493}
494
drhe9278182007-07-18 18:16:29 +0000495/* Compare two actions for sorting purposes. Return negative, zero, or
496** positive if the first action is less than, equal to, or greater than
497** the first
498*/
499static int actioncmp(
500 struct action *ap1,
501 struct action *ap2
502){
drh75897232000-05-29 14:26:00 +0000503 int rc;
504 rc = ap1->sp->index - ap2->sp->index;
drh75897232000-05-29 14:26:00 +0000505 if( rc==0 ){
drh9892c5d2007-12-21 00:02:11 +0000506 rc = (int)ap1->type - (int)ap2->type;
507 }
drh3bd48ab2015-09-07 18:23:37 +0000508 if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){
drh75897232000-05-29 14:26:00 +0000509 rc = ap1->x.rp->index - ap2->x.rp->index;
510 }
drhe594bc32009-11-03 13:02:25 +0000511 if( rc==0 ){
icculus7b429aa2010-03-03 17:09:01 +0000512 rc = (int) (ap2 - ap1);
drhe594bc32009-11-03 13:02:25 +0000513 }
drh75897232000-05-29 14:26:00 +0000514 return rc;
515}
516
517/* Sort parser actions */
drhe9278182007-07-18 18:16:29 +0000518static struct action *Action_sort(
519 struct action *ap
520){
521 ap = (struct action *)msort((char *)ap,(char **)&ap->next,
522 (int(*)(const char*,const char*))actioncmp);
drh75897232000-05-29 14:26:00 +0000523 return ap;
524}
525
icculus9e44cf12010-02-14 17:14:22 +0000526void Action_add(
527 struct action **app,
528 enum e_action type,
529 struct symbol *sp,
530 char *arg
531){
532 struct action *newaction;
533 newaction = Action_new();
534 newaction->next = *app;
535 *app = newaction;
536 newaction->type = type;
537 newaction->sp = sp;
drhc173ad82016-05-23 16:15:02 +0000538 newaction->spOpt = 0;
drh75897232000-05-29 14:26:00 +0000539 if( type==SHIFT ){
icculus9e44cf12010-02-14 17:14:22 +0000540 newaction->x.stp = (struct state *)arg;
drh75897232000-05-29 14:26:00 +0000541 }else{
icculus9e44cf12010-02-14 17:14:22 +0000542 newaction->x.rp = (struct rule *)arg;
drh75897232000-05-29 14:26:00 +0000543 }
544}
drh8b582012003-10-21 13:16:03 +0000545/********************** New code to implement the "acttab" module ***********/
546/*
547** This module implements routines use to construct the yy_action[] table.
548*/
549
550/*
551** The state of the yy_action table under construction is an instance of
drh8dc3e8f2010-01-07 03:53:03 +0000552** the following structure.
553**
554** The yy_action table maps the pair (state_number, lookahead) into an
555** action_number. The table is an array of integers pairs. The state_number
556** determines an initial offset into the yy_action array. The lookahead
557** value is then added to this initial offset to get an index X into the
558** yy_action array. If the aAction[X].lookahead equals the value of the
559** of the lookahead input, then the value of the action_number output is
560** aAction[X].action. If the lookaheads do not match then the
561** default action for the state_number is returned.
562**
563** All actions associated with a single state_number are first entered
drh06f60d82017-04-14 19:46:12 +0000564** into aLookahead[] using multiple calls to acttab_action(). Then the
565** actions for that single state_number are placed into the aAction[]
drh8dc3e8f2010-01-07 03:53:03 +0000566** array with a single call to acttab_insert(). The acttab_insert() call
567** also resets the aLookahead[] array in preparation for the next
568** state number.
drh8b582012003-10-21 13:16:03 +0000569*/
icculus9e44cf12010-02-14 17:14:22 +0000570struct lookahead_action {
571 int lookahead; /* Value of the lookahead token */
572 int action; /* Action to take on the given lookahead */
573};
drh8b582012003-10-21 13:16:03 +0000574typedef struct acttab acttab;
575struct acttab {
576 int nAction; /* Number of used slots in aAction[] */
577 int nActionAlloc; /* Slots allocated for aAction[] */
icculus9e44cf12010-02-14 17:14:22 +0000578 struct lookahead_action
579 *aAction, /* The yy_action[] table under construction */
drh8b582012003-10-21 13:16:03 +0000580 *aLookahead; /* A single new transaction set */
581 int mnLookahead; /* Minimum aLookahead[].lookahead */
582 int mnAction; /* Action associated with mnLookahead */
583 int mxLookahead; /* Maximum aLookahead[].lookahead */
584 int nLookahead; /* Used slots in aLookahead[] */
585 int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
586};
587
588/* Return the number of entries in the yy_action table */
589#define acttab_size(X) ((X)->nAction)
590
591/* The value for the N-th entry in yy_action */
592#define acttab_yyaction(X,N) ((X)->aAction[N].action)
593
594/* The value for the N-th entry in yy_lookahead */
595#define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
596
597/* Free all memory associated with the given acttab */
598void acttab_free(acttab *p){
599 free( p->aAction );
600 free( p->aLookahead );
601 free( p );
602}
603
604/* Allocate a new acttab structure */
605acttab *acttab_alloc(void){
icculus9e44cf12010-02-14 17:14:22 +0000606 acttab *p = (acttab *) calloc( 1, sizeof(*p) );
drh8b582012003-10-21 13:16:03 +0000607 if( p==0 ){
608 fprintf(stderr,"Unable to allocate memory for a new acttab.");
609 exit(1);
610 }
611 memset(p, 0, sizeof(*p));
612 return p;
613}
614
drh06f60d82017-04-14 19:46:12 +0000615/* Add a new action to the current transaction set.
drh8dc3e8f2010-01-07 03:53:03 +0000616**
617** This routine is called once for each lookahead for a particular
618** state.
drh8b582012003-10-21 13:16:03 +0000619*/
620void acttab_action(acttab *p, int lookahead, int action){
621 if( p->nLookahead>=p->nLookaheadAlloc ){
622 p->nLookaheadAlloc += 25;
icculus9e44cf12010-02-14 17:14:22 +0000623 p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
drh8b582012003-10-21 13:16:03 +0000624 sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
625 if( p->aLookahead==0 ){
626 fprintf(stderr,"malloc failed\n");
627 exit(1);
628 }
629 }
630 if( p->nLookahead==0 ){
631 p->mxLookahead = lookahead;
632 p->mnLookahead = lookahead;
633 p->mnAction = action;
634 }else{
635 if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
636 if( p->mnLookahead>lookahead ){
637 p->mnLookahead = lookahead;
638 p->mnAction = action;
639 }
640 }
641 p->aLookahead[p->nLookahead].lookahead = lookahead;
642 p->aLookahead[p->nLookahead].action = action;
643 p->nLookahead++;
644}
645
646/*
647** Add the transaction set built up with prior calls to acttab_action()
648** into the current action table. Then reset the transaction set back
649** to an empty set in preparation for a new round of acttab_action() calls.
650**
651** Return the offset into the action table of the new transaction.
652*/
653int acttab_insert(acttab *p){
654 int i, j, k, n;
655 assert( p->nLookahead>0 );
656
657 /* Make sure we have enough space to hold the expanded action table
658 ** in the worst case. The worst case occurs if the transaction set
659 ** must be appended to the current action table
660 */
drh784d86f2004-02-19 18:41:53 +0000661 n = p->mxLookahead + 1;
drh8dc3e8f2010-01-07 03:53:03 +0000662 if( p->nAction + n >= p->nActionAlloc ){
drhfdbf9282003-10-21 16:34:41 +0000663 int oldAlloc = p->nActionAlloc;
drh8b582012003-10-21 13:16:03 +0000664 p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
icculus9e44cf12010-02-14 17:14:22 +0000665 p->aAction = (struct lookahead_action *) realloc( p->aAction,
drh8b582012003-10-21 13:16:03 +0000666 sizeof(p->aAction[0])*p->nActionAlloc);
667 if( p->aAction==0 ){
668 fprintf(stderr,"malloc failed\n");
669 exit(1);
670 }
drhfdbf9282003-10-21 16:34:41 +0000671 for(i=oldAlloc; i<p->nActionAlloc; i++){
drh8b582012003-10-21 13:16:03 +0000672 p->aAction[i].lookahead = -1;
673 p->aAction[i].action = -1;
674 }
675 }
676
drh06f60d82017-04-14 19:46:12 +0000677 /* Scan the existing action table looking for an offset that is a
drh8dc3e8f2010-01-07 03:53:03 +0000678 ** duplicate of the current transaction set. Fall out of the loop
679 ** if and when the duplicate is found.
drh8b582012003-10-21 13:16:03 +0000680 **
681 ** i is the index in p->aAction[] where p->mnLookahead is inserted.
682 */
drh8dc3e8f2010-01-07 03:53:03 +0000683 for(i=p->nAction-1; i>=0; i--){
drhf16371d2009-11-03 19:18:31 +0000684 if( p->aAction[i].lookahead==p->mnLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000685 /* All lookaheads and actions in the aLookahead[] transaction
686 ** must match against the candidate aAction[i] entry. */
drh8b582012003-10-21 13:16:03 +0000687 if( p->aAction[i].action!=p->mnAction ) continue;
688 for(j=0; j<p->nLookahead; j++){
689 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
690 if( k<0 || k>=p->nAction ) break;
691 if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
692 if( p->aLookahead[j].action!=p->aAction[k].action ) break;
693 }
694 if( j<p->nLookahead ) continue;
drh8dc3e8f2010-01-07 03:53:03 +0000695
696 /* No possible lookahead value that is not in the aLookahead[]
697 ** transaction is allowed to match aAction[i] */
drh8b582012003-10-21 13:16:03 +0000698 n = 0;
699 for(j=0; j<p->nAction; j++){
drhfdbf9282003-10-21 16:34:41 +0000700 if( p->aAction[j].lookahead<0 ) continue;
701 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
drh8b582012003-10-21 13:16:03 +0000702 }
drhfdbf9282003-10-21 16:34:41 +0000703 if( n==p->nLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000704 break; /* An exact match is found at offset i */
drhfdbf9282003-10-21 16:34:41 +0000705 }
drh8b582012003-10-21 13:16:03 +0000706 }
707 }
drh8dc3e8f2010-01-07 03:53:03 +0000708
709 /* If no existing offsets exactly match the current transaction, find an
710 ** an empty offset in the aAction[] table in which we can add the
711 ** aLookahead[] transaction.
712 */
drhf16371d2009-11-03 19:18:31 +0000713 if( i<0 ){
drh8dc3e8f2010-01-07 03:53:03 +0000714 /* Look for holes in the aAction[] table that fit the current
715 ** aLookahead[] transaction. Leave i set to the offset of the hole.
716 ** If no holes are found, i is left at p->nAction, which means the
717 ** transaction will be appended. */
718 for(i=0; i<p->nActionAlloc - p->mxLookahead; i++){
drhf16371d2009-11-03 19:18:31 +0000719 if( p->aAction[i].lookahead<0 ){
720 for(j=0; j<p->nLookahead; j++){
721 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
722 if( k<0 ) break;
723 if( p->aAction[k].lookahead>=0 ) break;
724 }
725 if( j<p->nLookahead ) continue;
726 for(j=0; j<p->nAction; j++){
727 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
728 }
729 if( j==p->nAction ){
730 break; /* Fits in empty slots */
731 }
732 }
733 }
734 }
drh8b582012003-10-21 13:16:03 +0000735 /* Insert transaction set at index i. */
736 for(j=0; j<p->nLookahead; j++){
737 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
738 p->aAction[k] = p->aLookahead[j];
739 if( k>=p->nAction ) p->nAction = k+1;
740 }
741 p->nLookahead = 0;
742
743 /* Return the offset that is added to the lookahead in order to get the
744 ** index into yy_action of the action */
745 return i - p->mnLookahead;
746}
747
drh75897232000-05-29 14:26:00 +0000748/********************** From the file "build.c" *****************************/
749/*
750** Routines to construction the finite state machine for the LEMON
751** parser generator.
752*/
753
754/* Find a precedence symbol of every rule in the grammar.
drh06f60d82017-04-14 19:46:12 +0000755**
drh75897232000-05-29 14:26:00 +0000756** Those rules which have a precedence symbol coded in the input
757** grammar using the "[symbol]" construct will already have the
758** rp->precsym field filled. Other rules take as their precedence
759** symbol the first RHS symbol with a defined precedence. If there
760** are not RHS symbols with a defined precedence, the precedence
761** symbol field is left blank.
762*/
icculus9e44cf12010-02-14 17:14:22 +0000763void FindRulePrecedences(struct lemon *xp)
drh75897232000-05-29 14:26:00 +0000764{
765 struct rule *rp;
766 for(rp=xp->rule; rp; rp=rp->next){
767 if( rp->precsym==0 ){
drhfd405312005-11-06 04:06:59 +0000768 int i, j;
769 for(i=0; i<rp->nrhs && rp->precsym==0; i++){
770 struct symbol *sp = rp->rhs[i];
771 if( sp->type==MULTITERMINAL ){
772 for(j=0; j<sp->nsubsym; j++){
773 if( sp->subsym[j]->prec>=0 ){
774 rp->precsym = sp->subsym[j];
775 break;
776 }
777 }
778 }else if( sp->prec>=0 ){
drh75897232000-05-29 14:26:00 +0000779 rp->precsym = rp->rhs[i];
drhf2f105d2012-08-20 15:53:54 +0000780 }
drh75897232000-05-29 14:26:00 +0000781 }
782 }
783 }
784 return;
785}
786
787/* Find all nonterminals which will generate the empty string.
788** Then go back and compute the first sets of every nonterminal.
789** The first set is the set of all terminal symbols which can begin
790** a string generated by that nonterminal.
791*/
icculus9e44cf12010-02-14 17:14:22 +0000792void FindFirstSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000793{
drhfd405312005-11-06 04:06:59 +0000794 int i, j;
drh75897232000-05-29 14:26:00 +0000795 struct rule *rp;
796 int progress;
797
798 for(i=0; i<lemp->nsymbol; i++){
drhaa9f1122007-08-23 02:50:56 +0000799 lemp->symbols[i]->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +0000800 }
801 for(i=lemp->nterminal; i<lemp->nsymbol; i++){
802 lemp->symbols[i]->firstset = SetNew();
803 }
804
805 /* First compute all lambdas */
806 do{
807 progress = 0;
808 for(rp=lemp->rule; rp; rp=rp->next){
809 if( rp->lhs->lambda ) continue;
810 for(i=0; i<rp->nrhs; i++){
drh7dd1ac62012-01-07 15:17:18 +0000811 struct symbol *sp = rp->rhs[i];
812 assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE );
813 if( sp->lambda==LEMON_FALSE ) break;
drh75897232000-05-29 14:26:00 +0000814 }
815 if( i==rp->nrhs ){
drhaa9f1122007-08-23 02:50:56 +0000816 rp->lhs->lambda = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +0000817 progress = 1;
818 }
819 }
820 }while( progress );
821
822 /* Now compute all first sets */
823 do{
824 struct symbol *s1, *s2;
825 progress = 0;
826 for(rp=lemp->rule; rp; rp=rp->next){
827 s1 = rp->lhs;
828 for(i=0; i<rp->nrhs; i++){
829 s2 = rp->rhs[i];
830 if( s2->type==TERMINAL ){
831 progress += SetAdd(s1->firstset,s2->index);
832 break;
drhfd405312005-11-06 04:06:59 +0000833 }else if( s2->type==MULTITERMINAL ){
834 for(j=0; j<s2->nsubsym; j++){
835 progress += SetAdd(s1->firstset,s2->subsym[j]->index);
836 }
837 break;
drhf2f105d2012-08-20 15:53:54 +0000838 }else if( s1==s2 ){
drhaa9f1122007-08-23 02:50:56 +0000839 if( s1->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000840 }else{
drh75897232000-05-29 14:26:00 +0000841 progress += SetUnion(s1->firstset,s2->firstset);
drhaa9f1122007-08-23 02:50:56 +0000842 if( s2->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000843 }
drh75897232000-05-29 14:26:00 +0000844 }
845 }
846 }while( progress );
847 return;
848}
849
850/* Compute all LR(0) states for the grammar. Links
851** are added to between some states so that the LR(1) follow sets
852** can be computed later.
853*/
icculus9e44cf12010-02-14 17:14:22 +0000854PRIVATE struct state *getstate(struct lemon *); /* forward reference */
855void FindStates(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000856{
857 struct symbol *sp;
858 struct rule *rp;
859
860 Configlist_init();
861
862 /* Find the start symbol */
863 if( lemp->start ){
864 sp = Symbol_find(lemp->start);
865 if( sp==0 ){
866 ErrorMsg(lemp->filename,0,
867"The specified start symbol \"%s\" is not \
868in a nonterminal of the grammar. \"%s\" will be used as the start \
drh4ef07702016-03-16 19:45:54 +0000869symbol instead.",lemp->start,lemp->startRule->lhs->name);
drh75897232000-05-29 14:26:00 +0000870 lemp->errorcnt++;
drh4ef07702016-03-16 19:45:54 +0000871 sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +0000872 }
873 }else{
drh4ef07702016-03-16 19:45:54 +0000874 sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +0000875 }
876
877 /* Make sure the start symbol doesn't occur on the right-hand side of
878 ** any rule. Report an error if it does. (YACC would generate a new
879 ** start symbol in this case.) */
880 for(rp=lemp->rule; rp; rp=rp->next){
881 int i;
882 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +0000883 if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */
drh75897232000-05-29 14:26:00 +0000884 ErrorMsg(lemp->filename,0,
885"The start symbol \"%s\" occurs on the \
886right-hand side of a rule. This will result in a parser which \
887does not work properly.",sp->name);
888 lemp->errorcnt++;
889 }
890 }
891 }
892
893 /* The basis configuration set for the first state
894 ** is all rules which have the start symbol as their
895 ** left-hand side */
896 for(rp=sp->rule; rp; rp=rp->nextlhs){
897 struct config *newcfp;
drhb4960992007-10-05 16:16:36 +0000898 rp->lhsStart = 1;
drh75897232000-05-29 14:26:00 +0000899 newcfp = Configlist_addbasis(rp,0);
900 SetAdd(newcfp->fws,0);
901 }
902
903 /* Compute the first state. All other states will be
904 ** computed automatically during the computation of the first one.
905 ** The returned pointer to the first state is not used. */
906 (void)getstate(lemp);
907 return;
908}
909
910/* Return a pointer to a state which is described by the configuration
911** list which has been built from calls to Configlist_add.
912*/
icculus9e44cf12010-02-14 17:14:22 +0000913PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
914PRIVATE struct state *getstate(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000915{
916 struct config *cfp, *bp;
917 struct state *stp;
918
919 /* Extract the sorted basis of the new state. The basis was constructed
920 ** by prior calls to "Configlist_addbasis()". */
921 Configlist_sortbasis();
922 bp = Configlist_basis();
923
924 /* Get a state with the same basis */
925 stp = State_find(bp);
926 if( stp ){
927 /* A state with the same basis already exists! Copy all the follow-set
928 ** propagation links from the state under construction into the
929 ** preexisting state, then return a pointer to the preexisting state */
930 struct config *x, *y;
931 for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
932 Plink_copy(&y->bplp,x->bplp);
933 Plink_delete(x->fplp);
934 x->fplp = x->bplp = 0;
935 }
936 cfp = Configlist_return();
937 Configlist_eat(cfp);
938 }else{
939 /* This really is a new state. Construct all the details */
940 Configlist_closure(lemp); /* Compute the configuration closure */
941 Configlist_sort(); /* Sort the configuration closure */
942 cfp = Configlist_return(); /* Get a pointer to the config list */
943 stp = State_new(); /* A new state structure */
944 MemoryCheck(stp);
945 stp->bp = bp; /* Remember the configuration basis */
946 stp->cfp = cfp; /* Remember the configuration closure */
drhada354d2005-11-05 15:03:59 +0000947 stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
drh75897232000-05-29 14:26:00 +0000948 stp->ap = 0; /* No actions, yet. */
949 State_insert(stp,stp->bp); /* Add to the state table */
950 buildshifts(lemp,stp); /* Recursively compute successor states */
951 }
952 return stp;
953}
954
drhfd405312005-11-06 04:06:59 +0000955/*
956** Return true if two symbols are the same.
957*/
icculus9e44cf12010-02-14 17:14:22 +0000958int same_symbol(struct symbol *a, struct symbol *b)
drhfd405312005-11-06 04:06:59 +0000959{
960 int i;
961 if( a==b ) return 1;
962 if( a->type!=MULTITERMINAL ) return 0;
963 if( b->type!=MULTITERMINAL ) return 0;
964 if( a->nsubsym!=b->nsubsym ) return 0;
965 for(i=0; i<a->nsubsym; i++){
966 if( a->subsym[i]!=b->subsym[i] ) return 0;
967 }
968 return 1;
969}
970
drh75897232000-05-29 14:26:00 +0000971/* Construct all successor states to the given state. A "successor"
972** state is any state which can be reached by a shift action.
973*/
icculus9e44cf12010-02-14 17:14:22 +0000974PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
drh75897232000-05-29 14:26:00 +0000975{
976 struct config *cfp; /* For looping thru the config closure of "stp" */
977 struct config *bcfp; /* For the inner loop on config closure of "stp" */
icculus9e44cf12010-02-14 17:14:22 +0000978 struct config *newcfg; /* */
drh75897232000-05-29 14:26:00 +0000979 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
980 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
981 struct state *newstp; /* A pointer to a successor state */
982
983 /* Each configuration becomes complete after it contibutes to a successor
984 ** state. Initially, all configurations are incomplete */
985 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
986
987 /* Loop through all configurations of the state "stp" */
988 for(cfp=stp->cfp; cfp; cfp=cfp->next){
989 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
990 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
991 Configlist_reset(); /* Reset the new config set */
992 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
993
994 /* For every configuration in the state "stp" which has the symbol "sp"
995 ** following its dot, add the same configuration to the basis set under
996 ** construction but with the dot shifted one symbol to the right. */
997 for(bcfp=cfp; bcfp; bcfp=bcfp->next){
998 if( bcfp->status==COMPLETE ) continue; /* Already used */
999 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
1000 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
drhfd405312005-11-06 04:06:59 +00001001 if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
drh75897232000-05-29 14:26:00 +00001002 bcfp->status = COMPLETE; /* Mark this config as used */
icculus9e44cf12010-02-14 17:14:22 +00001003 newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
1004 Plink_add(&newcfg->bplp,bcfp);
drh75897232000-05-29 14:26:00 +00001005 }
1006
1007 /* Get a pointer to the state described by the basis configuration set
1008 ** constructed in the preceding loop */
1009 newstp = getstate(lemp);
1010
1011 /* The state "newstp" is reached from the state "stp" by a shift action
1012 ** on the symbol "sp" */
drhfd405312005-11-06 04:06:59 +00001013 if( sp->type==MULTITERMINAL ){
1014 int i;
1015 for(i=0; i<sp->nsubsym; i++){
1016 Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
1017 }
1018 }else{
1019 Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
1020 }
drh75897232000-05-29 14:26:00 +00001021 }
1022}
1023
1024/*
1025** Construct the propagation links
1026*/
icculus9e44cf12010-02-14 17:14:22 +00001027void FindLinks(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001028{
1029 int i;
1030 struct config *cfp, *other;
1031 struct state *stp;
1032 struct plink *plp;
1033
1034 /* Housekeeping detail:
1035 ** Add to every propagate link a pointer back to the state to
1036 ** which the link is attached. */
1037 for(i=0; i<lemp->nstate; i++){
1038 stp = lemp->sorted[i];
1039 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1040 cfp->stp = stp;
1041 }
1042 }
1043
1044 /* Convert all backlinks into forward links. Only the forward
1045 ** links are used in the follow-set computation. */
1046 for(i=0; i<lemp->nstate; i++){
1047 stp = lemp->sorted[i];
1048 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1049 for(plp=cfp->bplp; plp; plp=plp->next){
1050 other = plp->cfp;
1051 Plink_add(&other->fplp,cfp);
1052 }
1053 }
1054 }
1055}
1056
1057/* Compute all followsets.
1058**
1059** A followset is the set of all symbols which can come immediately
1060** after a configuration.
1061*/
icculus9e44cf12010-02-14 17:14:22 +00001062void FindFollowSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001063{
1064 int i;
1065 struct config *cfp;
1066 struct plink *plp;
1067 int progress;
1068 int change;
1069
1070 for(i=0; i<lemp->nstate; i++){
1071 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1072 cfp->status = INCOMPLETE;
1073 }
1074 }
drh06f60d82017-04-14 19:46:12 +00001075
drh75897232000-05-29 14:26:00 +00001076 do{
1077 progress = 0;
1078 for(i=0; i<lemp->nstate; i++){
1079 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1080 if( cfp->status==COMPLETE ) continue;
1081 for(plp=cfp->fplp; plp; plp=plp->next){
1082 change = SetUnion(plp->cfp->fws,cfp->fws);
1083 if( change ){
1084 plp->cfp->status = INCOMPLETE;
1085 progress = 1;
drhf2f105d2012-08-20 15:53:54 +00001086 }
1087 }
drh75897232000-05-29 14:26:00 +00001088 cfp->status = COMPLETE;
1089 }
1090 }
1091 }while( progress );
1092}
1093
drh3cb2f6e2012-01-09 14:19:05 +00001094static int resolve_conflict(struct action *,struct action *);
drh75897232000-05-29 14:26:00 +00001095
1096/* Compute the reduce actions, and resolve conflicts.
1097*/
icculus9e44cf12010-02-14 17:14:22 +00001098void FindActions(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001099{
1100 int i,j;
1101 struct config *cfp;
1102 struct state *stp;
1103 struct symbol *sp;
1104 struct rule *rp;
1105
drh06f60d82017-04-14 19:46:12 +00001106 /* Add all of the reduce actions
drh75897232000-05-29 14:26:00 +00001107 ** A reduce action is added for each element of the followset of
1108 ** a configuration which has its dot at the extreme right.
1109 */
1110 for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
1111 stp = lemp->sorted[i];
1112 for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
1113 if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
1114 for(j=0; j<lemp->nterminal; j++){
1115 if( SetFind(cfp->fws,j) ){
1116 /* Add a reduce action to the state "stp" which will reduce by the
1117 ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
drh218dc692004-05-31 23:13:45 +00001118 Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
drh75897232000-05-29 14:26:00 +00001119 }
drhf2f105d2012-08-20 15:53:54 +00001120 }
drh75897232000-05-29 14:26:00 +00001121 }
1122 }
1123 }
1124
1125 /* Add the accepting token */
1126 if( lemp->start ){
1127 sp = Symbol_find(lemp->start);
drh4ef07702016-03-16 19:45:54 +00001128 if( sp==0 ) sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +00001129 }else{
drh4ef07702016-03-16 19:45:54 +00001130 sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +00001131 }
1132 /* Add to the first state (which is always the starting state of the
1133 ** finite state machine) an action to ACCEPT if the lookahead is the
1134 ** start nonterminal. */
1135 Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
1136
1137 /* Resolve conflicts */
1138 for(i=0; i<lemp->nstate; i++){
1139 struct action *ap, *nap;
drh75897232000-05-29 14:26:00 +00001140 stp = lemp->sorted[i];
drhe9278182007-07-18 18:16:29 +00001141 /* assert( stp->ap ); */
drh75897232000-05-29 14:26:00 +00001142 stp->ap = Action_sort(stp->ap);
drhb59499c2002-02-23 18:45:13 +00001143 for(ap=stp->ap; ap && ap->next; ap=ap->next){
drh75897232000-05-29 14:26:00 +00001144 for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
1145 /* The two actions "ap" and "nap" have the same lookahead.
1146 ** Figure out which one should be used */
drh3cb2f6e2012-01-09 14:19:05 +00001147 lemp->nconflict += resolve_conflict(ap,nap);
drh75897232000-05-29 14:26:00 +00001148 }
1149 }
1150 }
1151
1152 /* Report an error for each rule that can never be reduced. */
drhaa9f1122007-08-23 02:50:56 +00001153 for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00001154 for(i=0; i<lemp->nstate; i++){
1155 struct action *ap;
1156 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
drhaa9f1122007-08-23 02:50:56 +00001157 if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +00001158 }
1159 }
1160 for(rp=lemp->rule; rp; rp=rp->next){
1161 if( rp->canReduce ) continue;
1162 ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
1163 lemp->errorcnt++;
1164 }
1165}
1166
1167/* Resolve a conflict between the two given actions. If the
drh34ff57b2008-07-14 12:27:51 +00001168** conflict can't be resolved, return non-zero.
drh75897232000-05-29 14:26:00 +00001169**
1170** NO LONGER TRUE:
1171** To resolve a conflict, first look to see if either action
1172** is on an error rule. In that case, take the action which
1173** is not associated with the error rule. If neither or both
1174** actions are associated with an error rule, then try to
1175** use precedence to resolve the conflict.
1176**
1177** If either action is a SHIFT, then it must be apx. This
1178** function won't work if apx->type==REDUCE and apy->type==SHIFT.
1179*/
icculus9e44cf12010-02-14 17:14:22 +00001180static int resolve_conflict(
1181 struct action *apx,
drh3cb2f6e2012-01-09 14:19:05 +00001182 struct action *apy
icculus9e44cf12010-02-14 17:14:22 +00001183){
drh75897232000-05-29 14:26:00 +00001184 struct symbol *spx, *spy;
1185 int errcnt = 0;
1186 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
drhf0fa1c12006-12-14 01:06:22 +00001187 if( apx->type==SHIFT && apy->type==SHIFT ){
drh9892c5d2007-12-21 00:02:11 +00001188 apy->type = SSCONFLICT;
drhf0fa1c12006-12-14 01:06:22 +00001189 errcnt++;
1190 }
drh75897232000-05-29 14:26:00 +00001191 if( apx->type==SHIFT && apy->type==REDUCE ){
1192 spx = apx->sp;
1193 spy = apy->x.rp->precsym;
1194 if( spy==0 || spx->prec<0 || spy->prec<0 ){
1195 /* Not enough precedence information. */
drh9892c5d2007-12-21 00:02:11 +00001196 apy->type = SRCONFLICT;
drh75897232000-05-29 14:26:00 +00001197 errcnt++;
drhdd7e9db2010-07-19 01:52:07 +00001198 }else if( spx->prec>spy->prec ){ /* higher precedence wins */
drh75897232000-05-29 14:26:00 +00001199 apy->type = RD_RESOLVED;
1200 }else if( spx->prec<spy->prec ){
1201 apx->type = SH_RESOLVED;
1202 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
1203 apy->type = RD_RESOLVED; /* associativity */
1204 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
1205 apx->type = SH_RESOLVED;
1206 }else{
1207 assert( spx->prec==spy->prec && spx->assoc==NONE );
drh62a223e2014-06-09 13:11:40 +00001208 apx->type = ERROR;
drh75897232000-05-29 14:26:00 +00001209 }
1210 }else if( apx->type==REDUCE && apy->type==REDUCE ){
1211 spx = apx->x.rp->precsym;
1212 spy = apy->x.rp->precsym;
1213 if( spx==0 || spy==0 || spx->prec<0 ||
1214 spy->prec<0 || spx->prec==spy->prec ){
drh9892c5d2007-12-21 00:02:11 +00001215 apy->type = RRCONFLICT;
drh75897232000-05-29 14:26:00 +00001216 errcnt++;
1217 }else if( spx->prec>spy->prec ){
1218 apy->type = RD_RESOLVED;
1219 }else if( spx->prec<spy->prec ){
1220 apx->type = RD_RESOLVED;
1221 }
1222 }else{
drh06f60d82017-04-14 19:46:12 +00001223 assert(
drhb59499c2002-02-23 18:45:13 +00001224 apx->type==SH_RESOLVED ||
1225 apx->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001226 apx->type==SSCONFLICT ||
1227 apx->type==SRCONFLICT ||
1228 apx->type==RRCONFLICT ||
drhb59499c2002-02-23 18:45:13 +00001229 apy->type==SH_RESOLVED ||
1230 apy->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001231 apy->type==SSCONFLICT ||
1232 apy->type==SRCONFLICT ||
1233 apy->type==RRCONFLICT
drhb59499c2002-02-23 18:45:13 +00001234 );
1235 /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
1236 ** REDUCEs on the list. If we reach this point it must be because
1237 ** the parser conflict had already been resolved. */
drh75897232000-05-29 14:26:00 +00001238 }
1239 return errcnt;
1240}
1241/********************* From the file "configlist.c" *************************/
1242/*
1243** Routines to processing a configuration list and building a state
1244** in the LEMON parser generator.
1245*/
1246
1247static struct config *freelist = 0; /* List of free configurations */
1248static struct config *current = 0; /* Top of list of configurations */
1249static struct config **currentend = 0; /* Last on list of configs */
1250static struct config *basis = 0; /* Top of list of basis configs */
1251static struct config **basisend = 0; /* End of list of basis configs */
1252
1253/* Return a pointer to a new configuration */
drh14d88552017-04-14 19:44:15 +00001254PRIVATE struct config *newconfig(void){
icculus9e44cf12010-02-14 17:14:22 +00001255 struct config *newcfg;
drh75897232000-05-29 14:26:00 +00001256 if( freelist==0 ){
1257 int i;
1258 int amt = 3;
drh9892c5d2007-12-21 00:02:11 +00001259 freelist = (struct config *)calloc( amt, sizeof(struct config) );
drh75897232000-05-29 14:26:00 +00001260 if( freelist==0 ){
1261 fprintf(stderr,"Unable to allocate memory for a new configuration.");
1262 exit(1);
1263 }
1264 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
1265 freelist[amt-1].next = 0;
1266 }
icculus9e44cf12010-02-14 17:14:22 +00001267 newcfg = freelist;
drh75897232000-05-29 14:26:00 +00001268 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00001269 return newcfg;
drh75897232000-05-29 14:26:00 +00001270}
1271
1272/* The configuration "old" is no longer used */
icculus9e44cf12010-02-14 17:14:22 +00001273PRIVATE void deleteconfig(struct config *old)
drh75897232000-05-29 14:26:00 +00001274{
1275 old->next = freelist;
1276 freelist = old;
1277}
1278
1279/* Initialized the configuration list builder */
drh14d88552017-04-14 19:44:15 +00001280void Configlist_init(void){
drh75897232000-05-29 14:26:00 +00001281 current = 0;
1282 currentend = &current;
1283 basis = 0;
1284 basisend = &basis;
1285 Configtable_init();
1286 return;
1287}
1288
1289/* Initialized the configuration list builder */
drh14d88552017-04-14 19:44:15 +00001290void Configlist_reset(void){
drh75897232000-05-29 14:26:00 +00001291 current = 0;
1292 currentend = &current;
1293 basis = 0;
1294 basisend = &basis;
1295 Configtable_clear(0);
1296 return;
1297}
1298
1299/* Add another configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001300struct config *Configlist_add(
1301 struct rule *rp, /* The rule */
1302 int dot /* Index into the RHS of the rule where the dot goes */
1303){
drh75897232000-05-29 14:26:00 +00001304 struct config *cfp, model;
1305
1306 assert( currentend!=0 );
1307 model.rp = rp;
1308 model.dot = dot;
1309 cfp = Configtable_find(&model);
1310 if( cfp==0 ){
1311 cfp = newconfig();
1312 cfp->rp = rp;
1313 cfp->dot = dot;
1314 cfp->fws = SetNew();
1315 cfp->stp = 0;
1316 cfp->fplp = cfp->bplp = 0;
1317 cfp->next = 0;
1318 cfp->bp = 0;
1319 *currentend = cfp;
1320 currentend = &cfp->next;
1321 Configtable_insert(cfp);
1322 }
1323 return cfp;
1324}
1325
1326/* Add a basis configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001327struct config *Configlist_addbasis(struct rule *rp, int dot)
drh75897232000-05-29 14:26:00 +00001328{
1329 struct config *cfp, model;
1330
1331 assert( basisend!=0 );
1332 assert( currentend!=0 );
1333 model.rp = rp;
1334 model.dot = dot;
1335 cfp = Configtable_find(&model);
1336 if( cfp==0 ){
1337 cfp = newconfig();
1338 cfp->rp = rp;
1339 cfp->dot = dot;
1340 cfp->fws = SetNew();
1341 cfp->stp = 0;
1342 cfp->fplp = cfp->bplp = 0;
1343 cfp->next = 0;
1344 cfp->bp = 0;
1345 *currentend = cfp;
1346 currentend = &cfp->next;
1347 *basisend = cfp;
1348 basisend = &cfp->bp;
1349 Configtable_insert(cfp);
1350 }
1351 return cfp;
1352}
1353
1354/* Compute the closure of the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001355void Configlist_closure(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001356{
1357 struct config *cfp, *newcfp;
1358 struct rule *rp, *newrp;
1359 struct symbol *sp, *xsp;
1360 int i, dot;
1361
1362 assert( currentend!=0 );
1363 for(cfp=current; cfp; cfp=cfp->next){
1364 rp = cfp->rp;
1365 dot = cfp->dot;
1366 if( dot>=rp->nrhs ) continue;
1367 sp = rp->rhs[dot];
1368 if( sp->type==NONTERMINAL ){
1369 if( sp->rule==0 && sp!=lemp->errsym ){
1370 ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
1371 sp->name);
1372 lemp->errorcnt++;
1373 }
1374 for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
1375 newcfp = Configlist_add(newrp,0);
1376 for(i=dot+1; i<rp->nrhs; i++){
1377 xsp = rp->rhs[i];
1378 if( xsp->type==TERMINAL ){
1379 SetAdd(newcfp->fws,xsp->index);
1380 break;
drhfd405312005-11-06 04:06:59 +00001381 }else if( xsp->type==MULTITERMINAL ){
1382 int k;
1383 for(k=0; k<xsp->nsubsym; k++){
1384 SetAdd(newcfp->fws, xsp->subsym[k]->index);
1385 }
1386 break;
drhf2f105d2012-08-20 15:53:54 +00001387 }else{
drh75897232000-05-29 14:26:00 +00001388 SetUnion(newcfp->fws,xsp->firstset);
drhaa9f1122007-08-23 02:50:56 +00001389 if( xsp->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +00001390 }
1391 }
drh75897232000-05-29 14:26:00 +00001392 if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
1393 }
1394 }
1395 }
1396 return;
1397}
1398
1399/* Sort the configuration list */
drh14d88552017-04-14 19:44:15 +00001400void Configlist_sort(void){
drh25473362015-09-04 18:03:45 +00001401 current = (struct config*)msort((char*)current,(char**)&(current->next),
1402 Configcmp);
drh75897232000-05-29 14:26:00 +00001403 currentend = 0;
1404 return;
1405}
1406
1407/* Sort the basis configuration list */
drh14d88552017-04-14 19:44:15 +00001408void Configlist_sortbasis(void){
drh25473362015-09-04 18:03:45 +00001409 basis = (struct config*)msort((char*)current,(char**)&(current->bp),
1410 Configcmp);
drh75897232000-05-29 14:26:00 +00001411 basisend = 0;
1412 return;
1413}
1414
1415/* Return a pointer to the head of the configuration list and
1416** reset the list */
drh14d88552017-04-14 19:44:15 +00001417struct config *Configlist_return(void){
drh75897232000-05-29 14:26:00 +00001418 struct config *old;
1419 old = current;
1420 current = 0;
1421 currentend = 0;
1422 return old;
1423}
1424
1425/* Return a pointer to the head of the configuration list and
1426** reset the list */
drh14d88552017-04-14 19:44:15 +00001427struct config *Configlist_basis(void){
drh75897232000-05-29 14:26:00 +00001428 struct config *old;
1429 old = basis;
1430 basis = 0;
1431 basisend = 0;
1432 return old;
1433}
1434
1435/* Free all elements of the given configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001436void Configlist_eat(struct config *cfp)
drh75897232000-05-29 14:26:00 +00001437{
1438 struct config *nextcfp;
1439 for(; cfp; cfp=nextcfp){
1440 nextcfp = cfp->next;
1441 assert( cfp->fplp==0 );
1442 assert( cfp->bplp==0 );
1443 if( cfp->fws ) SetFree(cfp->fws);
1444 deleteconfig(cfp);
1445 }
1446 return;
1447}
1448/***************** From the file "error.c" *********************************/
1449/*
1450** Code for printing error message.
1451*/
1452
drhf9a2e7b2003-04-15 01:49:48 +00001453void ErrorMsg(const char *filename, int lineno, const char *format, ...){
icculus15a2cec2010-02-16 16:07:28 +00001454 va_list ap;
icculus1c11f742010-02-15 00:01:04 +00001455 fprintf(stderr, "%s:%d: ", filename, lineno);
1456 va_start(ap, format);
1457 vfprintf(stderr,format,ap);
1458 va_end(ap);
1459 fprintf(stderr, "\n");
drh75897232000-05-29 14:26:00 +00001460}
1461/**************** From the file "main.c" ************************************/
1462/*
1463** Main program file for the LEMON parser generator.
1464*/
1465
1466/* Report an out-of-memory condition and abort. This function
1467** is used mostly by the "MemoryCheck" macro in struct.h
1468*/
drh14d88552017-04-14 19:44:15 +00001469void memory_error(void){
drh75897232000-05-29 14:26:00 +00001470 fprintf(stderr,"Out of memory. Aborting...\n");
1471 exit(1);
1472}
1473
drh6d08b4d2004-07-20 12:45:22 +00001474static int nDefine = 0; /* Number of -D options on the command line */
1475static char **azDefine = 0; /* Name of the -D macros */
1476
1477/* This routine is called with the argument to each -D command-line option.
1478** Add the macro defined to the azDefine array.
1479*/
1480static void handle_D_option(char *z){
1481 char **paz;
1482 nDefine++;
icculus9e44cf12010-02-14 17:14:22 +00001483 azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine);
drh6d08b4d2004-07-20 12:45:22 +00001484 if( azDefine==0 ){
1485 fprintf(stderr,"out of memory\n");
1486 exit(1);
1487 }
1488 paz = &azDefine[nDefine-1];
icculus9e44cf12010-02-14 17:14:22 +00001489 *paz = (char *) malloc( lemonStrlen(z)+1 );
drh6d08b4d2004-07-20 12:45:22 +00001490 if( *paz==0 ){
1491 fprintf(stderr,"out of memory\n");
1492 exit(1);
1493 }
drh898799f2014-01-10 23:21:00 +00001494 lemon_strcpy(*paz, z);
drh6d08b4d2004-07-20 12:45:22 +00001495 for(z=*paz; *z && *z!='='; z++){}
1496 *z = 0;
1497}
1498
icculus3e143bd2010-02-14 00:48:49 +00001499static char *user_templatename = NULL;
1500static void handle_T_option(char *z){
icculus9e44cf12010-02-14 17:14:22 +00001501 user_templatename = (char *) malloc( lemonStrlen(z)+1 );
icculus3e143bd2010-02-14 00:48:49 +00001502 if( user_templatename==0 ){
1503 memory_error();
1504 }
drh898799f2014-01-10 23:21:00 +00001505 lemon_strcpy(user_templatename, z);
icculus3e143bd2010-02-14 00:48:49 +00001506}
drh75897232000-05-29 14:26:00 +00001507
drh711c9812016-05-23 14:24:31 +00001508/* Merge together to lists of rules ordered by rule.iRule */
drh4ef07702016-03-16 19:45:54 +00001509static struct rule *Rule_merge(struct rule *pA, struct rule *pB){
1510 struct rule *pFirst = 0;
1511 struct rule **ppPrev = &pFirst;
1512 while( pA && pB ){
1513 if( pA->iRule<pB->iRule ){
1514 *ppPrev = pA;
1515 ppPrev = &pA->next;
1516 pA = pA->next;
1517 }else{
1518 *ppPrev = pB;
1519 ppPrev = &pB->next;
1520 pB = pB->next;
1521 }
1522 }
1523 if( pA ){
1524 *ppPrev = pA;
1525 }else{
1526 *ppPrev = pB;
1527 }
1528 return pFirst;
1529}
1530
1531/*
1532** Sort a list of rules in order of increasing iRule value
1533*/
1534static struct rule *Rule_sort(struct rule *rp){
1535 int i;
1536 struct rule *pNext;
1537 struct rule *x[32];
1538 memset(x, 0, sizeof(x));
1539 while( rp ){
1540 pNext = rp->next;
1541 rp->next = 0;
1542 for(i=0; i<sizeof(x)/sizeof(x[0]) && x[i]; i++){
1543 rp = Rule_merge(x[i], rp);
1544 x[i] = 0;
1545 }
1546 x[i] = rp;
1547 rp = pNext;
1548 }
1549 rp = 0;
1550 for(i=0; i<sizeof(x)/sizeof(x[0]); i++){
1551 rp = Rule_merge(x[i], rp);
1552 }
1553 return rp;
1554}
1555
drhc75e0162015-09-07 02:23:02 +00001556/* forward reference */
1557static const char *minimum_size_type(int lwr, int upr, int *pnByte);
1558
1559/* Print a single line of the "Parser Stats" output
1560*/
1561static void stats_line(const char *zLabel, int iValue){
1562 int nLabel = lemonStrlen(zLabel);
1563 printf(" %s%.*s %5d\n", zLabel,
1564 35-nLabel, "................................",
1565 iValue);
1566}
1567
drh75897232000-05-29 14:26:00 +00001568/* The main program. Parse the command line and do it... */
icculus9e44cf12010-02-14 17:14:22 +00001569int main(int argc, char **argv)
drh75897232000-05-29 14:26:00 +00001570{
1571 static int version = 0;
1572 static int rpflag = 0;
1573 static int basisflag = 0;
1574 static int compress = 0;
1575 static int quiet = 0;
1576 static int statistics = 0;
1577 static int mhflag = 0;
shane58543932008-12-10 20:10:04 +00001578 static int nolinenosflag = 0;
drhdd7e9db2010-07-19 01:52:07 +00001579 static int noResort = 0;
drh75897232000-05-29 14:26:00 +00001580 static struct s_options options[] = {
1581 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1582 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
drh6d08b4d2004-07-20 12:45:22 +00001583 {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
drh0325d392015-01-01 19:11:22 +00001584 {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"},
drh75897232000-05-29 14:26:00 +00001585 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
drh0325d392015-01-01 19:11:22 +00001586 {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"},
shane58543932008-12-10 20:10:04 +00001587 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
1588 {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
drh0325d392015-01-01 19:11:22 +00001589 {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"},
drhf5c4e0f2010-07-18 11:35:53 +00001590 {OPT_FLAG, "p", (char*)&showPrecedenceConflict,
1591 "Show conflicts resolved by precedence rules"},
drh75897232000-05-29 14:26:00 +00001592 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
drhdd7e9db2010-07-19 01:52:07 +00001593 {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"},
drh6d08b4d2004-07-20 12:45:22 +00001594 {OPT_FLAG, "s", (char*)&statistics,
1595 "Print parser stats to standard output."},
drh75897232000-05-29 14:26:00 +00001596 {OPT_FLAG, "x", (char*)&version, "Print the version number."},
drh0325d392015-01-01 19:11:22 +00001597 {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
1598 {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"},
drh75897232000-05-29 14:26:00 +00001599 {OPT_FLAG,0,0,0}
1600 };
1601 int i;
icculus42585cf2010-02-14 05:19:56 +00001602 int exitcode;
drh75897232000-05-29 14:26:00 +00001603 struct lemon lem;
drh4ef07702016-03-16 19:45:54 +00001604 struct rule *rp;
drh75897232000-05-29 14:26:00 +00001605
drhb0c86772000-06-02 23:21:26 +00001606 OptInit(argv,options,stderr);
drh75897232000-05-29 14:26:00 +00001607 if( version ){
drhb19a2bc2001-09-16 00:13:26 +00001608 printf("Lemon version 1.0\n");
drh06f60d82017-04-14 19:46:12 +00001609 exit(0);
drh75897232000-05-29 14:26:00 +00001610 }
drhb0c86772000-06-02 23:21:26 +00001611 if( OptNArgs()!=1 ){
drh75897232000-05-29 14:26:00 +00001612 fprintf(stderr,"Exactly one filename argument is required.\n");
1613 exit(1);
1614 }
drh954f6b42006-06-13 13:27:46 +00001615 memset(&lem, 0, sizeof(lem));
drh75897232000-05-29 14:26:00 +00001616 lem.errorcnt = 0;
1617
1618 /* Initialize the machine */
1619 Strsafe_init();
1620 Symbol_init();
1621 State_init();
1622 lem.argv0 = argv[0];
drhb0c86772000-06-02 23:21:26 +00001623 lem.filename = OptArg(0);
drh75897232000-05-29 14:26:00 +00001624 lem.basisflag = basisflag;
shane58543932008-12-10 20:10:04 +00001625 lem.nolinenosflag = nolinenosflag;
drh75897232000-05-29 14:26:00 +00001626 Symbol_new("$");
1627 lem.errsym = Symbol_new("error");
drhc4dd3fd2008-01-22 01:48:05 +00001628 lem.errsym->useCnt = 0;
drh75897232000-05-29 14:26:00 +00001629
1630 /* Parse the input file */
1631 Parse(&lem);
1632 if( lem.errorcnt ) exit(lem.errorcnt);
drh954f6b42006-06-13 13:27:46 +00001633 if( lem.nrule==0 ){
drh75897232000-05-29 14:26:00 +00001634 fprintf(stderr,"Empty grammar.\n");
1635 exit(1);
1636 }
1637
1638 /* Count and index the symbols of the grammar */
drh75897232000-05-29 14:26:00 +00001639 Symbol_new("{default}");
drh61f92cd2014-01-11 03:06:18 +00001640 lem.nsymbol = Symbol_count();
drh75897232000-05-29 14:26:00 +00001641 lem.symbols = Symbol_arrayof();
drh61f92cd2014-01-11 03:06:18 +00001642 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1643 qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp);
1644 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1645 while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; }
1646 assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 );
1647 lem.nsymbol = i - 1;
drhc56fac72015-10-29 13:48:15 +00001648 for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++);
drh75897232000-05-29 14:26:00 +00001649 lem.nterminal = i;
1650
drh711c9812016-05-23 14:24:31 +00001651 /* Assign sequential rule numbers. Start with 0. Put rules that have no
1652 ** reduce action C-code associated with them last, so that the switch()
1653 ** statement that selects reduction actions will have a smaller jump table.
1654 */
drh4ef07702016-03-16 19:45:54 +00001655 for(i=0, rp=lem.rule; rp; rp=rp->next){
1656 rp->iRule = rp->code ? i++ : -1;
1657 }
1658 for(rp=lem.rule; rp; rp=rp->next){
1659 if( rp->iRule<0 ) rp->iRule = i++;
1660 }
1661 lem.startRule = lem.rule;
1662 lem.rule = Rule_sort(lem.rule);
1663
drh75897232000-05-29 14:26:00 +00001664 /* Generate a reprint of the grammar, if requested on the command line */
1665 if( rpflag ){
1666 Reprint(&lem);
1667 }else{
1668 /* Initialize the size for all follow and first sets */
drh9892c5d2007-12-21 00:02:11 +00001669 SetSize(lem.nterminal+1);
drh75897232000-05-29 14:26:00 +00001670
1671 /* Find the precedence for every production rule (that has one) */
1672 FindRulePrecedences(&lem);
1673
1674 /* Compute the lambda-nonterminals and the first-sets for every
1675 ** nonterminal */
1676 FindFirstSets(&lem);
1677
1678 /* Compute all LR(0) states. Also record follow-set propagation
1679 ** links so that the follow-set can be computed later */
1680 lem.nstate = 0;
1681 FindStates(&lem);
1682 lem.sorted = State_arrayof();
1683
1684 /* Tie up loose ends on the propagation links */
1685 FindLinks(&lem);
1686
1687 /* Compute the follow set of every reducible configuration */
1688 FindFollowSets(&lem);
1689
1690 /* Compute the action tables */
1691 FindActions(&lem);
1692
1693 /* Compress the action tables */
1694 if( compress==0 ) CompressTables(&lem);
1695
drhada354d2005-11-05 15:03:59 +00001696 /* Reorder and renumber the states so that states with fewer choices
drhdd7e9db2010-07-19 01:52:07 +00001697 ** occur at the end. This is an optimization that helps make the
1698 ** generated parser tables smaller. */
1699 if( noResort==0 ) ResortStates(&lem);
drhada354d2005-11-05 15:03:59 +00001700
drh75897232000-05-29 14:26:00 +00001701 /* Generate a report of the parser generated. (the "y.output" file) */
1702 if( !quiet ) ReportOutput(&lem);
1703
1704 /* Generate the source code for the parser */
1705 ReportTable(&lem, mhflag);
1706
1707 /* Produce a header file for use by the scanner. (This step is
1708 ** omitted if the "-m" option is used because makeheaders will
1709 ** generate the file for us.) */
1710 if( !mhflag ) ReportHeader(&lem);
1711 }
1712 if( statistics ){
drhc75e0162015-09-07 02:23:02 +00001713 printf("Parser statistics:\n");
1714 stats_line("terminal symbols", lem.nterminal);
1715 stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal);
1716 stats_line("total symbols", lem.nsymbol);
1717 stats_line("rules", lem.nrule);
drh3bd48ab2015-09-07 18:23:37 +00001718 stats_line("states", lem.nxstate);
drhc75e0162015-09-07 02:23:02 +00001719 stats_line("conflicts", lem.nconflict);
1720 stats_line("action table entries", lem.nactiontab);
1721 stats_line("total table size (bytes)", lem.tablesize);
drh75897232000-05-29 14:26:00 +00001722 }
icculus8e158022010-02-16 16:09:03 +00001723 if( lem.nconflict > 0 ){
1724 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
icculus42585cf2010-02-14 05:19:56 +00001725 }
1726
1727 /* return 0 on success, 1 on failure. */
icculus8e158022010-02-16 16:09:03 +00001728 exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
icculus42585cf2010-02-14 05:19:56 +00001729 exit(exitcode);
1730 return (exitcode);
drh75897232000-05-29 14:26:00 +00001731}
1732/******************** From the file "msort.c" *******************************/
1733/*
1734** A generic merge-sort program.
1735**
1736** USAGE:
1737** Let "ptr" be a pointer to some structure which is at the head of
1738** a null-terminated list. Then to sort the list call:
1739**
1740** ptr = msort(ptr,&(ptr->next),cmpfnc);
1741**
1742** In the above, "cmpfnc" is a pointer to a function which compares
1743** two instances of the structure and returns an integer, as in
1744** strcmp. The second argument is a pointer to the pointer to the
1745** second element of the linked list. This address is used to compute
1746** the offset to the "next" field within the structure. The offset to
1747** the "next" field must be constant for all structures in the list.
1748**
1749** The function returns a new pointer which is the head of the list
1750** after sorting.
1751**
1752** ALGORITHM:
1753** Merge-sort.
1754*/
1755
1756/*
1757** Return a pointer to the next structure in the linked list.
1758*/
drhd25d6922012-04-18 09:59:56 +00001759#define NEXT(A) (*(char**)(((char*)A)+offset))
drh75897232000-05-29 14:26:00 +00001760
1761/*
1762** Inputs:
1763** a: A sorted, null-terminated linked list. (May be null).
1764** b: A sorted, null-terminated linked list. (May be null).
1765** cmp: A pointer to the comparison function.
1766** offset: Offset in the structure to the "next" field.
1767**
1768** Return Value:
1769** A pointer to the head of a sorted list containing the elements
1770** of both a and b.
1771**
1772** Side effects:
1773** The "next" pointers for elements in the lists a and b are
1774** changed.
1775*/
drhe9278182007-07-18 18:16:29 +00001776static char *merge(
1777 char *a,
1778 char *b,
1779 int (*cmp)(const char*,const char*),
1780 int offset
1781){
drh75897232000-05-29 14:26:00 +00001782 char *ptr, *head;
1783
1784 if( a==0 ){
1785 head = b;
1786 }else if( b==0 ){
1787 head = a;
1788 }else{
drhe594bc32009-11-03 13:02:25 +00001789 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001790 ptr = a;
1791 a = NEXT(a);
1792 }else{
1793 ptr = b;
1794 b = NEXT(b);
1795 }
1796 head = ptr;
1797 while( a && b ){
drhe594bc32009-11-03 13:02:25 +00001798 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001799 NEXT(ptr) = a;
1800 ptr = a;
1801 a = NEXT(a);
1802 }else{
1803 NEXT(ptr) = b;
1804 ptr = b;
1805 b = NEXT(b);
1806 }
1807 }
1808 if( a ) NEXT(ptr) = a;
1809 else NEXT(ptr) = b;
1810 }
1811 return head;
1812}
1813
1814/*
1815** Inputs:
1816** list: Pointer to a singly-linked list of structures.
1817** next: Pointer to pointer to the second element of the list.
1818** cmp: A comparison function.
1819**
1820** Return Value:
1821** A pointer to the head of a sorted list containing the elements
1822** orginally in list.
1823**
1824** Side effects:
1825** The "next" pointers for elements in list are changed.
1826*/
1827#define LISTSIZE 30
drhe9278182007-07-18 18:16:29 +00001828static char *msort(
1829 char *list,
1830 char **next,
1831 int (*cmp)(const char*,const char*)
1832){
drhba99af52001-10-25 20:37:16 +00001833 unsigned long offset;
drh75897232000-05-29 14:26:00 +00001834 char *ep;
1835 char *set[LISTSIZE];
1836 int i;
drh1cc0d112015-03-31 15:15:48 +00001837 offset = (unsigned long)((char*)next - (char*)list);
drh75897232000-05-29 14:26:00 +00001838 for(i=0; i<LISTSIZE; i++) set[i] = 0;
1839 while( list ){
1840 ep = list;
1841 list = NEXT(list);
1842 NEXT(ep) = 0;
1843 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1844 ep = merge(ep,set[i],cmp,offset);
1845 set[i] = 0;
1846 }
1847 set[i] = ep;
1848 }
1849 ep = 0;
drhe594bc32009-11-03 13:02:25 +00001850 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset);
drh75897232000-05-29 14:26:00 +00001851 return ep;
1852}
1853/************************ From the file "option.c" **************************/
1854static char **argv;
1855static struct s_options *op;
1856static FILE *errstream;
1857
1858#define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1859
1860/*
1861** Print the command line with a carrot pointing to the k-th character
1862** of the n-th field.
1863*/
icculus9e44cf12010-02-14 17:14:22 +00001864static void errline(int n, int k, FILE *err)
drh75897232000-05-29 14:26:00 +00001865{
1866 int spcnt, i;
drh75897232000-05-29 14:26:00 +00001867 if( argv[0] ) fprintf(err,"%s",argv[0]);
drh87cf1372008-08-13 20:09:06 +00001868 spcnt = lemonStrlen(argv[0]) + 1;
drh75897232000-05-29 14:26:00 +00001869 for(i=1; i<n && argv[i]; i++){
1870 fprintf(err," %s",argv[i]);
drh87cf1372008-08-13 20:09:06 +00001871 spcnt += lemonStrlen(argv[i])+1;
drh75897232000-05-29 14:26:00 +00001872 }
1873 spcnt += k;
1874 for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1875 if( spcnt<20 ){
1876 fprintf(err,"\n%*s^-- here\n",spcnt,"");
1877 }else{
1878 fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1879 }
1880}
1881
1882/*
1883** Return the index of the N-th non-switch argument. Return -1
1884** if N is out of range.
1885*/
icculus9e44cf12010-02-14 17:14:22 +00001886static int argindex(int n)
drh75897232000-05-29 14:26:00 +00001887{
1888 int i;
1889 int dashdash = 0;
1890 if( argv!=0 && *argv!=0 ){
1891 for(i=1; argv[i]; i++){
1892 if( dashdash || !ISOPT(argv[i]) ){
1893 if( n==0 ) return i;
1894 n--;
1895 }
1896 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1897 }
1898 }
1899 return -1;
1900}
1901
1902static char emsg[] = "Command line syntax error: ";
1903
1904/*
1905** Process a flag command line argument.
1906*/
icculus9e44cf12010-02-14 17:14:22 +00001907static int handleflags(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001908{
1909 int v;
1910 int errcnt = 0;
1911 int j;
1912 for(j=0; op[j].label; j++){
drh87cf1372008-08-13 20:09:06 +00001913 if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
drh75897232000-05-29 14:26:00 +00001914 }
1915 v = argv[i][0]=='-' ? 1 : 0;
1916 if( op[j].label==0 ){
1917 if( err ){
1918 fprintf(err,"%sundefined option.\n",emsg);
1919 errline(i,1,err);
1920 }
1921 errcnt++;
drh0325d392015-01-01 19:11:22 +00001922 }else if( op[j].arg==0 ){
1923 /* Ignore this option */
drh75897232000-05-29 14:26:00 +00001924 }else if( op[j].type==OPT_FLAG ){
1925 *((int*)op[j].arg) = v;
1926 }else if( op[j].type==OPT_FFLAG ){
icculus9e44cf12010-02-14 17:14:22 +00001927 (*(void(*)(int))(op[j].arg))(v);
drh6d08b4d2004-07-20 12:45:22 +00001928 }else if( op[j].type==OPT_FSTR ){
icculus9e44cf12010-02-14 17:14:22 +00001929 (*(void(*)(char *))(op[j].arg))(&argv[i][2]);
drh75897232000-05-29 14:26:00 +00001930 }else{
1931 if( err ){
1932 fprintf(err,"%smissing argument on switch.\n",emsg);
1933 errline(i,1,err);
1934 }
1935 errcnt++;
1936 }
1937 return errcnt;
1938}
1939
1940/*
1941** Process a command line switch which has an argument.
1942*/
icculus9e44cf12010-02-14 17:14:22 +00001943static int handleswitch(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001944{
1945 int lv = 0;
1946 double dv = 0.0;
1947 char *sv = 0, *end;
1948 char *cp;
1949 int j;
1950 int errcnt = 0;
1951 cp = strchr(argv[i],'=');
drh43617e92006-03-06 20:55:46 +00001952 assert( cp!=0 );
drh75897232000-05-29 14:26:00 +00001953 *cp = 0;
1954 for(j=0; op[j].label; j++){
1955 if( strcmp(argv[i],op[j].label)==0 ) break;
1956 }
1957 *cp = '=';
1958 if( op[j].label==0 ){
1959 if( err ){
1960 fprintf(err,"%sundefined option.\n",emsg);
1961 errline(i,0,err);
1962 }
1963 errcnt++;
1964 }else{
1965 cp++;
1966 switch( op[j].type ){
1967 case OPT_FLAG:
1968 case OPT_FFLAG:
1969 if( err ){
1970 fprintf(err,"%soption requires an argument.\n",emsg);
1971 errline(i,0,err);
1972 }
1973 errcnt++;
1974 break;
1975 case OPT_DBL:
1976 case OPT_FDBL:
1977 dv = strtod(cp,&end);
1978 if( *end ){
1979 if( err ){
drh25473362015-09-04 18:03:45 +00001980 fprintf(err,
1981 "%sillegal character in floating-point argument.\n",emsg);
drh1cc0d112015-03-31 15:15:48 +00001982 errline(i,(int)((char*)end-(char*)argv[i]),err);
drh75897232000-05-29 14:26:00 +00001983 }
1984 errcnt++;
1985 }
1986 break;
1987 case OPT_INT:
1988 case OPT_FINT:
1989 lv = strtol(cp,&end,0);
1990 if( *end ){
1991 if( err ){
1992 fprintf(err,"%sillegal character in integer argument.\n",emsg);
drh1cc0d112015-03-31 15:15:48 +00001993 errline(i,(int)((char*)end-(char*)argv[i]),err);
drh75897232000-05-29 14:26:00 +00001994 }
1995 errcnt++;
1996 }
1997 break;
1998 case OPT_STR:
1999 case OPT_FSTR:
2000 sv = cp;
2001 break;
2002 }
2003 switch( op[j].type ){
2004 case OPT_FLAG:
2005 case OPT_FFLAG:
2006 break;
2007 case OPT_DBL:
2008 *(double*)(op[j].arg) = dv;
2009 break;
2010 case OPT_FDBL:
icculus9e44cf12010-02-14 17:14:22 +00002011 (*(void(*)(double))(op[j].arg))(dv);
drh75897232000-05-29 14:26:00 +00002012 break;
2013 case OPT_INT:
2014 *(int*)(op[j].arg) = lv;
2015 break;
2016 case OPT_FINT:
icculus9e44cf12010-02-14 17:14:22 +00002017 (*(void(*)(int))(op[j].arg))((int)lv);
drh75897232000-05-29 14:26:00 +00002018 break;
2019 case OPT_STR:
2020 *(char**)(op[j].arg) = sv;
2021 break;
2022 case OPT_FSTR:
icculus9e44cf12010-02-14 17:14:22 +00002023 (*(void(*)(char *))(op[j].arg))(sv);
drh75897232000-05-29 14:26:00 +00002024 break;
2025 }
2026 }
2027 return errcnt;
2028}
2029
icculus9e44cf12010-02-14 17:14:22 +00002030int OptInit(char **a, struct s_options *o, FILE *err)
drh75897232000-05-29 14:26:00 +00002031{
2032 int errcnt = 0;
2033 argv = a;
2034 op = o;
2035 errstream = err;
2036 if( argv && *argv && op ){
2037 int i;
2038 for(i=1; argv[i]; i++){
2039 if( argv[i][0]=='+' || argv[i][0]=='-' ){
2040 errcnt += handleflags(i,err);
2041 }else if( strchr(argv[i],'=') ){
2042 errcnt += handleswitch(i,err);
2043 }
2044 }
2045 }
2046 if( errcnt>0 ){
2047 fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
drhb0c86772000-06-02 23:21:26 +00002048 OptPrint();
drh75897232000-05-29 14:26:00 +00002049 exit(1);
2050 }
2051 return 0;
2052}
2053
drh14d88552017-04-14 19:44:15 +00002054int OptNArgs(void){
drh75897232000-05-29 14:26:00 +00002055 int cnt = 0;
2056 int dashdash = 0;
2057 int i;
2058 if( argv!=0 && argv[0]!=0 ){
2059 for(i=1; argv[i]; i++){
2060 if( dashdash || !ISOPT(argv[i]) ) cnt++;
2061 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
2062 }
2063 }
2064 return cnt;
2065}
2066
icculus9e44cf12010-02-14 17:14:22 +00002067char *OptArg(int n)
drh75897232000-05-29 14:26:00 +00002068{
2069 int i;
2070 i = argindex(n);
2071 return i>=0 ? argv[i] : 0;
2072}
2073
icculus9e44cf12010-02-14 17:14:22 +00002074void OptErr(int n)
drh75897232000-05-29 14:26:00 +00002075{
2076 int i;
2077 i = argindex(n);
2078 if( i>=0 ) errline(i,0,errstream);
2079}
2080
drh14d88552017-04-14 19:44:15 +00002081void OptPrint(void){
drh75897232000-05-29 14:26:00 +00002082 int i;
2083 int max, len;
2084 max = 0;
2085 for(i=0; op[i].label; i++){
drh87cf1372008-08-13 20:09:06 +00002086 len = lemonStrlen(op[i].label) + 1;
drh75897232000-05-29 14:26:00 +00002087 switch( op[i].type ){
2088 case OPT_FLAG:
2089 case OPT_FFLAG:
2090 break;
2091 case OPT_INT:
2092 case OPT_FINT:
2093 len += 9; /* length of "<integer>" */
2094 break;
2095 case OPT_DBL:
2096 case OPT_FDBL:
2097 len += 6; /* length of "<real>" */
2098 break;
2099 case OPT_STR:
2100 case OPT_FSTR:
2101 len += 8; /* length of "<string>" */
2102 break;
2103 }
2104 if( len>max ) max = len;
2105 }
2106 for(i=0; op[i].label; i++){
2107 switch( op[i].type ){
2108 case OPT_FLAG:
2109 case OPT_FFLAG:
2110 fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
2111 break;
2112 case OPT_INT:
2113 case OPT_FINT:
drh0325d392015-01-01 19:11:22 +00002114 fprintf(errstream," -%s<integer>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002115 (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002116 break;
2117 case OPT_DBL:
2118 case OPT_FDBL:
drh0325d392015-01-01 19:11:22 +00002119 fprintf(errstream," -%s<real>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002120 (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002121 break;
2122 case OPT_STR:
2123 case OPT_FSTR:
drh0325d392015-01-01 19:11:22 +00002124 fprintf(errstream," -%s<string>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002125 (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002126 break;
2127 }
2128 }
2129}
2130/*********************** From the file "parse.c" ****************************/
2131/*
2132** Input file parser for the LEMON parser generator.
2133*/
2134
2135/* The state of the parser */
icculus9e44cf12010-02-14 17:14:22 +00002136enum e_state {
2137 INITIALIZE,
2138 WAITING_FOR_DECL_OR_RULE,
2139 WAITING_FOR_DECL_KEYWORD,
2140 WAITING_FOR_DECL_ARG,
2141 WAITING_FOR_PRECEDENCE_SYMBOL,
2142 WAITING_FOR_ARROW,
2143 IN_RHS,
2144 LHS_ALIAS_1,
2145 LHS_ALIAS_2,
2146 LHS_ALIAS_3,
2147 RHS_ALIAS_1,
2148 RHS_ALIAS_2,
2149 PRECEDENCE_MARK_1,
2150 PRECEDENCE_MARK_2,
2151 RESYNC_AFTER_RULE_ERROR,
2152 RESYNC_AFTER_DECL_ERROR,
2153 WAITING_FOR_DESTRUCTOR_SYMBOL,
2154 WAITING_FOR_DATATYPE_SYMBOL,
2155 WAITING_FOR_FALLBACK_ID,
drh61f92cd2014-01-11 03:06:18 +00002156 WAITING_FOR_WILDCARD_ID,
2157 WAITING_FOR_CLASS_ID,
drh59c435a2017-08-02 03:21:11 +00002158 WAITING_FOR_CLASS_TOKEN,
2159 WAITING_FOR_TOKEN_NAME
icculus9e44cf12010-02-14 17:14:22 +00002160};
drh75897232000-05-29 14:26:00 +00002161struct pstate {
2162 char *filename; /* Name of the input file */
2163 int tokenlineno; /* Linenumber at which current token starts */
2164 int errorcnt; /* Number of errors so far */
2165 char *tokenstart; /* Text of current token */
2166 struct lemon *gp; /* Global state vector */
icculus9e44cf12010-02-14 17:14:22 +00002167 enum e_state state; /* The state of the parser */
drh0bd1f4e2002-06-06 18:54:39 +00002168 struct symbol *fallback; /* The fallback token */
drh61f92cd2014-01-11 03:06:18 +00002169 struct symbol *tkclass; /* Token class symbol */
drh75897232000-05-29 14:26:00 +00002170 struct symbol *lhs; /* Left-hand side of current rule */
icculus9e44cf12010-02-14 17:14:22 +00002171 const char *lhsalias; /* Alias for the LHS */
drh75897232000-05-29 14:26:00 +00002172 int nrhs; /* Number of right-hand side symbols seen */
2173 struct symbol *rhs[MAXRHS]; /* RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +00002174 const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
drh75897232000-05-29 14:26:00 +00002175 struct rule *prevrule; /* Previous rule parsed */
icculus9e44cf12010-02-14 17:14:22 +00002176 const char *declkeyword; /* Keyword of a declaration */
drh75897232000-05-29 14:26:00 +00002177 char **declargslot; /* Where the declaration argument should be put */
drha5808f32008-04-27 22:19:44 +00002178 int insertLineMacro; /* Add #line before declaration insert */
drh4dc8ef52008-07-01 17:13:57 +00002179 int *decllinenoslot; /* Where to write declaration line number */
drh75897232000-05-29 14:26:00 +00002180 enum e_assoc declassoc; /* Assign this association to decl arguments */
2181 int preccounter; /* Assign this precedence to decl arguments */
2182 struct rule *firstrule; /* Pointer to first rule in the grammar */
2183 struct rule *lastrule; /* Pointer to the most recently parsed rule */
2184};
2185
2186/* Parse a single token */
icculus9e44cf12010-02-14 17:14:22 +00002187static void parseonetoken(struct pstate *psp)
drh75897232000-05-29 14:26:00 +00002188{
icculus9e44cf12010-02-14 17:14:22 +00002189 const char *x;
drh75897232000-05-29 14:26:00 +00002190 x = Strsafe(psp->tokenstart); /* Save the token permanently */
2191#if 0
2192 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
2193 x,psp->state);
2194#endif
2195 switch( psp->state ){
2196 case INITIALIZE:
2197 psp->prevrule = 0;
2198 psp->preccounter = 0;
2199 psp->firstrule = psp->lastrule = 0;
2200 psp->gp->nrule = 0;
2201 /* Fall thru to next case */
2202 case WAITING_FOR_DECL_OR_RULE:
2203 if( x[0]=='%' ){
2204 psp->state = WAITING_FOR_DECL_KEYWORD;
drhc56fac72015-10-29 13:48:15 +00002205 }else if( ISLOWER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002206 psp->lhs = Symbol_new(x);
2207 psp->nrhs = 0;
2208 psp->lhsalias = 0;
2209 psp->state = WAITING_FOR_ARROW;
2210 }else if( x[0]=='{' ){
2211 if( psp->prevrule==0 ){
2212 ErrorMsg(psp->filename,psp->tokenlineno,
drh3cb2f6e2012-01-09 14:19:05 +00002213"There is no prior rule upon which to attach the code \
drh75897232000-05-29 14:26:00 +00002214fragment which begins on this line.");
2215 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002216 }else if( psp->prevrule->code!=0 ){
drh75897232000-05-29 14:26:00 +00002217 ErrorMsg(psp->filename,psp->tokenlineno,
2218"Code fragment beginning on this line is not the first \
2219to follow the previous rule.");
2220 psp->errorcnt++;
2221 }else{
2222 psp->prevrule->line = psp->tokenlineno;
2223 psp->prevrule->code = &x[1];
drh711c9812016-05-23 14:24:31 +00002224 psp->prevrule->noCode = 0;
drhf2f105d2012-08-20 15:53:54 +00002225 }
drh75897232000-05-29 14:26:00 +00002226 }else if( x[0]=='[' ){
2227 psp->state = PRECEDENCE_MARK_1;
2228 }else{
2229 ErrorMsg(psp->filename,psp->tokenlineno,
2230 "Token \"%s\" should be either \"%%\" or a nonterminal name.",
2231 x);
2232 psp->errorcnt++;
2233 }
2234 break;
2235 case PRECEDENCE_MARK_1:
drhc56fac72015-10-29 13:48:15 +00002236 if( !ISUPPER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002237 ErrorMsg(psp->filename,psp->tokenlineno,
2238 "The precedence symbol must be a terminal.");
2239 psp->errorcnt++;
2240 }else if( psp->prevrule==0 ){
2241 ErrorMsg(psp->filename,psp->tokenlineno,
2242 "There is no prior rule to assign precedence \"[%s]\".",x);
2243 psp->errorcnt++;
2244 }else if( psp->prevrule->precsym!=0 ){
2245 ErrorMsg(psp->filename,psp->tokenlineno,
2246"Precedence mark on this line is not the first \
2247to follow the previous rule.");
2248 psp->errorcnt++;
2249 }else{
2250 psp->prevrule->precsym = Symbol_new(x);
2251 }
2252 psp->state = PRECEDENCE_MARK_2;
2253 break;
2254 case PRECEDENCE_MARK_2:
2255 if( x[0]!=']' ){
2256 ErrorMsg(psp->filename,psp->tokenlineno,
2257 "Missing \"]\" on precedence mark.");
2258 psp->errorcnt++;
2259 }
2260 psp->state = WAITING_FOR_DECL_OR_RULE;
2261 break;
2262 case WAITING_FOR_ARROW:
2263 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2264 psp->state = IN_RHS;
2265 }else if( x[0]=='(' ){
2266 psp->state = LHS_ALIAS_1;
2267 }else{
2268 ErrorMsg(psp->filename,psp->tokenlineno,
2269 "Expected to see a \":\" following the LHS symbol \"%s\".",
2270 psp->lhs->name);
2271 psp->errorcnt++;
2272 psp->state = RESYNC_AFTER_RULE_ERROR;
2273 }
2274 break;
2275 case LHS_ALIAS_1:
drhc56fac72015-10-29 13:48:15 +00002276 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002277 psp->lhsalias = x;
2278 psp->state = LHS_ALIAS_2;
2279 }else{
2280 ErrorMsg(psp->filename,psp->tokenlineno,
2281 "\"%s\" is not a valid alias for the LHS \"%s\"\n",
2282 x,psp->lhs->name);
2283 psp->errorcnt++;
2284 psp->state = RESYNC_AFTER_RULE_ERROR;
2285 }
2286 break;
2287 case LHS_ALIAS_2:
2288 if( x[0]==')' ){
2289 psp->state = LHS_ALIAS_3;
2290 }else{
2291 ErrorMsg(psp->filename,psp->tokenlineno,
2292 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2293 psp->errorcnt++;
2294 psp->state = RESYNC_AFTER_RULE_ERROR;
2295 }
2296 break;
2297 case LHS_ALIAS_3:
2298 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2299 psp->state = IN_RHS;
2300 }else{
2301 ErrorMsg(psp->filename,psp->tokenlineno,
2302 "Missing \"->\" following: \"%s(%s)\".",
2303 psp->lhs->name,psp->lhsalias);
2304 psp->errorcnt++;
2305 psp->state = RESYNC_AFTER_RULE_ERROR;
2306 }
2307 break;
2308 case IN_RHS:
2309 if( x[0]=='.' ){
2310 struct rule *rp;
drh06f60d82017-04-14 19:46:12 +00002311 rp = (struct rule *)calloc( sizeof(struct rule) +
drh9892c5d2007-12-21 00:02:11 +00002312 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
drh75897232000-05-29 14:26:00 +00002313 if( rp==0 ){
2314 ErrorMsg(psp->filename,psp->tokenlineno,
2315 "Can't allocate enough memory for this rule.");
2316 psp->errorcnt++;
2317 psp->prevrule = 0;
drhf2f105d2012-08-20 15:53:54 +00002318 }else{
drh75897232000-05-29 14:26:00 +00002319 int i;
2320 rp->ruleline = psp->tokenlineno;
2321 rp->rhs = (struct symbol**)&rp[1];
icculus9e44cf12010-02-14 17:14:22 +00002322 rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
drh75897232000-05-29 14:26:00 +00002323 for(i=0; i<psp->nrhs; i++){
2324 rp->rhs[i] = psp->rhs[i];
2325 rp->rhsalias[i] = psp->alias[i];
drhf2f105d2012-08-20 15:53:54 +00002326 }
drh75897232000-05-29 14:26:00 +00002327 rp->lhs = psp->lhs;
2328 rp->lhsalias = psp->lhsalias;
2329 rp->nrhs = psp->nrhs;
2330 rp->code = 0;
drh711c9812016-05-23 14:24:31 +00002331 rp->noCode = 1;
drh75897232000-05-29 14:26:00 +00002332 rp->precsym = 0;
2333 rp->index = psp->gp->nrule++;
2334 rp->nextlhs = rp->lhs->rule;
2335 rp->lhs->rule = rp;
2336 rp->next = 0;
2337 if( psp->firstrule==0 ){
2338 psp->firstrule = psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002339 }else{
drh75897232000-05-29 14:26:00 +00002340 psp->lastrule->next = rp;
2341 psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002342 }
drh75897232000-05-29 14:26:00 +00002343 psp->prevrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002344 }
drh75897232000-05-29 14:26:00 +00002345 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002346 }else if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002347 if( psp->nrhs>=MAXRHS ){
2348 ErrorMsg(psp->filename,psp->tokenlineno,
drhc4dd3fd2008-01-22 01:48:05 +00002349 "Too many symbols on RHS of rule beginning at \"%s\".",
drh75897232000-05-29 14:26:00 +00002350 x);
2351 psp->errorcnt++;
2352 psp->state = RESYNC_AFTER_RULE_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002353 }else{
drh75897232000-05-29 14:26:00 +00002354 psp->rhs[psp->nrhs] = Symbol_new(x);
2355 psp->alias[psp->nrhs] = 0;
2356 psp->nrhs++;
drhf2f105d2012-08-20 15:53:54 +00002357 }
drhfd405312005-11-06 04:06:59 +00002358 }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
2359 struct symbol *msp = psp->rhs[psp->nrhs-1];
2360 if( msp->type!=MULTITERMINAL ){
2361 struct symbol *origsp = msp;
icculus9e44cf12010-02-14 17:14:22 +00002362 msp = (struct symbol *) calloc(1,sizeof(*msp));
drhfd405312005-11-06 04:06:59 +00002363 memset(msp, 0, sizeof(*msp));
2364 msp->type = MULTITERMINAL;
2365 msp->nsubsym = 1;
icculus9e44cf12010-02-14 17:14:22 +00002366 msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*));
drhfd405312005-11-06 04:06:59 +00002367 msp->subsym[0] = origsp;
2368 msp->name = origsp->name;
2369 psp->rhs[psp->nrhs-1] = msp;
2370 }
2371 msp->nsubsym++;
icculus9e44cf12010-02-14 17:14:22 +00002372 msp->subsym = (struct symbol **) realloc(msp->subsym,
2373 sizeof(struct symbol*)*msp->nsubsym);
drhfd405312005-11-06 04:06:59 +00002374 msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
drhc56fac72015-10-29 13:48:15 +00002375 if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){
drhfd405312005-11-06 04:06:59 +00002376 ErrorMsg(psp->filename,psp->tokenlineno,
2377 "Cannot form a compound containing a non-terminal");
2378 psp->errorcnt++;
2379 }
drh75897232000-05-29 14:26:00 +00002380 }else if( x[0]=='(' && psp->nrhs>0 ){
2381 psp->state = RHS_ALIAS_1;
2382 }else{
2383 ErrorMsg(psp->filename,psp->tokenlineno,
2384 "Illegal character on RHS of rule: \"%s\".",x);
2385 psp->errorcnt++;
2386 psp->state = RESYNC_AFTER_RULE_ERROR;
2387 }
2388 break;
2389 case RHS_ALIAS_1:
drhc56fac72015-10-29 13:48:15 +00002390 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002391 psp->alias[psp->nrhs-1] = x;
2392 psp->state = RHS_ALIAS_2;
2393 }else{
2394 ErrorMsg(psp->filename,psp->tokenlineno,
2395 "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
2396 x,psp->rhs[psp->nrhs-1]->name);
2397 psp->errorcnt++;
2398 psp->state = RESYNC_AFTER_RULE_ERROR;
2399 }
2400 break;
2401 case RHS_ALIAS_2:
2402 if( x[0]==')' ){
2403 psp->state = IN_RHS;
2404 }else{
2405 ErrorMsg(psp->filename,psp->tokenlineno,
2406 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2407 psp->errorcnt++;
2408 psp->state = RESYNC_AFTER_RULE_ERROR;
2409 }
2410 break;
2411 case WAITING_FOR_DECL_KEYWORD:
drhc56fac72015-10-29 13:48:15 +00002412 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002413 psp->declkeyword = x;
2414 psp->declargslot = 0;
drh4dc8ef52008-07-01 17:13:57 +00002415 psp->decllinenoslot = 0;
drha5808f32008-04-27 22:19:44 +00002416 psp->insertLineMacro = 1;
drh75897232000-05-29 14:26:00 +00002417 psp->state = WAITING_FOR_DECL_ARG;
2418 if( strcmp(x,"name")==0 ){
2419 psp->declargslot = &(psp->gp->name);
drha5808f32008-04-27 22:19:44 +00002420 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002421 }else if( strcmp(x,"include")==0 ){
drh75897232000-05-29 14:26:00 +00002422 psp->declargslot = &(psp->gp->include);
drhf2f105d2012-08-20 15:53:54 +00002423 }else if( strcmp(x,"code")==0 ){
drh75897232000-05-29 14:26:00 +00002424 psp->declargslot = &(psp->gp->extracode);
drhf2f105d2012-08-20 15:53:54 +00002425 }else if( strcmp(x,"token_destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002426 psp->declargslot = &psp->gp->tokendest;
drhf2f105d2012-08-20 15:53:54 +00002427 }else if( strcmp(x,"default_destructor")==0 ){
drh960e8c62001-04-03 16:53:21 +00002428 psp->declargslot = &psp->gp->vardest;
drhf2f105d2012-08-20 15:53:54 +00002429 }else if( strcmp(x,"token_prefix")==0 ){
drh75897232000-05-29 14:26:00 +00002430 psp->declargslot = &psp->gp->tokenprefix;
drha5808f32008-04-27 22:19:44 +00002431 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002432 }else if( strcmp(x,"syntax_error")==0 ){
drh75897232000-05-29 14:26:00 +00002433 psp->declargslot = &(psp->gp->error);
drhf2f105d2012-08-20 15:53:54 +00002434 }else if( strcmp(x,"parse_accept")==0 ){
drh75897232000-05-29 14:26:00 +00002435 psp->declargslot = &(psp->gp->accept);
drhf2f105d2012-08-20 15:53:54 +00002436 }else if( strcmp(x,"parse_failure")==0 ){
drh75897232000-05-29 14:26:00 +00002437 psp->declargslot = &(psp->gp->failure);
drhf2f105d2012-08-20 15:53:54 +00002438 }else if( strcmp(x,"stack_overflow")==0 ){
drh75897232000-05-29 14:26:00 +00002439 psp->declargslot = &(psp->gp->overflow);
drh75897232000-05-29 14:26:00 +00002440 }else if( strcmp(x,"extra_argument")==0 ){
2441 psp->declargslot = &(psp->gp->arg);
drha5808f32008-04-27 22:19:44 +00002442 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002443 }else if( strcmp(x,"token_type")==0 ){
2444 psp->declargslot = &(psp->gp->tokentype);
drha5808f32008-04-27 22:19:44 +00002445 psp->insertLineMacro = 0;
drh960e8c62001-04-03 16:53:21 +00002446 }else if( strcmp(x,"default_type")==0 ){
2447 psp->declargslot = &(psp->gp->vartype);
drha5808f32008-04-27 22:19:44 +00002448 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002449 }else if( strcmp(x,"stack_size")==0 ){
2450 psp->declargslot = &(psp->gp->stacksize);
drha5808f32008-04-27 22:19:44 +00002451 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002452 }else if( strcmp(x,"start_symbol")==0 ){
2453 psp->declargslot = &(psp->gp->start);
drha5808f32008-04-27 22:19:44 +00002454 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002455 }else if( strcmp(x,"left")==0 ){
2456 psp->preccounter++;
2457 psp->declassoc = LEFT;
2458 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2459 }else if( strcmp(x,"right")==0 ){
2460 psp->preccounter++;
2461 psp->declassoc = RIGHT;
2462 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2463 }else if( strcmp(x,"nonassoc")==0 ){
2464 psp->preccounter++;
2465 psp->declassoc = NONE;
2466 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002467 }else if( strcmp(x,"destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002468 psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002469 }else if( strcmp(x,"type")==0 ){
drh75897232000-05-29 14:26:00 +00002470 psp->state = WAITING_FOR_DATATYPE_SYMBOL;
drh0bd1f4e2002-06-06 18:54:39 +00002471 }else if( strcmp(x,"fallback")==0 ){
2472 psp->fallback = 0;
2473 psp->state = WAITING_FOR_FALLBACK_ID;
drh59c435a2017-08-02 03:21:11 +00002474 }else if( strcmp(x,"token")==0 ){
2475 psp->state = WAITING_FOR_TOKEN_NAME;
drhe09daa92006-06-10 13:29:31 +00002476 }else if( strcmp(x,"wildcard")==0 ){
2477 psp->state = WAITING_FOR_WILDCARD_ID;
drh61f92cd2014-01-11 03:06:18 +00002478 }else if( strcmp(x,"token_class")==0 ){
2479 psp->state = WAITING_FOR_CLASS_ID;
drh75897232000-05-29 14:26:00 +00002480 }else{
2481 ErrorMsg(psp->filename,psp->tokenlineno,
2482 "Unknown declaration keyword: \"%%%s\".",x);
2483 psp->errorcnt++;
2484 psp->state = RESYNC_AFTER_DECL_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002485 }
drh75897232000-05-29 14:26:00 +00002486 }else{
2487 ErrorMsg(psp->filename,psp->tokenlineno,
2488 "Illegal declaration keyword: \"%s\".",x);
2489 psp->errorcnt++;
2490 psp->state = RESYNC_AFTER_DECL_ERROR;
2491 }
2492 break;
2493 case WAITING_FOR_DESTRUCTOR_SYMBOL:
drhc56fac72015-10-29 13:48:15 +00002494 if( !ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002495 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002496 "Symbol name missing after %%destructor keyword");
drh75897232000-05-29 14:26:00 +00002497 psp->errorcnt++;
2498 psp->state = RESYNC_AFTER_DECL_ERROR;
2499 }else{
icculusd286fa62010-03-03 17:06:32 +00002500 struct symbol *sp = Symbol_new(x);
2501 psp->declargslot = &sp->destructor;
2502 psp->decllinenoslot = &sp->destLineno;
2503 psp->insertLineMacro = 1;
2504 psp->state = WAITING_FOR_DECL_ARG;
drh75897232000-05-29 14:26:00 +00002505 }
2506 break;
2507 case WAITING_FOR_DATATYPE_SYMBOL:
drhc56fac72015-10-29 13:48:15 +00002508 if( !ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002509 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002510 "Symbol name missing after %%type keyword");
drh75897232000-05-29 14:26:00 +00002511 psp->errorcnt++;
2512 psp->state = RESYNC_AFTER_DECL_ERROR;
2513 }else{
icculus866bf1e2010-02-17 20:31:32 +00002514 struct symbol *sp = Symbol_find(x);
2515 if((sp) && (sp->datatype)){
2516 ErrorMsg(psp->filename,psp->tokenlineno,
2517 "Symbol %%type \"%s\" already defined", x);
2518 psp->errorcnt++;
2519 psp->state = RESYNC_AFTER_DECL_ERROR;
2520 }else{
2521 if (!sp){
2522 sp = Symbol_new(x);
2523 }
2524 psp->declargslot = &sp->datatype;
2525 psp->insertLineMacro = 0;
2526 psp->state = WAITING_FOR_DECL_ARG;
2527 }
drh75897232000-05-29 14:26:00 +00002528 }
2529 break;
2530 case WAITING_FOR_PRECEDENCE_SYMBOL:
2531 if( x[0]=='.' ){
2532 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002533 }else if( ISUPPER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002534 struct symbol *sp;
2535 sp = Symbol_new(x);
2536 if( sp->prec>=0 ){
2537 ErrorMsg(psp->filename,psp->tokenlineno,
2538 "Symbol \"%s\" has already be given a precedence.",x);
2539 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002540 }else{
drh75897232000-05-29 14:26:00 +00002541 sp->prec = psp->preccounter;
2542 sp->assoc = psp->declassoc;
drhf2f105d2012-08-20 15:53:54 +00002543 }
drh75897232000-05-29 14:26:00 +00002544 }else{
2545 ErrorMsg(psp->filename,psp->tokenlineno,
2546 "Can't assign a precedence to \"%s\".",x);
2547 psp->errorcnt++;
2548 }
2549 break;
2550 case WAITING_FOR_DECL_ARG:
drhc56fac72015-10-29 13:48:15 +00002551 if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){
icculus9e44cf12010-02-14 17:14:22 +00002552 const char *zOld, *zNew;
2553 char *zBuf, *z;
mistachkin2318d332015-01-12 18:02:52 +00002554 int nOld, n, nLine = 0, nNew, nBack;
drhb5bd49e2008-07-14 12:21:08 +00002555 int addLineMacro;
drha5808f32008-04-27 22:19:44 +00002556 char zLine[50];
2557 zNew = x;
2558 if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
drh87cf1372008-08-13 20:09:06 +00002559 nNew = lemonStrlen(zNew);
drha5808f32008-04-27 22:19:44 +00002560 if( *psp->declargslot ){
2561 zOld = *psp->declargslot;
2562 }else{
2563 zOld = "";
2564 }
drh87cf1372008-08-13 20:09:06 +00002565 nOld = lemonStrlen(zOld);
drha5808f32008-04-27 22:19:44 +00002566 n = nOld + nNew + 20;
shane58543932008-12-10 20:10:04 +00002567 addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro &&
drhb5bd49e2008-07-14 12:21:08 +00002568 (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
2569 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002570 for(z=psp->filename, nBack=0; *z; z++){
2571 if( *z=='\\' ) nBack++;
2572 }
drh898799f2014-01-10 23:21:00 +00002573 lemon_sprintf(zLine, "#line %d ", psp->tokenlineno);
drh87cf1372008-08-13 20:09:06 +00002574 nLine = lemonStrlen(zLine);
2575 n += nLine + lemonStrlen(psp->filename) + nBack;
drha5808f32008-04-27 22:19:44 +00002576 }
icculus9e44cf12010-02-14 17:14:22 +00002577 *psp->declargslot = (char *) realloc(*psp->declargslot, n);
2578 zBuf = *psp->declargslot + nOld;
drhb5bd49e2008-07-14 12:21:08 +00002579 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002580 if( nOld && zBuf[-1]!='\n' ){
2581 *(zBuf++) = '\n';
2582 }
2583 memcpy(zBuf, zLine, nLine);
2584 zBuf += nLine;
2585 *(zBuf++) = '"';
2586 for(z=psp->filename; *z; z++){
2587 if( *z=='\\' ){
2588 *(zBuf++) = '\\';
2589 }
2590 *(zBuf++) = *z;
2591 }
2592 *(zBuf++) = '"';
2593 *(zBuf++) = '\n';
2594 }
drh4dc8ef52008-07-01 17:13:57 +00002595 if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){
2596 psp->decllinenoslot[0] = psp->tokenlineno;
2597 }
drha5808f32008-04-27 22:19:44 +00002598 memcpy(zBuf, zNew, nNew);
2599 zBuf += nNew;
2600 *zBuf = 0;
2601 psp->state = WAITING_FOR_DECL_OR_RULE;
drh75897232000-05-29 14:26:00 +00002602 }else{
2603 ErrorMsg(psp->filename,psp->tokenlineno,
2604 "Illegal argument to %%%s: %s",psp->declkeyword,x);
2605 psp->errorcnt++;
2606 psp->state = RESYNC_AFTER_DECL_ERROR;
2607 }
2608 break;
drh0bd1f4e2002-06-06 18:54:39 +00002609 case WAITING_FOR_FALLBACK_ID:
2610 if( x[0]=='.' ){
2611 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002612 }else if( !ISUPPER(x[0]) ){
drh0bd1f4e2002-06-06 18:54:39 +00002613 ErrorMsg(psp->filename, psp->tokenlineno,
2614 "%%fallback argument \"%s\" should be a token", x);
2615 psp->errorcnt++;
2616 }else{
2617 struct symbol *sp = Symbol_new(x);
2618 if( psp->fallback==0 ){
2619 psp->fallback = sp;
2620 }else if( sp->fallback ){
2621 ErrorMsg(psp->filename, psp->tokenlineno,
2622 "More than one fallback assigned to token %s", x);
2623 psp->errorcnt++;
2624 }else{
2625 sp->fallback = psp->fallback;
2626 psp->gp->has_fallback = 1;
2627 }
2628 }
2629 break;
drh59c435a2017-08-02 03:21:11 +00002630 case WAITING_FOR_TOKEN_NAME:
2631 /* Tokens do not have to be declared before use. But they can be
2632 ** in order to control their assigned integer number. The number for
2633 ** each token is assigned when it is first seen. So by including
2634 **
2635 ** %token ONE TWO THREE
2636 **
2637 ** early in the grammar file, that assigns small consecutive values
2638 ** to each of the tokens ONE TWO and THREE.
2639 */
2640 if( x[0]=='.' ){
2641 psp->state = WAITING_FOR_DECL_OR_RULE;
2642 }else if( !ISUPPER(x[0]) ){
2643 ErrorMsg(psp->filename, psp->tokenlineno,
2644 "%%token argument \"%s\" should be a token", x);
2645 psp->errorcnt++;
2646 }else{
2647 (void)Symbol_new(x);
2648 }
2649 break;
drhe09daa92006-06-10 13:29:31 +00002650 case WAITING_FOR_WILDCARD_ID:
2651 if( x[0]=='.' ){
2652 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002653 }else if( !ISUPPER(x[0]) ){
drhe09daa92006-06-10 13:29:31 +00002654 ErrorMsg(psp->filename, psp->tokenlineno,
2655 "%%wildcard argument \"%s\" should be a token", x);
2656 psp->errorcnt++;
2657 }else{
2658 struct symbol *sp = Symbol_new(x);
2659 if( psp->gp->wildcard==0 ){
2660 psp->gp->wildcard = sp;
2661 }else{
2662 ErrorMsg(psp->filename, psp->tokenlineno,
2663 "Extra wildcard to token: %s", x);
2664 psp->errorcnt++;
2665 }
2666 }
2667 break;
drh61f92cd2014-01-11 03:06:18 +00002668 case WAITING_FOR_CLASS_ID:
drhc56fac72015-10-29 13:48:15 +00002669 if( !ISLOWER(x[0]) ){
drh61f92cd2014-01-11 03:06:18 +00002670 ErrorMsg(psp->filename, psp->tokenlineno,
2671 "%%token_class must be followed by an identifier: ", x);
2672 psp->errorcnt++;
2673 psp->state = RESYNC_AFTER_DECL_ERROR;
2674 }else if( Symbol_find(x) ){
2675 ErrorMsg(psp->filename, psp->tokenlineno,
2676 "Symbol \"%s\" already used", x);
2677 psp->errorcnt++;
2678 psp->state = RESYNC_AFTER_DECL_ERROR;
2679 }else{
2680 psp->tkclass = Symbol_new(x);
2681 psp->tkclass->type = MULTITERMINAL;
2682 psp->state = WAITING_FOR_CLASS_TOKEN;
2683 }
2684 break;
2685 case WAITING_FOR_CLASS_TOKEN:
2686 if( x[0]=='.' ){
2687 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002688 }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){
drh61f92cd2014-01-11 03:06:18 +00002689 struct symbol *msp = psp->tkclass;
2690 msp->nsubsym++;
2691 msp->subsym = (struct symbol **) realloc(msp->subsym,
2692 sizeof(struct symbol*)*msp->nsubsym);
drhc56fac72015-10-29 13:48:15 +00002693 if( !ISUPPER(x[0]) ) x++;
drh61f92cd2014-01-11 03:06:18 +00002694 msp->subsym[msp->nsubsym-1] = Symbol_new(x);
2695 }else{
2696 ErrorMsg(psp->filename, psp->tokenlineno,
2697 "%%token_class argument \"%s\" should be a token", x);
2698 psp->errorcnt++;
2699 psp->state = RESYNC_AFTER_DECL_ERROR;
2700 }
2701 break;
drh75897232000-05-29 14:26:00 +00002702 case RESYNC_AFTER_RULE_ERROR:
2703/* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2704** break; */
2705 case RESYNC_AFTER_DECL_ERROR:
2706 if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2707 if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
2708 break;
2709 }
2710}
2711
drh34ff57b2008-07-14 12:27:51 +00002712/* Run the preprocessor over the input file text. The global variables
drh6d08b4d2004-07-20 12:45:22 +00002713** azDefine[0] through azDefine[nDefine-1] contains the names of all defined
2714** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and
2715** comments them out. Text in between is also commented out as appropriate.
2716*/
danielk1977940fac92005-01-23 22:41:37 +00002717static void preprocess_input(char *z){
drh6d08b4d2004-07-20 12:45:22 +00002718 int i, j, k, n;
2719 int exclude = 0;
rse38514a92007-09-20 11:34:17 +00002720 int start = 0;
drh6d08b4d2004-07-20 12:45:22 +00002721 int lineno = 1;
rse38514a92007-09-20 11:34:17 +00002722 int start_lineno = 1;
drh6d08b4d2004-07-20 12:45:22 +00002723 for(i=0; z[i]; i++){
2724 if( z[i]=='\n' ) lineno++;
2725 if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue;
drhc56fac72015-10-29 13:48:15 +00002726 if( strncmp(&z[i],"%endif",6)==0 && ISSPACE(z[i+6]) ){
drh6d08b4d2004-07-20 12:45:22 +00002727 if( exclude ){
2728 exclude--;
2729 if( exclude==0 ){
2730 for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
2731 }
2732 }
2733 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
drhc56fac72015-10-29 13:48:15 +00002734 }else if( (strncmp(&z[i],"%ifdef",6)==0 && ISSPACE(z[i+6]))
2735 || (strncmp(&z[i],"%ifndef",7)==0 && ISSPACE(z[i+7])) ){
drh6d08b4d2004-07-20 12:45:22 +00002736 if( exclude ){
2737 exclude++;
2738 }else{
drhc56fac72015-10-29 13:48:15 +00002739 for(j=i+7; ISSPACE(z[j]); j++){}
2740 for(n=0; z[j+n] && !ISSPACE(z[j+n]); n++){}
drh6d08b4d2004-07-20 12:45:22 +00002741 exclude = 1;
2742 for(k=0; k<nDefine; k++){
drh87cf1372008-08-13 20:09:06 +00002743 if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){
drh6d08b4d2004-07-20 12:45:22 +00002744 exclude = 0;
2745 break;
2746 }
2747 }
2748 if( z[i+3]=='n' ) exclude = !exclude;
2749 if( exclude ){
2750 start = i;
2751 start_lineno = lineno;
2752 }
2753 }
2754 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2755 }
2756 }
2757 if( exclude ){
2758 fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
2759 exit(1);
2760 }
2761}
2762
drh75897232000-05-29 14:26:00 +00002763/* In spite of its name, this function is really a scanner. It read
2764** in the entire input file (all at once) then tokenizes it. Each
2765** token is passed to the function "parseonetoken" which builds all
2766** the appropriate data structures in the global state vector "gp".
2767*/
icculus9e44cf12010-02-14 17:14:22 +00002768void Parse(struct lemon *gp)
drh75897232000-05-29 14:26:00 +00002769{
2770 struct pstate ps;
2771 FILE *fp;
2772 char *filebuf;
mistachkin2318d332015-01-12 18:02:52 +00002773 unsigned int filesize;
drh75897232000-05-29 14:26:00 +00002774 int lineno;
2775 int c;
2776 char *cp, *nextcp;
2777 int startline = 0;
2778
rse38514a92007-09-20 11:34:17 +00002779 memset(&ps, '\0', sizeof(ps));
drh75897232000-05-29 14:26:00 +00002780 ps.gp = gp;
2781 ps.filename = gp->filename;
2782 ps.errorcnt = 0;
2783 ps.state = INITIALIZE;
2784
2785 /* Begin by reading the input file */
2786 fp = fopen(ps.filename,"rb");
2787 if( fp==0 ){
2788 ErrorMsg(ps.filename,0,"Can't open this file for reading.");
2789 gp->errorcnt++;
2790 return;
2791 }
2792 fseek(fp,0,2);
2793 filesize = ftell(fp);
2794 rewind(fp);
2795 filebuf = (char *)malloc( filesize+1 );
drh03e1b1f2014-01-11 12:52:25 +00002796 if( filesize>100000000 || filebuf==0 ){
2797 ErrorMsg(ps.filename,0,"Input file too large.");
drh75897232000-05-29 14:26:00 +00002798 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002799 fclose(fp);
drh75897232000-05-29 14:26:00 +00002800 return;
2801 }
2802 if( fread(filebuf,1,filesize,fp)!=filesize ){
2803 ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
2804 filesize);
2805 free(filebuf);
2806 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002807 fclose(fp);
drh75897232000-05-29 14:26:00 +00002808 return;
2809 }
2810 fclose(fp);
2811 filebuf[filesize] = 0;
2812
drh6d08b4d2004-07-20 12:45:22 +00002813 /* Make an initial pass through the file to handle %ifdef and %ifndef */
2814 preprocess_input(filebuf);
2815
drh75897232000-05-29 14:26:00 +00002816 /* Now scan the text of the input file */
2817 lineno = 1;
2818 for(cp=filebuf; (c= *cp)!=0; ){
2819 if( c=='\n' ) lineno++; /* Keep track of the line number */
drhc56fac72015-10-29 13:48:15 +00002820 if( ISSPACE(c) ){ cp++; continue; } /* Skip all white space */
drh75897232000-05-29 14:26:00 +00002821 if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
2822 cp+=2;
2823 while( (c= *cp)!=0 && c!='\n' ) cp++;
2824 continue;
2825 }
2826 if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
2827 cp+=2;
2828 while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
2829 if( c=='\n' ) lineno++;
2830 cp++;
2831 }
2832 if( c ) cp++;
2833 continue;
2834 }
2835 ps.tokenstart = cp; /* Mark the beginning of the token */
2836 ps.tokenlineno = lineno; /* Linenumber on which token begins */
2837 if( c=='\"' ){ /* String literals */
2838 cp++;
2839 while( (c= *cp)!=0 && c!='\"' ){
2840 if( c=='\n' ) lineno++;
2841 cp++;
2842 }
2843 if( c==0 ){
2844 ErrorMsg(ps.filename,startline,
2845"String starting on this line is not terminated before the end of the file.");
2846 ps.errorcnt++;
2847 nextcp = cp;
2848 }else{
2849 nextcp = cp+1;
2850 }
2851 }else if( c=='{' ){ /* A block of C code */
2852 int level;
2853 cp++;
2854 for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
2855 if( c=='\n' ) lineno++;
2856 else if( c=='{' ) level++;
2857 else if( c=='}' ) level--;
2858 else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
2859 int prevc;
2860 cp = &cp[2];
2861 prevc = 0;
2862 while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
2863 if( c=='\n' ) lineno++;
2864 prevc = c;
2865 cp++;
drhf2f105d2012-08-20 15:53:54 +00002866 }
2867 }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
drh75897232000-05-29 14:26:00 +00002868 cp = &cp[2];
2869 while( (c= *cp)!=0 && c!='\n' ) cp++;
2870 if( c ) lineno++;
drhf2f105d2012-08-20 15:53:54 +00002871 }else if( c=='\'' || c=='\"' ){ /* String a character literals */
drh75897232000-05-29 14:26:00 +00002872 int startchar, prevc;
2873 startchar = c;
2874 prevc = 0;
2875 for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
2876 if( c=='\n' ) lineno++;
2877 if( prevc=='\\' ) prevc = 0;
2878 else prevc = c;
drhf2f105d2012-08-20 15:53:54 +00002879 }
2880 }
drh75897232000-05-29 14:26:00 +00002881 }
2882 if( c==0 ){
drh960e8c62001-04-03 16:53:21 +00002883 ErrorMsg(ps.filename,ps.tokenlineno,
drh75897232000-05-29 14:26:00 +00002884"C code starting on this line is not terminated before the end of the file.");
2885 ps.errorcnt++;
2886 nextcp = cp;
2887 }else{
2888 nextcp = cp+1;
2889 }
drhc56fac72015-10-29 13:48:15 +00002890 }else if( ISALNUM(c) ){ /* Identifiers */
2891 while( (c= *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
drh75897232000-05-29 14:26:00 +00002892 nextcp = cp;
2893 }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
2894 cp += 3;
2895 nextcp = cp;
drhc56fac72015-10-29 13:48:15 +00002896 }else if( (c=='/' || c=='|') && ISALPHA(cp[1]) ){
drhfd405312005-11-06 04:06:59 +00002897 cp += 2;
drhc56fac72015-10-29 13:48:15 +00002898 while( (c = *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
drhfd405312005-11-06 04:06:59 +00002899 nextcp = cp;
drh75897232000-05-29 14:26:00 +00002900 }else{ /* All other (one character) operators */
2901 cp++;
2902 nextcp = cp;
2903 }
2904 c = *cp;
2905 *cp = 0; /* Null terminate the token */
2906 parseonetoken(&ps); /* Parse the token */
mistachkin2318d332015-01-12 18:02:52 +00002907 *cp = (char)c; /* Restore the buffer */
drh75897232000-05-29 14:26:00 +00002908 cp = nextcp;
2909 }
2910 free(filebuf); /* Release the buffer after parsing */
2911 gp->rule = ps.firstrule;
2912 gp->errorcnt = ps.errorcnt;
2913}
2914/*************************** From the file "plink.c" *********************/
2915/*
2916** Routines processing configuration follow-set propagation links
2917** in the LEMON parser generator.
2918*/
2919static struct plink *plink_freelist = 0;
2920
2921/* Allocate a new plink */
drh14d88552017-04-14 19:44:15 +00002922struct plink *Plink_new(void){
icculus9e44cf12010-02-14 17:14:22 +00002923 struct plink *newlink;
drh75897232000-05-29 14:26:00 +00002924
2925 if( plink_freelist==0 ){
2926 int i;
2927 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +00002928 plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
drh75897232000-05-29 14:26:00 +00002929 if( plink_freelist==0 ){
2930 fprintf(stderr,
2931 "Unable to allocate memory for a new follow-set propagation link.\n");
2932 exit(1);
2933 }
2934 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2935 plink_freelist[amt-1].next = 0;
2936 }
icculus9e44cf12010-02-14 17:14:22 +00002937 newlink = plink_freelist;
drh75897232000-05-29 14:26:00 +00002938 plink_freelist = plink_freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00002939 return newlink;
drh75897232000-05-29 14:26:00 +00002940}
2941
2942/* Add a plink to a plink list */
icculus9e44cf12010-02-14 17:14:22 +00002943void Plink_add(struct plink **plpp, struct config *cfp)
drh75897232000-05-29 14:26:00 +00002944{
icculus9e44cf12010-02-14 17:14:22 +00002945 struct plink *newlink;
2946 newlink = Plink_new();
2947 newlink->next = *plpp;
2948 *plpp = newlink;
2949 newlink->cfp = cfp;
drh75897232000-05-29 14:26:00 +00002950}
2951
2952/* Transfer every plink on the list "from" to the list "to" */
icculus9e44cf12010-02-14 17:14:22 +00002953void Plink_copy(struct plink **to, struct plink *from)
drh75897232000-05-29 14:26:00 +00002954{
2955 struct plink *nextpl;
2956 while( from ){
2957 nextpl = from->next;
2958 from->next = *to;
2959 *to = from;
2960 from = nextpl;
2961 }
2962}
2963
2964/* Delete every plink on the list */
icculus9e44cf12010-02-14 17:14:22 +00002965void Plink_delete(struct plink *plp)
drh75897232000-05-29 14:26:00 +00002966{
2967 struct plink *nextpl;
2968
2969 while( plp ){
2970 nextpl = plp->next;
2971 plp->next = plink_freelist;
2972 plink_freelist = plp;
2973 plp = nextpl;
2974 }
2975}
2976/*********************** From the file "report.c" **************************/
2977/*
2978** Procedures for generating reports and tables in the LEMON parser generator.
2979*/
2980
2981/* Generate a filename with the given suffix. Space to hold the
2982** name comes from malloc() and must be freed by the calling
2983** function.
2984*/
icculus9e44cf12010-02-14 17:14:22 +00002985PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
drh75897232000-05-29 14:26:00 +00002986{
2987 char *name;
2988 char *cp;
2989
icculus9e44cf12010-02-14 17:14:22 +00002990 name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
drh75897232000-05-29 14:26:00 +00002991 if( name==0 ){
2992 fprintf(stderr,"Can't allocate space for a filename.\n");
2993 exit(1);
2994 }
drh898799f2014-01-10 23:21:00 +00002995 lemon_strcpy(name,lemp->filename);
drh75897232000-05-29 14:26:00 +00002996 cp = strrchr(name,'.');
2997 if( cp ) *cp = 0;
drh898799f2014-01-10 23:21:00 +00002998 lemon_strcat(name,suffix);
drh75897232000-05-29 14:26:00 +00002999 return name;
3000}
3001
3002/* Open a file with a name based on the name of the input file,
3003** but with a different (specified) suffix, and return a pointer
3004** to the stream */
icculus9e44cf12010-02-14 17:14:22 +00003005PRIVATE FILE *file_open(
3006 struct lemon *lemp,
3007 const char *suffix,
3008 const char *mode
3009){
drh75897232000-05-29 14:26:00 +00003010 FILE *fp;
3011
3012 if( lemp->outname ) free(lemp->outname);
3013 lemp->outname = file_makename(lemp, suffix);
3014 fp = fopen(lemp->outname,mode);
3015 if( fp==0 && *mode=='w' ){
3016 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
3017 lemp->errorcnt++;
3018 return 0;
3019 }
3020 return fp;
3021}
3022
drh06f60d82017-04-14 19:46:12 +00003023/* Duplicate the input file without comments and without actions
drh75897232000-05-29 14:26:00 +00003024** on rules */
icculus9e44cf12010-02-14 17:14:22 +00003025void Reprint(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003026{
3027 struct rule *rp;
3028 struct symbol *sp;
3029 int i, j, maxlen, len, ncolumns, skip;
3030 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
3031 maxlen = 10;
3032 for(i=0; i<lemp->nsymbol; i++){
3033 sp = lemp->symbols[i];
drh87cf1372008-08-13 20:09:06 +00003034 len = lemonStrlen(sp->name);
drh75897232000-05-29 14:26:00 +00003035 if( len>maxlen ) maxlen = len;
3036 }
3037 ncolumns = 76/(maxlen+5);
3038 if( ncolumns<1 ) ncolumns = 1;
3039 skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
3040 for(i=0; i<skip; i++){
3041 printf("//");
3042 for(j=i; j<lemp->nsymbol; j+=skip){
3043 sp = lemp->symbols[j];
3044 assert( sp->index==j );
3045 printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
3046 }
3047 printf("\n");
3048 }
3049 for(rp=lemp->rule; rp; rp=rp->next){
3050 printf("%s",rp->lhs->name);
drhfd405312005-11-06 04:06:59 +00003051 /* if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
drh75897232000-05-29 14:26:00 +00003052 printf(" ::=");
3053 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +00003054 sp = rp->rhs[i];
drhfd405312005-11-06 04:06:59 +00003055 if( sp->type==MULTITERMINAL ){
drh61f92cd2014-01-11 03:06:18 +00003056 printf(" %s", sp->subsym[0]->name);
drhfd405312005-11-06 04:06:59 +00003057 for(j=1; j<sp->nsubsym; j++){
3058 printf("|%s", sp->subsym[j]->name);
3059 }
drh61f92cd2014-01-11 03:06:18 +00003060 }else{
3061 printf(" %s", sp->name);
drhfd405312005-11-06 04:06:59 +00003062 }
3063 /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
drh75897232000-05-29 14:26:00 +00003064 }
3065 printf(".");
3066 if( rp->precsym ) printf(" [%s]",rp->precsym->name);
drhfd405312005-11-06 04:06:59 +00003067 /* if( rp->code ) printf("\n %s",rp->code); */
drh75897232000-05-29 14:26:00 +00003068 printf("\n");
3069 }
3070}
3071
drh7e698e92015-09-07 14:22:24 +00003072/* Print a single rule.
3073*/
3074void RulePrint(FILE *fp, struct rule *rp, int iCursor){
drhfd405312005-11-06 04:06:59 +00003075 struct symbol *sp;
3076 int i, j;
drh75897232000-05-29 14:26:00 +00003077 fprintf(fp,"%s ::=",rp->lhs->name);
3078 for(i=0; i<=rp->nrhs; i++){
drh7e698e92015-09-07 14:22:24 +00003079 if( i==iCursor ) fprintf(fp," *");
drh75897232000-05-29 14:26:00 +00003080 if( i==rp->nrhs ) break;
drhfd405312005-11-06 04:06:59 +00003081 sp = rp->rhs[i];
drhfd405312005-11-06 04:06:59 +00003082 if( sp->type==MULTITERMINAL ){
drh61f92cd2014-01-11 03:06:18 +00003083 fprintf(fp," %s", sp->subsym[0]->name);
drhfd405312005-11-06 04:06:59 +00003084 for(j=1; j<sp->nsubsym; j++){
3085 fprintf(fp,"|%s",sp->subsym[j]->name);
3086 }
drh61f92cd2014-01-11 03:06:18 +00003087 }else{
3088 fprintf(fp," %s", sp->name);
drhfd405312005-11-06 04:06:59 +00003089 }
drh75897232000-05-29 14:26:00 +00003090 }
3091}
3092
drh7e698e92015-09-07 14:22:24 +00003093/* Print the rule for a configuration.
3094*/
3095void ConfigPrint(FILE *fp, struct config *cfp){
3096 RulePrint(fp, cfp->rp, cfp->dot);
3097}
3098
drh75897232000-05-29 14:26:00 +00003099/* #define TEST */
drhfd405312005-11-06 04:06:59 +00003100#if 0
drh75897232000-05-29 14:26:00 +00003101/* Print a set */
3102PRIVATE void SetPrint(out,set,lemp)
3103FILE *out;
3104char *set;
3105struct lemon *lemp;
3106{
3107 int i;
3108 char *spacer;
3109 spacer = "";
3110 fprintf(out,"%12s[","");
3111 for(i=0; i<lemp->nterminal; i++){
3112 if( SetFind(set,i) ){
3113 fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
3114 spacer = " ";
3115 }
3116 }
3117 fprintf(out,"]\n");
3118}
3119
3120/* Print a plink chain */
3121PRIVATE void PlinkPrint(out,plp,tag)
3122FILE *out;
3123struct plink *plp;
3124char *tag;
3125{
3126 while( plp ){
drhada354d2005-11-05 15:03:59 +00003127 fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum);
drh75897232000-05-29 14:26:00 +00003128 ConfigPrint(out,plp->cfp);
3129 fprintf(out,"\n");
3130 plp = plp->next;
3131 }
3132}
3133#endif
3134
3135/* Print an action to the given file descriptor. Return FALSE if
3136** nothing was actually printed.
3137*/
drh7e698e92015-09-07 14:22:24 +00003138int PrintAction(
3139 struct action *ap, /* The action to print */
3140 FILE *fp, /* Print the action here */
drh3bd48ab2015-09-07 18:23:37 +00003141 int indent /* Indent by this amount */
drh7e698e92015-09-07 14:22:24 +00003142){
drh75897232000-05-29 14:26:00 +00003143 int result = 1;
3144 switch( ap->type ){
drh7e698e92015-09-07 14:22:24 +00003145 case SHIFT: {
3146 struct state *stp = ap->x.stp;
drh3bd48ab2015-09-07 18:23:37 +00003147 fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum);
drh75897232000-05-29 14:26:00 +00003148 break;
drh7e698e92015-09-07 14:22:24 +00003149 }
3150 case REDUCE: {
3151 struct rule *rp = ap->x.rp;
drh4ef07702016-03-16 19:45:54 +00003152 fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->iRule);
drh3bd48ab2015-09-07 18:23:37 +00003153 RulePrint(fp, rp, -1);
3154 break;
3155 }
3156 case SHIFTREDUCE: {
3157 struct rule *rp = ap->x.rp;
drh4ef07702016-03-16 19:45:54 +00003158 fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->iRule);
drh3bd48ab2015-09-07 18:23:37 +00003159 RulePrint(fp, rp, -1);
drh75897232000-05-29 14:26:00 +00003160 break;
drh7e698e92015-09-07 14:22:24 +00003161 }
drh75897232000-05-29 14:26:00 +00003162 case ACCEPT:
3163 fprintf(fp,"%*s accept",indent,ap->sp->name);
3164 break;
3165 case ERROR:
3166 fprintf(fp,"%*s error",indent,ap->sp->name);
3167 break;
drh9892c5d2007-12-21 00:02:11 +00003168 case SRCONFLICT:
3169 case RRCONFLICT:
drh3bd48ab2015-09-07 18:23:37 +00003170 fprintf(fp,"%*s reduce %-7d ** Parsing conflict **",
drh4ef07702016-03-16 19:45:54 +00003171 indent,ap->sp->name,ap->x.rp->iRule);
drh75897232000-05-29 14:26:00 +00003172 break;
drh9892c5d2007-12-21 00:02:11 +00003173 case SSCONFLICT:
drh06f60d82017-04-14 19:46:12 +00003174 fprintf(fp,"%*s shift %-7d ** Parsing conflict **",
drh9892c5d2007-12-21 00:02:11 +00003175 indent,ap->sp->name,ap->x.stp->statenum);
3176 break;
drh75897232000-05-29 14:26:00 +00003177 case SH_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00003178 if( showPrecedenceConflict ){
drh3bd48ab2015-09-07 18:23:37 +00003179 fprintf(fp,"%*s shift %-7d -- dropped by precedence",
drhf5c4e0f2010-07-18 11:35:53 +00003180 indent,ap->sp->name,ap->x.stp->statenum);
3181 }else{
3182 result = 0;
3183 }
3184 break;
drh75897232000-05-29 14:26:00 +00003185 case RD_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00003186 if( showPrecedenceConflict ){
drh7e698e92015-09-07 14:22:24 +00003187 fprintf(fp,"%*s reduce %-7d -- dropped by precedence",
drh4ef07702016-03-16 19:45:54 +00003188 indent,ap->sp->name,ap->x.rp->iRule);
drhf5c4e0f2010-07-18 11:35:53 +00003189 }else{
3190 result = 0;
3191 }
3192 break;
drh75897232000-05-29 14:26:00 +00003193 case NOT_USED:
3194 result = 0;
3195 break;
3196 }
drhc173ad82016-05-23 16:15:02 +00003197 if( result && ap->spOpt ){
3198 fprintf(fp," /* because %s==%s */", ap->sp->name, ap->spOpt->name);
3199 }
drh75897232000-05-29 14:26:00 +00003200 return result;
3201}
3202
drh3bd48ab2015-09-07 18:23:37 +00003203/* Generate the "*.out" log file */
icculus9e44cf12010-02-14 17:14:22 +00003204void ReportOutput(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003205{
3206 int i;
3207 struct state *stp;
3208 struct config *cfp;
3209 struct action *ap;
3210 FILE *fp;
3211
drh2aa6ca42004-09-10 00:14:04 +00003212 fp = file_open(lemp,".out","wb");
drh75897232000-05-29 14:26:00 +00003213 if( fp==0 ) return;
drh3bd48ab2015-09-07 18:23:37 +00003214 for(i=0; i<lemp->nxstate; i++){
drh75897232000-05-29 14:26:00 +00003215 stp = lemp->sorted[i];
drhada354d2005-11-05 15:03:59 +00003216 fprintf(fp,"State %d:\n",stp->statenum);
drh75897232000-05-29 14:26:00 +00003217 if( lemp->basisflag ) cfp=stp->bp;
3218 else cfp=stp->cfp;
3219 while( cfp ){
3220 char buf[20];
3221 if( cfp->dot==cfp->rp->nrhs ){
drh4ef07702016-03-16 19:45:54 +00003222 lemon_sprintf(buf,"(%d)",cfp->rp->iRule);
drh75897232000-05-29 14:26:00 +00003223 fprintf(fp," %5s ",buf);
3224 }else{
3225 fprintf(fp," ");
3226 }
3227 ConfigPrint(fp,cfp);
3228 fprintf(fp,"\n");
drhfd405312005-11-06 04:06:59 +00003229#if 0
drh75897232000-05-29 14:26:00 +00003230 SetPrint(fp,cfp->fws,lemp);
3231 PlinkPrint(fp,cfp->fplp,"To ");
3232 PlinkPrint(fp,cfp->bplp,"From");
3233#endif
3234 if( lemp->basisflag ) cfp=cfp->bp;
3235 else cfp=cfp->next;
3236 }
3237 fprintf(fp,"\n");
3238 for(ap=stp->ap; ap; ap=ap->next){
drh3bd48ab2015-09-07 18:23:37 +00003239 if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
drh75897232000-05-29 14:26:00 +00003240 }
3241 fprintf(fp,"\n");
3242 }
drhe9278182007-07-18 18:16:29 +00003243 fprintf(fp, "----------------------------------------------------\n");
3244 fprintf(fp, "Symbols:\n");
3245 for(i=0; i<lemp->nsymbol; i++){
3246 int j;
3247 struct symbol *sp;
3248
3249 sp = lemp->symbols[i];
3250 fprintf(fp, " %3d: %s", i, sp->name);
3251 if( sp->type==NONTERMINAL ){
3252 fprintf(fp, ":");
3253 if( sp->lambda ){
3254 fprintf(fp, " <lambda>");
3255 }
3256 for(j=0; j<lemp->nterminal; j++){
3257 if( sp->firstset && SetFind(sp->firstset, j) ){
3258 fprintf(fp, " %s", lemp->symbols[j]->name);
3259 }
3260 }
3261 }
3262 fprintf(fp, "\n");
3263 }
drh75897232000-05-29 14:26:00 +00003264 fclose(fp);
3265 return;
3266}
3267
3268/* Search for the file "name" which is in the same directory as
3269** the exacutable */
icculus9e44cf12010-02-14 17:14:22 +00003270PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
drh75897232000-05-29 14:26:00 +00003271{
icculus9e44cf12010-02-14 17:14:22 +00003272 const char *pathlist;
3273 char *pathbufptr;
3274 char *pathbuf;
drh75897232000-05-29 14:26:00 +00003275 char *path,*cp;
3276 char c;
drh75897232000-05-29 14:26:00 +00003277
3278#ifdef __WIN32__
3279 cp = strrchr(argv0,'\\');
3280#else
3281 cp = strrchr(argv0,'/');
3282#endif
3283 if( cp ){
3284 c = *cp;
3285 *cp = 0;
drh87cf1372008-08-13 20:09:06 +00003286 path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
drh898799f2014-01-10 23:21:00 +00003287 if( path ) lemon_sprintf(path,"%s/%s",argv0,name);
drh75897232000-05-29 14:26:00 +00003288 *cp = c;
3289 }else{
drh75897232000-05-29 14:26:00 +00003290 pathlist = getenv("PATH");
3291 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
icculus9e44cf12010-02-14 17:14:22 +00003292 pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
drh87cf1372008-08-13 20:09:06 +00003293 path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
icculus9e44cf12010-02-14 17:14:22 +00003294 if( (pathbuf != 0) && (path!=0) ){
3295 pathbufptr = pathbuf;
drh898799f2014-01-10 23:21:00 +00003296 lemon_strcpy(pathbuf, pathlist);
icculus9e44cf12010-02-14 17:14:22 +00003297 while( *pathbuf ){
3298 cp = strchr(pathbuf,':');
3299 if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
drh75897232000-05-29 14:26:00 +00003300 c = *cp;
3301 *cp = 0;
drh898799f2014-01-10 23:21:00 +00003302 lemon_sprintf(path,"%s/%s",pathbuf,name);
drh75897232000-05-29 14:26:00 +00003303 *cp = c;
icculus9e44cf12010-02-14 17:14:22 +00003304 if( c==0 ) pathbuf[0] = 0;
3305 else pathbuf = &cp[1];
drh75897232000-05-29 14:26:00 +00003306 if( access(path,modemask)==0 ) break;
3307 }
icculus9e44cf12010-02-14 17:14:22 +00003308 free(pathbufptr);
drh75897232000-05-29 14:26:00 +00003309 }
3310 }
3311 return path;
3312}
3313
3314/* Given an action, compute the integer value for that action
3315** which is to be put in the action table of the generated machine.
3316** Return negative if no action should be generated.
3317*/
icculus9e44cf12010-02-14 17:14:22 +00003318PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
drh75897232000-05-29 14:26:00 +00003319{
3320 int act;
3321 switch( ap->type ){
drh3bd48ab2015-09-07 18:23:37 +00003322 case SHIFT: act = ap->x.stp->statenum; break;
drhbd8fcc12017-06-28 11:56:18 +00003323 case SHIFTREDUCE: {
3324 act = ap->x.rp->iRule + lemp->nstate;
3325 /* Since a SHIFT is inherient after a prior REDUCE, convert any
3326 ** SHIFTREDUCE action with a nonterminal on the LHS into a simple
3327 ** REDUCE action: */
3328 if( ap->sp->index>=lemp->nterminal ) act += lemp->nrule;
3329 break;
3330 }
drh4ef07702016-03-16 19:45:54 +00003331 case REDUCE: act = ap->x.rp->iRule + lemp->nstate+lemp->nrule; break;
drh3bd48ab2015-09-07 18:23:37 +00003332 case ERROR: act = lemp->nstate + lemp->nrule*2; break;
3333 case ACCEPT: act = lemp->nstate + lemp->nrule*2 + 1; break;
drh75897232000-05-29 14:26:00 +00003334 default: act = -1; break;
3335 }
3336 return act;
3337}
3338
3339#define LINESIZE 1000
3340/* The next cluster of routines are for reading the template file
3341** and writing the results to the generated parser */
3342/* The first function transfers data from "in" to "out" until
3343** a line is seen which begins with "%%". The line number is
3344** tracked.
3345**
3346** if name!=0, then any word that begin with "Parse" is changed to
3347** begin with *name instead.
3348*/
icculus9e44cf12010-02-14 17:14:22 +00003349PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
drh75897232000-05-29 14:26:00 +00003350{
3351 int i, iStart;
3352 char line[LINESIZE];
3353 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
3354 (*lineno)++;
3355 iStart = 0;
3356 if( name ){
3357 for(i=0; line[i]; i++){
3358 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
drhc56fac72015-10-29 13:48:15 +00003359 && (i==0 || !ISALPHA(line[i-1]))
drh75897232000-05-29 14:26:00 +00003360 ){
3361 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
3362 fprintf(out,"%s",name);
3363 i += 4;
3364 iStart = i+1;
3365 }
3366 }
3367 }
3368 fprintf(out,"%s",&line[iStart]);
3369 }
3370}
3371
3372/* The next function finds the template file and opens it, returning
3373** a pointer to the opened file. */
icculus9e44cf12010-02-14 17:14:22 +00003374PRIVATE FILE *tplt_open(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003375{
3376 static char templatename[] = "lempar.c";
3377 char buf[1000];
3378 FILE *in;
3379 char *tpltname;
3380 char *cp;
3381
icculus3e143bd2010-02-14 00:48:49 +00003382 /* first, see if user specified a template filename on the command line. */
3383 if (user_templatename != 0) {
3384 if( access(user_templatename,004)==-1 ){
3385 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3386 user_templatename);
3387 lemp->errorcnt++;
3388 return 0;
3389 }
3390 in = fopen(user_templatename,"rb");
3391 if( in==0 ){
drh25473362015-09-04 18:03:45 +00003392 fprintf(stderr,"Can't open the template file \"%s\".\n",
3393 user_templatename);
icculus3e143bd2010-02-14 00:48:49 +00003394 lemp->errorcnt++;
3395 return 0;
3396 }
3397 return in;
3398 }
3399
drh75897232000-05-29 14:26:00 +00003400 cp = strrchr(lemp->filename,'.');
3401 if( cp ){
drh898799f2014-01-10 23:21:00 +00003402 lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
drh75897232000-05-29 14:26:00 +00003403 }else{
drh898799f2014-01-10 23:21:00 +00003404 lemon_sprintf(buf,"%s.lt",lemp->filename);
drh75897232000-05-29 14:26:00 +00003405 }
3406 if( access(buf,004)==0 ){
3407 tpltname = buf;
drh960e8c62001-04-03 16:53:21 +00003408 }else if( access(templatename,004)==0 ){
3409 tpltname = templatename;
drh75897232000-05-29 14:26:00 +00003410 }else{
3411 tpltname = pathsearch(lemp->argv0,templatename,0);
3412 }
3413 if( tpltname==0 ){
3414 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3415 templatename);
3416 lemp->errorcnt++;
3417 return 0;
3418 }
drh2aa6ca42004-09-10 00:14:04 +00003419 in = fopen(tpltname,"rb");
drh75897232000-05-29 14:26:00 +00003420 if( in==0 ){
3421 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
3422 lemp->errorcnt++;
3423 return 0;
3424 }
3425 return in;
3426}
3427
drhaf805ca2004-09-07 11:28:25 +00003428/* Print a #line directive line to the output file. */
icculus9e44cf12010-02-14 17:14:22 +00003429PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
drhaf805ca2004-09-07 11:28:25 +00003430{
3431 fprintf(out,"#line %d \"",lineno);
3432 while( *filename ){
3433 if( *filename == '\\' ) putc('\\',out);
3434 putc(*filename,out);
3435 filename++;
3436 }
3437 fprintf(out,"\"\n");
3438}
3439
drh75897232000-05-29 14:26:00 +00003440/* Print a string to the file and keep the linenumber up to date */
icculus9e44cf12010-02-14 17:14:22 +00003441PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
drh75897232000-05-29 14:26:00 +00003442{
3443 if( str==0 ) return;
drh75897232000-05-29 14:26:00 +00003444 while( *str ){
drh75897232000-05-29 14:26:00 +00003445 putc(*str,out);
shane58543932008-12-10 20:10:04 +00003446 if( *str=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003447 str++;
3448 }
drh9db55df2004-09-09 14:01:21 +00003449 if( str[-1]!='\n' ){
3450 putc('\n',out);
3451 (*lineno)++;
3452 }
shane58543932008-12-10 20:10:04 +00003453 if (!lemp->nolinenosflag) {
drh06f60d82017-04-14 19:46:12 +00003454 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
shane58543932008-12-10 20:10:04 +00003455 }
drh75897232000-05-29 14:26:00 +00003456 return;
3457}
3458
3459/*
3460** The following routine emits code for the destructor for the
3461** symbol sp
3462*/
icculus9e44cf12010-02-14 17:14:22 +00003463void emit_destructor_code(
3464 FILE *out,
3465 struct symbol *sp,
3466 struct lemon *lemp,
3467 int *lineno
3468){
drhcc83b6e2004-04-23 23:38:42 +00003469 char *cp = 0;
drh75897232000-05-29 14:26:00 +00003470
drh75897232000-05-29 14:26:00 +00003471 if( sp->type==TERMINAL ){
3472 cp = lemp->tokendest;
3473 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003474 fprintf(out,"{\n"); (*lineno)++;
drh960e8c62001-04-03 16:53:21 +00003475 }else if( sp->destructor ){
drh75897232000-05-29 14:26:00 +00003476 cp = sp->destructor;
drha5808f32008-04-27 22:19:44 +00003477 fprintf(out,"{\n"); (*lineno)++;
drh25473362015-09-04 18:03:45 +00003478 if( !lemp->nolinenosflag ){
3479 (*lineno)++;
3480 tplt_linedir(out,sp->destLineno,lemp->filename);
3481 }
drh960e8c62001-04-03 16:53:21 +00003482 }else if( lemp->vardest ){
3483 cp = lemp->vardest;
3484 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003485 fprintf(out,"{\n"); (*lineno)++;
drhcc83b6e2004-04-23 23:38:42 +00003486 }else{
3487 assert( 0 ); /* Cannot happen */
drh75897232000-05-29 14:26:00 +00003488 }
3489 for(; *cp; cp++){
3490 if( *cp=='$' && cp[1]=='$' ){
3491 fprintf(out,"(yypminor->yy%d)",sp->dtnum);
3492 cp++;
3493 continue;
3494 }
shane58543932008-12-10 20:10:04 +00003495 if( *cp=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003496 fputc(*cp,out);
3497 }
shane58543932008-12-10 20:10:04 +00003498 fprintf(out,"\n"); (*lineno)++;
drh06f60d82017-04-14 19:46:12 +00003499 if (!lemp->nolinenosflag) {
3500 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
shane58543932008-12-10 20:10:04 +00003501 }
3502 fprintf(out,"}\n"); (*lineno)++;
drh75897232000-05-29 14:26:00 +00003503 return;
3504}
3505
3506/*
drh960e8c62001-04-03 16:53:21 +00003507** Return TRUE (non-zero) if the given symbol has a destructor.
drh75897232000-05-29 14:26:00 +00003508*/
icculus9e44cf12010-02-14 17:14:22 +00003509int has_destructor(struct symbol *sp, struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003510{
3511 int ret;
3512 if( sp->type==TERMINAL ){
3513 ret = lemp->tokendest!=0;
3514 }else{
drh960e8c62001-04-03 16:53:21 +00003515 ret = lemp->vardest!=0 || sp->destructor!=0;
drh75897232000-05-29 14:26:00 +00003516 }
3517 return ret;
3518}
3519
drh0bb132b2004-07-20 14:06:51 +00003520/*
3521** Append text to a dynamically allocated string. If zText is 0 then
3522** reset the string to be empty again. Always return the complete text
3523** of the string (which is overwritten with each call).
drh7ac25c72004-08-19 15:12:26 +00003524**
3525** n bytes of zText are stored. If n==0 then all of zText up to the first
3526** \000 terminator is stored. zText can contain up to two instances of
3527** %d. The values of p1 and p2 are written into the first and second
3528** %d.
3529**
3530** If n==-1, then the previous character is overwritten.
drh0bb132b2004-07-20 14:06:51 +00003531*/
icculus9e44cf12010-02-14 17:14:22 +00003532PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
3533 static char empty[1] = { 0 };
drh0bb132b2004-07-20 14:06:51 +00003534 static char *z = 0;
3535 static int alloced = 0;
3536 static int used = 0;
drhaf805ca2004-09-07 11:28:25 +00003537 int c;
drh0bb132b2004-07-20 14:06:51 +00003538 char zInt[40];
drh0bb132b2004-07-20 14:06:51 +00003539 if( zText==0 ){
drh4dd0d3f2016-02-17 01:18:33 +00003540 if( used==0 && z!=0 ) z[0] = 0;
drh0bb132b2004-07-20 14:06:51 +00003541 used = 0;
3542 return z;
3543 }
drh7ac25c72004-08-19 15:12:26 +00003544 if( n<=0 ){
3545 if( n<0 ){
3546 used += n;
3547 assert( used>=0 );
3548 }
drh87cf1372008-08-13 20:09:06 +00003549 n = lemonStrlen(zText);
drh7ac25c72004-08-19 15:12:26 +00003550 }
drhdf609712010-11-23 20:55:27 +00003551 if( (int) (n+sizeof(zInt)*2+used) >= alloced ){
drh0bb132b2004-07-20 14:06:51 +00003552 alloced = n + sizeof(zInt)*2 + used + 200;
icculus9e44cf12010-02-14 17:14:22 +00003553 z = (char *) realloc(z, alloced);
drh0bb132b2004-07-20 14:06:51 +00003554 }
icculus9e44cf12010-02-14 17:14:22 +00003555 if( z==0 ) return empty;
drh0bb132b2004-07-20 14:06:51 +00003556 while( n-- > 0 ){
3557 c = *(zText++);
drh50489622006-10-13 12:25:29 +00003558 if( c=='%' && n>0 && zText[0]=='d' ){
drh898799f2014-01-10 23:21:00 +00003559 lemon_sprintf(zInt, "%d", p1);
drh0bb132b2004-07-20 14:06:51 +00003560 p1 = p2;
drh898799f2014-01-10 23:21:00 +00003561 lemon_strcpy(&z[used], zInt);
drh87cf1372008-08-13 20:09:06 +00003562 used += lemonStrlen(&z[used]);
drh0bb132b2004-07-20 14:06:51 +00003563 zText++;
3564 n--;
3565 }else{
mistachkin2318d332015-01-12 18:02:52 +00003566 z[used++] = (char)c;
drh0bb132b2004-07-20 14:06:51 +00003567 }
3568 }
3569 z[used] = 0;
3570 return z;
3571}
3572
3573/*
drh711c9812016-05-23 14:24:31 +00003574** Write and transform the rp->code string so that symbols are expanded.
3575** Populate the rp->codePrefix and rp->codeSuffix strings, as appropriate.
drhdabd04c2016-02-17 01:46:19 +00003576**
3577** Return 1 if the expanded code requires that "yylhsminor" local variable
3578** to be defined.
drh0bb132b2004-07-20 14:06:51 +00003579*/
drhdabd04c2016-02-17 01:46:19 +00003580PRIVATE int translate_code(struct lemon *lemp, struct rule *rp){
drh0bb132b2004-07-20 14:06:51 +00003581 char *cp, *xp;
3582 int i;
drhcf82f0d2016-02-17 04:33:10 +00003583 int rc = 0; /* True if yylhsminor is used */
drh43303de2016-02-17 12:34:03 +00003584 int dontUseRhs0 = 0; /* If true, use of left-most RHS label is illegal */
drhcf82f0d2016-02-17 04:33:10 +00003585 const char *zSkip = 0; /* The zOvwrt comment within rp->code, or NULL */
3586 char lhsused = 0; /* True if the LHS element has been used */
3587 char lhsdirect; /* True if LHS writes directly into stack */
3588 char used[MAXRHS]; /* True for each RHS element which is used */
3589 char zLhs[50]; /* Convert the LHS symbol into this string */
3590 char zOvwrt[900]; /* Comment that to allow LHS to overwrite RHS */
drh0bb132b2004-07-20 14:06:51 +00003591
3592 for(i=0; i<rp->nrhs; i++) used[i] = 0;
3593 lhsused = 0;
3594
drh19c9e562007-03-29 20:13:53 +00003595 if( rp->code==0 ){
icculus9e44cf12010-02-14 17:14:22 +00003596 static char newlinestr[2] = { '\n', '\0' };
3597 rp->code = newlinestr;
drh19c9e562007-03-29 20:13:53 +00003598 rp->line = rp->ruleline;
drh711c9812016-05-23 14:24:31 +00003599 rp->noCode = 1;
3600 }else{
3601 rp->noCode = 0;
drh19c9e562007-03-29 20:13:53 +00003602 }
3603
drh4dd0d3f2016-02-17 01:18:33 +00003604
drh2e55b042016-04-30 17:19:30 +00003605 if( rp->nrhs==0 ){
drh4dd0d3f2016-02-17 01:18:33 +00003606 /* If there are no RHS symbols, then writing directly to the LHS is ok */
3607 lhsdirect = 1;
3608 }else if( rp->rhsalias[0]==0 ){
drh2e55b042016-04-30 17:19:30 +00003609 /* The left-most RHS symbol has no value. LHS direct is ok. But
drh4dd0d3f2016-02-17 01:18:33 +00003610 ** we have to call the distructor on the RHS symbol first. */
3611 lhsdirect = 1;
3612 if( has_destructor(rp->rhs[0],lemp) ){
3613 append_str(0,0,0,0);
3614 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
3615 rp->rhs[0]->index,1-rp->nrhs);
3616 rp->codePrefix = Strsafe(append_str(0,0,0,0));
drh711c9812016-05-23 14:24:31 +00003617 rp->noCode = 0;
drh4dd0d3f2016-02-17 01:18:33 +00003618 }
drh2e55b042016-04-30 17:19:30 +00003619 }else if( rp->lhsalias==0 ){
3620 /* There is no LHS value symbol. */
3621 lhsdirect = 1;
drh4dd0d3f2016-02-17 01:18:33 +00003622 }else if( strcmp(rp->lhsalias,rp->rhsalias[0])==0 ){
drh06f60d82017-04-14 19:46:12 +00003623 /* The LHS symbol and the left-most RHS symbol are the same, so
drh4dd0d3f2016-02-17 01:18:33 +00003624 ** direct writing is allowed */
3625 lhsdirect = 1;
3626 lhsused = 1;
3627 used[0] = 1;
3628 if( rp->lhs->dtnum!=rp->rhs[0]->dtnum ){
3629 ErrorMsg(lemp->filename,rp->ruleline,
3630 "%s(%s) and %s(%s) share the same label but have "
3631 "different datatypes.",
3632 rp->lhs->name, rp->lhsalias, rp->rhs[0]->name, rp->rhsalias[0]);
3633 lemp->errorcnt++;
drh06f60d82017-04-14 19:46:12 +00003634 }
drh4dd0d3f2016-02-17 01:18:33 +00003635 }else{
drhcf82f0d2016-02-17 04:33:10 +00003636 lemon_sprintf(zOvwrt, "/*%s-overwrites-%s*/",
3637 rp->lhsalias, rp->rhsalias[0]);
3638 zSkip = strstr(rp->code, zOvwrt);
3639 if( zSkip!=0 ){
3640 /* The code contains a special comment that indicates that it is safe
3641 ** for the LHS label to overwrite left-most RHS label. */
3642 lhsdirect = 1;
3643 }else{
3644 lhsdirect = 0;
3645 }
drh4dd0d3f2016-02-17 01:18:33 +00003646 }
3647 if( lhsdirect ){
3648 sprintf(zLhs, "yymsp[%d].minor.yy%d",1-rp->nrhs,rp->lhs->dtnum);
3649 }else{
drhdabd04c2016-02-17 01:46:19 +00003650 rc = 1;
drh4dd0d3f2016-02-17 01:18:33 +00003651 sprintf(zLhs, "yylhsminor.yy%d",rp->lhs->dtnum);
3652 }
3653
drh0bb132b2004-07-20 14:06:51 +00003654 append_str(0,0,0,0);
icculus9e44cf12010-02-14 17:14:22 +00003655
3656 /* This const cast is wrong but harmless, if we're careful. */
3657 for(cp=(char *)rp->code; *cp; cp++){
drhcf82f0d2016-02-17 04:33:10 +00003658 if( cp==zSkip ){
3659 append_str(zOvwrt,0,0,0);
3660 cp += lemonStrlen(zOvwrt)-1;
drh43303de2016-02-17 12:34:03 +00003661 dontUseRhs0 = 1;
drhcf82f0d2016-02-17 04:33:10 +00003662 continue;
3663 }
drhc56fac72015-10-29 13:48:15 +00003664 if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){
drh0bb132b2004-07-20 14:06:51 +00003665 char saved;
drhc56fac72015-10-29 13:48:15 +00003666 for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++);
drh0bb132b2004-07-20 14:06:51 +00003667 saved = *xp;
3668 *xp = 0;
3669 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
drh4dd0d3f2016-02-17 01:18:33 +00003670 append_str(zLhs,0,0,0);
drh0bb132b2004-07-20 14:06:51 +00003671 cp = xp;
3672 lhsused = 1;
3673 }else{
3674 for(i=0; i<rp->nrhs; i++){
3675 if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
drh43303de2016-02-17 12:34:03 +00003676 if( i==0 && dontUseRhs0 ){
3677 ErrorMsg(lemp->filename,rp->ruleline,
3678 "Label %s used after '%s'.",
3679 rp->rhsalias[0], zOvwrt);
3680 lemp->errorcnt++;
3681 }else if( cp!=rp->code && cp[-1]=='@' ){
drh7ac25c72004-08-19 15:12:26 +00003682 /* If the argument is of the form @X then substituted
3683 ** the token number of X, not the value of X */
3684 append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0);
3685 }else{
drhfd405312005-11-06 04:06:59 +00003686 struct symbol *sp = rp->rhs[i];
3687 int dtnum;
3688 if( sp->type==MULTITERMINAL ){
3689 dtnum = sp->subsym[0]->dtnum;
3690 }else{
3691 dtnum = sp->dtnum;
3692 }
3693 append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum);
drh7ac25c72004-08-19 15:12:26 +00003694 }
drh0bb132b2004-07-20 14:06:51 +00003695 cp = xp;
3696 used[i] = 1;
3697 break;
3698 }
3699 }
3700 }
3701 *xp = saved;
3702 }
3703 append_str(cp, 1, 0, 0);
3704 } /* End loop */
3705
drh4dd0d3f2016-02-17 01:18:33 +00003706 /* Main code generation completed */
3707 cp = append_str(0,0,0,0);
3708 if( cp && cp[0] ) rp->code = Strsafe(cp);
3709 append_str(0,0,0,0);
3710
drh0bb132b2004-07-20 14:06:51 +00003711 /* Check to make sure the LHS has been used */
3712 if( rp->lhsalias && !lhsused ){
3713 ErrorMsg(lemp->filename,rp->ruleline,
3714 "Label \"%s\" for \"%s(%s)\" is never used.",
3715 rp->lhsalias,rp->lhs->name,rp->lhsalias);
3716 lemp->errorcnt++;
3717 }
3718
drh4dd0d3f2016-02-17 01:18:33 +00003719 /* Generate destructor code for RHS minor values which are not referenced.
3720 ** Generate error messages for unused labels and duplicate labels.
3721 */
drh0bb132b2004-07-20 14:06:51 +00003722 for(i=0; i<rp->nrhs; i++){
drh4dd0d3f2016-02-17 01:18:33 +00003723 if( rp->rhsalias[i] ){
3724 if( i>0 ){
3725 int j;
3726 if( rp->lhsalias && strcmp(rp->lhsalias,rp->rhsalias[i])==0 ){
3727 ErrorMsg(lemp->filename,rp->ruleline,
3728 "%s(%s) has the same label as the LHS but is not the left-most "
3729 "symbol on the RHS.",
3730 rp->rhs[i]->name, rp->rhsalias);
3731 lemp->errorcnt++;
3732 }
3733 for(j=0; j<i; j++){
3734 if( rp->rhsalias[j] && strcmp(rp->rhsalias[j],rp->rhsalias[i])==0 ){
3735 ErrorMsg(lemp->filename,rp->ruleline,
3736 "Label %s used for multiple symbols on the RHS of a rule.",
3737 rp->rhsalias[i]);
3738 lemp->errorcnt++;
3739 break;
3740 }
3741 }
drh0bb132b2004-07-20 14:06:51 +00003742 }
drh4dd0d3f2016-02-17 01:18:33 +00003743 if( !used[i] ){
3744 ErrorMsg(lemp->filename,rp->ruleline,
3745 "Label %s for \"%s(%s)\" is never used.",
3746 rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
3747 lemp->errorcnt++;
3748 }
3749 }else if( i>0 && has_destructor(rp->rhs[i],lemp) ){
3750 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
3751 rp->rhs[i]->index,i-rp->nrhs+1);
drh0bb132b2004-07-20 14:06:51 +00003752 }
3753 }
drh4dd0d3f2016-02-17 01:18:33 +00003754
3755 /* If unable to write LHS values directly into the stack, write the
3756 ** saved LHS value now. */
3757 if( lhsdirect==0 ){
3758 append_str(" yymsp[%d].minor.yy%d = ", 0, 1-rp->nrhs, rp->lhs->dtnum);
3759 append_str(zLhs, 0, 0, 0);
3760 append_str(";\n", 0, 0, 0);
drh61e339a2007-01-16 03:09:02 +00003761 }
drh4dd0d3f2016-02-17 01:18:33 +00003762
3763 /* Suffix code generation complete */
3764 cp = append_str(0,0,0,0);
drh711c9812016-05-23 14:24:31 +00003765 if( cp && cp[0] ){
3766 rp->codeSuffix = Strsafe(cp);
3767 rp->noCode = 0;
3768 }
drhdabd04c2016-02-17 01:46:19 +00003769
3770 return rc;
drh0bb132b2004-07-20 14:06:51 +00003771}
3772
drh06f60d82017-04-14 19:46:12 +00003773/*
drh75897232000-05-29 14:26:00 +00003774** Generate code which executes when the rule "rp" is reduced. Write
3775** the code to "out". Make sure lineno stays up-to-date.
3776*/
icculus9e44cf12010-02-14 17:14:22 +00003777PRIVATE void emit_code(
3778 FILE *out,
3779 struct rule *rp,
3780 struct lemon *lemp,
3781 int *lineno
3782){
3783 const char *cp;
drh75897232000-05-29 14:26:00 +00003784
drh4dd0d3f2016-02-17 01:18:33 +00003785 /* Setup code prior to the #line directive */
3786 if( rp->codePrefix && rp->codePrefix[0] ){
3787 fprintf(out, "{%s", rp->codePrefix);
3788 for(cp=rp->codePrefix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
3789 }
3790
drh75897232000-05-29 14:26:00 +00003791 /* Generate code to do the reduce action */
3792 if( rp->code ){
drh25473362015-09-04 18:03:45 +00003793 if( !lemp->nolinenosflag ){
3794 (*lineno)++;
3795 tplt_linedir(out,rp->line,lemp->filename);
3796 }
drhaf805ca2004-09-07 11:28:25 +00003797 fprintf(out,"{%s",rp->code);
drh4dd0d3f2016-02-17 01:18:33 +00003798 for(cp=rp->code; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
shane58543932008-12-10 20:10:04 +00003799 fprintf(out,"}\n"); (*lineno)++;
drh25473362015-09-04 18:03:45 +00003800 if( !lemp->nolinenosflag ){
3801 (*lineno)++;
3802 tplt_linedir(out,*lineno,lemp->outname);
3803 }
drh4dd0d3f2016-02-17 01:18:33 +00003804 }
3805
3806 /* Generate breakdown code that occurs after the #line directive */
3807 if( rp->codeSuffix && rp->codeSuffix[0] ){
3808 fprintf(out, "%s", rp->codeSuffix);
3809 for(cp=rp->codeSuffix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
3810 }
3811
3812 if( rp->codePrefix ){
3813 fprintf(out, "}\n"); (*lineno)++;
3814 }
drh75897232000-05-29 14:26:00 +00003815
drh75897232000-05-29 14:26:00 +00003816 return;
3817}
3818
3819/*
3820** Print the definition of the union used for the parser's data stack.
3821** This union contains fields for every possible data type for tokens
3822** and nonterminals. In the process of computing and printing this
3823** union, also set the ".dtnum" field of every terminal and nonterminal
3824** symbol.
3825*/
icculus9e44cf12010-02-14 17:14:22 +00003826void print_stack_union(
3827 FILE *out, /* The output stream */
3828 struct lemon *lemp, /* The main info structure for this parser */
3829 int *plineno, /* Pointer to the line number */
3830 int mhflag /* True if generating makeheaders output */
3831){
drh75897232000-05-29 14:26:00 +00003832 int lineno = *plineno; /* The line number of the output */
3833 char **types; /* A hash table of datatypes */
3834 int arraysize; /* Size of the "types" array */
3835 int maxdtlength; /* Maximum length of any ".datatype" field. */
3836 char *stddt; /* Standardized name for a datatype */
3837 int i,j; /* Loop counters */
drh01f75f22013-10-02 20:46:30 +00003838 unsigned hash; /* For hashing the name of a type */
icculus9e44cf12010-02-14 17:14:22 +00003839 const char *name; /* Name of the parser */
drh75897232000-05-29 14:26:00 +00003840
3841 /* Allocate and initialize types[] and allocate stddt[] */
3842 arraysize = lemp->nsymbol * 2;
drh9892c5d2007-12-21 00:02:11 +00003843 types = (char**)calloc( arraysize, sizeof(char*) );
drh070d4222011-06-02 15:48:51 +00003844 if( types==0 ){
3845 fprintf(stderr,"Out of memory.\n");
3846 exit(1);
3847 }
drh75897232000-05-29 14:26:00 +00003848 for(i=0; i<arraysize; i++) types[i] = 0;
3849 maxdtlength = 0;
drh960e8c62001-04-03 16:53:21 +00003850 if( lemp->vartype ){
drh87cf1372008-08-13 20:09:06 +00003851 maxdtlength = lemonStrlen(lemp->vartype);
drh960e8c62001-04-03 16:53:21 +00003852 }
drh75897232000-05-29 14:26:00 +00003853 for(i=0; i<lemp->nsymbol; i++){
3854 int len;
3855 struct symbol *sp = lemp->symbols[i];
3856 if( sp->datatype==0 ) continue;
drh87cf1372008-08-13 20:09:06 +00003857 len = lemonStrlen(sp->datatype);
drh75897232000-05-29 14:26:00 +00003858 if( len>maxdtlength ) maxdtlength = len;
3859 }
3860 stddt = (char*)malloc( maxdtlength*2 + 1 );
drh070d4222011-06-02 15:48:51 +00003861 if( stddt==0 ){
drh75897232000-05-29 14:26:00 +00003862 fprintf(stderr,"Out of memory.\n");
3863 exit(1);
3864 }
3865
3866 /* Build a hash table of datatypes. The ".dtnum" field of each symbol
3867 ** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
drh960e8c62001-04-03 16:53:21 +00003868 ** used for terminal symbols. If there is no %default_type defined then
3869 ** 0 is also used as the .dtnum value for nonterminals which do not specify
3870 ** a datatype using the %type directive.
3871 */
drh75897232000-05-29 14:26:00 +00003872 for(i=0; i<lemp->nsymbol; i++){
3873 struct symbol *sp = lemp->symbols[i];
3874 char *cp;
3875 if( sp==lemp->errsym ){
3876 sp->dtnum = arraysize+1;
3877 continue;
3878 }
drh960e8c62001-04-03 16:53:21 +00003879 if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
drh75897232000-05-29 14:26:00 +00003880 sp->dtnum = 0;
3881 continue;
3882 }
3883 cp = sp->datatype;
drh960e8c62001-04-03 16:53:21 +00003884 if( cp==0 ) cp = lemp->vartype;
drh75897232000-05-29 14:26:00 +00003885 j = 0;
drhc56fac72015-10-29 13:48:15 +00003886 while( ISSPACE(*cp) ) cp++;
drh75897232000-05-29 14:26:00 +00003887 while( *cp ) stddt[j++] = *cp++;
drhc56fac72015-10-29 13:48:15 +00003888 while( j>0 && ISSPACE(stddt[j-1]) ) j--;
drh75897232000-05-29 14:26:00 +00003889 stddt[j] = 0;
drh02368c92009-04-05 15:18:02 +00003890 if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){
drh32c4d742008-07-01 16:34:49 +00003891 sp->dtnum = 0;
3892 continue;
3893 }
drh75897232000-05-29 14:26:00 +00003894 hash = 0;
3895 for(j=0; stddt[j]; j++){
3896 hash = hash*53 + stddt[j];
3897 }
drh3b2129c2003-05-13 00:34:21 +00003898 hash = (hash & 0x7fffffff)%arraysize;
drh75897232000-05-29 14:26:00 +00003899 while( types[hash] ){
3900 if( strcmp(types[hash],stddt)==0 ){
3901 sp->dtnum = hash + 1;
3902 break;
3903 }
3904 hash++;
drh2b51f212013-10-11 23:01:02 +00003905 if( hash>=(unsigned)arraysize ) hash = 0;
drh75897232000-05-29 14:26:00 +00003906 }
3907 if( types[hash]==0 ){
3908 sp->dtnum = hash + 1;
drh87cf1372008-08-13 20:09:06 +00003909 types[hash] = (char*)malloc( lemonStrlen(stddt)+1 );
drh75897232000-05-29 14:26:00 +00003910 if( types[hash]==0 ){
3911 fprintf(stderr,"Out of memory.\n");
3912 exit(1);
3913 }
drh898799f2014-01-10 23:21:00 +00003914 lemon_strcpy(types[hash],stddt);
drh75897232000-05-29 14:26:00 +00003915 }
3916 }
3917
3918 /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
3919 name = lemp->name ? lemp->name : "Parse";
3920 lineno = *plineno;
3921 if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
3922 fprintf(out,"#define %sTOKENTYPE %s\n",name,
3923 lemp->tokentype?lemp->tokentype:"void*"); lineno++;
3924 if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
3925 fprintf(out,"typedef union {\n"); lineno++;
drh15b024c2008-12-11 02:20:43 +00003926 fprintf(out," int yyinit;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003927 fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
3928 for(i=0; i<arraysize; i++){
3929 if( types[i]==0 ) continue;
3930 fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
3931 free(types[i]);
3932 }
drhc4dd3fd2008-01-22 01:48:05 +00003933 if( lemp->errsym->useCnt ){
3934 fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
3935 }
drh75897232000-05-29 14:26:00 +00003936 free(stddt);
3937 free(types);
3938 fprintf(out,"} YYMINORTYPE;\n"); lineno++;
3939 *plineno = lineno;
3940}
3941
drhb29b0a52002-02-23 19:39:46 +00003942/*
3943** Return the name of a C datatype able to represent values between
drhc75e0162015-09-07 02:23:02 +00003944** lwr and upr, inclusive. If pnByte!=NULL then also write the sizeof
3945** for that type (1, 2, or 4) into *pnByte.
drhb29b0a52002-02-23 19:39:46 +00003946*/
drhc75e0162015-09-07 02:23:02 +00003947static const char *minimum_size_type(int lwr, int upr, int *pnByte){
3948 const char *zType = "int";
3949 int nByte = 4;
drh8b582012003-10-21 13:16:03 +00003950 if( lwr>=0 ){
3951 if( upr<=255 ){
drhc75e0162015-09-07 02:23:02 +00003952 zType = "unsigned char";
3953 nByte = 1;
drh8b582012003-10-21 13:16:03 +00003954 }else if( upr<65535 ){
drhc75e0162015-09-07 02:23:02 +00003955 zType = "unsigned short int";
3956 nByte = 2;
drh8b582012003-10-21 13:16:03 +00003957 }else{
drhc75e0162015-09-07 02:23:02 +00003958 zType = "unsigned int";
3959 nByte = 4;
drh8b582012003-10-21 13:16:03 +00003960 }
3961 }else if( lwr>=-127 && upr<=127 ){
drhc75e0162015-09-07 02:23:02 +00003962 zType = "signed char";
3963 nByte = 1;
drh8b582012003-10-21 13:16:03 +00003964 }else if( lwr>=-32767 && upr<32767 ){
drhc75e0162015-09-07 02:23:02 +00003965 zType = "short";
3966 nByte = 2;
drhb29b0a52002-02-23 19:39:46 +00003967 }
drhc75e0162015-09-07 02:23:02 +00003968 if( pnByte ) *pnByte = nByte;
3969 return zType;
drhb29b0a52002-02-23 19:39:46 +00003970}
3971
drhfdbf9282003-10-21 16:34:41 +00003972/*
3973** Each state contains a set of token transaction and a set of
3974** nonterminal transactions. Each of these sets makes an instance
3975** of the following structure. An array of these structures is used
3976** to order the creation of entries in the yy_action[] table.
3977*/
3978struct axset {
3979 struct state *stp; /* A pointer to a state */
3980 int isTkn; /* True to use tokens. False for non-terminals */
3981 int nAction; /* Number of actions */
drhe594bc32009-11-03 13:02:25 +00003982 int iOrder; /* Original order of action sets */
drhfdbf9282003-10-21 16:34:41 +00003983};
3984
3985/*
3986** Compare to axset structures for sorting purposes
3987*/
3988static int axset_compare(const void *a, const void *b){
3989 struct axset *p1 = (struct axset*)a;
3990 struct axset *p2 = (struct axset*)b;
drhe594bc32009-11-03 13:02:25 +00003991 int c;
3992 c = p2->nAction - p1->nAction;
3993 if( c==0 ){
drh337cd0d2015-09-07 23:40:42 +00003994 c = p1->iOrder - p2->iOrder;
drhe594bc32009-11-03 13:02:25 +00003995 }
3996 assert( c!=0 || p1==p2 );
3997 return c;
drhfdbf9282003-10-21 16:34:41 +00003998}
3999
drhc4dd3fd2008-01-22 01:48:05 +00004000/*
4001** Write text on "out" that describes the rule "rp".
4002*/
4003static void writeRuleText(FILE *out, struct rule *rp){
4004 int j;
4005 fprintf(out,"%s ::=", rp->lhs->name);
4006 for(j=0; j<rp->nrhs; j++){
4007 struct symbol *sp = rp->rhs[j];
drh61f92cd2014-01-11 03:06:18 +00004008 if( sp->type!=MULTITERMINAL ){
4009 fprintf(out," %s", sp->name);
4010 }else{
drhc4dd3fd2008-01-22 01:48:05 +00004011 int k;
drh61f92cd2014-01-11 03:06:18 +00004012 fprintf(out," %s", sp->subsym[0]->name);
drhc4dd3fd2008-01-22 01:48:05 +00004013 for(k=1; k<sp->nsubsym; k++){
4014 fprintf(out,"|%s",sp->subsym[k]->name);
4015 }
4016 }
4017 }
4018}
4019
4020
drh75897232000-05-29 14:26:00 +00004021/* Generate C source code for the parser */
icculus9e44cf12010-02-14 17:14:22 +00004022void ReportTable(
4023 struct lemon *lemp,
4024 int mhflag /* Output in makeheaders format if true */
4025){
drh75897232000-05-29 14:26:00 +00004026 FILE *out, *in;
4027 char line[LINESIZE];
4028 int lineno;
4029 struct state *stp;
4030 struct action *ap;
4031 struct rule *rp;
drh8b582012003-10-21 13:16:03 +00004032 struct acttab *pActtab;
drhc75e0162015-09-07 02:23:02 +00004033 int i, j, n, sz;
4034 int szActionType; /* sizeof(YYACTIONTYPE) */
4035 int szCodeType; /* sizeof(YYCODETYPE) */
icculus9e44cf12010-02-14 17:14:22 +00004036 const char *name;
drh8b582012003-10-21 13:16:03 +00004037 int mnTknOfst, mxTknOfst;
4038 int mnNtOfst, mxNtOfst;
drhfdbf9282003-10-21 16:34:41 +00004039 struct axset *ax;
drh75897232000-05-29 14:26:00 +00004040
4041 in = tplt_open(lemp);
4042 if( in==0 ) return;
drh2aa6ca42004-09-10 00:14:04 +00004043 out = file_open(lemp,".c","wb");
drh75897232000-05-29 14:26:00 +00004044 if( out==0 ){
4045 fclose(in);
4046 return;
4047 }
4048 lineno = 1;
4049 tplt_xfer(lemp->name,in,out,&lineno);
4050
4051 /* Generate the include code, if any */
drha5808f32008-04-27 22:19:44 +00004052 tplt_print(out,lemp,lemp->include,&lineno);
drh75897232000-05-29 14:26:00 +00004053 if( mhflag ){
mistachkin8e189222015-04-19 21:43:16 +00004054 char *incName = file_makename(lemp, ".h");
4055 fprintf(out,"#include \"%s\"\n", incName); lineno++;
4056 free(incName);
drh75897232000-05-29 14:26:00 +00004057 }
4058 tplt_xfer(lemp->name,in,out,&lineno);
4059
4060 /* Generate #defines for all tokens */
4061 if( mhflag ){
icculus9e44cf12010-02-14 17:14:22 +00004062 const char *prefix;
drh75897232000-05-29 14:26:00 +00004063 fprintf(out,"#if INTERFACE\n"); lineno++;
4064 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4065 else prefix = "";
4066 for(i=1; i<lemp->nterminal; i++){
4067 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
4068 lineno++;
4069 }
4070 fprintf(out,"#endif\n"); lineno++;
4071 }
4072 tplt_xfer(lemp->name,in,out,&lineno);
4073
4074 /* Generate the defines */
drh75897232000-05-29 14:26:00 +00004075 fprintf(out,"#define YYCODETYPE %s\n",
drhc75e0162015-09-07 02:23:02 +00004076 minimum_size_type(0, lemp->nsymbol+1, &szCodeType)); lineno++;
drh75897232000-05-29 14:26:00 +00004077 fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++;
4078 fprintf(out,"#define YYACTIONTYPE %s\n",
drh3bd48ab2015-09-07 18:23:37 +00004079 minimum_size_type(0,lemp->nstate+lemp->nrule*2+5,&szActionType)); lineno++;
drhe09daa92006-06-10 13:29:31 +00004080 if( lemp->wildcard ){
4081 fprintf(out,"#define YYWILDCARD %d\n",
4082 lemp->wildcard->index); lineno++;
4083 }
drh75897232000-05-29 14:26:00 +00004084 print_stack_union(out,lemp,&lineno,mhflag);
drhca44b5a2007-02-22 23:06:58 +00004085 fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004086 if( lemp->stacksize ){
drh75897232000-05-29 14:26:00 +00004087 fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
4088 }else{
4089 fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
4090 }
drhca44b5a2007-02-22 23:06:58 +00004091 fprintf(out, "#endif\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004092 if( mhflag ){
4093 fprintf(out,"#if INTERFACE\n"); lineno++;
4094 }
4095 name = lemp->name ? lemp->name : "Parse";
4096 if( lemp->arg && lemp->arg[0] ){
drh87cf1372008-08-13 20:09:06 +00004097 i = lemonStrlen(lemp->arg);
drhc56fac72015-10-29 13:48:15 +00004098 while( i>=1 && ISSPACE(lemp->arg[i-1]) ) i--;
4099 while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
drh1f245e42002-03-11 13:55:50 +00004100 fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++;
4101 fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++;
4102 fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n",
4103 name,lemp->arg,&lemp->arg[i]); lineno++;
4104 fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n",
4105 name,&lemp->arg[i],&lemp->arg[i]); lineno++;
drh75897232000-05-29 14:26:00 +00004106 }else{
drh1f245e42002-03-11 13:55:50 +00004107 fprintf(out,"#define %sARG_SDECL\n",name); lineno++;
4108 fprintf(out,"#define %sARG_PDECL\n",name); lineno++;
4109 fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
4110 fprintf(out,"#define %sARG_STORE\n",name); lineno++;
drh75897232000-05-29 14:26:00 +00004111 }
4112 if( mhflag ){
4113 fprintf(out,"#endif\n"); lineno++;
4114 }
drhc4dd3fd2008-01-22 01:48:05 +00004115 if( lemp->errsym->useCnt ){
drh3bd48ab2015-09-07 18:23:37 +00004116 fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
4117 fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
drhc4dd3fd2008-01-22 01:48:05 +00004118 }
drh0bd1f4e2002-06-06 18:54:39 +00004119 if( lemp->has_fallback ){
4120 fprintf(out,"#define YYFALLBACK 1\n"); lineno++;
4121 }
drh75897232000-05-29 14:26:00 +00004122
drh3bd48ab2015-09-07 18:23:37 +00004123 /* Compute the action table, but do not output it yet. The action
4124 ** table must be computed before generating the YYNSTATE macro because
4125 ** we need to know how many states can be eliminated.
drh75897232000-05-29 14:26:00 +00004126 */
drh3bd48ab2015-09-07 18:23:37 +00004127 ax = (struct axset *) calloc(lemp->nxstate*2, sizeof(ax[0]));
drhfdbf9282003-10-21 16:34:41 +00004128 if( ax==0 ){
4129 fprintf(stderr,"malloc failed\n");
4130 exit(1);
4131 }
drh3bd48ab2015-09-07 18:23:37 +00004132 for(i=0; i<lemp->nxstate; i++){
drh75897232000-05-29 14:26:00 +00004133 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00004134 ax[i*2].stp = stp;
4135 ax[i*2].isTkn = 1;
4136 ax[i*2].nAction = stp->nTknAct;
4137 ax[i*2+1].stp = stp;
4138 ax[i*2+1].isTkn = 0;
4139 ax[i*2+1].nAction = stp->nNtAct;
drh75897232000-05-29 14:26:00 +00004140 }
drh8b582012003-10-21 13:16:03 +00004141 mxTknOfst = mnTknOfst = 0;
4142 mxNtOfst = mnNtOfst = 0;
drh3bd48ab2015-09-07 18:23:37 +00004143 /* In an effort to minimize the action table size, use the heuristic
4144 ** of placing the largest action sets first */
4145 for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i;
4146 qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare);
drh8b582012003-10-21 13:16:03 +00004147 pActtab = acttab_alloc();
drh3bd48ab2015-09-07 18:23:37 +00004148 for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){
drhfdbf9282003-10-21 16:34:41 +00004149 stp = ax[i].stp;
4150 if( ax[i].isTkn ){
4151 for(ap=stp->ap; ap; ap=ap->next){
4152 int action;
4153 if( ap->sp->index>=lemp->nterminal ) continue;
4154 action = compute_action(lemp, ap);
4155 if( action<0 ) continue;
4156 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00004157 }
drhfdbf9282003-10-21 16:34:41 +00004158 stp->iTknOfst = acttab_insert(pActtab);
4159 if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
4160 if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
4161 }else{
4162 for(ap=stp->ap; ap; ap=ap->next){
4163 int action;
4164 if( ap->sp->index<lemp->nterminal ) continue;
4165 if( ap->sp->index==lemp->nsymbol ) continue;
4166 action = compute_action(lemp, ap);
4167 if( action<0 ) continue;
4168 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00004169 }
drhfdbf9282003-10-21 16:34:41 +00004170 stp->iNtOfst = acttab_insert(pActtab);
4171 if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
4172 if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
drh8b582012003-10-21 13:16:03 +00004173 }
drh337cd0d2015-09-07 23:40:42 +00004174#if 0 /* Uncomment for a trace of how the yy_action[] table fills out */
4175 { int jj, nn;
4176 for(jj=nn=0; jj<pActtab->nAction; jj++){
4177 if( pActtab->aAction[jj].action<0 ) nn++;
4178 }
4179 printf("%4d: State %3d %s n: %2d size: %5d freespace: %d\n",
4180 i, stp->statenum, ax[i].isTkn ? "Token" : "Var ",
4181 ax[i].nAction, pActtab->nAction, nn);
4182 }
4183#endif
drh8b582012003-10-21 13:16:03 +00004184 }
drhfdbf9282003-10-21 16:34:41 +00004185 free(ax);
drh8b582012003-10-21 13:16:03 +00004186
drh756b41e2016-05-24 18:55:08 +00004187 /* Mark rules that are actually used for reduce actions after all
4188 ** optimizations have been applied
4189 */
4190 for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE;
4191 for(i=0; i<lemp->nxstate; i++){
drh756b41e2016-05-24 18:55:08 +00004192 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
4193 if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){
drh69bfa552017-04-26 04:32:17 +00004194 ap->x.rp->doesReduce = 1;
drh756b41e2016-05-24 18:55:08 +00004195 }
4196 }
4197 }
4198
drh3bd48ab2015-09-07 18:23:37 +00004199 /* Finish rendering the constants now that the action table has
4200 ** been computed */
4201 fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++;
4202 fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
drh337cd0d2015-09-07 23:40:42 +00004203 fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004204 fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",lemp->nstate); lineno++;
4205 i = lemp->nstate + lemp->nrule;
4206 fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++;
4207 fprintf(out,"#define YY_MIN_REDUCE %d\n", i); lineno++;
4208 i = lemp->nstate + lemp->nrule*2;
4209 fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++;
4210 fprintf(out,"#define YY_ERROR_ACTION %d\n", i); lineno++;
4211 fprintf(out,"#define YY_ACCEPT_ACTION %d\n", i+1); lineno++;
4212 fprintf(out,"#define YY_NO_ACTION %d\n", i+2); lineno++;
4213 tplt_xfer(lemp->name,in,out,&lineno);
4214
4215 /* Now output the action table and its associates:
4216 **
4217 ** yy_action[] A single table containing all actions.
4218 ** yy_lookahead[] A table containing the lookahead for each entry in
4219 ** yy_action. Used to detect hash collisions.
4220 ** yy_shift_ofst[] For each state, the offset into yy_action for
4221 ** shifting terminals.
4222 ** yy_reduce_ofst[] For each state, the offset into yy_action for
4223 ** shifting non-terminals after a reduce.
4224 ** yy_default[] Default action for each state.
4225 */
4226
drh8b582012003-10-21 13:16:03 +00004227 /* Output the yy_action table */
drhc75e0162015-09-07 02:23:02 +00004228 lemp->nactiontab = n = acttab_size(pActtab);
4229 lemp->tablesize += n*szActionType;
drhf16371d2009-11-03 19:18:31 +00004230 fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
4231 fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00004232 for(i=j=0; i<n; i++){
4233 int action = acttab_yyaction(pActtab, i);
drhe0479212007-01-12 23:09:23 +00004234 if( action<0 ) action = lemp->nstate + lemp->nrule + 2;
drhfdbf9282003-10-21 16:34:41 +00004235 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004236 fprintf(out, " %4d,", action);
4237 if( j==9 || i==n-1 ){
4238 fprintf(out, "\n"); lineno++;
4239 j = 0;
4240 }else{
4241 j++;
4242 }
4243 }
4244 fprintf(out, "};\n"); lineno++;
4245
4246 /* Output the yy_lookahead table */
drhc75e0162015-09-07 02:23:02 +00004247 lemp->tablesize += n*szCodeType;
drh57196282004-10-06 15:41:16 +00004248 fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00004249 for(i=j=0; i<n; i++){
4250 int la = acttab_yylookahead(pActtab, i);
4251 if( la<0 ) la = lemp->nsymbol;
drhfdbf9282003-10-21 16:34:41 +00004252 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004253 fprintf(out, " %4d,", la);
4254 if( j==9 || i==n-1 ){
4255 fprintf(out, "\n"); lineno++;
4256 j = 0;
4257 }else{
4258 j++;
4259 }
4260 }
4261 fprintf(out, "};\n"); lineno++;
4262
4263 /* Output the yy_shift_ofst[] table */
drh3bd48ab2015-09-07 18:23:37 +00004264 n = lemp->nxstate;
drhada354d2005-11-05 15:03:59 +00004265 while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
drh701b6882016-08-10 13:30:43 +00004266 fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", lemp->nactiontab); lineno++;
4267 fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
4268 fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
4269 fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
drh06f60d82017-04-14 19:46:12 +00004270 fprintf(out, "static const %s yy_shift_ofst[] = {\n",
drh701b6882016-08-10 13:30:43 +00004271 minimum_size_type(mnTknOfst, lemp->nterminal+lemp->nactiontab, &sz));
4272 lineno++;
drhc75e0162015-09-07 02:23:02 +00004273 lemp->tablesize += n*sz;
drh8b582012003-10-21 13:16:03 +00004274 for(i=j=0; i<n; i++){
4275 int ofst;
4276 stp = lemp->sorted[i];
4277 ofst = stp->iTknOfst;
drh701b6882016-08-10 13:30:43 +00004278 if( ofst==NO_OFFSET ) ofst = lemp->nactiontab;
drhfdbf9282003-10-21 16:34:41 +00004279 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004280 fprintf(out, " %4d,", ofst);
4281 if( j==9 || i==n-1 ){
4282 fprintf(out, "\n"); lineno++;
4283 j = 0;
4284 }else{
4285 j++;
4286 }
4287 }
4288 fprintf(out, "};\n"); lineno++;
4289
4290 /* Output the yy_reduce_ofst[] table */
4291 fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004292 n = lemp->nxstate;
drhada354d2005-11-05 15:03:59 +00004293 while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00004294 fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
4295 fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
4296 fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
drh06f60d82017-04-14 19:46:12 +00004297 fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
drhc75e0162015-09-07 02:23:02 +00004298 minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++;
4299 lemp->tablesize += n*sz;
drh8b582012003-10-21 13:16:03 +00004300 for(i=j=0; i<n; i++){
4301 int ofst;
4302 stp = lemp->sorted[i];
4303 ofst = stp->iNtOfst;
4304 if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00004305 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004306 fprintf(out, " %4d,", ofst);
4307 if( j==9 || i==n-1 ){
4308 fprintf(out, "\n"); lineno++;
4309 j = 0;
4310 }else{
4311 j++;
4312 }
4313 }
4314 fprintf(out, "};\n"); lineno++;
4315
4316 /* Output the default action table */
drh57196282004-10-06 15:41:16 +00004317 fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004318 n = lemp->nxstate;
drhc75e0162015-09-07 02:23:02 +00004319 lemp->tablesize += n*szActionType;
drh8b582012003-10-21 13:16:03 +00004320 for(i=j=0; i<n; i++){
4321 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00004322 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh3bd48ab2015-09-07 18:23:37 +00004323 fprintf(out, " %4d,", stp->iDfltReduce+lemp->nstate+lemp->nrule);
drh8b582012003-10-21 13:16:03 +00004324 if( j==9 || i==n-1 ){
4325 fprintf(out, "\n"); lineno++;
4326 j = 0;
4327 }else{
4328 j++;
4329 }
4330 }
4331 fprintf(out, "};\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004332 tplt_xfer(lemp->name,in,out,&lineno);
4333
drh0bd1f4e2002-06-06 18:54:39 +00004334 /* Generate the table of fallback tokens.
4335 */
4336 if( lemp->has_fallback ){
drh1441f3e2009-06-12 12:50:50 +00004337 int mx = lemp->nterminal - 1;
4338 while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; }
drhc75e0162015-09-07 02:23:02 +00004339 lemp->tablesize += (mx+1)*szCodeType;
drh1441f3e2009-06-12 12:50:50 +00004340 for(i=0; i<=mx; i++){
drh0bd1f4e2002-06-06 18:54:39 +00004341 struct symbol *p = lemp->symbols[i];
4342 if( p->fallback==0 ){
4343 fprintf(out, " 0, /* %10s => nothing */\n", p->name);
4344 }else{
4345 fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index,
4346 p->name, p->fallback->name);
4347 }
4348 lineno++;
4349 }
4350 }
4351 tplt_xfer(lemp->name, in, out, &lineno);
4352
4353 /* Generate a table containing the symbolic name of every symbol
4354 */
drh75897232000-05-29 14:26:00 +00004355 for(i=0; i<lemp->nsymbol; i++){
drh898799f2014-01-10 23:21:00 +00004356 lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name);
drh75897232000-05-29 14:26:00 +00004357 fprintf(out," %-15s",line);
4358 if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
4359 }
4360 if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
4361 tplt_xfer(lemp->name,in,out,&lineno);
4362
drh0bd1f4e2002-06-06 18:54:39 +00004363 /* Generate a table containing a text string that describes every
drh34ff57b2008-07-14 12:27:51 +00004364 ** rule in the rule set of the grammar. This information is used
drh0bd1f4e2002-06-06 18:54:39 +00004365 ** when tracing REDUCE actions.
4366 */
4367 for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
drh4ef07702016-03-16 19:45:54 +00004368 assert( rp->iRule==i );
drhc4dd3fd2008-01-22 01:48:05 +00004369 fprintf(out," /* %3d */ \"", i);
4370 writeRuleText(out, rp);
drh0bd1f4e2002-06-06 18:54:39 +00004371 fprintf(out,"\",\n"); lineno++;
4372 }
4373 tplt_xfer(lemp->name,in,out,&lineno);
4374
drh75897232000-05-29 14:26:00 +00004375 /* Generate code which executes every time a symbol is popped from
drh06f60d82017-04-14 19:46:12 +00004376 ** the stack while processing errors or while destroying the parser.
drh0bd1f4e2002-06-06 18:54:39 +00004377 ** (In other words, generate the %destructor actions)
4378 */
drh75897232000-05-29 14:26:00 +00004379 if( lemp->tokendest ){
drh4dc8ef52008-07-01 17:13:57 +00004380 int once = 1;
drh75897232000-05-29 14:26:00 +00004381 for(i=0; i<lemp->nsymbol; i++){
4382 struct symbol *sp = lemp->symbols[i];
4383 if( sp==0 || sp->type!=TERMINAL ) continue;
drh4dc8ef52008-07-01 17:13:57 +00004384 if( once ){
4385 fprintf(out, " /* TERMINAL Destructor */\n"); lineno++;
4386 once = 0;
4387 }
drhc53eed12009-06-12 17:46:19 +00004388 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh75897232000-05-29 14:26:00 +00004389 }
4390 for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
4391 if( i<lemp->nsymbol ){
4392 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4393 fprintf(out," break;\n"); lineno++;
4394 }
4395 }
drh8d659732005-01-13 23:54:06 +00004396 if( lemp->vardest ){
4397 struct symbol *dflt_sp = 0;
drh4dc8ef52008-07-01 17:13:57 +00004398 int once = 1;
drh8d659732005-01-13 23:54:06 +00004399 for(i=0; i<lemp->nsymbol; i++){
4400 struct symbol *sp = lemp->symbols[i];
4401 if( sp==0 || sp->type==TERMINAL ||
4402 sp->index<=0 || sp->destructor!=0 ) continue;
drh4dc8ef52008-07-01 17:13:57 +00004403 if( once ){
4404 fprintf(out, " /* Default NON-TERMINAL Destructor */\n"); lineno++;
4405 once = 0;
4406 }
drhc53eed12009-06-12 17:46:19 +00004407 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh8d659732005-01-13 23:54:06 +00004408 dflt_sp = sp;
4409 }
4410 if( dflt_sp!=0 ){
4411 emit_destructor_code(out,dflt_sp,lemp,&lineno);
drh8d659732005-01-13 23:54:06 +00004412 }
drh4dc8ef52008-07-01 17:13:57 +00004413 fprintf(out," break;\n"); lineno++;
drh8d659732005-01-13 23:54:06 +00004414 }
drh75897232000-05-29 14:26:00 +00004415 for(i=0; i<lemp->nsymbol; i++){
4416 struct symbol *sp = lemp->symbols[i];
4417 if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
drh0f832dd2016-08-16 16:46:40 +00004418 if( sp->destLineno<0 ) continue; /* Already emitted */
drh75013012009-06-12 15:47:34 +00004419 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004420
4421 /* Combine duplicate destructors into a single case */
4422 for(j=i+1; j<lemp->nsymbol; j++){
4423 struct symbol *sp2 = lemp->symbols[j];
4424 if( sp2 && sp2->type!=TERMINAL && sp2->destructor
4425 && sp2->dtnum==sp->dtnum
4426 && strcmp(sp->destructor,sp2->destructor)==0 ){
drhc53eed12009-06-12 17:46:19 +00004427 fprintf(out," case %d: /* %s */\n",
4428 sp2->index, sp2->name); lineno++;
drh0f832dd2016-08-16 16:46:40 +00004429 sp2->destLineno = -1; /* Avoid emitting this destructor again */
drh0bb132b2004-07-20 14:06:51 +00004430 }
4431 }
4432
drh75897232000-05-29 14:26:00 +00004433 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4434 fprintf(out," break;\n"); lineno++;
4435 }
drh75897232000-05-29 14:26:00 +00004436 tplt_xfer(lemp->name,in,out,&lineno);
4437
4438 /* Generate code which executes whenever the parser stack overflows */
drha5808f32008-04-27 22:19:44 +00004439 tplt_print(out,lemp,lemp->overflow,&lineno);
drh75897232000-05-29 14:26:00 +00004440 tplt_xfer(lemp->name,in,out,&lineno);
4441
drh06f60d82017-04-14 19:46:12 +00004442 /* Generate the table of rule information
drh75897232000-05-29 14:26:00 +00004443 **
4444 ** Note: This code depends on the fact that rules are number
4445 ** sequentually beginning with 0.
4446 */
4447 for(rp=lemp->rule; rp; rp=rp->next){
drh6be95362017-06-28 13:47:56 +00004448 fprintf(out," { %d, %d },\n",rp->lhs->index,-rp->nrhs); lineno++;
drh75897232000-05-29 14:26:00 +00004449 }
4450 tplt_xfer(lemp->name,in,out,&lineno);
4451
4452 /* Generate code which execution during each REDUCE action */
drhdabd04c2016-02-17 01:46:19 +00004453 i = 0;
drh75897232000-05-29 14:26:00 +00004454 for(rp=lemp->rule; rp; rp=rp->next){
drhdabd04c2016-02-17 01:46:19 +00004455 i += translate_code(lemp, rp);
4456 }
4457 if( i ){
4458 fprintf(out," YYMINORTYPE yylhsminor;\n"); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004459 }
drhc53eed12009-06-12 17:46:19 +00004460 /* First output rules other than the default: rule */
drh0bb132b2004-07-20 14:06:51 +00004461 for(rp=lemp->rule; rp; rp=rp->next){
drhc53eed12009-06-12 17:46:19 +00004462 struct rule *rp2; /* Other rules with the same action */
drh711c9812016-05-23 14:24:31 +00004463 if( rp->codeEmitted ) continue;
4464 if( rp->noCode ){
4465 /* No C code actions, so this will be part of the "default:" rule */
drh2e55b042016-04-30 17:19:30 +00004466 continue;
4467 }
drh4ef07702016-03-16 19:45:54 +00004468 fprintf(out," case %d: /* ", rp->iRule);
drhc4dd3fd2008-01-22 01:48:05 +00004469 writeRuleText(out, rp);
4470 fprintf(out, " */\n"); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004471 for(rp2=rp->next; rp2; rp2=rp2->next){
drhafb8cd92016-04-29 11:28:35 +00004472 if( rp2->code==rp->code && rp2->codePrefix==rp->codePrefix
4473 && rp2->codeSuffix==rp->codeSuffix ){
drh4ef07702016-03-16 19:45:54 +00004474 fprintf(out," case %d: /* ", rp2->iRule);
drhc4dd3fd2008-01-22 01:48:05 +00004475 writeRuleText(out, rp2);
drh4ef07702016-03-16 19:45:54 +00004476 fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->iRule); lineno++;
drh711c9812016-05-23 14:24:31 +00004477 rp2->codeEmitted = 1;
drh0bb132b2004-07-20 14:06:51 +00004478 }
4479 }
drh75897232000-05-29 14:26:00 +00004480 emit_code(out,rp,lemp,&lineno);
4481 fprintf(out," break;\n"); lineno++;
drh711c9812016-05-23 14:24:31 +00004482 rp->codeEmitted = 1;
drh75897232000-05-29 14:26:00 +00004483 }
drhc53eed12009-06-12 17:46:19 +00004484 /* Finally, output the default: rule. We choose as the default: all
4485 ** empty actions. */
4486 fprintf(out," default:\n"); lineno++;
4487 for(rp=lemp->rule; rp; rp=rp->next){
drh711c9812016-05-23 14:24:31 +00004488 if( rp->codeEmitted ) continue;
4489 assert( rp->noCode );
drh4ef07702016-03-16 19:45:54 +00004490 fprintf(out," /* (%d) ", rp->iRule);
drhc53eed12009-06-12 17:46:19 +00004491 writeRuleText(out, rp);
drh756b41e2016-05-24 18:55:08 +00004492 if( rp->doesReduce ){
4493 fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->iRule); lineno++;
4494 }else{
4495 fprintf(out, " (OPTIMIZED OUT) */ assert(yyruleno!=%d);\n",
4496 rp->iRule); lineno++;
4497 }
drhc53eed12009-06-12 17:46:19 +00004498 }
4499 fprintf(out," break;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004500 tplt_xfer(lemp->name,in,out,&lineno);
4501
4502 /* Generate code which executes if a parse fails */
drha5808f32008-04-27 22:19:44 +00004503 tplt_print(out,lemp,lemp->failure,&lineno);
drh75897232000-05-29 14:26:00 +00004504 tplt_xfer(lemp->name,in,out,&lineno);
4505
4506 /* Generate code which executes when a syntax error occurs */
drha5808f32008-04-27 22:19:44 +00004507 tplt_print(out,lemp,lemp->error,&lineno);
drh75897232000-05-29 14:26:00 +00004508 tplt_xfer(lemp->name,in,out,&lineno);
4509
4510 /* Generate code which executes when the parser accepts its input */
drha5808f32008-04-27 22:19:44 +00004511 tplt_print(out,lemp,lemp->accept,&lineno);
drh75897232000-05-29 14:26:00 +00004512 tplt_xfer(lemp->name,in,out,&lineno);
4513
4514 /* Append any addition code the user desires */
drha5808f32008-04-27 22:19:44 +00004515 tplt_print(out,lemp,lemp->extracode,&lineno);
drh75897232000-05-29 14:26:00 +00004516
4517 fclose(in);
4518 fclose(out);
4519 return;
4520}
4521
4522/* Generate a header file for the parser */
icculus9e44cf12010-02-14 17:14:22 +00004523void ReportHeader(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004524{
4525 FILE *out, *in;
icculus9e44cf12010-02-14 17:14:22 +00004526 const char *prefix;
drh75897232000-05-29 14:26:00 +00004527 char line[LINESIZE];
4528 char pattern[LINESIZE];
4529 int i;
4530
4531 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4532 else prefix = "";
drh2aa6ca42004-09-10 00:14:04 +00004533 in = file_open(lemp,".h","rb");
drh75897232000-05-29 14:26:00 +00004534 if( in ){
drh8ba0d1c2012-06-16 15:26:31 +00004535 int nextChar;
drh75897232000-05-29 14:26:00 +00004536 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
drh61f92cd2014-01-11 03:06:18 +00004537 lemon_sprintf(pattern,"#define %s%-30s %3d\n",
4538 prefix,lemp->symbols[i]->name,i);
drh75897232000-05-29 14:26:00 +00004539 if( strcmp(line,pattern) ) break;
4540 }
drh8ba0d1c2012-06-16 15:26:31 +00004541 nextChar = fgetc(in);
drh75897232000-05-29 14:26:00 +00004542 fclose(in);
drh8ba0d1c2012-06-16 15:26:31 +00004543 if( i==lemp->nterminal && nextChar==EOF ){
drh75897232000-05-29 14:26:00 +00004544 /* No change in the file. Don't rewrite it. */
4545 return;
4546 }
4547 }
drh2aa6ca42004-09-10 00:14:04 +00004548 out = file_open(lemp,".h","wb");
drh75897232000-05-29 14:26:00 +00004549 if( out ){
4550 for(i=1; i<lemp->nterminal; i++){
drh61f92cd2014-01-11 03:06:18 +00004551 fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i);
drh75897232000-05-29 14:26:00 +00004552 }
drh06f60d82017-04-14 19:46:12 +00004553 fclose(out);
drh75897232000-05-29 14:26:00 +00004554 }
4555 return;
4556}
4557
4558/* Reduce the size of the action tables, if possible, by making use
4559** of defaults.
4560**
drhb59499c2002-02-23 18:45:13 +00004561** In this version, we take the most frequent REDUCE action and make
drhe09daa92006-06-10 13:29:31 +00004562** it the default. Except, there is no default if the wildcard token
4563** is a possible look-ahead.
drh75897232000-05-29 14:26:00 +00004564*/
icculus9e44cf12010-02-14 17:14:22 +00004565void CompressTables(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004566{
4567 struct state *stp;
drhc173ad82016-05-23 16:15:02 +00004568 struct action *ap, *ap2, *nextap;
drhb59499c2002-02-23 18:45:13 +00004569 struct rule *rp, *rp2, *rbest;
drh0c6dfaa2015-09-08 21:16:46 +00004570 int nbest, n;
drh75897232000-05-29 14:26:00 +00004571 int i;
drhe09daa92006-06-10 13:29:31 +00004572 int usesWildcard;
drh75897232000-05-29 14:26:00 +00004573
4574 for(i=0; i<lemp->nstate; i++){
4575 stp = lemp->sorted[i];
drhb59499c2002-02-23 18:45:13 +00004576 nbest = 0;
4577 rbest = 0;
drhe09daa92006-06-10 13:29:31 +00004578 usesWildcard = 0;
drh75897232000-05-29 14:26:00 +00004579
drhb59499c2002-02-23 18:45:13 +00004580 for(ap=stp->ap; ap; ap=ap->next){
drhe09daa92006-06-10 13:29:31 +00004581 if( ap->type==SHIFT && ap->sp==lemp->wildcard ){
4582 usesWildcard = 1;
4583 }
drhb59499c2002-02-23 18:45:13 +00004584 if( ap->type!=REDUCE ) continue;
4585 rp = ap->x.rp;
drhb4960992007-10-05 16:16:36 +00004586 if( rp->lhsStart ) continue;
drhb59499c2002-02-23 18:45:13 +00004587 if( rp==rbest ) continue;
4588 n = 1;
4589 for(ap2=ap->next; ap2; ap2=ap2->next){
4590 if( ap2->type!=REDUCE ) continue;
4591 rp2 = ap2->x.rp;
4592 if( rp2==rbest ) continue;
4593 if( rp2==rp ) n++;
4594 }
4595 if( n>nbest ){
4596 nbest = n;
4597 rbest = rp;
drh75897232000-05-29 14:26:00 +00004598 }
4599 }
drh06f60d82017-04-14 19:46:12 +00004600
drhb59499c2002-02-23 18:45:13 +00004601 /* Do not make a default if the number of rules to default
drhe09daa92006-06-10 13:29:31 +00004602 ** is not at least 1 or if the wildcard token is a possible
4603 ** lookahead.
4604 */
4605 if( nbest<1 || usesWildcard ) continue;
drh75897232000-05-29 14:26:00 +00004606
drhb59499c2002-02-23 18:45:13 +00004607
4608 /* Combine matching REDUCE actions into a single default */
4609 for(ap=stp->ap; ap; ap=ap->next){
4610 if( ap->type==REDUCE && ap->x.rp==rbest ) break;
4611 }
drh75897232000-05-29 14:26:00 +00004612 assert( ap );
4613 ap->sp = Symbol_new("{default}");
4614 for(ap=ap->next; ap; ap=ap->next){
drhb59499c2002-02-23 18:45:13 +00004615 if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
drh75897232000-05-29 14:26:00 +00004616 }
4617 stp->ap = Action_sort(stp->ap);
drh3bd48ab2015-09-07 18:23:37 +00004618
4619 for(ap=stp->ap; ap; ap=ap->next){
4620 if( ap->type==SHIFT ) break;
4621 if( ap->type==REDUCE && ap->x.rp!=rbest ) break;
4622 }
4623 if( ap==0 ){
4624 stp->autoReduce = 1;
4625 stp->pDfltReduce = rbest;
4626 }
4627 }
4628
4629 /* Make a second pass over all states and actions. Convert
4630 ** every action that is a SHIFT to an autoReduce state into
4631 ** a SHIFTREDUCE action.
4632 */
4633 for(i=0; i<lemp->nstate; i++){
4634 stp = lemp->sorted[i];
4635 for(ap=stp->ap; ap; ap=ap->next){
4636 struct state *pNextState;
4637 if( ap->type!=SHIFT ) continue;
4638 pNextState = ap->x.stp;
4639 if( pNextState->autoReduce && pNextState->pDfltReduce!=0 ){
4640 ap->type = SHIFTREDUCE;
4641 ap->x.rp = pNextState->pDfltReduce;
4642 }
4643 }
drh75897232000-05-29 14:26:00 +00004644 }
drhc173ad82016-05-23 16:15:02 +00004645
4646 /* If a SHIFTREDUCE action specifies a rule that has a single RHS term
4647 ** (meaning that the SHIFTREDUCE will land back in the state where it
4648 ** started) and if there is no C-code associated with the reduce action,
4649 ** then we can go ahead and convert the action to be the same as the
4650 ** action for the RHS of the rule.
4651 */
4652 for(i=0; i<lemp->nstate; i++){
4653 stp = lemp->sorted[i];
4654 for(ap=stp->ap; ap; ap=nextap){
4655 nextap = ap->next;
4656 if( ap->type!=SHIFTREDUCE ) continue;
4657 rp = ap->x.rp;
4658 if( rp->noCode==0 ) continue;
4659 if( rp->nrhs!=1 ) continue;
4660#if 1
4661 /* Only apply this optimization to non-terminals. It would be OK to
4662 ** apply it to terminal symbols too, but that makes the parser tables
4663 ** larger. */
4664 if( ap->sp->index<lemp->nterminal ) continue;
4665#endif
4666 /* If we reach this point, it means the optimization can be applied */
4667 nextap = ap;
4668 for(ap2=stp->ap; ap2 && (ap2==ap || ap2->sp!=rp->lhs); ap2=ap2->next){}
4669 assert( ap2!=0 );
4670 ap->spOpt = ap2->sp;
4671 ap->type = ap2->type;
4672 ap->x = ap2->x;
4673 }
4674 }
drh75897232000-05-29 14:26:00 +00004675}
drhb59499c2002-02-23 18:45:13 +00004676
drhada354d2005-11-05 15:03:59 +00004677
4678/*
4679** Compare two states for sorting purposes. The smaller state is the
4680** one with the most non-terminal actions. If they have the same number
4681** of non-terminal actions, then the smaller is the one with the most
4682** token actions.
4683*/
4684static int stateResortCompare(const void *a, const void *b){
4685 const struct state *pA = *(const struct state**)a;
4686 const struct state *pB = *(const struct state**)b;
4687 int n;
4688
4689 n = pB->nNtAct - pA->nNtAct;
4690 if( n==0 ){
4691 n = pB->nTknAct - pA->nTknAct;
drhe594bc32009-11-03 13:02:25 +00004692 if( n==0 ){
4693 n = pB->statenum - pA->statenum;
4694 }
drhada354d2005-11-05 15:03:59 +00004695 }
drhe594bc32009-11-03 13:02:25 +00004696 assert( n!=0 );
drhada354d2005-11-05 15:03:59 +00004697 return n;
4698}
4699
4700
4701/*
4702** Renumber and resort states so that states with fewer choices
4703** occur at the end. Except, keep state 0 as the first state.
4704*/
icculus9e44cf12010-02-14 17:14:22 +00004705void ResortStates(struct lemon *lemp)
drhada354d2005-11-05 15:03:59 +00004706{
4707 int i;
4708 struct state *stp;
4709 struct action *ap;
4710
4711 for(i=0; i<lemp->nstate; i++){
4712 stp = lemp->sorted[i];
4713 stp->nTknAct = stp->nNtAct = 0;
drh3bd48ab2015-09-07 18:23:37 +00004714 stp->iDfltReduce = lemp->nrule; /* Init dflt action to "syntax error" */
drhada354d2005-11-05 15:03:59 +00004715 stp->iTknOfst = NO_OFFSET;
4716 stp->iNtOfst = NO_OFFSET;
4717 for(ap=stp->ap; ap; ap=ap->next){
drh3bd48ab2015-09-07 18:23:37 +00004718 int iAction = compute_action(lemp,ap);
4719 if( iAction>=0 ){
drhada354d2005-11-05 15:03:59 +00004720 if( ap->sp->index<lemp->nterminal ){
4721 stp->nTknAct++;
4722 }else if( ap->sp->index<lemp->nsymbol ){
4723 stp->nNtAct++;
4724 }else{
drh3bd48ab2015-09-07 18:23:37 +00004725 assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp );
4726 stp->iDfltReduce = iAction - lemp->nstate - lemp->nrule;
drhada354d2005-11-05 15:03:59 +00004727 }
4728 }
4729 }
4730 }
4731 qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
4732 stateResortCompare);
4733 for(i=0; i<lemp->nstate; i++){
4734 lemp->sorted[i]->statenum = i;
4735 }
drh3bd48ab2015-09-07 18:23:37 +00004736 lemp->nxstate = lemp->nstate;
4737 while( lemp->nxstate>1 && lemp->sorted[lemp->nxstate-1]->autoReduce ){
4738 lemp->nxstate--;
4739 }
drhada354d2005-11-05 15:03:59 +00004740}
4741
4742
drh75897232000-05-29 14:26:00 +00004743/***************** From the file "set.c" ************************************/
4744/*
4745** Set manipulation routines for the LEMON parser generator.
4746*/
4747
4748static int size = 0;
4749
4750/* Set the set size */
icculus9e44cf12010-02-14 17:14:22 +00004751void SetSize(int n)
drh75897232000-05-29 14:26:00 +00004752{
4753 size = n+1;
4754}
4755
4756/* Allocate a new set */
drh14d88552017-04-14 19:44:15 +00004757char *SetNew(void){
drh75897232000-05-29 14:26:00 +00004758 char *s;
drh9892c5d2007-12-21 00:02:11 +00004759 s = (char*)calloc( size, 1);
drh75897232000-05-29 14:26:00 +00004760 if( s==0 ){
4761 extern void memory_error();
4762 memory_error();
4763 }
drh75897232000-05-29 14:26:00 +00004764 return s;
4765}
4766
4767/* Deallocate a set */
icculus9e44cf12010-02-14 17:14:22 +00004768void SetFree(char *s)
drh75897232000-05-29 14:26:00 +00004769{
4770 free(s);
4771}
4772
4773/* Add a new element to the set. Return TRUE if the element was added
4774** and FALSE if it was already there. */
icculus9e44cf12010-02-14 17:14:22 +00004775int SetAdd(char *s, int e)
drh75897232000-05-29 14:26:00 +00004776{
4777 int rv;
drh9892c5d2007-12-21 00:02:11 +00004778 assert( e>=0 && e<size );
drh75897232000-05-29 14:26:00 +00004779 rv = s[e];
4780 s[e] = 1;
4781 return !rv;
4782}
4783
4784/* Add every element of s2 to s1. Return TRUE if s1 changes. */
icculus9e44cf12010-02-14 17:14:22 +00004785int SetUnion(char *s1, char *s2)
drh75897232000-05-29 14:26:00 +00004786{
4787 int i, progress;
4788 progress = 0;
4789 for(i=0; i<size; i++){
4790 if( s2[i]==0 ) continue;
4791 if( s1[i]==0 ){
4792 progress = 1;
4793 s1[i] = 1;
4794 }
4795 }
4796 return progress;
4797}
4798/********************** From the file "table.c" ****************************/
4799/*
4800** All code in this file has been automatically generated
4801** from a specification in the file
4802** "table.q"
4803** by the associative array code building program "aagen".
4804** Do not edit this file! Instead, edit the specification
4805** file, then rerun aagen.
4806*/
4807/*
4808** Code for processing tables in the LEMON parser generator.
4809*/
4810
drh01f75f22013-10-02 20:46:30 +00004811PRIVATE unsigned strhash(const char *x)
drh75897232000-05-29 14:26:00 +00004812{
drh01f75f22013-10-02 20:46:30 +00004813 unsigned h = 0;
4814 while( *x ) h = h*13 + *(x++);
drh75897232000-05-29 14:26:00 +00004815 return h;
4816}
4817
4818/* Works like strdup, sort of. Save a string in malloced memory, but
4819** keep strings in a table so that the same string is not in more
4820** than one place.
4821*/
icculus9e44cf12010-02-14 17:14:22 +00004822const char *Strsafe(const char *y)
drh75897232000-05-29 14:26:00 +00004823{
icculus9e44cf12010-02-14 17:14:22 +00004824 const char *z;
4825 char *cpy;
drh75897232000-05-29 14:26:00 +00004826
drh916f75f2006-07-17 00:19:39 +00004827 if( y==0 ) return 0;
drh75897232000-05-29 14:26:00 +00004828 z = Strsafe_find(y);
icculus9e44cf12010-02-14 17:14:22 +00004829 if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
drh898799f2014-01-10 23:21:00 +00004830 lemon_strcpy(cpy,y);
icculus9e44cf12010-02-14 17:14:22 +00004831 z = cpy;
drh75897232000-05-29 14:26:00 +00004832 Strsafe_insert(z);
4833 }
4834 MemoryCheck(z);
4835 return z;
4836}
4837
4838/* There is one instance of the following structure for each
4839** associative array of type "x1".
4840*/
4841struct s_x1 {
4842 int size; /* The number of available slots. */
4843 /* Must be a power of 2 greater than or */
4844 /* equal to 1 */
4845 int count; /* Number of currently slots filled */
4846 struct s_x1node *tbl; /* The data stored here */
4847 struct s_x1node **ht; /* Hash table for lookups */
4848};
4849
4850/* There is one instance of this structure for every data element
4851** in an associative array of type "x1".
4852*/
4853typedef struct s_x1node {
icculus9e44cf12010-02-14 17:14:22 +00004854 const char *data; /* The data */
drh75897232000-05-29 14:26:00 +00004855 struct s_x1node *next; /* Next entry with the same hash */
4856 struct s_x1node **from; /* Previous link */
4857} x1node;
4858
4859/* There is only one instance of the array, which is the following */
4860static struct s_x1 *x1a;
4861
4862/* Allocate a new associative array */
drh14d88552017-04-14 19:44:15 +00004863void Strsafe_init(void){
drh75897232000-05-29 14:26:00 +00004864 if( x1a ) return;
4865 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
4866 if( x1a ){
4867 x1a->size = 1024;
4868 x1a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00004869 x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*));
drh75897232000-05-29 14:26:00 +00004870 if( x1a->tbl==0 ){
4871 free(x1a);
4872 x1a = 0;
4873 }else{
4874 int i;
4875 x1a->ht = (x1node**)&(x1a->tbl[1024]);
4876 for(i=0; i<1024; i++) x1a->ht[i] = 0;
4877 }
4878 }
4879}
4880/* Insert a new record into the array. Return TRUE if successful.
4881** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004882int Strsafe_insert(const char *data)
drh75897232000-05-29 14:26:00 +00004883{
4884 x1node *np;
drh01f75f22013-10-02 20:46:30 +00004885 unsigned h;
4886 unsigned ph;
drh75897232000-05-29 14:26:00 +00004887
4888 if( x1a==0 ) return 0;
4889 ph = strhash(data);
4890 h = ph & (x1a->size-1);
4891 np = x1a->ht[h];
4892 while( np ){
4893 if( strcmp(np->data,data)==0 ){
4894 /* An existing entry with the same key is found. */
4895 /* Fail because overwrite is not allows. */
4896 return 0;
4897 }
4898 np = np->next;
4899 }
4900 if( x1a->count>=x1a->size ){
4901 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00004902 int i,arrSize;
drh75897232000-05-29 14:26:00 +00004903 struct s_x1 array;
mistachkin8e189222015-04-19 21:43:16 +00004904 array.size = arrSize = x1a->size*2;
drh75897232000-05-29 14:26:00 +00004905 array.count = x1a->count;
mistachkin8e189222015-04-19 21:43:16 +00004906 array.tbl = (x1node*)calloc(arrSize, sizeof(x1node) + sizeof(x1node*));
drh75897232000-05-29 14:26:00 +00004907 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00004908 array.ht = (x1node**)&(array.tbl[arrSize]);
4909 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00004910 for(i=0; i<x1a->count; i++){
4911 x1node *oldnp, *newnp;
4912 oldnp = &(x1a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00004913 h = strhash(oldnp->data) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00004914 newnp = &(array.tbl[i]);
4915 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4916 newnp->next = array.ht[h];
4917 newnp->data = oldnp->data;
4918 newnp->from = &(array.ht[h]);
4919 array.ht[h] = newnp;
4920 }
4921 free(x1a->tbl);
4922 *x1a = array;
4923 }
4924 /* Insert the new data */
4925 h = ph & (x1a->size-1);
4926 np = &(x1a->tbl[x1a->count++]);
4927 np->data = data;
4928 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
4929 np->next = x1a->ht[h];
4930 x1a->ht[h] = np;
4931 np->from = &(x1a->ht[h]);
4932 return 1;
4933}
4934
4935/* Return a pointer to data assigned to the given key. Return NULL
4936** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004937const char *Strsafe_find(const char *key)
drh75897232000-05-29 14:26:00 +00004938{
drh01f75f22013-10-02 20:46:30 +00004939 unsigned h;
drh75897232000-05-29 14:26:00 +00004940 x1node *np;
4941
4942 if( x1a==0 ) return 0;
4943 h = strhash(key) & (x1a->size-1);
4944 np = x1a->ht[h];
4945 while( np ){
4946 if( strcmp(np->data,key)==0 ) break;
4947 np = np->next;
4948 }
4949 return np ? np->data : 0;
4950}
4951
4952/* Return a pointer to the (terminal or nonterminal) symbol "x".
4953** Create a new symbol if this is the first time "x" has been seen.
4954*/
icculus9e44cf12010-02-14 17:14:22 +00004955struct symbol *Symbol_new(const char *x)
drh75897232000-05-29 14:26:00 +00004956{
4957 struct symbol *sp;
4958
4959 sp = Symbol_find(x);
4960 if( sp==0 ){
drh9892c5d2007-12-21 00:02:11 +00004961 sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
drh75897232000-05-29 14:26:00 +00004962 MemoryCheck(sp);
4963 sp->name = Strsafe(x);
drhc56fac72015-10-29 13:48:15 +00004964 sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL;
drh75897232000-05-29 14:26:00 +00004965 sp->rule = 0;
drh0bd1f4e2002-06-06 18:54:39 +00004966 sp->fallback = 0;
drh75897232000-05-29 14:26:00 +00004967 sp->prec = -1;
4968 sp->assoc = UNK;
4969 sp->firstset = 0;
drhaa9f1122007-08-23 02:50:56 +00004970 sp->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00004971 sp->destructor = 0;
drh4dc8ef52008-07-01 17:13:57 +00004972 sp->destLineno = 0;
drh75897232000-05-29 14:26:00 +00004973 sp->datatype = 0;
drhc4dd3fd2008-01-22 01:48:05 +00004974 sp->useCnt = 0;
drh75897232000-05-29 14:26:00 +00004975 Symbol_insert(sp,sp->name);
4976 }
drhc4dd3fd2008-01-22 01:48:05 +00004977 sp->useCnt++;
drh75897232000-05-29 14:26:00 +00004978 return sp;
4979}
4980
drh61f92cd2014-01-11 03:06:18 +00004981/* Compare two symbols for sorting purposes. Return negative,
4982** zero, or positive if a is less then, equal to, or greater
4983** than b.
drh60d31652004-02-22 00:08:04 +00004984**
4985** Symbols that begin with upper case letters (terminals or tokens)
4986** must sort before symbols that begin with lower case letters
drh61f92cd2014-01-11 03:06:18 +00004987** (non-terminals). And MULTITERMINAL symbols (created using the
4988** %token_class directive) must sort at the very end. Other than
4989** that, the order does not matter.
drh60d31652004-02-22 00:08:04 +00004990**
4991** We find experimentally that leaving the symbols in their original
4992** order (the order they appeared in the grammar file) gives the
4993** smallest parser tables in SQLite.
4994*/
icculus9e44cf12010-02-14 17:14:22 +00004995int Symbolcmpp(const void *_a, const void *_b)
4996{
drh61f92cd2014-01-11 03:06:18 +00004997 const struct symbol *a = *(const struct symbol **) _a;
4998 const struct symbol *b = *(const struct symbol **) _b;
4999 int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1;
5000 int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1;
5001 return i1==i2 ? a->index - b->index : i1 - i2;
drh75897232000-05-29 14:26:00 +00005002}
5003
5004/* There is one instance of the following structure for each
5005** associative array of type "x2".
5006*/
5007struct s_x2 {
5008 int size; /* The number of available slots. */
5009 /* Must be a power of 2 greater than or */
5010 /* equal to 1 */
5011 int count; /* Number of currently slots filled */
5012 struct s_x2node *tbl; /* The data stored here */
5013 struct s_x2node **ht; /* Hash table for lookups */
5014};
5015
5016/* There is one instance of this structure for every data element
5017** in an associative array of type "x2".
5018*/
5019typedef struct s_x2node {
icculus9e44cf12010-02-14 17:14:22 +00005020 struct symbol *data; /* The data */
5021 const char *key; /* The key */
drh75897232000-05-29 14:26:00 +00005022 struct s_x2node *next; /* Next entry with the same hash */
5023 struct s_x2node **from; /* Previous link */
5024} x2node;
5025
5026/* There is only one instance of the array, which is the following */
5027static struct s_x2 *x2a;
5028
5029/* Allocate a new associative array */
drh14d88552017-04-14 19:44:15 +00005030void Symbol_init(void){
drh75897232000-05-29 14:26:00 +00005031 if( x2a ) return;
5032 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
5033 if( x2a ){
5034 x2a->size = 128;
5035 x2a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005036 x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*));
drh75897232000-05-29 14:26:00 +00005037 if( x2a->tbl==0 ){
5038 free(x2a);
5039 x2a = 0;
5040 }else{
5041 int i;
5042 x2a->ht = (x2node**)&(x2a->tbl[128]);
5043 for(i=0; i<128; i++) x2a->ht[i] = 0;
5044 }
5045 }
5046}
5047/* Insert a new record into the array. Return TRUE if successful.
5048** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005049int Symbol_insert(struct symbol *data, const char *key)
drh75897232000-05-29 14:26:00 +00005050{
5051 x2node *np;
drh01f75f22013-10-02 20:46:30 +00005052 unsigned h;
5053 unsigned ph;
drh75897232000-05-29 14:26:00 +00005054
5055 if( x2a==0 ) return 0;
5056 ph = strhash(key);
5057 h = ph & (x2a->size-1);
5058 np = x2a->ht[h];
5059 while( np ){
5060 if( strcmp(np->key,key)==0 ){
5061 /* An existing entry with the same key is found. */
5062 /* Fail because overwrite is not allows. */
5063 return 0;
5064 }
5065 np = np->next;
5066 }
5067 if( x2a->count>=x2a->size ){
5068 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005069 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005070 struct s_x2 array;
mistachkin8e189222015-04-19 21:43:16 +00005071 array.size = arrSize = x2a->size*2;
drh75897232000-05-29 14:26:00 +00005072 array.count = x2a->count;
mistachkin8e189222015-04-19 21:43:16 +00005073 array.tbl = (x2node*)calloc(arrSize, sizeof(x2node) + sizeof(x2node*));
drh75897232000-05-29 14:26:00 +00005074 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005075 array.ht = (x2node**)&(array.tbl[arrSize]);
5076 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005077 for(i=0; i<x2a->count; i++){
5078 x2node *oldnp, *newnp;
5079 oldnp = &(x2a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005080 h = strhash(oldnp->key) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005081 newnp = &(array.tbl[i]);
5082 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5083 newnp->next = array.ht[h];
5084 newnp->key = oldnp->key;
5085 newnp->data = oldnp->data;
5086 newnp->from = &(array.ht[h]);
5087 array.ht[h] = newnp;
5088 }
5089 free(x2a->tbl);
5090 *x2a = array;
5091 }
5092 /* Insert the new data */
5093 h = ph & (x2a->size-1);
5094 np = &(x2a->tbl[x2a->count++]);
5095 np->key = key;
5096 np->data = data;
5097 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
5098 np->next = x2a->ht[h];
5099 x2a->ht[h] = np;
5100 np->from = &(x2a->ht[h]);
5101 return 1;
5102}
5103
5104/* Return a pointer to data assigned to the given key. Return NULL
5105** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005106struct symbol *Symbol_find(const char *key)
drh75897232000-05-29 14:26:00 +00005107{
drh01f75f22013-10-02 20:46:30 +00005108 unsigned h;
drh75897232000-05-29 14:26:00 +00005109 x2node *np;
5110
5111 if( x2a==0 ) return 0;
5112 h = strhash(key) & (x2a->size-1);
5113 np = x2a->ht[h];
5114 while( np ){
5115 if( strcmp(np->key,key)==0 ) break;
5116 np = np->next;
5117 }
5118 return np ? np->data : 0;
5119}
5120
5121/* Return the n-th data. Return NULL if n is out of range. */
icculus9e44cf12010-02-14 17:14:22 +00005122struct symbol *Symbol_Nth(int n)
drh75897232000-05-29 14:26:00 +00005123{
5124 struct symbol *data;
5125 if( x2a && n>0 && n<=x2a->count ){
5126 data = x2a->tbl[n-1].data;
5127 }else{
5128 data = 0;
5129 }
5130 return data;
5131}
5132
5133/* Return the size of the array */
5134int Symbol_count()
5135{
5136 return x2a ? x2a->count : 0;
5137}
5138
5139/* Return an array of pointers to all data in the table.
5140** The array is obtained from malloc. Return NULL if memory allocation
5141** problems, or if the array is empty. */
5142struct symbol **Symbol_arrayof()
5143{
5144 struct symbol **array;
mistachkin8e189222015-04-19 21:43:16 +00005145 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005146 if( x2a==0 ) return 0;
mistachkin8e189222015-04-19 21:43:16 +00005147 arrSize = x2a->count;
5148 array = (struct symbol **)calloc(arrSize, sizeof(struct symbol *));
drh75897232000-05-29 14:26:00 +00005149 if( array ){
mistachkin8e189222015-04-19 21:43:16 +00005150 for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data;
drh75897232000-05-29 14:26:00 +00005151 }
5152 return array;
5153}
5154
5155/* Compare two configurations */
icculus9e44cf12010-02-14 17:14:22 +00005156int Configcmp(const char *_a,const char *_b)
drh75897232000-05-29 14:26:00 +00005157{
icculus9e44cf12010-02-14 17:14:22 +00005158 const struct config *a = (struct config *) _a;
5159 const struct config *b = (struct config *) _b;
drh75897232000-05-29 14:26:00 +00005160 int x;
5161 x = a->rp->index - b->rp->index;
5162 if( x==0 ) x = a->dot - b->dot;
5163 return x;
5164}
5165
5166/* Compare two states */
icculus9e44cf12010-02-14 17:14:22 +00005167PRIVATE int statecmp(struct config *a, struct config *b)
drh75897232000-05-29 14:26:00 +00005168{
5169 int rc;
5170 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
5171 rc = a->rp->index - b->rp->index;
5172 if( rc==0 ) rc = a->dot - b->dot;
5173 }
5174 if( rc==0 ){
5175 if( a ) rc = 1;
5176 if( b ) rc = -1;
5177 }
5178 return rc;
5179}
5180
5181/* Hash a state */
drh01f75f22013-10-02 20:46:30 +00005182PRIVATE unsigned statehash(struct config *a)
drh75897232000-05-29 14:26:00 +00005183{
drh01f75f22013-10-02 20:46:30 +00005184 unsigned h=0;
drh75897232000-05-29 14:26:00 +00005185 while( a ){
5186 h = h*571 + a->rp->index*37 + a->dot;
5187 a = a->bp;
5188 }
5189 return h;
5190}
5191
5192/* Allocate a new state structure */
5193struct state *State_new()
5194{
icculus9e44cf12010-02-14 17:14:22 +00005195 struct state *newstate;
5196 newstate = (struct state *)calloc(1, sizeof(struct state) );
5197 MemoryCheck(newstate);
5198 return newstate;
drh75897232000-05-29 14:26:00 +00005199}
5200
5201/* There is one instance of the following structure for each
5202** associative array of type "x3".
5203*/
5204struct s_x3 {
5205 int size; /* The number of available slots. */
5206 /* Must be a power of 2 greater than or */
5207 /* equal to 1 */
5208 int count; /* Number of currently slots filled */
5209 struct s_x3node *tbl; /* The data stored here */
5210 struct s_x3node **ht; /* Hash table for lookups */
5211};
5212
5213/* There is one instance of this structure for every data element
5214** in an associative array of type "x3".
5215*/
5216typedef struct s_x3node {
5217 struct state *data; /* The data */
5218 struct config *key; /* The key */
5219 struct s_x3node *next; /* Next entry with the same hash */
5220 struct s_x3node **from; /* Previous link */
5221} x3node;
5222
5223/* There is only one instance of the array, which is the following */
5224static struct s_x3 *x3a;
5225
5226/* Allocate a new associative array */
drh14d88552017-04-14 19:44:15 +00005227void State_init(void){
drh75897232000-05-29 14:26:00 +00005228 if( x3a ) return;
5229 x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
5230 if( x3a ){
5231 x3a->size = 128;
5232 x3a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005233 x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*));
drh75897232000-05-29 14:26:00 +00005234 if( x3a->tbl==0 ){
5235 free(x3a);
5236 x3a = 0;
5237 }else{
5238 int i;
5239 x3a->ht = (x3node**)&(x3a->tbl[128]);
5240 for(i=0; i<128; i++) x3a->ht[i] = 0;
5241 }
5242 }
5243}
5244/* Insert a new record into the array. Return TRUE if successful.
5245** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005246int State_insert(struct state *data, struct config *key)
drh75897232000-05-29 14:26:00 +00005247{
5248 x3node *np;
drh01f75f22013-10-02 20:46:30 +00005249 unsigned h;
5250 unsigned ph;
drh75897232000-05-29 14:26:00 +00005251
5252 if( x3a==0 ) return 0;
5253 ph = statehash(key);
5254 h = ph & (x3a->size-1);
5255 np = x3a->ht[h];
5256 while( np ){
5257 if( statecmp(np->key,key)==0 ){
5258 /* An existing entry with the same key is found. */
5259 /* Fail because overwrite is not allows. */
5260 return 0;
5261 }
5262 np = np->next;
5263 }
5264 if( x3a->count>=x3a->size ){
5265 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005266 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005267 struct s_x3 array;
mistachkin8e189222015-04-19 21:43:16 +00005268 array.size = arrSize = x3a->size*2;
drh75897232000-05-29 14:26:00 +00005269 array.count = x3a->count;
mistachkin8e189222015-04-19 21:43:16 +00005270 array.tbl = (x3node*)calloc(arrSize, sizeof(x3node) + sizeof(x3node*));
drh75897232000-05-29 14:26:00 +00005271 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005272 array.ht = (x3node**)&(array.tbl[arrSize]);
5273 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005274 for(i=0; i<x3a->count; i++){
5275 x3node *oldnp, *newnp;
5276 oldnp = &(x3a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005277 h = statehash(oldnp->key) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005278 newnp = &(array.tbl[i]);
5279 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5280 newnp->next = array.ht[h];
5281 newnp->key = oldnp->key;
5282 newnp->data = oldnp->data;
5283 newnp->from = &(array.ht[h]);
5284 array.ht[h] = newnp;
5285 }
5286 free(x3a->tbl);
5287 *x3a = array;
5288 }
5289 /* Insert the new data */
5290 h = ph & (x3a->size-1);
5291 np = &(x3a->tbl[x3a->count++]);
5292 np->key = key;
5293 np->data = data;
5294 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
5295 np->next = x3a->ht[h];
5296 x3a->ht[h] = np;
5297 np->from = &(x3a->ht[h]);
5298 return 1;
5299}
5300
5301/* Return a pointer to data assigned to the given key. Return NULL
5302** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005303struct state *State_find(struct config *key)
drh75897232000-05-29 14:26:00 +00005304{
drh01f75f22013-10-02 20:46:30 +00005305 unsigned h;
drh75897232000-05-29 14:26:00 +00005306 x3node *np;
5307
5308 if( x3a==0 ) return 0;
5309 h = statehash(key) & (x3a->size-1);
5310 np = x3a->ht[h];
5311 while( np ){
5312 if( statecmp(np->key,key)==0 ) break;
5313 np = np->next;
5314 }
5315 return np ? np->data : 0;
5316}
5317
5318/* Return an array of pointers to all data in the table.
5319** The array is obtained from malloc. Return NULL if memory allocation
5320** problems, or if the array is empty. */
drh14d88552017-04-14 19:44:15 +00005321struct state **State_arrayof(void)
drh75897232000-05-29 14:26:00 +00005322{
5323 struct state **array;
mistachkin8e189222015-04-19 21:43:16 +00005324 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005325 if( x3a==0 ) return 0;
mistachkin8e189222015-04-19 21:43:16 +00005326 arrSize = x3a->count;
5327 array = (struct state **)calloc(arrSize, sizeof(struct state *));
drh75897232000-05-29 14:26:00 +00005328 if( array ){
mistachkin8e189222015-04-19 21:43:16 +00005329 for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data;
drh75897232000-05-29 14:26:00 +00005330 }
5331 return array;
5332}
5333
5334/* Hash a configuration */
drh01f75f22013-10-02 20:46:30 +00005335PRIVATE unsigned confighash(struct config *a)
drh75897232000-05-29 14:26:00 +00005336{
drh01f75f22013-10-02 20:46:30 +00005337 unsigned h=0;
drh75897232000-05-29 14:26:00 +00005338 h = h*571 + a->rp->index*37 + a->dot;
5339 return h;
5340}
5341
5342/* There is one instance of the following structure for each
5343** associative array of type "x4".
5344*/
5345struct s_x4 {
5346 int size; /* The number of available slots. */
5347 /* Must be a power of 2 greater than or */
5348 /* equal to 1 */
5349 int count; /* Number of currently slots filled */
5350 struct s_x4node *tbl; /* The data stored here */
5351 struct s_x4node **ht; /* Hash table for lookups */
5352};
5353
5354/* There is one instance of this structure for every data element
5355** in an associative array of type "x4".
5356*/
5357typedef struct s_x4node {
5358 struct config *data; /* The data */
5359 struct s_x4node *next; /* Next entry with the same hash */
5360 struct s_x4node **from; /* Previous link */
5361} x4node;
5362
5363/* There is only one instance of the array, which is the following */
5364static struct s_x4 *x4a;
5365
5366/* Allocate a new associative array */
drh14d88552017-04-14 19:44:15 +00005367void Configtable_init(void){
drh75897232000-05-29 14:26:00 +00005368 if( x4a ) return;
5369 x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
5370 if( x4a ){
5371 x4a->size = 64;
5372 x4a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005373 x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*));
drh75897232000-05-29 14:26:00 +00005374 if( x4a->tbl==0 ){
5375 free(x4a);
5376 x4a = 0;
5377 }else{
5378 int i;
5379 x4a->ht = (x4node**)&(x4a->tbl[64]);
5380 for(i=0; i<64; i++) x4a->ht[i] = 0;
5381 }
5382 }
5383}
5384/* Insert a new record into the array. Return TRUE if successful.
5385** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005386int Configtable_insert(struct config *data)
drh75897232000-05-29 14:26:00 +00005387{
5388 x4node *np;
drh01f75f22013-10-02 20:46:30 +00005389 unsigned h;
5390 unsigned ph;
drh75897232000-05-29 14:26:00 +00005391
5392 if( x4a==0 ) return 0;
5393 ph = confighash(data);
5394 h = ph & (x4a->size-1);
5395 np = x4a->ht[h];
5396 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00005397 if( Configcmp((const char *) np->data,(const char *) data)==0 ){
drh75897232000-05-29 14:26:00 +00005398 /* An existing entry with the same key is found. */
5399 /* Fail because overwrite is not allows. */
5400 return 0;
5401 }
5402 np = np->next;
5403 }
5404 if( x4a->count>=x4a->size ){
5405 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005406 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005407 struct s_x4 array;
mistachkin8e189222015-04-19 21:43:16 +00005408 array.size = arrSize = x4a->size*2;
drh75897232000-05-29 14:26:00 +00005409 array.count = x4a->count;
mistachkin8e189222015-04-19 21:43:16 +00005410 array.tbl = (x4node*)calloc(arrSize, sizeof(x4node) + sizeof(x4node*));
drh75897232000-05-29 14:26:00 +00005411 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005412 array.ht = (x4node**)&(array.tbl[arrSize]);
5413 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005414 for(i=0; i<x4a->count; i++){
5415 x4node *oldnp, *newnp;
5416 oldnp = &(x4a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005417 h = confighash(oldnp->data) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005418 newnp = &(array.tbl[i]);
5419 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5420 newnp->next = array.ht[h];
5421 newnp->data = oldnp->data;
5422 newnp->from = &(array.ht[h]);
5423 array.ht[h] = newnp;
5424 }
5425 free(x4a->tbl);
5426 *x4a = array;
5427 }
5428 /* Insert the new data */
5429 h = ph & (x4a->size-1);
5430 np = &(x4a->tbl[x4a->count++]);
5431 np->data = data;
5432 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
5433 np->next = x4a->ht[h];
5434 x4a->ht[h] = np;
5435 np->from = &(x4a->ht[h]);
5436 return 1;
5437}
5438
5439/* Return a pointer to data assigned to the given key. Return NULL
5440** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005441struct config *Configtable_find(struct config *key)
drh75897232000-05-29 14:26:00 +00005442{
5443 int h;
5444 x4node *np;
5445
5446 if( x4a==0 ) return 0;
5447 h = confighash(key) & (x4a->size-1);
5448 np = x4a->ht[h];
5449 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00005450 if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
drh75897232000-05-29 14:26:00 +00005451 np = np->next;
5452 }
5453 return np ? np->data : 0;
5454}
5455
5456/* Remove all data from the table. Pass each data to the function "f"
5457** as it is removed. ("f" may be null to avoid this step.) */
icculus9e44cf12010-02-14 17:14:22 +00005458void Configtable_clear(int(*f)(struct config *))
drh75897232000-05-29 14:26:00 +00005459{
5460 int i;
5461 if( x4a==0 || x4a->count==0 ) return;
5462 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
5463 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
5464 x4a->count = 0;
5465 return;
5466}