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