blob: d704deb624d42b39ca455db5bc5617e2197d62d9 [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 */
drh75897232000-05-29 14:26:00 +0000289 struct symbol *precsym; /* Precedence symbol for this rule */
290 int index; /* An index number for this rule */
291 Boolean canReduce; /* True if this rule is ever reduced */
292 struct rule *nextlhs; /* Next rule with the same LHS */
293 struct rule *next; /* Next rule in the global list */
294};
295
296/* A configuration is a production rule of the grammar together with
297** a mark (dot) showing how much of that rule has been processed so far.
298** Configurations also contain a follow-set which is a list of terminal
299** symbols which are allowed to immediately follow the end of the rule.
300** Every configuration is recorded as an instance of the following: */
icculus9e44cf12010-02-14 17:14:22 +0000301enum cfgstatus {
302 COMPLETE,
303 INCOMPLETE
304};
drh75897232000-05-29 14:26:00 +0000305struct config {
306 struct rule *rp; /* The rule upon which the configuration is based */
307 int dot; /* The parse point */
308 char *fws; /* Follow-set for this configuration only */
309 struct plink *fplp; /* Follow-set forward propagation links */
310 struct plink *bplp; /* Follow-set backwards propagation links */
311 struct state *stp; /* Pointer to state which contains this */
icculus9e44cf12010-02-14 17:14:22 +0000312 enum cfgstatus status; /* used during followset and shift computations */
drh75897232000-05-29 14:26:00 +0000313 struct config *next; /* Next configuration in the state */
314 struct config *bp; /* The next basis configuration */
315};
316
icculus9e44cf12010-02-14 17:14:22 +0000317enum e_action {
318 SHIFT,
319 ACCEPT,
320 REDUCE,
321 ERROR,
322 SSCONFLICT, /* A shift/shift conflict */
323 SRCONFLICT, /* Was a reduce, but part of a conflict */
324 RRCONFLICT, /* Was a reduce, but part of a conflict */
325 SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
326 RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
drh3bd48ab2015-09-07 18:23:37 +0000327 NOT_USED, /* Deleted by compression */
328 SHIFTREDUCE /* Shift first, then reduce */
icculus9e44cf12010-02-14 17:14:22 +0000329};
330
drh75897232000-05-29 14:26:00 +0000331/* Every shift or reduce operation is stored as one of the following */
332struct action {
333 struct symbol *sp; /* The look-ahead symbol */
icculus9e44cf12010-02-14 17:14:22 +0000334 enum e_action type;
drh75897232000-05-29 14:26:00 +0000335 union {
336 struct state *stp; /* The new state, if a shift */
337 struct rule *rp; /* The rule, if a reduce */
338 } x;
339 struct action *next; /* Next action for this state */
340 struct action *collide; /* Next action with the same hash */
341};
342
343/* Each state of the generated parser's finite state machine
344** is encoded as an instance of the following structure. */
345struct state {
346 struct config *bp; /* The basis configurations for this state */
347 struct config *cfp; /* All configurations in this set */
drh34ff57b2008-07-14 12:27:51 +0000348 int statenum; /* Sequential number for this state */
drh75897232000-05-29 14:26:00 +0000349 struct action *ap; /* Array of actions for this state */
drh8b582012003-10-21 13:16:03 +0000350 int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */
351 int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */
drh3bd48ab2015-09-07 18:23:37 +0000352 int iDfltReduce; /* Default action is to REDUCE by this rule */
353 struct rule *pDfltReduce;/* The default REDUCE rule. */
354 int autoReduce; /* True if this is an auto-reduce state */
drh75897232000-05-29 14:26:00 +0000355};
drh8b582012003-10-21 13:16:03 +0000356#define NO_OFFSET (-2147483647)
drh75897232000-05-29 14:26:00 +0000357
358/* A followset propagation link indicates that the contents of one
359** configuration followset should be propagated to another whenever
360** the first changes. */
361struct plink {
362 struct config *cfp; /* The configuration to which linked */
363 struct plink *next; /* The next propagate link */
364};
365
366/* The state vector for the entire parser generator is recorded as
367** follows. (LEMON uses no global variables and makes little use of
368** static variables. Fields in the following structure can be thought
369** of as begin global variables in the program.) */
370struct lemon {
371 struct state **sorted; /* Table of states sorted by state number */
372 struct rule *rule; /* List of all rules */
373 int nstate; /* Number of states */
drh3bd48ab2015-09-07 18:23:37 +0000374 int nxstate; /* nstate with tail degenerate states removed */
drh75897232000-05-29 14:26:00 +0000375 int nrule; /* Number of rules */
376 int nsymbol; /* Number of terminal and nonterminal symbols */
377 int nterminal; /* Number of terminal symbols */
378 struct symbol **symbols; /* Sorted array of pointers to symbols */
379 int errorcnt; /* Number of errors */
380 struct symbol *errsym; /* The error symbol */
drhe09daa92006-06-10 13:29:31 +0000381 struct symbol *wildcard; /* Token that matches anything */
drh75897232000-05-29 14:26:00 +0000382 char *name; /* Name of the generated parser */
383 char *arg; /* Declaration of the 3th argument to parser */
384 char *tokentype; /* Type of terminal symbols in the parser stack */
drh960e8c62001-04-03 16:53:21 +0000385 char *vartype; /* The default type of non-terminal symbols */
drh75897232000-05-29 14:26:00 +0000386 char *start; /* Name of the start symbol for the grammar */
387 char *stacksize; /* Size of the parser stack */
388 char *include; /* Code to put at the start of the C file */
drh75897232000-05-29 14:26:00 +0000389 char *error; /* Code to execute when an error is seen */
drh75897232000-05-29 14:26:00 +0000390 char *overflow; /* Code to execute on a stack overflow */
drh75897232000-05-29 14:26:00 +0000391 char *failure; /* Code to execute on parser failure */
drh75897232000-05-29 14:26:00 +0000392 char *accept; /* Code to execute when the parser excepts */
drh75897232000-05-29 14:26:00 +0000393 char *extracode; /* Code appended to the generated file */
drh75897232000-05-29 14:26:00 +0000394 char *tokendest; /* Code to execute to destroy token data */
drh960e8c62001-04-03 16:53:21 +0000395 char *vardest; /* Code for the default non-terminal destructor */
drh75897232000-05-29 14:26:00 +0000396 char *filename; /* Name of the input file */
397 char *outname; /* Name of the current output file */
398 char *tokenprefix; /* A prefix added to token names in the .h file */
399 int nconflict; /* Number of parsing conflicts */
drhc75e0162015-09-07 02:23:02 +0000400 int nactiontab; /* Number of entries in the yy_action[] table */
401 int tablesize; /* Total table size of all tables in bytes */
drh75897232000-05-29 14:26:00 +0000402 int basisflag; /* Print only basis configurations */
drh34ff57b2008-07-14 12:27:51 +0000403 int has_fallback; /* True if any %fallback is seen in the grammar */
shane58543932008-12-10 20:10:04 +0000404 int nolinenosflag; /* True if #line statements should not be printed */
drh75897232000-05-29 14:26:00 +0000405 char *argv0; /* Name of the program */
406};
407
408#define MemoryCheck(X) if((X)==0){ \
409 extern void memory_error(); \
410 memory_error(); \
411}
412
413/**************** From the file "table.h" *********************************/
414/*
415** All code in this file has been automatically generated
416** from a specification in the file
417** "table.q"
418** by the associative array code building program "aagen".
419** Do not edit this file! Instead, edit the specification
420** file, then rerun aagen.
421*/
422/*
423** Code for processing tables in the LEMON parser generator.
424*/
drh75897232000-05-29 14:26:00 +0000425/* Routines for handling a strings */
426
icculus9e44cf12010-02-14 17:14:22 +0000427const char *Strsafe(const char *);
drh75897232000-05-29 14:26:00 +0000428
icculus9e44cf12010-02-14 17:14:22 +0000429void Strsafe_init(void);
430int Strsafe_insert(const char *);
431const char *Strsafe_find(const char *);
drh75897232000-05-29 14:26:00 +0000432
433/* Routines for handling symbols of the grammar */
434
icculus9e44cf12010-02-14 17:14:22 +0000435struct symbol *Symbol_new(const char *);
436int Symbolcmpp(const void *, const void *);
437void Symbol_init(void);
438int Symbol_insert(struct symbol *, const char *);
439struct symbol *Symbol_find(const char *);
440struct symbol *Symbol_Nth(int);
441int Symbol_count(void);
442struct symbol **Symbol_arrayof(void);
drh75897232000-05-29 14:26:00 +0000443
444/* Routines to manage the state table */
445
icculus9e44cf12010-02-14 17:14:22 +0000446int Configcmp(const char *, const char *);
447struct state *State_new(void);
448void State_init(void);
449int State_insert(struct state *, struct config *);
450struct state *State_find(struct config *);
drh75897232000-05-29 14:26:00 +0000451struct state **State_arrayof(/* */);
452
453/* Routines used for efficiency in Configlist_add */
454
icculus9e44cf12010-02-14 17:14:22 +0000455void Configtable_init(void);
456int Configtable_insert(struct config *);
457struct config *Configtable_find(struct config *);
458void Configtable_clear(int(*)(struct config *));
459
drh75897232000-05-29 14:26:00 +0000460/****************** From the file "action.c" *******************************/
461/*
462** Routines processing parser actions in the LEMON parser generator.
463*/
464
465/* Allocate a new parser action */
drhe9278182007-07-18 18:16:29 +0000466static struct action *Action_new(void){
drh75897232000-05-29 14:26:00 +0000467 static struct action *freelist = 0;
icculus9e44cf12010-02-14 17:14:22 +0000468 struct action *newaction;
drh75897232000-05-29 14:26:00 +0000469
470 if( freelist==0 ){
471 int i;
472 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +0000473 freelist = (struct action *)calloc(amt, sizeof(struct action));
drh75897232000-05-29 14:26:00 +0000474 if( freelist==0 ){
475 fprintf(stderr,"Unable to allocate memory for a new parser action.");
476 exit(1);
477 }
478 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
479 freelist[amt-1].next = 0;
480 }
icculus9e44cf12010-02-14 17:14:22 +0000481 newaction = freelist;
drh75897232000-05-29 14:26:00 +0000482 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +0000483 return newaction;
drh75897232000-05-29 14:26:00 +0000484}
485
drhe9278182007-07-18 18:16:29 +0000486/* Compare two actions for sorting purposes. Return negative, zero, or
487** positive if the first action is less than, equal to, or greater than
488** the first
489*/
490static int actioncmp(
491 struct action *ap1,
492 struct action *ap2
493){
drh75897232000-05-29 14:26:00 +0000494 int rc;
495 rc = ap1->sp->index - ap2->sp->index;
drh75897232000-05-29 14:26:00 +0000496 if( rc==0 ){
drh9892c5d2007-12-21 00:02:11 +0000497 rc = (int)ap1->type - (int)ap2->type;
498 }
drh3bd48ab2015-09-07 18:23:37 +0000499 if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){
drh75897232000-05-29 14:26:00 +0000500 rc = ap1->x.rp->index - ap2->x.rp->index;
501 }
drhe594bc32009-11-03 13:02:25 +0000502 if( rc==0 ){
icculus7b429aa2010-03-03 17:09:01 +0000503 rc = (int) (ap2 - ap1);
drhe594bc32009-11-03 13:02:25 +0000504 }
drh75897232000-05-29 14:26:00 +0000505 return rc;
506}
507
508/* Sort parser actions */
drhe9278182007-07-18 18:16:29 +0000509static struct action *Action_sort(
510 struct action *ap
511){
512 ap = (struct action *)msort((char *)ap,(char **)&ap->next,
513 (int(*)(const char*,const char*))actioncmp);
drh75897232000-05-29 14:26:00 +0000514 return ap;
515}
516
icculus9e44cf12010-02-14 17:14:22 +0000517void Action_add(
518 struct action **app,
519 enum e_action type,
520 struct symbol *sp,
521 char *arg
522){
523 struct action *newaction;
524 newaction = Action_new();
525 newaction->next = *app;
526 *app = newaction;
527 newaction->type = type;
528 newaction->sp = sp;
drh75897232000-05-29 14:26:00 +0000529 if( type==SHIFT ){
icculus9e44cf12010-02-14 17:14:22 +0000530 newaction->x.stp = (struct state *)arg;
drh75897232000-05-29 14:26:00 +0000531 }else{
icculus9e44cf12010-02-14 17:14:22 +0000532 newaction->x.rp = (struct rule *)arg;
drh75897232000-05-29 14:26:00 +0000533 }
534}
drh8b582012003-10-21 13:16:03 +0000535/********************** New code to implement the "acttab" module ***********/
536/*
537** This module implements routines use to construct the yy_action[] table.
538*/
539
540/*
541** The state of the yy_action table under construction is an instance of
drh8dc3e8f2010-01-07 03:53:03 +0000542** the following structure.
543**
544** The yy_action table maps the pair (state_number, lookahead) into an
545** action_number. The table is an array of integers pairs. The state_number
546** determines an initial offset into the yy_action array. The lookahead
547** value is then added to this initial offset to get an index X into the
548** yy_action array. If the aAction[X].lookahead equals the value of the
549** of the lookahead input, then the value of the action_number output is
550** aAction[X].action. If the lookaheads do not match then the
551** default action for the state_number is returned.
552**
553** All actions associated with a single state_number are first entered
554** into aLookahead[] using multiple calls to acttab_action(). Then the
555** actions for that single state_number are placed into the aAction[]
556** array with a single call to acttab_insert(). The acttab_insert() call
557** also resets the aLookahead[] array in preparation for the next
558** state number.
drh8b582012003-10-21 13:16:03 +0000559*/
icculus9e44cf12010-02-14 17:14:22 +0000560struct lookahead_action {
561 int lookahead; /* Value of the lookahead token */
562 int action; /* Action to take on the given lookahead */
563};
drh8b582012003-10-21 13:16:03 +0000564typedef struct acttab acttab;
565struct acttab {
566 int nAction; /* Number of used slots in aAction[] */
567 int nActionAlloc; /* Slots allocated for aAction[] */
icculus9e44cf12010-02-14 17:14:22 +0000568 struct lookahead_action
569 *aAction, /* The yy_action[] table under construction */
drh8b582012003-10-21 13:16:03 +0000570 *aLookahead; /* A single new transaction set */
571 int mnLookahead; /* Minimum aLookahead[].lookahead */
572 int mnAction; /* Action associated with mnLookahead */
573 int mxLookahead; /* Maximum aLookahead[].lookahead */
574 int nLookahead; /* Used slots in aLookahead[] */
575 int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
576};
577
578/* Return the number of entries in the yy_action table */
579#define acttab_size(X) ((X)->nAction)
580
581/* The value for the N-th entry in yy_action */
582#define acttab_yyaction(X,N) ((X)->aAction[N].action)
583
584/* The value for the N-th entry in yy_lookahead */
585#define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
586
587/* Free all memory associated with the given acttab */
588void acttab_free(acttab *p){
589 free( p->aAction );
590 free( p->aLookahead );
591 free( p );
592}
593
594/* Allocate a new acttab structure */
595acttab *acttab_alloc(void){
icculus9e44cf12010-02-14 17:14:22 +0000596 acttab *p = (acttab *) calloc( 1, sizeof(*p) );
drh8b582012003-10-21 13:16:03 +0000597 if( p==0 ){
598 fprintf(stderr,"Unable to allocate memory for a new acttab.");
599 exit(1);
600 }
601 memset(p, 0, sizeof(*p));
602 return p;
603}
604
drh8dc3e8f2010-01-07 03:53:03 +0000605/* Add a new action to the current transaction set.
606**
607** This routine is called once for each lookahead for a particular
608** state.
drh8b582012003-10-21 13:16:03 +0000609*/
610void acttab_action(acttab *p, int lookahead, int action){
611 if( p->nLookahead>=p->nLookaheadAlloc ){
612 p->nLookaheadAlloc += 25;
icculus9e44cf12010-02-14 17:14:22 +0000613 p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
drh8b582012003-10-21 13:16:03 +0000614 sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
615 if( p->aLookahead==0 ){
616 fprintf(stderr,"malloc failed\n");
617 exit(1);
618 }
619 }
620 if( p->nLookahead==0 ){
621 p->mxLookahead = lookahead;
622 p->mnLookahead = lookahead;
623 p->mnAction = action;
624 }else{
625 if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
626 if( p->mnLookahead>lookahead ){
627 p->mnLookahead = lookahead;
628 p->mnAction = action;
629 }
630 }
631 p->aLookahead[p->nLookahead].lookahead = lookahead;
632 p->aLookahead[p->nLookahead].action = action;
633 p->nLookahead++;
634}
635
636/*
637** Add the transaction set built up with prior calls to acttab_action()
638** into the current action table. Then reset the transaction set back
639** to an empty set in preparation for a new round of acttab_action() calls.
640**
641** Return the offset into the action table of the new transaction.
642*/
643int acttab_insert(acttab *p){
644 int i, j, k, n;
645 assert( p->nLookahead>0 );
646
647 /* Make sure we have enough space to hold the expanded action table
648 ** in the worst case. The worst case occurs if the transaction set
649 ** must be appended to the current action table
650 */
drh784d86f2004-02-19 18:41:53 +0000651 n = p->mxLookahead + 1;
drh8dc3e8f2010-01-07 03:53:03 +0000652 if( p->nAction + n >= p->nActionAlloc ){
drhfdbf9282003-10-21 16:34:41 +0000653 int oldAlloc = p->nActionAlloc;
drh8b582012003-10-21 13:16:03 +0000654 p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
icculus9e44cf12010-02-14 17:14:22 +0000655 p->aAction = (struct lookahead_action *) realloc( p->aAction,
drh8b582012003-10-21 13:16:03 +0000656 sizeof(p->aAction[0])*p->nActionAlloc);
657 if( p->aAction==0 ){
658 fprintf(stderr,"malloc failed\n");
659 exit(1);
660 }
drhfdbf9282003-10-21 16:34:41 +0000661 for(i=oldAlloc; i<p->nActionAlloc; i++){
drh8b582012003-10-21 13:16:03 +0000662 p->aAction[i].lookahead = -1;
663 p->aAction[i].action = -1;
664 }
665 }
666
drh8dc3e8f2010-01-07 03:53:03 +0000667 /* Scan the existing action table looking for an offset that is a
668 ** duplicate of the current transaction set. Fall out of the loop
669 ** if and when the duplicate is found.
drh8b582012003-10-21 13:16:03 +0000670 **
671 ** i is the index in p->aAction[] where p->mnLookahead is inserted.
672 */
drh8dc3e8f2010-01-07 03:53:03 +0000673 for(i=p->nAction-1; i>=0; i--){
drhf16371d2009-11-03 19:18:31 +0000674 if( p->aAction[i].lookahead==p->mnLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000675 /* All lookaheads and actions in the aLookahead[] transaction
676 ** must match against the candidate aAction[i] entry. */
drh8b582012003-10-21 13:16:03 +0000677 if( p->aAction[i].action!=p->mnAction ) continue;
678 for(j=0; j<p->nLookahead; j++){
679 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
680 if( k<0 || k>=p->nAction ) break;
681 if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
682 if( p->aLookahead[j].action!=p->aAction[k].action ) break;
683 }
684 if( j<p->nLookahead ) continue;
drh8dc3e8f2010-01-07 03:53:03 +0000685
686 /* No possible lookahead value that is not in the aLookahead[]
687 ** transaction is allowed to match aAction[i] */
drh8b582012003-10-21 13:16:03 +0000688 n = 0;
689 for(j=0; j<p->nAction; j++){
drhfdbf9282003-10-21 16:34:41 +0000690 if( p->aAction[j].lookahead<0 ) continue;
691 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
drh8b582012003-10-21 13:16:03 +0000692 }
drhfdbf9282003-10-21 16:34:41 +0000693 if( n==p->nLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000694 break; /* An exact match is found at offset i */
drhfdbf9282003-10-21 16:34:41 +0000695 }
drh8b582012003-10-21 13:16:03 +0000696 }
697 }
drh8dc3e8f2010-01-07 03:53:03 +0000698
699 /* If no existing offsets exactly match the current transaction, find an
700 ** an empty offset in the aAction[] table in which we can add the
701 ** aLookahead[] transaction.
702 */
drhf16371d2009-11-03 19:18:31 +0000703 if( i<0 ){
drh8dc3e8f2010-01-07 03:53:03 +0000704 /* Look for holes in the aAction[] table that fit the current
705 ** aLookahead[] transaction. Leave i set to the offset of the hole.
706 ** If no holes are found, i is left at p->nAction, which means the
707 ** transaction will be appended. */
708 for(i=0; i<p->nActionAlloc - p->mxLookahead; i++){
drhf16371d2009-11-03 19:18:31 +0000709 if( p->aAction[i].lookahead<0 ){
710 for(j=0; j<p->nLookahead; j++){
711 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
712 if( k<0 ) break;
713 if( p->aAction[k].lookahead>=0 ) break;
714 }
715 if( j<p->nLookahead ) continue;
716 for(j=0; j<p->nAction; j++){
717 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
718 }
719 if( j==p->nAction ){
720 break; /* Fits in empty slots */
721 }
722 }
723 }
724 }
drh8b582012003-10-21 13:16:03 +0000725 /* Insert transaction set at index i. */
726 for(j=0; j<p->nLookahead; j++){
727 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
728 p->aAction[k] = p->aLookahead[j];
729 if( k>=p->nAction ) p->nAction = k+1;
730 }
731 p->nLookahead = 0;
732
733 /* Return the offset that is added to the lookahead in order to get the
734 ** index into yy_action of the action */
735 return i - p->mnLookahead;
736}
737
drh75897232000-05-29 14:26:00 +0000738/********************** From the file "build.c" *****************************/
739/*
740** Routines to construction the finite state machine for the LEMON
741** parser generator.
742*/
743
744/* Find a precedence symbol of every rule in the grammar.
745**
746** Those rules which have a precedence symbol coded in the input
747** grammar using the "[symbol]" construct will already have the
748** rp->precsym field filled. Other rules take as their precedence
749** symbol the first RHS symbol with a defined precedence. If there
750** are not RHS symbols with a defined precedence, the precedence
751** symbol field is left blank.
752*/
icculus9e44cf12010-02-14 17:14:22 +0000753void FindRulePrecedences(struct lemon *xp)
drh75897232000-05-29 14:26:00 +0000754{
755 struct rule *rp;
756 for(rp=xp->rule; rp; rp=rp->next){
757 if( rp->precsym==0 ){
drhfd405312005-11-06 04:06:59 +0000758 int i, j;
759 for(i=0; i<rp->nrhs && rp->precsym==0; i++){
760 struct symbol *sp = rp->rhs[i];
761 if( sp->type==MULTITERMINAL ){
762 for(j=0; j<sp->nsubsym; j++){
763 if( sp->subsym[j]->prec>=0 ){
764 rp->precsym = sp->subsym[j];
765 break;
766 }
767 }
768 }else if( sp->prec>=0 ){
drh75897232000-05-29 14:26:00 +0000769 rp->precsym = rp->rhs[i];
drhf2f105d2012-08-20 15:53:54 +0000770 }
drh75897232000-05-29 14:26:00 +0000771 }
772 }
773 }
774 return;
775}
776
777/* Find all nonterminals which will generate the empty string.
778** Then go back and compute the first sets of every nonterminal.
779** The first set is the set of all terminal symbols which can begin
780** a string generated by that nonterminal.
781*/
icculus9e44cf12010-02-14 17:14:22 +0000782void FindFirstSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000783{
drhfd405312005-11-06 04:06:59 +0000784 int i, j;
drh75897232000-05-29 14:26:00 +0000785 struct rule *rp;
786 int progress;
787
788 for(i=0; i<lemp->nsymbol; i++){
drhaa9f1122007-08-23 02:50:56 +0000789 lemp->symbols[i]->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +0000790 }
791 for(i=lemp->nterminal; i<lemp->nsymbol; i++){
792 lemp->symbols[i]->firstset = SetNew();
793 }
794
795 /* First compute all lambdas */
796 do{
797 progress = 0;
798 for(rp=lemp->rule; rp; rp=rp->next){
799 if( rp->lhs->lambda ) continue;
800 for(i=0; i<rp->nrhs; i++){
drh7dd1ac62012-01-07 15:17:18 +0000801 struct symbol *sp = rp->rhs[i];
802 assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE );
803 if( sp->lambda==LEMON_FALSE ) break;
drh75897232000-05-29 14:26:00 +0000804 }
805 if( i==rp->nrhs ){
drhaa9f1122007-08-23 02:50:56 +0000806 rp->lhs->lambda = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +0000807 progress = 1;
808 }
809 }
810 }while( progress );
811
812 /* Now compute all first sets */
813 do{
814 struct symbol *s1, *s2;
815 progress = 0;
816 for(rp=lemp->rule; rp; rp=rp->next){
817 s1 = rp->lhs;
818 for(i=0; i<rp->nrhs; i++){
819 s2 = rp->rhs[i];
820 if( s2->type==TERMINAL ){
821 progress += SetAdd(s1->firstset,s2->index);
822 break;
drhfd405312005-11-06 04:06:59 +0000823 }else if( s2->type==MULTITERMINAL ){
824 for(j=0; j<s2->nsubsym; j++){
825 progress += SetAdd(s1->firstset,s2->subsym[j]->index);
826 }
827 break;
drhf2f105d2012-08-20 15:53:54 +0000828 }else if( s1==s2 ){
drhaa9f1122007-08-23 02:50:56 +0000829 if( s1->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000830 }else{
drh75897232000-05-29 14:26:00 +0000831 progress += SetUnion(s1->firstset,s2->firstset);
drhaa9f1122007-08-23 02:50:56 +0000832 if( s2->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000833 }
drh75897232000-05-29 14:26:00 +0000834 }
835 }
836 }while( progress );
837 return;
838}
839
840/* Compute all LR(0) states for the grammar. Links
841** are added to between some states so that the LR(1) follow sets
842** can be computed later.
843*/
icculus9e44cf12010-02-14 17:14:22 +0000844PRIVATE struct state *getstate(struct lemon *); /* forward reference */
845void FindStates(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000846{
847 struct symbol *sp;
848 struct rule *rp;
849
850 Configlist_init();
851
852 /* Find the start symbol */
853 if( lemp->start ){
854 sp = Symbol_find(lemp->start);
855 if( sp==0 ){
856 ErrorMsg(lemp->filename,0,
857"The specified start symbol \"%s\" is not \
858in a nonterminal of the grammar. \"%s\" will be used as the start \
859symbol instead.",lemp->start,lemp->rule->lhs->name);
860 lemp->errorcnt++;
861 sp = lemp->rule->lhs;
862 }
863 }else{
864 sp = lemp->rule->lhs;
865 }
866
867 /* Make sure the start symbol doesn't occur on the right-hand side of
868 ** any rule. Report an error if it does. (YACC would generate a new
869 ** start symbol in this case.) */
870 for(rp=lemp->rule; rp; rp=rp->next){
871 int i;
872 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +0000873 if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */
drh75897232000-05-29 14:26:00 +0000874 ErrorMsg(lemp->filename,0,
875"The start symbol \"%s\" occurs on the \
876right-hand side of a rule. This will result in a parser which \
877does not work properly.",sp->name);
878 lemp->errorcnt++;
879 }
880 }
881 }
882
883 /* The basis configuration set for the first state
884 ** is all rules which have the start symbol as their
885 ** left-hand side */
886 for(rp=sp->rule; rp; rp=rp->nextlhs){
887 struct config *newcfp;
drhb4960992007-10-05 16:16:36 +0000888 rp->lhsStart = 1;
drh75897232000-05-29 14:26:00 +0000889 newcfp = Configlist_addbasis(rp,0);
890 SetAdd(newcfp->fws,0);
891 }
892
893 /* Compute the first state. All other states will be
894 ** computed automatically during the computation of the first one.
895 ** The returned pointer to the first state is not used. */
896 (void)getstate(lemp);
897 return;
898}
899
900/* Return a pointer to a state which is described by the configuration
901** list which has been built from calls to Configlist_add.
902*/
icculus9e44cf12010-02-14 17:14:22 +0000903PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
904PRIVATE struct state *getstate(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000905{
906 struct config *cfp, *bp;
907 struct state *stp;
908
909 /* Extract the sorted basis of the new state. The basis was constructed
910 ** by prior calls to "Configlist_addbasis()". */
911 Configlist_sortbasis();
912 bp = Configlist_basis();
913
914 /* Get a state with the same basis */
915 stp = State_find(bp);
916 if( stp ){
917 /* A state with the same basis already exists! Copy all the follow-set
918 ** propagation links from the state under construction into the
919 ** preexisting state, then return a pointer to the preexisting state */
920 struct config *x, *y;
921 for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
922 Plink_copy(&y->bplp,x->bplp);
923 Plink_delete(x->fplp);
924 x->fplp = x->bplp = 0;
925 }
926 cfp = Configlist_return();
927 Configlist_eat(cfp);
928 }else{
929 /* This really is a new state. Construct all the details */
930 Configlist_closure(lemp); /* Compute the configuration closure */
931 Configlist_sort(); /* Sort the configuration closure */
932 cfp = Configlist_return(); /* Get a pointer to the config list */
933 stp = State_new(); /* A new state structure */
934 MemoryCheck(stp);
935 stp->bp = bp; /* Remember the configuration basis */
936 stp->cfp = cfp; /* Remember the configuration closure */
drhada354d2005-11-05 15:03:59 +0000937 stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
drh75897232000-05-29 14:26:00 +0000938 stp->ap = 0; /* No actions, yet. */
939 State_insert(stp,stp->bp); /* Add to the state table */
940 buildshifts(lemp,stp); /* Recursively compute successor states */
941 }
942 return stp;
943}
944
drhfd405312005-11-06 04:06:59 +0000945/*
946** Return true if two symbols are the same.
947*/
icculus9e44cf12010-02-14 17:14:22 +0000948int same_symbol(struct symbol *a, struct symbol *b)
drhfd405312005-11-06 04:06:59 +0000949{
950 int i;
951 if( a==b ) return 1;
952 if( a->type!=MULTITERMINAL ) return 0;
953 if( b->type!=MULTITERMINAL ) return 0;
954 if( a->nsubsym!=b->nsubsym ) return 0;
955 for(i=0; i<a->nsubsym; i++){
956 if( a->subsym[i]!=b->subsym[i] ) return 0;
957 }
958 return 1;
959}
960
drh75897232000-05-29 14:26:00 +0000961/* Construct all successor states to the given state. A "successor"
962** state is any state which can be reached by a shift action.
963*/
icculus9e44cf12010-02-14 17:14:22 +0000964PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
drh75897232000-05-29 14:26:00 +0000965{
966 struct config *cfp; /* For looping thru the config closure of "stp" */
967 struct config *bcfp; /* For the inner loop on config closure of "stp" */
icculus9e44cf12010-02-14 17:14:22 +0000968 struct config *newcfg; /* */
drh75897232000-05-29 14:26:00 +0000969 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
970 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
971 struct state *newstp; /* A pointer to a successor state */
972
973 /* Each configuration becomes complete after it contibutes to a successor
974 ** state. Initially, all configurations are incomplete */
975 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
976
977 /* Loop through all configurations of the state "stp" */
978 for(cfp=stp->cfp; cfp; cfp=cfp->next){
979 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
980 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
981 Configlist_reset(); /* Reset the new config set */
982 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
983
984 /* For every configuration in the state "stp" which has the symbol "sp"
985 ** following its dot, add the same configuration to the basis set under
986 ** construction but with the dot shifted one symbol to the right. */
987 for(bcfp=cfp; bcfp; bcfp=bcfp->next){
988 if( bcfp->status==COMPLETE ) continue; /* Already used */
989 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
990 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
drhfd405312005-11-06 04:06:59 +0000991 if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
drh75897232000-05-29 14:26:00 +0000992 bcfp->status = COMPLETE; /* Mark this config as used */
icculus9e44cf12010-02-14 17:14:22 +0000993 newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
994 Plink_add(&newcfg->bplp,bcfp);
drh75897232000-05-29 14:26:00 +0000995 }
996
997 /* Get a pointer to the state described by the basis configuration set
998 ** constructed in the preceding loop */
999 newstp = getstate(lemp);
1000
1001 /* The state "newstp" is reached from the state "stp" by a shift action
1002 ** on the symbol "sp" */
drhfd405312005-11-06 04:06:59 +00001003 if( sp->type==MULTITERMINAL ){
1004 int i;
1005 for(i=0; i<sp->nsubsym; i++){
1006 Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
1007 }
1008 }else{
1009 Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
1010 }
drh75897232000-05-29 14:26:00 +00001011 }
1012}
1013
1014/*
1015** Construct the propagation links
1016*/
icculus9e44cf12010-02-14 17:14:22 +00001017void FindLinks(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001018{
1019 int i;
1020 struct config *cfp, *other;
1021 struct state *stp;
1022 struct plink *plp;
1023
1024 /* Housekeeping detail:
1025 ** Add to every propagate link a pointer back to the state to
1026 ** which the link is attached. */
1027 for(i=0; i<lemp->nstate; i++){
1028 stp = lemp->sorted[i];
1029 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1030 cfp->stp = stp;
1031 }
1032 }
1033
1034 /* Convert all backlinks into forward links. Only the forward
1035 ** links are used in the follow-set computation. */
1036 for(i=0; i<lemp->nstate; i++){
1037 stp = lemp->sorted[i];
1038 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1039 for(plp=cfp->bplp; plp; plp=plp->next){
1040 other = plp->cfp;
1041 Plink_add(&other->fplp,cfp);
1042 }
1043 }
1044 }
1045}
1046
1047/* Compute all followsets.
1048**
1049** A followset is the set of all symbols which can come immediately
1050** after a configuration.
1051*/
icculus9e44cf12010-02-14 17:14:22 +00001052void FindFollowSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001053{
1054 int i;
1055 struct config *cfp;
1056 struct plink *plp;
1057 int progress;
1058 int change;
1059
1060 for(i=0; i<lemp->nstate; i++){
1061 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1062 cfp->status = INCOMPLETE;
1063 }
1064 }
1065
1066 do{
1067 progress = 0;
1068 for(i=0; i<lemp->nstate; i++){
1069 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1070 if( cfp->status==COMPLETE ) continue;
1071 for(plp=cfp->fplp; plp; plp=plp->next){
1072 change = SetUnion(plp->cfp->fws,cfp->fws);
1073 if( change ){
1074 plp->cfp->status = INCOMPLETE;
1075 progress = 1;
drhf2f105d2012-08-20 15:53:54 +00001076 }
1077 }
drh75897232000-05-29 14:26:00 +00001078 cfp->status = COMPLETE;
1079 }
1080 }
1081 }while( progress );
1082}
1083
drh3cb2f6e2012-01-09 14:19:05 +00001084static int resolve_conflict(struct action *,struct action *);
drh75897232000-05-29 14:26:00 +00001085
1086/* Compute the reduce actions, and resolve conflicts.
1087*/
icculus9e44cf12010-02-14 17:14:22 +00001088void FindActions(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001089{
1090 int i,j;
1091 struct config *cfp;
1092 struct state *stp;
1093 struct symbol *sp;
1094 struct rule *rp;
1095
1096 /* Add all of the reduce actions
1097 ** A reduce action is added for each element of the followset of
1098 ** a configuration which has its dot at the extreme right.
1099 */
1100 for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
1101 stp = lemp->sorted[i];
1102 for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
1103 if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
1104 for(j=0; j<lemp->nterminal; j++){
1105 if( SetFind(cfp->fws,j) ){
1106 /* Add a reduce action to the state "stp" which will reduce by the
1107 ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
drh218dc692004-05-31 23:13:45 +00001108 Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
drh75897232000-05-29 14:26:00 +00001109 }
drhf2f105d2012-08-20 15:53:54 +00001110 }
drh75897232000-05-29 14:26:00 +00001111 }
1112 }
1113 }
1114
1115 /* Add the accepting token */
1116 if( lemp->start ){
1117 sp = Symbol_find(lemp->start);
1118 if( sp==0 ) sp = lemp->rule->lhs;
1119 }else{
1120 sp = lemp->rule->lhs;
1121 }
1122 /* Add to the first state (which is always the starting state of the
1123 ** finite state machine) an action to ACCEPT if the lookahead is the
1124 ** start nonterminal. */
1125 Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
1126
1127 /* Resolve conflicts */
1128 for(i=0; i<lemp->nstate; i++){
1129 struct action *ap, *nap;
drh75897232000-05-29 14:26:00 +00001130 stp = lemp->sorted[i];
drhe9278182007-07-18 18:16:29 +00001131 /* assert( stp->ap ); */
drh75897232000-05-29 14:26:00 +00001132 stp->ap = Action_sort(stp->ap);
drhb59499c2002-02-23 18:45:13 +00001133 for(ap=stp->ap; ap && ap->next; ap=ap->next){
drh75897232000-05-29 14:26:00 +00001134 for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
1135 /* The two actions "ap" and "nap" have the same lookahead.
1136 ** Figure out which one should be used */
drh3cb2f6e2012-01-09 14:19:05 +00001137 lemp->nconflict += resolve_conflict(ap,nap);
drh75897232000-05-29 14:26:00 +00001138 }
1139 }
1140 }
1141
1142 /* Report an error for each rule that can never be reduced. */
drhaa9f1122007-08-23 02:50:56 +00001143 for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00001144 for(i=0; i<lemp->nstate; i++){
1145 struct action *ap;
1146 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
drhaa9f1122007-08-23 02:50:56 +00001147 if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +00001148 }
1149 }
1150 for(rp=lemp->rule; rp; rp=rp->next){
1151 if( rp->canReduce ) continue;
1152 ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
1153 lemp->errorcnt++;
1154 }
1155}
1156
1157/* Resolve a conflict between the two given actions. If the
drh34ff57b2008-07-14 12:27:51 +00001158** conflict can't be resolved, return non-zero.
drh75897232000-05-29 14:26:00 +00001159**
1160** NO LONGER TRUE:
1161** To resolve a conflict, first look to see if either action
1162** is on an error rule. In that case, take the action which
1163** is not associated with the error rule. If neither or both
1164** actions are associated with an error rule, then try to
1165** use precedence to resolve the conflict.
1166**
1167** If either action is a SHIFT, then it must be apx. This
1168** function won't work if apx->type==REDUCE and apy->type==SHIFT.
1169*/
icculus9e44cf12010-02-14 17:14:22 +00001170static int resolve_conflict(
1171 struct action *apx,
drh3cb2f6e2012-01-09 14:19:05 +00001172 struct action *apy
icculus9e44cf12010-02-14 17:14:22 +00001173){
drh75897232000-05-29 14:26:00 +00001174 struct symbol *spx, *spy;
1175 int errcnt = 0;
1176 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
drhf0fa1c12006-12-14 01:06:22 +00001177 if( apx->type==SHIFT && apy->type==SHIFT ){
drh9892c5d2007-12-21 00:02:11 +00001178 apy->type = SSCONFLICT;
drhf0fa1c12006-12-14 01:06:22 +00001179 errcnt++;
1180 }
drh75897232000-05-29 14:26:00 +00001181 if( apx->type==SHIFT && apy->type==REDUCE ){
1182 spx = apx->sp;
1183 spy = apy->x.rp->precsym;
1184 if( spy==0 || spx->prec<0 || spy->prec<0 ){
1185 /* Not enough precedence information. */
drh9892c5d2007-12-21 00:02:11 +00001186 apy->type = SRCONFLICT;
drh75897232000-05-29 14:26:00 +00001187 errcnt++;
drhdd7e9db2010-07-19 01:52:07 +00001188 }else if( spx->prec>spy->prec ){ /* higher precedence wins */
drh75897232000-05-29 14:26:00 +00001189 apy->type = RD_RESOLVED;
1190 }else if( spx->prec<spy->prec ){
1191 apx->type = SH_RESOLVED;
1192 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
1193 apy->type = RD_RESOLVED; /* associativity */
1194 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
1195 apx->type = SH_RESOLVED;
1196 }else{
1197 assert( spx->prec==spy->prec && spx->assoc==NONE );
drh62a223e2014-06-09 13:11:40 +00001198 apx->type = ERROR;
drh75897232000-05-29 14:26:00 +00001199 }
1200 }else if( apx->type==REDUCE && apy->type==REDUCE ){
1201 spx = apx->x.rp->precsym;
1202 spy = apy->x.rp->precsym;
1203 if( spx==0 || spy==0 || spx->prec<0 ||
1204 spy->prec<0 || spx->prec==spy->prec ){
drh9892c5d2007-12-21 00:02:11 +00001205 apy->type = RRCONFLICT;
drh75897232000-05-29 14:26:00 +00001206 errcnt++;
1207 }else if( spx->prec>spy->prec ){
1208 apy->type = RD_RESOLVED;
1209 }else if( spx->prec<spy->prec ){
1210 apx->type = RD_RESOLVED;
1211 }
1212 }else{
drhb59499c2002-02-23 18:45:13 +00001213 assert(
1214 apx->type==SH_RESOLVED ||
1215 apx->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001216 apx->type==SSCONFLICT ||
1217 apx->type==SRCONFLICT ||
1218 apx->type==RRCONFLICT ||
drhb59499c2002-02-23 18:45:13 +00001219 apy->type==SH_RESOLVED ||
1220 apy->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001221 apy->type==SSCONFLICT ||
1222 apy->type==SRCONFLICT ||
1223 apy->type==RRCONFLICT
drhb59499c2002-02-23 18:45:13 +00001224 );
1225 /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
1226 ** REDUCEs on the list. If we reach this point it must be because
1227 ** the parser conflict had already been resolved. */
drh75897232000-05-29 14:26:00 +00001228 }
1229 return errcnt;
1230}
1231/********************* From the file "configlist.c" *************************/
1232/*
1233** Routines to processing a configuration list and building a state
1234** in the LEMON parser generator.
1235*/
1236
1237static struct config *freelist = 0; /* List of free configurations */
1238static struct config *current = 0; /* Top of list of configurations */
1239static struct config **currentend = 0; /* Last on list of configs */
1240static struct config *basis = 0; /* Top of list of basis configs */
1241static struct config **basisend = 0; /* End of list of basis configs */
1242
1243/* Return a pointer to a new configuration */
1244PRIVATE struct config *newconfig(){
icculus9e44cf12010-02-14 17:14:22 +00001245 struct config *newcfg;
drh75897232000-05-29 14:26:00 +00001246 if( freelist==0 ){
1247 int i;
1248 int amt = 3;
drh9892c5d2007-12-21 00:02:11 +00001249 freelist = (struct config *)calloc( amt, sizeof(struct config) );
drh75897232000-05-29 14:26:00 +00001250 if( freelist==0 ){
1251 fprintf(stderr,"Unable to allocate memory for a new configuration.");
1252 exit(1);
1253 }
1254 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
1255 freelist[amt-1].next = 0;
1256 }
icculus9e44cf12010-02-14 17:14:22 +00001257 newcfg = freelist;
drh75897232000-05-29 14:26:00 +00001258 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00001259 return newcfg;
drh75897232000-05-29 14:26:00 +00001260}
1261
1262/* The configuration "old" is no longer used */
icculus9e44cf12010-02-14 17:14:22 +00001263PRIVATE void deleteconfig(struct config *old)
drh75897232000-05-29 14:26:00 +00001264{
1265 old->next = freelist;
1266 freelist = old;
1267}
1268
1269/* Initialized the configuration list builder */
1270void Configlist_init(){
1271 current = 0;
1272 currentend = &current;
1273 basis = 0;
1274 basisend = &basis;
1275 Configtable_init();
1276 return;
1277}
1278
1279/* Initialized the configuration list builder */
1280void Configlist_reset(){
1281 current = 0;
1282 currentend = &current;
1283 basis = 0;
1284 basisend = &basis;
1285 Configtable_clear(0);
1286 return;
1287}
1288
1289/* Add another configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001290struct config *Configlist_add(
1291 struct rule *rp, /* The rule */
1292 int dot /* Index into the RHS of the rule where the dot goes */
1293){
drh75897232000-05-29 14:26:00 +00001294 struct config *cfp, model;
1295
1296 assert( currentend!=0 );
1297 model.rp = rp;
1298 model.dot = dot;
1299 cfp = Configtable_find(&model);
1300 if( cfp==0 ){
1301 cfp = newconfig();
1302 cfp->rp = rp;
1303 cfp->dot = dot;
1304 cfp->fws = SetNew();
1305 cfp->stp = 0;
1306 cfp->fplp = cfp->bplp = 0;
1307 cfp->next = 0;
1308 cfp->bp = 0;
1309 *currentend = cfp;
1310 currentend = &cfp->next;
1311 Configtable_insert(cfp);
1312 }
1313 return cfp;
1314}
1315
1316/* Add a basis configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001317struct config *Configlist_addbasis(struct rule *rp, int dot)
drh75897232000-05-29 14:26:00 +00001318{
1319 struct config *cfp, model;
1320
1321 assert( basisend!=0 );
1322 assert( currentend!=0 );
1323 model.rp = rp;
1324 model.dot = dot;
1325 cfp = Configtable_find(&model);
1326 if( cfp==0 ){
1327 cfp = newconfig();
1328 cfp->rp = rp;
1329 cfp->dot = dot;
1330 cfp->fws = SetNew();
1331 cfp->stp = 0;
1332 cfp->fplp = cfp->bplp = 0;
1333 cfp->next = 0;
1334 cfp->bp = 0;
1335 *currentend = cfp;
1336 currentend = &cfp->next;
1337 *basisend = cfp;
1338 basisend = &cfp->bp;
1339 Configtable_insert(cfp);
1340 }
1341 return cfp;
1342}
1343
1344/* Compute the closure of the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001345void Configlist_closure(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001346{
1347 struct config *cfp, *newcfp;
1348 struct rule *rp, *newrp;
1349 struct symbol *sp, *xsp;
1350 int i, dot;
1351
1352 assert( currentend!=0 );
1353 for(cfp=current; cfp; cfp=cfp->next){
1354 rp = cfp->rp;
1355 dot = cfp->dot;
1356 if( dot>=rp->nrhs ) continue;
1357 sp = rp->rhs[dot];
1358 if( sp->type==NONTERMINAL ){
1359 if( sp->rule==0 && sp!=lemp->errsym ){
1360 ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
1361 sp->name);
1362 lemp->errorcnt++;
1363 }
1364 for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
1365 newcfp = Configlist_add(newrp,0);
1366 for(i=dot+1; i<rp->nrhs; i++){
1367 xsp = rp->rhs[i];
1368 if( xsp->type==TERMINAL ){
1369 SetAdd(newcfp->fws,xsp->index);
1370 break;
drhfd405312005-11-06 04:06:59 +00001371 }else if( xsp->type==MULTITERMINAL ){
1372 int k;
1373 for(k=0; k<xsp->nsubsym; k++){
1374 SetAdd(newcfp->fws, xsp->subsym[k]->index);
1375 }
1376 break;
drhf2f105d2012-08-20 15:53:54 +00001377 }else{
drh75897232000-05-29 14:26:00 +00001378 SetUnion(newcfp->fws,xsp->firstset);
drhaa9f1122007-08-23 02:50:56 +00001379 if( xsp->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +00001380 }
1381 }
drh75897232000-05-29 14:26:00 +00001382 if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
1383 }
1384 }
1385 }
1386 return;
1387}
1388
1389/* Sort the configuration list */
1390void Configlist_sort(){
drh25473362015-09-04 18:03:45 +00001391 current = (struct config*)msort((char*)current,(char**)&(current->next),
1392 Configcmp);
drh75897232000-05-29 14:26:00 +00001393 currentend = 0;
1394 return;
1395}
1396
1397/* Sort the basis configuration list */
1398void Configlist_sortbasis(){
drh25473362015-09-04 18:03:45 +00001399 basis = (struct config*)msort((char*)current,(char**)&(current->bp),
1400 Configcmp);
drh75897232000-05-29 14:26:00 +00001401 basisend = 0;
1402 return;
1403}
1404
1405/* Return a pointer to the head of the configuration list and
1406** reset the list */
1407struct config *Configlist_return(){
1408 struct config *old;
1409 old = current;
1410 current = 0;
1411 currentend = 0;
1412 return old;
1413}
1414
1415/* Return a pointer to the head of the configuration list and
1416** reset the list */
1417struct config *Configlist_basis(){
1418 struct config *old;
1419 old = basis;
1420 basis = 0;
1421 basisend = 0;
1422 return old;
1423}
1424
1425/* Free all elements of the given configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001426void Configlist_eat(struct config *cfp)
drh75897232000-05-29 14:26:00 +00001427{
1428 struct config *nextcfp;
1429 for(; cfp; cfp=nextcfp){
1430 nextcfp = cfp->next;
1431 assert( cfp->fplp==0 );
1432 assert( cfp->bplp==0 );
1433 if( cfp->fws ) SetFree(cfp->fws);
1434 deleteconfig(cfp);
1435 }
1436 return;
1437}
1438/***************** From the file "error.c" *********************************/
1439/*
1440** Code for printing error message.
1441*/
1442
drhf9a2e7b2003-04-15 01:49:48 +00001443void ErrorMsg(const char *filename, int lineno, const char *format, ...){
icculus15a2cec2010-02-16 16:07:28 +00001444 va_list ap;
icculus1c11f742010-02-15 00:01:04 +00001445 fprintf(stderr, "%s:%d: ", filename, lineno);
1446 va_start(ap, format);
1447 vfprintf(stderr,format,ap);
1448 va_end(ap);
1449 fprintf(stderr, "\n");
drh75897232000-05-29 14:26:00 +00001450}
1451/**************** From the file "main.c" ************************************/
1452/*
1453** Main program file for the LEMON parser generator.
1454*/
1455
1456/* Report an out-of-memory condition and abort. This function
1457** is used mostly by the "MemoryCheck" macro in struct.h
1458*/
1459void memory_error(){
1460 fprintf(stderr,"Out of memory. Aborting...\n");
1461 exit(1);
1462}
1463
drh6d08b4d2004-07-20 12:45:22 +00001464static int nDefine = 0; /* Number of -D options on the command line */
1465static char **azDefine = 0; /* Name of the -D macros */
1466
1467/* This routine is called with the argument to each -D command-line option.
1468** Add the macro defined to the azDefine array.
1469*/
1470static void handle_D_option(char *z){
1471 char **paz;
1472 nDefine++;
icculus9e44cf12010-02-14 17:14:22 +00001473 azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine);
drh6d08b4d2004-07-20 12:45:22 +00001474 if( azDefine==0 ){
1475 fprintf(stderr,"out of memory\n");
1476 exit(1);
1477 }
1478 paz = &azDefine[nDefine-1];
icculus9e44cf12010-02-14 17:14:22 +00001479 *paz = (char *) malloc( lemonStrlen(z)+1 );
drh6d08b4d2004-07-20 12:45:22 +00001480 if( *paz==0 ){
1481 fprintf(stderr,"out of memory\n");
1482 exit(1);
1483 }
drh898799f2014-01-10 23:21:00 +00001484 lemon_strcpy(*paz, z);
drh6d08b4d2004-07-20 12:45:22 +00001485 for(z=*paz; *z && *z!='='; z++){}
1486 *z = 0;
1487}
1488
icculus3e143bd2010-02-14 00:48:49 +00001489static char *user_templatename = NULL;
1490static void handle_T_option(char *z){
icculus9e44cf12010-02-14 17:14:22 +00001491 user_templatename = (char *) malloc( lemonStrlen(z)+1 );
icculus3e143bd2010-02-14 00:48:49 +00001492 if( user_templatename==0 ){
1493 memory_error();
1494 }
drh898799f2014-01-10 23:21:00 +00001495 lemon_strcpy(user_templatename, z);
icculus3e143bd2010-02-14 00:48:49 +00001496}
drh75897232000-05-29 14:26:00 +00001497
drhc75e0162015-09-07 02:23:02 +00001498/* forward reference */
1499static const char *minimum_size_type(int lwr, int upr, int *pnByte);
1500
1501/* Print a single line of the "Parser Stats" output
1502*/
1503static void stats_line(const char *zLabel, int iValue){
1504 int nLabel = lemonStrlen(zLabel);
1505 printf(" %s%.*s %5d\n", zLabel,
1506 35-nLabel, "................................",
1507 iValue);
1508}
1509
drh75897232000-05-29 14:26:00 +00001510/* The main program. Parse the command line and do it... */
icculus9e44cf12010-02-14 17:14:22 +00001511int main(int argc, char **argv)
drh75897232000-05-29 14:26:00 +00001512{
1513 static int version = 0;
1514 static int rpflag = 0;
1515 static int basisflag = 0;
1516 static int compress = 0;
1517 static int quiet = 0;
1518 static int statistics = 0;
1519 static int mhflag = 0;
shane58543932008-12-10 20:10:04 +00001520 static int nolinenosflag = 0;
drhdd7e9db2010-07-19 01:52:07 +00001521 static int noResort = 0;
drh75897232000-05-29 14:26:00 +00001522 static struct s_options options[] = {
1523 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1524 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
drh6d08b4d2004-07-20 12:45:22 +00001525 {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
drh0325d392015-01-01 19:11:22 +00001526 {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"},
drh75897232000-05-29 14:26:00 +00001527 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
drh0325d392015-01-01 19:11:22 +00001528 {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"},
shane58543932008-12-10 20:10:04 +00001529 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
1530 {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
drh0325d392015-01-01 19:11:22 +00001531 {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"},
drhf5c4e0f2010-07-18 11:35:53 +00001532 {OPT_FLAG, "p", (char*)&showPrecedenceConflict,
1533 "Show conflicts resolved by precedence rules"},
drh75897232000-05-29 14:26:00 +00001534 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
drhdd7e9db2010-07-19 01:52:07 +00001535 {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"},
drh6d08b4d2004-07-20 12:45:22 +00001536 {OPT_FLAG, "s", (char*)&statistics,
1537 "Print parser stats to standard output."},
drh75897232000-05-29 14:26:00 +00001538 {OPT_FLAG, "x", (char*)&version, "Print the version number."},
drh0325d392015-01-01 19:11:22 +00001539 {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
1540 {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"},
drh75897232000-05-29 14:26:00 +00001541 {OPT_FLAG,0,0,0}
1542 };
1543 int i;
icculus42585cf2010-02-14 05:19:56 +00001544 int exitcode;
drh75897232000-05-29 14:26:00 +00001545 struct lemon lem;
1546
drhb0c86772000-06-02 23:21:26 +00001547 OptInit(argv,options,stderr);
drh75897232000-05-29 14:26:00 +00001548 if( version ){
drhb19a2bc2001-09-16 00:13:26 +00001549 printf("Lemon version 1.0\n");
drh75897232000-05-29 14:26:00 +00001550 exit(0);
1551 }
drhb0c86772000-06-02 23:21:26 +00001552 if( OptNArgs()!=1 ){
drh75897232000-05-29 14:26:00 +00001553 fprintf(stderr,"Exactly one filename argument is required.\n");
1554 exit(1);
1555 }
drh954f6b42006-06-13 13:27:46 +00001556 memset(&lem, 0, sizeof(lem));
drh75897232000-05-29 14:26:00 +00001557 lem.errorcnt = 0;
1558
1559 /* Initialize the machine */
1560 Strsafe_init();
1561 Symbol_init();
1562 State_init();
1563 lem.argv0 = argv[0];
drhb0c86772000-06-02 23:21:26 +00001564 lem.filename = OptArg(0);
drh75897232000-05-29 14:26:00 +00001565 lem.basisflag = basisflag;
shane58543932008-12-10 20:10:04 +00001566 lem.nolinenosflag = nolinenosflag;
drh75897232000-05-29 14:26:00 +00001567 Symbol_new("$");
1568 lem.errsym = Symbol_new("error");
drhc4dd3fd2008-01-22 01:48:05 +00001569 lem.errsym->useCnt = 0;
drh75897232000-05-29 14:26:00 +00001570
1571 /* Parse the input file */
1572 Parse(&lem);
1573 if( lem.errorcnt ) exit(lem.errorcnt);
drh954f6b42006-06-13 13:27:46 +00001574 if( lem.nrule==0 ){
drh75897232000-05-29 14:26:00 +00001575 fprintf(stderr,"Empty grammar.\n");
1576 exit(1);
1577 }
1578
1579 /* Count and index the symbols of the grammar */
drh75897232000-05-29 14:26:00 +00001580 Symbol_new("{default}");
drh61f92cd2014-01-11 03:06:18 +00001581 lem.nsymbol = Symbol_count();
drh75897232000-05-29 14:26:00 +00001582 lem.symbols = Symbol_arrayof();
drh61f92cd2014-01-11 03:06:18 +00001583 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1584 qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp);
1585 for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
1586 while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; }
1587 assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 );
1588 lem.nsymbol = i - 1;
drhc56fac72015-10-29 13:48:15 +00001589 for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++);
drh75897232000-05-29 14:26:00 +00001590 lem.nterminal = i;
1591
1592 /* Generate a reprint of the grammar, if requested on the command line */
1593 if( rpflag ){
1594 Reprint(&lem);
1595 }else{
1596 /* Initialize the size for all follow and first sets */
drh9892c5d2007-12-21 00:02:11 +00001597 SetSize(lem.nterminal+1);
drh75897232000-05-29 14:26:00 +00001598
1599 /* Find the precedence for every production rule (that has one) */
1600 FindRulePrecedences(&lem);
1601
1602 /* Compute the lambda-nonterminals and the first-sets for every
1603 ** nonterminal */
1604 FindFirstSets(&lem);
1605
1606 /* Compute all LR(0) states. Also record follow-set propagation
1607 ** links so that the follow-set can be computed later */
1608 lem.nstate = 0;
1609 FindStates(&lem);
1610 lem.sorted = State_arrayof();
1611
1612 /* Tie up loose ends on the propagation links */
1613 FindLinks(&lem);
1614
1615 /* Compute the follow set of every reducible configuration */
1616 FindFollowSets(&lem);
1617
1618 /* Compute the action tables */
1619 FindActions(&lem);
1620
1621 /* Compress the action tables */
1622 if( compress==0 ) CompressTables(&lem);
1623
drhada354d2005-11-05 15:03:59 +00001624 /* Reorder and renumber the states so that states with fewer choices
drhdd7e9db2010-07-19 01:52:07 +00001625 ** occur at the end. This is an optimization that helps make the
1626 ** generated parser tables smaller. */
1627 if( noResort==0 ) ResortStates(&lem);
drhada354d2005-11-05 15:03:59 +00001628
drh75897232000-05-29 14:26:00 +00001629 /* Generate a report of the parser generated. (the "y.output" file) */
1630 if( !quiet ) ReportOutput(&lem);
1631
1632 /* Generate the source code for the parser */
1633 ReportTable(&lem, mhflag);
1634
1635 /* Produce a header file for use by the scanner. (This step is
1636 ** omitted if the "-m" option is used because makeheaders will
1637 ** generate the file for us.) */
1638 if( !mhflag ) ReportHeader(&lem);
1639 }
1640 if( statistics ){
drhc75e0162015-09-07 02:23:02 +00001641 printf("Parser statistics:\n");
1642 stats_line("terminal symbols", lem.nterminal);
1643 stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal);
1644 stats_line("total symbols", lem.nsymbol);
1645 stats_line("rules", lem.nrule);
drh3bd48ab2015-09-07 18:23:37 +00001646 stats_line("states", lem.nxstate);
drhc75e0162015-09-07 02:23:02 +00001647 stats_line("conflicts", lem.nconflict);
1648 stats_line("action table entries", lem.nactiontab);
1649 stats_line("total table size (bytes)", lem.tablesize);
drh75897232000-05-29 14:26:00 +00001650 }
icculus8e158022010-02-16 16:09:03 +00001651 if( lem.nconflict > 0 ){
1652 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
icculus42585cf2010-02-14 05:19:56 +00001653 }
1654
1655 /* return 0 on success, 1 on failure. */
icculus8e158022010-02-16 16:09:03 +00001656 exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
icculus42585cf2010-02-14 05:19:56 +00001657 exit(exitcode);
1658 return (exitcode);
drh75897232000-05-29 14:26:00 +00001659}
1660/******************** From the file "msort.c" *******************************/
1661/*
1662** A generic merge-sort program.
1663**
1664** USAGE:
1665** Let "ptr" be a pointer to some structure which is at the head of
1666** a null-terminated list. Then to sort the list call:
1667**
1668** ptr = msort(ptr,&(ptr->next),cmpfnc);
1669**
1670** In the above, "cmpfnc" is a pointer to a function which compares
1671** two instances of the structure and returns an integer, as in
1672** strcmp. The second argument is a pointer to the pointer to the
1673** second element of the linked list. This address is used to compute
1674** the offset to the "next" field within the structure. The offset to
1675** the "next" field must be constant for all structures in the list.
1676**
1677** The function returns a new pointer which is the head of the list
1678** after sorting.
1679**
1680** ALGORITHM:
1681** Merge-sort.
1682*/
1683
1684/*
1685** Return a pointer to the next structure in the linked list.
1686*/
drhd25d6922012-04-18 09:59:56 +00001687#define NEXT(A) (*(char**)(((char*)A)+offset))
drh75897232000-05-29 14:26:00 +00001688
1689/*
1690** Inputs:
1691** a: A sorted, null-terminated linked list. (May be null).
1692** b: A sorted, null-terminated linked list. (May be null).
1693** cmp: A pointer to the comparison function.
1694** offset: Offset in the structure to the "next" field.
1695**
1696** Return Value:
1697** A pointer to the head of a sorted list containing the elements
1698** of both a and b.
1699**
1700** Side effects:
1701** The "next" pointers for elements in the lists a and b are
1702** changed.
1703*/
drhe9278182007-07-18 18:16:29 +00001704static char *merge(
1705 char *a,
1706 char *b,
1707 int (*cmp)(const char*,const char*),
1708 int offset
1709){
drh75897232000-05-29 14:26:00 +00001710 char *ptr, *head;
1711
1712 if( a==0 ){
1713 head = b;
1714 }else if( b==0 ){
1715 head = a;
1716 }else{
drhe594bc32009-11-03 13:02:25 +00001717 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001718 ptr = a;
1719 a = NEXT(a);
1720 }else{
1721 ptr = b;
1722 b = NEXT(b);
1723 }
1724 head = ptr;
1725 while( a && b ){
drhe594bc32009-11-03 13:02:25 +00001726 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001727 NEXT(ptr) = a;
1728 ptr = a;
1729 a = NEXT(a);
1730 }else{
1731 NEXT(ptr) = b;
1732 ptr = b;
1733 b = NEXT(b);
1734 }
1735 }
1736 if( a ) NEXT(ptr) = a;
1737 else NEXT(ptr) = b;
1738 }
1739 return head;
1740}
1741
1742/*
1743** Inputs:
1744** list: Pointer to a singly-linked list of structures.
1745** next: Pointer to pointer to the second element of the list.
1746** cmp: A comparison function.
1747**
1748** Return Value:
1749** A pointer to the head of a sorted list containing the elements
1750** orginally in list.
1751**
1752** Side effects:
1753** The "next" pointers for elements in list are changed.
1754*/
1755#define LISTSIZE 30
drhe9278182007-07-18 18:16:29 +00001756static char *msort(
1757 char *list,
1758 char **next,
1759 int (*cmp)(const char*,const char*)
1760){
drhba99af52001-10-25 20:37:16 +00001761 unsigned long offset;
drh75897232000-05-29 14:26:00 +00001762 char *ep;
1763 char *set[LISTSIZE];
1764 int i;
drh1cc0d112015-03-31 15:15:48 +00001765 offset = (unsigned long)((char*)next - (char*)list);
drh75897232000-05-29 14:26:00 +00001766 for(i=0; i<LISTSIZE; i++) set[i] = 0;
1767 while( list ){
1768 ep = list;
1769 list = NEXT(list);
1770 NEXT(ep) = 0;
1771 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1772 ep = merge(ep,set[i],cmp,offset);
1773 set[i] = 0;
1774 }
1775 set[i] = ep;
1776 }
1777 ep = 0;
drhe594bc32009-11-03 13:02:25 +00001778 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset);
drh75897232000-05-29 14:26:00 +00001779 return ep;
1780}
1781/************************ From the file "option.c" **************************/
1782static char **argv;
1783static struct s_options *op;
1784static FILE *errstream;
1785
1786#define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1787
1788/*
1789** Print the command line with a carrot pointing to the k-th character
1790** of the n-th field.
1791*/
icculus9e44cf12010-02-14 17:14:22 +00001792static void errline(int n, int k, FILE *err)
drh75897232000-05-29 14:26:00 +00001793{
1794 int spcnt, i;
drh75897232000-05-29 14:26:00 +00001795 if( argv[0] ) fprintf(err,"%s",argv[0]);
drh87cf1372008-08-13 20:09:06 +00001796 spcnt = lemonStrlen(argv[0]) + 1;
drh75897232000-05-29 14:26:00 +00001797 for(i=1; i<n && argv[i]; i++){
1798 fprintf(err," %s",argv[i]);
drh87cf1372008-08-13 20:09:06 +00001799 spcnt += lemonStrlen(argv[i])+1;
drh75897232000-05-29 14:26:00 +00001800 }
1801 spcnt += k;
1802 for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1803 if( spcnt<20 ){
1804 fprintf(err,"\n%*s^-- here\n",spcnt,"");
1805 }else{
1806 fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1807 }
1808}
1809
1810/*
1811** Return the index of the N-th non-switch argument. Return -1
1812** if N is out of range.
1813*/
icculus9e44cf12010-02-14 17:14:22 +00001814static int argindex(int n)
drh75897232000-05-29 14:26:00 +00001815{
1816 int i;
1817 int dashdash = 0;
1818 if( argv!=0 && *argv!=0 ){
1819 for(i=1; argv[i]; i++){
1820 if( dashdash || !ISOPT(argv[i]) ){
1821 if( n==0 ) return i;
1822 n--;
1823 }
1824 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1825 }
1826 }
1827 return -1;
1828}
1829
1830static char emsg[] = "Command line syntax error: ";
1831
1832/*
1833** Process a flag command line argument.
1834*/
icculus9e44cf12010-02-14 17:14:22 +00001835static int handleflags(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001836{
1837 int v;
1838 int errcnt = 0;
1839 int j;
1840 for(j=0; op[j].label; j++){
drh87cf1372008-08-13 20:09:06 +00001841 if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
drh75897232000-05-29 14:26:00 +00001842 }
1843 v = argv[i][0]=='-' ? 1 : 0;
1844 if( op[j].label==0 ){
1845 if( err ){
1846 fprintf(err,"%sundefined option.\n",emsg);
1847 errline(i,1,err);
1848 }
1849 errcnt++;
drh0325d392015-01-01 19:11:22 +00001850 }else if( op[j].arg==0 ){
1851 /* Ignore this option */
drh75897232000-05-29 14:26:00 +00001852 }else if( op[j].type==OPT_FLAG ){
1853 *((int*)op[j].arg) = v;
1854 }else if( op[j].type==OPT_FFLAG ){
icculus9e44cf12010-02-14 17:14:22 +00001855 (*(void(*)(int))(op[j].arg))(v);
drh6d08b4d2004-07-20 12:45:22 +00001856 }else if( op[j].type==OPT_FSTR ){
icculus9e44cf12010-02-14 17:14:22 +00001857 (*(void(*)(char *))(op[j].arg))(&argv[i][2]);
drh75897232000-05-29 14:26:00 +00001858 }else{
1859 if( err ){
1860 fprintf(err,"%smissing argument on switch.\n",emsg);
1861 errline(i,1,err);
1862 }
1863 errcnt++;
1864 }
1865 return errcnt;
1866}
1867
1868/*
1869** Process a command line switch which has an argument.
1870*/
icculus9e44cf12010-02-14 17:14:22 +00001871static int handleswitch(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001872{
1873 int lv = 0;
1874 double dv = 0.0;
1875 char *sv = 0, *end;
1876 char *cp;
1877 int j;
1878 int errcnt = 0;
1879 cp = strchr(argv[i],'=');
drh43617e92006-03-06 20:55:46 +00001880 assert( cp!=0 );
drh75897232000-05-29 14:26:00 +00001881 *cp = 0;
1882 for(j=0; op[j].label; j++){
1883 if( strcmp(argv[i],op[j].label)==0 ) break;
1884 }
1885 *cp = '=';
1886 if( op[j].label==0 ){
1887 if( err ){
1888 fprintf(err,"%sundefined option.\n",emsg);
1889 errline(i,0,err);
1890 }
1891 errcnt++;
1892 }else{
1893 cp++;
1894 switch( op[j].type ){
1895 case OPT_FLAG:
1896 case OPT_FFLAG:
1897 if( err ){
1898 fprintf(err,"%soption requires an argument.\n",emsg);
1899 errline(i,0,err);
1900 }
1901 errcnt++;
1902 break;
1903 case OPT_DBL:
1904 case OPT_FDBL:
1905 dv = strtod(cp,&end);
1906 if( *end ){
1907 if( err ){
drh25473362015-09-04 18:03:45 +00001908 fprintf(err,
1909 "%sillegal character in floating-point argument.\n",emsg);
drh1cc0d112015-03-31 15:15:48 +00001910 errline(i,(int)((char*)end-(char*)argv[i]),err);
drh75897232000-05-29 14:26:00 +00001911 }
1912 errcnt++;
1913 }
1914 break;
1915 case OPT_INT:
1916 case OPT_FINT:
1917 lv = strtol(cp,&end,0);
1918 if( *end ){
1919 if( err ){
1920 fprintf(err,"%sillegal character in integer argument.\n",emsg);
drh1cc0d112015-03-31 15:15:48 +00001921 errline(i,(int)((char*)end-(char*)argv[i]),err);
drh75897232000-05-29 14:26:00 +00001922 }
1923 errcnt++;
1924 }
1925 break;
1926 case OPT_STR:
1927 case OPT_FSTR:
1928 sv = cp;
1929 break;
1930 }
1931 switch( op[j].type ){
1932 case OPT_FLAG:
1933 case OPT_FFLAG:
1934 break;
1935 case OPT_DBL:
1936 *(double*)(op[j].arg) = dv;
1937 break;
1938 case OPT_FDBL:
icculus9e44cf12010-02-14 17:14:22 +00001939 (*(void(*)(double))(op[j].arg))(dv);
drh75897232000-05-29 14:26:00 +00001940 break;
1941 case OPT_INT:
1942 *(int*)(op[j].arg) = lv;
1943 break;
1944 case OPT_FINT:
icculus9e44cf12010-02-14 17:14:22 +00001945 (*(void(*)(int))(op[j].arg))((int)lv);
drh75897232000-05-29 14:26:00 +00001946 break;
1947 case OPT_STR:
1948 *(char**)(op[j].arg) = sv;
1949 break;
1950 case OPT_FSTR:
icculus9e44cf12010-02-14 17:14:22 +00001951 (*(void(*)(char *))(op[j].arg))(sv);
drh75897232000-05-29 14:26:00 +00001952 break;
1953 }
1954 }
1955 return errcnt;
1956}
1957
icculus9e44cf12010-02-14 17:14:22 +00001958int OptInit(char **a, struct s_options *o, FILE *err)
drh75897232000-05-29 14:26:00 +00001959{
1960 int errcnt = 0;
1961 argv = a;
1962 op = o;
1963 errstream = err;
1964 if( argv && *argv && op ){
1965 int i;
1966 for(i=1; argv[i]; i++){
1967 if( argv[i][0]=='+' || argv[i][0]=='-' ){
1968 errcnt += handleflags(i,err);
1969 }else if( strchr(argv[i],'=') ){
1970 errcnt += handleswitch(i,err);
1971 }
1972 }
1973 }
1974 if( errcnt>0 ){
1975 fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
drhb0c86772000-06-02 23:21:26 +00001976 OptPrint();
drh75897232000-05-29 14:26:00 +00001977 exit(1);
1978 }
1979 return 0;
1980}
1981
drhb0c86772000-06-02 23:21:26 +00001982int OptNArgs(){
drh75897232000-05-29 14:26:00 +00001983 int cnt = 0;
1984 int dashdash = 0;
1985 int i;
1986 if( argv!=0 && argv[0]!=0 ){
1987 for(i=1; argv[i]; i++){
1988 if( dashdash || !ISOPT(argv[i]) ) cnt++;
1989 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1990 }
1991 }
1992 return cnt;
1993}
1994
icculus9e44cf12010-02-14 17:14:22 +00001995char *OptArg(int n)
drh75897232000-05-29 14:26:00 +00001996{
1997 int i;
1998 i = argindex(n);
1999 return i>=0 ? argv[i] : 0;
2000}
2001
icculus9e44cf12010-02-14 17:14:22 +00002002void OptErr(int n)
drh75897232000-05-29 14:26:00 +00002003{
2004 int i;
2005 i = argindex(n);
2006 if( i>=0 ) errline(i,0,errstream);
2007}
2008
drhb0c86772000-06-02 23:21:26 +00002009void OptPrint(){
drh75897232000-05-29 14:26:00 +00002010 int i;
2011 int max, len;
2012 max = 0;
2013 for(i=0; op[i].label; i++){
drh87cf1372008-08-13 20:09:06 +00002014 len = lemonStrlen(op[i].label) + 1;
drh75897232000-05-29 14:26:00 +00002015 switch( op[i].type ){
2016 case OPT_FLAG:
2017 case OPT_FFLAG:
2018 break;
2019 case OPT_INT:
2020 case OPT_FINT:
2021 len += 9; /* length of "<integer>" */
2022 break;
2023 case OPT_DBL:
2024 case OPT_FDBL:
2025 len += 6; /* length of "<real>" */
2026 break;
2027 case OPT_STR:
2028 case OPT_FSTR:
2029 len += 8; /* length of "<string>" */
2030 break;
2031 }
2032 if( len>max ) max = len;
2033 }
2034 for(i=0; op[i].label; i++){
2035 switch( op[i].type ){
2036 case OPT_FLAG:
2037 case OPT_FFLAG:
2038 fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
2039 break;
2040 case OPT_INT:
2041 case OPT_FINT:
drh0325d392015-01-01 19:11:22 +00002042 fprintf(errstream," -%s<integer>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002043 (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002044 break;
2045 case OPT_DBL:
2046 case OPT_FDBL:
drh0325d392015-01-01 19:11:22 +00002047 fprintf(errstream," -%s<real>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002048 (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002049 break;
2050 case OPT_STR:
2051 case OPT_FSTR:
drh0325d392015-01-01 19:11:22 +00002052 fprintf(errstream," -%s<string>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002053 (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002054 break;
2055 }
2056 }
2057}
2058/*********************** From the file "parse.c" ****************************/
2059/*
2060** Input file parser for the LEMON parser generator.
2061*/
2062
2063/* The state of the parser */
icculus9e44cf12010-02-14 17:14:22 +00002064enum e_state {
2065 INITIALIZE,
2066 WAITING_FOR_DECL_OR_RULE,
2067 WAITING_FOR_DECL_KEYWORD,
2068 WAITING_FOR_DECL_ARG,
2069 WAITING_FOR_PRECEDENCE_SYMBOL,
2070 WAITING_FOR_ARROW,
2071 IN_RHS,
2072 LHS_ALIAS_1,
2073 LHS_ALIAS_2,
2074 LHS_ALIAS_3,
2075 RHS_ALIAS_1,
2076 RHS_ALIAS_2,
2077 PRECEDENCE_MARK_1,
2078 PRECEDENCE_MARK_2,
2079 RESYNC_AFTER_RULE_ERROR,
2080 RESYNC_AFTER_DECL_ERROR,
2081 WAITING_FOR_DESTRUCTOR_SYMBOL,
2082 WAITING_FOR_DATATYPE_SYMBOL,
2083 WAITING_FOR_FALLBACK_ID,
drh61f92cd2014-01-11 03:06:18 +00002084 WAITING_FOR_WILDCARD_ID,
2085 WAITING_FOR_CLASS_ID,
2086 WAITING_FOR_CLASS_TOKEN
icculus9e44cf12010-02-14 17:14:22 +00002087};
drh75897232000-05-29 14:26:00 +00002088struct pstate {
2089 char *filename; /* Name of the input file */
2090 int tokenlineno; /* Linenumber at which current token starts */
2091 int errorcnt; /* Number of errors so far */
2092 char *tokenstart; /* Text of current token */
2093 struct lemon *gp; /* Global state vector */
icculus9e44cf12010-02-14 17:14:22 +00002094 enum e_state state; /* The state of the parser */
drh0bd1f4e2002-06-06 18:54:39 +00002095 struct symbol *fallback; /* The fallback token */
drh61f92cd2014-01-11 03:06:18 +00002096 struct symbol *tkclass; /* Token class symbol */
drh75897232000-05-29 14:26:00 +00002097 struct symbol *lhs; /* Left-hand side of current rule */
icculus9e44cf12010-02-14 17:14:22 +00002098 const char *lhsalias; /* Alias for the LHS */
drh75897232000-05-29 14:26:00 +00002099 int nrhs; /* Number of right-hand side symbols seen */
2100 struct symbol *rhs[MAXRHS]; /* RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +00002101 const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
drh75897232000-05-29 14:26:00 +00002102 struct rule *prevrule; /* Previous rule parsed */
icculus9e44cf12010-02-14 17:14:22 +00002103 const char *declkeyword; /* Keyword of a declaration */
drh75897232000-05-29 14:26:00 +00002104 char **declargslot; /* Where the declaration argument should be put */
drha5808f32008-04-27 22:19:44 +00002105 int insertLineMacro; /* Add #line before declaration insert */
drh4dc8ef52008-07-01 17:13:57 +00002106 int *decllinenoslot; /* Where to write declaration line number */
drh75897232000-05-29 14:26:00 +00002107 enum e_assoc declassoc; /* Assign this association to decl arguments */
2108 int preccounter; /* Assign this precedence to decl arguments */
2109 struct rule *firstrule; /* Pointer to first rule in the grammar */
2110 struct rule *lastrule; /* Pointer to the most recently parsed rule */
2111};
2112
2113/* Parse a single token */
icculus9e44cf12010-02-14 17:14:22 +00002114static void parseonetoken(struct pstate *psp)
drh75897232000-05-29 14:26:00 +00002115{
icculus9e44cf12010-02-14 17:14:22 +00002116 const char *x;
drh75897232000-05-29 14:26:00 +00002117 x = Strsafe(psp->tokenstart); /* Save the token permanently */
2118#if 0
2119 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
2120 x,psp->state);
2121#endif
2122 switch( psp->state ){
2123 case INITIALIZE:
2124 psp->prevrule = 0;
2125 psp->preccounter = 0;
2126 psp->firstrule = psp->lastrule = 0;
2127 psp->gp->nrule = 0;
2128 /* Fall thru to next case */
2129 case WAITING_FOR_DECL_OR_RULE:
2130 if( x[0]=='%' ){
2131 psp->state = WAITING_FOR_DECL_KEYWORD;
drhc56fac72015-10-29 13:48:15 +00002132 }else if( ISLOWER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002133 psp->lhs = Symbol_new(x);
2134 psp->nrhs = 0;
2135 psp->lhsalias = 0;
2136 psp->state = WAITING_FOR_ARROW;
2137 }else if( x[0]=='{' ){
2138 if( psp->prevrule==0 ){
2139 ErrorMsg(psp->filename,psp->tokenlineno,
drh3cb2f6e2012-01-09 14:19:05 +00002140"There is no prior rule upon which to attach the code \
drh75897232000-05-29 14:26:00 +00002141fragment which begins on this line.");
2142 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002143 }else if( psp->prevrule->code!=0 ){
drh75897232000-05-29 14:26:00 +00002144 ErrorMsg(psp->filename,psp->tokenlineno,
2145"Code fragment beginning on this line is not the first \
2146to follow the previous rule.");
2147 psp->errorcnt++;
2148 }else{
2149 psp->prevrule->line = psp->tokenlineno;
2150 psp->prevrule->code = &x[1];
drhf2f105d2012-08-20 15:53:54 +00002151 }
drh75897232000-05-29 14:26:00 +00002152 }else if( x[0]=='[' ){
2153 psp->state = PRECEDENCE_MARK_1;
2154 }else{
2155 ErrorMsg(psp->filename,psp->tokenlineno,
2156 "Token \"%s\" should be either \"%%\" or a nonterminal name.",
2157 x);
2158 psp->errorcnt++;
2159 }
2160 break;
2161 case PRECEDENCE_MARK_1:
drhc56fac72015-10-29 13:48:15 +00002162 if( !ISUPPER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002163 ErrorMsg(psp->filename,psp->tokenlineno,
2164 "The precedence symbol must be a terminal.");
2165 psp->errorcnt++;
2166 }else if( psp->prevrule==0 ){
2167 ErrorMsg(psp->filename,psp->tokenlineno,
2168 "There is no prior rule to assign precedence \"[%s]\".",x);
2169 psp->errorcnt++;
2170 }else if( psp->prevrule->precsym!=0 ){
2171 ErrorMsg(psp->filename,psp->tokenlineno,
2172"Precedence mark on this line is not the first \
2173to follow the previous rule.");
2174 psp->errorcnt++;
2175 }else{
2176 psp->prevrule->precsym = Symbol_new(x);
2177 }
2178 psp->state = PRECEDENCE_MARK_2;
2179 break;
2180 case PRECEDENCE_MARK_2:
2181 if( x[0]!=']' ){
2182 ErrorMsg(psp->filename,psp->tokenlineno,
2183 "Missing \"]\" on precedence mark.");
2184 psp->errorcnt++;
2185 }
2186 psp->state = WAITING_FOR_DECL_OR_RULE;
2187 break;
2188 case WAITING_FOR_ARROW:
2189 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2190 psp->state = IN_RHS;
2191 }else if( x[0]=='(' ){
2192 psp->state = LHS_ALIAS_1;
2193 }else{
2194 ErrorMsg(psp->filename,psp->tokenlineno,
2195 "Expected to see a \":\" following the LHS symbol \"%s\".",
2196 psp->lhs->name);
2197 psp->errorcnt++;
2198 psp->state = RESYNC_AFTER_RULE_ERROR;
2199 }
2200 break;
2201 case LHS_ALIAS_1:
drhc56fac72015-10-29 13:48:15 +00002202 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002203 psp->lhsalias = x;
2204 psp->state = LHS_ALIAS_2;
2205 }else{
2206 ErrorMsg(psp->filename,psp->tokenlineno,
2207 "\"%s\" is not a valid alias for the LHS \"%s\"\n",
2208 x,psp->lhs->name);
2209 psp->errorcnt++;
2210 psp->state = RESYNC_AFTER_RULE_ERROR;
2211 }
2212 break;
2213 case LHS_ALIAS_2:
2214 if( x[0]==')' ){
2215 psp->state = LHS_ALIAS_3;
2216 }else{
2217 ErrorMsg(psp->filename,psp->tokenlineno,
2218 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2219 psp->errorcnt++;
2220 psp->state = RESYNC_AFTER_RULE_ERROR;
2221 }
2222 break;
2223 case LHS_ALIAS_3:
2224 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2225 psp->state = IN_RHS;
2226 }else{
2227 ErrorMsg(psp->filename,psp->tokenlineno,
2228 "Missing \"->\" following: \"%s(%s)\".",
2229 psp->lhs->name,psp->lhsalias);
2230 psp->errorcnt++;
2231 psp->state = RESYNC_AFTER_RULE_ERROR;
2232 }
2233 break;
2234 case IN_RHS:
2235 if( x[0]=='.' ){
2236 struct rule *rp;
drh9892c5d2007-12-21 00:02:11 +00002237 rp = (struct rule *)calloc( sizeof(struct rule) +
2238 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
drh75897232000-05-29 14:26:00 +00002239 if( rp==0 ){
2240 ErrorMsg(psp->filename,psp->tokenlineno,
2241 "Can't allocate enough memory for this rule.");
2242 psp->errorcnt++;
2243 psp->prevrule = 0;
drhf2f105d2012-08-20 15:53:54 +00002244 }else{
drh75897232000-05-29 14:26:00 +00002245 int i;
2246 rp->ruleline = psp->tokenlineno;
2247 rp->rhs = (struct symbol**)&rp[1];
icculus9e44cf12010-02-14 17:14:22 +00002248 rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
drh75897232000-05-29 14:26:00 +00002249 for(i=0; i<psp->nrhs; i++){
2250 rp->rhs[i] = psp->rhs[i];
2251 rp->rhsalias[i] = psp->alias[i];
drhf2f105d2012-08-20 15:53:54 +00002252 }
drh75897232000-05-29 14:26:00 +00002253 rp->lhs = psp->lhs;
2254 rp->lhsalias = psp->lhsalias;
2255 rp->nrhs = psp->nrhs;
2256 rp->code = 0;
2257 rp->precsym = 0;
2258 rp->index = psp->gp->nrule++;
2259 rp->nextlhs = rp->lhs->rule;
2260 rp->lhs->rule = rp;
2261 rp->next = 0;
2262 if( psp->firstrule==0 ){
2263 psp->firstrule = psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002264 }else{
drh75897232000-05-29 14:26:00 +00002265 psp->lastrule->next = rp;
2266 psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002267 }
drh75897232000-05-29 14:26:00 +00002268 psp->prevrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002269 }
drh75897232000-05-29 14:26:00 +00002270 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002271 }else if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002272 if( psp->nrhs>=MAXRHS ){
2273 ErrorMsg(psp->filename,psp->tokenlineno,
drhc4dd3fd2008-01-22 01:48:05 +00002274 "Too many symbols on RHS of rule beginning at \"%s\".",
drh75897232000-05-29 14:26:00 +00002275 x);
2276 psp->errorcnt++;
2277 psp->state = RESYNC_AFTER_RULE_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002278 }else{
drh75897232000-05-29 14:26:00 +00002279 psp->rhs[psp->nrhs] = Symbol_new(x);
2280 psp->alias[psp->nrhs] = 0;
2281 psp->nrhs++;
drhf2f105d2012-08-20 15:53:54 +00002282 }
drhfd405312005-11-06 04:06:59 +00002283 }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
2284 struct symbol *msp = psp->rhs[psp->nrhs-1];
2285 if( msp->type!=MULTITERMINAL ){
2286 struct symbol *origsp = msp;
icculus9e44cf12010-02-14 17:14:22 +00002287 msp = (struct symbol *) calloc(1,sizeof(*msp));
drhfd405312005-11-06 04:06:59 +00002288 memset(msp, 0, sizeof(*msp));
2289 msp->type = MULTITERMINAL;
2290 msp->nsubsym = 1;
icculus9e44cf12010-02-14 17:14:22 +00002291 msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*));
drhfd405312005-11-06 04:06:59 +00002292 msp->subsym[0] = origsp;
2293 msp->name = origsp->name;
2294 psp->rhs[psp->nrhs-1] = msp;
2295 }
2296 msp->nsubsym++;
icculus9e44cf12010-02-14 17:14:22 +00002297 msp->subsym = (struct symbol **) realloc(msp->subsym,
2298 sizeof(struct symbol*)*msp->nsubsym);
drhfd405312005-11-06 04:06:59 +00002299 msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
drhc56fac72015-10-29 13:48:15 +00002300 if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){
drhfd405312005-11-06 04:06:59 +00002301 ErrorMsg(psp->filename,psp->tokenlineno,
2302 "Cannot form a compound containing a non-terminal");
2303 psp->errorcnt++;
2304 }
drh75897232000-05-29 14:26:00 +00002305 }else if( x[0]=='(' && psp->nrhs>0 ){
2306 psp->state = RHS_ALIAS_1;
2307 }else{
2308 ErrorMsg(psp->filename,psp->tokenlineno,
2309 "Illegal character on RHS of rule: \"%s\".",x);
2310 psp->errorcnt++;
2311 psp->state = RESYNC_AFTER_RULE_ERROR;
2312 }
2313 break;
2314 case RHS_ALIAS_1:
drhc56fac72015-10-29 13:48:15 +00002315 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002316 psp->alias[psp->nrhs-1] = x;
2317 psp->state = RHS_ALIAS_2;
2318 }else{
2319 ErrorMsg(psp->filename,psp->tokenlineno,
2320 "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
2321 x,psp->rhs[psp->nrhs-1]->name);
2322 psp->errorcnt++;
2323 psp->state = RESYNC_AFTER_RULE_ERROR;
2324 }
2325 break;
2326 case RHS_ALIAS_2:
2327 if( x[0]==')' ){
2328 psp->state = IN_RHS;
2329 }else{
2330 ErrorMsg(psp->filename,psp->tokenlineno,
2331 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2332 psp->errorcnt++;
2333 psp->state = RESYNC_AFTER_RULE_ERROR;
2334 }
2335 break;
2336 case WAITING_FOR_DECL_KEYWORD:
drhc56fac72015-10-29 13:48:15 +00002337 if( ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002338 psp->declkeyword = x;
2339 psp->declargslot = 0;
drh4dc8ef52008-07-01 17:13:57 +00002340 psp->decllinenoslot = 0;
drha5808f32008-04-27 22:19:44 +00002341 psp->insertLineMacro = 1;
drh75897232000-05-29 14:26:00 +00002342 psp->state = WAITING_FOR_DECL_ARG;
2343 if( strcmp(x,"name")==0 ){
2344 psp->declargslot = &(psp->gp->name);
drha5808f32008-04-27 22:19:44 +00002345 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002346 }else if( strcmp(x,"include")==0 ){
drh75897232000-05-29 14:26:00 +00002347 psp->declargslot = &(psp->gp->include);
drhf2f105d2012-08-20 15:53:54 +00002348 }else if( strcmp(x,"code")==0 ){
drh75897232000-05-29 14:26:00 +00002349 psp->declargslot = &(psp->gp->extracode);
drhf2f105d2012-08-20 15:53:54 +00002350 }else if( strcmp(x,"token_destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002351 psp->declargslot = &psp->gp->tokendest;
drhf2f105d2012-08-20 15:53:54 +00002352 }else if( strcmp(x,"default_destructor")==0 ){
drh960e8c62001-04-03 16:53:21 +00002353 psp->declargslot = &psp->gp->vardest;
drhf2f105d2012-08-20 15:53:54 +00002354 }else if( strcmp(x,"token_prefix")==0 ){
drh75897232000-05-29 14:26:00 +00002355 psp->declargslot = &psp->gp->tokenprefix;
drha5808f32008-04-27 22:19:44 +00002356 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002357 }else if( strcmp(x,"syntax_error")==0 ){
drh75897232000-05-29 14:26:00 +00002358 psp->declargslot = &(psp->gp->error);
drhf2f105d2012-08-20 15:53:54 +00002359 }else if( strcmp(x,"parse_accept")==0 ){
drh75897232000-05-29 14:26:00 +00002360 psp->declargslot = &(psp->gp->accept);
drhf2f105d2012-08-20 15:53:54 +00002361 }else if( strcmp(x,"parse_failure")==0 ){
drh75897232000-05-29 14:26:00 +00002362 psp->declargslot = &(psp->gp->failure);
drhf2f105d2012-08-20 15:53:54 +00002363 }else if( strcmp(x,"stack_overflow")==0 ){
drh75897232000-05-29 14:26:00 +00002364 psp->declargslot = &(psp->gp->overflow);
drh75897232000-05-29 14:26:00 +00002365 }else if( strcmp(x,"extra_argument")==0 ){
2366 psp->declargslot = &(psp->gp->arg);
drha5808f32008-04-27 22:19:44 +00002367 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002368 }else if( strcmp(x,"token_type")==0 ){
2369 psp->declargslot = &(psp->gp->tokentype);
drha5808f32008-04-27 22:19:44 +00002370 psp->insertLineMacro = 0;
drh960e8c62001-04-03 16:53:21 +00002371 }else if( strcmp(x,"default_type")==0 ){
2372 psp->declargslot = &(psp->gp->vartype);
drha5808f32008-04-27 22:19:44 +00002373 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002374 }else if( strcmp(x,"stack_size")==0 ){
2375 psp->declargslot = &(psp->gp->stacksize);
drha5808f32008-04-27 22:19:44 +00002376 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002377 }else if( strcmp(x,"start_symbol")==0 ){
2378 psp->declargslot = &(psp->gp->start);
drha5808f32008-04-27 22:19:44 +00002379 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002380 }else if( strcmp(x,"left")==0 ){
2381 psp->preccounter++;
2382 psp->declassoc = LEFT;
2383 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2384 }else if( strcmp(x,"right")==0 ){
2385 psp->preccounter++;
2386 psp->declassoc = RIGHT;
2387 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2388 }else if( strcmp(x,"nonassoc")==0 ){
2389 psp->preccounter++;
2390 psp->declassoc = NONE;
2391 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002392 }else if( strcmp(x,"destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002393 psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002394 }else if( strcmp(x,"type")==0 ){
drh75897232000-05-29 14:26:00 +00002395 psp->state = WAITING_FOR_DATATYPE_SYMBOL;
drh0bd1f4e2002-06-06 18:54:39 +00002396 }else if( strcmp(x,"fallback")==0 ){
2397 psp->fallback = 0;
2398 psp->state = WAITING_FOR_FALLBACK_ID;
drhe09daa92006-06-10 13:29:31 +00002399 }else if( strcmp(x,"wildcard")==0 ){
2400 psp->state = WAITING_FOR_WILDCARD_ID;
drh61f92cd2014-01-11 03:06:18 +00002401 }else if( strcmp(x,"token_class")==0 ){
2402 psp->state = WAITING_FOR_CLASS_ID;
drh75897232000-05-29 14:26:00 +00002403 }else{
2404 ErrorMsg(psp->filename,psp->tokenlineno,
2405 "Unknown declaration keyword: \"%%%s\".",x);
2406 psp->errorcnt++;
2407 psp->state = RESYNC_AFTER_DECL_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002408 }
drh75897232000-05-29 14:26:00 +00002409 }else{
2410 ErrorMsg(psp->filename,psp->tokenlineno,
2411 "Illegal declaration keyword: \"%s\".",x);
2412 psp->errorcnt++;
2413 psp->state = RESYNC_AFTER_DECL_ERROR;
2414 }
2415 break;
2416 case WAITING_FOR_DESTRUCTOR_SYMBOL:
drhc56fac72015-10-29 13:48:15 +00002417 if( !ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002418 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002419 "Symbol name missing after %%destructor keyword");
drh75897232000-05-29 14:26:00 +00002420 psp->errorcnt++;
2421 psp->state = RESYNC_AFTER_DECL_ERROR;
2422 }else{
icculusd286fa62010-03-03 17:06:32 +00002423 struct symbol *sp = Symbol_new(x);
2424 psp->declargslot = &sp->destructor;
2425 psp->decllinenoslot = &sp->destLineno;
2426 psp->insertLineMacro = 1;
2427 psp->state = WAITING_FOR_DECL_ARG;
drh75897232000-05-29 14:26:00 +00002428 }
2429 break;
2430 case WAITING_FOR_DATATYPE_SYMBOL:
drhc56fac72015-10-29 13:48:15 +00002431 if( !ISALPHA(x[0]) ){
drh75897232000-05-29 14:26:00 +00002432 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002433 "Symbol name missing after %%type keyword");
drh75897232000-05-29 14:26:00 +00002434 psp->errorcnt++;
2435 psp->state = RESYNC_AFTER_DECL_ERROR;
2436 }else{
icculus866bf1e2010-02-17 20:31:32 +00002437 struct symbol *sp = Symbol_find(x);
2438 if((sp) && (sp->datatype)){
2439 ErrorMsg(psp->filename,psp->tokenlineno,
2440 "Symbol %%type \"%s\" already defined", x);
2441 psp->errorcnt++;
2442 psp->state = RESYNC_AFTER_DECL_ERROR;
2443 }else{
2444 if (!sp){
2445 sp = Symbol_new(x);
2446 }
2447 psp->declargslot = &sp->datatype;
2448 psp->insertLineMacro = 0;
2449 psp->state = WAITING_FOR_DECL_ARG;
2450 }
drh75897232000-05-29 14:26:00 +00002451 }
2452 break;
2453 case WAITING_FOR_PRECEDENCE_SYMBOL:
2454 if( x[0]=='.' ){
2455 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002456 }else if( ISUPPER(x[0]) ){
drh75897232000-05-29 14:26:00 +00002457 struct symbol *sp;
2458 sp = Symbol_new(x);
2459 if( sp->prec>=0 ){
2460 ErrorMsg(psp->filename,psp->tokenlineno,
2461 "Symbol \"%s\" has already be given a precedence.",x);
2462 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002463 }else{
drh75897232000-05-29 14:26:00 +00002464 sp->prec = psp->preccounter;
2465 sp->assoc = psp->declassoc;
drhf2f105d2012-08-20 15:53:54 +00002466 }
drh75897232000-05-29 14:26:00 +00002467 }else{
2468 ErrorMsg(psp->filename,psp->tokenlineno,
2469 "Can't assign a precedence to \"%s\".",x);
2470 psp->errorcnt++;
2471 }
2472 break;
2473 case WAITING_FOR_DECL_ARG:
drhc56fac72015-10-29 13:48:15 +00002474 if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){
icculus9e44cf12010-02-14 17:14:22 +00002475 const char *zOld, *zNew;
2476 char *zBuf, *z;
mistachkin2318d332015-01-12 18:02:52 +00002477 int nOld, n, nLine = 0, nNew, nBack;
drhb5bd49e2008-07-14 12:21:08 +00002478 int addLineMacro;
drha5808f32008-04-27 22:19:44 +00002479 char zLine[50];
2480 zNew = x;
2481 if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
drh87cf1372008-08-13 20:09:06 +00002482 nNew = lemonStrlen(zNew);
drha5808f32008-04-27 22:19:44 +00002483 if( *psp->declargslot ){
2484 zOld = *psp->declargslot;
2485 }else{
2486 zOld = "";
2487 }
drh87cf1372008-08-13 20:09:06 +00002488 nOld = lemonStrlen(zOld);
drha5808f32008-04-27 22:19:44 +00002489 n = nOld + nNew + 20;
shane58543932008-12-10 20:10:04 +00002490 addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro &&
drhb5bd49e2008-07-14 12:21:08 +00002491 (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
2492 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002493 for(z=psp->filename, nBack=0; *z; z++){
2494 if( *z=='\\' ) nBack++;
2495 }
drh898799f2014-01-10 23:21:00 +00002496 lemon_sprintf(zLine, "#line %d ", psp->tokenlineno);
drh87cf1372008-08-13 20:09:06 +00002497 nLine = lemonStrlen(zLine);
2498 n += nLine + lemonStrlen(psp->filename) + nBack;
drha5808f32008-04-27 22:19:44 +00002499 }
icculus9e44cf12010-02-14 17:14:22 +00002500 *psp->declargslot = (char *) realloc(*psp->declargslot, n);
2501 zBuf = *psp->declargslot + nOld;
drhb5bd49e2008-07-14 12:21:08 +00002502 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002503 if( nOld && zBuf[-1]!='\n' ){
2504 *(zBuf++) = '\n';
2505 }
2506 memcpy(zBuf, zLine, nLine);
2507 zBuf += nLine;
2508 *(zBuf++) = '"';
2509 for(z=psp->filename; *z; z++){
2510 if( *z=='\\' ){
2511 *(zBuf++) = '\\';
2512 }
2513 *(zBuf++) = *z;
2514 }
2515 *(zBuf++) = '"';
2516 *(zBuf++) = '\n';
2517 }
drh4dc8ef52008-07-01 17:13:57 +00002518 if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){
2519 psp->decllinenoslot[0] = psp->tokenlineno;
2520 }
drha5808f32008-04-27 22:19:44 +00002521 memcpy(zBuf, zNew, nNew);
2522 zBuf += nNew;
2523 *zBuf = 0;
2524 psp->state = WAITING_FOR_DECL_OR_RULE;
drh75897232000-05-29 14:26:00 +00002525 }else{
2526 ErrorMsg(psp->filename,psp->tokenlineno,
2527 "Illegal argument to %%%s: %s",psp->declkeyword,x);
2528 psp->errorcnt++;
2529 psp->state = RESYNC_AFTER_DECL_ERROR;
2530 }
2531 break;
drh0bd1f4e2002-06-06 18:54:39 +00002532 case WAITING_FOR_FALLBACK_ID:
2533 if( x[0]=='.' ){
2534 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002535 }else if( !ISUPPER(x[0]) ){
drh0bd1f4e2002-06-06 18:54:39 +00002536 ErrorMsg(psp->filename, psp->tokenlineno,
2537 "%%fallback argument \"%s\" should be a token", x);
2538 psp->errorcnt++;
2539 }else{
2540 struct symbol *sp = Symbol_new(x);
2541 if( psp->fallback==0 ){
2542 psp->fallback = sp;
2543 }else if( sp->fallback ){
2544 ErrorMsg(psp->filename, psp->tokenlineno,
2545 "More than one fallback assigned to token %s", x);
2546 psp->errorcnt++;
2547 }else{
2548 sp->fallback = psp->fallback;
2549 psp->gp->has_fallback = 1;
2550 }
2551 }
2552 break;
drhe09daa92006-06-10 13:29:31 +00002553 case WAITING_FOR_WILDCARD_ID:
2554 if( x[0]=='.' ){
2555 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002556 }else if( !ISUPPER(x[0]) ){
drhe09daa92006-06-10 13:29:31 +00002557 ErrorMsg(psp->filename, psp->tokenlineno,
2558 "%%wildcard argument \"%s\" should be a token", x);
2559 psp->errorcnt++;
2560 }else{
2561 struct symbol *sp = Symbol_new(x);
2562 if( psp->gp->wildcard==0 ){
2563 psp->gp->wildcard = sp;
2564 }else{
2565 ErrorMsg(psp->filename, psp->tokenlineno,
2566 "Extra wildcard to token: %s", x);
2567 psp->errorcnt++;
2568 }
2569 }
2570 break;
drh61f92cd2014-01-11 03:06:18 +00002571 case WAITING_FOR_CLASS_ID:
drhc56fac72015-10-29 13:48:15 +00002572 if( !ISLOWER(x[0]) ){
drh61f92cd2014-01-11 03:06:18 +00002573 ErrorMsg(psp->filename, psp->tokenlineno,
2574 "%%token_class must be followed by an identifier: ", x);
2575 psp->errorcnt++;
2576 psp->state = RESYNC_AFTER_DECL_ERROR;
2577 }else if( Symbol_find(x) ){
2578 ErrorMsg(psp->filename, psp->tokenlineno,
2579 "Symbol \"%s\" already used", x);
2580 psp->errorcnt++;
2581 psp->state = RESYNC_AFTER_DECL_ERROR;
2582 }else{
2583 psp->tkclass = Symbol_new(x);
2584 psp->tkclass->type = MULTITERMINAL;
2585 psp->state = WAITING_FOR_CLASS_TOKEN;
2586 }
2587 break;
2588 case WAITING_FOR_CLASS_TOKEN:
2589 if( x[0]=='.' ){
2590 psp->state = WAITING_FOR_DECL_OR_RULE;
drhc56fac72015-10-29 13:48:15 +00002591 }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){
drh61f92cd2014-01-11 03:06:18 +00002592 struct symbol *msp = psp->tkclass;
2593 msp->nsubsym++;
2594 msp->subsym = (struct symbol **) realloc(msp->subsym,
2595 sizeof(struct symbol*)*msp->nsubsym);
drhc56fac72015-10-29 13:48:15 +00002596 if( !ISUPPER(x[0]) ) x++;
drh61f92cd2014-01-11 03:06:18 +00002597 msp->subsym[msp->nsubsym-1] = Symbol_new(x);
2598 }else{
2599 ErrorMsg(psp->filename, psp->tokenlineno,
2600 "%%token_class argument \"%s\" should be a token", x);
2601 psp->errorcnt++;
2602 psp->state = RESYNC_AFTER_DECL_ERROR;
2603 }
2604 break;
drh75897232000-05-29 14:26:00 +00002605 case RESYNC_AFTER_RULE_ERROR:
2606/* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2607** break; */
2608 case RESYNC_AFTER_DECL_ERROR:
2609 if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2610 if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
2611 break;
2612 }
2613}
2614
drh34ff57b2008-07-14 12:27:51 +00002615/* Run the preprocessor over the input file text. The global variables
drh6d08b4d2004-07-20 12:45:22 +00002616** azDefine[0] through azDefine[nDefine-1] contains the names of all defined
2617** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and
2618** comments them out. Text in between is also commented out as appropriate.
2619*/
danielk1977940fac92005-01-23 22:41:37 +00002620static void preprocess_input(char *z){
drh6d08b4d2004-07-20 12:45:22 +00002621 int i, j, k, n;
2622 int exclude = 0;
rse38514a92007-09-20 11:34:17 +00002623 int start = 0;
drh6d08b4d2004-07-20 12:45:22 +00002624 int lineno = 1;
rse38514a92007-09-20 11:34:17 +00002625 int start_lineno = 1;
drh6d08b4d2004-07-20 12:45:22 +00002626 for(i=0; z[i]; i++){
2627 if( z[i]=='\n' ) lineno++;
2628 if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue;
drhc56fac72015-10-29 13:48:15 +00002629 if( strncmp(&z[i],"%endif",6)==0 && ISSPACE(z[i+6]) ){
drh6d08b4d2004-07-20 12:45:22 +00002630 if( exclude ){
2631 exclude--;
2632 if( exclude==0 ){
2633 for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
2634 }
2635 }
2636 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
drhc56fac72015-10-29 13:48:15 +00002637 }else if( (strncmp(&z[i],"%ifdef",6)==0 && ISSPACE(z[i+6]))
2638 || (strncmp(&z[i],"%ifndef",7)==0 && ISSPACE(z[i+7])) ){
drh6d08b4d2004-07-20 12:45:22 +00002639 if( exclude ){
2640 exclude++;
2641 }else{
drhc56fac72015-10-29 13:48:15 +00002642 for(j=i+7; ISSPACE(z[j]); j++){}
2643 for(n=0; z[j+n] && !ISSPACE(z[j+n]); n++){}
drh6d08b4d2004-07-20 12:45:22 +00002644 exclude = 1;
2645 for(k=0; k<nDefine; k++){
drh87cf1372008-08-13 20:09:06 +00002646 if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){
drh6d08b4d2004-07-20 12:45:22 +00002647 exclude = 0;
2648 break;
2649 }
2650 }
2651 if( z[i+3]=='n' ) exclude = !exclude;
2652 if( exclude ){
2653 start = i;
2654 start_lineno = lineno;
2655 }
2656 }
2657 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2658 }
2659 }
2660 if( exclude ){
2661 fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
2662 exit(1);
2663 }
2664}
2665
drh75897232000-05-29 14:26:00 +00002666/* In spite of its name, this function is really a scanner. It read
2667** in the entire input file (all at once) then tokenizes it. Each
2668** token is passed to the function "parseonetoken" which builds all
2669** the appropriate data structures in the global state vector "gp".
2670*/
icculus9e44cf12010-02-14 17:14:22 +00002671void Parse(struct lemon *gp)
drh75897232000-05-29 14:26:00 +00002672{
2673 struct pstate ps;
2674 FILE *fp;
2675 char *filebuf;
mistachkin2318d332015-01-12 18:02:52 +00002676 unsigned int filesize;
drh75897232000-05-29 14:26:00 +00002677 int lineno;
2678 int c;
2679 char *cp, *nextcp;
2680 int startline = 0;
2681
rse38514a92007-09-20 11:34:17 +00002682 memset(&ps, '\0', sizeof(ps));
drh75897232000-05-29 14:26:00 +00002683 ps.gp = gp;
2684 ps.filename = gp->filename;
2685 ps.errorcnt = 0;
2686 ps.state = INITIALIZE;
2687
2688 /* Begin by reading the input file */
2689 fp = fopen(ps.filename,"rb");
2690 if( fp==0 ){
2691 ErrorMsg(ps.filename,0,"Can't open this file for reading.");
2692 gp->errorcnt++;
2693 return;
2694 }
2695 fseek(fp,0,2);
2696 filesize = ftell(fp);
2697 rewind(fp);
2698 filebuf = (char *)malloc( filesize+1 );
drh03e1b1f2014-01-11 12:52:25 +00002699 if( filesize>100000000 || filebuf==0 ){
2700 ErrorMsg(ps.filename,0,"Input file too large.");
drh75897232000-05-29 14:26:00 +00002701 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002702 fclose(fp);
drh75897232000-05-29 14:26:00 +00002703 return;
2704 }
2705 if( fread(filebuf,1,filesize,fp)!=filesize ){
2706 ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
2707 filesize);
2708 free(filebuf);
2709 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002710 fclose(fp);
drh75897232000-05-29 14:26:00 +00002711 return;
2712 }
2713 fclose(fp);
2714 filebuf[filesize] = 0;
2715
drh6d08b4d2004-07-20 12:45:22 +00002716 /* Make an initial pass through the file to handle %ifdef and %ifndef */
2717 preprocess_input(filebuf);
2718
drh75897232000-05-29 14:26:00 +00002719 /* Now scan the text of the input file */
2720 lineno = 1;
2721 for(cp=filebuf; (c= *cp)!=0; ){
2722 if( c=='\n' ) lineno++; /* Keep track of the line number */
drhc56fac72015-10-29 13:48:15 +00002723 if( ISSPACE(c) ){ cp++; continue; } /* Skip all white space */
drh75897232000-05-29 14:26:00 +00002724 if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
2725 cp+=2;
2726 while( (c= *cp)!=0 && c!='\n' ) cp++;
2727 continue;
2728 }
2729 if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
2730 cp+=2;
2731 while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
2732 if( c=='\n' ) lineno++;
2733 cp++;
2734 }
2735 if( c ) cp++;
2736 continue;
2737 }
2738 ps.tokenstart = cp; /* Mark the beginning of the token */
2739 ps.tokenlineno = lineno; /* Linenumber on which token begins */
2740 if( c=='\"' ){ /* String literals */
2741 cp++;
2742 while( (c= *cp)!=0 && c!='\"' ){
2743 if( c=='\n' ) lineno++;
2744 cp++;
2745 }
2746 if( c==0 ){
2747 ErrorMsg(ps.filename,startline,
2748"String starting on this line is not terminated before the end of the file.");
2749 ps.errorcnt++;
2750 nextcp = cp;
2751 }else{
2752 nextcp = cp+1;
2753 }
2754 }else if( c=='{' ){ /* A block of C code */
2755 int level;
2756 cp++;
2757 for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
2758 if( c=='\n' ) lineno++;
2759 else if( c=='{' ) level++;
2760 else if( c=='}' ) level--;
2761 else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
2762 int prevc;
2763 cp = &cp[2];
2764 prevc = 0;
2765 while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
2766 if( c=='\n' ) lineno++;
2767 prevc = c;
2768 cp++;
drhf2f105d2012-08-20 15:53:54 +00002769 }
2770 }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
drh75897232000-05-29 14:26:00 +00002771 cp = &cp[2];
2772 while( (c= *cp)!=0 && c!='\n' ) cp++;
2773 if( c ) lineno++;
drhf2f105d2012-08-20 15:53:54 +00002774 }else if( c=='\'' || c=='\"' ){ /* String a character literals */
drh75897232000-05-29 14:26:00 +00002775 int startchar, prevc;
2776 startchar = c;
2777 prevc = 0;
2778 for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
2779 if( c=='\n' ) lineno++;
2780 if( prevc=='\\' ) prevc = 0;
2781 else prevc = c;
drhf2f105d2012-08-20 15:53:54 +00002782 }
2783 }
drh75897232000-05-29 14:26:00 +00002784 }
2785 if( c==0 ){
drh960e8c62001-04-03 16:53:21 +00002786 ErrorMsg(ps.filename,ps.tokenlineno,
drh75897232000-05-29 14:26:00 +00002787"C code starting on this line is not terminated before the end of the file.");
2788 ps.errorcnt++;
2789 nextcp = cp;
2790 }else{
2791 nextcp = cp+1;
2792 }
drhc56fac72015-10-29 13:48:15 +00002793 }else if( ISALNUM(c) ){ /* Identifiers */
2794 while( (c= *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
drh75897232000-05-29 14:26:00 +00002795 nextcp = cp;
2796 }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
2797 cp += 3;
2798 nextcp = cp;
drhc56fac72015-10-29 13:48:15 +00002799 }else if( (c=='/' || c=='|') && ISALPHA(cp[1]) ){
drhfd405312005-11-06 04:06:59 +00002800 cp += 2;
drhc56fac72015-10-29 13:48:15 +00002801 while( (c = *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
drhfd405312005-11-06 04:06:59 +00002802 nextcp = cp;
drh75897232000-05-29 14:26:00 +00002803 }else{ /* All other (one character) operators */
2804 cp++;
2805 nextcp = cp;
2806 }
2807 c = *cp;
2808 *cp = 0; /* Null terminate the token */
2809 parseonetoken(&ps); /* Parse the token */
mistachkin2318d332015-01-12 18:02:52 +00002810 *cp = (char)c; /* Restore the buffer */
drh75897232000-05-29 14:26:00 +00002811 cp = nextcp;
2812 }
2813 free(filebuf); /* Release the buffer after parsing */
2814 gp->rule = ps.firstrule;
2815 gp->errorcnt = ps.errorcnt;
2816}
2817/*************************** From the file "plink.c" *********************/
2818/*
2819** Routines processing configuration follow-set propagation links
2820** in the LEMON parser generator.
2821*/
2822static struct plink *plink_freelist = 0;
2823
2824/* Allocate a new plink */
2825struct plink *Plink_new(){
icculus9e44cf12010-02-14 17:14:22 +00002826 struct plink *newlink;
drh75897232000-05-29 14:26:00 +00002827
2828 if( plink_freelist==0 ){
2829 int i;
2830 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +00002831 plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
drh75897232000-05-29 14:26:00 +00002832 if( plink_freelist==0 ){
2833 fprintf(stderr,
2834 "Unable to allocate memory for a new follow-set propagation link.\n");
2835 exit(1);
2836 }
2837 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2838 plink_freelist[amt-1].next = 0;
2839 }
icculus9e44cf12010-02-14 17:14:22 +00002840 newlink = plink_freelist;
drh75897232000-05-29 14:26:00 +00002841 plink_freelist = plink_freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00002842 return newlink;
drh75897232000-05-29 14:26:00 +00002843}
2844
2845/* Add a plink to a plink list */
icculus9e44cf12010-02-14 17:14:22 +00002846void Plink_add(struct plink **plpp, struct config *cfp)
drh75897232000-05-29 14:26:00 +00002847{
icculus9e44cf12010-02-14 17:14:22 +00002848 struct plink *newlink;
2849 newlink = Plink_new();
2850 newlink->next = *plpp;
2851 *plpp = newlink;
2852 newlink->cfp = cfp;
drh75897232000-05-29 14:26:00 +00002853}
2854
2855/* Transfer every plink on the list "from" to the list "to" */
icculus9e44cf12010-02-14 17:14:22 +00002856void Plink_copy(struct plink **to, struct plink *from)
drh75897232000-05-29 14:26:00 +00002857{
2858 struct plink *nextpl;
2859 while( from ){
2860 nextpl = from->next;
2861 from->next = *to;
2862 *to = from;
2863 from = nextpl;
2864 }
2865}
2866
2867/* Delete every plink on the list */
icculus9e44cf12010-02-14 17:14:22 +00002868void Plink_delete(struct plink *plp)
drh75897232000-05-29 14:26:00 +00002869{
2870 struct plink *nextpl;
2871
2872 while( plp ){
2873 nextpl = plp->next;
2874 plp->next = plink_freelist;
2875 plink_freelist = plp;
2876 plp = nextpl;
2877 }
2878}
2879/*********************** From the file "report.c" **************************/
2880/*
2881** Procedures for generating reports and tables in the LEMON parser generator.
2882*/
2883
2884/* Generate a filename with the given suffix. Space to hold the
2885** name comes from malloc() and must be freed by the calling
2886** function.
2887*/
icculus9e44cf12010-02-14 17:14:22 +00002888PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
drh75897232000-05-29 14:26:00 +00002889{
2890 char *name;
2891 char *cp;
2892
icculus9e44cf12010-02-14 17:14:22 +00002893 name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
drh75897232000-05-29 14:26:00 +00002894 if( name==0 ){
2895 fprintf(stderr,"Can't allocate space for a filename.\n");
2896 exit(1);
2897 }
drh898799f2014-01-10 23:21:00 +00002898 lemon_strcpy(name,lemp->filename);
drh75897232000-05-29 14:26:00 +00002899 cp = strrchr(name,'.');
2900 if( cp ) *cp = 0;
drh898799f2014-01-10 23:21:00 +00002901 lemon_strcat(name,suffix);
drh75897232000-05-29 14:26:00 +00002902 return name;
2903}
2904
2905/* Open a file with a name based on the name of the input file,
2906** but with a different (specified) suffix, and return a pointer
2907** to the stream */
icculus9e44cf12010-02-14 17:14:22 +00002908PRIVATE FILE *file_open(
2909 struct lemon *lemp,
2910 const char *suffix,
2911 const char *mode
2912){
drh75897232000-05-29 14:26:00 +00002913 FILE *fp;
2914
2915 if( lemp->outname ) free(lemp->outname);
2916 lemp->outname = file_makename(lemp, suffix);
2917 fp = fopen(lemp->outname,mode);
2918 if( fp==0 && *mode=='w' ){
2919 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
2920 lemp->errorcnt++;
2921 return 0;
2922 }
2923 return fp;
2924}
2925
2926/* Duplicate the input file without comments and without actions
2927** on rules */
icculus9e44cf12010-02-14 17:14:22 +00002928void Reprint(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00002929{
2930 struct rule *rp;
2931 struct symbol *sp;
2932 int i, j, maxlen, len, ncolumns, skip;
2933 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
2934 maxlen = 10;
2935 for(i=0; i<lemp->nsymbol; i++){
2936 sp = lemp->symbols[i];
drh87cf1372008-08-13 20:09:06 +00002937 len = lemonStrlen(sp->name);
drh75897232000-05-29 14:26:00 +00002938 if( len>maxlen ) maxlen = len;
2939 }
2940 ncolumns = 76/(maxlen+5);
2941 if( ncolumns<1 ) ncolumns = 1;
2942 skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
2943 for(i=0; i<skip; i++){
2944 printf("//");
2945 for(j=i; j<lemp->nsymbol; j+=skip){
2946 sp = lemp->symbols[j];
2947 assert( sp->index==j );
2948 printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
2949 }
2950 printf("\n");
2951 }
2952 for(rp=lemp->rule; rp; rp=rp->next){
2953 printf("%s",rp->lhs->name);
drhfd405312005-11-06 04:06:59 +00002954 /* if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
drh75897232000-05-29 14:26:00 +00002955 printf(" ::=");
2956 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +00002957 sp = rp->rhs[i];
drhfd405312005-11-06 04:06:59 +00002958 if( sp->type==MULTITERMINAL ){
drh61f92cd2014-01-11 03:06:18 +00002959 printf(" %s", sp->subsym[0]->name);
drhfd405312005-11-06 04:06:59 +00002960 for(j=1; j<sp->nsubsym; j++){
2961 printf("|%s", sp->subsym[j]->name);
2962 }
drh61f92cd2014-01-11 03:06:18 +00002963 }else{
2964 printf(" %s", sp->name);
drhfd405312005-11-06 04:06:59 +00002965 }
2966 /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
drh75897232000-05-29 14:26:00 +00002967 }
2968 printf(".");
2969 if( rp->precsym ) printf(" [%s]",rp->precsym->name);
drhfd405312005-11-06 04:06:59 +00002970 /* if( rp->code ) printf("\n %s",rp->code); */
drh75897232000-05-29 14:26:00 +00002971 printf("\n");
2972 }
2973}
2974
drh7e698e92015-09-07 14:22:24 +00002975/* Print a single rule.
2976*/
2977void RulePrint(FILE *fp, struct rule *rp, int iCursor){
drhfd405312005-11-06 04:06:59 +00002978 struct symbol *sp;
2979 int i, j;
drh75897232000-05-29 14:26:00 +00002980 fprintf(fp,"%s ::=",rp->lhs->name);
2981 for(i=0; i<=rp->nrhs; i++){
drh7e698e92015-09-07 14:22:24 +00002982 if( i==iCursor ) fprintf(fp," *");
drh75897232000-05-29 14:26:00 +00002983 if( i==rp->nrhs ) break;
drhfd405312005-11-06 04:06:59 +00002984 sp = rp->rhs[i];
drhfd405312005-11-06 04:06:59 +00002985 if( sp->type==MULTITERMINAL ){
drh61f92cd2014-01-11 03:06:18 +00002986 fprintf(fp," %s", sp->subsym[0]->name);
drhfd405312005-11-06 04:06:59 +00002987 for(j=1; j<sp->nsubsym; j++){
2988 fprintf(fp,"|%s",sp->subsym[j]->name);
2989 }
drh61f92cd2014-01-11 03:06:18 +00002990 }else{
2991 fprintf(fp," %s", sp->name);
drhfd405312005-11-06 04:06:59 +00002992 }
drh75897232000-05-29 14:26:00 +00002993 }
2994}
2995
drh7e698e92015-09-07 14:22:24 +00002996/* Print the rule for a configuration.
2997*/
2998void ConfigPrint(FILE *fp, struct config *cfp){
2999 RulePrint(fp, cfp->rp, cfp->dot);
3000}
3001
drh75897232000-05-29 14:26:00 +00003002/* #define TEST */
drhfd405312005-11-06 04:06:59 +00003003#if 0
drh75897232000-05-29 14:26:00 +00003004/* Print a set */
3005PRIVATE void SetPrint(out,set,lemp)
3006FILE *out;
3007char *set;
3008struct lemon *lemp;
3009{
3010 int i;
3011 char *spacer;
3012 spacer = "";
3013 fprintf(out,"%12s[","");
3014 for(i=0; i<lemp->nterminal; i++){
3015 if( SetFind(set,i) ){
3016 fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
3017 spacer = " ";
3018 }
3019 }
3020 fprintf(out,"]\n");
3021}
3022
3023/* Print a plink chain */
3024PRIVATE void PlinkPrint(out,plp,tag)
3025FILE *out;
3026struct plink *plp;
3027char *tag;
3028{
3029 while( plp ){
drhada354d2005-11-05 15:03:59 +00003030 fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum);
drh75897232000-05-29 14:26:00 +00003031 ConfigPrint(out,plp->cfp);
3032 fprintf(out,"\n");
3033 plp = plp->next;
3034 }
3035}
3036#endif
3037
3038/* Print an action to the given file descriptor. Return FALSE if
3039** nothing was actually printed.
3040*/
drh7e698e92015-09-07 14:22:24 +00003041int PrintAction(
3042 struct action *ap, /* The action to print */
3043 FILE *fp, /* Print the action here */
drh3bd48ab2015-09-07 18:23:37 +00003044 int indent /* Indent by this amount */
drh7e698e92015-09-07 14:22:24 +00003045){
drh75897232000-05-29 14:26:00 +00003046 int result = 1;
3047 switch( ap->type ){
drh7e698e92015-09-07 14:22:24 +00003048 case SHIFT: {
3049 struct state *stp = ap->x.stp;
drh3bd48ab2015-09-07 18:23:37 +00003050 fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum);
drh75897232000-05-29 14:26:00 +00003051 break;
drh7e698e92015-09-07 14:22:24 +00003052 }
3053 case REDUCE: {
3054 struct rule *rp = ap->x.rp;
drh3bd48ab2015-09-07 18:23:37 +00003055 fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->index);
3056 RulePrint(fp, rp, -1);
3057 break;
3058 }
3059 case SHIFTREDUCE: {
3060 struct rule *rp = ap->x.rp;
3061 fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->index);
3062 RulePrint(fp, rp, -1);
drh75897232000-05-29 14:26:00 +00003063 break;
drh7e698e92015-09-07 14:22:24 +00003064 }
drh75897232000-05-29 14:26:00 +00003065 case ACCEPT:
3066 fprintf(fp,"%*s accept",indent,ap->sp->name);
3067 break;
3068 case ERROR:
3069 fprintf(fp,"%*s error",indent,ap->sp->name);
3070 break;
drh9892c5d2007-12-21 00:02:11 +00003071 case SRCONFLICT:
3072 case RRCONFLICT:
drh3bd48ab2015-09-07 18:23:37 +00003073 fprintf(fp,"%*s reduce %-7d ** Parsing conflict **",
drh75897232000-05-29 14:26:00 +00003074 indent,ap->sp->name,ap->x.rp->index);
3075 break;
drh9892c5d2007-12-21 00:02:11 +00003076 case SSCONFLICT:
drh3bd48ab2015-09-07 18:23:37 +00003077 fprintf(fp,"%*s shift %-7d ** Parsing conflict **",
drh9892c5d2007-12-21 00:02:11 +00003078 indent,ap->sp->name,ap->x.stp->statenum);
3079 break;
drh75897232000-05-29 14:26:00 +00003080 case SH_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00003081 if( showPrecedenceConflict ){
drh3bd48ab2015-09-07 18:23:37 +00003082 fprintf(fp,"%*s shift %-7d -- dropped by precedence",
drhf5c4e0f2010-07-18 11:35:53 +00003083 indent,ap->sp->name,ap->x.stp->statenum);
3084 }else{
3085 result = 0;
3086 }
3087 break;
drh75897232000-05-29 14:26:00 +00003088 case RD_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00003089 if( showPrecedenceConflict ){
drh7e698e92015-09-07 14:22:24 +00003090 fprintf(fp,"%*s reduce %-7d -- dropped by precedence",
drhf5c4e0f2010-07-18 11:35:53 +00003091 indent,ap->sp->name,ap->x.rp->index);
3092 }else{
3093 result = 0;
3094 }
3095 break;
drh75897232000-05-29 14:26:00 +00003096 case NOT_USED:
3097 result = 0;
3098 break;
3099 }
3100 return result;
3101}
3102
drh3bd48ab2015-09-07 18:23:37 +00003103/* Generate the "*.out" log file */
icculus9e44cf12010-02-14 17:14:22 +00003104void ReportOutput(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003105{
3106 int i;
3107 struct state *stp;
3108 struct config *cfp;
3109 struct action *ap;
3110 FILE *fp;
3111
drh2aa6ca42004-09-10 00:14:04 +00003112 fp = file_open(lemp,".out","wb");
drh75897232000-05-29 14:26:00 +00003113 if( fp==0 ) return;
drh3bd48ab2015-09-07 18:23:37 +00003114 for(i=0; i<lemp->nxstate; i++){
drh75897232000-05-29 14:26:00 +00003115 stp = lemp->sorted[i];
drhada354d2005-11-05 15:03:59 +00003116 fprintf(fp,"State %d:\n",stp->statenum);
drh75897232000-05-29 14:26:00 +00003117 if( lemp->basisflag ) cfp=stp->bp;
3118 else cfp=stp->cfp;
3119 while( cfp ){
3120 char buf[20];
3121 if( cfp->dot==cfp->rp->nrhs ){
drh898799f2014-01-10 23:21:00 +00003122 lemon_sprintf(buf,"(%d)",cfp->rp->index);
drh75897232000-05-29 14:26:00 +00003123 fprintf(fp," %5s ",buf);
3124 }else{
3125 fprintf(fp," ");
3126 }
3127 ConfigPrint(fp,cfp);
3128 fprintf(fp,"\n");
drhfd405312005-11-06 04:06:59 +00003129#if 0
drh75897232000-05-29 14:26:00 +00003130 SetPrint(fp,cfp->fws,lemp);
3131 PlinkPrint(fp,cfp->fplp,"To ");
3132 PlinkPrint(fp,cfp->bplp,"From");
3133#endif
3134 if( lemp->basisflag ) cfp=cfp->bp;
3135 else cfp=cfp->next;
3136 }
3137 fprintf(fp,"\n");
3138 for(ap=stp->ap; ap; ap=ap->next){
drh3bd48ab2015-09-07 18:23:37 +00003139 if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
drh75897232000-05-29 14:26:00 +00003140 }
3141 fprintf(fp,"\n");
3142 }
drhe9278182007-07-18 18:16:29 +00003143 fprintf(fp, "----------------------------------------------------\n");
3144 fprintf(fp, "Symbols:\n");
3145 for(i=0; i<lemp->nsymbol; i++){
3146 int j;
3147 struct symbol *sp;
3148
3149 sp = lemp->symbols[i];
3150 fprintf(fp, " %3d: %s", i, sp->name);
3151 if( sp->type==NONTERMINAL ){
3152 fprintf(fp, ":");
3153 if( sp->lambda ){
3154 fprintf(fp, " <lambda>");
3155 }
3156 for(j=0; j<lemp->nterminal; j++){
3157 if( sp->firstset && SetFind(sp->firstset, j) ){
3158 fprintf(fp, " %s", lemp->symbols[j]->name);
3159 }
3160 }
3161 }
3162 fprintf(fp, "\n");
3163 }
drh75897232000-05-29 14:26:00 +00003164 fclose(fp);
3165 return;
3166}
3167
3168/* Search for the file "name" which is in the same directory as
3169** the exacutable */
icculus9e44cf12010-02-14 17:14:22 +00003170PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
drh75897232000-05-29 14:26:00 +00003171{
icculus9e44cf12010-02-14 17:14:22 +00003172 const char *pathlist;
3173 char *pathbufptr;
3174 char *pathbuf;
drh75897232000-05-29 14:26:00 +00003175 char *path,*cp;
3176 char c;
drh75897232000-05-29 14:26:00 +00003177
3178#ifdef __WIN32__
3179 cp = strrchr(argv0,'\\');
3180#else
3181 cp = strrchr(argv0,'/');
3182#endif
3183 if( cp ){
3184 c = *cp;
3185 *cp = 0;
drh87cf1372008-08-13 20:09:06 +00003186 path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
drh898799f2014-01-10 23:21:00 +00003187 if( path ) lemon_sprintf(path,"%s/%s",argv0,name);
drh75897232000-05-29 14:26:00 +00003188 *cp = c;
3189 }else{
drh75897232000-05-29 14:26:00 +00003190 pathlist = getenv("PATH");
3191 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
icculus9e44cf12010-02-14 17:14:22 +00003192 pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
drh87cf1372008-08-13 20:09:06 +00003193 path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
icculus9e44cf12010-02-14 17:14:22 +00003194 if( (pathbuf != 0) && (path!=0) ){
3195 pathbufptr = pathbuf;
drh898799f2014-01-10 23:21:00 +00003196 lemon_strcpy(pathbuf, pathlist);
icculus9e44cf12010-02-14 17:14:22 +00003197 while( *pathbuf ){
3198 cp = strchr(pathbuf,':');
3199 if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
drh75897232000-05-29 14:26:00 +00003200 c = *cp;
3201 *cp = 0;
drh898799f2014-01-10 23:21:00 +00003202 lemon_sprintf(path,"%s/%s",pathbuf,name);
drh75897232000-05-29 14:26:00 +00003203 *cp = c;
icculus9e44cf12010-02-14 17:14:22 +00003204 if( c==0 ) pathbuf[0] = 0;
3205 else pathbuf = &cp[1];
drh75897232000-05-29 14:26:00 +00003206 if( access(path,modemask)==0 ) break;
3207 }
icculus9e44cf12010-02-14 17:14:22 +00003208 free(pathbufptr);
drh75897232000-05-29 14:26:00 +00003209 }
3210 }
3211 return path;
3212}
3213
3214/* Given an action, compute the integer value for that action
3215** which is to be put in the action table of the generated machine.
3216** Return negative if no action should be generated.
3217*/
icculus9e44cf12010-02-14 17:14:22 +00003218PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
drh75897232000-05-29 14:26:00 +00003219{
3220 int act;
3221 switch( ap->type ){
drh3bd48ab2015-09-07 18:23:37 +00003222 case SHIFT: act = ap->x.stp->statenum; break;
3223 case SHIFTREDUCE: act = ap->x.rp->index + lemp->nstate; break;
3224 case REDUCE: act = ap->x.rp->index + lemp->nstate+lemp->nrule; break;
3225 case ERROR: act = lemp->nstate + lemp->nrule*2; break;
3226 case ACCEPT: act = lemp->nstate + lemp->nrule*2 + 1; break;
drh75897232000-05-29 14:26:00 +00003227 default: act = -1; break;
3228 }
3229 return act;
3230}
3231
3232#define LINESIZE 1000
3233/* The next cluster of routines are for reading the template file
3234** and writing the results to the generated parser */
3235/* The first function transfers data from "in" to "out" until
3236** a line is seen which begins with "%%". The line number is
3237** tracked.
3238**
3239** if name!=0, then any word that begin with "Parse" is changed to
3240** begin with *name instead.
3241*/
icculus9e44cf12010-02-14 17:14:22 +00003242PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
drh75897232000-05-29 14:26:00 +00003243{
3244 int i, iStart;
3245 char line[LINESIZE];
3246 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
3247 (*lineno)++;
3248 iStart = 0;
3249 if( name ){
3250 for(i=0; line[i]; i++){
3251 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
drhc56fac72015-10-29 13:48:15 +00003252 && (i==0 || !ISALPHA(line[i-1]))
drh75897232000-05-29 14:26:00 +00003253 ){
3254 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
3255 fprintf(out,"%s",name);
3256 i += 4;
3257 iStart = i+1;
3258 }
3259 }
3260 }
3261 fprintf(out,"%s",&line[iStart]);
3262 }
3263}
3264
3265/* The next function finds the template file and opens it, returning
3266** a pointer to the opened file. */
icculus9e44cf12010-02-14 17:14:22 +00003267PRIVATE FILE *tplt_open(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003268{
3269 static char templatename[] = "lempar.c";
3270 char buf[1000];
3271 FILE *in;
3272 char *tpltname;
3273 char *cp;
3274
icculus3e143bd2010-02-14 00:48:49 +00003275 /* first, see if user specified a template filename on the command line. */
3276 if (user_templatename != 0) {
3277 if( access(user_templatename,004)==-1 ){
3278 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3279 user_templatename);
3280 lemp->errorcnt++;
3281 return 0;
3282 }
3283 in = fopen(user_templatename,"rb");
3284 if( in==0 ){
drh25473362015-09-04 18:03:45 +00003285 fprintf(stderr,"Can't open the template file \"%s\".\n",
3286 user_templatename);
icculus3e143bd2010-02-14 00:48:49 +00003287 lemp->errorcnt++;
3288 return 0;
3289 }
3290 return in;
3291 }
3292
drh75897232000-05-29 14:26:00 +00003293 cp = strrchr(lemp->filename,'.');
3294 if( cp ){
drh898799f2014-01-10 23:21:00 +00003295 lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
drh75897232000-05-29 14:26:00 +00003296 }else{
drh898799f2014-01-10 23:21:00 +00003297 lemon_sprintf(buf,"%s.lt",lemp->filename);
drh75897232000-05-29 14:26:00 +00003298 }
3299 if( access(buf,004)==0 ){
3300 tpltname = buf;
drh960e8c62001-04-03 16:53:21 +00003301 }else if( access(templatename,004)==0 ){
3302 tpltname = templatename;
drh75897232000-05-29 14:26:00 +00003303 }else{
3304 tpltname = pathsearch(lemp->argv0,templatename,0);
3305 }
3306 if( tpltname==0 ){
3307 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3308 templatename);
3309 lemp->errorcnt++;
3310 return 0;
3311 }
drh2aa6ca42004-09-10 00:14:04 +00003312 in = fopen(tpltname,"rb");
drh75897232000-05-29 14:26:00 +00003313 if( in==0 ){
3314 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
3315 lemp->errorcnt++;
3316 return 0;
3317 }
3318 return in;
3319}
3320
drhaf805ca2004-09-07 11:28:25 +00003321/* Print a #line directive line to the output file. */
icculus9e44cf12010-02-14 17:14:22 +00003322PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
drhaf805ca2004-09-07 11:28:25 +00003323{
3324 fprintf(out,"#line %d \"",lineno);
3325 while( *filename ){
3326 if( *filename == '\\' ) putc('\\',out);
3327 putc(*filename,out);
3328 filename++;
3329 }
3330 fprintf(out,"\"\n");
3331}
3332
drh75897232000-05-29 14:26:00 +00003333/* Print a string to the file and keep the linenumber up to date */
icculus9e44cf12010-02-14 17:14:22 +00003334PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
drh75897232000-05-29 14:26:00 +00003335{
3336 if( str==0 ) return;
drh75897232000-05-29 14:26:00 +00003337 while( *str ){
drh75897232000-05-29 14:26:00 +00003338 putc(*str,out);
shane58543932008-12-10 20:10:04 +00003339 if( *str=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003340 str++;
3341 }
drh9db55df2004-09-09 14:01:21 +00003342 if( str[-1]!='\n' ){
3343 putc('\n',out);
3344 (*lineno)++;
3345 }
shane58543932008-12-10 20:10:04 +00003346 if (!lemp->nolinenosflag) {
3347 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3348 }
drh75897232000-05-29 14:26:00 +00003349 return;
3350}
3351
3352/*
3353** The following routine emits code for the destructor for the
3354** symbol sp
3355*/
icculus9e44cf12010-02-14 17:14:22 +00003356void emit_destructor_code(
3357 FILE *out,
3358 struct symbol *sp,
3359 struct lemon *lemp,
3360 int *lineno
3361){
drhcc83b6e2004-04-23 23:38:42 +00003362 char *cp = 0;
drh75897232000-05-29 14:26:00 +00003363
drh75897232000-05-29 14:26:00 +00003364 if( sp->type==TERMINAL ){
3365 cp = lemp->tokendest;
3366 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003367 fprintf(out,"{\n"); (*lineno)++;
drh960e8c62001-04-03 16:53:21 +00003368 }else if( sp->destructor ){
drh75897232000-05-29 14:26:00 +00003369 cp = sp->destructor;
drha5808f32008-04-27 22:19:44 +00003370 fprintf(out,"{\n"); (*lineno)++;
drh25473362015-09-04 18:03:45 +00003371 if( !lemp->nolinenosflag ){
3372 (*lineno)++;
3373 tplt_linedir(out,sp->destLineno,lemp->filename);
3374 }
drh960e8c62001-04-03 16:53:21 +00003375 }else if( lemp->vardest ){
3376 cp = lemp->vardest;
3377 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003378 fprintf(out,"{\n"); (*lineno)++;
drhcc83b6e2004-04-23 23:38:42 +00003379 }else{
3380 assert( 0 ); /* Cannot happen */
drh75897232000-05-29 14:26:00 +00003381 }
3382 for(; *cp; cp++){
3383 if( *cp=='$' && cp[1]=='$' ){
3384 fprintf(out,"(yypminor->yy%d)",sp->dtnum);
3385 cp++;
3386 continue;
3387 }
shane58543932008-12-10 20:10:04 +00003388 if( *cp=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003389 fputc(*cp,out);
3390 }
shane58543932008-12-10 20:10:04 +00003391 fprintf(out,"\n"); (*lineno)++;
3392 if (!lemp->nolinenosflag) {
3393 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3394 }
3395 fprintf(out,"}\n"); (*lineno)++;
drh75897232000-05-29 14:26:00 +00003396 return;
3397}
3398
3399/*
drh960e8c62001-04-03 16:53:21 +00003400** Return TRUE (non-zero) if the given symbol has a destructor.
drh75897232000-05-29 14:26:00 +00003401*/
icculus9e44cf12010-02-14 17:14:22 +00003402int has_destructor(struct symbol *sp, struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003403{
3404 int ret;
3405 if( sp->type==TERMINAL ){
3406 ret = lemp->tokendest!=0;
3407 }else{
drh960e8c62001-04-03 16:53:21 +00003408 ret = lemp->vardest!=0 || sp->destructor!=0;
drh75897232000-05-29 14:26:00 +00003409 }
3410 return ret;
3411}
3412
drh0bb132b2004-07-20 14:06:51 +00003413/*
3414** Append text to a dynamically allocated string. If zText is 0 then
3415** reset the string to be empty again. Always return the complete text
3416** of the string (which is overwritten with each call).
drh7ac25c72004-08-19 15:12:26 +00003417**
3418** n bytes of zText are stored. If n==0 then all of zText up to the first
3419** \000 terminator is stored. zText can contain up to two instances of
3420** %d. The values of p1 and p2 are written into the first and second
3421** %d.
3422**
3423** If n==-1, then the previous character is overwritten.
drh0bb132b2004-07-20 14:06:51 +00003424*/
icculus9e44cf12010-02-14 17:14:22 +00003425PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
3426 static char empty[1] = { 0 };
drh0bb132b2004-07-20 14:06:51 +00003427 static char *z = 0;
3428 static int alloced = 0;
3429 static int used = 0;
drhaf805ca2004-09-07 11:28:25 +00003430 int c;
drh0bb132b2004-07-20 14:06:51 +00003431 char zInt[40];
drh0bb132b2004-07-20 14:06:51 +00003432 if( zText==0 ){
3433 used = 0;
3434 return z;
3435 }
drh7ac25c72004-08-19 15:12:26 +00003436 if( n<=0 ){
3437 if( n<0 ){
3438 used += n;
3439 assert( used>=0 );
3440 }
drh87cf1372008-08-13 20:09:06 +00003441 n = lemonStrlen(zText);
drh7ac25c72004-08-19 15:12:26 +00003442 }
drhdf609712010-11-23 20:55:27 +00003443 if( (int) (n+sizeof(zInt)*2+used) >= alloced ){
drh0bb132b2004-07-20 14:06:51 +00003444 alloced = n + sizeof(zInt)*2 + used + 200;
icculus9e44cf12010-02-14 17:14:22 +00003445 z = (char *) realloc(z, alloced);
drh0bb132b2004-07-20 14:06:51 +00003446 }
icculus9e44cf12010-02-14 17:14:22 +00003447 if( z==0 ) return empty;
drh0bb132b2004-07-20 14:06:51 +00003448 while( n-- > 0 ){
3449 c = *(zText++);
drh50489622006-10-13 12:25:29 +00003450 if( c=='%' && n>0 && zText[0]=='d' ){
drh898799f2014-01-10 23:21:00 +00003451 lemon_sprintf(zInt, "%d", p1);
drh0bb132b2004-07-20 14:06:51 +00003452 p1 = p2;
drh898799f2014-01-10 23:21:00 +00003453 lemon_strcpy(&z[used], zInt);
drh87cf1372008-08-13 20:09:06 +00003454 used += lemonStrlen(&z[used]);
drh0bb132b2004-07-20 14:06:51 +00003455 zText++;
3456 n--;
3457 }else{
mistachkin2318d332015-01-12 18:02:52 +00003458 z[used++] = (char)c;
drh0bb132b2004-07-20 14:06:51 +00003459 }
3460 }
3461 z[used] = 0;
3462 return z;
3463}
3464
3465/*
3466** zCode is a string that is the action associated with a rule. Expand
3467** the symbols in this string so that the refer to elements of the parser
drhaf805ca2004-09-07 11:28:25 +00003468** stack.
drh0bb132b2004-07-20 14:06:51 +00003469*/
drhaf805ca2004-09-07 11:28:25 +00003470PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){
drh0bb132b2004-07-20 14:06:51 +00003471 char *cp, *xp;
3472 int i;
3473 char lhsused = 0; /* True if the LHS element has been used */
3474 char used[MAXRHS]; /* True for each RHS element which is used */
3475
3476 for(i=0; i<rp->nrhs; i++) used[i] = 0;
3477 lhsused = 0;
3478
drh19c9e562007-03-29 20:13:53 +00003479 if( rp->code==0 ){
icculus9e44cf12010-02-14 17:14:22 +00003480 static char newlinestr[2] = { '\n', '\0' };
3481 rp->code = newlinestr;
drh19c9e562007-03-29 20:13:53 +00003482 rp->line = rp->ruleline;
3483 }
3484
drh0bb132b2004-07-20 14:06:51 +00003485 append_str(0,0,0,0);
icculus9e44cf12010-02-14 17:14:22 +00003486
3487 /* This const cast is wrong but harmless, if we're careful. */
3488 for(cp=(char *)rp->code; *cp; cp++){
drhc56fac72015-10-29 13:48:15 +00003489 if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){
drh0bb132b2004-07-20 14:06:51 +00003490 char saved;
drhc56fac72015-10-29 13:48:15 +00003491 for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++);
drh0bb132b2004-07-20 14:06:51 +00003492 saved = *xp;
3493 *xp = 0;
3494 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
drh7ac25c72004-08-19 15:12:26 +00003495 append_str("yygotominor.yy%d",0,rp->lhs->dtnum,0);
drh0bb132b2004-07-20 14:06:51 +00003496 cp = xp;
3497 lhsused = 1;
3498 }else{
3499 for(i=0; i<rp->nrhs; i++){
3500 if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
drh7ac25c72004-08-19 15:12:26 +00003501 if( cp!=rp->code && cp[-1]=='@' ){
3502 /* If the argument is of the form @X then substituted
3503 ** the token number of X, not the value of X */
3504 append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0);
3505 }else{
drhfd405312005-11-06 04:06:59 +00003506 struct symbol *sp = rp->rhs[i];
3507 int dtnum;
3508 if( sp->type==MULTITERMINAL ){
3509 dtnum = sp->subsym[0]->dtnum;
3510 }else{
3511 dtnum = sp->dtnum;
3512 }
3513 append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum);
drh7ac25c72004-08-19 15:12:26 +00003514 }
drh0bb132b2004-07-20 14:06:51 +00003515 cp = xp;
3516 used[i] = 1;
3517 break;
3518 }
3519 }
3520 }
3521 *xp = saved;
3522 }
3523 append_str(cp, 1, 0, 0);
3524 } /* End loop */
3525
3526 /* Check to make sure the LHS has been used */
3527 if( rp->lhsalias && !lhsused ){
3528 ErrorMsg(lemp->filename,rp->ruleline,
3529 "Label \"%s\" for \"%s(%s)\" is never used.",
3530 rp->lhsalias,rp->lhs->name,rp->lhsalias);
3531 lemp->errorcnt++;
3532 }
3533
3534 /* Generate destructor code for RHS symbols which are not used in the
3535 ** reduce code */
3536 for(i=0; i<rp->nrhs; i++){
3537 if( rp->rhsalias[i] && !used[i] ){
3538 ErrorMsg(lemp->filename,rp->ruleline,
3539 "Label %s for \"%s(%s)\" is never used.",
3540 rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
3541 lemp->errorcnt++;
3542 }else if( rp->rhsalias[i]==0 ){
3543 if( has_destructor(rp->rhs[i],lemp) ){
drh639efd02008-08-13 20:04:29 +00003544 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
drh0bb132b2004-07-20 14:06:51 +00003545 rp->rhs[i]->index,i-rp->nrhs+1);
3546 }else{
3547 /* No destructor defined for this term */
3548 }
3549 }
3550 }
drh61e339a2007-01-16 03:09:02 +00003551 if( rp->code ){
3552 cp = append_str(0,0,0,0);
3553 rp->code = Strsafe(cp?cp:"");
3554 }
drh0bb132b2004-07-20 14:06:51 +00003555}
3556
drh75897232000-05-29 14:26:00 +00003557/*
3558** Generate code which executes when the rule "rp" is reduced. Write
3559** the code to "out". Make sure lineno stays up-to-date.
3560*/
icculus9e44cf12010-02-14 17:14:22 +00003561PRIVATE void emit_code(
3562 FILE *out,
3563 struct rule *rp,
3564 struct lemon *lemp,
3565 int *lineno
3566){
3567 const char *cp;
drh75897232000-05-29 14:26:00 +00003568
3569 /* Generate code to do the reduce action */
3570 if( rp->code ){
drh25473362015-09-04 18:03:45 +00003571 if( !lemp->nolinenosflag ){
3572 (*lineno)++;
3573 tplt_linedir(out,rp->line,lemp->filename);
3574 }
drhaf805ca2004-09-07 11:28:25 +00003575 fprintf(out,"{%s",rp->code);
drh75897232000-05-29 14:26:00 +00003576 for(cp=rp->code; *cp; cp++){
shane58543932008-12-10 20:10:04 +00003577 if( *cp=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003578 } /* End loop */
shane58543932008-12-10 20:10:04 +00003579 fprintf(out,"}\n"); (*lineno)++;
drh25473362015-09-04 18:03:45 +00003580 if( !lemp->nolinenosflag ){
3581 (*lineno)++;
3582 tplt_linedir(out,*lineno,lemp->outname);
3583 }
drh75897232000-05-29 14:26:00 +00003584 } /* End if( rp->code ) */
3585
drh75897232000-05-29 14:26:00 +00003586 return;
3587}
3588
3589/*
3590** Print the definition of the union used for the parser's data stack.
3591** This union contains fields for every possible data type for tokens
3592** and nonterminals. In the process of computing and printing this
3593** union, also set the ".dtnum" field of every terminal and nonterminal
3594** symbol.
3595*/
icculus9e44cf12010-02-14 17:14:22 +00003596void print_stack_union(
3597 FILE *out, /* The output stream */
3598 struct lemon *lemp, /* The main info structure for this parser */
3599 int *plineno, /* Pointer to the line number */
3600 int mhflag /* True if generating makeheaders output */
3601){
drh75897232000-05-29 14:26:00 +00003602 int lineno = *plineno; /* The line number of the output */
3603 char **types; /* A hash table of datatypes */
3604 int arraysize; /* Size of the "types" array */
3605 int maxdtlength; /* Maximum length of any ".datatype" field. */
3606 char *stddt; /* Standardized name for a datatype */
3607 int i,j; /* Loop counters */
drh01f75f22013-10-02 20:46:30 +00003608 unsigned hash; /* For hashing the name of a type */
icculus9e44cf12010-02-14 17:14:22 +00003609 const char *name; /* Name of the parser */
drh75897232000-05-29 14:26:00 +00003610
3611 /* Allocate and initialize types[] and allocate stddt[] */
3612 arraysize = lemp->nsymbol * 2;
drh9892c5d2007-12-21 00:02:11 +00003613 types = (char**)calloc( arraysize, sizeof(char*) );
drh070d4222011-06-02 15:48:51 +00003614 if( types==0 ){
3615 fprintf(stderr,"Out of memory.\n");
3616 exit(1);
3617 }
drh75897232000-05-29 14:26:00 +00003618 for(i=0; i<arraysize; i++) types[i] = 0;
3619 maxdtlength = 0;
drh960e8c62001-04-03 16:53:21 +00003620 if( lemp->vartype ){
drh87cf1372008-08-13 20:09:06 +00003621 maxdtlength = lemonStrlen(lemp->vartype);
drh960e8c62001-04-03 16:53:21 +00003622 }
drh75897232000-05-29 14:26:00 +00003623 for(i=0; i<lemp->nsymbol; i++){
3624 int len;
3625 struct symbol *sp = lemp->symbols[i];
3626 if( sp->datatype==0 ) continue;
drh87cf1372008-08-13 20:09:06 +00003627 len = lemonStrlen(sp->datatype);
drh75897232000-05-29 14:26:00 +00003628 if( len>maxdtlength ) maxdtlength = len;
3629 }
3630 stddt = (char*)malloc( maxdtlength*2 + 1 );
drh070d4222011-06-02 15:48:51 +00003631 if( stddt==0 ){
drh75897232000-05-29 14:26:00 +00003632 fprintf(stderr,"Out of memory.\n");
3633 exit(1);
3634 }
3635
3636 /* Build a hash table of datatypes. The ".dtnum" field of each symbol
3637 ** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
drh960e8c62001-04-03 16:53:21 +00003638 ** used for terminal symbols. If there is no %default_type defined then
3639 ** 0 is also used as the .dtnum value for nonterminals which do not specify
3640 ** a datatype using the %type directive.
3641 */
drh75897232000-05-29 14:26:00 +00003642 for(i=0; i<lemp->nsymbol; i++){
3643 struct symbol *sp = lemp->symbols[i];
3644 char *cp;
3645 if( sp==lemp->errsym ){
3646 sp->dtnum = arraysize+1;
3647 continue;
3648 }
drh960e8c62001-04-03 16:53:21 +00003649 if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
drh75897232000-05-29 14:26:00 +00003650 sp->dtnum = 0;
3651 continue;
3652 }
3653 cp = sp->datatype;
drh960e8c62001-04-03 16:53:21 +00003654 if( cp==0 ) cp = lemp->vartype;
drh75897232000-05-29 14:26:00 +00003655 j = 0;
drhc56fac72015-10-29 13:48:15 +00003656 while( ISSPACE(*cp) ) cp++;
drh75897232000-05-29 14:26:00 +00003657 while( *cp ) stddt[j++] = *cp++;
drhc56fac72015-10-29 13:48:15 +00003658 while( j>0 && ISSPACE(stddt[j-1]) ) j--;
drh75897232000-05-29 14:26:00 +00003659 stddt[j] = 0;
drh02368c92009-04-05 15:18:02 +00003660 if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){
drh32c4d742008-07-01 16:34:49 +00003661 sp->dtnum = 0;
3662 continue;
3663 }
drh75897232000-05-29 14:26:00 +00003664 hash = 0;
3665 for(j=0; stddt[j]; j++){
3666 hash = hash*53 + stddt[j];
3667 }
drh3b2129c2003-05-13 00:34:21 +00003668 hash = (hash & 0x7fffffff)%arraysize;
drh75897232000-05-29 14:26:00 +00003669 while( types[hash] ){
3670 if( strcmp(types[hash],stddt)==0 ){
3671 sp->dtnum = hash + 1;
3672 break;
3673 }
3674 hash++;
drh2b51f212013-10-11 23:01:02 +00003675 if( hash>=(unsigned)arraysize ) hash = 0;
drh75897232000-05-29 14:26:00 +00003676 }
3677 if( types[hash]==0 ){
3678 sp->dtnum = hash + 1;
drh87cf1372008-08-13 20:09:06 +00003679 types[hash] = (char*)malloc( lemonStrlen(stddt)+1 );
drh75897232000-05-29 14:26:00 +00003680 if( types[hash]==0 ){
3681 fprintf(stderr,"Out of memory.\n");
3682 exit(1);
3683 }
drh898799f2014-01-10 23:21:00 +00003684 lemon_strcpy(types[hash],stddt);
drh75897232000-05-29 14:26:00 +00003685 }
3686 }
3687
3688 /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
3689 name = lemp->name ? lemp->name : "Parse";
3690 lineno = *plineno;
3691 if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
3692 fprintf(out,"#define %sTOKENTYPE %s\n",name,
3693 lemp->tokentype?lemp->tokentype:"void*"); lineno++;
3694 if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
3695 fprintf(out,"typedef union {\n"); lineno++;
drh15b024c2008-12-11 02:20:43 +00003696 fprintf(out," int yyinit;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003697 fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
3698 for(i=0; i<arraysize; i++){
3699 if( types[i]==0 ) continue;
3700 fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
3701 free(types[i]);
3702 }
drhc4dd3fd2008-01-22 01:48:05 +00003703 if( lemp->errsym->useCnt ){
3704 fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
3705 }
drh75897232000-05-29 14:26:00 +00003706 free(stddt);
3707 free(types);
3708 fprintf(out,"} YYMINORTYPE;\n"); lineno++;
3709 *plineno = lineno;
3710}
3711
drhb29b0a52002-02-23 19:39:46 +00003712/*
3713** Return the name of a C datatype able to represent values between
drhc75e0162015-09-07 02:23:02 +00003714** lwr and upr, inclusive. If pnByte!=NULL then also write the sizeof
3715** for that type (1, 2, or 4) into *pnByte.
drhb29b0a52002-02-23 19:39:46 +00003716*/
drhc75e0162015-09-07 02:23:02 +00003717static const char *minimum_size_type(int lwr, int upr, int *pnByte){
3718 const char *zType = "int";
3719 int nByte = 4;
drh8b582012003-10-21 13:16:03 +00003720 if( lwr>=0 ){
3721 if( upr<=255 ){
drhc75e0162015-09-07 02:23:02 +00003722 zType = "unsigned char";
3723 nByte = 1;
drh8b582012003-10-21 13:16:03 +00003724 }else if( upr<65535 ){
drhc75e0162015-09-07 02:23:02 +00003725 zType = "unsigned short int";
3726 nByte = 2;
drh8b582012003-10-21 13:16:03 +00003727 }else{
drhc75e0162015-09-07 02:23:02 +00003728 zType = "unsigned int";
3729 nByte = 4;
drh8b582012003-10-21 13:16:03 +00003730 }
3731 }else if( lwr>=-127 && upr<=127 ){
drhc75e0162015-09-07 02:23:02 +00003732 zType = "signed char";
3733 nByte = 1;
drh8b582012003-10-21 13:16:03 +00003734 }else if( lwr>=-32767 && upr<32767 ){
drhc75e0162015-09-07 02:23:02 +00003735 zType = "short";
3736 nByte = 2;
drhb29b0a52002-02-23 19:39:46 +00003737 }
drhc75e0162015-09-07 02:23:02 +00003738 if( pnByte ) *pnByte = nByte;
3739 return zType;
drhb29b0a52002-02-23 19:39:46 +00003740}
3741
drhfdbf9282003-10-21 16:34:41 +00003742/*
3743** Each state contains a set of token transaction and a set of
3744** nonterminal transactions. Each of these sets makes an instance
3745** of the following structure. An array of these structures is used
3746** to order the creation of entries in the yy_action[] table.
3747*/
3748struct axset {
3749 struct state *stp; /* A pointer to a state */
3750 int isTkn; /* True to use tokens. False for non-terminals */
3751 int nAction; /* Number of actions */
drhe594bc32009-11-03 13:02:25 +00003752 int iOrder; /* Original order of action sets */
drhfdbf9282003-10-21 16:34:41 +00003753};
3754
3755/*
3756** Compare to axset structures for sorting purposes
3757*/
3758static int axset_compare(const void *a, const void *b){
3759 struct axset *p1 = (struct axset*)a;
3760 struct axset *p2 = (struct axset*)b;
drhe594bc32009-11-03 13:02:25 +00003761 int c;
3762 c = p2->nAction - p1->nAction;
3763 if( c==0 ){
drh337cd0d2015-09-07 23:40:42 +00003764 c = p1->iOrder - p2->iOrder;
drhe594bc32009-11-03 13:02:25 +00003765 }
3766 assert( c!=0 || p1==p2 );
3767 return c;
drhfdbf9282003-10-21 16:34:41 +00003768}
3769
drhc4dd3fd2008-01-22 01:48:05 +00003770/*
3771** Write text on "out" that describes the rule "rp".
3772*/
3773static void writeRuleText(FILE *out, struct rule *rp){
3774 int j;
3775 fprintf(out,"%s ::=", rp->lhs->name);
3776 for(j=0; j<rp->nrhs; j++){
3777 struct symbol *sp = rp->rhs[j];
drh61f92cd2014-01-11 03:06:18 +00003778 if( sp->type!=MULTITERMINAL ){
3779 fprintf(out," %s", sp->name);
3780 }else{
drhc4dd3fd2008-01-22 01:48:05 +00003781 int k;
drh61f92cd2014-01-11 03:06:18 +00003782 fprintf(out," %s", sp->subsym[0]->name);
drhc4dd3fd2008-01-22 01:48:05 +00003783 for(k=1; k<sp->nsubsym; k++){
3784 fprintf(out,"|%s",sp->subsym[k]->name);
3785 }
3786 }
3787 }
3788}
3789
3790
drh75897232000-05-29 14:26:00 +00003791/* Generate C source code for the parser */
icculus9e44cf12010-02-14 17:14:22 +00003792void ReportTable(
3793 struct lemon *lemp,
3794 int mhflag /* Output in makeheaders format if true */
3795){
drh75897232000-05-29 14:26:00 +00003796 FILE *out, *in;
3797 char line[LINESIZE];
3798 int lineno;
3799 struct state *stp;
3800 struct action *ap;
3801 struct rule *rp;
drh8b582012003-10-21 13:16:03 +00003802 struct acttab *pActtab;
drhc75e0162015-09-07 02:23:02 +00003803 int i, j, n, sz;
3804 int szActionType; /* sizeof(YYACTIONTYPE) */
3805 int szCodeType; /* sizeof(YYCODETYPE) */
icculus9e44cf12010-02-14 17:14:22 +00003806 const char *name;
drh8b582012003-10-21 13:16:03 +00003807 int mnTknOfst, mxTknOfst;
3808 int mnNtOfst, mxNtOfst;
drhfdbf9282003-10-21 16:34:41 +00003809 struct axset *ax;
drh75897232000-05-29 14:26:00 +00003810
3811 in = tplt_open(lemp);
3812 if( in==0 ) return;
drh2aa6ca42004-09-10 00:14:04 +00003813 out = file_open(lemp,".c","wb");
drh75897232000-05-29 14:26:00 +00003814 if( out==0 ){
3815 fclose(in);
3816 return;
3817 }
3818 lineno = 1;
3819 tplt_xfer(lemp->name,in,out,&lineno);
3820
3821 /* Generate the include code, if any */
drha5808f32008-04-27 22:19:44 +00003822 tplt_print(out,lemp,lemp->include,&lineno);
drh75897232000-05-29 14:26:00 +00003823 if( mhflag ){
mistachkin8e189222015-04-19 21:43:16 +00003824 char *incName = file_makename(lemp, ".h");
3825 fprintf(out,"#include \"%s\"\n", incName); lineno++;
3826 free(incName);
drh75897232000-05-29 14:26:00 +00003827 }
3828 tplt_xfer(lemp->name,in,out,&lineno);
3829
3830 /* Generate #defines for all tokens */
3831 if( mhflag ){
icculus9e44cf12010-02-14 17:14:22 +00003832 const char *prefix;
drh75897232000-05-29 14:26:00 +00003833 fprintf(out,"#if INTERFACE\n"); lineno++;
3834 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
3835 else prefix = "";
3836 for(i=1; i<lemp->nterminal; i++){
3837 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3838 lineno++;
3839 }
3840 fprintf(out,"#endif\n"); lineno++;
3841 }
3842 tplt_xfer(lemp->name,in,out,&lineno);
3843
3844 /* Generate the defines */
drh75897232000-05-29 14:26:00 +00003845 fprintf(out,"#define YYCODETYPE %s\n",
drhc75e0162015-09-07 02:23:02 +00003846 minimum_size_type(0, lemp->nsymbol+1, &szCodeType)); lineno++;
drh75897232000-05-29 14:26:00 +00003847 fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++;
3848 fprintf(out,"#define YYACTIONTYPE %s\n",
drh3bd48ab2015-09-07 18:23:37 +00003849 minimum_size_type(0,lemp->nstate+lemp->nrule*2+5,&szActionType)); lineno++;
drhe09daa92006-06-10 13:29:31 +00003850 if( lemp->wildcard ){
3851 fprintf(out,"#define YYWILDCARD %d\n",
3852 lemp->wildcard->index); lineno++;
3853 }
drh75897232000-05-29 14:26:00 +00003854 print_stack_union(out,lemp,&lineno,mhflag);
drhca44b5a2007-02-22 23:06:58 +00003855 fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003856 if( lemp->stacksize ){
drh75897232000-05-29 14:26:00 +00003857 fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
3858 }else{
3859 fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
3860 }
drhca44b5a2007-02-22 23:06:58 +00003861 fprintf(out, "#endif\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003862 if( mhflag ){
3863 fprintf(out,"#if INTERFACE\n"); lineno++;
3864 }
3865 name = lemp->name ? lemp->name : "Parse";
3866 if( lemp->arg && lemp->arg[0] ){
drh87cf1372008-08-13 20:09:06 +00003867 i = lemonStrlen(lemp->arg);
drhc56fac72015-10-29 13:48:15 +00003868 while( i>=1 && ISSPACE(lemp->arg[i-1]) ) i--;
3869 while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
drh1f245e42002-03-11 13:55:50 +00003870 fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++;
3871 fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++;
3872 fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n",
3873 name,lemp->arg,&lemp->arg[i]); lineno++;
3874 fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n",
3875 name,&lemp->arg[i],&lemp->arg[i]); lineno++;
drh75897232000-05-29 14:26:00 +00003876 }else{
drh1f245e42002-03-11 13:55:50 +00003877 fprintf(out,"#define %sARG_SDECL\n",name); lineno++;
3878 fprintf(out,"#define %sARG_PDECL\n",name); lineno++;
3879 fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
3880 fprintf(out,"#define %sARG_STORE\n",name); lineno++;
drh75897232000-05-29 14:26:00 +00003881 }
3882 if( mhflag ){
3883 fprintf(out,"#endif\n"); lineno++;
3884 }
drhc4dd3fd2008-01-22 01:48:05 +00003885 if( lemp->errsym->useCnt ){
drh3bd48ab2015-09-07 18:23:37 +00003886 fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
3887 fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
drhc4dd3fd2008-01-22 01:48:05 +00003888 }
drh0bd1f4e2002-06-06 18:54:39 +00003889 if( lemp->has_fallback ){
3890 fprintf(out,"#define YYFALLBACK 1\n"); lineno++;
3891 }
drh75897232000-05-29 14:26:00 +00003892
drh3bd48ab2015-09-07 18:23:37 +00003893 /* Compute the action table, but do not output it yet. The action
3894 ** table must be computed before generating the YYNSTATE macro because
3895 ** we need to know how many states can be eliminated.
drh75897232000-05-29 14:26:00 +00003896 */
drh3bd48ab2015-09-07 18:23:37 +00003897 ax = (struct axset *) calloc(lemp->nxstate*2, sizeof(ax[0]));
drhfdbf9282003-10-21 16:34:41 +00003898 if( ax==0 ){
3899 fprintf(stderr,"malloc failed\n");
3900 exit(1);
3901 }
drh3bd48ab2015-09-07 18:23:37 +00003902 for(i=0; i<lemp->nxstate; i++){
drh75897232000-05-29 14:26:00 +00003903 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00003904 ax[i*2].stp = stp;
3905 ax[i*2].isTkn = 1;
3906 ax[i*2].nAction = stp->nTknAct;
3907 ax[i*2+1].stp = stp;
3908 ax[i*2+1].isTkn = 0;
3909 ax[i*2+1].nAction = stp->nNtAct;
drh75897232000-05-29 14:26:00 +00003910 }
drh8b582012003-10-21 13:16:03 +00003911 mxTknOfst = mnTknOfst = 0;
3912 mxNtOfst = mnNtOfst = 0;
drh3bd48ab2015-09-07 18:23:37 +00003913 /* In an effort to minimize the action table size, use the heuristic
3914 ** of placing the largest action sets first */
3915 for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i;
3916 qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare);
drh8b582012003-10-21 13:16:03 +00003917 pActtab = acttab_alloc();
drh3bd48ab2015-09-07 18:23:37 +00003918 for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){
drhfdbf9282003-10-21 16:34:41 +00003919 stp = ax[i].stp;
3920 if( ax[i].isTkn ){
3921 for(ap=stp->ap; ap; ap=ap->next){
3922 int action;
3923 if( ap->sp->index>=lemp->nterminal ) continue;
3924 action = compute_action(lemp, ap);
3925 if( action<0 ) continue;
3926 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00003927 }
drhfdbf9282003-10-21 16:34:41 +00003928 stp->iTknOfst = acttab_insert(pActtab);
3929 if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
3930 if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
3931 }else{
3932 for(ap=stp->ap; ap; ap=ap->next){
3933 int action;
3934 if( ap->sp->index<lemp->nterminal ) continue;
3935 if( ap->sp->index==lemp->nsymbol ) continue;
3936 action = compute_action(lemp, ap);
3937 if( action<0 ) continue;
3938 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00003939 }
drhfdbf9282003-10-21 16:34:41 +00003940 stp->iNtOfst = acttab_insert(pActtab);
3941 if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
3942 if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
drh8b582012003-10-21 13:16:03 +00003943 }
drh337cd0d2015-09-07 23:40:42 +00003944#if 0 /* Uncomment for a trace of how the yy_action[] table fills out */
3945 { int jj, nn;
3946 for(jj=nn=0; jj<pActtab->nAction; jj++){
3947 if( pActtab->aAction[jj].action<0 ) nn++;
3948 }
3949 printf("%4d: State %3d %s n: %2d size: %5d freespace: %d\n",
3950 i, stp->statenum, ax[i].isTkn ? "Token" : "Var ",
3951 ax[i].nAction, pActtab->nAction, nn);
3952 }
3953#endif
drh8b582012003-10-21 13:16:03 +00003954 }
drhfdbf9282003-10-21 16:34:41 +00003955 free(ax);
drh8b582012003-10-21 13:16:03 +00003956
drh3bd48ab2015-09-07 18:23:37 +00003957 /* Finish rendering the constants now that the action table has
3958 ** been computed */
3959 fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++;
3960 fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
drh337cd0d2015-09-07 23:40:42 +00003961 fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00003962 fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",lemp->nstate); lineno++;
3963 i = lemp->nstate + lemp->nrule;
3964 fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++;
3965 fprintf(out,"#define YY_MIN_REDUCE %d\n", i); lineno++;
3966 i = lemp->nstate + lemp->nrule*2;
3967 fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++;
3968 fprintf(out,"#define YY_ERROR_ACTION %d\n", i); lineno++;
3969 fprintf(out,"#define YY_ACCEPT_ACTION %d\n", i+1); lineno++;
3970 fprintf(out,"#define YY_NO_ACTION %d\n", i+2); lineno++;
3971 tplt_xfer(lemp->name,in,out,&lineno);
3972
3973 /* Now output the action table and its associates:
3974 **
3975 ** yy_action[] A single table containing all actions.
3976 ** yy_lookahead[] A table containing the lookahead for each entry in
3977 ** yy_action. Used to detect hash collisions.
3978 ** yy_shift_ofst[] For each state, the offset into yy_action for
3979 ** shifting terminals.
3980 ** yy_reduce_ofst[] For each state, the offset into yy_action for
3981 ** shifting non-terminals after a reduce.
3982 ** yy_default[] Default action for each state.
3983 */
3984
drh8b582012003-10-21 13:16:03 +00003985 /* Output the yy_action table */
drhc75e0162015-09-07 02:23:02 +00003986 lemp->nactiontab = n = acttab_size(pActtab);
3987 lemp->tablesize += n*szActionType;
drhf16371d2009-11-03 19:18:31 +00003988 fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
3989 fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00003990 for(i=j=0; i<n; i++){
3991 int action = acttab_yyaction(pActtab, i);
drhe0479212007-01-12 23:09:23 +00003992 if( action<0 ) action = lemp->nstate + lemp->nrule + 2;
drhfdbf9282003-10-21 16:34:41 +00003993 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003994 fprintf(out, " %4d,", action);
3995 if( j==9 || i==n-1 ){
3996 fprintf(out, "\n"); lineno++;
3997 j = 0;
3998 }else{
3999 j++;
4000 }
4001 }
4002 fprintf(out, "};\n"); lineno++;
4003
4004 /* Output the yy_lookahead table */
drhc75e0162015-09-07 02:23:02 +00004005 lemp->tablesize += n*szCodeType;
drh57196282004-10-06 15:41:16 +00004006 fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00004007 for(i=j=0; i<n; i++){
4008 int la = acttab_yylookahead(pActtab, i);
4009 if( la<0 ) la = lemp->nsymbol;
drhfdbf9282003-10-21 16:34:41 +00004010 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004011 fprintf(out, " %4d,", la);
4012 if( j==9 || i==n-1 ){
4013 fprintf(out, "\n"); lineno++;
4014 j = 0;
4015 }else{
4016 j++;
4017 }
4018 }
4019 fprintf(out, "};\n"); lineno++;
4020
4021 /* Output the yy_shift_ofst[] table */
4022 fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004023 n = lemp->nxstate;
drhada354d2005-11-05 15:03:59 +00004024 while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00004025 fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
4026 fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
4027 fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
drh57196282004-10-06 15:41:16 +00004028 fprintf(out, "static const %s yy_shift_ofst[] = {\n",
drhc75e0162015-09-07 02:23:02 +00004029 minimum_size_type(mnTknOfst-1, mxTknOfst, &sz)); lineno++;
4030 lemp->tablesize += n*sz;
drh8b582012003-10-21 13:16:03 +00004031 for(i=j=0; i<n; i++){
4032 int ofst;
4033 stp = lemp->sorted[i];
4034 ofst = stp->iTknOfst;
4035 if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00004036 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004037 fprintf(out, " %4d,", ofst);
4038 if( j==9 || i==n-1 ){
4039 fprintf(out, "\n"); lineno++;
4040 j = 0;
4041 }else{
4042 j++;
4043 }
4044 }
4045 fprintf(out, "};\n"); lineno++;
4046
4047 /* Output the yy_reduce_ofst[] table */
4048 fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004049 n = lemp->nxstate;
drhada354d2005-11-05 15:03:59 +00004050 while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00004051 fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
4052 fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
4053 fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
drh57196282004-10-06 15:41:16 +00004054 fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
drhc75e0162015-09-07 02:23:02 +00004055 minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++;
4056 lemp->tablesize += n*sz;
drh8b582012003-10-21 13:16:03 +00004057 for(i=j=0; i<n; i++){
4058 int ofst;
4059 stp = lemp->sorted[i];
4060 ofst = stp->iNtOfst;
4061 if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00004062 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00004063 fprintf(out, " %4d,", ofst);
4064 if( j==9 || i==n-1 ){
4065 fprintf(out, "\n"); lineno++;
4066 j = 0;
4067 }else{
4068 j++;
4069 }
4070 }
4071 fprintf(out, "};\n"); lineno++;
4072
4073 /* Output the default action table */
drh57196282004-10-06 15:41:16 +00004074 fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
drh3bd48ab2015-09-07 18:23:37 +00004075 n = lemp->nxstate;
drhc75e0162015-09-07 02:23:02 +00004076 lemp->tablesize += n*szActionType;
drh8b582012003-10-21 13:16:03 +00004077 for(i=j=0; i<n; i++){
4078 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00004079 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh3bd48ab2015-09-07 18:23:37 +00004080 fprintf(out, " %4d,", stp->iDfltReduce+lemp->nstate+lemp->nrule);
drh8b582012003-10-21 13:16:03 +00004081 if( j==9 || i==n-1 ){
4082 fprintf(out, "\n"); lineno++;
4083 j = 0;
4084 }else{
4085 j++;
4086 }
4087 }
4088 fprintf(out, "};\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004089 tplt_xfer(lemp->name,in,out,&lineno);
4090
drh0bd1f4e2002-06-06 18:54:39 +00004091 /* Generate the table of fallback tokens.
4092 */
4093 if( lemp->has_fallback ){
drh1441f3e2009-06-12 12:50:50 +00004094 int mx = lemp->nterminal - 1;
4095 while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; }
drhc75e0162015-09-07 02:23:02 +00004096 lemp->tablesize += (mx+1)*szCodeType;
drh1441f3e2009-06-12 12:50:50 +00004097 for(i=0; i<=mx; i++){
drh0bd1f4e2002-06-06 18:54:39 +00004098 struct symbol *p = lemp->symbols[i];
4099 if( p->fallback==0 ){
4100 fprintf(out, " 0, /* %10s => nothing */\n", p->name);
4101 }else{
4102 fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index,
4103 p->name, p->fallback->name);
4104 }
4105 lineno++;
4106 }
4107 }
4108 tplt_xfer(lemp->name, in, out, &lineno);
4109
4110 /* Generate a table containing the symbolic name of every symbol
4111 */
drh75897232000-05-29 14:26:00 +00004112 for(i=0; i<lemp->nsymbol; i++){
drh898799f2014-01-10 23:21:00 +00004113 lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name);
drh75897232000-05-29 14:26:00 +00004114 fprintf(out," %-15s",line);
4115 if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
4116 }
4117 if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
4118 tplt_xfer(lemp->name,in,out,&lineno);
4119
drh0bd1f4e2002-06-06 18:54:39 +00004120 /* Generate a table containing a text string that describes every
drh34ff57b2008-07-14 12:27:51 +00004121 ** rule in the rule set of the grammar. This information is used
drh0bd1f4e2002-06-06 18:54:39 +00004122 ** when tracing REDUCE actions.
4123 */
4124 for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
4125 assert( rp->index==i );
drhc4dd3fd2008-01-22 01:48:05 +00004126 fprintf(out," /* %3d */ \"", i);
4127 writeRuleText(out, rp);
drh0bd1f4e2002-06-06 18:54:39 +00004128 fprintf(out,"\",\n"); lineno++;
4129 }
4130 tplt_xfer(lemp->name,in,out,&lineno);
4131
drh75897232000-05-29 14:26:00 +00004132 /* Generate code which executes every time a symbol is popped from
4133 ** the stack while processing errors or while destroying the parser.
drh0bd1f4e2002-06-06 18:54:39 +00004134 ** (In other words, generate the %destructor actions)
4135 */
drh75897232000-05-29 14:26:00 +00004136 if( lemp->tokendest ){
drh4dc8ef52008-07-01 17:13:57 +00004137 int once = 1;
drh75897232000-05-29 14:26:00 +00004138 for(i=0; i<lemp->nsymbol; i++){
4139 struct symbol *sp = lemp->symbols[i];
4140 if( sp==0 || sp->type!=TERMINAL ) continue;
drh4dc8ef52008-07-01 17:13:57 +00004141 if( once ){
4142 fprintf(out, " /* TERMINAL Destructor */\n"); lineno++;
4143 once = 0;
4144 }
drhc53eed12009-06-12 17:46:19 +00004145 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh75897232000-05-29 14:26:00 +00004146 }
4147 for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
4148 if( i<lemp->nsymbol ){
4149 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4150 fprintf(out," break;\n"); lineno++;
4151 }
4152 }
drh8d659732005-01-13 23:54:06 +00004153 if( lemp->vardest ){
4154 struct symbol *dflt_sp = 0;
drh4dc8ef52008-07-01 17:13:57 +00004155 int once = 1;
drh8d659732005-01-13 23:54:06 +00004156 for(i=0; i<lemp->nsymbol; i++){
4157 struct symbol *sp = lemp->symbols[i];
4158 if( sp==0 || sp->type==TERMINAL ||
4159 sp->index<=0 || sp->destructor!=0 ) continue;
drh4dc8ef52008-07-01 17:13:57 +00004160 if( once ){
4161 fprintf(out, " /* Default NON-TERMINAL Destructor */\n"); lineno++;
4162 once = 0;
4163 }
drhc53eed12009-06-12 17:46:19 +00004164 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh8d659732005-01-13 23:54:06 +00004165 dflt_sp = sp;
4166 }
4167 if( dflt_sp!=0 ){
4168 emit_destructor_code(out,dflt_sp,lemp,&lineno);
drh8d659732005-01-13 23:54:06 +00004169 }
drh4dc8ef52008-07-01 17:13:57 +00004170 fprintf(out," break;\n"); lineno++;
drh8d659732005-01-13 23:54:06 +00004171 }
drh75897232000-05-29 14:26:00 +00004172 for(i=0; i<lemp->nsymbol; i++){
4173 struct symbol *sp = lemp->symbols[i];
4174 if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
drh75013012009-06-12 15:47:34 +00004175 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004176
4177 /* Combine duplicate destructors into a single case */
4178 for(j=i+1; j<lemp->nsymbol; j++){
4179 struct symbol *sp2 = lemp->symbols[j];
4180 if( sp2 && sp2->type!=TERMINAL && sp2->destructor
4181 && sp2->dtnum==sp->dtnum
4182 && strcmp(sp->destructor,sp2->destructor)==0 ){
drhc53eed12009-06-12 17:46:19 +00004183 fprintf(out," case %d: /* %s */\n",
4184 sp2->index, sp2->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004185 sp2->destructor = 0;
4186 }
4187 }
4188
drh75897232000-05-29 14:26:00 +00004189 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4190 fprintf(out," break;\n"); lineno++;
4191 }
drh75897232000-05-29 14:26:00 +00004192 tplt_xfer(lemp->name,in,out,&lineno);
4193
4194 /* Generate code which executes whenever the parser stack overflows */
drha5808f32008-04-27 22:19:44 +00004195 tplt_print(out,lemp,lemp->overflow,&lineno);
drh75897232000-05-29 14:26:00 +00004196 tplt_xfer(lemp->name,in,out,&lineno);
4197
4198 /* Generate the table of rule information
4199 **
4200 ** Note: This code depends on the fact that rules are number
4201 ** sequentually beginning with 0.
4202 */
4203 for(rp=lemp->rule; rp; rp=rp->next){
4204 fprintf(out," { %d, %d },\n",rp->lhs->index,rp->nrhs); lineno++;
4205 }
4206 tplt_xfer(lemp->name,in,out,&lineno);
4207
4208 /* Generate code which execution during each REDUCE action */
4209 for(rp=lemp->rule; rp; rp=rp->next){
drh61e339a2007-01-16 03:09:02 +00004210 translate_code(lemp, rp);
drh0bb132b2004-07-20 14:06:51 +00004211 }
drhc53eed12009-06-12 17:46:19 +00004212 /* First output rules other than the default: rule */
drh0bb132b2004-07-20 14:06:51 +00004213 for(rp=lemp->rule; rp; rp=rp->next){
drhc53eed12009-06-12 17:46:19 +00004214 struct rule *rp2; /* Other rules with the same action */
drh0bb132b2004-07-20 14:06:51 +00004215 if( rp->code==0 ) continue;
drhc53eed12009-06-12 17:46:19 +00004216 if( rp->code[0]=='\n' && rp->code[1]==0 ) continue; /* Will be default: */
drhc4dd3fd2008-01-22 01:48:05 +00004217 fprintf(out," case %d: /* ", rp->index);
4218 writeRuleText(out, rp);
4219 fprintf(out, " */\n"); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004220 for(rp2=rp->next; rp2; rp2=rp2->next){
4221 if( rp2->code==rp->code ){
drhc4dd3fd2008-01-22 01:48:05 +00004222 fprintf(out," case %d: /* ", rp2->index);
4223 writeRuleText(out, rp2);
drh8a415d32009-06-12 13:53:51 +00004224 fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->index); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004225 rp2->code = 0;
4226 }
4227 }
drh75897232000-05-29 14:26:00 +00004228 emit_code(out,rp,lemp,&lineno);
4229 fprintf(out," break;\n"); lineno++;
drhc53eed12009-06-12 17:46:19 +00004230 rp->code = 0;
drh75897232000-05-29 14:26:00 +00004231 }
drhc53eed12009-06-12 17:46:19 +00004232 /* Finally, output the default: rule. We choose as the default: all
4233 ** empty actions. */
4234 fprintf(out," default:\n"); lineno++;
4235 for(rp=lemp->rule; rp; rp=rp->next){
4236 if( rp->code==0 ) continue;
4237 assert( rp->code[0]=='\n' && rp->code[1]==0 );
4238 fprintf(out," /* (%d) ", rp->index);
4239 writeRuleText(out, rp);
4240 fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->index); lineno++;
4241 }
4242 fprintf(out," break;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004243 tplt_xfer(lemp->name,in,out,&lineno);
4244
4245 /* Generate code which executes if a parse fails */
drha5808f32008-04-27 22:19:44 +00004246 tplt_print(out,lemp,lemp->failure,&lineno);
drh75897232000-05-29 14:26:00 +00004247 tplt_xfer(lemp->name,in,out,&lineno);
4248
4249 /* Generate code which executes when a syntax error occurs */
drha5808f32008-04-27 22:19:44 +00004250 tplt_print(out,lemp,lemp->error,&lineno);
drh75897232000-05-29 14:26:00 +00004251 tplt_xfer(lemp->name,in,out,&lineno);
4252
4253 /* Generate code which executes when the parser accepts its input */
drha5808f32008-04-27 22:19:44 +00004254 tplt_print(out,lemp,lemp->accept,&lineno);
drh75897232000-05-29 14:26:00 +00004255 tplt_xfer(lemp->name,in,out,&lineno);
4256
4257 /* Append any addition code the user desires */
drha5808f32008-04-27 22:19:44 +00004258 tplt_print(out,lemp,lemp->extracode,&lineno);
drh75897232000-05-29 14:26:00 +00004259
4260 fclose(in);
4261 fclose(out);
4262 return;
4263}
4264
4265/* Generate a header file for the parser */
icculus9e44cf12010-02-14 17:14:22 +00004266void ReportHeader(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004267{
4268 FILE *out, *in;
icculus9e44cf12010-02-14 17:14:22 +00004269 const char *prefix;
drh75897232000-05-29 14:26:00 +00004270 char line[LINESIZE];
4271 char pattern[LINESIZE];
4272 int i;
4273
4274 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4275 else prefix = "";
drh2aa6ca42004-09-10 00:14:04 +00004276 in = file_open(lemp,".h","rb");
drh75897232000-05-29 14:26:00 +00004277 if( in ){
drh8ba0d1c2012-06-16 15:26:31 +00004278 int nextChar;
drh75897232000-05-29 14:26:00 +00004279 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
drh61f92cd2014-01-11 03:06:18 +00004280 lemon_sprintf(pattern,"#define %s%-30s %3d\n",
4281 prefix,lemp->symbols[i]->name,i);
drh75897232000-05-29 14:26:00 +00004282 if( strcmp(line,pattern) ) break;
4283 }
drh8ba0d1c2012-06-16 15:26:31 +00004284 nextChar = fgetc(in);
drh75897232000-05-29 14:26:00 +00004285 fclose(in);
drh8ba0d1c2012-06-16 15:26:31 +00004286 if( i==lemp->nterminal && nextChar==EOF ){
drh75897232000-05-29 14:26:00 +00004287 /* No change in the file. Don't rewrite it. */
4288 return;
4289 }
4290 }
drh2aa6ca42004-09-10 00:14:04 +00004291 out = file_open(lemp,".h","wb");
drh75897232000-05-29 14:26:00 +00004292 if( out ){
4293 for(i=1; i<lemp->nterminal; i++){
drh61f92cd2014-01-11 03:06:18 +00004294 fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i);
drh75897232000-05-29 14:26:00 +00004295 }
4296 fclose(out);
4297 }
4298 return;
4299}
4300
4301/* Reduce the size of the action tables, if possible, by making use
4302** of defaults.
4303**
drhb59499c2002-02-23 18:45:13 +00004304** In this version, we take the most frequent REDUCE action and make
drhe09daa92006-06-10 13:29:31 +00004305** it the default. Except, there is no default if the wildcard token
4306** is a possible look-ahead.
drh75897232000-05-29 14:26:00 +00004307*/
icculus9e44cf12010-02-14 17:14:22 +00004308void CompressTables(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004309{
4310 struct state *stp;
drhb59499c2002-02-23 18:45:13 +00004311 struct action *ap, *ap2;
4312 struct rule *rp, *rp2, *rbest;
drh0c6dfaa2015-09-08 21:16:46 +00004313 int nbest, n;
drh75897232000-05-29 14:26:00 +00004314 int i;
drhe09daa92006-06-10 13:29:31 +00004315 int usesWildcard;
drh75897232000-05-29 14:26:00 +00004316
4317 for(i=0; i<lemp->nstate; i++){
4318 stp = lemp->sorted[i];
drhb59499c2002-02-23 18:45:13 +00004319 nbest = 0;
4320 rbest = 0;
drhe09daa92006-06-10 13:29:31 +00004321 usesWildcard = 0;
drh75897232000-05-29 14:26:00 +00004322
drhb59499c2002-02-23 18:45:13 +00004323 for(ap=stp->ap; ap; ap=ap->next){
drhe09daa92006-06-10 13:29:31 +00004324 if( ap->type==SHIFT && ap->sp==lemp->wildcard ){
4325 usesWildcard = 1;
4326 }
drhb59499c2002-02-23 18:45:13 +00004327 if( ap->type!=REDUCE ) continue;
4328 rp = ap->x.rp;
drhb4960992007-10-05 16:16:36 +00004329 if( rp->lhsStart ) continue;
drhb59499c2002-02-23 18:45:13 +00004330 if( rp==rbest ) continue;
4331 n = 1;
4332 for(ap2=ap->next; ap2; ap2=ap2->next){
4333 if( ap2->type!=REDUCE ) continue;
4334 rp2 = ap2->x.rp;
4335 if( rp2==rbest ) continue;
4336 if( rp2==rp ) n++;
4337 }
4338 if( n>nbest ){
4339 nbest = n;
4340 rbest = rp;
drh75897232000-05-29 14:26:00 +00004341 }
4342 }
drhb59499c2002-02-23 18:45:13 +00004343
4344 /* Do not make a default if the number of rules to default
drhe09daa92006-06-10 13:29:31 +00004345 ** is not at least 1 or if the wildcard token is a possible
4346 ** lookahead.
4347 */
4348 if( nbest<1 || usesWildcard ) continue;
drh75897232000-05-29 14:26:00 +00004349
drhb59499c2002-02-23 18:45:13 +00004350
4351 /* Combine matching REDUCE actions into a single default */
4352 for(ap=stp->ap; ap; ap=ap->next){
4353 if( ap->type==REDUCE && ap->x.rp==rbest ) break;
4354 }
drh75897232000-05-29 14:26:00 +00004355 assert( ap );
4356 ap->sp = Symbol_new("{default}");
4357 for(ap=ap->next; ap; ap=ap->next){
drhb59499c2002-02-23 18:45:13 +00004358 if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
drh75897232000-05-29 14:26:00 +00004359 }
4360 stp->ap = Action_sort(stp->ap);
drh3bd48ab2015-09-07 18:23:37 +00004361
4362 for(ap=stp->ap; ap; ap=ap->next){
4363 if( ap->type==SHIFT ) break;
4364 if( ap->type==REDUCE && ap->x.rp!=rbest ) break;
4365 }
4366 if( ap==0 ){
4367 stp->autoReduce = 1;
4368 stp->pDfltReduce = rbest;
4369 }
4370 }
4371
4372 /* Make a second pass over all states and actions. Convert
4373 ** every action that is a SHIFT to an autoReduce state into
4374 ** a SHIFTREDUCE action.
4375 */
4376 for(i=0; i<lemp->nstate; i++){
4377 stp = lemp->sorted[i];
4378 for(ap=stp->ap; ap; ap=ap->next){
4379 struct state *pNextState;
4380 if( ap->type!=SHIFT ) continue;
4381 pNextState = ap->x.stp;
4382 if( pNextState->autoReduce && pNextState->pDfltReduce!=0 ){
4383 ap->type = SHIFTREDUCE;
4384 ap->x.rp = pNextState->pDfltReduce;
4385 }
4386 }
drh75897232000-05-29 14:26:00 +00004387 }
4388}
drhb59499c2002-02-23 18:45:13 +00004389
drhada354d2005-11-05 15:03:59 +00004390
4391/*
4392** Compare two states for sorting purposes. The smaller state is the
4393** one with the most non-terminal actions. If they have the same number
4394** of non-terminal actions, then the smaller is the one with the most
4395** token actions.
4396*/
4397static int stateResortCompare(const void *a, const void *b){
4398 const struct state *pA = *(const struct state**)a;
4399 const struct state *pB = *(const struct state**)b;
4400 int n;
4401
4402 n = pB->nNtAct - pA->nNtAct;
4403 if( n==0 ){
4404 n = pB->nTknAct - pA->nTknAct;
drhe594bc32009-11-03 13:02:25 +00004405 if( n==0 ){
4406 n = pB->statenum - pA->statenum;
4407 }
drhada354d2005-11-05 15:03:59 +00004408 }
drhe594bc32009-11-03 13:02:25 +00004409 assert( n!=0 );
drhada354d2005-11-05 15:03:59 +00004410 return n;
4411}
4412
4413
4414/*
4415** Renumber and resort states so that states with fewer choices
4416** occur at the end. Except, keep state 0 as the first state.
4417*/
icculus9e44cf12010-02-14 17:14:22 +00004418void ResortStates(struct lemon *lemp)
drhada354d2005-11-05 15:03:59 +00004419{
4420 int i;
4421 struct state *stp;
4422 struct action *ap;
4423
4424 for(i=0; i<lemp->nstate; i++){
4425 stp = lemp->sorted[i];
4426 stp->nTknAct = stp->nNtAct = 0;
drh3bd48ab2015-09-07 18:23:37 +00004427 stp->iDfltReduce = lemp->nrule; /* Init dflt action to "syntax error" */
drhada354d2005-11-05 15:03:59 +00004428 stp->iTknOfst = NO_OFFSET;
4429 stp->iNtOfst = NO_OFFSET;
4430 for(ap=stp->ap; ap; ap=ap->next){
drh3bd48ab2015-09-07 18:23:37 +00004431 int iAction = compute_action(lemp,ap);
4432 if( iAction>=0 ){
drhada354d2005-11-05 15:03:59 +00004433 if( ap->sp->index<lemp->nterminal ){
4434 stp->nTknAct++;
4435 }else if( ap->sp->index<lemp->nsymbol ){
4436 stp->nNtAct++;
4437 }else{
drh3bd48ab2015-09-07 18:23:37 +00004438 assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp );
4439 stp->iDfltReduce = iAction - lemp->nstate - lemp->nrule;
drhada354d2005-11-05 15:03:59 +00004440 }
4441 }
4442 }
4443 }
4444 qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
4445 stateResortCompare);
4446 for(i=0; i<lemp->nstate; i++){
4447 lemp->sorted[i]->statenum = i;
4448 }
drh3bd48ab2015-09-07 18:23:37 +00004449 lemp->nxstate = lemp->nstate;
4450 while( lemp->nxstate>1 && lemp->sorted[lemp->nxstate-1]->autoReduce ){
4451 lemp->nxstate--;
4452 }
drhada354d2005-11-05 15:03:59 +00004453}
4454
4455
drh75897232000-05-29 14:26:00 +00004456/***************** From the file "set.c" ************************************/
4457/*
4458** Set manipulation routines for the LEMON parser generator.
4459*/
4460
4461static int size = 0;
4462
4463/* Set the set size */
icculus9e44cf12010-02-14 17:14:22 +00004464void SetSize(int n)
drh75897232000-05-29 14:26:00 +00004465{
4466 size = n+1;
4467}
4468
4469/* Allocate a new set */
4470char *SetNew(){
4471 char *s;
drh9892c5d2007-12-21 00:02:11 +00004472 s = (char*)calloc( size, 1);
drh75897232000-05-29 14:26:00 +00004473 if( s==0 ){
4474 extern void memory_error();
4475 memory_error();
4476 }
drh75897232000-05-29 14:26:00 +00004477 return s;
4478}
4479
4480/* Deallocate a set */
icculus9e44cf12010-02-14 17:14:22 +00004481void SetFree(char *s)
drh75897232000-05-29 14:26:00 +00004482{
4483 free(s);
4484}
4485
4486/* Add a new element to the set. Return TRUE if the element was added
4487** and FALSE if it was already there. */
icculus9e44cf12010-02-14 17:14:22 +00004488int SetAdd(char *s, int e)
drh75897232000-05-29 14:26:00 +00004489{
4490 int rv;
drh9892c5d2007-12-21 00:02:11 +00004491 assert( e>=0 && e<size );
drh75897232000-05-29 14:26:00 +00004492 rv = s[e];
4493 s[e] = 1;
4494 return !rv;
4495}
4496
4497/* Add every element of s2 to s1. Return TRUE if s1 changes. */
icculus9e44cf12010-02-14 17:14:22 +00004498int SetUnion(char *s1, char *s2)
drh75897232000-05-29 14:26:00 +00004499{
4500 int i, progress;
4501 progress = 0;
4502 for(i=0; i<size; i++){
4503 if( s2[i]==0 ) continue;
4504 if( s1[i]==0 ){
4505 progress = 1;
4506 s1[i] = 1;
4507 }
4508 }
4509 return progress;
4510}
4511/********************** From the file "table.c" ****************************/
4512/*
4513** All code in this file has been automatically generated
4514** from a specification in the file
4515** "table.q"
4516** by the associative array code building program "aagen".
4517** Do not edit this file! Instead, edit the specification
4518** file, then rerun aagen.
4519*/
4520/*
4521** Code for processing tables in the LEMON parser generator.
4522*/
4523
drh01f75f22013-10-02 20:46:30 +00004524PRIVATE unsigned strhash(const char *x)
drh75897232000-05-29 14:26:00 +00004525{
drh01f75f22013-10-02 20:46:30 +00004526 unsigned h = 0;
4527 while( *x ) h = h*13 + *(x++);
drh75897232000-05-29 14:26:00 +00004528 return h;
4529}
4530
4531/* Works like strdup, sort of. Save a string in malloced memory, but
4532** keep strings in a table so that the same string is not in more
4533** than one place.
4534*/
icculus9e44cf12010-02-14 17:14:22 +00004535const char *Strsafe(const char *y)
drh75897232000-05-29 14:26:00 +00004536{
icculus9e44cf12010-02-14 17:14:22 +00004537 const char *z;
4538 char *cpy;
drh75897232000-05-29 14:26:00 +00004539
drh916f75f2006-07-17 00:19:39 +00004540 if( y==0 ) return 0;
drh75897232000-05-29 14:26:00 +00004541 z = Strsafe_find(y);
icculus9e44cf12010-02-14 17:14:22 +00004542 if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
drh898799f2014-01-10 23:21:00 +00004543 lemon_strcpy(cpy,y);
icculus9e44cf12010-02-14 17:14:22 +00004544 z = cpy;
drh75897232000-05-29 14:26:00 +00004545 Strsafe_insert(z);
4546 }
4547 MemoryCheck(z);
4548 return z;
4549}
4550
4551/* There is one instance of the following structure for each
4552** associative array of type "x1".
4553*/
4554struct s_x1 {
4555 int size; /* The number of available slots. */
4556 /* Must be a power of 2 greater than or */
4557 /* equal to 1 */
4558 int count; /* Number of currently slots filled */
4559 struct s_x1node *tbl; /* The data stored here */
4560 struct s_x1node **ht; /* Hash table for lookups */
4561};
4562
4563/* There is one instance of this structure for every data element
4564** in an associative array of type "x1".
4565*/
4566typedef struct s_x1node {
icculus9e44cf12010-02-14 17:14:22 +00004567 const char *data; /* The data */
drh75897232000-05-29 14:26:00 +00004568 struct s_x1node *next; /* Next entry with the same hash */
4569 struct s_x1node **from; /* Previous link */
4570} x1node;
4571
4572/* There is only one instance of the array, which is the following */
4573static struct s_x1 *x1a;
4574
4575/* Allocate a new associative array */
4576void Strsafe_init(){
4577 if( x1a ) return;
4578 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
4579 if( x1a ){
4580 x1a->size = 1024;
4581 x1a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00004582 x1a->tbl = (x1node*)calloc(1024, sizeof(x1node) + sizeof(x1node*));
drh75897232000-05-29 14:26:00 +00004583 if( x1a->tbl==0 ){
4584 free(x1a);
4585 x1a = 0;
4586 }else{
4587 int i;
4588 x1a->ht = (x1node**)&(x1a->tbl[1024]);
4589 for(i=0; i<1024; i++) x1a->ht[i] = 0;
4590 }
4591 }
4592}
4593/* Insert a new record into the array. Return TRUE if successful.
4594** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004595int Strsafe_insert(const char *data)
drh75897232000-05-29 14:26:00 +00004596{
4597 x1node *np;
drh01f75f22013-10-02 20:46:30 +00004598 unsigned h;
4599 unsigned ph;
drh75897232000-05-29 14:26:00 +00004600
4601 if( x1a==0 ) return 0;
4602 ph = strhash(data);
4603 h = ph & (x1a->size-1);
4604 np = x1a->ht[h];
4605 while( np ){
4606 if( strcmp(np->data,data)==0 ){
4607 /* An existing entry with the same key is found. */
4608 /* Fail because overwrite is not allows. */
4609 return 0;
4610 }
4611 np = np->next;
4612 }
4613 if( x1a->count>=x1a->size ){
4614 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00004615 int i,arrSize;
drh75897232000-05-29 14:26:00 +00004616 struct s_x1 array;
mistachkin8e189222015-04-19 21:43:16 +00004617 array.size = arrSize = x1a->size*2;
drh75897232000-05-29 14:26:00 +00004618 array.count = x1a->count;
mistachkin8e189222015-04-19 21:43:16 +00004619 array.tbl = (x1node*)calloc(arrSize, sizeof(x1node) + sizeof(x1node*));
drh75897232000-05-29 14:26:00 +00004620 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00004621 array.ht = (x1node**)&(array.tbl[arrSize]);
4622 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00004623 for(i=0; i<x1a->count; i++){
4624 x1node *oldnp, *newnp;
4625 oldnp = &(x1a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00004626 h = strhash(oldnp->data) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00004627 newnp = &(array.tbl[i]);
4628 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4629 newnp->next = array.ht[h];
4630 newnp->data = oldnp->data;
4631 newnp->from = &(array.ht[h]);
4632 array.ht[h] = newnp;
4633 }
4634 free(x1a->tbl);
4635 *x1a = array;
4636 }
4637 /* Insert the new data */
4638 h = ph & (x1a->size-1);
4639 np = &(x1a->tbl[x1a->count++]);
4640 np->data = data;
4641 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
4642 np->next = x1a->ht[h];
4643 x1a->ht[h] = np;
4644 np->from = &(x1a->ht[h]);
4645 return 1;
4646}
4647
4648/* Return a pointer to data assigned to the given key. Return NULL
4649** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004650const char *Strsafe_find(const char *key)
drh75897232000-05-29 14:26:00 +00004651{
drh01f75f22013-10-02 20:46:30 +00004652 unsigned h;
drh75897232000-05-29 14:26:00 +00004653 x1node *np;
4654
4655 if( x1a==0 ) return 0;
4656 h = strhash(key) & (x1a->size-1);
4657 np = x1a->ht[h];
4658 while( np ){
4659 if( strcmp(np->data,key)==0 ) break;
4660 np = np->next;
4661 }
4662 return np ? np->data : 0;
4663}
4664
4665/* Return a pointer to the (terminal or nonterminal) symbol "x".
4666** Create a new symbol if this is the first time "x" has been seen.
4667*/
icculus9e44cf12010-02-14 17:14:22 +00004668struct symbol *Symbol_new(const char *x)
drh75897232000-05-29 14:26:00 +00004669{
4670 struct symbol *sp;
4671
4672 sp = Symbol_find(x);
4673 if( sp==0 ){
drh9892c5d2007-12-21 00:02:11 +00004674 sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
drh75897232000-05-29 14:26:00 +00004675 MemoryCheck(sp);
4676 sp->name = Strsafe(x);
drhc56fac72015-10-29 13:48:15 +00004677 sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL;
drh75897232000-05-29 14:26:00 +00004678 sp->rule = 0;
drh0bd1f4e2002-06-06 18:54:39 +00004679 sp->fallback = 0;
drh75897232000-05-29 14:26:00 +00004680 sp->prec = -1;
4681 sp->assoc = UNK;
4682 sp->firstset = 0;
drhaa9f1122007-08-23 02:50:56 +00004683 sp->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00004684 sp->destructor = 0;
drh4dc8ef52008-07-01 17:13:57 +00004685 sp->destLineno = 0;
drh75897232000-05-29 14:26:00 +00004686 sp->datatype = 0;
drhc4dd3fd2008-01-22 01:48:05 +00004687 sp->useCnt = 0;
drh75897232000-05-29 14:26:00 +00004688 Symbol_insert(sp,sp->name);
4689 }
drhc4dd3fd2008-01-22 01:48:05 +00004690 sp->useCnt++;
drh75897232000-05-29 14:26:00 +00004691 return sp;
4692}
4693
drh61f92cd2014-01-11 03:06:18 +00004694/* Compare two symbols for sorting purposes. Return negative,
4695** zero, or positive if a is less then, equal to, or greater
4696** than b.
drh60d31652004-02-22 00:08:04 +00004697**
4698** Symbols that begin with upper case letters (terminals or tokens)
4699** must sort before symbols that begin with lower case letters
drh61f92cd2014-01-11 03:06:18 +00004700** (non-terminals). And MULTITERMINAL symbols (created using the
4701** %token_class directive) must sort at the very end. Other than
4702** that, the order does not matter.
drh60d31652004-02-22 00:08:04 +00004703**
4704** We find experimentally that leaving the symbols in their original
4705** order (the order they appeared in the grammar file) gives the
4706** smallest parser tables in SQLite.
4707*/
icculus9e44cf12010-02-14 17:14:22 +00004708int Symbolcmpp(const void *_a, const void *_b)
4709{
drh61f92cd2014-01-11 03:06:18 +00004710 const struct symbol *a = *(const struct symbol **) _a;
4711 const struct symbol *b = *(const struct symbol **) _b;
4712 int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1;
4713 int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1;
4714 return i1==i2 ? a->index - b->index : i1 - i2;
drh75897232000-05-29 14:26:00 +00004715}
4716
4717/* There is one instance of the following structure for each
4718** associative array of type "x2".
4719*/
4720struct s_x2 {
4721 int size; /* The number of available slots. */
4722 /* Must be a power of 2 greater than or */
4723 /* equal to 1 */
4724 int count; /* Number of currently slots filled */
4725 struct s_x2node *tbl; /* The data stored here */
4726 struct s_x2node **ht; /* Hash table for lookups */
4727};
4728
4729/* There is one instance of this structure for every data element
4730** in an associative array of type "x2".
4731*/
4732typedef struct s_x2node {
icculus9e44cf12010-02-14 17:14:22 +00004733 struct symbol *data; /* The data */
4734 const char *key; /* The key */
drh75897232000-05-29 14:26:00 +00004735 struct s_x2node *next; /* Next entry with the same hash */
4736 struct s_x2node **from; /* Previous link */
4737} x2node;
4738
4739/* There is only one instance of the array, which is the following */
4740static struct s_x2 *x2a;
4741
4742/* Allocate a new associative array */
4743void Symbol_init(){
4744 if( x2a ) return;
4745 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
4746 if( x2a ){
4747 x2a->size = 128;
4748 x2a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00004749 x2a->tbl = (x2node*)calloc(128, sizeof(x2node) + sizeof(x2node*));
drh75897232000-05-29 14:26:00 +00004750 if( x2a->tbl==0 ){
4751 free(x2a);
4752 x2a = 0;
4753 }else{
4754 int i;
4755 x2a->ht = (x2node**)&(x2a->tbl[128]);
4756 for(i=0; i<128; i++) x2a->ht[i] = 0;
4757 }
4758 }
4759}
4760/* Insert a new record into the array. Return TRUE if successful.
4761** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004762int Symbol_insert(struct symbol *data, const char *key)
drh75897232000-05-29 14:26:00 +00004763{
4764 x2node *np;
drh01f75f22013-10-02 20:46:30 +00004765 unsigned h;
4766 unsigned ph;
drh75897232000-05-29 14:26:00 +00004767
4768 if( x2a==0 ) return 0;
4769 ph = strhash(key);
4770 h = ph & (x2a->size-1);
4771 np = x2a->ht[h];
4772 while( np ){
4773 if( strcmp(np->key,key)==0 ){
4774 /* An existing entry with the same key is found. */
4775 /* Fail because overwrite is not allows. */
4776 return 0;
4777 }
4778 np = np->next;
4779 }
4780 if( x2a->count>=x2a->size ){
4781 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00004782 int i,arrSize;
drh75897232000-05-29 14:26:00 +00004783 struct s_x2 array;
mistachkin8e189222015-04-19 21:43:16 +00004784 array.size = arrSize = x2a->size*2;
drh75897232000-05-29 14:26:00 +00004785 array.count = x2a->count;
mistachkin8e189222015-04-19 21:43:16 +00004786 array.tbl = (x2node*)calloc(arrSize, sizeof(x2node) + sizeof(x2node*));
drh75897232000-05-29 14:26:00 +00004787 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00004788 array.ht = (x2node**)&(array.tbl[arrSize]);
4789 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00004790 for(i=0; i<x2a->count; i++){
4791 x2node *oldnp, *newnp;
4792 oldnp = &(x2a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00004793 h = strhash(oldnp->key) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00004794 newnp = &(array.tbl[i]);
4795 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4796 newnp->next = array.ht[h];
4797 newnp->key = oldnp->key;
4798 newnp->data = oldnp->data;
4799 newnp->from = &(array.ht[h]);
4800 array.ht[h] = newnp;
4801 }
4802 free(x2a->tbl);
4803 *x2a = array;
4804 }
4805 /* Insert the new data */
4806 h = ph & (x2a->size-1);
4807 np = &(x2a->tbl[x2a->count++]);
4808 np->key = key;
4809 np->data = data;
4810 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
4811 np->next = x2a->ht[h];
4812 x2a->ht[h] = np;
4813 np->from = &(x2a->ht[h]);
4814 return 1;
4815}
4816
4817/* Return a pointer to data assigned to the given key. Return NULL
4818** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004819struct symbol *Symbol_find(const char *key)
drh75897232000-05-29 14:26:00 +00004820{
drh01f75f22013-10-02 20:46:30 +00004821 unsigned h;
drh75897232000-05-29 14:26:00 +00004822 x2node *np;
4823
4824 if( x2a==0 ) return 0;
4825 h = strhash(key) & (x2a->size-1);
4826 np = x2a->ht[h];
4827 while( np ){
4828 if( strcmp(np->key,key)==0 ) break;
4829 np = np->next;
4830 }
4831 return np ? np->data : 0;
4832}
4833
4834/* Return the n-th data. Return NULL if n is out of range. */
icculus9e44cf12010-02-14 17:14:22 +00004835struct symbol *Symbol_Nth(int n)
drh75897232000-05-29 14:26:00 +00004836{
4837 struct symbol *data;
4838 if( x2a && n>0 && n<=x2a->count ){
4839 data = x2a->tbl[n-1].data;
4840 }else{
4841 data = 0;
4842 }
4843 return data;
4844}
4845
4846/* Return the size of the array */
4847int Symbol_count()
4848{
4849 return x2a ? x2a->count : 0;
4850}
4851
4852/* Return an array of pointers to all data in the table.
4853** The array is obtained from malloc. Return NULL if memory allocation
4854** problems, or if the array is empty. */
4855struct symbol **Symbol_arrayof()
4856{
4857 struct symbol **array;
mistachkin8e189222015-04-19 21:43:16 +00004858 int i,arrSize;
drh75897232000-05-29 14:26:00 +00004859 if( x2a==0 ) return 0;
mistachkin8e189222015-04-19 21:43:16 +00004860 arrSize = x2a->count;
4861 array = (struct symbol **)calloc(arrSize, sizeof(struct symbol *));
drh75897232000-05-29 14:26:00 +00004862 if( array ){
mistachkin8e189222015-04-19 21:43:16 +00004863 for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data;
drh75897232000-05-29 14:26:00 +00004864 }
4865 return array;
4866}
4867
4868/* Compare two configurations */
icculus9e44cf12010-02-14 17:14:22 +00004869int Configcmp(const char *_a,const char *_b)
drh75897232000-05-29 14:26:00 +00004870{
icculus9e44cf12010-02-14 17:14:22 +00004871 const struct config *a = (struct config *) _a;
4872 const struct config *b = (struct config *) _b;
drh75897232000-05-29 14:26:00 +00004873 int x;
4874 x = a->rp->index - b->rp->index;
4875 if( x==0 ) x = a->dot - b->dot;
4876 return x;
4877}
4878
4879/* Compare two states */
icculus9e44cf12010-02-14 17:14:22 +00004880PRIVATE int statecmp(struct config *a, struct config *b)
drh75897232000-05-29 14:26:00 +00004881{
4882 int rc;
4883 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
4884 rc = a->rp->index - b->rp->index;
4885 if( rc==0 ) rc = a->dot - b->dot;
4886 }
4887 if( rc==0 ){
4888 if( a ) rc = 1;
4889 if( b ) rc = -1;
4890 }
4891 return rc;
4892}
4893
4894/* Hash a state */
drh01f75f22013-10-02 20:46:30 +00004895PRIVATE unsigned statehash(struct config *a)
drh75897232000-05-29 14:26:00 +00004896{
drh01f75f22013-10-02 20:46:30 +00004897 unsigned h=0;
drh75897232000-05-29 14:26:00 +00004898 while( a ){
4899 h = h*571 + a->rp->index*37 + a->dot;
4900 a = a->bp;
4901 }
4902 return h;
4903}
4904
4905/* Allocate a new state structure */
4906struct state *State_new()
4907{
icculus9e44cf12010-02-14 17:14:22 +00004908 struct state *newstate;
4909 newstate = (struct state *)calloc(1, sizeof(struct state) );
4910 MemoryCheck(newstate);
4911 return newstate;
drh75897232000-05-29 14:26:00 +00004912}
4913
4914/* There is one instance of the following structure for each
4915** associative array of type "x3".
4916*/
4917struct s_x3 {
4918 int size; /* The number of available slots. */
4919 /* Must be a power of 2 greater than or */
4920 /* equal to 1 */
4921 int count; /* Number of currently slots filled */
4922 struct s_x3node *tbl; /* The data stored here */
4923 struct s_x3node **ht; /* Hash table for lookups */
4924};
4925
4926/* There is one instance of this structure for every data element
4927** in an associative array of type "x3".
4928*/
4929typedef struct s_x3node {
4930 struct state *data; /* The data */
4931 struct config *key; /* The key */
4932 struct s_x3node *next; /* Next entry with the same hash */
4933 struct s_x3node **from; /* Previous link */
4934} x3node;
4935
4936/* There is only one instance of the array, which is the following */
4937static struct s_x3 *x3a;
4938
4939/* Allocate a new associative array */
4940void State_init(){
4941 if( x3a ) return;
4942 x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
4943 if( x3a ){
4944 x3a->size = 128;
4945 x3a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00004946 x3a->tbl = (x3node*)calloc(128, sizeof(x3node) + sizeof(x3node*));
drh75897232000-05-29 14:26:00 +00004947 if( x3a->tbl==0 ){
4948 free(x3a);
4949 x3a = 0;
4950 }else{
4951 int i;
4952 x3a->ht = (x3node**)&(x3a->tbl[128]);
4953 for(i=0; i<128; i++) x3a->ht[i] = 0;
4954 }
4955 }
4956}
4957/* Insert a new record into the array. Return TRUE if successful.
4958** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004959int State_insert(struct state *data, struct config *key)
drh75897232000-05-29 14:26:00 +00004960{
4961 x3node *np;
drh01f75f22013-10-02 20:46:30 +00004962 unsigned h;
4963 unsigned ph;
drh75897232000-05-29 14:26:00 +00004964
4965 if( x3a==0 ) return 0;
4966 ph = statehash(key);
4967 h = ph & (x3a->size-1);
4968 np = x3a->ht[h];
4969 while( np ){
4970 if( statecmp(np->key,key)==0 ){
4971 /* An existing entry with the same key is found. */
4972 /* Fail because overwrite is not allows. */
4973 return 0;
4974 }
4975 np = np->next;
4976 }
4977 if( x3a->count>=x3a->size ){
4978 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00004979 int i,arrSize;
drh75897232000-05-29 14:26:00 +00004980 struct s_x3 array;
mistachkin8e189222015-04-19 21:43:16 +00004981 array.size = arrSize = x3a->size*2;
drh75897232000-05-29 14:26:00 +00004982 array.count = x3a->count;
mistachkin8e189222015-04-19 21:43:16 +00004983 array.tbl = (x3node*)calloc(arrSize, sizeof(x3node) + sizeof(x3node*));
drh75897232000-05-29 14:26:00 +00004984 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00004985 array.ht = (x3node**)&(array.tbl[arrSize]);
4986 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00004987 for(i=0; i<x3a->count; i++){
4988 x3node *oldnp, *newnp;
4989 oldnp = &(x3a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00004990 h = statehash(oldnp->key) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00004991 newnp = &(array.tbl[i]);
4992 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4993 newnp->next = array.ht[h];
4994 newnp->key = oldnp->key;
4995 newnp->data = oldnp->data;
4996 newnp->from = &(array.ht[h]);
4997 array.ht[h] = newnp;
4998 }
4999 free(x3a->tbl);
5000 *x3a = array;
5001 }
5002 /* Insert the new data */
5003 h = ph & (x3a->size-1);
5004 np = &(x3a->tbl[x3a->count++]);
5005 np->key = key;
5006 np->data = data;
5007 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
5008 np->next = x3a->ht[h];
5009 x3a->ht[h] = np;
5010 np->from = &(x3a->ht[h]);
5011 return 1;
5012}
5013
5014/* Return a pointer to data assigned to the given key. Return NULL
5015** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005016struct state *State_find(struct config *key)
drh75897232000-05-29 14:26:00 +00005017{
drh01f75f22013-10-02 20:46:30 +00005018 unsigned h;
drh75897232000-05-29 14:26:00 +00005019 x3node *np;
5020
5021 if( x3a==0 ) return 0;
5022 h = statehash(key) & (x3a->size-1);
5023 np = x3a->ht[h];
5024 while( np ){
5025 if( statecmp(np->key,key)==0 ) break;
5026 np = np->next;
5027 }
5028 return np ? np->data : 0;
5029}
5030
5031/* Return an array of pointers to all data in the table.
5032** The array is obtained from malloc. Return NULL if memory allocation
5033** problems, or if the array is empty. */
5034struct state **State_arrayof()
5035{
5036 struct state **array;
mistachkin8e189222015-04-19 21:43:16 +00005037 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005038 if( x3a==0 ) return 0;
mistachkin8e189222015-04-19 21:43:16 +00005039 arrSize = x3a->count;
5040 array = (struct state **)calloc(arrSize, sizeof(struct state *));
drh75897232000-05-29 14:26:00 +00005041 if( array ){
mistachkin8e189222015-04-19 21:43:16 +00005042 for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data;
drh75897232000-05-29 14:26:00 +00005043 }
5044 return array;
5045}
5046
5047/* Hash a configuration */
drh01f75f22013-10-02 20:46:30 +00005048PRIVATE unsigned confighash(struct config *a)
drh75897232000-05-29 14:26:00 +00005049{
drh01f75f22013-10-02 20:46:30 +00005050 unsigned h=0;
drh75897232000-05-29 14:26:00 +00005051 h = h*571 + a->rp->index*37 + a->dot;
5052 return h;
5053}
5054
5055/* There is one instance of the following structure for each
5056** associative array of type "x4".
5057*/
5058struct s_x4 {
5059 int size; /* The number of available slots. */
5060 /* Must be a power of 2 greater than or */
5061 /* equal to 1 */
5062 int count; /* Number of currently slots filled */
5063 struct s_x4node *tbl; /* The data stored here */
5064 struct s_x4node **ht; /* Hash table for lookups */
5065};
5066
5067/* There is one instance of this structure for every data element
5068** in an associative array of type "x4".
5069*/
5070typedef struct s_x4node {
5071 struct config *data; /* The data */
5072 struct s_x4node *next; /* Next entry with the same hash */
5073 struct s_x4node **from; /* Previous link */
5074} x4node;
5075
5076/* There is only one instance of the array, which is the following */
5077static struct s_x4 *x4a;
5078
5079/* Allocate a new associative array */
5080void Configtable_init(){
5081 if( x4a ) return;
5082 x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
5083 if( x4a ){
5084 x4a->size = 64;
5085 x4a->count = 0;
drh03e1b1f2014-01-11 12:52:25 +00005086 x4a->tbl = (x4node*)calloc(64, sizeof(x4node) + sizeof(x4node*));
drh75897232000-05-29 14:26:00 +00005087 if( x4a->tbl==0 ){
5088 free(x4a);
5089 x4a = 0;
5090 }else{
5091 int i;
5092 x4a->ht = (x4node**)&(x4a->tbl[64]);
5093 for(i=0; i<64; i++) x4a->ht[i] = 0;
5094 }
5095 }
5096}
5097/* Insert a new record into the array. Return TRUE if successful.
5098** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00005099int Configtable_insert(struct config *data)
drh75897232000-05-29 14:26:00 +00005100{
5101 x4node *np;
drh01f75f22013-10-02 20:46:30 +00005102 unsigned h;
5103 unsigned ph;
drh75897232000-05-29 14:26:00 +00005104
5105 if( x4a==0 ) return 0;
5106 ph = confighash(data);
5107 h = ph & (x4a->size-1);
5108 np = x4a->ht[h];
5109 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00005110 if( Configcmp((const char *) np->data,(const char *) data)==0 ){
drh75897232000-05-29 14:26:00 +00005111 /* An existing entry with the same key is found. */
5112 /* Fail because overwrite is not allows. */
5113 return 0;
5114 }
5115 np = np->next;
5116 }
5117 if( x4a->count>=x4a->size ){
5118 /* Need to make the hash table bigger */
mistachkin8e189222015-04-19 21:43:16 +00005119 int i,arrSize;
drh75897232000-05-29 14:26:00 +00005120 struct s_x4 array;
mistachkin8e189222015-04-19 21:43:16 +00005121 array.size = arrSize = x4a->size*2;
drh75897232000-05-29 14:26:00 +00005122 array.count = x4a->count;
mistachkin8e189222015-04-19 21:43:16 +00005123 array.tbl = (x4node*)calloc(arrSize, sizeof(x4node) + sizeof(x4node*));
drh75897232000-05-29 14:26:00 +00005124 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
mistachkin8e189222015-04-19 21:43:16 +00005125 array.ht = (x4node**)&(array.tbl[arrSize]);
5126 for(i=0; i<arrSize; i++) array.ht[i] = 0;
drh75897232000-05-29 14:26:00 +00005127 for(i=0; i<x4a->count; i++){
5128 x4node *oldnp, *newnp;
5129 oldnp = &(x4a->tbl[i]);
mistachkin8e189222015-04-19 21:43:16 +00005130 h = confighash(oldnp->data) & (arrSize-1);
drh75897232000-05-29 14:26:00 +00005131 newnp = &(array.tbl[i]);
5132 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
5133 newnp->next = array.ht[h];
5134 newnp->data = oldnp->data;
5135 newnp->from = &(array.ht[h]);
5136 array.ht[h] = newnp;
5137 }
5138 free(x4a->tbl);
5139 *x4a = array;
5140 }
5141 /* Insert the new data */
5142 h = ph & (x4a->size-1);
5143 np = &(x4a->tbl[x4a->count++]);
5144 np->data = data;
5145 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
5146 np->next = x4a->ht[h];
5147 x4a->ht[h] = np;
5148 np->from = &(x4a->ht[h]);
5149 return 1;
5150}
5151
5152/* Return a pointer to data assigned to the given key. Return NULL
5153** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00005154struct config *Configtable_find(struct config *key)
drh75897232000-05-29 14:26:00 +00005155{
5156 int h;
5157 x4node *np;
5158
5159 if( x4a==0 ) return 0;
5160 h = confighash(key) & (x4a->size-1);
5161 np = x4a->ht[h];
5162 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00005163 if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
drh75897232000-05-29 14:26:00 +00005164 np = np->next;
5165 }
5166 return np ? np->data : 0;
5167}
5168
5169/* Remove all data from the table. Pass each data to the function "f"
5170** as it is removed. ("f" may be null to avoid this step.) */
icculus9e44cf12010-02-14 17:14:22 +00005171void Configtable_clear(int(*f)(struct config *))
drh75897232000-05-29 14:26:00 +00005172{
5173 int i;
5174 if( x4a==0 || x4a->count==0 ) return;
5175 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
5176 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
5177 x4a->count = 0;
5178 return;
5179}