blob: 191ab0d88bd7542b8e773f25e820ae1915042804 [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
drh75897232000-05-29 14:26:00 +000016#ifndef __WIN32__
17# if defined(_WIN32) || defined(WIN32)
drhf2f105d2012-08-20 15:53:54 +000018# define __WIN32__
drh75897232000-05-29 14:26:00 +000019# endif
20#endif
21
rse8f304482007-07-30 18:31:53 +000022#ifdef __WIN32__
drhdf609712010-11-23 20:55:27 +000023#ifdef __cplusplus
24extern "C" {
25#endif
26extern int access(const char *path, int mode);
27#ifdef __cplusplus
28}
29#endif
rse8f304482007-07-30 18:31:53 +000030#else
31#include <unistd.h>
32#endif
33
drh75897232000-05-29 14:26:00 +000034/* #define PRIVATE static */
35#define PRIVATE
36
37#ifdef TEST
38#define MAXRHS 5 /* Set low to exercise exception code */
39#else
40#define MAXRHS 1000
41#endif
42
drhf5c4e0f2010-07-18 11:35:53 +000043static int showPrecedenceConflict = 0;
drhe9278182007-07-18 18:16:29 +000044static char *msort(char*,char**,int(*)(const char*,const char*));
drh75897232000-05-29 14:26:00 +000045
drh87cf1372008-08-13 20:09:06 +000046/*
47** Compilers are getting increasingly pedantic about type conversions
48** as C evolves ever closer to Ada.... To work around the latest problems
49** we have to define the following variant of strlen().
50*/
51#define lemonStrlen(X) ((int)strlen(X))
52
drh898799f2014-01-10 23:21:00 +000053/*
54** Compilers are starting to complain about the use of sprintf() and strcpy(),
55** saying they are unsafe. So we define our own versions of those routines too.
56**
57** There are three routines here: lemon_sprintf(), lemon_vsprintf(), and
58** lemon_addtext(). The first two are replacements for sprintf() and vsprintf().
59** The third is a helper routine for vsnprintf() that adds texts to the end of a
60** buffer, making sure the buffer is always zero-terminated.
61**
62** The string formatter is a minimal subset of stdlib sprintf() supporting only
63** a few simply conversions:
64**
65** %d
66** %s
67** %.*s
68**
69*/
70static void lemon_addtext(
71 char *zBuf, /* The buffer to which text is added */
72 int *pnUsed, /* Slots of the buffer used so far */
73 const char *zIn, /* Text to add */
74 int nIn /* Bytes of text to add. -1 to use strlen() */
75){
76 if( nIn<0 ) for(nIn=0; zIn[nIn]; nIn++){}
77 if( nIn==0 ) return;
78 memcpy(&zBuf[*pnUsed], zIn, nIn);
79 *pnUsed += nIn;
80 zBuf[*pnUsed] = 0;
81}
82static int lemon_vsprintf(char *str, const char *zFormat, va_list ap){
83 int i, j, k, c, size;
84 int nUsed = 0;
85 const char *z;
86 char zTemp[50];
87 str[0] = 0;
88 for(i=j=0; (c = zFormat[i])!=0; i++){
89 if( c=='%' ){
90 lemon_addtext(str, &nUsed, &zFormat[j], i-j);
91 c = zFormat[++i];
92 if( c=='d' ){
93 int v = va_arg(ap, int);
94 if( v<0 ){
95 lemon_addtext(str, &nUsed, "-", 1);
96 v = -v;
97 }else if( v==0 ){
98 lemon_addtext(str, &nUsed, "0", 1);
99 }
100 k = 0;
101 while( v>0 ){
102 k++;
103 zTemp[sizeof(zTemp)-k] = (v%10) + '0';
104 v /= 10;
105 }
106 lemon_addtext(str, &nUsed, &zTemp[sizeof(zTemp)-k], k);
107 }else if( c=='s' ){
108 z = va_arg(ap, const char*);
109 lemon_addtext(str, &nUsed, z, -1);
110 }else if( c=='.' && memcmp(&zFormat[i], ".*s", 3)==0 ){
111 i += 2;
112 k = va_arg(ap, int);
113 z = va_arg(ap, const char*);
114 lemon_addtext(str, &nUsed, z, k);
115 }else if( c=='%' ){
116 lemon_addtext(str, &nUsed, "%", 1);
117 }else{
118 fprintf(stderr, "illegal format\n");
119 exit(1);
120 }
121 j = i+1;
122 }
123 }
124 lemon_addtext(str, &nUsed, &zFormat[j], i-j);
125 return nUsed;
126}
127static int lemon_sprintf(char *str, const char *format, ...){
128 va_list ap;
129 int rc;
130 va_start(ap, format);
131 rc = lemon_vsprintf(str, format, ap);
132 va_end(ap);
133 return rc;
134}
135static void lemon_strcpy(char *dest, const char *src){
136 while( (*(dest++) = *(src++))!=0 ){}
137}
138static void lemon_strcat(char *dest, const char *src){
139 while( *dest ) dest++;
140 lemon_strcpy(dest, src);
141}
142
143
icculus9e44cf12010-02-14 17:14:22 +0000144/* a few forward declarations... */
145struct rule;
146struct lemon;
147struct action;
148
drhe9278182007-07-18 18:16:29 +0000149static struct action *Action_new(void);
150static struct action *Action_sort(struct action *);
drh75897232000-05-29 14:26:00 +0000151
152/********** From the file "build.h" ************************************/
153void FindRulePrecedences();
154void FindFirstSets();
155void FindStates();
156void FindLinks();
157void FindFollowSets();
158void FindActions();
159
160/********* From the file "configlist.h" *********************************/
icculus9e44cf12010-02-14 17:14:22 +0000161void Configlist_init(void);
162struct config *Configlist_add(struct rule *, int);
163struct config *Configlist_addbasis(struct rule *, int);
164void Configlist_closure(struct lemon *);
165void Configlist_sort(void);
166void Configlist_sortbasis(void);
167struct config *Configlist_return(void);
168struct config *Configlist_basis(void);
169void Configlist_eat(struct config *);
170void Configlist_reset(void);
drh75897232000-05-29 14:26:00 +0000171
172/********* From the file "error.h" ***************************************/
drhf9a2e7b2003-04-15 01:49:48 +0000173void ErrorMsg(const char *, int,const char *, ...);
drh75897232000-05-29 14:26:00 +0000174
175/****** From the file "option.h" ******************************************/
icculus9e44cf12010-02-14 17:14:22 +0000176enum option_type { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR,
177 OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR};
drh75897232000-05-29 14:26:00 +0000178struct s_options {
icculus9e44cf12010-02-14 17:14:22 +0000179 enum option_type type;
180 const char *label;
drh75897232000-05-29 14:26:00 +0000181 char *arg;
icculus9e44cf12010-02-14 17:14:22 +0000182 const char *message;
drh75897232000-05-29 14:26:00 +0000183};
icculus9e44cf12010-02-14 17:14:22 +0000184int OptInit(char**,struct s_options*,FILE*);
185int OptNArgs(void);
186char *OptArg(int);
187void OptErr(int);
188void OptPrint(void);
drh75897232000-05-29 14:26:00 +0000189
190/******** From the file "parse.h" *****************************************/
icculus9e44cf12010-02-14 17:14:22 +0000191void Parse(struct lemon *lemp);
drh75897232000-05-29 14:26:00 +0000192
193/********* From the file "plink.h" ***************************************/
icculus9e44cf12010-02-14 17:14:22 +0000194struct plink *Plink_new(void);
195void Plink_add(struct plink **, struct config *);
196void Plink_copy(struct plink **, struct plink *);
197void Plink_delete(struct plink *);
drh75897232000-05-29 14:26:00 +0000198
199/********** From the file "report.h" *************************************/
icculus9e44cf12010-02-14 17:14:22 +0000200void Reprint(struct lemon *);
201void ReportOutput(struct lemon *);
202void ReportTable(struct lemon *, int);
203void ReportHeader(struct lemon *);
204void CompressTables(struct lemon *);
205void ResortStates(struct lemon *);
drh75897232000-05-29 14:26:00 +0000206
207/********** From the file "set.h" ****************************************/
icculus9e44cf12010-02-14 17:14:22 +0000208void SetSize(int); /* All sets will be of size N */
209char *SetNew(void); /* A new set for element 0..N */
210void SetFree(char*); /* Deallocate a set */
icculus9e44cf12010-02-14 17:14:22 +0000211int SetAdd(char*,int); /* Add element to a set */
212int SetUnion(char *,char *); /* A <- A U B, thru element N */
drh75897232000-05-29 14:26:00 +0000213#define SetFind(X,Y) (X[Y]) /* True if Y is in set X */
214
215/********** From the file "struct.h" *************************************/
216/*
217** Principal data structures for the LEMON parser generator.
218*/
219
drhaa9f1122007-08-23 02:50:56 +0000220typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean;
drh75897232000-05-29 14:26:00 +0000221
222/* Symbols (terminals and nonterminals) of the grammar are stored
223** in the following: */
icculus9e44cf12010-02-14 17:14:22 +0000224enum symbol_type {
225 TERMINAL,
226 NONTERMINAL,
227 MULTITERMINAL
228};
229enum e_assoc {
drh75897232000-05-29 14:26:00 +0000230 LEFT,
231 RIGHT,
232 NONE,
233 UNK
icculus9e44cf12010-02-14 17:14:22 +0000234};
235struct symbol {
236 const char *name; /* Name of the symbol */
237 int index; /* Index number for this symbol */
238 enum symbol_type type; /* Symbols are all either TERMINALS or NTs */
239 struct rule *rule; /* Linked list of rules of this (if an NT) */
240 struct symbol *fallback; /* fallback token in case this token doesn't parse */
241 int prec; /* Precedence if defined (-1 otherwise) */
242 enum e_assoc assoc; /* Associativity if precedence is defined */
drh75897232000-05-29 14:26:00 +0000243 char *firstset; /* First-set for all rules of this symbol */
244 Boolean lambda; /* True if NT and can generate an empty string */
drhc4dd3fd2008-01-22 01:48:05 +0000245 int useCnt; /* Number of times used */
drh75897232000-05-29 14:26:00 +0000246 char *destructor; /* Code which executes whenever this symbol is
247 ** popped from the stack during error processing */
drh4dc8ef52008-07-01 17:13:57 +0000248 int destLineno; /* Line number for start of destructor */
drh75897232000-05-29 14:26:00 +0000249 char *datatype; /* The data type of information held by this
250 ** object. Only used if type==NONTERMINAL */
251 int dtnum; /* The data type number. In the parser, the value
252 ** stack is a union. The .yy%d element of this
253 ** union is the correct data type for this object */
drhfd405312005-11-06 04:06:59 +0000254 /* The following fields are used by MULTITERMINALs only */
255 int nsubsym; /* Number of constituent symbols in the MULTI */
256 struct symbol **subsym; /* Array of constituent symbols */
drh75897232000-05-29 14:26:00 +0000257};
258
259/* Each production rule in the grammar is stored in the following
260** structure. */
261struct rule {
262 struct symbol *lhs; /* Left-hand side of the rule */
icculus9e44cf12010-02-14 17:14:22 +0000263 const char *lhsalias; /* Alias for the LHS (NULL if none) */
drhb4960992007-10-05 16:16:36 +0000264 int lhsStart; /* True if left-hand side is the start symbol */
drh75897232000-05-29 14:26:00 +0000265 int ruleline; /* Line number for the rule */
266 int nrhs; /* Number of RHS symbols */
267 struct symbol **rhs; /* The RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +0000268 const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */
drh75897232000-05-29 14:26:00 +0000269 int line; /* Line number at which code begins */
icculus9e44cf12010-02-14 17:14:22 +0000270 const char *code; /* The code executed when this rule is reduced */
drh75897232000-05-29 14:26:00 +0000271 struct symbol *precsym; /* Precedence symbol for this rule */
272 int index; /* An index number for this rule */
273 Boolean canReduce; /* True if this rule is ever reduced */
274 struct rule *nextlhs; /* Next rule with the same LHS */
275 struct rule *next; /* Next rule in the global list */
276};
277
278/* A configuration is a production rule of the grammar together with
279** a mark (dot) showing how much of that rule has been processed so far.
280** Configurations also contain a follow-set which is a list of terminal
281** symbols which are allowed to immediately follow the end of the rule.
282** Every configuration is recorded as an instance of the following: */
icculus9e44cf12010-02-14 17:14:22 +0000283enum cfgstatus {
284 COMPLETE,
285 INCOMPLETE
286};
drh75897232000-05-29 14:26:00 +0000287struct config {
288 struct rule *rp; /* The rule upon which the configuration is based */
289 int dot; /* The parse point */
290 char *fws; /* Follow-set for this configuration only */
291 struct plink *fplp; /* Follow-set forward propagation links */
292 struct plink *bplp; /* Follow-set backwards propagation links */
293 struct state *stp; /* Pointer to state which contains this */
icculus9e44cf12010-02-14 17:14:22 +0000294 enum cfgstatus status; /* used during followset and shift computations */
drh75897232000-05-29 14:26:00 +0000295 struct config *next; /* Next configuration in the state */
296 struct config *bp; /* The next basis configuration */
297};
298
icculus9e44cf12010-02-14 17:14:22 +0000299enum e_action {
300 SHIFT,
301 ACCEPT,
302 REDUCE,
303 ERROR,
304 SSCONFLICT, /* A shift/shift conflict */
305 SRCONFLICT, /* Was a reduce, but part of a conflict */
306 RRCONFLICT, /* Was a reduce, but part of a conflict */
307 SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
308 RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
309 NOT_USED /* Deleted by compression */
310};
311
drh75897232000-05-29 14:26:00 +0000312/* Every shift or reduce operation is stored as one of the following */
313struct action {
314 struct symbol *sp; /* The look-ahead symbol */
icculus9e44cf12010-02-14 17:14:22 +0000315 enum e_action type;
drh75897232000-05-29 14:26:00 +0000316 union {
317 struct state *stp; /* The new state, if a shift */
318 struct rule *rp; /* The rule, if a reduce */
319 } x;
320 struct action *next; /* Next action for this state */
321 struct action *collide; /* Next action with the same hash */
322};
323
324/* Each state of the generated parser's finite state machine
325** is encoded as an instance of the following structure. */
326struct state {
327 struct config *bp; /* The basis configurations for this state */
328 struct config *cfp; /* All configurations in this set */
drh34ff57b2008-07-14 12:27:51 +0000329 int statenum; /* Sequential number for this state */
drh75897232000-05-29 14:26:00 +0000330 struct action *ap; /* Array of actions for this state */
drh8b582012003-10-21 13:16:03 +0000331 int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */
332 int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */
333 int iDflt; /* Default action */
drh75897232000-05-29 14:26:00 +0000334};
drh8b582012003-10-21 13:16:03 +0000335#define NO_OFFSET (-2147483647)
drh75897232000-05-29 14:26:00 +0000336
337/* A followset propagation link indicates that the contents of one
338** configuration followset should be propagated to another whenever
339** the first changes. */
340struct plink {
341 struct config *cfp; /* The configuration to which linked */
342 struct plink *next; /* The next propagate link */
343};
344
345/* The state vector for the entire parser generator is recorded as
346** follows. (LEMON uses no global variables and makes little use of
347** static variables. Fields in the following structure can be thought
348** of as begin global variables in the program.) */
349struct lemon {
350 struct state **sorted; /* Table of states sorted by state number */
351 struct rule *rule; /* List of all rules */
352 int nstate; /* Number of states */
353 int nrule; /* Number of rules */
354 int nsymbol; /* Number of terminal and nonterminal symbols */
355 int nterminal; /* Number of terminal symbols */
356 struct symbol **symbols; /* Sorted array of pointers to symbols */
357 int errorcnt; /* Number of errors */
358 struct symbol *errsym; /* The error symbol */
drhe09daa92006-06-10 13:29:31 +0000359 struct symbol *wildcard; /* Token that matches anything */
drh75897232000-05-29 14:26:00 +0000360 char *name; /* Name of the generated parser */
361 char *arg; /* Declaration of the 3th argument to parser */
362 char *tokentype; /* Type of terminal symbols in the parser stack */
drh960e8c62001-04-03 16:53:21 +0000363 char *vartype; /* The default type of non-terminal symbols */
drh75897232000-05-29 14:26:00 +0000364 char *start; /* Name of the start symbol for the grammar */
365 char *stacksize; /* Size of the parser stack */
366 char *include; /* Code to put at the start of the C file */
drh75897232000-05-29 14:26:00 +0000367 char *error; /* Code to execute when an error is seen */
drh75897232000-05-29 14:26:00 +0000368 char *overflow; /* Code to execute on a stack overflow */
drh75897232000-05-29 14:26:00 +0000369 char *failure; /* Code to execute on parser failure */
drh75897232000-05-29 14:26:00 +0000370 char *accept; /* Code to execute when the parser excepts */
drh75897232000-05-29 14:26:00 +0000371 char *extracode; /* Code appended to the generated file */
drh75897232000-05-29 14:26:00 +0000372 char *tokendest; /* Code to execute to destroy token data */
drh960e8c62001-04-03 16:53:21 +0000373 char *vardest; /* Code for the default non-terminal destructor */
drh75897232000-05-29 14:26:00 +0000374 char *filename; /* Name of the input file */
375 char *outname; /* Name of the current output file */
376 char *tokenprefix; /* A prefix added to token names in the .h file */
377 int nconflict; /* Number of parsing conflicts */
378 int tablesize; /* Size of the parse tables */
379 int basisflag; /* Print only basis configurations */
drh34ff57b2008-07-14 12:27:51 +0000380 int has_fallback; /* True if any %fallback is seen in the grammar */
shane58543932008-12-10 20:10:04 +0000381 int nolinenosflag; /* True if #line statements should not be printed */
drh75897232000-05-29 14:26:00 +0000382 char *argv0; /* Name of the program */
383};
384
385#define MemoryCheck(X) if((X)==0){ \
386 extern void memory_error(); \
387 memory_error(); \
388}
389
390/**************** From the file "table.h" *********************************/
391/*
392** All code in this file has been automatically generated
393** from a specification in the file
394** "table.q"
395** by the associative array code building program "aagen".
396** Do not edit this file! Instead, edit the specification
397** file, then rerun aagen.
398*/
399/*
400** Code for processing tables in the LEMON parser generator.
401*/
drh75897232000-05-29 14:26:00 +0000402/* Routines for handling a strings */
403
icculus9e44cf12010-02-14 17:14:22 +0000404const char *Strsafe(const char *);
drh75897232000-05-29 14:26:00 +0000405
icculus9e44cf12010-02-14 17:14:22 +0000406void Strsafe_init(void);
407int Strsafe_insert(const char *);
408const char *Strsafe_find(const char *);
drh75897232000-05-29 14:26:00 +0000409
410/* Routines for handling symbols of the grammar */
411
icculus9e44cf12010-02-14 17:14:22 +0000412struct symbol *Symbol_new(const char *);
413int Symbolcmpp(const void *, const void *);
414void Symbol_init(void);
415int Symbol_insert(struct symbol *, const char *);
416struct symbol *Symbol_find(const char *);
417struct symbol *Symbol_Nth(int);
418int Symbol_count(void);
419struct symbol **Symbol_arrayof(void);
drh75897232000-05-29 14:26:00 +0000420
421/* Routines to manage the state table */
422
icculus9e44cf12010-02-14 17:14:22 +0000423int Configcmp(const char *, const char *);
424struct state *State_new(void);
425void State_init(void);
426int State_insert(struct state *, struct config *);
427struct state *State_find(struct config *);
drh75897232000-05-29 14:26:00 +0000428struct state **State_arrayof(/* */);
429
430/* Routines used for efficiency in Configlist_add */
431
icculus9e44cf12010-02-14 17:14:22 +0000432void Configtable_init(void);
433int Configtable_insert(struct config *);
434struct config *Configtable_find(struct config *);
435void Configtable_clear(int(*)(struct config *));
436
drh75897232000-05-29 14:26:00 +0000437/****************** From the file "action.c" *******************************/
438/*
439** Routines processing parser actions in the LEMON parser generator.
440*/
441
442/* Allocate a new parser action */
drhe9278182007-07-18 18:16:29 +0000443static struct action *Action_new(void){
drh75897232000-05-29 14:26:00 +0000444 static struct action *freelist = 0;
icculus9e44cf12010-02-14 17:14:22 +0000445 struct action *newaction;
drh75897232000-05-29 14:26:00 +0000446
447 if( freelist==0 ){
448 int i;
449 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +0000450 freelist = (struct action *)calloc(amt, sizeof(struct action));
drh75897232000-05-29 14:26:00 +0000451 if( freelist==0 ){
452 fprintf(stderr,"Unable to allocate memory for a new parser action.");
453 exit(1);
454 }
455 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
456 freelist[amt-1].next = 0;
457 }
icculus9e44cf12010-02-14 17:14:22 +0000458 newaction = freelist;
drh75897232000-05-29 14:26:00 +0000459 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +0000460 return newaction;
drh75897232000-05-29 14:26:00 +0000461}
462
drhe9278182007-07-18 18:16:29 +0000463/* Compare two actions for sorting purposes. Return negative, zero, or
464** positive if the first action is less than, equal to, or greater than
465** the first
466*/
467static int actioncmp(
468 struct action *ap1,
469 struct action *ap2
470){
drh75897232000-05-29 14:26:00 +0000471 int rc;
472 rc = ap1->sp->index - ap2->sp->index;
drh75897232000-05-29 14:26:00 +0000473 if( rc==0 ){
drh9892c5d2007-12-21 00:02:11 +0000474 rc = (int)ap1->type - (int)ap2->type;
475 }
476 if( rc==0 && ap1->type==REDUCE ){
drh75897232000-05-29 14:26:00 +0000477 rc = ap1->x.rp->index - ap2->x.rp->index;
478 }
drhe594bc32009-11-03 13:02:25 +0000479 if( rc==0 ){
icculus7b429aa2010-03-03 17:09:01 +0000480 rc = (int) (ap2 - ap1);
drhe594bc32009-11-03 13:02:25 +0000481 }
drh75897232000-05-29 14:26:00 +0000482 return rc;
483}
484
485/* Sort parser actions */
drhe9278182007-07-18 18:16:29 +0000486static struct action *Action_sort(
487 struct action *ap
488){
489 ap = (struct action *)msort((char *)ap,(char **)&ap->next,
490 (int(*)(const char*,const char*))actioncmp);
drh75897232000-05-29 14:26:00 +0000491 return ap;
492}
493
icculus9e44cf12010-02-14 17:14:22 +0000494void Action_add(
495 struct action **app,
496 enum e_action type,
497 struct symbol *sp,
498 char *arg
499){
500 struct action *newaction;
501 newaction = Action_new();
502 newaction->next = *app;
503 *app = newaction;
504 newaction->type = type;
505 newaction->sp = sp;
drh75897232000-05-29 14:26:00 +0000506 if( type==SHIFT ){
icculus9e44cf12010-02-14 17:14:22 +0000507 newaction->x.stp = (struct state *)arg;
drh75897232000-05-29 14:26:00 +0000508 }else{
icculus9e44cf12010-02-14 17:14:22 +0000509 newaction->x.rp = (struct rule *)arg;
drh75897232000-05-29 14:26:00 +0000510 }
511}
drh8b582012003-10-21 13:16:03 +0000512/********************** New code to implement the "acttab" module ***********/
513/*
514** This module implements routines use to construct the yy_action[] table.
515*/
516
517/*
518** The state of the yy_action table under construction is an instance of
drh8dc3e8f2010-01-07 03:53:03 +0000519** the following structure.
520**
521** The yy_action table maps the pair (state_number, lookahead) into an
522** action_number. The table is an array of integers pairs. The state_number
523** determines an initial offset into the yy_action array. The lookahead
524** value is then added to this initial offset to get an index X into the
525** yy_action array. If the aAction[X].lookahead equals the value of the
526** of the lookahead input, then the value of the action_number output is
527** aAction[X].action. If the lookaheads do not match then the
528** default action for the state_number is returned.
529**
530** All actions associated with a single state_number are first entered
531** into aLookahead[] using multiple calls to acttab_action(). Then the
532** actions for that single state_number are placed into the aAction[]
533** array with a single call to acttab_insert(). The acttab_insert() call
534** also resets the aLookahead[] array in preparation for the next
535** state number.
drh8b582012003-10-21 13:16:03 +0000536*/
icculus9e44cf12010-02-14 17:14:22 +0000537struct lookahead_action {
538 int lookahead; /* Value of the lookahead token */
539 int action; /* Action to take on the given lookahead */
540};
drh8b582012003-10-21 13:16:03 +0000541typedef struct acttab acttab;
542struct acttab {
543 int nAction; /* Number of used slots in aAction[] */
544 int nActionAlloc; /* Slots allocated for aAction[] */
icculus9e44cf12010-02-14 17:14:22 +0000545 struct lookahead_action
546 *aAction, /* The yy_action[] table under construction */
drh8b582012003-10-21 13:16:03 +0000547 *aLookahead; /* A single new transaction set */
548 int mnLookahead; /* Minimum aLookahead[].lookahead */
549 int mnAction; /* Action associated with mnLookahead */
550 int mxLookahead; /* Maximum aLookahead[].lookahead */
551 int nLookahead; /* Used slots in aLookahead[] */
552 int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
553};
554
555/* Return the number of entries in the yy_action table */
556#define acttab_size(X) ((X)->nAction)
557
558/* The value for the N-th entry in yy_action */
559#define acttab_yyaction(X,N) ((X)->aAction[N].action)
560
561/* The value for the N-th entry in yy_lookahead */
562#define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
563
564/* Free all memory associated with the given acttab */
565void acttab_free(acttab *p){
566 free( p->aAction );
567 free( p->aLookahead );
568 free( p );
569}
570
571/* Allocate a new acttab structure */
572acttab *acttab_alloc(void){
icculus9e44cf12010-02-14 17:14:22 +0000573 acttab *p = (acttab *) calloc( 1, sizeof(*p) );
drh8b582012003-10-21 13:16:03 +0000574 if( p==0 ){
575 fprintf(stderr,"Unable to allocate memory for a new acttab.");
576 exit(1);
577 }
578 memset(p, 0, sizeof(*p));
579 return p;
580}
581
drh8dc3e8f2010-01-07 03:53:03 +0000582/* Add a new action to the current transaction set.
583**
584** This routine is called once for each lookahead for a particular
585** state.
drh8b582012003-10-21 13:16:03 +0000586*/
587void acttab_action(acttab *p, int lookahead, int action){
588 if( p->nLookahead>=p->nLookaheadAlloc ){
589 p->nLookaheadAlloc += 25;
icculus9e44cf12010-02-14 17:14:22 +0000590 p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
drh8b582012003-10-21 13:16:03 +0000591 sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
592 if( p->aLookahead==0 ){
593 fprintf(stderr,"malloc failed\n");
594 exit(1);
595 }
596 }
597 if( p->nLookahead==0 ){
598 p->mxLookahead = lookahead;
599 p->mnLookahead = lookahead;
600 p->mnAction = action;
601 }else{
602 if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
603 if( p->mnLookahead>lookahead ){
604 p->mnLookahead = lookahead;
605 p->mnAction = action;
606 }
607 }
608 p->aLookahead[p->nLookahead].lookahead = lookahead;
609 p->aLookahead[p->nLookahead].action = action;
610 p->nLookahead++;
611}
612
613/*
614** Add the transaction set built up with prior calls to acttab_action()
615** into the current action table. Then reset the transaction set back
616** to an empty set in preparation for a new round of acttab_action() calls.
617**
618** Return the offset into the action table of the new transaction.
619*/
620int acttab_insert(acttab *p){
621 int i, j, k, n;
622 assert( p->nLookahead>0 );
623
624 /* Make sure we have enough space to hold the expanded action table
625 ** in the worst case. The worst case occurs if the transaction set
626 ** must be appended to the current action table
627 */
drh784d86f2004-02-19 18:41:53 +0000628 n = p->mxLookahead + 1;
drh8dc3e8f2010-01-07 03:53:03 +0000629 if( p->nAction + n >= p->nActionAlloc ){
drhfdbf9282003-10-21 16:34:41 +0000630 int oldAlloc = p->nActionAlloc;
drh8b582012003-10-21 13:16:03 +0000631 p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
icculus9e44cf12010-02-14 17:14:22 +0000632 p->aAction = (struct lookahead_action *) realloc( p->aAction,
drh8b582012003-10-21 13:16:03 +0000633 sizeof(p->aAction[0])*p->nActionAlloc);
634 if( p->aAction==0 ){
635 fprintf(stderr,"malloc failed\n");
636 exit(1);
637 }
drhfdbf9282003-10-21 16:34:41 +0000638 for(i=oldAlloc; i<p->nActionAlloc; i++){
drh8b582012003-10-21 13:16:03 +0000639 p->aAction[i].lookahead = -1;
640 p->aAction[i].action = -1;
641 }
642 }
643
drh8dc3e8f2010-01-07 03:53:03 +0000644 /* Scan the existing action table looking for an offset that is a
645 ** duplicate of the current transaction set. Fall out of the loop
646 ** if and when the duplicate is found.
drh8b582012003-10-21 13:16:03 +0000647 **
648 ** i is the index in p->aAction[] where p->mnLookahead is inserted.
649 */
drh8dc3e8f2010-01-07 03:53:03 +0000650 for(i=p->nAction-1; i>=0; i--){
drhf16371d2009-11-03 19:18:31 +0000651 if( p->aAction[i].lookahead==p->mnLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000652 /* All lookaheads and actions in the aLookahead[] transaction
653 ** must match against the candidate aAction[i] entry. */
drh8b582012003-10-21 13:16:03 +0000654 if( p->aAction[i].action!=p->mnAction ) continue;
655 for(j=0; j<p->nLookahead; j++){
656 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
657 if( k<0 || k>=p->nAction ) break;
658 if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
659 if( p->aLookahead[j].action!=p->aAction[k].action ) break;
660 }
661 if( j<p->nLookahead ) continue;
drh8dc3e8f2010-01-07 03:53:03 +0000662
663 /* No possible lookahead value that is not in the aLookahead[]
664 ** transaction is allowed to match aAction[i] */
drh8b582012003-10-21 13:16:03 +0000665 n = 0;
666 for(j=0; j<p->nAction; j++){
drhfdbf9282003-10-21 16:34:41 +0000667 if( p->aAction[j].lookahead<0 ) continue;
668 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
drh8b582012003-10-21 13:16:03 +0000669 }
drhfdbf9282003-10-21 16:34:41 +0000670 if( n==p->nLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000671 break; /* An exact match is found at offset i */
drhfdbf9282003-10-21 16:34:41 +0000672 }
drh8b582012003-10-21 13:16:03 +0000673 }
674 }
drh8dc3e8f2010-01-07 03:53:03 +0000675
676 /* If no existing offsets exactly match the current transaction, find an
677 ** an empty offset in the aAction[] table in which we can add the
678 ** aLookahead[] transaction.
679 */
drhf16371d2009-11-03 19:18:31 +0000680 if( i<0 ){
drh8dc3e8f2010-01-07 03:53:03 +0000681 /* Look for holes in the aAction[] table that fit the current
682 ** aLookahead[] transaction. Leave i set to the offset of the hole.
683 ** If no holes are found, i is left at p->nAction, which means the
684 ** transaction will be appended. */
685 for(i=0; i<p->nActionAlloc - p->mxLookahead; i++){
drhf16371d2009-11-03 19:18:31 +0000686 if( p->aAction[i].lookahead<0 ){
687 for(j=0; j<p->nLookahead; j++){
688 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
689 if( k<0 ) break;
690 if( p->aAction[k].lookahead>=0 ) break;
691 }
692 if( j<p->nLookahead ) continue;
693 for(j=0; j<p->nAction; j++){
694 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
695 }
696 if( j==p->nAction ){
697 break; /* Fits in empty slots */
698 }
699 }
700 }
701 }
drh8b582012003-10-21 13:16:03 +0000702 /* Insert transaction set at index i. */
703 for(j=0; j<p->nLookahead; j++){
704 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
705 p->aAction[k] = p->aLookahead[j];
706 if( k>=p->nAction ) p->nAction = k+1;
707 }
708 p->nLookahead = 0;
709
710 /* Return the offset that is added to the lookahead in order to get the
711 ** index into yy_action of the action */
712 return i - p->mnLookahead;
713}
714
drh75897232000-05-29 14:26:00 +0000715/********************** From the file "build.c" *****************************/
716/*
717** Routines to construction the finite state machine for the LEMON
718** parser generator.
719*/
720
721/* Find a precedence symbol of every rule in the grammar.
722**
723** Those rules which have a precedence symbol coded in the input
724** grammar using the "[symbol]" construct will already have the
725** rp->precsym field filled. Other rules take as their precedence
726** symbol the first RHS symbol with a defined precedence. If there
727** are not RHS symbols with a defined precedence, the precedence
728** symbol field is left blank.
729*/
icculus9e44cf12010-02-14 17:14:22 +0000730void FindRulePrecedences(struct lemon *xp)
drh75897232000-05-29 14:26:00 +0000731{
732 struct rule *rp;
733 for(rp=xp->rule; rp; rp=rp->next){
734 if( rp->precsym==0 ){
drhfd405312005-11-06 04:06:59 +0000735 int i, j;
736 for(i=0; i<rp->nrhs && rp->precsym==0; i++){
737 struct symbol *sp = rp->rhs[i];
738 if( sp->type==MULTITERMINAL ){
739 for(j=0; j<sp->nsubsym; j++){
740 if( sp->subsym[j]->prec>=0 ){
741 rp->precsym = sp->subsym[j];
742 break;
743 }
744 }
745 }else if( sp->prec>=0 ){
drh75897232000-05-29 14:26:00 +0000746 rp->precsym = rp->rhs[i];
drhf2f105d2012-08-20 15:53:54 +0000747 }
drh75897232000-05-29 14:26:00 +0000748 }
749 }
750 }
751 return;
752}
753
754/* Find all nonterminals which will generate the empty string.
755** Then go back and compute the first sets of every nonterminal.
756** The first set is the set of all terminal symbols which can begin
757** a string generated by that nonterminal.
758*/
icculus9e44cf12010-02-14 17:14:22 +0000759void FindFirstSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000760{
drhfd405312005-11-06 04:06:59 +0000761 int i, j;
drh75897232000-05-29 14:26:00 +0000762 struct rule *rp;
763 int progress;
764
765 for(i=0; i<lemp->nsymbol; i++){
drhaa9f1122007-08-23 02:50:56 +0000766 lemp->symbols[i]->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +0000767 }
768 for(i=lemp->nterminal; i<lemp->nsymbol; i++){
769 lemp->symbols[i]->firstset = SetNew();
770 }
771
772 /* First compute all lambdas */
773 do{
774 progress = 0;
775 for(rp=lemp->rule; rp; rp=rp->next){
776 if( rp->lhs->lambda ) continue;
777 for(i=0; i<rp->nrhs; i++){
drh7dd1ac62012-01-07 15:17:18 +0000778 struct symbol *sp = rp->rhs[i];
779 assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE );
780 if( sp->lambda==LEMON_FALSE ) break;
drh75897232000-05-29 14:26:00 +0000781 }
782 if( i==rp->nrhs ){
drhaa9f1122007-08-23 02:50:56 +0000783 rp->lhs->lambda = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +0000784 progress = 1;
785 }
786 }
787 }while( progress );
788
789 /* Now compute all first sets */
790 do{
791 struct symbol *s1, *s2;
792 progress = 0;
793 for(rp=lemp->rule; rp; rp=rp->next){
794 s1 = rp->lhs;
795 for(i=0; i<rp->nrhs; i++){
796 s2 = rp->rhs[i];
797 if( s2->type==TERMINAL ){
798 progress += SetAdd(s1->firstset,s2->index);
799 break;
drhfd405312005-11-06 04:06:59 +0000800 }else if( s2->type==MULTITERMINAL ){
801 for(j=0; j<s2->nsubsym; j++){
802 progress += SetAdd(s1->firstset,s2->subsym[j]->index);
803 }
804 break;
drhf2f105d2012-08-20 15:53:54 +0000805 }else if( s1==s2 ){
drhaa9f1122007-08-23 02:50:56 +0000806 if( s1->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000807 }else{
drh75897232000-05-29 14:26:00 +0000808 progress += SetUnion(s1->firstset,s2->firstset);
drhaa9f1122007-08-23 02:50:56 +0000809 if( s2->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +0000810 }
drh75897232000-05-29 14:26:00 +0000811 }
812 }
813 }while( progress );
814 return;
815}
816
817/* Compute all LR(0) states for the grammar. Links
818** are added to between some states so that the LR(1) follow sets
819** can be computed later.
820*/
icculus9e44cf12010-02-14 17:14:22 +0000821PRIVATE struct state *getstate(struct lemon *); /* forward reference */
822void FindStates(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000823{
824 struct symbol *sp;
825 struct rule *rp;
826
827 Configlist_init();
828
829 /* Find the start symbol */
830 if( lemp->start ){
831 sp = Symbol_find(lemp->start);
832 if( sp==0 ){
833 ErrorMsg(lemp->filename,0,
834"The specified start symbol \"%s\" is not \
835in a nonterminal of the grammar. \"%s\" will be used as the start \
836symbol instead.",lemp->start,lemp->rule->lhs->name);
837 lemp->errorcnt++;
838 sp = lemp->rule->lhs;
839 }
840 }else{
841 sp = lemp->rule->lhs;
842 }
843
844 /* Make sure the start symbol doesn't occur on the right-hand side of
845 ** any rule. Report an error if it does. (YACC would generate a new
846 ** start symbol in this case.) */
847 for(rp=lemp->rule; rp; rp=rp->next){
848 int i;
849 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +0000850 if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */
drh75897232000-05-29 14:26:00 +0000851 ErrorMsg(lemp->filename,0,
852"The start symbol \"%s\" occurs on the \
853right-hand side of a rule. This will result in a parser which \
854does not work properly.",sp->name);
855 lemp->errorcnt++;
856 }
857 }
858 }
859
860 /* The basis configuration set for the first state
861 ** is all rules which have the start symbol as their
862 ** left-hand side */
863 for(rp=sp->rule; rp; rp=rp->nextlhs){
864 struct config *newcfp;
drhb4960992007-10-05 16:16:36 +0000865 rp->lhsStart = 1;
drh75897232000-05-29 14:26:00 +0000866 newcfp = Configlist_addbasis(rp,0);
867 SetAdd(newcfp->fws,0);
868 }
869
870 /* Compute the first state. All other states will be
871 ** computed automatically during the computation of the first one.
872 ** The returned pointer to the first state is not used. */
873 (void)getstate(lemp);
874 return;
875}
876
877/* Return a pointer to a state which is described by the configuration
878** list which has been built from calls to Configlist_add.
879*/
icculus9e44cf12010-02-14 17:14:22 +0000880PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
881PRIVATE struct state *getstate(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000882{
883 struct config *cfp, *bp;
884 struct state *stp;
885
886 /* Extract the sorted basis of the new state. The basis was constructed
887 ** by prior calls to "Configlist_addbasis()". */
888 Configlist_sortbasis();
889 bp = Configlist_basis();
890
891 /* Get a state with the same basis */
892 stp = State_find(bp);
893 if( stp ){
894 /* A state with the same basis already exists! Copy all the follow-set
895 ** propagation links from the state under construction into the
896 ** preexisting state, then return a pointer to the preexisting state */
897 struct config *x, *y;
898 for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
899 Plink_copy(&y->bplp,x->bplp);
900 Plink_delete(x->fplp);
901 x->fplp = x->bplp = 0;
902 }
903 cfp = Configlist_return();
904 Configlist_eat(cfp);
905 }else{
906 /* This really is a new state. Construct all the details */
907 Configlist_closure(lemp); /* Compute the configuration closure */
908 Configlist_sort(); /* Sort the configuration closure */
909 cfp = Configlist_return(); /* Get a pointer to the config list */
910 stp = State_new(); /* A new state structure */
911 MemoryCheck(stp);
912 stp->bp = bp; /* Remember the configuration basis */
913 stp->cfp = cfp; /* Remember the configuration closure */
drhada354d2005-11-05 15:03:59 +0000914 stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
drh75897232000-05-29 14:26:00 +0000915 stp->ap = 0; /* No actions, yet. */
916 State_insert(stp,stp->bp); /* Add to the state table */
917 buildshifts(lemp,stp); /* Recursively compute successor states */
918 }
919 return stp;
920}
921
drhfd405312005-11-06 04:06:59 +0000922/*
923** Return true if two symbols are the same.
924*/
icculus9e44cf12010-02-14 17:14:22 +0000925int same_symbol(struct symbol *a, struct symbol *b)
drhfd405312005-11-06 04:06:59 +0000926{
927 int i;
928 if( a==b ) return 1;
929 if( a->type!=MULTITERMINAL ) return 0;
930 if( b->type!=MULTITERMINAL ) return 0;
931 if( a->nsubsym!=b->nsubsym ) return 0;
932 for(i=0; i<a->nsubsym; i++){
933 if( a->subsym[i]!=b->subsym[i] ) return 0;
934 }
935 return 1;
936}
937
drh75897232000-05-29 14:26:00 +0000938/* Construct all successor states to the given state. A "successor"
939** state is any state which can be reached by a shift action.
940*/
icculus9e44cf12010-02-14 17:14:22 +0000941PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
drh75897232000-05-29 14:26:00 +0000942{
943 struct config *cfp; /* For looping thru the config closure of "stp" */
944 struct config *bcfp; /* For the inner loop on config closure of "stp" */
icculus9e44cf12010-02-14 17:14:22 +0000945 struct config *newcfg; /* */
drh75897232000-05-29 14:26:00 +0000946 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
947 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
948 struct state *newstp; /* A pointer to a successor state */
949
950 /* Each configuration becomes complete after it contibutes to a successor
951 ** state. Initially, all configurations are incomplete */
952 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
953
954 /* Loop through all configurations of the state "stp" */
955 for(cfp=stp->cfp; cfp; cfp=cfp->next){
956 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
957 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
958 Configlist_reset(); /* Reset the new config set */
959 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
960
961 /* For every configuration in the state "stp" which has the symbol "sp"
962 ** following its dot, add the same configuration to the basis set under
963 ** construction but with the dot shifted one symbol to the right. */
964 for(bcfp=cfp; bcfp; bcfp=bcfp->next){
965 if( bcfp->status==COMPLETE ) continue; /* Already used */
966 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
967 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
drhfd405312005-11-06 04:06:59 +0000968 if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
drh75897232000-05-29 14:26:00 +0000969 bcfp->status = COMPLETE; /* Mark this config as used */
icculus9e44cf12010-02-14 17:14:22 +0000970 newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
971 Plink_add(&newcfg->bplp,bcfp);
drh75897232000-05-29 14:26:00 +0000972 }
973
974 /* Get a pointer to the state described by the basis configuration set
975 ** constructed in the preceding loop */
976 newstp = getstate(lemp);
977
978 /* The state "newstp" is reached from the state "stp" by a shift action
979 ** on the symbol "sp" */
drhfd405312005-11-06 04:06:59 +0000980 if( sp->type==MULTITERMINAL ){
981 int i;
982 for(i=0; i<sp->nsubsym; i++){
983 Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
984 }
985 }else{
986 Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
987 }
drh75897232000-05-29 14:26:00 +0000988 }
989}
990
991/*
992** Construct the propagation links
993*/
icculus9e44cf12010-02-14 17:14:22 +0000994void FindLinks(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000995{
996 int i;
997 struct config *cfp, *other;
998 struct state *stp;
999 struct plink *plp;
1000
1001 /* Housekeeping detail:
1002 ** Add to every propagate link a pointer back to the state to
1003 ** which the link is attached. */
1004 for(i=0; i<lemp->nstate; i++){
1005 stp = lemp->sorted[i];
1006 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1007 cfp->stp = stp;
1008 }
1009 }
1010
1011 /* Convert all backlinks into forward links. Only the forward
1012 ** links are used in the follow-set computation. */
1013 for(i=0; i<lemp->nstate; i++){
1014 stp = lemp->sorted[i];
1015 for(cfp=stp->cfp; cfp; cfp=cfp->next){
1016 for(plp=cfp->bplp; plp; plp=plp->next){
1017 other = plp->cfp;
1018 Plink_add(&other->fplp,cfp);
1019 }
1020 }
1021 }
1022}
1023
1024/* Compute all followsets.
1025**
1026** A followset is the set of all symbols which can come immediately
1027** after a configuration.
1028*/
icculus9e44cf12010-02-14 17:14:22 +00001029void FindFollowSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001030{
1031 int i;
1032 struct config *cfp;
1033 struct plink *plp;
1034 int progress;
1035 int change;
1036
1037 for(i=0; i<lemp->nstate; i++){
1038 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1039 cfp->status = INCOMPLETE;
1040 }
1041 }
1042
1043 do{
1044 progress = 0;
1045 for(i=0; i<lemp->nstate; i++){
1046 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
1047 if( cfp->status==COMPLETE ) continue;
1048 for(plp=cfp->fplp; plp; plp=plp->next){
1049 change = SetUnion(plp->cfp->fws,cfp->fws);
1050 if( change ){
1051 plp->cfp->status = INCOMPLETE;
1052 progress = 1;
drhf2f105d2012-08-20 15:53:54 +00001053 }
1054 }
drh75897232000-05-29 14:26:00 +00001055 cfp->status = COMPLETE;
1056 }
1057 }
1058 }while( progress );
1059}
1060
drh3cb2f6e2012-01-09 14:19:05 +00001061static int resolve_conflict(struct action *,struct action *);
drh75897232000-05-29 14:26:00 +00001062
1063/* Compute the reduce actions, and resolve conflicts.
1064*/
icculus9e44cf12010-02-14 17:14:22 +00001065void FindActions(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001066{
1067 int i,j;
1068 struct config *cfp;
1069 struct state *stp;
1070 struct symbol *sp;
1071 struct rule *rp;
1072
1073 /* Add all of the reduce actions
1074 ** A reduce action is added for each element of the followset of
1075 ** a configuration which has its dot at the extreme right.
1076 */
1077 for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
1078 stp = lemp->sorted[i];
1079 for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
1080 if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
1081 for(j=0; j<lemp->nterminal; j++){
1082 if( SetFind(cfp->fws,j) ){
1083 /* Add a reduce action to the state "stp" which will reduce by the
1084 ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
drh218dc692004-05-31 23:13:45 +00001085 Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
drh75897232000-05-29 14:26:00 +00001086 }
drhf2f105d2012-08-20 15:53:54 +00001087 }
drh75897232000-05-29 14:26:00 +00001088 }
1089 }
1090 }
1091
1092 /* Add the accepting token */
1093 if( lemp->start ){
1094 sp = Symbol_find(lemp->start);
1095 if( sp==0 ) sp = lemp->rule->lhs;
1096 }else{
1097 sp = lemp->rule->lhs;
1098 }
1099 /* Add to the first state (which is always the starting state of the
1100 ** finite state machine) an action to ACCEPT if the lookahead is the
1101 ** start nonterminal. */
1102 Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
1103
1104 /* Resolve conflicts */
1105 for(i=0; i<lemp->nstate; i++){
1106 struct action *ap, *nap;
1107 struct state *stp;
1108 stp = lemp->sorted[i];
drhe9278182007-07-18 18:16:29 +00001109 /* assert( stp->ap ); */
drh75897232000-05-29 14:26:00 +00001110 stp->ap = Action_sort(stp->ap);
drhb59499c2002-02-23 18:45:13 +00001111 for(ap=stp->ap; ap && ap->next; ap=ap->next){
drh75897232000-05-29 14:26:00 +00001112 for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
1113 /* The two actions "ap" and "nap" have the same lookahead.
1114 ** Figure out which one should be used */
drh3cb2f6e2012-01-09 14:19:05 +00001115 lemp->nconflict += resolve_conflict(ap,nap);
drh75897232000-05-29 14:26:00 +00001116 }
1117 }
1118 }
1119
1120 /* Report an error for each rule that can never be reduced. */
drhaa9f1122007-08-23 02:50:56 +00001121 for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00001122 for(i=0; i<lemp->nstate; i++){
1123 struct action *ap;
1124 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
drhaa9f1122007-08-23 02:50:56 +00001125 if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +00001126 }
1127 }
1128 for(rp=lemp->rule; rp; rp=rp->next){
1129 if( rp->canReduce ) continue;
1130 ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
1131 lemp->errorcnt++;
1132 }
1133}
1134
1135/* Resolve a conflict between the two given actions. If the
drh34ff57b2008-07-14 12:27:51 +00001136** conflict can't be resolved, return non-zero.
drh75897232000-05-29 14:26:00 +00001137**
1138** NO LONGER TRUE:
1139** To resolve a conflict, first look to see if either action
1140** is on an error rule. In that case, take the action which
1141** is not associated with the error rule. If neither or both
1142** actions are associated with an error rule, then try to
1143** use precedence to resolve the conflict.
1144**
1145** If either action is a SHIFT, then it must be apx. This
1146** function won't work if apx->type==REDUCE and apy->type==SHIFT.
1147*/
icculus9e44cf12010-02-14 17:14:22 +00001148static int resolve_conflict(
1149 struct action *apx,
drh3cb2f6e2012-01-09 14:19:05 +00001150 struct action *apy
icculus9e44cf12010-02-14 17:14:22 +00001151){
drh75897232000-05-29 14:26:00 +00001152 struct symbol *spx, *spy;
1153 int errcnt = 0;
1154 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
drhf0fa1c12006-12-14 01:06:22 +00001155 if( apx->type==SHIFT && apy->type==SHIFT ){
drh9892c5d2007-12-21 00:02:11 +00001156 apy->type = SSCONFLICT;
drhf0fa1c12006-12-14 01:06:22 +00001157 errcnt++;
1158 }
drh75897232000-05-29 14:26:00 +00001159 if( apx->type==SHIFT && apy->type==REDUCE ){
1160 spx = apx->sp;
1161 spy = apy->x.rp->precsym;
1162 if( spy==0 || spx->prec<0 || spy->prec<0 ){
1163 /* Not enough precedence information. */
drh9892c5d2007-12-21 00:02:11 +00001164 apy->type = SRCONFLICT;
drh75897232000-05-29 14:26:00 +00001165 errcnt++;
drhdd7e9db2010-07-19 01:52:07 +00001166 }else if( spx->prec>spy->prec ){ /* higher precedence wins */
drh75897232000-05-29 14:26:00 +00001167 apy->type = RD_RESOLVED;
1168 }else if( spx->prec<spy->prec ){
1169 apx->type = SH_RESOLVED;
1170 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
1171 apy->type = RD_RESOLVED; /* associativity */
1172 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
1173 apx->type = SH_RESOLVED;
1174 }else{
1175 assert( spx->prec==spy->prec && spx->assoc==NONE );
drh9892c5d2007-12-21 00:02:11 +00001176 apy->type = SRCONFLICT;
drh75897232000-05-29 14:26:00 +00001177 errcnt++;
1178 }
1179 }else if( apx->type==REDUCE && apy->type==REDUCE ){
1180 spx = apx->x.rp->precsym;
1181 spy = apy->x.rp->precsym;
1182 if( spx==0 || spy==0 || spx->prec<0 ||
1183 spy->prec<0 || spx->prec==spy->prec ){
drh9892c5d2007-12-21 00:02:11 +00001184 apy->type = RRCONFLICT;
drh75897232000-05-29 14:26:00 +00001185 errcnt++;
1186 }else if( spx->prec>spy->prec ){
1187 apy->type = RD_RESOLVED;
1188 }else if( spx->prec<spy->prec ){
1189 apx->type = RD_RESOLVED;
1190 }
1191 }else{
drhb59499c2002-02-23 18:45:13 +00001192 assert(
1193 apx->type==SH_RESOLVED ||
1194 apx->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001195 apx->type==SSCONFLICT ||
1196 apx->type==SRCONFLICT ||
1197 apx->type==RRCONFLICT ||
drhb59499c2002-02-23 18:45:13 +00001198 apy->type==SH_RESOLVED ||
1199 apy->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001200 apy->type==SSCONFLICT ||
1201 apy->type==SRCONFLICT ||
1202 apy->type==RRCONFLICT
drhb59499c2002-02-23 18:45:13 +00001203 );
1204 /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
1205 ** REDUCEs on the list. If we reach this point it must be because
1206 ** the parser conflict had already been resolved. */
drh75897232000-05-29 14:26:00 +00001207 }
1208 return errcnt;
1209}
1210/********************* From the file "configlist.c" *************************/
1211/*
1212** Routines to processing a configuration list and building a state
1213** in the LEMON parser generator.
1214*/
1215
1216static struct config *freelist = 0; /* List of free configurations */
1217static struct config *current = 0; /* Top of list of configurations */
1218static struct config **currentend = 0; /* Last on list of configs */
1219static struct config *basis = 0; /* Top of list of basis configs */
1220static struct config **basisend = 0; /* End of list of basis configs */
1221
1222/* Return a pointer to a new configuration */
1223PRIVATE struct config *newconfig(){
icculus9e44cf12010-02-14 17:14:22 +00001224 struct config *newcfg;
drh75897232000-05-29 14:26:00 +00001225 if( freelist==0 ){
1226 int i;
1227 int amt = 3;
drh9892c5d2007-12-21 00:02:11 +00001228 freelist = (struct config *)calloc( amt, sizeof(struct config) );
drh75897232000-05-29 14:26:00 +00001229 if( freelist==0 ){
1230 fprintf(stderr,"Unable to allocate memory for a new configuration.");
1231 exit(1);
1232 }
1233 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
1234 freelist[amt-1].next = 0;
1235 }
icculus9e44cf12010-02-14 17:14:22 +00001236 newcfg = freelist;
drh75897232000-05-29 14:26:00 +00001237 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00001238 return newcfg;
drh75897232000-05-29 14:26:00 +00001239}
1240
1241/* The configuration "old" is no longer used */
icculus9e44cf12010-02-14 17:14:22 +00001242PRIVATE void deleteconfig(struct config *old)
drh75897232000-05-29 14:26:00 +00001243{
1244 old->next = freelist;
1245 freelist = old;
1246}
1247
1248/* Initialized the configuration list builder */
1249void Configlist_init(){
1250 current = 0;
1251 currentend = &current;
1252 basis = 0;
1253 basisend = &basis;
1254 Configtable_init();
1255 return;
1256}
1257
1258/* Initialized the configuration list builder */
1259void Configlist_reset(){
1260 current = 0;
1261 currentend = &current;
1262 basis = 0;
1263 basisend = &basis;
1264 Configtable_clear(0);
1265 return;
1266}
1267
1268/* Add another configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001269struct config *Configlist_add(
1270 struct rule *rp, /* The rule */
1271 int dot /* Index into the RHS of the rule where the dot goes */
1272){
drh75897232000-05-29 14:26:00 +00001273 struct config *cfp, model;
1274
1275 assert( currentend!=0 );
1276 model.rp = rp;
1277 model.dot = dot;
1278 cfp = Configtable_find(&model);
1279 if( cfp==0 ){
1280 cfp = newconfig();
1281 cfp->rp = rp;
1282 cfp->dot = dot;
1283 cfp->fws = SetNew();
1284 cfp->stp = 0;
1285 cfp->fplp = cfp->bplp = 0;
1286 cfp->next = 0;
1287 cfp->bp = 0;
1288 *currentend = cfp;
1289 currentend = &cfp->next;
1290 Configtable_insert(cfp);
1291 }
1292 return cfp;
1293}
1294
1295/* Add a basis configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001296struct config *Configlist_addbasis(struct rule *rp, int dot)
drh75897232000-05-29 14:26:00 +00001297{
1298 struct config *cfp, model;
1299
1300 assert( basisend!=0 );
1301 assert( currentend!=0 );
1302 model.rp = rp;
1303 model.dot = dot;
1304 cfp = Configtable_find(&model);
1305 if( cfp==0 ){
1306 cfp = newconfig();
1307 cfp->rp = rp;
1308 cfp->dot = dot;
1309 cfp->fws = SetNew();
1310 cfp->stp = 0;
1311 cfp->fplp = cfp->bplp = 0;
1312 cfp->next = 0;
1313 cfp->bp = 0;
1314 *currentend = cfp;
1315 currentend = &cfp->next;
1316 *basisend = cfp;
1317 basisend = &cfp->bp;
1318 Configtable_insert(cfp);
1319 }
1320 return cfp;
1321}
1322
1323/* Compute the closure of the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001324void Configlist_closure(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001325{
1326 struct config *cfp, *newcfp;
1327 struct rule *rp, *newrp;
1328 struct symbol *sp, *xsp;
1329 int i, dot;
1330
1331 assert( currentend!=0 );
1332 for(cfp=current; cfp; cfp=cfp->next){
1333 rp = cfp->rp;
1334 dot = cfp->dot;
1335 if( dot>=rp->nrhs ) continue;
1336 sp = rp->rhs[dot];
1337 if( sp->type==NONTERMINAL ){
1338 if( sp->rule==0 && sp!=lemp->errsym ){
1339 ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
1340 sp->name);
1341 lemp->errorcnt++;
1342 }
1343 for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
1344 newcfp = Configlist_add(newrp,0);
1345 for(i=dot+1; i<rp->nrhs; i++){
1346 xsp = rp->rhs[i];
1347 if( xsp->type==TERMINAL ){
1348 SetAdd(newcfp->fws,xsp->index);
1349 break;
drhfd405312005-11-06 04:06:59 +00001350 }else if( xsp->type==MULTITERMINAL ){
1351 int k;
1352 for(k=0; k<xsp->nsubsym; k++){
1353 SetAdd(newcfp->fws, xsp->subsym[k]->index);
1354 }
1355 break;
drhf2f105d2012-08-20 15:53:54 +00001356 }else{
drh75897232000-05-29 14:26:00 +00001357 SetUnion(newcfp->fws,xsp->firstset);
drhaa9f1122007-08-23 02:50:56 +00001358 if( xsp->lambda==LEMON_FALSE ) break;
drhf2f105d2012-08-20 15:53:54 +00001359 }
1360 }
drh75897232000-05-29 14:26:00 +00001361 if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
1362 }
1363 }
1364 }
1365 return;
1366}
1367
1368/* Sort the configuration list */
1369void Configlist_sort(){
drh218dc692004-05-31 23:13:45 +00001370 current = (struct config *)msort((char *)current,(char **)&(current->next),Configcmp);
drh75897232000-05-29 14:26:00 +00001371 currentend = 0;
1372 return;
1373}
1374
1375/* Sort the basis configuration list */
1376void Configlist_sortbasis(){
drh218dc692004-05-31 23:13:45 +00001377 basis = (struct config *)msort((char *)current,(char **)&(current->bp),Configcmp);
drh75897232000-05-29 14:26:00 +00001378 basisend = 0;
1379 return;
1380}
1381
1382/* Return a pointer to the head of the configuration list and
1383** reset the list */
1384struct config *Configlist_return(){
1385 struct config *old;
1386 old = current;
1387 current = 0;
1388 currentend = 0;
1389 return old;
1390}
1391
1392/* Return a pointer to the head of the configuration list and
1393** reset the list */
1394struct config *Configlist_basis(){
1395 struct config *old;
1396 old = basis;
1397 basis = 0;
1398 basisend = 0;
1399 return old;
1400}
1401
1402/* Free all elements of the given configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001403void Configlist_eat(struct config *cfp)
drh75897232000-05-29 14:26:00 +00001404{
1405 struct config *nextcfp;
1406 for(; cfp; cfp=nextcfp){
1407 nextcfp = cfp->next;
1408 assert( cfp->fplp==0 );
1409 assert( cfp->bplp==0 );
1410 if( cfp->fws ) SetFree(cfp->fws);
1411 deleteconfig(cfp);
1412 }
1413 return;
1414}
1415/***************** From the file "error.c" *********************************/
1416/*
1417** Code for printing error message.
1418*/
1419
drhf9a2e7b2003-04-15 01:49:48 +00001420void ErrorMsg(const char *filename, int lineno, const char *format, ...){
icculus15a2cec2010-02-16 16:07:28 +00001421 va_list ap;
icculus1c11f742010-02-15 00:01:04 +00001422 fprintf(stderr, "%s:%d: ", filename, lineno);
1423 va_start(ap, format);
1424 vfprintf(stderr,format,ap);
1425 va_end(ap);
1426 fprintf(stderr, "\n");
drh75897232000-05-29 14:26:00 +00001427}
1428/**************** From the file "main.c" ************************************/
1429/*
1430** Main program file for the LEMON parser generator.
1431*/
1432
1433/* Report an out-of-memory condition and abort. This function
1434** is used mostly by the "MemoryCheck" macro in struct.h
1435*/
1436void memory_error(){
1437 fprintf(stderr,"Out of memory. Aborting...\n");
1438 exit(1);
1439}
1440
drh6d08b4d2004-07-20 12:45:22 +00001441static int nDefine = 0; /* Number of -D options on the command line */
1442static char **azDefine = 0; /* Name of the -D macros */
1443
1444/* This routine is called with the argument to each -D command-line option.
1445** Add the macro defined to the azDefine array.
1446*/
1447static void handle_D_option(char *z){
1448 char **paz;
1449 nDefine++;
icculus9e44cf12010-02-14 17:14:22 +00001450 azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine);
drh6d08b4d2004-07-20 12:45:22 +00001451 if( azDefine==0 ){
1452 fprintf(stderr,"out of memory\n");
1453 exit(1);
1454 }
1455 paz = &azDefine[nDefine-1];
icculus9e44cf12010-02-14 17:14:22 +00001456 *paz = (char *) malloc( lemonStrlen(z)+1 );
drh6d08b4d2004-07-20 12:45:22 +00001457 if( *paz==0 ){
1458 fprintf(stderr,"out of memory\n");
1459 exit(1);
1460 }
drh898799f2014-01-10 23:21:00 +00001461 lemon_strcpy(*paz, z);
drh6d08b4d2004-07-20 12:45:22 +00001462 for(z=*paz; *z && *z!='='; z++){}
1463 *z = 0;
1464}
1465
icculus3e143bd2010-02-14 00:48:49 +00001466static char *user_templatename = NULL;
1467static void handle_T_option(char *z){
icculus9e44cf12010-02-14 17:14:22 +00001468 user_templatename = (char *) malloc( lemonStrlen(z)+1 );
icculus3e143bd2010-02-14 00:48:49 +00001469 if( user_templatename==0 ){
1470 memory_error();
1471 }
drh898799f2014-01-10 23:21:00 +00001472 lemon_strcpy(user_templatename, z);
icculus3e143bd2010-02-14 00:48:49 +00001473}
drh75897232000-05-29 14:26:00 +00001474
1475/* The main program. Parse the command line and do it... */
icculus9e44cf12010-02-14 17:14:22 +00001476int main(int argc, char **argv)
drh75897232000-05-29 14:26:00 +00001477{
1478 static int version = 0;
1479 static int rpflag = 0;
1480 static int basisflag = 0;
1481 static int compress = 0;
1482 static int quiet = 0;
1483 static int statistics = 0;
1484 static int mhflag = 0;
shane58543932008-12-10 20:10:04 +00001485 static int nolinenosflag = 0;
drhdd7e9db2010-07-19 01:52:07 +00001486 static int noResort = 0;
drh75897232000-05-29 14:26:00 +00001487 static struct s_options options[] = {
1488 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1489 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
drh6d08b4d2004-07-20 12:45:22 +00001490 {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
icculus3e143bd2010-02-14 00:48:49 +00001491 {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
drh75897232000-05-29 14:26:00 +00001492 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
shane58543932008-12-10 20:10:04 +00001493 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
1494 {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
drhf5c4e0f2010-07-18 11:35:53 +00001495 {OPT_FLAG, "p", (char*)&showPrecedenceConflict,
1496 "Show conflicts resolved by precedence rules"},
drh75897232000-05-29 14:26:00 +00001497 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
drhdd7e9db2010-07-19 01:52:07 +00001498 {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"},
drh6d08b4d2004-07-20 12:45:22 +00001499 {OPT_FLAG, "s", (char*)&statistics,
1500 "Print parser stats to standard output."},
drh75897232000-05-29 14:26:00 +00001501 {OPT_FLAG, "x", (char*)&version, "Print the version number."},
1502 {OPT_FLAG,0,0,0}
1503 };
1504 int i;
icculus42585cf2010-02-14 05:19:56 +00001505 int exitcode;
drh75897232000-05-29 14:26:00 +00001506 struct lemon lem;
1507
drhb0c86772000-06-02 23:21:26 +00001508 OptInit(argv,options,stderr);
drh75897232000-05-29 14:26:00 +00001509 if( version ){
drhb19a2bc2001-09-16 00:13:26 +00001510 printf("Lemon version 1.0\n");
drh75897232000-05-29 14:26:00 +00001511 exit(0);
1512 }
drhb0c86772000-06-02 23:21:26 +00001513 if( OptNArgs()!=1 ){
drh75897232000-05-29 14:26:00 +00001514 fprintf(stderr,"Exactly one filename argument is required.\n");
1515 exit(1);
1516 }
drh954f6b42006-06-13 13:27:46 +00001517 memset(&lem, 0, sizeof(lem));
drh75897232000-05-29 14:26:00 +00001518 lem.errorcnt = 0;
1519
1520 /* Initialize the machine */
1521 Strsafe_init();
1522 Symbol_init();
1523 State_init();
1524 lem.argv0 = argv[0];
drhb0c86772000-06-02 23:21:26 +00001525 lem.filename = OptArg(0);
drh75897232000-05-29 14:26:00 +00001526 lem.basisflag = basisflag;
shane58543932008-12-10 20:10:04 +00001527 lem.nolinenosflag = nolinenosflag;
drh75897232000-05-29 14:26:00 +00001528 Symbol_new("$");
1529 lem.errsym = Symbol_new("error");
drhc4dd3fd2008-01-22 01:48:05 +00001530 lem.errsym->useCnt = 0;
drh75897232000-05-29 14:26:00 +00001531
1532 /* Parse the input file */
1533 Parse(&lem);
1534 if( lem.errorcnt ) exit(lem.errorcnt);
drh954f6b42006-06-13 13:27:46 +00001535 if( lem.nrule==0 ){
drh75897232000-05-29 14:26:00 +00001536 fprintf(stderr,"Empty grammar.\n");
1537 exit(1);
1538 }
1539
1540 /* Count and index the symbols of the grammar */
1541 lem.nsymbol = Symbol_count();
1542 Symbol_new("{default}");
1543 lem.symbols = Symbol_arrayof();
drh60d31652004-02-22 00:08:04 +00001544 for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
icculus9e44cf12010-02-14 17:14:22 +00001545 qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*), Symbolcmpp);
drh75897232000-05-29 14:26:00 +00001546 for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
1547 for(i=1; isupper(lem.symbols[i]->name[0]); i++);
1548 lem.nterminal = i;
1549
1550 /* Generate a reprint of the grammar, if requested on the command line */
1551 if( rpflag ){
1552 Reprint(&lem);
1553 }else{
1554 /* Initialize the size for all follow and first sets */
drh9892c5d2007-12-21 00:02:11 +00001555 SetSize(lem.nterminal+1);
drh75897232000-05-29 14:26:00 +00001556
1557 /* Find the precedence for every production rule (that has one) */
1558 FindRulePrecedences(&lem);
1559
1560 /* Compute the lambda-nonterminals and the first-sets for every
1561 ** nonterminal */
1562 FindFirstSets(&lem);
1563
1564 /* Compute all LR(0) states. Also record follow-set propagation
1565 ** links so that the follow-set can be computed later */
1566 lem.nstate = 0;
1567 FindStates(&lem);
1568 lem.sorted = State_arrayof();
1569
1570 /* Tie up loose ends on the propagation links */
1571 FindLinks(&lem);
1572
1573 /* Compute the follow set of every reducible configuration */
1574 FindFollowSets(&lem);
1575
1576 /* Compute the action tables */
1577 FindActions(&lem);
1578
1579 /* Compress the action tables */
1580 if( compress==0 ) CompressTables(&lem);
1581
drhada354d2005-11-05 15:03:59 +00001582 /* Reorder and renumber the states so that states with fewer choices
drhdd7e9db2010-07-19 01:52:07 +00001583 ** occur at the end. This is an optimization that helps make the
1584 ** generated parser tables smaller. */
1585 if( noResort==0 ) ResortStates(&lem);
drhada354d2005-11-05 15:03:59 +00001586
drh75897232000-05-29 14:26:00 +00001587 /* Generate a report of the parser generated. (the "y.output" file) */
1588 if( !quiet ) ReportOutput(&lem);
1589
1590 /* Generate the source code for the parser */
1591 ReportTable(&lem, mhflag);
1592
1593 /* Produce a header file for use by the scanner. (This step is
1594 ** omitted if the "-m" option is used because makeheaders will
1595 ** generate the file for us.) */
1596 if( !mhflag ) ReportHeader(&lem);
1597 }
1598 if( statistics ){
1599 printf("Parser statistics: %d terminals, %d nonterminals, %d rules\n",
1600 lem.nterminal, lem.nsymbol - lem.nterminal, lem.nrule);
1601 printf(" %d states, %d parser table entries, %d conflicts\n",
1602 lem.nstate, lem.tablesize, lem.nconflict);
1603 }
icculus8e158022010-02-16 16:09:03 +00001604 if( lem.nconflict > 0 ){
1605 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
icculus42585cf2010-02-14 05:19:56 +00001606 }
1607
1608 /* return 0 on success, 1 on failure. */
icculus8e158022010-02-16 16:09:03 +00001609 exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
icculus42585cf2010-02-14 05:19:56 +00001610 exit(exitcode);
1611 return (exitcode);
drh75897232000-05-29 14:26:00 +00001612}
1613/******************** From the file "msort.c" *******************************/
1614/*
1615** A generic merge-sort program.
1616**
1617** USAGE:
1618** Let "ptr" be a pointer to some structure which is at the head of
1619** a null-terminated list. Then to sort the list call:
1620**
1621** ptr = msort(ptr,&(ptr->next),cmpfnc);
1622**
1623** In the above, "cmpfnc" is a pointer to a function which compares
1624** two instances of the structure and returns an integer, as in
1625** strcmp. The second argument is a pointer to the pointer to the
1626** second element of the linked list. This address is used to compute
1627** the offset to the "next" field within the structure. The offset to
1628** the "next" field must be constant for all structures in the list.
1629**
1630** The function returns a new pointer which is the head of the list
1631** after sorting.
1632**
1633** ALGORITHM:
1634** Merge-sort.
1635*/
1636
1637/*
1638** Return a pointer to the next structure in the linked list.
1639*/
drhd25d6922012-04-18 09:59:56 +00001640#define NEXT(A) (*(char**)(((char*)A)+offset))
drh75897232000-05-29 14:26:00 +00001641
1642/*
1643** Inputs:
1644** a: A sorted, null-terminated linked list. (May be null).
1645** b: A sorted, null-terminated linked list. (May be null).
1646** cmp: A pointer to the comparison function.
1647** offset: Offset in the structure to the "next" field.
1648**
1649** Return Value:
1650** A pointer to the head of a sorted list containing the elements
1651** of both a and b.
1652**
1653** Side effects:
1654** The "next" pointers for elements in the lists a and b are
1655** changed.
1656*/
drhe9278182007-07-18 18:16:29 +00001657static char *merge(
1658 char *a,
1659 char *b,
1660 int (*cmp)(const char*,const char*),
1661 int offset
1662){
drh75897232000-05-29 14:26:00 +00001663 char *ptr, *head;
1664
1665 if( a==0 ){
1666 head = b;
1667 }else if( b==0 ){
1668 head = a;
1669 }else{
drhe594bc32009-11-03 13:02:25 +00001670 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001671 ptr = a;
1672 a = NEXT(a);
1673 }else{
1674 ptr = b;
1675 b = NEXT(b);
1676 }
1677 head = ptr;
1678 while( a && b ){
drhe594bc32009-11-03 13:02:25 +00001679 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001680 NEXT(ptr) = a;
1681 ptr = a;
1682 a = NEXT(a);
1683 }else{
1684 NEXT(ptr) = b;
1685 ptr = b;
1686 b = NEXT(b);
1687 }
1688 }
1689 if( a ) NEXT(ptr) = a;
1690 else NEXT(ptr) = b;
1691 }
1692 return head;
1693}
1694
1695/*
1696** Inputs:
1697** list: Pointer to a singly-linked list of structures.
1698** next: Pointer to pointer to the second element of the list.
1699** cmp: A comparison function.
1700**
1701** Return Value:
1702** A pointer to the head of a sorted list containing the elements
1703** orginally in list.
1704**
1705** Side effects:
1706** The "next" pointers for elements in list are changed.
1707*/
1708#define LISTSIZE 30
drhe9278182007-07-18 18:16:29 +00001709static char *msort(
1710 char *list,
1711 char **next,
1712 int (*cmp)(const char*,const char*)
1713){
drhba99af52001-10-25 20:37:16 +00001714 unsigned long offset;
drh75897232000-05-29 14:26:00 +00001715 char *ep;
1716 char *set[LISTSIZE];
1717 int i;
drhba99af52001-10-25 20:37:16 +00001718 offset = (unsigned long)next - (unsigned long)list;
drh75897232000-05-29 14:26:00 +00001719 for(i=0; i<LISTSIZE; i++) set[i] = 0;
1720 while( list ){
1721 ep = list;
1722 list = NEXT(list);
1723 NEXT(ep) = 0;
1724 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1725 ep = merge(ep,set[i],cmp,offset);
1726 set[i] = 0;
1727 }
1728 set[i] = ep;
1729 }
1730 ep = 0;
drhe594bc32009-11-03 13:02:25 +00001731 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset);
drh75897232000-05-29 14:26:00 +00001732 return ep;
1733}
1734/************************ From the file "option.c" **************************/
1735static char **argv;
1736static struct s_options *op;
1737static FILE *errstream;
1738
1739#define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1740
1741/*
1742** Print the command line with a carrot pointing to the k-th character
1743** of the n-th field.
1744*/
icculus9e44cf12010-02-14 17:14:22 +00001745static void errline(int n, int k, FILE *err)
drh75897232000-05-29 14:26:00 +00001746{
1747 int spcnt, i;
drh75897232000-05-29 14:26:00 +00001748 if( argv[0] ) fprintf(err,"%s",argv[0]);
drh87cf1372008-08-13 20:09:06 +00001749 spcnt = lemonStrlen(argv[0]) + 1;
drh75897232000-05-29 14:26:00 +00001750 for(i=1; i<n && argv[i]; i++){
1751 fprintf(err," %s",argv[i]);
drh87cf1372008-08-13 20:09:06 +00001752 spcnt += lemonStrlen(argv[i])+1;
drh75897232000-05-29 14:26:00 +00001753 }
1754 spcnt += k;
1755 for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1756 if( spcnt<20 ){
1757 fprintf(err,"\n%*s^-- here\n",spcnt,"");
1758 }else{
1759 fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1760 }
1761}
1762
1763/*
1764** Return the index of the N-th non-switch argument. Return -1
1765** if N is out of range.
1766*/
icculus9e44cf12010-02-14 17:14:22 +00001767static int argindex(int n)
drh75897232000-05-29 14:26:00 +00001768{
1769 int i;
1770 int dashdash = 0;
1771 if( argv!=0 && *argv!=0 ){
1772 for(i=1; argv[i]; i++){
1773 if( dashdash || !ISOPT(argv[i]) ){
1774 if( n==0 ) return i;
1775 n--;
1776 }
1777 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1778 }
1779 }
1780 return -1;
1781}
1782
1783static char emsg[] = "Command line syntax error: ";
1784
1785/*
1786** Process a flag command line argument.
1787*/
icculus9e44cf12010-02-14 17:14:22 +00001788static int handleflags(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001789{
1790 int v;
1791 int errcnt = 0;
1792 int j;
1793 for(j=0; op[j].label; j++){
drh87cf1372008-08-13 20:09:06 +00001794 if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
drh75897232000-05-29 14:26:00 +00001795 }
1796 v = argv[i][0]=='-' ? 1 : 0;
1797 if( op[j].label==0 ){
1798 if( err ){
1799 fprintf(err,"%sundefined option.\n",emsg);
1800 errline(i,1,err);
1801 }
1802 errcnt++;
1803 }else if( op[j].type==OPT_FLAG ){
1804 *((int*)op[j].arg) = v;
1805 }else if( op[j].type==OPT_FFLAG ){
icculus9e44cf12010-02-14 17:14:22 +00001806 (*(void(*)(int))(op[j].arg))(v);
drh6d08b4d2004-07-20 12:45:22 +00001807 }else if( op[j].type==OPT_FSTR ){
icculus9e44cf12010-02-14 17:14:22 +00001808 (*(void(*)(char *))(op[j].arg))(&argv[i][2]);
drh75897232000-05-29 14:26:00 +00001809 }else{
1810 if( err ){
1811 fprintf(err,"%smissing argument on switch.\n",emsg);
1812 errline(i,1,err);
1813 }
1814 errcnt++;
1815 }
1816 return errcnt;
1817}
1818
1819/*
1820** Process a command line switch which has an argument.
1821*/
icculus9e44cf12010-02-14 17:14:22 +00001822static int handleswitch(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001823{
1824 int lv = 0;
1825 double dv = 0.0;
1826 char *sv = 0, *end;
1827 char *cp;
1828 int j;
1829 int errcnt = 0;
1830 cp = strchr(argv[i],'=');
drh43617e92006-03-06 20:55:46 +00001831 assert( cp!=0 );
drh75897232000-05-29 14:26:00 +00001832 *cp = 0;
1833 for(j=0; op[j].label; j++){
1834 if( strcmp(argv[i],op[j].label)==0 ) break;
1835 }
1836 *cp = '=';
1837 if( op[j].label==0 ){
1838 if( err ){
1839 fprintf(err,"%sundefined option.\n",emsg);
1840 errline(i,0,err);
1841 }
1842 errcnt++;
1843 }else{
1844 cp++;
1845 switch( op[j].type ){
1846 case OPT_FLAG:
1847 case OPT_FFLAG:
1848 if( err ){
1849 fprintf(err,"%soption requires an argument.\n",emsg);
1850 errline(i,0,err);
1851 }
1852 errcnt++;
1853 break;
1854 case OPT_DBL:
1855 case OPT_FDBL:
1856 dv = strtod(cp,&end);
1857 if( *end ){
1858 if( err ){
1859 fprintf(err,"%sillegal character in floating-point argument.\n",emsg);
drhba99af52001-10-25 20:37:16 +00001860 errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
drh75897232000-05-29 14:26:00 +00001861 }
1862 errcnt++;
1863 }
1864 break;
1865 case OPT_INT:
1866 case OPT_FINT:
1867 lv = strtol(cp,&end,0);
1868 if( *end ){
1869 if( err ){
1870 fprintf(err,"%sillegal character in integer argument.\n",emsg);
drhba99af52001-10-25 20:37:16 +00001871 errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
drh75897232000-05-29 14:26:00 +00001872 }
1873 errcnt++;
1874 }
1875 break;
1876 case OPT_STR:
1877 case OPT_FSTR:
1878 sv = cp;
1879 break;
1880 }
1881 switch( op[j].type ){
1882 case OPT_FLAG:
1883 case OPT_FFLAG:
1884 break;
1885 case OPT_DBL:
1886 *(double*)(op[j].arg) = dv;
1887 break;
1888 case OPT_FDBL:
icculus9e44cf12010-02-14 17:14:22 +00001889 (*(void(*)(double))(op[j].arg))(dv);
drh75897232000-05-29 14:26:00 +00001890 break;
1891 case OPT_INT:
1892 *(int*)(op[j].arg) = lv;
1893 break;
1894 case OPT_FINT:
icculus9e44cf12010-02-14 17:14:22 +00001895 (*(void(*)(int))(op[j].arg))((int)lv);
drh75897232000-05-29 14:26:00 +00001896 break;
1897 case OPT_STR:
1898 *(char**)(op[j].arg) = sv;
1899 break;
1900 case OPT_FSTR:
icculus9e44cf12010-02-14 17:14:22 +00001901 (*(void(*)(char *))(op[j].arg))(sv);
drh75897232000-05-29 14:26:00 +00001902 break;
1903 }
1904 }
1905 return errcnt;
1906}
1907
icculus9e44cf12010-02-14 17:14:22 +00001908int OptInit(char **a, struct s_options *o, FILE *err)
drh75897232000-05-29 14:26:00 +00001909{
1910 int errcnt = 0;
1911 argv = a;
1912 op = o;
1913 errstream = err;
1914 if( argv && *argv && op ){
1915 int i;
1916 for(i=1; argv[i]; i++){
1917 if( argv[i][0]=='+' || argv[i][0]=='-' ){
1918 errcnt += handleflags(i,err);
1919 }else if( strchr(argv[i],'=') ){
1920 errcnt += handleswitch(i,err);
1921 }
1922 }
1923 }
1924 if( errcnt>0 ){
1925 fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
drhb0c86772000-06-02 23:21:26 +00001926 OptPrint();
drh75897232000-05-29 14:26:00 +00001927 exit(1);
1928 }
1929 return 0;
1930}
1931
drhb0c86772000-06-02 23:21:26 +00001932int OptNArgs(){
drh75897232000-05-29 14:26:00 +00001933 int cnt = 0;
1934 int dashdash = 0;
1935 int i;
1936 if( argv!=0 && argv[0]!=0 ){
1937 for(i=1; argv[i]; i++){
1938 if( dashdash || !ISOPT(argv[i]) ) cnt++;
1939 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1940 }
1941 }
1942 return cnt;
1943}
1944
icculus9e44cf12010-02-14 17:14:22 +00001945char *OptArg(int n)
drh75897232000-05-29 14:26:00 +00001946{
1947 int i;
1948 i = argindex(n);
1949 return i>=0 ? argv[i] : 0;
1950}
1951
icculus9e44cf12010-02-14 17:14:22 +00001952void OptErr(int n)
drh75897232000-05-29 14:26:00 +00001953{
1954 int i;
1955 i = argindex(n);
1956 if( i>=0 ) errline(i,0,errstream);
1957}
1958
drhb0c86772000-06-02 23:21:26 +00001959void OptPrint(){
drh75897232000-05-29 14:26:00 +00001960 int i;
1961 int max, len;
1962 max = 0;
1963 for(i=0; op[i].label; i++){
drh87cf1372008-08-13 20:09:06 +00001964 len = lemonStrlen(op[i].label) + 1;
drh75897232000-05-29 14:26:00 +00001965 switch( op[i].type ){
1966 case OPT_FLAG:
1967 case OPT_FFLAG:
1968 break;
1969 case OPT_INT:
1970 case OPT_FINT:
1971 len += 9; /* length of "<integer>" */
1972 break;
1973 case OPT_DBL:
1974 case OPT_FDBL:
1975 len += 6; /* length of "<real>" */
1976 break;
1977 case OPT_STR:
1978 case OPT_FSTR:
1979 len += 8; /* length of "<string>" */
1980 break;
1981 }
1982 if( len>max ) max = len;
1983 }
1984 for(i=0; op[i].label; i++){
1985 switch( op[i].type ){
1986 case OPT_FLAG:
1987 case OPT_FFLAG:
1988 fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
1989 break;
1990 case OPT_INT:
1991 case OPT_FINT:
1992 fprintf(errstream," %s=<integer>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00001993 (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message);
drh75897232000-05-29 14:26:00 +00001994 break;
1995 case OPT_DBL:
1996 case OPT_FDBL:
1997 fprintf(errstream," %s=<real>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00001998 (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message);
drh75897232000-05-29 14:26:00 +00001999 break;
2000 case OPT_STR:
2001 case OPT_FSTR:
2002 fprintf(errstream," %s=<string>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00002003 (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message);
drh75897232000-05-29 14:26:00 +00002004 break;
2005 }
2006 }
2007}
2008/*********************** From the file "parse.c" ****************************/
2009/*
2010** Input file parser for the LEMON parser generator.
2011*/
2012
2013/* The state of the parser */
icculus9e44cf12010-02-14 17:14:22 +00002014enum e_state {
2015 INITIALIZE,
2016 WAITING_FOR_DECL_OR_RULE,
2017 WAITING_FOR_DECL_KEYWORD,
2018 WAITING_FOR_DECL_ARG,
2019 WAITING_FOR_PRECEDENCE_SYMBOL,
2020 WAITING_FOR_ARROW,
2021 IN_RHS,
2022 LHS_ALIAS_1,
2023 LHS_ALIAS_2,
2024 LHS_ALIAS_3,
2025 RHS_ALIAS_1,
2026 RHS_ALIAS_2,
2027 PRECEDENCE_MARK_1,
2028 PRECEDENCE_MARK_2,
2029 RESYNC_AFTER_RULE_ERROR,
2030 RESYNC_AFTER_DECL_ERROR,
2031 WAITING_FOR_DESTRUCTOR_SYMBOL,
2032 WAITING_FOR_DATATYPE_SYMBOL,
2033 WAITING_FOR_FALLBACK_ID,
icculus9e44cf12010-02-14 17:14:22 +00002034 WAITING_FOR_WILDCARD_ID
2035};
drh75897232000-05-29 14:26:00 +00002036struct pstate {
2037 char *filename; /* Name of the input file */
2038 int tokenlineno; /* Linenumber at which current token starts */
2039 int errorcnt; /* Number of errors so far */
2040 char *tokenstart; /* Text of current token */
2041 struct lemon *gp; /* Global state vector */
icculus9e44cf12010-02-14 17:14:22 +00002042 enum e_state state; /* The state of the parser */
drh0bd1f4e2002-06-06 18:54:39 +00002043 struct symbol *fallback; /* The fallback token */
drh75897232000-05-29 14:26:00 +00002044 struct symbol *lhs; /* Left-hand side of current rule */
icculus9e44cf12010-02-14 17:14:22 +00002045 const char *lhsalias; /* Alias for the LHS */
drh75897232000-05-29 14:26:00 +00002046 int nrhs; /* Number of right-hand side symbols seen */
2047 struct symbol *rhs[MAXRHS]; /* RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +00002048 const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
drh75897232000-05-29 14:26:00 +00002049 struct rule *prevrule; /* Previous rule parsed */
icculus9e44cf12010-02-14 17:14:22 +00002050 const char *declkeyword; /* Keyword of a declaration */
drh75897232000-05-29 14:26:00 +00002051 char **declargslot; /* Where the declaration argument should be put */
drha5808f32008-04-27 22:19:44 +00002052 int insertLineMacro; /* Add #line before declaration insert */
drh4dc8ef52008-07-01 17:13:57 +00002053 int *decllinenoslot; /* Where to write declaration line number */
drh75897232000-05-29 14:26:00 +00002054 enum e_assoc declassoc; /* Assign this association to decl arguments */
2055 int preccounter; /* Assign this precedence to decl arguments */
2056 struct rule *firstrule; /* Pointer to first rule in the grammar */
2057 struct rule *lastrule; /* Pointer to the most recently parsed rule */
2058};
2059
2060/* Parse a single token */
icculus9e44cf12010-02-14 17:14:22 +00002061static void parseonetoken(struct pstate *psp)
drh75897232000-05-29 14:26:00 +00002062{
icculus9e44cf12010-02-14 17:14:22 +00002063 const char *x;
drh75897232000-05-29 14:26:00 +00002064 x = Strsafe(psp->tokenstart); /* Save the token permanently */
2065#if 0
2066 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
2067 x,psp->state);
2068#endif
2069 switch( psp->state ){
2070 case INITIALIZE:
2071 psp->prevrule = 0;
2072 psp->preccounter = 0;
2073 psp->firstrule = psp->lastrule = 0;
2074 psp->gp->nrule = 0;
2075 /* Fall thru to next case */
2076 case WAITING_FOR_DECL_OR_RULE:
2077 if( x[0]=='%' ){
2078 psp->state = WAITING_FOR_DECL_KEYWORD;
2079 }else if( islower(x[0]) ){
2080 psp->lhs = Symbol_new(x);
2081 psp->nrhs = 0;
2082 psp->lhsalias = 0;
2083 psp->state = WAITING_FOR_ARROW;
2084 }else if( x[0]=='{' ){
2085 if( psp->prevrule==0 ){
2086 ErrorMsg(psp->filename,psp->tokenlineno,
drh3cb2f6e2012-01-09 14:19:05 +00002087"There is no prior rule upon which to attach the code \
drh75897232000-05-29 14:26:00 +00002088fragment which begins on this line.");
2089 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002090 }else if( psp->prevrule->code!=0 ){
drh75897232000-05-29 14:26:00 +00002091 ErrorMsg(psp->filename,psp->tokenlineno,
2092"Code fragment beginning on this line is not the first \
2093to follow the previous rule.");
2094 psp->errorcnt++;
2095 }else{
2096 psp->prevrule->line = psp->tokenlineno;
2097 psp->prevrule->code = &x[1];
drhf2f105d2012-08-20 15:53:54 +00002098 }
drh75897232000-05-29 14:26:00 +00002099 }else if( x[0]=='[' ){
2100 psp->state = PRECEDENCE_MARK_1;
2101 }else{
2102 ErrorMsg(psp->filename,psp->tokenlineno,
2103 "Token \"%s\" should be either \"%%\" or a nonterminal name.",
2104 x);
2105 psp->errorcnt++;
2106 }
2107 break;
2108 case PRECEDENCE_MARK_1:
2109 if( !isupper(x[0]) ){
2110 ErrorMsg(psp->filename,psp->tokenlineno,
2111 "The precedence symbol must be a terminal.");
2112 psp->errorcnt++;
2113 }else if( psp->prevrule==0 ){
2114 ErrorMsg(psp->filename,psp->tokenlineno,
2115 "There is no prior rule to assign precedence \"[%s]\".",x);
2116 psp->errorcnt++;
2117 }else if( psp->prevrule->precsym!=0 ){
2118 ErrorMsg(psp->filename,psp->tokenlineno,
2119"Precedence mark on this line is not the first \
2120to follow the previous rule.");
2121 psp->errorcnt++;
2122 }else{
2123 psp->prevrule->precsym = Symbol_new(x);
2124 }
2125 psp->state = PRECEDENCE_MARK_2;
2126 break;
2127 case PRECEDENCE_MARK_2:
2128 if( x[0]!=']' ){
2129 ErrorMsg(psp->filename,psp->tokenlineno,
2130 "Missing \"]\" on precedence mark.");
2131 psp->errorcnt++;
2132 }
2133 psp->state = WAITING_FOR_DECL_OR_RULE;
2134 break;
2135 case WAITING_FOR_ARROW:
2136 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2137 psp->state = IN_RHS;
2138 }else if( x[0]=='(' ){
2139 psp->state = LHS_ALIAS_1;
2140 }else{
2141 ErrorMsg(psp->filename,psp->tokenlineno,
2142 "Expected to see a \":\" following the LHS symbol \"%s\".",
2143 psp->lhs->name);
2144 psp->errorcnt++;
2145 psp->state = RESYNC_AFTER_RULE_ERROR;
2146 }
2147 break;
2148 case LHS_ALIAS_1:
2149 if( isalpha(x[0]) ){
2150 psp->lhsalias = x;
2151 psp->state = LHS_ALIAS_2;
2152 }else{
2153 ErrorMsg(psp->filename,psp->tokenlineno,
2154 "\"%s\" is not a valid alias for the LHS \"%s\"\n",
2155 x,psp->lhs->name);
2156 psp->errorcnt++;
2157 psp->state = RESYNC_AFTER_RULE_ERROR;
2158 }
2159 break;
2160 case LHS_ALIAS_2:
2161 if( x[0]==')' ){
2162 psp->state = LHS_ALIAS_3;
2163 }else{
2164 ErrorMsg(psp->filename,psp->tokenlineno,
2165 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2166 psp->errorcnt++;
2167 psp->state = RESYNC_AFTER_RULE_ERROR;
2168 }
2169 break;
2170 case LHS_ALIAS_3:
2171 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2172 psp->state = IN_RHS;
2173 }else{
2174 ErrorMsg(psp->filename,psp->tokenlineno,
2175 "Missing \"->\" following: \"%s(%s)\".",
2176 psp->lhs->name,psp->lhsalias);
2177 psp->errorcnt++;
2178 psp->state = RESYNC_AFTER_RULE_ERROR;
2179 }
2180 break;
2181 case IN_RHS:
2182 if( x[0]=='.' ){
2183 struct rule *rp;
drh9892c5d2007-12-21 00:02:11 +00002184 rp = (struct rule *)calloc( sizeof(struct rule) +
2185 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
drh75897232000-05-29 14:26:00 +00002186 if( rp==0 ){
2187 ErrorMsg(psp->filename,psp->tokenlineno,
2188 "Can't allocate enough memory for this rule.");
2189 psp->errorcnt++;
2190 psp->prevrule = 0;
drhf2f105d2012-08-20 15:53:54 +00002191 }else{
drh75897232000-05-29 14:26:00 +00002192 int i;
2193 rp->ruleline = psp->tokenlineno;
2194 rp->rhs = (struct symbol**)&rp[1];
icculus9e44cf12010-02-14 17:14:22 +00002195 rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
drh75897232000-05-29 14:26:00 +00002196 for(i=0; i<psp->nrhs; i++){
2197 rp->rhs[i] = psp->rhs[i];
2198 rp->rhsalias[i] = psp->alias[i];
drhf2f105d2012-08-20 15:53:54 +00002199 }
drh75897232000-05-29 14:26:00 +00002200 rp->lhs = psp->lhs;
2201 rp->lhsalias = psp->lhsalias;
2202 rp->nrhs = psp->nrhs;
2203 rp->code = 0;
2204 rp->precsym = 0;
2205 rp->index = psp->gp->nrule++;
2206 rp->nextlhs = rp->lhs->rule;
2207 rp->lhs->rule = rp;
2208 rp->next = 0;
2209 if( psp->firstrule==0 ){
2210 psp->firstrule = psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002211 }else{
drh75897232000-05-29 14:26:00 +00002212 psp->lastrule->next = rp;
2213 psp->lastrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002214 }
drh75897232000-05-29 14:26:00 +00002215 psp->prevrule = rp;
drhf2f105d2012-08-20 15:53:54 +00002216 }
drh75897232000-05-29 14:26:00 +00002217 psp->state = WAITING_FOR_DECL_OR_RULE;
2218 }else if( isalpha(x[0]) ){
2219 if( psp->nrhs>=MAXRHS ){
2220 ErrorMsg(psp->filename,psp->tokenlineno,
drhc4dd3fd2008-01-22 01:48:05 +00002221 "Too many symbols on RHS of rule beginning at \"%s\".",
drh75897232000-05-29 14:26:00 +00002222 x);
2223 psp->errorcnt++;
2224 psp->state = RESYNC_AFTER_RULE_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002225 }else{
drh75897232000-05-29 14:26:00 +00002226 psp->rhs[psp->nrhs] = Symbol_new(x);
2227 psp->alias[psp->nrhs] = 0;
2228 psp->nrhs++;
drhf2f105d2012-08-20 15:53:54 +00002229 }
drhfd405312005-11-06 04:06:59 +00002230 }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
2231 struct symbol *msp = psp->rhs[psp->nrhs-1];
2232 if( msp->type!=MULTITERMINAL ){
2233 struct symbol *origsp = msp;
icculus9e44cf12010-02-14 17:14:22 +00002234 msp = (struct symbol *) calloc(1,sizeof(*msp));
drhfd405312005-11-06 04:06:59 +00002235 memset(msp, 0, sizeof(*msp));
2236 msp->type = MULTITERMINAL;
2237 msp->nsubsym = 1;
icculus9e44cf12010-02-14 17:14:22 +00002238 msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*));
drhfd405312005-11-06 04:06:59 +00002239 msp->subsym[0] = origsp;
2240 msp->name = origsp->name;
2241 psp->rhs[psp->nrhs-1] = msp;
2242 }
2243 msp->nsubsym++;
icculus9e44cf12010-02-14 17:14:22 +00002244 msp->subsym = (struct symbol **) realloc(msp->subsym,
2245 sizeof(struct symbol*)*msp->nsubsym);
drhfd405312005-11-06 04:06:59 +00002246 msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
2247 if( islower(x[1]) || islower(msp->subsym[0]->name[0]) ){
2248 ErrorMsg(psp->filename,psp->tokenlineno,
2249 "Cannot form a compound containing a non-terminal");
2250 psp->errorcnt++;
2251 }
drh75897232000-05-29 14:26:00 +00002252 }else if( x[0]=='(' && psp->nrhs>0 ){
2253 psp->state = RHS_ALIAS_1;
2254 }else{
2255 ErrorMsg(psp->filename,psp->tokenlineno,
2256 "Illegal character on RHS of rule: \"%s\".",x);
2257 psp->errorcnt++;
2258 psp->state = RESYNC_AFTER_RULE_ERROR;
2259 }
2260 break;
2261 case RHS_ALIAS_1:
2262 if( isalpha(x[0]) ){
2263 psp->alias[psp->nrhs-1] = x;
2264 psp->state = RHS_ALIAS_2;
2265 }else{
2266 ErrorMsg(psp->filename,psp->tokenlineno,
2267 "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
2268 x,psp->rhs[psp->nrhs-1]->name);
2269 psp->errorcnt++;
2270 psp->state = RESYNC_AFTER_RULE_ERROR;
2271 }
2272 break;
2273 case RHS_ALIAS_2:
2274 if( x[0]==')' ){
2275 psp->state = IN_RHS;
2276 }else{
2277 ErrorMsg(psp->filename,psp->tokenlineno,
2278 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2279 psp->errorcnt++;
2280 psp->state = RESYNC_AFTER_RULE_ERROR;
2281 }
2282 break;
2283 case WAITING_FOR_DECL_KEYWORD:
2284 if( isalpha(x[0]) ){
2285 psp->declkeyword = x;
2286 psp->declargslot = 0;
drh4dc8ef52008-07-01 17:13:57 +00002287 psp->decllinenoslot = 0;
drha5808f32008-04-27 22:19:44 +00002288 psp->insertLineMacro = 1;
drh75897232000-05-29 14:26:00 +00002289 psp->state = WAITING_FOR_DECL_ARG;
2290 if( strcmp(x,"name")==0 ){
2291 psp->declargslot = &(psp->gp->name);
drha5808f32008-04-27 22:19:44 +00002292 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002293 }else if( strcmp(x,"include")==0 ){
drh75897232000-05-29 14:26:00 +00002294 psp->declargslot = &(psp->gp->include);
drhf2f105d2012-08-20 15:53:54 +00002295 }else if( strcmp(x,"code")==0 ){
drh75897232000-05-29 14:26:00 +00002296 psp->declargslot = &(psp->gp->extracode);
drhf2f105d2012-08-20 15:53:54 +00002297 }else if( strcmp(x,"token_destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002298 psp->declargslot = &psp->gp->tokendest;
drhf2f105d2012-08-20 15:53:54 +00002299 }else if( strcmp(x,"default_destructor")==0 ){
drh960e8c62001-04-03 16:53:21 +00002300 psp->declargslot = &psp->gp->vardest;
drhf2f105d2012-08-20 15:53:54 +00002301 }else if( strcmp(x,"token_prefix")==0 ){
drh75897232000-05-29 14:26:00 +00002302 psp->declargslot = &psp->gp->tokenprefix;
drha5808f32008-04-27 22:19:44 +00002303 psp->insertLineMacro = 0;
drhf2f105d2012-08-20 15:53:54 +00002304 }else if( strcmp(x,"syntax_error")==0 ){
drh75897232000-05-29 14:26:00 +00002305 psp->declargslot = &(psp->gp->error);
drhf2f105d2012-08-20 15:53:54 +00002306 }else if( strcmp(x,"parse_accept")==0 ){
drh75897232000-05-29 14:26:00 +00002307 psp->declargslot = &(psp->gp->accept);
drhf2f105d2012-08-20 15:53:54 +00002308 }else if( strcmp(x,"parse_failure")==0 ){
drh75897232000-05-29 14:26:00 +00002309 psp->declargslot = &(psp->gp->failure);
drhf2f105d2012-08-20 15:53:54 +00002310 }else if( strcmp(x,"stack_overflow")==0 ){
drh75897232000-05-29 14:26:00 +00002311 psp->declargslot = &(psp->gp->overflow);
drh75897232000-05-29 14:26:00 +00002312 }else if( strcmp(x,"extra_argument")==0 ){
2313 psp->declargslot = &(psp->gp->arg);
drha5808f32008-04-27 22:19:44 +00002314 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002315 }else if( strcmp(x,"token_type")==0 ){
2316 psp->declargslot = &(psp->gp->tokentype);
drha5808f32008-04-27 22:19:44 +00002317 psp->insertLineMacro = 0;
drh960e8c62001-04-03 16:53:21 +00002318 }else if( strcmp(x,"default_type")==0 ){
2319 psp->declargslot = &(psp->gp->vartype);
drha5808f32008-04-27 22:19:44 +00002320 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002321 }else if( strcmp(x,"stack_size")==0 ){
2322 psp->declargslot = &(psp->gp->stacksize);
drha5808f32008-04-27 22:19:44 +00002323 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002324 }else if( strcmp(x,"start_symbol")==0 ){
2325 psp->declargslot = &(psp->gp->start);
drha5808f32008-04-27 22:19:44 +00002326 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002327 }else if( strcmp(x,"left")==0 ){
2328 psp->preccounter++;
2329 psp->declassoc = LEFT;
2330 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2331 }else if( strcmp(x,"right")==0 ){
2332 psp->preccounter++;
2333 psp->declassoc = RIGHT;
2334 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2335 }else if( strcmp(x,"nonassoc")==0 ){
2336 psp->preccounter++;
2337 psp->declassoc = NONE;
2338 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002339 }else if( strcmp(x,"destructor")==0 ){
drh75897232000-05-29 14:26:00 +00002340 psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
drhf2f105d2012-08-20 15:53:54 +00002341 }else if( strcmp(x,"type")==0 ){
drh75897232000-05-29 14:26:00 +00002342 psp->state = WAITING_FOR_DATATYPE_SYMBOL;
drh0bd1f4e2002-06-06 18:54:39 +00002343 }else if( strcmp(x,"fallback")==0 ){
2344 psp->fallback = 0;
2345 psp->state = WAITING_FOR_FALLBACK_ID;
drhe09daa92006-06-10 13:29:31 +00002346 }else if( strcmp(x,"wildcard")==0 ){
2347 psp->state = WAITING_FOR_WILDCARD_ID;
drh75897232000-05-29 14:26:00 +00002348 }else{
2349 ErrorMsg(psp->filename,psp->tokenlineno,
2350 "Unknown declaration keyword: \"%%%s\".",x);
2351 psp->errorcnt++;
2352 psp->state = RESYNC_AFTER_DECL_ERROR;
drhf2f105d2012-08-20 15:53:54 +00002353 }
drh75897232000-05-29 14:26:00 +00002354 }else{
2355 ErrorMsg(psp->filename,psp->tokenlineno,
2356 "Illegal declaration keyword: \"%s\".",x);
2357 psp->errorcnt++;
2358 psp->state = RESYNC_AFTER_DECL_ERROR;
2359 }
2360 break;
2361 case WAITING_FOR_DESTRUCTOR_SYMBOL:
2362 if( !isalpha(x[0]) ){
2363 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002364 "Symbol name missing after %%destructor keyword");
drh75897232000-05-29 14:26:00 +00002365 psp->errorcnt++;
2366 psp->state = RESYNC_AFTER_DECL_ERROR;
2367 }else{
icculusd286fa62010-03-03 17:06:32 +00002368 struct symbol *sp = Symbol_new(x);
2369 psp->declargslot = &sp->destructor;
2370 psp->decllinenoslot = &sp->destLineno;
2371 psp->insertLineMacro = 1;
2372 psp->state = WAITING_FOR_DECL_ARG;
drh75897232000-05-29 14:26:00 +00002373 }
2374 break;
2375 case WAITING_FOR_DATATYPE_SYMBOL:
2376 if( !isalpha(x[0]) ){
2377 ErrorMsg(psp->filename,psp->tokenlineno,
icculusd0d97b02010-02-17 20:22:10 +00002378 "Symbol name missing after %%type keyword");
drh75897232000-05-29 14:26:00 +00002379 psp->errorcnt++;
2380 psp->state = RESYNC_AFTER_DECL_ERROR;
2381 }else{
icculus866bf1e2010-02-17 20:31:32 +00002382 struct symbol *sp = Symbol_find(x);
2383 if((sp) && (sp->datatype)){
2384 ErrorMsg(psp->filename,psp->tokenlineno,
2385 "Symbol %%type \"%s\" already defined", x);
2386 psp->errorcnt++;
2387 psp->state = RESYNC_AFTER_DECL_ERROR;
2388 }else{
2389 if (!sp){
2390 sp = Symbol_new(x);
2391 }
2392 psp->declargslot = &sp->datatype;
2393 psp->insertLineMacro = 0;
2394 psp->state = WAITING_FOR_DECL_ARG;
2395 }
drh75897232000-05-29 14:26:00 +00002396 }
2397 break;
2398 case WAITING_FOR_PRECEDENCE_SYMBOL:
2399 if( x[0]=='.' ){
2400 psp->state = WAITING_FOR_DECL_OR_RULE;
2401 }else if( isupper(x[0]) ){
2402 struct symbol *sp;
2403 sp = Symbol_new(x);
2404 if( sp->prec>=0 ){
2405 ErrorMsg(psp->filename,psp->tokenlineno,
2406 "Symbol \"%s\" has already be given a precedence.",x);
2407 psp->errorcnt++;
drhf2f105d2012-08-20 15:53:54 +00002408 }else{
drh75897232000-05-29 14:26:00 +00002409 sp->prec = psp->preccounter;
2410 sp->assoc = psp->declassoc;
drhf2f105d2012-08-20 15:53:54 +00002411 }
drh75897232000-05-29 14:26:00 +00002412 }else{
2413 ErrorMsg(psp->filename,psp->tokenlineno,
2414 "Can't assign a precedence to \"%s\".",x);
2415 psp->errorcnt++;
2416 }
2417 break;
2418 case WAITING_FOR_DECL_ARG:
drha5808f32008-04-27 22:19:44 +00002419 if( x[0]=='{' || x[0]=='\"' || isalnum(x[0]) ){
icculus9e44cf12010-02-14 17:14:22 +00002420 const char *zOld, *zNew;
2421 char *zBuf, *z;
drha5808f32008-04-27 22:19:44 +00002422 int nOld, n, nLine, nNew, nBack;
drhb5bd49e2008-07-14 12:21:08 +00002423 int addLineMacro;
drha5808f32008-04-27 22:19:44 +00002424 char zLine[50];
2425 zNew = x;
2426 if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
drh87cf1372008-08-13 20:09:06 +00002427 nNew = lemonStrlen(zNew);
drha5808f32008-04-27 22:19:44 +00002428 if( *psp->declargslot ){
2429 zOld = *psp->declargslot;
2430 }else{
2431 zOld = "";
2432 }
drh87cf1372008-08-13 20:09:06 +00002433 nOld = lemonStrlen(zOld);
drha5808f32008-04-27 22:19:44 +00002434 n = nOld + nNew + 20;
shane58543932008-12-10 20:10:04 +00002435 addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro &&
drhb5bd49e2008-07-14 12:21:08 +00002436 (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
2437 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002438 for(z=psp->filename, nBack=0; *z; z++){
2439 if( *z=='\\' ) nBack++;
2440 }
drh898799f2014-01-10 23:21:00 +00002441 lemon_sprintf(zLine, "#line %d ", psp->tokenlineno);
drh87cf1372008-08-13 20:09:06 +00002442 nLine = lemonStrlen(zLine);
2443 n += nLine + lemonStrlen(psp->filename) + nBack;
drha5808f32008-04-27 22:19:44 +00002444 }
icculus9e44cf12010-02-14 17:14:22 +00002445 *psp->declargslot = (char *) realloc(*psp->declargslot, n);
2446 zBuf = *psp->declargslot + nOld;
drhb5bd49e2008-07-14 12:21:08 +00002447 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002448 if( nOld && zBuf[-1]!='\n' ){
2449 *(zBuf++) = '\n';
2450 }
2451 memcpy(zBuf, zLine, nLine);
2452 zBuf += nLine;
2453 *(zBuf++) = '"';
2454 for(z=psp->filename; *z; z++){
2455 if( *z=='\\' ){
2456 *(zBuf++) = '\\';
2457 }
2458 *(zBuf++) = *z;
2459 }
2460 *(zBuf++) = '"';
2461 *(zBuf++) = '\n';
2462 }
drh4dc8ef52008-07-01 17:13:57 +00002463 if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){
2464 psp->decllinenoslot[0] = psp->tokenlineno;
2465 }
drha5808f32008-04-27 22:19:44 +00002466 memcpy(zBuf, zNew, nNew);
2467 zBuf += nNew;
2468 *zBuf = 0;
2469 psp->state = WAITING_FOR_DECL_OR_RULE;
drh75897232000-05-29 14:26:00 +00002470 }else{
2471 ErrorMsg(psp->filename,psp->tokenlineno,
2472 "Illegal argument to %%%s: %s",psp->declkeyword,x);
2473 psp->errorcnt++;
2474 psp->state = RESYNC_AFTER_DECL_ERROR;
2475 }
2476 break;
drh0bd1f4e2002-06-06 18:54:39 +00002477 case WAITING_FOR_FALLBACK_ID:
2478 if( x[0]=='.' ){
2479 psp->state = WAITING_FOR_DECL_OR_RULE;
2480 }else if( !isupper(x[0]) ){
2481 ErrorMsg(psp->filename, psp->tokenlineno,
2482 "%%fallback argument \"%s\" should be a token", x);
2483 psp->errorcnt++;
2484 }else{
2485 struct symbol *sp = Symbol_new(x);
2486 if( psp->fallback==0 ){
2487 psp->fallback = sp;
2488 }else if( sp->fallback ){
2489 ErrorMsg(psp->filename, psp->tokenlineno,
2490 "More than one fallback assigned to token %s", x);
2491 psp->errorcnt++;
2492 }else{
2493 sp->fallback = psp->fallback;
2494 psp->gp->has_fallback = 1;
2495 }
2496 }
2497 break;
drhe09daa92006-06-10 13:29:31 +00002498 case WAITING_FOR_WILDCARD_ID:
2499 if( x[0]=='.' ){
2500 psp->state = WAITING_FOR_DECL_OR_RULE;
2501 }else if( !isupper(x[0]) ){
2502 ErrorMsg(psp->filename, psp->tokenlineno,
2503 "%%wildcard argument \"%s\" should be a token", x);
2504 psp->errorcnt++;
2505 }else{
2506 struct symbol *sp = Symbol_new(x);
2507 if( psp->gp->wildcard==0 ){
2508 psp->gp->wildcard = sp;
2509 }else{
2510 ErrorMsg(psp->filename, psp->tokenlineno,
2511 "Extra wildcard to token: %s", x);
2512 psp->errorcnt++;
2513 }
2514 }
2515 break;
drh75897232000-05-29 14:26:00 +00002516 case RESYNC_AFTER_RULE_ERROR:
2517/* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2518** break; */
2519 case RESYNC_AFTER_DECL_ERROR:
2520 if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2521 if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
2522 break;
2523 }
2524}
2525
drh34ff57b2008-07-14 12:27:51 +00002526/* Run the preprocessor over the input file text. The global variables
drh6d08b4d2004-07-20 12:45:22 +00002527** azDefine[0] through azDefine[nDefine-1] contains the names of all defined
2528** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and
2529** comments them out. Text in between is also commented out as appropriate.
2530*/
danielk1977940fac92005-01-23 22:41:37 +00002531static void preprocess_input(char *z){
drh6d08b4d2004-07-20 12:45:22 +00002532 int i, j, k, n;
2533 int exclude = 0;
rse38514a92007-09-20 11:34:17 +00002534 int start = 0;
drh6d08b4d2004-07-20 12:45:22 +00002535 int lineno = 1;
rse38514a92007-09-20 11:34:17 +00002536 int start_lineno = 1;
drh6d08b4d2004-07-20 12:45:22 +00002537 for(i=0; z[i]; i++){
2538 if( z[i]=='\n' ) lineno++;
2539 if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue;
2540 if( strncmp(&z[i],"%endif",6)==0 && isspace(z[i+6]) ){
2541 if( exclude ){
2542 exclude--;
2543 if( exclude==0 ){
2544 for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
2545 }
2546 }
2547 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2548 }else if( (strncmp(&z[i],"%ifdef",6)==0 && isspace(z[i+6]))
2549 || (strncmp(&z[i],"%ifndef",7)==0 && isspace(z[i+7])) ){
2550 if( exclude ){
2551 exclude++;
2552 }else{
2553 for(j=i+7; isspace(z[j]); j++){}
2554 for(n=0; z[j+n] && !isspace(z[j+n]); n++){}
2555 exclude = 1;
2556 for(k=0; k<nDefine; k++){
drh87cf1372008-08-13 20:09:06 +00002557 if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){
drh6d08b4d2004-07-20 12:45:22 +00002558 exclude = 0;
2559 break;
2560 }
2561 }
2562 if( z[i+3]=='n' ) exclude = !exclude;
2563 if( exclude ){
2564 start = i;
2565 start_lineno = lineno;
2566 }
2567 }
2568 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2569 }
2570 }
2571 if( exclude ){
2572 fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
2573 exit(1);
2574 }
2575}
2576
drh75897232000-05-29 14:26:00 +00002577/* In spite of its name, this function is really a scanner. It read
2578** in the entire input file (all at once) then tokenizes it. Each
2579** token is passed to the function "parseonetoken" which builds all
2580** the appropriate data structures in the global state vector "gp".
2581*/
icculus9e44cf12010-02-14 17:14:22 +00002582void Parse(struct lemon *gp)
drh75897232000-05-29 14:26:00 +00002583{
2584 struct pstate ps;
2585 FILE *fp;
2586 char *filebuf;
2587 int filesize;
2588 int lineno;
2589 int c;
2590 char *cp, *nextcp;
2591 int startline = 0;
2592
rse38514a92007-09-20 11:34:17 +00002593 memset(&ps, '\0', sizeof(ps));
drh75897232000-05-29 14:26:00 +00002594 ps.gp = gp;
2595 ps.filename = gp->filename;
2596 ps.errorcnt = 0;
2597 ps.state = INITIALIZE;
2598
2599 /* Begin by reading the input file */
2600 fp = fopen(ps.filename,"rb");
2601 if( fp==0 ){
2602 ErrorMsg(ps.filename,0,"Can't open this file for reading.");
2603 gp->errorcnt++;
2604 return;
2605 }
2606 fseek(fp,0,2);
2607 filesize = ftell(fp);
2608 rewind(fp);
2609 filebuf = (char *)malloc( filesize+1 );
2610 if( filebuf==0 ){
2611 ErrorMsg(ps.filename,0,"Can't allocate %d of memory to hold this file.",
2612 filesize+1);
2613 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002614 fclose(fp);
drh75897232000-05-29 14:26:00 +00002615 return;
2616 }
2617 if( fread(filebuf,1,filesize,fp)!=filesize ){
2618 ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
2619 filesize);
2620 free(filebuf);
2621 gp->errorcnt++;
drhe0a59cf2011-08-30 00:58:58 +00002622 fclose(fp);
drh75897232000-05-29 14:26:00 +00002623 return;
2624 }
2625 fclose(fp);
2626 filebuf[filesize] = 0;
2627
drh6d08b4d2004-07-20 12:45:22 +00002628 /* Make an initial pass through the file to handle %ifdef and %ifndef */
2629 preprocess_input(filebuf);
2630
drh75897232000-05-29 14:26:00 +00002631 /* Now scan the text of the input file */
2632 lineno = 1;
2633 for(cp=filebuf; (c= *cp)!=0; ){
2634 if( c=='\n' ) lineno++; /* Keep track of the line number */
2635 if( isspace(c) ){ cp++; continue; } /* Skip all white space */
2636 if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
2637 cp+=2;
2638 while( (c= *cp)!=0 && c!='\n' ) cp++;
2639 continue;
2640 }
2641 if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
2642 cp+=2;
2643 while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
2644 if( c=='\n' ) lineno++;
2645 cp++;
2646 }
2647 if( c ) cp++;
2648 continue;
2649 }
2650 ps.tokenstart = cp; /* Mark the beginning of the token */
2651 ps.tokenlineno = lineno; /* Linenumber on which token begins */
2652 if( c=='\"' ){ /* String literals */
2653 cp++;
2654 while( (c= *cp)!=0 && c!='\"' ){
2655 if( c=='\n' ) lineno++;
2656 cp++;
2657 }
2658 if( c==0 ){
2659 ErrorMsg(ps.filename,startline,
2660"String starting on this line is not terminated before the end of the file.");
2661 ps.errorcnt++;
2662 nextcp = cp;
2663 }else{
2664 nextcp = cp+1;
2665 }
2666 }else if( c=='{' ){ /* A block of C code */
2667 int level;
2668 cp++;
2669 for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
2670 if( c=='\n' ) lineno++;
2671 else if( c=='{' ) level++;
2672 else if( c=='}' ) level--;
2673 else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
2674 int prevc;
2675 cp = &cp[2];
2676 prevc = 0;
2677 while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
2678 if( c=='\n' ) lineno++;
2679 prevc = c;
2680 cp++;
drhf2f105d2012-08-20 15:53:54 +00002681 }
2682 }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
drh75897232000-05-29 14:26:00 +00002683 cp = &cp[2];
2684 while( (c= *cp)!=0 && c!='\n' ) cp++;
2685 if( c ) lineno++;
drhf2f105d2012-08-20 15:53:54 +00002686 }else if( c=='\'' || c=='\"' ){ /* String a character literals */
drh75897232000-05-29 14:26:00 +00002687 int startchar, prevc;
2688 startchar = c;
2689 prevc = 0;
2690 for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
2691 if( c=='\n' ) lineno++;
2692 if( prevc=='\\' ) prevc = 0;
2693 else prevc = c;
drhf2f105d2012-08-20 15:53:54 +00002694 }
2695 }
drh75897232000-05-29 14:26:00 +00002696 }
2697 if( c==0 ){
drh960e8c62001-04-03 16:53:21 +00002698 ErrorMsg(ps.filename,ps.tokenlineno,
drh75897232000-05-29 14:26:00 +00002699"C code starting on this line is not terminated before the end of the file.");
2700 ps.errorcnt++;
2701 nextcp = cp;
2702 }else{
2703 nextcp = cp+1;
2704 }
2705 }else if( isalnum(c) ){ /* Identifiers */
2706 while( (c= *cp)!=0 && (isalnum(c) || c=='_') ) cp++;
2707 nextcp = cp;
2708 }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
2709 cp += 3;
2710 nextcp = cp;
drhfd405312005-11-06 04:06:59 +00002711 }else if( (c=='/' || c=='|') && isalpha(cp[1]) ){
2712 cp += 2;
2713 while( (c = *cp)!=0 && (isalnum(c) || c=='_') ) cp++;
2714 nextcp = cp;
drh75897232000-05-29 14:26:00 +00002715 }else{ /* All other (one character) operators */
2716 cp++;
2717 nextcp = cp;
2718 }
2719 c = *cp;
2720 *cp = 0; /* Null terminate the token */
2721 parseonetoken(&ps); /* Parse the token */
2722 *cp = c; /* Restore the buffer */
2723 cp = nextcp;
2724 }
2725 free(filebuf); /* Release the buffer after parsing */
2726 gp->rule = ps.firstrule;
2727 gp->errorcnt = ps.errorcnt;
2728}
2729/*************************** From the file "plink.c" *********************/
2730/*
2731** Routines processing configuration follow-set propagation links
2732** in the LEMON parser generator.
2733*/
2734static struct plink *plink_freelist = 0;
2735
2736/* Allocate a new plink */
2737struct plink *Plink_new(){
icculus9e44cf12010-02-14 17:14:22 +00002738 struct plink *newlink;
drh75897232000-05-29 14:26:00 +00002739
2740 if( plink_freelist==0 ){
2741 int i;
2742 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +00002743 plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
drh75897232000-05-29 14:26:00 +00002744 if( plink_freelist==0 ){
2745 fprintf(stderr,
2746 "Unable to allocate memory for a new follow-set propagation link.\n");
2747 exit(1);
2748 }
2749 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2750 plink_freelist[amt-1].next = 0;
2751 }
icculus9e44cf12010-02-14 17:14:22 +00002752 newlink = plink_freelist;
drh75897232000-05-29 14:26:00 +00002753 plink_freelist = plink_freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00002754 return newlink;
drh75897232000-05-29 14:26:00 +00002755}
2756
2757/* Add a plink to a plink list */
icculus9e44cf12010-02-14 17:14:22 +00002758void Plink_add(struct plink **plpp, struct config *cfp)
drh75897232000-05-29 14:26:00 +00002759{
icculus9e44cf12010-02-14 17:14:22 +00002760 struct plink *newlink;
2761 newlink = Plink_new();
2762 newlink->next = *plpp;
2763 *plpp = newlink;
2764 newlink->cfp = cfp;
drh75897232000-05-29 14:26:00 +00002765}
2766
2767/* Transfer every plink on the list "from" to the list "to" */
icculus9e44cf12010-02-14 17:14:22 +00002768void Plink_copy(struct plink **to, struct plink *from)
drh75897232000-05-29 14:26:00 +00002769{
2770 struct plink *nextpl;
2771 while( from ){
2772 nextpl = from->next;
2773 from->next = *to;
2774 *to = from;
2775 from = nextpl;
2776 }
2777}
2778
2779/* Delete every plink on the list */
icculus9e44cf12010-02-14 17:14:22 +00002780void Plink_delete(struct plink *plp)
drh75897232000-05-29 14:26:00 +00002781{
2782 struct plink *nextpl;
2783
2784 while( plp ){
2785 nextpl = plp->next;
2786 plp->next = plink_freelist;
2787 plink_freelist = plp;
2788 plp = nextpl;
2789 }
2790}
2791/*********************** From the file "report.c" **************************/
2792/*
2793** Procedures for generating reports and tables in the LEMON parser generator.
2794*/
2795
2796/* Generate a filename with the given suffix. Space to hold the
2797** name comes from malloc() and must be freed by the calling
2798** function.
2799*/
icculus9e44cf12010-02-14 17:14:22 +00002800PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
drh75897232000-05-29 14:26:00 +00002801{
2802 char *name;
2803 char *cp;
2804
icculus9e44cf12010-02-14 17:14:22 +00002805 name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
drh75897232000-05-29 14:26:00 +00002806 if( name==0 ){
2807 fprintf(stderr,"Can't allocate space for a filename.\n");
2808 exit(1);
2809 }
drh898799f2014-01-10 23:21:00 +00002810 lemon_strcpy(name,lemp->filename);
drh75897232000-05-29 14:26:00 +00002811 cp = strrchr(name,'.');
2812 if( cp ) *cp = 0;
drh898799f2014-01-10 23:21:00 +00002813 lemon_strcat(name,suffix);
drh75897232000-05-29 14:26:00 +00002814 return name;
2815}
2816
2817/* Open a file with a name based on the name of the input file,
2818** but with a different (specified) suffix, and return a pointer
2819** to the stream */
icculus9e44cf12010-02-14 17:14:22 +00002820PRIVATE FILE *file_open(
2821 struct lemon *lemp,
2822 const char *suffix,
2823 const char *mode
2824){
drh75897232000-05-29 14:26:00 +00002825 FILE *fp;
2826
2827 if( lemp->outname ) free(lemp->outname);
2828 lemp->outname = file_makename(lemp, suffix);
2829 fp = fopen(lemp->outname,mode);
2830 if( fp==0 && *mode=='w' ){
2831 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
2832 lemp->errorcnt++;
2833 return 0;
2834 }
2835 return fp;
2836}
2837
2838/* Duplicate the input file without comments and without actions
2839** on rules */
icculus9e44cf12010-02-14 17:14:22 +00002840void Reprint(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00002841{
2842 struct rule *rp;
2843 struct symbol *sp;
2844 int i, j, maxlen, len, ncolumns, skip;
2845 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
2846 maxlen = 10;
2847 for(i=0; i<lemp->nsymbol; i++){
2848 sp = lemp->symbols[i];
drh87cf1372008-08-13 20:09:06 +00002849 len = lemonStrlen(sp->name);
drh75897232000-05-29 14:26:00 +00002850 if( len>maxlen ) maxlen = len;
2851 }
2852 ncolumns = 76/(maxlen+5);
2853 if( ncolumns<1 ) ncolumns = 1;
2854 skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
2855 for(i=0; i<skip; i++){
2856 printf("//");
2857 for(j=i; j<lemp->nsymbol; j+=skip){
2858 sp = lemp->symbols[j];
2859 assert( sp->index==j );
2860 printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
2861 }
2862 printf("\n");
2863 }
2864 for(rp=lemp->rule; rp; rp=rp->next){
2865 printf("%s",rp->lhs->name);
drhfd405312005-11-06 04:06:59 +00002866 /* if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
drh75897232000-05-29 14:26:00 +00002867 printf(" ::=");
2868 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +00002869 sp = rp->rhs[i];
2870 printf(" %s", sp->name);
2871 if( sp->type==MULTITERMINAL ){
2872 for(j=1; j<sp->nsubsym; j++){
2873 printf("|%s", sp->subsym[j]->name);
2874 }
2875 }
2876 /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
drh75897232000-05-29 14:26:00 +00002877 }
2878 printf(".");
2879 if( rp->precsym ) printf(" [%s]",rp->precsym->name);
drhfd405312005-11-06 04:06:59 +00002880 /* if( rp->code ) printf("\n %s",rp->code); */
drh75897232000-05-29 14:26:00 +00002881 printf("\n");
2882 }
2883}
2884
icculus9e44cf12010-02-14 17:14:22 +00002885void ConfigPrint(FILE *fp, struct config *cfp)
drh75897232000-05-29 14:26:00 +00002886{
2887 struct rule *rp;
drhfd405312005-11-06 04:06:59 +00002888 struct symbol *sp;
2889 int i, j;
drh75897232000-05-29 14:26:00 +00002890 rp = cfp->rp;
2891 fprintf(fp,"%s ::=",rp->lhs->name);
2892 for(i=0; i<=rp->nrhs; i++){
2893 if( i==cfp->dot ) fprintf(fp," *");
2894 if( i==rp->nrhs ) break;
drhfd405312005-11-06 04:06:59 +00002895 sp = rp->rhs[i];
2896 fprintf(fp," %s", sp->name);
2897 if( sp->type==MULTITERMINAL ){
2898 for(j=1; j<sp->nsubsym; j++){
2899 fprintf(fp,"|%s",sp->subsym[j]->name);
2900 }
2901 }
drh75897232000-05-29 14:26:00 +00002902 }
2903}
2904
2905/* #define TEST */
drhfd405312005-11-06 04:06:59 +00002906#if 0
drh75897232000-05-29 14:26:00 +00002907/* Print a set */
2908PRIVATE void SetPrint(out,set,lemp)
2909FILE *out;
2910char *set;
2911struct lemon *lemp;
2912{
2913 int i;
2914 char *spacer;
2915 spacer = "";
2916 fprintf(out,"%12s[","");
2917 for(i=0; i<lemp->nterminal; i++){
2918 if( SetFind(set,i) ){
2919 fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
2920 spacer = " ";
2921 }
2922 }
2923 fprintf(out,"]\n");
2924}
2925
2926/* Print a plink chain */
2927PRIVATE void PlinkPrint(out,plp,tag)
2928FILE *out;
2929struct plink *plp;
2930char *tag;
2931{
2932 while( plp ){
drhada354d2005-11-05 15:03:59 +00002933 fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum);
drh75897232000-05-29 14:26:00 +00002934 ConfigPrint(out,plp->cfp);
2935 fprintf(out,"\n");
2936 plp = plp->next;
2937 }
2938}
2939#endif
2940
2941/* Print an action to the given file descriptor. Return FALSE if
2942** nothing was actually printed.
2943*/
2944int PrintAction(struct action *ap, FILE *fp, int indent){
2945 int result = 1;
2946 switch( ap->type ){
2947 case SHIFT:
drhada354d2005-11-05 15:03:59 +00002948 fprintf(fp,"%*s shift %d",indent,ap->sp->name,ap->x.stp->statenum);
drh75897232000-05-29 14:26:00 +00002949 break;
2950 case REDUCE:
2951 fprintf(fp,"%*s reduce %d",indent,ap->sp->name,ap->x.rp->index);
2952 break;
2953 case ACCEPT:
2954 fprintf(fp,"%*s accept",indent,ap->sp->name);
2955 break;
2956 case ERROR:
2957 fprintf(fp,"%*s error",indent,ap->sp->name);
2958 break;
drh9892c5d2007-12-21 00:02:11 +00002959 case SRCONFLICT:
2960 case RRCONFLICT:
drh75897232000-05-29 14:26:00 +00002961 fprintf(fp,"%*s reduce %-3d ** Parsing conflict **",
2962 indent,ap->sp->name,ap->x.rp->index);
2963 break;
drh9892c5d2007-12-21 00:02:11 +00002964 case SSCONFLICT:
drhdd7e9db2010-07-19 01:52:07 +00002965 fprintf(fp,"%*s shift %-3d ** Parsing conflict **",
drh9892c5d2007-12-21 00:02:11 +00002966 indent,ap->sp->name,ap->x.stp->statenum);
2967 break;
drh75897232000-05-29 14:26:00 +00002968 case SH_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00002969 if( showPrecedenceConflict ){
drhdd7e9db2010-07-19 01:52:07 +00002970 fprintf(fp,"%*s shift %-3d -- dropped by precedence",
drhf5c4e0f2010-07-18 11:35:53 +00002971 indent,ap->sp->name,ap->x.stp->statenum);
2972 }else{
2973 result = 0;
2974 }
2975 break;
drh75897232000-05-29 14:26:00 +00002976 case RD_RESOLVED:
drhf5c4e0f2010-07-18 11:35:53 +00002977 if( showPrecedenceConflict ){
drhdd7e9db2010-07-19 01:52:07 +00002978 fprintf(fp,"%*s reduce %-3d -- dropped by precedence",
drhf5c4e0f2010-07-18 11:35:53 +00002979 indent,ap->sp->name,ap->x.rp->index);
2980 }else{
2981 result = 0;
2982 }
2983 break;
drh75897232000-05-29 14:26:00 +00002984 case NOT_USED:
2985 result = 0;
2986 break;
2987 }
2988 return result;
2989}
2990
2991/* Generate the "y.output" log file */
icculus9e44cf12010-02-14 17:14:22 +00002992void ReportOutput(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00002993{
2994 int i;
2995 struct state *stp;
2996 struct config *cfp;
2997 struct action *ap;
2998 FILE *fp;
2999
drh2aa6ca42004-09-10 00:14:04 +00003000 fp = file_open(lemp,".out","wb");
drh75897232000-05-29 14:26:00 +00003001 if( fp==0 ) return;
drh75897232000-05-29 14:26:00 +00003002 for(i=0; i<lemp->nstate; i++){
3003 stp = lemp->sorted[i];
drhada354d2005-11-05 15:03:59 +00003004 fprintf(fp,"State %d:\n",stp->statenum);
drh75897232000-05-29 14:26:00 +00003005 if( lemp->basisflag ) cfp=stp->bp;
3006 else cfp=stp->cfp;
3007 while( cfp ){
3008 char buf[20];
3009 if( cfp->dot==cfp->rp->nrhs ){
drh898799f2014-01-10 23:21:00 +00003010 lemon_sprintf(buf,"(%d)",cfp->rp->index);
drh75897232000-05-29 14:26:00 +00003011 fprintf(fp," %5s ",buf);
3012 }else{
3013 fprintf(fp," ");
3014 }
3015 ConfigPrint(fp,cfp);
3016 fprintf(fp,"\n");
drhfd405312005-11-06 04:06:59 +00003017#if 0
drh75897232000-05-29 14:26:00 +00003018 SetPrint(fp,cfp->fws,lemp);
3019 PlinkPrint(fp,cfp->fplp,"To ");
3020 PlinkPrint(fp,cfp->bplp,"From");
3021#endif
3022 if( lemp->basisflag ) cfp=cfp->bp;
3023 else cfp=cfp->next;
3024 }
3025 fprintf(fp,"\n");
3026 for(ap=stp->ap; ap; ap=ap->next){
3027 if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
3028 }
3029 fprintf(fp,"\n");
3030 }
drhe9278182007-07-18 18:16:29 +00003031 fprintf(fp, "----------------------------------------------------\n");
3032 fprintf(fp, "Symbols:\n");
3033 for(i=0; i<lemp->nsymbol; i++){
3034 int j;
3035 struct symbol *sp;
3036
3037 sp = lemp->symbols[i];
3038 fprintf(fp, " %3d: %s", i, sp->name);
3039 if( sp->type==NONTERMINAL ){
3040 fprintf(fp, ":");
3041 if( sp->lambda ){
3042 fprintf(fp, " <lambda>");
3043 }
3044 for(j=0; j<lemp->nterminal; j++){
3045 if( sp->firstset && SetFind(sp->firstset, j) ){
3046 fprintf(fp, " %s", lemp->symbols[j]->name);
3047 }
3048 }
3049 }
3050 fprintf(fp, "\n");
3051 }
drh75897232000-05-29 14:26:00 +00003052 fclose(fp);
3053 return;
3054}
3055
3056/* Search for the file "name" which is in the same directory as
3057** the exacutable */
icculus9e44cf12010-02-14 17:14:22 +00003058PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
drh75897232000-05-29 14:26:00 +00003059{
icculus9e44cf12010-02-14 17:14:22 +00003060 const char *pathlist;
3061 char *pathbufptr;
3062 char *pathbuf;
drh75897232000-05-29 14:26:00 +00003063 char *path,*cp;
3064 char c;
drh75897232000-05-29 14:26:00 +00003065
3066#ifdef __WIN32__
3067 cp = strrchr(argv0,'\\');
3068#else
3069 cp = strrchr(argv0,'/');
3070#endif
3071 if( cp ){
3072 c = *cp;
3073 *cp = 0;
drh87cf1372008-08-13 20:09:06 +00003074 path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
drh898799f2014-01-10 23:21:00 +00003075 if( path ) lemon_sprintf(path,"%s/%s",argv0,name);
drh75897232000-05-29 14:26:00 +00003076 *cp = c;
3077 }else{
drh75897232000-05-29 14:26:00 +00003078 pathlist = getenv("PATH");
3079 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
icculus9e44cf12010-02-14 17:14:22 +00003080 pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
drh87cf1372008-08-13 20:09:06 +00003081 path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
icculus9e44cf12010-02-14 17:14:22 +00003082 if( (pathbuf != 0) && (path!=0) ){
3083 pathbufptr = pathbuf;
drh898799f2014-01-10 23:21:00 +00003084 lemon_strcpy(pathbuf, pathlist);
icculus9e44cf12010-02-14 17:14:22 +00003085 while( *pathbuf ){
3086 cp = strchr(pathbuf,':');
3087 if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
drh75897232000-05-29 14:26:00 +00003088 c = *cp;
3089 *cp = 0;
drh898799f2014-01-10 23:21:00 +00003090 lemon_sprintf(path,"%s/%s",pathbuf,name);
drh75897232000-05-29 14:26:00 +00003091 *cp = c;
icculus9e44cf12010-02-14 17:14:22 +00003092 if( c==0 ) pathbuf[0] = 0;
3093 else pathbuf = &cp[1];
drh75897232000-05-29 14:26:00 +00003094 if( access(path,modemask)==0 ) break;
3095 }
icculus9e44cf12010-02-14 17:14:22 +00003096 free(pathbufptr);
drh75897232000-05-29 14:26:00 +00003097 }
3098 }
3099 return path;
3100}
3101
3102/* Given an action, compute the integer value for that action
3103** which is to be put in the action table of the generated machine.
3104** Return negative if no action should be generated.
3105*/
icculus9e44cf12010-02-14 17:14:22 +00003106PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
drh75897232000-05-29 14:26:00 +00003107{
3108 int act;
3109 switch( ap->type ){
drhada354d2005-11-05 15:03:59 +00003110 case SHIFT: act = ap->x.stp->statenum; break;
drh75897232000-05-29 14:26:00 +00003111 case REDUCE: act = ap->x.rp->index + lemp->nstate; break;
3112 case ERROR: act = lemp->nstate + lemp->nrule; break;
3113 case ACCEPT: act = lemp->nstate + lemp->nrule + 1; break;
3114 default: act = -1; break;
3115 }
3116 return act;
3117}
3118
3119#define LINESIZE 1000
3120/* The next cluster of routines are for reading the template file
3121** and writing the results to the generated parser */
3122/* The first function transfers data from "in" to "out" until
3123** a line is seen which begins with "%%". The line number is
3124** tracked.
3125**
3126** if name!=0, then any word that begin with "Parse" is changed to
3127** begin with *name instead.
3128*/
icculus9e44cf12010-02-14 17:14:22 +00003129PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
drh75897232000-05-29 14:26:00 +00003130{
3131 int i, iStart;
3132 char line[LINESIZE];
3133 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
3134 (*lineno)++;
3135 iStart = 0;
3136 if( name ){
3137 for(i=0; line[i]; i++){
3138 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
3139 && (i==0 || !isalpha(line[i-1]))
3140 ){
3141 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
3142 fprintf(out,"%s",name);
3143 i += 4;
3144 iStart = i+1;
3145 }
3146 }
3147 }
3148 fprintf(out,"%s",&line[iStart]);
3149 }
3150}
3151
3152/* The next function finds the template file and opens it, returning
3153** a pointer to the opened file. */
icculus9e44cf12010-02-14 17:14:22 +00003154PRIVATE FILE *tplt_open(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003155{
3156 static char templatename[] = "lempar.c";
3157 char buf[1000];
3158 FILE *in;
3159 char *tpltname;
3160 char *cp;
3161
icculus3e143bd2010-02-14 00:48:49 +00003162 /* first, see if user specified a template filename on the command line. */
3163 if (user_templatename != 0) {
3164 if( access(user_templatename,004)==-1 ){
3165 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3166 user_templatename);
3167 lemp->errorcnt++;
3168 return 0;
3169 }
3170 in = fopen(user_templatename,"rb");
3171 if( in==0 ){
3172 fprintf(stderr,"Can't open the template file \"%s\".\n",user_templatename);
3173 lemp->errorcnt++;
3174 return 0;
3175 }
3176 return in;
3177 }
3178
drh75897232000-05-29 14:26:00 +00003179 cp = strrchr(lemp->filename,'.');
3180 if( cp ){
drh898799f2014-01-10 23:21:00 +00003181 lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
drh75897232000-05-29 14:26:00 +00003182 }else{
drh898799f2014-01-10 23:21:00 +00003183 lemon_sprintf(buf,"%s.lt",lemp->filename);
drh75897232000-05-29 14:26:00 +00003184 }
3185 if( access(buf,004)==0 ){
3186 tpltname = buf;
drh960e8c62001-04-03 16:53:21 +00003187 }else if( access(templatename,004)==0 ){
3188 tpltname = templatename;
drh75897232000-05-29 14:26:00 +00003189 }else{
3190 tpltname = pathsearch(lemp->argv0,templatename,0);
3191 }
3192 if( tpltname==0 ){
3193 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3194 templatename);
3195 lemp->errorcnt++;
3196 return 0;
3197 }
drh2aa6ca42004-09-10 00:14:04 +00003198 in = fopen(tpltname,"rb");
drh75897232000-05-29 14:26:00 +00003199 if( in==0 ){
3200 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
3201 lemp->errorcnt++;
3202 return 0;
3203 }
3204 return in;
3205}
3206
drhaf805ca2004-09-07 11:28:25 +00003207/* Print a #line directive line to the output file. */
icculus9e44cf12010-02-14 17:14:22 +00003208PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
drhaf805ca2004-09-07 11:28:25 +00003209{
3210 fprintf(out,"#line %d \"",lineno);
3211 while( *filename ){
3212 if( *filename == '\\' ) putc('\\',out);
3213 putc(*filename,out);
3214 filename++;
3215 }
3216 fprintf(out,"\"\n");
3217}
3218
drh75897232000-05-29 14:26:00 +00003219/* Print a string to the file and keep the linenumber up to date */
icculus9e44cf12010-02-14 17:14:22 +00003220PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
drh75897232000-05-29 14:26:00 +00003221{
3222 if( str==0 ) return;
drh75897232000-05-29 14:26:00 +00003223 while( *str ){
drh75897232000-05-29 14:26:00 +00003224 putc(*str,out);
shane58543932008-12-10 20:10:04 +00003225 if( *str=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003226 str++;
3227 }
drh9db55df2004-09-09 14:01:21 +00003228 if( str[-1]!='\n' ){
3229 putc('\n',out);
3230 (*lineno)++;
3231 }
shane58543932008-12-10 20:10:04 +00003232 if (!lemp->nolinenosflag) {
3233 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3234 }
drh75897232000-05-29 14:26:00 +00003235 return;
3236}
3237
3238/*
3239** The following routine emits code for the destructor for the
3240** symbol sp
3241*/
icculus9e44cf12010-02-14 17:14:22 +00003242void emit_destructor_code(
3243 FILE *out,
3244 struct symbol *sp,
3245 struct lemon *lemp,
3246 int *lineno
3247){
drhcc83b6e2004-04-23 23:38:42 +00003248 char *cp = 0;
drh75897232000-05-29 14:26:00 +00003249
drh75897232000-05-29 14:26:00 +00003250 if( sp->type==TERMINAL ){
3251 cp = lemp->tokendest;
3252 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003253 fprintf(out,"{\n"); (*lineno)++;
drh960e8c62001-04-03 16:53:21 +00003254 }else if( sp->destructor ){
drh75897232000-05-29 14:26:00 +00003255 cp = sp->destructor;
drha5808f32008-04-27 22:19:44 +00003256 fprintf(out,"{\n"); (*lineno)++;
shane58543932008-12-10 20:10:04 +00003257 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,sp->destLineno,lemp->filename); }
drh960e8c62001-04-03 16:53:21 +00003258 }else if( lemp->vardest ){
3259 cp = lemp->vardest;
3260 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003261 fprintf(out,"{\n"); (*lineno)++;
drhcc83b6e2004-04-23 23:38:42 +00003262 }else{
3263 assert( 0 ); /* Cannot happen */
drh75897232000-05-29 14:26:00 +00003264 }
3265 for(; *cp; cp++){
3266 if( *cp=='$' && cp[1]=='$' ){
3267 fprintf(out,"(yypminor->yy%d)",sp->dtnum);
3268 cp++;
3269 continue;
3270 }
shane58543932008-12-10 20:10:04 +00003271 if( *cp=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003272 fputc(*cp,out);
3273 }
shane58543932008-12-10 20:10:04 +00003274 fprintf(out,"\n"); (*lineno)++;
3275 if (!lemp->nolinenosflag) {
3276 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3277 }
3278 fprintf(out,"}\n"); (*lineno)++;
drh75897232000-05-29 14:26:00 +00003279 return;
3280}
3281
3282/*
drh960e8c62001-04-03 16:53:21 +00003283** Return TRUE (non-zero) if the given symbol has a destructor.
drh75897232000-05-29 14:26:00 +00003284*/
icculus9e44cf12010-02-14 17:14:22 +00003285int has_destructor(struct symbol *sp, struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003286{
3287 int ret;
3288 if( sp->type==TERMINAL ){
3289 ret = lemp->tokendest!=0;
3290 }else{
drh960e8c62001-04-03 16:53:21 +00003291 ret = lemp->vardest!=0 || sp->destructor!=0;
drh75897232000-05-29 14:26:00 +00003292 }
3293 return ret;
3294}
3295
drh0bb132b2004-07-20 14:06:51 +00003296/*
3297** Append text to a dynamically allocated string. If zText is 0 then
3298** reset the string to be empty again. Always return the complete text
3299** of the string (which is overwritten with each call).
drh7ac25c72004-08-19 15:12:26 +00003300**
3301** n bytes of zText are stored. If n==0 then all of zText up to the first
3302** \000 terminator is stored. zText can contain up to two instances of
3303** %d. The values of p1 and p2 are written into the first and second
3304** %d.
3305**
3306** If n==-1, then the previous character is overwritten.
drh0bb132b2004-07-20 14:06:51 +00003307*/
icculus9e44cf12010-02-14 17:14:22 +00003308PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
3309 static char empty[1] = { 0 };
drh0bb132b2004-07-20 14:06:51 +00003310 static char *z = 0;
3311 static int alloced = 0;
3312 static int used = 0;
drhaf805ca2004-09-07 11:28:25 +00003313 int c;
drh0bb132b2004-07-20 14:06:51 +00003314 char zInt[40];
drh0bb132b2004-07-20 14:06:51 +00003315 if( zText==0 ){
3316 used = 0;
3317 return z;
3318 }
drh7ac25c72004-08-19 15:12:26 +00003319 if( n<=0 ){
3320 if( n<0 ){
3321 used += n;
3322 assert( used>=0 );
3323 }
drh87cf1372008-08-13 20:09:06 +00003324 n = lemonStrlen(zText);
drh7ac25c72004-08-19 15:12:26 +00003325 }
drhdf609712010-11-23 20:55:27 +00003326 if( (int) (n+sizeof(zInt)*2+used) >= alloced ){
drh0bb132b2004-07-20 14:06:51 +00003327 alloced = n + sizeof(zInt)*2 + used + 200;
icculus9e44cf12010-02-14 17:14:22 +00003328 z = (char *) realloc(z, alloced);
drh0bb132b2004-07-20 14:06:51 +00003329 }
icculus9e44cf12010-02-14 17:14:22 +00003330 if( z==0 ) return empty;
drh0bb132b2004-07-20 14:06:51 +00003331 while( n-- > 0 ){
3332 c = *(zText++);
drh50489622006-10-13 12:25:29 +00003333 if( c=='%' && n>0 && zText[0]=='d' ){
drh898799f2014-01-10 23:21:00 +00003334 lemon_sprintf(zInt, "%d", p1);
drh0bb132b2004-07-20 14:06:51 +00003335 p1 = p2;
drh898799f2014-01-10 23:21:00 +00003336 lemon_strcpy(&z[used], zInt);
drh87cf1372008-08-13 20:09:06 +00003337 used += lemonStrlen(&z[used]);
drh0bb132b2004-07-20 14:06:51 +00003338 zText++;
3339 n--;
3340 }else{
3341 z[used++] = c;
3342 }
3343 }
3344 z[used] = 0;
3345 return z;
3346}
3347
3348/*
3349** zCode is a string that is the action associated with a rule. Expand
3350** the symbols in this string so that the refer to elements of the parser
drhaf805ca2004-09-07 11:28:25 +00003351** stack.
drh0bb132b2004-07-20 14:06:51 +00003352*/
drhaf805ca2004-09-07 11:28:25 +00003353PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){
drh0bb132b2004-07-20 14:06:51 +00003354 char *cp, *xp;
3355 int i;
3356 char lhsused = 0; /* True if the LHS element has been used */
3357 char used[MAXRHS]; /* True for each RHS element which is used */
3358
3359 for(i=0; i<rp->nrhs; i++) used[i] = 0;
3360 lhsused = 0;
3361
drh19c9e562007-03-29 20:13:53 +00003362 if( rp->code==0 ){
icculus9e44cf12010-02-14 17:14:22 +00003363 static char newlinestr[2] = { '\n', '\0' };
3364 rp->code = newlinestr;
drh19c9e562007-03-29 20:13:53 +00003365 rp->line = rp->ruleline;
3366 }
3367
drh0bb132b2004-07-20 14:06:51 +00003368 append_str(0,0,0,0);
icculus9e44cf12010-02-14 17:14:22 +00003369
3370 /* This const cast is wrong but harmless, if we're careful. */
3371 for(cp=(char *)rp->code; *cp; cp++){
drh0bb132b2004-07-20 14:06:51 +00003372 if( isalpha(*cp) && (cp==rp->code || (!isalnum(cp[-1]) && cp[-1]!='_')) ){
3373 char saved;
3374 for(xp= &cp[1]; isalnum(*xp) || *xp=='_'; xp++);
3375 saved = *xp;
3376 *xp = 0;
3377 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
drh7ac25c72004-08-19 15:12:26 +00003378 append_str("yygotominor.yy%d",0,rp->lhs->dtnum,0);
drh0bb132b2004-07-20 14:06:51 +00003379 cp = xp;
3380 lhsused = 1;
3381 }else{
3382 for(i=0; i<rp->nrhs; i++){
3383 if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
drh7ac25c72004-08-19 15:12:26 +00003384 if( cp!=rp->code && cp[-1]=='@' ){
3385 /* If the argument is of the form @X then substituted
3386 ** the token number of X, not the value of X */
3387 append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0);
3388 }else{
drhfd405312005-11-06 04:06:59 +00003389 struct symbol *sp = rp->rhs[i];
3390 int dtnum;
3391 if( sp->type==MULTITERMINAL ){
3392 dtnum = sp->subsym[0]->dtnum;
3393 }else{
3394 dtnum = sp->dtnum;
3395 }
3396 append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum);
drh7ac25c72004-08-19 15:12:26 +00003397 }
drh0bb132b2004-07-20 14:06:51 +00003398 cp = xp;
3399 used[i] = 1;
3400 break;
3401 }
3402 }
3403 }
3404 *xp = saved;
3405 }
3406 append_str(cp, 1, 0, 0);
3407 } /* End loop */
3408
3409 /* Check to make sure the LHS has been used */
3410 if( rp->lhsalias && !lhsused ){
3411 ErrorMsg(lemp->filename,rp->ruleline,
3412 "Label \"%s\" for \"%s(%s)\" is never used.",
3413 rp->lhsalias,rp->lhs->name,rp->lhsalias);
3414 lemp->errorcnt++;
3415 }
3416
3417 /* Generate destructor code for RHS symbols which are not used in the
3418 ** reduce code */
3419 for(i=0; i<rp->nrhs; i++){
3420 if( rp->rhsalias[i] && !used[i] ){
3421 ErrorMsg(lemp->filename,rp->ruleline,
3422 "Label %s for \"%s(%s)\" is never used.",
3423 rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
3424 lemp->errorcnt++;
3425 }else if( rp->rhsalias[i]==0 ){
3426 if( has_destructor(rp->rhs[i],lemp) ){
drh639efd02008-08-13 20:04:29 +00003427 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
drh0bb132b2004-07-20 14:06:51 +00003428 rp->rhs[i]->index,i-rp->nrhs+1);
3429 }else{
3430 /* No destructor defined for this term */
3431 }
3432 }
3433 }
drh61e339a2007-01-16 03:09:02 +00003434 if( rp->code ){
3435 cp = append_str(0,0,0,0);
3436 rp->code = Strsafe(cp?cp:"");
3437 }
drh0bb132b2004-07-20 14:06:51 +00003438}
3439
drh75897232000-05-29 14:26:00 +00003440/*
3441** Generate code which executes when the rule "rp" is reduced. Write
3442** the code to "out". Make sure lineno stays up-to-date.
3443*/
icculus9e44cf12010-02-14 17:14:22 +00003444PRIVATE void emit_code(
3445 FILE *out,
3446 struct rule *rp,
3447 struct lemon *lemp,
3448 int *lineno
3449){
3450 const char *cp;
drh75897232000-05-29 14:26:00 +00003451
3452 /* Generate code to do the reduce action */
3453 if( rp->code ){
shane58543932008-12-10 20:10:04 +00003454 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,rp->line,lemp->filename); }
drhaf805ca2004-09-07 11:28:25 +00003455 fprintf(out,"{%s",rp->code);
drh75897232000-05-29 14:26:00 +00003456 for(cp=rp->code; *cp; cp++){
shane58543932008-12-10 20:10:04 +00003457 if( *cp=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003458 } /* End loop */
shane58543932008-12-10 20:10:04 +00003459 fprintf(out,"}\n"); (*lineno)++;
3460 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); }
drh75897232000-05-29 14:26:00 +00003461 } /* End if( rp->code ) */
3462
drh75897232000-05-29 14:26:00 +00003463 return;
3464}
3465
3466/*
3467** Print the definition of the union used for the parser's data stack.
3468** This union contains fields for every possible data type for tokens
3469** and nonterminals. In the process of computing and printing this
3470** union, also set the ".dtnum" field of every terminal and nonterminal
3471** symbol.
3472*/
icculus9e44cf12010-02-14 17:14:22 +00003473void print_stack_union(
3474 FILE *out, /* The output stream */
3475 struct lemon *lemp, /* The main info structure for this parser */
3476 int *plineno, /* Pointer to the line number */
3477 int mhflag /* True if generating makeheaders output */
3478){
drh75897232000-05-29 14:26:00 +00003479 int lineno = *plineno; /* The line number of the output */
3480 char **types; /* A hash table of datatypes */
3481 int arraysize; /* Size of the "types" array */
3482 int maxdtlength; /* Maximum length of any ".datatype" field. */
3483 char *stddt; /* Standardized name for a datatype */
3484 int i,j; /* Loop counters */
drh01f75f22013-10-02 20:46:30 +00003485 unsigned hash; /* For hashing the name of a type */
icculus9e44cf12010-02-14 17:14:22 +00003486 const char *name; /* Name of the parser */
drh75897232000-05-29 14:26:00 +00003487
3488 /* Allocate and initialize types[] and allocate stddt[] */
3489 arraysize = lemp->nsymbol * 2;
drh9892c5d2007-12-21 00:02:11 +00003490 types = (char**)calloc( arraysize, sizeof(char*) );
drh070d4222011-06-02 15:48:51 +00003491 if( types==0 ){
3492 fprintf(stderr,"Out of memory.\n");
3493 exit(1);
3494 }
drh75897232000-05-29 14:26:00 +00003495 for(i=0; i<arraysize; i++) types[i] = 0;
3496 maxdtlength = 0;
drh960e8c62001-04-03 16:53:21 +00003497 if( lemp->vartype ){
drh87cf1372008-08-13 20:09:06 +00003498 maxdtlength = lemonStrlen(lemp->vartype);
drh960e8c62001-04-03 16:53:21 +00003499 }
drh75897232000-05-29 14:26:00 +00003500 for(i=0; i<lemp->nsymbol; i++){
3501 int len;
3502 struct symbol *sp = lemp->symbols[i];
3503 if( sp->datatype==0 ) continue;
drh87cf1372008-08-13 20:09:06 +00003504 len = lemonStrlen(sp->datatype);
drh75897232000-05-29 14:26:00 +00003505 if( len>maxdtlength ) maxdtlength = len;
3506 }
3507 stddt = (char*)malloc( maxdtlength*2 + 1 );
drh070d4222011-06-02 15:48:51 +00003508 if( stddt==0 ){
drh75897232000-05-29 14:26:00 +00003509 fprintf(stderr,"Out of memory.\n");
3510 exit(1);
3511 }
3512
3513 /* Build a hash table of datatypes. The ".dtnum" field of each symbol
3514 ** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
drh960e8c62001-04-03 16:53:21 +00003515 ** used for terminal symbols. If there is no %default_type defined then
3516 ** 0 is also used as the .dtnum value for nonterminals which do not specify
3517 ** a datatype using the %type directive.
3518 */
drh75897232000-05-29 14:26:00 +00003519 for(i=0; i<lemp->nsymbol; i++){
3520 struct symbol *sp = lemp->symbols[i];
3521 char *cp;
3522 if( sp==lemp->errsym ){
3523 sp->dtnum = arraysize+1;
3524 continue;
3525 }
drh960e8c62001-04-03 16:53:21 +00003526 if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
drh75897232000-05-29 14:26:00 +00003527 sp->dtnum = 0;
3528 continue;
3529 }
3530 cp = sp->datatype;
drh960e8c62001-04-03 16:53:21 +00003531 if( cp==0 ) cp = lemp->vartype;
drh75897232000-05-29 14:26:00 +00003532 j = 0;
3533 while( isspace(*cp) ) cp++;
3534 while( *cp ) stddt[j++] = *cp++;
3535 while( j>0 && isspace(stddt[j-1]) ) j--;
3536 stddt[j] = 0;
drh02368c92009-04-05 15:18:02 +00003537 if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){
drh32c4d742008-07-01 16:34:49 +00003538 sp->dtnum = 0;
3539 continue;
3540 }
drh75897232000-05-29 14:26:00 +00003541 hash = 0;
3542 for(j=0; stddt[j]; j++){
3543 hash = hash*53 + stddt[j];
3544 }
drh3b2129c2003-05-13 00:34:21 +00003545 hash = (hash & 0x7fffffff)%arraysize;
drh75897232000-05-29 14:26:00 +00003546 while( types[hash] ){
3547 if( strcmp(types[hash],stddt)==0 ){
3548 sp->dtnum = hash + 1;
3549 break;
3550 }
3551 hash++;
drh2b51f212013-10-11 23:01:02 +00003552 if( hash>=(unsigned)arraysize ) hash = 0;
drh75897232000-05-29 14:26:00 +00003553 }
3554 if( types[hash]==0 ){
3555 sp->dtnum = hash + 1;
drh87cf1372008-08-13 20:09:06 +00003556 types[hash] = (char*)malloc( lemonStrlen(stddt)+1 );
drh75897232000-05-29 14:26:00 +00003557 if( types[hash]==0 ){
3558 fprintf(stderr,"Out of memory.\n");
3559 exit(1);
3560 }
drh898799f2014-01-10 23:21:00 +00003561 lemon_strcpy(types[hash],stddt);
drh75897232000-05-29 14:26:00 +00003562 }
3563 }
3564
3565 /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
3566 name = lemp->name ? lemp->name : "Parse";
3567 lineno = *plineno;
3568 if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
3569 fprintf(out,"#define %sTOKENTYPE %s\n",name,
3570 lemp->tokentype?lemp->tokentype:"void*"); lineno++;
3571 if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
3572 fprintf(out,"typedef union {\n"); lineno++;
drh15b024c2008-12-11 02:20:43 +00003573 fprintf(out," int yyinit;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003574 fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
3575 for(i=0; i<arraysize; i++){
3576 if( types[i]==0 ) continue;
3577 fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
3578 free(types[i]);
3579 }
drhc4dd3fd2008-01-22 01:48:05 +00003580 if( lemp->errsym->useCnt ){
3581 fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
3582 }
drh75897232000-05-29 14:26:00 +00003583 free(stddt);
3584 free(types);
3585 fprintf(out,"} YYMINORTYPE;\n"); lineno++;
3586 *plineno = lineno;
3587}
3588
drhb29b0a52002-02-23 19:39:46 +00003589/*
3590** Return the name of a C datatype able to represent values between
drh8b582012003-10-21 13:16:03 +00003591** lwr and upr, inclusive.
drhb29b0a52002-02-23 19:39:46 +00003592*/
drh8b582012003-10-21 13:16:03 +00003593static const char *minimum_size_type(int lwr, int upr){
3594 if( lwr>=0 ){
3595 if( upr<=255 ){
3596 return "unsigned char";
3597 }else if( upr<65535 ){
3598 return "unsigned short int";
3599 }else{
3600 return "unsigned int";
3601 }
3602 }else if( lwr>=-127 && upr<=127 ){
3603 return "signed char";
3604 }else if( lwr>=-32767 && upr<32767 ){
3605 return "short";
drhb29b0a52002-02-23 19:39:46 +00003606 }else{
drh8b582012003-10-21 13:16:03 +00003607 return "int";
drhb29b0a52002-02-23 19:39:46 +00003608 }
3609}
3610
drhfdbf9282003-10-21 16:34:41 +00003611/*
3612** Each state contains a set of token transaction and a set of
3613** nonterminal transactions. Each of these sets makes an instance
3614** of the following structure. An array of these structures is used
3615** to order the creation of entries in the yy_action[] table.
3616*/
3617struct axset {
3618 struct state *stp; /* A pointer to a state */
3619 int isTkn; /* True to use tokens. False for non-terminals */
3620 int nAction; /* Number of actions */
drhe594bc32009-11-03 13:02:25 +00003621 int iOrder; /* Original order of action sets */
drhfdbf9282003-10-21 16:34:41 +00003622};
3623
3624/*
3625** Compare to axset structures for sorting purposes
3626*/
3627static int axset_compare(const void *a, const void *b){
3628 struct axset *p1 = (struct axset*)a;
3629 struct axset *p2 = (struct axset*)b;
drhe594bc32009-11-03 13:02:25 +00003630 int c;
3631 c = p2->nAction - p1->nAction;
3632 if( c==0 ){
3633 c = p2->iOrder - p1->iOrder;
3634 }
3635 assert( c!=0 || p1==p2 );
3636 return c;
drhfdbf9282003-10-21 16:34:41 +00003637}
3638
drhc4dd3fd2008-01-22 01:48:05 +00003639/*
3640** Write text on "out" that describes the rule "rp".
3641*/
3642static void writeRuleText(FILE *out, struct rule *rp){
3643 int j;
3644 fprintf(out,"%s ::=", rp->lhs->name);
3645 for(j=0; j<rp->nrhs; j++){
3646 struct symbol *sp = rp->rhs[j];
3647 fprintf(out," %s", sp->name);
3648 if( sp->type==MULTITERMINAL ){
3649 int k;
3650 for(k=1; k<sp->nsubsym; k++){
3651 fprintf(out,"|%s",sp->subsym[k]->name);
3652 }
3653 }
3654 }
3655}
3656
3657
drh75897232000-05-29 14:26:00 +00003658/* Generate C source code for the parser */
icculus9e44cf12010-02-14 17:14:22 +00003659void ReportTable(
3660 struct lemon *lemp,
3661 int mhflag /* Output in makeheaders format if true */
3662){
drh75897232000-05-29 14:26:00 +00003663 FILE *out, *in;
3664 char line[LINESIZE];
3665 int lineno;
3666 struct state *stp;
3667 struct action *ap;
3668 struct rule *rp;
drh8b582012003-10-21 13:16:03 +00003669 struct acttab *pActtab;
icculusa3191192010-02-17 05:40:34 +00003670 int i, j, n;
icculus9e44cf12010-02-14 17:14:22 +00003671 const char *name;
drh8b582012003-10-21 13:16:03 +00003672 int mnTknOfst, mxTknOfst;
3673 int mnNtOfst, mxNtOfst;
drhfdbf9282003-10-21 16:34:41 +00003674 struct axset *ax;
drh75897232000-05-29 14:26:00 +00003675
3676 in = tplt_open(lemp);
3677 if( in==0 ) return;
drh2aa6ca42004-09-10 00:14:04 +00003678 out = file_open(lemp,".c","wb");
drh75897232000-05-29 14:26:00 +00003679 if( out==0 ){
3680 fclose(in);
3681 return;
3682 }
3683 lineno = 1;
3684 tplt_xfer(lemp->name,in,out,&lineno);
3685
3686 /* Generate the include code, if any */
drha5808f32008-04-27 22:19:44 +00003687 tplt_print(out,lemp,lemp->include,&lineno);
drh75897232000-05-29 14:26:00 +00003688 if( mhflag ){
3689 char *name = file_makename(lemp, ".h");
3690 fprintf(out,"#include \"%s\"\n", name); lineno++;
3691 free(name);
3692 }
3693 tplt_xfer(lemp->name,in,out,&lineno);
3694
3695 /* Generate #defines for all tokens */
3696 if( mhflag ){
icculus9e44cf12010-02-14 17:14:22 +00003697 const char *prefix;
drh75897232000-05-29 14:26:00 +00003698 fprintf(out,"#if INTERFACE\n"); lineno++;
3699 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
3700 else prefix = "";
3701 for(i=1; i<lemp->nterminal; i++){
3702 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3703 lineno++;
3704 }
3705 fprintf(out,"#endif\n"); lineno++;
3706 }
3707 tplt_xfer(lemp->name,in,out,&lineno);
3708
3709 /* Generate the defines */
drh75897232000-05-29 14:26:00 +00003710 fprintf(out,"#define YYCODETYPE %s\n",
drh8a415d32009-06-12 13:53:51 +00003711 minimum_size_type(0, lemp->nsymbol+1)); lineno++;
drh75897232000-05-29 14:26:00 +00003712 fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++;
3713 fprintf(out,"#define YYACTIONTYPE %s\n",
drh8b582012003-10-21 13:16:03 +00003714 minimum_size_type(0, lemp->nstate+lemp->nrule+5)); lineno++;
drhe09daa92006-06-10 13:29:31 +00003715 if( lemp->wildcard ){
3716 fprintf(out,"#define YYWILDCARD %d\n",
3717 lemp->wildcard->index); lineno++;
3718 }
drh75897232000-05-29 14:26:00 +00003719 print_stack_union(out,lemp,&lineno,mhflag);
drhca44b5a2007-02-22 23:06:58 +00003720 fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003721 if( lemp->stacksize ){
drh75897232000-05-29 14:26:00 +00003722 fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
3723 }else{
3724 fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
3725 }
drhca44b5a2007-02-22 23:06:58 +00003726 fprintf(out, "#endif\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003727 if( mhflag ){
3728 fprintf(out,"#if INTERFACE\n"); lineno++;
3729 }
3730 name = lemp->name ? lemp->name : "Parse";
3731 if( lemp->arg && lemp->arg[0] ){
3732 int i;
drh87cf1372008-08-13 20:09:06 +00003733 i = lemonStrlen(lemp->arg);
drhb1edd012000-06-02 18:52:12 +00003734 while( i>=1 && isspace(lemp->arg[i-1]) ) i--;
3735 while( i>=1 && (isalnum(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
drh1f245e42002-03-11 13:55:50 +00003736 fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++;
3737 fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++;
3738 fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n",
3739 name,lemp->arg,&lemp->arg[i]); lineno++;
3740 fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n",
3741 name,&lemp->arg[i],&lemp->arg[i]); lineno++;
drh75897232000-05-29 14:26:00 +00003742 }else{
drh1f245e42002-03-11 13:55:50 +00003743 fprintf(out,"#define %sARG_SDECL\n",name); lineno++;
3744 fprintf(out,"#define %sARG_PDECL\n",name); lineno++;
3745 fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
3746 fprintf(out,"#define %sARG_STORE\n",name); lineno++;
drh75897232000-05-29 14:26:00 +00003747 }
3748 if( mhflag ){
3749 fprintf(out,"#endif\n"); lineno++;
3750 }
3751 fprintf(out,"#define YYNSTATE %d\n",lemp->nstate); lineno++;
3752 fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
drhc4dd3fd2008-01-22 01:48:05 +00003753 if( lemp->errsym->useCnt ){
3754 fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
3755 fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
3756 }
drh0bd1f4e2002-06-06 18:54:39 +00003757 if( lemp->has_fallback ){
3758 fprintf(out,"#define YYFALLBACK 1\n"); lineno++;
3759 }
drh75897232000-05-29 14:26:00 +00003760 tplt_xfer(lemp->name,in,out,&lineno);
3761
drh8b582012003-10-21 13:16:03 +00003762 /* Generate the action table and its associates:
drh75897232000-05-29 14:26:00 +00003763 **
drh8b582012003-10-21 13:16:03 +00003764 ** yy_action[] A single table containing all actions.
3765 ** yy_lookahead[] A table containing the lookahead for each entry in
3766 ** yy_action. Used to detect hash collisions.
3767 ** yy_shift_ofst[] For each state, the offset into yy_action for
3768 ** shifting terminals.
3769 ** yy_reduce_ofst[] For each state, the offset into yy_action for
3770 ** shifting non-terminals after a reduce.
3771 ** yy_default[] Default action for each state.
drh75897232000-05-29 14:26:00 +00003772 */
drh75897232000-05-29 14:26:00 +00003773
drh8b582012003-10-21 13:16:03 +00003774 /* Compute the actions on all states and count them up */
icculus9e44cf12010-02-14 17:14:22 +00003775 ax = (struct axset *) calloc(lemp->nstate*2, sizeof(ax[0]));
drhfdbf9282003-10-21 16:34:41 +00003776 if( ax==0 ){
3777 fprintf(stderr,"malloc failed\n");
3778 exit(1);
3779 }
drh75897232000-05-29 14:26:00 +00003780 for(i=0; i<lemp->nstate; i++){
drh75897232000-05-29 14:26:00 +00003781 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00003782 ax[i*2].stp = stp;
3783 ax[i*2].isTkn = 1;
3784 ax[i*2].nAction = stp->nTknAct;
3785 ax[i*2+1].stp = stp;
3786 ax[i*2+1].isTkn = 0;
3787 ax[i*2+1].nAction = stp->nNtAct;
drh75897232000-05-29 14:26:00 +00003788 }
drh8b582012003-10-21 13:16:03 +00003789 mxTknOfst = mnTknOfst = 0;
3790 mxNtOfst = mnNtOfst = 0;
3791
drhfdbf9282003-10-21 16:34:41 +00003792 /* Compute the action table. In order to try to keep the size of the
3793 ** action table to a minimum, the heuristic of placing the largest action
3794 ** sets first is used.
drh8b582012003-10-21 13:16:03 +00003795 */
drhe594bc32009-11-03 13:02:25 +00003796 for(i=0; i<lemp->nstate*2; i++) ax[i].iOrder = i;
drhfdbf9282003-10-21 16:34:41 +00003797 qsort(ax, lemp->nstate*2, sizeof(ax[0]), axset_compare);
drh8b582012003-10-21 13:16:03 +00003798 pActtab = acttab_alloc();
drhfdbf9282003-10-21 16:34:41 +00003799 for(i=0; i<lemp->nstate*2 && ax[i].nAction>0; i++){
3800 stp = ax[i].stp;
3801 if( ax[i].isTkn ){
3802 for(ap=stp->ap; ap; ap=ap->next){
3803 int action;
3804 if( ap->sp->index>=lemp->nterminal ) continue;
3805 action = compute_action(lemp, ap);
3806 if( action<0 ) continue;
3807 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00003808 }
drhfdbf9282003-10-21 16:34:41 +00003809 stp->iTknOfst = acttab_insert(pActtab);
3810 if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
3811 if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
3812 }else{
3813 for(ap=stp->ap; ap; ap=ap->next){
3814 int action;
3815 if( ap->sp->index<lemp->nterminal ) continue;
3816 if( ap->sp->index==lemp->nsymbol ) continue;
3817 action = compute_action(lemp, ap);
3818 if( action<0 ) continue;
3819 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00003820 }
drhfdbf9282003-10-21 16:34:41 +00003821 stp->iNtOfst = acttab_insert(pActtab);
3822 if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
3823 if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
drh8b582012003-10-21 13:16:03 +00003824 }
3825 }
drhfdbf9282003-10-21 16:34:41 +00003826 free(ax);
drh8b582012003-10-21 13:16:03 +00003827
3828 /* Output the yy_action table */
drh8b582012003-10-21 13:16:03 +00003829 n = acttab_size(pActtab);
drhf16371d2009-11-03 19:18:31 +00003830 fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
3831 fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00003832 for(i=j=0; i<n; i++){
3833 int action = acttab_yyaction(pActtab, i);
drhe0479212007-01-12 23:09:23 +00003834 if( action<0 ) action = lemp->nstate + lemp->nrule + 2;
drhfdbf9282003-10-21 16:34:41 +00003835 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003836 fprintf(out, " %4d,", action);
3837 if( j==9 || i==n-1 ){
3838 fprintf(out, "\n"); lineno++;
3839 j = 0;
3840 }else{
3841 j++;
3842 }
3843 }
3844 fprintf(out, "};\n"); lineno++;
3845
3846 /* Output the yy_lookahead table */
drh57196282004-10-06 15:41:16 +00003847 fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00003848 for(i=j=0; i<n; i++){
3849 int la = acttab_yylookahead(pActtab, i);
3850 if( la<0 ) la = lemp->nsymbol;
drhfdbf9282003-10-21 16:34:41 +00003851 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003852 fprintf(out, " %4d,", la);
3853 if( j==9 || i==n-1 ){
3854 fprintf(out, "\n"); lineno++;
3855 j = 0;
3856 }else{
3857 j++;
3858 }
3859 }
3860 fprintf(out, "};\n"); lineno++;
3861
3862 /* Output the yy_shift_ofst[] table */
3863 fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++;
drhada354d2005-11-05 15:03:59 +00003864 n = lemp->nstate;
3865 while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00003866 fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
3867 fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
3868 fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
drh57196282004-10-06 15:41:16 +00003869 fprintf(out, "static const %s yy_shift_ofst[] = {\n",
drh8b582012003-10-21 13:16:03 +00003870 minimum_size_type(mnTknOfst-1, mxTknOfst)); lineno++;
drh8b582012003-10-21 13:16:03 +00003871 for(i=j=0; i<n; i++){
3872 int ofst;
3873 stp = lemp->sorted[i];
3874 ofst = stp->iTknOfst;
3875 if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00003876 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003877 fprintf(out, " %4d,", ofst);
3878 if( j==9 || i==n-1 ){
3879 fprintf(out, "\n"); lineno++;
3880 j = 0;
3881 }else{
3882 j++;
3883 }
3884 }
3885 fprintf(out, "};\n"); lineno++;
3886
3887 /* Output the yy_reduce_ofst[] table */
3888 fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
drhada354d2005-11-05 15:03:59 +00003889 n = lemp->nstate;
3890 while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00003891 fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
3892 fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
3893 fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
drh57196282004-10-06 15:41:16 +00003894 fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
drh8b582012003-10-21 13:16:03 +00003895 minimum_size_type(mnNtOfst-1, mxNtOfst)); lineno++;
drh8b582012003-10-21 13:16:03 +00003896 for(i=j=0; i<n; i++){
3897 int ofst;
3898 stp = lemp->sorted[i];
3899 ofst = stp->iNtOfst;
3900 if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00003901 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003902 fprintf(out, " %4d,", ofst);
3903 if( j==9 || i==n-1 ){
3904 fprintf(out, "\n"); lineno++;
3905 j = 0;
3906 }else{
3907 j++;
3908 }
3909 }
3910 fprintf(out, "};\n"); lineno++;
3911
3912 /* Output the default action table */
drh57196282004-10-06 15:41:16 +00003913 fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00003914 n = lemp->nstate;
3915 for(i=j=0; i<n; i++){
3916 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00003917 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003918 fprintf(out, " %4d,", stp->iDflt);
3919 if( j==9 || i==n-1 ){
3920 fprintf(out, "\n"); lineno++;
3921 j = 0;
3922 }else{
3923 j++;
3924 }
3925 }
3926 fprintf(out, "};\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003927 tplt_xfer(lemp->name,in,out,&lineno);
3928
drh0bd1f4e2002-06-06 18:54:39 +00003929 /* Generate the table of fallback tokens.
3930 */
3931 if( lemp->has_fallback ){
drh1441f3e2009-06-12 12:50:50 +00003932 int mx = lemp->nterminal - 1;
3933 while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; }
3934 for(i=0; i<=mx; i++){
drh0bd1f4e2002-06-06 18:54:39 +00003935 struct symbol *p = lemp->symbols[i];
3936 if( p->fallback==0 ){
3937 fprintf(out, " 0, /* %10s => nothing */\n", p->name);
3938 }else{
3939 fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index,
3940 p->name, p->fallback->name);
3941 }
3942 lineno++;
3943 }
3944 }
3945 tplt_xfer(lemp->name, in, out, &lineno);
3946
3947 /* Generate a table containing the symbolic name of every symbol
3948 */
drh75897232000-05-29 14:26:00 +00003949 for(i=0; i<lemp->nsymbol; i++){
drh898799f2014-01-10 23:21:00 +00003950 lemon_sprintf(line,"\"%s\",",lemp->symbols[i]->name);
drh75897232000-05-29 14:26:00 +00003951 fprintf(out," %-15s",line);
3952 if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
3953 }
3954 if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
3955 tplt_xfer(lemp->name,in,out,&lineno);
3956
drh0bd1f4e2002-06-06 18:54:39 +00003957 /* Generate a table containing a text string that describes every
drh34ff57b2008-07-14 12:27:51 +00003958 ** rule in the rule set of the grammar. This information is used
drh0bd1f4e2002-06-06 18:54:39 +00003959 ** when tracing REDUCE actions.
3960 */
3961 for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
3962 assert( rp->index==i );
drhc4dd3fd2008-01-22 01:48:05 +00003963 fprintf(out," /* %3d */ \"", i);
3964 writeRuleText(out, rp);
drh0bd1f4e2002-06-06 18:54:39 +00003965 fprintf(out,"\",\n"); lineno++;
3966 }
3967 tplt_xfer(lemp->name,in,out,&lineno);
3968
drh75897232000-05-29 14:26:00 +00003969 /* Generate code which executes every time a symbol is popped from
3970 ** the stack while processing errors or while destroying the parser.
drh0bd1f4e2002-06-06 18:54:39 +00003971 ** (In other words, generate the %destructor actions)
3972 */
drh75897232000-05-29 14:26:00 +00003973 if( lemp->tokendest ){
drh4dc8ef52008-07-01 17:13:57 +00003974 int once = 1;
drh75897232000-05-29 14:26:00 +00003975 for(i=0; i<lemp->nsymbol; i++){
3976 struct symbol *sp = lemp->symbols[i];
3977 if( sp==0 || sp->type!=TERMINAL ) continue;
drh4dc8ef52008-07-01 17:13:57 +00003978 if( once ){
3979 fprintf(out, " /* TERMINAL Destructor */\n"); lineno++;
3980 once = 0;
3981 }
drhc53eed12009-06-12 17:46:19 +00003982 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh75897232000-05-29 14:26:00 +00003983 }
3984 for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
3985 if( i<lemp->nsymbol ){
3986 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
3987 fprintf(out," break;\n"); lineno++;
3988 }
3989 }
drh8d659732005-01-13 23:54:06 +00003990 if( lemp->vardest ){
3991 struct symbol *dflt_sp = 0;
drh4dc8ef52008-07-01 17:13:57 +00003992 int once = 1;
drh8d659732005-01-13 23:54:06 +00003993 for(i=0; i<lemp->nsymbol; i++){
3994 struct symbol *sp = lemp->symbols[i];
3995 if( sp==0 || sp->type==TERMINAL ||
3996 sp->index<=0 || sp->destructor!=0 ) continue;
drh4dc8ef52008-07-01 17:13:57 +00003997 if( once ){
3998 fprintf(out, " /* Default NON-TERMINAL Destructor */\n"); lineno++;
3999 once = 0;
4000 }
drhc53eed12009-06-12 17:46:19 +00004001 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh8d659732005-01-13 23:54:06 +00004002 dflt_sp = sp;
4003 }
4004 if( dflt_sp!=0 ){
4005 emit_destructor_code(out,dflt_sp,lemp,&lineno);
drh8d659732005-01-13 23:54:06 +00004006 }
drh4dc8ef52008-07-01 17:13:57 +00004007 fprintf(out," break;\n"); lineno++;
drh8d659732005-01-13 23:54:06 +00004008 }
drh75897232000-05-29 14:26:00 +00004009 for(i=0; i<lemp->nsymbol; i++){
4010 struct symbol *sp = lemp->symbols[i];
4011 if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
drh75013012009-06-12 15:47:34 +00004012 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004013
4014 /* Combine duplicate destructors into a single case */
4015 for(j=i+1; j<lemp->nsymbol; j++){
4016 struct symbol *sp2 = lemp->symbols[j];
4017 if( sp2 && sp2->type!=TERMINAL && sp2->destructor
4018 && sp2->dtnum==sp->dtnum
4019 && strcmp(sp->destructor,sp2->destructor)==0 ){
drhc53eed12009-06-12 17:46:19 +00004020 fprintf(out," case %d: /* %s */\n",
4021 sp2->index, sp2->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004022 sp2->destructor = 0;
4023 }
4024 }
4025
drh75897232000-05-29 14:26:00 +00004026 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
4027 fprintf(out," break;\n"); lineno++;
4028 }
drh75897232000-05-29 14:26:00 +00004029 tplt_xfer(lemp->name,in,out,&lineno);
4030
4031 /* Generate code which executes whenever the parser stack overflows */
drha5808f32008-04-27 22:19:44 +00004032 tplt_print(out,lemp,lemp->overflow,&lineno);
drh75897232000-05-29 14:26:00 +00004033 tplt_xfer(lemp->name,in,out,&lineno);
4034
4035 /* Generate the table of rule information
4036 **
4037 ** Note: This code depends on the fact that rules are number
4038 ** sequentually beginning with 0.
4039 */
4040 for(rp=lemp->rule; rp; rp=rp->next){
4041 fprintf(out," { %d, %d },\n",rp->lhs->index,rp->nrhs); lineno++;
4042 }
4043 tplt_xfer(lemp->name,in,out,&lineno);
4044
4045 /* Generate code which execution during each REDUCE action */
4046 for(rp=lemp->rule; rp; rp=rp->next){
drh61e339a2007-01-16 03:09:02 +00004047 translate_code(lemp, rp);
drh0bb132b2004-07-20 14:06:51 +00004048 }
drhc53eed12009-06-12 17:46:19 +00004049 /* First output rules other than the default: rule */
drh0bb132b2004-07-20 14:06:51 +00004050 for(rp=lemp->rule; rp; rp=rp->next){
drhc53eed12009-06-12 17:46:19 +00004051 struct rule *rp2; /* Other rules with the same action */
drh0bb132b2004-07-20 14:06:51 +00004052 if( rp->code==0 ) continue;
drhc53eed12009-06-12 17:46:19 +00004053 if( rp->code[0]=='\n' && rp->code[1]==0 ) continue; /* Will be default: */
drhc4dd3fd2008-01-22 01:48:05 +00004054 fprintf(out," case %d: /* ", rp->index);
4055 writeRuleText(out, rp);
4056 fprintf(out, " */\n"); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004057 for(rp2=rp->next; rp2; rp2=rp2->next){
4058 if( rp2->code==rp->code ){
drhc4dd3fd2008-01-22 01:48:05 +00004059 fprintf(out," case %d: /* ", rp2->index);
4060 writeRuleText(out, rp2);
drh8a415d32009-06-12 13:53:51 +00004061 fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->index); lineno++;
drh0bb132b2004-07-20 14:06:51 +00004062 rp2->code = 0;
4063 }
4064 }
drh75897232000-05-29 14:26:00 +00004065 emit_code(out,rp,lemp,&lineno);
4066 fprintf(out," break;\n"); lineno++;
drhc53eed12009-06-12 17:46:19 +00004067 rp->code = 0;
drh75897232000-05-29 14:26:00 +00004068 }
drhc53eed12009-06-12 17:46:19 +00004069 /* Finally, output the default: rule. We choose as the default: all
4070 ** empty actions. */
4071 fprintf(out," default:\n"); lineno++;
4072 for(rp=lemp->rule; rp; rp=rp->next){
4073 if( rp->code==0 ) continue;
4074 assert( rp->code[0]=='\n' && rp->code[1]==0 );
4075 fprintf(out," /* (%d) ", rp->index);
4076 writeRuleText(out, rp);
4077 fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->index); lineno++;
4078 }
4079 fprintf(out," break;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00004080 tplt_xfer(lemp->name,in,out,&lineno);
4081
4082 /* Generate code which executes if a parse fails */
drha5808f32008-04-27 22:19:44 +00004083 tplt_print(out,lemp,lemp->failure,&lineno);
drh75897232000-05-29 14:26:00 +00004084 tplt_xfer(lemp->name,in,out,&lineno);
4085
4086 /* Generate code which executes when a syntax error occurs */
drha5808f32008-04-27 22:19:44 +00004087 tplt_print(out,lemp,lemp->error,&lineno);
drh75897232000-05-29 14:26:00 +00004088 tplt_xfer(lemp->name,in,out,&lineno);
4089
4090 /* Generate code which executes when the parser accepts its input */
drha5808f32008-04-27 22:19:44 +00004091 tplt_print(out,lemp,lemp->accept,&lineno);
drh75897232000-05-29 14:26:00 +00004092 tplt_xfer(lemp->name,in,out,&lineno);
4093
4094 /* Append any addition code the user desires */
drha5808f32008-04-27 22:19:44 +00004095 tplt_print(out,lemp,lemp->extracode,&lineno);
drh75897232000-05-29 14:26:00 +00004096
4097 fclose(in);
4098 fclose(out);
4099 return;
4100}
4101
4102/* Generate a header file for the parser */
icculus9e44cf12010-02-14 17:14:22 +00004103void ReportHeader(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004104{
4105 FILE *out, *in;
icculus9e44cf12010-02-14 17:14:22 +00004106 const char *prefix;
drh75897232000-05-29 14:26:00 +00004107 char line[LINESIZE];
4108 char pattern[LINESIZE];
4109 int i;
4110
4111 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4112 else prefix = "";
drh2aa6ca42004-09-10 00:14:04 +00004113 in = file_open(lemp,".h","rb");
drh75897232000-05-29 14:26:00 +00004114 if( in ){
drh8ba0d1c2012-06-16 15:26:31 +00004115 int nextChar;
drh75897232000-05-29 14:26:00 +00004116 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
drh898799f2014-01-10 23:21:00 +00004117 lemon_sprintf(pattern,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
drh75897232000-05-29 14:26:00 +00004118 if( strcmp(line,pattern) ) break;
4119 }
drh8ba0d1c2012-06-16 15:26:31 +00004120 nextChar = fgetc(in);
drh75897232000-05-29 14:26:00 +00004121 fclose(in);
drh8ba0d1c2012-06-16 15:26:31 +00004122 if( i==lemp->nterminal && nextChar==EOF ){
drh75897232000-05-29 14:26:00 +00004123 /* No change in the file. Don't rewrite it. */
4124 return;
4125 }
4126 }
drh2aa6ca42004-09-10 00:14:04 +00004127 out = file_open(lemp,".h","wb");
drh75897232000-05-29 14:26:00 +00004128 if( out ){
4129 for(i=1; i<lemp->nterminal; i++){
4130 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
4131 }
4132 fclose(out);
4133 }
4134 return;
4135}
4136
4137/* Reduce the size of the action tables, if possible, by making use
4138** of defaults.
4139**
drhb59499c2002-02-23 18:45:13 +00004140** In this version, we take the most frequent REDUCE action and make
drhe09daa92006-06-10 13:29:31 +00004141** it the default. Except, there is no default if the wildcard token
4142** is a possible look-ahead.
drh75897232000-05-29 14:26:00 +00004143*/
icculus9e44cf12010-02-14 17:14:22 +00004144void CompressTables(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004145{
4146 struct state *stp;
drhb59499c2002-02-23 18:45:13 +00004147 struct action *ap, *ap2;
4148 struct rule *rp, *rp2, *rbest;
4149 int nbest, n;
drh75897232000-05-29 14:26:00 +00004150 int i;
drhe09daa92006-06-10 13:29:31 +00004151 int usesWildcard;
drh75897232000-05-29 14:26:00 +00004152
4153 for(i=0; i<lemp->nstate; i++){
4154 stp = lemp->sorted[i];
drhb59499c2002-02-23 18:45:13 +00004155 nbest = 0;
4156 rbest = 0;
drhe09daa92006-06-10 13:29:31 +00004157 usesWildcard = 0;
drh75897232000-05-29 14:26:00 +00004158
drhb59499c2002-02-23 18:45:13 +00004159 for(ap=stp->ap; ap; ap=ap->next){
drhe09daa92006-06-10 13:29:31 +00004160 if( ap->type==SHIFT && ap->sp==lemp->wildcard ){
4161 usesWildcard = 1;
4162 }
drhb59499c2002-02-23 18:45:13 +00004163 if( ap->type!=REDUCE ) continue;
4164 rp = ap->x.rp;
drhb4960992007-10-05 16:16:36 +00004165 if( rp->lhsStart ) continue;
drhb59499c2002-02-23 18:45:13 +00004166 if( rp==rbest ) continue;
4167 n = 1;
4168 for(ap2=ap->next; ap2; ap2=ap2->next){
4169 if( ap2->type!=REDUCE ) continue;
4170 rp2 = ap2->x.rp;
4171 if( rp2==rbest ) continue;
4172 if( rp2==rp ) n++;
4173 }
4174 if( n>nbest ){
4175 nbest = n;
4176 rbest = rp;
drh75897232000-05-29 14:26:00 +00004177 }
4178 }
drhb59499c2002-02-23 18:45:13 +00004179
4180 /* Do not make a default if the number of rules to default
drhe09daa92006-06-10 13:29:31 +00004181 ** is not at least 1 or if the wildcard token is a possible
4182 ** lookahead.
4183 */
4184 if( nbest<1 || usesWildcard ) continue;
drh75897232000-05-29 14:26:00 +00004185
drhb59499c2002-02-23 18:45:13 +00004186
4187 /* Combine matching REDUCE actions into a single default */
4188 for(ap=stp->ap; ap; ap=ap->next){
4189 if( ap->type==REDUCE && ap->x.rp==rbest ) break;
4190 }
drh75897232000-05-29 14:26:00 +00004191 assert( ap );
4192 ap->sp = Symbol_new("{default}");
4193 for(ap=ap->next; ap; ap=ap->next){
drhb59499c2002-02-23 18:45:13 +00004194 if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
drh75897232000-05-29 14:26:00 +00004195 }
4196 stp->ap = Action_sort(stp->ap);
4197 }
4198}
drhb59499c2002-02-23 18:45:13 +00004199
drhada354d2005-11-05 15:03:59 +00004200
4201/*
4202** Compare two states for sorting purposes. The smaller state is the
4203** one with the most non-terminal actions. If they have the same number
4204** of non-terminal actions, then the smaller is the one with the most
4205** token actions.
4206*/
4207static int stateResortCompare(const void *a, const void *b){
4208 const struct state *pA = *(const struct state**)a;
4209 const struct state *pB = *(const struct state**)b;
4210 int n;
4211
4212 n = pB->nNtAct - pA->nNtAct;
4213 if( n==0 ){
4214 n = pB->nTknAct - pA->nTknAct;
drhe594bc32009-11-03 13:02:25 +00004215 if( n==0 ){
4216 n = pB->statenum - pA->statenum;
4217 }
drhada354d2005-11-05 15:03:59 +00004218 }
drhe594bc32009-11-03 13:02:25 +00004219 assert( n!=0 );
drhada354d2005-11-05 15:03:59 +00004220 return n;
4221}
4222
4223
4224/*
4225** Renumber and resort states so that states with fewer choices
4226** occur at the end. Except, keep state 0 as the first state.
4227*/
icculus9e44cf12010-02-14 17:14:22 +00004228void ResortStates(struct lemon *lemp)
drhada354d2005-11-05 15:03:59 +00004229{
4230 int i;
4231 struct state *stp;
4232 struct action *ap;
4233
4234 for(i=0; i<lemp->nstate; i++){
4235 stp = lemp->sorted[i];
4236 stp->nTknAct = stp->nNtAct = 0;
4237 stp->iDflt = lemp->nstate + lemp->nrule;
4238 stp->iTknOfst = NO_OFFSET;
4239 stp->iNtOfst = NO_OFFSET;
4240 for(ap=stp->ap; ap; ap=ap->next){
4241 if( compute_action(lemp,ap)>=0 ){
4242 if( ap->sp->index<lemp->nterminal ){
4243 stp->nTknAct++;
4244 }else if( ap->sp->index<lemp->nsymbol ){
4245 stp->nNtAct++;
4246 }else{
4247 stp->iDflt = compute_action(lemp, ap);
4248 }
4249 }
4250 }
4251 }
4252 qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
4253 stateResortCompare);
4254 for(i=0; i<lemp->nstate; i++){
4255 lemp->sorted[i]->statenum = i;
4256 }
4257}
4258
4259
drh75897232000-05-29 14:26:00 +00004260/***************** From the file "set.c" ************************************/
4261/*
4262** Set manipulation routines for the LEMON parser generator.
4263*/
4264
4265static int size = 0;
4266
4267/* Set the set size */
icculus9e44cf12010-02-14 17:14:22 +00004268void SetSize(int n)
drh75897232000-05-29 14:26:00 +00004269{
4270 size = n+1;
4271}
4272
4273/* Allocate a new set */
4274char *SetNew(){
4275 char *s;
drh9892c5d2007-12-21 00:02:11 +00004276 s = (char*)calloc( size, 1);
drh75897232000-05-29 14:26:00 +00004277 if( s==0 ){
4278 extern void memory_error();
4279 memory_error();
4280 }
drh75897232000-05-29 14:26:00 +00004281 return s;
4282}
4283
4284/* Deallocate a set */
icculus9e44cf12010-02-14 17:14:22 +00004285void SetFree(char *s)
drh75897232000-05-29 14:26:00 +00004286{
4287 free(s);
4288}
4289
4290/* Add a new element to the set. Return TRUE if the element was added
4291** and FALSE if it was already there. */
icculus9e44cf12010-02-14 17:14:22 +00004292int SetAdd(char *s, int e)
drh75897232000-05-29 14:26:00 +00004293{
4294 int rv;
drh9892c5d2007-12-21 00:02:11 +00004295 assert( e>=0 && e<size );
drh75897232000-05-29 14:26:00 +00004296 rv = s[e];
4297 s[e] = 1;
4298 return !rv;
4299}
4300
4301/* Add every element of s2 to s1. Return TRUE if s1 changes. */
icculus9e44cf12010-02-14 17:14:22 +00004302int SetUnion(char *s1, char *s2)
drh75897232000-05-29 14:26:00 +00004303{
4304 int i, progress;
4305 progress = 0;
4306 for(i=0; i<size; i++){
4307 if( s2[i]==0 ) continue;
4308 if( s1[i]==0 ){
4309 progress = 1;
4310 s1[i] = 1;
4311 }
4312 }
4313 return progress;
4314}
4315/********************** From the file "table.c" ****************************/
4316/*
4317** All code in this file has been automatically generated
4318** from a specification in the file
4319** "table.q"
4320** by the associative array code building program "aagen".
4321** Do not edit this file! Instead, edit the specification
4322** file, then rerun aagen.
4323*/
4324/*
4325** Code for processing tables in the LEMON parser generator.
4326*/
4327
drh01f75f22013-10-02 20:46:30 +00004328PRIVATE unsigned strhash(const char *x)
drh75897232000-05-29 14:26:00 +00004329{
drh01f75f22013-10-02 20:46:30 +00004330 unsigned h = 0;
4331 while( *x ) h = h*13 + *(x++);
drh75897232000-05-29 14:26:00 +00004332 return h;
4333}
4334
4335/* Works like strdup, sort of. Save a string in malloced memory, but
4336** keep strings in a table so that the same string is not in more
4337** than one place.
4338*/
icculus9e44cf12010-02-14 17:14:22 +00004339const char *Strsafe(const char *y)
drh75897232000-05-29 14:26:00 +00004340{
icculus9e44cf12010-02-14 17:14:22 +00004341 const char *z;
4342 char *cpy;
drh75897232000-05-29 14:26:00 +00004343
drh916f75f2006-07-17 00:19:39 +00004344 if( y==0 ) return 0;
drh75897232000-05-29 14:26:00 +00004345 z = Strsafe_find(y);
icculus9e44cf12010-02-14 17:14:22 +00004346 if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
drh898799f2014-01-10 23:21:00 +00004347 lemon_strcpy(cpy,y);
icculus9e44cf12010-02-14 17:14:22 +00004348 z = cpy;
drh75897232000-05-29 14:26:00 +00004349 Strsafe_insert(z);
4350 }
4351 MemoryCheck(z);
4352 return z;
4353}
4354
4355/* There is one instance of the following structure for each
4356** associative array of type "x1".
4357*/
4358struct s_x1 {
4359 int size; /* The number of available slots. */
4360 /* Must be a power of 2 greater than or */
4361 /* equal to 1 */
4362 int count; /* Number of currently slots filled */
4363 struct s_x1node *tbl; /* The data stored here */
4364 struct s_x1node **ht; /* Hash table for lookups */
4365};
4366
4367/* There is one instance of this structure for every data element
4368** in an associative array of type "x1".
4369*/
4370typedef struct s_x1node {
icculus9e44cf12010-02-14 17:14:22 +00004371 const char *data; /* The data */
drh75897232000-05-29 14:26:00 +00004372 struct s_x1node *next; /* Next entry with the same hash */
4373 struct s_x1node **from; /* Previous link */
4374} x1node;
4375
4376/* There is only one instance of the array, which is the following */
4377static struct s_x1 *x1a;
4378
4379/* Allocate a new associative array */
4380void Strsafe_init(){
4381 if( x1a ) return;
4382 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
4383 if( x1a ){
4384 x1a->size = 1024;
4385 x1a->count = 0;
4386 x1a->tbl = (x1node*)malloc(
4387 (sizeof(x1node) + sizeof(x1node*))*1024 );
4388 if( x1a->tbl==0 ){
4389 free(x1a);
4390 x1a = 0;
4391 }else{
4392 int i;
4393 x1a->ht = (x1node**)&(x1a->tbl[1024]);
4394 for(i=0; i<1024; i++) x1a->ht[i] = 0;
4395 }
4396 }
4397}
4398/* Insert a new record into the array. Return TRUE if successful.
4399** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004400int Strsafe_insert(const char *data)
drh75897232000-05-29 14:26:00 +00004401{
4402 x1node *np;
drh01f75f22013-10-02 20:46:30 +00004403 unsigned h;
4404 unsigned ph;
drh75897232000-05-29 14:26:00 +00004405
4406 if( x1a==0 ) return 0;
4407 ph = strhash(data);
4408 h = ph & (x1a->size-1);
4409 np = x1a->ht[h];
4410 while( np ){
4411 if( strcmp(np->data,data)==0 ){
4412 /* An existing entry with the same key is found. */
4413 /* Fail because overwrite is not allows. */
4414 return 0;
4415 }
4416 np = np->next;
4417 }
4418 if( x1a->count>=x1a->size ){
4419 /* Need to make the hash table bigger */
4420 int i,size;
4421 struct s_x1 array;
4422 array.size = size = x1a->size*2;
4423 array.count = x1a->count;
4424 array.tbl = (x1node*)malloc(
4425 (sizeof(x1node) + sizeof(x1node*))*size );
4426 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4427 array.ht = (x1node**)&(array.tbl[size]);
4428 for(i=0; i<size; i++) array.ht[i] = 0;
4429 for(i=0; i<x1a->count; i++){
4430 x1node *oldnp, *newnp;
4431 oldnp = &(x1a->tbl[i]);
4432 h = strhash(oldnp->data) & (size-1);
4433 newnp = &(array.tbl[i]);
4434 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4435 newnp->next = array.ht[h];
4436 newnp->data = oldnp->data;
4437 newnp->from = &(array.ht[h]);
4438 array.ht[h] = newnp;
4439 }
4440 free(x1a->tbl);
4441 *x1a = array;
4442 }
4443 /* Insert the new data */
4444 h = ph & (x1a->size-1);
4445 np = &(x1a->tbl[x1a->count++]);
4446 np->data = data;
4447 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
4448 np->next = x1a->ht[h];
4449 x1a->ht[h] = np;
4450 np->from = &(x1a->ht[h]);
4451 return 1;
4452}
4453
4454/* Return a pointer to data assigned to the given key. Return NULL
4455** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004456const char *Strsafe_find(const char *key)
drh75897232000-05-29 14:26:00 +00004457{
drh01f75f22013-10-02 20:46:30 +00004458 unsigned h;
drh75897232000-05-29 14:26:00 +00004459 x1node *np;
4460
4461 if( x1a==0 ) return 0;
4462 h = strhash(key) & (x1a->size-1);
4463 np = x1a->ht[h];
4464 while( np ){
4465 if( strcmp(np->data,key)==0 ) break;
4466 np = np->next;
4467 }
4468 return np ? np->data : 0;
4469}
4470
4471/* Return a pointer to the (terminal or nonterminal) symbol "x".
4472** Create a new symbol if this is the first time "x" has been seen.
4473*/
icculus9e44cf12010-02-14 17:14:22 +00004474struct symbol *Symbol_new(const char *x)
drh75897232000-05-29 14:26:00 +00004475{
4476 struct symbol *sp;
4477
4478 sp = Symbol_find(x);
4479 if( sp==0 ){
drh9892c5d2007-12-21 00:02:11 +00004480 sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
drh75897232000-05-29 14:26:00 +00004481 MemoryCheck(sp);
4482 sp->name = Strsafe(x);
4483 sp->type = isupper(*x) ? TERMINAL : NONTERMINAL;
4484 sp->rule = 0;
drh0bd1f4e2002-06-06 18:54:39 +00004485 sp->fallback = 0;
drh75897232000-05-29 14:26:00 +00004486 sp->prec = -1;
4487 sp->assoc = UNK;
4488 sp->firstset = 0;
drhaa9f1122007-08-23 02:50:56 +00004489 sp->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00004490 sp->destructor = 0;
drh4dc8ef52008-07-01 17:13:57 +00004491 sp->destLineno = 0;
drh75897232000-05-29 14:26:00 +00004492 sp->datatype = 0;
drhc4dd3fd2008-01-22 01:48:05 +00004493 sp->useCnt = 0;
drh75897232000-05-29 14:26:00 +00004494 Symbol_insert(sp,sp->name);
4495 }
drhc4dd3fd2008-01-22 01:48:05 +00004496 sp->useCnt++;
drh75897232000-05-29 14:26:00 +00004497 return sp;
4498}
4499
drh60d31652004-02-22 00:08:04 +00004500/* Compare two symbols for working purposes
4501**
4502** Symbols that begin with upper case letters (terminals or tokens)
4503** must sort before symbols that begin with lower case letters
4504** (non-terminals). Other than that, the order does not matter.
4505**
4506** We find experimentally that leaving the symbols in their original
4507** order (the order they appeared in the grammar file) gives the
4508** smallest parser tables in SQLite.
4509*/
icculus9e44cf12010-02-14 17:14:22 +00004510int Symbolcmpp(const void *_a, const void *_b)
4511{
4512 const struct symbol **a = (const struct symbol **) _a;
4513 const struct symbol **b = (const struct symbol **) _b;
drh60d31652004-02-22 00:08:04 +00004514 int i1 = (**a).index + 10000000*((**a).name[0]>'Z');
4515 int i2 = (**b).index + 10000000*((**b).name[0]>'Z');
drhe594bc32009-11-03 13:02:25 +00004516 assert( i1!=i2 || strcmp((**a).name,(**b).name)==0 );
drh60d31652004-02-22 00:08:04 +00004517 return i1-i2;
drh75897232000-05-29 14:26:00 +00004518}
4519
4520/* There is one instance of the following structure for each
4521** associative array of type "x2".
4522*/
4523struct s_x2 {
4524 int size; /* The number of available slots. */
4525 /* Must be a power of 2 greater than or */
4526 /* equal to 1 */
4527 int count; /* Number of currently slots filled */
4528 struct s_x2node *tbl; /* The data stored here */
4529 struct s_x2node **ht; /* Hash table for lookups */
4530};
4531
4532/* There is one instance of this structure for every data element
4533** in an associative array of type "x2".
4534*/
4535typedef struct s_x2node {
icculus9e44cf12010-02-14 17:14:22 +00004536 struct symbol *data; /* The data */
4537 const char *key; /* The key */
drh75897232000-05-29 14:26:00 +00004538 struct s_x2node *next; /* Next entry with the same hash */
4539 struct s_x2node **from; /* Previous link */
4540} x2node;
4541
4542/* There is only one instance of the array, which is the following */
4543static struct s_x2 *x2a;
4544
4545/* Allocate a new associative array */
4546void Symbol_init(){
4547 if( x2a ) return;
4548 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
4549 if( x2a ){
4550 x2a->size = 128;
4551 x2a->count = 0;
4552 x2a->tbl = (x2node*)malloc(
4553 (sizeof(x2node) + sizeof(x2node*))*128 );
4554 if( x2a->tbl==0 ){
4555 free(x2a);
4556 x2a = 0;
4557 }else{
4558 int i;
4559 x2a->ht = (x2node**)&(x2a->tbl[128]);
4560 for(i=0; i<128; i++) x2a->ht[i] = 0;
4561 }
4562 }
4563}
4564/* Insert a new record into the array. Return TRUE if successful.
4565** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004566int Symbol_insert(struct symbol *data, const char *key)
drh75897232000-05-29 14:26:00 +00004567{
4568 x2node *np;
drh01f75f22013-10-02 20:46:30 +00004569 unsigned h;
4570 unsigned ph;
drh75897232000-05-29 14:26:00 +00004571
4572 if( x2a==0 ) return 0;
4573 ph = strhash(key);
4574 h = ph & (x2a->size-1);
4575 np = x2a->ht[h];
4576 while( np ){
4577 if( strcmp(np->key,key)==0 ){
4578 /* An existing entry with the same key is found. */
4579 /* Fail because overwrite is not allows. */
4580 return 0;
4581 }
4582 np = np->next;
4583 }
4584 if( x2a->count>=x2a->size ){
4585 /* Need to make the hash table bigger */
4586 int i,size;
4587 struct s_x2 array;
4588 array.size = size = x2a->size*2;
4589 array.count = x2a->count;
4590 array.tbl = (x2node*)malloc(
4591 (sizeof(x2node) + sizeof(x2node*))*size );
4592 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4593 array.ht = (x2node**)&(array.tbl[size]);
4594 for(i=0; i<size; i++) array.ht[i] = 0;
4595 for(i=0; i<x2a->count; i++){
4596 x2node *oldnp, *newnp;
4597 oldnp = &(x2a->tbl[i]);
4598 h = strhash(oldnp->key) & (size-1);
4599 newnp = &(array.tbl[i]);
4600 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4601 newnp->next = array.ht[h];
4602 newnp->key = oldnp->key;
4603 newnp->data = oldnp->data;
4604 newnp->from = &(array.ht[h]);
4605 array.ht[h] = newnp;
4606 }
4607 free(x2a->tbl);
4608 *x2a = array;
4609 }
4610 /* Insert the new data */
4611 h = ph & (x2a->size-1);
4612 np = &(x2a->tbl[x2a->count++]);
4613 np->key = key;
4614 np->data = data;
4615 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
4616 np->next = x2a->ht[h];
4617 x2a->ht[h] = np;
4618 np->from = &(x2a->ht[h]);
4619 return 1;
4620}
4621
4622/* Return a pointer to data assigned to the given key. Return NULL
4623** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004624struct symbol *Symbol_find(const char *key)
drh75897232000-05-29 14:26:00 +00004625{
drh01f75f22013-10-02 20:46:30 +00004626 unsigned h;
drh75897232000-05-29 14:26:00 +00004627 x2node *np;
4628
4629 if( x2a==0 ) return 0;
4630 h = strhash(key) & (x2a->size-1);
4631 np = x2a->ht[h];
4632 while( np ){
4633 if( strcmp(np->key,key)==0 ) break;
4634 np = np->next;
4635 }
4636 return np ? np->data : 0;
4637}
4638
4639/* Return the n-th data. Return NULL if n is out of range. */
icculus9e44cf12010-02-14 17:14:22 +00004640struct symbol *Symbol_Nth(int n)
drh75897232000-05-29 14:26:00 +00004641{
4642 struct symbol *data;
4643 if( x2a && n>0 && n<=x2a->count ){
4644 data = x2a->tbl[n-1].data;
4645 }else{
4646 data = 0;
4647 }
4648 return data;
4649}
4650
4651/* Return the size of the array */
4652int Symbol_count()
4653{
4654 return x2a ? x2a->count : 0;
4655}
4656
4657/* Return an array of pointers to all data in the table.
4658** The array is obtained from malloc. Return NULL if memory allocation
4659** problems, or if the array is empty. */
4660struct symbol **Symbol_arrayof()
4661{
4662 struct symbol **array;
4663 int i,size;
4664 if( x2a==0 ) return 0;
4665 size = x2a->count;
drh9892c5d2007-12-21 00:02:11 +00004666 array = (struct symbol **)calloc(size, sizeof(struct symbol *));
drh75897232000-05-29 14:26:00 +00004667 if( array ){
4668 for(i=0; i<size; i++) array[i] = x2a->tbl[i].data;
4669 }
4670 return array;
4671}
4672
4673/* Compare two configurations */
icculus9e44cf12010-02-14 17:14:22 +00004674int Configcmp(const char *_a,const char *_b)
drh75897232000-05-29 14:26:00 +00004675{
icculus9e44cf12010-02-14 17:14:22 +00004676 const struct config *a = (struct config *) _a;
4677 const struct config *b = (struct config *) _b;
drh75897232000-05-29 14:26:00 +00004678 int x;
4679 x = a->rp->index - b->rp->index;
4680 if( x==0 ) x = a->dot - b->dot;
4681 return x;
4682}
4683
4684/* Compare two states */
icculus9e44cf12010-02-14 17:14:22 +00004685PRIVATE int statecmp(struct config *a, struct config *b)
drh75897232000-05-29 14:26:00 +00004686{
4687 int rc;
4688 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
4689 rc = a->rp->index - b->rp->index;
4690 if( rc==0 ) rc = a->dot - b->dot;
4691 }
4692 if( rc==0 ){
4693 if( a ) rc = 1;
4694 if( b ) rc = -1;
4695 }
4696 return rc;
4697}
4698
4699/* Hash a state */
drh01f75f22013-10-02 20:46:30 +00004700PRIVATE unsigned statehash(struct config *a)
drh75897232000-05-29 14:26:00 +00004701{
drh01f75f22013-10-02 20:46:30 +00004702 unsigned h=0;
drh75897232000-05-29 14:26:00 +00004703 while( a ){
4704 h = h*571 + a->rp->index*37 + a->dot;
4705 a = a->bp;
4706 }
4707 return h;
4708}
4709
4710/* Allocate a new state structure */
4711struct state *State_new()
4712{
icculus9e44cf12010-02-14 17:14:22 +00004713 struct state *newstate;
4714 newstate = (struct state *)calloc(1, sizeof(struct state) );
4715 MemoryCheck(newstate);
4716 return newstate;
drh75897232000-05-29 14:26:00 +00004717}
4718
4719/* There is one instance of the following structure for each
4720** associative array of type "x3".
4721*/
4722struct s_x3 {
4723 int size; /* The number of available slots. */
4724 /* Must be a power of 2 greater than or */
4725 /* equal to 1 */
4726 int count; /* Number of currently slots filled */
4727 struct s_x3node *tbl; /* The data stored here */
4728 struct s_x3node **ht; /* Hash table for lookups */
4729};
4730
4731/* There is one instance of this structure for every data element
4732** in an associative array of type "x3".
4733*/
4734typedef struct s_x3node {
4735 struct state *data; /* The data */
4736 struct config *key; /* The key */
4737 struct s_x3node *next; /* Next entry with the same hash */
4738 struct s_x3node **from; /* Previous link */
4739} x3node;
4740
4741/* There is only one instance of the array, which is the following */
4742static struct s_x3 *x3a;
4743
4744/* Allocate a new associative array */
4745void State_init(){
4746 if( x3a ) return;
4747 x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
4748 if( x3a ){
4749 x3a->size = 128;
4750 x3a->count = 0;
4751 x3a->tbl = (x3node*)malloc(
4752 (sizeof(x3node) + sizeof(x3node*))*128 );
4753 if( x3a->tbl==0 ){
4754 free(x3a);
4755 x3a = 0;
4756 }else{
4757 int i;
4758 x3a->ht = (x3node**)&(x3a->tbl[128]);
4759 for(i=0; i<128; i++) x3a->ht[i] = 0;
4760 }
4761 }
4762}
4763/* Insert a new record into the array. Return TRUE if successful.
4764** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004765int State_insert(struct state *data, struct config *key)
drh75897232000-05-29 14:26:00 +00004766{
4767 x3node *np;
drh01f75f22013-10-02 20:46:30 +00004768 unsigned h;
4769 unsigned ph;
drh75897232000-05-29 14:26:00 +00004770
4771 if( x3a==0 ) return 0;
4772 ph = statehash(key);
4773 h = ph & (x3a->size-1);
4774 np = x3a->ht[h];
4775 while( np ){
4776 if( statecmp(np->key,key)==0 ){
4777 /* An existing entry with the same key is found. */
4778 /* Fail because overwrite is not allows. */
4779 return 0;
4780 }
4781 np = np->next;
4782 }
4783 if( x3a->count>=x3a->size ){
4784 /* Need to make the hash table bigger */
4785 int i,size;
4786 struct s_x3 array;
4787 array.size = size = x3a->size*2;
4788 array.count = x3a->count;
4789 array.tbl = (x3node*)malloc(
4790 (sizeof(x3node) + sizeof(x3node*))*size );
4791 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4792 array.ht = (x3node**)&(array.tbl[size]);
4793 for(i=0; i<size; i++) array.ht[i] = 0;
4794 for(i=0; i<x3a->count; i++){
4795 x3node *oldnp, *newnp;
4796 oldnp = &(x3a->tbl[i]);
4797 h = statehash(oldnp->key) & (size-1);
4798 newnp = &(array.tbl[i]);
4799 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4800 newnp->next = array.ht[h];
4801 newnp->key = oldnp->key;
4802 newnp->data = oldnp->data;
4803 newnp->from = &(array.ht[h]);
4804 array.ht[h] = newnp;
4805 }
4806 free(x3a->tbl);
4807 *x3a = array;
4808 }
4809 /* Insert the new data */
4810 h = ph & (x3a->size-1);
4811 np = &(x3a->tbl[x3a->count++]);
4812 np->key = key;
4813 np->data = data;
4814 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
4815 np->next = x3a->ht[h];
4816 x3a->ht[h] = np;
4817 np->from = &(x3a->ht[h]);
4818 return 1;
4819}
4820
4821/* Return a pointer to data assigned to the given key. Return NULL
4822** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004823struct state *State_find(struct config *key)
drh75897232000-05-29 14:26:00 +00004824{
drh01f75f22013-10-02 20:46:30 +00004825 unsigned h;
drh75897232000-05-29 14:26:00 +00004826 x3node *np;
4827
4828 if( x3a==0 ) return 0;
4829 h = statehash(key) & (x3a->size-1);
4830 np = x3a->ht[h];
4831 while( np ){
4832 if( statecmp(np->key,key)==0 ) break;
4833 np = np->next;
4834 }
4835 return np ? np->data : 0;
4836}
4837
4838/* Return an array of pointers to all data in the table.
4839** The array is obtained from malloc. Return NULL if memory allocation
4840** problems, or if the array is empty. */
4841struct state **State_arrayof()
4842{
4843 struct state **array;
4844 int i,size;
4845 if( x3a==0 ) return 0;
4846 size = x3a->count;
4847 array = (struct state **)malloc( sizeof(struct state *)*size );
4848 if( array ){
4849 for(i=0; i<size; i++) array[i] = x3a->tbl[i].data;
4850 }
4851 return array;
4852}
4853
4854/* Hash a configuration */
drh01f75f22013-10-02 20:46:30 +00004855PRIVATE unsigned confighash(struct config *a)
drh75897232000-05-29 14:26:00 +00004856{
drh01f75f22013-10-02 20:46:30 +00004857 unsigned h=0;
drh75897232000-05-29 14:26:00 +00004858 h = h*571 + a->rp->index*37 + a->dot;
4859 return h;
4860}
4861
4862/* There is one instance of the following structure for each
4863** associative array of type "x4".
4864*/
4865struct s_x4 {
4866 int size; /* The number of available slots. */
4867 /* Must be a power of 2 greater than or */
4868 /* equal to 1 */
4869 int count; /* Number of currently slots filled */
4870 struct s_x4node *tbl; /* The data stored here */
4871 struct s_x4node **ht; /* Hash table for lookups */
4872};
4873
4874/* There is one instance of this structure for every data element
4875** in an associative array of type "x4".
4876*/
4877typedef struct s_x4node {
4878 struct config *data; /* The data */
4879 struct s_x4node *next; /* Next entry with the same hash */
4880 struct s_x4node **from; /* Previous link */
4881} x4node;
4882
4883/* There is only one instance of the array, which is the following */
4884static struct s_x4 *x4a;
4885
4886/* Allocate a new associative array */
4887void Configtable_init(){
4888 if( x4a ) return;
4889 x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
4890 if( x4a ){
4891 x4a->size = 64;
4892 x4a->count = 0;
4893 x4a->tbl = (x4node*)malloc(
4894 (sizeof(x4node) + sizeof(x4node*))*64 );
4895 if( x4a->tbl==0 ){
4896 free(x4a);
4897 x4a = 0;
4898 }else{
4899 int i;
4900 x4a->ht = (x4node**)&(x4a->tbl[64]);
4901 for(i=0; i<64; i++) x4a->ht[i] = 0;
4902 }
4903 }
4904}
4905/* Insert a new record into the array. Return TRUE if successful.
4906** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004907int Configtable_insert(struct config *data)
drh75897232000-05-29 14:26:00 +00004908{
4909 x4node *np;
drh01f75f22013-10-02 20:46:30 +00004910 unsigned h;
4911 unsigned ph;
drh75897232000-05-29 14:26:00 +00004912
4913 if( x4a==0 ) return 0;
4914 ph = confighash(data);
4915 h = ph & (x4a->size-1);
4916 np = x4a->ht[h];
4917 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00004918 if( Configcmp((const char *) np->data,(const char *) data)==0 ){
drh75897232000-05-29 14:26:00 +00004919 /* An existing entry with the same key is found. */
4920 /* Fail because overwrite is not allows. */
4921 return 0;
4922 }
4923 np = np->next;
4924 }
4925 if( x4a->count>=x4a->size ){
4926 /* Need to make the hash table bigger */
4927 int i,size;
4928 struct s_x4 array;
4929 array.size = size = x4a->size*2;
4930 array.count = x4a->count;
4931 array.tbl = (x4node*)malloc(
4932 (sizeof(x4node) + sizeof(x4node*))*size );
4933 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4934 array.ht = (x4node**)&(array.tbl[size]);
4935 for(i=0; i<size; i++) array.ht[i] = 0;
4936 for(i=0; i<x4a->count; i++){
4937 x4node *oldnp, *newnp;
4938 oldnp = &(x4a->tbl[i]);
4939 h = confighash(oldnp->data) & (size-1);
4940 newnp = &(array.tbl[i]);
4941 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4942 newnp->next = array.ht[h];
4943 newnp->data = oldnp->data;
4944 newnp->from = &(array.ht[h]);
4945 array.ht[h] = newnp;
4946 }
4947 free(x4a->tbl);
4948 *x4a = array;
4949 }
4950 /* Insert the new data */
4951 h = ph & (x4a->size-1);
4952 np = &(x4a->tbl[x4a->count++]);
4953 np->data = data;
4954 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
4955 np->next = x4a->ht[h];
4956 x4a->ht[h] = np;
4957 np->from = &(x4a->ht[h]);
4958 return 1;
4959}
4960
4961/* Return a pointer to data assigned to the given key. Return NULL
4962** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004963struct config *Configtable_find(struct config *key)
drh75897232000-05-29 14:26:00 +00004964{
4965 int h;
4966 x4node *np;
4967
4968 if( x4a==0 ) return 0;
4969 h = confighash(key) & (x4a->size-1);
4970 np = x4a->ht[h];
4971 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00004972 if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
drh75897232000-05-29 14:26:00 +00004973 np = np->next;
4974 }
4975 return np ? np->data : 0;
4976}
4977
4978/* Remove all data from the table. Pass each data to the function "f"
4979** as it is removed. ("f" may be null to avoid this step.) */
icculus9e44cf12010-02-14 17:14:22 +00004980void Configtable_clear(int(*f)(struct config *))
drh75897232000-05-29 14:26:00 +00004981{
4982 int i;
4983 if( x4a==0 || x4a->count==0 ) return;
4984 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
4985 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
4986 x4a->count = 0;
4987 return;
4988}