blob: 5f124601db4778e1f9123a9ca02ef8ec51a03318 [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" ************************************/
171void FindRulePrecedences();
172void FindFirstSets();
173void FindStates();
174void FindLinks();
175void FindFollowSets();
176void FindActions();
177
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 */
drh4dc8ef52008-07-01 17:13:57 +0000266 int destLineno; /* Line number for start of destructor */
drh75897232000-05-29 14:26:00 +0000267 char *datatype; /* The data type of information held by this
268 ** object. Only used if type==NONTERMINAL */
269 int dtnum; /* The data type number. In the parser, the value
270 ** stack is a union. The .yy%d element of this
271 ** union is the correct data type for this object */
drhfd405312005-11-06 04:06:59 +0000272 /* The following fields are used by MULTITERMINALs only */
273 int nsubsym; /* Number of constituent symbols in the MULTI */
274 struct symbol **subsym; /* Array of constituent symbols */
drh75897232000-05-29 14:26:00 +0000275};
276
277/* Each production rule in the grammar is stored in the following
278** structure. */
279struct rule {
280 struct symbol *lhs; /* Left-hand side of the rule */
icculus9e44cf12010-02-14 17:14:22 +0000281 const char *lhsalias; /* Alias for the LHS (NULL if none) */
drhb4960992007-10-05 16:16:36 +0000282 int lhsStart; /* True if left-hand side is the start symbol */
drh75897232000-05-29 14:26:00 +0000283 int ruleline; /* Line number for the rule */
284 int nrhs; /* Number of RHS symbols */
285 struct symbol **rhs; /* The RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +0000286 const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */
drh75897232000-05-29 14:26:00 +0000287 int line; /* Line number at which code begins */
icculus9e44cf12010-02-14 17:14:22 +0000288 const char *code; /* The code executed when this rule is reduced */
drh4dd0d3f2016-02-17 01:18:33 +0000289 const char *codePrefix; /* Setup code before code[] above */
290 const char *codeSuffix; /* Breakdown code after code[] above */
drh711c9812016-05-23 14:24:31 +0000291 int noCode; /* True if this rule has no associated C code */
292 int codeEmitted; /* True if the code has been emitted already */
drh75897232000-05-29 14:26:00 +0000293 struct symbol *precsym; /* Precedence symbol for this rule */
294 int index; /* An index number for this rule */
drh4ef07702016-03-16 19:45:54 +0000295 int iRule; /* Rule number as used in the generated tables */
drh75897232000-05-29 14:26:00 +0000296 Boolean canReduce; /* True if this rule is ever reduced */
drh756b41e2016-05-24 18:55:08 +0000297 Boolean doesReduce; /* Reduce actions occur after optimization */
drh75897232000-05-29 14:26:00 +0000298 struct rule *nextlhs; /* Next rule with the same LHS */
299 struct rule *next; /* Next rule in the global list */
300};
301
302/* A configuration is a production rule of the grammar together with
303** a mark (dot) showing how much of that rule has been processed so far.
304** Configurations also contain a follow-set which is a list of terminal
305** symbols which are allowed to immediately follow the end of the rule.
306** Every configuration is recorded as an instance of the following: */
icculus9e44cf12010-02-14 17:14:22 +0000307enum cfgstatus {
308 COMPLETE,
309 INCOMPLETE
310};
drh75897232000-05-29 14:26:00 +0000311struct config {
312 struct rule *rp; /* The rule upon which the configuration is based */
313 int dot; /* The parse point */
314 char *fws; /* Follow-set for this configuration only */
315 struct plink *fplp; /* Follow-set forward propagation links */
316 struct plink *bplp; /* Follow-set backwards propagation links */
317 struct state *stp; /* Pointer to state which contains this */
icculus9e44cf12010-02-14 17:14:22 +0000318 enum cfgstatus status; /* used during followset and shift computations */
drh75897232000-05-29 14:26:00 +0000319 struct config *next; /* Next configuration in the state */
320 struct config *bp; /* The next basis configuration */
321};
322
icculus9e44cf12010-02-14 17:14:22 +0000323enum e_action {
324 SHIFT,
325 ACCEPT,
326 REDUCE,
327 ERROR,
328 SSCONFLICT, /* A shift/shift conflict */
329 SRCONFLICT, /* Was a reduce, but part of a conflict */
330 RRCONFLICT, /* Was a reduce, but part of a conflict */
331 SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
332 RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
drh3bd48ab2015-09-07 18:23:37 +0000333 NOT_USED, /* Deleted by compression */
334 SHIFTREDUCE /* Shift first, then reduce */
icculus9e44cf12010-02-14 17:14:22 +0000335};
336
drh75897232000-05-29 14:26:00 +0000337/* Every shift or reduce operation is stored as one of the following */
338struct action {
339 struct symbol *sp; /* The look-ahead symbol */
icculus9e44cf12010-02-14 17:14:22 +0000340 enum e_action type;
drh75897232000-05-29 14:26:00 +0000341 union {
342 struct state *stp; /* The new state, if a shift */
343 struct rule *rp; /* The rule, if a reduce */
344 } x;
drhc173ad82016-05-23 16:15:02 +0000345 struct symbol *spOpt; /* SHIFTREDUCE optimization to this symbol */
drh75897232000-05-29 14:26:00 +0000346 struct action *next; /* Next action for this state */
347 struct action *collide; /* Next action with the same hash */
348};
349
350/* Each state of the generated parser's finite state machine
351** is encoded as an instance of the following structure. */
352struct state {
353 struct config *bp; /* The basis configurations for this state */
354 struct config *cfp; /* All configurations in this set */
drh34ff57b2008-07-14 12:27:51 +0000355 int statenum; /* Sequential number for this state */
drh711c9812016-05-23 14:24:31 +0000356 struct action *ap; /* List of actions for this state */
drh8b582012003-10-21 13:16:03 +0000357 int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */
358 int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */
drh3bd48ab2015-09-07 18:23:37 +0000359 int iDfltReduce; /* Default action is to REDUCE by this rule */
360 struct rule *pDfltReduce;/* The default REDUCE rule. */
361 int autoReduce; /* True if this is an auto-reduce state */
drh75897232000-05-29 14:26:00 +0000362};
drh8b582012003-10-21 13:16:03 +0000363#define NO_OFFSET (-2147483647)
drh75897232000-05-29 14:26:00 +0000364
365/* A followset propagation link indicates that the contents of one
366** configuration followset should be propagated to another whenever
367** the first changes. */
368struct plink {
369 struct config *cfp; /* The configuration to which linked */
370 struct plink *next; /* The next propagate link */
371};
372
373/* The state vector for the entire parser generator is recorded as
374** follows. (LEMON uses no global variables and makes little use of
375** static variables. Fields in the following structure can be thought
376** of as begin global variables in the program.) */
377struct lemon {
378 struct state **sorted; /* Table of states sorted by state number */
379 struct rule *rule; /* List of all rules */
drh4ef07702016-03-16 19:45:54 +0000380 struct rule *startRule; /* First rule */
drh75897232000-05-29 14:26:00 +0000381 int nstate; /* Number of states */
drh3bd48ab2015-09-07 18:23:37 +0000382 int nxstate; /* nstate with tail degenerate states removed */
drh75897232000-05-29 14:26:00 +0000383 int nrule; /* Number of rules */
384 int nsymbol; /* Number of terminal and nonterminal symbols */
385 int nterminal; /* Number of terminal symbols */
386 struct symbol **symbols; /* Sorted array of pointers to symbols */
387 int errorcnt; /* Number of errors */
388 struct symbol *errsym; /* The error symbol */
drhe09daa92006-06-10 13:29:31 +0000389 struct symbol *wildcard; /* Token that matches anything */
drh75897232000-05-29 14:26:00 +0000390 char *name; /* Name of the generated parser */
391 char *arg; /* Declaration of the 3th argument to parser */
392 char *tokentype; /* Type of terminal symbols in the parser stack */
drh960e8c62001-04-03 16:53:21 +0000393 char *vartype; /* The default type of non-terminal symbols */
drh75897232000-05-29 14:26:00 +0000394 char *start; /* Name of the start symbol for the grammar */
395 char *stacksize; /* Size of the parser stack */
396 char *include; /* Code to put at the start of the C file */
drh75897232000-05-29 14:26:00 +0000397 char *error; /* Code to execute when an error is seen */
drh75897232000-05-29 14:26:00 +0000398 char *overflow; /* Code to execute on a stack overflow */
drh75897232000-05-29 14:26:00 +0000399 char *failure; /* Code to execute on parser failure */
drh75897232000-05-29 14:26:00 +0000400 char *accept; /* Code to execute when the parser excepts */
drh75897232000-05-29 14:26:00 +0000401 char *extracode; /* Code appended to the generated file */
drh75897232000-05-29 14:26:00 +0000402 char *tokendest; /* Code to execute to destroy token data */
drh960e8c62001-04-03 16:53:21 +0000403 char *vardest; /* Code for the default non-terminal destructor */
drh75897232000-05-29 14:26:00 +0000404 char *filename; /* Name of the input file */
405 char *outname; /* Name of the current output file */
406 char *tokenprefix; /* A prefix added to token names in the .h file */
407 int nconflict; /* Number of parsing conflicts */
drhc75e0162015-09-07 02:23:02 +0000408 int nactiontab; /* Number of entries in the yy_action[] table */
409 int tablesize; /* Total table size of all tables in bytes */
drh75897232000-05-29 14:26:00 +0000410 int basisflag; /* Print only basis configurations */
drh34ff57b2008-07-14 12:27:51 +0000411 int has_fallback; /* True if any %fallback is seen in the grammar */
shane58543932008-12-10 20:10:04 +0000412 int nolinenosflag; /* True if #line statements should not be printed */
drh75897232000-05-29 14:26:00 +0000413 char *argv0; /* Name of the program */
414};
415
416#define MemoryCheck(X) if((X)==0){ \
417 extern void memory_error(); \
418 memory_error(); \
419}
420
421/**************** From the file "table.h" *********************************/
422/*
423** All code in this file has been automatically generated
424** from a specification in the file
425** "table.q"
426** by the associative array code building program "aagen".
427** Do not edit this file! Instead, edit the specification
428** file, then rerun aagen.
429*/
430/*
431** Code for processing tables in the LEMON parser generator.
432*/
drh75897232000-05-29 14:26:00 +0000433/* Routines for handling a strings */
434
icculus9e44cf12010-02-14 17:14:22 +0000435const char *Strsafe(const char *);
drh75897232000-05-29 14:26:00 +0000436
icculus9e44cf12010-02-14 17:14:22 +0000437void Strsafe_init(void);
438int Strsafe_insert(const char *);
439const char *Strsafe_find(const char *);
drh75897232000-05-29 14:26:00 +0000440
441/* Routines for handling symbols of the grammar */
442
icculus9e44cf12010-02-14 17:14:22 +0000443struct symbol *Symbol_new(const char *);
444int Symbolcmpp(const void *, const void *);
445void Symbol_init(void);
446int Symbol_insert(struct symbol *, const char *);
447struct symbol *Symbol_find(const char *);
448struct symbol *Symbol_Nth(int);
449int Symbol_count(void);
450struct symbol **Symbol_arrayof(void);
drh75897232000-05-29 14:26:00 +0000451
452/* Routines to manage the state table */
453
icculus9e44cf12010-02-14 17:14:22 +0000454int Configcmp(const char *, const char *);
455struct state *State_new(void);
456void State_init(void);
457int State_insert(struct state *, struct config *);
458struct state *State_find(struct config *);
drh75897232000-05-29 14:26:00 +0000459struct state **State_arrayof(/* */);
460
461/* Routines used for efficiency in Configlist_add */
462
icculus9e44cf12010-02-14 17:14:22 +0000463void Configtable_init(void);
464int Configtable_insert(struct config *);
465struct config *Configtable_find(struct config *);
466void Configtable_clear(int(*)(struct config *));
467
drh75897232000-05-29 14:26:00 +0000468/****************** From the file "action.c" *******************************/
469/*
470** Routines processing parser actions in the LEMON parser generator.
471*/
472
473/* Allocate a new parser action */
drhe9278182007-07-18 18:16:29 +0000474static struct action *Action_new(void){
drh75897232000-05-29 14:26:00 +0000475 static struct action *freelist = 0;
icculus9e44cf12010-02-14 17:14:22 +0000476 struct action *newaction;
drh75897232000-05-29 14:26:00 +0000477
478 if( freelist==0 ){
479 int i;
480 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +0000481 freelist = (struct action *)calloc(amt, sizeof(struct action));
drh75897232000-05-29 14:26:00 +0000482 if( freelist==0 ){
483 fprintf(stderr,"Unable to allocate memory for a new parser action.");
484 exit(1);
485 }
486 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
487 freelist[amt-1].next = 0;
488 }
icculus9e44cf12010-02-14 17:14:22 +0000489 newaction = freelist;
drh75897232000-05-29 14:26:00 +0000490 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +0000491 return newaction;
drh75897232000-05-29 14:26:00 +0000492}
493
drhe9278182007-07-18 18:16:29 +0000494/* Compare two actions for sorting purposes. Return negative, zero, or
495** positive if the first action is less than, equal to, or greater than
496** the first
497*/
498static int actioncmp(
499 struct action *ap1,
500 struct action *ap2
501){
drh75897232000-05-29 14:26:00 +0000502 int rc;
503 rc = ap1->sp->index - ap2->sp->index;
drh75897232000-05-29 14:26:00 +0000504 if( rc==0 ){
drh9892c5d2007-12-21 00:02:11 +0000505 rc = (int)ap1->type - (int)ap2->type;
506 }
drh3bd48ab2015-09-07 18:23:37 +0000507 if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){
drh75897232000-05-29 14:26:00 +0000508 rc = ap1->x.rp->index - ap2->x.rp->index;
509 }
drhe594bc32009-11-03 13:02:25 +0000510 if( rc==0 ){
icculus7b429aa2010-03-03 17:09:01 +0000511 rc = (int) (ap2 - ap1);
drhe594bc32009-11-03 13:02:25 +0000512 }
drh75897232000-05-29 14:26:00 +0000513 return rc;
514}
515
516/* Sort parser actions */
drhe9278182007-07-18 18:16:29 +0000517static struct action *Action_sort(
518 struct action *ap
519){
520 ap = (struct action *)msort((char *)ap,(char **)&ap->next,
521 (int(*)(const char*,const char*))actioncmp);
drh75897232000-05-29 14:26:00 +0000522 return ap;
523}
524
icculus9e44cf12010-02-14 17:14:22 +0000525void Action_add(
526 struct action **app,
527 enum e_action type,
528 struct symbol *sp,
529 char *arg
530){
531 struct action *newaction;
532 newaction = Action_new();
533 newaction->next = *app;
534 *app = newaction;
535 newaction->type = type;
536 newaction->sp = sp;
drhc173ad82016-05-23 16:15:02 +0000537 newaction->spOpt = 0;
drh75897232000-05-29 14:26:00 +0000538 if( type==SHIFT ){
icculus9e44cf12010-02-14 17:14:22 +0000539 newaction->x.stp = (struct state *)arg;
drh75897232000-05-29 14:26:00 +0000540 }else{
icculus9e44cf12010-02-14 17:14:22 +0000541 newaction->x.rp = (struct rule *)arg;
drh75897232000-05-29 14:26:00 +0000542 }
543}
drh8b582012003-10-21 13:16:03 +0000544/********************** New code to implement the "acttab" module ***********/
545/*
546** This module implements routines use to construct the yy_action[] table.
547*/
548
549/*
550** The state of the yy_action table under construction is an instance of
drh8dc3e8f2010-01-07 03:53:03 +0000551** the following structure.
552**
553** The yy_action table maps the pair (state_number, lookahead) into an
554** action_number. The table is an array of integers pairs. The state_number
555** determines an initial offset into the yy_action array. The lookahead
556** value is then added to this initial offset to get an index X into the
557** yy_action array. If the aAction[X].lookahead equals the value of the
558** of the lookahead input, then the value of the action_number output is
559** aAction[X].action. If the lookaheads do not match then the
560** default action for the state_number is returned.
561**
562** All actions associated with a single state_number are first entered
563** into aLookahead[] using multiple calls to acttab_action(). Then the
564** actions for that single state_number are placed into the aAction[]
565** array with a single call to acttab_insert(). The acttab_insert() call
566** also resets the aLookahead[] array in preparation for the next
567** state number.
drh8b582012003-10-21 13:16:03 +0000568*/
icculus9e44cf12010-02-14 17:14:22 +0000569struct lookahead_action {
570 int lookahead; /* Value of the lookahead token */
571 int action; /* Action to take on the given lookahead */
572};
drh8b582012003-10-21 13:16:03 +0000573typedef struct acttab acttab;
574struct acttab {
575 int nAction; /* Number of used slots in aAction[] */
576 int nActionAlloc; /* Slots allocated for aAction[] */
icculus9e44cf12010-02-14 17:14:22 +0000577 struct lookahead_action
578 *aAction, /* The yy_action[] table under construction */
drh8b582012003-10-21 13:16:03 +0000579 *aLookahead; /* A single new transaction set */
580 int mnLookahead; /* Minimum aLookahead[].lookahead */
581 int mnAction; /* Action associated with mnLookahead */
582 int mxLookahead; /* Maximum aLookahead[].lookahead */
583 int nLookahead; /* Used slots in aLookahead[] */
584 int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
585};
586
587/* Return the number of entries in the yy_action table */
588#define acttab_size(X) ((X)->nAction)
589
590/* The value for the N-th entry in yy_action */
591#define acttab_yyaction(X,N) ((X)->aAction[N].action)
592
593/* The value for the N-th entry in yy_lookahead */
594#define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
595
596/* Free all memory associated with the given acttab */
597void acttab_free(acttab *p){
598 free( p->aAction );
599 free( p->aLookahead );
600 free( p );
601}
602
603/* Allocate a new acttab structure */
604acttab *acttab_alloc(void){
icculus9e44cf12010-02-14 17:14:22 +0000605 acttab *p = (acttab *) calloc( 1, sizeof(*p) );
drh8b582012003-10-21 13:16:03 +0000606 if( p==0 ){
607 fprintf(stderr,"Unable to allocate memory for a new acttab.");
608 exit(1);
609 }
610 memset(p, 0, sizeof(*p));
611 return p;
612}
613
drh8dc3e8f2010-01-07 03:53:03 +0000614/* Add a new action to the current transaction set.
615**
616** This routine is called once for each lookahead for a particular
617** state.
drh8b582012003-10-21 13:16:03 +0000618*/
619void acttab_action(acttab *p, int lookahead, int action){
620 if( p->nLookahead>=p->nLookaheadAlloc ){
621 p->nLookaheadAlloc += 25;
icculus9e44cf12010-02-14 17:14:22 +0000622 p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
drh8b582012003-10-21 13:16:03 +0000623 sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
624 if( p->aLookahead==0 ){
625 fprintf(stderr,"malloc failed\n");
626 exit(1);
627 }
628 }
629 if( p->nLookahead==0 ){
630 p->mxLookahead = lookahead;
631 p->mnLookahead = lookahead;
632 p->mnAction = action;
633 }else{
634 if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
635 if( p->mnLookahead>lookahead ){
636 p->mnLookahead = lookahead;
637 p->mnAction = action;
638 }
639 }
640 p->aLookahead[p->nLookahead].lookahead = lookahead;
641 p->aLookahead[p->nLookahead].action = action;
642 p->nLookahead++;
643}
644
645/*
646** Add the transaction set built up with prior calls to acttab_action()
647** into the current action table. Then reset the transaction set back
648** to an empty set in preparation for a new round of acttab_action() calls.
649**
650** Return the offset into the action table of the new transaction.
651*/
652int acttab_insert(acttab *p){
653 int i, j, k, n;
654 assert( p->nLookahead>0 );
655
656 /* Make sure we have enough space to hold the expanded action table
657 ** in the worst case. The worst case occurs if the transaction set
658 ** must be appended to the current action table
659 */
drh784d86f2004-02-19 18:41:53 +0000660 n = p->mxLookahead + 1;
drh8dc3e8f2010-01-07 03:53:03 +0000661 if( p->nAction + n >= p->nActionAlloc ){
drhfdbf9282003-10-21 16:34:41 +0000662 int oldAlloc = p->nActionAlloc;
drh8b582012003-10-21 13:16:03 +0000663 p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
icculus9e44cf12010-02-14 17:14:22 +0000664 p->aAction = (struct lookahead_action *) realloc( p->aAction,
drh8b582012003-10-21 13:16:03 +0000665 sizeof(p->aAction[0])*p->nActionAlloc);
666 if( p->aAction==0 ){
667 fprintf(stderr,"malloc failed\n");
668 exit(1);
669 }
drhfdbf9282003-10-21 16:34:41 +0000670 for(i=oldAlloc; i<p->nActionAlloc; i++){
drh8b582012003-10-21 13:16:03 +0000671 p->aAction[i].lookahead = -1;
672 p->aAction[i].action = -1;
673 }
674 }
675
drh8dc3e8f2010-01-07 03:53:03 +0000676 /* Scan the existing action table looking for an offset that is a
677 ** duplicate of the current transaction set. Fall out of the loop
678 ** if and when the duplicate is found.
drh8b582012003-10-21 13:16:03 +0000679 **
680 ** i is the index in p->aAction[] where p->mnLookahead is inserted.
681 */
drh8dc3e8f2010-01-07 03:53:03 +0000682 for(i=p->nAction-1; i>=0; i--){
drhf16371d2009-11-03 19:18:31 +0000683 if( p->aAction[i].lookahead==p->mnLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000684 /* All lookaheads and actions in the aLookahead[] transaction
685 ** must match against the candidate aAction[i] entry. */
drh8b582012003-10-21 13:16:03 +0000686 if( p->aAction[i].action!=p->mnAction ) continue;
687 for(j=0; j<p->nLookahead; j++){
688 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
689 if( k<0 || k>=p->nAction ) break;
690 if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
691 if( p->aLookahead[j].action!=p->aAction[k].action ) break;
692 }
693 if( j<p->nLookahead ) continue;
drh8dc3e8f2010-01-07 03:53:03 +0000694
695 /* No possible lookahead value that is not in the aLookahead[]
696 ** transaction is allowed to match aAction[i] */
drh8b582012003-10-21 13:16:03 +0000697 n = 0;
698 for(j=0; j<p->nAction; j++){
drhfdbf9282003-10-21 16:34:41 +0000699 if( p->aAction[j].lookahead<0 ) continue;
700 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
drh8b582012003-10-21 13:16:03 +0000701 }
drhfdbf9282003-10-21 16:34:41 +0000702 if( n==p->nLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000703 break; /* An exact match is found at offset i */
drhfdbf9282003-10-21 16:34:41 +0000704 }
drh8b582012003-10-21 13:16:03 +0000705 }
706 }
drh8dc3e8f2010-01-07 03:53:03 +0000707
708 /* If no existing offsets exactly match the current transaction, find an
709 ** an empty offset in the aAction[] table in which we can add the
710 ** aLookahead[] transaction.
711 */
drhf16371d2009-11-03 19:18:31 +0000712 if( i<0 ){
drh8dc3e8f2010-01-07 03:53:03 +0000713 /* Look for holes in the aAction[] table that fit the current
714 ** aLookahead[] transaction. Leave i set to the offset of the hole.
715 ** If no holes are found, i is left at p->nAction, which means the
716 ** transaction will be appended. */
717 for(i=0; i<p->nActionAlloc - p->mxLookahead; i++){
drhf16371d2009-11-03 19:18:31 +0000718 if( p->aAction[i].lookahead<0 ){
719 for(j=0; j<p->nLookahead; j++){
720 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
721 if( k<0 ) break;
722 if( p->aAction[k].lookahead>=0 ) break;
723 }
724 if( j<p->nLookahead ) continue;
725 for(j=0; j<p->nAction; j++){
726 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
727 }
728 if( j==p->nAction ){
729 break; /* Fits in empty slots */
730 }
731 }
732 }
733 }
drh8b582012003-10-21 13:16:03 +0000734 /* Insert transaction set at index i. */
735 for(j=0; j<p->nLookahead; j++){
736 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
737 p->aAction[k] = p->aLookahead[j];
738 if( k>=p->nAction ) p->nAction = k+1;
739 }
740 p->nLookahead = 0;
741
742 /* Return the offset that is added to the lookahead in order to get the
743 ** index into yy_action of the action */
744 return i - p->mnLookahead;
745}
746
drh75897232000-05-29 14:26:00 +0000747/********************** From the file "build.c" *****************************/
748/*
749** Routines to construction the finite state machine for the LEMON
750** parser generator.
751*/
752
753/* Find a precedence symbol of every rule in the grammar.
754**
755** Those rules which have a precedence symbol coded in the input
756** grammar using the "[symbol]" construct will already have the
757** rp->precsym field filled. Other rules take as their precedence
758** symbol the first RHS symbol with a defined precedence. If there
759** are not RHS symbols with a defined precedence, the precedence
760** symbol field is left blank.
761*/
icculus9e44cf12010-02-14 17:14:22 +0000762void FindRulePrecedences(struct lemon *xp)
drh75897232000-05-29 14:26:00 +0000763{
764 struct rule *rp;
765 for(rp=xp->rule; rp; rp=rp->next){
766 if( rp->precsym==0 ){
drhfd405312005-11-06 04:06:59 +0000767 int i, j;
768 for(i=0; i<rp->nrhs && rp->precsym==0; i++){
769 struct symbol *sp = rp->rhs[i];
770 if( sp->type==MULTITERMINAL ){
771 for(j=0; j<sp->nsubsym; j++){
772 if( sp->subsym[j]->prec>=0 ){
773 rp->precsym = sp->subsym[j];
774 break;
775 }
776 }
777 }else if( sp->prec>=0 ){
drh75897232000-05-29 14:26:00 +0000778 rp->precsym = rp->rhs[i];
drhf2f105d2012-08-20 15:53:54 +0000779 }
drh75897232000-05-29 14:26:00 +0000780 }
781 }
782 }
783 return;
784}
785
786/* Find all nonterminals which will generate the empty string.
787** Then go back and compute the first sets of every nonterminal.
788** The first set is the set of all terminal symbols which can begin
789** a string generated by that nonterminal.
790*/
icculus9e44cf12010-02-14 17:14:22 +0000791void FindFirstSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000792{
drhfd405312005-11-06 04:06:59 +0000793 int i, j;
drh75897232000-05-29 14:26:00 +0000794 struct rule *rp;
795 int progress;
796
797 for(i=0; i<lemp->nsymbol; i++){
drhaa9f1122007-08-23 02:50:56 +0000798 lemp->symbols[i]->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +0000799 }
800 for(i=lemp->nterminal; i<lemp->nsymbol; i++){
801 lemp->symbols[i]->firstset = SetNew();
802 }
803
804 /* First compute all lambdas */
805 do{
806 progress = 0;
807 for(rp=lemp->rule; rp; rp=rp->next){
808 if( rp->lhs->lambda ) continue;
809 for(i=0; i<rp->nrhs; i++){
drh7dd1ac62012-01-07 15:17:18 +0000810 struct symbol *sp = rp->rhs[i];
811 assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE );
812 if( sp->lambda==LEMON_FALSE ) break;
drh75897232000-05-29 14:26:00 +0000813 }
814 if( i==rp->nrhs ){
drhaa9f1122007-08-23 02:50:56 +0000815 rp->lhs->lambda = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +0000816 progress = 1;
817 }
818 }
819 }while( progress );
820
821 /* Now compute all first sets */
822 do{
823 struct symbol *s1, *s2;
824 progress = 0;
825 for(rp=lemp->rule; rp; rp=rp->next){
826 s1 = rp->lhs;
827 for(i=0; i<rp->nrhs; i++){
828 s2 = rp->rhs[i];
829 if( s2->type==TERMINAL ){
830 progress += SetAdd(s1->firstset,s2->index);
831 break;
drhfd405312005-11-06 04:06:59 +0000832 }else if( s2->type==MULTITERMINAL ){
833 for(j=0; j<s2->nsubsym; j++){
834 progress += SetAdd(s1->firstset,s2->subsym[j]->index);
835 }
836 break;
drhf2f105d2012-08-20 15:53:54 +0000837 }else if( s1==s2 ){
drhaa9f1122007-08-23 02:50:56 +0000838 if( s1->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000839 }else{
drh75897232000-05-29 14:26:00 +0000840 progress += SetUnion(s1->firstset,s2->firstset);
drhaa9f1122007-08-23 02:50:56 +0000841 if( s2->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000842 }
drh75897232000-05-29 14:26:00 +0000843 }
844 }
845 }while( progress );
846 return;
847}
848
849/* Compute all LR(0) states for the grammar. Links
850** are added to between some states so that the LR(1) follow sets
851** can be computed later.
852*/
icculus9e44cf12010-02-14 17:14:22 +0000853PRIVATE struct state *getstate(struct lemon *); /* forward reference */
854void FindStates(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000855{
856 struct symbol *sp;
857 struct rule *rp;
858
859 Configlist_init();
860
861 /* Find the start symbol */
862 if( lemp->start ){
863 sp = Symbol_find(lemp->start);
864 if( sp==0 ){
865 ErrorMsg(lemp->filename,0,
866"The specified start symbol \"%s\" is not \
867in a nonterminal of the grammar. \"%s\" will be used as the start \
drh4ef07702016-03-16 19:45:54 +0000868symbol instead.",lemp->start,lemp->startRule->lhs->name);
drh75897232000-05-29 14:26:00 +0000869 lemp->errorcnt++;
drh4ef07702016-03-16 19:45:54 +0000870 sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +0000871 }
872 }else{
drh4ef07702016-03-16 19:45:54 +0000873 sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +0000874 }
875
876 /* Make sure the start symbol doesn't occur on the right-hand side of
877 ** any rule. Report an error if it does. (YACC would generate a new
878 ** start symbol in this case.) */
879 for(rp=lemp->rule; rp; rp=rp->next){
880 int i;
881 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +0000882 if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */
drh75897232000-05-29 14:26:00 +0000883 ErrorMsg(lemp->filename,0,
884"The start symbol \"%s\" occurs on the \
885right-hand side of a rule. This will result in a parser which \
886does not work properly.",sp->name);
887 lemp->errorcnt++;
888 }
889 }
890 }
891
892 /* The basis configuration set for the first state
893 ** is all rules which have the start symbol as their
894 ** left-hand side */
895 for(rp=sp->rule; rp; rp=rp->nextlhs){
896 struct config *newcfp;
drhb4960992007-10-05 16:16:36 +0000897 rp->lhsStart = 1;
drh75897232000-05-29 14:26:00 +0000898 newcfp = Configlist_addbasis(rp,0);
899 SetAdd(newcfp->fws,0);
900 }
901
902 /* Compute the first state. All other states will be
903 ** computed automatically during the computation of the first one.
904 ** The returned pointer to the first state is not used. */
905 (void)getstate(lemp);
906 return;
907}
908
909/* Return a pointer to a state which is described by the configuration
910** list which has been built from calls to Configlist_add.
911*/
icculus9e44cf12010-02-14 17:14:22 +0000912PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
913PRIVATE struct state *getstate(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000914{
915 struct config *cfp, *bp;
916 struct state *stp;
917
918 /* Extract the sorted basis of the new state. The basis was constructed
919 ** by prior calls to "Configlist_addbasis()". */
920 Configlist_sortbasis();
921 bp = Configlist_basis();
922
923 /* Get a state with the same basis */
924 stp = State_find(bp);
925 if( stp ){
926 /* A state with the same basis already exists! Copy all the follow-set
927 ** propagation links from the state under construction into the
928 ** preexisting state, then return a pointer to the preexisting state */
929 struct config *x, *y;
930 for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
931 Plink_copy(&y->bplp,x->bplp);
932 Plink_delete(x->fplp);
933 x->fplp = x->bplp = 0;
934 }
935 cfp = Configlist_return();
936 Configlist_eat(cfp);
937 }else{
938 /* This really is a new state. Construct all the details */
939 Configlist_closure(lemp); /* Compute the configuration closure */
940 Configlist_sort(); /* Sort the configuration closure */
941 cfp = Configlist_return(); /* Get a pointer to the config list */
942 stp = State_new(); /* A new state structure */
943 MemoryCheck(stp);
944 stp->bp = bp; /* Remember the configuration basis */
945 stp->cfp = cfp; /* Remember the configuration closure */
drhada354d2005-11-05 15:03:59 +0000946 stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
drh75897232000-05-29 14:26:00 +0000947 stp->ap = 0; /* No actions, yet. */
948 State_insert(stp,stp->bp); /* Add to the state table */
949 buildshifts(lemp,stp); /* Recursively compute successor states */
950 }
951 return stp;
952}
953
drhfd405312005-11-06 04:06:59 +0000954/*
955** Return true if two symbols are the same.
956*/
icculus9e44cf12010-02-14 17:14:22 +0000957int same_symbol(struct symbol *a, struct symbol *b)
drhfd405312005-11-06 04:06:59 +0000958{
959 int i;
960 if( a==b ) return 1;
961 if( a->type!=MULTITERMINAL ) return 0;
962 if( b->type!=MULTITERMINAL ) return 0;
963 if( a->nsubsym!=b->nsubsym ) return 0;
964 for(i=0; i<a->nsubsym; i++){
965 if( a->subsym[i]!=b->subsym[i] ) return 0;
966 }
967 return 1;
968}
969
drh75897232000-05-29 14:26:00 +0000970/* Construct all successor states to the given state. A "successor"
971** state is any state which can be reached by a shift action.
972*/
icculus9e44cf12010-02-14 17:14:22 +0000973PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
drh75897232000-05-29 14:26:00 +0000974{
975 struct config *cfp; /* For looping thru the config closure of "stp" */
976 struct config *bcfp; /* For the inner loop on config closure of "stp" */
icculus9e44cf12010-02-14 17:14:22 +0000977 struct config *newcfg; /* */
drh75897232000-05-29 14:26:00 +0000978 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
979 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
980 struct state *newstp; /* A pointer to a successor state */
981
982 /* Each configuration becomes complete after it contibutes to a successor
983 ** state. Initially, all configurations are incomplete */
984 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
985
986 /* Loop through all configurations of the state "stp" */
987 for(cfp=stp->cfp; cfp; cfp=cfp->next){
988 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
989 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
990 Configlist_reset(); /* Reset the new config set */
991 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
992
993 /* For every configuration in the state "stp" which has the symbol "sp"
994 ** following its dot, add the same configuration to the basis set under
995 ** construction but with the dot shifted one symbol to the right. */
996 for(bcfp=cfp; bcfp; bcfp=bcfp->next){
997 if( bcfp->status==COMPLETE ) continue; /* Already used */
998 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
999 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
drhfd405312005-11-06 04:06:59 +00001000 if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
drh75897232000-05-29 14:26:00 +00001001 bcfp->status = COMPLETE; /* Mark this config as used */
icculus9e44cf12010-02-14 17:14:22 +00001002 newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
1003 Plink_add(&newcfg->bplp,bcfp);
drh75897232000-05-29 14:26:00 +00001004 }
1005
1006 /* Get a pointer to the state described by the basis configuration set
1007 ** constructed in the preceding loop */
1008 newstp = getstate(lemp);
1009
1010 /* The state "newstp" is reached from the state "stp" by a shift action
1011 ** on the symbol "sp" */
drhfd405312005-11-06 04:06:59 +00001012 if( sp->type==MULTITERMINAL ){
1013 int i;
1014 for(i=0; i<sp->nsubsym; i++){
1015 Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
1016 }
1017 }else{
1018 Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
1019 }
drh75897232000-05-29 14:26:00 +00001020 }
1021}
1022
1023/*
1024** Construct the propagation links
1025*/
icculus9e44cf12010-02-14 17:14:22 +00001026void FindLinks(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001027{
1028 int i;
1029 struct config *cfp, *other;
1030 struct state *stp;
1031 struct plink *plp;
1032
1033 /* Housekeeping detail:
1034 ** Add to every propagate link a pointer back to the state to
1035 ** which the link is attached. */
1036 for(i=0; i<lemp->nstate; i++){
1037 stp = lemp->sorted[i];
1038 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1039 cfp->stp = stp;
1040 }
1041 }
1042
1043 /* Convert all backlinks into forward links. Only the forward
1044 ** links are used in the follow-set computation. */
1045 for(i=0; i<lemp->nstate; i++){
1046 stp = lemp->sorted[i];
1047 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1048 for(plp=cfp->bplp; plp; plp=plp->next){
1049 other = plp->cfp;
1050 Plink_add(&other->fplp,cfp);
1051 }
1052 }
1053 }
1054}
1055
1056/* Compute all followsets.
1057**
1058** A followset is the set of all symbols which can come immediately
1059** after a configuration.
1060*/
icculus9e44cf12010-02-14 17:14:22 +00001061void FindFollowSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001062{
1063 int i;
1064 struct config *cfp;
1065 struct plink *plp;
1066 int progress;
1067 int change;
1068
1069 for(i=0; i<lemp->nstate; i++){
1070 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1071 cfp->status = INCOMPLETE;
1072 }
1073 }
1074
1075 do{
1076 progress = 0;
1077 for(i=0; i<lemp->nstate; i++){
1078 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1079 if( cfp->status==COMPLETE ) continue;
1080 for(plp=cfp->fplp; plp; plp=plp->next){
1081 change = SetUnion(plp->cfp->fws,cfp->fws);
1082 if( change ){
1083 plp->cfp->status = INCOMPLETE;
1084 progress = 1;
drhf2f105d2012-08-20 15:53:54 +00001085 }
1086 }
drh75897232000-05-29 14:26:00 +00001087 cfp->status = COMPLETE;
1088 }
1089 }
1090 }while( progress );
1091}
1092
drh3cb2f6e2012-01-09 14:19:05 +00001093static int resolve_conflict(struct action *,struct action *);
drh75897232000-05-29 14:26:00 +00001094
1095/* Compute the reduce actions, and resolve conflicts.
1096*/
icculus9e44cf12010-02-14 17:14:22 +00001097void FindActions(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001098{
1099 int i,j;
1100 struct config *cfp;
1101 struct state *stp;
1102 struct symbol *sp;
1103 struct rule *rp;
1104
1105 /* Add all of the reduce actions
1106 ** A reduce action is added for each element of the followset of
1107 ** a configuration which has its dot at the extreme right.
1108 */
1109 for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
1110 stp = lemp->sorted[i];
1111 for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
1112 if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
1113 for(j=0; j<lemp->nterminal; j++){
1114 if( SetFind(cfp->fws,j) ){
1115 /* Add a reduce action to the state "stp" which will reduce by the
1116 ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
drh218dc692004-05-31 23:13:45 +00001117 Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
drh75897232000-05-29 14:26:00 +00001118 }
drhf2f105d2012-08-20 15:53:54 +00001119 }
drh75897232000-05-29 14:26:00 +00001120 }
1121 }
1122 }
1123
1124 /* Add the accepting token */
1125 if( lemp->start ){
1126 sp = Symbol_find(lemp->start);
drh4ef07702016-03-16 19:45:54 +00001127 if( sp==0 ) sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +00001128 }else{
drh4ef07702016-03-16 19:45:54 +00001129 sp = lemp->startRule->lhs;
drh75897232000-05-29 14:26:00 +00001130 }
1131 /* Add to the first state (which is always the starting state of the
1132 ** finite state machine) an action to ACCEPT if the lookahead is the
1133 ** start nonterminal. */
1134 Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
1135
1136 /* Resolve conflicts */
1137 for(i=0; i<lemp->nstate; i++){
1138 struct action *ap, *nap;
drh75897232000-05-29 14:26:00 +00001139 stp = lemp->sorted[i];
drhe9278182007-07-18 18:16:29 +00001140 /* assert( stp->ap ); */
drh75897232000-05-29 14:26:00 +00001141 stp->ap = Action_sort(stp->ap);
drhb59499c2002-02-23 18:45:13 +00001142 for(ap=stp->ap; ap && ap->next; ap=ap->next){
drh75897232000-05-29 14:26:00 +00001143 for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
1144 /* The two actions "ap" and "nap" have the same lookahead.
1145 ** Figure out which one should be used */
drh3cb2f6e2012-01-09 14:19:05 +00001146 lemp->nconflict += resolve_conflict(ap,nap);
drh75897232000-05-29 14:26:00 +00001147 }
1148 }
1149 }
1150
1151 /* Report an error for each rule that can never be reduced. */
drhaa9f1122007-08-23 02:50:56 +00001152 for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00001153 for(i=0; i<lemp->nstate; i++){
1154 struct action *ap;
1155 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
drhaa9f1122007-08-23 02:50:56 +00001156 if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +00001157 }
1158 }
1159 for(rp=lemp->rule; rp; rp=rp->next){
1160 if( rp->canReduce ) continue;
1161 ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
1162 lemp->errorcnt++;
1163 }
1164}
1165
1166/* Resolve a conflict between the two given actions. If the
drh34ff57b2008-07-14 12:27:51 +00001167** conflict can't be resolved, return non-zero.
drh75897232000-05-29 14:26:00 +00001168**
1169** NO LONGER TRUE:
1170** To resolve a conflict, first look to see if either action
1171** is on an error rule. In that case, take the action which
1172** is not associated with the error rule. If neither or both
1173** actions are associated with an error rule, then try to
1174** use precedence to resolve the conflict.
1175**
1176** If either action is a SHIFT, then it must be apx. This
1177** function won't work if apx->type==REDUCE and apy->type==SHIFT.
1178*/
icculus9e44cf12010-02-14 17:14:22 +00001179static int resolve_conflict(
1180 struct action *apx,
drh3cb2f6e2012-01-09 14:19:05 +00001181 struct action *apy
icculus9e44cf12010-02-14 17:14:22 +00001182){
drh75897232000-05-29 14:26:00 +00001183 struct symbol *spx, *spy;
1184 int errcnt = 0;
1185 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
drhf0fa1c12006-12-14 01:06:22 +00001186 if( apx->type==SHIFT && apy->type==SHIFT ){
drh9892c5d2007-12-21 00:02:11 +00001187 apy->type = SSCONFLICT;
drhf0fa1c12006-12-14 01:06:22 +00001188 errcnt++;
1189 }
drh75897232000-05-29 14:26:00 +00001190 if( apx->type==SHIFT && apy->type==REDUCE ){
1191 spx = apx->sp;
1192 spy = apy->x.rp->precsym;
1193 if( spy==0 || spx->prec<0 || spy->prec<0 ){
1194 /* Not enough precedence information. */
drh9892c5d2007-12-21 00:02:11 +00001195 apy->type = SRCONFLICT;
drh75897232000-05-29 14:26:00 +00001196 errcnt++;
drhdd7e9db2010-07-19 01:52:07 +00001197 }else if( spx->prec>spy->prec ){ /* higher precedence wins */
drh75897232000-05-29 14:26:00 +00001198 apy->type = RD_RESOLVED;
1199 }else if( spx->prec<spy->prec ){
1200 apx->type = SH_RESOLVED;
1201 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
1202 apy->type = RD_RESOLVED; /* associativity */
1203 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
1204 apx->type = SH_RESOLVED;
1205 }else{
1206 assert( spx->prec==spy->prec && spx->assoc==NONE );
drh62a223e2014-06-09 13:11:40 +00001207 apx->type = ERROR;
drh75897232000-05-29 14:26:00 +00001208 }
1209 }else if( apx->type==REDUCE && apy->type==REDUCE ){
1210 spx = apx->x.rp->precsym;
1211 spy = apy->x.rp->precsym;
1212 if( spx==0 || spy==0 || spx->prec<0 ||
1213 spy->prec<0 || spx->prec==spy->prec ){
drh9892c5d2007-12-21 00:02:11 +00001214 apy->type = RRCONFLICT;
drh75897232000-05-29 14:26:00 +00001215 errcnt++;
1216 }else if( spx->prec>spy->prec ){
1217 apy->type = RD_RESOLVED;
1218 }else if( spx->prec<spy->prec ){
1219 apx->type = RD_RESOLVED;
1220 }
1221 }else{
drhb59499c2002-02-23 18:45:13 +00001222 assert(
1223 apx->type==SH_RESOLVED ||
1224 apx->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001225 apx->type==SSCONFLICT ||
1226 apx->type==SRCONFLICT ||
1227 apx->type==RRCONFLICT ||
drhb59499c2002-02-23 18:45:13 +00001228 apy->type==SH_RESOLVED ||
1229 apy->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001230 apy->type==SSCONFLICT ||
1231 apy->type==SRCONFLICT ||
1232 apy->type==RRCONFLICT
drhb59499c2002-02-23 18:45:13 +00001233 );
1234 /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
1235 ** REDUCEs on the list. If we reach this point it must be because
1236 ** the parser conflict had already been resolved. */
drh75897232000-05-29 14:26:00 +00001237 }
1238 return errcnt;
1239}
1240/********************* From the file "configlist.c" *************************/
1241/*
1242** Routines to processing a configuration list and building a state
1243** in the LEMON parser generator.
1244*/
1245
1246static struct config *freelist = 0; /* List of free configurations */
1247static struct config *current = 0; /* Top of list of configurations */
1248static struct config **currentend = 0; /* Last on list of configs */
1249static struct config *basis = 0; /* Top of list of basis configs */
1250static struct config **basisend = 0; /* End of list of basis configs */
1251
1252/* Return a pointer to a new configuration */
1253PRIVATE struct config *newconfig(){
icculus9e44cf12010-02-14 17:14:22 +00001254 struct config *newcfg;
drh75897232000-05-29 14:26:00 +00001255 if( freelist==0 ){
1256 int i;
1257 int amt = 3;
drh9892c5d2007-12-21 00:02:11 +00001258 freelist = (struct config *)calloc( amt, sizeof(struct config) );
drh75897232000-05-29 14:26:00 +00001259 if( freelist==0 ){
1260 fprintf(stderr,"Unable to allocate memory for a new configuration.");
1261 exit(1);
1262 }
1263 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
1264 freelist[amt-1].next = 0;
1265 }
icculus9e44cf12010-02-14 17:14:22 +00001266 newcfg = freelist;
drh75897232000-05-29 14:26:00 +00001267 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00001268 return newcfg;
drh75897232000-05-29 14:26:00 +00001269}
1270
1271/* The configuration "old" is no longer used */
icculus9e44cf12010-02-14 17:14:22 +00001272PRIVATE void deleteconfig(struct config *old)
drh75897232000-05-29 14:26:00 +00001273{
1274 old->next = freelist;
1275 freelist = old;
1276}
1277
1278/* Initialized the configuration list builder */
1279void Configlist_init(){
1280 current = 0;
1281 currentend = &current;
1282 basis = 0;
1283 basisend = &basis;
1284 Configtable_init();
1285 return;
1286}
1287
1288/* Initialized the configuration list builder */
1289void Configlist_reset(){
1290 current = 0;
1291 currentend = &current;
1292 basis = 0;
1293 basisend = &basis;
1294 Configtable_clear(0);
1295 return;
1296}
1297
1298/* Add another configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001299struct config *Configlist_add(
1300 struct rule *rp, /* The rule */
1301 int dot /* Index into the RHS of the rule where the dot goes */
1302){
drh75897232000-05-29 14:26:00 +00001303 struct config *cfp, model;
1304
1305 assert( currentend!=0 );
1306 model.rp = rp;
1307 model.dot = dot;
1308 cfp = Configtable_find(&model);
1309 if( cfp==0 ){
1310 cfp = newconfig();
1311 cfp->rp = rp;
1312 cfp->dot = dot;
1313 cfp->fws = SetNew();
1314 cfp->stp = 0;
1315 cfp->fplp = cfp->bplp = 0;
1316 cfp->next = 0;
1317 cfp->bp = 0;
1318 *currentend = cfp;
1319 currentend = &cfp->next;
1320 Configtable_insert(cfp);
1321 }
1322 return cfp;
1323}
1324
1325/* Add a basis configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001326struct config *Configlist_addbasis(struct rule *rp, int dot)
drh75897232000-05-29 14:26:00 +00001327{
1328 struct config *cfp, model;
1329
1330 assert( basisend!=0 );
1331 assert( currentend!=0 );
1332 model.rp = rp;
1333 model.dot = dot;
1334 cfp = Configtable_find(&model);
1335 if( cfp==0 ){
1336 cfp = newconfig();
1337 cfp->rp = rp;
1338 cfp->dot = dot;
1339 cfp->fws = SetNew();
1340 cfp->stp = 0;
1341 cfp->fplp = cfp->bplp = 0;
1342 cfp->next = 0;
1343 cfp->bp = 0;
1344 *currentend = cfp;
1345 currentend = &cfp->next;
1346 *basisend = cfp;
1347 basisend = &cfp->bp;
1348 Configtable_insert(cfp);
1349 }
1350 return cfp;
1351}
1352
1353/* Compute the closure of the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001354void Configlist_closure(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001355{
1356 struct config *cfp, *newcfp;
1357 struct rule *rp, *newrp;
1358 struct symbol *sp, *xsp;
1359 int i, dot;
1360
1361 assert( currentend!=0 );
1362 for(cfp=current; cfp; cfp=cfp->next){
1363 rp = cfp->rp;
1364 dot = cfp->dot;
1365 if( dot>=rp->nrhs ) continue;
1366 sp = rp->rhs[dot];
1367 if( sp->type==NONTERMINAL ){
1368 if( sp->rule==0 && sp!=lemp->errsym ){
1369 ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
1370 sp->name);
1371 lemp->errorcnt++;
1372 }
1373 for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
1374 newcfp = Configlist_add(newrp,0);
1375 for(i=dot+1; i<rp->nrhs; i++){
1376 xsp = rp->rhs[i];
1377 if( xsp->type==TERMINAL ){
1378 SetAdd(newcfp->fws,xsp->index);
1379 break;
drhfd405312005-11-06 04:06:59 +00001380 }else if( xsp->type==MULTITERMINAL ){
1381 int k;
1382 for(k=0; k<xsp->nsubsym; k++){
1383 SetAdd(newcfp->fws, xsp->subsym[k]->index);
1384 }
1385 break;
drhf2f105d2012-08-20 15:53:54 +00001386 }else{
drh75897232000-05-29 14:26:00 +00001387 SetUnion(newcfp->fws,xsp->firstset);
drhaa9f1122007-08-23 02:50:56 +00001388 if( xsp->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +00001389 }
1390 }
drh75897232000-05-29 14:26:00 +00001391 if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
1392 }
1393 }
1394 }
1395 return;
1396}
1397
1398/* Sort the configuration list */
1399void Configlist_sort(){
drh25473362015-09-04 18:03:45 +00001400 current = (struct config*)msort((char*)current,(char**)&(current->next),
1401 Configcmp);
drh75897232000-05-29 14:26:00 +00001402 currentend = 0;
1403 return;
1404}
1405
1406/* Sort the basis configuration list */
1407void Configlist_sortbasis(){
drh25473362015-09-04 18:03:45 +00001408 basis = (struct config*)msort((char*)current,(char**)&(current->bp),
1409 Configcmp);
drh75897232000-05-29 14:26:00 +00001410 basisend = 0;
1411 return;
1412}
1413
1414/* Return a pointer to the head of the configuration list and
1415** reset the list */
1416struct config *Configlist_return(){
1417 struct config *old;
1418 old = current;
1419 current = 0;
1420 currentend = 0;
1421 return old;
1422}
1423
1424/* Return a pointer to the head of the configuration list and
1425** reset the list */
1426struct config *Configlist_basis(){
1427 struct config *old;
1428 old = basis;
1429 basis = 0;
1430 basisend = 0;
1431 return old;
1432}
1433
1434/* Free all elements of the given configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001435void Configlist_eat(struct config *cfp)
drh75897232000-05-29 14:26:00 +00001436{
1437 struct config *nextcfp;
1438 for(; cfp; cfp=nextcfp){
1439 nextcfp = cfp->next;
1440 assert( cfp->fplp==0 );
1441 assert( cfp->bplp==0 );
1442 if( cfp->fws ) SetFree(cfp->fws);
1443 deleteconfig(cfp);
1444 }
1445 return;
1446}
1447/***************** From the file "error.c" *********************************/
1448/*
1449** Code for printing error message.
1450*/
1451
drhf9a2e7b2003-04-15 01:49:48 +00001452void ErrorMsg(const char *filename, int lineno, const char *format, ...){
icculus15a2cec2010-02-16 16:07:28 +00001453 va_list ap;
icculus1c11f742010-02-15 00:01:04 +00001454 fprintf(stderr, "%s:%d: ", filename, lineno);
1455 va_start(ap, format);
1456 vfprintf(stderr,format,ap);
1457 va_end(ap);
1458 fprintf(stderr, "\n");
drh75897232000-05-29 14:26:00 +00001459}
1460/**************** From the file "main.c" ************************************/
1461/*
1462** Main program file for the LEMON parser generator.
1463*/
1464
1465/* Report an out-of-memory condition and abort. This function
1466** is used mostly by the "MemoryCheck" macro in struct.h
1467*/
1468void memory_error(){
1469 fprintf(stderr,"Out of memory. Aborting...\n");
1470 exit(1);
1471}
1472
drh6d08b4d2004-07-20 12:45:22 +00001473static int nDefine = 0; /* Number of -D options on the command line */
1474static char **azDefine = 0; /* Name of the -D macros */
1475
1476/* This routine is called with the argument to each -D command-line option.
1477** Add the macro defined to the azDefine array.
1478*/
1479static void handle_D_option(char *z){
1480 char **paz;
1481 nDefine++;
icculus9e44cf12010-02-14 17:14:22 +00001482 azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine);
drh6d08b4d2004-07-20 12:45:22 +00001483 if( azDefine==0 ){
1484 fprintf(stderr,"out of memory\n");
1485 exit(1);
1486 }
1487 paz = &azDefine[nDefine-1];
icculus9e44cf12010-02-14 17:14:22 +00001488 *paz = (char *) malloc( lemonStrlen(z)+1 );
drh6d08b4d2004-07-20 12:45:22 +00001489 if( *paz==0 ){
1490 fprintf(stderr,"out of memory\n");
1491 exit(1);
1492 }
drh898799f2014-01-10 23:21:00 +00001493 lemon_strcpy(*paz, z);
drh6d08b4d2004-07-20 12:45:22 +00001494 for(z=*paz; *z && *z!='='; z++){}
1495 *z = 0;
1496}
1497
icculus3e143bd2010-02-14 00:48:49 +00001498static char *user_templatename = NULL;
1499static void handle_T_option(char *z){
icculus9e44cf12010-02-14 17:14:22 +00001500 user_templatename = (char *) malloc( lemonStrlen(z)+1 );
icculus3e143bd2010-02-14 00:48:49 +00001501 if( user_templatename==0 ){
1502 memory_error();
1503 }
drh898799f2014-01-10 23:21:00 +00001504 lemon_strcpy(user_templatename, z);
icculus3e143bd2010-02-14 00:48:49 +00001505}
drh75897232000-05-29 14:26:00 +00001506
drh711c9812016-05-23 14:24:31 +00001507/* Merge together to lists of rules ordered by rule.iRule */
drh4ef07702016-03-16 19:45:54 +00001508static struct rule *Rule_merge(struct rule *pA, struct rule *pB){
1509 struct rule *pFirst = 0;
1510 struct rule **ppPrev = &pFirst;
1511 while( pA && pB ){
1512 if( pA->iRule<pB->iRule ){
1513 *ppPrev = pA;
1514 ppPrev = &pA->next;
1515 pA = pA->next;
1516 }else{
1517 *ppPrev = pB;
1518 ppPrev = &pB->next;
1519 pB = pB->next;
1520 }
1521 }
1522 if( pA ){
1523 *ppPrev = pA;
1524 }else{
1525 *ppPrev = pB;
1526 }
1527 return pFirst;
1528}
1529
1530/*
1531** Sort a list of rules in order of increasing iRule value
1532*/
1533static struct rule *Rule_sort(struct rule *rp){
1534 int i;
1535 struct rule *pNext;
1536 struct rule *x[32];
1537 memset(x, 0, sizeof(x));
1538 while( rp ){
1539 pNext = rp->next;
1540 rp->next = 0;
1541 for(i=0; i<sizeof(x)/sizeof(x[0]) && x[i]; i++){
1542 rp = Rule_merge(x[i], rp);
1543 x[i] = 0;
1544 }
1545 x[i] = rp;
1546 rp = pNext;
1547 }
1548 rp = 0;
1549 for(i=0; i<sizeof(x)/sizeof(x[0]); i++){
1550 rp = Rule_merge(x[i], rp);
1551 }
1552 return rp;
1553}
1554
drhc75e0162015-09-07 02:23:02 +00001555/* forward reference */
1556static const char *minimum_size_type(int lwr, int upr, int *pnByte);
1557
1558/* Print a single line of the "Parser Stats" output
1559*/
1560static void stats_line(const char *zLabel, int iValue){
1561 int nLabel = lemonStrlen(zLabel);
1562 printf(" %s%.*s %5d\n", zLabel,
1563 35-nLabel, "................................",
1564 iValue);
1565}
1566
drh75897232000-05-29 14:26:00 +00001567/* The main program. Parse the command line and do it... */
icculus9e44cf12010-02-14 17:14:22 +00001568int main(int argc, char **argv)
drh75897232000-05-29 14:26:00 +00001569{
1570 static int version = 0;
1571 static int rpflag = 0;
1572 static int basisflag = 0;
1573 static int compress = 0;
1574 static int quiet = 0;
1575 static int statistics = 0;
1576 static int mhflag = 0;
shane58543932008-12-10 20:10:04 +00001577 static int nolinenosflag = 0;
drhdd7e9db2010-07-19 01:52:07 +00001578 static int noResort = 0;
drh75897232000-05-29 14:26:00 +00001579 static struct s_options options[] = {
1580 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1581 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
drh6d08b4d2004-07-20 12:45:22 +00001582 {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
drh0325d392015-01-01 19:11:22 +00001583 {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"},
drh75897232000-05-29 14:26:00 +00001584 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
drh0325d392015-01-01 19:11:22 +00001585 {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"},
shane58543932008-12-10 20:10:04 +00001586 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
1587 {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
drh0325d392015-01-01 19:11:22 +00001588 {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"},
drhf5c4e0f2010-07-18 11:35:53 +00001589 {OPT_FLAG, "p", (char*)&showPrecedenceConflict,
1590 "Show conflicts resolved by precedence rules"},
drh75897232000-05-29 14:26:00 +00001591 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
drhdd7e9db2010-07-19 01:52:07 +00001592 {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"},
drh6d08b4d2004-07-20 12:45:22 +00001593 {OPT_FLAG, "s", (char*)&statistics,
1594 "Print parser stats to standard output."},
drh75897232000-05-29 14:26:00 +00001595 {OPT_FLAG, "x", (char*)&version, "Print the version number."},
drh0325d392015-01-01 19:11:22 +00001596 {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
1597 {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"},
drh75897232000-05-29 14:26:00 +00001598 {OPT_FLAG,0,0,0}
1599 };
1600 int i;
icculus42585cf2010-02-14 05:19:56 +00001601 int exitcode;
drh75897232000-05-29 14:26:00 +00001602 struct lemon lem;
drh4ef07702016-03-16 19:45:54 +00001603 struct rule *rp;
drh75897232000-05-29 14:26:00 +00001604
drhb0c86772000-06-02 23:21:26 +00001605 OptInit(argv,options,stderr);
drh75897232000-05-29 14:26:00 +00001606 if( version ){
drhb19a2bc2001-09-16 00:13:26 +00001607 printf("Lemon version 1.0\n");
drh75897232000-05-29 14:26:00 +00001608 exit(0);
1609 }
drhb0c86772000-06-02 23:21:26 +00001610 if( OptNArgs()!=1 ){
drh75897232000-05-29 14:26:00 +00001611 fprintf(stderr,"Exactly one filename argument is required.\n");
1612 exit(1);
1613 }
drh954f6b42006-06-13 13:27:46 +00001614 memset(&lem, 0, sizeof(lem));
drh75897232000-05-29 14:26:00 +00001615 lem.errorcnt = 0;
1616
1617 /* Initialize the machine */
1618 Strsafe_init();
1619 Symbol_init();
1620 State_init();
1621 lem.argv0 = argv[0];
drhb0c86772000-06-02 23:21:26 +00001622 lem.filename = OptArg(0);
drh75897232000-05-29 14:26:00 +00001623 lem.basisflag = basisflag;
shane58543932008-12-10 20:10:04 +00001624 lem.nolinenosflag = nolinenosflag;
drh75897232000-05-29 14:26:00 +00001625 Symbol_new("$");
1626 lem.errsym = Symbol_new("error");
drhc4dd3fd2008-01-22 01:48:05 +00001627 lem.errsym->useCnt = 0;
drh75897232000-05-29 14:26:00 +00001628
1629 /* Parse the input file */
1630 Parse(&lem);
1631 if( lem.errorcnt ) exit(lem.errorcnt);
drh954f6b42006-06-13 13:27:46 +00001632 if( lem.nrule==0 ){
drh75897232000-05-29 14:26:00 +00001633 fprintf(stderr,"Empty grammar.\n");
1634 exit(1);
1635 }
1636
1637 /* Count and index the symbols of the grammar */
drh75897232000-05-29 14:26:00 +00001638 Symbol_new("{default}");
drh61f92cd2014-01-11 03:06:18 +00001639 lem.nsymbol = Symbol_count();
drh75897232000-05-29 14:26:00 +00001640 lem.symbols = Symbol_arrayof();
drh61f92cd2014-01-11 03:06:18 +00001641 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1642 qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp);
1643 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1644 while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; }
1645 assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 );
1646 lem.nsymbol = i - 1;
drhc56fac72015-10-29 13:48:15 +00001647 for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++);
drh75897232000-05-29 14:26:00 +00001648 lem.nterminal = i;
1649
drh711c9812016-05-23 14:24:31 +00001650 /* Assign sequential rule numbers. Start with 0. Put rules that have no
1651 ** reduce action C-code associated with them last, so that the switch()
1652 ** statement that selects reduction actions will have a smaller jump table.
1653 */
drh4ef07702016-03-16 19:45:54 +00001654 for(i=0, rp=lem.rule; rp; rp=rp->next){
1655 rp->iRule = rp->code ? i++ : -1;
1656 }
1657 for(rp=lem.rule; rp; rp=rp->next){
1658 if( rp->iRule<0 ) rp->iRule = i++;
1659 }
1660 lem.startRule = lem.rule;
1661 lem.rule = Rule_sort(lem.rule);
1662
drh75897232000-05-29 14:26:00 +00001663 /* Generate a reprint of the grammar, if requested on the command line */
1664 if( rpflag ){
1665 Reprint(&lem);
1666 }else{
1667 /* Initialize the size for all follow and first sets */
drh9892c5d2007-12-21 00:02:11 +00001668 SetSize(lem.nterminal+1);
drh75897232000-05-29 14:26:00 +00001669
1670 /* Find the precedence for every production rule (that has one) */
1671 FindRulePrecedences(&lem);
1672
1673 /* Compute the lambda-nonterminals and the first-sets for every
1674 ** nonterminal */
1675 FindFirstSets(&lem);
1676
1677 /* Compute all LR(0) states. Also record follow-set propagation
1678 ** links so that the follow-set can be computed later */
1679 lem.nstate = 0;
1680 FindStates(&lem);
1681 lem.sorted = State_arrayof();
1682
1683 /* Tie up loose ends on the propagation links */
1684 FindLinks(&lem);
1685
1686 /* Compute the follow set of every reducible configuration */
1687 FindFollowSets(&lem);
1688
1689 /* Compute the action tables */
1690 FindActions(&lem);
1691
1692 /* Compress the action tables */
1693 if( compress==0 ) CompressTables(&lem);
1694
drhada354d2005-11-05 15:03:59 +00001695 /* Reorder and renumber the states so that states with fewer choices
drhdd7e9db2010-07-19 01:52:07 +00001696 ** occur at the end. This is an optimization that helps make the
1697 ** generated parser tables smaller. */
1698 if( noResort==0 ) ResortStates(&lem);
drhada354d2005-11-05 15:03:59 +00001699
drh75897232000-05-29 14:26:00 +00001700 /* Generate a report of the parser generated. (the "y.output" file) */
1701 if( !quiet ) ReportOutput(&lem);
1702
1703 /* Generate the source code for the parser */
1704 ReportTable(&lem, mhflag);
1705
1706 /* Produce a header file for use by the scanner. (This step is
1707 ** omitted if the "-m" option is used because makeheaders will
1708 ** generate the file for us.) */
1709 if( !mhflag ) ReportHeader(&lem);
1710 }
1711 if( statistics ){
drhc75e0162015-09-07 02:23:02 +00001712 printf("Parser statistics:\n");
1713 stats_line("terminal symbols", lem.nterminal);
1714 stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal);
1715 stats_line("total symbols", lem.nsymbol);
1716 stats_line("rules", lem.nrule);
drh3bd48ab2015-09-07 18:23:37 +00001717 stats_line("states", lem.nxstate);
drhc75e0162015-09-07 02:23:02 +00001718 stats_line("conflicts", lem.nconflict);
1719 stats_line("action table entries", lem.nactiontab);
1720 stats_line("total table size (bytes)", lem.tablesize);
drh75897232000-05-29 14:26:00 +00001721 }
icculus8e158022010-02-16 16:09:03 +00001722 if( lem.nconflict > 0 ){
1723 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
icculus42585cf2010-02-14 05:19:56 +00001724 }
1725
1726 /* return 0 on success, 1 on failure. */
icculus8e158022010-02-16 16:09:03 +00001727 exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
icculus42585cf2010-02-14 05:19:56 +00001728 exit(exitcode);
1729 return (exitcode);
drh75897232000-05-29 14:26:00 +00001730}
1731/******************** From the file "msort.c" *******************************/
1732/*
1733** A generic merge-sort program.
1734**
1735** USAGE:
1736** Let "ptr" be a pointer to some structure which is at the head of
1737** a null-terminated list. Then to sort the list call:
1738**
1739** ptr = msort(ptr,&(ptr->next),cmpfnc);
1740**
1741** In the above, "cmpfnc" is a pointer to a function which compares
1742** two instances of the structure and returns an integer, as in
1743** strcmp. The second argument is a pointer to the pointer to the
1744** second element of the linked list. This address is used to compute
1745** the offset to the "next" field within the structure. The offset to
1746** the "next" field must be constant for all structures in the list.
1747**
1748** The function returns a new pointer which is the head of the list
1749** after sorting.
1750**
1751** ALGORITHM:
1752** Merge-sort.
1753*/
1754
1755/*
1756** Return a pointer to the next structure in the linked list.
1757*/
drhd25d6922012-04-18 09:59:56 +00001758#define NEXT(A) (*(char**)(((char*)A)+offset))
drh75897232000-05-29 14:26:00 +00001759
1760/*
1761** Inputs:
1762** a: A sorted, null-terminated linked list. (May be null).
1763** b: A sorted, null-terminated linked list. (May be null).
1764** cmp: A pointer to the comparison function.
1765** offset: Offset in the structure to the "next" field.
1766**
1767** Return Value:
1768** A pointer to the head of a sorted list containing the elements
1769** of both a and b.
1770**
1771** Side effects:
1772** The "next" pointers for elements in the lists a and b are
1773** changed.
1774*/
drhe9278182007-07-18 18:16:29 +00001775static char *merge(
1776 char *a,
1777 char *b,
1778 int (*cmp)(const char*,const char*),
1779 int offset
1780){
drh75897232000-05-29 14:26:00 +00001781 char *ptr, *head;
1782
1783 if( a==0 ){
1784 head = b;
1785 }else if( b==0 ){
1786 head = a;
1787 }else{
drhe594bc32009-11-03 13:02:25 +00001788 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001789 ptr = a;
1790 a = NEXT(a);
1791 }else{
1792 ptr = b;
1793 b = NEXT(b);
1794 }
1795 head = ptr;
1796 while( a && b ){
drhe594bc32009-11-03 13:02:25 +00001797 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001798 NEXT(ptr) = a;
1799 ptr = a;
1800 a = NEXT(a);
1801 }else{
1802 NEXT(ptr) = b;
1803 ptr = b;
1804 b = NEXT(b);
1805 }
1806 }
1807 if( a ) NEXT(ptr) = a;
1808 else NEXT(ptr) = b;
1809 }
1810 return head;
1811}
1812
1813/*
1814** Inputs:
1815** list: Pointer to a singly-linked list of structures.
1816** next: Pointer to pointer to the second element of the list.
1817** cmp: A comparison function.
1818**
1819** Return Value:
1820** A pointer to the head of a sorted list containing the elements
1821** orginally in list.
1822**
1823** Side effects:
1824** The "next" pointers for elements in list are changed.
1825*/
1826#define LISTSIZE 30
drhe9278182007-07-18 18:16:29 +00001827static char *msort(
1828 char *list,
1829 char **next,
1830 int (*cmp)(const char*,const char*)
1831){
drhba99af52001-10-25 20:37:16 +00001832 unsigned long offset;
drh75897232000-05-29 14:26:00 +00001833 char *ep;
1834 char *set[LISTSIZE];
1835 int i;
drh1cc0d112015-03-31 15:15:48 +00001836 offset = (unsigned long)((char*)next - (char*)list);
drh75897232000-05-29 14:26:00 +00001837 for(i=0; i<LISTSIZE; i++) set[i] = 0;
1838 while( list ){
1839 ep = list;
1840 list = NEXT(list);
1841 NEXT(ep) = 0;
1842 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1843 ep = merge(ep,set[i],cmp,offset);
1844 set[i] = 0;
1845 }
1846 set[i] = ep;
1847 }
1848 ep = 0;
drhe594bc32009-11-03 13:02:25 +00001849 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset);
drh75897232000-05-29 14:26:00 +00001850 return ep;
1851}
1852/************************ From the file "option.c" **************************/
1853static char **argv;
1854static struct s_options *op;
1855static FILE *errstream;
1856
1857#define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1858
1859/*
1860** Print the command line with a carrot pointing to the k-th character
1861** of the n-th field.
1862*/
icculus9e44cf12010-02-14 17:14:22 +00001863static void errline(int n, int k, FILE *err)
drh75897232000-05-29 14:26:00 +00001864{
1865 int spcnt, i;
drh75897232000-05-29 14:26:00 +00001866 if( argv[0] ) fprintf(err,"%s",argv[0]);
drh87cf1372008-08-13 20:09:06 +00001867 spcnt = lemonStrlen(argv[0]) + 1;
drh75897232000-05-29 14:26:00 +00001868 for(i=1; i<n && argv[i]; i++){
1869 fprintf(err," %s",argv[i]);
drh87cf1372008-08-13 20:09:06 +00001870 spcnt += lemonStrlen(argv[i])+1;
drh75897232000-05-29 14:26:00 +00001871 }
1872 spcnt += k;
1873 for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1874 if( spcnt<20 ){
1875 fprintf(err,"\n%*s^-- here\n",spcnt,"");
1876 }else{
1877 fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1878 }
1879}
1880
1881/*
1882** Return the index of the N-th non-switch argument. Return -1
1883** if N is out of range.
1884*/
icculus9e44cf12010-02-14 17:14:22 +00001885static int argindex(int n)
drh75897232000-05-29 14:26:00 +00001886{
1887 int i;
1888 int dashdash = 0;
1889 if( argv!=0 && *argv!=0 ){
1890 for(i=1; argv[i]; i++){
1891 if( dashdash || !ISOPT(argv[i]) ){
1892 if( n==0 ) return i;
1893 n--;
1894 }
1895 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1896 }
1897 }
1898 return -1;
1899}
1900
1901static char emsg[] = "Command line syntax error: ";
1902
1903/*
1904** Process a flag command line argument.
1905*/
icculus9e44cf12010-02-14 17:14:22 +00001906static int handleflags(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001907{
1908 int v;
1909 int errcnt = 0;
1910 int j;
1911 for(j=0; op[j].label; j++){
drh87cf1372008-08-13 20:09:06 +00001912 if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
drh75897232000-05-29 14:26:00 +00001913 }
1914 v = argv[i][0]=='-' ? 1 : 0;
1915 if( op[j].label==0 ){
1916 if( err ){
1917 fprintf(err,"%sundefined option.\n",emsg);
1918 errline(i,1,err);
1919 }
1920 errcnt++;
drh0325d392015-01-01 19:11:22 +00001921 }else if( op[j].arg==0 ){
1922 /* Ignore this option */
drh75897232000-05-29 14:26:00 +00001923 }else if( op[j].type==OPT_FLAG ){
1924 *((int*)op[j].arg) = v;
1925 }else if( op[j].type==OPT_FFLAG ){
icculus9e44cf12010-02-14 17:14:22 +00001926 (*(void(*)(int))(op[j].arg))(v);
drh6d08b4d2004-07-20 12:45:22 +00001927 }else if( op[j].type==OPT_FSTR ){
icculus9e44cf12010-02-14 17:14:22 +00001928 (*(void(*)(char *))(op[j].arg))(&argv[i][2]);
drh75897232000-05-29 14:26:00 +00001929 }else{
1930 if( err ){
1931 fprintf(err,"%smissing argument on switch.\n",emsg);
1932 errline(i,1,err);
1933 }
1934 errcnt++;
1935 }
1936 return errcnt;
1937}
1938
1939/*
1940** Process a command line switch which has an argument.
1941*/
icculus9e44cf12010-02-14 17:14:22 +00001942static int handleswitch(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001943{
1944 int lv = 0;
1945 double dv = 0.0;
1946 char *sv = 0, *end;
1947 char *cp;
1948 int j;
1949 int errcnt = 0;
1950 cp = strchr(argv[i],'=');
drh43617e92006-03-06 20:55:46 +00001951 assert( cp!=0 );
drh75897232000-05-29 14:26:00 +00001952 *cp = 0;
1953 for(j=0; op[j].label; j++){
1954 if( strcmp(argv[i],op[j].label)==0 ) break;
1955 }
1956 *cp = '=';
1957 if( op[j].label==0 ){
1958 if( err ){
1959 fprintf(err,"%sundefined option.\n",emsg);
1960 errline(i,0,err);
1961 }
1962 errcnt++;
1963 }else{
1964 cp++;
1965 switch( op[j].type ){
1966 case OPT_FLAG:
1967 case OPT_FFLAG:
1968 if( err ){
1969 fprintf(err,"%soption requires an argument.\n",emsg);
1970 errline(i,0,err);
1971 }
1972 errcnt++;
1973 break;
1974 case OPT_DBL:
1975 case OPT_FDBL:
1976 dv = strtod(cp,&end);
1977 if( *end ){
1978 if( err ){
drh25473362015-09-04 18:03:45 +00001979 fprintf(err,
1980 "%sillegal character in floating-point argument.\n",emsg);
drh1cc0d112015-03-31 15:15:48 +00001981 errline(i,(int)((char*)end-(char*)argv[i]),err);
drh75897232000-05-29 14:26:00 +00001982 }
1983 errcnt++;
1984 }
1985 break;
1986 case OPT_INT:
1987 case OPT_FINT:
1988 lv = strtol(cp,&end,0);
1989 if( *end ){
1990 if( err ){
1991 fprintf(err,"%sillegal character in integer argument.\n",emsg);
drh1cc0d112015-03-31 15:15:48 +00001992 errline(i,(int)((char*)end-(char*)argv[i]),err);
drh75897232000-05-29 14:26:00 +00001993 }
1994 errcnt++;
1995 }
1996 break;
1997 case OPT_STR:
1998 case OPT_FSTR:
1999 sv = cp;
2000 break;
2001 }
2002 switch( op[j].type ){
2003 case OPT_FLAG:
2004 case OPT_FFLAG:
2005 break;
2006 case OPT_DBL:
2007 *(double*)(op[j].arg) = dv;
2008 break;
2009 case OPT_FDBL:
icculus9e44cf12010-02-14 17:14:22 +00002010 (*(void(*)(double))(op[j].arg))(dv);
drh75897232000-05-29 14:26:00 +00002011 break;
2012 case OPT_INT:
2013 *(int*)(op[j].arg) = lv;
2014 break;
2015 case OPT_FINT:
icculus9e44cf12010-02-14 17:14:22 +00002016 (*(void(*)(int))(op[j].arg))((int)lv);
drh75897232000-05-29 14:26:00 +00002017 break;
2018 case OPT_STR:
2019 *(char**)(op[j].arg) = sv;
2020 break;
2021 case OPT_FSTR:
icculus9e44cf12010-02-14 17:14:22 +00002022 (*(void(*)(char *))(op[j].arg))(sv);
drh75897232000-05-29 14:26:00 +00002023 break;
2024 }
2025 }
2026 return errcnt;
2027}
2028
icculus9e44cf12010-02-14 17:14:22 +00002029int OptInit(char **a, struct s_options *o, FILE *err)
drh75897232000-05-29 14:26:00 +00002030{
2031 int errcnt = 0;
2032 argv = a;
2033 op = o;
2034 errstream = err;
2035 if( argv && *argv && op ){
2036 int i;
2037 for(i=1; argv[i]; i++){
2038 if( argv[i][0]=='+' || argv[i][0]=='-' ){
2039 errcnt += handleflags(i,err);
2040 }else if( strchr(argv[i],'=') ){
2041 errcnt += handleswitch(i,err);
2042 }
2043 }
2044 }
2045 if( errcnt>0 ){
2046 fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
drhb0c86772000-06-02 23:21:26 +00002047 OptPrint();
drh75897232000-05-29 14:26:00 +00002048 exit(1);
2049 }
2050 return 0;
2051}
2052
drhb0c86772000-06-02 23:21:26 +00002053int OptNArgs(){
drh75897232000-05-29 14:26:00 +00002054 int cnt = 0;
2055 int dashdash = 0;
2056 int i;
2057 if( argv!=0 && argv[0]!=0 ){
2058 for(i=1; argv[i]; i++){
2059 if( dashdash || !ISOPT(argv[i]) ) cnt++;
2060 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
2061 }
2062 }
2063 return cnt;
2064}
2065
icculus9e44cf12010-02-14 17:14:22 +00002066char *OptArg(int n)
drh75897232000-05-29 14:26:00 +00002067{
2068 int i;
2069 i = argindex(n);
2070 return i>=0 ? argv[i] : 0;
2071}
2072
icculus9e44cf12010-02-14 17:14:22 +00002073void OptErr(int n)
drh75897232000-05-29 14:26:00 +00002074{
2075 int i;
2076 i = argindex(n);
2077 if( i>=0 ) errline(i,0,errstream);
2078}
2079
drhb0c86772000-06-02 23:21:26 +00002080void OptPrint(){
drh75897232000-05-29 14:26:00 +00002081 int i;
2082 int max, len;
2083 max = 0;
2084 for(i=0; op[i].label; i++){
drh87cf1372008-08-13 20:09:06 +00002085 len = lemonStrlen(op[i].label) + 1;
drh75897232000-05-29 14:26:00 +00002086 switch( op[i].type ){
2087 case OPT_FLAG:
2088 case OPT_FFLAG:
2089 break;
2090 case OPT_INT:
2091 case OPT_FINT:
2092 len += 9; /* length of "<integer>" */
2093 break;
2094 case OPT_DBL:
2095 case OPT_FDBL:
2096 len += 6; /* length of "<real>" */
2097 break;
2098 case OPT_STR:
2099 case OPT_FSTR:
2100 len += 8; /* length of "<string>" */
2101 break;
2102 }
2103 if( len>max ) max = len;
2104 }
2105 for(i=0; op[i].label; i++){
2106 switch( op[i].type ){
2107 case OPT_FLAG:
2108 case OPT_FFLAG:
2109 fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
2110 break;
2111 case OPT_INT:
2112 case OPT_FINT:
drh0325d392015-01-01 19:11:22 +00002113 fprintf(errstream," -%s<integer>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002114 (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002115 break;
2116 case OPT_DBL:
2117 case OPT_FDBL:
drh0325d392015-01-01 19:11:22 +00002118 fprintf(errstream," -%s<real>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002119 (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002120 break;
2121 case OPT_STR:
2122 case OPT_FSTR:
drh0325d392015-01-01 19:11:22 +00002123 fprintf(errstream," -%s<string>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002124 (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002125 break;
2126 }
2127 }
2128}
2129/*********************** From the file "parse.c" ****************************/
2130/*
2131** Input file parser for the LEMON parser generator.
2132*/
2133
2134/* The state of the parser */
icculus9e44cf12010-02-14 17:14:22 +00002135enum e_state {
2136 INITIALIZE,
2137 WAITING_FOR_DECL_OR_RULE,
2138 WAITING_FOR_DECL_KEYWORD,
2139 WAITING_FOR_DECL_ARG,
2140 WAITING_FOR_PRECEDENCE_SYMBOL,
2141 WAITING_FOR_ARROW,
2142 IN_RHS,
2143 LHS_ALIAS_1,
2144 LHS_ALIAS_2,
2145 LHS_ALIAS_3,
2146 RHS_ALIAS_1,
2147 RHS_ALIAS_2,
2148 PRECEDENCE_MARK_1,
2149 PRECEDENCE_MARK_2,
2150 RESYNC_AFTER_RULE_ERROR,
2151 RESYNC_AFTER_DECL_ERROR,
2152 WAITING_FOR_DESTRUCTOR_SYMBOL,
2153 WAITING_FOR_DATATYPE_SYMBOL,
2154 WAITING_FOR_FALLBACK_ID,
drh61f92cd2014-01-11 03:06:18 +00002155 WAITING_FOR_WILDCARD_ID,
2156 WAITING_FOR_CLASS_ID,
2157 WAITING_FOR_CLASS_TOKEN
icculus9e44cf12010-02-14 17:14:22 +00002158};
drh75897232000-05-29 14:26:00 +00002159struct pstate {
2160 char *filename; /* Name of the input file */
2161 int tokenlineno; /* Linenumber at which current token starts */
2162 int errorcnt; /* Number of errors so far */
2163 char *tokenstart; /* Text of current token */
2164 struct lemon *gp; /* Global state vector */
icculus9e44cf12010-02-14 17:14:22 +00002165 enum e_state state; /* The state of the parser */
drh0bd1f4e2002-06-06 18:54:39 +00002166 struct symbol *fallback; /* The fallback token */
drh61f92cd2014-01-11 03:06:18 +00002167 struct symbol *tkclass; /* Token class symbol */
drh75897232000-05-29 14:26:00 +00002168 struct symbol *lhs; /* Left-hand side of current rule */
icculus9e44cf12010-02-14 17:14:22 +00002169 const char *lhsalias; /* Alias for the LHS */
drh75897232000-05-29 14:26:00 +00002170 int nrhs; /* Number of right-hand side symbols seen */
2171 struct symbol *rhs[MAXRHS]; /* RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +00002172 const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
drh75897232000-05-29 14:26:00 +00002173 struct rule *prevrule; /* Previous rule parsed */
icculus9e44cf12010-02-14 17:14:22 +00002174 const char *declkeyword; /* Keyword of a declaration */
drh75897232000-05-29 14:26:00 +00002175 char **declargslot; /* Where the declaration argument should be put */
drha5808f32008-04-27 22:19:44 +00002176 int insertLineMacro; /* Add #line before declaration insert */
drh4dc8ef52008-07-01 17:13:57 +00002177 int *decllinenoslot; /* Where to write declaration line number */
drh75897232000-05-29 14:26:00 +00002178 enum e_assoc declassoc; /* Assign this association to decl arguments */
2179 int preccounter; /* Assign this precedence to decl arguments */
2180 struct rule *firstrule; /* Pointer to first rule in the grammar */
2181 struct rule *lastrule; /* Pointer to the most recently parsed rule */
2182};
2183
2184/* Parse a single token */
icculus9e44cf12010-02-14 17:14:22 +00002185static void parseonetoken(struct pstate *psp)
drh75897232000-05-29 14:26:00 +00002186{
icculus9e44cf12010-02-14 17:14:22 +00002187 const char *x;
drh75897232000-05-29 14:26:00 +00002188 x = Strsafe(psp->tokenstart); /* Save the token permanently */
2189#if 0
2190 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
2191 x,psp->state);
2192#endif
2193 switch( psp->state ){
2194 case INITIALIZE:
2195 psp->prevrule = 0;
2196 psp->preccounter = 0;
2197 psp->firstrule = psp->lastrule = 0;
2198 psp->gp->nrule = 0;
2199 /* Fall thru to next case */
2200 case WAITING_FOR_DECL_OR_RULE:
2201 if( x[0]=='%' ){
2202 psp->state = WAITING_FOR_DECL_KEYWORD;
drhc56fac72015-10-29 13:48:15 +00002203 }else if( ISLOWER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002204 psp->lhs = Symbol_new(x);
2205 psp->nrhs = 0;
2206 psp->lhsalias = 0;
2207 psp->state = WAITING_FOR_ARROW;
2208 }else if( x[0]=='{' ){
2209 if( psp->prevrule==0 ){
2210 ErrorMsg(psp->filename,psp->tokenlineno,
drh3cb2f6e2012-01-09 14:19:05 +00002211"There is no prior rule upon which to attach the code \
drh75897232000-05-29 14:26:00 +00002212fragment which begins on this line.");
2213 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002214 }else if( psp->prevrule->code!=0 ){
drh75897232000-05-29 14:26:00 +00002215 ErrorMsg(psp->filename,psp->tokenlineno,
2216"Code fragment beginning on this line is not the first \
2217to follow the previous rule.");
2218 psp->errorcnt++;
2219 }else{
2220 psp->prevrule->line = psp->tokenlineno;
2221 psp->prevrule->code = &x[1];
drh711c9812016-05-23 14:24:31 +00002222 psp->prevrule->noCode = 0;
drhf2f105d2012-08-20 15:53:54 +00002223 }
drh75897232000-05-29 14:26:00 +00002224 }else if( x[0]=='[' ){
2225 psp->state = PRECEDENCE_MARK_1;
2226 }else{
2227 ErrorMsg(psp->filename,psp->tokenlineno,
2228 "Token \"%s\" should be either \"%%\" or a nonterminal name.",
2229 x);
2230 psp->errorcnt++;
2231 }
2232 break;
2233 case PRECEDENCE_MARK_1:
drhc56fac72015-10-29 13:48:15 +00002234 if( !ISUPPER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002235 ErrorMsg(psp->filename,psp->tokenlineno,
2236 "The precedence symbol must be a terminal.");
2237 psp->errorcnt++;
2238 }else if( psp->prevrule==0 ){
2239 ErrorMsg(psp->filename,psp->tokenlineno,
2240 "There is no prior rule to assign precedence \"[%s]\".",x);
2241 psp->errorcnt++;
2242 }else if( psp->prevrule->precsym!=0 ){
2243 ErrorMsg(psp->filename,psp->tokenlineno,
2244"Precedence mark on this line is not the first \
2245to follow the previous rule.");
2246 psp->errorcnt++;
2247 }else{
2248 psp->prevrule->precsym = Symbol_new(x);
2249 }
2250 psp->state = PRECEDENCE_MARK_2;
2251 break;
2252 case PRECEDENCE_MARK_2:
2253 if( x[0]!=']' ){
2254 ErrorMsg(psp->filename,psp->tokenlineno,
2255 "Missing \"]\" on precedence mark.");
2256 psp->errorcnt++;
2257 }
2258 psp->state = WAITING_FOR_DECL_OR_RULE;
2259 break;
2260 case WAITING_FOR_ARROW:
2261 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2262 psp->state = IN_RHS;
2263 }else if( x[0]=='(' ){
2264 psp->state = LHS_ALIAS_1;
2265 }else{
2266 ErrorMsg(psp->filename,psp->tokenlineno,
2267 "Expected to see a \":\" following the LHS symbol \"%s\".",
2268 psp->lhs->name);
2269 psp->errorcnt++;
2270 psp->state = RESYNC_AFTER_RULE_ERROR;
2271 }
2272 break;
2273 case LHS_ALIAS_1:
drhc56fac72015-10-29 13:48:15 +00002274 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002275 psp->lhsalias = x;
2276 psp->state = LHS_ALIAS_2;
2277 }else{
2278 ErrorMsg(psp->filename,psp->tokenlineno,
2279 "\"%s\" is not a valid alias for the LHS \"%s\"\n",
2280 x,psp->lhs->name);
2281 psp->errorcnt++;
2282 psp->state = RESYNC_AFTER_RULE_ERROR;
2283 }
2284 break;
2285 case LHS_ALIAS_2:
2286 if( x[0]==')' ){
2287 psp->state = LHS_ALIAS_3;
2288 }else{
2289 ErrorMsg(psp->filename,psp->tokenlineno,
2290 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2291 psp->errorcnt++;
2292 psp->state = RESYNC_AFTER_RULE_ERROR;
2293 }
2294 break;
2295 case LHS_ALIAS_3:
2296 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2297 psp->state = IN_RHS;
2298 }else{
2299 ErrorMsg(psp->filename,psp->tokenlineno,
2300 "Missing \"->\" following: \"%s(%s)\".",
2301 psp->lhs->name,psp->lhsalias);
2302 psp->errorcnt++;
2303 psp->state = RESYNC_AFTER_RULE_ERROR;
2304 }
2305 break;
2306 case IN_RHS:
2307 if( x[0]=='.' ){
2308 struct rule *rp;
drh9892c5d2007-12-21 00:02:11 +00002309 rp = (struct rule *)calloc( sizeof(struct rule) +
2310 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
drh75897232000-05-29 14:26:00 +00002311 if( rp==0 ){
2312 ErrorMsg(psp->filename,psp->tokenlineno,
2313 "Can't allocate enough memory for this rule.");
2314 psp->errorcnt++;
2315 psp->prevrule = 0;
drhf2f105d2012-08-20 15:53:54 +00002316 }else{
drh75897232000-05-29 14:26:00 +00002317 int i;
2318 rp->ruleline = psp->tokenlineno;
2319 rp->rhs = (struct symbol**)&rp[1];
icculus9e44cf12010-02-14 17:14:22 +00002320 rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
drh75897232000-05-29 14:26:00 +00002321 for(i=0; i<psp->nrhs; i++){
2322 rp->rhs[i] = psp->rhs[i];
2323 rp->rhsalias[i] = psp->alias[i];
drhf2f105d2012-08-20 15:53:54 +00002324 }
drh75897232000-05-29 14:26:00 +00002325 rp->lhs = psp->lhs;
2326 rp->lhsalias = psp->lhsalias;
2327 rp->nrhs = psp->nrhs;
2328 rp->code = 0;
drh711c9812016-05-23 14:24:31 +00002329 rp->noCode = 1;
drh75897232000-05-29 14:26:00 +00002330 rp->precsym = 0;
2331 rp->index = psp->gp->nrule++;
2332 rp->nextlhs = rp->lhs->rule;
2333 rp->lhs->rule = rp;
2334 rp->next = 0;
2335 if( psp->firstrule==0 ){
2336 psp->firstrule = psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002337 }else{
drh75897232000-05-29 14:26:00 +00002338 psp->lastrule->next = rp;
2339 psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002340 }
drh75897232000-05-29 14:26:00 +00002341 psp->prevrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002342 }
drh75897232000-05-29 14:26:00 +00002343 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002344 }else if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002345 if( psp->nrhs>=MAXRHS ){
2346 ErrorMsg(psp->filename,psp->tokenlineno,
drhc4dd3fd2008-01-22 01:48:05 +00002347 "Too many symbols on RHS of rule beginning at \"%s\".",
drh75897232000-05-29 14:26:00 +00002348 x);
2349 psp->errorcnt++;
2350 psp->state = RESYNC_AFTER_RULE_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002351 }else{
drh75897232000-05-29 14:26:00 +00002352 psp->rhs[psp->nrhs] = Symbol_new(x);
2353 psp->alias[psp->nrhs] = 0;
2354 psp->nrhs++;
drhf2f105d2012-08-20 15:53:54 +00002355 }
drhfd405312005-11-06 04:06:59 +00002356 }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
2357 struct symbol *msp = psp->rhs[psp->nrhs-1];
2358 if( msp->type!=MULTITERMINAL ){
2359 struct symbol *origsp = msp;
icculus9e44cf12010-02-14 17:14:22 +00002360 msp = (struct symbol *) calloc(1,sizeof(*msp));
drhfd405312005-11-06 04:06:59 +00002361 memset(msp, 0, sizeof(*msp));
2362 msp->type = MULTITERMINAL;
2363 msp->nsubsym = 1;
icculus9e44cf12010-02-14 17:14:22 +00002364 msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*));
drhfd405312005-11-06 04:06:59 +00002365 msp->subsym[0] = origsp;
2366 msp->name = origsp->name;
2367 psp->rhs[psp->nrhs-1] = msp;
2368 }
2369 msp->nsubsym++;
icculus9e44cf12010-02-14 17:14:22 +00002370 msp->subsym = (struct symbol **) realloc(msp->subsym,
2371 sizeof(struct symbol*)*msp->nsubsym);
drhfd405312005-11-06 04:06:59 +00002372 msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
drhc56fac72015-10-29 13:48:15 +00002373 if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){
drhfd405312005-11-06 04:06:59 +00002374 ErrorMsg(psp->filename,psp->tokenlineno,
2375 "Cannot form a compound containing a non-terminal");
2376 psp->errorcnt++;
2377 }
drh75897232000-05-29 14:26:00 +00002378 }else if( x[0]=='(' && psp->nrhs>0 ){
2379 psp->state = RHS_ALIAS_1;
2380 }else{
2381 ErrorMsg(psp->filename,psp->tokenlineno,
2382 "Illegal character on RHS of rule: \"%s\".",x);
2383 psp->errorcnt++;
2384 psp->state = RESYNC_AFTER_RULE_ERROR;
2385 }
2386 break;
2387 case RHS_ALIAS_1:
drhc56fac72015-10-29 13:48:15 +00002388 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002389 psp->alias[psp->nrhs-1] = x;
2390 psp->state = RHS_ALIAS_2;
2391 }else{
2392 ErrorMsg(psp->filename,psp->tokenlineno,
2393 "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
2394 x,psp->rhs[psp->nrhs-1]->name);
2395 psp->errorcnt++;
2396 psp->state = RESYNC_AFTER_RULE_ERROR;
2397 }
2398 break;
2399 case RHS_ALIAS_2:
2400 if( x[0]==')' ){
2401 psp->state = IN_RHS;
2402 }else{
2403 ErrorMsg(psp->filename,psp->tokenlineno,
2404 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2405 psp->errorcnt++;
2406 psp->state = RESYNC_AFTER_RULE_ERROR;
2407 }
2408 break;
2409 case WAITING_FOR_DECL_KEYWORD:
drhc56fac72015-10-29 13:48:15 +00002410 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002411 psp->declkeyword = x;
2412 psp->declargslot = 0;
drh4dc8ef52008-07-01 17:13:57 +00002413 psp->decllinenoslot = 0;
drha5808f32008-04-27 22:19:44 +00002414 psp->insertLineMacro = 1;
drh75897232000-05-29 14:26:00 +00002415 psp->state = WAITING_FOR_DECL_ARG;
2416 if( strcmp(x,"name")==0 ){
2417 psp->declargslot = &(psp->gp->name);
drha5808f32008-04-27 22:19:44 +00002418 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002419 }else if( strcmp(x,"include")==0 ){
drh75897232000-05-29 14:26:00 +00002420 psp->declargslot = &(psp->gp->include);
drhf2f105d2012-08-20 15:53:54 +00002421 }else if( strcmp(x,"code")==0 ){
drh75897232000-05-29 14:26:00 +00002422 psp->declargslot = &(psp->gp->extracode);
drhf2f105d2012-08-20 15:53:54 +00002423 }else if( strcmp(x,"token_destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002424 psp->declargslot = &psp->gp->tokendest;
drhf2f105d2012-08-20 15:53:54 +00002425 }else if( strcmp(x,"default_destructor")==0 ){
drh960e8c62001-04-03 16:53:21 +00002426 psp->declargslot = &psp->gp->vardest;
drhf2f105d2012-08-20 15:53:54 +00002427 }else if( strcmp(x,"token_prefix")==0 ){
drh75897232000-05-29 14:26:00 +00002428 psp->declargslot = &psp->gp->tokenprefix;
drha5808f32008-04-27 22:19:44 +00002429 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002430 }else if( strcmp(x,"syntax_error")==0 ){
drh75897232000-05-29 14:26:00 +00002431 psp->declargslot = &(psp->gp->error);
drhf2f105d2012-08-20 15:53:54 +00002432 }else if( strcmp(x,"parse_accept")==0 ){
drh75897232000-05-29 14:26:00 +00002433 psp->declargslot = &(psp->gp->accept);
drhf2f105d2012-08-20 15:53:54 +00002434 }else if( strcmp(x,"parse_failure")==0 ){
drh75897232000-05-29 14:26:00 +00002435 psp->declargslot = &(psp->gp->failure);
drhf2f105d2012-08-20 15:53:54 +00002436 }else if( strcmp(x,"stack_overflow")==0 ){
drh75897232000-05-29 14:26:00 +00002437 psp->declargslot = &(psp->gp->overflow);
drh75897232000-05-29 14:26:00 +00002438 }else if( strcmp(x,"extra_argument")==0 ){
2439 psp->declargslot = &(psp->gp->arg);
drha5808f32008-04-27 22:19:44 +00002440 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002441 }else if( strcmp(x,"token_type")==0 ){
2442 psp->declargslot = &(psp->gp->tokentype);
drha5808f32008-04-27 22:19:44 +00002443 psp->insertLineMacro = 0;
drh960e8c62001-04-03 16:53:21 +00002444 }else if( strcmp(x,"default_type")==0 ){
2445 psp->declargslot = &(psp->gp->vartype);
drha5808f32008-04-27 22:19:44 +00002446 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002447 }else if( strcmp(x,"stack_size")==0 ){
2448 psp->declargslot = &(psp->gp->stacksize);
drha5808f32008-04-27 22:19:44 +00002449 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002450 }else if( strcmp(x,"start_symbol")==0 ){
2451 psp->declargslot = &(psp->gp->start);
drha5808f32008-04-27 22:19:44 +00002452 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002453 }else if( strcmp(x,"left")==0 ){
2454 psp->preccounter++;
2455 psp->declassoc = LEFT;
2456 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2457 }else if( strcmp(x,"right")==0 ){
2458 psp->preccounter++;
2459 psp->declassoc = RIGHT;
2460 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2461 }else if( strcmp(x,"nonassoc")==0 ){
2462 psp->preccounter++;
2463 psp->declassoc = NONE;
2464 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002465 }else if( strcmp(x,"destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002466 psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002467 }else if( strcmp(x,"type")==0 ){
drh75897232000-05-29 14:26:00 +00002468 psp->state = WAITING_FOR_DATATYPE_SYMBOL;
drh0bd1f4e2002-06-06 18:54:39 +00002469 }else if( strcmp(x,"fallback")==0 ){
2470 psp->fallback = 0;
2471 psp->state = WAITING_FOR_FALLBACK_ID;
drhe09daa92006-06-10 13:29:31 +00002472 }else if( strcmp(x,"wildcard")==0 ){
2473 psp->state = WAITING_FOR_WILDCARD_ID;
drh61f92cd2014-01-11 03:06:18 +00002474 }else if( strcmp(x,"token_class")==0 ){
2475 psp->state = WAITING_FOR_CLASS_ID;
drh75897232000-05-29 14:26:00 +00002476 }else{
2477 ErrorMsg(psp->filename,psp->tokenlineno,
2478 "Unknown declaration keyword: \"%%%s\".",x);
2479 psp->errorcnt++;
2480 psp->state = RESYNC_AFTER_DECL_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002481 }
drh75897232000-05-29 14:26:00 +00002482 }else{
2483 ErrorMsg(psp->filename,psp->tokenlineno,
2484 "Illegal declaration keyword: \"%s\".",x);
2485 psp->errorcnt++;
2486 psp->state = RESYNC_AFTER_DECL_ERROR;
2487 }
2488 break;
2489 case WAITING_FOR_DESTRUCTOR_SYMBOL:
drhc56fac72015-10-29 13:48:15 +00002490 if( !ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002491 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002492 "Symbol name missing after %%destructor keyword");
drh75897232000-05-29 14:26:00 +00002493 psp->errorcnt++;
2494 psp->state = RESYNC_AFTER_DECL_ERROR;
2495 }else{
icculusd286fa62010-03-03 17:06:32 +00002496 struct symbol *sp = Symbol_new(x);
2497 psp->declargslot = &sp->destructor;
2498 psp->decllinenoslot = &sp->destLineno;
2499 psp->insertLineMacro = 1;
2500 psp->state = WAITING_FOR_DECL_ARG;
drh75897232000-05-29 14:26:00 +00002501 }
2502 break;
2503 case WAITING_FOR_DATATYPE_SYMBOL:
drhc56fac72015-10-29 13:48:15 +00002504 if( !ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002505 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002506 "Symbol name missing after %%type keyword");
drh75897232000-05-29 14:26:00 +00002507 psp->errorcnt++;
2508 psp->state = RESYNC_AFTER_DECL_ERROR;
2509 }else{
icculus866bf1e2010-02-17 20:31:32 +00002510 struct symbol *sp = Symbol_find(x);
2511 if((sp) && (sp->datatype)){
2512 ErrorMsg(psp->filename,psp->tokenlineno,
2513 "Symbol %%type \"%s\" already defined", x);
2514 psp->errorcnt++;
2515 psp->state = RESYNC_AFTER_DECL_ERROR;
2516 }else{
2517 if (!sp){
2518 sp = Symbol_new(x);
2519 }
2520 psp->declargslot = &sp->datatype;
2521 psp->insertLineMacro = 0;
2522 psp->state = WAITING_FOR_DECL_ARG;
2523 }
drh75897232000-05-29 14:26:00 +00002524 }
2525 break;
2526 case WAITING_FOR_PRECEDENCE_SYMBOL:
2527 if( x[0]=='.' ){
2528 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002529 }else if( ISUPPER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002530 struct symbol *sp;
2531 sp = Symbol_new(x);
2532 if( sp->prec>=0 ){
2533 ErrorMsg(psp->filename,psp->tokenlineno,
2534 "Symbol \"%s\" has already be given a precedence.",x);
2535 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002536 }else{
drh75897232000-05-29 14:26:00 +00002537 sp->prec = psp->preccounter;
2538 sp->assoc = psp->declassoc;
drhf2f105d2012-08-20 15:53:54 +00002539 }
drh75897232000-05-29 14:26:00 +00002540 }else{
2541 ErrorMsg(psp->filename,psp->tokenlineno,
2542 "Can't assign a precedence to \"%s\".",x);
2543 psp->errorcnt++;
2544 }
2545 break;
2546 case WAITING_FOR_DECL_ARG:
drhc56fac72015-10-29 13:48:15 +00002547 if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){
icculus9e44cf12010-02-14 17:14:22 +00002548 const char *zOld, *zNew;
2549 char *zBuf, *z;
mistachkin2318d332015-01-12 18:02:52 +00002550 int nOld, n, nLine = 0, nNew, nBack;
drhb5bd49e2008-07-14 12:21:08 +00002551 int addLineMacro;
drha5808f32008-04-27 22:19:44 +00002552 char zLine[50];
2553 zNew = x;
2554 if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
drh87cf1372008-08-13 20:09:06 +00002555 nNew = lemonStrlen(zNew);
drha5808f32008-04-27 22:19:44 +00002556 if( *psp->declargslot ){
2557 zOld = *psp->declargslot;
2558 }else{
2559 zOld = "";
2560 }
drh87cf1372008-08-13 20:09:06 +00002561 nOld = lemonStrlen(zOld);
drha5808f32008-04-27 22:19:44 +00002562 n = nOld + nNew + 20;
shane58543932008-12-10 20:10:04 +00002563 addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro &&
drhb5bd49e2008-07-14 12:21:08 +00002564 (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
2565 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002566 for(z=psp->filename, nBack=0; *z; z++){
2567 if( *z=='\\' ) nBack++;
2568 }
drh898799f2014-01-10 23:21:00 +00002569 lemon_sprintf(zLine, "#line %d ", psp->tokenlineno);
drh87cf1372008-08-13 20:09:06 +00002570 nLine = lemonStrlen(zLine);
2571 n += nLine + lemonStrlen(psp->filename) + nBack;
drha5808f32008-04-27 22:19:44 +00002572 }
icculus9e44cf12010-02-14 17:14:22 +00002573 *psp->declargslot = (char *) realloc(*psp->declargslot, n);
2574 zBuf = *psp->declargslot + nOld;
drhb5bd49e2008-07-14 12:21:08 +00002575 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002576 if( nOld && zBuf[-1]!='\n' ){
2577 *(zBuf++) = '\n';
2578 }
2579 memcpy(zBuf, zLine, nLine);
2580 zBuf += nLine;
2581 *(zBuf++) = '"';
2582 for(z=psp->filename; *z; z++){
2583 if( *z=='\\' ){
2584 *(zBuf++) = '\\';
2585 }
2586 *(zBuf++) = *z;
2587 }
2588 *(zBuf++) = '"';
2589 *(zBuf++) = '\n';
2590 }
drh4dc8ef52008-07-01 17:13:57 +00002591 if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){
2592 psp->decllinenoslot[0] = psp->tokenlineno;
2593 }
drha5808f32008-04-27 22:19:44 +00002594 memcpy(zBuf, zNew, nNew);
2595 zBuf += nNew;
2596 *zBuf = 0;
2597 psp->state = WAITING_FOR_DECL_OR_RULE;
drh75897232000-05-29 14:26:00 +00002598 }else{
2599 ErrorMsg(psp->filename,psp->tokenlineno,
2600 "Illegal argument to %%%s: %s",psp->declkeyword,x);
2601 psp->errorcnt++;
2602 psp->state = RESYNC_AFTER_DECL_ERROR;
2603 }
2604 break;
drh0bd1f4e2002-06-06 18:54:39 +00002605 case WAITING_FOR_FALLBACK_ID:
2606 if( x[0]=='.' ){
2607 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002608 }else if( !ISUPPER(x[0]) ){
drh0bd1f4e2002-06-06 18:54:39 +00002609 ErrorMsg(psp->filename, psp->tokenlineno,
2610 "%%fallback argument \"%s\" should be a token", x);
2611 psp->errorcnt++;
2612 }else{
2613 struct symbol *sp = Symbol_new(x);
2614 if( psp->fallback==0 ){
2615 psp->fallback = sp;
2616 }else if( sp->fallback ){
2617 ErrorMsg(psp->filename, psp->tokenlineno,
2618 "More than one fallback assigned to token %s", x);
2619 psp->errorcnt++;
2620 }else{
2621 sp->fallback = psp->fallback;
2622 psp->gp->has_fallback = 1;
2623 }
2624 }
2625 break;
drhe09daa92006-06-10 13:29:31 +00002626 case WAITING_FOR_WILDCARD_ID:
2627 if( x[0]=='.' ){
2628 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002629 }else if( !ISUPPER(x[0]) ){
drhe09daa92006-06-10 13:29:31 +00002630 ErrorMsg(psp->filename, psp->tokenlineno,
2631 "%%wildcard argument \"%s\" should be a token", x);
2632 psp->errorcnt++;
2633 }else{
2634 struct symbol *sp = Symbol_new(x);
2635 if( psp->gp->wildcard==0 ){
2636 psp->gp->wildcard = sp;
2637 }else{
2638 ErrorMsg(psp->filename, psp->tokenlineno,
2639 "Extra wildcard to token: %s", x);
2640 psp->errorcnt++;
2641 }
2642 }
2643 break;
drh61f92cd2014-01-11 03:06:18 +00002644 case WAITING_FOR_CLASS_ID:
drhc56fac72015-10-29 13:48:15 +00002645 if( !ISLOWER(x[0]) ){
drh61f92cd2014-01-11 03:06:18 +00002646 ErrorMsg(psp->filename, psp->tokenlineno,
2647 "%%token_class must be followed by an identifier: ", x);
2648 psp->errorcnt++;
2649 psp->state = RESYNC_AFTER_DECL_ERROR;
2650 }else if( Symbol_find(x) ){
2651 ErrorMsg(psp->filename, psp->tokenlineno,
2652 "Symbol \"%s\" already used", x);
2653 psp->errorcnt++;
2654 psp->state = RESYNC_AFTER_DECL_ERROR;
2655 }else{
2656 psp->tkclass = Symbol_new(x);
2657 psp->tkclass->type = MULTITERMINAL;
2658 psp->state = WAITING_FOR_CLASS_TOKEN;
2659 }
2660 break;
2661 case WAITING_FOR_CLASS_TOKEN:
2662 if( x[0]=='.' ){
2663 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002664 }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){
drh61f92cd2014-01-11 03:06:18 +00002665 struct symbol *msp = psp->tkclass;
2666 msp->nsubsym++;
2667 msp->subsym = (struct symbol **) realloc(msp->subsym,
2668 sizeof(struct symbol*)*msp->nsubsym);
drhc56fac72015-10-29 13:48:15 +00002669 if( !ISUPPER(x[0]) ) x++;
drh61f92cd2014-01-11 03:06:18 +00002670 msp->subsym[msp->nsubsym-1] = Symbol_new(x);
2671 }else{
2672 ErrorMsg(psp->filename, psp->tokenlineno,
2673 "%%token_class argument \"%s\" should be a token", x);
2674 psp->errorcnt++;
2675 psp->state = RESYNC_AFTER_DECL_ERROR;
2676 }
2677 break;
drh75897232000-05-29 14:26:00 +00002678 case RESYNC_AFTER_RULE_ERROR:
2679/* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2680** break; */
2681 case RESYNC_AFTER_DECL_ERROR:
2682 if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2683 if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
2684 break;
2685 }
2686}
2687
drh34ff57b2008-07-14 12:27:51 +00002688/* Run the preprocessor over the input file text. The global variables
drh6d08b4d2004-07-20 12:45:22 +00002689** azDefine[0] through azDefine[nDefine-1] contains the names of all defined
2690** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and
2691** comments them out. Text in between is also commented out as appropriate.
2692*/
danielk1977940fac92005-01-23 22:41:37 +00002693static void preprocess_input(char *z){
drh6d08b4d2004-07-20 12:45:22 +00002694 int i, j, k, n;
2695 int exclude = 0;
rse38514a92007-09-20 11:34:17 +00002696 int start = 0;
drh6d08b4d2004-07-20 12:45:22 +00002697 int lineno = 1;
rse38514a92007-09-20 11:34:17 +00002698 int start_lineno = 1;
drh6d08b4d2004-07-20 12:45:22 +00002699 for(i=0; z[i]; i++){
2700 if( z[i]=='\n' ) lineno++;
2701 if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue;
drhc56fac72015-10-29 13:48:15 +00002702 if( strncmp(&z[i],"%endif",6)==0 && ISSPACE(z[i+6]) ){
drh6d08b4d2004-07-20 12:45:22 +00002703 if( exclude ){
2704 exclude--;
2705 if( exclude==0 ){
2706 for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
2707 }
2708 }
2709 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
drhc56fac72015-10-29 13:48:15 +00002710 }else if( (strncmp(&z[i],"%ifdef",6)==0 && ISSPACE(z[i+6]))
2711 || (strncmp(&z[i],"%ifndef",7)==0 && ISSPACE(z[i+7])) ){
drh6d08b4d2004-07-20 12:45:22 +00002712 if( exclude ){
2713 exclude++;
2714 }else{
drhc56fac72015-10-29 13:48:15 +00002715 for(j=i+7; ISSPACE(z[j]); j++){}
2716 for(n=0; z[j+n] && !ISSPACE(z[j+n]); n++){}
drh6d08b4d2004-07-20 12:45:22 +00002717 exclude = 1;
2718 for(k=0; k<nDefine; k++){
drh87cf1372008-08-13 20:09:06 +00002719 if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){
drh6d08b4d2004-07-20 12:45:22 +00002720 exclude = 0;
2721 break;
2722 }
2723 }
2724 if( z[i+3]=='n' ) exclude = !exclude;
2725 if( exclude ){
2726 start = i;
2727 start_lineno = lineno;
2728 }
2729 }
2730 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2731 }
2732 }
2733 if( exclude ){
2734 fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
2735 exit(1);
2736 }
2737}
2738
drh75897232000-05-29 14:26:00 +00002739/* In spite of its name, this function is really a scanner. It read
2740** in the entire input file (all at once) then tokenizes it. Each
2741** token is passed to the function "parseonetoken" which builds all
2742** the appropriate data structures in the global state vector "gp".
2743*/
icculus9e44cf12010-02-14 17:14:22 +00002744void Parse(struct lemon *gp)
drh75897232000-05-29 14:26:00 +00002745{
2746 struct pstate ps;
2747 FILE *fp;
2748 char *filebuf;
mistachkin2318d332015-01-12 18:02:52 +00002749 unsigned int filesize;
drh75897232000-05-29 14:26:00 +00002750 int lineno;
2751 int c;
2752 char *cp, *nextcp;
2753 int startline = 0;
2754
rse38514a92007-09-20 11:34:17 +00002755 memset(&ps, '\0', sizeof(ps));
drh75897232000-05-29 14:26:00 +00002756 ps.gp = gp;
2757 ps.filename = gp->filename;
2758 ps.errorcnt = 0;
2759 ps.state = INITIALIZE;
2760
2761 /* Begin by reading the input file */
2762 fp = fopen(ps.filename,"rb");
2763 if( fp==0 ){
2764 ErrorMsg(ps.filename,0,"Can't open this file for reading.");
2765 gp->errorcnt++;
2766 return;
2767 }
2768 fseek(fp,0,2);
2769 filesize = ftell(fp);
2770 rewind(fp);
2771 filebuf = (char *)malloc( filesize+1 );
drh03e1b1f2014-01-11 12:52:25 +00002772 if( filesize>100000000 || filebuf==0 ){
2773 ErrorMsg(ps.filename,0,"Input file too large.");
drh75897232000-05-29 14:26:00 +00002774 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002775 fclose(fp);
drh75897232000-05-29 14:26:00 +00002776 return;
2777 }
2778 if( fread(filebuf,1,filesize,fp)!=filesize ){
2779 ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
2780 filesize);
2781 free(filebuf);
2782 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002783 fclose(fp);
drh75897232000-05-29 14:26:00 +00002784 return;
2785 }
2786 fclose(fp);
2787 filebuf[filesize] = 0;
2788
drh6d08b4d2004-07-20 12:45:22 +00002789 /* Make an initial pass through the file to handle %ifdef and %ifndef */
2790 preprocess_input(filebuf);
2791
drh75897232000-05-29 14:26:00 +00002792 /* Now scan the text of the input file */
2793 lineno = 1;
2794 for(cp=filebuf; (c= *cp)!=0; ){
2795 if( c=='\n' ) lineno++; /* Keep track of the line number */
drhc56fac72015-10-29 13:48:15 +00002796 if( ISSPACE(c) ){ cp++; continue; } /* Skip all white space */
drh75897232000-05-29 14:26:00 +00002797 if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
2798 cp+=2;
2799 while( (c= *cp)!=0 && c!='\n' ) cp++;
2800 continue;
2801 }
2802 if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
2803 cp+=2;
2804 while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
2805 if( c=='\n' ) lineno++;
2806 cp++;
2807 }
2808 if( c ) cp++;
2809 continue;
2810 }
2811 ps.tokenstart = cp; /* Mark the beginning of the token */
2812 ps.tokenlineno = lineno; /* Linenumber on which token begins */
2813 if( c=='\"' ){ /* String literals */
2814 cp++;
2815 while( (c= *cp)!=0 && c!='\"' ){
2816 if( c=='\n' ) lineno++;
2817 cp++;
2818 }
2819 if( c==0 ){
2820 ErrorMsg(ps.filename,startline,
2821"String starting on this line is not terminated before the end of the file.");
2822 ps.errorcnt++;
2823 nextcp = cp;
2824 }else{
2825 nextcp = cp+1;
2826 }
2827 }else if( c=='{' ){ /* A block of C code */
2828 int level;
2829 cp++;
2830 for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
2831 if( c=='\n' ) lineno++;
2832 else if( c=='{' ) level++;
2833 else if( c=='}' ) level--;
2834 else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
2835 int prevc;
2836 cp = &cp[2];
2837 prevc = 0;
2838 while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
2839 if( c=='\n' ) lineno++;
2840 prevc = c;
2841 cp++;
drhf2f105d2012-08-20 15:53:54 +00002842 }
2843 }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
drh75897232000-05-29 14:26:00 +00002844 cp = &cp[2];
2845 while( (c= *cp)!=0 && c!='\n' ) cp++;
2846 if( c ) lineno++;
drhf2f105d2012-08-20 15:53:54 +00002847 }else if( c=='\'' || c=='\"' ){ /* String a character literals */
drh75897232000-05-29 14:26:00 +00002848 int startchar, prevc;
2849 startchar = c;
2850 prevc = 0;
2851 for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
2852 if( c=='\n' ) lineno++;
2853 if( prevc=='\\' ) prevc = 0;
2854 else prevc = c;
drhf2f105d2012-08-20 15:53:54 +00002855 }
2856 }
drh75897232000-05-29 14:26:00 +00002857 }
2858 if( c==0 ){
drh960e8c62001-04-03 16:53:21 +00002859 ErrorMsg(ps.filename,ps.tokenlineno,
drh75897232000-05-29 14:26:00 +00002860"C code starting on this line is not terminated before the end of the file.");
2861 ps.errorcnt++;
2862 nextcp = cp;
2863 }else{
2864 nextcp = cp+1;
2865 }
drhc56fac72015-10-29 13:48:15 +00002866 }else if( ISALNUM(c) ){ /* Identifiers */
2867 while( (c= *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
drh75897232000-05-29 14:26:00 +00002868 nextcp = cp;
2869 }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
2870 cp += 3;
2871 nextcp = cp;
drhc56fac72015-10-29 13:48:15 +00002872 }else if( (c=='/' || c=='|') && ISALPHA(cp[1]) ){
drhfd405312005-11-06 04:06:59 +00002873 cp += 2;
drhc56fac72015-10-29 13:48:15 +00002874 while( (c = *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
drhfd405312005-11-06 04:06:59 +00002875 nextcp = cp;
drh75897232000-05-29 14:26:00 +00002876 }else{ /* All other (one character) operators */
2877 cp++;
2878 nextcp = cp;
2879 }
2880 c = *cp;
2881 *cp = 0; /* Null terminate the token */
2882 parseonetoken(&ps); /* Parse the token */
mistachkin2318d332015-01-12 18:02:52 +00002883 *cp = (char)c; /* Restore the buffer */
drh75897232000-05-29 14:26:00 +00002884 cp = nextcp;
2885 }
2886 free(filebuf); /* Release the buffer after parsing */
2887 gp->rule = ps.firstrule;
2888 gp->errorcnt = ps.errorcnt;
2889}
2890/*************************** From the file "plink.c" *********************/
2891/*
2892** Routines processing configuration follow-set propagation links
2893** in the LEMON parser generator.
2894*/
2895static struct plink *plink_freelist = 0;
2896
2897/* Allocate a new plink */
2898struct plink *Plink_new(){
icculus9e44cf12010-02-14 17:14:22 +00002899 struct plink *newlink;
drh75897232000-05-29 14:26:00 +00002900
2901 if( plink_freelist==0 ){
2902 int i;
2903 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +00002904 plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
drh75897232000-05-29 14:26:00 +00002905 if( plink_freelist==0 ){
2906 fprintf(stderr,
2907 "Unable to allocate memory for a new follow-set propagation link.\n");
2908 exit(1);
2909 }
2910 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2911 plink_freelist[amt-1].next = 0;
2912 }
icculus9e44cf12010-02-14 17:14:22 +00002913 newlink = plink_freelist;
drh75897232000-05-29 14:26:00 +00002914 plink_freelist = plink_freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00002915 return newlink;
drh75897232000-05-29 14:26:00 +00002916}
2917
2918/* Add a plink to a plink list */
icculus9e44cf12010-02-14 17:14:22 +00002919void Plink_add(struct plink **plpp, struct config *cfp)
drh75897232000-05-29 14:26:00 +00002920{
icculus9e44cf12010-02-14 17:14:22 +00002921 struct plink *newlink;
2922 newlink = Plink_new();
2923 newlink->next = *plpp;
2924 *plpp = newlink;
2925 newlink->cfp = cfp;
drh75897232000-05-29 14:26:00 +00002926}
2927
2928/* Transfer every plink on the list "from" to the list "to" */
icculus9e44cf12010-02-14 17:14:22 +00002929void Plink_copy(struct plink **to, struct plink *from)
drh75897232000-05-29 14:26:00 +00002930{
2931 struct plink *nextpl;
2932 while( from ){
2933 nextpl = from->next;
2934 from->next = *to;
2935 *to = from;
2936 from = nextpl;
2937 }
2938}
2939
2940/* Delete every plink on the list */
icculus9e44cf12010-02-14 17:14:22 +00002941void Plink_delete(struct plink *plp)
drh75897232000-05-29 14:26:00 +00002942{
2943 struct plink *nextpl;
2944
2945 while( plp ){
2946 nextpl = plp->next;
2947 plp->next = plink_freelist;
2948 plink_freelist = plp;
2949 plp = nextpl;
2950 }
2951}
2952/*********************** From the file "report.c" **************************/
2953/*
2954** Procedures for generating reports and tables in the LEMON parser generator.
2955*/
2956
2957/* Generate a filename with the given suffix. Space to hold the
2958** name comes from malloc() and must be freed by the calling
2959** function.
2960*/
icculus9e44cf12010-02-14 17:14:22 +00002961PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
drh75897232000-05-29 14:26:00 +00002962{
2963 char *name;
2964 char *cp;
2965
icculus9e44cf12010-02-14 17:14:22 +00002966 name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
drh75897232000-05-29 14:26:00 +00002967 if( name==0 ){
2968 fprintf(stderr,"Can't allocate space for a filename.\n");
2969 exit(1);
2970 }
drh898799f2014-01-10 23:21:00 +00002971 lemon_strcpy(name,lemp->filename);
drh75897232000-05-29 14:26:00 +00002972 cp = strrchr(name,'.');
2973 if( cp ) *cp = 0;
drh898799f2014-01-10 23:21:00 +00002974 lemon_strcat(name,suffix);
drh75897232000-05-29 14:26:00 +00002975 return name;
2976}
2977
2978/* Open a file with a name based on the name of the input file,
2979** but with a different (specified) suffix, and return a pointer
2980** to the stream */
icculus9e44cf12010-02-14 17:14:22 +00002981PRIVATE FILE *file_open(
2982 struct lemon *lemp,
2983 const char *suffix,
2984 const char *mode
2985){
drh75897232000-05-29 14:26:00 +00002986 FILE *fp;
2987
2988 if( lemp->outname ) free(lemp->outname);
2989 lemp->outname = file_makename(lemp, suffix);
2990 fp = fopen(lemp->outname,mode);
2991 if( fp==0 && *mode=='w' ){
2992 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
2993 lemp->errorcnt++;
2994 return 0;
2995 }
2996 return fp;
2997}
2998
2999/* Duplicate the input file without comments and without actions
3000** on rules */
icculus9e44cf12010-02-14 17:14:22 +00003001void Reprint(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003002{
3003 struct rule *rp;
3004 struct symbol *sp;
3005 int i, j, maxlen, len, ncolumns, skip;
3006 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
3007 maxlen = 10;
3008 for(i=0; i<lemp->nsymbol; i++){
3009 sp = lemp->symbols[i];
drh87cf1372008-08-13 20:09:06 +00003010 len = lemonStrlen(sp->name);
drh75897232000-05-29 14:26:00 +00003011 if( len>maxlen ) maxlen = len;
3012 }
3013 ncolumns = 76/(maxlen+5);
3014 if( ncolumns<1 ) ncolumns = 1;
3015 skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
3016 for(i=0; i<skip; i++){
3017 printf("//");
3018 for(j=i; j<lemp->nsymbol; j+=skip){
3019 sp = lemp->symbols[j];
3020 assert( sp->index==j );
3021 printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
3022 }
3023 printf("\n");
3024 }
3025 for(rp=lemp->rule; rp; rp=rp->next){
3026 printf("%s",rp->lhs->name);
drhfd405312005-11-06 04:06:59 +00003027 /* if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
drh75897232000-05-29 14:26:00 +00003028 printf(" ::=");
3029 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +00003030 sp = rp->rhs[i];
drhfd405312005-11-06 04:06:59 +00003031 if( sp->type==MULTITERMINAL ){
drh61f92cd2014-01-11 03:06:18 +00003032 printf(" %s", sp->subsym[0]->name);
drhfd405312005-11-06 04:06:59 +00003033 for(j=1; j<sp->nsubsym; j++){
3034 printf("|%s", sp->subsym[j]->name);
3035 }
drh61f92cd2014-01-11 03:06:18 +00003036 }else{
3037 printf(" %s", sp->name);
drhfd405312005-11-06 04:06:59 +00003038 }
3039 /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
drh75897232000-05-29 14:26:00 +00003040 }
3041 printf(".");
3042 if( rp->precsym ) printf(" [%s]",rp->precsym->name);
drhfd405312005-11-06 04:06:59 +00003043 /* if( rp->code ) printf("\n %s",rp->code); */
drh75897232000-05-29 14:26:00 +00003044 printf("\n");
3045 }
3046}
3047
drh7e698e92015-09-07 14:22:24 +00003048/* Print a single rule.
3049*/
3050void RulePrint(FILE *fp, struct rule *rp, int iCursor){
drhfd405312005-11-06 04:06:59 +00003051 struct symbol *sp;
3052 int i, j;
drh75897232000-05-29 14:26:00 +00003053 fprintf(fp,"%s ::=",rp->lhs->name);
3054 for(i=0; i<=rp->nrhs; i++){
drh7e698e92015-09-07 14:22:24 +00003055 if( i==iCursor ) fprintf(fp," *");
drh75897232000-05-29 14:26:00 +00003056 if( i==rp->nrhs ) break;
drhfd405312005-11-06 04:06:59 +00003057 sp = rp->rhs[i];
drhfd405312005-11-06 04:06:59 +00003058 if( sp->type==MULTITERMINAL ){
drh61f92cd2014-01-11 03:06:18 +00003059 fprintf(fp," %s", sp->subsym[0]->name);
drhfd405312005-11-06 04:06:59 +00003060 for(j=1; j<sp->nsubsym; j++){
3061 fprintf(fp,"|%s",sp->subsym[j]->name);
3062 }
drh61f92cd2014-01-11 03:06:18 +00003063 }else{
3064 fprintf(fp," %s", sp->name);
drhfd405312005-11-06 04:06:59 +00003065 }
drh75897232000-05-29 14:26:00 +00003066 }
3067}
3068
drh7e698e92015-09-07 14:22:24 +00003069/* Print the rule for a configuration.
3070*/
3071void ConfigPrint(FILE *fp, struct config *cfp){
3072 RulePrint(fp, cfp->rp, cfp->dot);
3073}
3074
drh75897232000-05-29 14:26:00 +00003075/* #define TEST */
drhfd405312005-11-06 04:06:59 +00003076#if 0
drh75897232000-05-29 14:26:00 +00003077/* Print a set */
3078PRIVATE void SetPrint(out,set,lemp)
3079FILE *out;
3080char *set;
3081struct lemon *lemp;
3082{
3083 int i;
3084 char *spacer;
3085 spacer = "";
3086 fprintf(out,"%12s[","");
3087 for(i=0; i<lemp->nterminal; i++){
3088 if( SetFind(set,i) ){
3089 fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
3090 spacer = " ";
3091 }
3092 }
3093 fprintf(out,"]\n");
3094}
3095
3096/* Print a plink chain */
3097PRIVATE void PlinkPrint(out,plp,tag)
3098FILE *out;
3099struct plink *plp;
3100char *tag;
3101{
3102 while( plp ){
drhada354d2005-11-05 15:03:59 +00003103 fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum);
drh75897232000-05-29 14:26:00 +00003104 ConfigPrint(out,plp->cfp);
3105 fprintf(out,"\n");
3106 plp = plp->next;
3107 }
3108}
3109#endif
3110
3111/* Print an action to the given file descriptor. Return FALSE if
3112** nothing was actually printed.
3113*/
drh7e698e92015-09-07 14:22:24 +00003114int PrintAction(
3115 struct action *ap, /* The action to print */
3116 FILE *fp, /* Print the action here */
drh3bd48ab2015-09-07 18:23:37 +00003117 int indent /* Indent by this amount */
drh7e698e92015-09-07 14:22:24 +00003118){
drh75897232000-05-29 14:26:00 +00003119 int result = 1;
3120 switch( ap->type ){
drh7e698e92015-09-07 14:22:24 +00003121 case SHIFT: {
3122 struct state *stp = ap->x.stp;
drh3bd48ab2015-09-07 18:23:37 +00003123 fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum);
drh75897232000-05-29 14:26:00 +00003124 break;
drh7e698e92015-09-07 14:22:24 +00003125 }
3126 case REDUCE: {
3127 struct rule *rp = ap->x.rp;
drh4ef07702016-03-16 19:45:54 +00003128 fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->iRule);
drh3bd48ab2015-09-07 18:23:37 +00003129 RulePrint(fp, rp, -1);
3130 break;
3131 }
3132 case SHIFTREDUCE: {
3133 struct rule *rp = ap->x.rp;
drh4ef07702016-03-16 19:45:54 +00003134 fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->iRule);
drh3bd48ab2015-09-07 18:23:37 +00003135 RulePrint(fp, rp, -1);
drh75897232000-05-29 14:26:00 +00003136 break;
drh7e698e92015-09-07 14:22:24 +00003137 }
drh75897232000-05-29 14:26:00 +00003138 case ACCEPT:
3139 fprintf(fp,"%*s accept",indent,ap->sp->name);
3140 break;
3141 case ERROR:
3142 fprintf(fp,"%*s error",indent,ap->sp->name);
3143 break;
drh9892c5d2007-12-21 00:02:11 +00003144 case SRCONFLICT:
3145 case RRCONFLICT:
drh3bd48ab2015-09-07 18:23:37 +00003146 fprintf(fp,"%*s reduce %-7d ** Parsing conflict **",
drh4ef07702016-03-16 19:45:54 +00003147 indent,ap->sp->name,ap->x.rp->iRule);
drh75897232000-05-29 14:26:00 +00003148 break;
drh9892c5d2007-12-21 00:02:11 +00003149 case SSCONFLICT:
drh3bd48ab2015-09-07 18:23:37 +00003150 fprintf(fp,"%*s shift %-7d ** Parsing conflict **",
drh9892c5d2007-12-21 00:02:11 +00003151 indent,ap->sp->name,ap->x.stp->statenum);
3152 break;
drh75897232000-05-29 14:26:00 +00003153 case SH_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00003154 if( showPrecedenceConflict ){
drh3bd48ab2015-09-07 18:23:37 +00003155 fprintf(fp,"%*s shift %-7d -- dropped by precedence",
drhf5c4e0f2010-07-18 11:35:53 +00003156 indent,ap->sp->name,ap->x.stp->statenum);
3157 }else{
3158 result = 0;
3159 }
3160 break;
drh75897232000-05-29 14:26:00 +00003161 case RD_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00003162 if( showPrecedenceConflict ){
drh7e698e92015-09-07 14:22:24 +00003163 fprintf(fp,"%*s reduce %-7d -- dropped by precedence",
drh4ef07702016-03-16 19:45:54 +00003164 indent,ap->sp->name,ap->x.rp->iRule);
drhf5c4e0f2010-07-18 11:35:53 +00003165 }else{
3166 result = 0;
3167 }
3168 break;
drh75897232000-05-29 14:26:00 +00003169 case NOT_USED:
3170 result = 0;
3171 break;
3172 }
drhc173ad82016-05-23 16:15:02 +00003173 if( result && ap->spOpt ){
3174 fprintf(fp," /* because %s==%s */", ap->sp->name, ap->spOpt->name);
3175 }
drh75897232000-05-29 14:26:00 +00003176 return result;
3177}
3178
drh3bd48ab2015-09-07 18:23:37 +00003179/* Generate the "*.out" log file */
icculus9e44cf12010-02-14 17:14:22 +00003180void ReportOutput(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003181{
3182 int i;
3183 struct state *stp;
3184 struct config *cfp;
3185 struct action *ap;
3186 FILE *fp;
3187
drh2aa6ca42004-09-10 00:14:04 +00003188 fp = file_open(lemp,".out","wb");
drh75897232000-05-29 14:26:00 +00003189 if( fp==0 ) return;
drh3bd48ab2015-09-07 18:23:37 +00003190 for(i=0; i<lemp->nxstate; i++){
drh75897232000-05-29 14:26:00 +00003191 stp = lemp->sorted[i];
drhada354d2005-11-05 15:03:59 +00003192 fprintf(fp,"State %d:\n",stp->statenum);
drh75897232000-05-29 14:26:00 +00003193 if( lemp->basisflag ) cfp=stp->bp;
3194 else cfp=stp->cfp;
3195 while( cfp ){
3196 char buf[20];
3197 if( cfp->dot==cfp->rp->nrhs ){
drh4ef07702016-03-16 19:45:54 +00003198 lemon_sprintf(buf,"(%d)",cfp->rp->iRule);
drh75897232000-05-29 14:26:00 +00003199 fprintf(fp," %5s ",buf);
3200 }else{
3201 fprintf(fp," ");
3202 }
3203 ConfigPrint(fp,cfp);
3204 fprintf(fp,"\n");
drhfd405312005-11-06 04:06:59 +00003205#if 0
drh75897232000-05-29 14:26:00 +00003206 SetPrint(fp,cfp->fws,lemp);
3207 PlinkPrint(fp,cfp->fplp,"To ");
3208 PlinkPrint(fp,cfp->bplp,"From");
3209#endif
3210 if( lemp->basisflag ) cfp=cfp->bp;
3211 else cfp=cfp->next;
3212 }
3213 fprintf(fp,"\n");
3214 for(ap=stp->ap; ap; ap=ap->next){
drh3bd48ab2015-09-07 18:23:37 +00003215 if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
drh75897232000-05-29 14:26:00 +00003216 }
3217 fprintf(fp,"\n");
3218 }
drhe9278182007-07-18 18:16:29 +00003219 fprintf(fp, "----------------------------------------------------\n");
3220 fprintf(fp, "Symbols:\n");
3221 for(i=0; i<lemp->nsymbol; i++){
3222 int j;
3223 struct symbol *sp;
3224
3225 sp = lemp->symbols[i];
3226 fprintf(fp, " %3d: %s", i, sp->name);
3227 if( sp->type==NONTERMINAL ){
3228 fprintf(fp, ":");
3229 if( sp->lambda ){
3230 fprintf(fp, " <lambda>");
3231 }
3232 for(j=0; j<lemp->nterminal; j++){
3233 if( sp->firstset && SetFind(sp->firstset, j) ){
3234 fprintf(fp, " %s", lemp->symbols[j]->name);
3235 }
3236 }
3237 }
3238 fprintf(fp, "\n");
3239 }
drh75897232000-05-29 14:26:00 +00003240 fclose(fp);
3241 return;
3242}
3243
3244/* Search for the file "name" which is in the same directory as
3245** the exacutable */
icculus9e44cf12010-02-14 17:14:22 +00003246PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
drh75897232000-05-29 14:26:00 +00003247{
icculus9e44cf12010-02-14 17:14:22 +00003248 const char *pathlist;
3249 char *pathbufptr;
3250 char *pathbuf;
drh75897232000-05-29 14:26:00 +00003251 char *path,*cp;
3252 char c;
drh75897232000-05-29 14:26:00 +00003253
3254#ifdef __WIN32__
3255 cp = strrchr(argv0,'\\');
3256#else
3257 cp = strrchr(argv0,'/');
3258#endif
3259 if( cp ){
3260 c = *cp;
3261 *cp = 0;
drh87cf1372008-08-13 20:09:06 +00003262 path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
drh898799f2014-01-10 23:21:00 +00003263 if( path ) lemon_sprintf(path,"%s/%s",argv0,name);
drh75897232000-05-29 14:26:00 +00003264 *cp = c;
3265 }else{
drh75897232000-05-29 14:26:00 +00003266 pathlist = getenv("PATH");
3267 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
icculus9e44cf12010-02-14 17:14:22 +00003268 pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
drh87cf1372008-08-13 20:09:06 +00003269 path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
icculus9e44cf12010-02-14 17:14:22 +00003270 if( (pathbuf != 0) && (path!=0) ){
3271 pathbufptr = pathbuf;
drh898799f2014-01-10 23:21:00 +00003272 lemon_strcpy(pathbuf, pathlist);
icculus9e44cf12010-02-14 17:14:22 +00003273 while( *pathbuf ){
3274 cp = strchr(pathbuf,':');
3275 if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
drh75897232000-05-29 14:26:00 +00003276 c = *cp;
3277 *cp = 0;
drh898799f2014-01-10 23:21:00 +00003278 lemon_sprintf(path,"%s/%s",pathbuf,name);
drh75897232000-05-29 14:26:00 +00003279 *cp = c;
icculus9e44cf12010-02-14 17:14:22 +00003280 if( c==0 ) pathbuf[0] = 0;
3281 else pathbuf = &cp[1];
drh75897232000-05-29 14:26:00 +00003282 if( access(path,modemask)==0 ) break;
3283 }
icculus9e44cf12010-02-14 17:14:22 +00003284 free(pathbufptr);
drh75897232000-05-29 14:26:00 +00003285 }
3286 }
3287 return path;
3288}
3289
3290/* Given an action, compute the integer value for that action
3291** which is to be put in the action table of the generated machine.
3292** Return negative if no action should be generated.
3293*/
icculus9e44cf12010-02-14 17:14:22 +00003294PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
drh75897232000-05-29 14:26:00 +00003295{
3296 int act;
3297 switch( ap->type ){
drh3bd48ab2015-09-07 18:23:37 +00003298 case SHIFT: act = ap->x.stp->statenum; break;
drh4ef07702016-03-16 19:45:54 +00003299 case SHIFTREDUCE: act = ap->x.rp->iRule + lemp->nstate; break;
3300 case REDUCE: act = ap->x.rp->iRule + lemp->nstate+lemp->nrule; break;
drh3bd48ab2015-09-07 18:23:37 +00003301 case ERROR: act = lemp->nstate + lemp->nrule*2; break;
3302 case ACCEPT: act = lemp->nstate + lemp->nrule*2 + 1; break;
drh75897232000-05-29 14:26:00 +00003303 default: act = -1; break;
3304 }
3305 return act;
3306}
3307
3308#define LINESIZE 1000
3309/* The next cluster of routines are for reading the template file
3310** and writing the results to the generated parser */
3311/* The first function transfers data from "in" to "out" until
3312** a line is seen which begins with "%%". The line number is
3313** tracked.
3314**
3315** if name!=0, then any word that begin with "Parse" is changed to
3316** begin with *name instead.
3317*/
icculus9e44cf12010-02-14 17:14:22 +00003318PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
drh75897232000-05-29 14:26:00 +00003319{
3320 int i, iStart;
3321 char line[LINESIZE];
3322 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
3323 (*lineno)++;
3324 iStart = 0;
3325 if( name ){
3326 for(i=0; line[i]; i++){
3327 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
drhc56fac72015-10-29 13:48:15 +00003328 && (i==0 || !ISALPHA(line[i-1]))
drh75897232000-05-29 14:26:00 +00003329 ){
3330 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
3331 fprintf(out,"%s",name);
3332 i += 4;
3333 iStart = i+1;
3334 }
3335 }
3336 }
3337 fprintf(out,"%s",&line[iStart]);
3338 }
3339}
3340
3341/* The next function finds the template file and opens it, returning
3342** a pointer to the opened file. */
icculus9e44cf12010-02-14 17:14:22 +00003343PRIVATE FILE *tplt_open(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003344{
3345 static char templatename[] = "lempar.c";
3346 char buf[1000];
3347 FILE *in;
3348 char *tpltname;
3349 char *cp;
3350
icculus3e143bd2010-02-14 00:48:49 +00003351 /* first, see if user specified a template filename on the command line. */
3352 if (user_templatename != 0) {
3353 if( access(user_templatename,004)==-1 ){
3354 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3355 user_templatename);
3356 lemp->errorcnt++;
3357 return 0;
3358 }
3359 in = fopen(user_templatename,"rb");
3360 if( in==0 ){
drh25473362015-09-04 18:03:45 +00003361 fprintf(stderr,"Can't open the template file \"%s\".\n",
3362 user_templatename);
icculus3e143bd2010-02-14 00:48:49 +00003363 lemp->errorcnt++;
3364 return 0;
3365 }
3366 return in;
3367 }
3368
drh75897232000-05-29 14:26:00 +00003369 cp = strrchr(lemp->filename,'.');
3370 if( cp ){
drh898799f2014-01-10 23:21:00 +00003371 lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
drh75897232000-05-29 14:26:00 +00003372 }else{
drh898799f2014-01-10 23:21:00 +00003373 lemon_sprintf(buf,"%s.lt",lemp->filename);
drh75897232000-05-29 14:26:00 +00003374 }
3375 if( access(buf,004)==0 ){
3376 tpltname = buf;
drh960e8c62001-04-03 16:53:21 +00003377 }else if( access(templatename,004)==0 ){
3378 tpltname = templatename;
drh75897232000-05-29 14:26:00 +00003379 }else{
3380 tpltname = pathsearch(lemp->argv0,templatename,0);
3381 }
3382 if( tpltname==0 ){
3383 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3384 templatename);
3385 lemp->errorcnt++;
3386 return 0;
3387 }
drh2aa6ca42004-09-10 00:14:04 +00003388 in = fopen(tpltname,"rb");
drh75897232000-05-29 14:26:00 +00003389 if( in==0 ){
3390 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
3391 lemp->errorcnt++;
3392 return 0;
3393 }
3394 return in;
3395}
3396
drhaf805ca2004-09-07 11:28:25 +00003397/* Print a #line directive line to the output file. */
icculus9e44cf12010-02-14 17:14:22 +00003398PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
drhaf805ca2004-09-07 11:28:25 +00003399{
3400 fprintf(out,"#line %d \"",lineno);
3401 while( *filename ){
3402 if( *filename == '\\' ) putc('\\',out);
3403 putc(*filename,out);
3404 filename++;
3405 }
3406 fprintf(out,"\"\n");
3407}
3408
drh75897232000-05-29 14:26:00 +00003409/* Print a string to the file and keep the linenumber up to date */
icculus9e44cf12010-02-14 17:14:22 +00003410PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
drh75897232000-05-29 14:26:00 +00003411{
3412 if( str==0 ) return;
drh75897232000-05-29 14:26:00 +00003413 while( *str ){
drh75897232000-05-29 14:26:00 +00003414 putc(*str,out);
shane58543932008-12-10 20:10:04 +00003415 if( *str=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003416 str++;
3417 }
drh9db55df2004-09-09 14:01:21 +00003418 if( str[-1]!='\n' ){
3419 putc('\n',out);
3420 (*lineno)++;
3421 }
shane58543932008-12-10 20:10:04 +00003422 if (!lemp->nolinenosflag) {
3423 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3424 }
drh75897232000-05-29 14:26:00 +00003425 return;
3426}
3427
3428/*
3429** The following routine emits code for the destructor for the
3430** symbol sp
3431*/
icculus9e44cf12010-02-14 17:14:22 +00003432void emit_destructor_code(
3433 FILE *out,
3434 struct symbol *sp,
3435 struct lemon *lemp,
3436 int *lineno
3437){
drhcc83b6e2004-04-23 23:38:42 +00003438 char *cp = 0;
drh75897232000-05-29 14:26:00 +00003439
drh75897232000-05-29 14:26:00 +00003440 if( sp->type==TERMINAL ){
3441 cp = lemp->tokendest;
3442 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003443 fprintf(out,"{\n"); (*lineno)++;
drh960e8c62001-04-03 16:53:21 +00003444 }else if( sp->destructor ){
drh75897232000-05-29 14:26:00 +00003445 cp = sp->destructor;
drha5808f32008-04-27 22:19:44 +00003446 fprintf(out,"{\n"); (*lineno)++;
drh25473362015-09-04 18:03:45 +00003447 if( !lemp->nolinenosflag ){
3448 (*lineno)++;
3449 tplt_linedir(out,sp->destLineno,lemp->filename);
3450 }
drh960e8c62001-04-03 16:53:21 +00003451 }else if( lemp->vardest ){
3452 cp = lemp->vardest;
3453 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003454 fprintf(out,"{\n"); (*lineno)++;
drhcc83b6e2004-04-23 23:38:42 +00003455 }else{
3456 assert( 0 ); /* Cannot happen */
drh75897232000-05-29 14:26:00 +00003457 }
3458 for(; *cp; cp++){
3459 if( *cp=='$' && cp[1]=='$' ){
3460 fprintf(out,"(yypminor->yy%d)",sp->dtnum);
3461 cp++;
3462 continue;
3463 }
shane58543932008-12-10 20:10:04 +00003464 if( *cp=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003465 fputc(*cp,out);
3466 }
shane58543932008-12-10 20:10:04 +00003467 fprintf(out,"\n"); (*lineno)++;
3468 if (!lemp->nolinenosflag) {
3469 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3470 }
3471 fprintf(out,"}\n"); (*lineno)++;
drh75897232000-05-29 14:26:00 +00003472 return;
3473}
3474
3475/*
drh960e8c62001-04-03 16:53:21 +00003476** Return TRUE (non-zero) if the given symbol has a destructor.
drh75897232000-05-29 14:26:00 +00003477*/
icculus9e44cf12010-02-14 17:14:22 +00003478int has_destructor(struct symbol *sp, struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003479{
3480 int ret;
3481 if( sp->type==TERMINAL ){
3482 ret = lemp->tokendest!=0;
3483 }else{
drh960e8c62001-04-03 16:53:21 +00003484 ret = lemp->vardest!=0 || sp->destructor!=0;
drh75897232000-05-29 14:26:00 +00003485 }
3486 return ret;
3487}
3488
drh0bb132b2004-07-20 14:06:51 +00003489/*
3490** Append text to a dynamically allocated string. If zText is 0 then
3491** reset the string to be empty again. Always return the complete text
3492** of the string (which is overwritten with each call).
drh7ac25c72004-08-19 15:12:26 +00003493**
3494** n bytes of zText are stored. If n==0 then all of zText up to the first
3495** \000 terminator is stored. zText can contain up to two instances of
3496** %d. The values of p1 and p2 are written into the first and second
3497** %d.
3498**
3499** If n==-1, then the previous character is overwritten.
drh0bb132b2004-07-20 14:06:51 +00003500*/
icculus9e44cf12010-02-14 17:14:22 +00003501PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
3502 static char empty[1] = { 0 };
drh0bb132b2004-07-20 14:06:51 +00003503 static char *z = 0;
3504 static int alloced = 0;
3505 static int used = 0;
drhaf805ca2004-09-07 11:28:25 +00003506 int c;
drh0bb132b2004-07-20 14:06:51 +00003507 char zInt[40];
drh0bb132b2004-07-20 14:06:51 +00003508 if( zText==0 ){
drh4dd0d3f2016-02-17 01:18:33 +00003509 if( used==0 && z!=0 ) z[0] = 0;
drh0bb132b2004-07-20 14:06:51 +00003510 used = 0;
3511 return z;
3512 }
drh7ac25c72004-08-19 15:12:26 +00003513 if( n<=0 ){
3514 if( n<0 ){
3515 used += n;
3516 assert( used>=0 );
3517 }
drh87cf1372008-08-13 20:09:06 +00003518 n = lemonStrlen(zText);
drh7ac25c72004-08-19 15:12:26 +00003519 }
drhdf609712010-11-23 20:55:27 +00003520 if( (int) (n+sizeof(zInt)*2+used) >= alloced ){
drh0bb132b2004-07-20 14:06:51 +00003521 alloced = n + sizeof(zInt)*2 + used + 200;
icculus9e44cf12010-02-14 17:14:22 +00003522 z = (char *) realloc(z, alloced);
drh0bb132b2004-07-20 14:06:51 +00003523 }
icculus9e44cf12010-02-14 17:14:22 +00003524 if( z==0 ) return empty;
drh0bb132b2004-07-20 14:06:51 +00003525 while( n-- > 0 ){
3526 c = *(zText++);
drh50489622006-10-13 12:25:29 +00003527 if( c=='%' && n>0 && zText[0]=='d' ){
drh898799f2014-01-10 23:21:00 +00003528 lemon_sprintf(zInt, "%d", p1);
drh0bb132b2004-07-20 14:06:51 +00003529 p1 = p2;
drh898799f2014-01-10 23:21:00 +00003530 lemon_strcpy(&z[used], zInt);
drh87cf1372008-08-13 20:09:06 +00003531 used += lemonStrlen(&z[used]);
drh0bb132b2004-07-20 14:06:51 +00003532 zText++;
3533 n--;
3534 }else{
mistachkin2318d332015-01-12 18:02:52 +00003535 z[used++] = (char)c;
drh0bb132b2004-07-20 14:06:51 +00003536 }
3537 }
3538 z[used] = 0;
3539 return z;
3540}
3541
3542/*
drh711c9812016-05-23 14:24:31 +00003543** Write and transform the rp->code string so that symbols are expanded.
3544** Populate the rp->codePrefix and rp->codeSuffix strings, as appropriate.
drhdabd04c2016-02-17 01:46:19 +00003545**
3546** Return 1 if the expanded code requires that "yylhsminor" local variable
3547** to be defined.
drh0bb132b2004-07-20 14:06:51 +00003548*/
drhdabd04c2016-02-17 01:46:19 +00003549PRIVATE int translate_code(struct lemon *lemp, struct rule *rp){
drh0bb132b2004-07-20 14:06:51 +00003550 char *cp, *xp;
3551 int i;
drhcf82f0d2016-02-17 04:33:10 +00003552 int rc = 0; /* True if yylhsminor is used */
drh43303de2016-02-17 12:34:03 +00003553 int dontUseRhs0 = 0; /* If true, use of left-most RHS label is illegal */
drhcf82f0d2016-02-17 04:33:10 +00003554 const char *zSkip = 0; /* The zOvwrt comment within rp->code, or NULL */
3555 char lhsused = 0; /* True if the LHS element has been used */
3556 char lhsdirect; /* True if LHS writes directly into stack */
3557 char used[MAXRHS]; /* True for each RHS element which is used */
3558 char zLhs[50]; /* Convert the LHS symbol into this string */
3559 char zOvwrt[900]; /* Comment that to allow LHS to overwrite RHS */
drh0bb132b2004-07-20 14:06:51 +00003560
3561 for(i=0; i<rp->nrhs; i++) used[i] = 0;
3562 lhsused = 0;
3563
drh19c9e562007-03-29 20:13:53 +00003564 if( rp->code==0 ){
icculus9e44cf12010-02-14 17:14:22 +00003565 static char newlinestr[2] = { '\n', '\0' };
3566 rp->code = newlinestr;
drh19c9e562007-03-29 20:13:53 +00003567 rp->line = rp->ruleline;
drh711c9812016-05-23 14:24:31 +00003568 rp->noCode = 1;
3569 }else{
3570 rp->noCode = 0;
drh19c9e562007-03-29 20:13:53 +00003571 }
3572
drh4dd0d3f2016-02-17 01:18:33 +00003573
drh2e55b042016-04-30 17:19:30 +00003574 if( rp->nrhs==0 ){
drh4dd0d3f2016-02-17 01:18:33 +00003575 /* If there are no RHS symbols, then writing directly to the LHS is ok */
3576 lhsdirect = 1;
3577 }else if( rp->rhsalias[0]==0 ){
drh2e55b042016-04-30 17:19:30 +00003578 /* The left-most RHS symbol has no value. LHS direct is ok. But
drh4dd0d3f2016-02-17 01:18:33 +00003579 ** we have to call the distructor on the RHS symbol first. */
3580 lhsdirect = 1;
3581 if( has_destructor(rp->rhs[0],lemp) ){
3582 append_str(0,0,0,0);
3583 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
3584 rp->rhs[0]->index,1-rp->nrhs);
3585 rp->codePrefix = Strsafe(append_str(0,0,0,0));
drh711c9812016-05-23 14:24:31 +00003586 rp->noCode = 0;
drh4dd0d3f2016-02-17 01:18:33 +00003587 }
drh2e55b042016-04-30 17:19:30 +00003588 }else if( rp->lhsalias==0 ){
3589 /* There is no LHS value symbol. */
3590 lhsdirect = 1;
drh4dd0d3f2016-02-17 01:18:33 +00003591 }else if( strcmp(rp->lhsalias,rp->rhsalias[0])==0 ){
3592 /* The LHS symbol and the left-most RHS symbol are the same, so
3593 ** direct writing is allowed */
3594 lhsdirect = 1;
3595 lhsused = 1;
3596 used[0] = 1;
3597 if( rp->lhs->dtnum!=rp->rhs[0]->dtnum ){
3598 ErrorMsg(lemp->filename,rp->ruleline,
3599 "%s(%s) and %s(%s) share the same label but have "
3600 "different datatypes.",
3601 rp->lhs->name, rp->lhsalias, rp->rhs[0]->name, rp->rhsalias[0]);
3602 lemp->errorcnt++;
3603 }
3604 }else{
drhcf82f0d2016-02-17 04:33:10 +00003605 lemon_sprintf(zOvwrt, "/*%s-overwrites-%s*/",
3606 rp->lhsalias, rp->rhsalias[0]);
3607 zSkip = strstr(rp->code, zOvwrt);
3608 if( zSkip!=0 ){
3609 /* The code contains a special comment that indicates that it is safe
3610 ** for the LHS label to overwrite left-most RHS label. */
3611 lhsdirect = 1;
3612 }else{
3613 lhsdirect = 0;
3614 }
drh4dd0d3f2016-02-17 01:18:33 +00003615 }
3616 if( lhsdirect ){
3617 sprintf(zLhs, "yymsp[%d].minor.yy%d",1-rp->nrhs,rp->lhs->dtnum);
3618 }else{
drhdabd04c2016-02-17 01:46:19 +00003619 rc = 1;
drh4dd0d3f2016-02-17 01:18:33 +00003620 sprintf(zLhs, "yylhsminor.yy%d",rp->lhs->dtnum);
3621 }
3622
drh0bb132b2004-07-20 14:06:51 +00003623 append_str(0,0,0,0);
icculus9e44cf12010-02-14 17:14:22 +00003624
3625 /* This const cast is wrong but harmless, if we're careful. */
3626 for(cp=(char *)rp->code; *cp; cp++){
drhcf82f0d2016-02-17 04:33:10 +00003627 if( cp==zSkip ){
3628 append_str(zOvwrt,0,0,0);
3629 cp += lemonStrlen(zOvwrt)-1;
drh43303de2016-02-17 12:34:03 +00003630 dontUseRhs0 = 1;
drhcf82f0d2016-02-17 04:33:10 +00003631 continue;
3632 }
drhc56fac72015-10-29 13:48:15 +00003633 if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){
drh0bb132b2004-07-20 14:06:51 +00003634 char saved;
drhc56fac72015-10-29 13:48:15 +00003635 for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++);
drh0bb132b2004-07-20 14:06:51 +00003636 saved = *xp;
3637 *xp = 0;
3638 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
drh4dd0d3f2016-02-17 01:18:33 +00003639 append_str(zLhs,0,0,0);
drh0bb132b2004-07-20 14:06:51 +00003640 cp = xp;
3641 lhsused = 1;
3642 }else{
3643 for(i=0; i<rp->nrhs; i++){
3644 if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
drh43303de2016-02-17 12:34:03 +00003645 if( i==0 && dontUseRhs0 ){
3646 ErrorMsg(lemp->filename,rp->ruleline,
3647 "Label %s used after '%s'.",
3648 rp->rhsalias[0], zOvwrt);
3649 lemp->errorcnt++;
3650 }else if( cp!=rp->code && cp[-1]=='@' ){
drh7ac25c72004-08-19 15:12:26 +00003651 /* If the argument is of the form @X then substituted
3652 ** the token number of X, not the value of X */
3653 append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0);
3654 }else{
drhfd405312005-11-06 04:06:59 +00003655 struct symbol *sp = rp->rhs[i];
3656 int dtnum;
3657 if( sp->type==MULTITERMINAL ){
3658 dtnum = sp->subsym[0]->dtnum;
3659 }else{
3660 dtnum = sp->dtnum;
3661 }
3662 append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum);
drh7ac25c72004-08-19 15:12:26 +00003663 }
drh0bb132b2004-07-20 14:06:51 +00003664 cp = xp;
3665 used[i] = 1;
3666 break;
3667 }
3668 }
3669 }
3670 *xp = saved;
3671 }
3672 append_str(cp, 1, 0, 0);
3673 } /* End loop */
3674
drh4dd0d3f2016-02-17 01:18:33 +00003675 /* Main code generation completed */
3676 cp = append_str(0,0,0,0);
3677 if( cp && cp[0] ) rp->code = Strsafe(cp);
3678 append_str(0,0,0,0);
3679
drh0bb132b2004-07-20 14:06:51 +00003680 /* Check to make sure the LHS has been used */
3681 if( rp->lhsalias && !lhsused ){
3682 ErrorMsg(lemp->filename,rp->ruleline,
3683 "Label \"%s\" for \"%s(%s)\" is never used.",
3684 rp->lhsalias,rp->lhs->name,rp->lhsalias);
3685 lemp->errorcnt++;
3686 }
3687
drh4dd0d3f2016-02-17 01:18:33 +00003688 /* Generate destructor code for RHS minor values which are not referenced.
3689 ** Generate error messages for unused labels and duplicate labels.
3690 */
drh0bb132b2004-07-20 14:06:51 +00003691 for(i=0; i<rp->nrhs; i++){
drh4dd0d3f2016-02-17 01:18:33 +00003692 if( rp->rhsalias[i] ){
3693 if( i>0 ){
3694 int j;
3695 if( rp->lhsalias && strcmp(rp->lhsalias,rp->rhsalias[i])==0 ){
3696 ErrorMsg(lemp->filename,rp->ruleline,
3697 "%s(%s) has the same label as the LHS but is not the left-most "
3698 "symbol on the RHS.",
3699 rp->rhs[i]->name, rp->rhsalias);
3700 lemp->errorcnt++;
3701 }
3702 for(j=0; j<i; j++){
3703 if( rp->rhsalias[j] && strcmp(rp->rhsalias[j],rp->rhsalias[i])==0 ){
3704 ErrorMsg(lemp->filename,rp->ruleline,
3705 "Label %s used for multiple symbols on the RHS of a rule.",
3706 rp->rhsalias[i]);
3707 lemp->errorcnt++;
3708 break;
3709 }
3710 }
drh0bb132b2004-07-20 14:06:51 +00003711 }
drh4dd0d3f2016-02-17 01:18:33 +00003712 if( !used[i] ){
3713 ErrorMsg(lemp->filename,rp->ruleline,
3714 "Label %s for \"%s(%s)\" is never used.",
3715 rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
3716 lemp->errorcnt++;
3717 }
3718 }else if( i>0 && has_destructor(rp->rhs[i],lemp) ){
3719 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
3720 rp->rhs[i]->index,i-rp->nrhs+1);
drh0bb132b2004-07-20 14:06:51 +00003721 }
3722 }
drh4dd0d3f2016-02-17 01:18:33 +00003723
3724 /* If unable to write LHS values directly into the stack, write the
3725 ** saved LHS value now. */
3726 if( lhsdirect==0 ){
3727 append_str(" yymsp[%d].minor.yy%d = ", 0, 1-rp->nrhs, rp->lhs->dtnum);
3728 append_str(zLhs, 0, 0, 0);
3729 append_str(";\n", 0, 0, 0);
drh61e339a2007-01-16 03:09:02 +00003730 }
drh4dd0d3f2016-02-17 01:18:33 +00003731
3732 /* Suffix code generation complete */
3733 cp = append_str(0,0,0,0);
drh711c9812016-05-23 14:24:31 +00003734 if( cp && cp[0] ){
3735 rp->codeSuffix = Strsafe(cp);
3736 rp->noCode = 0;
3737 }
drhdabd04c2016-02-17 01:46:19 +00003738
3739 return rc;
drh0bb132b2004-07-20 14:06:51 +00003740}
3741
drh75897232000-05-29 14:26:00 +00003742/*
3743** Generate code which executes when the rule "rp" is reduced. Write
3744** the code to "out". Make sure lineno stays up-to-date.
3745*/
icculus9e44cf12010-02-14 17:14:22 +00003746PRIVATE void emit_code(
3747 FILE *out,
3748 struct rule *rp,
3749 struct lemon *lemp,
3750 int *lineno
3751){
3752 const char *cp;
drh75897232000-05-29 14:26:00 +00003753
drh4dd0d3f2016-02-17 01:18:33 +00003754 /* Setup code prior to the #line directive */
3755 if( rp->codePrefix && rp->codePrefix[0] ){
3756 fprintf(out, "{%s", rp->codePrefix);
3757 for(cp=rp->codePrefix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
3758 }
3759
drh75897232000-05-29 14:26:00 +00003760 /* Generate code to do the reduce action */
3761 if( rp->code ){
drh25473362015-09-04 18:03:45 +00003762 if( !lemp->nolinenosflag ){
3763 (*lineno)++;
3764 tplt_linedir(out,rp->line,lemp->filename);
3765 }
drhaf805ca2004-09-07 11:28:25 +00003766 fprintf(out,"{%s",rp->code);
drh4dd0d3f2016-02-17 01:18:33 +00003767 for(cp=rp->code; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
shane58543932008-12-10 20:10:04 +00003768 fprintf(out,"}\n"); (*lineno)++;
drh25473362015-09-04 18:03:45 +00003769 if( !lemp->nolinenosflag ){
3770 (*lineno)++;
3771 tplt_linedir(out,*lineno,lemp->outname);
3772 }
drh4dd0d3f2016-02-17 01:18:33 +00003773 }
3774
3775 /* Generate breakdown code that occurs after the #line directive */
3776 if( rp->codeSuffix && rp->codeSuffix[0] ){
3777 fprintf(out, "%s", rp->codeSuffix);
3778 for(cp=rp->codeSuffix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
3779 }
3780
3781 if( rp->codePrefix ){
3782 fprintf(out, "}\n"); (*lineno)++;
3783 }
drh75897232000-05-29 14:26:00 +00003784
drh75897232000-05-29 14:26:00 +00003785 return;
3786}
3787
3788/*
3789** Print the definition of the union used for the parser's data stack.
3790** This union contains fields for every possible data type for tokens
3791** and nonterminals. In the process of computing and printing this
3792** union, also set the ".dtnum" field of every terminal and nonterminal
3793** symbol.
3794*/
icculus9e44cf12010-02-14 17:14:22 +00003795void print_stack_union(
3796 FILE *out, /* The output stream */
3797 struct lemon *lemp, /* The main info structure for this parser */
3798 int *plineno, /* Pointer to the line number */
3799 int mhflag /* True if generating makeheaders output */
3800){
drh75897232000-05-29 14:26:00 +00003801 int lineno = *plineno; /* The line number of the output */
3802 char **types; /* A hash table of datatypes */
3803 int arraysize; /* Size of the "types" array */
3804 int maxdtlength; /* Maximum length of any ".datatype" field. */
3805 char *stddt; /* Standardized name for a datatype */
3806 int i,j; /* Loop counters */
drh01f75f22013-10-02 20:46:30 +00003807 unsigned hash; /* For hashing the name of a type */
icculus9e44cf12010-02-14 17:14:22 +00003808 const char *name; /* Name of the parser */
drh75897232000-05-29 14:26:00 +00003809
3810 /* Allocate and initialize types[] and allocate stddt[] */
3811 arraysize = lemp->nsymbol * 2;
drh9892c5d2007-12-21 00:02:11 +00003812 types = (char**)calloc( arraysize, sizeof(char*) );
drh070d4222011-06-02 15:48:51 +00003813 if( types==0 ){
3814 fprintf(stderr,"Out of memory.\n");
3815 exit(1);
3816 }
drh75897232000-05-29 14:26:00 +00003817 for(i=0; i<arraysize; i++) types[i] = 0;
3818 maxdtlength = 0;
drh960e8c62001-04-03 16:53:21 +00003819 if( lemp->vartype ){
drh87cf1372008-08-13 20:09:06 +00003820 maxdtlength = lemonStrlen(lemp->vartype);
drh960e8c62001-04-03 16:53:21 +00003821 }
drh75897232000-05-29 14:26:00 +00003822 for(i=0; i<lemp->nsymbol; i++){
3823 int len;
3824 struct symbol *sp = lemp->symbols[i];
3825 if( sp->datatype==0 ) continue;
drh87cf1372008-08-13 20:09:06 +00003826 len = lemonStrlen(sp->datatype);
drh75897232000-05-29 14:26:00 +00003827 if( len>maxdtlength ) maxdtlength = len;
3828 }
3829 stddt = (char*)malloc( maxdtlength*2 + 1 );
drh070d4222011-06-02 15:48:51 +00003830 if( stddt==0 ){
drh75897232000-05-29 14:26:00 +00003831 fprintf(stderr,"Out of memory.\n");
3832 exit(1);
3833 }
3834
3835 /* Build a hash table of datatypes. The ".dtnum" field of each symbol
3836 ** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
drh960e8c62001-04-03 16:53:21 +00003837 ** used for terminal symbols. If there is no %default_type defined then
3838 ** 0 is also used as the .dtnum value for nonterminals which do not specify
3839 ** a datatype using the %type directive.
3840 */
drh75897232000-05-29 14:26:00 +00003841 for(i=0; i<lemp->nsymbol; i++){
3842 struct symbol *sp = lemp->symbols[i];
3843 char *cp;
3844 if( sp==lemp->errsym ){
3845 sp->dtnum = arraysize+1;
3846 continue;
3847 }
drh960e8c62001-04-03 16:53:21 +00003848 if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
drh75897232000-05-29 14:26:00 +00003849 sp->dtnum = 0;
3850 continue;
3851 }
3852 cp = sp->datatype;
drh960e8c62001-04-03 16:53:21 +00003853 if( cp==0 ) cp = lemp->vartype;
drh75897232000-05-29 14:26:00 +00003854 j = 0;
drhc56fac72015-10-29 13:48:15 +00003855 while( ISSPACE(*cp) ) cp++;
drh75897232000-05-29 14:26:00 +00003856 while( *cp ) stddt[j++] = *cp++;
drhc56fac72015-10-29 13:48:15 +00003857 while( j>0 && ISSPACE(stddt[j-1]) ) j--;
drh75897232000-05-29 14:26:00 +00003858 stddt[j] = 0;
drh02368c92009-04-05 15:18:02 +00003859 if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){
drh32c4d742008-07-01 16:34:49 +00003860 sp->dtnum = 0;
3861 continue;
3862 }
drh75897232000-05-29 14:26:00 +00003863 hash = 0;
3864 for(j=0; stddt[j]; j++){
3865 hash = hash*53 + stddt[j];
3866 }
drh3b2129c2003-05-13 00:34:21 +00003867 hash = (hash & 0x7fffffff)%arraysize;
drh75897232000-05-29 14:26:00 +00003868 while( types[hash] ){
3869 if( strcmp(types[hash],stddt)==0 ){
3870 sp->dtnum = hash + 1;
3871 break;
3872 }
3873 hash++;
drh2b51f212013-10-11 23:01:02 +00003874 if( hash>=(unsigned)arraysize ) hash = 0;
drh75897232000-05-29 14:26:00 +00003875 }
3876 if( types[hash]==0 ){
3877 sp->dtnum = hash + 1;
drh87cf1372008-08-13 20:09:06 +00003878 types[hash] = (char*)malloc( lemonStrlen(stddt)+1 );
drh75897232000-05-29 14:26:00 +00003879 if( types[hash]==0 ){
3880 fprintf(stderr,"Out of memory.\n");
3881 exit(1);
3882 }
drh898799f2014-01-10 23:21:00 +00003883 lemon_strcpy(types[hash],stddt);
drh75897232000-05-29 14:26:00 +00003884 }
3885 }
3886
3887 /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
3888 name = lemp->name ? lemp->name : "Parse";
3889 lineno = *plineno;
3890 if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
3891 fprintf(out,"#define %sTOKENTYPE %s\n",name,
3892 lemp->tokentype?lemp->tokentype:"void*"); lineno++;
3893 if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
3894 fprintf(out,"typedef union {\n"); lineno++;
drh15b024c2008-12-11 02:20:43 +00003895 fprintf(out," int yyinit;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003896 fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
3897 for(i=0; i<arraysize; i++){
3898 if( types[i]==0 ) continue;
3899 fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
3900 free(types[i]);
3901 }
drhc4dd3fd2008-01-22 01:48:05 +00003902 if( lemp->errsym->useCnt ){
3903 fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
3904 }
drh75897232000-05-29 14:26:00 +00003905 free(stddt);
3906 free(types);
3907 fprintf(out,"} YYMINORTYPE;\n"); lineno++;
3908 *plineno = lineno;
3909}
3910
drhb29b0a52002-02-23 19:39:46 +00003911/*
3912** Return the name of a C datatype able to represent values between
drhc75e0162015-09-07 02:23:02 +00003913** lwr and upr, inclusive. If pnByte!=NULL then also write the sizeof
3914** for that type (1, 2, or 4) into *pnByte.
drhb29b0a52002-02-23 19:39:46 +00003915*/
drhc75e0162015-09-07 02:23:02 +00003916static const char *minimum_size_type(int lwr, int upr, int *pnByte){
3917 const char *zType = "int";
3918 int nByte = 4;
drh8b582012003-10-21 13:16:03 +00003919 if( lwr>=0 ){
3920 if( upr<=255 ){
drhc75e0162015-09-07 02:23:02 +00003921 zType = "unsigned char";
3922 nByte = 1;
drh8b582012003-10-21 13:16:03 +00003923 }else if( upr<65535 ){
drhc75e0162015-09-07 02:23:02 +00003924 zType = "unsigned short int";
3925 nByte = 2;
drh8b582012003-10-21 13:16:03 +00003926 }else{
drhc75e0162015-09-07 02:23:02 +00003927 zType = "unsigned int";
3928 nByte = 4;
drh8b582012003-10-21 13:16:03 +00003929 }
3930 }else if( lwr>=-127 && upr<=127 ){
drhc75e0162015-09-07 02:23:02 +00003931 zType = "signed char";
3932 nByte = 1;
drh8b582012003-10-21 13:16:03 +00003933 }else if( lwr>=-32767 && upr<32767 ){
drhc75e0162015-09-07 02:23:02 +00003934 zType = "short";
3935 nByte = 2;
drhb29b0a52002-02-23 19:39:46 +00003936 }
drhc75e0162015-09-07 02:23:02 +00003937 if( pnByte ) *pnByte = nByte;
3938 return zType;
drhb29b0a52002-02-23 19:39:46 +00003939}
3940
drhfdbf9282003-10-21 16:34:41 +00003941/*
3942** Each state contains a set of token transaction and a set of
3943** nonterminal transactions. Each of these sets makes an instance
3944** of the following structure. An array of these structures is used
3945** to order the creation of entries in the yy_action[] table.
3946*/
3947struct axset {
3948 struct state *stp; /* A pointer to a state */
3949 int isTkn; /* True to use tokens. False for non-terminals */
3950 int nAction; /* Number of actions */
drhe594bc32009-11-03 13:02:25 +00003951 int iOrder; /* Original order of action sets */
drhfdbf9282003-10-21 16:34:41 +00003952};
3953
3954/*
3955** Compare to axset structures for sorting purposes
3956*/
3957static int axset_compare(const void *a, const void *b){
3958 struct axset *p1 = (struct axset*)a;
3959 struct axset *p2 = (struct axset*)b;
drhe594bc32009-11-03 13:02:25 +00003960 int c;
3961 c = p2->nAction - p1->nAction;
3962 if( c==0 ){
drh337cd0d2015-09-07 23:40:42 +00003963 c = p1->iOrder - p2->iOrder;
drhe594bc32009-11-03 13:02:25 +00003964 }
3965 assert( c!=0 || p1==p2 );
3966 return c;
drhfdbf9282003-10-21 16:34:41 +00003967}
3968
drhc4dd3fd2008-01-22 01:48:05 +00003969/*
3970** Write text on "out" that describes the rule "rp".
3971*/
3972static void writeRuleText(FILE *out, struct rule *rp){
3973 int j;
3974 fprintf(out,"%s ::=", rp->lhs->name);
3975 for(j=0; j<rp->nrhs; j++){
3976 struct symbol *sp = rp->rhs[j];
drh61f92cd2014-01-11 03:06:18 +00003977 if( sp->type!=MULTITERMINAL ){
3978 fprintf(out," %s", sp->name);
3979 }else{
drhc4dd3fd2008-01-22 01:48:05 +00003980 int k;
drh61f92cd2014-01-11 03:06:18 +00003981 fprintf(out," %s", sp->subsym[0]->name);
drhc4dd3fd2008-01-22 01:48:05 +00003982 for(k=1; k<sp->nsubsym; k++){
3983 fprintf(out,"|%s",sp->subsym[k]->name);
3984 }
3985 }
3986 }
3987}
3988
3989
drh75897232000-05-29 14:26:00 +00003990/* Generate C source code for the parser */
icculus9e44cf12010-02-14 17:14:22 +00003991void ReportTable(
3992 struct lemon *lemp,
3993 int mhflag /* Output in makeheaders format if true */
3994){
drh75897232000-05-29 14:26:00 +00003995 FILE *out, *in;
3996 char line[LINESIZE];
3997 int lineno;
3998 struct state *stp;
3999 struct action *ap;
4000 struct rule *rp;
drh8b582012003-10-21 13:16:03 +00004001 struct acttab *pActtab;
drhc75e0162015-09-07 02:23:02 +00004002 int i, j, n, sz;
4003 int szActionType; /* sizeof(YYACTIONTYPE) */
4004 int szCodeType; /* sizeof(YYCODETYPE) */
icculus9e44cf12010-02-14 17:14:22 +00004005 const char *name;
drh8b582012003-10-21 13:16:03 +00004006 int mnTknOfst, mxTknOfst;
4007 int mnNtOfst, mxNtOfst;
drhfdbf9282003-10-21 16:34:41 +00004008 struct axset *ax;
drh75897232000-05-29 14:26:00 +00004009
4010 in = tplt_open(lemp);
4011 if( in==0 ) return;
drh2aa6ca42004-09-10 00:14:04 +00004012 out = file_open(lemp,".c","wb");
drh75897232000-05-29 14:26:00 +00004013 if( out==0 ){
4014 fclose(in);
4015 return;
4016 }
4017 lineno = 1;
4018 tplt_xfer(lemp->name,in,out,&lineno);
4019
4020 /* Generate the include code, if any */
drha5808f32008-04-27 22:19:44 +00004021 tplt_print(out,lemp,lemp->include,&lineno);
drh75897232000-05-29 14:26:00 +00004022 if( mhflag ){
mistachkin8e189222015-04-19 21:43:16 +00004023 char *incName = file_makename(lemp, ".h");
4024 fprintf(out,"#include \"%s\"\n", incName); lineno++;
4025 free(incName);
drh75897232000-05-29 14:26:00 +00004026 }
4027 tplt_xfer(lemp->name,in,out,&lineno);
4028
4029 /* Generate #defines for all tokens */
4030 if( mhflag ){
icculus9e44cf12010-02-14 17:14:22 +00004031 const char *prefix;
drh75897232000-05-29 14:26:00 +00004032 fprintf(out,"#if INTERFACE\n"); lineno++;
4033 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4034 else prefix = "";
4035 for(i=1; i<lemp->nterminal; i++){
4036 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
4037 lineno++;
4038 }
4039 fprintf(out,"#endif\n"); lineno++;
4040 }
4041 tplt_xfer(lemp->name,in,out,&lineno);
4042
4043 /* Generate the defines */
drh75897232000-05-29 14:26:00 +00004044 fprintf(out,"#define YYCODETYPE %s\n",
drhc75e0162015-09-07 02:23:02 +00004045 minimum_size_type(0, lemp->nsymbol+1, &szCodeType)); lineno++;
drh75897232000-05-29 14:26:00 +00004046 fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++;
4047 fprintf(out,"#define YYACTIONTYPE %s\n",
drh3bd48ab2015-09-07 18:23:37 +00004048 minimum_size_type(0,lemp->nstate+lemp->nrule*2+5,&szActionType)); lineno++;
drhe09daa92006-06-10 13:29:31 +00004049 if( lemp->wildcard ){
4050 fprintf(out,"#define YYWILDCARD %d\n",
4051 lemp->wildcard->index); lineno++;
4052 }
drh75897232000-05-29 14:26:00 +00004053 print_stack_union(out,lemp,&lineno,mhflag);
drhca44b5a2007-02-22 23:06:58 +00004054 fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004055 if( lemp->stacksize ){
drh75897232000-05-29 14:26:00 +00004056 fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
4057 }else{
4058 fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
4059 }
drhca44b5a2007-02-22 23:06:58 +00004060 fprintf(out, "#endif\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004061 if( mhflag ){
4062 fprintf(out,"#if INTERFACE\n"); lineno++;
4063 }
4064 name = lemp->name ? lemp->name : "Parse";
4065 if( lemp->arg && lemp->arg[0] ){
drh87cf1372008-08-13 20:09:06 +00004066 i = lemonStrlen(lemp->arg);
drhc56fac72015-10-29 13:48:15 +00004067 while( i>=1 && ISSPACE(lemp->arg[i-1]) ) i--;
4068 while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
drh1f245e42002-03-11 13:55:50 +00004069 fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++;
4070 fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++;
4071 fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n",
4072 name,lemp->arg,&lemp->arg[i]); lineno++;
4073 fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n",
4074 name,&lemp->arg[i],&lemp->arg[i]); lineno++;
drh75897232000-05-29 14:26:00 +00004075 }else{
drh1f245e42002-03-11 13:55:50 +00004076 fprintf(out,"#define %sARG_SDECL\n",name); lineno++;
4077 fprintf(out,"#define %sARG_PDECL\n",name); lineno++;
4078 fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
4079 fprintf(out,"#define %sARG_STORE\n",name); lineno++;
drh75897232000-05-29 14:26:00 +00004080 }
4081 if( mhflag ){
4082 fprintf(out,"#endif\n"); lineno++;
4083 }
drhc4dd3fd2008-01-22 01:48:05 +00004084 if( lemp->errsym->useCnt ){
drh3bd48ab2015-09-07 18:23:37 +00004085 fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
4086 fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
drhc4dd3fd2008-01-22 01:48:05 +00004087 }
drh0bd1f4e2002-06-06 18:54:39 +00004088 if( lemp->has_fallback ){
4089 fprintf(out,"#define YYFALLBACK 1\n"); lineno++;
4090 }
drh75897232000-05-29 14:26:00 +00004091
drh3bd48ab2015-09-07 18:23:37 +00004092 /* Compute the action table, but do not output it yet. The action
4093 ** table must be computed before generating the YYNSTATE macro because
4094 ** we need to know how many states can be eliminated.
drh75897232000-05-29 14:26:00 +00004095 */
drh3bd48ab2015-09-07 18:23:37 +00004096 ax = (struct axset *) calloc(lemp->nxstate*2, sizeof(ax[0]));
drhfdbf9282003-10-21 16:34:41 +00004097 if( ax==0 ){
4098 fprintf(stderr,"malloc failed\n");
4099 exit(1);
4100 }
drh3bd48ab2015-09-07 18:23:37 +00004101 for(i=0; i<lemp->nxstate; i++){
drh75897232000-05-29 14:26:00 +00004102 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00004103 ax[i*2].stp = stp;
4104 ax[i*2].isTkn = 1;
4105 ax[i*2].nAction = stp->nTknAct;
4106 ax[i*2+1].stp = stp;
4107 ax[i*2+1].isTkn = 0;
4108 ax[i*2+1].nAction = stp->nNtAct;
drh75897232000-05-29 14:26:00 +00004109 }
drh8b582012003-10-21 13:16:03 +00004110 mxTknOfst = mnTknOfst = 0;
4111 mxNtOfst = mnNtOfst = 0;
drh3bd48ab2015-09-07 18:23:37 +00004112 /* In an effort to minimize the action table size, use the heuristic
4113 ** of placing the largest action sets first */
4114 for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i;
4115 qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare);
drh8b582012003-10-21 13:16:03 +00004116 pActtab = acttab_alloc();
drh3bd48ab2015-09-07 18:23:37 +00004117 for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){
drhfdbf9282003-10-21 16:34:41 +00004118 stp = ax[i].stp;
4119 if( ax[i].isTkn ){
4120 for(ap=stp->ap; ap; ap=ap->next){
4121 int action;
4122 if( ap->sp->index>=lemp->nterminal ) continue;
4123 action = compute_action(lemp, ap);
4124 if( action<0 ) continue;
4125 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00004126 }
drhfdbf9282003-10-21 16:34:41 +00004127 stp->iTknOfst = acttab_insert(pActtab);
4128 if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
4129 if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
4130 }else{
4131 for(ap=stp->ap; ap; ap=ap->next){
4132 int action;
4133 if( ap->sp->index<lemp->nterminal ) continue;
4134 if( ap->sp->index==lemp->nsymbol ) continue;
4135 action = compute_action(lemp, ap);
4136 if( action<0 ) continue;
4137 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00004138 }
drhfdbf9282003-10-21 16:34:41 +00004139 stp->iNtOfst = acttab_insert(pActtab);
4140 if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
4141 if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
drh8b582012003-10-21 13:16:03 +00004142 }
drh337cd0d2015-09-07 23:40:42 +00004143#if 0 /* Uncomment for a trace of how the yy_action[] table fills out */
4144 { int jj, nn;
4145 for(jj=nn=0; jj<pActtab->nAction; jj++){
4146 if( pActtab->aAction[jj].action<0 ) nn++;
4147 }
4148 printf("%4d: State %3d %s n: %2d size: %5d freespace: %d\n",
4149 i, stp->statenum, ax[i].isTkn ? "Token" : "Var ",
4150 ax[i].nAction, pActtab->nAction, nn);
4151 }
4152#endif
drh8b582012003-10-21 13:16:03 +00004153 }
drhfdbf9282003-10-21 16:34:41 +00004154 free(ax);
drh8b582012003-10-21 13:16:03 +00004155
drh756b41e2016-05-24 18:55:08 +00004156 /* Mark rules that are actually used for reduce actions after all
4157 ** optimizations have been applied
4158 */
4159 for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE;
4160 for(i=0; i<lemp->nxstate; i++){
4161 struct action *ap;
4162 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
4163 if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){
4164 ap->x.rp->doesReduce = i;
4165 }
4166 }
4167 }
4168
drh3bd48ab2015-09-07 18:23:37 +00004169 /* Finish rendering the constants now that the action table has
4170 ** been computed */
4171 fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++;
4172 fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
drh337cd0d2015-09-07 23:40:42 +00004173 fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004174 fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",lemp->nstate); lineno++;
4175 i = lemp->nstate + lemp->nrule;
4176 fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++;
4177 fprintf(out,"#define YY_MIN_REDUCE %d\n", i); lineno++;
4178 i = lemp->nstate + lemp->nrule*2;
4179 fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++;
4180 fprintf(out,"#define YY_ERROR_ACTION %d\n", i); lineno++;
4181 fprintf(out,"#define YY_ACCEPT_ACTION %d\n", i+1); lineno++;
4182 fprintf(out,"#define YY_NO_ACTION %d\n", i+2); lineno++;
4183 tplt_xfer(lemp->name,in,out,&lineno);
4184
4185 /* Now output the action table and its associates:
4186 **
4187 ** yy_action[] A single table containing all actions.
4188 ** yy_lookahead[] A table containing the lookahead for each entry in
4189 ** yy_action. Used to detect hash collisions.
4190 ** yy_shift_ofst[] For each state, the offset into yy_action for
4191 ** shifting terminals.
4192 ** yy_reduce_ofst[] For each state, the offset into yy_action for
4193 ** shifting non-terminals after a reduce.
4194 ** yy_default[] Default action for each state.
4195 */
4196
drh8b582012003-10-21 13:16:03 +00004197 /* Output the yy_action table */
drhc75e0162015-09-07 02:23:02 +00004198 lemp->nactiontab = n = acttab_size(pActtab);
4199 lemp->tablesize += n*szActionType;
drhf16371d2009-11-03 19:18:31 +00004200 fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
4201 fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00004202 for(i=j=0; i<n; i++){
4203 int action = acttab_yyaction(pActtab, i);
drhe0479212007-01-12 23:09:23 +00004204 if( action<0 ) action = lemp->nstate + lemp->nrule + 2;
drhfdbf9282003-10-21 16:34:41 +00004205 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004206 fprintf(out, " %4d,", action);
4207 if( j==9 || i==n-1 ){
4208 fprintf(out, "\n"); lineno++;
4209 j = 0;
4210 }else{
4211 j++;
4212 }
4213 }
4214 fprintf(out, "};\n"); lineno++;
4215
4216 /* Output the yy_lookahead table */
drhc75e0162015-09-07 02:23:02 +00004217 lemp->tablesize += n*szCodeType;
drh57196282004-10-06 15:41:16 +00004218 fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00004219 for(i=j=0; i<n; i++){
4220 int la = acttab_yylookahead(pActtab, i);
4221 if( la<0 ) la = lemp->nsymbol;
drhfdbf9282003-10-21 16:34:41 +00004222 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004223 fprintf(out, " %4d,", la);
4224 if( j==9 || i==n-1 ){
4225 fprintf(out, "\n"); lineno++;
4226 j = 0;
4227 }else{
4228 j++;
4229 }
4230 }
4231 fprintf(out, "};\n"); lineno++;
4232
4233 /* Output the yy_shift_ofst[] table */
4234 fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004235 n = lemp->nxstate;
drhada354d2005-11-05 15:03:59 +00004236 while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00004237 fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
4238 fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
4239 fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
drh57196282004-10-06 15:41:16 +00004240 fprintf(out, "static const %s yy_shift_ofst[] = {\n",
drhc75e0162015-09-07 02:23:02 +00004241 minimum_size_type(mnTknOfst-1, mxTknOfst, &sz)); lineno++;
4242 lemp->tablesize += n*sz;
drh8b582012003-10-21 13:16:03 +00004243 for(i=j=0; i<n; i++){
4244 int ofst;
4245 stp = lemp->sorted[i];
4246 ofst = stp->iTknOfst;
4247 if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00004248 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004249 fprintf(out, " %4d,", ofst);
4250 if( j==9 || i==n-1 ){
4251 fprintf(out, "\n"); lineno++;
4252 j = 0;
4253 }else{
4254 j++;
4255 }
4256 }
4257 fprintf(out, "};\n"); lineno++;
4258
4259 /* Output the yy_reduce_ofst[] table */
4260 fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004261 n = lemp->nxstate;
drhada354d2005-11-05 15:03:59 +00004262 while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00004263 fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
4264 fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
4265 fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
drh57196282004-10-06 15:41:16 +00004266 fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
drhc75e0162015-09-07 02:23:02 +00004267 minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++;
4268 lemp->tablesize += n*sz;
drh8b582012003-10-21 13:16:03 +00004269 for(i=j=0; i<n; i++){
4270 int ofst;
4271 stp = lemp->sorted[i];
4272 ofst = stp->iNtOfst;
4273 if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00004274 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004275 fprintf(out, " %4d,", ofst);
4276 if( j==9 || i==n-1 ){
4277 fprintf(out, "\n"); lineno++;
4278 j = 0;
4279 }else{
4280 j++;
4281 }
4282 }
4283 fprintf(out, "};\n"); lineno++;
4284
4285 /* Output the default action table */
drh57196282004-10-06 15:41:16 +00004286 fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004287 n = lemp->nxstate;
drhc75e0162015-09-07 02:23:02 +00004288 lemp->tablesize += n*szActionType;
drh8b582012003-10-21 13:16:03 +00004289 for(i=j=0; i<n; i++){
4290 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00004291 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh3bd48ab2015-09-07 18:23:37 +00004292 fprintf(out, " %4d,", stp->iDfltReduce+lemp->nstate+lemp->nrule);
drh8b582012003-10-21 13:16:03 +00004293 if( j==9 || i==n-1 ){
4294 fprintf(out, "\n"); lineno++;
4295 j = 0;
4296 }else{
4297 j++;
4298 }
4299 }
4300 fprintf(out, "};\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004301 tplt_xfer(lemp->name,in,out,&lineno);
4302
drh0bd1f4e2002-06-06 18:54:39 +00004303 /* Generate the table of fallback tokens.
4304 */
4305 if( lemp->has_fallback ){
drh1441f3e2009-06-12 12:50:50 +00004306 int mx = lemp->nterminal - 1;
4307 while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; }
drhc75e0162015-09-07 02:23:02 +00004308 lemp->tablesize += (mx+1)*szCodeType;
drh1441f3e2009-06-12 12:50:50 +00004309 for(i=0; i<=mx; i++){
drh0bd1f4e2002-06-06 18:54:39 +00004310 struct symbol *p = lemp->symbols[i];
4311 if( p->fallback==0 ){
4312 fprintf(out, " 0, /* %10s => nothing */\n", p->name);
4313 }else{
4314 fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index,
4315 p->name, p->fallback->name);
4316 }
4317 lineno++;
4318 }
4319 }
4320 tplt_xfer(lemp->name, in, out, &lineno);
4321
4322 /* Generate a table containing the symbolic name of every symbol
4323 */
drh75897232000-05-29 14:26:00 +00004324 for(i=0; i<lemp->nsymbol; i++){
drh898799f2014-01-10 23:21:00 +00004325 lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name);
drh75897232000-05-29 14:26:00 +00004326 fprintf(out," %-15s",line);
4327 if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
4328 }
4329 if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
4330 tplt_xfer(lemp->name,in,out,&lineno);
4331
drh0bd1f4e2002-06-06 18:54:39 +00004332 /* Generate a table containing a text string that describes every
drh34ff57b2008-07-14 12:27:51 +00004333 ** rule in the rule set of the grammar. This information is used
drh0bd1f4e2002-06-06 18:54:39 +00004334 ** when tracing REDUCE actions.
4335 */
4336 for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
drh4ef07702016-03-16 19:45:54 +00004337 assert( rp->iRule==i );
drhc4dd3fd2008-01-22 01:48:05 +00004338 fprintf(out," /* %3d */ \"", i);
4339 writeRuleText(out, rp);
drh0bd1f4e2002-06-06 18:54:39 +00004340 fprintf(out,"\",\n"); lineno++;
4341 }
4342 tplt_xfer(lemp->name,in,out,&lineno);
4343
drh75897232000-05-29 14:26:00 +00004344 /* Generate code which executes every time a symbol is popped from
4345 ** the stack while processing errors or while destroying the parser.
drh0bd1f4e2002-06-06 18:54:39 +00004346 ** (In other words, generate the %destructor actions)
4347 */
drh75897232000-05-29 14:26:00 +00004348 if( lemp->tokendest ){
drh4dc8ef52008-07-01 17:13:57 +00004349 int once = 1;
drh75897232000-05-29 14:26:00 +00004350 for(i=0; i<lemp->nsymbol; i++){
4351 struct symbol *sp = lemp->symbols[i];
4352 if( sp==0 || sp->type!=TERMINAL ) continue;
drh4dc8ef52008-07-01 17:13:57 +00004353 if( once ){
4354 fprintf(out, " /* TERMINAL Destructor */\n"); lineno++;
4355 once = 0;
4356 }
drhc53eed12009-06-12 17:46:19 +00004357 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh75897232000-05-29 14:26:00 +00004358 }
4359 for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
4360 if( i<lemp->nsymbol ){
4361 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4362 fprintf(out," break;\n"); lineno++;
4363 }
4364 }
drh8d659732005-01-13 23:54:06 +00004365 if( lemp->vardest ){
4366 struct symbol *dflt_sp = 0;
drh4dc8ef52008-07-01 17:13:57 +00004367 int once = 1;
drh8d659732005-01-13 23:54:06 +00004368 for(i=0; i<lemp->nsymbol; i++){
4369 struct symbol *sp = lemp->symbols[i];
4370 if( sp==0 || sp->type==TERMINAL ||
4371 sp->index<=0 || sp->destructor!=0 ) continue;
drh4dc8ef52008-07-01 17:13:57 +00004372 if( once ){
4373 fprintf(out, " /* Default NON-TERMINAL Destructor */\n"); lineno++;
4374 once = 0;
4375 }
drhc53eed12009-06-12 17:46:19 +00004376 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh8d659732005-01-13 23:54:06 +00004377 dflt_sp = sp;
4378 }
4379 if( dflt_sp!=0 ){
4380 emit_destructor_code(out,dflt_sp,lemp,&lineno);
drh8d659732005-01-13 23:54:06 +00004381 }
drh4dc8ef52008-07-01 17:13:57 +00004382 fprintf(out," break;\n"); lineno++;
drh8d659732005-01-13 23:54:06 +00004383 }
drh75897232000-05-29 14:26:00 +00004384 for(i=0; i<lemp->nsymbol; i++){
4385 struct symbol *sp = lemp->symbols[i];
4386 if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
drh75013012009-06-12 15:47:34 +00004387 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004388
4389 /* Combine duplicate destructors into a single case */
4390 for(j=i+1; j<lemp->nsymbol; j++){
4391 struct symbol *sp2 = lemp->symbols[j];
4392 if( sp2 && sp2->type!=TERMINAL && sp2->destructor
4393 && sp2->dtnum==sp->dtnum
4394 && strcmp(sp->destructor,sp2->destructor)==0 ){
drhc53eed12009-06-12 17:46:19 +00004395 fprintf(out," case %d: /* %s */\n",
4396 sp2->index, sp2->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004397 sp2->destructor = 0;
4398 }
4399 }
4400
drh75897232000-05-29 14:26:00 +00004401 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4402 fprintf(out," break;\n"); lineno++;
4403 }
drh75897232000-05-29 14:26:00 +00004404 tplt_xfer(lemp->name,in,out,&lineno);
4405
4406 /* Generate code which executes whenever the parser stack overflows */
drha5808f32008-04-27 22:19:44 +00004407 tplt_print(out,lemp,lemp->overflow,&lineno);
drh75897232000-05-29 14:26:00 +00004408 tplt_xfer(lemp->name,in,out,&lineno);
4409
4410 /* Generate the table of rule information
4411 **
4412 ** Note: This code depends on the fact that rules are number
4413 ** sequentually beginning with 0.
4414 */
4415 for(rp=lemp->rule; rp; rp=rp->next){
4416 fprintf(out," { %d, %d },\n",rp->lhs->index,rp->nrhs); lineno++;
4417 }
4418 tplt_xfer(lemp->name,in,out,&lineno);
4419
4420 /* Generate code which execution during each REDUCE action */
drhdabd04c2016-02-17 01:46:19 +00004421 i = 0;
drh75897232000-05-29 14:26:00 +00004422 for(rp=lemp->rule; rp; rp=rp->next){
drhdabd04c2016-02-17 01:46:19 +00004423 i += translate_code(lemp, rp);
4424 }
4425 if( i ){
4426 fprintf(out," YYMINORTYPE yylhsminor;\n"); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004427 }
drhc53eed12009-06-12 17:46:19 +00004428 /* First output rules other than the default: rule */
drh0bb132b2004-07-20 14:06:51 +00004429 for(rp=lemp->rule; rp; rp=rp->next){
drhc53eed12009-06-12 17:46:19 +00004430 struct rule *rp2; /* Other rules with the same action */
drh711c9812016-05-23 14:24:31 +00004431 if( rp->codeEmitted ) continue;
4432 if( rp->noCode ){
4433 /* No C code actions, so this will be part of the "default:" rule */
drh2e55b042016-04-30 17:19:30 +00004434 continue;
4435 }
drh4ef07702016-03-16 19:45:54 +00004436 fprintf(out," case %d: /* ", rp->iRule);
drhc4dd3fd2008-01-22 01:48:05 +00004437 writeRuleText(out, rp);
4438 fprintf(out, " */\n"); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004439 for(rp2=rp->next; rp2; rp2=rp2->next){
drhafb8cd92016-04-29 11:28:35 +00004440 if( rp2->code==rp->code && rp2->codePrefix==rp->codePrefix
4441 && rp2->codeSuffix==rp->codeSuffix ){
drh4ef07702016-03-16 19:45:54 +00004442 fprintf(out," case %d: /* ", rp2->iRule);
drhc4dd3fd2008-01-22 01:48:05 +00004443 writeRuleText(out, rp2);
drh4ef07702016-03-16 19:45:54 +00004444 fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->iRule); lineno++;
drh711c9812016-05-23 14:24:31 +00004445 rp2->codeEmitted = 1;
drh0bb132b2004-07-20 14:06:51 +00004446 }
4447 }
drh75897232000-05-29 14:26:00 +00004448 emit_code(out,rp,lemp,&lineno);
4449 fprintf(out," break;\n"); lineno++;
drh711c9812016-05-23 14:24:31 +00004450 rp->codeEmitted = 1;
drh75897232000-05-29 14:26:00 +00004451 }
drhc53eed12009-06-12 17:46:19 +00004452 /* Finally, output the default: rule. We choose as the default: all
4453 ** empty actions. */
4454 fprintf(out," default:\n"); lineno++;
4455 for(rp=lemp->rule; rp; rp=rp->next){
drh711c9812016-05-23 14:24:31 +00004456 if( rp->codeEmitted ) continue;
4457 assert( rp->noCode );
drh4ef07702016-03-16 19:45:54 +00004458 fprintf(out," /* (%d) ", rp->iRule);
drhc53eed12009-06-12 17:46:19 +00004459 writeRuleText(out, rp);
drh756b41e2016-05-24 18:55:08 +00004460 if( rp->doesReduce ){
4461 fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->iRule); lineno++;
4462 }else{
4463 fprintf(out, " (OPTIMIZED OUT) */ assert(yyruleno!=%d);\n",
4464 rp->iRule); lineno++;
4465 }
drhc53eed12009-06-12 17:46:19 +00004466 }
4467 fprintf(out," break;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004468 tplt_xfer(lemp->name,in,out,&lineno);
4469
4470 /* Generate code which executes if a parse fails */
drha5808f32008-04-27 22:19:44 +00004471 tplt_print(out,lemp,lemp->failure,&lineno);
drh75897232000-05-29 14:26:00 +00004472 tplt_xfer(lemp->name,in,out,&lineno);
4473
4474 /* Generate code which executes when a syntax error occurs */
drha5808f32008-04-27 22:19:44 +00004475 tplt_print(out,lemp,lemp->error,&lineno);
drh75897232000-05-29 14:26:00 +00004476 tplt_xfer(lemp->name,in,out,&lineno);
4477
4478 /* Generate code which executes when the parser accepts its input */
drha5808f32008-04-27 22:19:44 +00004479 tplt_print(out,lemp,lemp->accept,&lineno);
drh75897232000-05-29 14:26:00 +00004480 tplt_xfer(lemp->name,in,out,&lineno);
4481
4482 /* Append any addition code the user desires */
drha5808f32008-04-27 22:19:44 +00004483 tplt_print(out,lemp,lemp->extracode,&lineno);
drh75897232000-05-29 14:26:00 +00004484
4485 fclose(in);
4486 fclose(out);
4487 return;
4488}
4489
4490/* Generate a header file for the parser */
icculus9e44cf12010-02-14 17:14:22 +00004491void ReportHeader(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004492{
4493 FILE *out, *in;
icculus9e44cf12010-02-14 17:14:22 +00004494 const char *prefix;
drh75897232000-05-29 14:26:00 +00004495 char line[LINESIZE];
4496 char pattern[LINESIZE];
4497 int i;
4498
4499 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4500 else prefix = "";
drh2aa6ca42004-09-10 00:14:04 +00004501 in = file_open(lemp,".h","rb");
drh75897232000-05-29 14:26:00 +00004502 if( in ){
drh8ba0d1c2012-06-16 15:26:31 +00004503 int nextChar;
drh75897232000-05-29 14:26:00 +00004504 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
drh61f92cd2014-01-11 03:06:18 +00004505 lemon_sprintf(pattern,"#define %s%-30s %3d\n",
4506 prefix,lemp->symbols[i]->name,i);
drh75897232000-05-29 14:26:00 +00004507 if( strcmp(line,pattern) ) break;
4508 }
drh8ba0d1c2012-06-16 15:26:31 +00004509 nextChar = fgetc(in);
drh75897232000-05-29 14:26:00 +00004510 fclose(in);
drh8ba0d1c2012-06-16 15:26:31 +00004511 if( i==lemp->nterminal && nextChar==EOF ){
drh75897232000-05-29 14:26:00 +00004512 /* No change in the file. Don't rewrite it. */
4513 return;
4514 }
4515 }
drh2aa6ca42004-09-10 00:14:04 +00004516 out = file_open(lemp,".h","wb");
drh75897232000-05-29 14:26:00 +00004517 if( out ){
4518 for(i=1; i<lemp->nterminal; i++){
drh61f92cd2014-01-11 03:06:18 +00004519 fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i);
drh75897232000-05-29 14:26:00 +00004520 }
4521 fclose(out);
4522 }
4523 return;
4524}
4525
4526/* Reduce the size of the action tables, if possible, by making use
4527** of defaults.
4528**
drhb59499c2002-02-23 18:45:13 +00004529** In this version, we take the most frequent REDUCE action and make
drhe09daa92006-06-10 13:29:31 +00004530** it the default. Except, there is no default if the wildcard token
4531** is a possible look-ahead.
drh75897232000-05-29 14:26:00 +00004532*/
icculus9e44cf12010-02-14 17:14:22 +00004533void CompressTables(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004534{
4535 struct state *stp;
drhc173ad82016-05-23 16:15:02 +00004536 struct action *ap, *ap2, *nextap;
drhb59499c2002-02-23 18:45:13 +00004537 struct rule *rp, *rp2, *rbest;
drh0c6dfaa2015-09-08 21:16:46 +00004538 int nbest, n;
drh75897232000-05-29 14:26:00 +00004539 int i;
drhe09daa92006-06-10 13:29:31 +00004540 int usesWildcard;
drh75897232000-05-29 14:26:00 +00004541
4542 for(i=0; i<lemp->nstate; i++){
4543 stp = lemp->sorted[i];
drhb59499c2002-02-23 18:45:13 +00004544 nbest = 0;
4545 rbest = 0;
drhe09daa92006-06-10 13:29:31 +00004546 usesWildcard = 0;
drh75897232000-05-29 14:26:00 +00004547
drhb59499c2002-02-23 18:45:13 +00004548 for(ap=stp->ap; ap; ap=ap->next){
drhe09daa92006-06-10 13:29:31 +00004549 if( ap->type==SHIFT && ap->sp==lemp->wildcard ){
4550 usesWildcard = 1;
4551 }
drhb59499c2002-02-23 18:45:13 +00004552 if( ap->type!=REDUCE ) continue;
4553 rp = ap->x.rp;
drhb4960992007-10-05 16:16:36 +00004554 if( rp->lhsStart ) continue;
drhb59499c2002-02-23 18:45:13 +00004555 if( rp==rbest ) continue;
4556 n = 1;
4557 for(ap2=ap->next; ap2; ap2=ap2->next){
4558 if( ap2->type!=REDUCE ) continue;
4559 rp2 = ap2->x.rp;
4560 if( rp2==rbest ) continue;
4561 if( rp2==rp ) n++;
4562 }
4563 if( n>nbest ){
4564 nbest = n;
4565 rbest = rp;
drh75897232000-05-29 14:26:00 +00004566 }
4567 }
drhb59499c2002-02-23 18:45:13 +00004568
4569 /* Do not make a default if the number of rules to default
drhe09daa92006-06-10 13:29:31 +00004570 ** is not at least 1 or if the wildcard token is a possible
4571 ** lookahead.
4572 */
4573 if( nbest<1 || usesWildcard ) continue;
drh75897232000-05-29 14:26:00 +00004574
drhb59499c2002-02-23 18:45:13 +00004575
4576 /* Combine matching REDUCE actions into a single default */
4577 for(ap=stp->ap; ap; ap=ap->next){
4578 if( ap->type==REDUCE && ap->x.rp==rbest ) break;
4579 }
drh75897232000-05-29 14:26:00 +00004580 assert( ap );
4581 ap->sp = Symbol_new("{default}");
4582 for(ap=ap->next; ap; ap=ap->next){
drhb59499c2002-02-23 18:45:13 +00004583 if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
drh75897232000-05-29 14:26:00 +00004584 }
4585 stp->ap = Action_sort(stp->ap);
drh3bd48ab2015-09-07 18:23:37 +00004586
4587 for(ap=stp->ap; ap; ap=ap->next){
4588 if( ap->type==SHIFT ) break;
4589 if( ap->type==REDUCE && ap->x.rp!=rbest ) break;
4590 }
4591 if( ap==0 ){
4592 stp->autoReduce = 1;
4593 stp->pDfltReduce = rbest;
4594 }
4595 }
4596
4597 /* Make a second pass over all states and actions. Convert
4598 ** every action that is a SHIFT to an autoReduce state into
4599 ** a SHIFTREDUCE action.
4600 */
4601 for(i=0; i<lemp->nstate; i++){
4602 stp = lemp->sorted[i];
4603 for(ap=stp->ap; ap; ap=ap->next){
4604 struct state *pNextState;
4605 if( ap->type!=SHIFT ) continue;
4606 pNextState = ap->x.stp;
4607 if( pNextState->autoReduce && pNextState->pDfltReduce!=0 ){
4608 ap->type = SHIFTREDUCE;
4609 ap->x.rp = pNextState->pDfltReduce;
4610 }
4611 }
drh75897232000-05-29 14:26:00 +00004612 }
drhc173ad82016-05-23 16:15:02 +00004613
4614 /* If a SHIFTREDUCE action specifies a rule that has a single RHS term
4615 ** (meaning that the SHIFTREDUCE will land back in the state where it
4616 ** started) and if there is no C-code associated with the reduce action,
4617 ** then we can go ahead and convert the action to be the same as the
4618 ** action for the RHS of the rule.
4619 */
4620 for(i=0; i<lemp->nstate; i++){
4621 stp = lemp->sorted[i];
4622 for(ap=stp->ap; ap; ap=nextap){
4623 nextap = ap->next;
4624 if( ap->type!=SHIFTREDUCE ) continue;
4625 rp = ap->x.rp;
4626 if( rp->noCode==0 ) continue;
4627 if( rp->nrhs!=1 ) continue;
4628#if 1
4629 /* Only apply this optimization to non-terminals. It would be OK to
4630 ** apply it to terminal symbols too, but that makes the parser tables
4631 ** larger. */
4632 if( ap->sp->index<lemp->nterminal ) continue;
4633#endif
4634 /* If we reach this point, it means the optimization can be applied */
4635 nextap = ap;
4636 for(ap2=stp->ap; ap2 && (ap2==ap || ap2->sp!=rp->lhs); ap2=ap2->next){}
4637 assert( ap2!=0 );
4638 ap->spOpt = ap2->sp;
4639 ap->type = ap2->type;
4640 ap->x = ap2->x;
4641 }
4642 }
drh75897232000-05-29 14:26:00 +00004643}
drhb59499c2002-02-23 18:45:13 +00004644
drhada354d2005-11-05 15:03:59 +00004645
4646/*
4647** Compare two states for sorting purposes. The smaller state is the
4648** one with the most non-terminal actions. If they have the same number
4649** of non-terminal actions, then the smaller is the one with the most
4650** token actions.
4651*/
4652static int stateResortCompare(const void *a, const void *b){
4653 const struct state *pA = *(const struct state**)a;
4654 const struct state *pB = *(const struct state**)b;
4655 int n;
4656
4657 n = pB->nNtAct - pA->nNtAct;
4658 if( n==0 ){
4659 n = pB->nTknAct - pA->nTknAct;
drhe594bc32009-11-03 13:02:25 +00004660 if( n==0 ){
4661 n = pB->statenum - pA->statenum;
4662 }
drhada354d2005-11-05 15:03:59 +00004663 }
drhe594bc32009-11-03 13:02:25 +00004664 assert( n!=0 );
drhada354d2005-11-05 15:03:59 +00004665 return n;
4666}
4667
4668
4669/*
4670** Renumber and resort states so that states with fewer choices
4671** occur at the end. Except, keep state 0 as the first state.
4672*/
icculus9e44cf12010-02-14 17:14:22 +00004673void ResortStates(struct lemon *lemp)
drhada354d2005-11-05 15:03:59 +00004674{
4675 int i;
4676 struct state *stp;
4677 struct action *ap;
4678
4679 for(i=0; i<lemp->nstate; i++){
4680 stp = lemp->sorted[i];
4681 stp->nTknAct = stp->nNtAct = 0;
drh3bd48ab2015-09-07 18:23:37 +00004682 stp->iDfltReduce = lemp->nrule; /* Init dflt action to "syntax error" */
drhada354d2005-11-05 15:03:59 +00004683 stp->iTknOfst = NO_OFFSET;
4684 stp->iNtOfst = NO_OFFSET;
4685 for(ap=stp->ap; ap; ap=ap->next){
drh3bd48ab2015-09-07 18:23:37 +00004686 int iAction = compute_action(lemp,ap);
4687 if( iAction>=0 ){
drhada354d2005-11-05 15:03:59 +00004688 if( ap->sp->index<lemp->nterminal ){
4689 stp->nTknAct++;
4690 }else if( ap->sp->index<lemp->nsymbol ){
4691 stp->nNtAct++;
4692 }else{
drh3bd48ab2015-09-07 18:23:37 +00004693 assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp );
4694 stp->iDfltReduce = iAction - lemp->nstate - lemp->nrule;
drhada354d2005-11-05 15:03:59 +00004695 }
4696 }
4697 }
4698 }
4699 qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
4700 stateResortCompare);
4701 for(i=0; i<lemp->nstate; i++){
4702 lemp->sorted[i]->statenum = i;
4703 }
drh3bd48ab2015-09-07 18:23:37 +00004704 lemp->nxstate = lemp->nstate;
4705 while( lemp->nxstate>1 && lemp->sorted[lemp->nxstate-1]->autoReduce ){
4706 lemp->nxstate--;
4707 }
drhada354d2005-11-05 15:03:59 +00004708}
4709
4710
drh75897232000-05-29 14:26:00 +00004711/***************** From the file "set.c" ************************************/
4712/*
4713** Set manipulation routines for the LEMON parser generator.
4714*/
4715
4716static int size = 0;
4717
4718/* Set the set size */
icculus9e44cf12010-02-14 17:14:22 +00004719void SetSize(int n)
drh75897232000-05-29 14:26:00 +00004720{
4721 size = n+1;
4722}
4723
4724/* Allocate a new set */
4725char *SetNew(){
4726 char *s;
drh9892c5d2007-12-21 00:02:11 +00004727 s = (char*)calloc( size, 1);
drh75897232000-05-29 14:26:00 +00004728 if( s==0 ){
4729 extern void memory_error();
4730 memory_error();
4731 }
drh75897232000-05-29 14:26:00 +00004732 return s;
4733}
4734
4735/* Deallocate a set */
icculus9e44cf12010-02-14 17:14:22 +00004736void SetFree(char *s)
drh75897232000-05-29 14:26:00 +00004737{
4738 free(s);
4739}
4740
4741/* Add a new element to the set. Return TRUE if the element was added
4742** and FALSE if it was already there. */
icculus9e44cf12010-02-14 17:14:22 +00004743int SetAdd(char *s, int e)
drh75897232000-05-29 14:26:00 +00004744{
4745 int rv;
drh9892c5d2007-12-21 00:02:11 +00004746 assert( e>=0 && e<size );
drh75897232000-05-29 14:26:00 +00004747 rv = s[e];
4748 s[e] = 1;
4749 return !rv;
4750}
4751
4752/* Add every element of s2 to s1. Return TRUE if s1 changes. */
icculus9e44cf12010-02-14 17:14:22 +00004753int SetUnion(char *s1, char *s2)
drh75897232000-05-29 14:26:00 +00004754{
4755 int i, progress;
4756 progress = 0;
4757 for(i=0; i<size; i++){
4758 if( s2[i]==0 ) continue;
4759 if( s1[i]==0 ){
4760 progress = 1;
4761 s1[i] = 1;
4762 }
4763 }
4764 return progress;
4765}
4766/********************** From the file "table.c" ****************************/
4767/*
4768** All code in this file has been automatically generated
4769** from a specification in the file
4770** "table.q"
4771** by the associative array code building program "aagen".
4772** Do not edit this file! Instead, edit the specification
4773** file, then rerun aagen.
4774*/
4775/*
4776** Code for processing tables in the LEMON parser generator.
4777*/
4778
drh01f75f22013-10-02 20:46:30 +00004779PRIVATE unsigned strhash(const char *x)
drh75897232000-05-29 14:26:00 +00004780{
drh01f75f22013-10-02 20:46:30 +00004781 unsigned h = 0;
4782 while( *x ) h = h*13 + *(x++);
drh75897232000-05-29 14:26:00 +00004783 return h;
4784}
4785
4786/* Works like strdup, sort of. Save a string in malloced memory, but
4787** keep strings in a table so that the same string is not in more
4788** than one place.
4789*/
icculus9e44cf12010-02-14 17:14:22 +00004790const char *Strsafe(const char *y)
drh75897232000-05-29 14:26:00 +00004791{
icculus9e44cf12010-02-14 17:14:22 +00004792 const char *z;
4793 char *cpy;
drh75897232000-05-29 14:26:00 +00004794
drh916f75f2006-07-17 00:19:39 +00004795 if( y==0 ) return 0;
drh75897232000-05-29 14:26:00 +00004796 z = Strsafe_find(y);
icculus9e44cf12010-02-14 17:14:22 +00004797 if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
drh898799f2014-01-10 23:21:00 +00004798 lemon_strcpy(cpy,y);
icculus9e44cf12010-02-14 17:14:22 +00004799 z = cpy;
drh75897232000-05-29 14:26:00 +00004800 Strsafe_insert(z);
4801 }
4802 MemoryCheck(z);
4803 return z;
4804}
4805
4806/* There is one instance of the following structure for each
4807** associative array of type "x1".
4808*/
4809struct s_x1 {
4810 int size; /* The number of available slots. */
4811 /* Must be a power of 2 greater than or */
4812 /* equal to 1 */
4813 int count; /* Number of currently slots filled */
4814 struct s_x1node *tbl; /* The data stored here */
4815 struct s_x1node **ht; /* Hash table for lookups */
4816};
4817
4818/* There is one instance of this structure for every data element
4819** in an associative array of type "x1".
4820*/
4821typedef struct s_x1node {
icculus9e44cf12010-02-14 17:14:22 +00004822 const char *data; /* The data */
drh75897232000-05-29 14:26:00 +00004823 struct s_x1node *next; /* Next entry with the same hash */
4824 struct s_x1node **from; /* Previous link */
4825} x1node;
4826
4827/* There is only one instance of the array, which is the following */
4828static struct s_x1 *x1a;
4829
4830/* Allocate a new associative array */
4831void Strsafe_init(){
4832 if( x1a ) return;
4833 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
4834 if( x1a ){
4835 x1a->size = 1024;
4836 x1a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00004837 x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*));
drh75897232000-05-29 14:26:00 +00004838 if( x1a->tbl==0 ){
4839 free(x1a);
4840 x1a = 0;
4841 }else{
4842 int i;
4843 x1a->ht = (x1node**)&(x1a->tbl[1024]);
4844 for(i=0; i<1024; i++) x1a->ht[i] = 0;
4845 }
4846 }
4847}
4848/* Insert a new record into the array. Return TRUE if successful.
4849** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004850int Strsafe_insert(const char *data)
drh75897232000-05-29 14:26:00 +00004851{
4852 x1node *np;
drh01f75f22013-10-02 20:46:30 +00004853 unsigned h;
4854 unsigned ph;
drh75897232000-05-29 14:26:00 +00004855
4856 if( x1a==0 ) return 0;
4857 ph = strhash(data);
4858 h = ph & (x1a->size-1);
4859 np = x1a->ht[h];
4860 while( np ){
4861 if( strcmp(np->data,data)==0 ){
4862 /* An existing entry with the same key is found. */
4863 /* Fail because overwrite is not allows. */
4864 return 0;
4865 }
4866 np = np->next;
4867 }
4868 if( x1a->count>=x1a->size ){
4869 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00004870 int i,arrSize;
drh75897232000-05-29 14:26:00 +00004871 struct s_x1 array;
mistachkin8e189222015-04-19 21:43:16 +00004872 array.size = arrSize = x1a->size*2;
drh75897232000-05-29 14:26:00 +00004873 array.count = x1a->count;
mistachkin8e189222015-04-19 21:43:16 +00004874 array.tbl = (x1node*)calloc(arrSize, sizeof(x1node) + sizeof(x1node*));
drh75897232000-05-29 14:26:00 +00004875 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00004876 array.ht = (x1node**)&(array.tbl[arrSize]);
4877 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00004878 for(i=0; i<x1a->count; i++){
4879 x1node *oldnp, *newnp;
4880 oldnp = &(x1a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00004881 h = strhash(oldnp->data) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00004882 newnp = &(array.tbl[i]);
4883 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4884 newnp->next = array.ht[h];
4885 newnp->data = oldnp->data;
4886 newnp->from = &(array.ht[h]);
4887 array.ht[h] = newnp;
4888 }
4889 free(x1a->tbl);
4890 *x1a = array;
4891 }
4892 /* Insert the new data */
4893 h = ph & (x1a->size-1);
4894 np = &(x1a->tbl[x1a->count++]);
4895 np->data = data;
4896 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
4897 np->next = x1a->ht[h];
4898 x1a->ht[h] = np;
4899 np->from = &(x1a->ht[h]);
4900 return 1;
4901}
4902
4903/* Return a pointer to data assigned to the given key. Return NULL
4904** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004905const char *Strsafe_find(const char *key)
drh75897232000-05-29 14:26:00 +00004906{
drh01f75f22013-10-02 20:46:30 +00004907 unsigned h;
drh75897232000-05-29 14:26:00 +00004908 x1node *np;
4909
4910 if( x1a==0 ) return 0;
4911 h = strhash(key) & (x1a->size-1);
4912 np = x1a->ht[h];
4913 while( np ){
4914 if( strcmp(np->data,key)==0 ) break;
4915 np = np->next;
4916 }
4917 return np ? np->data : 0;
4918}
4919
4920/* Return a pointer to the (terminal or nonterminal) symbol "x".
4921** Create a new symbol if this is the first time "x" has been seen.
4922*/
icculus9e44cf12010-02-14 17:14:22 +00004923struct symbol *Symbol_new(const char *x)
drh75897232000-05-29 14:26:00 +00004924{
4925 struct symbol *sp;
4926
4927 sp = Symbol_find(x);
4928 if( sp==0 ){
drh9892c5d2007-12-21 00:02:11 +00004929 sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
drh75897232000-05-29 14:26:00 +00004930 MemoryCheck(sp);
4931 sp->name = Strsafe(x);
drhc56fac72015-10-29 13:48:15 +00004932 sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL;
drh75897232000-05-29 14:26:00 +00004933 sp->rule = 0;
drh0bd1f4e2002-06-06 18:54:39 +00004934 sp->fallback = 0;
drh75897232000-05-29 14:26:00 +00004935 sp->prec = -1;
4936 sp->assoc = UNK;
4937 sp->firstset = 0;
drhaa9f1122007-08-23 02:50:56 +00004938 sp->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00004939 sp->destructor = 0;
drh4dc8ef52008-07-01 17:13:57 +00004940 sp->destLineno = 0;
drh75897232000-05-29 14:26:00 +00004941 sp->datatype = 0;
drhc4dd3fd2008-01-22 01:48:05 +00004942 sp->useCnt = 0;
drh75897232000-05-29 14:26:00 +00004943 Symbol_insert(sp,sp->name);
4944 }
drhc4dd3fd2008-01-22 01:48:05 +00004945 sp->useCnt++;
drh75897232000-05-29 14:26:00 +00004946 return sp;
4947}
4948
drh61f92cd2014-01-11 03:06:18 +00004949/* Compare two symbols for sorting purposes. Return negative,
4950** zero, or positive if a is less then, equal to, or greater
4951** than b.
drh60d31652004-02-22 00:08:04 +00004952**
4953** Symbols that begin with upper case letters (terminals or tokens)
4954** must sort before symbols that begin with lower case letters
drh61f92cd2014-01-11 03:06:18 +00004955** (non-terminals). And MULTITERMINAL symbols (created using the
4956** %token_class directive) must sort at the very end. Other than
4957** that, the order does not matter.
drh60d31652004-02-22 00:08:04 +00004958**
4959** We find experimentally that leaving the symbols in their original
4960** order (the order they appeared in the grammar file) gives the
4961** smallest parser tables in SQLite.
4962*/
icculus9e44cf12010-02-14 17:14:22 +00004963int Symbolcmpp(const void *_a, const void *_b)
4964{
drh61f92cd2014-01-11 03:06:18 +00004965 const struct symbol *a = *(const struct symbol **) _a;
4966 const struct symbol *b = *(const struct symbol **) _b;
4967 int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1;
4968 int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1;
4969 return i1==i2 ? a->index - b->index : i1 - i2;
drh75897232000-05-29 14:26:00 +00004970}
4971
4972/* There is one instance of the following structure for each
4973** associative array of type "x2".
4974*/
4975struct s_x2 {
4976 int size; /* The number of available slots. */
4977 /* Must be a power of 2 greater than or */
4978 /* equal to 1 */
4979 int count; /* Number of currently slots filled */
4980 struct s_x2node *tbl; /* The data stored here */
4981 struct s_x2node **ht; /* Hash table for lookups */
4982};
4983
4984/* There is one instance of this structure for every data element
4985** in an associative array of type "x2".
4986*/
4987typedef struct s_x2node {
icculus9e44cf12010-02-14 17:14:22 +00004988 struct symbol *data; /* The data */
4989 const char *key; /* The key */
drh75897232000-05-29 14:26:00 +00004990 struct s_x2node *next; /* Next entry with the same hash */
4991 struct s_x2node **from; /* Previous link */
4992} x2node;
4993
4994/* There is only one instance of the array, which is the following */
4995static struct s_x2 *x2a;
4996
4997/* Allocate a new associative array */
4998void Symbol_init(){
4999 if( x2a ) return;
5000 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
5001 if( x2a ){
5002 x2a->size = 128;
5003 x2a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005004 x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*));
drh75897232000-05-29 14:26:00 +00005005 if( x2a->tbl==0 ){
5006 free(x2a);
5007 x2a = 0;
5008 }else{
5009 int i;
5010 x2a->ht = (x2node**)&(x2a->tbl[128]);
5011 for(i=0; i<128; i++) x2a->ht[i] = 0;
5012 }
5013 }
5014}
5015/* Insert a new record into the array. Return TRUE if successful.
5016** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005017int Symbol_insert(struct symbol *data, const char *key)
drh75897232000-05-29 14:26:00 +00005018{
5019 x2node *np;
drh01f75f22013-10-02 20:46:30 +00005020 unsigned h;
5021 unsigned ph;
drh75897232000-05-29 14:26:00 +00005022
5023 if( x2a==0 ) return 0;
5024 ph = strhash(key);
5025 h = ph & (x2a->size-1);
5026 np = x2a->ht[h];
5027 while( np ){
5028 if( strcmp(np->key,key)==0 ){
5029 /* An existing entry with the same key is found. */
5030 /* Fail because overwrite is not allows. */
5031 return 0;
5032 }
5033 np = np->next;
5034 }
5035 if( x2a->count>=x2a->size ){
5036 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005037 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005038 struct s_x2 array;
mistachkin8e189222015-04-19 21:43:16 +00005039 array.size = arrSize = x2a->size*2;
drh75897232000-05-29 14:26:00 +00005040 array.count = x2a->count;
mistachkin8e189222015-04-19 21:43:16 +00005041 array.tbl = (x2node*)calloc(arrSize, sizeof(x2node) + sizeof(x2node*));
drh75897232000-05-29 14:26:00 +00005042 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005043 array.ht = (x2node**)&(array.tbl[arrSize]);
5044 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005045 for(i=0; i<x2a->count; i++){
5046 x2node *oldnp, *newnp;
5047 oldnp = &(x2a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005048 h = strhash(oldnp->key) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005049 newnp = &(array.tbl[i]);
5050 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5051 newnp->next = array.ht[h];
5052 newnp->key = oldnp->key;
5053 newnp->data = oldnp->data;
5054 newnp->from = &(array.ht[h]);
5055 array.ht[h] = newnp;
5056 }
5057 free(x2a->tbl);
5058 *x2a = array;
5059 }
5060 /* Insert the new data */
5061 h = ph & (x2a->size-1);
5062 np = &(x2a->tbl[x2a->count++]);
5063 np->key = key;
5064 np->data = data;
5065 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
5066 np->next = x2a->ht[h];
5067 x2a->ht[h] = np;
5068 np->from = &(x2a->ht[h]);
5069 return 1;
5070}
5071
5072/* Return a pointer to data assigned to the given key. Return NULL
5073** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005074struct symbol *Symbol_find(const char *key)
drh75897232000-05-29 14:26:00 +00005075{
drh01f75f22013-10-02 20:46:30 +00005076 unsigned h;
drh75897232000-05-29 14:26:00 +00005077 x2node *np;
5078
5079 if( x2a==0 ) return 0;
5080 h = strhash(key) & (x2a->size-1);
5081 np = x2a->ht[h];
5082 while( np ){
5083 if( strcmp(np->key,key)==0 ) break;
5084 np = np->next;
5085 }
5086 return np ? np->data : 0;
5087}
5088
5089/* Return the n-th data. Return NULL if n is out of range. */
icculus9e44cf12010-02-14 17:14:22 +00005090struct symbol *Symbol_Nth(int n)
drh75897232000-05-29 14:26:00 +00005091{
5092 struct symbol *data;
5093 if( x2a && n>0 && n<=x2a->count ){
5094 data = x2a->tbl[n-1].data;
5095 }else{
5096 data = 0;
5097 }
5098 return data;
5099}
5100
5101/* Return the size of the array */
5102int Symbol_count()
5103{
5104 return x2a ? x2a->count : 0;
5105}
5106
5107/* Return an array of pointers to all data in the table.
5108** The array is obtained from malloc. Return NULL if memory allocation
5109** problems, or if the array is empty. */
5110struct symbol **Symbol_arrayof()
5111{
5112 struct symbol **array;
mistachkin8e189222015-04-19 21:43:16 +00005113 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005114 if( x2a==0 ) return 0;
mistachkin8e189222015-04-19 21:43:16 +00005115 arrSize = x2a->count;
5116 array = (struct symbol **)calloc(arrSize, sizeof(struct symbol *));
drh75897232000-05-29 14:26:00 +00005117 if( array ){
mistachkin8e189222015-04-19 21:43:16 +00005118 for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data;
drh75897232000-05-29 14:26:00 +00005119 }
5120 return array;
5121}
5122
5123/* Compare two configurations */
icculus9e44cf12010-02-14 17:14:22 +00005124int Configcmp(const char *_a,const char *_b)
drh75897232000-05-29 14:26:00 +00005125{
icculus9e44cf12010-02-14 17:14:22 +00005126 const struct config *a = (struct config *) _a;
5127 const struct config *b = (struct config *) _b;
drh75897232000-05-29 14:26:00 +00005128 int x;
5129 x = a->rp->index - b->rp->index;
5130 if( x==0 ) x = a->dot - b->dot;
5131 return x;
5132}
5133
5134/* Compare two states */
icculus9e44cf12010-02-14 17:14:22 +00005135PRIVATE int statecmp(struct config *a, struct config *b)
drh75897232000-05-29 14:26:00 +00005136{
5137 int rc;
5138 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
5139 rc = a->rp->index - b->rp->index;
5140 if( rc==0 ) rc = a->dot - b->dot;
5141 }
5142 if( rc==0 ){
5143 if( a ) rc = 1;
5144 if( b ) rc = -1;
5145 }
5146 return rc;
5147}
5148
5149/* Hash a state */
drh01f75f22013-10-02 20:46:30 +00005150PRIVATE unsigned statehash(struct config *a)
drh75897232000-05-29 14:26:00 +00005151{
drh01f75f22013-10-02 20:46:30 +00005152 unsigned h=0;
drh75897232000-05-29 14:26:00 +00005153 while( a ){
5154 h = h*571 + a->rp->index*37 + a->dot;
5155 a = a->bp;
5156 }
5157 return h;
5158}
5159
5160/* Allocate a new state structure */
5161struct state *State_new()
5162{
icculus9e44cf12010-02-14 17:14:22 +00005163 struct state *newstate;
5164 newstate = (struct state *)calloc(1, sizeof(struct state) );
5165 MemoryCheck(newstate);
5166 return newstate;
drh75897232000-05-29 14:26:00 +00005167}
5168
5169/* There is one instance of the following structure for each
5170** associative array of type "x3".
5171*/
5172struct s_x3 {
5173 int size; /* The number of available slots. */
5174 /* Must be a power of 2 greater than or */
5175 /* equal to 1 */
5176 int count; /* Number of currently slots filled */
5177 struct s_x3node *tbl; /* The data stored here */
5178 struct s_x3node **ht; /* Hash table for lookups */
5179};
5180
5181/* There is one instance of this structure for every data element
5182** in an associative array of type "x3".
5183*/
5184typedef struct s_x3node {
5185 struct state *data; /* The data */
5186 struct config *key; /* The key */
5187 struct s_x3node *next; /* Next entry with the same hash */
5188 struct s_x3node **from; /* Previous link */
5189} x3node;
5190
5191/* There is only one instance of the array, which is the following */
5192static struct s_x3 *x3a;
5193
5194/* Allocate a new associative array */
5195void State_init(){
5196 if( x3a ) return;
5197 x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
5198 if( x3a ){
5199 x3a->size = 128;
5200 x3a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005201 x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*));
drh75897232000-05-29 14:26:00 +00005202 if( x3a->tbl==0 ){
5203 free(x3a);
5204 x3a = 0;
5205 }else{
5206 int i;
5207 x3a->ht = (x3node**)&(x3a->tbl[128]);
5208 for(i=0; i<128; i++) x3a->ht[i] = 0;
5209 }
5210 }
5211}
5212/* Insert a new record into the array. Return TRUE if successful.
5213** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005214int State_insert(struct state *data, struct config *key)
drh75897232000-05-29 14:26:00 +00005215{
5216 x3node *np;
drh01f75f22013-10-02 20:46:30 +00005217 unsigned h;
5218 unsigned ph;
drh75897232000-05-29 14:26:00 +00005219
5220 if( x3a==0 ) return 0;
5221 ph = statehash(key);
5222 h = ph & (x3a->size-1);
5223 np = x3a->ht[h];
5224 while( np ){
5225 if( statecmp(np->key,key)==0 ){
5226 /* An existing entry with the same key is found. */
5227 /* Fail because overwrite is not allows. */
5228 return 0;
5229 }
5230 np = np->next;
5231 }
5232 if( x3a->count>=x3a->size ){
5233 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005234 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005235 struct s_x3 array;
mistachkin8e189222015-04-19 21:43:16 +00005236 array.size = arrSize = x3a->size*2;
drh75897232000-05-29 14:26:00 +00005237 array.count = x3a->count;
mistachkin8e189222015-04-19 21:43:16 +00005238 array.tbl = (x3node*)calloc(arrSize, sizeof(x3node) + sizeof(x3node*));
drh75897232000-05-29 14:26:00 +00005239 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005240 array.ht = (x3node**)&(array.tbl[arrSize]);
5241 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005242 for(i=0; i<x3a->count; i++){
5243 x3node *oldnp, *newnp;
5244 oldnp = &(x3a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005245 h = statehash(oldnp->key) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005246 newnp = &(array.tbl[i]);
5247 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5248 newnp->next = array.ht[h];
5249 newnp->key = oldnp->key;
5250 newnp->data = oldnp->data;
5251 newnp->from = &(array.ht[h]);
5252 array.ht[h] = newnp;
5253 }
5254 free(x3a->tbl);
5255 *x3a = array;
5256 }
5257 /* Insert the new data */
5258 h = ph & (x3a->size-1);
5259 np = &(x3a->tbl[x3a->count++]);
5260 np->key = key;
5261 np->data = data;
5262 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
5263 np->next = x3a->ht[h];
5264 x3a->ht[h] = np;
5265 np->from = &(x3a->ht[h]);
5266 return 1;
5267}
5268
5269/* Return a pointer to data assigned to the given key. Return NULL
5270** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005271struct state *State_find(struct config *key)
drh75897232000-05-29 14:26:00 +00005272{
drh01f75f22013-10-02 20:46:30 +00005273 unsigned h;
drh75897232000-05-29 14:26:00 +00005274 x3node *np;
5275
5276 if( x3a==0 ) return 0;
5277 h = statehash(key) & (x3a->size-1);
5278 np = x3a->ht[h];
5279 while( np ){
5280 if( statecmp(np->key,key)==0 ) break;
5281 np = np->next;
5282 }
5283 return np ? np->data : 0;
5284}
5285
5286/* Return an array of pointers to all data in the table.
5287** The array is obtained from malloc. Return NULL if memory allocation
5288** problems, or if the array is empty. */
5289struct state **State_arrayof()
5290{
5291 struct state **array;
mistachkin8e189222015-04-19 21:43:16 +00005292 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005293 if( x3a==0 ) return 0;
mistachkin8e189222015-04-19 21:43:16 +00005294 arrSize = x3a->count;
5295 array = (struct state **)calloc(arrSize, sizeof(struct state *));
drh75897232000-05-29 14:26:00 +00005296 if( array ){
mistachkin8e189222015-04-19 21:43:16 +00005297 for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data;
drh75897232000-05-29 14:26:00 +00005298 }
5299 return array;
5300}
5301
5302/* Hash a configuration */
drh01f75f22013-10-02 20:46:30 +00005303PRIVATE unsigned confighash(struct config *a)
drh75897232000-05-29 14:26:00 +00005304{
drh01f75f22013-10-02 20:46:30 +00005305 unsigned h=0;
drh75897232000-05-29 14:26:00 +00005306 h = h*571 + a->rp->index*37 + a->dot;
5307 return h;
5308}
5309
5310/* There is one instance of the following structure for each
5311** associative array of type "x4".
5312*/
5313struct s_x4 {
5314 int size; /* The number of available slots. */
5315 /* Must be a power of 2 greater than or */
5316 /* equal to 1 */
5317 int count; /* Number of currently slots filled */
5318 struct s_x4node *tbl; /* The data stored here */
5319 struct s_x4node **ht; /* Hash table for lookups */
5320};
5321
5322/* There is one instance of this structure for every data element
5323** in an associative array of type "x4".
5324*/
5325typedef struct s_x4node {
5326 struct config *data; /* The data */
5327 struct s_x4node *next; /* Next entry with the same hash */
5328 struct s_x4node **from; /* Previous link */
5329} x4node;
5330
5331/* There is only one instance of the array, which is the following */
5332static struct s_x4 *x4a;
5333
5334/* Allocate a new associative array */
5335void Configtable_init(){
5336 if( x4a ) return;
5337 x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
5338 if( x4a ){
5339 x4a->size = 64;
5340 x4a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005341 x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*));
drh75897232000-05-29 14:26:00 +00005342 if( x4a->tbl==0 ){
5343 free(x4a);
5344 x4a = 0;
5345 }else{
5346 int i;
5347 x4a->ht = (x4node**)&(x4a->tbl[64]);
5348 for(i=0; i<64; i++) x4a->ht[i] = 0;
5349 }
5350 }
5351}
5352/* Insert a new record into the array. Return TRUE if successful.
5353** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005354int Configtable_insert(struct config *data)
drh75897232000-05-29 14:26:00 +00005355{
5356 x4node *np;
drh01f75f22013-10-02 20:46:30 +00005357 unsigned h;
5358 unsigned ph;
drh75897232000-05-29 14:26:00 +00005359
5360 if( x4a==0 ) return 0;
5361 ph = confighash(data);
5362 h = ph & (x4a->size-1);
5363 np = x4a->ht[h];
5364 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00005365 if( Configcmp((const char *) np->data,(const char *) data)==0 ){
drh75897232000-05-29 14:26:00 +00005366 /* An existing entry with the same key is found. */
5367 /* Fail because overwrite is not allows. */
5368 return 0;
5369 }
5370 np = np->next;
5371 }
5372 if( x4a->count>=x4a->size ){
5373 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005374 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005375 struct s_x4 array;
mistachkin8e189222015-04-19 21:43:16 +00005376 array.size = arrSize = x4a->size*2;
drh75897232000-05-29 14:26:00 +00005377 array.count = x4a->count;
mistachkin8e189222015-04-19 21:43:16 +00005378 array.tbl = (x4node*)calloc(arrSize, sizeof(x4node) + sizeof(x4node*));
drh75897232000-05-29 14:26:00 +00005379 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005380 array.ht = (x4node**)&(array.tbl[arrSize]);
5381 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005382 for(i=0; i<x4a->count; i++){
5383 x4node *oldnp, *newnp;
5384 oldnp = &(x4a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005385 h = confighash(oldnp->data) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005386 newnp = &(array.tbl[i]);
5387 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5388 newnp->next = array.ht[h];
5389 newnp->data = oldnp->data;
5390 newnp->from = &(array.ht[h]);
5391 array.ht[h] = newnp;
5392 }
5393 free(x4a->tbl);
5394 *x4a = array;
5395 }
5396 /* Insert the new data */
5397 h = ph & (x4a->size-1);
5398 np = &(x4a->tbl[x4a->count++]);
5399 np->data = data;
5400 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
5401 np->next = x4a->ht[h];
5402 x4a->ht[h] = np;
5403 np->from = &(x4a->ht[h]);
5404 return 1;
5405}
5406
5407/* Return a pointer to data assigned to the given key. Return NULL
5408** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005409struct config *Configtable_find(struct config *key)
drh75897232000-05-29 14:26:00 +00005410{
5411 int h;
5412 x4node *np;
5413
5414 if( x4a==0 ) return 0;
5415 h = confighash(key) & (x4a->size-1);
5416 np = x4a->ht[h];
5417 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00005418 if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
drh75897232000-05-29 14:26:00 +00005419 np = np->next;
5420 }
5421 return np ? np->data : 0;
5422}
5423
5424/* Remove all data from the table. Pass each data to the function "f"
5425** as it is removed. ("f" may be null to avoid this step.) */
icculus9e44cf12010-02-14 17:14:22 +00005426void Configtable_clear(int(*f)(struct config *))
drh75897232000-05-29 14:26:00 +00005427{
5428 int i;
5429 if( x4a==0 || x4a->count==0 ) return;
5430 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
5431 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
5432 x4a->count = 0;
5433 return;
5434}