blob: 146f7e20ae374ebeb42bc90aa0e43b2ea40ceaaa [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)
18# define __WIN32__
19# endif
20#endif
21
rse8f304482007-07-30 18:31:53 +000022#ifdef __WIN32__
23extern int access();
24#else
25#include <unistd.h>
26#endif
27
drh75897232000-05-29 14:26:00 +000028/* #define PRIVATE static */
29#define PRIVATE
30
31#ifdef TEST
32#define MAXRHS 5 /* Set low to exercise exception code */
33#else
34#define MAXRHS 1000
35#endif
36
icculusf5ad8242010-02-14 05:34:42 +000037static const char **made_files = NULL;
38static int made_files_count = 0;
39static int successful_exit = 0;
40static void LemonAtExit(void)
41{
42 /* if we failed, delete (most) files we made, to unconfuse build tools. */
43 int i;
44 for (i = 0; i < made_files_count; i++) {
45 if (!successful_exit) {
46 remove(made_files[i]);
47 }
48 free((void *) made_files[i]);
49 }
50 free(made_files);
51 made_files_count = 0;
52 made_files = NULL;
53}
54
drhe9278182007-07-18 18:16:29 +000055static char *msort(char*,char**,int(*)(const char*,const char*));
drh75897232000-05-29 14:26:00 +000056
drh87cf1372008-08-13 20:09:06 +000057/*
58** Compilers are getting increasingly pedantic about type conversions
59** as C evolves ever closer to Ada.... To work around the latest problems
60** we have to define the following variant of strlen().
61*/
62#define lemonStrlen(X) ((int)strlen(X))
63
icculus9e44cf12010-02-14 17:14:22 +000064/* a few forward declarations... */
65struct rule;
66struct lemon;
67struct action;
68
drhe9278182007-07-18 18:16:29 +000069static struct action *Action_new(void);
70static struct action *Action_sort(struct action *);
drh75897232000-05-29 14:26:00 +000071
72/********** From the file "build.h" ************************************/
73void FindRulePrecedences();
74void FindFirstSets();
75void FindStates();
76void FindLinks();
77void FindFollowSets();
78void FindActions();
79
80/********* From the file "configlist.h" *********************************/
icculus9e44cf12010-02-14 17:14:22 +000081void Configlist_init(void);
82struct config *Configlist_add(struct rule *, int);
83struct config *Configlist_addbasis(struct rule *, int);
84void Configlist_closure(struct lemon *);
85void Configlist_sort(void);
86void Configlist_sortbasis(void);
87struct config *Configlist_return(void);
88struct config *Configlist_basis(void);
89void Configlist_eat(struct config *);
90void Configlist_reset(void);
drh75897232000-05-29 14:26:00 +000091
92/********* From the file "error.h" ***************************************/
drhf9a2e7b2003-04-15 01:49:48 +000093void ErrorMsg(const char *, int,const char *, ...);
drh75897232000-05-29 14:26:00 +000094
95/****** From the file "option.h" ******************************************/
icculus9e44cf12010-02-14 17:14:22 +000096enum option_type { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR,
97 OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR};
drh75897232000-05-29 14:26:00 +000098struct s_options {
icculus9e44cf12010-02-14 17:14:22 +000099 enum option_type type;
100 const char *label;
drh75897232000-05-29 14:26:00 +0000101 char *arg;
icculus9e44cf12010-02-14 17:14:22 +0000102 const char *message;
drh75897232000-05-29 14:26:00 +0000103};
icculus9e44cf12010-02-14 17:14:22 +0000104int OptInit(char**,struct s_options*,FILE*);
105int OptNArgs(void);
106char *OptArg(int);
107void OptErr(int);
108void OptPrint(void);
drh75897232000-05-29 14:26:00 +0000109
110/******** From the file "parse.h" *****************************************/
icculus9e44cf12010-02-14 17:14:22 +0000111void Parse(struct lemon *lemp);
drh75897232000-05-29 14:26:00 +0000112
113/********* From the file "plink.h" ***************************************/
icculus9e44cf12010-02-14 17:14:22 +0000114struct plink *Plink_new(void);
115void Plink_add(struct plink **, struct config *);
116void Plink_copy(struct plink **, struct plink *);
117void Plink_delete(struct plink *);
drh75897232000-05-29 14:26:00 +0000118
119/********** From the file "report.h" *************************************/
icculus9e44cf12010-02-14 17:14:22 +0000120void Reprint(struct lemon *);
121void ReportOutput(struct lemon *);
122void ReportTable(struct lemon *, int);
123void ReportHeader(struct lemon *);
124void CompressTables(struct lemon *);
125void ResortStates(struct lemon *);
drh75897232000-05-29 14:26:00 +0000126
127/********** From the file "set.h" ****************************************/
icculus9e44cf12010-02-14 17:14:22 +0000128void SetSize(int); /* All sets will be of size N */
129char *SetNew(void); /* A new set for element 0..N */
130void SetFree(char*); /* Deallocate a set */
drh75897232000-05-29 14:26:00 +0000131
icculus9e44cf12010-02-14 17:14:22 +0000132char *SetNew(void); /* A new set for element 0..N */
133int SetAdd(char*,int); /* Add element to a set */
134int SetUnion(char *,char *); /* A <- A U B, thru element N */
drh75897232000-05-29 14:26:00 +0000135#define SetFind(X,Y) (X[Y]) /* True if Y is in set X */
136
137/********** From the file "struct.h" *************************************/
138/*
139** Principal data structures for the LEMON parser generator.
140*/
141
drhaa9f1122007-08-23 02:50:56 +0000142typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean;
drh75897232000-05-29 14:26:00 +0000143
144/* Symbols (terminals and nonterminals) of the grammar are stored
145** in the following: */
icculus9e44cf12010-02-14 17:14:22 +0000146enum symbol_type {
147 TERMINAL,
148 NONTERMINAL,
149 MULTITERMINAL
150};
151enum e_assoc {
drh75897232000-05-29 14:26:00 +0000152 LEFT,
153 RIGHT,
154 NONE,
155 UNK
icculus9e44cf12010-02-14 17:14:22 +0000156};
157struct symbol {
158 const char *name; /* Name of the symbol */
159 int index; /* Index number for this symbol */
160 enum symbol_type type; /* Symbols are all either TERMINALS or NTs */
161 struct rule *rule; /* Linked list of rules of this (if an NT) */
162 struct symbol *fallback; /* fallback token in case this token doesn't parse */
163 int prec; /* Precedence if defined (-1 otherwise) */
164 enum e_assoc assoc; /* Associativity if precedence is defined */
drh75897232000-05-29 14:26:00 +0000165 char *firstset; /* First-set for all rules of this symbol */
166 Boolean lambda; /* True if NT and can generate an empty string */
drhc4dd3fd2008-01-22 01:48:05 +0000167 int useCnt; /* Number of times used */
drh75897232000-05-29 14:26:00 +0000168 char *destructor; /* Code which executes whenever this symbol is
169 ** popped from the stack during error processing */
drh4dc8ef52008-07-01 17:13:57 +0000170 int destLineno; /* Line number for start of destructor */
drh75897232000-05-29 14:26:00 +0000171 char *datatype; /* The data type of information held by this
172 ** object. Only used if type==NONTERMINAL */
173 int dtnum; /* The data type number. In the parser, the value
174 ** stack is a union. The .yy%d element of this
175 ** union is the correct data type for this object */
drhfd405312005-11-06 04:06:59 +0000176 /* The following fields are used by MULTITERMINALs only */
177 int nsubsym; /* Number of constituent symbols in the MULTI */
178 struct symbol **subsym; /* Array of constituent symbols */
drh75897232000-05-29 14:26:00 +0000179};
180
181/* Each production rule in the grammar is stored in the following
182** structure. */
183struct rule {
184 struct symbol *lhs; /* Left-hand side of the rule */
icculus9e44cf12010-02-14 17:14:22 +0000185 const char *lhsalias; /* Alias for the LHS (NULL if none) */
drhb4960992007-10-05 16:16:36 +0000186 int lhsStart; /* True if left-hand side is the start symbol */
drh75897232000-05-29 14:26:00 +0000187 int ruleline; /* Line number for the rule */
188 int nrhs; /* Number of RHS symbols */
189 struct symbol **rhs; /* The RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +0000190 const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */
drh75897232000-05-29 14:26:00 +0000191 int line; /* Line number at which code begins */
icculus9e44cf12010-02-14 17:14:22 +0000192 const char *code; /* The code executed when this rule is reduced */
drh75897232000-05-29 14:26:00 +0000193 struct symbol *precsym; /* Precedence symbol for this rule */
194 int index; /* An index number for this rule */
195 Boolean canReduce; /* True if this rule is ever reduced */
196 struct rule *nextlhs; /* Next rule with the same LHS */
197 struct rule *next; /* Next rule in the global list */
198};
199
200/* A configuration is a production rule of the grammar together with
201** a mark (dot) showing how much of that rule has been processed so far.
202** Configurations also contain a follow-set which is a list of terminal
203** symbols which are allowed to immediately follow the end of the rule.
204** Every configuration is recorded as an instance of the following: */
icculus9e44cf12010-02-14 17:14:22 +0000205enum cfgstatus {
206 COMPLETE,
207 INCOMPLETE
208};
drh75897232000-05-29 14:26:00 +0000209struct config {
210 struct rule *rp; /* The rule upon which the configuration is based */
211 int dot; /* The parse point */
212 char *fws; /* Follow-set for this configuration only */
213 struct plink *fplp; /* Follow-set forward propagation links */
214 struct plink *bplp; /* Follow-set backwards propagation links */
215 struct state *stp; /* Pointer to state which contains this */
icculus9e44cf12010-02-14 17:14:22 +0000216 enum cfgstatus status; /* used during followset and shift computations */
drh75897232000-05-29 14:26:00 +0000217 struct config *next; /* Next configuration in the state */
218 struct config *bp; /* The next basis configuration */
219};
220
icculus9e44cf12010-02-14 17:14:22 +0000221enum e_action {
222 SHIFT,
223 ACCEPT,
224 REDUCE,
225 ERROR,
226 SSCONFLICT, /* A shift/shift conflict */
227 SRCONFLICT, /* Was a reduce, but part of a conflict */
228 RRCONFLICT, /* Was a reduce, but part of a conflict */
229 SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
230 RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
231 NOT_USED /* Deleted by compression */
232};
233
drh75897232000-05-29 14:26:00 +0000234/* Every shift or reduce operation is stored as one of the following */
235struct action {
236 struct symbol *sp; /* The look-ahead symbol */
icculus9e44cf12010-02-14 17:14:22 +0000237 enum e_action type;
drh75897232000-05-29 14:26:00 +0000238 union {
239 struct state *stp; /* The new state, if a shift */
240 struct rule *rp; /* The rule, if a reduce */
241 } x;
242 struct action *next; /* Next action for this state */
243 struct action *collide; /* Next action with the same hash */
244};
245
246/* Each state of the generated parser's finite state machine
247** is encoded as an instance of the following structure. */
248struct state {
249 struct config *bp; /* The basis configurations for this state */
250 struct config *cfp; /* All configurations in this set */
drh34ff57b2008-07-14 12:27:51 +0000251 int statenum; /* Sequential number for this state */
drh75897232000-05-29 14:26:00 +0000252 struct action *ap; /* Array of actions for this state */
drh8b582012003-10-21 13:16:03 +0000253 int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */
254 int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */
255 int iDflt; /* Default action */
drh75897232000-05-29 14:26:00 +0000256};
drh8b582012003-10-21 13:16:03 +0000257#define NO_OFFSET (-2147483647)
drh75897232000-05-29 14:26:00 +0000258
259/* A followset propagation link indicates that the contents of one
260** configuration followset should be propagated to another whenever
261** the first changes. */
262struct plink {
263 struct config *cfp; /* The configuration to which linked */
264 struct plink *next; /* The next propagate link */
265};
266
267/* The state vector for the entire parser generator is recorded as
268** follows. (LEMON uses no global variables and makes little use of
269** static variables. Fields in the following structure can be thought
270** of as begin global variables in the program.) */
271struct lemon {
272 struct state **sorted; /* Table of states sorted by state number */
273 struct rule *rule; /* List of all rules */
274 int nstate; /* Number of states */
275 int nrule; /* Number of rules */
276 int nsymbol; /* Number of terminal and nonterminal symbols */
277 int nterminal; /* Number of terminal symbols */
278 struct symbol **symbols; /* Sorted array of pointers to symbols */
279 int errorcnt; /* Number of errors */
280 struct symbol *errsym; /* The error symbol */
drhe09daa92006-06-10 13:29:31 +0000281 struct symbol *wildcard; /* Token that matches anything */
drh75897232000-05-29 14:26:00 +0000282 char *name; /* Name of the generated parser */
283 char *arg; /* Declaration of the 3th argument to parser */
284 char *tokentype; /* Type of terminal symbols in the parser stack */
drh960e8c62001-04-03 16:53:21 +0000285 char *vartype; /* The default type of non-terminal symbols */
drh75897232000-05-29 14:26:00 +0000286 char *start; /* Name of the start symbol for the grammar */
287 char *stacksize; /* Size of the parser stack */
288 char *include; /* Code to put at the start of the C file */
drh75897232000-05-29 14:26:00 +0000289 char *error; /* Code to execute when an error is seen */
drh75897232000-05-29 14:26:00 +0000290 char *overflow; /* Code to execute on a stack overflow */
drh75897232000-05-29 14:26:00 +0000291 char *failure; /* Code to execute on parser failure */
drh75897232000-05-29 14:26:00 +0000292 char *accept; /* Code to execute when the parser excepts */
drh75897232000-05-29 14:26:00 +0000293 char *extracode; /* Code appended to the generated file */
drh75897232000-05-29 14:26:00 +0000294 char *tokendest; /* Code to execute to destroy token data */
drh960e8c62001-04-03 16:53:21 +0000295 char *vardest; /* Code for the default non-terminal destructor */
drh75897232000-05-29 14:26:00 +0000296 char *filename; /* Name of the input file */
297 char *outname; /* Name of the current output file */
298 char *tokenprefix; /* A prefix added to token names in the .h file */
299 int nconflict; /* Number of parsing conflicts */
300 int tablesize; /* Size of the parse tables */
301 int basisflag; /* Print only basis configurations */
drh34ff57b2008-07-14 12:27:51 +0000302 int has_fallback; /* True if any %fallback is seen in the grammar */
shane58543932008-12-10 20:10:04 +0000303 int nolinenosflag; /* True if #line statements should not be printed */
drh75897232000-05-29 14:26:00 +0000304 char *argv0; /* Name of the program */
305};
306
307#define MemoryCheck(X) if((X)==0){ \
308 extern void memory_error(); \
309 memory_error(); \
310}
311
312/**************** From the file "table.h" *********************************/
313/*
314** All code in this file has been automatically generated
315** from a specification in the file
316** "table.q"
317** by the associative array code building program "aagen".
318** Do not edit this file! Instead, edit the specification
319** file, then rerun aagen.
320*/
321/*
322** Code for processing tables in the LEMON parser generator.
323*/
drh75897232000-05-29 14:26:00 +0000324/* Routines for handling a strings */
325
icculus9e44cf12010-02-14 17:14:22 +0000326const char *Strsafe(const char *);
drh75897232000-05-29 14:26:00 +0000327
icculus9e44cf12010-02-14 17:14:22 +0000328void Strsafe_init(void);
329int Strsafe_insert(const char *);
330const char *Strsafe_find(const char *);
drh75897232000-05-29 14:26:00 +0000331
332/* Routines for handling symbols of the grammar */
333
icculus9e44cf12010-02-14 17:14:22 +0000334struct symbol *Symbol_new(const char *);
335int Symbolcmpp(const void *, const void *);
336void Symbol_init(void);
337int Symbol_insert(struct symbol *, const char *);
338struct symbol *Symbol_find(const char *);
339struct symbol *Symbol_Nth(int);
340int Symbol_count(void);
341struct symbol **Symbol_arrayof(void);
drh75897232000-05-29 14:26:00 +0000342
343/* Routines to manage the state table */
344
icculus9e44cf12010-02-14 17:14:22 +0000345int Configcmp(const char *, const char *);
346struct state *State_new(void);
347void State_init(void);
348int State_insert(struct state *, struct config *);
349struct state *State_find(struct config *);
drh75897232000-05-29 14:26:00 +0000350struct state **State_arrayof(/* */);
351
352/* Routines used for efficiency in Configlist_add */
353
icculus9e44cf12010-02-14 17:14:22 +0000354void Configtable_init(void);
355int Configtable_insert(struct config *);
356struct config *Configtable_find(struct config *);
357void Configtable_clear(int(*)(struct config *));
358
drh75897232000-05-29 14:26:00 +0000359/****************** From the file "action.c" *******************************/
360/*
361** Routines processing parser actions in the LEMON parser generator.
362*/
363
364/* Allocate a new parser action */
drhe9278182007-07-18 18:16:29 +0000365static struct action *Action_new(void){
drh75897232000-05-29 14:26:00 +0000366 static struct action *freelist = 0;
icculus9e44cf12010-02-14 17:14:22 +0000367 struct action *newaction;
drh75897232000-05-29 14:26:00 +0000368
369 if( freelist==0 ){
370 int i;
371 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +0000372 freelist = (struct action *)calloc(amt, sizeof(struct action));
drh75897232000-05-29 14:26:00 +0000373 if( freelist==0 ){
374 fprintf(stderr,"Unable to allocate memory for a new parser action.");
375 exit(1);
376 }
377 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
378 freelist[amt-1].next = 0;
379 }
icculus9e44cf12010-02-14 17:14:22 +0000380 newaction = freelist;
drh75897232000-05-29 14:26:00 +0000381 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +0000382 return newaction;
drh75897232000-05-29 14:26:00 +0000383}
384
drhe9278182007-07-18 18:16:29 +0000385/* Compare two actions for sorting purposes. Return negative, zero, or
386** positive if the first action is less than, equal to, or greater than
387** the first
388*/
389static int actioncmp(
390 struct action *ap1,
391 struct action *ap2
392){
drh75897232000-05-29 14:26:00 +0000393 int rc;
394 rc = ap1->sp->index - ap2->sp->index;
drh75897232000-05-29 14:26:00 +0000395 if( rc==0 ){
drh9892c5d2007-12-21 00:02:11 +0000396 rc = (int)ap1->type - (int)ap2->type;
397 }
398 if( rc==0 && ap1->type==REDUCE ){
drh75897232000-05-29 14:26:00 +0000399 rc = ap1->x.rp->index - ap2->x.rp->index;
400 }
drhe594bc32009-11-03 13:02:25 +0000401 if( rc==0 ){
402 rc = ap2 - ap1;
403 }
drh75897232000-05-29 14:26:00 +0000404 return rc;
405}
406
407/* Sort parser actions */
drhe9278182007-07-18 18:16:29 +0000408static struct action *Action_sort(
409 struct action *ap
410){
411 ap = (struct action *)msort((char *)ap,(char **)&ap->next,
412 (int(*)(const char*,const char*))actioncmp);
drh75897232000-05-29 14:26:00 +0000413 return ap;
414}
415
icculus9e44cf12010-02-14 17:14:22 +0000416void Action_add(
417 struct action **app,
418 enum e_action type,
419 struct symbol *sp,
420 char *arg
421){
422 struct action *newaction;
423 newaction = Action_new();
424 newaction->next = *app;
425 *app = newaction;
426 newaction->type = type;
427 newaction->sp = sp;
drh75897232000-05-29 14:26:00 +0000428 if( type==SHIFT ){
icculus9e44cf12010-02-14 17:14:22 +0000429 newaction->x.stp = (struct state *)arg;
drh75897232000-05-29 14:26:00 +0000430 }else{
icculus9e44cf12010-02-14 17:14:22 +0000431 newaction->x.rp = (struct rule *)arg;
drh75897232000-05-29 14:26:00 +0000432 }
433}
drh8b582012003-10-21 13:16:03 +0000434/********************** New code to implement the "acttab" module ***********/
435/*
436** This module implements routines use to construct the yy_action[] table.
437*/
438
439/*
440** The state of the yy_action table under construction is an instance of
drh8dc3e8f2010-01-07 03:53:03 +0000441** the following structure.
442**
443** The yy_action table maps the pair (state_number, lookahead) into an
444** action_number. The table is an array of integers pairs. The state_number
445** determines an initial offset into the yy_action array. The lookahead
446** value is then added to this initial offset to get an index X into the
447** yy_action array. If the aAction[X].lookahead equals the value of the
448** of the lookahead input, then the value of the action_number output is
449** aAction[X].action. If the lookaheads do not match then the
450** default action for the state_number is returned.
451**
452** All actions associated with a single state_number are first entered
453** into aLookahead[] using multiple calls to acttab_action(). Then the
454** actions for that single state_number are placed into the aAction[]
455** array with a single call to acttab_insert(). The acttab_insert() call
456** also resets the aLookahead[] array in preparation for the next
457** state number.
drh8b582012003-10-21 13:16:03 +0000458*/
icculus9e44cf12010-02-14 17:14:22 +0000459struct lookahead_action {
460 int lookahead; /* Value of the lookahead token */
461 int action; /* Action to take on the given lookahead */
462};
drh8b582012003-10-21 13:16:03 +0000463typedef struct acttab acttab;
464struct acttab {
465 int nAction; /* Number of used slots in aAction[] */
466 int nActionAlloc; /* Slots allocated for aAction[] */
icculus9e44cf12010-02-14 17:14:22 +0000467 struct lookahead_action
468 *aAction, /* The yy_action[] table under construction */
drh8b582012003-10-21 13:16:03 +0000469 *aLookahead; /* A single new transaction set */
470 int mnLookahead; /* Minimum aLookahead[].lookahead */
471 int mnAction; /* Action associated with mnLookahead */
472 int mxLookahead; /* Maximum aLookahead[].lookahead */
473 int nLookahead; /* Used slots in aLookahead[] */
474 int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
475};
476
477/* Return the number of entries in the yy_action table */
478#define acttab_size(X) ((X)->nAction)
479
480/* The value for the N-th entry in yy_action */
481#define acttab_yyaction(X,N) ((X)->aAction[N].action)
482
483/* The value for the N-th entry in yy_lookahead */
484#define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
485
486/* Free all memory associated with the given acttab */
487void acttab_free(acttab *p){
488 free( p->aAction );
489 free( p->aLookahead );
490 free( p );
491}
492
493/* Allocate a new acttab structure */
494acttab *acttab_alloc(void){
icculus9e44cf12010-02-14 17:14:22 +0000495 acttab *p = (acttab *) calloc( 1, sizeof(*p) );
drh8b582012003-10-21 13:16:03 +0000496 if( p==0 ){
497 fprintf(stderr,"Unable to allocate memory for a new acttab.");
498 exit(1);
499 }
500 memset(p, 0, sizeof(*p));
501 return p;
502}
503
drh8dc3e8f2010-01-07 03:53:03 +0000504/* Add a new action to the current transaction set.
505**
506** This routine is called once for each lookahead for a particular
507** state.
drh8b582012003-10-21 13:16:03 +0000508*/
509void acttab_action(acttab *p, int lookahead, int action){
510 if( p->nLookahead>=p->nLookaheadAlloc ){
511 p->nLookaheadAlloc += 25;
icculus9e44cf12010-02-14 17:14:22 +0000512 p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
drh8b582012003-10-21 13:16:03 +0000513 sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
514 if( p->aLookahead==0 ){
515 fprintf(stderr,"malloc failed\n");
516 exit(1);
517 }
518 }
519 if( p->nLookahead==0 ){
520 p->mxLookahead = lookahead;
521 p->mnLookahead = lookahead;
522 p->mnAction = action;
523 }else{
524 if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
525 if( p->mnLookahead>lookahead ){
526 p->mnLookahead = lookahead;
527 p->mnAction = action;
528 }
529 }
530 p->aLookahead[p->nLookahead].lookahead = lookahead;
531 p->aLookahead[p->nLookahead].action = action;
532 p->nLookahead++;
533}
534
535/*
536** Add the transaction set built up with prior calls to acttab_action()
537** into the current action table. Then reset the transaction set back
538** to an empty set in preparation for a new round of acttab_action() calls.
539**
540** Return the offset into the action table of the new transaction.
541*/
542int acttab_insert(acttab *p){
543 int i, j, k, n;
544 assert( p->nLookahead>0 );
545
546 /* Make sure we have enough space to hold the expanded action table
547 ** in the worst case. The worst case occurs if the transaction set
548 ** must be appended to the current action table
549 */
drh784d86f2004-02-19 18:41:53 +0000550 n = p->mxLookahead + 1;
drh8dc3e8f2010-01-07 03:53:03 +0000551 if( p->nAction + n >= p->nActionAlloc ){
drhfdbf9282003-10-21 16:34:41 +0000552 int oldAlloc = p->nActionAlloc;
drh8b582012003-10-21 13:16:03 +0000553 p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
icculus9e44cf12010-02-14 17:14:22 +0000554 p->aAction = (struct lookahead_action *) realloc( p->aAction,
drh8b582012003-10-21 13:16:03 +0000555 sizeof(p->aAction[0])*p->nActionAlloc);
556 if( p->aAction==0 ){
557 fprintf(stderr,"malloc failed\n");
558 exit(1);
559 }
drhfdbf9282003-10-21 16:34:41 +0000560 for(i=oldAlloc; i<p->nActionAlloc; i++){
drh8b582012003-10-21 13:16:03 +0000561 p->aAction[i].lookahead = -1;
562 p->aAction[i].action = -1;
563 }
564 }
565
drh8dc3e8f2010-01-07 03:53:03 +0000566 /* Scan the existing action table looking for an offset that is a
567 ** duplicate of the current transaction set. Fall out of the loop
568 ** if and when the duplicate is found.
drh8b582012003-10-21 13:16:03 +0000569 **
570 ** i is the index in p->aAction[] where p->mnLookahead is inserted.
571 */
drh8dc3e8f2010-01-07 03:53:03 +0000572 for(i=p->nAction-1; i>=0; i--){
drhf16371d2009-11-03 19:18:31 +0000573 if( p->aAction[i].lookahead==p->mnLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000574 /* All lookaheads and actions in the aLookahead[] transaction
575 ** must match against the candidate aAction[i] entry. */
drh8b582012003-10-21 13:16:03 +0000576 if( p->aAction[i].action!=p->mnAction ) continue;
577 for(j=0; j<p->nLookahead; j++){
578 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
579 if( k<0 || k>=p->nAction ) break;
580 if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
581 if( p->aLookahead[j].action!=p->aAction[k].action ) break;
582 }
583 if( j<p->nLookahead ) continue;
drh8dc3e8f2010-01-07 03:53:03 +0000584
585 /* No possible lookahead value that is not in the aLookahead[]
586 ** transaction is allowed to match aAction[i] */
drh8b582012003-10-21 13:16:03 +0000587 n = 0;
588 for(j=0; j<p->nAction; j++){
drhfdbf9282003-10-21 16:34:41 +0000589 if( p->aAction[j].lookahead<0 ) continue;
590 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
drh8b582012003-10-21 13:16:03 +0000591 }
drhfdbf9282003-10-21 16:34:41 +0000592 if( n==p->nLookahead ){
drh8dc3e8f2010-01-07 03:53:03 +0000593 break; /* An exact match is found at offset i */
drhfdbf9282003-10-21 16:34:41 +0000594 }
drh8b582012003-10-21 13:16:03 +0000595 }
596 }
drh8dc3e8f2010-01-07 03:53:03 +0000597
598 /* If no existing offsets exactly match the current transaction, find an
599 ** an empty offset in the aAction[] table in which we can add the
600 ** aLookahead[] transaction.
601 */
drhf16371d2009-11-03 19:18:31 +0000602 if( i<0 ){
drh8dc3e8f2010-01-07 03:53:03 +0000603 /* Look for holes in the aAction[] table that fit the current
604 ** aLookahead[] transaction. Leave i set to the offset of the hole.
605 ** If no holes are found, i is left at p->nAction, which means the
606 ** transaction will be appended. */
607 for(i=0; i<p->nActionAlloc - p->mxLookahead; i++){
drhf16371d2009-11-03 19:18:31 +0000608 if( p->aAction[i].lookahead<0 ){
609 for(j=0; j<p->nLookahead; j++){
610 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
611 if( k<0 ) break;
612 if( p->aAction[k].lookahead>=0 ) break;
613 }
614 if( j<p->nLookahead ) continue;
615 for(j=0; j<p->nAction; j++){
616 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
617 }
618 if( j==p->nAction ){
619 break; /* Fits in empty slots */
620 }
621 }
622 }
623 }
drh8b582012003-10-21 13:16:03 +0000624 /* Insert transaction set at index i. */
625 for(j=0; j<p->nLookahead; j++){
626 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
627 p->aAction[k] = p->aLookahead[j];
628 if( k>=p->nAction ) p->nAction = k+1;
629 }
630 p->nLookahead = 0;
631
632 /* Return the offset that is added to the lookahead in order to get the
633 ** index into yy_action of the action */
634 return i - p->mnLookahead;
635}
636
drh75897232000-05-29 14:26:00 +0000637/********************** From the file "build.c" *****************************/
638/*
639** Routines to construction the finite state machine for the LEMON
640** parser generator.
641*/
642
643/* Find a precedence symbol of every rule in the grammar.
644**
645** Those rules which have a precedence symbol coded in the input
646** grammar using the "[symbol]" construct will already have the
647** rp->precsym field filled. Other rules take as their precedence
648** symbol the first RHS symbol with a defined precedence. If there
649** are not RHS symbols with a defined precedence, the precedence
650** symbol field is left blank.
651*/
icculus9e44cf12010-02-14 17:14:22 +0000652void FindRulePrecedences(struct lemon *xp)
drh75897232000-05-29 14:26:00 +0000653{
654 struct rule *rp;
655 for(rp=xp->rule; rp; rp=rp->next){
656 if( rp->precsym==0 ){
drhfd405312005-11-06 04:06:59 +0000657 int i, j;
658 for(i=0; i<rp->nrhs && rp->precsym==0; i++){
659 struct symbol *sp = rp->rhs[i];
660 if( sp->type==MULTITERMINAL ){
661 for(j=0; j<sp->nsubsym; j++){
662 if( sp->subsym[j]->prec>=0 ){
663 rp->precsym = sp->subsym[j];
664 break;
665 }
666 }
667 }else if( sp->prec>=0 ){
drh75897232000-05-29 14:26:00 +0000668 rp->precsym = rp->rhs[i];
drh75897232000-05-29 14:26:00 +0000669 }
670 }
671 }
672 }
673 return;
674}
675
676/* Find all nonterminals which will generate the empty string.
677** Then go back and compute the first sets of every nonterminal.
678** The first set is the set of all terminal symbols which can begin
679** a string generated by that nonterminal.
680*/
icculus9e44cf12010-02-14 17:14:22 +0000681void FindFirstSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000682{
drhfd405312005-11-06 04:06:59 +0000683 int i, j;
drh75897232000-05-29 14:26:00 +0000684 struct rule *rp;
685 int progress;
686
687 for(i=0; i<lemp->nsymbol; i++){
drhaa9f1122007-08-23 02:50:56 +0000688 lemp->symbols[i]->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +0000689 }
690 for(i=lemp->nterminal; i<lemp->nsymbol; i++){
691 lemp->symbols[i]->firstset = SetNew();
692 }
693
694 /* First compute all lambdas */
695 do{
696 progress = 0;
697 for(rp=lemp->rule; rp; rp=rp->next){
698 if( rp->lhs->lambda ) continue;
699 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +0000700 struct symbol *sp = rp->rhs[i];
drhaa9f1122007-08-23 02:50:56 +0000701 if( sp->type!=TERMINAL || sp->lambda==LEMON_FALSE ) break;
drh75897232000-05-29 14:26:00 +0000702 }
703 if( i==rp->nrhs ){
drhaa9f1122007-08-23 02:50:56 +0000704 rp->lhs->lambda = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +0000705 progress = 1;
706 }
707 }
708 }while( progress );
709
710 /* Now compute all first sets */
711 do{
712 struct symbol *s1, *s2;
713 progress = 0;
714 for(rp=lemp->rule; rp; rp=rp->next){
715 s1 = rp->lhs;
716 for(i=0; i<rp->nrhs; i++){
717 s2 = rp->rhs[i];
718 if( s2->type==TERMINAL ){
719 progress += SetAdd(s1->firstset,s2->index);
720 break;
drhfd405312005-11-06 04:06:59 +0000721 }else if( s2->type==MULTITERMINAL ){
722 for(j=0; j<s2->nsubsym; j++){
723 progress += SetAdd(s1->firstset,s2->subsym[j]->index);
724 }
725 break;
drh75897232000-05-29 14:26:00 +0000726 }else if( s1==s2 ){
drhaa9f1122007-08-23 02:50:56 +0000727 if( s1->lambda==LEMON_FALSE ) break;
drh75897232000-05-29 14:26:00 +0000728 }else{
729 progress += SetUnion(s1->firstset,s2->firstset);
drhaa9f1122007-08-23 02:50:56 +0000730 if( s2->lambda==LEMON_FALSE ) break;
drh75897232000-05-29 14:26:00 +0000731 }
732 }
733 }
734 }while( progress );
735 return;
736}
737
738/* Compute all LR(0) states for the grammar. Links
739** are added to between some states so that the LR(1) follow sets
740** can be computed later.
741*/
icculus9e44cf12010-02-14 17:14:22 +0000742PRIVATE struct state *getstate(struct lemon *); /* forward reference */
743void FindStates(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000744{
745 struct symbol *sp;
746 struct rule *rp;
747
748 Configlist_init();
749
750 /* Find the start symbol */
751 if( lemp->start ){
752 sp = Symbol_find(lemp->start);
753 if( sp==0 ){
754 ErrorMsg(lemp->filename,0,
755"The specified start symbol \"%s\" is not \
756in a nonterminal of the grammar. \"%s\" will be used as the start \
757symbol instead.",lemp->start,lemp->rule->lhs->name);
758 lemp->errorcnt++;
759 sp = lemp->rule->lhs;
760 }
761 }else{
762 sp = lemp->rule->lhs;
763 }
764
765 /* Make sure the start symbol doesn't occur on the right-hand side of
766 ** any rule. Report an error if it does. (YACC would generate a new
767 ** start symbol in this case.) */
768 for(rp=lemp->rule; rp; rp=rp->next){
769 int i;
770 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +0000771 if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */
drh75897232000-05-29 14:26:00 +0000772 ErrorMsg(lemp->filename,0,
773"The start symbol \"%s\" occurs on the \
774right-hand side of a rule. This will result in a parser which \
775does not work properly.",sp->name);
776 lemp->errorcnt++;
777 }
778 }
779 }
780
781 /* The basis configuration set for the first state
782 ** is all rules which have the start symbol as their
783 ** left-hand side */
784 for(rp=sp->rule; rp; rp=rp->nextlhs){
785 struct config *newcfp;
drhb4960992007-10-05 16:16:36 +0000786 rp->lhsStart = 1;
drh75897232000-05-29 14:26:00 +0000787 newcfp = Configlist_addbasis(rp,0);
788 SetAdd(newcfp->fws,0);
789 }
790
791 /* Compute the first state. All other states will be
792 ** computed automatically during the computation of the first one.
793 ** The returned pointer to the first state is not used. */
794 (void)getstate(lemp);
795 return;
796}
797
798/* Return a pointer to a state which is described by the configuration
799** list which has been built from calls to Configlist_add.
800*/
icculus9e44cf12010-02-14 17:14:22 +0000801PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
802PRIVATE struct state *getstate(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000803{
804 struct config *cfp, *bp;
805 struct state *stp;
806
807 /* Extract the sorted basis of the new state. The basis was constructed
808 ** by prior calls to "Configlist_addbasis()". */
809 Configlist_sortbasis();
810 bp = Configlist_basis();
811
812 /* Get a state with the same basis */
813 stp = State_find(bp);
814 if( stp ){
815 /* A state with the same basis already exists! Copy all the follow-set
816 ** propagation links from the state under construction into the
817 ** preexisting state, then return a pointer to the preexisting state */
818 struct config *x, *y;
819 for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
820 Plink_copy(&y->bplp,x->bplp);
821 Plink_delete(x->fplp);
822 x->fplp = x->bplp = 0;
823 }
824 cfp = Configlist_return();
825 Configlist_eat(cfp);
826 }else{
827 /* This really is a new state. Construct all the details */
828 Configlist_closure(lemp); /* Compute the configuration closure */
829 Configlist_sort(); /* Sort the configuration closure */
830 cfp = Configlist_return(); /* Get a pointer to the config list */
831 stp = State_new(); /* A new state structure */
832 MemoryCheck(stp);
833 stp->bp = bp; /* Remember the configuration basis */
834 stp->cfp = cfp; /* Remember the configuration closure */
drhada354d2005-11-05 15:03:59 +0000835 stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
drh75897232000-05-29 14:26:00 +0000836 stp->ap = 0; /* No actions, yet. */
837 State_insert(stp,stp->bp); /* Add to the state table */
838 buildshifts(lemp,stp); /* Recursively compute successor states */
839 }
840 return stp;
841}
842
drhfd405312005-11-06 04:06:59 +0000843/*
844** Return true if two symbols are the same.
845*/
icculus9e44cf12010-02-14 17:14:22 +0000846int same_symbol(struct symbol *a, struct symbol *b)
drhfd405312005-11-06 04:06:59 +0000847{
848 int i;
849 if( a==b ) return 1;
850 if( a->type!=MULTITERMINAL ) return 0;
851 if( b->type!=MULTITERMINAL ) return 0;
852 if( a->nsubsym!=b->nsubsym ) return 0;
853 for(i=0; i<a->nsubsym; i++){
854 if( a->subsym[i]!=b->subsym[i] ) return 0;
855 }
856 return 1;
857}
858
drh75897232000-05-29 14:26:00 +0000859/* Construct all successor states to the given state. A "successor"
860** state is any state which can be reached by a shift action.
861*/
icculus9e44cf12010-02-14 17:14:22 +0000862PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
drh75897232000-05-29 14:26:00 +0000863{
864 struct config *cfp; /* For looping thru the config closure of "stp" */
865 struct config *bcfp; /* For the inner loop on config closure of "stp" */
icculus9e44cf12010-02-14 17:14:22 +0000866 struct config *newcfg; /* */
drh75897232000-05-29 14:26:00 +0000867 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
868 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
869 struct state *newstp; /* A pointer to a successor state */
870
871 /* Each configuration becomes complete after it contibutes to a successor
872 ** state. Initially, all configurations are incomplete */
873 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
874
875 /* Loop through all configurations of the state "stp" */
876 for(cfp=stp->cfp; cfp; cfp=cfp->next){
877 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
878 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
879 Configlist_reset(); /* Reset the new config set */
880 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
881
882 /* For every configuration in the state "stp" which has the symbol "sp"
883 ** following its dot, add the same configuration to the basis set under
884 ** construction but with the dot shifted one symbol to the right. */
885 for(bcfp=cfp; bcfp; bcfp=bcfp->next){
886 if( bcfp->status==COMPLETE ) continue; /* Already used */
887 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
888 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
drhfd405312005-11-06 04:06:59 +0000889 if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
drh75897232000-05-29 14:26:00 +0000890 bcfp->status = COMPLETE; /* Mark this config as used */
icculus9e44cf12010-02-14 17:14:22 +0000891 newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
892 Plink_add(&newcfg->bplp,bcfp);
drh75897232000-05-29 14:26:00 +0000893 }
894
895 /* Get a pointer to the state described by the basis configuration set
896 ** constructed in the preceding loop */
897 newstp = getstate(lemp);
898
899 /* The state "newstp" is reached from the state "stp" by a shift action
900 ** on the symbol "sp" */
drhfd405312005-11-06 04:06:59 +0000901 if( sp->type==MULTITERMINAL ){
902 int i;
903 for(i=0; i<sp->nsubsym; i++){
904 Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
905 }
906 }else{
907 Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
908 }
drh75897232000-05-29 14:26:00 +0000909 }
910}
911
912/*
913** Construct the propagation links
914*/
icculus9e44cf12010-02-14 17:14:22 +0000915void FindLinks(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000916{
917 int i;
918 struct config *cfp, *other;
919 struct state *stp;
920 struct plink *plp;
921
922 /* Housekeeping detail:
923 ** Add to every propagate link a pointer back to the state to
924 ** which the link is attached. */
925 for(i=0; i<lemp->nstate; i++){
926 stp = lemp->sorted[i];
927 for(cfp=stp->cfp; cfp; cfp=cfp->next){
928 cfp->stp = stp;
929 }
930 }
931
932 /* Convert all backlinks into forward links. Only the forward
933 ** links are used in the follow-set computation. */
934 for(i=0; i<lemp->nstate; i++){
935 stp = lemp->sorted[i];
936 for(cfp=stp->cfp; cfp; cfp=cfp->next){
937 for(plp=cfp->bplp; plp; plp=plp->next){
938 other = plp->cfp;
939 Plink_add(&other->fplp,cfp);
940 }
941 }
942 }
943}
944
945/* Compute all followsets.
946**
947** A followset is the set of all symbols which can come immediately
948** after a configuration.
949*/
icculus9e44cf12010-02-14 17:14:22 +0000950void FindFollowSets(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000951{
952 int i;
953 struct config *cfp;
954 struct plink *plp;
955 int progress;
956 int change;
957
958 for(i=0; i<lemp->nstate; i++){
959 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
960 cfp->status = INCOMPLETE;
961 }
962 }
963
964 do{
965 progress = 0;
966 for(i=0; i<lemp->nstate; i++){
967 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
968 if( cfp->status==COMPLETE ) continue;
969 for(plp=cfp->fplp; plp; plp=plp->next){
970 change = SetUnion(plp->cfp->fws,cfp->fws);
971 if( change ){
972 plp->cfp->status = INCOMPLETE;
973 progress = 1;
974 }
975 }
976 cfp->status = COMPLETE;
977 }
978 }
979 }while( progress );
980}
981
icculus9e44cf12010-02-14 17:14:22 +0000982static int resolve_conflict(struct action *,struct action *, struct symbol *);
drh75897232000-05-29 14:26:00 +0000983
984/* Compute the reduce actions, and resolve conflicts.
985*/
icculus9e44cf12010-02-14 17:14:22 +0000986void FindActions(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +0000987{
988 int i,j;
989 struct config *cfp;
990 struct state *stp;
991 struct symbol *sp;
992 struct rule *rp;
993
994 /* Add all of the reduce actions
995 ** A reduce action is added for each element of the followset of
996 ** a configuration which has its dot at the extreme right.
997 */
998 for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
999 stp = lemp->sorted[i];
1000 for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
1001 if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
1002 for(j=0; j<lemp->nterminal; j++){
1003 if( SetFind(cfp->fws,j) ){
1004 /* Add a reduce action to the state "stp" which will reduce by the
1005 ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
drh218dc692004-05-31 23:13:45 +00001006 Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
drh75897232000-05-29 14:26:00 +00001007 }
1008 }
1009 }
1010 }
1011 }
1012
1013 /* Add the accepting token */
1014 if( lemp->start ){
1015 sp = Symbol_find(lemp->start);
1016 if( sp==0 ) sp = lemp->rule->lhs;
1017 }else{
1018 sp = lemp->rule->lhs;
1019 }
1020 /* Add to the first state (which is always the starting state of the
1021 ** finite state machine) an action to ACCEPT if the lookahead is the
1022 ** start nonterminal. */
1023 Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
1024
1025 /* Resolve conflicts */
1026 for(i=0; i<lemp->nstate; i++){
1027 struct action *ap, *nap;
1028 struct state *stp;
1029 stp = lemp->sorted[i];
drhe9278182007-07-18 18:16:29 +00001030 /* assert( stp->ap ); */
drh75897232000-05-29 14:26:00 +00001031 stp->ap = Action_sort(stp->ap);
drhb59499c2002-02-23 18:45:13 +00001032 for(ap=stp->ap; ap && ap->next; ap=ap->next){
drh75897232000-05-29 14:26:00 +00001033 for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
1034 /* The two actions "ap" and "nap" have the same lookahead.
1035 ** Figure out which one should be used */
1036 lemp->nconflict += resolve_conflict(ap,nap,lemp->errsym);
1037 }
1038 }
1039 }
1040
1041 /* Report an error for each rule that can never be reduced. */
drhaa9f1122007-08-23 02:50:56 +00001042 for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00001043 for(i=0; i<lemp->nstate; i++){
1044 struct action *ap;
1045 for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
drhaa9f1122007-08-23 02:50:56 +00001046 if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE;
drh75897232000-05-29 14:26:00 +00001047 }
1048 }
1049 for(rp=lemp->rule; rp; rp=rp->next){
1050 if( rp->canReduce ) continue;
1051 ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
1052 lemp->errorcnt++;
1053 }
1054}
1055
1056/* Resolve a conflict between the two given actions. If the
drh34ff57b2008-07-14 12:27:51 +00001057** conflict can't be resolved, return non-zero.
drh75897232000-05-29 14:26:00 +00001058**
1059** NO LONGER TRUE:
1060** To resolve a conflict, first look to see if either action
1061** is on an error rule. In that case, take the action which
1062** is not associated with the error rule. If neither or both
1063** actions are associated with an error rule, then try to
1064** use precedence to resolve the conflict.
1065**
1066** If either action is a SHIFT, then it must be apx. This
1067** function won't work if apx->type==REDUCE and apy->type==SHIFT.
1068*/
icculus9e44cf12010-02-14 17:14:22 +00001069static int resolve_conflict(
1070 struct action *apx,
1071 struct action *apy,
1072 struct symbol *errsym /* The error symbol (if defined. NULL otherwise) */
1073){
drh75897232000-05-29 14:26:00 +00001074 struct symbol *spx, *spy;
1075 int errcnt = 0;
1076 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
drhf0fa1c12006-12-14 01:06:22 +00001077 if( apx->type==SHIFT && apy->type==SHIFT ){
drh9892c5d2007-12-21 00:02:11 +00001078 apy->type = SSCONFLICT;
drhf0fa1c12006-12-14 01:06:22 +00001079 errcnt++;
1080 }
drh75897232000-05-29 14:26:00 +00001081 if( apx->type==SHIFT && apy->type==REDUCE ){
1082 spx = apx->sp;
1083 spy = apy->x.rp->precsym;
1084 if( spy==0 || spx->prec<0 || spy->prec<0 ){
1085 /* Not enough precedence information. */
drh9892c5d2007-12-21 00:02:11 +00001086 apy->type = SRCONFLICT;
drh75897232000-05-29 14:26:00 +00001087 errcnt++;
1088 }else if( spx->prec>spy->prec ){ /* Lower precedence wins */
1089 apy->type = RD_RESOLVED;
1090 }else if( spx->prec<spy->prec ){
1091 apx->type = SH_RESOLVED;
1092 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
1093 apy->type = RD_RESOLVED; /* associativity */
1094 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
1095 apx->type = SH_RESOLVED;
1096 }else{
1097 assert( spx->prec==spy->prec && spx->assoc==NONE );
drh9892c5d2007-12-21 00:02:11 +00001098 apy->type = SRCONFLICT;
drh75897232000-05-29 14:26:00 +00001099 errcnt++;
1100 }
1101 }else if( apx->type==REDUCE && apy->type==REDUCE ){
1102 spx = apx->x.rp->precsym;
1103 spy = apy->x.rp->precsym;
1104 if( spx==0 || spy==0 || spx->prec<0 ||
1105 spy->prec<0 || spx->prec==spy->prec ){
drh9892c5d2007-12-21 00:02:11 +00001106 apy->type = RRCONFLICT;
drh75897232000-05-29 14:26:00 +00001107 errcnt++;
1108 }else if( spx->prec>spy->prec ){
1109 apy->type = RD_RESOLVED;
1110 }else if( spx->prec<spy->prec ){
1111 apx->type = RD_RESOLVED;
1112 }
1113 }else{
drhb59499c2002-02-23 18:45:13 +00001114 assert(
1115 apx->type==SH_RESOLVED ||
1116 apx->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001117 apx->type==SSCONFLICT ||
1118 apx->type==SRCONFLICT ||
1119 apx->type==RRCONFLICT ||
drhb59499c2002-02-23 18:45:13 +00001120 apy->type==SH_RESOLVED ||
1121 apy->type==RD_RESOLVED ||
drh9892c5d2007-12-21 00:02:11 +00001122 apy->type==SSCONFLICT ||
1123 apy->type==SRCONFLICT ||
1124 apy->type==RRCONFLICT
drhb59499c2002-02-23 18:45:13 +00001125 );
1126 /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
1127 ** REDUCEs on the list. If we reach this point it must be because
1128 ** the parser conflict had already been resolved. */
drh75897232000-05-29 14:26:00 +00001129 }
1130 return errcnt;
1131}
1132/********************* From the file "configlist.c" *************************/
1133/*
1134** Routines to processing a configuration list and building a state
1135** in the LEMON parser generator.
1136*/
1137
1138static struct config *freelist = 0; /* List of free configurations */
1139static struct config *current = 0; /* Top of list of configurations */
1140static struct config **currentend = 0; /* Last on list of configs */
1141static struct config *basis = 0; /* Top of list of basis configs */
1142static struct config **basisend = 0; /* End of list of basis configs */
1143
1144/* Return a pointer to a new configuration */
1145PRIVATE struct config *newconfig(){
icculus9e44cf12010-02-14 17:14:22 +00001146 struct config *newcfg;
drh75897232000-05-29 14:26:00 +00001147 if( freelist==0 ){
1148 int i;
1149 int amt = 3;
drh9892c5d2007-12-21 00:02:11 +00001150 freelist = (struct config *)calloc( amt, sizeof(struct config) );
drh75897232000-05-29 14:26:00 +00001151 if( freelist==0 ){
1152 fprintf(stderr,"Unable to allocate memory for a new configuration.");
1153 exit(1);
1154 }
1155 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
1156 freelist[amt-1].next = 0;
1157 }
icculus9e44cf12010-02-14 17:14:22 +00001158 newcfg = freelist;
drh75897232000-05-29 14:26:00 +00001159 freelist = freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00001160 return newcfg;
drh75897232000-05-29 14:26:00 +00001161}
1162
1163/* The configuration "old" is no longer used */
icculus9e44cf12010-02-14 17:14:22 +00001164PRIVATE void deleteconfig(struct config *old)
drh75897232000-05-29 14:26:00 +00001165{
1166 old->next = freelist;
1167 freelist = old;
1168}
1169
1170/* Initialized the configuration list builder */
1171void Configlist_init(){
1172 current = 0;
1173 currentend = &current;
1174 basis = 0;
1175 basisend = &basis;
1176 Configtable_init();
1177 return;
1178}
1179
1180/* Initialized the configuration list builder */
1181void Configlist_reset(){
1182 current = 0;
1183 currentend = &current;
1184 basis = 0;
1185 basisend = &basis;
1186 Configtable_clear(0);
1187 return;
1188}
1189
1190/* Add another configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001191struct config *Configlist_add(
1192 struct rule *rp, /* The rule */
1193 int dot /* Index into the RHS of the rule where the dot goes */
1194){
drh75897232000-05-29 14:26:00 +00001195 struct config *cfp, model;
1196
1197 assert( currentend!=0 );
1198 model.rp = rp;
1199 model.dot = dot;
1200 cfp = Configtable_find(&model);
1201 if( cfp==0 ){
1202 cfp = newconfig();
1203 cfp->rp = rp;
1204 cfp->dot = dot;
1205 cfp->fws = SetNew();
1206 cfp->stp = 0;
1207 cfp->fplp = cfp->bplp = 0;
1208 cfp->next = 0;
1209 cfp->bp = 0;
1210 *currentend = cfp;
1211 currentend = &cfp->next;
1212 Configtable_insert(cfp);
1213 }
1214 return cfp;
1215}
1216
1217/* Add a basis configuration to the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001218struct config *Configlist_addbasis(struct rule *rp, int dot)
drh75897232000-05-29 14:26:00 +00001219{
1220 struct config *cfp, model;
1221
1222 assert( basisend!=0 );
1223 assert( currentend!=0 );
1224 model.rp = rp;
1225 model.dot = dot;
1226 cfp = Configtable_find(&model);
1227 if( cfp==0 ){
1228 cfp = newconfig();
1229 cfp->rp = rp;
1230 cfp->dot = dot;
1231 cfp->fws = SetNew();
1232 cfp->stp = 0;
1233 cfp->fplp = cfp->bplp = 0;
1234 cfp->next = 0;
1235 cfp->bp = 0;
1236 *currentend = cfp;
1237 currentend = &cfp->next;
1238 *basisend = cfp;
1239 basisend = &cfp->bp;
1240 Configtable_insert(cfp);
1241 }
1242 return cfp;
1243}
1244
1245/* Compute the closure of the configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001246void Configlist_closure(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00001247{
1248 struct config *cfp, *newcfp;
1249 struct rule *rp, *newrp;
1250 struct symbol *sp, *xsp;
1251 int i, dot;
1252
1253 assert( currentend!=0 );
1254 for(cfp=current; cfp; cfp=cfp->next){
1255 rp = cfp->rp;
1256 dot = cfp->dot;
1257 if( dot>=rp->nrhs ) continue;
1258 sp = rp->rhs[dot];
1259 if( sp->type==NONTERMINAL ){
1260 if( sp->rule==0 && sp!=lemp->errsym ){
1261 ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
1262 sp->name);
1263 lemp->errorcnt++;
1264 }
1265 for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
1266 newcfp = Configlist_add(newrp,0);
1267 for(i=dot+1; i<rp->nrhs; i++){
1268 xsp = rp->rhs[i];
1269 if( xsp->type==TERMINAL ){
1270 SetAdd(newcfp->fws,xsp->index);
1271 break;
drhfd405312005-11-06 04:06:59 +00001272 }else if( xsp->type==MULTITERMINAL ){
1273 int k;
1274 for(k=0; k<xsp->nsubsym; k++){
1275 SetAdd(newcfp->fws, xsp->subsym[k]->index);
1276 }
1277 break;
drh75897232000-05-29 14:26:00 +00001278 }else{
1279 SetUnion(newcfp->fws,xsp->firstset);
drhaa9f1122007-08-23 02:50:56 +00001280 if( xsp->lambda==LEMON_FALSE ) break;
drh75897232000-05-29 14:26:00 +00001281 }
1282 }
1283 if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
1284 }
1285 }
1286 }
1287 return;
1288}
1289
1290/* Sort the configuration list */
1291void Configlist_sort(){
drh218dc692004-05-31 23:13:45 +00001292 current = (struct config *)msort((char *)current,(char **)&(current->next),Configcmp);
drh75897232000-05-29 14:26:00 +00001293 currentend = 0;
1294 return;
1295}
1296
1297/* Sort the basis configuration list */
1298void Configlist_sortbasis(){
drh218dc692004-05-31 23:13:45 +00001299 basis = (struct config *)msort((char *)current,(char **)&(current->bp),Configcmp);
drh75897232000-05-29 14:26:00 +00001300 basisend = 0;
1301 return;
1302}
1303
1304/* Return a pointer to the head of the configuration list and
1305** reset the list */
1306struct config *Configlist_return(){
1307 struct config *old;
1308 old = current;
1309 current = 0;
1310 currentend = 0;
1311 return old;
1312}
1313
1314/* Return a pointer to the head of the configuration list and
1315** reset the list */
1316struct config *Configlist_basis(){
1317 struct config *old;
1318 old = basis;
1319 basis = 0;
1320 basisend = 0;
1321 return old;
1322}
1323
1324/* Free all elements of the given configuration list */
icculus9e44cf12010-02-14 17:14:22 +00001325void Configlist_eat(struct config *cfp)
drh75897232000-05-29 14:26:00 +00001326{
1327 struct config *nextcfp;
1328 for(; cfp; cfp=nextcfp){
1329 nextcfp = cfp->next;
1330 assert( cfp->fplp==0 );
1331 assert( cfp->bplp==0 );
1332 if( cfp->fws ) SetFree(cfp->fws);
1333 deleteconfig(cfp);
1334 }
1335 return;
1336}
1337/***************** From the file "error.c" *********************************/
1338/*
1339** Code for printing error message.
1340*/
1341
drhf9a2e7b2003-04-15 01:49:48 +00001342void ErrorMsg(const char *filename, int lineno, const char *format, ...){
icculus15a2cec2010-02-16 16:07:28 +00001343 va_list ap;
icculus1c11f742010-02-15 00:01:04 +00001344 fprintf(stderr, "%s:%d: ", filename, lineno);
1345 va_start(ap, format);
1346 vfprintf(stderr,format,ap);
1347 va_end(ap);
1348 fprintf(stderr, "\n");
drh75897232000-05-29 14:26:00 +00001349}
1350/**************** From the file "main.c" ************************************/
1351/*
1352** Main program file for the LEMON parser generator.
1353*/
1354
1355/* Report an out-of-memory condition and abort. This function
1356** is used mostly by the "MemoryCheck" macro in struct.h
1357*/
1358void memory_error(){
1359 fprintf(stderr,"Out of memory. Aborting...\n");
1360 exit(1);
1361}
1362
drh6d08b4d2004-07-20 12:45:22 +00001363static int nDefine = 0; /* Number of -D options on the command line */
1364static char **azDefine = 0; /* Name of the -D macros */
1365
1366/* This routine is called with the argument to each -D command-line option.
1367** Add the macro defined to the azDefine array.
1368*/
1369static void handle_D_option(char *z){
1370 char **paz;
1371 nDefine++;
icculus9e44cf12010-02-14 17:14:22 +00001372 azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine);
drh6d08b4d2004-07-20 12:45:22 +00001373 if( azDefine==0 ){
1374 fprintf(stderr,"out of memory\n");
1375 exit(1);
1376 }
1377 paz = &azDefine[nDefine-1];
icculus9e44cf12010-02-14 17:14:22 +00001378 *paz = (char *) malloc( lemonStrlen(z)+1 );
drh6d08b4d2004-07-20 12:45:22 +00001379 if( *paz==0 ){
1380 fprintf(stderr,"out of memory\n");
1381 exit(1);
1382 }
1383 strcpy(*paz, z);
1384 for(z=*paz; *z && *z!='='; z++){}
1385 *z = 0;
1386}
1387
icculus3e143bd2010-02-14 00:48:49 +00001388static char *user_templatename = NULL;
1389static void handle_T_option(char *z){
icculus9e44cf12010-02-14 17:14:22 +00001390 user_templatename = (char *) malloc( lemonStrlen(z)+1 );
icculus3e143bd2010-02-14 00:48:49 +00001391 if( user_templatename==0 ){
1392 memory_error();
1393 }
1394 strcpy(user_templatename, z);
1395}
drh75897232000-05-29 14:26:00 +00001396
1397/* The main program. Parse the command line and do it... */
icculus9e44cf12010-02-14 17:14:22 +00001398int main(int argc, char **argv)
drh75897232000-05-29 14:26:00 +00001399{
1400 static int version = 0;
1401 static int rpflag = 0;
1402 static int basisflag = 0;
1403 static int compress = 0;
1404 static int quiet = 0;
1405 static int statistics = 0;
1406 static int mhflag = 0;
shane58543932008-12-10 20:10:04 +00001407 static int nolinenosflag = 0;
drh75897232000-05-29 14:26:00 +00001408 static struct s_options options[] = {
1409 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1410 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
drh6d08b4d2004-07-20 12:45:22 +00001411 {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
icculus3e143bd2010-02-14 00:48:49 +00001412 {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
drh75897232000-05-29 14:26:00 +00001413 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
shane58543932008-12-10 20:10:04 +00001414 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
1415 {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
drh75897232000-05-29 14:26:00 +00001416 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
drh6d08b4d2004-07-20 12:45:22 +00001417 {OPT_FLAG, "s", (char*)&statistics,
1418 "Print parser stats to standard output."},
drh75897232000-05-29 14:26:00 +00001419 {OPT_FLAG, "x", (char*)&version, "Print the version number."},
1420 {OPT_FLAG,0,0,0}
1421 };
1422 int i;
icculus42585cf2010-02-14 05:19:56 +00001423 int exitcode;
drh75897232000-05-29 14:26:00 +00001424 struct lemon lem;
1425
icculusf5ad8242010-02-14 05:34:42 +00001426 atexit(LemonAtExit);
1427
drhb0c86772000-06-02 23:21:26 +00001428 OptInit(argv,options,stderr);
drh75897232000-05-29 14:26:00 +00001429 if( version ){
drhb19a2bc2001-09-16 00:13:26 +00001430 printf("Lemon version 1.0\n");
drh75897232000-05-29 14:26:00 +00001431 exit(0);
1432 }
drhb0c86772000-06-02 23:21:26 +00001433 if( OptNArgs()!=1 ){
drh75897232000-05-29 14:26:00 +00001434 fprintf(stderr,"Exactly one filename argument is required.\n");
1435 exit(1);
1436 }
drh954f6b42006-06-13 13:27:46 +00001437 memset(&lem, 0, sizeof(lem));
drh75897232000-05-29 14:26:00 +00001438 lem.errorcnt = 0;
1439
1440 /* Initialize the machine */
1441 Strsafe_init();
1442 Symbol_init();
1443 State_init();
1444 lem.argv0 = argv[0];
drhb0c86772000-06-02 23:21:26 +00001445 lem.filename = OptArg(0);
drh75897232000-05-29 14:26:00 +00001446 lem.basisflag = basisflag;
shane58543932008-12-10 20:10:04 +00001447 lem.nolinenosflag = nolinenosflag;
drh75897232000-05-29 14:26:00 +00001448 Symbol_new("$");
1449 lem.errsym = Symbol_new("error");
drhc4dd3fd2008-01-22 01:48:05 +00001450 lem.errsym->useCnt = 0;
drh75897232000-05-29 14:26:00 +00001451
1452 /* Parse the input file */
1453 Parse(&lem);
1454 if( lem.errorcnt ) exit(lem.errorcnt);
drh954f6b42006-06-13 13:27:46 +00001455 if( lem.nrule==0 ){
drh75897232000-05-29 14:26:00 +00001456 fprintf(stderr,"Empty grammar.\n");
1457 exit(1);
1458 }
1459
1460 /* Count and index the symbols of the grammar */
1461 lem.nsymbol = Symbol_count();
1462 Symbol_new("{default}");
1463 lem.symbols = Symbol_arrayof();
drh60d31652004-02-22 00:08:04 +00001464 for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
icculus9e44cf12010-02-14 17:14:22 +00001465 qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*), Symbolcmpp);
drh75897232000-05-29 14:26:00 +00001466 for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
1467 for(i=1; isupper(lem.symbols[i]->name[0]); i++);
1468 lem.nterminal = i;
1469
1470 /* Generate a reprint of the grammar, if requested on the command line */
1471 if( rpflag ){
1472 Reprint(&lem);
1473 }else{
1474 /* Initialize the size for all follow and first sets */
drh9892c5d2007-12-21 00:02:11 +00001475 SetSize(lem.nterminal+1);
drh75897232000-05-29 14:26:00 +00001476
1477 /* Find the precedence for every production rule (that has one) */
1478 FindRulePrecedences(&lem);
1479
1480 /* Compute the lambda-nonterminals and the first-sets for every
1481 ** nonterminal */
1482 FindFirstSets(&lem);
1483
1484 /* Compute all LR(0) states. Also record follow-set propagation
1485 ** links so that the follow-set can be computed later */
1486 lem.nstate = 0;
1487 FindStates(&lem);
1488 lem.sorted = State_arrayof();
1489
1490 /* Tie up loose ends on the propagation links */
1491 FindLinks(&lem);
1492
1493 /* Compute the follow set of every reducible configuration */
1494 FindFollowSets(&lem);
1495
1496 /* Compute the action tables */
1497 FindActions(&lem);
1498
1499 /* Compress the action tables */
1500 if( compress==0 ) CompressTables(&lem);
1501
drhada354d2005-11-05 15:03:59 +00001502 /* Reorder and renumber the states so that states with fewer choices
1503 ** occur at the end. */
1504 ResortStates(&lem);
1505
drh75897232000-05-29 14:26:00 +00001506 /* Generate a report of the parser generated. (the "y.output" file) */
1507 if( !quiet ) ReportOutput(&lem);
1508
1509 /* Generate the source code for the parser */
1510 ReportTable(&lem, mhflag);
1511
1512 /* Produce a header file for use by the scanner. (This step is
1513 ** omitted if the "-m" option is used because makeheaders will
1514 ** generate the file for us.) */
1515 if( !mhflag ) ReportHeader(&lem);
1516 }
1517 if( statistics ){
1518 printf("Parser statistics: %d terminals, %d nonterminals, %d rules\n",
1519 lem.nterminal, lem.nsymbol - lem.nterminal, lem.nrule);
1520 printf(" %d states, %d parser table entries, %d conflicts\n",
1521 lem.nstate, lem.tablesize, lem.nconflict);
1522 }
icculus8e158022010-02-16 16:09:03 +00001523 if( lem.nconflict > 0 ){
1524 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
icculus42585cf2010-02-14 05:19:56 +00001525 }
1526
1527 /* return 0 on success, 1 on failure. */
icculus8e158022010-02-16 16:09:03 +00001528 exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
icculusf5ad8242010-02-14 05:34:42 +00001529 successful_exit = (exitcode == 0);
icculus42585cf2010-02-14 05:19:56 +00001530 exit(exitcode);
1531 return (exitcode);
drh75897232000-05-29 14:26:00 +00001532}
1533/******************** From the file "msort.c" *******************************/
1534/*
1535** A generic merge-sort program.
1536**
1537** USAGE:
1538** Let "ptr" be a pointer to some structure which is at the head of
1539** a null-terminated list. Then to sort the list call:
1540**
1541** ptr = msort(ptr,&(ptr->next),cmpfnc);
1542**
1543** In the above, "cmpfnc" is a pointer to a function which compares
1544** two instances of the structure and returns an integer, as in
1545** strcmp. The second argument is a pointer to the pointer to the
1546** second element of the linked list. This address is used to compute
1547** the offset to the "next" field within the structure. The offset to
1548** the "next" field must be constant for all structures in the list.
1549**
1550** The function returns a new pointer which is the head of the list
1551** after sorting.
1552**
1553** ALGORITHM:
1554** Merge-sort.
1555*/
1556
1557/*
1558** Return a pointer to the next structure in the linked list.
1559*/
drhba99af52001-10-25 20:37:16 +00001560#define NEXT(A) (*(char**)(((unsigned long)A)+offset))
drh75897232000-05-29 14:26:00 +00001561
1562/*
1563** Inputs:
1564** a: A sorted, null-terminated linked list. (May be null).
1565** b: A sorted, null-terminated linked list. (May be null).
1566** cmp: A pointer to the comparison function.
1567** offset: Offset in the structure to the "next" field.
1568**
1569** Return Value:
1570** A pointer to the head of a sorted list containing the elements
1571** of both a and b.
1572**
1573** Side effects:
1574** The "next" pointers for elements in the lists a and b are
1575** changed.
1576*/
drhe9278182007-07-18 18:16:29 +00001577static char *merge(
1578 char *a,
1579 char *b,
1580 int (*cmp)(const char*,const char*),
1581 int offset
1582){
drh75897232000-05-29 14:26:00 +00001583 char *ptr, *head;
1584
1585 if( a==0 ){
1586 head = b;
1587 }else if( b==0 ){
1588 head = a;
1589 }else{
drhe594bc32009-11-03 13:02:25 +00001590 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001591 ptr = a;
1592 a = NEXT(a);
1593 }else{
1594 ptr = b;
1595 b = NEXT(b);
1596 }
1597 head = ptr;
1598 while( a && b ){
drhe594bc32009-11-03 13:02:25 +00001599 if( (*cmp)(a,b)<=0 ){
drh75897232000-05-29 14:26:00 +00001600 NEXT(ptr) = a;
1601 ptr = a;
1602 a = NEXT(a);
1603 }else{
1604 NEXT(ptr) = b;
1605 ptr = b;
1606 b = NEXT(b);
1607 }
1608 }
1609 if( a ) NEXT(ptr) = a;
1610 else NEXT(ptr) = b;
1611 }
1612 return head;
1613}
1614
1615/*
1616** Inputs:
1617** list: Pointer to a singly-linked list of structures.
1618** next: Pointer to pointer to the second element of the list.
1619** cmp: A comparison function.
1620**
1621** Return Value:
1622** A pointer to the head of a sorted list containing the elements
1623** orginally in list.
1624**
1625** Side effects:
1626** The "next" pointers for elements in list are changed.
1627*/
1628#define LISTSIZE 30
drhe9278182007-07-18 18:16:29 +00001629static char *msort(
1630 char *list,
1631 char **next,
1632 int (*cmp)(const char*,const char*)
1633){
drhba99af52001-10-25 20:37:16 +00001634 unsigned long offset;
drh75897232000-05-29 14:26:00 +00001635 char *ep;
1636 char *set[LISTSIZE];
1637 int i;
drhba99af52001-10-25 20:37:16 +00001638 offset = (unsigned long)next - (unsigned long)list;
drh75897232000-05-29 14:26:00 +00001639 for(i=0; i<LISTSIZE; i++) set[i] = 0;
1640 while( list ){
1641 ep = list;
1642 list = NEXT(list);
1643 NEXT(ep) = 0;
1644 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1645 ep = merge(ep,set[i],cmp,offset);
1646 set[i] = 0;
1647 }
1648 set[i] = ep;
1649 }
1650 ep = 0;
drhe594bc32009-11-03 13:02:25 +00001651 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset);
drh75897232000-05-29 14:26:00 +00001652 return ep;
1653}
1654/************************ From the file "option.c" **************************/
1655static char **argv;
1656static struct s_options *op;
1657static FILE *errstream;
1658
1659#define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1660
1661/*
1662** Print the command line with a carrot pointing to the k-th character
1663** of the n-th field.
1664*/
icculus9e44cf12010-02-14 17:14:22 +00001665static void errline(int n, int k, FILE *err)
drh75897232000-05-29 14:26:00 +00001666{
1667 int spcnt, i;
drh75897232000-05-29 14:26:00 +00001668 if( argv[0] ) fprintf(err,"%s",argv[0]);
drh87cf1372008-08-13 20:09:06 +00001669 spcnt = lemonStrlen(argv[0]) + 1;
drh75897232000-05-29 14:26:00 +00001670 for(i=1; i<n && argv[i]; i++){
1671 fprintf(err," %s",argv[i]);
drh87cf1372008-08-13 20:09:06 +00001672 spcnt += lemonStrlen(argv[i])+1;
drh75897232000-05-29 14:26:00 +00001673 }
1674 spcnt += k;
1675 for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1676 if( spcnt<20 ){
1677 fprintf(err,"\n%*s^-- here\n",spcnt,"");
1678 }else{
1679 fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1680 }
1681}
1682
1683/*
1684** Return the index of the N-th non-switch argument. Return -1
1685** if N is out of range.
1686*/
icculus9e44cf12010-02-14 17:14:22 +00001687static int argindex(int n)
drh75897232000-05-29 14:26:00 +00001688{
1689 int i;
1690 int dashdash = 0;
1691 if( argv!=0 && *argv!=0 ){
1692 for(i=1; argv[i]; i++){
1693 if( dashdash || !ISOPT(argv[i]) ){
1694 if( n==0 ) return i;
1695 n--;
1696 }
1697 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1698 }
1699 }
1700 return -1;
1701}
1702
1703static char emsg[] = "Command line syntax error: ";
1704
1705/*
1706** Process a flag command line argument.
1707*/
icculus9e44cf12010-02-14 17:14:22 +00001708static int handleflags(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001709{
1710 int v;
1711 int errcnt = 0;
1712 int j;
1713 for(j=0; op[j].label; j++){
drh87cf1372008-08-13 20:09:06 +00001714 if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
drh75897232000-05-29 14:26:00 +00001715 }
1716 v = argv[i][0]=='-' ? 1 : 0;
1717 if( op[j].label==0 ){
1718 if( err ){
1719 fprintf(err,"%sundefined option.\n",emsg);
1720 errline(i,1,err);
1721 }
1722 errcnt++;
1723 }else if( op[j].type==OPT_FLAG ){
1724 *((int*)op[j].arg) = v;
1725 }else if( op[j].type==OPT_FFLAG ){
icculus9e44cf12010-02-14 17:14:22 +00001726 (*(void(*)(int))(op[j].arg))(v);
drh6d08b4d2004-07-20 12:45:22 +00001727 }else if( op[j].type==OPT_FSTR ){
icculus9e44cf12010-02-14 17:14:22 +00001728 (*(void(*)(char *))(op[j].arg))(&argv[i][2]);
drh75897232000-05-29 14:26:00 +00001729 }else{
1730 if( err ){
1731 fprintf(err,"%smissing argument on switch.\n",emsg);
1732 errline(i,1,err);
1733 }
1734 errcnt++;
1735 }
1736 return errcnt;
1737}
1738
1739/*
1740** Process a command line switch which has an argument.
1741*/
icculus9e44cf12010-02-14 17:14:22 +00001742static int handleswitch(int i, FILE *err)
drh75897232000-05-29 14:26:00 +00001743{
1744 int lv = 0;
1745 double dv = 0.0;
1746 char *sv = 0, *end;
1747 char *cp;
1748 int j;
1749 int errcnt = 0;
1750 cp = strchr(argv[i],'=');
drh43617e92006-03-06 20:55:46 +00001751 assert( cp!=0 );
drh75897232000-05-29 14:26:00 +00001752 *cp = 0;
1753 for(j=0; op[j].label; j++){
1754 if( strcmp(argv[i],op[j].label)==0 ) break;
1755 }
1756 *cp = '=';
1757 if( op[j].label==0 ){
1758 if( err ){
1759 fprintf(err,"%sundefined option.\n",emsg);
1760 errline(i,0,err);
1761 }
1762 errcnt++;
1763 }else{
1764 cp++;
1765 switch( op[j].type ){
1766 case OPT_FLAG:
1767 case OPT_FFLAG:
1768 if( err ){
1769 fprintf(err,"%soption requires an argument.\n",emsg);
1770 errline(i,0,err);
1771 }
1772 errcnt++;
1773 break;
1774 case OPT_DBL:
1775 case OPT_FDBL:
1776 dv = strtod(cp,&end);
1777 if( *end ){
1778 if( err ){
1779 fprintf(err,"%sillegal character in floating-point argument.\n",emsg);
drhba99af52001-10-25 20:37:16 +00001780 errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
drh75897232000-05-29 14:26:00 +00001781 }
1782 errcnt++;
1783 }
1784 break;
1785 case OPT_INT:
1786 case OPT_FINT:
1787 lv = strtol(cp,&end,0);
1788 if( *end ){
1789 if( err ){
1790 fprintf(err,"%sillegal character in integer argument.\n",emsg);
drhba99af52001-10-25 20:37:16 +00001791 errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
drh75897232000-05-29 14:26:00 +00001792 }
1793 errcnt++;
1794 }
1795 break;
1796 case OPT_STR:
1797 case OPT_FSTR:
1798 sv = cp;
1799 break;
1800 }
1801 switch( op[j].type ){
1802 case OPT_FLAG:
1803 case OPT_FFLAG:
1804 break;
1805 case OPT_DBL:
1806 *(double*)(op[j].arg) = dv;
1807 break;
1808 case OPT_FDBL:
icculus9e44cf12010-02-14 17:14:22 +00001809 (*(void(*)(double))(op[j].arg))(dv);
drh75897232000-05-29 14:26:00 +00001810 break;
1811 case OPT_INT:
1812 *(int*)(op[j].arg) = lv;
1813 break;
1814 case OPT_FINT:
icculus9e44cf12010-02-14 17:14:22 +00001815 (*(void(*)(int))(op[j].arg))((int)lv);
drh75897232000-05-29 14:26:00 +00001816 break;
1817 case OPT_STR:
1818 *(char**)(op[j].arg) = sv;
1819 break;
1820 case OPT_FSTR:
icculus9e44cf12010-02-14 17:14:22 +00001821 (*(void(*)(char *))(op[j].arg))(sv);
drh75897232000-05-29 14:26:00 +00001822 break;
1823 }
1824 }
1825 return errcnt;
1826}
1827
icculus9e44cf12010-02-14 17:14:22 +00001828int OptInit(char **a, struct s_options *o, FILE *err)
drh75897232000-05-29 14:26:00 +00001829{
1830 int errcnt = 0;
1831 argv = a;
1832 op = o;
1833 errstream = err;
1834 if( argv && *argv && op ){
1835 int i;
1836 for(i=1; argv[i]; i++){
1837 if( argv[i][0]=='+' || argv[i][0]=='-' ){
1838 errcnt += handleflags(i,err);
1839 }else if( strchr(argv[i],'=') ){
1840 errcnt += handleswitch(i,err);
1841 }
1842 }
1843 }
1844 if( errcnt>0 ){
1845 fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
drhb0c86772000-06-02 23:21:26 +00001846 OptPrint();
drh75897232000-05-29 14:26:00 +00001847 exit(1);
1848 }
1849 return 0;
1850}
1851
drhb0c86772000-06-02 23:21:26 +00001852int OptNArgs(){
drh75897232000-05-29 14:26:00 +00001853 int cnt = 0;
1854 int dashdash = 0;
1855 int i;
1856 if( argv!=0 && argv[0]!=0 ){
1857 for(i=1; argv[i]; i++){
1858 if( dashdash || !ISOPT(argv[i]) ) cnt++;
1859 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1860 }
1861 }
1862 return cnt;
1863}
1864
icculus9e44cf12010-02-14 17:14:22 +00001865char *OptArg(int n)
drh75897232000-05-29 14:26:00 +00001866{
1867 int i;
1868 i = argindex(n);
1869 return i>=0 ? argv[i] : 0;
1870}
1871
icculus9e44cf12010-02-14 17:14:22 +00001872void OptErr(int n)
drh75897232000-05-29 14:26:00 +00001873{
1874 int i;
1875 i = argindex(n);
1876 if( i>=0 ) errline(i,0,errstream);
1877}
1878
drhb0c86772000-06-02 23:21:26 +00001879void OptPrint(){
drh75897232000-05-29 14:26:00 +00001880 int i;
1881 int max, len;
1882 max = 0;
1883 for(i=0; op[i].label; i++){
drh87cf1372008-08-13 20:09:06 +00001884 len = lemonStrlen(op[i].label) + 1;
drh75897232000-05-29 14:26:00 +00001885 switch( op[i].type ){
1886 case OPT_FLAG:
1887 case OPT_FFLAG:
1888 break;
1889 case OPT_INT:
1890 case OPT_FINT:
1891 len += 9; /* length of "<integer>" */
1892 break;
1893 case OPT_DBL:
1894 case OPT_FDBL:
1895 len += 6; /* length of "<real>" */
1896 break;
1897 case OPT_STR:
1898 case OPT_FSTR:
1899 len += 8; /* length of "<string>" */
1900 break;
1901 }
1902 if( len>max ) max = len;
1903 }
1904 for(i=0; op[i].label; i++){
1905 switch( op[i].type ){
1906 case OPT_FLAG:
1907 case OPT_FFLAG:
1908 fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
1909 break;
1910 case OPT_INT:
1911 case OPT_FINT:
1912 fprintf(errstream," %s=<integer>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00001913 (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message);
drh75897232000-05-29 14:26:00 +00001914 break;
1915 case OPT_DBL:
1916 case OPT_FDBL:
1917 fprintf(errstream," %s=<real>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00001918 (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message);
drh75897232000-05-29 14:26:00 +00001919 break;
1920 case OPT_STR:
1921 case OPT_FSTR:
1922 fprintf(errstream," %s=<string>%*s %s\n",op[i].label,
drh87cf1372008-08-13 20:09:06 +00001923 (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message);
drh75897232000-05-29 14:26:00 +00001924 break;
1925 }
1926 }
1927}
1928/*********************** From the file "parse.c" ****************************/
1929/*
1930** Input file parser for the LEMON parser generator.
1931*/
1932
1933/* The state of the parser */
icculus9e44cf12010-02-14 17:14:22 +00001934enum e_state {
1935 INITIALIZE,
1936 WAITING_FOR_DECL_OR_RULE,
1937 WAITING_FOR_DECL_KEYWORD,
1938 WAITING_FOR_DECL_ARG,
1939 WAITING_FOR_PRECEDENCE_SYMBOL,
1940 WAITING_FOR_ARROW,
1941 IN_RHS,
1942 LHS_ALIAS_1,
1943 LHS_ALIAS_2,
1944 LHS_ALIAS_3,
1945 RHS_ALIAS_1,
1946 RHS_ALIAS_2,
1947 PRECEDENCE_MARK_1,
1948 PRECEDENCE_MARK_2,
1949 RESYNC_AFTER_RULE_ERROR,
1950 RESYNC_AFTER_DECL_ERROR,
1951 WAITING_FOR_DESTRUCTOR_SYMBOL,
1952 WAITING_FOR_DATATYPE_SYMBOL,
1953 WAITING_FOR_FALLBACK_ID,
icculus9e44cf12010-02-14 17:14:22 +00001954 WAITING_FOR_WILDCARD_ID
1955};
drh75897232000-05-29 14:26:00 +00001956struct pstate {
1957 char *filename; /* Name of the input file */
1958 int tokenlineno; /* Linenumber at which current token starts */
1959 int errorcnt; /* Number of errors so far */
1960 char *tokenstart; /* Text of current token */
1961 struct lemon *gp; /* Global state vector */
icculus9e44cf12010-02-14 17:14:22 +00001962 enum e_state state; /* The state of the parser */
drh0bd1f4e2002-06-06 18:54:39 +00001963 struct symbol *fallback; /* The fallback token */
drh75897232000-05-29 14:26:00 +00001964 struct symbol *lhs; /* Left-hand side of current rule */
icculus9e44cf12010-02-14 17:14:22 +00001965 const char *lhsalias; /* Alias for the LHS */
drh75897232000-05-29 14:26:00 +00001966 int nrhs; /* Number of right-hand side symbols seen */
1967 struct symbol *rhs[MAXRHS]; /* RHS symbols */
icculus9e44cf12010-02-14 17:14:22 +00001968 const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
drh75897232000-05-29 14:26:00 +00001969 struct rule *prevrule; /* Previous rule parsed */
icculus9e44cf12010-02-14 17:14:22 +00001970 const char *declkeyword; /* Keyword of a declaration */
drh75897232000-05-29 14:26:00 +00001971 char **declargslot; /* Where the declaration argument should be put */
drha5808f32008-04-27 22:19:44 +00001972 int insertLineMacro; /* Add #line before declaration insert */
drh4dc8ef52008-07-01 17:13:57 +00001973 int *decllinenoslot; /* Where to write declaration line number */
drh75897232000-05-29 14:26:00 +00001974 enum e_assoc declassoc; /* Assign this association to decl arguments */
1975 int preccounter; /* Assign this precedence to decl arguments */
1976 struct rule *firstrule; /* Pointer to first rule in the grammar */
1977 struct rule *lastrule; /* Pointer to the most recently parsed rule */
1978};
1979
1980/* Parse a single token */
icculus9e44cf12010-02-14 17:14:22 +00001981static void parseonetoken(struct pstate *psp)
drh75897232000-05-29 14:26:00 +00001982{
icculus42585cf2010-02-14 05:19:56 +00001983 char *endptr;
icculus9e44cf12010-02-14 17:14:22 +00001984 const char *x;
drh75897232000-05-29 14:26:00 +00001985 x = Strsafe(psp->tokenstart); /* Save the token permanently */
1986#if 0
1987 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
1988 x,psp->state);
1989#endif
1990 switch( psp->state ){
1991 case INITIALIZE:
1992 psp->prevrule = 0;
1993 psp->preccounter = 0;
1994 psp->firstrule = psp->lastrule = 0;
1995 psp->gp->nrule = 0;
1996 /* Fall thru to next case */
1997 case WAITING_FOR_DECL_OR_RULE:
1998 if( x[0]=='%' ){
1999 psp->state = WAITING_FOR_DECL_KEYWORD;
2000 }else if( islower(x[0]) ){
2001 psp->lhs = Symbol_new(x);
2002 psp->nrhs = 0;
2003 psp->lhsalias = 0;
2004 psp->state = WAITING_FOR_ARROW;
2005 }else if( x[0]=='{' ){
2006 if( psp->prevrule==0 ){
2007 ErrorMsg(psp->filename,psp->tokenlineno,
drh34ff57b2008-07-14 12:27:51 +00002008"There is no prior rule opon which to attach the code \
drh75897232000-05-29 14:26:00 +00002009fragment which begins on this line.");
2010 psp->errorcnt++;
2011 }else if( psp->prevrule->code!=0 ){
2012 ErrorMsg(psp->filename,psp->tokenlineno,
2013"Code fragment beginning on this line is not the first \
2014to follow the previous rule.");
2015 psp->errorcnt++;
2016 }else{
2017 psp->prevrule->line = psp->tokenlineno;
2018 psp->prevrule->code = &x[1];
2019 }
2020 }else if( x[0]=='[' ){
2021 psp->state = PRECEDENCE_MARK_1;
2022 }else{
2023 ErrorMsg(psp->filename,psp->tokenlineno,
2024 "Token \"%s\" should be either \"%%\" or a nonterminal name.",
2025 x);
2026 psp->errorcnt++;
2027 }
2028 break;
2029 case PRECEDENCE_MARK_1:
2030 if( !isupper(x[0]) ){
2031 ErrorMsg(psp->filename,psp->tokenlineno,
2032 "The precedence symbol must be a terminal.");
2033 psp->errorcnt++;
2034 }else if( psp->prevrule==0 ){
2035 ErrorMsg(psp->filename,psp->tokenlineno,
2036 "There is no prior rule to assign precedence \"[%s]\".",x);
2037 psp->errorcnt++;
2038 }else if( psp->prevrule->precsym!=0 ){
2039 ErrorMsg(psp->filename,psp->tokenlineno,
2040"Precedence mark on this line is not the first \
2041to follow the previous rule.");
2042 psp->errorcnt++;
2043 }else{
2044 psp->prevrule->precsym = Symbol_new(x);
2045 }
2046 psp->state = PRECEDENCE_MARK_2;
2047 break;
2048 case PRECEDENCE_MARK_2:
2049 if( x[0]!=']' ){
2050 ErrorMsg(psp->filename,psp->tokenlineno,
2051 "Missing \"]\" on precedence mark.");
2052 psp->errorcnt++;
2053 }
2054 psp->state = WAITING_FOR_DECL_OR_RULE;
2055 break;
2056 case WAITING_FOR_ARROW:
2057 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2058 psp->state = IN_RHS;
2059 }else if( x[0]=='(' ){
2060 psp->state = LHS_ALIAS_1;
2061 }else{
2062 ErrorMsg(psp->filename,psp->tokenlineno,
2063 "Expected to see a \":\" following the LHS symbol \"%s\".",
2064 psp->lhs->name);
2065 psp->errorcnt++;
2066 psp->state = RESYNC_AFTER_RULE_ERROR;
2067 }
2068 break;
2069 case LHS_ALIAS_1:
2070 if( isalpha(x[0]) ){
2071 psp->lhsalias = x;
2072 psp->state = LHS_ALIAS_2;
2073 }else{
2074 ErrorMsg(psp->filename,psp->tokenlineno,
2075 "\"%s\" is not a valid alias for the LHS \"%s\"\n",
2076 x,psp->lhs->name);
2077 psp->errorcnt++;
2078 psp->state = RESYNC_AFTER_RULE_ERROR;
2079 }
2080 break;
2081 case LHS_ALIAS_2:
2082 if( x[0]==')' ){
2083 psp->state = LHS_ALIAS_3;
2084 }else{
2085 ErrorMsg(psp->filename,psp->tokenlineno,
2086 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2087 psp->errorcnt++;
2088 psp->state = RESYNC_AFTER_RULE_ERROR;
2089 }
2090 break;
2091 case LHS_ALIAS_3:
2092 if( x[0]==':' && x[1]==':' && x[2]=='=' ){
2093 psp->state = IN_RHS;
2094 }else{
2095 ErrorMsg(psp->filename,psp->tokenlineno,
2096 "Missing \"->\" following: \"%s(%s)\".",
2097 psp->lhs->name,psp->lhsalias);
2098 psp->errorcnt++;
2099 psp->state = RESYNC_AFTER_RULE_ERROR;
2100 }
2101 break;
2102 case IN_RHS:
2103 if( x[0]=='.' ){
2104 struct rule *rp;
drh9892c5d2007-12-21 00:02:11 +00002105 rp = (struct rule *)calloc( sizeof(struct rule) +
2106 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
drh75897232000-05-29 14:26:00 +00002107 if( rp==0 ){
2108 ErrorMsg(psp->filename,psp->tokenlineno,
2109 "Can't allocate enough memory for this rule.");
2110 psp->errorcnt++;
2111 psp->prevrule = 0;
2112 }else{
2113 int i;
2114 rp->ruleline = psp->tokenlineno;
2115 rp->rhs = (struct symbol**)&rp[1];
icculus9e44cf12010-02-14 17:14:22 +00002116 rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
drh75897232000-05-29 14:26:00 +00002117 for(i=0; i<psp->nrhs; i++){
2118 rp->rhs[i] = psp->rhs[i];
2119 rp->rhsalias[i] = psp->alias[i];
2120 }
2121 rp->lhs = psp->lhs;
2122 rp->lhsalias = psp->lhsalias;
2123 rp->nrhs = psp->nrhs;
2124 rp->code = 0;
2125 rp->precsym = 0;
2126 rp->index = psp->gp->nrule++;
2127 rp->nextlhs = rp->lhs->rule;
2128 rp->lhs->rule = rp;
2129 rp->next = 0;
2130 if( psp->firstrule==0 ){
2131 psp->firstrule = psp->lastrule = rp;
2132 }else{
2133 psp->lastrule->next = rp;
2134 psp->lastrule = rp;
2135 }
2136 psp->prevrule = rp;
2137 }
2138 psp->state = WAITING_FOR_DECL_OR_RULE;
2139 }else if( isalpha(x[0]) ){
2140 if( psp->nrhs>=MAXRHS ){
2141 ErrorMsg(psp->filename,psp->tokenlineno,
drhc4dd3fd2008-01-22 01:48:05 +00002142 "Too many symbols on RHS of rule beginning at \"%s\".",
drh75897232000-05-29 14:26:00 +00002143 x);
2144 psp->errorcnt++;
2145 psp->state = RESYNC_AFTER_RULE_ERROR;
2146 }else{
2147 psp->rhs[psp->nrhs] = Symbol_new(x);
2148 psp->alias[psp->nrhs] = 0;
2149 psp->nrhs++;
2150 }
drhfd405312005-11-06 04:06:59 +00002151 }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
2152 struct symbol *msp = psp->rhs[psp->nrhs-1];
2153 if( msp->type!=MULTITERMINAL ){
2154 struct symbol *origsp = msp;
icculus9e44cf12010-02-14 17:14:22 +00002155 msp = (struct symbol *) calloc(1,sizeof(*msp));
drhfd405312005-11-06 04:06:59 +00002156 memset(msp, 0, sizeof(*msp));
2157 msp->type = MULTITERMINAL;
2158 msp->nsubsym = 1;
icculus9e44cf12010-02-14 17:14:22 +00002159 msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*));
drhfd405312005-11-06 04:06:59 +00002160 msp->subsym[0] = origsp;
2161 msp->name = origsp->name;
2162 psp->rhs[psp->nrhs-1] = msp;
2163 }
2164 msp->nsubsym++;
icculus9e44cf12010-02-14 17:14:22 +00002165 msp->subsym = (struct symbol **) realloc(msp->subsym,
2166 sizeof(struct symbol*)*msp->nsubsym);
drhfd405312005-11-06 04:06:59 +00002167 msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
2168 if( islower(x[1]) || islower(msp->subsym[0]->name[0]) ){
2169 ErrorMsg(psp->filename,psp->tokenlineno,
2170 "Cannot form a compound containing a non-terminal");
2171 psp->errorcnt++;
2172 }
drh75897232000-05-29 14:26:00 +00002173 }else if( x[0]=='(' && psp->nrhs>0 ){
2174 psp->state = RHS_ALIAS_1;
2175 }else{
2176 ErrorMsg(psp->filename,psp->tokenlineno,
2177 "Illegal character on RHS of rule: \"%s\".",x);
2178 psp->errorcnt++;
2179 psp->state = RESYNC_AFTER_RULE_ERROR;
2180 }
2181 break;
2182 case RHS_ALIAS_1:
2183 if( isalpha(x[0]) ){
2184 psp->alias[psp->nrhs-1] = x;
2185 psp->state = RHS_ALIAS_2;
2186 }else{
2187 ErrorMsg(psp->filename,psp->tokenlineno,
2188 "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
2189 x,psp->rhs[psp->nrhs-1]->name);
2190 psp->errorcnt++;
2191 psp->state = RESYNC_AFTER_RULE_ERROR;
2192 }
2193 break;
2194 case RHS_ALIAS_2:
2195 if( x[0]==')' ){
2196 psp->state = IN_RHS;
2197 }else{
2198 ErrorMsg(psp->filename,psp->tokenlineno,
2199 "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
2200 psp->errorcnt++;
2201 psp->state = RESYNC_AFTER_RULE_ERROR;
2202 }
2203 break;
2204 case WAITING_FOR_DECL_KEYWORD:
2205 if( isalpha(x[0]) ){
2206 psp->declkeyword = x;
2207 psp->declargslot = 0;
drh4dc8ef52008-07-01 17:13:57 +00002208 psp->decllinenoslot = 0;
drha5808f32008-04-27 22:19:44 +00002209 psp->insertLineMacro = 1;
drh75897232000-05-29 14:26:00 +00002210 psp->state = WAITING_FOR_DECL_ARG;
2211 if( strcmp(x,"name")==0 ){
2212 psp->declargslot = &(psp->gp->name);
drha5808f32008-04-27 22:19:44 +00002213 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002214 }else if( strcmp(x,"include")==0 ){
2215 psp->declargslot = &(psp->gp->include);
drh75897232000-05-29 14:26:00 +00002216 }else if( strcmp(x,"code")==0 ){
2217 psp->declargslot = &(psp->gp->extracode);
drh75897232000-05-29 14:26:00 +00002218 }else if( strcmp(x,"token_destructor")==0 ){
2219 psp->declargslot = &psp->gp->tokendest;
drh960e8c62001-04-03 16:53:21 +00002220 }else if( strcmp(x,"default_destructor")==0 ){
2221 psp->declargslot = &psp->gp->vardest;
drh75897232000-05-29 14:26:00 +00002222 }else if( strcmp(x,"token_prefix")==0 ){
2223 psp->declargslot = &psp->gp->tokenprefix;
drha5808f32008-04-27 22:19:44 +00002224 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002225 }else if( strcmp(x,"syntax_error")==0 ){
2226 psp->declargslot = &(psp->gp->error);
drh75897232000-05-29 14:26:00 +00002227 }else if( strcmp(x,"parse_accept")==0 ){
2228 psp->declargslot = &(psp->gp->accept);
drh75897232000-05-29 14:26:00 +00002229 }else if( strcmp(x,"parse_failure")==0 ){
2230 psp->declargslot = &(psp->gp->failure);
drh75897232000-05-29 14:26:00 +00002231 }else if( strcmp(x,"stack_overflow")==0 ){
2232 psp->declargslot = &(psp->gp->overflow);
drh75897232000-05-29 14:26:00 +00002233 }else if( strcmp(x,"extra_argument")==0 ){
2234 psp->declargslot = &(psp->gp->arg);
drha5808f32008-04-27 22:19:44 +00002235 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002236 }else if( strcmp(x,"token_type")==0 ){
2237 psp->declargslot = &(psp->gp->tokentype);
drha5808f32008-04-27 22:19:44 +00002238 psp->insertLineMacro = 0;
drh960e8c62001-04-03 16:53:21 +00002239 }else if( strcmp(x,"default_type")==0 ){
2240 psp->declargslot = &(psp->gp->vartype);
drha5808f32008-04-27 22:19:44 +00002241 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002242 }else if( strcmp(x,"stack_size")==0 ){
2243 psp->declargslot = &(psp->gp->stacksize);
drha5808f32008-04-27 22:19:44 +00002244 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002245 }else if( strcmp(x,"start_symbol")==0 ){
2246 psp->declargslot = &(psp->gp->start);
drha5808f32008-04-27 22:19:44 +00002247 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002248 }else if( strcmp(x,"left")==0 ){
2249 psp->preccounter++;
2250 psp->declassoc = LEFT;
2251 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2252 }else if( strcmp(x,"right")==0 ){
2253 psp->preccounter++;
2254 psp->declassoc = RIGHT;
2255 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2256 }else if( strcmp(x,"nonassoc")==0 ){
2257 psp->preccounter++;
2258 psp->declassoc = NONE;
2259 psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
2260 }else if( strcmp(x,"destructor")==0 ){
2261 psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
2262 }else if( strcmp(x,"type")==0 ){
2263 psp->state = WAITING_FOR_DATATYPE_SYMBOL;
drh0bd1f4e2002-06-06 18:54:39 +00002264 }else if( strcmp(x,"fallback")==0 ){
2265 psp->fallback = 0;
2266 psp->state = WAITING_FOR_FALLBACK_ID;
drhe09daa92006-06-10 13:29:31 +00002267 }else if( strcmp(x,"wildcard")==0 ){
2268 psp->state = WAITING_FOR_WILDCARD_ID;
drh75897232000-05-29 14:26:00 +00002269 }else{
2270 ErrorMsg(psp->filename,psp->tokenlineno,
2271 "Unknown declaration keyword: \"%%%s\".",x);
2272 psp->errorcnt++;
2273 psp->state = RESYNC_AFTER_DECL_ERROR;
2274 }
2275 }else{
2276 ErrorMsg(psp->filename,psp->tokenlineno,
2277 "Illegal declaration keyword: \"%s\".",x);
2278 psp->errorcnt++;
2279 psp->state = RESYNC_AFTER_DECL_ERROR;
2280 }
2281 break;
2282 case WAITING_FOR_DESTRUCTOR_SYMBOL:
2283 if( !isalpha(x[0]) ){
2284 ErrorMsg(psp->filename,psp->tokenlineno,
2285 "Symbol name missing after %destructor keyword");
2286 psp->errorcnt++;
2287 psp->state = RESYNC_AFTER_DECL_ERROR;
2288 }else{
2289 struct symbol *sp = Symbol_new(x);
2290 psp->declargslot = &sp->destructor;
drh4dc8ef52008-07-01 17:13:57 +00002291 psp->decllinenoslot = &sp->destLineno;
drha5808f32008-04-27 22:19:44 +00002292 psp->insertLineMacro = 1;
drh75897232000-05-29 14:26:00 +00002293 psp->state = WAITING_FOR_DECL_ARG;
2294 }
2295 break;
2296 case WAITING_FOR_DATATYPE_SYMBOL:
2297 if( !isalpha(x[0]) ){
2298 ErrorMsg(psp->filename,psp->tokenlineno,
2299 "Symbol name missing after %destructor keyword");
2300 psp->errorcnt++;
2301 psp->state = RESYNC_AFTER_DECL_ERROR;
2302 }else{
2303 struct symbol *sp = Symbol_new(x);
2304 psp->declargslot = &sp->datatype;
drha5808f32008-04-27 22:19:44 +00002305 psp->insertLineMacro = 0;
drh75897232000-05-29 14:26:00 +00002306 psp->state = WAITING_FOR_DECL_ARG;
2307 }
2308 break;
2309 case WAITING_FOR_PRECEDENCE_SYMBOL:
2310 if( x[0]=='.' ){
2311 psp->state = WAITING_FOR_DECL_OR_RULE;
2312 }else if( isupper(x[0]) ){
2313 struct symbol *sp;
2314 sp = Symbol_new(x);
2315 if( sp->prec>=0 ){
2316 ErrorMsg(psp->filename,psp->tokenlineno,
2317 "Symbol \"%s\" has already be given a precedence.",x);
2318 psp->errorcnt++;
2319 }else{
2320 sp->prec = psp->preccounter;
2321 sp->assoc = psp->declassoc;
2322 }
2323 }else{
2324 ErrorMsg(psp->filename,psp->tokenlineno,
2325 "Can't assign a precedence to \"%s\".",x);
2326 psp->errorcnt++;
2327 }
2328 break;
2329 case WAITING_FOR_DECL_ARG:
drha5808f32008-04-27 22:19:44 +00002330 if( x[0]=='{' || x[0]=='\"' || isalnum(x[0]) ){
icculus9e44cf12010-02-14 17:14:22 +00002331 const char *zOld, *zNew;
2332 char *zBuf, *z;
drha5808f32008-04-27 22:19:44 +00002333 int nOld, n, nLine, nNew, nBack;
drhb5bd49e2008-07-14 12:21:08 +00002334 int addLineMacro;
drha5808f32008-04-27 22:19:44 +00002335 char zLine[50];
2336 zNew = x;
2337 if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
drh87cf1372008-08-13 20:09:06 +00002338 nNew = lemonStrlen(zNew);
drha5808f32008-04-27 22:19:44 +00002339 if( *psp->declargslot ){
2340 zOld = *psp->declargslot;
2341 }else{
2342 zOld = "";
2343 }
drh87cf1372008-08-13 20:09:06 +00002344 nOld = lemonStrlen(zOld);
drha5808f32008-04-27 22:19:44 +00002345 n = nOld + nNew + 20;
shane58543932008-12-10 20:10:04 +00002346 addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro &&
drhb5bd49e2008-07-14 12:21:08 +00002347 (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
2348 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002349 for(z=psp->filename, nBack=0; *z; z++){
2350 if( *z=='\\' ) nBack++;
2351 }
2352 sprintf(zLine, "#line %d ", psp->tokenlineno);
drh87cf1372008-08-13 20:09:06 +00002353 nLine = lemonStrlen(zLine);
2354 n += nLine + lemonStrlen(psp->filename) + nBack;
drha5808f32008-04-27 22:19:44 +00002355 }
icculus9e44cf12010-02-14 17:14:22 +00002356 *psp->declargslot = (char *) realloc(*psp->declargslot, n);
2357 zBuf = *psp->declargslot + nOld;
drhb5bd49e2008-07-14 12:21:08 +00002358 if( addLineMacro ){
drha5808f32008-04-27 22:19:44 +00002359 if( nOld && zBuf[-1]!='\n' ){
2360 *(zBuf++) = '\n';
2361 }
2362 memcpy(zBuf, zLine, nLine);
2363 zBuf += nLine;
2364 *(zBuf++) = '"';
2365 for(z=psp->filename; *z; z++){
2366 if( *z=='\\' ){
2367 *(zBuf++) = '\\';
2368 }
2369 *(zBuf++) = *z;
2370 }
2371 *(zBuf++) = '"';
2372 *(zBuf++) = '\n';
2373 }
drh4dc8ef52008-07-01 17:13:57 +00002374 if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){
2375 psp->decllinenoslot[0] = psp->tokenlineno;
2376 }
drha5808f32008-04-27 22:19:44 +00002377 memcpy(zBuf, zNew, nNew);
2378 zBuf += nNew;
2379 *zBuf = 0;
2380 psp->state = WAITING_FOR_DECL_OR_RULE;
drh75897232000-05-29 14:26:00 +00002381 }else{
2382 ErrorMsg(psp->filename,psp->tokenlineno,
2383 "Illegal argument to %%%s: %s",psp->declkeyword,x);
2384 psp->errorcnt++;
2385 psp->state = RESYNC_AFTER_DECL_ERROR;
2386 }
2387 break;
drh0bd1f4e2002-06-06 18:54:39 +00002388 case WAITING_FOR_FALLBACK_ID:
2389 if( x[0]=='.' ){
2390 psp->state = WAITING_FOR_DECL_OR_RULE;
2391 }else if( !isupper(x[0]) ){
2392 ErrorMsg(psp->filename, psp->tokenlineno,
2393 "%%fallback argument \"%s\" should be a token", x);
2394 psp->errorcnt++;
2395 }else{
2396 struct symbol *sp = Symbol_new(x);
2397 if( psp->fallback==0 ){
2398 psp->fallback = sp;
2399 }else if( sp->fallback ){
2400 ErrorMsg(psp->filename, psp->tokenlineno,
2401 "More than one fallback assigned to token %s", x);
2402 psp->errorcnt++;
2403 }else{
2404 sp->fallback = psp->fallback;
2405 psp->gp->has_fallback = 1;
2406 }
2407 }
2408 break;
drhe09daa92006-06-10 13:29:31 +00002409 case WAITING_FOR_WILDCARD_ID:
2410 if( x[0]=='.' ){
2411 psp->state = WAITING_FOR_DECL_OR_RULE;
2412 }else if( !isupper(x[0]) ){
2413 ErrorMsg(psp->filename, psp->tokenlineno,
2414 "%%wildcard argument \"%s\" should be a token", x);
2415 psp->errorcnt++;
2416 }else{
2417 struct symbol *sp = Symbol_new(x);
2418 if( psp->gp->wildcard==0 ){
2419 psp->gp->wildcard = sp;
2420 }else{
2421 ErrorMsg(psp->filename, psp->tokenlineno,
2422 "Extra wildcard to token: %s", x);
2423 psp->errorcnt++;
2424 }
2425 }
2426 break;
drh75897232000-05-29 14:26:00 +00002427 case RESYNC_AFTER_RULE_ERROR:
2428/* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2429** break; */
2430 case RESYNC_AFTER_DECL_ERROR:
2431 if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
2432 if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
2433 break;
2434 }
2435}
2436
drh34ff57b2008-07-14 12:27:51 +00002437/* Run the preprocessor over the input file text. The global variables
drh6d08b4d2004-07-20 12:45:22 +00002438** azDefine[0] through azDefine[nDefine-1] contains the names of all defined
2439** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and
2440** comments them out. Text in between is also commented out as appropriate.
2441*/
danielk1977940fac92005-01-23 22:41:37 +00002442static void preprocess_input(char *z){
drh6d08b4d2004-07-20 12:45:22 +00002443 int i, j, k, n;
2444 int exclude = 0;
rse38514a92007-09-20 11:34:17 +00002445 int start = 0;
drh6d08b4d2004-07-20 12:45:22 +00002446 int lineno = 1;
rse38514a92007-09-20 11:34:17 +00002447 int start_lineno = 1;
drh6d08b4d2004-07-20 12:45:22 +00002448 for(i=0; z[i]; i++){
2449 if( z[i]=='\n' ) lineno++;
2450 if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue;
2451 if( strncmp(&z[i],"%endif",6)==0 && isspace(z[i+6]) ){
2452 if( exclude ){
2453 exclude--;
2454 if( exclude==0 ){
2455 for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
2456 }
2457 }
2458 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2459 }else if( (strncmp(&z[i],"%ifdef",6)==0 && isspace(z[i+6]))
2460 || (strncmp(&z[i],"%ifndef",7)==0 && isspace(z[i+7])) ){
2461 if( exclude ){
2462 exclude++;
2463 }else{
2464 for(j=i+7; isspace(z[j]); j++){}
2465 for(n=0; z[j+n] && !isspace(z[j+n]); n++){}
2466 exclude = 1;
2467 for(k=0; k<nDefine; k++){
drh87cf1372008-08-13 20:09:06 +00002468 if( strncmp(azDefine[k],&z[j],n)==0 && lemonStrlen(azDefine[k])==n ){
drh6d08b4d2004-07-20 12:45:22 +00002469 exclude = 0;
2470 break;
2471 }
2472 }
2473 if( z[i+3]=='n' ) exclude = !exclude;
2474 if( exclude ){
2475 start = i;
2476 start_lineno = lineno;
2477 }
2478 }
2479 for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
2480 }
2481 }
2482 if( exclude ){
2483 fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
2484 exit(1);
2485 }
2486}
2487
drh75897232000-05-29 14:26:00 +00002488/* In spite of its name, this function is really a scanner. It read
2489** in the entire input file (all at once) then tokenizes it. Each
2490** token is passed to the function "parseonetoken" which builds all
2491** the appropriate data structures in the global state vector "gp".
2492*/
icculus9e44cf12010-02-14 17:14:22 +00002493void Parse(struct lemon *gp)
drh75897232000-05-29 14:26:00 +00002494{
2495 struct pstate ps;
2496 FILE *fp;
2497 char *filebuf;
2498 int filesize;
2499 int lineno;
2500 int c;
2501 char *cp, *nextcp;
2502 int startline = 0;
2503
rse38514a92007-09-20 11:34:17 +00002504 memset(&ps, '\0', sizeof(ps));
drh75897232000-05-29 14:26:00 +00002505 ps.gp = gp;
2506 ps.filename = gp->filename;
2507 ps.errorcnt = 0;
2508 ps.state = INITIALIZE;
2509
2510 /* Begin by reading the input file */
2511 fp = fopen(ps.filename,"rb");
2512 if( fp==0 ){
2513 ErrorMsg(ps.filename,0,"Can't open this file for reading.");
2514 gp->errorcnt++;
2515 return;
2516 }
2517 fseek(fp,0,2);
2518 filesize = ftell(fp);
2519 rewind(fp);
2520 filebuf = (char *)malloc( filesize+1 );
2521 if( filebuf==0 ){
2522 ErrorMsg(ps.filename,0,"Can't allocate %d of memory to hold this file.",
2523 filesize+1);
2524 gp->errorcnt++;
2525 return;
2526 }
2527 if( fread(filebuf,1,filesize,fp)!=filesize ){
2528 ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
2529 filesize);
2530 free(filebuf);
2531 gp->errorcnt++;
2532 return;
2533 }
2534 fclose(fp);
2535 filebuf[filesize] = 0;
2536
drh6d08b4d2004-07-20 12:45:22 +00002537 /* Make an initial pass through the file to handle %ifdef and %ifndef */
2538 preprocess_input(filebuf);
2539
drh75897232000-05-29 14:26:00 +00002540 /* Now scan the text of the input file */
2541 lineno = 1;
2542 for(cp=filebuf; (c= *cp)!=0; ){
2543 if( c=='\n' ) lineno++; /* Keep track of the line number */
2544 if( isspace(c) ){ cp++; continue; } /* Skip all white space */
2545 if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
2546 cp+=2;
2547 while( (c= *cp)!=0 && c!='\n' ) cp++;
2548 continue;
2549 }
2550 if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
2551 cp+=2;
2552 while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
2553 if( c=='\n' ) lineno++;
2554 cp++;
2555 }
2556 if( c ) cp++;
2557 continue;
2558 }
2559 ps.tokenstart = cp; /* Mark the beginning of the token */
2560 ps.tokenlineno = lineno; /* Linenumber on which token begins */
2561 if( c=='\"' ){ /* String literals */
2562 cp++;
2563 while( (c= *cp)!=0 && c!='\"' ){
2564 if( c=='\n' ) lineno++;
2565 cp++;
2566 }
2567 if( c==0 ){
2568 ErrorMsg(ps.filename,startline,
2569"String starting on this line is not terminated before the end of the file.");
2570 ps.errorcnt++;
2571 nextcp = cp;
2572 }else{
2573 nextcp = cp+1;
2574 }
2575 }else if( c=='{' ){ /* A block of C code */
2576 int level;
2577 cp++;
2578 for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
2579 if( c=='\n' ) lineno++;
2580 else if( c=='{' ) level++;
2581 else if( c=='}' ) level--;
2582 else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
2583 int prevc;
2584 cp = &cp[2];
2585 prevc = 0;
2586 while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
2587 if( c=='\n' ) lineno++;
2588 prevc = c;
2589 cp++;
2590 }
2591 }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
2592 cp = &cp[2];
2593 while( (c= *cp)!=0 && c!='\n' ) cp++;
2594 if( c ) lineno++;
2595 }else if( c=='\'' || c=='\"' ){ /* String a character literals */
2596 int startchar, prevc;
2597 startchar = c;
2598 prevc = 0;
2599 for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
2600 if( c=='\n' ) lineno++;
2601 if( prevc=='\\' ) prevc = 0;
2602 else prevc = c;
2603 }
2604 }
2605 }
2606 if( c==0 ){
drh960e8c62001-04-03 16:53:21 +00002607 ErrorMsg(ps.filename,ps.tokenlineno,
drh75897232000-05-29 14:26:00 +00002608"C code starting on this line is not terminated before the end of the file.");
2609 ps.errorcnt++;
2610 nextcp = cp;
2611 }else{
2612 nextcp = cp+1;
2613 }
2614 }else if( isalnum(c) ){ /* Identifiers */
2615 while( (c= *cp)!=0 && (isalnum(c) || c=='_') ) cp++;
2616 nextcp = cp;
2617 }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
2618 cp += 3;
2619 nextcp = cp;
drhfd405312005-11-06 04:06:59 +00002620 }else if( (c=='/' || c=='|') && isalpha(cp[1]) ){
2621 cp += 2;
2622 while( (c = *cp)!=0 && (isalnum(c) || c=='_') ) cp++;
2623 nextcp = cp;
drh75897232000-05-29 14:26:00 +00002624 }else{ /* All other (one character) operators */
2625 cp++;
2626 nextcp = cp;
2627 }
2628 c = *cp;
2629 *cp = 0; /* Null terminate the token */
2630 parseonetoken(&ps); /* Parse the token */
2631 *cp = c; /* Restore the buffer */
2632 cp = nextcp;
2633 }
2634 free(filebuf); /* Release the buffer after parsing */
2635 gp->rule = ps.firstrule;
2636 gp->errorcnt = ps.errorcnt;
2637}
2638/*************************** From the file "plink.c" *********************/
2639/*
2640** Routines processing configuration follow-set propagation links
2641** in the LEMON parser generator.
2642*/
2643static struct plink *plink_freelist = 0;
2644
2645/* Allocate a new plink */
2646struct plink *Plink_new(){
icculus9e44cf12010-02-14 17:14:22 +00002647 struct plink *newlink;
drh75897232000-05-29 14:26:00 +00002648
2649 if( plink_freelist==0 ){
2650 int i;
2651 int amt = 100;
drh9892c5d2007-12-21 00:02:11 +00002652 plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
drh75897232000-05-29 14:26:00 +00002653 if( plink_freelist==0 ){
2654 fprintf(stderr,
2655 "Unable to allocate memory for a new follow-set propagation link.\n");
2656 exit(1);
2657 }
2658 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2659 plink_freelist[amt-1].next = 0;
2660 }
icculus9e44cf12010-02-14 17:14:22 +00002661 newlink = plink_freelist;
drh75897232000-05-29 14:26:00 +00002662 plink_freelist = plink_freelist->next;
icculus9e44cf12010-02-14 17:14:22 +00002663 return newlink;
drh75897232000-05-29 14:26:00 +00002664}
2665
2666/* Add a plink to a plink list */
icculus9e44cf12010-02-14 17:14:22 +00002667void Plink_add(struct plink **plpp, struct config *cfp)
drh75897232000-05-29 14:26:00 +00002668{
icculus9e44cf12010-02-14 17:14:22 +00002669 struct plink *newlink;
2670 newlink = Plink_new();
2671 newlink->next = *plpp;
2672 *plpp = newlink;
2673 newlink->cfp = cfp;
drh75897232000-05-29 14:26:00 +00002674}
2675
2676/* Transfer every plink on the list "from" to the list "to" */
icculus9e44cf12010-02-14 17:14:22 +00002677void Plink_copy(struct plink **to, struct plink *from)
drh75897232000-05-29 14:26:00 +00002678{
2679 struct plink *nextpl;
2680 while( from ){
2681 nextpl = from->next;
2682 from->next = *to;
2683 *to = from;
2684 from = nextpl;
2685 }
2686}
2687
2688/* Delete every plink on the list */
icculus9e44cf12010-02-14 17:14:22 +00002689void Plink_delete(struct plink *plp)
drh75897232000-05-29 14:26:00 +00002690{
2691 struct plink *nextpl;
2692
2693 while( plp ){
2694 nextpl = plp->next;
2695 plp->next = plink_freelist;
2696 plink_freelist = plp;
2697 plp = nextpl;
2698 }
2699}
2700/*********************** From the file "report.c" **************************/
2701/*
2702** Procedures for generating reports and tables in the LEMON parser generator.
2703*/
2704
2705/* Generate a filename with the given suffix. Space to hold the
2706** name comes from malloc() and must be freed by the calling
2707** function.
2708*/
icculus9e44cf12010-02-14 17:14:22 +00002709PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
drh75897232000-05-29 14:26:00 +00002710{
2711 char *name;
2712 char *cp;
2713
icculus9e44cf12010-02-14 17:14:22 +00002714 name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
drh75897232000-05-29 14:26:00 +00002715 if( name==0 ){
2716 fprintf(stderr,"Can't allocate space for a filename.\n");
2717 exit(1);
2718 }
2719 strcpy(name,lemp->filename);
2720 cp = strrchr(name,'.');
2721 if( cp ) *cp = 0;
2722 strcat(name,suffix);
2723 return name;
2724}
2725
2726/* Open a file with a name based on the name of the input file,
2727** but with a different (specified) suffix, and return a pointer
2728** to the stream */
icculus9e44cf12010-02-14 17:14:22 +00002729PRIVATE FILE *file_open(
2730 struct lemon *lemp,
2731 const char *suffix,
2732 const char *mode
2733){
drh75897232000-05-29 14:26:00 +00002734 FILE *fp;
2735
2736 if( lemp->outname ) free(lemp->outname);
2737 lemp->outname = file_makename(lemp, suffix);
2738 fp = fopen(lemp->outname,mode);
2739 if( fp==0 && *mode=='w' ){
2740 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
2741 lemp->errorcnt++;
2742 return 0;
2743 }
icculusf5ad8242010-02-14 05:34:42 +00002744
2745 /* Add files we create to a list, so we can delete them if we fail. This
2746 ** is to keep makefiles from getting confused. We don't include .out files,
2747 ** though: this is debug information, and you don't want it deleted if there
2748 ** was an error you need to track down.
2749 */
2750 if(( *mode=='w' ) && (strcmp(suffix, ".out") != 0)){
2751 const char **ptr = (const char **)
2752 realloc(made_files, sizeof (const char **) * (made_files_count + 1));
2753 char *fname = strdup(lemp->outname);
2754 if ((ptr == NULL) || (fname == NULL)) {
2755 free(ptr);
2756 free(fname);
2757 memory_error();
2758 }
2759 made_files = ptr;
2760 made_files[made_files_count++] = fname;
2761 }
drh75897232000-05-29 14:26:00 +00002762 return fp;
2763}
2764
2765/* Duplicate the input file without comments and without actions
2766** on rules */
icculus9e44cf12010-02-14 17:14:22 +00002767void Reprint(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00002768{
2769 struct rule *rp;
2770 struct symbol *sp;
2771 int i, j, maxlen, len, ncolumns, skip;
2772 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
2773 maxlen = 10;
2774 for(i=0; i<lemp->nsymbol; i++){
2775 sp = lemp->symbols[i];
drh87cf1372008-08-13 20:09:06 +00002776 len = lemonStrlen(sp->name);
drh75897232000-05-29 14:26:00 +00002777 if( len>maxlen ) maxlen = len;
2778 }
2779 ncolumns = 76/(maxlen+5);
2780 if( ncolumns<1 ) ncolumns = 1;
2781 skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
2782 for(i=0; i<skip; i++){
2783 printf("//");
2784 for(j=i; j<lemp->nsymbol; j+=skip){
2785 sp = lemp->symbols[j];
2786 assert( sp->index==j );
2787 printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
2788 }
2789 printf("\n");
2790 }
2791 for(rp=lemp->rule; rp; rp=rp->next){
2792 printf("%s",rp->lhs->name);
drhfd405312005-11-06 04:06:59 +00002793 /* if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
drh75897232000-05-29 14:26:00 +00002794 printf(" ::=");
2795 for(i=0; i<rp->nrhs; i++){
drhfd405312005-11-06 04:06:59 +00002796 sp = rp->rhs[i];
2797 printf(" %s", sp->name);
2798 if( sp->type==MULTITERMINAL ){
2799 for(j=1; j<sp->nsubsym; j++){
2800 printf("|%s", sp->subsym[j]->name);
2801 }
2802 }
2803 /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
drh75897232000-05-29 14:26:00 +00002804 }
2805 printf(".");
2806 if( rp->precsym ) printf(" [%s]",rp->precsym->name);
drhfd405312005-11-06 04:06:59 +00002807 /* if( rp->code ) printf("\n %s",rp->code); */
drh75897232000-05-29 14:26:00 +00002808 printf("\n");
2809 }
2810}
2811
icculus9e44cf12010-02-14 17:14:22 +00002812void ConfigPrint(FILE *fp, struct config *cfp)
drh75897232000-05-29 14:26:00 +00002813{
2814 struct rule *rp;
drhfd405312005-11-06 04:06:59 +00002815 struct symbol *sp;
2816 int i, j;
drh75897232000-05-29 14:26:00 +00002817 rp = cfp->rp;
2818 fprintf(fp,"%s ::=",rp->lhs->name);
2819 for(i=0; i<=rp->nrhs; i++){
2820 if( i==cfp->dot ) fprintf(fp," *");
2821 if( i==rp->nrhs ) break;
drhfd405312005-11-06 04:06:59 +00002822 sp = rp->rhs[i];
2823 fprintf(fp," %s", sp->name);
2824 if( sp->type==MULTITERMINAL ){
2825 for(j=1; j<sp->nsubsym; j++){
2826 fprintf(fp,"|%s",sp->subsym[j]->name);
2827 }
2828 }
drh75897232000-05-29 14:26:00 +00002829 }
2830}
2831
2832/* #define TEST */
drhfd405312005-11-06 04:06:59 +00002833#if 0
drh75897232000-05-29 14:26:00 +00002834/* Print a set */
2835PRIVATE void SetPrint(out,set,lemp)
2836FILE *out;
2837char *set;
2838struct lemon *lemp;
2839{
2840 int i;
2841 char *spacer;
2842 spacer = "";
2843 fprintf(out,"%12s[","");
2844 for(i=0; i<lemp->nterminal; i++){
2845 if( SetFind(set,i) ){
2846 fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
2847 spacer = " ";
2848 }
2849 }
2850 fprintf(out,"]\n");
2851}
2852
2853/* Print a plink chain */
2854PRIVATE void PlinkPrint(out,plp,tag)
2855FILE *out;
2856struct plink *plp;
2857char *tag;
2858{
2859 while( plp ){
drhada354d2005-11-05 15:03:59 +00002860 fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum);
drh75897232000-05-29 14:26:00 +00002861 ConfigPrint(out,plp->cfp);
2862 fprintf(out,"\n");
2863 plp = plp->next;
2864 }
2865}
2866#endif
2867
2868/* Print an action to the given file descriptor. Return FALSE if
2869** nothing was actually printed.
2870*/
2871int PrintAction(struct action *ap, FILE *fp, int indent){
2872 int result = 1;
2873 switch( ap->type ){
2874 case SHIFT:
drhada354d2005-11-05 15:03:59 +00002875 fprintf(fp,"%*s shift %d",indent,ap->sp->name,ap->x.stp->statenum);
drh75897232000-05-29 14:26:00 +00002876 break;
2877 case REDUCE:
2878 fprintf(fp,"%*s reduce %d",indent,ap->sp->name,ap->x.rp->index);
2879 break;
2880 case ACCEPT:
2881 fprintf(fp,"%*s accept",indent,ap->sp->name);
2882 break;
2883 case ERROR:
2884 fprintf(fp,"%*s error",indent,ap->sp->name);
2885 break;
drh9892c5d2007-12-21 00:02:11 +00002886 case SRCONFLICT:
2887 case RRCONFLICT:
drh75897232000-05-29 14:26:00 +00002888 fprintf(fp,"%*s reduce %-3d ** Parsing conflict **",
2889 indent,ap->sp->name,ap->x.rp->index);
2890 break;
drh9892c5d2007-12-21 00:02:11 +00002891 case SSCONFLICT:
2892 fprintf(fp,"%*s shift %d ** Parsing conflict **",
2893 indent,ap->sp->name,ap->x.stp->statenum);
2894 break;
drh75897232000-05-29 14:26:00 +00002895 case SH_RESOLVED:
2896 case RD_RESOLVED:
2897 case NOT_USED:
2898 result = 0;
2899 break;
2900 }
2901 return result;
2902}
2903
2904/* Generate the "y.output" log file */
icculus9e44cf12010-02-14 17:14:22 +00002905void ReportOutput(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00002906{
2907 int i;
2908 struct state *stp;
2909 struct config *cfp;
2910 struct action *ap;
2911 FILE *fp;
2912
drh2aa6ca42004-09-10 00:14:04 +00002913 fp = file_open(lemp,".out","wb");
drh75897232000-05-29 14:26:00 +00002914 if( fp==0 ) return;
drh75897232000-05-29 14:26:00 +00002915 for(i=0; i<lemp->nstate; i++){
2916 stp = lemp->sorted[i];
drhada354d2005-11-05 15:03:59 +00002917 fprintf(fp,"State %d:\n",stp->statenum);
drh75897232000-05-29 14:26:00 +00002918 if( lemp->basisflag ) cfp=stp->bp;
2919 else cfp=stp->cfp;
2920 while( cfp ){
2921 char buf[20];
2922 if( cfp->dot==cfp->rp->nrhs ){
2923 sprintf(buf,"(%d)",cfp->rp->index);
2924 fprintf(fp," %5s ",buf);
2925 }else{
2926 fprintf(fp," ");
2927 }
2928 ConfigPrint(fp,cfp);
2929 fprintf(fp,"\n");
drhfd405312005-11-06 04:06:59 +00002930#if 0
drh75897232000-05-29 14:26:00 +00002931 SetPrint(fp,cfp->fws,lemp);
2932 PlinkPrint(fp,cfp->fplp,"To ");
2933 PlinkPrint(fp,cfp->bplp,"From");
2934#endif
2935 if( lemp->basisflag ) cfp=cfp->bp;
2936 else cfp=cfp->next;
2937 }
2938 fprintf(fp,"\n");
2939 for(ap=stp->ap; ap; ap=ap->next){
2940 if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
2941 }
2942 fprintf(fp,"\n");
2943 }
drhe9278182007-07-18 18:16:29 +00002944 fprintf(fp, "----------------------------------------------------\n");
2945 fprintf(fp, "Symbols:\n");
2946 for(i=0; i<lemp->nsymbol; i++){
2947 int j;
2948 struct symbol *sp;
2949
2950 sp = lemp->symbols[i];
2951 fprintf(fp, " %3d: %s", i, sp->name);
2952 if( sp->type==NONTERMINAL ){
2953 fprintf(fp, ":");
2954 if( sp->lambda ){
2955 fprintf(fp, " <lambda>");
2956 }
2957 for(j=0; j<lemp->nterminal; j++){
2958 if( sp->firstset && SetFind(sp->firstset, j) ){
2959 fprintf(fp, " %s", lemp->symbols[j]->name);
2960 }
2961 }
2962 }
2963 fprintf(fp, "\n");
2964 }
drh75897232000-05-29 14:26:00 +00002965 fclose(fp);
2966 return;
2967}
2968
2969/* Search for the file "name" which is in the same directory as
2970** the exacutable */
icculus9e44cf12010-02-14 17:14:22 +00002971PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
drh75897232000-05-29 14:26:00 +00002972{
icculus9e44cf12010-02-14 17:14:22 +00002973 const char *pathlist;
2974 char *pathbufptr;
2975 char *pathbuf;
drh75897232000-05-29 14:26:00 +00002976 char *path,*cp;
2977 char c;
drh75897232000-05-29 14:26:00 +00002978
2979#ifdef __WIN32__
2980 cp = strrchr(argv0,'\\');
2981#else
2982 cp = strrchr(argv0,'/');
2983#endif
2984 if( cp ){
2985 c = *cp;
2986 *cp = 0;
drh87cf1372008-08-13 20:09:06 +00002987 path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
drh75897232000-05-29 14:26:00 +00002988 if( path ) sprintf(path,"%s/%s",argv0,name);
2989 *cp = c;
2990 }else{
drh75897232000-05-29 14:26:00 +00002991 pathlist = getenv("PATH");
2992 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
icculus9e44cf12010-02-14 17:14:22 +00002993 pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
drh87cf1372008-08-13 20:09:06 +00002994 path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
icculus9e44cf12010-02-14 17:14:22 +00002995 if( (pathbuf != 0) && (path!=0) ){
2996 pathbufptr = pathbuf;
2997 strcpy(pathbuf, pathlist);
2998 while( *pathbuf ){
2999 cp = strchr(pathbuf,':');
3000 if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
drh75897232000-05-29 14:26:00 +00003001 c = *cp;
3002 *cp = 0;
icculus9e44cf12010-02-14 17:14:22 +00003003 sprintf(path,"%s/%s",pathbuf,name);
drh75897232000-05-29 14:26:00 +00003004 *cp = c;
icculus9e44cf12010-02-14 17:14:22 +00003005 if( c==0 ) pathbuf[0] = 0;
3006 else pathbuf = &cp[1];
drh75897232000-05-29 14:26:00 +00003007 if( access(path,modemask)==0 ) break;
3008 }
icculus9e44cf12010-02-14 17:14:22 +00003009 free(pathbufptr);
drh75897232000-05-29 14:26:00 +00003010 }
3011 }
3012 return path;
3013}
3014
3015/* Given an action, compute the integer value for that action
3016** which is to be put in the action table of the generated machine.
3017** Return negative if no action should be generated.
3018*/
icculus9e44cf12010-02-14 17:14:22 +00003019PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
drh75897232000-05-29 14:26:00 +00003020{
3021 int act;
3022 switch( ap->type ){
drhada354d2005-11-05 15:03:59 +00003023 case SHIFT: act = ap->x.stp->statenum; break;
drh75897232000-05-29 14:26:00 +00003024 case REDUCE: act = ap->x.rp->index + lemp->nstate; break;
3025 case ERROR: act = lemp->nstate + lemp->nrule; break;
3026 case ACCEPT: act = lemp->nstate + lemp->nrule + 1; break;
3027 default: act = -1; break;
3028 }
3029 return act;
3030}
3031
3032#define LINESIZE 1000
3033/* The next cluster of routines are for reading the template file
3034** and writing the results to the generated parser */
3035/* The first function transfers data from "in" to "out" until
3036** a line is seen which begins with "%%". The line number is
3037** tracked.
3038**
3039** if name!=0, then any word that begin with "Parse" is changed to
3040** begin with *name instead.
3041*/
icculus9e44cf12010-02-14 17:14:22 +00003042PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
drh75897232000-05-29 14:26:00 +00003043{
3044 int i, iStart;
3045 char line[LINESIZE];
3046 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
3047 (*lineno)++;
3048 iStart = 0;
3049 if( name ){
3050 for(i=0; line[i]; i++){
3051 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
3052 && (i==0 || !isalpha(line[i-1]))
3053 ){
3054 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
3055 fprintf(out,"%s",name);
3056 i += 4;
3057 iStart = i+1;
3058 }
3059 }
3060 }
3061 fprintf(out,"%s",&line[iStart]);
3062 }
3063}
3064
3065/* The next function finds the template file and opens it, returning
3066** a pointer to the opened file. */
icculus9e44cf12010-02-14 17:14:22 +00003067PRIVATE FILE *tplt_open(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003068{
3069 static char templatename[] = "lempar.c";
3070 char buf[1000];
3071 FILE *in;
3072 char *tpltname;
3073 char *cp;
3074
icculus3e143bd2010-02-14 00:48:49 +00003075 /* first, see if user specified a template filename on the command line. */
3076 if (user_templatename != 0) {
3077 if( access(user_templatename,004)==-1 ){
3078 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3079 user_templatename);
3080 lemp->errorcnt++;
3081 return 0;
3082 }
3083 in = fopen(user_templatename,"rb");
3084 if( in==0 ){
3085 fprintf(stderr,"Can't open the template file \"%s\".\n",user_templatename);
3086 lemp->errorcnt++;
3087 return 0;
3088 }
3089 return in;
3090 }
3091
drh75897232000-05-29 14:26:00 +00003092 cp = strrchr(lemp->filename,'.');
3093 if( cp ){
drh8b582012003-10-21 13:16:03 +00003094 sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
drh75897232000-05-29 14:26:00 +00003095 }else{
3096 sprintf(buf,"%s.lt",lemp->filename);
3097 }
3098 if( access(buf,004)==0 ){
3099 tpltname = buf;
drh960e8c62001-04-03 16:53:21 +00003100 }else if( access(templatename,004)==0 ){
3101 tpltname = templatename;
drh75897232000-05-29 14:26:00 +00003102 }else{
3103 tpltname = pathsearch(lemp->argv0,templatename,0);
3104 }
3105 if( tpltname==0 ){
3106 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3107 templatename);
3108 lemp->errorcnt++;
3109 return 0;
3110 }
drh2aa6ca42004-09-10 00:14:04 +00003111 in = fopen(tpltname,"rb");
drh75897232000-05-29 14:26:00 +00003112 if( in==0 ){
3113 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
3114 lemp->errorcnt++;
3115 return 0;
3116 }
3117 return in;
3118}
3119
drhaf805ca2004-09-07 11:28:25 +00003120/* Print a #line directive line to the output file. */
icculus9e44cf12010-02-14 17:14:22 +00003121PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
drhaf805ca2004-09-07 11:28:25 +00003122{
3123 fprintf(out,"#line %d \"",lineno);
3124 while( *filename ){
3125 if( *filename == '\\' ) putc('\\',out);
3126 putc(*filename,out);
3127 filename++;
3128 }
3129 fprintf(out,"\"\n");
3130}
3131
drh75897232000-05-29 14:26:00 +00003132/* Print a string to the file and keep the linenumber up to date */
icculus9e44cf12010-02-14 17:14:22 +00003133PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
drh75897232000-05-29 14:26:00 +00003134{
3135 if( str==0 ) return;
drh75897232000-05-29 14:26:00 +00003136 while( *str ){
drh75897232000-05-29 14:26:00 +00003137 putc(*str,out);
shane58543932008-12-10 20:10:04 +00003138 if( *str=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003139 str++;
3140 }
drh9db55df2004-09-09 14:01:21 +00003141 if( str[-1]!='\n' ){
3142 putc('\n',out);
3143 (*lineno)++;
3144 }
shane58543932008-12-10 20:10:04 +00003145 if (!lemp->nolinenosflag) {
3146 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3147 }
drh75897232000-05-29 14:26:00 +00003148 return;
3149}
3150
3151/*
3152** The following routine emits code for the destructor for the
3153** symbol sp
3154*/
icculus9e44cf12010-02-14 17:14:22 +00003155void emit_destructor_code(
3156 FILE *out,
3157 struct symbol *sp,
3158 struct lemon *lemp,
3159 int *lineno
3160){
drhcc83b6e2004-04-23 23:38:42 +00003161 char *cp = 0;
drh75897232000-05-29 14:26:00 +00003162
drh75897232000-05-29 14:26:00 +00003163 if( sp->type==TERMINAL ){
3164 cp = lemp->tokendest;
3165 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003166 fprintf(out,"{\n"); (*lineno)++;
drh960e8c62001-04-03 16:53:21 +00003167 }else if( sp->destructor ){
drh75897232000-05-29 14:26:00 +00003168 cp = sp->destructor;
drha5808f32008-04-27 22:19:44 +00003169 fprintf(out,"{\n"); (*lineno)++;
shane58543932008-12-10 20:10:04 +00003170 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,sp->destLineno,lemp->filename); }
drh960e8c62001-04-03 16:53:21 +00003171 }else if( lemp->vardest ){
3172 cp = lemp->vardest;
3173 if( cp==0 ) return;
drha5808f32008-04-27 22:19:44 +00003174 fprintf(out,"{\n"); (*lineno)++;
drhcc83b6e2004-04-23 23:38:42 +00003175 }else{
3176 assert( 0 ); /* Cannot happen */
drh75897232000-05-29 14:26:00 +00003177 }
3178 for(; *cp; cp++){
3179 if( *cp=='$' && cp[1]=='$' ){
3180 fprintf(out,"(yypminor->yy%d)",sp->dtnum);
3181 cp++;
3182 continue;
3183 }
shane58543932008-12-10 20:10:04 +00003184 if( *cp=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003185 fputc(*cp,out);
3186 }
shane58543932008-12-10 20:10:04 +00003187 fprintf(out,"\n"); (*lineno)++;
3188 if (!lemp->nolinenosflag) {
3189 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3190 }
3191 fprintf(out,"}\n"); (*lineno)++;
drh75897232000-05-29 14:26:00 +00003192 return;
3193}
3194
3195/*
drh960e8c62001-04-03 16:53:21 +00003196** Return TRUE (non-zero) if the given symbol has a destructor.
drh75897232000-05-29 14:26:00 +00003197*/
icculus9e44cf12010-02-14 17:14:22 +00003198int has_destructor(struct symbol *sp, struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00003199{
3200 int ret;
3201 if( sp->type==TERMINAL ){
3202 ret = lemp->tokendest!=0;
3203 }else{
drh960e8c62001-04-03 16:53:21 +00003204 ret = lemp->vardest!=0 || sp->destructor!=0;
drh75897232000-05-29 14:26:00 +00003205 }
3206 return ret;
3207}
3208
drh0bb132b2004-07-20 14:06:51 +00003209/*
3210** Append text to a dynamically allocated string. If zText is 0 then
3211** reset the string to be empty again. Always return the complete text
3212** of the string (which is overwritten with each call).
drh7ac25c72004-08-19 15:12:26 +00003213**
3214** n bytes of zText are stored. If n==0 then all of zText up to the first
3215** \000 terminator is stored. zText can contain up to two instances of
3216** %d. The values of p1 and p2 are written into the first and second
3217** %d.
3218**
3219** If n==-1, then the previous character is overwritten.
drh0bb132b2004-07-20 14:06:51 +00003220*/
icculus9e44cf12010-02-14 17:14:22 +00003221PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
3222 static char empty[1] = { 0 };
drh0bb132b2004-07-20 14:06:51 +00003223 static char *z = 0;
3224 static int alloced = 0;
3225 static int used = 0;
drhaf805ca2004-09-07 11:28:25 +00003226 int c;
drh0bb132b2004-07-20 14:06:51 +00003227 char zInt[40];
drh0bb132b2004-07-20 14:06:51 +00003228 if( zText==0 ){
3229 used = 0;
3230 return z;
3231 }
drh7ac25c72004-08-19 15:12:26 +00003232 if( n<=0 ){
3233 if( n<0 ){
3234 used += n;
3235 assert( used>=0 );
3236 }
drh87cf1372008-08-13 20:09:06 +00003237 n = lemonStrlen(zText);
drh7ac25c72004-08-19 15:12:26 +00003238 }
drh0bb132b2004-07-20 14:06:51 +00003239 if( n+sizeof(zInt)*2+used >= alloced ){
3240 alloced = n + sizeof(zInt)*2 + used + 200;
icculus9e44cf12010-02-14 17:14:22 +00003241 z = (char *) realloc(z, alloced);
drh0bb132b2004-07-20 14:06:51 +00003242 }
icculus9e44cf12010-02-14 17:14:22 +00003243 if( z==0 ) return empty;
drh0bb132b2004-07-20 14:06:51 +00003244 while( n-- > 0 ){
3245 c = *(zText++);
drh50489622006-10-13 12:25:29 +00003246 if( c=='%' && n>0 && zText[0]=='d' ){
drh0bb132b2004-07-20 14:06:51 +00003247 sprintf(zInt, "%d", p1);
3248 p1 = p2;
3249 strcpy(&z[used], zInt);
drh87cf1372008-08-13 20:09:06 +00003250 used += lemonStrlen(&z[used]);
drh0bb132b2004-07-20 14:06:51 +00003251 zText++;
3252 n--;
3253 }else{
3254 z[used++] = c;
3255 }
3256 }
3257 z[used] = 0;
3258 return z;
3259}
3260
3261/*
3262** zCode is a string that is the action associated with a rule. Expand
3263** the symbols in this string so that the refer to elements of the parser
drhaf805ca2004-09-07 11:28:25 +00003264** stack.
drh0bb132b2004-07-20 14:06:51 +00003265*/
drhaf805ca2004-09-07 11:28:25 +00003266PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){
drh0bb132b2004-07-20 14:06:51 +00003267 char *cp, *xp;
3268 int i;
3269 char lhsused = 0; /* True if the LHS element has been used */
3270 char used[MAXRHS]; /* True for each RHS element which is used */
3271
3272 for(i=0; i<rp->nrhs; i++) used[i] = 0;
3273 lhsused = 0;
3274
drh19c9e562007-03-29 20:13:53 +00003275 if( rp->code==0 ){
icculus9e44cf12010-02-14 17:14:22 +00003276 static char newlinestr[2] = { '\n', '\0' };
3277 rp->code = newlinestr;
drh19c9e562007-03-29 20:13:53 +00003278 rp->line = rp->ruleline;
3279 }
3280
drh0bb132b2004-07-20 14:06:51 +00003281 append_str(0,0,0,0);
icculus9e44cf12010-02-14 17:14:22 +00003282
3283 /* This const cast is wrong but harmless, if we're careful. */
3284 for(cp=(char *)rp->code; *cp; cp++){
drh0bb132b2004-07-20 14:06:51 +00003285 if( isalpha(*cp) && (cp==rp->code || (!isalnum(cp[-1]) && cp[-1]!='_')) ){
3286 char saved;
3287 for(xp= &cp[1]; isalnum(*xp) || *xp=='_'; xp++);
3288 saved = *xp;
3289 *xp = 0;
3290 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
drh7ac25c72004-08-19 15:12:26 +00003291 append_str("yygotominor.yy%d",0,rp->lhs->dtnum,0);
drh0bb132b2004-07-20 14:06:51 +00003292 cp = xp;
3293 lhsused = 1;
3294 }else{
3295 for(i=0; i<rp->nrhs; i++){
3296 if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
drh7ac25c72004-08-19 15:12:26 +00003297 if( cp!=rp->code && cp[-1]=='@' ){
3298 /* If the argument is of the form @X then substituted
3299 ** the token number of X, not the value of X */
3300 append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0);
3301 }else{
drhfd405312005-11-06 04:06:59 +00003302 struct symbol *sp = rp->rhs[i];
3303 int dtnum;
3304 if( sp->type==MULTITERMINAL ){
3305 dtnum = sp->subsym[0]->dtnum;
3306 }else{
3307 dtnum = sp->dtnum;
3308 }
3309 append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum);
drh7ac25c72004-08-19 15:12:26 +00003310 }
drh0bb132b2004-07-20 14:06:51 +00003311 cp = xp;
3312 used[i] = 1;
3313 break;
3314 }
3315 }
3316 }
3317 *xp = saved;
3318 }
3319 append_str(cp, 1, 0, 0);
3320 } /* End loop */
3321
3322 /* Check to make sure the LHS has been used */
3323 if( rp->lhsalias && !lhsused ){
3324 ErrorMsg(lemp->filename,rp->ruleline,
3325 "Label \"%s\" for \"%s(%s)\" is never used.",
3326 rp->lhsalias,rp->lhs->name,rp->lhsalias);
3327 lemp->errorcnt++;
3328 }
3329
3330 /* Generate destructor code for RHS symbols which are not used in the
3331 ** reduce code */
3332 for(i=0; i<rp->nrhs; i++){
3333 if( rp->rhsalias[i] && !used[i] ){
3334 ErrorMsg(lemp->filename,rp->ruleline,
3335 "Label %s for \"%s(%s)\" is never used.",
3336 rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
3337 lemp->errorcnt++;
3338 }else if( rp->rhsalias[i]==0 ){
3339 if( has_destructor(rp->rhs[i],lemp) ){
drh639efd02008-08-13 20:04:29 +00003340 append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
drh0bb132b2004-07-20 14:06:51 +00003341 rp->rhs[i]->index,i-rp->nrhs+1);
3342 }else{
3343 /* No destructor defined for this term */
3344 }
3345 }
3346 }
drh61e339a2007-01-16 03:09:02 +00003347 if( rp->code ){
3348 cp = append_str(0,0,0,0);
3349 rp->code = Strsafe(cp?cp:"");
3350 }
drh0bb132b2004-07-20 14:06:51 +00003351}
3352
drh75897232000-05-29 14:26:00 +00003353/*
3354** Generate code which executes when the rule "rp" is reduced. Write
3355** the code to "out". Make sure lineno stays up-to-date.
3356*/
icculus9e44cf12010-02-14 17:14:22 +00003357PRIVATE void emit_code(
3358 FILE *out,
3359 struct rule *rp,
3360 struct lemon *lemp,
3361 int *lineno
3362){
3363 const char *cp;
drh75897232000-05-29 14:26:00 +00003364
3365 /* Generate code to do the reduce action */
3366 if( rp->code ){
shane58543932008-12-10 20:10:04 +00003367 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,rp->line,lemp->filename); }
drhaf805ca2004-09-07 11:28:25 +00003368 fprintf(out,"{%s",rp->code);
drh75897232000-05-29 14:26:00 +00003369 for(cp=rp->code; *cp; cp++){
shane58543932008-12-10 20:10:04 +00003370 if( *cp=='\n' ) (*lineno)++;
drh75897232000-05-29 14:26:00 +00003371 } /* End loop */
shane58543932008-12-10 20:10:04 +00003372 fprintf(out,"}\n"); (*lineno)++;
3373 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); }
drh75897232000-05-29 14:26:00 +00003374 } /* End if( rp->code ) */
3375
drh75897232000-05-29 14:26:00 +00003376 return;
3377}
3378
3379/*
3380** Print the definition of the union used for the parser's data stack.
3381** This union contains fields for every possible data type for tokens
3382** and nonterminals. In the process of computing and printing this
3383** union, also set the ".dtnum" field of every terminal and nonterminal
3384** symbol.
3385*/
icculus9e44cf12010-02-14 17:14:22 +00003386void print_stack_union(
3387 FILE *out, /* The output stream */
3388 struct lemon *lemp, /* The main info structure for this parser */
3389 int *plineno, /* Pointer to the line number */
3390 int mhflag /* True if generating makeheaders output */
3391){
drh75897232000-05-29 14:26:00 +00003392 int lineno = *plineno; /* The line number of the output */
3393 char **types; /* A hash table of datatypes */
3394 int arraysize; /* Size of the "types" array */
3395 int maxdtlength; /* Maximum length of any ".datatype" field. */
3396 char *stddt; /* Standardized name for a datatype */
3397 int i,j; /* Loop counters */
3398 int hash; /* For hashing the name of a type */
icculus9e44cf12010-02-14 17:14:22 +00003399 const char *name; /* Name of the parser */
drh75897232000-05-29 14:26:00 +00003400
3401 /* Allocate and initialize types[] and allocate stddt[] */
3402 arraysize = lemp->nsymbol * 2;
drh9892c5d2007-12-21 00:02:11 +00003403 types = (char**)calloc( arraysize, sizeof(char*) );
drh75897232000-05-29 14:26:00 +00003404 for(i=0; i<arraysize; i++) types[i] = 0;
3405 maxdtlength = 0;
drh960e8c62001-04-03 16:53:21 +00003406 if( lemp->vartype ){
drh87cf1372008-08-13 20:09:06 +00003407 maxdtlength = lemonStrlen(lemp->vartype);
drh960e8c62001-04-03 16:53:21 +00003408 }
drh75897232000-05-29 14:26:00 +00003409 for(i=0; i<lemp->nsymbol; i++){
3410 int len;
3411 struct symbol *sp = lemp->symbols[i];
3412 if( sp->datatype==0 ) continue;
drh87cf1372008-08-13 20:09:06 +00003413 len = lemonStrlen(sp->datatype);
drh75897232000-05-29 14:26:00 +00003414 if( len>maxdtlength ) maxdtlength = len;
3415 }
3416 stddt = (char*)malloc( maxdtlength*2 + 1 );
3417 if( types==0 || stddt==0 ){
3418 fprintf(stderr,"Out of memory.\n");
3419 exit(1);
3420 }
3421
3422 /* Build a hash table of datatypes. The ".dtnum" field of each symbol
3423 ** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
drh960e8c62001-04-03 16:53:21 +00003424 ** used for terminal symbols. If there is no %default_type defined then
3425 ** 0 is also used as the .dtnum value for nonterminals which do not specify
3426 ** a datatype using the %type directive.
3427 */
drh75897232000-05-29 14:26:00 +00003428 for(i=0; i<lemp->nsymbol; i++){
3429 struct symbol *sp = lemp->symbols[i];
3430 char *cp;
3431 if( sp==lemp->errsym ){
3432 sp->dtnum = arraysize+1;
3433 continue;
3434 }
drh960e8c62001-04-03 16:53:21 +00003435 if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
drh75897232000-05-29 14:26:00 +00003436 sp->dtnum = 0;
3437 continue;
3438 }
3439 cp = sp->datatype;
drh960e8c62001-04-03 16:53:21 +00003440 if( cp==0 ) cp = lemp->vartype;
drh75897232000-05-29 14:26:00 +00003441 j = 0;
3442 while( isspace(*cp) ) cp++;
3443 while( *cp ) stddt[j++] = *cp++;
3444 while( j>0 && isspace(stddt[j-1]) ) j--;
3445 stddt[j] = 0;
drh02368c92009-04-05 15:18:02 +00003446 if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){
drh32c4d742008-07-01 16:34:49 +00003447 sp->dtnum = 0;
3448 continue;
3449 }
drh75897232000-05-29 14:26:00 +00003450 hash = 0;
3451 for(j=0; stddt[j]; j++){
3452 hash = hash*53 + stddt[j];
3453 }
drh3b2129c2003-05-13 00:34:21 +00003454 hash = (hash & 0x7fffffff)%arraysize;
drh75897232000-05-29 14:26:00 +00003455 while( types[hash] ){
3456 if( strcmp(types[hash],stddt)==0 ){
3457 sp->dtnum = hash + 1;
3458 break;
3459 }
3460 hash++;
3461 if( hash>=arraysize ) hash = 0;
3462 }
3463 if( types[hash]==0 ){
3464 sp->dtnum = hash + 1;
drh87cf1372008-08-13 20:09:06 +00003465 types[hash] = (char*)malloc( lemonStrlen(stddt)+1 );
drh75897232000-05-29 14:26:00 +00003466 if( types[hash]==0 ){
3467 fprintf(stderr,"Out of memory.\n");
3468 exit(1);
3469 }
3470 strcpy(types[hash],stddt);
3471 }
3472 }
3473
3474 /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
3475 name = lemp->name ? lemp->name : "Parse";
3476 lineno = *plineno;
3477 if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
3478 fprintf(out,"#define %sTOKENTYPE %s\n",name,
3479 lemp->tokentype?lemp->tokentype:"void*"); lineno++;
3480 if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
3481 fprintf(out,"typedef union {\n"); lineno++;
drh15b024c2008-12-11 02:20:43 +00003482 fprintf(out," int yyinit;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003483 fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
3484 for(i=0; i<arraysize; i++){
3485 if( types[i]==0 ) continue;
3486 fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
3487 free(types[i]);
3488 }
drhc4dd3fd2008-01-22 01:48:05 +00003489 if( lemp->errsym->useCnt ){
3490 fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
3491 }
drh75897232000-05-29 14:26:00 +00003492 free(stddt);
3493 free(types);
3494 fprintf(out,"} YYMINORTYPE;\n"); lineno++;
3495 *plineno = lineno;
3496}
3497
drhb29b0a52002-02-23 19:39:46 +00003498/*
3499** Return the name of a C datatype able to represent values between
drh8b582012003-10-21 13:16:03 +00003500** lwr and upr, inclusive.
drhb29b0a52002-02-23 19:39:46 +00003501*/
drh8b582012003-10-21 13:16:03 +00003502static const char *minimum_size_type(int lwr, int upr){
3503 if( lwr>=0 ){
3504 if( upr<=255 ){
3505 return "unsigned char";
3506 }else if( upr<65535 ){
3507 return "unsigned short int";
3508 }else{
3509 return "unsigned int";
3510 }
3511 }else if( lwr>=-127 && upr<=127 ){
3512 return "signed char";
3513 }else if( lwr>=-32767 && upr<32767 ){
3514 return "short";
drhb29b0a52002-02-23 19:39:46 +00003515 }else{
drh8b582012003-10-21 13:16:03 +00003516 return "int";
drhb29b0a52002-02-23 19:39:46 +00003517 }
3518}
3519
drhfdbf9282003-10-21 16:34:41 +00003520/*
3521** Each state contains a set of token transaction and a set of
3522** nonterminal transactions. Each of these sets makes an instance
3523** of the following structure. An array of these structures is used
3524** to order the creation of entries in the yy_action[] table.
3525*/
3526struct axset {
3527 struct state *stp; /* A pointer to a state */
3528 int isTkn; /* True to use tokens. False for non-terminals */
3529 int nAction; /* Number of actions */
drhe594bc32009-11-03 13:02:25 +00003530 int iOrder; /* Original order of action sets */
drhfdbf9282003-10-21 16:34:41 +00003531};
3532
3533/*
3534** Compare to axset structures for sorting purposes
3535*/
3536static int axset_compare(const void *a, const void *b){
3537 struct axset *p1 = (struct axset*)a;
3538 struct axset *p2 = (struct axset*)b;
drhe594bc32009-11-03 13:02:25 +00003539 int c;
3540 c = p2->nAction - p1->nAction;
3541 if( c==0 ){
3542 c = p2->iOrder - p1->iOrder;
3543 }
3544 assert( c!=0 || p1==p2 );
3545 return c;
drhfdbf9282003-10-21 16:34:41 +00003546}
3547
drhc4dd3fd2008-01-22 01:48:05 +00003548/*
3549** Write text on "out" that describes the rule "rp".
3550*/
3551static void writeRuleText(FILE *out, struct rule *rp){
3552 int j;
3553 fprintf(out,"%s ::=", rp->lhs->name);
3554 for(j=0; j<rp->nrhs; j++){
3555 struct symbol *sp = rp->rhs[j];
3556 fprintf(out," %s", sp->name);
3557 if( sp->type==MULTITERMINAL ){
3558 int k;
3559 for(k=1; k<sp->nsubsym; k++){
3560 fprintf(out,"|%s",sp->subsym[k]->name);
3561 }
3562 }
3563 }
3564}
3565
3566
drh75897232000-05-29 14:26:00 +00003567/* Generate C source code for the parser */
icculus9e44cf12010-02-14 17:14:22 +00003568void ReportTable(
3569 struct lemon *lemp,
3570 int mhflag /* Output in makeheaders format if true */
3571){
drh75897232000-05-29 14:26:00 +00003572 FILE *out, *in;
3573 char line[LINESIZE];
3574 int lineno;
3575 struct state *stp;
3576 struct action *ap;
3577 struct rule *rp;
drh8b582012003-10-21 13:16:03 +00003578 struct acttab *pActtab;
drhf16371d2009-11-03 19:18:31 +00003579 int i, j, k, n;
icculus9e44cf12010-02-14 17:14:22 +00003580 const char *name;
drh8b582012003-10-21 13:16:03 +00003581 int mnTknOfst, mxTknOfst;
3582 int mnNtOfst, mxNtOfst;
drhfdbf9282003-10-21 16:34:41 +00003583 struct axset *ax;
drh75897232000-05-29 14:26:00 +00003584
3585 in = tplt_open(lemp);
3586 if( in==0 ) return;
drh2aa6ca42004-09-10 00:14:04 +00003587 out = file_open(lemp,".c","wb");
drh75897232000-05-29 14:26:00 +00003588 if( out==0 ){
3589 fclose(in);
3590 return;
3591 }
3592 lineno = 1;
3593 tplt_xfer(lemp->name,in,out,&lineno);
3594
3595 /* Generate the include code, if any */
drha5808f32008-04-27 22:19:44 +00003596 tplt_print(out,lemp,lemp->include,&lineno);
drh75897232000-05-29 14:26:00 +00003597 if( mhflag ){
3598 char *name = file_makename(lemp, ".h");
3599 fprintf(out,"#include \"%s\"\n", name); lineno++;
3600 free(name);
3601 }
3602 tplt_xfer(lemp->name,in,out,&lineno);
3603
3604 /* Generate #defines for all tokens */
3605 if( mhflag ){
icculus9e44cf12010-02-14 17:14:22 +00003606 const char *prefix;
drh75897232000-05-29 14:26:00 +00003607 fprintf(out,"#if INTERFACE\n"); lineno++;
3608 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
3609 else prefix = "";
3610 for(i=1; i<lemp->nterminal; i++){
3611 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3612 lineno++;
3613 }
3614 fprintf(out,"#endif\n"); lineno++;
3615 }
3616 tplt_xfer(lemp->name,in,out,&lineno);
3617
3618 /* Generate the defines */
drh75897232000-05-29 14:26:00 +00003619 fprintf(out,"#define YYCODETYPE %s\n",
drh8a415d32009-06-12 13:53:51 +00003620 minimum_size_type(0, lemp->nsymbol+1)); lineno++;
drh75897232000-05-29 14:26:00 +00003621 fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1); lineno++;
3622 fprintf(out,"#define YYACTIONTYPE %s\n",
drh8b582012003-10-21 13:16:03 +00003623 minimum_size_type(0, lemp->nstate+lemp->nrule+5)); lineno++;
drhe09daa92006-06-10 13:29:31 +00003624 if( lemp->wildcard ){
3625 fprintf(out,"#define YYWILDCARD %d\n",
3626 lemp->wildcard->index); lineno++;
3627 }
drh75897232000-05-29 14:26:00 +00003628 print_stack_union(out,lemp,&lineno,mhflag);
drhca44b5a2007-02-22 23:06:58 +00003629 fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003630 if( lemp->stacksize ){
drh75897232000-05-29 14:26:00 +00003631 fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
3632 }else{
3633 fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
3634 }
drhca44b5a2007-02-22 23:06:58 +00003635 fprintf(out, "#endif\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003636 if( mhflag ){
3637 fprintf(out,"#if INTERFACE\n"); lineno++;
3638 }
3639 name = lemp->name ? lemp->name : "Parse";
3640 if( lemp->arg && lemp->arg[0] ){
3641 int i;
drh87cf1372008-08-13 20:09:06 +00003642 i = lemonStrlen(lemp->arg);
drhb1edd012000-06-02 18:52:12 +00003643 while( i>=1 && isspace(lemp->arg[i-1]) ) i--;
3644 while( i>=1 && (isalnum(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
drh1f245e42002-03-11 13:55:50 +00003645 fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++;
3646 fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++;
3647 fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n",
3648 name,lemp->arg,&lemp->arg[i]); lineno++;
3649 fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n",
3650 name,&lemp->arg[i],&lemp->arg[i]); lineno++;
drh75897232000-05-29 14:26:00 +00003651 }else{
drh1f245e42002-03-11 13:55:50 +00003652 fprintf(out,"#define %sARG_SDECL\n",name); lineno++;
3653 fprintf(out,"#define %sARG_PDECL\n",name); lineno++;
3654 fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
3655 fprintf(out,"#define %sARG_STORE\n",name); lineno++;
drh75897232000-05-29 14:26:00 +00003656 }
3657 if( mhflag ){
3658 fprintf(out,"#endif\n"); lineno++;
3659 }
3660 fprintf(out,"#define YYNSTATE %d\n",lemp->nstate); lineno++;
3661 fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
drhc4dd3fd2008-01-22 01:48:05 +00003662 if( lemp->errsym->useCnt ){
3663 fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
3664 fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
3665 }
drh0bd1f4e2002-06-06 18:54:39 +00003666 if( lemp->has_fallback ){
3667 fprintf(out,"#define YYFALLBACK 1\n"); lineno++;
3668 }
drh75897232000-05-29 14:26:00 +00003669 tplt_xfer(lemp->name,in,out,&lineno);
3670
drh8b582012003-10-21 13:16:03 +00003671 /* Generate the action table and its associates:
drh75897232000-05-29 14:26:00 +00003672 **
drh8b582012003-10-21 13:16:03 +00003673 ** yy_action[] A single table containing all actions.
3674 ** yy_lookahead[] A table containing the lookahead for each entry in
3675 ** yy_action. Used to detect hash collisions.
3676 ** yy_shift_ofst[] For each state, the offset into yy_action for
3677 ** shifting terminals.
3678 ** yy_reduce_ofst[] For each state, the offset into yy_action for
3679 ** shifting non-terminals after a reduce.
3680 ** yy_default[] Default action for each state.
drh75897232000-05-29 14:26:00 +00003681 */
drh75897232000-05-29 14:26:00 +00003682
drh8b582012003-10-21 13:16:03 +00003683 /* Compute the actions on all states and count them up */
icculus9e44cf12010-02-14 17:14:22 +00003684 ax = (struct axset *) calloc(lemp->nstate*2, sizeof(ax[0]));
drhfdbf9282003-10-21 16:34:41 +00003685 if( ax==0 ){
3686 fprintf(stderr,"malloc failed\n");
3687 exit(1);
3688 }
drh75897232000-05-29 14:26:00 +00003689 for(i=0; i<lemp->nstate; i++){
drh75897232000-05-29 14:26:00 +00003690 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00003691 ax[i*2].stp = stp;
3692 ax[i*2].isTkn = 1;
3693 ax[i*2].nAction = stp->nTknAct;
3694 ax[i*2+1].stp = stp;
3695 ax[i*2+1].isTkn = 0;
3696 ax[i*2+1].nAction = stp->nNtAct;
drh75897232000-05-29 14:26:00 +00003697 }
drh8b582012003-10-21 13:16:03 +00003698 mxTknOfst = mnTknOfst = 0;
3699 mxNtOfst = mnNtOfst = 0;
3700
drhfdbf9282003-10-21 16:34:41 +00003701 /* Compute the action table. In order to try to keep the size of the
3702 ** action table to a minimum, the heuristic of placing the largest action
3703 ** sets first is used.
drh8b582012003-10-21 13:16:03 +00003704 */
drhe594bc32009-11-03 13:02:25 +00003705 for(i=0; i<lemp->nstate*2; i++) ax[i].iOrder = i;
drhfdbf9282003-10-21 16:34:41 +00003706 qsort(ax, lemp->nstate*2, sizeof(ax[0]), axset_compare);
drh8b582012003-10-21 13:16:03 +00003707 pActtab = acttab_alloc();
drhfdbf9282003-10-21 16:34:41 +00003708 for(i=0; i<lemp->nstate*2 && ax[i].nAction>0; i++){
3709 stp = ax[i].stp;
3710 if( ax[i].isTkn ){
3711 for(ap=stp->ap; ap; ap=ap->next){
3712 int action;
3713 if( ap->sp->index>=lemp->nterminal ) continue;
3714 action = compute_action(lemp, ap);
3715 if( action<0 ) continue;
3716 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00003717 }
drhfdbf9282003-10-21 16:34:41 +00003718 stp->iTknOfst = acttab_insert(pActtab);
3719 if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
3720 if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
3721 }else{
3722 for(ap=stp->ap; ap; ap=ap->next){
3723 int action;
3724 if( ap->sp->index<lemp->nterminal ) continue;
3725 if( ap->sp->index==lemp->nsymbol ) continue;
3726 action = compute_action(lemp, ap);
3727 if( action<0 ) continue;
3728 acttab_action(pActtab, ap->sp->index, action);
drh8b582012003-10-21 13:16:03 +00003729 }
drhfdbf9282003-10-21 16:34:41 +00003730 stp->iNtOfst = acttab_insert(pActtab);
3731 if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
3732 if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
drh8b582012003-10-21 13:16:03 +00003733 }
3734 }
drhfdbf9282003-10-21 16:34:41 +00003735 free(ax);
drh8b582012003-10-21 13:16:03 +00003736
3737 /* Output the yy_action table */
drh8b582012003-10-21 13:16:03 +00003738 n = acttab_size(pActtab);
drhf16371d2009-11-03 19:18:31 +00003739 fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
3740 fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00003741 for(i=j=0; i<n; i++){
3742 int action = acttab_yyaction(pActtab, i);
drhe0479212007-01-12 23:09:23 +00003743 if( action<0 ) action = lemp->nstate + lemp->nrule + 2;
drhfdbf9282003-10-21 16:34:41 +00003744 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003745 fprintf(out, " %4d,", action);
3746 if( j==9 || i==n-1 ){
3747 fprintf(out, "\n"); lineno++;
3748 j = 0;
3749 }else{
3750 j++;
3751 }
3752 }
3753 fprintf(out, "};\n"); lineno++;
3754
3755 /* Output the yy_lookahead table */
drh57196282004-10-06 15:41:16 +00003756 fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00003757 for(i=j=0; i<n; i++){
3758 int la = acttab_yylookahead(pActtab, i);
3759 if( la<0 ) la = lemp->nsymbol;
drhfdbf9282003-10-21 16:34:41 +00003760 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003761 fprintf(out, " %4d,", la);
3762 if( j==9 || i==n-1 ){
3763 fprintf(out, "\n"); lineno++;
3764 j = 0;
3765 }else{
3766 j++;
3767 }
3768 }
3769 fprintf(out, "};\n"); lineno++;
3770
3771 /* Output the yy_shift_ofst[] table */
3772 fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++;
drhada354d2005-11-05 15:03:59 +00003773 n = lemp->nstate;
3774 while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00003775 fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
3776 fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
3777 fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
drh57196282004-10-06 15:41:16 +00003778 fprintf(out, "static const %s yy_shift_ofst[] = {\n",
drh8b582012003-10-21 13:16:03 +00003779 minimum_size_type(mnTknOfst-1, mxTknOfst)); lineno++;
drh8b582012003-10-21 13:16:03 +00003780 for(i=j=0; i<n; i++){
3781 int ofst;
3782 stp = lemp->sorted[i];
3783 ofst = stp->iTknOfst;
3784 if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00003785 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003786 fprintf(out, " %4d,", ofst);
3787 if( j==9 || i==n-1 ){
3788 fprintf(out, "\n"); lineno++;
3789 j = 0;
3790 }else{
3791 j++;
3792 }
3793 }
3794 fprintf(out, "};\n"); lineno++;
3795
3796 /* Output the yy_reduce_ofst[] table */
3797 fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
drhada354d2005-11-05 15:03:59 +00003798 n = lemp->nstate;
3799 while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
drhf16371d2009-11-03 19:18:31 +00003800 fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
3801 fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
3802 fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
drh57196282004-10-06 15:41:16 +00003803 fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
drh8b582012003-10-21 13:16:03 +00003804 minimum_size_type(mnNtOfst-1, mxNtOfst)); lineno++;
drh8b582012003-10-21 13:16:03 +00003805 for(i=j=0; i<n; i++){
3806 int ofst;
3807 stp = lemp->sorted[i];
3808 ofst = stp->iNtOfst;
3809 if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
drhfdbf9282003-10-21 16:34:41 +00003810 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003811 fprintf(out, " %4d,", ofst);
3812 if( j==9 || i==n-1 ){
3813 fprintf(out, "\n"); lineno++;
3814 j = 0;
3815 }else{
3816 j++;
3817 }
3818 }
3819 fprintf(out, "};\n"); lineno++;
3820
3821 /* Output the default action table */
drh57196282004-10-06 15:41:16 +00003822 fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
drh8b582012003-10-21 13:16:03 +00003823 n = lemp->nstate;
3824 for(i=j=0; i<n; i++){
3825 stp = lemp->sorted[i];
drhfdbf9282003-10-21 16:34:41 +00003826 if( j==0 ) fprintf(out," /* %5d */ ", i);
drh8b582012003-10-21 13:16:03 +00003827 fprintf(out, " %4d,", stp->iDflt);
3828 if( j==9 || i==n-1 ){
3829 fprintf(out, "\n"); lineno++;
3830 j = 0;
3831 }else{
3832 j++;
3833 }
3834 }
3835 fprintf(out, "};\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003836 tplt_xfer(lemp->name,in,out,&lineno);
3837
drh0bd1f4e2002-06-06 18:54:39 +00003838 /* Generate the table of fallback tokens.
3839 */
3840 if( lemp->has_fallback ){
drh1441f3e2009-06-12 12:50:50 +00003841 int mx = lemp->nterminal - 1;
3842 while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; }
3843 for(i=0; i<=mx; i++){
drh0bd1f4e2002-06-06 18:54:39 +00003844 struct symbol *p = lemp->symbols[i];
3845 if( p->fallback==0 ){
3846 fprintf(out, " 0, /* %10s => nothing */\n", p->name);
3847 }else{
3848 fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index,
3849 p->name, p->fallback->name);
3850 }
3851 lineno++;
3852 }
3853 }
3854 tplt_xfer(lemp->name, in, out, &lineno);
3855
3856 /* Generate a table containing the symbolic name of every symbol
3857 */
drh75897232000-05-29 14:26:00 +00003858 for(i=0; i<lemp->nsymbol; i++){
3859 sprintf(line,"\"%s\",",lemp->symbols[i]->name);
3860 fprintf(out," %-15s",line);
3861 if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
3862 }
3863 if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
3864 tplt_xfer(lemp->name,in,out,&lineno);
3865
drh0bd1f4e2002-06-06 18:54:39 +00003866 /* Generate a table containing a text string that describes every
drh34ff57b2008-07-14 12:27:51 +00003867 ** rule in the rule set of the grammar. This information is used
drh0bd1f4e2002-06-06 18:54:39 +00003868 ** when tracing REDUCE actions.
3869 */
3870 for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
3871 assert( rp->index==i );
drhc4dd3fd2008-01-22 01:48:05 +00003872 fprintf(out," /* %3d */ \"", i);
3873 writeRuleText(out, rp);
drh0bd1f4e2002-06-06 18:54:39 +00003874 fprintf(out,"\",\n"); lineno++;
3875 }
3876 tplt_xfer(lemp->name,in,out,&lineno);
3877
drh75897232000-05-29 14:26:00 +00003878 /* Generate code which executes every time a symbol is popped from
3879 ** the stack while processing errors or while destroying the parser.
drh0bd1f4e2002-06-06 18:54:39 +00003880 ** (In other words, generate the %destructor actions)
3881 */
drh75897232000-05-29 14:26:00 +00003882 if( lemp->tokendest ){
drh4dc8ef52008-07-01 17:13:57 +00003883 int once = 1;
drh75897232000-05-29 14:26:00 +00003884 for(i=0; i<lemp->nsymbol; i++){
3885 struct symbol *sp = lemp->symbols[i];
3886 if( sp==0 || sp->type!=TERMINAL ) continue;
drh4dc8ef52008-07-01 17:13:57 +00003887 if( once ){
3888 fprintf(out, " /* TERMINAL Destructor */\n"); lineno++;
3889 once = 0;
3890 }
drhc53eed12009-06-12 17:46:19 +00003891 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh75897232000-05-29 14:26:00 +00003892 }
3893 for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
3894 if( i<lemp->nsymbol ){
3895 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
3896 fprintf(out," break;\n"); lineno++;
3897 }
3898 }
drh8d659732005-01-13 23:54:06 +00003899 if( lemp->vardest ){
3900 struct symbol *dflt_sp = 0;
drh4dc8ef52008-07-01 17:13:57 +00003901 int once = 1;
drh8d659732005-01-13 23:54:06 +00003902 for(i=0; i<lemp->nsymbol; i++){
3903 struct symbol *sp = lemp->symbols[i];
3904 if( sp==0 || sp->type==TERMINAL ||
3905 sp->index<=0 || sp->destructor!=0 ) continue;
drh4dc8ef52008-07-01 17:13:57 +00003906 if( once ){
3907 fprintf(out, " /* Default NON-TERMINAL Destructor */\n"); lineno++;
3908 once = 0;
3909 }
drhc53eed12009-06-12 17:46:19 +00003910 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh8d659732005-01-13 23:54:06 +00003911 dflt_sp = sp;
3912 }
3913 if( dflt_sp!=0 ){
3914 emit_destructor_code(out,dflt_sp,lemp,&lineno);
drh8d659732005-01-13 23:54:06 +00003915 }
drh4dc8ef52008-07-01 17:13:57 +00003916 fprintf(out," break;\n"); lineno++;
drh8d659732005-01-13 23:54:06 +00003917 }
drh75897232000-05-29 14:26:00 +00003918 for(i=0; i<lemp->nsymbol; i++){
3919 struct symbol *sp = lemp->symbols[i];
3920 if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
drh75013012009-06-12 15:47:34 +00003921 fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00003922
3923 /* Combine duplicate destructors into a single case */
3924 for(j=i+1; j<lemp->nsymbol; j++){
3925 struct symbol *sp2 = lemp->symbols[j];
3926 if( sp2 && sp2->type!=TERMINAL && sp2->destructor
3927 && sp2->dtnum==sp->dtnum
3928 && strcmp(sp->destructor,sp2->destructor)==0 ){
drhc53eed12009-06-12 17:46:19 +00003929 fprintf(out," case %d: /* %s */\n",
3930 sp2->index, sp2->name); lineno++;
drh0bb132b2004-07-20 14:06:51 +00003931 sp2->destructor = 0;
3932 }
3933 }
3934
drh75897232000-05-29 14:26:00 +00003935 emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
3936 fprintf(out," break;\n"); lineno++;
3937 }
drh75897232000-05-29 14:26:00 +00003938 tplt_xfer(lemp->name,in,out,&lineno);
3939
3940 /* Generate code which executes whenever the parser stack overflows */
drha5808f32008-04-27 22:19:44 +00003941 tplt_print(out,lemp,lemp->overflow,&lineno);
drh75897232000-05-29 14:26:00 +00003942 tplt_xfer(lemp->name,in,out,&lineno);
3943
3944 /* Generate the table of rule information
3945 **
3946 ** Note: This code depends on the fact that rules are number
3947 ** sequentually beginning with 0.
3948 */
3949 for(rp=lemp->rule; rp; rp=rp->next){
3950 fprintf(out," { %d, %d },\n",rp->lhs->index,rp->nrhs); lineno++;
3951 }
3952 tplt_xfer(lemp->name,in,out,&lineno);
3953
3954 /* Generate code which execution during each REDUCE action */
3955 for(rp=lemp->rule; rp; rp=rp->next){
drh61e339a2007-01-16 03:09:02 +00003956 translate_code(lemp, rp);
drh0bb132b2004-07-20 14:06:51 +00003957 }
drhc53eed12009-06-12 17:46:19 +00003958 /* First output rules other than the default: rule */
drh0bb132b2004-07-20 14:06:51 +00003959 for(rp=lemp->rule; rp; rp=rp->next){
drhc53eed12009-06-12 17:46:19 +00003960 struct rule *rp2; /* Other rules with the same action */
drh0bb132b2004-07-20 14:06:51 +00003961 if( rp->code==0 ) continue;
drhc53eed12009-06-12 17:46:19 +00003962 if( rp->code[0]=='\n' && rp->code[1]==0 ) continue; /* Will be default: */
drhc4dd3fd2008-01-22 01:48:05 +00003963 fprintf(out," case %d: /* ", rp->index);
3964 writeRuleText(out, rp);
3965 fprintf(out, " */\n"); lineno++;
drh0bb132b2004-07-20 14:06:51 +00003966 for(rp2=rp->next; rp2; rp2=rp2->next){
3967 if( rp2->code==rp->code ){
drhc4dd3fd2008-01-22 01:48:05 +00003968 fprintf(out," case %d: /* ", rp2->index);
3969 writeRuleText(out, rp2);
drh8a415d32009-06-12 13:53:51 +00003970 fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->index); lineno++;
drh0bb132b2004-07-20 14:06:51 +00003971 rp2->code = 0;
3972 }
3973 }
drh75897232000-05-29 14:26:00 +00003974 emit_code(out,rp,lemp,&lineno);
3975 fprintf(out," break;\n"); lineno++;
drhc53eed12009-06-12 17:46:19 +00003976 rp->code = 0;
drh75897232000-05-29 14:26:00 +00003977 }
drhc53eed12009-06-12 17:46:19 +00003978 /* Finally, output the default: rule. We choose as the default: all
3979 ** empty actions. */
3980 fprintf(out," default:\n"); lineno++;
3981 for(rp=lemp->rule; rp; rp=rp->next){
3982 if( rp->code==0 ) continue;
3983 assert( rp->code[0]=='\n' && rp->code[1]==0 );
3984 fprintf(out," /* (%d) ", rp->index);
3985 writeRuleText(out, rp);
3986 fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->index); lineno++;
3987 }
3988 fprintf(out," break;\n"); lineno++;
drh75897232000-05-29 14:26:00 +00003989 tplt_xfer(lemp->name,in,out,&lineno);
3990
3991 /* Generate code which executes if a parse fails */
drha5808f32008-04-27 22:19:44 +00003992 tplt_print(out,lemp,lemp->failure,&lineno);
drh75897232000-05-29 14:26:00 +00003993 tplt_xfer(lemp->name,in,out,&lineno);
3994
3995 /* Generate code which executes when a syntax error occurs */
drha5808f32008-04-27 22:19:44 +00003996 tplt_print(out,lemp,lemp->error,&lineno);
drh75897232000-05-29 14:26:00 +00003997 tplt_xfer(lemp->name,in,out,&lineno);
3998
3999 /* Generate code which executes when the parser accepts its input */
drha5808f32008-04-27 22:19:44 +00004000 tplt_print(out,lemp,lemp->accept,&lineno);
drh75897232000-05-29 14:26:00 +00004001 tplt_xfer(lemp->name,in,out,&lineno);
4002
4003 /* Append any addition code the user desires */
drha5808f32008-04-27 22:19:44 +00004004 tplt_print(out,lemp,lemp->extracode,&lineno);
drh75897232000-05-29 14:26:00 +00004005
4006 fclose(in);
4007 fclose(out);
4008 return;
4009}
4010
4011/* Generate a header file for the parser */
icculus9e44cf12010-02-14 17:14:22 +00004012void ReportHeader(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004013{
4014 FILE *out, *in;
icculus9e44cf12010-02-14 17:14:22 +00004015 const char *prefix;
drh75897232000-05-29 14:26:00 +00004016 char line[LINESIZE];
4017 char pattern[LINESIZE];
4018 int i;
4019
4020 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
4021 else prefix = "";
drh2aa6ca42004-09-10 00:14:04 +00004022 in = file_open(lemp,".h","rb");
drh75897232000-05-29 14:26:00 +00004023 if( in ){
4024 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
4025 sprintf(pattern,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
4026 if( strcmp(line,pattern) ) break;
4027 }
4028 fclose(in);
4029 if( i==lemp->nterminal ){
4030 /* No change in the file. Don't rewrite it. */
4031 return;
4032 }
4033 }
drh2aa6ca42004-09-10 00:14:04 +00004034 out = file_open(lemp,".h","wb");
drh75897232000-05-29 14:26:00 +00004035 if( out ){
4036 for(i=1; i<lemp->nterminal; i++){
4037 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
4038 }
4039 fclose(out);
4040 }
4041 return;
4042}
4043
4044/* Reduce the size of the action tables, if possible, by making use
4045** of defaults.
4046**
drhb59499c2002-02-23 18:45:13 +00004047** In this version, we take the most frequent REDUCE action and make
drhe09daa92006-06-10 13:29:31 +00004048** it the default. Except, there is no default if the wildcard token
4049** is a possible look-ahead.
drh75897232000-05-29 14:26:00 +00004050*/
icculus9e44cf12010-02-14 17:14:22 +00004051void CompressTables(struct lemon *lemp)
drh75897232000-05-29 14:26:00 +00004052{
4053 struct state *stp;
drhb59499c2002-02-23 18:45:13 +00004054 struct action *ap, *ap2;
4055 struct rule *rp, *rp2, *rbest;
4056 int nbest, n;
drh75897232000-05-29 14:26:00 +00004057 int i;
drhe09daa92006-06-10 13:29:31 +00004058 int usesWildcard;
drh75897232000-05-29 14:26:00 +00004059
4060 for(i=0; i<lemp->nstate; i++){
4061 stp = lemp->sorted[i];
drhb59499c2002-02-23 18:45:13 +00004062 nbest = 0;
4063 rbest = 0;
drhe09daa92006-06-10 13:29:31 +00004064 usesWildcard = 0;
drh75897232000-05-29 14:26:00 +00004065
drhb59499c2002-02-23 18:45:13 +00004066 for(ap=stp->ap; ap; ap=ap->next){
drhe09daa92006-06-10 13:29:31 +00004067 if( ap->type==SHIFT && ap->sp==lemp->wildcard ){
4068 usesWildcard = 1;
4069 }
drhb59499c2002-02-23 18:45:13 +00004070 if( ap->type!=REDUCE ) continue;
4071 rp = ap->x.rp;
drhb4960992007-10-05 16:16:36 +00004072 if( rp->lhsStart ) continue;
drhb59499c2002-02-23 18:45:13 +00004073 if( rp==rbest ) continue;
4074 n = 1;
4075 for(ap2=ap->next; ap2; ap2=ap2->next){
4076 if( ap2->type!=REDUCE ) continue;
4077 rp2 = ap2->x.rp;
4078 if( rp2==rbest ) continue;
4079 if( rp2==rp ) n++;
4080 }
4081 if( n>nbest ){
4082 nbest = n;
4083 rbest = rp;
drh75897232000-05-29 14:26:00 +00004084 }
4085 }
drhb59499c2002-02-23 18:45:13 +00004086
4087 /* Do not make a default if the number of rules to default
drhe09daa92006-06-10 13:29:31 +00004088 ** is not at least 1 or if the wildcard token is a possible
4089 ** lookahead.
4090 */
4091 if( nbest<1 || usesWildcard ) continue;
drh75897232000-05-29 14:26:00 +00004092
drhb59499c2002-02-23 18:45:13 +00004093
4094 /* Combine matching REDUCE actions into a single default */
4095 for(ap=stp->ap; ap; ap=ap->next){
4096 if( ap->type==REDUCE && ap->x.rp==rbest ) break;
4097 }
drh75897232000-05-29 14:26:00 +00004098 assert( ap );
4099 ap->sp = Symbol_new("{default}");
4100 for(ap=ap->next; ap; ap=ap->next){
drhb59499c2002-02-23 18:45:13 +00004101 if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
drh75897232000-05-29 14:26:00 +00004102 }
4103 stp->ap = Action_sort(stp->ap);
4104 }
4105}
drhb59499c2002-02-23 18:45:13 +00004106
drhada354d2005-11-05 15:03:59 +00004107
4108/*
4109** Compare two states for sorting purposes. The smaller state is the
4110** one with the most non-terminal actions. If they have the same number
4111** of non-terminal actions, then the smaller is the one with the most
4112** token actions.
4113*/
4114static int stateResortCompare(const void *a, const void *b){
4115 const struct state *pA = *(const struct state**)a;
4116 const struct state *pB = *(const struct state**)b;
4117 int n;
4118
4119 n = pB->nNtAct - pA->nNtAct;
4120 if( n==0 ){
4121 n = pB->nTknAct - pA->nTknAct;
drhe594bc32009-11-03 13:02:25 +00004122 if( n==0 ){
4123 n = pB->statenum - pA->statenum;
4124 }
drhada354d2005-11-05 15:03:59 +00004125 }
drhe594bc32009-11-03 13:02:25 +00004126 assert( n!=0 );
drhada354d2005-11-05 15:03:59 +00004127 return n;
4128}
4129
4130
4131/*
4132** Renumber and resort states so that states with fewer choices
4133** occur at the end. Except, keep state 0 as the first state.
4134*/
icculus9e44cf12010-02-14 17:14:22 +00004135void ResortStates(struct lemon *lemp)
drhada354d2005-11-05 15:03:59 +00004136{
4137 int i;
4138 struct state *stp;
4139 struct action *ap;
4140
4141 for(i=0; i<lemp->nstate; i++){
4142 stp = lemp->sorted[i];
4143 stp->nTknAct = stp->nNtAct = 0;
4144 stp->iDflt = lemp->nstate + lemp->nrule;
4145 stp->iTknOfst = NO_OFFSET;
4146 stp->iNtOfst = NO_OFFSET;
4147 for(ap=stp->ap; ap; ap=ap->next){
4148 if( compute_action(lemp,ap)>=0 ){
4149 if( ap->sp->index<lemp->nterminal ){
4150 stp->nTknAct++;
4151 }else if( ap->sp->index<lemp->nsymbol ){
4152 stp->nNtAct++;
4153 }else{
4154 stp->iDflt = compute_action(lemp, ap);
4155 }
4156 }
4157 }
4158 }
4159 qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
4160 stateResortCompare);
4161 for(i=0; i<lemp->nstate; i++){
4162 lemp->sorted[i]->statenum = i;
4163 }
4164}
4165
4166
drh75897232000-05-29 14:26:00 +00004167/***************** From the file "set.c" ************************************/
4168/*
4169** Set manipulation routines for the LEMON parser generator.
4170*/
4171
4172static int size = 0;
4173
4174/* Set the set size */
icculus9e44cf12010-02-14 17:14:22 +00004175void SetSize(int n)
drh75897232000-05-29 14:26:00 +00004176{
4177 size = n+1;
4178}
4179
4180/* Allocate a new set */
4181char *SetNew(){
4182 char *s;
drh9892c5d2007-12-21 00:02:11 +00004183 s = (char*)calloc( size, 1);
drh75897232000-05-29 14:26:00 +00004184 if( s==0 ){
4185 extern void memory_error();
4186 memory_error();
4187 }
drh75897232000-05-29 14:26:00 +00004188 return s;
4189}
4190
4191/* Deallocate a set */
icculus9e44cf12010-02-14 17:14:22 +00004192void SetFree(char *s)
drh75897232000-05-29 14:26:00 +00004193{
4194 free(s);
4195}
4196
4197/* Add a new element to the set. Return TRUE if the element was added
4198** and FALSE if it was already there. */
icculus9e44cf12010-02-14 17:14:22 +00004199int SetAdd(char *s, int e)
drh75897232000-05-29 14:26:00 +00004200{
4201 int rv;
drh9892c5d2007-12-21 00:02:11 +00004202 assert( e>=0 && e<size );
drh75897232000-05-29 14:26:00 +00004203 rv = s[e];
4204 s[e] = 1;
4205 return !rv;
4206}
4207
4208/* Add every element of s2 to s1. Return TRUE if s1 changes. */
icculus9e44cf12010-02-14 17:14:22 +00004209int SetUnion(char *s1, char *s2)
drh75897232000-05-29 14:26:00 +00004210{
4211 int i, progress;
4212 progress = 0;
4213 for(i=0; i<size; i++){
4214 if( s2[i]==0 ) continue;
4215 if( s1[i]==0 ){
4216 progress = 1;
4217 s1[i] = 1;
4218 }
4219 }
4220 return progress;
4221}
4222/********************** From the file "table.c" ****************************/
4223/*
4224** All code in this file has been automatically generated
4225** from a specification in the file
4226** "table.q"
4227** by the associative array code building program "aagen".
4228** Do not edit this file! Instead, edit the specification
4229** file, then rerun aagen.
4230*/
4231/*
4232** Code for processing tables in the LEMON parser generator.
4233*/
4234
icculus9e44cf12010-02-14 17:14:22 +00004235PRIVATE int strhash(const char *x)
drh75897232000-05-29 14:26:00 +00004236{
4237 int h = 0;
4238 while( *x) h = h*13 + *(x++);
4239 return h;
4240}
4241
4242/* Works like strdup, sort of. Save a string in malloced memory, but
4243** keep strings in a table so that the same string is not in more
4244** than one place.
4245*/
icculus9e44cf12010-02-14 17:14:22 +00004246const char *Strsafe(const char *y)
drh75897232000-05-29 14:26:00 +00004247{
icculus9e44cf12010-02-14 17:14:22 +00004248 const char *z;
4249 char *cpy;
drh75897232000-05-29 14:26:00 +00004250
drh916f75f2006-07-17 00:19:39 +00004251 if( y==0 ) return 0;
drh75897232000-05-29 14:26:00 +00004252 z = Strsafe_find(y);
icculus9e44cf12010-02-14 17:14:22 +00004253 if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
4254 strcpy(cpy,y);
4255 z = cpy;
drh75897232000-05-29 14:26:00 +00004256 Strsafe_insert(z);
4257 }
4258 MemoryCheck(z);
4259 return z;
4260}
4261
4262/* There is one instance of the following structure for each
4263** associative array of type "x1".
4264*/
4265struct s_x1 {
4266 int size; /* The number of available slots. */
4267 /* Must be a power of 2 greater than or */
4268 /* equal to 1 */
4269 int count; /* Number of currently slots filled */
4270 struct s_x1node *tbl; /* The data stored here */
4271 struct s_x1node **ht; /* Hash table for lookups */
4272};
4273
4274/* There is one instance of this structure for every data element
4275** in an associative array of type "x1".
4276*/
4277typedef struct s_x1node {
icculus9e44cf12010-02-14 17:14:22 +00004278 const char *data; /* The data */
drh75897232000-05-29 14:26:00 +00004279 struct s_x1node *next; /* Next entry with the same hash */
4280 struct s_x1node **from; /* Previous link */
4281} x1node;
4282
4283/* There is only one instance of the array, which is the following */
4284static struct s_x1 *x1a;
4285
4286/* Allocate a new associative array */
4287void Strsafe_init(){
4288 if( x1a ) return;
4289 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
4290 if( x1a ){
4291 x1a->size = 1024;
4292 x1a->count = 0;
4293 x1a->tbl = (x1node*)malloc(
4294 (sizeof(x1node) + sizeof(x1node*))*1024 );
4295 if( x1a->tbl==0 ){
4296 free(x1a);
4297 x1a = 0;
4298 }else{
4299 int i;
4300 x1a->ht = (x1node**)&(x1a->tbl[1024]);
4301 for(i=0; i<1024; i++) x1a->ht[i] = 0;
4302 }
4303 }
4304}
4305/* Insert a new record into the array. Return TRUE if successful.
4306** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004307int Strsafe_insert(const char *data)
drh75897232000-05-29 14:26:00 +00004308{
4309 x1node *np;
4310 int h;
4311 int ph;
4312
4313 if( x1a==0 ) return 0;
4314 ph = strhash(data);
4315 h = ph & (x1a->size-1);
4316 np = x1a->ht[h];
4317 while( np ){
4318 if( strcmp(np->data,data)==0 ){
4319 /* An existing entry with the same key is found. */
4320 /* Fail because overwrite is not allows. */
4321 return 0;
4322 }
4323 np = np->next;
4324 }
4325 if( x1a->count>=x1a->size ){
4326 /* Need to make the hash table bigger */
4327 int i,size;
4328 struct s_x1 array;
4329 array.size = size = x1a->size*2;
4330 array.count = x1a->count;
4331 array.tbl = (x1node*)malloc(
4332 (sizeof(x1node) + sizeof(x1node*))*size );
4333 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4334 array.ht = (x1node**)&(array.tbl[size]);
4335 for(i=0; i<size; i++) array.ht[i] = 0;
4336 for(i=0; i<x1a->count; i++){
4337 x1node *oldnp, *newnp;
4338 oldnp = &(x1a->tbl[i]);
4339 h = strhash(oldnp->data) & (size-1);
4340 newnp = &(array.tbl[i]);
4341 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4342 newnp->next = array.ht[h];
4343 newnp->data = oldnp->data;
4344 newnp->from = &(array.ht[h]);
4345 array.ht[h] = newnp;
4346 }
4347 free(x1a->tbl);
4348 *x1a = array;
4349 }
4350 /* Insert the new data */
4351 h = ph & (x1a->size-1);
4352 np = &(x1a->tbl[x1a->count++]);
4353 np->data = data;
4354 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
4355 np->next = x1a->ht[h];
4356 x1a->ht[h] = np;
4357 np->from = &(x1a->ht[h]);
4358 return 1;
4359}
4360
4361/* Return a pointer to data assigned to the given key. Return NULL
4362** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004363const char *Strsafe_find(const char *key)
drh75897232000-05-29 14:26:00 +00004364{
4365 int h;
4366 x1node *np;
4367
4368 if( x1a==0 ) return 0;
4369 h = strhash(key) & (x1a->size-1);
4370 np = x1a->ht[h];
4371 while( np ){
4372 if( strcmp(np->data,key)==0 ) break;
4373 np = np->next;
4374 }
4375 return np ? np->data : 0;
4376}
4377
4378/* Return a pointer to the (terminal or nonterminal) symbol "x".
4379** Create a new symbol if this is the first time "x" has been seen.
4380*/
icculus9e44cf12010-02-14 17:14:22 +00004381struct symbol *Symbol_new(const char *x)
drh75897232000-05-29 14:26:00 +00004382{
4383 struct symbol *sp;
4384
4385 sp = Symbol_find(x);
4386 if( sp==0 ){
drh9892c5d2007-12-21 00:02:11 +00004387 sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
drh75897232000-05-29 14:26:00 +00004388 MemoryCheck(sp);
4389 sp->name = Strsafe(x);
4390 sp->type = isupper(*x) ? TERMINAL : NONTERMINAL;
4391 sp->rule = 0;
drh0bd1f4e2002-06-06 18:54:39 +00004392 sp->fallback = 0;
drh75897232000-05-29 14:26:00 +00004393 sp->prec = -1;
4394 sp->assoc = UNK;
4395 sp->firstset = 0;
drhaa9f1122007-08-23 02:50:56 +00004396 sp->lambda = LEMON_FALSE;
drh75897232000-05-29 14:26:00 +00004397 sp->destructor = 0;
drh4dc8ef52008-07-01 17:13:57 +00004398 sp->destLineno = 0;
drh75897232000-05-29 14:26:00 +00004399 sp->datatype = 0;
drhc4dd3fd2008-01-22 01:48:05 +00004400 sp->useCnt = 0;
drh75897232000-05-29 14:26:00 +00004401 Symbol_insert(sp,sp->name);
4402 }
drhc4dd3fd2008-01-22 01:48:05 +00004403 sp->useCnt++;
drh75897232000-05-29 14:26:00 +00004404 return sp;
4405}
4406
drh60d31652004-02-22 00:08:04 +00004407/* Compare two symbols for working purposes
4408**
4409** Symbols that begin with upper case letters (terminals or tokens)
4410** must sort before symbols that begin with lower case letters
4411** (non-terminals). Other than that, the order does not matter.
4412**
4413** We find experimentally that leaving the symbols in their original
4414** order (the order they appeared in the grammar file) gives the
4415** smallest parser tables in SQLite.
4416*/
icculus9e44cf12010-02-14 17:14:22 +00004417int Symbolcmpp(const void *_a, const void *_b)
4418{
4419 const struct symbol **a = (const struct symbol **) _a;
4420 const struct symbol **b = (const struct symbol **) _b;
drh60d31652004-02-22 00:08:04 +00004421 int i1 = (**a).index + 10000000*((**a).name[0]>'Z');
4422 int i2 = (**b).index + 10000000*((**b).name[0]>'Z');
drhe594bc32009-11-03 13:02:25 +00004423 assert( i1!=i2 || strcmp((**a).name,(**b).name)==0 );
drh60d31652004-02-22 00:08:04 +00004424 return i1-i2;
drh75897232000-05-29 14:26:00 +00004425}
4426
4427/* There is one instance of the following structure for each
4428** associative array of type "x2".
4429*/
4430struct s_x2 {
4431 int size; /* The number of available slots. */
4432 /* Must be a power of 2 greater than or */
4433 /* equal to 1 */
4434 int count; /* Number of currently slots filled */
4435 struct s_x2node *tbl; /* The data stored here */
4436 struct s_x2node **ht; /* Hash table for lookups */
4437};
4438
4439/* There is one instance of this structure for every data element
4440** in an associative array of type "x2".
4441*/
4442typedef struct s_x2node {
icculus9e44cf12010-02-14 17:14:22 +00004443 struct symbol *data; /* The data */
4444 const char *key; /* The key */
drh75897232000-05-29 14:26:00 +00004445 struct s_x2node *next; /* Next entry with the same hash */
4446 struct s_x2node **from; /* Previous link */
4447} x2node;
4448
4449/* There is only one instance of the array, which is the following */
4450static struct s_x2 *x2a;
4451
4452/* Allocate a new associative array */
4453void Symbol_init(){
4454 if( x2a ) return;
4455 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
4456 if( x2a ){
4457 x2a->size = 128;
4458 x2a->count = 0;
4459 x2a->tbl = (x2node*)malloc(
4460 (sizeof(x2node) + sizeof(x2node*))*128 );
4461 if( x2a->tbl==0 ){
4462 free(x2a);
4463 x2a = 0;
4464 }else{
4465 int i;
4466 x2a->ht = (x2node**)&(x2a->tbl[128]);
4467 for(i=0; i<128; i++) x2a->ht[i] = 0;
4468 }
4469 }
4470}
4471/* Insert a new record into the array. Return TRUE if successful.
4472** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004473int Symbol_insert(struct symbol *data, const char *key)
drh75897232000-05-29 14:26:00 +00004474{
4475 x2node *np;
4476 int h;
4477 int ph;
4478
4479 if( x2a==0 ) return 0;
4480 ph = strhash(key);
4481 h = ph & (x2a->size-1);
4482 np = x2a->ht[h];
4483 while( np ){
4484 if( strcmp(np->key,key)==0 ){
4485 /* An existing entry with the same key is found. */
4486 /* Fail because overwrite is not allows. */
4487 return 0;
4488 }
4489 np = np->next;
4490 }
4491 if( x2a->count>=x2a->size ){
4492 /* Need to make the hash table bigger */
4493 int i,size;
4494 struct s_x2 array;
4495 array.size = size = x2a->size*2;
4496 array.count = x2a->count;
4497 array.tbl = (x2node*)malloc(
4498 (sizeof(x2node) + sizeof(x2node*))*size );
4499 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4500 array.ht = (x2node**)&(array.tbl[size]);
4501 for(i=0; i<size; i++) array.ht[i] = 0;
4502 for(i=0; i<x2a->count; i++){
4503 x2node *oldnp, *newnp;
4504 oldnp = &(x2a->tbl[i]);
4505 h = strhash(oldnp->key) & (size-1);
4506 newnp = &(array.tbl[i]);
4507 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4508 newnp->next = array.ht[h];
4509 newnp->key = oldnp->key;
4510 newnp->data = oldnp->data;
4511 newnp->from = &(array.ht[h]);
4512 array.ht[h] = newnp;
4513 }
4514 free(x2a->tbl);
4515 *x2a = array;
4516 }
4517 /* Insert the new data */
4518 h = ph & (x2a->size-1);
4519 np = &(x2a->tbl[x2a->count++]);
4520 np->key = key;
4521 np->data = data;
4522 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
4523 np->next = x2a->ht[h];
4524 x2a->ht[h] = np;
4525 np->from = &(x2a->ht[h]);
4526 return 1;
4527}
4528
4529/* Return a pointer to data assigned to the given key. Return NULL
4530** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004531struct symbol *Symbol_find(const char *key)
drh75897232000-05-29 14:26:00 +00004532{
4533 int h;
4534 x2node *np;
4535
4536 if( x2a==0 ) return 0;
4537 h = strhash(key) & (x2a->size-1);
4538 np = x2a->ht[h];
4539 while( np ){
4540 if( strcmp(np->key,key)==0 ) break;
4541 np = np->next;
4542 }
4543 return np ? np->data : 0;
4544}
4545
4546/* Return the n-th data. Return NULL if n is out of range. */
icculus9e44cf12010-02-14 17:14:22 +00004547struct symbol *Symbol_Nth(int n)
drh75897232000-05-29 14:26:00 +00004548{
4549 struct symbol *data;
4550 if( x2a && n>0 && n<=x2a->count ){
4551 data = x2a->tbl[n-1].data;
4552 }else{
4553 data = 0;
4554 }
4555 return data;
4556}
4557
4558/* Return the size of the array */
4559int Symbol_count()
4560{
4561 return x2a ? x2a->count : 0;
4562}
4563
4564/* Return an array of pointers to all data in the table.
4565** The array is obtained from malloc. Return NULL if memory allocation
4566** problems, or if the array is empty. */
4567struct symbol **Symbol_arrayof()
4568{
4569 struct symbol **array;
4570 int i,size;
4571 if( x2a==0 ) return 0;
4572 size = x2a->count;
drh9892c5d2007-12-21 00:02:11 +00004573 array = (struct symbol **)calloc(size, sizeof(struct symbol *));
drh75897232000-05-29 14:26:00 +00004574 if( array ){
4575 for(i=0; i<size; i++) array[i] = x2a->tbl[i].data;
4576 }
4577 return array;
4578}
4579
4580/* Compare two configurations */
icculus9e44cf12010-02-14 17:14:22 +00004581int Configcmp(const char *_a,const char *_b)
drh75897232000-05-29 14:26:00 +00004582{
icculus9e44cf12010-02-14 17:14:22 +00004583 const struct config *a = (struct config *) _a;
4584 const struct config *b = (struct config *) _b;
drh75897232000-05-29 14:26:00 +00004585 int x;
4586 x = a->rp->index - b->rp->index;
4587 if( x==0 ) x = a->dot - b->dot;
4588 return x;
4589}
4590
4591/* Compare two states */
icculus9e44cf12010-02-14 17:14:22 +00004592PRIVATE int statecmp(struct config *a, struct config *b)
drh75897232000-05-29 14:26:00 +00004593{
4594 int rc;
4595 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
4596 rc = a->rp->index - b->rp->index;
4597 if( rc==0 ) rc = a->dot - b->dot;
4598 }
4599 if( rc==0 ){
4600 if( a ) rc = 1;
4601 if( b ) rc = -1;
4602 }
4603 return rc;
4604}
4605
4606/* Hash a state */
icculus9e44cf12010-02-14 17:14:22 +00004607PRIVATE int statehash(struct config *a)
drh75897232000-05-29 14:26:00 +00004608{
4609 int h=0;
4610 while( a ){
4611 h = h*571 + a->rp->index*37 + a->dot;
4612 a = a->bp;
4613 }
4614 return h;
4615}
4616
4617/* Allocate a new state structure */
4618struct state *State_new()
4619{
icculus9e44cf12010-02-14 17:14:22 +00004620 struct state *newstate;
4621 newstate = (struct state *)calloc(1, sizeof(struct state) );
4622 MemoryCheck(newstate);
4623 return newstate;
drh75897232000-05-29 14:26:00 +00004624}
4625
4626/* There is one instance of the following structure for each
4627** associative array of type "x3".
4628*/
4629struct s_x3 {
4630 int size; /* The number of available slots. */
4631 /* Must be a power of 2 greater than or */
4632 /* equal to 1 */
4633 int count; /* Number of currently slots filled */
4634 struct s_x3node *tbl; /* The data stored here */
4635 struct s_x3node **ht; /* Hash table for lookups */
4636};
4637
4638/* There is one instance of this structure for every data element
4639** in an associative array of type "x3".
4640*/
4641typedef struct s_x3node {
4642 struct state *data; /* The data */
4643 struct config *key; /* The key */
4644 struct s_x3node *next; /* Next entry with the same hash */
4645 struct s_x3node **from; /* Previous link */
4646} x3node;
4647
4648/* There is only one instance of the array, which is the following */
4649static struct s_x3 *x3a;
4650
4651/* Allocate a new associative array */
4652void State_init(){
4653 if( x3a ) return;
4654 x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
4655 if( x3a ){
4656 x3a->size = 128;
4657 x3a->count = 0;
4658 x3a->tbl = (x3node*)malloc(
4659 (sizeof(x3node) + sizeof(x3node*))*128 );
4660 if( x3a->tbl==0 ){
4661 free(x3a);
4662 x3a = 0;
4663 }else{
4664 int i;
4665 x3a->ht = (x3node**)&(x3a->tbl[128]);
4666 for(i=0; i<128; i++) x3a->ht[i] = 0;
4667 }
4668 }
4669}
4670/* Insert a new record into the array. Return TRUE if successful.
4671** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004672int State_insert(struct state *data, struct config *key)
drh75897232000-05-29 14:26:00 +00004673{
4674 x3node *np;
4675 int h;
4676 int ph;
4677
4678 if( x3a==0 ) return 0;
4679 ph = statehash(key);
4680 h = ph & (x3a->size-1);
4681 np = x3a->ht[h];
4682 while( np ){
4683 if( statecmp(np->key,key)==0 ){
4684 /* An existing entry with the same key is found. */
4685 /* Fail because overwrite is not allows. */
4686 return 0;
4687 }
4688 np = np->next;
4689 }
4690 if( x3a->count>=x3a->size ){
4691 /* Need to make the hash table bigger */
4692 int i,size;
4693 struct s_x3 array;
4694 array.size = size = x3a->size*2;
4695 array.count = x3a->count;
4696 array.tbl = (x3node*)malloc(
4697 (sizeof(x3node) + sizeof(x3node*))*size );
4698 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4699 array.ht = (x3node**)&(array.tbl[size]);
4700 for(i=0; i<size; i++) array.ht[i] = 0;
4701 for(i=0; i<x3a->count; i++){
4702 x3node *oldnp, *newnp;
4703 oldnp = &(x3a->tbl[i]);
4704 h = statehash(oldnp->key) & (size-1);
4705 newnp = &(array.tbl[i]);
4706 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4707 newnp->next = array.ht[h];
4708 newnp->key = oldnp->key;
4709 newnp->data = oldnp->data;
4710 newnp->from = &(array.ht[h]);
4711 array.ht[h] = newnp;
4712 }
4713 free(x3a->tbl);
4714 *x3a = array;
4715 }
4716 /* Insert the new data */
4717 h = ph & (x3a->size-1);
4718 np = &(x3a->tbl[x3a->count++]);
4719 np->key = key;
4720 np->data = data;
4721 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
4722 np->next = x3a->ht[h];
4723 x3a->ht[h] = np;
4724 np->from = &(x3a->ht[h]);
4725 return 1;
4726}
4727
4728/* Return a pointer to data assigned to the given key. Return NULL
4729** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004730struct state *State_find(struct config *key)
drh75897232000-05-29 14:26:00 +00004731{
4732 int h;
4733 x3node *np;
4734
4735 if( x3a==0 ) return 0;
4736 h = statehash(key) & (x3a->size-1);
4737 np = x3a->ht[h];
4738 while( np ){
4739 if( statecmp(np->key,key)==0 ) break;
4740 np = np->next;
4741 }
4742 return np ? np->data : 0;
4743}
4744
4745/* Return an array of pointers to all data in the table.
4746** The array is obtained from malloc. Return NULL if memory allocation
4747** problems, or if the array is empty. */
4748struct state **State_arrayof()
4749{
4750 struct state **array;
4751 int i,size;
4752 if( x3a==0 ) return 0;
4753 size = x3a->count;
4754 array = (struct state **)malloc( sizeof(struct state *)*size );
4755 if( array ){
4756 for(i=0; i<size; i++) array[i] = x3a->tbl[i].data;
4757 }
4758 return array;
4759}
4760
4761/* Hash a configuration */
icculus9e44cf12010-02-14 17:14:22 +00004762PRIVATE int confighash(struct config *a)
drh75897232000-05-29 14:26:00 +00004763{
4764 int h=0;
4765 h = h*571 + a->rp->index*37 + a->dot;
4766 return h;
4767}
4768
4769/* There is one instance of the following structure for each
4770** associative array of type "x4".
4771*/
4772struct s_x4 {
4773 int size; /* The number of available slots. */
4774 /* Must be a power of 2 greater than or */
4775 /* equal to 1 */
4776 int count; /* Number of currently slots filled */
4777 struct s_x4node *tbl; /* The data stored here */
4778 struct s_x4node **ht; /* Hash table for lookups */
4779};
4780
4781/* There is one instance of this structure for every data element
4782** in an associative array of type "x4".
4783*/
4784typedef struct s_x4node {
4785 struct config *data; /* The data */
4786 struct s_x4node *next; /* Next entry with the same hash */
4787 struct s_x4node **from; /* Previous link */
4788} x4node;
4789
4790/* There is only one instance of the array, which is the following */
4791static struct s_x4 *x4a;
4792
4793/* Allocate a new associative array */
4794void Configtable_init(){
4795 if( x4a ) return;
4796 x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
4797 if( x4a ){
4798 x4a->size = 64;
4799 x4a->count = 0;
4800 x4a->tbl = (x4node*)malloc(
4801 (sizeof(x4node) + sizeof(x4node*))*64 );
4802 if( x4a->tbl==0 ){
4803 free(x4a);
4804 x4a = 0;
4805 }else{
4806 int i;
4807 x4a->ht = (x4node**)&(x4a->tbl[64]);
4808 for(i=0; i<64; i++) x4a->ht[i] = 0;
4809 }
4810 }
4811}
4812/* Insert a new record into the array. Return TRUE if successful.
4813** Prior data with the same key is NOT overwritten */
icculus9e44cf12010-02-14 17:14:22 +00004814int Configtable_insert(struct config *data)
drh75897232000-05-29 14:26:00 +00004815{
4816 x4node *np;
4817 int h;
4818 int ph;
4819
4820 if( x4a==0 ) return 0;
4821 ph = confighash(data);
4822 h = ph & (x4a->size-1);
4823 np = x4a->ht[h];
4824 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00004825 if( Configcmp((const char *) np->data,(const char *) data)==0 ){
drh75897232000-05-29 14:26:00 +00004826 /* An existing entry with the same key is found. */
4827 /* Fail because overwrite is not allows. */
4828 return 0;
4829 }
4830 np = np->next;
4831 }
4832 if( x4a->count>=x4a->size ){
4833 /* Need to make the hash table bigger */
4834 int i,size;
4835 struct s_x4 array;
4836 array.size = size = x4a->size*2;
4837 array.count = x4a->count;
4838 array.tbl = (x4node*)malloc(
4839 (sizeof(x4node) + sizeof(x4node*))*size );
4840 if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
4841 array.ht = (x4node**)&(array.tbl[size]);
4842 for(i=0; i<size; i++) array.ht[i] = 0;
4843 for(i=0; i<x4a->count; i++){
4844 x4node *oldnp, *newnp;
4845 oldnp = &(x4a->tbl[i]);
4846 h = confighash(oldnp->data) & (size-1);
4847 newnp = &(array.tbl[i]);
4848 if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
4849 newnp->next = array.ht[h];
4850 newnp->data = oldnp->data;
4851 newnp->from = &(array.ht[h]);
4852 array.ht[h] = newnp;
4853 }
4854 free(x4a->tbl);
4855 *x4a = array;
4856 }
4857 /* Insert the new data */
4858 h = ph & (x4a->size-1);
4859 np = &(x4a->tbl[x4a->count++]);
4860 np->data = data;
4861 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
4862 np->next = x4a->ht[h];
4863 x4a->ht[h] = np;
4864 np->from = &(x4a->ht[h]);
4865 return 1;
4866}
4867
4868/* Return a pointer to data assigned to the given key. Return NULL
4869** if no such key. */
icculus9e44cf12010-02-14 17:14:22 +00004870struct config *Configtable_find(struct config *key)
drh75897232000-05-29 14:26:00 +00004871{
4872 int h;
4873 x4node *np;
4874
4875 if( x4a==0 ) return 0;
4876 h = confighash(key) & (x4a->size-1);
4877 np = x4a->ht[h];
4878 while( np ){
icculus9e44cf12010-02-14 17:14:22 +00004879 if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
drh75897232000-05-29 14:26:00 +00004880 np = np->next;
4881 }
4882 return np ? np->data : 0;
4883}
4884
4885/* Remove all data from the table. Pass each data to the function "f"
4886** as it is removed. ("f" may be null to avoid this step.) */
icculus9e44cf12010-02-14 17:14:22 +00004887void Configtable_clear(int(*f)(struct config *))
drh75897232000-05-29 14:26:00 +00004888{
4889 int i;
4890 if( x4a==0 || x4a->count==0 ) return;
4891 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
4892 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
4893 x4a->count = 0;
4894 return;
4895}