Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(621)

Side by Side Diff: third_party/sqlite/src/tool/lemon.c

Issue 5626002: Update sqlite to 3.7.3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/third_party/sqlite/src
Patch Set: Remove misc change. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/src/tool/genfkey.test ('k') | third_party/sqlite/src/tool/lempar.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 ** This file contains all sources (including headers) to the LEMON 2 ** This file contains all sources (including headers) to the LEMON
3 ** LALR(1) parser generator. The sources have been combined into a 3 ** LALR(1) parser generator. The sources have been combined into a
4 ** single file to make it easy to include LEMON in the source tree 4 ** single file to make it easy to include LEMON in the source tree
5 ** and Makefile of another program. 5 ** and Makefile of another program.
6 ** 6 **
7 ** The author of this program disclaims copyright. 7 ** The author of this program disclaims copyright.
8 */ 8 */
9 #include <stdio.h> 9 #include <stdio.h>
10 #include <stdarg.h> 10 #include <stdarg.h>
(...skipping 16 matching lines...) Expand all
27 27
28 /* #define PRIVATE static */ 28 /* #define PRIVATE static */
29 #define PRIVATE 29 #define PRIVATE
30 30
31 #ifdef TEST 31 #ifdef TEST
32 #define MAXRHS 5 /* Set low to exercise exception code */ 32 #define MAXRHS 5 /* Set low to exercise exception code */
33 #else 33 #else
34 #define MAXRHS 1000 34 #define MAXRHS 1000
35 #endif 35 #endif
36 36
37 static int showPrecedenceConflict = 0;
38 static const char **made_files = NULL;
39 static int made_files_count = 0;
40 static int successful_exit = 0;
41 static void LemonAtExit(void)
42 {
43 /* if we failed, delete (most) files we made, to unconfuse build tools. */
44 int i;
45 for (i = 0; i < made_files_count; i++) {
46 if (!successful_exit) {
47 remove(made_files[i]);
48 }
49 }
50 free(made_files);
51 made_files_count = 0;
52 made_files = NULL;
53 }
54
37 static char *msort(char*,char**,int(*)(const char*,const char*)); 55 static char *msort(char*,char**,int(*)(const char*,const char*));
38 56
39 /* 57 /*
40 ** Compilers are getting increasingly pedantic about type conversions 58 ** Compilers are getting increasingly pedantic about type conversions
41 ** as C evolves ever closer to Ada.... To work around the latest problems 59 ** as C evolves ever closer to Ada.... To work around the latest problems
42 ** we have to define the following variant of strlen(). 60 ** we have to define the following variant of strlen().
43 */ 61 */
44 #define lemonStrlen(X) ((int)strlen(X)) 62 #define lemonStrlen(X) ((int)strlen(X))
45 63
64 /* a few forward declarations... */
65 struct rule;
66 struct lemon;
67 struct action;
68
46 static struct action *Action_new(void); 69 static struct action *Action_new(void);
47 static struct action *Action_sort(struct action *); 70 static struct action *Action_sort(struct action *);
48 71
49 /********** From the file "build.h" ************************************/ 72 /********** From the file "build.h" ************************************/
50 void FindRulePrecedences(); 73 void FindRulePrecedences();
51 void FindFirstSets(); 74 void FindFirstSets();
52 void FindStates(); 75 void FindStates();
53 void FindLinks(); 76 void FindLinks();
54 void FindFollowSets(); 77 void FindFollowSets();
55 void FindActions(); 78 void FindActions();
56 79
57 /********* From the file "configlist.h" *********************************/ 80 /********* From the file "configlist.h" *********************************/
58 void Configlist_init(/* void */); 81 void Configlist_init(void);
59 struct config *Configlist_add(/* struct rule *, int */); 82 struct config *Configlist_add(struct rule *, int);
60 struct config *Configlist_addbasis(/* struct rule *, int */); 83 struct config *Configlist_addbasis(struct rule *, int);
61 void Configlist_closure(/* void */); 84 void Configlist_closure(struct lemon *);
62 void Configlist_sort(/* void */); 85 void Configlist_sort(void);
63 void Configlist_sortbasis(/* void */); 86 void Configlist_sortbasis(void);
64 struct config *Configlist_return(/* void */); 87 struct config *Configlist_return(void);
65 struct config *Configlist_basis(/* void */); 88 struct config *Configlist_basis(void);
66 void Configlist_eat(/* struct config * */); 89 void Configlist_eat(struct config *);
67 void Configlist_reset(/* void */); 90 void Configlist_reset(void);
68 91
69 /********* From the file "error.h" ***************************************/ 92 /********* From the file "error.h" ***************************************/
70 void ErrorMsg(const char *, int,const char *, ...); 93 void ErrorMsg(const char *, int,const char *, ...);
71 94
72 /****** From the file "option.h" ******************************************/ 95 /****** From the file "option.h" ******************************************/
96 enum option_type { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR,
97 OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR};
73 struct s_options { 98 struct s_options {
74 enum { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR, 99 enum option_type type;
75 OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type; 100 const char *label;
76 char *label;
77 char *arg; 101 char *arg;
78 char *message; 102 const char *message;
79 }; 103 };
80 int OptInit(/* char**,struct s_options*,FILE* */); 104 int OptInit(char**,struct s_options*,FILE*);
81 int OptNArgs(/* void */); 105 int OptNArgs(void);
82 char *OptArg(/* int */); 106 char *OptArg(int);
83 void OptErr(/* int */); 107 void OptErr(int);
84 void OptPrint(/* void */); 108 void OptPrint(void);
85 109
86 /******** From the file "parse.h" *****************************************/ 110 /******** From the file "parse.h" *****************************************/
87 void Parse(/* struct lemon *lemp */); 111 void Parse(struct lemon *lemp);
88 112
89 /********* From the file "plink.h" ***************************************/ 113 /********* From the file "plink.h" ***************************************/
90 struct plink *Plink_new(/* void */); 114 struct plink *Plink_new(void);
91 void Plink_add(/* struct plink **, struct config * */); 115 void Plink_add(struct plink **, struct config *);
92 void Plink_copy(/* struct plink **, struct plink * */); 116 void Plink_copy(struct plink **, struct plink *);
93 void Plink_delete(/* struct plink * */); 117 void Plink_delete(struct plink *);
94 118
95 /********** From the file "report.h" *************************************/ 119 /********** From the file "report.h" *************************************/
96 void Reprint(/* struct lemon * */); 120 void Reprint(struct lemon *);
97 void ReportOutput(/* struct lemon * */); 121 void ReportOutput(struct lemon *);
98 void ReportTable(/* struct lemon * */); 122 void ReportTable(struct lemon *, int);
99 void ReportHeader(/* struct lemon * */); 123 void ReportHeader(struct lemon *);
100 void CompressTables(/* struct lemon * */); 124 void CompressTables(struct lemon *);
101 void ResortStates(/* struct lemon * */); 125 void ResortStates(struct lemon *);
102 126
103 /********** From the file "set.h" ****************************************/ 127 /********** From the file "set.h" ****************************************/
104 void SetSize(/* int N */); /* All sets will be of size N */ 128 void SetSize(int); /* All sets will be of size N */
105 char *SetNew(/* void */); /* A new set for element 0..N */ 129 char *SetNew(void); /* A new set for element 0..N */
106 void SetFree(/* char* */); /* Deallocate a set */ 130 void SetFree(char*); /* Deallocate a set */
107 131
108 int SetAdd(/* char*,int */); /* Add element to a set */ 132 char *SetNew(void); /* A new set for element 0..N */
109 int SetUnion(/* char *A,char *B */); /* A <- A U B, thru element N */ 133 int SetAdd(char*,int); /* Add element to a set */
110 134 int SetUnion(char *,char *); /* A <- A U B, thru element N */
111 #define SetFind(X,Y) (X[Y]) /* True if Y is in set X */ 135 #define SetFind(X,Y) (X[Y]) /* True if Y is in set X */
112 136
113 /********** From the file "struct.h" *************************************/ 137 /********** From the file "struct.h" *************************************/
114 /* 138 /*
115 ** Principal data structures for the LEMON parser generator. 139 ** Principal data structures for the LEMON parser generator.
116 */ 140 */
117 141
118 typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean; 142 typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean;
119 143
120 /* Symbols (terminals and nonterminals) of the grammar are stored 144 /* Symbols (terminals and nonterminals) of the grammar are stored
121 ** in the following: */ 145 ** in the following: */
122 struct symbol { 146 enum symbol_type {
123 char *name; /* Name of the symbol */ 147 TERMINAL,
124 int index; /* Index number for this symbol */ 148 NONTERMINAL,
125 enum { 149 MULTITERMINAL
126 TERMINAL, 150 };
127 NONTERMINAL, 151 enum e_assoc {
128 MULTITERMINAL
129 } type; /* Symbols are all either TERMINALS or NTs */
130 struct rule *rule; /* Linked list of rules of this (if an NT) */
131 struct symbol *fallback; /* fallback token in case this token doesn't parse */
132 int prec; /* Precedence if defined (-1 otherwise) */
133 enum e_assoc {
134 LEFT, 152 LEFT,
135 RIGHT, 153 RIGHT,
136 NONE, 154 NONE,
137 UNK 155 UNK
138 } assoc; /* Associativity if precedence is defined */ 156 };
157 struct 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 */
139 char *firstset; /* First-set for all rules of this symbol */ 165 char *firstset; /* First-set for all rules of this symbol */
140 Boolean lambda; /* True if NT and can generate an empty string */ 166 Boolean lambda; /* True if NT and can generate an empty string */
141 int useCnt; /* Number of times used */ 167 int useCnt; /* Number of times used */
142 char *destructor; /* Code which executes whenever this symbol is 168 char *destructor; /* Code which executes whenever this symbol is
143 ** popped from the stack during error processing */ 169 ** popped from the stack during error processing */
144 int destLineno; /* Line number for start of destructor */ 170 int destLineno; /* Line number for start of destructor */
145 char *datatype; /* The data type of information held by this 171 char *datatype; /* The data type of information held by this
146 ** object. Only used if type==NONTERMINAL */ 172 ** object. Only used if type==NONTERMINAL */
147 int dtnum; /* The data type number. In the parser, the value 173 int dtnum; /* The data type number. In the parser, the value
148 ** stack is a union. The .yy%d element of this 174 ** stack is a union. The .yy%d element of this
149 ** union is the correct data type for this object */ 175 ** union is the correct data type for this object */
150 /* The following fields are used by MULTITERMINALs only */ 176 /* The following fields are used by MULTITERMINALs only */
151 int nsubsym; /* Number of constituent symbols in the MULTI */ 177 int nsubsym; /* Number of constituent symbols in the MULTI */
152 struct symbol **subsym; /* Array of constituent symbols */ 178 struct symbol **subsym; /* Array of constituent symbols */
153 }; 179 };
154 180
155 /* Each production rule in the grammar is stored in the following 181 /* Each production rule in the grammar is stored in the following
156 ** structure. */ 182 ** structure. */
157 struct rule { 183 struct rule {
158 struct symbol *lhs; /* Left-hand side of the rule */ 184 struct symbol *lhs; /* Left-hand side of the rule */
159 char *lhsalias; /* Alias for the LHS (NULL if none) */ 185 const char *lhsalias; /* Alias for the LHS (NULL if none) */
160 int lhsStart; /* True if left-hand side is the start symbol */ 186 int lhsStart; /* True if left-hand side is the start symbol */
161 int ruleline; /* Line number for the rule */ 187 int ruleline; /* Line number for the rule */
162 int nrhs; /* Number of RHS symbols */ 188 int nrhs; /* Number of RHS symbols */
163 struct symbol **rhs; /* The RHS symbols */ 189 struct symbol **rhs; /* The RHS symbols */
164 char **rhsalias; /* An alias for each RHS symbol (NULL if none) */ 190 const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */
165 int line; /* Line number at which code begins */ 191 int line; /* Line number at which code begins */
166 char *code; /* The code executed when this rule is reduced */ 192 const char *code; /* The code executed when this rule is reduced */
167 struct symbol *precsym; /* Precedence symbol for this rule */ 193 struct symbol *precsym; /* Precedence symbol for this rule */
168 int index; /* An index number for this rule */ 194 int index; /* An index number for this rule */
169 Boolean canReduce; /* True if this rule is ever reduced */ 195 Boolean canReduce; /* True if this rule is ever reduced */
170 struct rule *nextlhs; /* Next rule with the same LHS */ 196 struct rule *nextlhs; /* Next rule with the same LHS */
171 struct rule *next; /* Next rule in the global list */ 197 struct rule *next; /* Next rule in the global list */
172 }; 198 };
173 199
174 /* A configuration is a production rule of the grammar together with 200 /* A configuration is a production rule of the grammar together with
175 ** a mark (dot) showing how much of that rule has been processed so far. 201 ** a mark (dot) showing how much of that rule has been processed so far.
176 ** Configurations also contain a follow-set which is a list of terminal 202 ** Configurations also contain a follow-set which is a list of terminal
177 ** symbols which are allowed to immediately follow the end of the rule. 203 ** symbols which are allowed to immediately follow the end of the rule.
178 ** Every configuration is recorded as an instance of the following: */ 204 ** Every configuration is recorded as an instance of the following: */
205 enum cfgstatus {
206 COMPLETE,
207 INCOMPLETE
208 };
179 struct config { 209 struct config {
180 struct rule *rp; /* The rule upon which the configuration is based */ 210 struct rule *rp; /* The rule upon which the configuration is based */
181 int dot; /* The parse point */ 211 int dot; /* The parse point */
182 char *fws; /* Follow-set for this configuration only */ 212 char *fws; /* Follow-set for this configuration only */
183 struct plink *fplp; /* Follow-set forward propagation links */ 213 struct plink *fplp; /* Follow-set forward propagation links */
184 struct plink *bplp; /* Follow-set backwards propagation links */ 214 struct plink *bplp; /* Follow-set backwards propagation links */
185 struct state *stp; /* Pointer to state which contains this */ 215 struct state *stp; /* Pointer to state which contains this */
186 enum { 216 enum cfgstatus status; /* used during followset and shift computations */
187 COMPLETE, /* The status is used during followset and */
188 INCOMPLETE /* shift computations */
189 } status;
190 struct config *next; /* Next configuration in the state */ 217 struct config *next; /* Next configuration in the state */
191 struct config *bp; /* The next basis configuration */ 218 struct config *bp; /* The next basis configuration */
192 }; 219 };
193 220
221 enum 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
194 /* Every shift or reduce operation is stored as one of the following */ 234 /* Every shift or reduce operation is stored as one of the following */
195 struct action { 235 struct action {
196 struct symbol *sp; /* The look-ahead symbol */ 236 struct symbol *sp; /* The look-ahead symbol */
197 enum e_action { 237 enum e_action type;
198 SHIFT,
199 ACCEPT,
200 REDUCE,
201 ERROR,
202 SSCONFLICT, /* A shift/shift conflict */
203 SRCONFLICT, /* Was a reduce, but part of a conflict */
204 RRCONFLICT, /* Was a reduce, but part of a conflict */
205 SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
206 RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
207 NOT_USED /* Deleted by compression */
208 } type;
209 union { 238 union {
210 struct state *stp; /* The new state, if a shift */ 239 struct state *stp; /* The new state, if a shift */
211 struct rule *rp; /* The rule, if a reduce */ 240 struct rule *rp; /* The rule, if a reduce */
212 } x; 241 } x;
213 struct action *next; /* Next action for this state */ 242 struct action *next; /* Next action for this state */
214 struct action *collide; /* Next action with the same hash */ 243 struct action *collide; /* Next action with the same hash */
215 }; 244 };
216 245
217 /* Each state of the generated parser's finite state machine 246 /* Each state of the generated parser's finite state machine
218 ** is encoded as an instance of the following structure. */ 247 ** is encoded as an instance of the following structure. */
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 ** All code in this file has been automatically generated 314 ** All code in this file has been automatically generated
286 ** from a specification in the file 315 ** from a specification in the file
287 ** "table.q" 316 ** "table.q"
288 ** by the associative array code building program "aagen". 317 ** by the associative array code building program "aagen".
289 ** Do not edit this file! Instead, edit the specification 318 ** Do not edit this file! Instead, edit the specification
290 ** file, then rerun aagen. 319 ** file, then rerun aagen.
291 */ 320 */
292 /* 321 /*
293 ** Code for processing tables in the LEMON parser generator. 322 ** Code for processing tables in the LEMON parser generator.
294 */ 323 */
295
296 /* Routines for handling a strings */ 324 /* Routines for handling a strings */
297 325
298 char *Strsafe(); 326 const char *Strsafe(const char *);
299 327
300 void Strsafe_init(/* void */); 328 void Strsafe_init(void);
301 int Strsafe_insert(/* char * */); 329 int Strsafe_insert(const char *);
302 char *Strsafe_find(/* char * */); 330 const char *Strsafe_find(const char *);
303 331
304 /* Routines for handling symbols of the grammar */ 332 /* Routines for handling symbols of the grammar */
305 333
306 struct symbol *Symbol_new(); 334 struct symbol *Symbol_new(const char *);
307 int Symbolcmpp(/* struct symbol **, struct symbol ** */); 335 int Symbolcmpp(const void *, const void *);
308 void Symbol_init(/* void */); 336 void Symbol_init(void);
309 int Symbol_insert(/* struct symbol *, char * */); 337 int Symbol_insert(struct symbol *, const char *);
310 struct symbol *Symbol_find(/* char * */); 338 struct symbol *Symbol_find(const char *);
311 struct symbol *Symbol_Nth(/* int */); 339 struct symbol *Symbol_Nth(int);
312 int Symbol_count(/* */); 340 int Symbol_count(void);
313 struct symbol **Symbol_arrayof(/* */); 341 struct symbol **Symbol_arrayof(void);
314 342
315 /* Routines to manage the state table */ 343 /* Routines to manage the state table */
316 344
317 int Configcmp(/* struct config *, struct config * */); 345 int Configcmp(const char *, const char *);
318 struct state *State_new(); 346 struct state *State_new(void);
319 void State_init(/* void */); 347 void State_init(void);
320 int State_insert(/* struct state *, struct config * */); 348 int State_insert(struct state *, struct config *);
321 struct state *State_find(/* struct config * */); 349 struct state *State_find(struct config *);
322 struct state **State_arrayof(/* */); 350 struct state **State_arrayof(/* */);
323 351
324 /* Routines used for efficiency in Configlist_add */ 352 /* Routines used for efficiency in Configlist_add */
325 353
326 void Configtable_init(/* void */); 354 void Configtable_init(void);
327 int Configtable_insert(/* struct config * */); 355 int Configtable_insert(struct config *);
328 struct config *Configtable_find(/* struct config * */); 356 struct config *Configtable_find(struct config *);
329 void Configtable_clear(/* int(*)(struct config *) */); 357 void Configtable_clear(int(*)(struct config *));
358
330 /****************** From the file "action.c" *******************************/ 359 /****************** From the file "action.c" *******************************/
331 /* 360 /*
332 ** Routines processing parser actions in the LEMON parser generator. 361 ** Routines processing parser actions in the LEMON parser generator.
333 */ 362 */
334 363
335 /* Allocate a new parser action */ 364 /* Allocate a new parser action */
336 static struct action *Action_new(void){ 365 static struct action *Action_new(void){
337 static struct action *freelist = 0; 366 static struct action *freelist = 0;
338 struct action *new; 367 struct action *newaction;
339 368
340 if( freelist==0 ){ 369 if( freelist==0 ){
341 int i; 370 int i;
342 int amt = 100; 371 int amt = 100;
343 freelist = (struct action *)calloc(amt, sizeof(struct action)); 372 freelist = (struct action *)calloc(amt, sizeof(struct action));
344 if( freelist==0 ){ 373 if( freelist==0 ){
345 fprintf(stderr,"Unable to allocate memory for a new parser action."); 374 fprintf(stderr,"Unable to allocate memory for a new parser action.");
346 exit(1); 375 exit(1);
347 } 376 }
348 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1]; 377 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
349 freelist[amt-1].next = 0; 378 freelist[amt-1].next = 0;
350 } 379 }
351 new = freelist; 380 newaction = freelist;
352 freelist = freelist->next; 381 freelist = freelist->next;
353 return new; 382 return newaction;
354 } 383 }
355 384
356 /* Compare two actions for sorting purposes. Return negative, zero, or 385 /* Compare two actions for sorting purposes. Return negative, zero, or
357 ** positive if the first action is less than, equal to, or greater than 386 ** positive if the first action is less than, equal to, or greater than
358 ** the first 387 ** the first
359 */ 388 */
360 static int actioncmp( 389 static int actioncmp(
361 struct action *ap1, 390 struct action *ap1,
362 struct action *ap2 391 struct action *ap2
363 ){ 392 ){
364 int rc; 393 int rc;
365 rc = ap1->sp->index - ap2->sp->index; 394 rc = ap1->sp->index - ap2->sp->index;
366 if( rc==0 ){ 395 if( rc==0 ){
367 rc = (int)ap1->type - (int)ap2->type; 396 rc = (int)ap1->type - (int)ap2->type;
368 } 397 }
369 if( rc==0 && ap1->type==REDUCE ){ 398 if( rc==0 && ap1->type==REDUCE ){
370 rc = ap1->x.rp->index - ap2->x.rp->index; 399 rc = ap1->x.rp->index - ap2->x.rp->index;
371 } 400 }
401 if( rc==0 ){
402 rc = (int) (ap2 - ap1);
403 }
372 return rc; 404 return rc;
373 } 405 }
374 406
375 /* Sort parser actions */ 407 /* Sort parser actions */
376 static struct action *Action_sort( 408 static struct action *Action_sort(
377 struct action *ap 409 struct action *ap
378 ){ 410 ){
379 ap = (struct action *)msort((char *)ap,(char **)&ap->next, 411 ap = (struct action *)msort((char *)ap,(char **)&ap->next,
380 (int(*)(const char*,const char*))actioncmp); 412 (int(*)(const char*,const char*))actioncmp);
381 return ap; 413 return ap;
382 } 414 }
383 415
384 void Action_add(app,type,sp,arg) 416 void Action_add(
385 struct action **app; 417 struct action **app,
386 enum e_action type; 418 enum e_action type,
387 struct symbol *sp; 419 struct symbol *sp,
388 char *arg; 420 char *arg
389 { 421 ){
390 struct action *new; 422 struct action *newaction;
391 new = Action_new(); 423 newaction = Action_new();
392 new->next = *app; 424 newaction->next = *app;
393 *app = new; 425 *app = newaction;
394 new->type = type; 426 newaction->type = type;
395 new->sp = sp; 427 newaction->sp = sp;
396 if( type==SHIFT ){ 428 if( type==SHIFT ){
397 new->x.stp = (struct state *)arg; 429 newaction->x.stp = (struct state *)arg;
398 }else{ 430 }else{
399 new->x.rp = (struct rule *)arg; 431 newaction->x.rp = (struct rule *)arg;
400 } 432 }
401 } 433 }
402 /********************** New code to implement the "acttab" module ***********/ 434 /********************** New code to implement the "acttab" module ***********/
403 /* 435 /*
404 ** This module implements routines use to construct the yy_action[] table. 436 ** This module implements routines use to construct the yy_action[] table.
405 */ 437 */
406 438
407 /* 439 /*
408 ** The state of the yy_action table under construction is an instance of 440 ** The state of the yy_action table under construction is an instance of
409 ** the following structure 441 ** 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.
410 */ 458 */
459 struct lookahead_action {
460 int lookahead; /* Value of the lookahead token */
461 int action; /* Action to take on the given lookahead */
462 };
411 typedef struct acttab acttab; 463 typedef struct acttab acttab;
412 struct acttab { 464 struct acttab {
413 int nAction; /* Number of used slots in aAction[] */ 465 int nAction; /* Number of used slots in aAction[] */
414 int nActionAlloc; /* Slots allocated for aAction[] */ 466 int nActionAlloc; /* Slots allocated for aAction[] */
415 struct { 467 struct lookahead_action
416 int lookahead; /* Value of the lookahead token */ 468 *aAction, /* The yy_action[] table under construction */
417 int action; /* Action to take on the given lookahead */
418 } *aAction, /* The yy_action[] table under construction */
419 *aLookahead; /* A single new transaction set */ 469 *aLookahead; /* A single new transaction set */
420 int mnLookahead; /* Minimum aLookahead[].lookahead */ 470 int mnLookahead; /* Minimum aLookahead[].lookahead */
421 int mnAction; /* Action associated with mnLookahead */ 471 int mnAction; /* Action associated with mnLookahead */
422 int mxLookahead; /* Maximum aLookahead[].lookahead */ 472 int mxLookahead; /* Maximum aLookahead[].lookahead */
423 int nLookahead; /* Used slots in aLookahead[] */ 473 int nLookahead; /* Used slots in aLookahead[] */
424 int nLookaheadAlloc; /* Slots allocated in aLookahead[] */ 474 int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
425 }; 475 };
426 476
427 /* Return the number of entries in the yy_action table */ 477 /* Return the number of entries in the yy_action table */
428 #define acttab_size(X) ((X)->nAction) 478 #define acttab_size(X) ((X)->nAction)
429 479
430 /* The value for the N-th entry in yy_action */ 480 /* The value for the N-th entry in yy_action */
431 #define acttab_yyaction(X,N) ((X)->aAction[N].action) 481 #define acttab_yyaction(X,N) ((X)->aAction[N].action)
432 482
433 /* The value for the N-th entry in yy_lookahead */ 483 /* The value for the N-th entry in yy_lookahead */
434 #define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead) 484 #define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
435 485
436 /* Free all memory associated with the given acttab */ 486 /* Free all memory associated with the given acttab */
437 void acttab_free(acttab *p){ 487 void acttab_free(acttab *p){
438 free( p->aAction ); 488 free( p->aAction );
439 free( p->aLookahead ); 489 free( p->aLookahead );
440 free( p ); 490 free( p );
441 } 491 }
442 492
443 /* Allocate a new acttab structure */ 493 /* Allocate a new acttab structure */
444 acttab *acttab_alloc(void){ 494 acttab *acttab_alloc(void){
445 acttab *p = calloc( 1, sizeof(*p) ); 495 acttab *p = (acttab *) calloc( 1, sizeof(*p) );
446 if( p==0 ){ 496 if( p==0 ){
447 fprintf(stderr,"Unable to allocate memory for a new acttab."); 497 fprintf(stderr,"Unable to allocate memory for a new acttab.");
448 exit(1); 498 exit(1);
449 } 499 }
450 memset(p, 0, sizeof(*p)); 500 memset(p, 0, sizeof(*p));
451 return p; 501 return p;
452 } 502 }
453 503
454 /* Add a new action to the current transaction set 504 /* Add a new action to the current transaction set.
505 **
506 ** This routine is called once for each lookahead for a particular
507 ** state.
455 */ 508 */
456 void acttab_action(acttab *p, int lookahead, int action){ 509 void acttab_action(acttab *p, int lookahead, int action){
457 if( p->nLookahead>=p->nLookaheadAlloc ){ 510 if( p->nLookahead>=p->nLookaheadAlloc ){
458 p->nLookaheadAlloc += 25; 511 p->nLookaheadAlloc += 25;
459 p->aLookahead = realloc( p->aLookahead, 512 p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
460 sizeof(p->aLookahead[0])*p->nLookaheadAlloc ); 513 sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
461 if( p->aLookahead==0 ){ 514 if( p->aLookahead==0 ){
462 fprintf(stderr,"malloc failed\n"); 515 fprintf(stderr,"malloc failed\n");
463 exit(1); 516 exit(1);
464 } 517 }
465 } 518 }
466 if( p->nLookahead==0 ){ 519 if( p->nLookahead==0 ){
467 p->mxLookahead = lookahead; 520 p->mxLookahead = lookahead;
468 p->mnLookahead = lookahead; 521 p->mnLookahead = lookahead;
469 p->mnAction = action; 522 p->mnAction = action;
(...skipping 21 matching lines...) Expand all
491 assert( p->nLookahead>0 ); 544 assert( p->nLookahead>0 );
492 545
493 /* Make sure we have enough space to hold the expanded action table 546 /* Make sure we have enough space to hold the expanded action table
494 ** in the worst case. The worst case occurs if the transaction set 547 ** in the worst case. The worst case occurs if the transaction set
495 ** must be appended to the current action table 548 ** must be appended to the current action table
496 */ 549 */
497 n = p->mxLookahead + 1; 550 n = p->mxLookahead + 1;
498 if( p->nAction + n >= p->nActionAlloc ){ 551 if( p->nAction + n >= p->nActionAlloc ){
499 int oldAlloc = p->nActionAlloc; 552 int oldAlloc = p->nActionAlloc;
500 p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20; 553 p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
501 p->aAction = realloc( p->aAction, 554 p->aAction = (struct lookahead_action *) realloc( p->aAction,
502 sizeof(p->aAction[0])*p->nActionAlloc); 555 sizeof(p->aAction[0])*p->nActionAlloc);
503 if( p->aAction==0 ){ 556 if( p->aAction==0 ){
504 fprintf(stderr,"malloc failed\n"); 557 fprintf(stderr,"malloc failed\n");
505 exit(1); 558 exit(1);
506 } 559 }
507 for(i=oldAlloc; i<p->nActionAlloc; i++){ 560 for(i=oldAlloc; i<p->nActionAlloc; i++){
508 p->aAction[i].lookahead = -1; 561 p->aAction[i].lookahead = -1;
509 p->aAction[i].action = -1; 562 p->aAction[i].action = -1;
510 } 563 }
511 } 564 }
512 565
513 /* Scan the existing action table looking for an offset where we can 566 /* Scan the existing action table looking for an offset that is a
514 ** insert the current transaction set. Fall out of the loop when that 567 ** duplicate of the current transaction set. Fall out of the loop
515 ** offset is found. In the worst case, we fall out of the loop when 568 ** if and when the duplicate is found.
516 ** i reaches p->nAction, which means we append the new transaction set.
517 ** 569 **
518 ** i is the index in p->aAction[] where p->mnLookahead is inserted. 570 ** i is the index in p->aAction[] where p->mnLookahead is inserted.
519 */ 571 */
520 for(i=0; i<p->nAction+p->mnLookahead; i++){ 572 for(i=p->nAction-1; i>=0; i--){
521 if( p->aAction[i].lookahead<0 ){ 573 if( p->aAction[i].lookahead==p->mnLookahead ){
522 for(j=0; j<p->nLookahead; j++){ 574 /* All lookaheads and actions in the aLookahead[] transaction
523 k = p->aLookahead[j].lookahead - p->mnLookahead + i; 575 ** must match against the candidate aAction[i] entry. */
524 if( k<0 ) break;
525 if( p->aAction[k].lookahead>=0 ) break;
526 }
527 if( j<p->nLookahead ) continue;
528 for(j=0; j<p->nAction; j++){
529 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
530 }
531 if( j==p->nAction ){
532 break; /* Fits in empty slots */
533 }
534 }else if( p->aAction[i].lookahead==p->mnLookahead ){
535 if( p->aAction[i].action!=p->mnAction ) continue; 576 if( p->aAction[i].action!=p->mnAction ) continue;
536 for(j=0; j<p->nLookahead; j++){ 577 for(j=0; j<p->nLookahead; j++){
537 k = p->aLookahead[j].lookahead - p->mnLookahead + i; 578 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
538 if( k<0 || k>=p->nAction ) break; 579 if( k<0 || k>=p->nAction ) break;
539 if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break; 580 if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
540 if( p->aLookahead[j].action!=p->aAction[k].action ) break; 581 if( p->aLookahead[j].action!=p->aAction[k].action ) break;
541 } 582 }
542 if( j<p->nLookahead ) continue; 583 if( j<p->nLookahead ) continue;
584
585 /* No possible lookahead value that is not in the aLookahead[]
586 ** transaction is allowed to match aAction[i] */
543 n = 0; 587 n = 0;
544 for(j=0; j<p->nAction; j++){ 588 for(j=0; j<p->nAction; j++){
545 if( p->aAction[j].lookahead<0 ) continue; 589 if( p->aAction[j].lookahead<0 ) continue;
546 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++; 590 if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
547 } 591 }
548 if( n==p->nLookahead ){ 592 if( n==p->nLookahead ){
549 break; /* Same as a prior transaction set */ 593 break; /* An exact match is found at offset i */
550 } 594 }
551 } 595 }
552 } 596 }
597
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 */
602 if( i<0 ){
603 /* 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++){
608 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 }
553 /* Insert transaction set at index i. */ 624 /* Insert transaction set at index i. */
554 for(j=0; j<p->nLookahead; j++){ 625 for(j=0; j<p->nLookahead; j++){
555 k = p->aLookahead[j].lookahead - p->mnLookahead + i; 626 k = p->aLookahead[j].lookahead - p->mnLookahead + i;
556 p->aAction[k] = p->aLookahead[j]; 627 p->aAction[k] = p->aLookahead[j];
557 if( k>=p->nAction ) p->nAction = k+1; 628 if( k>=p->nAction ) p->nAction = k+1;
558 } 629 }
559 p->nLookahead = 0; 630 p->nLookahead = 0;
560 631
561 /* Return the offset that is added to the lookahead in order to get the 632 /* Return the offset that is added to the lookahead in order to get the
562 ** index into yy_action of the action */ 633 ** index into yy_action of the action */
563 return i - p->mnLookahead; 634 return i - p->mnLookahead;
564 } 635 }
565 636
566 /********************** From the file "build.c" *****************************/ 637 /********************** From the file "build.c" *****************************/
567 /* 638 /*
568 ** Routines to construction the finite state machine for the LEMON 639 ** Routines to construction the finite state machine for the LEMON
569 ** parser generator. 640 ** parser generator.
570 */ 641 */
571 642
572 /* Find a precedence symbol of every rule in the grammar. 643 /* Find a precedence symbol of every rule in the grammar.
573 ** 644 **
574 ** Those rules which have a precedence symbol coded in the input 645 ** Those rules which have a precedence symbol coded in the input
575 ** grammar using the "[symbol]" construct will already have the 646 ** grammar using the "[symbol]" construct will already have the
576 ** rp->precsym field filled. Other rules take as their precedence 647 ** rp->precsym field filled. Other rules take as their precedence
577 ** symbol the first RHS symbol with a defined precedence. If there 648 ** symbol the first RHS symbol with a defined precedence. If there
578 ** are not RHS symbols with a defined precedence, the precedence 649 ** are not RHS symbols with a defined precedence, the precedence
579 ** symbol field is left blank. 650 ** symbol field is left blank.
580 */ 651 */
581 void FindRulePrecedences(xp) 652 void FindRulePrecedences(struct lemon *xp)
582 struct lemon *xp;
583 { 653 {
584 struct rule *rp; 654 struct rule *rp;
585 for(rp=xp->rule; rp; rp=rp->next){ 655 for(rp=xp->rule; rp; rp=rp->next){
586 if( rp->precsym==0 ){ 656 if( rp->precsym==0 ){
587 int i, j; 657 int i, j;
588 for(i=0; i<rp->nrhs && rp->precsym==0; i++){ 658 for(i=0; i<rp->nrhs && rp->precsym==0; i++){
589 struct symbol *sp = rp->rhs[i]; 659 struct symbol *sp = rp->rhs[i];
590 if( sp->type==MULTITERMINAL ){ 660 if( sp->type==MULTITERMINAL ){
591 for(j=0; j<sp->nsubsym; j++){ 661 for(j=0; j<sp->nsubsym; j++){
592 if( sp->subsym[j]->prec>=0 ){ 662 if( sp->subsym[j]->prec>=0 ){
593 rp->precsym = sp->subsym[j]; 663 rp->precsym = sp->subsym[j];
594 break; 664 break;
595 } 665 }
596 } 666 }
597 }else if( sp->prec>=0 ){ 667 }else if( sp->prec>=0 ){
598 rp->precsym = rp->rhs[i]; 668 rp->precsym = rp->rhs[i];
599 } 669 }
600 } 670 }
601 } 671 }
602 } 672 }
603 return; 673 return;
604 } 674 }
605 675
606 /* Find all nonterminals which will generate the empty string. 676 /* Find all nonterminals which will generate the empty string.
607 ** Then go back and compute the first sets of every nonterminal. 677 ** Then go back and compute the first sets of every nonterminal.
608 ** The first set is the set of all terminal symbols which can begin 678 ** The first set is the set of all terminal symbols which can begin
609 ** a string generated by that nonterminal. 679 ** a string generated by that nonterminal.
610 */ 680 */
611 void FindFirstSets(lemp) 681 void FindFirstSets(struct lemon *lemp)
612 struct lemon *lemp;
613 { 682 {
614 int i, j; 683 int i, j;
615 struct rule *rp; 684 struct rule *rp;
616 int progress; 685 int progress;
617 686
618 for(i=0; i<lemp->nsymbol; i++){ 687 for(i=0; i<lemp->nsymbol; i++){
619 lemp->symbols[i]->lambda = LEMON_FALSE; 688 lemp->symbols[i]->lambda = LEMON_FALSE;
620 } 689 }
621 for(i=lemp->nterminal; i<lemp->nsymbol; i++){ 690 for(i=lemp->nterminal; i<lemp->nsymbol; i++){
622 lemp->symbols[i]->firstset = SetNew(); 691 lemp->symbols[i]->firstset = SetNew();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 } 732 }
664 } 733 }
665 }while( progress ); 734 }while( progress );
666 return; 735 return;
667 } 736 }
668 737
669 /* Compute all LR(0) states for the grammar. Links 738 /* Compute all LR(0) states for the grammar. Links
670 ** are added to between some states so that the LR(1) follow sets 739 ** are added to between some states so that the LR(1) follow sets
671 ** can be computed later. 740 ** can be computed later.
672 */ 741 */
673 PRIVATE struct state *getstate(/* struct lemon * */); /* forward reference */ 742 PRIVATE struct state *getstate(struct lemon *); /* forward reference */
674 void FindStates(lemp) 743 void FindStates(struct lemon *lemp)
675 struct lemon *lemp;
676 { 744 {
677 struct symbol *sp; 745 struct symbol *sp;
678 struct rule *rp; 746 struct rule *rp;
679 747
680 Configlist_init(); 748 Configlist_init();
681 749
682 /* Find the start symbol */ 750 /* Find the start symbol */
683 if( lemp->start ){ 751 if( lemp->start ){
684 sp = Symbol_find(lemp->start); 752 sp = Symbol_find(lemp->start);
685 if( sp==0 ){ 753 if( sp==0 ){
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 /* Compute the first state. All other states will be 791 /* Compute the first state. All other states will be
724 ** computed automatically during the computation of the first one. 792 ** computed automatically during the computation of the first one.
725 ** The returned pointer to the first state is not used. */ 793 ** The returned pointer to the first state is not used. */
726 (void)getstate(lemp); 794 (void)getstate(lemp);
727 return; 795 return;
728 } 796 }
729 797
730 /* Return a pointer to a state which is described by the configuration 798 /* Return a pointer to a state which is described by the configuration
731 ** list which has been built from calls to Configlist_add. 799 ** list which has been built from calls to Configlist_add.
732 */ 800 */
733 PRIVATE void buildshifts(/* struct lemon *, struct state * */); /* Forwd ref */ 801 PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
734 PRIVATE struct state *getstate(lemp) 802 PRIVATE struct state *getstate(struct lemon *lemp)
735 struct lemon *lemp;
736 { 803 {
737 struct config *cfp, *bp; 804 struct config *cfp, *bp;
738 struct state *stp; 805 struct state *stp;
739 806
740 /* Extract the sorted basis of the new state. The basis was constructed 807 /* Extract the sorted basis of the new state. The basis was constructed
741 ** by prior calls to "Configlist_addbasis()". */ 808 ** by prior calls to "Configlist_addbasis()". */
742 Configlist_sortbasis(); 809 Configlist_sortbasis();
743 bp = Configlist_basis(); 810 bp = Configlist_basis();
744 811
745 /* Get a state with the same basis */ 812 /* Get a state with the same basis */
(...skipping 23 matching lines...) Expand all
769 stp->ap = 0; /* No actions, yet. */ 836 stp->ap = 0; /* No actions, yet. */
770 State_insert(stp,stp->bp); /* Add to the state table */ 837 State_insert(stp,stp->bp); /* Add to the state table */
771 buildshifts(lemp,stp); /* Recursively compute successor states */ 838 buildshifts(lemp,stp); /* Recursively compute successor states */
772 } 839 }
773 return stp; 840 return stp;
774 } 841 }
775 842
776 /* 843 /*
777 ** Return true if two symbols are the same. 844 ** Return true if two symbols are the same.
778 */ 845 */
779 int same_symbol(a,b) 846 int same_symbol(struct symbol *a, struct symbol *b)
780 struct symbol *a;
781 struct symbol *b;
782 { 847 {
783 int i; 848 int i;
784 if( a==b ) return 1; 849 if( a==b ) return 1;
785 if( a->type!=MULTITERMINAL ) return 0; 850 if( a->type!=MULTITERMINAL ) return 0;
786 if( b->type!=MULTITERMINAL ) return 0; 851 if( b->type!=MULTITERMINAL ) return 0;
787 if( a->nsubsym!=b->nsubsym ) return 0; 852 if( a->nsubsym!=b->nsubsym ) return 0;
788 for(i=0; i<a->nsubsym; i++){ 853 for(i=0; i<a->nsubsym; i++){
789 if( a->subsym[i]!=b->subsym[i] ) return 0; 854 if( a->subsym[i]!=b->subsym[i] ) return 0;
790 } 855 }
791 return 1; 856 return 1;
792 } 857 }
793 858
794 /* Construct all successor states to the given state. A "successor" 859 /* Construct all successor states to the given state. A "successor"
795 ** state is any state which can be reached by a shift action. 860 ** state is any state which can be reached by a shift action.
796 */ 861 */
797 PRIVATE void buildshifts(lemp,stp) 862 PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
798 struct lemon *lemp;
799 struct state *stp; /* The state from which successors are computed */
800 { 863 {
801 struct config *cfp; /* For looping thru the config closure of "stp" */ 864 struct config *cfp; /* For looping thru the config closure of "stp" */
802 struct config *bcfp; /* For the inner loop on config closure of "stp" */ 865 struct config *bcfp; /* For the inner loop on config closure of "stp" */
803 struct config *new; /* */ 866 struct config *newcfg; /* */
804 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */ 867 struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
805 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */ 868 struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
806 struct state *newstp; /* A pointer to a successor state */ 869 struct state *newstp; /* A pointer to a successor state */
807 870
808 /* Each configuration becomes complete after it contibutes to a successor 871 /* Each configuration becomes complete after it contibutes to a successor
809 ** state. Initially, all configurations are incomplete */ 872 ** state. Initially, all configurations are incomplete */
810 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE; 873 for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
811 874
812 /* Loop through all configurations of the state "stp" */ 875 /* Loop through all configurations of the state "stp" */
813 for(cfp=stp->cfp; cfp; cfp=cfp->next){ 876 for(cfp=stp->cfp; cfp; cfp=cfp->next){
814 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */ 877 if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
815 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */ 878 if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
816 Configlist_reset(); /* Reset the new config set */ 879 Configlist_reset(); /* Reset the new config set */
817 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */ 880 sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
818 881
819 /* For every configuration in the state "stp" which has the symbol "sp" 882 /* For every configuration in the state "stp" which has the symbol "sp"
820 ** following its dot, add the same configuration to the basis set under 883 ** following its dot, add the same configuration to the basis set under
821 ** construction but with the dot shifted one symbol to the right. */ 884 ** construction but with the dot shifted one symbol to the right. */
822 for(bcfp=cfp; bcfp; bcfp=bcfp->next){ 885 for(bcfp=cfp; bcfp; bcfp=bcfp->next){
823 if( bcfp->status==COMPLETE ) continue; /* Already used */ 886 if( bcfp->status==COMPLETE ) continue; /* Already used */
824 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */ 887 if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
825 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */ 888 bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
826 if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */ 889 if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
827 bcfp->status = COMPLETE; /* Mark this config as used */ 890 bcfp->status = COMPLETE; /* Mark this config as used */
828 new = Configlist_addbasis(bcfp->rp,bcfp->dot+1); 891 newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
829 Plink_add(&new->bplp,bcfp); 892 Plink_add(&newcfg->bplp,bcfp);
830 } 893 }
831 894
832 /* Get a pointer to the state described by the basis configuration set 895 /* Get a pointer to the state described by the basis configuration set
833 ** constructed in the preceding loop */ 896 ** constructed in the preceding loop */
834 newstp = getstate(lemp); 897 newstp = getstate(lemp);
835 898
836 /* The state "newstp" is reached from the state "stp" by a shift action 899 /* The state "newstp" is reached from the state "stp" by a shift action
837 ** on the symbol "sp" */ 900 ** on the symbol "sp" */
838 if( sp->type==MULTITERMINAL ){ 901 if( sp->type==MULTITERMINAL ){
839 int i; 902 int i;
840 for(i=0; i<sp->nsubsym; i++){ 903 for(i=0; i<sp->nsubsym; i++){
841 Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp); 904 Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
842 } 905 }
843 }else{ 906 }else{
844 Action_add(&stp->ap,SHIFT,sp,(char *)newstp); 907 Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
845 } 908 }
846 } 909 }
847 } 910 }
848 911
849 /* 912 /*
850 ** Construct the propagation links 913 ** Construct the propagation links
851 */ 914 */
852 void FindLinks(lemp) 915 void FindLinks(struct lemon *lemp)
853 struct lemon *lemp;
854 { 916 {
855 int i; 917 int i;
856 struct config *cfp, *other; 918 struct config *cfp, *other;
857 struct state *stp; 919 struct state *stp;
858 struct plink *plp; 920 struct plink *plp;
859 921
860 /* Housekeeping detail: 922 /* Housekeeping detail:
861 ** Add to every propagate link a pointer back to the state to 923 ** Add to every propagate link a pointer back to the state to
862 ** which the link is attached. */ 924 ** which the link is attached. */
863 for(i=0; i<lemp->nstate; i++){ 925 for(i=0; i<lemp->nstate; i++){
(...skipping 14 matching lines...) Expand all
878 } 940 }
879 } 941 }
880 } 942 }
881 } 943 }
882 944
883 /* Compute all followsets. 945 /* Compute all followsets.
884 ** 946 **
885 ** A followset is the set of all symbols which can come immediately 947 ** A followset is the set of all symbols which can come immediately
886 ** after a configuration. 948 ** after a configuration.
887 */ 949 */
888 void FindFollowSets(lemp) 950 void FindFollowSets(struct lemon *lemp)
889 struct lemon *lemp;
890 { 951 {
891 int i; 952 int i;
892 struct config *cfp; 953 struct config *cfp;
893 struct plink *plp; 954 struct plink *plp;
894 int progress; 955 int progress;
895 int change; 956 int change;
896 957
897 for(i=0; i<lemp->nstate; i++){ 958 for(i=0; i<lemp->nstate; i++){
898 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){ 959 for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
899 cfp->status = INCOMPLETE; 960 cfp->status = INCOMPLETE;
(...skipping 11 matching lines...) Expand all
911 plp->cfp->status = INCOMPLETE; 972 plp->cfp->status = INCOMPLETE;
912 progress = 1; 973 progress = 1;
913 } 974 }
914 } 975 }
915 cfp->status = COMPLETE; 976 cfp->status = COMPLETE;
916 } 977 }
917 } 978 }
918 }while( progress ); 979 }while( progress );
919 } 980 }
920 981
921 static int resolve_conflict(); 982 static int resolve_conflict(struct action *,struct action *, struct symbol *);
922 983
923 /* Compute the reduce actions, and resolve conflicts. 984 /* Compute the reduce actions, and resolve conflicts.
924 */ 985 */
925 void FindActions(lemp) 986 void FindActions(struct lemon *lemp)
926 struct lemon *lemp;
927 { 987 {
928 int i,j; 988 int i,j;
929 struct config *cfp; 989 struct config *cfp;
930 struct state *stp; 990 struct state *stp;
931 struct symbol *sp; 991 struct symbol *sp;
932 struct rule *rp; 992 struct rule *rp;
933 993
934 /* Add all of the reduce actions 994 /* Add all of the reduce actions
935 ** A reduce action is added for each element of the followset of 995 ** A reduce action is added for each element of the followset of
936 ** a configuration which has its dot at the extreme right. 996 ** a configuration which has its dot at the extreme right.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 ** NO LONGER TRUE: 1059 ** NO LONGER TRUE:
1000 ** To resolve a conflict, first look to see if either action 1060 ** To resolve a conflict, first look to see if either action
1001 ** is on an error rule. In that case, take the action which 1061 ** is on an error rule. In that case, take the action which
1002 ** is not associated with the error rule. If neither or both 1062 ** is not associated with the error rule. If neither or both
1003 ** actions are associated with an error rule, then try to 1063 ** actions are associated with an error rule, then try to
1004 ** use precedence to resolve the conflict. 1064 ** use precedence to resolve the conflict.
1005 ** 1065 **
1006 ** If either action is a SHIFT, then it must be apx. This 1066 ** If either action is a SHIFT, then it must be apx. This
1007 ** function won't work if apx->type==REDUCE and apy->type==SHIFT. 1067 ** function won't work if apx->type==REDUCE and apy->type==SHIFT.
1008 */ 1068 */
1009 static int resolve_conflict(apx,apy,errsym) 1069 static int resolve_conflict(
1010 struct action *apx; 1070 struct action *apx,
1011 struct action *apy; 1071 struct action *apy,
1012 struct symbol *errsym; /* The error symbol (if defined. NULL otherwise) */ 1072 struct symbol *errsym /* The error symbol (if defined. NULL otherwise) */
1013 { 1073 ){
1014 struct symbol *spx, *spy; 1074 struct symbol *spx, *spy;
1015 int errcnt = 0; 1075 int errcnt = 0;
1016 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */ 1076 assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
1017 if( apx->type==SHIFT && apy->type==SHIFT ){ 1077 if( apx->type==SHIFT && apy->type==SHIFT ){
1018 apy->type = SSCONFLICT; 1078 apy->type = SSCONFLICT;
1019 errcnt++; 1079 errcnt++;
1020 } 1080 }
1021 if( apx->type==SHIFT && apy->type==REDUCE ){ 1081 if( apx->type==SHIFT && apy->type==REDUCE ){
1022 spx = apx->sp; 1082 spx = apx->sp;
1023 spy = apy->x.rp->precsym; 1083 spy = apy->x.rp->precsym;
1024 if( spy==0 || spx->prec<0 || spy->prec<0 ){ 1084 if( spy==0 || spx->prec<0 || spy->prec<0 ){
1025 /* Not enough precedence information. */ 1085 /* Not enough precedence information. */
1026 apy->type = SRCONFLICT; 1086 apy->type = SRCONFLICT;
1027 errcnt++; 1087 errcnt++;
1028 }else if( spx->prec>spy->prec ){ /* Lower precedence wins */ 1088 }else if( spx->prec>spy->prec ){ /* higher precedence wins */
1029 apy->type = RD_RESOLVED; 1089 apy->type = RD_RESOLVED;
1030 }else if( spx->prec<spy->prec ){ 1090 }else if( spx->prec<spy->prec ){
1031 apx->type = SH_RESOLVED; 1091 apx->type = SH_RESOLVED;
1032 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */ 1092 }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
1033 apy->type = RD_RESOLVED; /* associativity */ 1093 apy->type = RD_RESOLVED; /* associativity */
1034 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */ 1094 }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
1035 apx->type = SH_RESOLVED; 1095 apx->type = SH_RESOLVED;
1036 }else{ 1096 }else{
1037 assert( spx->prec==spy->prec && spx->assoc==NONE ); 1097 assert( spx->prec==spy->prec && spx->assoc==NONE );
1038 apy->type = SRCONFLICT; 1098 apy->type = SRCONFLICT;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1076 */ 1136 */
1077 1137
1078 static struct config *freelist = 0; /* List of free configurations */ 1138 static struct config *freelist = 0; /* List of free configurations */
1079 static struct config *current = 0; /* Top of list of configurations */ 1139 static struct config *current = 0; /* Top of list of configurations */
1080 static struct config **currentend = 0; /* Last on list of configs */ 1140 static struct config **currentend = 0; /* Last on list of configs */
1081 static struct config *basis = 0; /* Top of list of basis configs */ 1141 static struct config *basis = 0; /* Top of list of basis configs */
1082 static struct config **basisend = 0; /* End of list of basis configs */ 1142 static struct config **basisend = 0; /* End of list of basis configs */
1083 1143
1084 /* Return a pointer to a new configuration */ 1144 /* Return a pointer to a new configuration */
1085 PRIVATE struct config *newconfig(){ 1145 PRIVATE struct config *newconfig(){
1086 struct config *new; 1146 struct config *newcfg;
1087 if( freelist==0 ){ 1147 if( freelist==0 ){
1088 int i; 1148 int i;
1089 int amt = 3; 1149 int amt = 3;
1090 freelist = (struct config *)calloc( amt, sizeof(struct config) ); 1150 freelist = (struct config *)calloc( amt, sizeof(struct config) );
1091 if( freelist==0 ){ 1151 if( freelist==0 ){
1092 fprintf(stderr,"Unable to allocate memory for a new configuration."); 1152 fprintf(stderr,"Unable to allocate memory for a new configuration.");
1093 exit(1); 1153 exit(1);
1094 } 1154 }
1095 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1]; 1155 for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
1096 freelist[amt-1].next = 0; 1156 freelist[amt-1].next = 0;
1097 } 1157 }
1098 new = freelist; 1158 newcfg = freelist;
1099 freelist = freelist->next; 1159 freelist = freelist->next;
1100 return new; 1160 return newcfg;
1101 } 1161 }
1102 1162
1103 /* The configuration "old" is no longer used */ 1163 /* The configuration "old" is no longer used */
1104 PRIVATE void deleteconfig(old) 1164 PRIVATE void deleteconfig(struct config *old)
1105 struct config *old;
1106 { 1165 {
1107 old->next = freelist; 1166 old->next = freelist;
1108 freelist = old; 1167 freelist = old;
1109 } 1168 }
1110 1169
1111 /* Initialized the configuration list builder */ 1170 /* Initialized the configuration list builder */
1112 void Configlist_init(){ 1171 void Configlist_init(){
1113 current = 0; 1172 current = 0;
1114 currentend = &current; 1173 currentend = &current;
1115 basis = 0; 1174 basis = 0;
1116 basisend = &basis; 1175 basisend = &basis;
1117 Configtable_init(); 1176 Configtable_init();
1118 return; 1177 return;
1119 } 1178 }
1120 1179
1121 /* Initialized the configuration list builder */ 1180 /* Initialized the configuration list builder */
1122 void Configlist_reset(){ 1181 void Configlist_reset(){
1123 current = 0; 1182 current = 0;
1124 currentend = &current; 1183 currentend = &current;
1125 basis = 0; 1184 basis = 0;
1126 basisend = &basis; 1185 basisend = &basis;
1127 Configtable_clear(0); 1186 Configtable_clear(0);
1128 return; 1187 return;
1129 } 1188 }
1130 1189
1131 /* Add another configuration to the configuration list */ 1190 /* Add another configuration to the configuration list */
1132 struct config *Configlist_add(rp,dot) 1191 struct config *Configlist_add(
1133 struct rule *rp; /* The rule */ 1192 struct rule *rp, /* The rule */
1134 int dot; /* Index into the RHS of the rule where the dot goes */ 1193 int dot /* Index into the RHS of the rule where the dot goes */
1135 { 1194 ){
1136 struct config *cfp, model; 1195 struct config *cfp, model;
1137 1196
1138 assert( currentend!=0 ); 1197 assert( currentend!=0 );
1139 model.rp = rp; 1198 model.rp = rp;
1140 model.dot = dot; 1199 model.dot = dot;
1141 cfp = Configtable_find(&model); 1200 cfp = Configtable_find(&model);
1142 if( cfp==0 ){ 1201 if( cfp==0 ){
1143 cfp = newconfig(); 1202 cfp = newconfig();
1144 cfp->rp = rp; 1203 cfp->rp = rp;
1145 cfp->dot = dot; 1204 cfp->dot = dot;
1146 cfp->fws = SetNew(); 1205 cfp->fws = SetNew();
1147 cfp->stp = 0; 1206 cfp->stp = 0;
1148 cfp->fplp = cfp->bplp = 0; 1207 cfp->fplp = cfp->bplp = 0;
1149 cfp->next = 0; 1208 cfp->next = 0;
1150 cfp->bp = 0; 1209 cfp->bp = 0;
1151 *currentend = cfp; 1210 *currentend = cfp;
1152 currentend = &cfp->next; 1211 currentend = &cfp->next;
1153 Configtable_insert(cfp); 1212 Configtable_insert(cfp);
1154 } 1213 }
1155 return cfp; 1214 return cfp;
1156 } 1215 }
1157 1216
1158 /* Add a basis configuration to the configuration list */ 1217 /* Add a basis configuration to the configuration list */
1159 struct config *Configlist_addbasis(rp,dot) 1218 struct config *Configlist_addbasis(struct rule *rp, int dot)
1160 struct rule *rp;
1161 int dot;
1162 { 1219 {
1163 struct config *cfp, model; 1220 struct config *cfp, model;
1164 1221
1165 assert( basisend!=0 ); 1222 assert( basisend!=0 );
1166 assert( currentend!=0 ); 1223 assert( currentend!=0 );
1167 model.rp = rp; 1224 model.rp = rp;
1168 model.dot = dot; 1225 model.dot = dot;
1169 cfp = Configtable_find(&model); 1226 cfp = Configtable_find(&model);
1170 if( cfp==0 ){ 1227 if( cfp==0 ){
1171 cfp = newconfig(); 1228 cfp = newconfig();
1172 cfp->rp = rp; 1229 cfp->rp = rp;
1173 cfp->dot = dot; 1230 cfp->dot = dot;
1174 cfp->fws = SetNew(); 1231 cfp->fws = SetNew();
1175 cfp->stp = 0; 1232 cfp->stp = 0;
1176 cfp->fplp = cfp->bplp = 0; 1233 cfp->fplp = cfp->bplp = 0;
1177 cfp->next = 0; 1234 cfp->next = 0;
1178 cfp->bp = 0; 1235 cfp->bp = 0;
1179 *currentend = cfp; 1236 *currentend = cfp;
1180 currentend = &cfp->next; 1237 currentend = &cfp->next;
1181 *basisend = cfp; 1238 *basisend = cfp;
1182 basisend = &cfp->bp; 1239 basisend = &cfp->bp;
1183 Configtable_insert(cfp); 1240 Configtable_insert(cfp);
1184 } 1241 }
1185 return cfp; 1242 return cfp;
1186 } 1243 }
1187 1244
1188 /* Compute the closure of the configuration list */ 1245 /* Compute the closure of the configuration list */
1189 void Configlist_closure(lemp) 1246 void Configlist_closure(struct lemon *lemp)
1190 struct lemon *lemp;
1191 { 1247 {
1192 struct config *cfp, *newcfp; 1248 struct config *cfp, *newcfp;
1193 struct rule *rp, *newrp; 1249 struct rule *rp, *newrp;
1194 struct symbol *sp, *xsp; 1250 struct symbol *sp, *xsp;
1195 int i, dot; 1251 int i, dot;
1196 1252
1197 assert( currentend!=0 ); 1253 assert( currentend!=0 );
1198 for(cfp=current; cfp; cfp=cfp->next){ 1254 for(cfp=current; cfp; cfp=cfp->next){
1199 rp = cfp->rp; 1255 rp = cfp->rp;
1200 dot = cfp->dot; 1256 dot = cfp->dot;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1259 ** reset the list */ 1315 ** reset the list */
1260 struct config *Configlist_basis(){ 1316 struct config *Configlist_basis(){
1261 struct config *old; 1317 struct config *old;
1262 old = basis; 1318 old = basis;
1263 basis = 0; 1319 basis = 0;
1264 basisend = 0; 1320 basisend = 0;
1265 return old; 1321 return old;
1266 } 1322 }
1267 1323
1268 /* Free all elements of the given configuration list */ 1324 /* Free all elements of the given configuration list */
1269 void Configlist_eat(cfp) 1325 void Configlist_eat(struct config *cfp)
1270 struct config *cfp;
1271 { 1326 {
1272 struct config *nextcfp; 1327 struct config *nextcfp;
1273 for(; cfp; cfp=nextcfp){ 1328 for(; cfp; cfp=nextcfp){
1274 nextcfp = cfp->next; 1329 nextcfp = cfp->next;
1275 assert( cfp->fplp==0 ); 1330 assert( cfp->fplp==0 );
1276 assert( cfp->bplp==0 ); 1331 assert( cfp->bplp==0 );
1277 if( cfp->fws ) SetFree(cfp->fws); 1332 if( cfp->fws ) SetFree(cfp->fws);
1278 deleteconfig(cfp); 1333 deleteconfig(cfp);
1279 } 1334 }
1280 return; 1335 return;
1281 } 1336 }
1282 /***************** From the file "error.c" *********************************/ 1337 /***************** From the file "error.c" *********************************/
1283 /* 1338 /*
1284 ** Code for printing error message. 1339 ** Code for printing error message.
1285 */ 1340 */
1286 1341
1287 /* Find a good place to break "msg" so that its length is at least "min"
1288 ** but no more than "max". Make the point as close to max as possible.
1289 */
1290 static int findbreak(msg,min,max)
1291 char *msg;
1292 int min;
1293 int max;
1294 {
1295 int i,spot;
1296 char c;
1297 for(i=spot=min; i<=max; i++){
1298 c = msg[i];
1299 if( c=='\t' ) msg[i] = ' ';
1300 if( c=='\n' ){ msg[i] = ' '; spot = i; break; }
1301 if( c==0 ){ spot = i; break; }
1302 if( c=='-' && i<max-1 ) spot = i+1;
1303 if( c==' ' ) spot = i;
1304 }
1305 return spot;
1306 }
1307
1308 /*
1309 ** The error message is split across multiple lines if necessary. The
1310 ** splits occur at a space, if there is a space available near the end
1311 ** of the line.
1312 */
1313 #define ERRMSGSIZE 10000 /* Hope this is big enough. No way to error check */
1314 #define LINEWIDTH 79 /* Max width of any output line */
1315 #define PREFIXLIMIT 30 /* Max width of the prefix on each line */
1316 void ErrorMsg(const char *filename, int lineno, const char *format, ...){ 1342 void ErrorMsg(const char *filename, int lineno, const char *format, ...){
1317 char errmsg[ERRMSGSIZE];
1318 char prefix[PREFIXLIMIT+10];
1319 int errmsgsize;
1320 int prefixsize;
1321 int availablewidth;
1322 va_list ap; 1343 va_list ap;
1323 int end, restart, base; 1344 fprintf(stderr, "%s:%d: ", filename, lineno);
1324
1325 va_start(ap, format); 1345 va_start(ap, format);
1326 /* Prepare a prefix to be prepended to every output line */ 1346 vfprintf(stderr,format,ap);
1327 if( lineno>0 ){
1328 sprintf(prefix,"%.*s:%d: ",PREFIXLIMIT-10,filename,lineno);
1329 }else{
1330 sprintf(prefix,"%.*s: ",PREFIXLIMIT-10,filename);
1331 }
1332 prefixsize = lemonStrlen(prefix);
1333 availablewidth = LINEWIDTH - prefixsize;
1334
1335 /* Generate the error message */
1336 vsprintf(errmsg,format,ap);
1337 va_end(ap); 1347 va_end(ap);
1338 errmsgsize = lemonStrlen(errmsg); 1348 fprintf(stderr, "\n");
1339 /* Remove trailing '\n's from the error message. */
1340 while( errmsgsize>0 && errmsg[errmsgsize-1]=='\n' ){
1341 errmsg[--errmsgsize] = 0;
1342 }
1343
1344 /* Print the error message */
1345 base = 0;
1346 while( errmsg[base]!=0 ){
1347 end = restart = findbreak(&errmsg[base],0,availablewidth);
1348 restart += base;
1349 while( errmsg[restart]==' ' ) restart++;
1350 fprintf(stdout,"%s%.*s\n",prefix,end,&errmsg[base]);
1351 base = restart;
1352 }
1353 } 1349 }
1354 /**************** From the file "main.c" ************************************/ 1350 /**************** From the file "main.c" ************************************/
1355 /* 1351 /*
1356 ** Main program file for the LEMON parser generator. 1352 ** Main program file for the LEMON parser generator.
1357 */ 1353 */
1358 1354
1359 /* Report an out-of-memory condition and abort. This function 1355 /* Report an out-of-memory condition and abort. This function
1360 ** is used mostly by the "MemoryCheck" macro in struct.h 1356 ** is used mostly by the "MemoryCheck" macro in struct.h
1361 */ 1357 */
1362 void memory_error(){ 1358 void memory_error(){
1363 fprintf(stderr,"Out of memory. Aborting...\n"); 1359 fprintf(stderr,"Out of memory. Aborting...\n");
1364 exit(1); 1360 exit(1);
1365 } 1361 }
1366 1362
1367 static int nDefine = 0; /* Number of -D options on the command line */ 1363 static int nDefine = 0; /* Number of -D options on the command line */
1368 static char **azDefine = 0; /* Name of the -D macros */ 1364 static char **azDefine = 0; /* Name of the -D macros */
1369 1365
1370 /* This routine is called with the argument to each -D command-line option. 1366 /* This routine is called with the argument to each -D command-line option.
1371 ** Add the macro defined to the azDefine array. 1367 ** Add the macro defined to the azDefine array.
1372 */ 1368 */
1373 static void handle_D_option(char *z){ 1369 static void handle_D_option(char *z){
1374 char **paz; 1370 char **paz;
1375 nDefine++; 1371 nDefine++;
1376 azDefine = realloc(azDefine, sizeof(azDefine[0])*nDefine); 1372 azDefine = (char **) realloc(azDefine, sizeof(azDefine[0])*nDefine);
1377 if( azDefine==0 ){ 1373 if( azDefine==0 ){
1378 fprintf(stderr,"out of memory\n"); 1374 fprintf(stderr,"out of memory\n");
1379 exit(1); 1375 exit(1);
1380 } 1376 }
1381 paz = &azDefine[nDefine-1]; 1377 paz = &azDefine[nDefine-1];
1382 *paz = malloc( lemonStrlen(z)+1 ); 1378 *paz = (char *) malloc( lemonStrlen(z)+1 );
1383 if( *paz==0 ){ 1379 if( *paz==0 ){
1384 fprintf(stderr,"out of memory\n"); 1380 fprintf(stderr,"out of memory\n");
1385 exit(1); 1381 exit(1);
1386 } 1382 }
1387 strcpy(*paz, z); 1383 strcpy(*paz, z);
1388 for(z=*paz; *z && *z!='='; z++){} 1384 for(z=*paz; *z && *z!='='; z++){}
1389 *z = 0; 1385 *z = 0;
1390 } 1386 }
1391 1387
1388 static char *user_templatename = NULL;
1389 static void handle_T_option(char *z){
1390 user_templatename = (char *) malloc( lemonStrlen(z)+1 );
1391 if( user_templatename==0 ){
1392 memory_error();
1393 }
1394 strcpy(user_templatename, z);
1395 }
1392 1396
1393 /* The main program. Parse the command line and do it... */ 1397 /* The main program. Parse the command line and do it... */
1394 int main(argc,argv) 1398 int main(int argc, char **argv)
1395 int argc;
1396 char **argv;
1397 { 1399 {
1398 static int version = 0; 1400 static int version = 0;
1399 static int rpflag = 0; 1401 static int rpflag = 0;
1400 static int basisflag = 0; 1402 static int basisflag = 0;
1401 static int compress = 0; 1403 static int compress = 0;
1402 static int quiet = 0; 1404 static int quiet = 0;
1403 static int statistics = 0; 1405 static int statistics = 0;
1404 static int mhflag = 0; 1406 static int mhflag = 0;
1405 static int nolinenosflag = 0; 1407 static int nolinenosflag = 0;
1408 static int noResort = 0;
1406 static struct s_options options[] = { 1409 static struct s_options options[] = {
1407 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."}, 1410 {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
1408 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."}, 1411 {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
1409 {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."}, 1412 {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
1413 {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
1410 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."}, 1414 {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
1411 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."}, 1415 {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
1412 {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."}, 1416 {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
1417 {OPT_FLAG, "p", (char*)&showPrecedenceConflict,
1418 "Show conflicts resolved by precedence rules"},
1413 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."}, 1419 {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
1420 {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"},
1414 {OPT_FLAG, "s", (char*)&statistics, 1421 {OPT_FLAG, "s", (char*)&statistics,
1415 "Print parser stats to standard output."}, 1422 "Print parser stats to standard output."},
1416 {OPT_FLAG, "x", (char*)&version, "Print the version number."}, 1423 {OPT_FLAG, "x", (char*)&version, "Print the version number."},
1417 {OPT_FLAG,0,0,0} 1424 {OPT_FLAG,0,0,0}
1418 }; 1425 };
1419 int i; 1426 int i;
1427 int exitcode;
1420 struct lemon lem; 1428 struct lemon lem;
1421 1429
1430 atexit(LemonAtExit);
1431
1422 OptInit(argv,options,stderr); 1432 OptInit(argv,options,stderr);
1423 if( version ){ 1433 if( version ){
1424 printf("Lemon version 1.0\n"); 1434 printf("Lemon version 1.0\n");
1425 exit(0); 1435 exit(0);
1426 } 1436 }
1427 if( OptNArgs()!=1 ){ 1437 if( OptNArgs()!=1 ){
1428 fprintf(stderr,"Exactly one filename argument is required.\n"); 1438 fprintf(stderr,"Exactly one filename argument is required.\n");
1429 exit(1); 1439 exit(1);
1430 } 1440 }
1431 memset(&lem, 0, sizeof(lem)); 1441 memset(&lem, 0, sizeof(lem));
(...skipping 17 matching lines...) Expand all
1449 if( lem.nrule==0 ){ 1459 if( lem.nrule==0 ){
1450 fprintf(stderr,"Empty grammar.\n"); 1460 fprintf(stderr,"Empty grammar.\n");
1451 exit(1); 1461 exit(1);
1452 } 1462 }
1453 1463
1454 /* Count and index the symbols of the grammar */ 1464 /* Count and index the symbols of the grammar */
1455 lem.nsymbol = Symbol_count(); 1465 lem.nsymbol = Symbol_count();
1456 Symbol_new("{default}"); 1466 Symbol_new("{default}");
1457 lem.symbols = Symbol_arrayof(); 1467 lem.symbols = Symbol_arrayof();
1458 for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i; 1468 for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
1459 qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*), 1469 qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*), Symbolcmpp);
1460 (int(*)())Symbolcmpp);
1461 for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i; 1470 for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
1462 for(i=1; isupper(lem.symbols[i]->name[0]); i++); 1471 for(i=1; isupper(lem.symbols[i]->name[0]); i++);
1463 lem.nterminal = i; 1472 lem.nterminal = i;
1464 1473
1465 /* Generate a reprint of the grammar, if requested on the command line */ 1474 /* Generate a reprint of the grammar, if requested on the command line */
1466 if( rpflag ){ 1475 if( rpflag ){
1467 Reprint(&lem); 1476 Reprint(&lem);
1468 }else{ 1477 }else{
1469 /* Initialize the size for all follow and first sets */ 1478 /* Initialize the size for all follow and first sets */
1470 SetSize(lem.nterminal+1); 1479 SetSize(lem.nterminal+1);
(...skipping 17 matching lines...) Expand all
1488 /* Compute the follow set of every reducible configuration */ 1497 /* Compute the follow set of every reducible configuration */
1489 FindFollowSets(&lem); 1498 FindFollowSets(&lem);
1490 1499
1491 /* Compute the action tables */ 1500 /* Compute the action tables */
1492 FindActions(&lem); 1501 FindActions(&lem);
1493 1502
1494 /* Compress the action tables */ 1503 /* Compress the action tables */
1495 if( compress==0 ) CompressTables(&lem); 1504 if( compress==0 ) CompressTables(&lem);
1496 1505
1497 /* Reorder and renumber the states so that states with fewer choices 1506 /* Reorder and renumber the states so that states with fewer choices
1498 ** occur at the end. */ 1507 ** occur at the end. This is an optimization that helps make the
1499 ResortStates(&lem); 1508 ** generated parser tables smaller. */
1509 if( noResort==0 ) ResortStates(&lem);
1500 1510
1501 /* Generate a report of the parser generated. (the "y.output" file) */ 1511 /* Generate a report of the parser generated. (the "y.output" file) */
1502 if( !quiet ) ReportOutput(&lem); 1512 if( !quiet ) ReportOutput(&lem);
1503 1513
1504 /* Generate the source code for the parser */ 1514 /* Generate the source code for the parser */
1505 ReportTable(&lem, mhflag); 1515 ReportTable(&lem, mhflag);
1506 1516
1507 /* Produce a header file for use by the scanner. (This step is 1517 /* Produce a header file for use by the scanner. (This step is
1508 ** omitted if the "-m" option is used because makeheaders will 1518 ** omitted if the "-m" option is used because makeheaders will
1509 ** generate the file for us.) */ 1519 ** generate the file for us.) */
1510 if( !mhflag ) ReportHeader(&lem); 1520 if( !mhflag ) ReportHeader(&lem);
1511 } 1521 }
1512 if( statistics ){ 1522 if( statistics ){
1513 printf("Parser statistics: %d terminals, %d nonterminals, %d rules\n", 1523 printf("Parser statistics: %d terminals, %d nonterminals, %d rules\n",
1514 lem.nterminal, lem.nsymbol - lem.nterminal, lem.nrule); 1524 lem.nterminal, lem.nsymbol - lem.nterminal, lem.nrule);
1515 printf(" %d states, %d parser table entries, %d conflicts\ n", 1525 printf(" %d states, %d parser table entries, %d conflicts\ n",
1516 lem.nstate, lem.tablesize, lem.nconflict); 1526 lem.nstate, lem.tablesize, lem.nconflict);
1517 } 1527 }
1518 if( lem.nconflict ){ 1528 if( lem.nconflict > 0 ){
1519 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict); 1529 fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
1520 } 1530 }
1521 exit(lem.errorcnt + lem.nconflict); 1531
1522 return (lem.errorcnt + lem.nconflict); 1532 /* return 0 on success, 1 on failure. */
1533 exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
1534 successful_exit = (exitcode == 0);
1535 exit(exitcode);
1536 return (exitcode);
1523 } 1537 }
1524 /******************** From the file "msort.c" *******************************/ 1538 /******************** From the file "msort.c" *******************************/
1525 /* 1539 /*
1526 ** A generic merge-sort program. 1540 ** A generic merge-sort program.
1527 ** 1541 **
1528 ** USAGE: 1542 ** USAGE:
1529 ** Let "ptr" be a pointer to some structure which is at the head of 1543 ** Let "ptr" be a pointer to some structure which is at the head of
1530 ** a null-terminated list. Then to sort the list call: 1544 ** a null-terminated list. Then to sort the list call:
1531 ** 1545 **
1532 ** ptr = msort(ptr,&(ptr->next),cmpfnc); 1546 ** ptr = msort(ptr,&(ptr->next),cmpfnc);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1571 int (*cmp)(const char*,const char*), 1585 int (*cmp)(const char*,const char*),
1572 int offset 1586 int offset
1573 ){ 1587 ){
1574 char *ptr, *head; 1588 char *ptr, *head;
1575 1589
1576 if( a==0 ){ 1590 if( a==0 ){
1577 head = b; 1591 head = b;
1578 }else if( b==0 ){ 1592 }else if( b==0 ){
1579 head = a; 1593 head = a;
1580 }else{ 1594 }else{
1581 if( (*cmp)(a,b)<0 ){ 1595 if( (*cmp)(a,b)<=0 ){
1582 ptr = a; 1596 ptr = a;
1583 a = NEXT(a); 1597 a = NEXT(a);
1584 }else{ 1598 }else{
1585 ptr = b; 1599 ptr = b;
1586 b = NEXT(b); 1600 b = NEXT(b);
1587 } 1601 }
1588 head = ptr; 1602 head = ptr;
1589 while( a && b ){ 1603 while( a && b ){
1590 if( (*cmp)(a,b)<0 ){ 1604 if( (*cmp)(a,b)<=0 ){
1591 NEXT(ptr) = a; 1605 NEXT(ptr) = a;
1592 ptr = a; 1606 ptr = a;
1593 a = NEXT(a); 1607 a = NEXT(a);
1594 }else{ 1608 }else{
1595 NEXT(ptr) = b; 1609 NEXT(ptr) = b;
1596 ptr = b; 1610 ptr = b;
1597 b = NEXT(b); 1611 b = NEXT(b);
1598 } 1612 }
1599 } 1613 }
1600 if( a ) NEXT(ptr) = a; 1614 if( a ) NEXT(ptr) = a;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1632 ep = list; 1646 ep = list;
1633 list = NEXT(list); 1647 list = NEXT(list);
1634 NEXT(ep) = 0; 1648 NEXT(ep) = 0;
1635 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){ 1649 for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
1636 ep = merge(ep,set[i],cmp,offset); 1650 ep = merge(ep,set[i],cmp,offset);
1637 set[i] = 0; 1651 set[i] = 0;
1638 } 1652 }
1639 set[i] = ep; 1653 set[i] = ep;
1640 } 1654 }
1641 ep = 0; 1655 ep = 0;
1642 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(ep,set[i],cmp,offset); 1656 for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset);
1643 return ep; 1657 return ep;
1644 } 1658 }
1645 /************************ From the file "option.c" **************************/ 1659 /************************ From the file "option.c" **************************/
1646 static char **argv; 1660 static char **argv;
1647 static struct s_options *op; 1661 static struct s_options *op;
1648 static FILE *errstream; 1662 static FILE *errstream;
1649 1663
1650 #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0) 1664 #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
1651 1665
1652 /* 1666 /*
1653 ** Print the command line with a carrot pointing to the k-th character 1667 ** Print the command line with a carrot pointing to the k-th character
1654 ** of the n-th field. 1668 ** of the n-th field.
1655 */ 1669 */
1656 static void errline(n,k,err) 1670 static void errline(int n, int k, FILE *err)
1657 int n;
1658 int k;
1659 FILE *err;
1660 { 1671 {
1661 int spcnt, i; 1672 int spcnt, i;
1662 if( argv[0] ) fprintf(err,"%s",argv[0]); 1673 if( argv[0] ) fprintf(err,"%s",argv[0]);
1663 spcnt = lemonStrlen(argv[0]) + 1; 1674 spcnt = lemonStrlen(argv[0]) + 1;
1664 for(i=1; i<n && argv[i]; i++){ 1675 for(i=1; i<n && argv[i]; i++){
1665 fprintf(err," %s",argv[i]); 1676 fprintf(err," %s",argv[i]);
1666 spcnt += lemonStrlen(argv[i])+1; 1677 spcnt += lemonStrlen(argv[i])+1;
1667 } 1678 }
1668 spcnt += k; 1679 spcnt += k;
1669 for(; argv[i]; i++) fprintf(err," %s",argv[i]); 1680 for(; argv[i]; i++) fprintf(err," %s",argv[i]);
1670 if( spcnt<20 ){ 1681 if( spcnt<20 ){
1671 fprintf(err,"\n%*s^-- here\n",spcnt,""); 1682 fprintf(err,"\n%*s^-- here\n",spcnt,"");
1672 }else{ 1683 }else{
1673 fprintf(err,"\n%*shere --^\n",spcnt-7,""); 1684 fprintf(err,"\n%*shere --^\n",spcnt-7,"");
1674 } 1685 }
1675 } 1686 }
1676 1687
1677 /* 1688 /*
1678 ** Return the index of the N-th non-switch argument. Return -1 1689 ** Return the index of the N-th non-switch argument. Return -1
1679 ** if N is out of range. 1690 ** if N is out of range.
1680 */ 1691 */
1681 static int argindex(n) 1692 static int argindex(int n)
1682 int n;
1683 { 1693 {
1684 int i; 1694 int i;
1685 int dashdash = 0; 1695 int dashdash = 0;
1686 if( argv!=0 && *argv!=0 ){ 1696 if( argv!=0 && *argv!=0 ){
1687 for(i=1; argv[i]; i++){ 1697 for(i=1; argv[i]; i++){
1688 if( dashdash || !ISOPT(argv[i]) ){ 1698 if( dashdash || !ISOPT(argv[i]) ){
1689 if( n==0 ) return i; 1699 if( n==0 ) return i;
1690 n--; 1700 n--;
1691 } 1701 }
1692 if( strcmp(argv[i],"--")==0 ) dashdash = 1; 1702 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1693 } 1703 }
1694 } 1704 }
1695 return -1; 1705 return -1;
1696 } 1706 }
1697 1707
1698 static char emsg[] = "Command line syntax error: "; 1708 static char emsg[] = "Command line syntax error: ";
1699 1709
1700 /* 1710 /*
1701 ** Process a flag command line argument. 1711 ** Process a flag command line argument.
1702 */ 1712 */
1703 static int handleflags(i,err) 1713 static int handleflags(int i, FILE *err)
1704 int i;
1705 FILE *err;
1706 { 1714 {
1707 int v; 1715 int v;
1708 int errcnt = 0; 1716 int errcnt = 0;
1709 int j; 1717 int j;
1710 for(j=0; op[j].label; j++){ 1718 for(j=0; op[j].label; j++){
1711 if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break; 1719 if( strncmp(&argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
1712 } 1720 }
1713 v = argv[i][0]=='-' ? 1 : 0; 1721 v = argv[i][0]=='-' ? 1 : 0;
1714 if( op[j].label==0 ){ 1722 if( op[j].label==0 ){
1715 if( err ){ 1723 if( err ){
1716 fprintf(err,"%sundefined option.\n",emsg); 1724 fprintf(err,"%sundefined option.\n",emsg);
1717 errline(i,1,err); 1725 errline(i,1,err);
1718 } 1726 }
1719 errcnt++; 1727 errcnt++;
1720 }else if( op[j].type==OPT_FLAG ){ 1728 }else if( op[j].type==OPT_FLAG ){
1721 *((int*)op[j].arg) = v; 1729 *((int*)op[j].arg) = v;
1722 }else if( op[j].type==OPT_FFLAG ){ 1730 }else if( op[j].type==OPT_FFLAG ){
1723 (*(void(*)())(op[j].arg))(v); 1731 (*(void(*)(int))(op[j].arg))(v);
1724 }else if( op[j].type==OPT_FSTR ){ 1732 }else if( op[j].type==OPT_FSTR ){
1725 (*(void(*)())(op[j].arg))(&argv[i][2]); 1733 (*(void(*)(char *))(op[j].arg))(&argv[i][2]);
1726 }else{ 1734 }else{
1727 if( err ){ 1735 if( err ){
1728 fprintf(err,"%smissing argument on switch.\n",emsg); 1736 fprintf(err,"%smissing argument on switch.\n",emsg);
1729 errline(i,1,err); 1737 errline(i,1,err);
1730 } 1738 }
1731 errcnt++; 1739 errcnt++;
1732 } 1740 }
1733 return errcnt; 1741 return errcnt;
1734 } 1742 }
1735 1743
1736 /* 1744 /*
1737 ** Process a command line switch which has an argument. 1745 ** Process a command line switch which has an argument.
1738 */ 1746 */
1739 static int handleswitch(i,err) 1747 static int handleswitch(int i, FILE *err)
1740 int i;
1741 FILE *err;
1742 { 1748 {
1743 int lv = 0; 1749 int lv = 0;
1744 double dv = 0.0; 1750 double dv = 0.0;
1745 char *sv = 0, *end; 1751 char *sv = 0, *end;
1746 char *cp; 1752 char *cp;
1747 int j; 1753 int j;
1748 int errcnt = 0; 1754 int errcnt = 0;
1749 cp = strchr(argv[i],'='); 1755 cp = strchr(argv[i],'=');
1750 assert( cp!=0 ); 1756 assert( cp!=0 );
1751 *cp = 0; 1757 *cp = 0;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1798 break; 1804 break;
1799 } 1805 }
1800 switch( op[j].type ){ 1806 switch( op[j].type ){
1801 case OPT_FLAG: 1807 case OPT_FLAG:
1802 case OPT_FFLAG: 1808 case OPT_FFLAG:
1803 break; 1809 break;
1804 case OPT_DBL: 1810 case OPT_DBL:
1805 *(double*)(op[j].arg) = dv; 1811 *(double*)(op[j].arg) = dv;
1806 break; 1812 break;
1807 case OPT_FDBL: 1813 case OPT_FDBL:
1808 (*(void(*)())(op[j].arg))(dv); 1814 (*(void(*)(double))(op[j].arg))(dv);
1809 break; 1815 break;
1810 case OPT_INT: 1816 case OPT_INT:
1811 *(int*)(op[j].arg) = lv; 1817 *(int*)(op[j].arg) = lv;
1812 break; 1818 break;
1813 case OPT_FINT: 1819 case OPT_FINT:
1814 (*(void(*)())(op[j].arg))((int)lv); 1820 (*(void(*)(int))(op[j].arg))((int)lv);
1815 break; 1821 break;
1816 case OPT_STR: 1822 case OPT_STR:
1817 *(char**)(op[j].arg) = sv; 1823 *(char**)(op[j].arg) = sv;
1818 break; 1824 break;
1819 case OPT_FSTR: 1825 case OPT_FSTR:
1820 (*(void(*)())(op[j].arg))(sv); 1826 (*(void(*)(char *))(op[j].arg))(sv);
1821 break; 1827 break;
1822 } 1828 }
1823 } 1829 }
1824 return errcnt; 1830 return errcnt;
1825 } 1831 }
1826 1832
1827 int OptInit(a,o,err) 1833 int OptInit(char **a, struct s_options *o, FILE *err)
1828 char **a;
1829 struct s_options *o;
1830 FILE *err;
1831 { 1834 {
1832 int errcnt = 0; 1835 int errcnt = 0;
1833 argv = a; 1836 argv = a;
1834 op = o; 1837 op = o;
1835 errstream = err; 1838 errstream = err;
1836 if( argv && *argv && op ){ 1839 if( argv && *argv && op ){
1837 int i; 1840 int i;
1838 for(i=1; argv[i]; i++){ 1841 for(i=1; argv[i]; i++){
1839 if( argv[i][0]=='+' || argv[i][0]=='-' ){ 1842 if( argv[i][0]=='+' || argv[i][0]=='-' ){
1840 errcnt += handleflags(i,err); 1843 errcnt += handleflags(i,err);
(...skipping 16 matching lines...) Expand all
1857 int i; 1860 int i;
1858 if( argv!=0 && argv[0]!=0 ){ 1861 if( argv!=0 && argv[0]!=0 ){
1859 for(i=1; argv[i]; i++){ 1862 for(i=1; argv[i]; i++){
1860 if( dashdash || !ISOPT(argv[i]) ) cnt++; 1863 if( dashdash || !ISOPT(argv[i]) ) cnt++;
1861 if( strcmp(argv[i],"--")==0 ) dashdash = 1; 1864 if( strcmp(argv[i],"--")==0 ) dashdash = 1;
1862 } 1865 }
1863 } 1866 }
1864 return cnt; 1867 return cnt;
1865 } 1868 }
1866 1869
1867 char *OptArg(n) 1870 char *OptArg(int n)
1868 int n;
1869 { 1871 {
1870 int i; 1872 int i;
1871 i = argindex(n); 1873 i = argindex(n);
1872 return i>=0 ? argv[i] : 0; 1874 return i>=0 ? argv[i] : 0;
1873 } 1875 }
1874 1876
1875 void OptErr(n) 1877 void OptErr(int n)
1876 int n;
1877 { 1878 {
1878 int i; 1879 int i;
1879 i = argindex(n); 1880 i = argindex(n);
1880 if( i>=0 ) errline(i,0,errstream); 1881 if( i>=0 ) errline(i,0,errstream);
1881 } 1882 }
1882 1883
1883 void OptPrint(){ 1884 void OptPrint(){
1884 int i; 1885 int i;
1885 int max, len; 1886 int max, len;
1886 max = 0; 1887 max = 0;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1928 break; 1929 break;
1929 } 1930 }
1930 } 1931 }
1931 } 1932 }
1932 /*********************** From the file "parse.c" ****************************/ 1933 /*********************** From the file "parse.c" ****************************/
1933 /* 1934 /*
1934 ** Input file parser for the LEMON parser generator. 1935 ** Input file parser for the LEMON parser generator.
1935 */ 1936 */
1936 1937
1937 /* The state of the parser */ 1938 /* The state of the parser */
1939 enum e_state {
1940 INITIALIZE,
1941 WAITING_FOR_DECL_OR_RULE,
1942 WAITING_FOR_DECL_KEYWORD,
1943 WAITING_FOR_DECL_ARG,
1944 WAITING_FOR_PRECEDENCE_SYMBOL,
1945 WAITING_FOR_ARROW,
1946 IN_RHS,
1947 LHS_ALIAS_1,
1948 LHS_ALIAS_2,
1949 LHS_ALIAS_3,
1950 RHS_ALIAS_1,
1951 RHS_ALIAS_2,
1952 PRECEDENCE_MARK_1,
1953 PRECEDENCE_MARK_2,
1954 RESYNC_AFTER_RULE_ERROR,
1955 RESYNC_AFTER_DECL_ERROR,
1956 WAITING_FOR_DESTRUCTOR_SYMBOL,
1957 WAITING_FOR_DATATYPE_SYMBOL,
1958 WAITING_FOR_FALLBACK_ID,
1959 WAITING_FOR_WILDCARD_ID
1960 };
1938 struct pstate { 1961 struct pstate {
1939 char *filename; /* Name of the input file */ 1962 char *filename; /* Name of the input file */
1940 int tokenlineno; /* Linenumber at which current token starts */ 1963 int tokenlineno; /* Linenumber at which current token starts */
1941 int errorcnt; /* Number of errors so far */ 1964 int errorcnt; /* Number of errors so far */
1942 char *tokenstart; /* Text of current token */ 1965 char *tokenstart; /* Text of current token */
1943 struct lemon *gp; /* Global state vector */ 1966 struct lemon *gp; /* Global state vector */
1944 enum e_state { 1967 enum e_state state; /* The state of the parser */
1945 INITIALIZE,
1946 WAITING_FOR_DECL_OR_RULE,
1947 WAITING_FOR_DECL_KEYWORD,
1948 WAITING_FOR_DECL_ARG,
1949 WAITING_FOR_PRECEDENCE_SYMBOL,
1950 WAITING_FOR_ARROW,
1951 IN_RHS,
1952 LHS_ALIAS_1,
1953 LHS_ALIAS_2,
1954 LHS_ALIAS_3,
1955 RHS_ALIAS_1,
1956 RHS_ALIAS_2,
1957 PRECEDENCE_MARK_1,
1958 PRECEDENCE_MARK_2,
1959 RESYNC_AFTER_RULE_ERROR,
1960 RESYNC_AFTER_DECL_ERROR,
1961 WAITING_FOR_DESTRUCTOR_SYMBOL,
1962 WAITING_FOR_DATATYPE_SYMBOL,
1963 WAITING_FOR_FALLBACK_ID,
1964 WAITING_FOR_WILDCARD_ID
1965 } state; /* The state of the parser */
1966 struct symbol *fallback; /* The fallback token */ 1968 struct symbol *fallback; /* The fallback token */
1967 struct symbol *lhs; /* Left-hand side of current rule */ 1969 struct symbol *lhs; /* Left-hand side of current rule */
1968 char *lhsalias; /* Alias for the LHS */ 1970 const char *lhsalias; /* Alias for the LHS */
1969 int nrhs; /* Number of right-hand side symbols seen */ 1971 int nrhs; /* Number of right-hand side symbols seen */
1970 struct symbol *rhs[MAXRHS]; /* RHS symbols */ 1972 struct symbol *rhs[MAXRHS]; /* RHS symbols */
1971 char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */ 1973 const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
1972 struct rule *prevrule; /* Previous rule parsed */ 1974 struct rule *prevrule; /* Previous rule parsed */
1973 char *declkeyword; /* Keyword of a declaration */ 1975 const char *declkeyword; /* Keyword of a declaration */
1974 char **declargslot; /* Where the declaration argument should be put */ 1976 char **declargslot; /* Where the declaration argument should be put */
1975 int insertLineMacro; /* Add #line before declaration insert */ 1977 int insertLineMacro; /* Add #line before declaration insert */
1976 int *decllinenoslot; /* Where to write declaration line number */ 1978 int *decllinenoslot; /* Where to write declaration line number */
1977 enum e_assoc declassoc; /* Assign this association to decl arguments */ 1979 enum e_assoc declassoc; /* Assign this association to decl arguments */
1978 int preccounter; /* Assign this precedence to decl arguments */ 1980 int preccounter; /* Assign this precedence to decl arguments */
1979 struct rule *firstrule; /* Pointer to first rule in the grammar */ 1981 struct rule *firstrule; /* Pointer to first rule in the grammar */
1980 struct rule *lastrule; /* Pointer to the most recently parsed rule */ 1982 struct rule *lastrule; /* Pointer to the most recently parsed rule */
1981 }; 1983 };
1982 1984
1983 /* Parse a single token */ 1985 /* Parse a single token */
1984 static void parseonetoken(psp) 1986 static void parseonetoken(struct pstate *psp)
1985 struct pstate *psp;
1986 { 1987 {
1987 char *x; 1988 const char *x;
1988 x = Strsafe(psp->tokenstart); /* Save the token permanently */ 1989 x = Strsafe(psp->tokenstart); /* Save the token permanently */
1989 #if 0 1990 #if 0
1990 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno, 1991 printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
1991 x,psp->state); 1992 x,psp->state);
1992 #endif 1993 #endif
1993 switch( psp->state ){ 1994 switch( psp->state ){
1994 case INITIALIZE: 1995 case INITIALIZE:
1995 psp->prevrule = 0; 1996 psp->prevrule = 0;
1996 psp->preccounter = 0; 1997 psp->preccounter = 0;
1997 psp->firstrule = psp->lastrule = 0; 1998 psp->firstrule = psp->lastrule = 0;
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
2109 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1); 2110 sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
2110 if( rp==0 ){ 2111 if( rp==0 ){
2111 ErrorMsg(psp->filename,psp->tokenlineno, 2112 ErrorMsg(psp->filename,psp->tokenlineno,
2112 "Can't allocate enough memory for this rule."); 2113 "Can't allocate enough memory for this rule.");
2113 psp->errorcnt++; 2114 psp->errorcnt++;
2114 psp->prevrule = 0; 2115 psp->prevrule = 0;
2115 }else{ 2116 }else{
2116 int i; 2117 int i;
2117 rp->ruleline = psp->tokenlineno; 2118 rp->ruleline = psp->tokenlineno;
2118 rp->rhs = (struct symbol**)&rp[1]; 2119 rp->rhs = (struct symbol**)&rp[1];
2119 rp->rhsalias = (char**)&(rp->rhs[psp->nrhs]); 2120 rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
2120 for(i=0; i<psp->nrhs; i++){ 2121 for(i=0; i<psp->nrhs; i++){
2121 rp->rhs[i] = psp->rhs[i]; 2122 rp->rhs[i] = psp->rhs[i];
2122 rp->rhsalias[i] = psp->alias[i]; 2123 rp->rhsalias[i] = psp->alias[i];
2123 } 2124 }
2124 rp->lhs = psp->lhs; 2125 rp->lhs = psp->lhs;
2125 rp->lhsalias = psp->lhsalias; 2126 rp->lhsalias = psp->lhsalias;
2126 rp->nrhs = psp->nrhs; 2127 rp->nrhs = psp->nrhs;
2127 rp->code = 0; 2128 rp->code = 0;
2128 rp->precsym = 0; 2129 rp->precsym = 0;
2129 rp->index = psp->gp->nrule++; 2130 rp->index = psp->gp->nrule++;
(...skipping 18 matching lines...) Expand all
2148 psp->state = RESYNC_AFTER_RULE_ERROR; 2149 psp->state = RESYNC_AFTER_RULE_ERROR;
2149 }else{ 2150 }else{
2150 psp->rhs[psp->nrhs] = Symbol_new(x); 2151 psp->rhs[psp->nrhs] = Symbol_new(x);
2151 psp->alias[psp->nrhs] = 0; 2152 psp->alias[psp->nrhs] = 0;
2152 psp->nrhs++; 2153 psp->nrhs++;
2153 } 2154 }
2154 }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){ 2155 }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 ){
2155 struct symbol *msp = psp->rhs[psp->nrhs-1]; 2156 struct symbol *msp = psp->rhs[psp->nrhs-1];
2156 if( msp->type!=MULTITERMINAL ){ 2157 if( msp->type!=MULTITERMINAL ){
2157 struct symbol *origsp = msp; 2158 struct symbol *origsp = msp;
2158 msp = calloc(1,sizeof(*msp)); 2159 msp = (struct symbol *) calloc(1,sizeof(*msp));
2159 memset(msp, 0, sizeof(*msp)); 2160 memset(msp, 0, sizeof(*msp));
2160 msp->type = MULTITERMINAL; 2161 msp->type = MULTITERMINAL;
2161 msp->nsubsym = 1; 2162 msp->nsubsym = 1;
2162 msp->subsym = calloc(1,sizeof(struct symbol*)); 2163 msp->subsym = (struct symbol **) calloc(1,sizeof(struct symbol*));
2163 msp->subsym[0] = origsp; 2164 msp->subsym[0] = origsp;
2164 msp->name = origsp->name; 2165 msp->name = origsp->name;
2165 psp->rhs[psp->nrhs-1] = msp; 2166 psp->rhs[psp->nrhs-1] = msp;
2166 } 2167 }
2167 msp->nsubsym++; 2168 msp->nsubsym++;
2168 msp->subsym = realloc(msp->subsym, sizeof(struct symbol*)*msp->nsubsym); 2169 msp->subsym = (struct symbol **) realloc(msp->subsym,
2170 sizeof(struct symbol*)*msp->nsubsym);
2169 msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]); 2171 msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
2170 if( islower(x[1]) || islower(msp->subsym[0]->name[0]) ){ 2172 if( islower(x[1]) || islower(msp->subsym[0]->name[0]) ){
2171 ErrorMsg(psp->filename,psp->tokenlineno, 2173 ErrorMsg(psp->filename,psp->tokenlineno,
2172 "Cannot form a compound containing a non-terminal"); 2174 "Cannot form a compound containing a non-terminal");
2173 psp->errorcnt++; 2175 psp->errorcnt++;
2174 } 2176 }
2175 }else if( x[0]=='(' && psp->nrhs>0 ){ 2177 }else if( x[0]=='(' && psp->nrhs>0 ){
2176 psp->state = RHS_ALIAS_1; 2178 psp->state = RHS_ALIAS_1;
2177 }else{ 2179 }else{
2178 ErrorMsg(psp->filename,psp->tokenlineno, 2180 ErrorMsg(psp->filename,psp->tokenlineno,
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
2277 }else{ 2279 }else{
2278 ErrorMsg(psp->filename,psp->tokenlineno, 2280 ErrorMsg(psp->filename,psp->tokenlineno,
2279 "Illegal declaration keyword: \"%s\".",x); 2281 "Illegal declaration keyword: \"%s\".",x);
2280 psp->errorcnt++; 2282 psp->errorcnt++;
2281 psp->state = RESYNC_AFTER_DECL_ERROR; 2283 psp->state = RESYNC_AFTER_DECL_ERROR;
2282 } 2284 }
2283 break; 2285 break;
2284 case WAITING_FOR_DESTRUCTOR_SYMBOL: 2286 case WAITING_FOR_DESTRUCTOR_SYMBOL:
2285 if( !isalpha(x[0]) ){ 2287 if( !isalpha(x[0]) ){
2286 ErrorMsg(psp->filename,psp->tokenlineno, 2288 ErrorMsg(psp->filename,psp->tokenlineno,
2287 "Symbol name missing after %destructor keyword"); 2289 "Symbol name missing after %%destructor keyword");
2288 psp->errorcnt++; 2290 psp->errorcnt++;
2289 psp->state = RESYNC_AFTER_DECL_ERROR; 2291 psp->state = RESYNC_AFTER_DECL_ERROR;
2290 }else{ 2292 }else{
2291 struct symbol *sp = Symbol_new(x); 2293 struct symbol *sp = Symbol_new(x);
2292 psp->declargslot = &sp->destructor; 2294 psp->declargslot = &sp->destructor;
2293 psp->decllinenoslot = &sp->destLineno; 2295 psp->decllinenoslot = &sp->destLineno;
2294 psp->insertLineMacro = 1; 2296 psp->insertLineMacro = 1;
2295 psp->state = WAITING_FOR_DECL_ARG; 2297 psp->state = WAITING_FOR_DECL_ARG;
2296 } 2298 }
2297 break; 2299 break;
2298 case WAITING_FOR_DATATYPE_SYMBOL: 2300 case WAITING_FOR_DATATYPE_SYMBOL:
2299 if( !isalpha(x[0]) ){ 2301 if( !isalpha(x[0]) ){
2300 ErrorMsg(psp->filename,psp->tokenlineno, 2302 ErrorMsg(psp->filename,psp->tokenlineno,
2301 "Symbol name missing after %destructor keyword"); 2303 "Symbol name missing after %%type keyword");
2302 psp->errorcnt++; 2304 psp->errorcnt++;
2303 psp->state = RESYNC_AFTER_DECL_ERROR; 2305 psp->state = RESYNC_AFTER_DECL_ERROR;
2304 }else{ 2306 }else{
2305 struct symbol *sp = Symbol_new(x); 2307 struct symbol *sp = Symbol_find(x);
2306 psp->declargslot = &sp->datatype; 2308 if((sp) && (sp->datatype)){
2307 psp->insertLineMacro = 0; 2309 ErrorMsg(psp->filename,psp->tokenlineno,
2308 psp->state = WAITING_FOR_DECL_ARG; 2310 "Symbol %%type \"%s\" already defined", x);
2311 psp->errorcnt++;
2312 psp->state = RESYNC_AFTER_DECL_ERROR;
2313 }else{
2314 if (!sp){
2315 sp = Symbol_new(x);
2316 }
2317 psp->declargslot = &sp->datatype;
2318 psp->insertLineMacro = 0;
2319 psp->state = WAITING_FOR_DECL_ARG;
2320 }
2309 } 2321 }
2310 break; 2322 break;
2311 case WAITING_FOR_PRECEDENCE_SYMBOL: 2323 case WAITING_FOR_PRECEDENCE_SYMBOL:
2312 if( x[0]=='.' ){ 2324 if( x[0]=='.' ){
2313 psp->state = WAITING_FOR_DECL_OR_RULE; 2325 psp->state = WAITING_FOR_DECL_OR_RULE;
2314 }else if( isupper(x[0]) ){ 2326 }else if( isupper(x[0]) ){
2315 struct symbol *sp; 2327 struct symbol *sp;
2316 sp = Symbol_new(x); 2328 sp = Symbol_new(x);
2317 if( sp->prec>=0 ){ 2329 if( sp->prec>=0 ){
2318 ErrorMsg(psp->filename,psp->tokenlineno, 2330 ErrorMsg(psp->filename,psp->tokenlineno,
2319 "Symbol \"%s\" has already be given a precedence.",x); 2331 "Symbol \"%s\" has already be given a precedence.",x);
2320 psp->errorcnt++; 2332 psp->errorcnt++;
2321 }else{ 2333 }else{
2322 sp->prec = psp->preccounter; 2334 sp->prec = psp->preccounter;
2323 sp->assoc = psp->declassoc; 2335 sp->assoc = psp->declassoc;
2324 } 2336 }
2325 }else{ 2337 }else{
2326 ErrorMsg(psp->filename,psp->tokenlineno, 2338 ErrorMsg(psp->filename,psp->tokenlineno,
2327 "Can't assign a precedence to \"%s\".",x); 2339 "Can't assign a precedence to \"%s\".",x);
2328 psp->errorcnt++; 2340 psp->errorcnt++;
2329 } 2341 }
2330 break; 2342 break;
2331 case WAITING_FOR_DECL_ARG: 2343 case WAITING_FOR_DECL_ARG:
2332 if( x[0]=='{' || x[0]=='\"' || isalnum(x[0]) ){ 2344 if( x[0]=='{' || x[0]=='\"' || isalnum(x[0]) ){
2333 char *zOld, *zNew, *zBuf, *z; 2345 const char *zOld, *zNew;
2346 char *zBuf, *z;
2334 int nOld, n, nLine, nNew, nBack; 2347 int nOld, n, nLine, nNew, nBack;
2335 int addLineMacro; 2348 int addLineMacro;
2336 char zLine[50]; 2349 char zLine[50];
2337 zNew = x; 2350 zNew = x;
2338 if( zNew[0]=='"' || zNew[0]=='{' ) zNew++; 2351 if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
2339 nNew = lemonStrlen(zNew); 2352 nNew = lemonStrlen(zNew);
2340 if( *psp->declargslot ){ 2353 if( *psp->declargslot ){
2341 zOld = *psp->declargslot; 2354 zOld = *psp->declargslot;
2342 }else{ 2355 }else{
2343 zOld = ""; 2356 zOld = "";
2344 } 2357 }
2345 nOld = lemonStrlen(zOld); 2358 nOld = lemonStrlen(zOld);
2346 n = nOld + nNew + 20; 2359 n = nOld + nNew + 20;
2347 addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro && 2360 addLineMacro = !psp->gp->nolinenosflag && psp->insertLineMacro &&
2348 (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0); 2361 (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
2349 if( addLineMacro ){ 2362 if( addLineMacro ){
2350 for(z=psp->filename, nBack=0; *z; z++){ 2363 for(z=psp->filename, nBack=0; *z; z++){
2351 if( *z=='\\' ) nBack++; 2364 if( *z=='\\' ) nBack++;
2352 } 2365 }
2353 sprintf(zLine, "#line %d ", psp->tokenlineno); 2366 sprintf(zLine, "#line %d ", psp->tokenlineno);
2354 nLine = lemonStrlen(zLine); 2367 nLine = lemonStrlen(zLine);
2355 n += nLine + lemonStrlen(psp->filename) + nBack; 2368 n += nLine + lemonStrlen(psp->filename) + nBack;
2356 } 2369 }
2357 *psp->declargslot = zBuf = realloc(*psp->declargslot, n); 2370 *psp->declargslot = (char *) realloc(*psp->declargslot, n);
2358 zBuf += nOld; 2371 zBuf = *psp->declargslot + nOld;
2359 if( addLineMacro ){ 2372 if( addLineMacro ){
2360 if( nOld && zBuf[-1]!='\n' ){ 2373 if( nOld && zBuf[-1]!='\n' ){
2361 *(zBuf++) = '\n'; 2374 *(zBuf++) = '\n';
2362 } 2375 }
2363 memcpy(zBuf, zLine, nLine); 2376 memcpy(zBuf, zLine, nLine);
2364 zBuf += nLine; 2377 zBuf += nLine;
2365 *(zBuf++) = '"'; 2378 *(zBuf++) = '"';
2366 for(z=psp->filename; *z; z++){ 2379 for(z=psp->filename; *z; z++){
2367 if( *z=='\\' ){ 2380 if( *z=='\\' ){
2368 *(zBuf++) = '\\'; 2381 *(zBuf++) = '\\';
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
2484 fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno); 2497 fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
2485 exit(1); 2498 exit(1);
2486 } 2499 }
2487 } 2500 }
2488 2501
2489 /* In spite of its name, this function is really a scanner. It read 2502 /* In spite of its name, this function is really a scanner. It read
2490 ** in the entire input file (all at once) then tokenizes it. Each 2503 ** in the entire input file (all at once) then tokenizes it. Each
2491 ** token is passed to the function "parseonetoken" which builds all 2504 ** token is passed to the function "parseonetoken" which builds all
2492 ** the appropriate data structures in the global state vector "gp". 2505 ** the appropriate data structures in the global state vector "gp".
2493 */ 2506 */
2494 void Parse(gp) 2507 void Parse(struct lemon *gp)
2495 struct lemon *gp;
2496 { 2508 {
2497 struct pstate ps; 2509 struct pstate ps;
2498 FILE *fp; 2510 FILE *fp;
2499 char *filebuf; 2511 char *filebuf;
2500 int filesize; 2512 int filesize;
2501 int lineno; 2513 int lineno;
2502 int c; 2514 int c;
2503 char *cp, *nextcp; 2515 char *cp, *nextcp;
2504 int startline = 0; 2516 int startline = 0;
2505 2517
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
2639 } 2651 }
2640 /*************************** From the file "plink.c" *********************/ 2652 /*************************** From the file "plink.c" *********************/
2641 /* 2653 /*
2642 ** Routines processing configuration follow-set propagation links 2654 ** Routines processing configuration follow-set propagation links
2643 ** in the LEMON parser generator. 2655 ** in the LEMON parser generator.
2644 */ 2656 */
2645 static struct plink *plink_freelist = 0; 2657 static struct plink *plink_freelist = 0;
2646 2658
2647 /* Allocate a new plink */ 2659 /* Allocate a new plink */
2648 struct plink *Plink_new(){ 2660 struct plink *Plink_new(){
2649 struct plink *new; 2661 struct plink *newlink;
2650 2662
2651 if( plink_freelist==0 ){ 2663 if( plink_freelist==0 ){
2652 int i; 2664 int i;
2653 int amt = 100; 2665 int amt = 100;
2654 plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) ); 2666 plink_freelist = (struct plink *)calloc( amt, sizeof(struct plink) );
2655 if( plink_freelist==0 ){ 2667 if( plink_freelist==0 ){
2656 fprintf(stderr, 2668 fprintf(stderr,
2657 "Unable to allocate memory for a new follow-set propagation link.\n"); 2669 "Unable to allocate memory for a new follow-set propagation link.\n");
2658 exit(1); 2670 exit(1);
2659 } 2671 }
2660 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1]; 2672 for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
2661 plink_freelist[amt-1].next = 0; 2673 plink_freelist[amt-1].next = 0;
2662 } 2674 }
2663 new = plink_freelist; 2675 newlink = plink_freelist;
2664 plink_freelist = plink_freelist->next; 2676 plink_freelist = plink_freelist->next;
2665 return new; 2677 return newlink;
2666 } 2678 }
2667 2679
2668 /* Add a plink to a plink list */ 2680 /* Add a plink to a plink list */
2669 void Plink_add(plpp,cfp) 2681 void Plink_add(struct plink **plpp, struct config *cfp)
2670 struct plink **plpp;
2671 struct config *cfp;
2672 { 2682 {
2673 struct plink *new; 2683 struct plink *newlink;
2674 new = Plink_new(); 2684 newlink = Plink_new();
2675 new->next = *plpp; 2685 newlink->next = *plpp;
2676 *plpp = new; 2686 *plpp = newlink;
2677 new->cfp = cfp; 2687 newlink->cfp = cfp;
2678 } 2688 }
2679 2689
2680 /* Transfer every plink on the list "from" to the list "to" */ 2690 /* Transfer every plink on the list "from" to the list "to" */
2681 void Plink_copy(to,from) 2691 void Plink_copy(struct plink **to, struct plink *from)
2682 struct plink **to;
2683 struct plink *from;
2684 { 2692 {
2685 struct plink *nextpl; 2693 struct plink *nextpl;
2686 while( from ){ 2694 while( from ){
2687 nextpl = from->next; 2695 nextpl = from->next;
2688 from->next = *to; 2696 from->next = *to;
2689 *to = from; 2697 *to = from;
2690 from = nextpl; 2698 from = nextpl;
2691 } 2699 }
2692 } 2700 }
2693 2701
2694 /* Delete every plink on the list */ 2702 /* Delete every plink on the list */
2695 void Plink_delete(plp) 2703 void Plink_delete(struct plink *plp)
2696 struct plink *plp;
2697 { 2704 {
2698 struct plink *nextpl; 2705 struct plink *nextpl;
2699 2706
2700 while( plp ){ 2707 while( plp ){
2701 nextpl = plp->next; 2708 nextpl = plp->next;
2702 plp->next = plink_freelist; 2709 plp->next = plink_freelist;
2703 plink_freelist = plp; 2710 plink_freelist = plp;
2704 plp = nextpl; 2711 plp = nextpl;
2705 } 2712 }
2706 } 2713 }
2707 /*********************** From the file "report.c" **************************/ 2714 /*********************** From the file "report.c" **************************/
2708 /* 2715 /*
2709 ** Procedures for generating reports and tables in the LEMON parser generator. 2716 ** Procedures for generating reports and tables in the LEMON parser generator.
2710 */ 2717 */
2711 2718
2712 /* Generate a filename with the given suffix. Space to hold the 2719 /* Generate a filename with the given suffix. Space to hold the
2713 ** name comes from malloc() and must be freed by the calling 2720 ** name comes from malloc() and must be freed by the calling
2714 ** function. 2721 ** function.
2715 */ 2722 */
2716 PRIVATE char *file_makename(lemp,suffix) 2723 PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
2717 struct lemon *lemp;
2718 char *suffix;
2719 { 2724 {
2720 char *name; 2725 char *name;
2721 char *cp; 2726 char *cp;
2722 2727
2723 name = malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 ); 2728 name = (char*)malloc( lemonStrlen(lemp->filename) + lemonStrlen(suffix) + 5 );
2724 if( name==0 ){ 2729 if( name==0 ){
2725 fprintf(stderr,"Can't allocate space for a filename.\n"); 2730 fprintf(stderr,"Can't allocate space for a filename.\n");
2726 exit(1); 2731 exit(1);
2727 } 2732 }
2728 strcpy(name,lemp->filename); 2733 strcpy(name,lemp->filename);
2729 cp = strrchr(name,'.'); 2734 cp = strrchr(name,'.');
2730 if( cp ) *cp = 0; 2735 if( cp ) *cp = 0;
2731 strcat(name,suffix); 2736 strcat(name,suffix);
2732 return name; 2737 return name;
2733 } 2738 }
2734 2739
2735 /* Open a file with a name based on the name of the input file, 2740 /* Open a file with a name based on the name of the input file,
2736 ** but with a different (specified) suffix, and return a pointer 2741 ** but with a different (specified) suffix, and return a pointer
2737 ** to the stream */ 2742 ** to the stream */
2738 PRIVATE FILE *file_open(lemp,suffix,mode) 2743 PRIVATE FILE *file_open(
2739 struct lemon *lemp; 2744 struct lemon *lemp,
2740 char *suffix; 2745 const char *suffix,
2741 char *mode; 2746 const char *mode
2742 { 2747 ){
2743 FILE *fp; 2748 FILE *fp;
2744 2749
2745 if( lemp->outname ) free(lemp->outname); 2750 if( lemp->outname ) free(lemp->outname);
2746 lemp->outname = file_makename(lemp, suffix); 2751 lemp->outname = file_makename(lemp, suffix);
2747 fp = fopen(lemp->outname,mode); 2752 fp = fopen(lemp->outname,mode);
2748 if( fp==0 && *mode=='w' ){ 2753 if( fp==0 && *mode=='w' ){
2749 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname); 2754 fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
2750 lemp->errorcnt++; 2755 lemp->errorcnt++;
2751 return 0; 2756 return 0;
2752 } 2757 }
2758
2759 /* Add files we create to a list, so we can delete them if we fail. This
2760 ** is to keep makefiles from getting confused. We don't include .out files,
2761 ** though: this is debug information, and you don't want it deleted if there
2762 ** was an error you need to track down.
2763 */
2764 if(( *mode=='w' ) && (strcmp(suffix, ".out") != 0)){
2765 const char **ptr = (const char **)
2766 realloc(made_files, sizeof (const char **) * (made_files_count + 1));
2767 const char *fname = Strsafe(lemp->outname);
2768 if ((ptr == NULL) || (fname == NULL)) {
2769 free(ptr);
2770 memory_error();
2771 }
2772 made_files = ptr;
2773 made_files[made_files_count++] = fname;
2774 }
2753 return fp; 2775 return fp;
2754 } 2776 }
2755 2777
2756 /* Duplicate the input file without comments and without actions 2778 /* Duplicate the input file without comments and without actions
2757 ** on rules */ 2779 ** on rules */
2758 void Reprint(lemp) 2780 void Reprint(struct lemon *lemp)
2759 struct lemon *lemp;
2760 { 2781 {
2761 struct rule *rp; 2782 struct rule *rp;
2762 struct symbol *sp; 2783 struct symbol *sp;
2763 int i, j, maxlen, len, ncolumns, skip; 2784 int i, j, maxlen, len, ncolumns, skip;
2764 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename); 2785 printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
2765 maxlen = 10; 2786 maxlen = 10;
2766 for(i=0; i<lemp->nsymbol; i++){ 2787 for(i=0; i<lemp->nsymbol; i++){
2767 sp = lemp->symbols[i]; 2788 sp = lemp->symbols[i];
2768 len = lemonStrlen(sp->name); 2789 len = lemonStrlen(sp->name);
2769 if( len>maxlen ) maxlen = len; 2790 if( len>maxlen ) maxlen = len;
(...skipping 24 matching lines...) Expand all
2794 } 2815 }
2795 /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */ 2816 /* if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
2796 } 2817 }
2797 printf("."); 2818 printf(".");
2798 if( rp->precsym ) printf(" [%s]",rp->precsym->name); 2819 if( rp->precsym ) printf(" [%s]",rp->precsym->name);
2799 /* if( rp->code ) printf("\n %s",rp->code); */ 2820 /* if( rp->code ) printf("\n %s",rp->code); */
2800 printf("\n"); 2821 printf("\n");
2801 } 2822 }
2802 } 2823 }
2803 2824
2804 void ConfigPrint(fp,cfp) 2825 void ConfigPrint(FILE *fp, struct config *cfp)
2805 FILE *fp;
2806 struct config *cfp;
2807 { 2826 {
2808 struct rule *rp; 2827 struct rule *rp;
2809 struct symbol *sp; 2828 struct symbol *sp;
2810 int i, j; 2829 int i, j;
2811 rp = cfp->rp; 2830 rp = cfp->rp;
2812 fprintf(fp,"%s ::=",rp->lhs->name); 2831 fprintf(fp,"%s ::=",rp->lhs->name);
2813 for(i=0; i<=rp->nrhs; i++){ 2832 for(i=0; i<=rp->nrhs; i++){
2814 if( i==cfp->dot ) fprintf(fp," *"); 2833 if( i==cfp->dot ) fprintf(fp," *");
2815 if( i==rp->nrhs ) break; 2834 if( i==rp->nrhs ) break;
2816 sp = rp->rhs[i]; 2835 sp = rp->rhs[i];
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
2876 break; 2895 break;
2877 case ERROR: 2896 case ERROR:
2878 fprintf(fp,"%*s error",indent,ap->sp->name); 2897 fprintf(fp,"%*s error",indent,ap->sp->name);
2879 break; 2898 break;
2880 case SRCONFLICT: 2899 case SRCONFLICT:
2881 case RRCONFLICT: 2900 case RRCONFLICT:
2882 fprintf(fp,"%*s reduce %-3d ** Parsing conflict **", 2901 fprintf(fp,"%*s reduce %-3d ** Parsing conflict **",
2883 indent,ap->sp->name,ap->x.rp->index); 2902 indent,ap->sp->name,ap->x.rp->index);
2884 break; 2903 break;
2885 case SSCONFLICT: 2904 case SSCONFLICT:
2886 fprintf(fp,"%*s shift %d ** Parsing conflict **", 2905 fprintf(fp,"%*s shift %-3d ** Parsing conflict **",
2887 indent,ap->sp->name,ap->x.stp->statenum); 2906 indent,ap->sp->name,ap->x.stp->statenum);
2888 break; 2907 break;
2889 case SH_RESOLVED: 2908 case SH_RESOLVED:
2909 if( showPrecedenceConflict ){
2910 fprintf(fp,"%*s shift %-3d -- dropped by precedence",
2911 indent,ap->sp->name,ap->x.stp->statenum);
2912 }else{
2913 result = 0;
2914 }
2915 break;
2890 case RD_RESOLVED: 2916 case RD_RESOLVED:
2917 if( showPrecedenceConflict ){
2918 fprintf(fp,"%*s reduce %-3d -- dropped by precedence",
2919 indent,ap->sp->name,ap->x.rp->index);
2920 }else{
2921 result = 0;
2922 }
2923 break;
2891 case NOT_USED: 2924 case NOT_USED:
2892 result = 0; 2925 result = 0;
2893 break; 2926 break;
2894 } 2927 }
2895 return result; 2928 return result;
2896 } 2929 }
2897 2930
2898 /* Generate the "y.output" log file */ 2931 /* Generate the "y.output" log file */
2899 void ReportOutput(lemp) 2932 void ReportOutput(struct lemon *lemp)
2900 struct lemon *lemp;
2901 { 2933 {
2902 int i; 2934 int i;
2903 struct state *stp; 2935 struct state *stp;
2904 struct config *cfp; 2936 struct config *cfp;
2905 struct action *ap; 2937 struct action *ap;
2906 FILE *fp; 2938 FILE *fp;
2907 2939
2908 fp = file_open(lemp,".out","wb"); 2940 fp = file_open(lemp,".out","wb");
2909 if( fp==0 ) return; 2941 if( fp==0 ) return;
2910 for(i=0; i<lemp->nstate; i++){ 2942 for(i=0; i<lemp->nstate; i++){
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
2956 } 2988 }
2957 } 2989 }
2958 fprintf(fp, "\n"); 2990 fprintf(fp, "\n");
2959 } 2991 }
2960 fclose(fp); 2992 fclose(fp);
2961 return; 2993 return;
2962 } 2994 }
2963 2995
2964 /* Search for the file "name" which is in the same directory as 2996 /* Search for the file "name" which is in the same directory as
2965 ** the exacutable */ 2997 ** the exacutable */
2966 PRIVATE char *pathsearch(argv0,name,modemask) 2998 PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
2967 char *argv0;
2968 char *name;
2969 int modemask;
2970 { 2999 {
2971 char *pathlist; 3000 const char *pathlist;
3001 char *pathbufptr;
3002 char *pathbuf;
2972 char *path,*cp; 3003 char *path,*cp;
2973 char c; 3004 char c;
2974 3005
2975 #ifdef __WIN32__ 3006 #ifdef __WIN32__
2976 cp = strrchr(argv0,'\\'); 3007 cp = strrchr(argv0,'\\');
2977 #else 3008 #else
2978 cp = strrchr(argv0,'/'); 3009 cp = strrchr(argv0,'/');
2979 #endif 3010 #endif
2980 if( cp ){ 3011 if( cp ){
2981 c = *cp; 3012 c = *cp;
2982 *cp = 0; 3013 *cp = 0;
2983 path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 ); 3014 path = (char *)malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
2984 if( path ) sprintf(path,"%s/%s",argv0,name); 3015 if( path ) sprintf(path,"%s/%s",argv0,name);
2985 *cp = c; 3016 *cp = c;
2986 }else{ 3017 }else{
2987 extern char *getenv();
2988 pathlist = getenv("PATH"); 3018 pathlist = getenv("PATH");
2989 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin"; 3019 if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
3020 pathbuf = (char *) malloc( lemonStrlen(pathlist) + 1 );
2990 path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 ); 3021 path = (char *)malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
2991 if( path!=0 ){ 3022 if( (pathbuf != 0) && (path!=0) ){
2992 while( *pathlist ){ 3023 pathbufptr = pathbuf;
2993 cp = strchr(pathlist,':'); 3024 strcpy(pathbuf, pathlist);
2994 if( cp==0 ) cp = &pathlist[lemonStrlen(pathlist)]; 3025 while( *pathbuf ){
3026 cp = strchr(pathbuf,':');
3027 if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
2995 c = *cp; 3028 c = *cp;
2996 *cp = 0; 3029 *cp = 0;
2997 sprintf(path,"%s/%s",pathlist,name); 3030 sprintf(path,"%s/%s",pathbuf,name);
2998 *cp = c; 3031 *cp = c;
2999 if( c==0 ) pathlist = ""; 3032 if( c==0 ) pathbuf[0] = 0;
3000 else pathlist = &cp[1]; 3033 else pathbuf = &cp[1];
3001 if( access(path,modemask)==0 ) break; 3034 if( access(path,modemask)==0 ) break;
3002 } 3035 }
3036 free(pathbufptr);
3003 } 3037 }
3004 } 3038 }
3005 return path; 3039 return path;
3006 } 3040 }
3007 3041
3008 /* Given an action, compute the integer value for that action 3042 /* Given an action, compute the integer value for that action
3009 ** which is to be put in the action table of the generated machine. 3043 ** which is to be put in the action table of the generated machine.
3010 ** Return negative if no action should be generated. 3044 ** Return negative if no action should be generated.
3011 */ 3045 */
3012 PRIVATE int compute_action(lemp,ap) 3046 PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
3013 struct lemon *lemp;
3014 struct action *ap;
3015 { 3047 {
3016 int act; 3048 int act;
3017 switch( ap->type ){ 3049 switch( ap->type ){
3018 case SHIFT: act = ap->x.stp->statenum; break; 3050 case SHIFT: act = ap->x.stp->statenum; break;
3019 case REDUCE: act = ap->x.rp->index + lemp->nstate; break; 3051 case REDUCE: act = ap->x.rp->index + lemp->nstate; break;
3020 case ERROR: act = lemp->nstate + lemp->nrule; break; 3052 case ERROR: act = lemp->nstate + lemp->nrule; break;
3021 case ACCEPT: act = lemp->nstate + lemp->nrule + 1; break; 3053 case ACCEPT: act = lemp->nstate + lemp->nrule + 1; break;
3022 default: act = -1; break; 3054 default: act = -1; break;
3023 } 3055 }
3024 return act; 3056 return act;
3025 } 3057 }
3026 3058
3027 #define LINESIZE 1000 3059 #define LINESIZE 1000
3028 /* The next cluster of routines are for reading the template file 3060 /* The next cluster of routines are for reading the template file
3029 ** and writing the results to the generated parser */ 3061 ** and writing the results to the generated parser */
3030 /* The first function transfers data from "in" to "out" until 3062 /* The first function transfers data from "in" to "out" until
3031 ** a line is seen which begins with "%%". The line number is 3063 ** a line is seen which begins with "%%". The line number is
3032 ** tracked. 3064 ** tracked.
3033 ** 3065 **
3034 ** if name!=0, then any word that begin with "Parse" is changed to 3066 ** if name!=0, then any word that begin with "Parse" is changed to
3035 ** begin with *name instead. 3067 ** begin with *name instead.
3036 */ 3068 */
3037 PRIVATE void tplt_xfer(name,in,out,lineno) 3069 PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
3038 char *name;
3039 FILE *in;
3040 FILE *out;
3041 int *lineno;
3042 { 3070 {
3043 int i, iStart; 3071 int i, iStart;
3044 char line[LINESIZE]; 3072 char line[LINESIZE];
3045 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){ 3073 while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
3046 (*lineno)++; 3074 (*lineno)++;
3047 iStart = 0; 3075 iStart = 0;
3048 if( name ){ 3076 if( name ){
3049 for(i=0; line[i]; i++){ 3077 for(i=0; line[i]; i++){
3050 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0 3078 if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
3051 && (i==0 || !isalpha(line[i-1])) 3079 && (i==0 || !isalpha(line[i-1]))
3052 ){ 3080 ){
3053 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]); 3081 if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
3054 fprintf(out,"%s",name); 3082 fprintf(out,"%s",name);
3055 i += 4; 3083 i += 4;
3056 iStart = i+1; 3084 iStart = i+1;
3057 } 3085 }
3058 } 3086 }
3059 } 3087 }
3060 fprintf(out,"%s",&line[iStart]); 3088 fprintf(out,"%s",&line[iStart]);
3061 } 3089 }
3062 } 3090 }
3063 3091
3064 /* The next function finds the template file and opens it, returning 3092 /* The next function finds the template file and opens it, returning
3065 ** a pointer to the opened file. */ 3093 ** a pointer to the opened file. */
3066 PRIVATE FILE *tplt_open(lemp) 3094 PRIVATE FILE *tplt_open(struct lemon *lemp)
3067 struct lemon *lemp;
3068 { 3095 {
3069 static char templatename[] = "lempar.c"; 3096 static char templatename[] = "lempar.c";
3070 char buf[1000]; 3097 char buf[1000];
3071 FILE *in; 3098 FILE *in;
3072 char *tpltname; 3099 char *tpltname;
3073 char *cp; 3100 char *cp;
3074 3101
3102 /* first, see if user specified a template filename on the command line. */
3103 if (user_templatename != 0) {
3104 if( access(user_templatename,004)==-1 ){
3105 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3106 user_templatename);
3107 lemp->errorcnt++;
3108 return 0;
3109 }
3110 in = fopen(user_templatename,"rb");
3111 if( in==0 ){
3112 fprintf(stderr,"Can't open the template file \"%s\".\n",user_templatename) ;
3113 lemp->errorcnt++;
3114 return 0;
3115 }
3116 return in;
3117 }
3118
3075 cp = strrchr(lemp->filename,'.'); 3119 cp = strrchr(lemp->filename,'.');
3076 if( cp ){ 3120 if( cp ){
3077 sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename); 3121 sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
3078 }else{ 3122 }else{
3079 sprintf(buf,"%s.lt",lemp->filename); 3123 sprintf(buf,"%s.lt",lemp->filename);
3080 } 3124 }
3081 if( access(buf,004)==0 ){ 3125 if( access(buf,004)==0 ){
3082 tpltname = buf; 3126 tpltname = buf;
3083 }else if( access(templatename,004)==0 ){ 3127 }else if( access(templatename,004)==0 ){
3084 tpltname = templatename; 3128 tpltname = templatename;
3085 }else{ 3129 }else{
3086 tpltname = pathsearch(lemp->argv0,templatename,0); 3130 tpltname = pathsearch(lemp->argv0,templatename,0);
3087 } 3131 }
3088 if( tpltname==0 ){ 3132 if( tpltname==0 ){
3089 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n", 3133 fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
3090 templatename); 3134 templatename);
3091 lemp->errorcnt++; 3135 lemp->errorcnt++;
3092 return 0; 3136 return 0;
3093 } 3137 }
3094 in = fopen(tpltname,"rb"); 3138 in = fopen(tpltname,"rb");
3095 if( in==0 ){ 3139 if( in==0 ){
3096 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename); 3140 fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
3097 lemp->errorcnt++; 3141 lemp->errorcnt++;
3098 return 0; 3142 return 0;
3099 } 3143 }
3100 return in; 3144 return in;
3101 } 3145 }
3102 3146
3103 /* Print a #line directive line to the output file. */ 3147 /* Print a #line directive line to the output file. */
3104 PRIVATE void tplt_linedir(out,lineno,filename) 3148 PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
3105 FILE *out;
3106 int lineno;
3107 char *filename;
3108 { 3149 {
3109 fprintf(out,"#line %d \"",lineno); 3150 fprintf(out,"#line %d \"",lineno);
3110 while( *filename ){ 3151 while( *filename ){
3111 if( *filename == '\\' ) putc('\\',out); 3152 if( *filename == '\\' ) putc('\\',out);
3112 putc(*filename,out); 3153 putc(*filename,out);
3113 filename++; 3154 filename++;
3114 } 3155 }
3115 fprintf(out,"\"\n"); 3156 fprintf(out,"\"\n");
3116 } 3157 }
3117 3158
3118 /* Print a string to the file and keep the linenumber up to date */ 3159 /* Print a string to the file and keep the linenumber up to date */
3119 PRIVATE void tplt_print(out,lemp,str,lineno) 3160 PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
3120 FILE *out;
3121 struct lemon *lemp;
3122 char *str;
3123 int *lineno;
3124 { 3161 {
3125 if( str==0 ) return; 3162 if( str==0 ) return;
3126 while( *str ){ 3163 while( *str ){
3127 putc(*str,out); 3164 putc(*str,out);
3128 if( *str=='\n' ) (*lineno)++; 3165 if( *str=='\n' ) (*lineno)++;
3129 str++; 3166 str++;
3130 } 3167 }
3131 if( str[-1]!='\n' ){ 3168 if( str[-1]!='\n' ){
3132 putc('\n',out); 3169 putc('\n',out);
3133 (*lineno)++; 3170 (*lineno)++;
3134 } 3171 }
3135 if (!lemp->nolinenosflag) { 3172 if (!lemp->nolinenosflag) {
3136 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); 3173 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3137 } 3174 }
3138 return; 3175 return;
3139 } 3176 }
3140 3177
3141 /* 3178 /*
3142 ** The following routine emits code for the destructor for the 3179 ** The following routine emits code for the destructor for the
3143 ** symbol sp 3180 ** symbol sp
3144 */ 3181 */
3145 void emit_destructor_code(out,sp,lemp,lineno) 3182 void emit_destructor_code(
3146 FILE *out; 3183 FILE *out,
3147 struct symbol *sp; 3184 struct symbol *sp,
3148 struct lemon *lemp; 3185 struct lemon *lemp,
3149 int *lineno; 3186 int *lineno
3150 { 3187 ){
3151 char *cp = 0; 3188 char *cp = 0;
3152 3189
3153 if( sp->type==TERMINAL ){ 3190 if( sp->type==TERMINAL ){
3154 cp = lemp->tokendest; 3191 cp = lemp->tokendest;
3155 if( cp==0 ) return; 3192 if( cp==0 ) return;
3156 fprintf(out,"{\n"); (*lineno)++; 3193 fprintf(out,"{\n"); (*lineno)++;
3157 }else if( sp->destructor ){ 3194 }else if( sp->destructor ){
3158 cp = sp->destructor; 3195 cp = sp->destructor;
3159 fprintf(out,"{\n"); (*lineno)++; 3196 fprintf(out,"{\n"); (*lineno)++;
3160 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,sp->destLineno,lemp ->filename); } 3197 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,sp->destLineno,lemp ->filename); }
(...skipping 17 matching lines...) Expand all
3178 if (!lemp->nolinenosflag) { 3215 if (!lemp->nolinenosflag) {
3179 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname); 3216 (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
3180 } 3217 }
3181 fprintf(out,"}\n"); (*lineno)++; 3218 fprintf(out,"}\n"); (*lineno)++;
3182 return; 3219 return;
3183 } 3220 }
3184 3221
3185 /* 3222 /*
3186 ** Return TRUE (non-zero) if the given symbol has a destructor. 3223 ** Return TRUE (non-zero) if the given symbol has a destructor.
3187 */ 3224 */
3188 int has_destructor(sp, lemp) 3225 int has_destructor(struct symbol *sp, struct lemon *lemp)
3189 struct symbol *sp;
3190 struct lemon *lemp;
3191 { 3226 {
3192 int ret; 3227 int ret;
3193 if( sp->type==TERMINAL ){ 3228 if( sp->type==TERMINAL ){
3194 ret = lemp->tokendest!=0; 3229 ret = lemp->tokendest!=0;
3195 }else{ 3230 }else{
3196 ret = lemp->vardest!=0 || sp->destructor!=0; 3231 ret = lemp->vardest!=0 || sp->destructor!=0;
3197 } 3232 }
3198 return ret; 3233 return ret;
3199 } 3234 }
3200 3235
3201 /* 3236 /*
3202 ** Append text to a dynamically allocated string. If zText is 0 then 3237 ** Append text to a dynamically allocated string. If zText is 0 then
3203 ** reset the string to be empty again. Always return the complete text 3238 ** reset the string to be empty again. Always return the complete text
3204 ** of the string (which is overwritten with each call). 3239 ** of the string (which is overwritten with each call).
3205 ** 3240 **
3206 ** n bytes of zText are stored. If n==0 then all of zText up to the first 3241 ** n bytes of zText are stored. If n==0 then all of zText up to the first
3207 ** \000 terminator is stored. zText can contain up to two instances of 3242 ** \000 terminator is stored. zText can contain up to two instances of
3208 ** %d. The values of p1 and p2 are written into the first and second 3243 ** %d. The values of p1 and p2 are written into the first and second
3209 ** %d. 3244 ** %d.
3210 ** 3245 **
3211 ** If n==-1, then the previous character is overwritten. 3246 ** If n==-1, then the previous character is overwritten.
3212 */ 3247 */
3213 PRIVATE char *append_str(char *zText, int n, int p1, int p2){ 3248 PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
3249 static char empty[1] = { 0 };
3214 static char *z = 0; 3250 static char *z = 0;
3215 static int alloced = 0; 3251 static int alloced = 0;
3216 static int used = 0; 3252 static int used = 0;
3217 int c; 3253 int c;
3218 char zInt[40]; 3254 char zInt[40];
3219
3220 if( zText==0 ){ 3255 if( zText==0 ){
3221 used = 0; 3256 used = 0;
3222 return z; 3257 return z;
3223 } 3258 }
3224 if( n<=0 ){ 3259 if( n<=0 ){
3225 if( n<0 ){ 3260 if( n<0 ){
3226 used += n; 3261 used += n;
3227 assert( used>=0 ); 3262 assert( used>=0 );
3228 } 3263 }
3229 n = lemonStrlen(zText); 3264 n = lemonStrlen(zText);
3230 } 3265 }
3231 if( n+sizeof(zInt)*2+used >= alloced ){ 3266 if( n+sizeof(zInt)*2+used >= alloced ){
3232 alloced = n + sizeof(zInt)*2 + used + 200; 3267 alloced = n + sizeof(zInt)*2 + used + 200;
3233 z = realloc(z, alloced); 3268 z = (char *) realloc(z, alloced);
3234 } 3269 }
3235 if( z==0 ) return ""; 3270 if( z==0 ) return empty;
3236 while( n-- > 0 ){ 3271 while( n-- > 0 ){
3237 c = *(zText++); 3272 c = *(zText++);
3238 if( c=='%' && n>0 && zText[0]=='d' ){ 3273 if( c=='%' && n>0 && zText[0]=='d' ){
3239 sprintf(zInt, "%d", p1); 3274 sprintf(zInt, "%d", p1);
3240 p1 = p2; 3275 p1 = p2;
3241 strcpy(&z[used], zInt); 3276 strcpy(&z[used], zInt);
3242 used += lemonStrlen(&z[used]); 3277 used += lemonStrlen(&z[used]);
3243 zText++; 3278 zText++;
3244 n--; 3279 n--;
3245 }else{ 3280 }else{
(...skipping 12 matching lines...) Expand all
3258 PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){ 3293 PRIVATE void translate_code(struct lemon *lemp, struct rule *rp){
3259 char *cp, *xp; 3294 char *cp, *xp;
3260 int i; 3295 int i;
3261 char lhsused = 0; /* True if the LHS element has been used */ 3296 char lhsused = 0; /* True if the LHS element has been used */
3262 char used[MAXRHS]; /* True for each RHS element which is used */ 3297 char used[MAXRHS]; /* True for each RHS element which is used */
3263 3298
3264 for(i=0; i<rp->nrhs; i++) used[i] = 0; 3299 for(i=0; i<rp->nrhs; i++) used[i] = 0;
3265 lhsused = 0; 3300 lhsused = 0;
3266 3301
3267 if( rp->code==0 ){ 3302 if( rp->code==0 ){
3268 rp->code = "\n"; 3303 static char newlinestr[2] = { '\n', '\0' };
3304 rp->code = newlinestr;
3269 rp->line = rp->ruleline; 3305 rp->line = rp->ruleline;
3270 } 3306 }
3271 3307
3272 append_str(0,0,0,0); 3308 append_str(0,0,0,0);
3273 for(cp=rp->code; *cp; cp++){ 3309
3310 /* This const cast is wrong but harmless, if we're careful. */
3311 for(cp=(char *)rp->code; *cp; cp++){
3274 if( isalpha(*cp) && (cp==rp->code || (!isalnum(cp[-1]) && cp[-1]!='_')) ){ 3312 if( isalpha(*cp) && (cp==rp->code || (!isalnum(cp[-1]) && cp[-1]!='_')) ){
3275 char saved; 3313 char saved;
3276 for(xp= &cp[1]; isalnum(*xp) || *xp=='_'; xp++); 3314 for(xp= &cp[1]; isalnum(*xp) || *xp=='_'; xp++);
3277 saved = *xp; 3315 saved = *xp;
3278 *xp = 0; 3316 *xp = 0;
3279 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){ 3317 if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
3280 append_str("yygotominor.yy%d",0,rp->lhs->dtnum,0); 3318 append_str("yygotominor.yy%d",0,rp->lhs->dtnum,0);
3281 cp = xp; 3319 cp = xp;
3282 lhsused = 1; 3320 lhsused = 1;
3283 }else{ 3321 }else{
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
3336 if( rp->code ){ 3374 if( rp->code ){
3337 cp = append_str(0,0,0,0); 3375 cp = append_str(0,0,0,0);
3338 rp->code = Strsafe(cp?cp:""); 3376 rp->code = Strsafe(cp?cp:"");
3339 } 3377 }
3340 } 3378 }
3341 3379
3342 /* 3380 /*
3343 ** Generate code which executes when the rule "rp" is reduced. Write 3381 ** Generate code which executes when the rule "rp" is reduced. Write
3344 ** the code to "out". Make sure lineno stays up-to-date. 3382 ** the code to "out". Make sure lineno stays up-to-date.
3345 */ 3383 */
3346 PRIVATE void emit_code(out,rp,lemp,lineno) 3384 PRIVATE void emit_code(
3347 FILE *out; 3385 FILE *out,
3348 struct rule *rp; 3386 struct rule *rp,
3349 struct lemon *lemp; 3387 struct lemon *lemp,
3350 int *lineno; 3388 int *lineno
3351 { 3389 ){
3352 char *cp; 3390 const char *cp;
3353 3391
3354 /* Generate code to do the reduce action */ 3392 /* Generate code to do the reduce action */
3355 if( rp->code ){ 3393 if( rp->code ){
3356 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,rp->line,lemp->file name); } 3394 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,rp->line,lemp->file name); }
3357 fprintf(out,"{%s",rp->code); 3395 fprintf(out,"{%s",rp->code);
3358 for(cp=rp->code; *cp; cp++){ 3396 for(cp=rp->code; *cp; cp++){
3359 if( *cp=='\n' ) (*lineno)++; 3397 if( *cp=='\n' ) (*lineno)++;
3360 } /* End loop */ 3398 } /* End loop */
3361 fprintf(out,"}\n"); (*lineno)++; 3399 fprintf(out,"}\n"); (*lineno)++;
3362 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,*lineno,lemp->outna me); } 3400 if (!lemp->nolinenosflag) { (*lineno)++; tplt_linedir(out,*lineno,lemp->outna me); }
3363 } /* End if( rp->code ) */ 3401 } /* End if( rp->code ) */
3364 3402
3365 return; 3403 return;
3366 } 3404 }
3367 3405
3368 /* 3406 /*
3369 ** Print the definition of the union used for the parser's data stack. 3407 ** Print the definition of the union used for the parser's data stack.
3370 ** This union contains fields for every possible data type for tokens 3408 ** This union contains fields for every possible data type for tokens
3371 ** and nonterminals. In the process of computing and printing this 3409 ** and nonterminals. In the process of computing and printing this
3372 ** union, also set the ".dtnum" field of every terminal and nonterminal 3410 ** union, also set the ".dtnum" field of every terminal and nonterminal
3373 ** symbol. 3411 ** symbol.
3374 */ 3412 */
3375 void print_stack_union(out,lemp,plineno,mhflag) 3413 void print_stack_union(
3376 FILE *out; /* The output stream */ 3414 FILE *out, /* The output stream */
3377 struct lemon *lemp; /* The main info structure for this parser */ 3415 struct lemon *lemp, /* The main info structure for this parser */
3378 int *plineno; /* Pointer to the line number */ 3416 int *plineno, /* Pointer to the line number */
3379 int mhflag; /* True if generating makeheaders output */ 3417 int mhflag /* True if generating makeheaders output */
3380 { 3418 ){
3381 int lineno = *plineno; /* The line number of the output */ 3419 int lineno = *plineno; /* The line number of the output */
3382 char **types; /* A hash table of datatypes */ 3420 char **types; /* A hash table of datatypes */
3383 int arraysize; /* Size of the "types" array */ 3421 int arraysize; /* Size of the "types" array */
3384 int maxdtlength; /* Maximum length of any ".datatype" field. */ 3422 int maxdtlength; /* Maximum length of any ".datatype" field. */
3385 char *stddt; /* Standardized name for a datatype */ 3423 char *stddt; /* Standardized name for a datatype */
3386 int i,j; /* Loop counters */ 3424 int i,j; /* Loop counters */
3387 int hash; /* For hashing the name of a type */ 3425 int hash; /* For hashing the name of a type */
3388 char *name; /* Name of the parser */ 3426 const char *name; /* Name of the parser */
3389 3427
3390 /* Allocate and initialize types[] and allocate stddt[] */ 3428 /* Allocate and initialize types[] and allocate stddt[] */
3391 arraysize = lemp->nsymbol * 2; 3429 arraysize = lemp->nsymbol * 2;
3392 types = (char**)calloc( arraysize, sizeof(char*) ); 3430 types = (char**)calloc( arraysize, sizeof(char*) );
3393 for(i=0; i<arraysize; i++) types[i] = 0; 3431 for(i=0; i<arraysize; i++) types[i] = 0;
3394 maxdtlength = 0; 3432 maxdtlength = 0;
3395 if( lemp->vartype ){ 3433 if( lemp->vartype ){
3396 maxdtlength = lemonStrlen(lemp->vartype); 3434 maxdtlength = lemonStrlen(lemp->vartype);
3397 } 3435 }
3398 for(i=0; i<lemp->nsymbol; i++){ 3436 for(i=0; i<lemp->nsymbol; i++){
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
3509 /* 3547 /*
3510 ** Each state contains a set of token transaction and a set of 3548 ** Each state contains a set of token transaction and a set of
3511 ** nonterminal transactions. Each of these sets makes an instance 3549 ** nonterminal transactions. Each of these sets makes an instance
3512 ** of the following structure. An array of these structures is used 3550 ** of the following structure. An array of these structures is used
3513 ** to order the creation of entries in the yy_action[] table. 3551 ** to order the creation of entries in the yy_action[] table.
3514 */ 3552 */
3515 struct axset { 3553 struct axset {
3516 struct state *stp; /* A pointer to a state */ 3554 struct state *stp; /* A pointer to a state */
3517 int isTkn; /* True to use tokens. False for non-terminals */ 3555 int isTkn; /* True to use tokens. False for non-terminals */
3518 int nAction; /* Number of actions */ 3556 int nAction; /* Number of actions */
3557 int iOrder; /* Original order of action sets */
3519 }; 3558 };
3520 3559
3521 /* 3560 /*
3522 ** Compare to axset structures for sorting purposes 3561 ** Compare to axset structures for sorting purposes
3523 */ 3562 */
3524 static int axset_compare(const void *a, const void *b){ 3563 static int axset_compare(const void *a, const void *b){
3525 struct axset *p1 = (struct axset*)a; 3564 struct axset *p1 = (struct axset*)a;
3526 struct axset *p2 = (struct axset*)b; 3565 struct axset *p2 = (struct axset*)b;
3527 return p2->nAction - p1->nAction; 3566 int c;
3567 c = p2->nAction - p1->nAction;
3568 if( c==0 ){
3569 c = p2->iOrder - p1->iOrder;
3570 }
3571 assert( c!=0 || p1==p2 );
3572 return c;
3528 } 3573 }
3529 3574
3530 /* 3575 /*
3531 ** Write text on "out" that describes the rule "rp". 3576 ** Write text on "out" that describes the rule "rp".
3532 */ 3577 */
3533 static void writeRuleText(FILE *out, struct rule *rp){ 3578 static void writeRuleText(FILE *out, struct rule *rp){
3534 int j; 3579 int j;
3535 fprintf(out,"%s ::=", rp->lhs->name); 3580 fprintf(out,"%s ::=", rp->lhs->name);
3536 for(j=0; j<rp->nrhs; j++){ 3581 for(j=0; j<rp->nrhs; j++){
3537 struct symbol *sp = rp->rhs[j]; 3582 struct symbol *sp = rp->rhs[j];
3538 fprintf(out," %s", sp->name); 3583 fprintf(out," %s", sp->name);
3539 if( sp->type==MULTITERMINAL ){ 3584 if( sp->type==MULTITERMINAL ){
3540 int k; 3585 int k;
3541 for(k=1; k<sp->nsubsym; k++){ 3586 for(k=1; k<sp->nsubsym; k++){
3542 fprintf(out,"|%s",sp->subsym[k]->name); 3587 fprintf(out,"|%s",sp->subsym[k]->name);
3543 } 3588 }
3544 } 3589 }
3545 } 3590 }
3546 } 3591 }
3547 3592
3548 3593
3549 /* Generate C source code for the parser */ 3594 /* Generate C source code for the parser */
3550 void ReportTable(lemp, mhflag) 3595 void ReportTable(
3551 struct lemon *lemp; 3596 struct lemon *lemp,
3552 int mhflag; /* Output in makeheaders format if true */ 3597 int mhflag /* Output in makeheaders format if true */
3553 { 3598 ){
3554 FILE *out, *in; 3599 FILE *out, *in;
3555 char line[LINESIZE]; 3600 char line[LINESIZE];
3556 int lineno; 3601 int lineno;
3557 struct state *stp; 3602 struct state *stp;
3558 struct action *ap; 3603 struct action *ap;
3559 struct rule *rp; 3604 struct rule *rp;
3560 struct acttab *pActtab; 3605 struct acttab *pActtab;
3561 int i, j, n; 3606 int i, j, n;
3562 char *name; 3607 const char *name;
3563 int mnTknOfst, mxTknOfst; 3608 int mnTknOfst, mxTknOfst;
3564 int mnNtOfst, mxNtOfst; 3609 int mnNtOfst, mxNtOfst;
3565 struct axset *ax; 3610 struct axset *ax;
3566 3611
3567 in = tplt_open(lemp); 3612 in = tplt_open(lemp);
3568 if( in==0 ) return; 3613 if( in==0 ) return;
3569 out = file_open(lemp,".c","wb"); 3614 out = file_open(lemp,".c","wb");
3570 if( out==0 ){ 3615 if( out==0 ){
3571 fclose(in); 3616 fclose(in);
3572 return; 3617 return;
3573 } 3618 }
3574 lineno = 1; 3619 lineno = 1;
3575 tplt_xfer(lemp->name,in,out,&lineno); 3620 tplt_xfer(lemp->name,in,out,&lineno);
3576 3621
3577 /* Generate the include code, if any */ 3622 /* Generate the include code, if any */
3578 tplt_print(out,lemp,lemp->include,&lineno); 3623 tplt_print(out,lemp,lemp->include,&lineno);
3579 if( mhflag ){ 3624 if( mhflag ){
3580 char *name = file_makename(lemp, ".h"); 3625 char *name = file_makename(lemp, ".h");
3581 fprintf(out,"#include \"%s\"\n", name); lineno++; 3626 fprintf(out,"#include \"%s\"\n", name); lineno++;
3582 free(name); 3627 free(name);
3583 } 3628 }
3584 tplt_xfer(lemp->name,in,out,&lineno); 3629 tplt_xfer(lemp->name,in,out,&lineno);
3585 3630
3586 /* Generate #defines for all tokens */ 3631 /* Generate #defines for all tokens */
3587 if( mhflag ){ 3632 if( mhflag ){
3588 char *prefix; 3633 const char *prefix;
3589 fprintf(out,"#if INTERFACE\n"); lineno++; 3634 fprintf(out,"#if INTERFACE\n"); lineno++;
3590 if( lemp->tokenprefix ) prefix = lemp->tokenprefix; 3635 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
3591 else prefix = ""; 3636 else prefix = "";
3592 for(i=1; i<lemp->nterminal; i++){ 3637 for(i=1; i<lemp->nterminal; i++){
3593 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i); 3638 fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
3594 lineno++; 3639 lineno++;
3595 } 3640 }
3596 fprintf(out,"#endif\n"); lineno++; 3641 fprintf(out,"#endif\n"); lineno++;
3597 } 3642 }
3598 tplt_xfer(lemp->name,in,out,&lineno); 3643 tplt_xfer(lemp->name,in,out,&lineno);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
3656 ** yy_lookahead[] A table containing the lookahead for each entry in 3701 ** yy_lookahead[] A table containing the lookahead for each entry in
3657 ** yy_action. Used to detect hash collisions. 3702 ** yy_action. Used to detect hash collisions.
3658 ** yy_shift_ofst[] For each state, the offset into yy_action for 3703 ** yy_shift_ofst[] For each state, the offset into yy_action for
3659 ** shifting terminals. 3704 ** shifting terminals.
3660 ** yy_reduce_ofst[] For each state, the offset into yy_action for 3705 ** yy_reduce_ofst[] For each state, the offset into yy_action for
3661 ** shifting non-terminals after a reduce. 3706 ** shifting non-terminals after a reduce.
3662 ** yy_default[] Default action for each state. 3707 ** yy_default[] Default action for each state.
3663 */ 3708 */
3664 3709
3665 /* Compute the actions on all states and count them up */ 3710 /* Compute the actions on all states and count them up */
3666 ax = calloc(lemp->nstate*2, sizeof(ax[0])); 3711 ax = (struct axset *) calloc(lemp->nstate*2, sizeof(ax[0]));
3667 if( ax==0 ){ 3712 if( ax==0 ){
3668 fprintf(stderr,"malloc failed\n"); 3713 fprintf(stderr,"malloc failed\n");
3669 exit(1); 3714 exit(1);
3670 } 3715 }
3671 for(i=0; i<lemp->nstate; i++){ 3716 for(i=0; i<lemp->nstate; i++){
3672 stp = lemp->sorted[i]; 3717 stp = lemp->sorted[i];
3673 ax[i*2].stp = stp; 3718 ax[i*2].stp = stp;
3674 ax[i*2].isTkn = 1; 3719 ax[i*2].isTkn = 1;
3675 ax[i*2].nAction = stp->nTknAct; 3720 ax[i*2].nAction = stp->nTknAct;
3676 ax[i*2+1].stp = stp; 3721 ax[i*2+1].stp = stp;
3677 ax[i*2+1].isTkn = 0; 3722 ax[i*2+1].isTkn = 0;
3678 ax[i*2+1].nAction = stp->nNtAct; 3723 ax[i*2+1].nAction = stp->nNtAct;
3679 } 3724 }
3680 mxTknOfst = mnTknOfst = 0; 3725 mxTknOfst = mnTknOfst = 0;
3681 mxNtOfst = mnNtOfst = 0; 3726 mxNtOfst = mnNtOfst = 0;
3682 3727
3683 /* Compute the action table. In order to try to keep the size of the 3728 /* Compute the action table. In order to try to keep the size of the
3684 ** action table to a minimum, the heuristic of placing the largest action 3729 ** action table to a minimum, the heuristic of placing the largest action
3685 ** sets first is used. 3730 ** sets first is used.
3686 */ 3731 */
3732 for(i=0; i<lemp->nstate*2; i++) ax[i].iOrder = i;
3687 qsort(ax, lemp->nstate*2, sizeof(ax[0]), axset_compare); 3733 qsort(ax, lemp->nstate*2, sizeof(ax[0]), axset_compare);
3688 pActtab = acttab_alloc(); 3734 pActtab = acttab_alloc();
3689 for(i=0; i<lemp->nstate*2 && ax[i].nAction>0; i++){ 3735 for(i=0; i<lemp->nstate*2 && ax[i].nAction>0; i++){
3690 stp = ax[i].stp; 3736 stp = ax[i].stp;
3691 if( ax[i].isTkn ){ 3737 if( ax[i].isTkn ){
3692 for(ap=stp->ap; ap; ap=ap->next){ 3738 for(ap=stp->ap; ap; ap=ap->next){
3693 int action; 3739 int action;
3694 if( ap->sp->index>=lemp->nterminal ) continue; 3740 if( ap->sp->index>=lemp->nterminal ) continue;
3695 action = compute_action(lemp, ap); 3741 action = compute_action(lemp, ap);
3696 if( action<0 ) continue; 3742 if( action<0 ) continue;
(...skipping 12 matching lines...) Expand all
3709 acttab_action(pActtab, ap->sp->index, action); 3755 acttab_action(pActtab, ap->sp->index, action);
3710 } 3756 }
3711 stp->iNtOfst = acttab_insert(pActtab); 3757 stp->iNtOfst = acttab_insert(pActtab);
3712 if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst; 3758 if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
3713 if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst; 3759 if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
3714 } 3760 }
3715 } 3761 }
3716 free(ax); 3762 free(ax);
3717 3763
3718 /* Output the yy_action table */ 3764 /* Output the yy_action table */
3765 n = acttab_size(pActtab);
3766 fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
3719 fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++; 3767 fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
3720 n = acttab_size(pActtab);
3721 for(i=j=0; i<n; i++){ 3768 for(i=j=0; i<n; i++){
3722 int action = acttab_yyaction(pActtab, i); 3769 int action = acttab_yyaction(pActtab, i);
3723 if( action<0 ) action = lemp->nstate + lemp->nrule + 2; 3770 if( action<0 ) action = lemp->nstate + lemp->nrule + 2;
3724 if( j==0 ) fprintf(out," /* %5d */ ", i); 3771 if( j==0 ) fprintf(out," /* %5d */ ", i);
3725 fprintf(out, " %4d,", action); 3772 fprintf(out, " %4d,", action);
3726 if( j==9 || i==n-1 ){ 3773 if( j==9 || i==n-1 ){
3727 fprintf(out, "\n"); lineno++; 3774 fprintf(out, "\n"); lineno++;
3728 j = 0; 3775 j = 0;
3729 }else{ 3776 }else{
3730 j++; 3777 j++;
(...skipping 14 matching lines...) Expand all
3745 }else{ 3792 }else{
3746 j++; 3793 j++;
3747 } 3794 }
3748 } 3795 }
3749 fprintf(out, "};\n"); lineno++; 3796 fprintf(out, "};\n"); lineno++;
3750 3797
3751 /* Output the yy_shift_ofst[] table */ 3798 /* Output the yy_shift_ofst[] table */
3752 fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++; 3799 fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++;
3753 n = lemp->nstate; 3800 n = lemp->nstate;
3754 while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--; 3801 while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
3755 fprintf(out, "#define YY_SHIFT_MAX %d\n", n-1); lineno++; 3802 fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
3803 fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
3804 fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
3756 fprintf(out, "static const %s yy_shift_ofst[] = {\n", 3805 fprintf(out, "static const %s yy_shift_ofst[] = {\n",
3757 minimum_size_type(mnTknOfst-1, mxTknOfst)); lineno++; 3806 minimum_size_type(mnTknOfst-1, mxTknOfst)); lineno++;
3758 for(i=j=0; i<n; i++){ 3807 for(i=j=0; i<n; i++){
3759 int ofst; 3808 int ofst;
3760 stp = lemp->sorted[i]; 3809 stp = lemp->sorted[i];
3761 ofst = stp->iTknOfst; 3810 ofst = stp->iTknOfst;
3762 if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1; 3811 if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1;
3763 if( j==0 ) fprintf(out," /* %5d */ ", i); 3812 if( j==0 ) fprintf(out," /* %5d */ ", i);
3764 fprintf(out, " %4d,", ofst); 3813 fprintf(out, " %4d,", ofst);
3765 if( j==9 || i==n-1 ){ 3814 if( j==9 || i==n-1 ){
3766 fprintf(out, "\n"); lineno++; 3815 fprintf(out, "\n"); lineno++;
3767 j = 0; 3816 j = 0;
3768 }else{ 3817 }else{
3769 j++; 3818 j++;
3770 } 3819 }
3771 } 3820 }
3772 fprintf(out, "};\n"); lineno++; 3821 fprintf(out, "};\n"); lineno++;
3773 3822
3774 /* Output the yy_reduce_ofst[] table */ 3823 /* Output the yy_reduce_ofst[] table */
3775 fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++; 3824 fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
3776 n = lemp->nstate; 3825 n = lemp->nstate;
3777 while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--; 3826 while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
3778 fprintf(out, "#define YY_REDUCE_MAX %d\n", n-1); lineno++; 3827 fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
3828 fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
3829 fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
3779 fprintf(out, "static const %s yy_reduce_ofst[] = {\n", 3830 fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
3780 minimum_size_type(mnNtOfst-1, mxNtOfst)); lineno++; 3831 minimum_size_type(mnNtOfst-1, mxNtOfst)); lineno++;
3781 for(i=j=0; i<n; i++){ 3832 for(i=j=0; i<n; i++){
3782 int ofst; 3833 int ofst;
3783 stp = lemp->sorted[i]; 3834 stp = lemp->sorted[i];
3784 ofst = stp->iNtOfst; 3835 ofst = stp->iNtOfst;
3785 if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1; 3836 if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
3786 if( j==0 ) fprintf(out," /* %5d */ ", i); 3837 if( j==0 ) fprintf(out," /* %5d */ ", i);
3787 fprintf(out, " %4d,", ofst); 3838 fprintf(out, " %4d,", ofst);
3788 if( j==9 || i==n-1 ){ 3839 if( j==9 || i==n-1 ){
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
3978 4029
3979 /* Append any addition code the user desires */ 4030 /* Append any addition code the user desires */
3980 tplt_print(out,lemp,lemp->extracode,&lineno); 4031 tplt_print(out,lemp,lemp->extracode,&lineno);
3981 4032
3982 fclose(in); 4033 fclose(in);
3983 fclose(out); 4034 fclose(out);
3984 return; 4035 return;
3985 } 4036 }
3986 4037
3987 /* Generate a header file for the parser */ 4038 /* Generate a header file for the parser */
3988 void ReportHeader(lemp) 4039 void ReportHeader(struct lemon *lemp)
3989 struct lemon *lemp;
3990 { 4040 {
3991 FILE *out, *in; 4041 FILE *out, *in;
3992 char *prefix; 4042 const char *prefix;
3993 char line[LINESIZE]; 4043 char line[LINESIZE];
3994 char pattern[LINESIZE]; 4044 char pattern[LINESIZE];
3995 int i; 4045 int i;
3996 4046
3997 if( lemp->tokenprefix ) prefix = lemp->tokenprefix; 4047 if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
3998 else prefix = ""; 4048 else prefix = "";
3999 in = file_open(lemp,".h","rb"); 4049 in = file_open(lemp,".h","rb");
4000 if( in ){ 4050 if( in ){
4001 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){ 4051 for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
4002 sprintf(pattern,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i); 4052 sprintf(pattern,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
(...skipping 15 matching lines...) Expand all
4018 return; 4068 return;
4019 } 4069 }
4020 4070
4021 /* Reduce the size of the action tables, if possible, by making use 4071 /* Reduce the size of the action tables, if possible, by making use
4022 ** of defaults. 4072 ** of defaults.
4023 ** 4073 **
4024 ** In this version, we take the most frequent REDUCE action and make 4074 ** In this version, we take the most frequent REDUCE action and make
4025 ** it the default. Except, there is no default if the wildcard token 4075 ** it the default. Except, there is no default if the wildcard token
4026 ** is a possible look-ahead. 4076 ** is a possible look-ahead.
4027 */ 4077 */
4028 void CompressTables(lemp) 4078 void CompressTables(struct lemon *lemp)
4029 struct lemon *lemp;
4030 { 4079 {
4031 struct state *stp; 4080 struct state *stp;
4032 struct action *ap, *ap2; 4081 struct action *ap, *ap2;
4033 struct rule *rp, *rp2, *rbest; 4082 struct rule *rp, *rp2, *rbest;
4034 int nbest, n; 4083 int nbest, n;
4035 int i; 4084 int i;
4036 int usesWildcard; 4085 int usesWildcard;
4037 4086
4038 for(i=0; i<lemp->nstate; i++){ 4087 for(i=0; i<lemp->nstate; i++){
4039 stp = lemp->sorted[i]; 4088 stp = lemp->sorted[i];
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
4090 ** token actions. 4139 ** token actions.
4091 */ 4140 */
4092 static int stateResortCompare(const void *a, const void *b){ 4141 static int stateResortCompare(const void *a, const void *b){
4093 const struct state *pA = *(const struct state**)a; 4142 const struct state *pA = *(const struct state**)a;
4094 const struct state *pB = *(const struct state**)b; 4143 const struct state *pB = *(const struct state**)b;
4095 int n; 4144 int n;
4096 4145
4097 n = pB->nNtAct - pA->nNtAct; 4146 n = pB->nNtAct - pA->nNtAct;
4098 if( n==0 ){ 4147 if( n==0 ){
4099 n = pB->nTknAct - pA->nTknAct; 4148 n = pB->nTknAct - pA->nTknAct;
4149 if( n==0 ){
4150 n = pB->statenum - pA->statenum;
4151 }
4100 } 4152 }
4153 assert( n!=0 );
4101 return n; 4154 return n;
4102 } 4155 }
4103 4156
4104 4157
4105 /* 4158 /*
4106 ** Renumber and resort states so that states with fewer choices 4159 ** Renumber and resort states so that states with fewer choices
4107 ** occur at the end. Except, keep state 0 as the first state. 4160 ** occur at the end. Except, keep state 0 as the first state.
4108 */ 4161 */
4109 void ResortStates(lemp) 4162 void ResortStates(struct lemon *lemp)
4110 struct lemon *lemp;
4111 { 4163 {
4112 int i; 4164 int i;
4113 struct state *stp; 4165 struct state *stp;
4114 struct action *ap; 4166 struct action *ap;
4115 4167
4116 for(i=0; i<lemp->nstate; i++){ 4168 for(i=0; i<lemp->nstate; i++){
4117 stp = lemp->sorted[i]; 4169 stp = lemp->sorted[i];
4118 stp->nTknAct = stp->nNtAct = 0; 4170 stp->nTknAct = stp->nNtAct = 0;
4119 stp->iDflt = lemp->nstate + lemp->nrule; 4171 stp->iDflt = lemp->nstate + lemp->nrule;
4120 stp->iTknOfst = NO_OFFSET; 4172 stp->iTknOfst = NO_OFFSET;
(...skipping 19 matching lines...) Expand all
4140 4192
4141 4193
4142 /***************** From the file "set.c" ************************************/ 4194 /***************** From the file "set.c" ************************************/
4143 /* 4195 /*
4144 ** Set manipulation routines for the LEMON parser generator. 4196 ** Set manipulation routines for the LEMON parser generator.
4145 */ 4197 */
4146 4198
4147 static int size = 0; 4199 static int size = 0;
4148 4200
4149 /* Set the set size */ 4201 /* Set the set size */
4150 void SetSize(n) 4202 void SetSize(int n)
4151 int n;
4152 { 4203 {
4153 size = n+1; 4204 size = n+1;
4154 } 4205 }
4155 4206
4156 /* Allocate a new set */ 4207 /* Allocate a new set */
4157 char *SetNew(){ 4208 char *SetNew(){
4158 char *s; 4209 char *s;
4159 s = (char*)calloc( size, 1); 4210 s = (char*)calloc( size, 1);
4160 if( s==0 ){ 4211 if( s==0 ){
4161 extern void memory_error(); 4212 extern void memory_error();
4162 memory_error(); 4213 memory_error();
4163 } 4214 }
4164 return s; 4215 return s;
4165 } 4216 }
4166 4217
4167 /* Deallocate a set */ 4218 /* Deallocate a set */
4168 void SetFree(s) 4219 void SetFree(char *s)
4169 char *s;
4170 { 4220 {
4171 free(s); 4221 free(s);
4172 } 4222 }
4173 4223
4174 /* Add a new element to the set. Return TRUE if the element was added 4224 /* Add a new element to the set. Return TRUE if the element was added
4175 ** and FALSE if it was already there. */ 4225 ** and FALSE if it was already there. */
4176 int SetAdd(s,e) 4226 int SetAdd(char *s, int e)
4177 char *s;
4178 int e;
4179 { 4227 {
4180 int rv; 4228 int rv;
4181 assert( e>=0 && e<size ); 4229 assert( e>=0 && e<size );
4182 rv = s[e]; 4230 rv = s[e];
4183 s[e] = 1; 4231 s[e] = 1;
4184 return !rv; 4232 return !rv;
4185 } 4233 }
4186 4234
4187 /* Add every element of s2 to s1. Return TRUE if s1 changes. */ 4235 /* Add every element of s2 to s1. Return TRUE if s1 changes. */
4188 int SetUnion(s1,s2) 4236 int SetUnion(char *s1, char *s2)
4189 char *s1;
4190 char *s2;
4191 { 4237 {
4192 int i, progress; 4238 int i, progress;
4193 progress = 0; 4239 progress = 0;
4194 for(i=0; i<size; i++){ 4240 for(i=0; i<size; i++){
4195 if( s2[i]==0 ) continue; 4241 if( s2[i]==0 ) continue;
4196 if( s1[i]==0 ){ 4242 if( s1[i]==0 ){
4197 progress = 1; 4243 progress = 1;
4198 s1[i] = 1; 4244 s1[i] = 1;
4199 } 4245 }
4200 } 4246 }
4201 return progress; 4247 return progress;
4202 } 4248 }
4203 /********************** From the file "table.c" ****************************/ 4249 /********************** From the file "table.c" ****************************/
4204 /* 4250 /*
4205 ** All code in this file has been automatically generated 4251 ** All code in this file has been automatically generated
4206 ** from a specification in the file 4252 ** from a specification in the file
4207 ** "table.q" 4253 ** "table.q"
4208 ** by the associative array code building program "aagen". 4254 ** by the associative array code building program "aagen".
4209 ** Do not edit this file! Instead, edit the specification 4255 ** Do not edit this file! Instead, edit the specification
4210 ** file, then rerun aagen. 4256 ** file, then rerun aagen.
4211 */ 4257 */
4212 /* 4258 /*
4213 ** Code for processing tables in the LEMON parser generator. 4259 ** Code for processing tables in the LEMON parser generator.
4214 */ 4260 */
4215 4261
4216 PRIVATE int strhash(x) 4262 PRIVATE int strhash(const char *x)
4217 char *x;
4218 { 4263 {
4219 int h = 0; 4264 int h = 0;
4220 while( *x) h = h*13 + *(x++); 4265 while( *x) h = h*13 + *(x++);
4221 return h; 4266 return h;
4222 } 4267 }
4223 4268
4224 /* Works like strdup, sort of. Save a string in malloced memory, but 4269 /* Works like strdup, sort of. Save a string in malloced memory, but
4225 ** keep strings in a table so that the same string is not in more 4270 ** keep strings in a table so that the same string is not in more
4226 ** than one place. 4271 ** than one place.
4227 */ 4272 */
4228 char *Strsafe(y) 4273 const char *Strsafe(const char *y)
4229 char *y;
4230 { 4274 {
4231 char *z; 4275 const char *z;
4276 char *cpy;
4232 4277
4233 if( y==0 ) return 0; 4278 if( y==0 ) return 0;
4234 z = Strsafe_find(y); 4279 z = Strsafe_find(y);
4235 if( z==0 && (z=malloc( lemonStrlen(y)+1 ))!=0 ){ 4280 if( z==0 && (cpy=(char *)malloc( lemonStrlen(y)+1 ))!=0 ){
4236 strcpy(z,y); 4281 strcpy(cpy,y);
4282 z = cpy;
4237 Strsafe_insert(z); 4283 Strsafe_insert(z);
4238 } 4284 }
4239 MemoryCheck(z); 4285 MemoryCheck(z);
4240 return z; 4286 return z;
4241 } 4287 }
4242 4288
4243 /* There is one instance of the following structure for each 4289 /* There is one instance of the following structure for each
4244 ** associative array of type "x1". 4290 ** associative array of type "x1".
4245 */ 4291 */
4246 struct s_x1 { 4292 struct s_x1 {
4247 int size; /* The number of available slots. */ 4293 int size; /* The number of available slots. */
4248 /* Must be a power of 2 greater than or */ 4294 /* Must be a power of 2 greater than or */
4249 /* equal to 1 */ 4295 /* equal to 1 */
4250 int count; /* Number of currently slots filled */ 4296 int count; /* Number of currently slots filled */
4251 struct s_x1node *tbl; /* The data stored here */ 4297 struct s_x1node *tbl; /* The data stored here */
4252 struct s_x1node **ht; /* Hash table for lookups */ 4298 struct s_x1node **ht; /* Hash table for lookups */
4253 }; 4299 };
4254 4300
4255 /* There is one instance of this structure for every data element 4301 /* There is one instance of this structure for every data element
4256 ** in an associative array of type "x1". 4302 ** in an associative array of type "x1".
4257 */ 4303 */
4258 typedef struct s_x1node { 4304 typedef struct s_x1node {
4259 char *data; /* The data */ 4305 const char *data; /* The data */
4260 struct s_x1node *next; /* Next entry with the same hash */ 4306 struct s_x1node *next; /* Next entry with the same hash */
4261 struct s_x1node **from; /* Previous link */ 4307 struct s_x1node **from; /* Previous link */
4262 } x1node; 4308 } x1node;
4263 4309
4264 /* There is only one instance of the array, which is the following */ 4310 /* There is only one instance of the array, which is the following */
4265 static struct s_x1 *x1a; 4311 static struct s_x1 *x1a;
4266 4312
4267 /* Allocate a new associative array */ 4313 /* Allocate a new associative array */
4268 void Strsafe_init(){ 4314 void Strsafe_init(){
4269 if( x1a ) return; 4315 if( x1a ) return;
4270 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) ); 4316 x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
4271 if( x1a ){ 4317 if( x1a ){
4272 x1a->size = 1024; 4318 x1a->size = 1024;
4273 x1a->count = 0; 4319 x1a->count = 0;
4274 x1a->tbl = (x1node*)malloc( 4320 x1a->tbl = (x1node*)malloc(
4275 (sizeof(x1node) + sizeof(x1node*))*1024 ); 4321 (sizeof(x1node) + sizeof(x1node*))*1024 );
4276 if( x1a->tbl==0 ){ 4322 if( x1a->tbl==0 ){
4277 free(x1a); 4323 free(x1a);
4278 x1a = 0; 4324 x1a = 0;
4279 }else{ 4325 }else{
4280 int i; 4326 int i;
4281 x1a->ht = (x1node**)&(x1a->tbl[1024]); 4327 x1a->ht = (x1node**)&(x1a->tbl[1024]);
4282 for(i=0; i<1024; i++) x1a->ht[i] = 0; 4328 for(i=0; i<1024; i++) x1a->ht[i] = 0;
4283 } 4329 }
4284 } 4330 }
4285 } 4331 }
4286 /* Insert a new record into the array. Return TRUE if successful. 4332 /* Insert a new record into the array. Return TRUE if successful.
4287 ** Prior data with the same key is NOT overwritten */ 4333 ** Prior data with the same key is NOT overwritten */
4288 int Strsafe_insert(data) 4334 int Strsafe_insert(const char *data)
4289 char *data;
4290 { 4335 {
4291 x1node *np; 4336 x1node *np;
4292 int h; 4337 int h;
4293 int ph; 4338 int ph;
4294 4339
4295 if( x1a==0 ) return 0; 4340 if( x1a==0 ) return 0;
4296 ph = strhash(data); 4341 ph = strhash(data);
4297 h = ph & (x1a->size-1); 4342 h = ph & (x1a->size-1);
4298 np = x1a->ht[h]; 4343 np = x1a->ht[h];
4299 while( np ){ 4344 while( np ){
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
4335 np->data = data; 4380 np->data = data;
4336 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next); 4381 if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
4337 np->next = x1a->ht[h]; 4382 np->next = x1a->ht[h];
4338 x1a->ht[h] = np; 4383 x1a->ht[h] = np;
4339 np->from = &(x1a->ht[h]); 4384 np->from = &(x1a->ht[h]);
4340 return 1; 4385 return 1;
4341 } 4386 }
4342 4387
4343 /* Return a pointer to data assigned to the given key. Return NULL 4388 /* Return a pointer to data assigned to the given key. Return NULL
4344 ** if no such key. */ 4389 ** if no such key. */
4345 char *Strsafe_find(key) 4390 const char *Strsafe_find(const char *key)
4346 char *key;
4347 { 4391 {
4348 int h; 4392 int h;
4349 x1node *np; 4393 x1node *np;
4350 4394
4351 if( x1a==0 ) return 0; 4395 if( x1a==0 ) return 0;
4352 h = strhash(key) & (x1a->size-1); 4396 h = strhash(key) & (x1a->size-1);
4353 np = x1a->ht[h]; 4397 np = x1a->ht[h];
4354 while( np ){ 4398 while( np ){
4355 if( strcmp(np->data,key)==0 ) break; 4399 if( strcmp(np->data,key)==0 ) break;
4356 np = np->next; 4400 np = np->next;
4357 } 4401 }
4358 return np ? np->data : 0; 4402 return np ? np->data : 0;
4359 } 4403 }
4360 4404
4361 /* Return a pointer to the (terminal or nonterminal) symbol "x". 4405 /* Return a pointer to the (terminal or nonterminal) symbol "x".
4362 ** Create a new symbol if this is the first time "x" has been seen. 4406 ** Create a new symbol if this is the first time "x" has been seen.
4363 */ 4407 */
4364 struct symbol *Symbol_new(x) 4408 struct symbol *Symbol_new(const char *x)
4365 char *x;
4366 { 4409 {
4367 struct symbol *sp; 4410 struct symbol *sp;
4368 4411
4369 sp = Symbol_find(x); 4412 sp = Symbol_find(x);
4370 if( sp==0 ){ 4413 if( sp==0 ){
4371 sp = (struct symbol *)calloc(1, sizeof(struct symbol) ); 4414 sp = (struct symbol *)calloc(1, sizeof(struct symbol) );
4372 MemoryCheck(sp); 4415 MemoryCheck(sp);
4373 sp->name = Strsafe(x); 4416 sp->name = Strsafe(x);
4374 sp->type = isupper(*x) ? TERMINAL : NONTERMINAL; 4417 sp->type = isupper(*x) ? TERMINAL : NONTERMINAL;
4375 sp->rule = 0; 4418 sp->rule = 0;
(...skipping 15 matching lines...) Expand all
4391 /* Compare two symbols for working purposes 4434 /* Compare two symbols for working purposes
4392 ** 4435 **
4393 ** Symbols that begin with upper case letters (terminals or tokens) 4436 ** Symbols that begin with upper case letters (terminals or tokens)
4394 ** must sort before symbols that begin with lower case letters 4437 ** must sort before symbols that begin with lower case letters
4395 ** (non-terminals). Other than that, the order does not matter. 4438 ** (non-terminals). Other than that, the order does not matter.
4396 ** 4439 **
4397 ** We find experimentally that leaving the symbols in their original 4440 ** We find experimentally that leaving the symbols in their original
4398 ** order (the order they appeared in the grammar file) gives the 4441 ** order (the order they appeared in the grammar file) gives the
4399 ** smallest parser tables in SQLite. 4442 ** smallest parser tables in SQLite.
4400 */ 4443 */
4401 int Symbolcmpp(struct symbol **a, struct symbol **b){ 4444 int Symbolcmpp(const void *_a, const void *_b)
4445 {
4446 const struct symbol **a = (const struct symbol **) _a;
4447 const struct symbol **b = (const struct symbol **) _b;
4402 int i1 = (**a).index + 10000000*((**a).name[0]>'Z'); 4448 int i1 = (**a).index + 10000000*((**a).name[0]>'Z');
4403 int i2 = (**b).index + 10000000*((**b).name[0]>'Z'); 4449 int i2 = (**b).index + 10000000*((**b).name[0]>'Z');
4450 assert( i1!=i2 || strcmp((**a).name,(**b).name)==0 );
4404 return i1-i2; 4451 return i1-i2;
4405 } 4452 }
4406 4453
4407 /* There is one instance of the following structure for each 4454 /* There is one instance of the following structure for each
4408 ** associative array of type "x2". 4455 ** associative array of type "x2".
4409 */ 4456 */
4410 struct s_x2 { 4457 struct s_x2 {
4411 int size; /* The number of available slots. */ 4458 int size; /* The number of available slots. */
4412 /* Must be a power of 2 greater than or */ 4459 /* Must be a power of 2 greater than or */
4413 /* equal to 1 */ 4460 /* equal to 1 */
4414 int count; /* Number of currently slots filled */ 4461 int count; /* Number of currently slots filled */
4415 struct s_x2node *tbl; /* The data stored here */ 4462 struct s_x2node *tbl; /* The data stored here */
4416 struct s_x2node **ht; /* Hash table for lookups */ 4463 struct s_x2node **ht; /* Hash table for lookups */
4417 }; 4464 };
4418 4465
4419 /* There is one instance of this structure for every data element 4466 /* There is one instance of this structure for every data element
4420 ** in an associative array of type "x2". 4467 ** in an associative array of type "x2".
4421 */ 4468 */
4422 typedef struct s_x2node { 4469 typedef struct s_x2node {
4423 struct symbol *data; /* The data */ 4470 struct symbol *data; /* The data */
4424 char *key; /* The key */ 4471 const char *key; /* The key */
4425 struct s_x2node *next; /* Next entry with the same hash */ 4472 struct s_x2node *next; /* Next entry with the same hash */
4426 struct s_x2node **from; /* Previous link */ 4473 struct s_x2node **from; /* Previous link */
4427 } x2node; 4474 } x2node;
4428 4475
4429 /* There is only one instance of the array, which is the following */ 4476 /* There is only one instance of the array, which is the following */
4430 static struct s_x2 *x2a; 4477 static struct s_x2 *x2a;
4431 4478
4432 /* Allocate a new associative array */ 4479 /* Allocate a new associative array */
4433 void Symbol_init(){ 4480 void Symbol_init(){
4434 if( x2a ) return; 4481 if( x2a ) return;
4435 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) ); 4482 x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
4436 if( x2a ){ 4483 if( x2a ){
4437 x2a->size = 128; 4484 x2a->size = 128;
4438 x2a->count = 0; 4485 x2a->count = 0;
4439 x2a->tbl = (x2node*)malloc( 4486 x2a->tbl = (x2node*)malloc(
4440 (sizeof(x2node) + sizeof(x2node*))*128 ); 4487 (sizeof(x2node) + sizeof(x2node*))*128 );
4441 if( x2a->tbl==0 ){ 4488 if( x2a->tbl==0 ){
4442 free(x2a); 4489 free(x2a);
4443 x2a = 0; 4490 x2a = 0;
4444 }else{ 4491 }else{
4445 int i; 4492 int i;
4446 x2a->ht = (x2node**)&(x2a->tbl[128]); 4493 x2a->ht = (x2node**)&(x2a->tbl[128]);
4447 for(i=0; i<128; i++) x2a->ht[i] = 0; 4494 for(i=0; i<128; i++) x2a->ht[i] = 0;
4448 } 4495 }
4449 } 4496 }
4450 } 4497 }
4451 /* Insert a new record into the array. Return TRUE if successful. 4498 /* Insert a new record into the array. Return TRUE if successful.
4452 ** Prior data with the same key is NOT overwritten */ 4499 ** Prior data with the same key is NOT overwritten */
4453 int Symbol_insert(data,key) 4500 int Symbol_insert(struct symbol *data, const char *key)
4454 struct symbol *data;
4455 char *key;
4456 { 4501 {
4457 x2node *np; 4502 x2node *np;
4458 int h; 4503 int h;
4459 int ph; 4504 int ph;
4460 4505
4461 if( x2a==0 ) return 0; 4506 if( x2a==0 ) return 0;
4462 ph = strhash(key); 4507 ph = strhash(key);
4463 h = ph & (x2a->size-1); 4508 h = ph & (x2a->size-1);
4464 np = x2a->ht[h]; 4509 np = x2a->ht[h];
4465 while( np ){ 4510 while( np ){
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
4503 np->data = data; 4548 np->data = data;
4504 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next); 4549 if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
4505 np->next = x2a->ht[h]; 4550 np->next = x2a->ht[h];
4506 x2a->ht[h] = np; 4551 x2a->ht[h] = np;
4507 np->from = &(x2a->ht[h]); 4552 np->from = &(x2a->ht[h]);
4508 return 1; 4553 return 1;
4509 } 4554 }
4510 4555
4511 /* Return a pointer to data assigned to the given key. Return NULL 4556 /* Return a pointer to data assigned to the given key. Return NULL
4512 ** if no such key. */ 4557 ** if no such key. */
4513 struct symbol *Symbol_find(key) 4558 struct symbol *Symbol_find(const char *key)
4514 char *key;
4515 { 4559 {
4516 int h; 4560 int h;
4517 x2node *np; 4561 x2node *np;
4518 4562
4519 if( x2a==0 ) return 0; 4563 if( x2a==0 ) return 0;
4520 h = strhash(key) & (x2a->size-1); 4564 h = strhash(key) & (x2a->size-1);
4521 np = x2a->ht[h]; 4565 np = x2a->ht[h];
4522 while( np ){ 4566 while( np ){
4523 if( strcmp(np->key,key)==0 ) break; 4567 if( strcmp(np->key,key)==0 ) break;
4524 np = np->next; 4568 np = np->next;
4525 } 4569 }
4526 return np ? np->data : 0; 4570 return np ? np->data : 0;
4527 } 4571 }
4528 4572
4529 /* Return the n-th data. Return NULL if n is out of range. */ 4573 /* Return the n-th data. Return NULL if n is out of range. */
4530 struct symbol *Symbol_Nth(n) 4574 struct symbol *Symbol_Nth(int n)
4531 int n;
4532 { 4575 {
4533 struct symbol *data; 4576 struct symbol *data;
4534 if( x2a && n>0 && n<=x2a->count ){ 4577 if( x2a && n>0 && n<=x2a->count ){
4535 data = x2a->tbl[n-1].data; 4578 data = x2a->tbl[n-1].data;
4536 }else{ 4579 }else{
4537 data = 0; 4580 data = 0;
4538 } 4581 }
4539 return data; 4582 return data;
4540 } 4583 }
4541 4584
(...skipping 13 matching lines...) Expand all
4555 if( x2a==0 ) return 0; 4598 if( x2a==0 ) return 0;
4556 size = x2a->count; 4599 size = x2a->count;
4557 array = (struct symbol **)calloc(size, sizeof(struct symbol *)); 4600 array = (struct symbol **)calloc(size, sizeof(struct symbol *));
4558 if( array ){ 4601 if( array ){
4559 for(i=0; i<size; i++) array[i] = x2a->tbl[i].data; 4602 for(i=0; i<size; i++) array[i] = x2a->tbl[i].data;
4560 } 4603 }
4561 return array; 4604 return array;
4562 } 4605 }
4563 4606
4564 /* Compare two configurations */ 4607 /* Compare two configurations */
4565 int Configcmp(a,b) 4608 int Configcmp(const char *_a,const char *_b)
4566 struct config *a;
4567 struct config *b;
4568 { 4609 {
4610 const struct config *a = (struct config *) _a;
4611 const struct config *b = (struct config *) _b;
4569 int x; 4612 int x;
4570 x = a->rp->index - b->rp->index; 4613 x = a->rp->index - b->rp->index;
4571 if( x==0 ) x = a->dot - b->dot; 4614 if( x==0 ) x = a->dot - b->dot;
4572 return x; 4615 return x;
4573 } 4616 }
4574 4617
4575 /* Compare two states */ 4618 /* Compare two states */
4576 PRIVATE int statecmp(a,b) 4619 PRIVATE int statecmp(struct config *a, struct config *b)
4577 struct config *a;
4578 struct config *b;
4579 { 4620 {
4580 int rc; 4621 int rc;
4581 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){ 4622 for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
4582 rc = a->rp->index - b->rp->index; 4623 rc = a->rp->index - b->rp->index;
4583 if( rc==0 ) rc = a->dot - b->dot; 4624 if( rc==0 ) rc = a->dot - b->dot;
4584 } 4625 }
4585 if( rc==0 ){ 4626 if( rc==0 ){
4586 if( a ) rc = 1; 4627 if( a ) rc = 1;
4587 if( b ) rc = -1; 4628 if( b ) rc = -1;
4588 } 4629 }
4589 return rc; 4630 return rc;
4590 } 4631 }
4591 4632
4592 /* Hash a state */ 4633 /* Hash a state */
4593 PRIVATE int statehash(a) 4634 PRIVATE int statehash(struct config *a)
4594 struct config *a;
4595 { 4635 {
4596 int h=0; 4636 int h=0;
4597 while( a ){ 4637 while( a ){
4598 h = h*571 + a->rp->index*37 + a->dot; 4638 h = h*571 + a->rp->index*37 + a->dot;
4599 a = a->bp; 4639 a = a->bp;
4600 } 4640 }
4601 return h; 4641 return h;
4602 } 4642 }
4603 4643
4604 /* Allocate a new state structure */ 4644 /* Allocate a new state structure */
4605 struct state *State_new() 4645 struct state *State_new()
4606 { 4646 {
4607 struct state *new; 4647 struct state *newstate;
4608 new = (struct state *)calloc(1, sizeof(struct state) ); 4648 newstate = (struct state *)calloc(1, sizeof(struct state) );
4609 MemoryCheck(new); 4649 MemoryCheck(newstate);
4610 return new; 4650 return newstate;
4611 } 4651 }
4612 4652
4613 /* There is one instance of the following structure for each 4653 /* There is one instance of the following structure for each
4614 ** associative array of type "x3". 4654 ** associative array of type "x3".
4615 */ 4655 */
4616 struct s_x3 { 4656 struct s_x3 {
4617 int size; /* The number of available slots. */ 4657 int size; /* The number of available slots. */
4618 /* Must be a power of 2 greater than or */ 4658 /* Must be a power of 2 greater than or */
4619 /* equal to 1 */ 4659 /* equal to 1 */
4620 int count; /* Number of currently slots filled */ 4660 int count; /* Number of currently slots filled */
(...skipping 28 matching lines...) Expand all
4649 x3a = 0; 4689 x3a = 0;
4650 }else{ 4690 }else{
4651 int i; 4691 int i;
4652 x3a->ht = (x3node**)&(x3a->tbl[128]); 4692 x3a->ht = (x3node**)&(x3a->tbl[128]);
4653 for(i=0; i<128; i++) x3a->ht[i] = 0; 4693 for(i=0; i<128; i++) x3a->ht[i] = 0;
4654 } 4694 }
4655 } 4695 }
4656 } 4696 }
4657 /* Insert a new record into the array. Return TRUE if successful. 4697 /* Insert a new record into the array. Return TRUE if successful.
4658 ** Prior data with the same key is NOT overwritten */ 4698 ** Prior data with the same key is NOT overwritten */
4659 int State_insert(data,key) 4699 int State_insert(struct state *data, struct config *key)
4660 struct state *data;
4661 struct config *key;
4662 { 4700 {
4663 x3node *np; 4701 x3node *np;
4664 int h; 4702 int h;
4665 int ph; 4703 int ph;
4666 4704
4667 if( x3a==0 ) return 0; 4705 if( x3a==0 ) return 0;
4668 ph = statehash(key); 4706 ph = statehash(key);
4669 h = ph & (x3a->size-1); 4707 h = ph & (x3a->size-1);
4670 np = x3a->ht[h]; 4708 np = x3a->ht[h];
4671 while( np ){ 4709 while( np ){
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
4709 np->data = data; 4747 np->data = data;
4710 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next); 4748 if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
4711 np->next = x3a->ht[h]; 4749 np->next = x3a->ht[h];
4712 x3a->ht[h] = np; 4750 x3a->ht[h] = np;
4713 np->from = &(x3a->ht[h]); 4751 np->from = &(x3a->ht[h]);
4714 return 1; 4752 return 1;
4715 } 4753 }
4716 4754
4717 /* Return a pointer to data assigned to the given key. Return NULL 4755 /* Return a pointer to data assigned to the given key. Return NULL
4718 ** if no such key. */ 4756 ** if no such key. */
4719 struct state *State_find(key) 4757 struct state *State_find(struct config *key)
4720 struct config *key;
4721 { 4758 {
4722 int h; 4759 int h;
4723 x3node *np; 4760 x3node *np;
4724 4761
4725 if( x3a==0 ) return 0; 4762 if( x3a==0 ) return 0;
4726 h = statehash(key) & (x3a->size-1); 4763 h = statehash(key) & (x3a->size-1);
4727 np = x3a->ht[h]; 4764 np = x3a->ht[h];
4728 while( np ){ 4765 while( np ){
4729 if( statecmp(np->key,key)==0 ) break; 4766 if( statecmp(np->key,key)==0 ) break;
4730 np = np->next; 4767 np = np->next;
(...skipping 11 matching lines...) Expand all
4742 if( x3a==0 ) return 0; 4779 if( x3a==0 ) return 0;
4743 size = x3a->count; 4780 size = x3a->count;
4744 array = (struct state **)malloc( sizeof(struct state *)*size ); 4781 array = (struct state **)malloc( sizeof(struct state *)*size );
4745 if( array ){ 4782 if( array ){
4746 for(i=0; i<size; i++) array[i] = x3a->tbl[i].data; 4783 for(i=0; i<size; i++) array[i] = x3a->tbl[i].data;
4747 } 4784 }
4748 return array; 4785 return array;
4749 } 4786 }
4750 4787
4751 /* Hash a configuration */ 4788 /* Hash a configuration */
4752 PRIVATE int confighash(a) 4789 PRIVATE int confighash(struct config *a)
4753 struct config *a;
4754 { 4790 {
4755 int h=0; 4791 int h=0;
4756 h = h*571 + a->rp->index*37 + a->dot; 4792 h = h*571 + a->rp->index*37 + a->dot;
4757 return h; 4793 return h;
4758 } 4794 }
4759 4795
4760 /* There is one instance of the following structure for each 4796 /* There is one instance of the following structure for each
4761 ** associative array of type "x4". 4797 ** associative array of type "x4".
4762 */ 4798 */
4763 struct s_x4 { 4799 struct s_x4 {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
4795 x4a = 0; 4831 x4a = 0;
4796 }else{ 4832 }else{
4797 int i; 4833 int i;
4798 x4a->ht = (x4node**)&(x4a->tbl[64]); 4834 x4a->ht = (x4node**)&(x4a->tbl[64]);
4799 for(i=0; i<64; i++) x4a->ht[i] = 0; 4835 for(i=0; i<64; i++) x4a->ht[i] = 0;
4800 } 4836 }
4801 } 4837 }
4802 } 4838 }
4803 /* Insert a new record into the array. Return TRUE if successful. 4839 /* Insert a new record into the array. Return TRUE if successful.
4804 ** Prior data with the same key is NOT overwritten */ 4840 ** Prior data with the same key is NOT overwritten */
4805 int Configtable_insert(data) 4841 int Configtable_insert(struct config *data)
4806 struct config *data;
4807 { 4842 {
4808 x4node *np; 4843 x4node *np;
4809 int h; 4844 int h;
4810 int ph; 4845 int ph;
4811 4846
4812 if( x4a==0 ) return 0; 4847 if( x4a==0 ) return 0;
4813 ph = confighash(data); 4848 ph = confighash(data);
4814 h = ph & (x4a->size-1); 4849 h = ph & (x4a->size-1);
4815 np = x4a->ht[h]; 4850 np = x4a->ht[h];
4816 while( np ){ 4851 while( np ){
4817 if( Configcmp(np->data,data)==0 ){ 4852 if( Configcmp((const char *) np->data,(const char *) data)==0 ){
4818 /* An existing entry with the same key is found. */ 4853 /* An existing entry with the same key is found. */
4819 /* Fail because overwrite is not allows. */ 4854 /* Fail because overwrite is not allows. */
4820 return 0; 4855 return 0;
4821 } 4856 }
4822 np = np->next; 4857 np = np->next;
4823 } 4858 }
4824 if( x4a->count>=x4a->size ){ 4859 if( x4a->count>=x4a->size ){
4825 /* Need to make the hash table bigger */ 4860 /* Need to make the hash table bigger */
4826 int i,size; 4861 int i,size;
4827 struct s_x4 array; 4862 struct s_x4 array;
(...skipping 24 matching lines...) Expand all
4852 np->data = data; 4887 np->data = data;
4853 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next); 4888 if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
4854 np->next = x4a->ht[h]; 4889 np->next = x4a->ht[h];
4855 x4a->ht[h] = np; 4890 x4a->ht[h] = np;
4856 np->from = &(x4a->ht[h]); 4891 np->from = &(x4a->ht[h]);
4857 return 1; 4892 return 1;
4858 } 4893 }
4859 4894
4860 /* Return a pointer to data assigned to the given key. Return NULL 4895 /* Return a pointer to data assigned to the given key. Return NULL
4861 ** if no such key. */ 4896 ** if no such key. */
4862 struct config *Configtable_find(key) 4897 struct config *Configtable_find(struct config *key)
4863 struct config *key;
4864 { 4898 {
4865 int h; 4899 int h;
4866 x4node *np; 4900 x4node *np;
4867 4901
4868 if( x4a==0 ) return 0; 4902 if( x4a==0 ) return 0;
4869 h = confighash(key) & (x4a->size-1); 4903 h = confighash(key) & (x4a->size-1);
4870 np = x4a->ht[h]; 4904 np = x4a->ht[h];
4871 while( np ){ 4905 while( np ){
4872 if( Configcmp(np->data,key)==0 ) break; 4906 if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
4873 np = np->next; 4907 np = np->next;
4874 } 4908 }
4875 return np ? np->data : 0; 4909 return np ? np->data : 0;
4876 } 4910 }
4877 4911
4878 /* Remove all data from the table. Pass each data to the function "f" 4912 /* Remove all data from the table. Pass each data to the function "f"
4879 ** as it is removed. ("f" may be null to avoid this step.) */ 4913 ** as it is removed. ("f" may be null to avoid this step.) */
4880 void Configtable_clear(f) 4914 void Configtable_clear(int(*f)(struct config *))
4881 int(*f)(/* struct config * */);
4882 { 4915 {
4883 int i; 4916 int i;
4884 if( x4a==0 || x4a->count==0 ) return; 4917 if( x4a==0 || x4a->count==0 ) return;
4885 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data); 4918 if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
4886 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0; 4919 for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
4887 x4a->count = 0; 4920 x4a->count = 0;
4888 return; 4921 return;
4889 } 4922 }
OLDNEW
« no previous file with comments | « third_party/sqlite/src/tool/genfkey.test ('k') | third_party/sqlite/src/tool/lempar.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698