xref: /illumos-gate/usr/src/lib/libsqlite/tool/lemon.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate ** This file contains all sources (including headers) to the LEMON
37c478bd9Sstevel@tonic-gate ** LALR(1) parser generator.  The sources have been combined into a
47c478bd9Sstevel@tonic-gate ** single file to make it easy to include LEMON in the source tree
57c478bd9Sstevel@tonic-gate ** and Makefile of another program.
67c478bd9Sstevel@tonic-gate **
77c478bd9Sstevel@tonic-gate ** The author of this program disclaims copyright.
87c478bd9Sstevel@tonic-gate */
97c478bd9Sstevel@tonic-gate #include <stdio.h>
107c478bd9Sstevel@tonic-gate #include <stdarg.h>
117c478bd9Sstevel@tonic-gate #include <string.h>
127c478bd9Sstevel@tonic-gate #include <ctype.h>
137c478bd9Sstevel@tonic-gate #include <stdlib.h>
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #ifndef __WIN32__
167c478bd9Sstevel@tonic-gate #   if defined(_WIN32) || defined(WIN32)
177c478bd9Sstevel@tonic-gate #	define __WIN32__
187c478bd9Sstevel@tonic-gate #   endif
197c478bd9Sstevel@tonic-gate #endif
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate /* #define PRIVATE static */
227c478bd9Sstevel@tonic-gate #define PRIVATE
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #ifdef TEST
257c478bd9Sstevel@tonic-gate #define MAXRHS 5       /* Set low to exercise exception code */
267c478bd9Sstevel@tonic-gate #else
277c478bd9Sstevel@tonic-gate #define MAXRHS 1000
287c478bd9Sstevel@tonic-gate #endif
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate char *msort();
317c478bd9Sstevel@tonic-gate extern void *malloc();
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /******** From the file "action.h" *************************************/
347c478bd9Sstevel@tonic-gate struct action *Action_new();
357c478bd9Sstevel@tonic-gate struct action *Action_sort();
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /********* From the file "assert.h" ************************************/
387c478bd9Sstevel@tonic-gate void myassert();
397c478bd9Sstevel@tonic-gate #ifndef NDEBUG
407c478bd9Sstevel@tonic-gate #  define assert(X) if(!(X))myassert(__FILE__,__LINE__)
417c478bd9Sstevel@tonic-gate #else
427c478bd9Sstevel@tonic-gate #  define assert(X)
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /********** From the file "build.h" ************************************/
467c478bd9Sstevel@tonic-gate void FindRulePrecedences();
477c478bd9Sstevel@tonic-gate void FindFirstSets();
487c478bd9Sstevel@tonic-gate void FindStates();
497c478bd9Sstevel@tonic-gate void FindLinks();
507c478bd9Sstevel@tonic-gate void FindFollowSets();
517c478bd9Sstevel@tonic-gate void FindActions();
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /********* From the file "configlist.h" *********************************/
547c478bd9Sstevel@tonic-gate void Configlist_init(/* void */);
557c478bd9Sstevel@tonic-gate struct config *Configlist_add(/* struct rule *, int */);
567c478bd9Sstevel@tonic-gate struct config *Configlist_addbasis(/* struct rule *, int */);
577c478bd9Sstevel@tonic-gate void Configlist_closure(/* void */);
587c478bd9Sstevel@tonic-gate void Configlist_sort(/* void */);
597c478bd9Sstevel@tonic-gate void Configlist_sortbasis(/* void */);
607c478bd9Sstevel@tonic-gate struct config *Configlist_return(/* void */);
617c478bd9Sstevel@tonic-gate struct config *Configlist_basis(/* void */);
627c478bd9Sstevel@tonic-gate void Configlist_eat(/* struct config * */);
637c478bd9Sstevel@tonic-gate void Configlist_reset(/* void */);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /********* From the file "error.h" ***************************************/
667c478bd9Sstevel@tonic-gate void ErrorMsg(const char *, int,const char *, ...);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /****** From the file "option.h" ******************************************/
697c478bd9Sstevel@tonic-gate struct s_options {
707c478bd9Sstevel@tonic-gate   enum { OPT_FLAG=1,  OPT_INT,  OPT_DBL,  OPT_STR,
717c478bd9Sstevel@tonic-gate          OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type;
727c478bd9Sstevel@tonic-gate   char *label;
737c478bd9Sstevel@tonic-gate   char *arg;
747c478bd9Sstevel@tonic-gate   char *message;
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate int    OptInit(/* char**,struct s_options*,FILE* */);
777c478bd9Sstevel@tonic-gate int    OptNArgs(/* void */);
787c478bd9Sstevel@tonic-gate char  *OptArg(/* int */);
797c478bd9Sstevel@tonic-gate void   OptErr(/* int */);
807c478bd9Sstevel@tonic-gate void   OptPrint(/* void */);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /******** From the file "parse.h" *****************************************/
837c478bd9Sstevel@tonic-gate void Parse(/* struct lemon *lemp */);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /********* From the file "plink.h" ***************************************/
867c478bd9Sstevel@tonic-gate struct plink *Plink_new(/* void */);
877c478bd9Sstevel@tonic-gate void Plink_add(/* struct plink **, struct config * */);
887c478bd9Sstevel@tonic-gate void Plink_copy(/* struct plink **, struct plink * */);
897c478bd9Sstevel@tonic-gate void Plink_delete(/* struct plink * */);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /********** From the file "report.h" *************************************/
927c478bd9Sstevel@tonic-gate void Reprint(/* struct lemon * */);
937c478bd9Sstevel@tonic-gate void ReportOutput(/* struct lemon * */);
947c478bd9Sstevel@tonic-gate void ReportTable(/* struct lemon * */);
957c478bd9Sstevel@tonic-gate void ReportHeader(/* struct lemon * */);
967c478bd9Sstevel@tonic-gate void CompressTables(/* struct lemon * */);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /********** From the file "set.h" ****************************************/
997c478bd9Sstevel@tonic-gate void  SetSize(/* int N */);             /* All sets will be of size N */
1007c478bd9Sstevel@tonic-gate char *SetNew(/* void */);               /* A new set for element 0..N */
1017c478bd9Sstevel@tonic-gate void  SetFree(/* char* */);             /* Deallocate a set */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int SetAdd(/* char*,int */);            /* Add element to a set */
1047c478bd9Sstevel@tonic-gate int SetUnion(/* char *A,char *B */);    /* A <- A U B, thru element N */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define SetFind(X,Y) (X[Y])       /* True if Y is in set X */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /********** From the file "struct.h" *************************************/
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate ** Principal data structures for the LEMON parser generator.
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate typedef enum {B_FALSE=0, B_TRUE} Boolean;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /* Symbols (terminals and nonterminals) of the grammar are stored
1167c478bd9Sstevel@tonic-gate ** in the following: */
1177c478bd9Sstevel@tonic-gate struct symbol {
1187c478bd9Sstevel@tonic-gate   char *name;              /* Name of the symbol */
1197c478bd9Sstevel@tonic-gate   int index;               /* Index number for this symbol */
1207c478bd9Sstevel@tonic-gate   enum {
1217c478bd9Sstevel@tonic-gate     TERMINAL,
1227c478bd9Sstevel@tonic-gate     NONTERMINAL
1237c478bd9Sstevel@tonic-gate   } type;                  /* Symbols are all either TERMINALS or NTs */
1247c478bd9Sstevel@tonic-gate   struct rule *rule;       /* Linked list of rules of this (if an NT) */
1257c478bd9Sstevel@tonic-gate   struct symbol *fallback; /* fallback token in case this token doesn't parse */
1267c478bd9Sstevel@tonic-gate   int prec;                /* Precedence if defined (-1 otherwise) */
1277c478bd9Sstevel@tonic-gate   enum e_assoc {
1287c478bd9Sstevel@tonic-gate     LEFT,
1297c478bd9Sstevel@tonic-gate     RIGHT,
1307c478bd9Sstevel@tonic-gate     NONE,
1317c478bd9Sstevel@tonic-gate     UNK
1327c478bd9Sstevel@tonic-gate   } assoc;                 /* Associativity if predecence is defined */
1337c478bd9Sstevel@tonic-gate   char *firstset;          /* First-set for all rules of this symbol */
1347c478bd9Sstevel@tonic-gate   Boolean lambda;          /* True if NT and can generate an empty string */
1357c478bd9Sstevel@tonic-gate   char *destructor;        /* Code which executes whenever this symbol is
1367c478bd9Sstevel@tonic-gate                            ** popped from the stack during error processing */
1377c478bd9Sstevel@tonic-gate   int destructorln;        /* Line number of destructor code */
1387c478bd9Sstevel@tonic-gate   char *datatype;          /* The data type of information held by this
1397c478bd9Sstevel@tonic-gate                            ** object. Only used if type==NONTERMINAL */
1407c478bd9Sstevel@tonic-gate   int dtnum;               /* The data type number.  In the parser, the value
1417c478bd9Sstevel@tonic-gate                            ** stack is a union.  The .yy%d element of this
1427c478bd9Sstevel@tonic-gate                            ** union is the correct data type for this object */
1437c478bd9Sstevel@tonic-gate };
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /* Each production rule in the grammar is stored in the following
1467c478bd9Sstevel@tonic-gate ** structure.  */
1477c478bd9Sstevel@tonic-gate struct rule {
1487c478bd9Sstevel@tonic-gate   struct symbol *lhs;      /* Left-hand side of the rule */
1497c478bd9Sstevel@tonic-gate   char *lhsalias;          /* Alias for the LHS (NULL if none) */
1507c478bd9Sstevel@tonic-gate   int ruleline;            /* Line number for the rule */
1517c478bd9Sstevel@tonic-gate   int nrhs;                /* Number of RHS symbols */
1527c478bd9Sstevel@tonic-gate   struct symbol **rhs;     /* The RHS symbols */
1537c478bd9Sstevel@tonic-gate   char **rhsalias;         /* An alias for each RHS symbol (NULL if none) */
1547c478bd9Sstevel@tonic-gate   int line;                /* Line number at which code begins */
1557c478bd9Sstevel@tonic-gate   char *code;              /* The code executed when this rule is reduced */
1567c478bd9Sstevel@tonic-gate   struct symbol *precsym;  /* Precedence symbol for this rule */
1577c478bd9Sstevel@tonic-gate   int index;               /* An index number for this rule */
1587c478bd9Sstevel@tonic-gate   Boolean canReduce;       /* True if this rule is ever reduced */
1597c478bd9Sstevel@tonic-gate   struct rule *nextlhs;    /* Next rule with the same LHS */
1607c478bd9Sstevel@tonic-gate   struct rule *next;       /* Next rule in the global list */
1617c478bd9Sstevel@tonic-gate };
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /* A configuration is a production rule of the grammar together with
1647c478bd9Sstevel@tonic-gate ** a mark (dot) showing how much of that rule has been processed so far.
1657c478bd9Sstevel@tonic-gate ** Configurations also contain a follow-set which is a list of terminal
1667c478bd9Sstevel@tonic-gate ** symbols which are allowed to immediately follow the end of the rule.
1677c478bd9Sstevel@tonic-gate ** Every configuration is recorded as an instance of the following: */
1687c478bd9Sstevel@tonic-gate struct config {
1697c478bd9Sstevel@tonic-gate   struct rule *rp;         /* The rule upon which the configuration is based */
1707c478bd9Sstevel@tonic-gate   int dot;                 /* The parse point */
1717c478bd9Sstevel@tonic-gate   char *fws;               /* Follow-set for this configuration only */
1727c478bd9Sstevel@tonic-gate   struct plink *fplp;      /* Follow-set forward propagation links */
1737c478bd9Sstevel@tonic-gate   struct plink *bplp;      /* Follow-set backwards propagation links */
1747c478bd9Sstevel@tonic-gate   struct state *stp;       /* Pointer to state which contains this */
1757c478bd9Sstevel@tonic-gate   enum {
1767c478bd9Sstevel@tonic-gate     COMPLETE,              /* The status is used during followset and */
1777c478bd9Sstevel@tonic-gate     INCOMPLETE             /*    shift computations */
1787c478bd9Sstevel@tonic-gate   } status;
1797c478bd9Sstevel@tonic-gate   struct config *next;     /* Next configuration in the state */
1807c478bd9Sstevel@tonic-gate   struct config *bp;       /* The next basis configuration */
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /* Every shift or reduce operation is stored as one of the following */
1847c478bd9Sstevel@tonic-gate struct action {
1857c478bd9Sstevel@tonic-gate   struct symbol *sp;       /* The look-ahead symbol */
1867c478bd9Sstevel@tonic-gate   enum e_action {
1877c478bd9Sstevel@tonic-gate     SHIFT,
1887c478bd9Sstevel@tonic-gate     ACCEPT,
1897c478bd9Sstevel@tonic-gate     REDUCE,
1907c478bd9Sstevel@tonic-gate     ERROR,
1917c478bd9Sstevel@tonic-gate     CONFLICT,                /* Was a reduce, but part of a conflict */
1927c478bd9Sstevel@tonic-gate     SH_RESOLVED,             /* Was a shift.  Precedence resolved conflict */
1937c478bd9Sstevel@tonic-gate     RD_RESOLVED,             /* Was reduce.  Precedence resolved conflict */
1947c478bd9Sstevel@tonic-gate     NOT_USED                 /* Deleted by compression */
1957c478bd9Sstevel@tonic-gate   } type;
1967c478bd9Sstevel@tonic-gate   union {
1977c478bd9Sstevel@tonic-gate     struct state *stp;     /* The new state, if a shift */
1987c478bd9Sstevel@tonic-gate     struct rule *rp;       /* The rule, if a reduce */
1997c478bd9Sstevel@tonic-gate   } x;
2007c478bd9Sstevel@tonic-gate   struct action *next;     /* Next action for this state */
2017c478bd9Sstevel@tonic-gate   struct action *collide;  /* Next action with the same hash */
2027c478bd9Sstevel@tonic-gate };
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /* Each state of the generated parser's finite state machine
2057c478bd9Sstevel@tonic-gate ** is encoded as an instance of the following structure. */
2067c478bd9Sstevel@tonic-gate struct state {
2077c478bd9Sstevel@tonic-gate   struct config *bp;       /* The basis configurations for this state */
2087c478bd9Sstevel@tonic-gate   struct config *cfp;      /* All configurations in this set */
2097c478bd9Sstevel@tonic-gate   int index;               /* Sequencial number for this state */
2107c478bd9Sstevel@tonic-gate   struct action *ap;       /* Array of actions for this state */
2117c478bd9Sstevel@tonic-gate   int nTknAct, nNtAct;     /* Number of actions on terminals and nonterminals */
2127c478bd9Sstevel@tonic-gate   int iTknOfst, iNtOfst;   /* yy_action[] offset for terminals and nonterms */
2137c478bd9Sstevel@tonic-gate   int iDflt;               /* Default action */
2147c478bd9Sstevel@tonic-gate };
2157c478bd9Sstevel@tonic-gate #define NO_OFFSET (-2147483647)
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /* A followset propagation link indicates that the contents of one
2187c478bd9Sstevel@tonic-gate ** configuration followset should be propagated to another whenever
2197c478bd9Sstevel@tonic-gate ** the first changes. */
2207c478bd9Sstevel@tonic-gate struct plink {
2217c478bd9Sstevel@tonic-gate   struct config *cfp;      /* The configuration to which linked */
2227c478bd9Sstevel@tonic-gate   struct plink *next;      /* The next propagate link */
2237c478bd9Sstevel@tonic-gate };
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* The state vector for the entire parser generator is recorded as
2267c478bd9Sstevel@tonic-gate ** follows.  (LEMON uses no global variables and makes little use of
2277c478bd9Sstevel@tonic-gate ** static variables.  Fields in the following structure can be thought
2287c478bd9Sstevel@tonic-gate ** of as begin global variables in the program.) */
2297c478bd9Sstevel@tonic-gate struct lemon {
2307c478bd9Sstevel@tonic-gate   struct state **sorted;   /* Table of states sorted by state number */
2317c478bd9Sstevel@tonic-gate   struct rule *rule;       /* List of all rules */
2327c478bd9Sstevel@tonic-gate   int nstate;              /* Number of states */
2337c478bd9Sstevel@tonic-gate   int nrule;               /* Number of rules */
2347c478bd9Sstevel@tonic-gate   int nsymbol;             /* Number of terminal and nonterminal symbols */
2357c478bd9Sstevel@tonic-gate   int nterminal;           /* Number of terminal symbols */
2367c478bd9Sstevel@tonic-gate   struct symbol **symbols; /* Sorted array of pointers to symbols */
2377c478bd9Sstevel@tonic-gate   int errorcnt;            /* Number of errors */
2387c478bd9Sstevel@tonic-gate   struct symbol *errsym;   /* The error symbol */
2397c478bd9Sstevel@tonic-gate   char *name;              /* Name of the generated parser */
2407c478bd9Sstevel@tonic-gate   char *arg;               /* Declaration of the 3th argument to parser */
2417c478bd9Sstevel@tonic-gate   char *tokentype;         /* Type of terminal symbols in the parser stack */
2427c478bd9Sstevel@tonic-gate   char *vartype;           /* The default type of non-terminal symbols */
2437c478bd9Sstevel@tonic-gate   char *start;             /* Name of the start symbol for the grammar */
2447c478bd9Sstevel@tonic-gate   char *stacksize;         /* Size of the parser stack */
2457c478bd9Sstevel@tonic-gate   char *include;           /* Code to put at the start of the C file */
2467c478bd9Sstevel@tonic-gate   int  includeln;          /* Line number for start of include code */
2477c478bd9Sstevel@tonic-gate   char *error;             /* Code to execute when an error is seen */
2487c478bd9Sstevel@tonic-gate   int  errorln;            /* Line number for start of error code */
2497c478bd9Sstevel@tonic-gate   char *overflow;          /* Code to execute on a stack overflow */
2507c478bd9Sstevel@tonic-gate   int  overflowln;         /* Line number for start of overflow code */
2517c478bd9Sstevel@tonic-gate   char *failure;           /* Code to execute on parser failure */
2527c478bd9Sstevel@tonic-gate   int  failureln;          /* Line number for start of failure code */
2537c478bd9Sstevel@tonic-gate   char *accept;            /* Code to execute when the parser excepts */
2547c478bd9Sstevel@tonic-gate   int  acceptln;           /* Line number for the start of accept code */
2557c478bd9Sstevel@tonic-gate   char *extracode;         /* Code appended to the generated file */
2567c478bd9Sstevel@tonic-gate   int  extracodeln;        /* Line number for the start of the extra code */
2577c478bd9Sstevel@tonic-gate   char *tokendest;         /* Code to execute to destroy token data */
2587c478bd9Sstevel@tonic-gate   int  tokendestln;        /* Line number for token destroyer code */
2597c478bd9Sstevel@tonic-gate   char *vardest;           /* Code for the default non-terminal destructor */
2607c478bd9Sstevel@tonic-gate   int  vardestln;          /* Line number for default non-term destructor code*/
2617c478bd9Sstevel@tonic-gate   char *filename;          /* Name of the input file */
2627c478bd9Sstevel@tonic-gate   char *outname;           /* Name of the current output file */
2637c478bd9Sstevel@tonic-gate   char *tokenprefix;       /* A prefix added to token names in the .h file */
2647c478bd9Sstevel@tonic-gate   int nconflict;           /* Number of parsing conflicts */
2657c478bd9Sstevel@tonic-gate   int tablesize;           /* Size of the parse tables */
2667c478bd9Sstevel@tonic-gate   int basisflag;           /* Print only basis configurations */
2677c478bd9Sstevel@tonic-gate   int has_fallback;        /* True if any %fallback is seen in the grammer */
2687c478bd9Sstevel@tonic-gate   char *argv0;             /* Name of the program */
2697c478bd9Sstevel@tonic-gate };
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate #define MemoryCheck(X) if((X)==0){ \
2727c478bd9Sstevel@tonic-gate   extern void memory_error(); \
2737c478bd9Sstevel@tonic-gate   memory_error(); \
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /**************** From the file "table.h" *********************************/
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate ** All code in this file has been automatically generated
2797c478bd9Sstevel@tonic-gate ** from a specification in the file
2807c478bd9Sstevel@tonic-gate **              "table.q"
2817c478bd9Sstevel@tonic-gate ** by the associative array code building program "aagen".
2827c478bd9Sstevel@tonic-gate ** Do not edit this file!  Instead, edit the specification
2837c478bd9Sstevel@tonic-gate ** file, then rerun aagen.
2847c478bd9Sstevel@tonic-gate */
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate ** Code for processing tables in the LEMON parser generator.
2877c478bd9Sstevel@tonic-gate */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /* Routines for handling a strings */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate char *Strsafe();
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate void Strsafe_init(/* void */);
2947c478bd9Sstevel@tonic-gate int Strsafe_insert(/* char * */);
2957c478bd9Sstevel@tonic-gate char *Strsafe_find(/* char * */);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /* Routines for handling symbols of the grammar */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate struct symbol *Symbol_new();
3007c478bd9Sstevel@tonic-gate int Symbolcmpp(/* struct symbol **, struct symbol ** */);
3017c478bd9Sstevel@tonic-gate void Symbol_init(/* void */);
3027c478bd9Sstevel@tonic-gate int Symbol_insert(/* struct symbol *, char * */);
3037c478bd9Sstevel@tonic-gate struct symbol *Symbol_find(/* char * */);
3047c478bd9Sstevel@tonic-gate struct symbol *Symbol_Nth(/* int */);
3057c478bd9Sstevel@tonic-gate int Symbol_count(/*  */);
3067c478bd9Sstevel@tonic-gate struct symbol **Symbol_arrayof(/*  */);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /* Routines to manage the state table */
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate int Configcmp(/* struct config *, struct config * */);
3117c478bd9Sstevel@tonic-gate struct state *State_new();
3127c478bd9Sstevel@tonic-gate void State_init(/* void */);
3137c478bd9Sstevel@tonic-gate int State_insert(/* struct state *, struct config * */);
3147c478bd9Sstevel@tonic-gate struct state *State_find(/* struct config * */);
3157c478bd9Sstevel@tonic-gate struct state **State_arrayof(/*  */);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /* Routines used for efficiency in Configlist_add */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate void Configtable_init(/* void */);
3207c478bd9Sstevel@tonic-gate int Configtable_insert(/* struct config * */);
3217c478bd9Sstevel@tonic-gate struct config *Configtable_find(/* struct config * */);
3227c478bd9Sstevel@tonic-gate void Configtable_clear(/* int(*)(struct config *) */);
3237c478bd9Sstevel@tonic-gate /****************** From the file "action.c" *******************************/
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate ** Routines processing parser actions in the LEMON parser generator.
3267c478bd9Sstevel@tonic-gate */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /* Allocate a new parser action */
Action_new()3297c478bd9Sstevel@tonic-gate struct action *Action_new(){
3307c478bd9Sstevel@tonic-gate   static struct action *freelist = 0;
3317c478bd9Sstevel@tonic-gate   struct action *new;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate   if( freelist==0 ){
3347c478bd9Sstevel@tonic-gate     int i;
3357c478bd9Sstevel@tonic-gate     int amt = 100;
3367c478bd9Sstevel@tonic-gate     freelist = (struct action *)malloc( sizeof(struct action)*amt );
3377c478bd9Sstevel@tonic-gate     if( freelist==0 ){
3387c478bd9Sstevel@tonic-gate       fprintf(stderr,"Unable to allocate memory for a new parser action.");
3397c478bd9Sstevel@tonic-gate       exit(1);
3407c478bd9Sstevel@tonic-gate     }
3417c478bd9Sstevel@tonic-gate     for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
3427c478bd9Sstevel@tonic-gate     freelist[amt-1].next = 0;
3437c478bd9Sstevel@tonic-gate   }
3447c478bd9Sstevel@tonic-gate   new = freelist;
3457c478bd9Sstevel@tonic-gate   freelist = freelist->next;
3467c478bd9Sstevel@tonic-gate   return new;
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /* Compare two actions */
actioncmp(ap1,ap2)3507c478bd9Sstevel@tonic-gate static int actioncmp(ap1,ap2)
3517c478bd9Sstevel@tonic-gate struct action *ap1;
3527c478bd9Sstevel@tonic-gate struct action *ap2;
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate   int rc;
3557c478bd9Sstevel@tonic-gate   rc = ap1->sp->index - ap2->sp->index;
3567c478bd9Sstevel@tonic-gate   if( rc==0 ) rc = (int)ap1->type - (int)ap2->type;
3577c478bd9Sstevel@tonic-gate   if( rc==0 ){
3587c478bd9Sstevel@tonic-gate     assert( ap1->type==REDUCE || ap1->type==RD_RESOLVED || ap1->type==CONFLICT);
3597c478bd9Sstevel@tonic-gate     assert( ap2->type==REDUCE || ap2->type==RD_RESOLVED || ap2->type==CONFLICT);
3607c478bd9Sstevel@tonic-gate     rc = ap1->x.rp->index - ap2->x.rp->index;
3617c478bd9Sstevel@tonic-gate   }
3627c478bd9Sstevel@tonic-gate   return rc;
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /* Sort parser actions */
Action_sort(ap)3667c478bd9Sstevel@tonic-gate struct action *Action_sort(ap)
3677c478bd9Sstevel@tonic-gate struct action *ap;
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate   ap = (struct action *)msort((char *)ap,(char **)&ap->next,actioncmp);
3707c478bd9Sstevel@tonic-gate   return ap;
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
Action_add(app,type,sp,arg)3737c478bd9Sstevel@tonic-gate void Action_add(app,type,sp,arg)
3747c478bd9Sstevel@tonic-gate struct action **app;
3757c478bd9Sstevel@tonic-gate enum e_action type;
3767c478bd9Sstevel@tonic-gate struct symbol *sp;
3777c478bd9Sstevel@tonic-gate char *arg;
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate   struct action *new;
3807c478bd9Sstevel@tonic-gate   new = Action_new();
3817c478bd9Sstevel@tonic-gate   new->next = *app;
3827c478bd9Sstevel@tonic-gate   *app = new;
3837c478bd9Sstevel@tonic-gate   new->type = type;
3847c478bd9Sstevel@tonic-gate   new->sp = sp;
3857c478bd9Sstevel@tonic-gate   if( type==SHIFT ){
3867c478bd9Sstevel@tonic-gate     new->x.stp = (struct state *)arg;
3877c478bd9Sstevel@tonic-gate   }else{
3887c478bd9Sstevel@tonic-gate     new->x.rp = (struct rule *)arg;
3897c478bd9Sstevel@tonic-gate   }
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate /********************** New code to implement the "acttab" module ***********/
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate ** This module implements routines use to construct the yy_action[] table.
3947c478bd9Sstevel@tonic-gate */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate ** The state of the yy_action table under construction is an instance of
3987c478bd9Sstevel@tonic-gate ** the following structure
3997c478bd9Sstevel@tonic-gate */
4007c478bd9Sstevel@tonic-gate typedef struct acttab acttab;
4017c478bd9Sstevel@tonic-gate struct acttab {
4027c478bd9Sstevel@tonic-gate   int nAction;                 /* Number of used slots in aAction[] */
4037c478bd9Sstevel@tonic-gate   int nActionAlloc;            /* Slots allocated for aAction[] */
4047c478bd9Sstevel@tonic-gate   struct {
4057c478bd9Sstevel@tonic-gate     int lookahead;             /* Value of the lookahead token */
4067c478bd9Sstevel@tonic-gate     int action;                /* Action to take on the given lookahead */
4077c478bd9Sstevel@tonic-gate   } *aAction,                  /* The yy_action[] table under construction */
4087c478bd9Sstevel@tonic-gate     *aLookahead;               /* A single new transaction set */
4097c478bd9Sstevel@tonic-gate   int mnLookahead;             /* Minimum aLookahead[].lookahead */
4107c478bd9Sstevel@tonic-gate   int mnAction;                /* Action associated with mnLookahead */
4117c478bd9Sstevel@tonic-gate   int mxLookahead;             /* Maximum aLookahead[].lookahead */
4127c478bd9Sstevel@tonic-gate   int nLookahead;              /* Used slots in aLookahead[] */
4137c478bd9Sstevel@tonic-gate   int nLookaheadAlloc;         /* Slots allocated in aLookahead[] */
4147c478bd9Sstevel@tonic-gate };
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /* Return the number of entries in the yy_action table */
4177c478bd9Sstevel@tonic-gate #define acttab_size(X) ((X)->nAction)
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /* The value for the N-th entry in yy_action */
4207c478bd9Sstevel@tonic-gate #define acttab_yyaction(X,N)  ((X)->aAction[N].action)
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate /* The value for the N-th entry in yy_lookahead */
4237c478bd9Sstevel@tonic-gate #define acttab_yylookahead(X,N)  ((X)->aAction[N].lookahead)
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /* Free all memory associated with the given acttab */
acttab_free(acttab * p)4267c478bd9Sstevel@tonic-gate void acttab_free(acttab *p){
4277c478bd9Sstevel@tonic-gate   free( p->aAction );
4287c478bd9Sstevel@tonic-gate   free( p->aLookahead );
4297c478bd9Sstevel@tonic-gate   free( p );
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /* Allocate a new acttab structure */
acttab_alloc(void)4337c478bd9Sstevel@tonic-gate acttab *acttab_alloc(void){
4347c478bd9Sstevel@tonic-gate   acttab *p = malloc( sizeof(*p) );
4357c478bd9Sstevel@tonic-gate   if( p==0 ){
4367c478bd9Sstevel@tonic-gate     fprintf(stderr,"Unable to allocate memory for a new acttab.");
4377c478bd9Sstevel@tonic-gate     exit(1);
4387c478bd9Sstevel@tonic-gate   }
4397c478bd9Sstevel@tonic-gate   memset(p, 0, sizeof(*p));
4407c478bd9Sstevel@tonic-gate   return p;
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate /* Add a new action to the current transaction set
4447c478bd9Sstevel@tonic-gate */
acttab_action(acttab * p,int lookahead,int action)4457c478bd9Sstevel@tonic-gate void acttab_action(acttab *p, int lookahead, int action){
4467c478bd9Sstevel@tonic-gate   if( p->nLookahead>=p->nLookaheadAlloc ){
4477c478bd9Sstevel@tonic-gate     p->nLookaheadAlloc += 25;
4487c478bd9Sstevel@tonic-gate     p->aLookahead = realloc( p->aLookahead,
4497c478bd9Sstevel@tonic-gate                              sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
4507c478bd9Sstevel@tonic-gate     if( p->aLookahead==0 ){
4517c478bd9Sstevel@tonic-gate       fprintf(stderr,"malloc failed\n");
4527c478bd9Sstevel@tonic-gate       exit(1);
4537c478bd9Sstevel@tonic-gate     }
4547c478bd9Sstevel@tonic-gate   }
4557c478bd9Sstevel@tonic-gate   if( p->nLookahead==0 ){
4567c478bd9Sstevel@tonic-gate     p->mxLookahead = lookahead;
4577c478bd9Sstevel@tonic-gate     p->mnLookahead = lookahead;
4587c478bd9Sstevel@tonic-gate     p->mnAction = action;
4597c478bd9Sstevel@tonic-gate   }else{
4607c478bd9Sstevel@tonic-gate     if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
4617c478bd9Sstevel@tonic-gate     if( p->mnLookahead>lookahead ){
4627c478bd9Sstevel@tonic-gate       p->mnLookahead = lookahead;
4637c478bd9Sstevel@tonic-gate       p->mnAction = action;
4647c478bd9Sstevel@tonic-gate     }
4657c478bd9Sstevel@tonic-gate   }
4667c478bd9Sstevel@tonic-gate   p->aLookahead[p->nLookahead].lookahead = lookahead;
4677c478bd9Sstevel@tonic-gate   p->aLookahead[p->nLookahead].action = action;
4687c478bd9Sstevel@tonic-gate   p->nLookahead++;
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate ** Add the transaction set built up with prior calls to acttab_action()
4737c478bd9Sstevel@tonic-gate ** into the current action table.  Then reset the transaction set back
4747c478bd9Sstevel@tonic-gate ** to an empty set in preparation for a new round of acttab_action() calls.
4757c478bd9Sstevel@tonic-gate **
4767c478bd9Sstevel@tonic-gate ** Return the offset into the action table of the new transaction.
4777c478bd9Sstevel@tonic-gate */
acttab_insert(acttab * p)4787c478bd9Sstevel@tonic-gate int acttab_insert(acttab *p){
4797c478bd9Sstevel@tonic-gate   int i, j, k, n;
4807c478bd9Sstevel@tonic-gate   assert( p->nLookahead>0 );
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate   /* Make sure we have enough space to hold the expanded action table
4837c478bd9Sstevel@tonic-gate   ** in the worst case.  The worst case occurs if the transaction set
4847c478bd9Sstevel@tonic-gate   ** must be appended to the current action table
4857c478bd9Sstevel@tonic-gate   */
4867c478bd9Sstevel@tonic-gate   n = p->mxLookahead + 1;
4877c478bd9Sstevel@tonic-gate   if( p->nAction + n >= p->nActionAlloc ){
4887c478bd9Sstevel@tonic-gate     int oldAlloc = p->nActionAlloc;
4897c478bd9Sstevel@tonic-gate     p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
4907c478bd9Sstevel@tonic-gate     p->aAction = realloc( p->aAction,
4917c478bd9Sstevel@tonic-gate                           sizeof(p->aAction[0])*p->nActionAlloc);
4927c478bd9Sstevel@tonic-gate     if( p->aAction==0 ){
4937c478bd9Sstevel@tonic-gate       fprintf(stderr,"malloc failed\n");
4947c478bd9Sstevel@tonic-gate       exit(1);
4957c478bd9Sstevel@tonic-gate     }
4967c478bd9Sstevel@tonic-gate     for(i=oldAlloc; i<p->nActionAlloc; i++){
4977c478bd9Sstevel@tonic-gate       p->aAction[i].lookahead = -1;
4987c478bd9Sstevel@tonic-gate       p->aAction[i].action = -1;
4997c478bd9Sstevel@tonic-gate     }
5007c478bd9Sstevel@tonic-gate   }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate   /* Scan the existing action table looking for an offset where we can
5037c478bd9Sstevel@tonic-gate   ** insert the current transaction set.  Fall out of the loop when that
5047c478bd9Sstevel@tonic-gate   ** offset is found.  In the worst case, we fall out of the loop when
5057c478bd9Sstevel@tonic-gate   ** i reaches p->nAction, which means we append the new transaction set.
5067c478bd9Sstevel@tonic-gate   **
5077c478bd9Sstevel@tonic-gate   ** i is the index in p->aAction[] where p->mnLookahead is inserted.
5087c478bd9Sstevel@tonic-gate   */
5097c478bd9Sstevel@tonic-gate   for(i=0; i<p->nAction+p->mnLookahead; i++){
5107c478bd9Sstevel@tonic-gate     if( p->aAction[i].lookahead<0 ){
5117c478bd9Sstevel@tonic-gate       for(j=0; j<p->nLookahead; j++){
5127c478bd9Sstevel@tonic-gate         k = p->aLookahead[j].lookahead - p->mnLookahead + i;
5137c478bd9Sstevel@tonic-gate         if( k<0 ) break;
5147c478bd9Sstevel@tonic-gate         if( p->aAction[k].lookahead>=0 ) break;
5157c478bd9Sstevel@tonic-gate       }
5167c478bd9Sstevel@tonic-gate       if( j<p->nLookahead ) continue;
5177c478bd9Sstevel@tonic-gate       for(j=0; j<p->nAction; j++){
5187c478bd9Sstevel@tonic-gate         if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
5197c478bd9Sstevel@tonic-gate       }
5207c478bd9Sstevel@tonic-gate       if( j==p->nAction ){
5217c478bd9Sstevel@tonic-gate         break;  /* Fits in empty slots */
5227c478bd9Sstevel@tonic-gate       }
5237c478bd9Sstevel@tonic-gate     }else if( p->aAction[i].lookahead==p->mnLookahead ){
5247c478bd9Sstevel@tonic-gate       if( p->aAction[i].action!=p->mnAction ) continue;
5257c478bd9Sstevel@tonic-gate       for(j=0; j<p->nLookahead; j++){
5267c478bd9Sstevel@tonic-gate         k = p->aLookahead[j].lookahead - p->mnLookahead + i;
5277c478bd9Sstevel@tonic-gate         if( k<0 || k>=p->nAction ) break;
5287c478bd9Sstevel@tonic-gate         if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
5297c478bd9Sstevel@tonic-gate         if( p->aLookahead[j].action!=p->aAction[k].action ) break;
5307c478bd9Sstevel@tonic-gate       }
5317c478bd9Sstevel@tonic-gate       if( j<p->nLookahead ) continue;
5327c478bd9Sstevel@tonic-gate       n = 0;
5337c478bd9Sstevel@tonic-gate       for(j=0; j<p->nAction; j++){
5347c478bd9Sstevel@tonic-gate         if( p->aAction[j].lookahead<0 ) continue;
5357c478bd9Sstevel@tonic-gate         if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
5367c478bd9Sstevel@tonic-gate       }
5377c478bd9Sstevel@tonic-gate       if( n==p->nLookahead ){
5387c478bd9Sstevel@tonic-gate         break;  /* Same as a prior transaction set */
5397c478bd9Sstevel@tonic-gate       }
5407c478bd9Sstevel@tonic-gate     }
5417c478bd9Sstevel@tonic-gate   }
5427c478bd9Sstevel@tonic-gate   /* Insert transaction set at index i. */
5437c478bd9Sstevel@tonic-gate   for(j=0; j<p->nLookahead; j++){
5447c478bd9Sstevel@tonic-gate     k = p->aLookahead[j].lookahead - p->mnLookahead + i;
5457c478bd9Sstevel@tonic-gate     p->aAction[k] = p->aLookahead[j];
5467c478bd9Sstevel@tonic-gate     if( k>=p->nAction ) p->nAction = k+1;
5477c478bd9Sstevel@tonic-gate   }
5487c478bd9Sstevel@tonic-gate   p->nLookahead = 0;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate   /* Return the offset that is added to the lookahead in order to get the
5517c478bd9Sstevel@tonic-gate   ** index into yy_action of the action */
5527c478bd9Sstevel@tonic-gate   return i - p->mnLookahead;
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /********************** From the file "assert.c" ****************************/
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate ** A more efficient way of handling assertions.
5587c478bd9Sstevel@tonic-gate */
myassert(file,line)5597c478bd9Sstevel@tonic-gate void myassert(file,line)
5607c478bd9Sstevel@tonic-gate char *file;
5617c478bd9Sstevel@tonic-gate int line;
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate   fprintf(stderr,"Assertion failed on line %d of file \"%s\"\n",line,file);
5647c478bd9Sstevel@tonic-gate   exit(1);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate /********************** From the file "build.c" *****************************/
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate ** Routines to construction the finite state machine for the LEMON
5697c478bd9Sstevel@tonic-gate ** parser generator.
5707c478bd9Sstevel@tonic-gate */
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /* Find a precedence symbol of every rule in the grammar.
573*55fea89dSDan Cross **
5747c478bd9Sstevel@tonic-gate ** Those rules which have a precedence symbol coded in the input
5757c478bd9Sstevel@tonic-gate ** grammar using the "[symbol]" construct will already have the
5767c478bd9Sstevel@tonic-gate ** rp->precsym field filled.  Other rules take as their precedence
5777c478bd9Sstevel@tonic-gate ** symbol the first RHS symbol with a defined precedence.  If there
5787c478bd9Sstevel@tonic-gate ** are not RHS symbols with a defined precedence, the precedence
5797c478bd9Sstevel@tonic-gate ** symbol field is left blank.
5807c478bd9Sstevel@tonic-gate */
FindRulePrecedences(xp)5817c478bd9Sstevel@tonic-gate void FindRulePrecedences(xp)
5827c478bd9Sstevel@tonic-gate struct lemon *xp;
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate   struct rule *rp;
5857c478bd9Sstevel@tonic-gate   for(rp=xp->rule; rp; rp=rp->next){
5867c478bd9Sstevel@tonic-gate     if( rp->precsym==0 ){
5877c478bd9Sstevel@tonic-gate       int i;
5887c478bd9Sstevel@tonic-gate       for(i=0; i<rp->nrhs; i++){
5897c478bd9Sstevel@tonic-gate         if( rp->rhs[i]->prec>=0 ){
5907c478bd9Sstevel@tonic-gate           rp->precsym = rp->rhs[i];
5917c478bd9Sstevel@tonic-gate           break;
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate       }
5947c478bd9Sstevel@tonic-gate     }
5957c478bd9Sstevel@tonic-gate   }
5967c478bd9Sstevel@tonic-gate   return;
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate /* Find all nonterminals which will generate the empty string.
6007c478bd9Sstevel@tonic-gate ** Then go back and compute the first sets of every nonterminal.
6017c478bd9Sstevel@tonic-gate ** The first set is the set of all terminal symbols which can begin
6027c478bd9Sstevel@tonic-gate ** a string generated by that nonterminal.
6037c478bd9Sstevel@tonic-gate */
FindFirstSets(lemp)6047c478bd9Sstevel@tonic-gate void FindFirstSets(lemp)
6057c478bd9Sstevel@tonic-gate struct lemon *lemp;
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate   int i;
6087c478bd9Sstevel@tonic-gate   struct rule *rp;
6097c478bd9Sstevel@tonic-gate   int progress;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nsymbol; i++){
6127c478bd9Sstevel@tonic-gate     lemp->symbols[i]->lambda = B_FALSE;
6137c478bd9Sstevel@tonic-gate   }
6147c478bd9Sstevel@tonic-gate   for(i=lemp->nterminal; i<lemp->nsymbol; i++){
6157c478bd9Sstevel@tonic-gate     lemp->symbols[i]->firstset = SetNew();
6167c478bd9Sstevel@tonic-gate   }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate   /* First compute all lambdas */
6197c478bd9Sstevel@tonic-gate   do{
6207c478bd9Sstevel@tonic-gate     progress = 0;
6217c478bd9Sstevel@tonic-gate     for(rp=lemp->rule; rp; rp=rp->next){
6227c478bd9Sstevel@tonic-gate       if( rp->lhs->lambda ) continue;
6237c478bd9Sstevel@tonic-gate       for(i=0; i<rp->nrhs; i++){
6247c478bd9Sstevel@tonic-gate          if( rp->rhs[i]->lambda==B_FALSE ) break;
6257c478bd9Sstevel@tonic-gate       }
6267c478bd9Sstevel@tonic-gate       if( i==rp->nrhs ){
6277c478bd9Sstevel@tonic-gate         rp->lhs->lambda = B_TRUE;
6287c478bd9Sstevel@tonic-gate         progress = 1;
6297c478bd9Sstevel@tonic-gate       }
6307c478bd9Sstevel@tonic-gate     }
6317c478bd9Sstevel@tonic-gate   }while( progress );
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate   /* Now compute all first sets */
6347c478bd9Sstevel@tonic-gate   do{
6357c478bd9Sstevel@tonic-gate     struct symbol *s1, *s2;
6367c478bd9Sstevel@tonic-gate     progress = 0;
6377c478bd9Sstevel@tonic-gate     for(rp=lemp->rule; rp; rp=rp->next){
6387c478bd9Sstevel@tonic-gate       s1 = rp->lhs;
6397c478bd9Sstevel@tonic-gate       for(i=0; i<rp->nrhs; i++){
6407c478bd9Sstevel@tonic-gate         s2 = rp->rhs[i];
6417c478bd9Sstevel@tonic-gate         if( s2->type==TERMINAL ){
6427c478bd9Sstevel@tonic-gate           progress += SetAdd(s1->firstset,s2->index);
6437c478bd9Sstevel@tonic-gate           break;
6447c478bd9Sstevel@tonic-gate 	}else if( s1==s2 ){
6457c478bd9Sstevel@tonic-gate           if( s1->lambda==B_FALSE ) break;
6467c478bd9Sstevel@tonic-gate 	}else{
6477c478bd9Sstevel@tonic-gate           progress += SetUnion(s1->firstset,s2->firstset);
6487c478bd9Sstevel@tonic-gate           if( s2->lambda==B_FALSE ) break;
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate       }
6517c478bd9Sstevel@tonic-gate     }
6527c478bd9Sstevel@tonic-gate   }while( progress );
6537c478bd9Sstevel@tonic-gate   return;
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate /* Compute all LR(0) states for the grammar.  Links
6577c478bd9Sstevel@tonic-gate ** are added to between some states so that the LR(1) follow sets
6587c478bd9Sstevel@tonic-gate ** can be computed later.
6597c478bd9Sstevel@tonic-gate */
6607c478bd9Sstevel@tonic-gate PRIVATE struct state *getstate(/* struct lemon * */);  /* forward reference */
FindStates(lemp)6617c478bd9Sstevel@tonic-gate void FindStates(lemp)
6627c478bd9Sstevel@tonic-gate struct lemon *lemp;
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate   struct symbol *sp;
6657c478bd9Sstevel@tonic-gate   struct rule *rp;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate   Configlist_init();
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate   /* Find the start symbol */
6707c478bd9Sstevel@tonic-gate   if( lemp->start ){
6717c478bd9Sstevel@tonic-gate     sp = Symbol_find(lemp->start);
6727c478bd9Sstevel@tonic-gate     if( sp==0 ){
6737c478bd9Sstevel@tonic-gate       ErrorMsg(lemp->filename,0,
6747c478bd9Sstevel@tonic-gate "The specified start symbol \"%s\" is not \
6757c478bd9Sstevel@tonic-gate in a nonterminal of the grammar.  \"%s\" will be used as the start \
6767c478bd9Sstevel@tonic-gate symbol instead.",lemp->start,lemp->rule->lhs->name);
6777c478bd9Sstevel@tonic-gate       lemp->errorcnt++;
6787c478bd9Sstevel@tonic-gate       sp = lemp->rule->lhs;
6797c478bd9Sstevel@tonic-gate     }
6807c478bd9Sstevel@tonic-gate   }else{
6817c478bd9Sstevel@tonic-gate     sp = lemp->rule->lhs;
6827c478bd9Sstevel@tonic-gate   }
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate   /* Make sure the start symbol doesn't occur on the right-hand side of
6857c478bd9Sstevel@tonic-gate   ** any rule.  Report an error if it does.  (YACC would generate a new
6867c478bd9Sstevel@tonic-gate   ** start symbol in this case.) */
6877c478bd9Sstevel@tonic-gate   for(rp=lemp->rule; rp; rp=rp->next){
6887c478bd9Sstevel@tonic-gate     int i;
6897c478bd9Sstevel@tonic-gate     for(i=0; i<rp->nrhs; i++){
6907c478bd9Sstevel@tonic-gate       if( rp->rhs[i]==sp ){
6917c478bd9Sstevel@tonic-gate         ErrorMsg(lemp->filename,0,
6927c478bd9Sstevel@tonic-gate "The start symbol \"%s\" occurs on the \
6937c478bd9Sstevel@tonic-gate right-hand side of a rule. This will result in a parser which \
6947c478bd9Sstevel@tonic-gate does not work properly.",sp->name);
6957c478bd9Sstevel@tonic-gate         lemp->errorcnt++;
6967c478bd9Sstevel@tonic-gate       }
6977c478bd9Sstevel@tonic-gate     }
6987c478bd9Sstevel@tonic-gate   }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate   /* The basis configuration set for the first state
7017c478bd9Sstevel@tonic-gate   ** is all rules which have the start symbol as their
7027c478bd9Sstevel@tonic-gate   ** left-hand side */
7037c478bd9Sstevel@tonic-gate   for(rp=sp->rule; rp; rp=rp->nextlhs){
7047c478bd9Sstevel@tonic-gate     struct config *newcfp;
7057c478bd9Sstevel@tonic-gate     newcfp = Configlist_addbasis(rp,0);
7067c478bd9Sstevel@tonic-gate     SetAdd(newcfp->fws,0);
7077c478bd9Sstevel@tonic-gate   }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate   /* Compute the first state.  All other states will be
7107c478bd9Sstevel@tonic-gate   ** computed automatically during the computation of the first one.
7117c478bd9Sstevel@tonic-gate   ** The returned pointer to the first state is not used. */
7127c478bd9Sstevel@tonic-gate   (void)getstate(lemp);
7137c478bd9Sstevel@tonic-gate   return;
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate /* Return a pointer to a state which is described by the configuration
7177c478bd9Sstevel@tonic-gate ** list which has been built from calls to Configlist_add.
7187c478bd9Sstevel@tonic-gate */
7197c478bd9Sstevel@tonic-gate PRIVATE void buildshifts(/* struct lemon *, struct state * */); /* Forwd ref */
getstate(lemp)7207c478bd9Sstevel@tonic-gate PRIVATE struct state *getstate(lemp)
7217c478bd9Sstevel@tonic-gate struct lemon *lemp;
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate   struct config *cfp, *bp;
7247c478bd9Sstevel@tonic-gate   struct state *stp;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate   /* Extract the sorted basis of the new state.  The basis was constructed
7277c478bd9Sstevel@tonic-gate   ** by prior calls to "Configlist_addbasis()". */
7287c478bd9Sstevel@tonic-gate   Configlist_sortbasis();
7297c478bd9Sstevel@tonic-gate   bp = Configlist_basis();
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate   /* Get a state with the same basis */
7327c478bd9Sstevel@tonic-gate   stp = State_find(bp);
7337c478bd9Sstevel@tonic-gate   if( stp ){
7347c478bd9Sstevel@tonic-gate     /* A state with the same basis already exists!  Copy all the follow-set
7357c478bd9Sstevel@tonic-gate     ** propagation links from the state under construction into the
7367c478bd9Sstevel@tonic-gate     ** preexisting state, then return a pointer to the preexisting state */
7377c478bd9Sstevel@tonic-gate     struct config *x, *y;
7387c478bd9Sstevel@tonic-gate     for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
7397c478bd9Sstevel@tonic-gate       Plink_copy(&y->bplp,x->bplp);
7407c478bd9Sstevel@tonic-gate       Plink_delete(x->fplp);
7417c478bd9Sstevel@tonic-gate       x->fplp = x->bplp = 0;
7427c478bd9Sstevel@tonic-gate     }
7437c478bd9Sstevel@tonic-gate     cfp = Configlist_return();
7447c478bd9Sstevel@tonic-gate     Configlist_eat(cfp);
7457c478bd9Sstevel@tonic-gate   }else{
7467c478bd9Sstevel@tonic-gate     /* This really is a new state.  Construct all the details */
7477c478bd9Sstevel@tonic-gate     Configlist_closure(lemp);    /* Compute the configuration closure */
7487c478bd9Sstevel@tonic-gate     Configlist_sort();           /* Sort the configuration closure */
7497c478bd9Sstevel@tonic-gate     cfp = Configlist_return();   /* Get a pointer to the config list */
7507c478bd9Sstevel@tonic-gate     stp = State_new();           /* A new state structure */
7517c478bd9Sstevel@tonic-gate     MemoryCheck(stp);
7527c478bd9Sstevel@tonic-gate     stp->bp = bp;                /* Remember the configuration basis */
7537c478bd9Sstevel@tonic-gate     stp->cfp = cfp;              /* Remember the configuration closure */
7547c478bd9Sstevel@tonic-gate     stp->index = lemp->nstate++; /* Every state gets a sequence number */
7557c478bd9Sstevel@tonic-gate     stp->ap = 0;                 /* No actions, yet. */
7567c478bd9Sstevel@tonic-gate     State_insert(stp,stp->bp);   /* Add to the state table */
7577c478bd9Sstevel@tonic-gate     buildshifts(lemp,stp);       /* Recursively compute successor states */
7587c478bd9Sstevel@tonic-gate   }
7597c478bd9Sstevel@tonic-gate   return stp;
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate /* Construct all successor states to the given state.  A "successor"
7637c478bd9Sstevel@tonic-gate ** state is any state which can be reached by a shift action.
7647c478bd9Sstevel@tonic-gate */
buildshifts(lemp,stp)7657c478bd9Sstevel@tonic-gate PRIVATE void buildshifts(lemp,stp)
7667c478bd9Sstevel@tonic-gate struct lemon *lemp;
7677c478bd9Sstevel@tonic-gate struct state *stp;     /* The state from which successors are computed */
7687c478bd9Sstevel@tonic-gate {
7697c478bd9Sstevel@tonic-gate   struct config *cfp;  /* For looping thru the config closure of "stp" */
7707c478bd9Sstevel@tonic-gate   struct config *bcfp; /* For the inner loop on config closure of "stp" */
7717c478bd9Sstevel@tonic-gate   struct config *new;  /* */
7727c478bd9Sstevel@tonic-gate   struct symbol *sp;   /* Symbol following the dot in configuration "cfp" */
7737c478bd9Sstevel@tonic-gate   struct symbol *bsp;  /* Symbol following the dot in configuration "bcfp" */
7747c478bd9Sstevel@tonic-gate   struct state *newstp; /* A pointer to a successor state */
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate   /* Each configuration becomes complete after it contibutes to a successor
7777c478bd9Sstevel@tonic-gate   ** state.  Initially, all configurations are incomplete */
7787c478bd9Sstevel@tonic-gate   for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate   /* Loop through all configurations of the state "stp" */
7817c478bd9Sstevel@tonic-gate   for(cfp=stp->cfp; cfp; cfp=cfp->next){
7827c478bd9Sstevel@tonic-gate     if( cfp->status==COMPLETE ) continue;    /* Already used by inner loop */
7837c478bd9Sstevel@tonic-gate     if( cfp->dot>=cfp->rp->nrhs ) continue;  /* Can't shift this config */
7847c478bd9Sstevel@tonic-gate     Configlist_reset();                      /* Reset the new config set */
7857c478bd9Sstevel@tonic-gate     sp = cfp->rp->rhs[cfp->dot];             /* Symbol after the dot */
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate     /* For every configuration in the state "stp" which has the symbol "sp"
7887c478bd9Sstevel@tonic-gate     ** following its dot, add the same configuration to the basis set under
7897c478bd9Sstevel@tonic-gate     ** construction but with the dot shifted one symbol to the right. */
7907c478bd9Sstevel@tonic-gate     for(bcfp=cfp; bcfp; bcfp=bcfp->next){
7917c478bd9Sstevel@tonic-gate       if( bcfp->status==COMPLETE ) continue;    /* Already used */
7927c478bd9Sstevel@tonic-gate       if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
7937c478bd9Sstevel@tonic-gate       bsp = bcfp->rp->rhs[bcfp->dot];           /* Get symbol after dot */
7947c478bd9Sstevel@tonic-gate       if( bsp!=sp ) continue;                   /* Must be same as for "cfp" */
7957c478bd9Sstevel@tonic-gate       bcfp->status = COMPLETE;                  /* Mark this config as used */
7967c478bd9Sstevel@tonic-gate       new = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
7977c478bd9Sstevel@tonic-gate       Plink_add(&new->bplp,bcfp);
7987c478bd9Sstevel@tonic-gate     }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate     /* Get a pointer to the state described by the basis configuration set
8017c478bd9Sstevel@tonic-gate     ** constructed in the preceding loop */
8027c478bd9Sstevel@tonic-gate     newstp = getstate(lemp);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate     /* The state "newstp" is reached from the state "stp" by a shift action
8057c478bd9Sstevel@tonic-gate     ** on the symbol "sp" */
8067c478bd9Sstevel@tonic-gate     Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
8077c478bd9Sstevel@tonic-gate   }
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate ** Construct the propagation links
8127c478bd9Sstevel@tonic-gate */
FindLinks(lemp)8137c478bd9Sstevel@tonic-gate void FindLinks(lemp)
8147c478bd9Sstevel@tonic-gate struct lemon *lemp;
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate   int i;
8177c478bd9Sstevel@tonic-gate   struct config *cfp, *other;
8187c478bd9Sstevel@tonic-gate   struct state *stp;
8197c478bd9Sstevel@tonic-gate   struct plink *plp;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate   /* Housekeeping detail:
8227c478bd9Sstevel@tonic-gate   ** Add to every propagate link a pointer back to the state to
8237c478bd9Sstevel@tonic-gate   ** which the link is attached. */
8247c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate; i++){
8257c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
8267c478bd9Sstevel@tonic-gate     for(cfp=stp->cfp; cfp; cfp=cfp->next){
8277c478bd9Sstevel@tonic-gate       cfp->stp = stp;
8287c478bd9Sstevel@tonic-gate     }
8297c478bd9Sstevel@tonic-gate   }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate   /* Convert all backlinks into forward links.  Only the forward
8327c478bd9Sstevel@tonic-gate   ** links are used in the follow-set computation. */
8337c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate; i++){
8347c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
8357c478bd9Sstevel@tonic-gate     for(cfp=stp->cfp; cfp; cfp=cfp->next){
8367c478bd9Sstevel@tonic-gate       for(plp=cfp->bplp; plp; plp=plp->next){
8377c478bd9Sstevel@tonic-gate         other = plp->cfp;
8387c478bd9Sstevel@tonic-gate         Plink_add(&other->fplp,cfp);
8397c478bd9Sstevel@tonic-gate       }
8407c478bd9Sstevel@tonic-gate     }
8417c478bd9Sstevel@tonic-gate   }
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /* Compute all followsets.
8457c478bd9Sstevel@tonic-gate **
8467c478bd9Sstevel@tonic-gate ** A followset is the set of all symbols which can come immediately
8477c478bd9Sstevel@tonic-gate ** after a configuration.
8487c478bd9Sstevel@tonic-gate */
FindFollowSets(lemp)8497c478bd9Sstevel@tonic-gate void FindFollowSets(lemp)
8507c478bd9Sstevel@tonic-gate struct lemon *lemp;
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate   int i;
8537c478bd9Sstevel@tonic-gate   struct config *cfp;
8547c478bd9Sstevel@tonic-gate   struct plink *plp;
8557c478bd9Sstevel@tonic-gate   int progress;
8567c478bd9Sstevel@tonic-gate   int change;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate; i++){
8597c478bd9Sstevel@tonic-gate     for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
8607c478bd9Sstevel@tonic-gate       cfp->status = INCOMPLETE;
8617c478bd9Sstevel@tonic-gate     }
8627c478bd9Sstevel@tonic-gate   }
863*55fea89dSDan Cross 
8647c478bd9Sstevel@tonic-gate   do{
8657c478bd9Sstevel@tonic-gate     progress = 0;
8667c478bd9Sstevel@tonic-gate     for(i=0; i<lemp->nstate; i++){
8677c478bd9Sstevel@tonic-gate       for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
8687c478bd9Sstevel@tonic-gate         if( cfp->status==COMPLETE ) continue;
8697c478bd9Sstevel@tonic-gate         for(plp=cfp->fplp; plp; plp=plp->next){
8707c478bd9Sstevel@tonic-gate           change = SetUnion(plp->cfp->fws,cfp->fws);
8717c478bd9Sstevel@tonic-gate           if( change ){
8727c478bd9Sstevel@tonic-gate             plp->cfp->status = INCOMPLETE;
8737c478bd9Sstevel@tonic-gate             progress = 1;
8747c478bd9Sstevel@tonic-gate 	  }
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate         cfp->status = COMPLETE;
8777c478bd9Sstevel@tonic-gate       }
8787c478bd9Sstevel@tonic-gate     }
8797c478bd9Sstevel@tonic-gate   }while( progress );
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate static int resolve_conflict();
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate /* Compute the reduce actions, and resolve conflicts.
8857c478bd9Sstevel@tonic-gate */
FindActions(lemp)8867c478bd9Sstevel@tonic-gate void FindActions(lemp)
8877c478bd9Sstevel@tonic-gate struct lemon *lemp;
8887c478bd9Sstevel@tonic-gate {
8897c478bd9Sstevel@tonic-gate   int i,j;
8907c478bd9Sstevel@tonic-gate   struct config *cfp;
8917c478bd9Sstevel@tonic-gate   struct state *stp;
8927c478bd9Sstevel@tonic-gate   struct symbol *sp;
8937c478bd9Sstevel@tonic-gate   struct rule *rp;
8947c478bd9Sstevel@tonic-gate 
895*55fea89dSDan Cross   /* Add all of the reduce actions
8967c478bd9Sstevel@tonic-gate   ** A reduce action is added for each element of the followset of
8977c478bd9Sstevel@tonic-gate   ** a configuration which has its dot at the extreme right.
8987c478bd9Sstevel@tonic-gate   */
8997c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate; i++){   /* Loop over all states */
9007c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
9017c478bd9Sstevel@tonic-gate     for(cfp=stp->cfp; cfp; cfp=cfp->next){  /* Loop over all configurations */
9027c478bd9Sstevel@tonic-gate       if( cfp->rp->nrhs==cfp->dot ){        /* Is dot at extreme right? */
9037c478bd9Sstevel@tonic-gate         for(j=0; j<lemp->nterminal; j++){
9047c478bd9Sstevel@tonic-gate           if( SetFind(cfp->fws,j) ){
9057c478bd9Sstevel@tonic-gate             /* Add a reduce action to the state "stp" which will reduce by the
9067c478bd9Sstevel@tonic-gate             ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
9077c478bd9Sstevel@tonic-gate             Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
9087c478bd9Sstevel@tonic-gate           }
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate       }
9117c478bd9Sstevel@tonic-gate     }
9127c478bd9Sstevel@tonic-gate   }
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate   /* Add the accepting token */
9157c478bd9Sstevel@tonic-gate   if( lemp->start ){
9167c478bd9Sstevel@tonic-gate     sp = Symbol_find(lemp->start);
9177c478bd9Sstevel@tonic-gate     if( sp==0 ) sp = lemp->rule->lhs;
9187c478bd9Sstevel@tonic-gate   }else{
9197c478bd9Sstevel@tonic-gate     sp = lemp->rule->lhs;
9207c478bd9Sstevel@tonic-gate   }
9217c478bd9Sstevel@tonic-gate   /* Add to the first state (which is always the starting state of the
9227c478bd9Sstevel@tonic-gate   ** finite state machine) an action to ACCEPT if the lookahead is the
9237c478bd9Sstevel@tonic-gate   ** start nonterminal.  */
9247c478bd9Sstevel@tonic-gate   Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate   /* Resolve conflicts */
9277c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate; i++){
9287c478bd9Sstevel@tonic-gate     struct action *ap, *nap;
9297c478bd9Sstevel@tonic-gate     struct state *stp;
9307c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
9317c478bd9Sstevel@tonic-gate     assert( stp->ap );
9327c478bd9Sstevel@tonic-gate     stp->ap = Action_sort(stp->ap);
9337c478bd9Sstevel@tonic-gate     for(ap=stp->ap; ap && ap->next; ap=ap->next){
9347c478bd9Sstevel@tonic-gate       for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
9357c478bd9Sstevel@tonic-gate          /* The two actions "ap" and "nap" have the same lookahead.
9367c478bd9Sstevel@tonic-gate          ** Figure out which one should be used */
9377c478bd9Sstevel@tonic-gate          lemp->nconflict += resolve_conflict(ap,nap,lemp->errsym);
9387c478bd9Sstevel@tonic-gate       }
9397c478bd9Sstevel@tonic-gate     }
9407c478bd9Sstevel@tonic-gate   }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate   /* Report an error for each rule that can never be reduced. */
9437c478bd9Sstevel@tonic-gate   for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = B_FALSE;
9447c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate; i++){
9457c478bd9Sstevel@tonic-gate     struct action *ap;
9467c478bd9Sstevel@tonic-gate     for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
9477c478bd9Sstevel@tonic-gate       if( ap->type==REDUCE ) ap->x.rp->canReduce = B_TRUE;
9487c478bd9Sstevel@tonic-gate     }
9497c478bd9Sstevel@tonic-gate   }
9507c478bd9Sstevel@tonic-gate   for(rp=lemp->rule; rp; rp=rp->next){
9517c478bd9Sstevel@tonic-gate     if( rp->canReduce ) continue;
9527c478bd9Sstevel@tonic-gate     ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
9537c478bd9Sstevel@tonic-gate     lemp->errorcnt++;
9547c478bd9Sstevel@tonic-gate   }
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate /* Resolve a conflict between the two given actions.  If the
9587c478bd9Sstevel@tonic-gate ** conflict can't be resolve, return non-zero.
9597c478bd9Sstevel@tonic-gate **
9607c478bd9Sstevel@tonic-gate ** NO LONGER TRUE:
9617c478bd9Sstevel@tonic-gate **   To resolve a conflict, first look to see if either action
9627c478bd9Sstevel@tonic-gate **   is on an error rule.  In that case, take the action which
9637c478bd9Sstevel@tonic-gate **   is not associated with the error rule.  If neither or both
9647c478bd9Sstevel@tonic-gate **   actions are associated with an error rule, then try to
9657c478bd9Sstevel@tonic-gate **   use precedence to resolve the conflict.
9667c478bd9Sstevel@tonic-gate **
9677c478bd9Sstevel@tonic-gate ** If either action is a SHIFT, then it must be apx.  This
9687c478bd9Sstevel@tonic-gate ** function won't work if apx->type==REDUCE and apy->type==SHIFT.
9697c478bd9Sstevel@tonic-gate */
resolve_conflict(apx,apy,errsym)9707c478bd9Sstevel@tonic-gate static int resolve_conflict(apx,apy,errsym)
9717c478bd9Sstevel@tonic-gate struct action *apx;
9727c478bd9Sstevel@tonic-gate struct action *apy;
9737c478bd9Sstevel@tonic-gate struct symbol *errsym;   /* The error symbol (if defined.  NULL otherwise) */
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate   struct symbol *spx, *spy;
9767c478bd9Sstevel@tonic-gate   int errcnt = 0;
9777c478bd9Sstevel@tonic-gate   assert( apx->sp==apy->sp );  /* Otherwise there would be no conflict */
9787c478bd9Sstevel@tonic-gate   if( apx->type==SHIFT && apy->type==REDUCE ){
9797c478bd9Sstevel@tonic-gate     spx = apx->sp;
9807c478bd9Sstevel@tonic-gate     spy = apy->x.rp->precsym;
9817c478bd9Sstevel@tonic-gate     if( spy==0 || spx->prec<0 || spy->prec<0 ){
9827c478bd9Sstevel@tonic-gate       /* Not enough precedence information. */
9837c478bd9Sstevel@tonic-gate       apy->type = CONFLICT;
9847c478bd9Sstevel@tonic-gate       errcnt++;
9857c478bd9Sstevel@tonic-gate     }else if( spx->prec>spy->prec ){    /* Lower precedence wins */
9867c478bd9Sstevel@tonic-gate       apy->type = RD_RESOLVED;
9877c478bd9Sstevel@tonic-gate     }else if( spx->prec<spy->prec ){
9887c478bd9Sstevel@tonic-gate       apx->type = SH_RESOLVED;
9897c478bd9Sstevel@tonic-gate     }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
9907c478bd9Sstevel@tonic-gate       apy->type = RD_RESOLVED;                             /* associativity */
9917c478bd9Sstevel@tonic-gate     }else if( spx->prec==spy->prec && spx->assoc==LEFT ){  /* to break tie */
9927c478bd9Sstevel@tonic-gate       apx->type = SH_RESOLVED;
9937c478bd9Sstevel@tonic-gate     }else{
9947c478bd9Sstevel@tonic-gate       assert( spx->prec==spy->prec && spx->assoc==NONE );
9957c478bd9Sstevel@tonic-gate       apy->type = CONFLICT;
9967c478bd9Sstevel@tonic-gate       errcnt++;
9977c478bd9Sstevel@tonic-gate     }
9987c478bd9Sstevel@tonic-gate   }else if( apx->type==REDUCE && apy->type==REDUCE ){
9997c478bd9Sstevel@tonic-gate     spx = apx->x.rp->precsym;
10007c478bd9Sstevel@tonic-gate     spy = apy->x.rp->precsym;
10017c478bd9Sstevel@tonic-gate     if( spx==0 || spy==0 || spx->prec<0 ||
10027c478bd9Sstevel@tonic-gate     spy->prec<0 || spx->prec==spy->prec ){
10037c478bd9Sstevel@tonic-gate       apy->type = CONFLICT;
10047c478bd9Sstevel@tonic-gate       errcnt++;
10057c478bd9Sstevel@tonic-gate     }else if( spx->prec>spy->prec ){
10067c478bd9Sstevel@tonic-gate       apy->type = RD_RESOLVED;
10077c478bd9Sstevel@tonic-gate     }else if( spx->prec<spy->prec ){
10087c478bd9Sstevel@tonic-gate       apx->type = RD_RESOLVED;
10097c478bd9Sstevel@tonic-gate     }
10107c478bd9Sstevel@tonic-gate   }else{
1011*55fea89dSDan Cross     assert(
10127c478bd9Sstevel@tonic-gate       apx->type==SH_RESOLVED ||
10137c478bd9Sstevel@tonic-gate       apx->type==RD_RESOLVED ||
10147c478bd9Sstevel@tonic-gate       apx->type==CONFLICT ||
10157c478bd9Sstevel@tonic-gate       apy->type==SH_RESOLVED ||
10167c478bd9Sstevel@tonic-gate       apy->type==RD_RESOLVED ||
10177c478bd9Sstevel@tonic-gate       apy->type==CONFLICT
10187c478bd9Sstevel@tonic-gate     );
10197c478bd9Sstevel@tonic-gate     /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
10207c478bd9Sstevel@tonic-gate     ** REDUCEs on the list.  If we reach this point it must be because
10217c478bd9Sstevel@tonic-gate     ** the parser conflict had already been resolved. */
10227c478bd9Sstevel@tonic-gate   }
10237c478bd9Sstevel@tonic-gate   return errcnt;
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate /********************* From the file "configlist.c" *************************/
10267c478bd9Sstevel@tonic-gate /*
10277c478bd9Sstevel@tonic-gate ** Routines to processing a configuration list and building a state
10287c478bd9Sstevel@tonic-gate ** in the LEMON parser generator.
10297c478bd9Sstevel@tonic-gate */
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate static struct config *freelist = 0;      /* List of free configurations */
10327c478bd9Sstevel@tonic-gate static struct config *current = 0;       /* Top of list of configurations */
10337c478bd9Sstevel@tonic-gate static struct config **currentend = 0;   /* Last on list of configs */
10347c478bd9Sstevel@tonic-gate static struct config *basis = 0;         /* Top of list of basis configs */
10357c478bd9Sstevel@tonic-gate static struct config **basisend = 0;     /* End of list of basis configs */
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate /* Return a pointer to a new configuration */
newconfig()10387c478bd9Sstevel@tonic-gate PRIVATE struct config *newconfig(){
10397c478bd9Sstevel@tonic-gate   struct config *new;
10407c478bd9Sstevel@tonic-gate   if( freelist==0 ){
10417c478bd9Sstevel@tonic-gate     int i;
10427c478bd9Sstevel@tonic-gate     int amt = 3;
10437c478bd9Sstevel@tonic-gate     freelist = (struct config *)malloc( sizeof(struct config)*amt );
10447c478bd9Sstevel@tonic-gate     if( freelist==0 ){
10457c478bd9Sstevel@tonic-gate       fprintf(stderr,"Unable to allocate memory for a new configuration.");
10467c478bd9Sstevel@tonic-gate       exit(1);
10477c478bd9Sstevel@tonic-gate     }
10487c478bd9Sstevel@tonic-gate     for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
10497c478bd9Sstevel@tonic-gate     freelist[amt-1].next = 0;
10507c478bd9Sstevel@tonic-gate   }
10517c478bd9Sstevel@tonic-gate   new = freelist;
10527c478bd9Sstevel@tonic-gate   freelist = freelist->next;
10537c478bd9Sstevel@tonic-gate   return new;
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate /* The configuration "old" is no longer used */
deleteconfig(old)10577c478bd9Sstevel@tonic-gate PRIVATE void deleteconfig(old)
10587c478bd9Sstevel@tonic-gate struct config *old;
10597c478bd9Sstevel@tonic-gate {
10607c478bd9Sstevel@tonic-gate   old->next = freelist;
10617c478bd9Sstevel@tonic-gate   freelist = old;
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /* Initialized the configuration list builder */
Configlist_init()10657c478bd9Sstevel@tonic-gate void Configlist_init(){
10667c478bd9Sstevel@tonic-gate   current = 0;
10677c478bd9Sstevel@tonic-gate   currentend = &current;
10687c478bd9Sstevel@tonic-gate   basis = 0;
10697c478bd9Sstevel@tonic-gate   basisend = &basis;
10707c478bd9Sstevel@tonic-gate   Configtable_init();
10717c478bd9Sstevel@tonic-gate   return;
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate /* Initialized the configuration list builder */
Configlist_reset()10757c478bd9Sstevel@tonic-gate void Configlist_reset(){
10767c478bd9Sstevel@tonic-gate   current = 0;
10777c478bd9Sstevel@tonic-gate   currentend = &current;
10787c478bd9Sstevel@tonic-gate   basis = 0;
10797c478bd9Sstevel@tonic-gate   basisend = &basis;
10807c478bd9Sstevel@tonic-gate   Configtable_clear(0);
10817c478bd9Sstevel@tonic-gate   return;
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /* Add another configuration to the configuration list */
Configlist_add(rp,dot)10857c478bd9Sstevel@tonic-gate struct config *Configlist_add(rp,dot)
10867c478bd9Sstevel@tonic-gate struct rule *rp;    /* The rule */
10877c478bd9Sstevel@tonic-gate int dot;            /* Index into the RHS of the rule where the dot goes */
10887c478bd9Sstevel@tonic-gate {
10897c478bd9Sstevel@tonic-gate   struct config *cfp, model;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate   assert( currentend!=0 );
10927c478bd9Sstevel@tonic-gate   model.rp = rp;
10937c478bd9Sstevel@tonic-gate   model.dot = dot;
10947c478bd9Sstevel@tonic-gate   cfp = Configtable_find(&model);
10957c478bd9Sstevel@tonic-gate   if( cfp==0 ){
10967c478bd9Sstevel@tonic-gate     cfp = newconfig();
10977c478bd9Sstevel@tonic-gate     cfp->rp = rp;
10987c478bd9Sstevel@tonic-gate     cfp->dot = dot;
10997c478bd9Sstevel@tonic-gate     cfp->fws = SetNew();
11007c478bd9Sstevel@tonic-gate     cfp->stp = 0;
11017c478bd9Sstevel@tonic-gate     cfp->fplp = cfp->bplp = 0;
11027c478bd9Sstevel@tonic-gate     cfp->next = 0;
11037c478bd9Sstevel@tonic-gate     cfp->bp = 0;
11047c478bd9Sstevel@tonic-gate     *currentend = cfp;
11057c478bd9Sstevel@tonic-gate     currentend = &cfp->next;
11067c478bd9Sstevel@tonic-gate     Configtable_insert(cfp);
11077c478bd9Sstevel@tonic-gate   }
11087c478bd9Sstevel@tonic-gate   return cfp;
11097c478bd9Sstevel@tonic-gate }
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate /* Add a basis configuration to the configuration list */
Configlist_addbasis(rp,dot)11127c478bd9Sstevel@tonic-gate struct config *Configlist_addbasis(rp,dot)
11137c478bd9Sstevel@tonic-gate struct rule *rp;
11147c478bd9Sstevel@tonic-gate int dot;
11157c478bd9Sstevel@tonic-gate {
11167c478bd9Sstevel@tonic-gate   struct config *cfp, model;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate   assert( basisend!=0 );
11197c478bd9Sstevel@tonic-gate   assert( currentend!=0 );
11207c478bd9Sstevel@tonic-gate   model.rp = rp;
11217c478bd9Sstevel@tonic-gate   model.dot = dot;
11227c478bd9Sstevel@tonic-gate   cfp = Configtable_find(&model);
11237c478bd9Sstevel@tonic-gate   if( cfp==0 ){
11247c478bd9Sstevel@tonic-gate     cfp = newconfig();
11257c478bd9Sstevel@tonic-gate     cfp->rp = rp;
11267c478bd9Sstevel@tonic-gate     cfp->dot = dot;
11277c478bd9Sstevel@tonic-gate     cfp->fws = SetNew();
11287c478bd9Sstevel@tonic-gate     cfp->stp = 0;
11297c478bd9Sstevel@tonic-gate     cfp->fplp = cfp->bplp = 0;
11307c478bd9Sstevel@tonic-gate     cfp->next = 0;
11317c478bd9Sstevel@tonic-gate     cfp->bp = 0;
11327c478bd9Sstevel@tonic-gate     *currentend = cfp;
11337c478bd9Sstevel@tonic-gate     currentend = &cfp->next;
11347c478bd9Sstevel@tonic-gate     *basisend = cfp;
11357c478bd9Sstevel@tonic-gate     basisend = &cfp->bp;
11367c478bd9Sstevel@tonic-gate     Configtable_insert(cfp);
11377c478bd9Sstevel@tonic-gate   }
11387c478bd9Sstevel@tonic-gate   return cfp;
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate /* Compute the closure of the configuration list */
Configlist_closure(lemp)11427c478bd9Sstevel@tonic-gate void Configlist_closure(lemp)
11437c478bd9Sstevel@tonic-gate struct lemon *lemp;
11447c478bd9Sstevel@tonic-gate {
11457c478bd9Sstevel@tonic-gate   struct config *cfp, *newcfp;
11467c478bd9Sstevel@tonic-gate   struct rule *rp, *newrp;
11477c478bd9Sstevel@tonic-gate   struct symbol *sp, *xsp;
11487c478bd9Sstevel@tonic-gate   int i, dot;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate   assert( currentend!=0 );
11517c478bd9Sstevel@tonic-gate   for(cfp=current; cfp; cfp=cfp->next){
11527c478bd9Sstevel@tonic-gate     rp = cfp->rp;
11537c478bd9Sstevel@tonic-gate     dot = cfp->dot;
11547c478bd9Sstevel@tonic-gate     if( dot>=rp->nrhs ) continue;
11557c478bd9Sstevel@tonic-gate     sp = rp->rhs[dot];
11567c478bd9Sstevel@tonic-gate     if( sp->type==NONTERMINAL ){
11577c478bd9Sstevel@tonic-gate       if( sp->rule==0 && sp!=lemp->errsym ){
11587c478bd9Sstevel@tonic-gate         ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
11597c478bd9Sstevel@tonic-gate           sp->name);
11607c478bd9Sstevel@tonic-gate         lemp->errorcnt++;
11617c478bd9Sstevel@tonic-gate       }
11627c478bd9Sstevel@tonic-gate       for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
11637c478bd9Sstevel@tonic-gate         newcfp = Configlist_add(newrp,0);
11647c478bd9Sstevel@tonic-gate         for(i=dot+1; i<rp->nrhs; i++){
11657c478bd9Sstevel@tonic-gate           xsp = rp->rhs[i];
11667c478bd9Sstevel@tonic-gate           if( xsp->type==TERMINAL ){
11677c478bd9Sstevel@tonic-gate             SetAdd(newcfp->fws,xsp->index);
11687c478bd9Sstevel@tonic-gate             break;
11697c478bd9Sstevel@tonic-gate 	  }else{
11707c478bd9Sstevel@tonic-gate             SetUnion(newcfp->fws,xsp->firstset);
11717c478bd9Sstevel@tonic-gate             if( xsp->lambda==B_FALSE ) break;
11727c478bd9Sstevel@tonic-gate 	  }
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate         if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
11757c478bd9Sstevel@tonic-gate       }
11767c478bd9Sstevel@tonic-gate     }
11777c478bd9Sstevel@tonic-gate   }
11787c478bd9Sstevel@tonic-gate   return;
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /* Sort the configuration list */
Configlist_sort()11827c478bd9Sstevel@tonic-gate void Configlist_sort(){
11837c478bd9Sstevel@tonic-gate   current = (struct config *)msort((char *)current,(char **)&(current->next),Configcmp);
11847c478bd9Sstevel@tonic-gate   currentend = 0;
11857c478bd9Sstevel@tonic-gate   return;
11867c478bd9Sstevel@tonic-gate }
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate /* Sort the basis configuration list */
Configlist_sortbasis()11897c478bd9Sstevel@tonic-gate void Configlist_sortbasis(){
11907c478bd9Sstevel@tonic-gate   basis = (struct config *)msort((char *)current,(char **)&(current->bp),Configcmp);
11917c478bd9Sstevel@tonic-gate   basisend = 0;
11927c478bd9Sstevel@tonic-gate   return;
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate /* Return a pointer to the head of the configuration list and
11967c478bd9Sstevel@tonic-gate ** reset the list */
Configlist_return()11977c478bd9Sstevel@tonic-gate struct config *Configlist_return(){
11987c478bd9Sstevel@tonic-gate   struct config *old;
11997c478bd9Sstevel@tonic-gate   old = current;
12007c478bd9Sstevel@tonic-gate   current = 0;
12017c478bd9Sstevel@tonic-gate   currentend = 0;
12027c478bd9Sstevel@tonic-gate   return old;
12037c478bd9Sstevel@tonic-gate }
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate /* Return a pointer to the head of the configuration list and
12067c478bd9Sstevel@tonic-gate ** reset the list */
Configlist_basis()12077c478bd9Sstevel@tonic-gate struct config *Configlist_basis(){
12087c478bd9Sstevel@tonic-gate   struct config *old;
12097c478bd9Sstevel@tonic-gate   old = basis;
12107c478bd9Sstevel@tonic-gate   basis = 0;
12117c478bd9Sstevel@tonic-gate   basisend = 0;
12127c478bd9Sstevel@tonic-gate   return old;
12137c478bd9Sstevel@tonic-gate }
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate /* Free all elements of the given configuration list */
Configlist_eat(cfp)12167c478bd9Sstevel@tonic-gate void Configlist_eat(cfp)
12177c478bd9Sstevel@tonic-gate struct config *cfp;
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate   struct config *nextcfp;
12207c478bd9Sstevel@tonic-gate   for(; cfp; cfp=nextcfp){
12217c478bd9Sstevel@tonic-gate     nextcfp = cfp->next;
12227c478bd9Sstevel@tonic-gate     assert( cfp->fplp==0 );
12237c478bd9Sstevel@tonic-gate     assert( cfp->bplp==0 );
12247c478bd9Sstevel@tonic-gate     if( cfp->fws ) SetFree(cfp->fws);
12257c478bd9Sstevel@tonic-gate     deleteconfig(cfp);
12267c478bd9Sstevel@tonic-gate   }
12277c478bd9Sstevel@tonic-gate   return;
12287c478bd9Sstevel@tonic-gate }
12297c478bd9Sstevel@tonic-gate /***************** From the file "error.c" *********************************/
12307c478bd9Sstevel@tonic-gate /*
12317c478bd9Sstevel@tonic-gate ** Code for printing error message.
12327c478bd9Sstevel@tonic-gate */
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate /* Find a good place to break "msg" so that its length is at least "min"
12357c478bd9Sstevel@tonic-gate ** but no more than "max".  Make the point as close to max as possible.
12367c478bd9Sstevel@tonic-gate */
findbreak(msg,min,max)12377c478bd9Sstevel@tonic-gate static int findbreak(msg,min,max)
12387c478bd9Sstevel@tonic-gate char *msg;
12397c478bd9Sstevel@tonic-gate int min;
12407c478bd9Sstevel@tonic-gate int max;
12417c478bd9Sstevel@tonic-gate {
12427c478bd9Sstevel@tonic-gate   int i,spot;
12437c478bd9Sstevel@tonic-gate   char c;
12447c478bd9Sstevel@tonic-gate   for(i=spot=min; i<=max; i++){
12457c478bd9Sstevel@tonic-gate     c = msg[i];
12467c478bd9Sstevel@tonic-gate     if( c=='\t' ) msg[i] = ' ';
12477c478bd9Sstevel@tonic-gate     if( c=='\n' ){ msg[i] = ' '; spot = i; break; }
12487c478bd9Sstevel@tonic-gate     if( c==0 ){ spot = i; break; }
12497c478bd9Sstevel@tonic-gate     if( c=='-' && i<max-1 ) spot = i+1;
12507c478bd9Sstevel@tonic-gate     if( c==' ' ) spot = i;
12517c478bd9Sstevel@tonic-gate   }
12527c478bd9Sstevel@tonic-gate   return spot;
12537c478bd9Sstevel@tonic-gate }
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate /*
12567c478bd9Sstevel@tonic-gate ** The error message is split across multiple lines if necessary.  The
12577c478bd9Sstevel@tonic-gate ** splits occur at a space, if there is a space available near the end
12587c478bd9Sstevel@tonic-gate ** of the line.
12597c478bd9Sstevel@tonic-gate */
12607c478bd9Sstevel@tonic-gate #define ERRMSGSIZE  10000 /* Hope this is big enough.  No way to error check */
12617c478bd9Sstevel@tonic-gate #define LINEWIDTH      79 /* Max width of any output line */
12627c478bd9Sstevel@tonic-gate #define PREFIXLIMIT    30 /* Max width of the prefix on each line */
ErrorMsg(const char * filename,int lineno,const char * format,...)12637c478bd9Sstevel@tonic-gate void ErrorMsg(const char *filename, int lineno, const char *format, ...){
12647c478bd9Sstevel@tonic-gate   char errmsg[ERRMSGSIZE];
12657c478bd9Sstevel@tonic-gate   char prefix[PREFIXLIMIT+10];
12667c478bd9Sstevel@tonic-gate   int errmsgsize;
12677c478bd9Sstevel@tonic-gate   int prefixsize;
12687c478bd9Sstevel@tonic-gate   int availablewidth;
12697c478bd9Sstevel@tonic-gate   va_list ap;
12707c478bd9Sstevel@tonic-gate   int end, restart, base;
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate   va_start(ap, format);
12737c478bd9Sstevel@tonic-gate   /* Prepare a prefix to be prepended to every output line */
12747c478bd9Sstevel@tonic-gate   if( lineno>0 ){
12757c478bd9Sstevel@tonic-gate     sprintf(prefix,"%.*s:%d: ",PREFIXLIMIT-10,filename,lineno);
12767c478bd9Sstevel@tonic-gate   }else{
12777c478bd9Sstevel@tonic-gate     sprintf(prefix,"%.*s: ",PREFIXLIMIT-10,filename);
12787c478bd9Sstevel@tonic-gate   }
12797c478bd9Sstevel@tonic-gate   prefixsize = strlen(prefix);
12807c478bd9Sstevel@tonic-gate   availablewidth = LINEWIDTH - prefixsize;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate   /* Generate the error message */
12837c478bd9Sstevel@tonic-gate   vsprintf(errmsg,format,ap);
12847c478bd9Sstevel@tonic-gate   va_end(ap);
12857c478bd9Sstevel@tonic-gate   errmsgsize = strlen(errmsg);
12867c478bd9Sstevel@tonic-gate   /* Remove trailing '\n's from the error message. */
12877c478bd9Sstevel@tonic-gate   while( errmsgsize>0 && errmsg[errmsgsize-1]=='\n' ){
12887c478bd9Sstevel@tonic-gate      errmsg[--errmsgsize] = 0;
12897c478bd9Sstevel@tonic-gate   }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate   /* Print the error message */
12927c478bd9Sstevel@tonic-gate   base = 0;
12937c478bd9Sstevel@tonic-gate   while( errmsg[base]!=0 ){
12947c478bd9Sstevel@tonic-gate     end = restart = findbreak(&errmsg[base],0,availablewidth);
12957c478bd9Sstevel@tonic-gate     restart += base;
12967c478bd9Sstevel@tonic-gate     while( errmsg[restart]==' ' ) restart++;
12977c478bd9Sstevel@tonic-gate     fprintf(stdout,"%s%.*s\n",prefix,end,&errmsg[base]);
12987c478bd9Sstevel@tonic-gate     base = restart;
12997c478bd9Sstevel@tonic-gate   }
13007c478bd9Sstevel@tonic-gate }
13017c478bd9Sstevel@tonic-gate /**************** From the file "main.c" ************************************/
13027c478bd9Sstevel@tonic-gate /*
13037c478bd9Sstevel@tonic-gate ** Main program file for the LEMON parser generator.
13047c478bd9Sstevel@tonic-gate */
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate /* Report an out-of-memory condition and abort.  This function
13077c478bd9Sstevel@tonic-gate ** is used mostly by the "MemoryCheck" macro in struct.h
13087c478bd9Sstevel@tonic-gate */
memory_error()13097c478bd9Sstevel@tonic-gate void memory_error(){
13107c478bd9Sstevel@tonic-gate   fprintf(stderr,"Out of memory.  Aborting...\n");
13117c478bd9Sstevel@tonic-gate   exit(1);
13127c478bd9Sstevel@tonic-gate }
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate /* The main program.  Parse the command line and do it... */
main(argc,argv)13167c478bd9Sstevel@tonic-gate int main(argc,argv)
13177c478bd9Sstevel@tonic-gate int argc;
13187c478bd9Sstevel@tonic-gate char **argv;
13197c478bd9Sstevel@tonic-gate {
13207c478bd9Sstevel@tonic-gate   static int version = 0;
13217c478bd9Sstevel@tonic-gate   static int rpflag = 0;
13227c478bd9Sstevel@tonic-gate   static int basisflag = 0;
13237c478bd9Sstevel@tonic-gate   static int compress = 0;
13247c478bd9Sstevel@tonic-gate   static int quiet = 0;
13257c478bd9Sstevel@tonic-gate   static int statistics = 0;
13267c478bd9Sstevel@tonic-gate   static int mhflag = 0;
13277c478bd9Sstevel@tonic-gate   static struct s_options options[] = {
13287c478bd9Sstevel@tonic-gate     {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
13297c478bd9Sstevel@tonic-gate     {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
13307c478bd9Sstevel@tonic-gate     {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
13317c478bd9Sstevel@tonic-gate     {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file"},
13327c478bd9Sstevel@tonic-gate     {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
13337c478bd9Sstevel@tonic-gate     {OPT_FLAG, "s", (char*)&statistics, "Print parser stats to standard output."},
13347c478bd9Sstevel@tonic-gate     {OPT_FLAG, "x", (char*)&version, "Print the version number."},
13357c478bd9Sstevel@tonic-gate     {OPT_FLAG,0,0,0}
13367c478bd9Sstevel@tonic-gate   };
13377c478bd9Sstevel@tonic-gate   int i;
13387c478bd9Sstevel@tonic-gate   struct lemon lem;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate   OptInit(argv,options,stderr);
13417c478bd9Sstevel@tonic-gate   if( version ){
13427c478bd9Sstevel@tonic-gate      printf("Lemon version 1.0\n");
1343*55fea89dSDan Cross      exit(0);
13447c478bd9Sstevel@tonic-gate   }
13457c478bd9Sstevel@tonic-gate   if( OptNArgs()!=1 ){
13467c478bd9Sstevel@tonic-gate     fprintf(stderr,"Exactly one filename argument is required.\n");
13477c478bd9Sstevel@tonic-gate     exit(1);
13487c478bd9Sstevel@tonic-gate   }
13497c478bd9Sstevel@tonic-gate   lem.errorcnt = 0;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate   /* Initialize the machine */
13527c478bd9Sstevel@tonic-gate   Strsafe_init();
13537c478bd9Sstevel@tonic-gate   Symbol_init();
13547c478bd9Sstevel@tonic-gate   State_init();
13557c478bd9Sstevel@tonic-gate   lem.argv0 = argv[0];
13567c478bd9Sstevel@tonic-gate   lem.filename = OptArg(0);
13577c478bd9Sstevel@tonic-gate   lem.basisflag = basisflag;
13587c478bd9Sstevel@tonic-gate   lem.has_fallback = 0;
13597c478bd9Sstevel@tonic-gate   lem.nconflict = 0;
13607c478bd9Sstevel@tonic-gate   lem.name = lem.include = lem.arg = lem.tokentype = lem.start = 0;
13617c478bd9Sstevel@tonic-gate   lem.vartype = 0;
13627c478bd9Sstevel@tonic-gate   lem.stacksize = 0;
13637c478bd9Sstevel@tonic-gate   lem.error = lem.overflow = lem.failure = lem.accept = lem.tokendest =
13647c478bd9Sstevel@tonic-gate      lem.tokenprefix = lem.outname = lem.extracode = 0;
13657c478bd9Sstevel@tonic-gate   lem.vardest = 0;
13667c478bd9Sstevel@tonic-gate   lem.tablesize = 0;
13677c478bd9Sstevel@tonic-gate   Symbol_new("$");
13687c478bd9Sstevel@tonic-gate   lem.errsym = Symbol_new("error");
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate   /* Parse the input file */
13717c478bd9Sstevel@tonic-gate   Parse(&lem);
13727c478bd9Sstevel@tonic-gate   if( lem.errorcnt ) exit(lem.errorcnt);
13737c478bd9Sstevel@tonic-gate   if( lem.rule==0 ){
13747c478bd9Sstevel@tonic-gate     fprintf(stderr,"Empty grammar.\n");
13757c478bd9Sstevel@tonic-gate     exit(1);
13767c478bd9Sstevel@tonic-gate   }
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate   /* Count and index the symbols of the grammar */
13797c478bd9Sstevel@tonic-gate   lem.nsymbol = Symbol_count();
13807c478bd9Sstevel@tonic-gate   Symbol_new("{default}");
13817c478bd9Sstevel@tonic-gate   lem.symbols = Symbol_arrayof();
13827c478bd9Sstevel@tonic-gate   for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
13837c478bd9Sstevel@tonic-gate   qsort(lem.symbols,lem.nsymbol+1,sizeof(struct symbol*),
13847c478bd9Sstevel@tonic-gate         (int(*)())Symbolcmpp);
13857c478bd9Sstevel@tonic-gate   for(i=0; i<=lem.nsymbol; i++) lem.symbols[i]->index = i;
13867c478bd9Sstevel@tonic-gate   for(i=1; isupper(lem.symbols[i]->name[0]); i++);
13877c478bd9Sstevel@tonic-gate   lem.nterminal = i;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate   /* Generate a reprint of the grammar, if requested on the command line */
13907c478bd9Sstevel@tonic-gate   if( rpflag ){
13917c478bd9Sstevel@tonic-gate     Reprint(&lem);
13927c478bd9Sstevel@tonic-gate   }else{
13937c478bd9Sstevel@tonic-gate     /* Initialize the size for all follow and first sets */
13947c478bd9Sstevel@tonic-gate     SetSize(lem.nterminal);
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate     /* Find the precedence for every production rule (that has one) */
13977c478bd9Sstevel@tonic-gate     FindRulePrecedences(&lem);
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate     /* Compute the lambda-nonterminals and the first-sets for every
14007c478bd9Sstevel@tonic-gate     ** nonterminal */
14017c478bd9Sstevel@tonic-gate     FindFirstSets(&lem);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate     /* Compute all LR(0) states.  Also record follow-set propagation
14047c478bd9Sstevel@tonic-gate     ** links so that the follow-set can be computed later */
14057c478bd9Sstevel@tonic-gate     lem.nstate = 0;
14067c478bd9Sstevel@tonic-gate     FindStates(&lem);
14077c478bd9Sstevel@tonic-gate     lem.sorted = State_arrayof();
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate     /* Tie up loose ends on the propagation links */
14107c478bd9Sstevel@tonic-gate     FindLinks(&lem);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate     /* Compute the follow set of every reducible configuration */
14137c478bd9Sstevel@tonic-gate     FindFollowSets(&lem);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate     /* Compute the action tables */
14167c478bd9Sstevel@tonic-gate     FindActions(&lem);
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate     /* Compress the action tables */
14197c478bd9Sstevel@tonic-gate     if( compress==0 ) CompressTables(&lem);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate     /* Generate a report of the parser generated.  (the "y.output" file) */
14227c478bd9Sstevel@tonic-gate     if( !quiet ) ReportOutput(&lem);
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate     /* Generate the source code for the parser */
14257c478bd9Sstevel@tonic-gate     ReportTable(&lem, mhflag);
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate     /* Produce a header file for use by the scanner.  (This step is
14287c478bd9Sstevel@tonic-gate     ** omitted if the "-m" option is used because makeheaders will
14297c478bd9Sstevel@tonic-gate     ** generate the file for us.) */
14307c478bd9Sstevel@tonic-gate     if( !mhflag ) ReportHeader(&lem);
14317c478bd9Sstevel@tonic-gate   }
14327c478bd9Sstevel@tonic-gate   if( statistics ){
14337c478bd9Sstevel@tonic-gate     printf("Parser statistics: %d terminals, %d nonterminals, %d rules\n",
14347c478bd9Sstevel@tonic-gate       lem.nterminal, lem.nsymbol - lem.nterminal, lem.nrule);
14357c478bd9Sstevel@tonic-gate     printf("                   %d states, %d parser table entries, %d conflicts\n",
14367c478bd9Sstevel@tonic-gate       lem.nstate, lem.tablesize, lem.nconflict);
14377c478bd9Sstevel@tonic-gate   }
14387c478bd9Sstevel@tonic-gate   if( lem.nconflict ){
14397c478bd9Sstevel@tonic-gate     fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
14407c478bd9Sstevel@tonic-gate   }
14417c478bd9Sstevel@tonic-gate   exit(lem.errorcnt + lem.nconflict);
14427c478bd9Sstevel@tonic-gate   return (lem.errorcnt + lem.nconflict);
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate /******************** From the file "msort.c" *******************************/
14457c478bd9Sstevel@tonic-gate /*
14467c478bd9Sstevel@tonic-gate ** A generic merge-sort program.
14477c478bd9Sstevel@tonic-gate **
14487c478bd9Sstevel@tonic-gate ** USAGE:
14497c478bd9Sstevel@tonic-gate ** Let "ptr" be a pointer to some structure which is at the head of
14507c478bd9Sstevel@tonic-gate ** a null-terminated list.  Then to sort the list call:
14517c478bd9Sstevel@tonic-gate **
14527c478bd9Sstevel@tonic-gate **     ptr = msort(ptr,&(ptr->next),cmpfnc);
14537c478bd9Sstevel@tonic-gate **
14547c478bd9Sstevel@tonic-gate ** In the above, "cmpfnc" is a pointer to a function which compares
14557c478bd9Sstevel@tonic-gate ** two instances of the structure and returns an integer, as in
14567c478bd9Sstevel@tonic-gate ** strcmp.  The second argument is a pointer to the pointer to the
14577c478bd9Sstevel@tonic-gate ** second element of the linked list.  This address is used to compute
14587c478bd9Sstevel@tonic-gate ** the offset to the "next" field within the structure.  The offset to
14597c478bd9Sstevel@tonic-gate ** the "next" field must be constant for all structures in the list.
14607c478bd9Sstevel@tonic-gate **
14617c478bd9Sstevel@tonic-gate ** The function returns a new pointer which is the head of the list
14627c478bd9Sstevel@tonic-gate ** after sorting.
14637c478bd9Sstevel@tonic-gate **
14647c478bd9Sstevel@tonic-gate ** ALGORITHM:
14657c478bd9Sstevel@tonic-gate ** Merge-sort.
14667c478bd9Sstevel@tonic-gate */
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate /*
14697c478bd9Sstevel@tonic-gate ** Return a pointer to the next structure in the linked list.
14707c478bd9Sstevel@tonic-gate */
14717c478bd9Sstevel@tonic-gate #define NEXT(A) (*(char**)(((unsigned long)A)+offset))
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate /*
14747c478bd9Sstevel@tonic-gate ** Inputs:
14757c478bd9Sstevel@tonic-gate **   a:       A sorted, null-terminated linked list.  (May be null).
14767c478bd9Sstevel@tonic-gate **   b:       A sorted, null-terminated linked list.  (May be null).
14777c478bd9Sstevel@tonic-gate **   cmp:     A pointer to the comparison function.
14787c478bd9Sstevel@tonic-gate **   offset:  Offset in the structure to the "next" field.
14797c478bd9Sstevel@tonic-gate **
14807c478bd9Sstevel@tonic-gate ** Return Value:
14817c478bd9Sstevel@tonic-gate **   A pointer to the head of a sorted list containing the elements
14827c478bd9Sstevel@tonic-gate **   of both a and b.
14837c478bd9Sstevel@tonic-gate **
14847c478bd9Sstevel@tonic-gate ** Side effects:
14857c478bd9Sstevel@tonic-gate **   The "next" pointers for elements in the lists a and b are
14867c478bd9Sstevel@tonic-gate **   changed.
14877c478bd9Sstevel@tonic-gate */
merge(a,b,cmp,offset)14887c478bd9Sstevel@tonic-gate static char *merge(a,b,cmp,offset)
14897c478bd9Sstevel@tonic-gate char *a;
14907c478bd9Sstevel@tonic-gate char *b;
14917c478bd9Sstevel@tonic-gate int (*cmp)();
14927c478bd9Sstevel@tonic-gate int offset;
14937c478bd9Sstevel@tonic-gate {
14947c478bd9Sstevel@tonic-gate   char *ptr, *head;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate   if( a==0 ){
14977c478bd9Sstevel@tonic-gate     head = b;
14987c478bd9Sstevel@tonic-gate   }else if( b==0 ){
14997c478bd9Sstevel@tonic-gate     head = a;
15007c478bd9Sstevel@tonic-gate   }else{
15017c478bd9Sstevel@tonic-gate     if( (*cmp)(a,b)<0 ){
15027c478bd9Sstevel@tonic-gate       ptr = a;
15037c478bd9Sstevel@tonic-gate       a = NEXT(a);
15047c478bd9Sstevel@tonic-gate     }else{
15057c478bd9Sstevel@tonic-gate       ptr = b;
15067c478bd9Sstevel@tonic-gate       b = NEXT(b);
15077c478bd9Sstevel@tonic-gate     }
15087c478bd9Sstevel@tonic-gate     head = ptr;
15097c478bd9Sstevel@tonic-gate     while( a && b ){
15107c478bd9Sstevel@tonic-gate       if( (*cmp)(a,b)<0 ){
15117c478bd9Sstevel@tonic-gate         NEXT(ptr) = a;
15127c478bd9Sstevel@tonic-gate         ptr = a;
15137c478bd9Sstevel@tonic-gate         a = NEXT(a);
15147c478bd9Sstevel@tonic-gate       }else{
15157c478bd9Sstevel@tonic-gate         NEXT(ptr) = b;
15167c478bd9Sstevel@tonic-gate         ptr = b;
15177c478bd9Sstevel@tonic-gate         b = NEXT(b);
15187c478bd9Sstevel@tonic-gate       }
15197c478bd9Sstevel@tonic-gate     }
15207c478bd9Sstevel@tonic-gate     if( a ) NEXT(ptr) = a;
15217c478bd9Sstevel@tonic-gate     else    NEXT(ptr) = b;
15227c478bd9Sstevel@tonic-gate   }
15237c478bd9Sstevel@tonic-gate   return head;
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate /*
15277c478bd9Sstevel@tonic-gate ** Inputs:
15287c478bd9Sstevel@tonic-gate **   list:      Pointer to a singly-linked list of structures.
15297c478bd9Sstevel@tonic-gate **   next:      Pointer to pointer to the second element of the list.
15307c478bd9Sstevel@tonic-gate **   cmp:       A comparison function.
15317c478bd9Sstevel@tonic-gate **
15327c478bd9Sstevel@tonic-gate ** Return Value:
15337c478bd9Sstevel@tonic-gate **   A pointer to the head of a sorted list containing the elements
15347c478bd9Sstevel@tonic-gate **   orginally in list.
15357c478bd9Sstevel@tonic-gate **
15367c478bd9Sstevel@tonic-gate ** Side effects:
15377c478bd9Sstevel@tonic-gate **   The "next" pointers for elements in list are changed.
15387c478bd9Sstevel@tonic-gate */
15397c478bd9Sstevel@tonic-gate #define LISTSIZE 30
msort(list,next,cmp)15407c478bd9Sstevel@tonic-gate char *msort(list,next,cmp)
15417c478bd9Sstevel@tonic-gate char *list;
15427c478bd9Sstevel@tonic-gate char **next;
15437c478bd9Sstevel@tonic-gate int (*cmp)();
15447c478bd9Sstevel@tonic-gate {
15457c478bd9Sstevel@tonic-gate   unsigned long offset;
15467c478bd9Sstevel@tonic-gate   char *ep;
15477c478bd9Sstevel@tonic-gate   char *set[LISTSIZE];
15487c478bd9Sstevel@tonic-gate   int i;
15497c478bd9Sstevel@tonic-gate   offset = (unsigned long)next - (unsigned long)list;
15507c478bd9Sstevel@tonic-gate   for(i=0; i<LISTSIZE; i++) set[i] = 0;
15517c478bd9Sstevel@tonic-gate   while( list ){
15527c478bd9Sstevel@tonic-gate     ep = list;
15537c478bd9Sstevel@tonic-gate     list = NEXT(list);
15547c478bd9Sstevel@tonic-gate     NEXT(ep) = 0;
15557c478bd9Sstevel@tonic-gate     for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
15567c478bd9Sstevel@tonic-gate       ep = merge(ep,set[i],cmp,offset);
15577c478bd9Sstevel@tonic-gate       set[i] = 0;
15587c478bd9Sstevel@tonic-gate     }
15597c478bd9Sstevel@tonic-gate     set[i] = ep;
15607c478bd9Sstevel@tonic-gate   }
15617c478bd9Sstevel@tonic-gate   ep = 0;
15627c478bd9Sstevel@tonic-gate   for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(ep,set[i],cmp,offset);
15637c478bd9Sstevel@tonic-gate   return ep;
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate /************************ From the file "option.c" **************************/
15667c478bd9Sstevel@tonic-gate static char **argv;
15677c478bd9Sstevel@tonic-gate static struct s_options *op;
15687c478bd9Sstevel@tonic-gate static FILE *errstream;
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate /*
15737c478bd9Sstevel@tonic-gate ** Print the command line with a carrot pointing to the k-th character
15747c478bd9Sstevel@tonic-gate ** of the n-th field.
15757c478bd9Sstevel@tonic-gate */
errline(n,k,err)15767c478bd9Sstevel@tonic-gate static void errline(n,k,err)
15777c478bd9Sstevel@tonic-gate int n;
15787c478bd9Sstevel@tonic-gate int k;
15797c478bd9Sstevel@tonic-gate FILE *err;
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate   int spcnt, i;
15827c478bd9Sstevel@tonic-gate   spcnt = 0;
15837c478bd9Sstevel@tonic-gate   if( argv[0] ) fprintf(err,"%s",argv[0]);
15847c478bd9Sstevel@tonic-gate   spcnt = strlen(argv[0]) + 1;
15857c478bd9Sstevel@tonic-gate   for(i=1; i<n && argv[i]; i++){
15867c478bd9Sstevel@tonic-gate     fprintf(err," %s",argv[i]);
15877c478bd9Sstevel@tonic-gate     spcnt += strlen(argv[i]+1);
15887c478bd9Sstevel@tonic-gate   }
15897c478bd9Sstevel@tonic-gate   spcnt += k;
15907c478bd9Sstevel@tonic-gate   for(; argv[i]; i++) fprintf(err," %s",argv[i]);
15917c478bd9Sstevel@tonic-gate   if( spcnt<20 ){
15927c478bd9Sstevel@tonic-gate     fprintf(err,"\n%*s^-- here\n",spcnt,"");
15937c478bd9Sstevel@tonic-gate   }else{
15947c478bd9Sstevel@tonic-gate     fprintf(err,"\n%*shere --^\n",spcnt-7,"");
15957c478bd9Sstevel@tonic-gate   }
15967c478bd9Sstevel@tonic-gate }
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate /*
15997c478bd9Sstevel@tonic-gate ** Return the index of the N-th non-switch argument.  Return -1
16007c478bd9Sstevel@tonic-gate ** if N is out of range.
16017c478bd9Sstevel@tonic-gate */
argindex(n)16027c478bd9Sstevel@tonic-gate static int argindex(n)
16037c478bd9Sstevel@tonic-gate int n;
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate   int i;
16067c478bd9Sstevel@tonic-gate   int dashdash = 0;
16077c478bd9Sstevel@tonic-gate   if( argv!=0 && *argv!=0 ){
16087c478bd9Sstevel@tonic-gate     for(i=1; argv[i]; i++){
16097c478bd9Sstevel@tonic-gate       if( dashdash || !ISOPT(argv[i]) ){
16107c478bd9Sstevel@tonic-gate         if( n==0 ) return i;
16117c478bd9Sstevel@tonic-gate         n--;
16127c478bd9Sstevel@tonic-gate       }
16137c478bd9Sstevel@tonic-gate       if( strcmp(argv[i],"--")==0 ) dashdash = 1;
16147c478bd9Sstevel@tonic-gate     }
16157c478bd9Sstevel@tonic-gate   }
16167c478bd9Sstevel@tonic-gate   return -1;
16177c478bd9Sstevel@tonic-gate }
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate static char emsg[] = "Command line syntax error: ";
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate /*
16227c478bd9Sstevel@tonic-gate ** Process a flag command line argument.
16237c478bd9Sstevel@tonic-gate */
handleflags(i,err)16247c478bd9Sstevel@tonic-gate static int handleflags(i,err)
16257c478bd9Sstevel@tonic-gate int i;
16267c478bd9Sstevel@tonic-gate FILE *err;
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate   int v;
16297c478bd9Sstevel@tonic-gate   int errcnt = 0;
16307c478bd9Sstevel@tonic-gate   int j;
16317c478bd9Sstevel@tonic-gate   for(j=0; op[j].label; j++){
16327c478bd9Sstevel@tonic-gate     if( strcmp(&argv[i][1],op[j].label)==0 ) break;
16337c478bd9Sstevel@tonic-gate   }
16347c478bd9Sstevel@tonic-gate   v = argv[i][0]=='-' ? 1 : 0;
16357c478bd9Sstevel@tonic-gate   if( op[j].label==0 ){
16367c478bd9Sstevel@tonic-gate     if( err ){
16377c478bd9Sstevel@tonic-gate       fprintf(err,"%sundefined option.\n",emsg);
16387c478bd9Sstevel@tonic-gate       errline(i,1,err);
16397c478bd9Sstevel@tonic-gate     }
16407c478bd9Sstevel@tonic-gate     errcnt++;
16417c478bd9Sstevel@tonic-gate   }else if( op[j].type==OPT_FLAG ){
16427c478bd9Sstevel@tonic-gate     *((int*)op[j].arg) = v;
16437c478bd9Sstevel@tonic-gate   }else if( op[j].type==OPT_FFLAG ){
16447c478bd9Sstevel@tonic-gate     (*(void(*)())(op[j].arg))(v);
16457c478bd9Sstevel@tonic-gate   }else{
16467c478bd9Sstevel@tonic-gate     if( err ){
16477c478bd9Sstevel@tonic-gate       fprintf(err,"%smissing argument on switch.\n",emsg);
16487c478bd9Sstevel@tonic-gate       errline(i,1,err);
16497c478bd9Sstevel@tonic-gate     }
16507c478bd9Sstevel@tonic-gate     errcnt++;
16517c478bd9Sstevel@tonic-gate   }
16527c478bd9Sstevel@tonic-gate   return errcnt;
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate /*
16567c478bd9Sstevel@tonic-gate ** Process a command line switch which has an argument.
16577c478bd9Sstevel@tonic-gate */
handleswitch(i,err)16587c478bd9Sstevel@tonic-gate static int handleswitch(i,err)
16597c478bd9Sstevel@tonic-gate int i;
16607c478bd9Sstevel@tonic-gate FILE *err;
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate   int lv = 0;
16637c478bd9Sstevel@tonic-gate   double dv = 0.0;
16647c478bd9Sstevel@tonic-gate   char *sv = 0, *end;
16657c478bd9Sstevel@tonic-gate   char *cp;
16667c478bd9Sstevel@tonic-gate   int j;
16677c478bd9Sstevel@tonic-gate   int errcnt = 0;
16687c478bd9Sstevel@tonic-gate   cp = strchr(argv[i],'=');
16697c478bd9Sstevel@tonic-gate   *cp = 0;
16707c478bd9Sstevel@tonic-gate   for(j=0; op[j].label; j++){
16717c478bd9Sstevel@tonic-gate     if( strcmp(argv[i],op[j].label)==0 ) break;
16727c478bd9Sstevel@tonic-gate   }
16737c478bd9Sstevel@tonic-gate   *cp = '=';
16747c478bd9Sstevel@tonic-gate   if( op[j].label==0 ){
16757c478bd9Sstevel@tonic-gate     if( err ){
16767c478bd9Sstevel@tonic-gate       fprintf(err,"%sundefined option.\n",emsg);
16777c478bd9Sstevel@tonic-gate       errline(i,0,err);
16787c478bd9Sstevel@tonic-gate     }
16797c478bd9Sstevel@tonic-gate     errcnt++;
16807c478bd9Sstevel@tonic-gate   }else{
16817c478bd9Sstevel@tonic-gate     cp++;
16827c478bd9Sstevel@tonic-gate     switch( op[j].type ){
16837c478bd9Sstevel@tonic-gate       case OPT_FLAG:
16847c478bd9Sstevel@tonic-gate       case OPT_FFLAG:
16857c478bd9Sstevel@tonic-gate         if( err ){
16867c478bd9Sstevel@tonic-gate           fprintf(err,"%soption requires an argument.\n",emsg);
16877c478bd9Sstevel@tonic-gate           errline(i,0,err);
16887c478bd9Sstevel@tonic-gate         }
16897c478bd9Sstevel@tonic-gate         errcnt++;
16907c478bd9Sstevel@tonic-gate         break;
16917c478bd9Sstevel@tonic-gate       case OPT_DBL:
16927c478bd9Sstevel@tonic-gate       case OPT_FDBL:
16937c478bd9Sstevel@tonic-gate         dv = strtod(cp,&end);
16947c478bd9Sstevel@tonic-gate         if( *end ){
16957c478bd9Sstevel@tonic-gate           if( err ){
16967c478bd9Sstevel@tonic-gate             fprintf(err,"%sillegal character in floating-point argument.\n",emsg);
16977c478bd9Sstevel@tonic-gate             errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
16987c478bd9Sstevel@tonic-gate           }
16997c478bd9Sstevel@tonic-gate           errcnt++;
17007c478bd9Sstevel@tonic-gate         }
17017c478bd9Sstevel@tonic-gate         break;
17027c478bd9Sstevel@tonic-gate       case OPT_INT:
17037c478bd9Sstevel@tonic-gate       case OPT_FINT:
17047c478bd9Sstevel@tonic-gate         lv = strtol(cp,&end,0);
17057c478bd9Sstevel@tonic-gate         if( *end ){
17067c478bd9Sstevel@tonic-gate           if( err ){
17077c478bd9Sstevel@tonic-gate             fprintf(err,"%sillegal character in integer argument.\n",emsg);
17087c478bd9Sstevel@tonic-gate             errline(i,((unsigned long)end)-(unsigned long)argv[i],err);
17097c478bd9Sstevel@tonic-gate           }
17107c478bd9Sstevel@tonic-gate           errcnt++;
17117c478bd9Sstevel@tonic-gate         }
17127c478bd9Sstevel@tonic-gate         break;
17137c478bd9Sstevel@tonic-gate       case OPT_STR:
17147c478bd9Sstevel@tonic-gate       case OPT_FSTR:
17157c478bd9Sstevel@tonic-gate         sv = cp;
17167c478bd9Sstevel@tonic-gate         break;
17177c478bd9Sstevel@tonic-gate     }
17187c478bd9Sstevel@tonic-gate     switch( op[j].type ){
17197c478bd9Sstevel@tonic-gate       case OPT_FLAG:
17207c478bd9Sstevel@tonic-gate       case OPT_FFLAG:
17217c478bd9Sstevel@tonic-gate         break;
17227c478bd9Sstevel@tonic-gate       case OPT_DBL:
17237c478bd9Sstevel@tonic-gate         *(double*)(op[j].arg) = dv;
17247c478bd9Sstevel@tonic-gate         break;
17257c478bd9Sstevel@tonic-gate       case OPT_FDBL:
17267c478bd9Sstevel@tonic-gate         (*(void(*)())(op[j].arg))(dv);
17277c478bd9Sstevel@tonic-gate         break;
17287c478bd9Sstevel@tonic-gate       case OPT_INT:
17297c478bd9Sstevel@tonic-gate         *(int*)(op[j].arg) = lv;
17307c478bd9Sstevel@tonic-gate         break;
17317c478bd9Sstevel@tonic-gate       case OPT_FINT:
17327c478bd9Sstevel@tonic-gate         (*(void(*)())(op[j].arg))((int)lv);
17337c478bd9Sstevel@tonic-gate         break;
17347c478bd9Sstevel@tonic-gate       case OPT_STR:
17357c478bd9Sstevel@tonic-gate         *(char**)(op[j].arg) = sv;
17367c478bd9Sstevel@tonic-gate         break;
17377c478bd9Sstevel@tonic-gate       case OPT_FSTR:
17387c478bd9Sstevel@tonic-gate         (*(void(*)())(op[j].arg))(sv);
17397c478bd9Sstevel@tonic-gate         break;
17407c478bd9Sstevel@tonic-gate     }
17417c478bd9Sstevel@tonic-gate   }
17427c478bd9Sstevel@tonic-gate   return errcnt;
17437c478bd9Sstevel@tonic-gate }
17447c478bd9Sstevel@tonic-gate 
OptInit(a,o,err)17457c478bd9Sstevel@tonic-gate int OptInit(a,o,err)
17467c478bd9Sstevel@tonic-gate char **a;
17477c478bd9Sstevel@tonic-gate struct s_options *o;
17487c478bd9Sstevel@tonic-gate FILE *err;
17497c478bd9Sstevel@tonic-gate {
17507c478bd9Sstevel@tonic-gate   int errcnt = 0;
17517c478bd9Sstevel@tonic-gate   argv = a;
17527c478bd9Sstevel@tonic-gate   op = o;
17537c478bd9Sstevel@tonic-gate   errstream = err;
17547c478bd9Sstevel@tonic-gate   if( argv && *argv && op ){
17557c478bd9Sstevel@tonic-gate     int i;
17567c478bd9Sstevel@tonic-gate     for(i=1; argv[i]; i++){
17577c478bd9Sstevel@tonic-gate       if( argv[i][0]=='+' || argv[i][0]=='-' ){
17587c478bd9Sstevel@tonic-gate         errcnt += handleflags(i,err);
17597c478bd9Sstevel@tonic-gate       }else if( strchr(argv[i],'=') ){
17607c478bd9Sstevel@tonic-gate         errcnt += handleswitch(i,err);
17617c478bd9Sstevel@tonic-gate       }
17627c478bd9Sstevel@tonic-gate     }
17637c478bd9Sstevel@tonic-gate   }
17647c478bd9Sstevel@tonic-gate   if( errcnt>0 ){
17657c478bd9Sstevel@tonic-gate     fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
17667c478bd9Sstevel@tonic-gate     OptPrint();
17677c478bd9Sstevel@tonic-gate     exit(1);
17687c478bd9Sstevel@tonic-gate   }
17697c478bd9Sstevel@tonic-gate   return 0;
17707c478bd9Sstevel@tonic-gate }
17717c478bd9Sstevel@tonic-gate 
OptNArgs()17727c478bd9Sstevel@tonic-gate int OptNArgs(){
17737c478bd9Sstevel@tonic-gate   int cnt = 0;
17747c478bd9Sstevel@tonic-gate   int dashdash = 0;
17757c478bd9Sstevel@tonic-gate   int i;
17767c478bd9Sstevel@tonic-gate   if( argv!=0 && argv[0]!=0 ){
17777c478bd9Sstevel@tonic-gate     for(i=1; argv[i]; i++){
17787c478bd9Sstevel@tonic-gate       if( dashdash || !ISOPT(argv[i]) ) cnt++;
17797c478bd9Sstevel@tonic-gate       if( strcmp(argv[i],"--")==0 ) dashdash = 1;
17807c478bd9Sstevel@tonic-gate     }
17817c478bd9Sstevel@tonic-gate   }
17827c478bd9Sstevel@tonic-gate   return cnt;
17837c478bd9Sstevel@tonic-gate }
17847c478bd9Sstevel@tonic-gate 
OptArg(n)17857c478bd9Sstevel@tonic-gate char *OptArg(n)
17867c478bd9Sstevel@tonic-gate int n;
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate   int i;
17897c478bd9Sstevel@tonic-gate   i = argindex(n);
17907c478bd9Sstevel@tonic-gate   return i>=0 ? argv[i] : 0;
17917c478bd9Sstevel@tonic-gate }
17927c478bd9Sstevel@tonic-gate 
OptErr(n)17937c478bd9Sstevel@tonic-gate void OptErr(n)
17947c478bd9Sstevel@tonic-gate int n;
17957c478bd9Sstevel@tonic-gate {
17967c478bd9Sstevel@tonic-gate   int i;
17977c478bd9Sstevel@tonic-gate   i = argindex(n);
17987c478bd9Sstevel@tonic-gate   if( i>=0 ) errline(i,0,errstream);
17997c478bd9Sstevel@tonic-gate }
18007c478bd9Sstevel@tonic-gate 
OptPrint()18017c478bd9Sstevel@tonic-gate void OptPrint(){
18027c478bd9Sstevel@tonic-gate   int i;
18037c478bd9Sstevel@tonic-gate   int max, len;
18047c478bd9Sstevel@tonic-gate   max = 0;
18057c478bd9Sstevel@tonic-gate   for(i=0; op[i].label; i++){
18067c478bd9Sstevel@tonic-gate     len = strlen(op[i].label) + 1;
18077c478bd9Sstevel@tonic-gate     switch( op[i].type ){
18087c478bd9Sstevel@tonic-gate       case OPT_FLAG:
18097c478bd9Sstevel@tonic-gate       case OPT_FFLAG:
18107c478bd9Sstevel@tonic-gate         break;
18117c478bd9Sstevel@tonic-gate       case OPT_INT:
18127c478bd9Sstevel@tonic-gate       case OPT_FINT:
18137c478bd9Sstevel@tonic-gate         len += 9;       /* length of "<integer>" */
18147c478bd9Sstevel@tonic-gate         break;
18157c478bd9Sstevel@tonic-gate       case OPT_DBL:
18167c478bd9Sstevel@tonic-gate       case OPT_FDBL:
18177c478bd9Sstevel@tonic-gate         len += 6;       /* length of "<real>" */
18187c478bd9Sstevel@tonic-gate         break;
18197c478bd9Sstevel@tonic-gate       case OPT_STR:
18207c478bd9Sstevel@tonic-gate       case OPT_FSTR:
18217c478bd9Sstevel@tonic-gate         len += 8;       /* length of "<string>" */
18227c478bd9Sstevel@tonic-gate         break;
18237c478bd9Sstevel@tonic-gate     }
18247c478bd9Sstevel@tonic-gate     if( len>max ) max = len;
18257c478bd9Sstevel@tonic-gate   }
18267c478bd9Sstevel@tonic-gate   for(i=0; op[i].label; i++){
18277c478bd9Sstevel@tonic-gate     switch( op[i].type ){
18287c478bd9Sstevel@tonic-gate       case OPT_FLAG:
18297c478bd9Sstevel@tonic-gate       case OPT_FFLAG:
18307c478bd9Sstevel@tonic-gate         fprintf(errstream,"  -%-*s  %s\n",max,op[i].label,op[i].message);
18317c478bd9Sstevel@tonic-gate         break;
18327c478bd9Sstevel@tonic-gate       case OPT_INT:
18337c478bd9Sstevel@tonic-gate       case OPT_FINT:
18347c478bd9Sstevel@tonic-gate         fprintf(errstream,"  %s=<integer>%*s  %s\n",op[i].label,
18357c478bd9Sstevel@tonic-gate           (int)(max-strlen(op[i].label)-9),"",op[i].message);
18367c478bd9Sstevel@tonic-gate         break;
18377c478bd9Sstevel@tonic-gate       case OPT_DBL:
18387c478bd9Sstevel@tonic-gate       case OPT_FDBL:
18397c478bd9Sstevel@tonic-gate         fprintf(errstream,"  %s=<real>%*s  %s\n",op[i].label,
18407c478bd9Sstevel@tonic-gate           (int)(max-strlen(op[i].label)-6),"",op[i].message);
18417c478bd9Sstevel@tonic-gate         break;
18427c478bd9Sstevel@tonic-gate       case OPT_STR:
18437c478bd9Sstevel@tonic-gate       case OPT_FSTR:
18447c478bd9Sstevel@tonic-gate         fprintf(errstream,"  %s=<string>%*s  %s\n",op[i].label,
18457c478bd9Sstevel@tonic-gate           (int)(max-strlen(op[i].label)-8),"",op[i].message);
18467c478bd9Sstevel@tonic-gate         break;
18477c478bd9Sstevel@tonic-gate     }
18487c478bd9Sstevel@tonic-gate   }
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate /*********************** From the file "parse.c" ****************************/
18517c478bd9Sstevel@tonic-gate /*
18527c478bd9Sstevel@tonic-gate ** Input file parser for the LEMON parser generator.
18537c478bd9Sstevel@tonic-gate */
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate /* The state of the parser */
18567c478bd9Sstevel@tonic-gate struct pstate {
18577c478bd9Sstevel@tonic-gate   char *filename;       /* Name of the input file */
18587c478bd9Sstevel@tonic-gate   int tokenlineno;      /* Linenumber at which current token starts */
18597c478bd9Sstevel@tonic-gate   int errorcnt;         /* Number of errors so far */
18607c478bd9Sstevel@tonic-gate   char *tokenstart;     /* Text of current token */
18617c478bd9Sstevel@tonic-gate   struct lemon *gp;     /* Global state vector */
18627c478bd9Sstevel@tonic-gate   enum e_state {
18637c478bd9Sstevel@tonic-gate     INITIALIZE,
18647c478bd9Sstevel@tonic-gate     WAITING_FOR_DECL_OR_RULE,
18657c478bd9Sstevel@tonic-gate     WAITING_FOR_DECL_KEYWORD,
18667c478bd9Sstevel@tonic-gate     WAITING_FOR_DECL_ARG,
18677c478bd9Sstevel@tonic-gate     WAITING_FOR_PRECEDENCE_SYMBOL,
18687c478bd9Sstevel@tonic-gate     WAITING_FOR_ARROW,
18697c478bd9Sstevel@tonic-gate     IN_RHS,
18707c478bd9Sstevel@tonic-gate     LHS_ALIAS_1,
18717c478bd9Sstevel@tonic-gate     LHS_ALIAS_2,
18727c478bd9Sstevel@tonic-gate     LHS_ALIAS_3,
18737c478bd9Sstevel@tonic-gate     RHS_ALIAS_1,
18747c478bd9Sstevel@tonic-gate     RHS_ALIAS_2,
18757c478bd9Sstevel@tonic-gate     PRECEDENCE_MARK_1,
18767c478bd9Sstevel@tonic-gate     PRECEDENCE_MARK_2,
18777c478bd9Sstevel@tonic-gate     RESYNC_AFTER_RULE_ERROR,
18787c478bd9Sstevel@tonic-gate     RESYNC_AFTER_DECL_ERROR,
18797c478bd9Sstevel@tonic-gate     WAITING_FOR_DESTRUCTOR_SYMBOL,
18807c478bd9Sstevel@tonic-gate     WAITING_FOR_DATATYPE_SYMBOL,
18817c478bd9Sstevel@tonic-gate     WAITING_FOR_FALLBACK_ID
18827c478bd9Sstevel@tonic-gate   } state;                   /* The state of the parser */
18837c478bd9Sstevel@tonic-gate   struct symbol *fallback;   /* The fallback token */
18847c478bd9Sstevel@tonic-gate   struct symbol *lhs;        /* Left-hand side of current rule */
18857c478bd9Sstevel@tonic-gate   char *lhsalias;            /* Alias for the LHS */
18867c478bd9Sstevel@tonic-gate   int nrhs;                  /* Number of right-hand side symbols seen */
18877c478bd9Sstevel@tonic-gate   struct symbol *rhs[MAXRHS];  /* RHS symbols */
18887c478bd9Sstevel@tonic-gate   char *alias[MAXRHS];       /* Aliases for each RHS symbol (or NULL) */
18897c478bd9Sstevel@tonic-gate   struct rule *prevrule;     /* Previous rule parsed */
18907c478bd9Sstevel@tonic-gate   char *declkeyword;         /* Keyword of a declaration */
18917c478bd9Sstevel@tonic-gate   char **declargslot;        /* Where the declaration argument should be put */
18927c478bd9Sstevel@tonic-gate   int *decllnslot;           /* Where the declaration linenumber is put */
18937c478bd9Sstevel@tonic-gate   enum e_assoc declassoc;    /* Assign this association to decl arguments */
18947c478bd9Sstevel@tonic-gate   int preccounter;           /* Assign this precedence to decl arguments */
18957c478bd9Sstevel@tonic-gate   struct rule *firstrule;    /* Pointer to first rule in the grammar */
18967c478bd9Sstevel@tonic-gate   struct rule *lastrule;     /* Pointer to the most recently parsed rule */
18977c478bd9Sstevel@tonic-gate };
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate /* Parse a single token */
parseonetoken(psp)19007c478bd9Sstevel@tonic-gate static void parseonetoken(psp)
19017c478bd9Sstevel@tonic-gate struct pstate *psp;
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate   char *x;
19047c478bd9Sstevel@tonic-gate   x = Strsafe(psp->tokenstart);     /* Save the token permanently */
19057c478bd9Sstevel@tonic-gate #if 0
19067c478bd9Sstevel@tonic-gate   printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
19077c478bd9Sstevel@tonic-gate     x,psp->state);
19087c478bd9Sstevel@tonic-gate #endif
19097c478bd9Sstevel@tonic-gate   switch( psp->state ){
19107c478bd9Sstevel@tonic-gate     case INITIALIZE:
19117c478bd9Sstevel@tonic-gate       psp->prevrule = 0;
19127c478bd9Sstevel@tonic-gate       psp->preccounter = 0;
19137c478bd9Sstevel@tonic-gate       psp->firstrule = psp->lastrule = 0;
19147c478bd9Sstevel@tonic-gate       psp->gp->nrule = 0;
191588e1588bSToomas Soome       /* FALLTHROUGH */
19167c478bd9Sstevel@tonic-gate     case WAITING_FOR_DECL_OR_RULE:
19177c478bd9Sstevel@tonic-gate       if( x[0]=='%' ){
19187c478bd9Sstevel@tonic-gate         psp->state = WAITING_FOR_DECL_KEYWORD;
19197c478bd9Sstevel@tonic-gate       }else if( islower(x[0]) ){
19207c478bd9Sstevel@tonic-gate         psp->lhs = Symbol_new(x);
19217c478bd9Sstevel@tonic-gate         psp->nrhs = 0;
19227c478bd9Sstevel@tonic-gate         psp->lhsalias = 0;
19237c478bd9Sstevel@tonic-gate         psp->state = WAITING_FOR_ARROW;
19247c478bd9Sstevel@tonic-gate       }else if( x[0]=='{' ){
19257c478bd9Sstevel@tonic-gate         if( psp->prevrule==0 ){
19267c478bd9Sstevel@tonic-gate           ErrorMsg(psp->filename,psp->tokenlineno,
19277c478bd9Sstevel@tonic-gate "There is not prior rule opon which to attach the code \
19287c478bd9Sstevel@tonic-gate fragment which begins on this line.");
19297c478bd9Sstevel@tonic-gate           psp->errorcnt++;
19307c478bd9Sstevel@tonic-gate 	}else if( psp->prevrule->code!=0 ){
19317c478bd9Sstevel@tonic-gate           ErrorMsg(psp->filename,psp->tokenlineno,
19327c478bd9Sstevel@tonic-gate "Code fragment beginning on this line is not the first \
19337c478bd9Sstevel@tonic-gate to follow the previous rule.");
19347c478bd9Sstevel@tonic-gate           psp->errorcnt++;
19357c478bd9Sstevel@tonic-gate         }else{
19367c478bd9Sstevel@tonic-gate           psp->prevrule->line = psp->tokenlineno;
19377c478bd9Sstevel@tonic-gate           psp->prevrule->code = &x[1];
19387c478bd9Sstevel@tonic-gate 	}
19397c478bd9Sstevel@tonic-gate       }else if( x[0]=='[' ){
19407c478bd9Sstevel@tonic-gate         psp->state = PRECEDENCE_MARK_1;
19417c478bd9Sstevel@tonic-gate       }else{
19427c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
19437c478bd9Sstevel@tonic-gate           "Token \"%s\" should be either \"%%\" or a nonterminal name.",
19447c478bd9Sstevel@tonic-gate           x);
19457c478bd9Sstevel@tonic-gate         psp->errorcnt++;
19467c478bd9Sstevel@tonic-gate       }
19477c478bd9Sstevel@tonic-gate       break;
19487c478bd9Sstevel@tonic-gate     case PRECEDENCE_MARK_1:
19497c478bd9Sstevel@tonic-gate       if( !isupper(x[0]) ){
19507c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
19517c478bd9Sstevel@tonic-gate           "The precedence symbol must be a terminal.");
19527c478bd9Sstevel@tonic-gate         psp->errorcnt++;
19537c478bd9Sstevel@tonic-gate       }else if( psp->prevrule==0 ){
19547c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
19557c478bd9Sstevel@tonic-gate           "There is no prior rule to assign precedence \"[%s]\".",x);
19567c478bd9Sstevel@tonic-gate         psp->errorcnt++;
19577c478bd9Sstevel@tonic-gate       }else if( psp->prevrule->precsym!=0 ){
19587c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
19597c478bd9Sstevel@tonic-gate "Precedence mark on this line is not the first \
19607c478bd9Sstevel@tonic-gate to follow the previous rule.");
19617c478bd9Sstevel@tonic-gate         psp->errorcnt++;
19627c478bd9Sstevel@tonic-gate       }else{
19637c478bd9Sstevel@tonic-gate         psp->prevrule->precsym = Symbol_new(x);
19647c478bd9Sstevel@tonic-gate       }
19657c478bd9Sstevel@tonic-gate       psp->state = PRECEDENCE_MARK_2;
19667c478bd9Sstevel@tonic-gate       break;
19677c478bd9Sstevel@tonic-gate     case PRECEDENCE_MARK_2:
19687c478bd9Sstevel@tonic-gate       if( x[0]!=']' ){
19697c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
19707c478bd9Sstevel@tonic-gate           "Missing \"]\" on precedence mark.");
19717c478bd9Sstevel@tonic-gate         psp->errorcnt++;
19727c478bd9Sstevel@tonic-gate       }
19737c478bd9Sstevel@tonic-gate       psp->state = WAITING_FOR_DECL_OR_RULE;
19747c478bd9Sstevel@tonic-gate       break;
19757c478bd9Sstevel@tonic-gate     case WAITING_FOR_ARROW:
19767c478bd9Sstevel@tonic-gate       if( x[0]==':' && x[1]==':' && x[2]=='=' ){
19777c478bd9Sstevel@tonic-gate         psp->state = IN_RHS;
19787c478bd9Sstevel@tonic-gate       }else if( x[0]=='(' ){
19797c478bd9Sstevel@tonic-gate         psp->state = LHS_ALIAS_1;
19807c478bd9Sstevel@tonic-gate       }else{
19817c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
19827c478bd9Sstevel@tonic-gate           "Expected to see a \":\" following the LHS symbol \"%s\".",
19837c478bd9Sstevel@tonic-gate           psp->lhs->name);
19847c478bd9Sstevel@tonic-gate         psp->errorcnt++;
19857c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_RULE_ERROR;
19867c478bd9Sstevel@tonic-gate       }
19877c478bd9Sstevel@tonic-gate       break;
19887c478bd9Sstevel@tonic-gate     case LHS_ALIAS_1:
19897c478bd9Sstevel@tonic-gate       if( isalpha(x[0]) ){
19907c478bd9Sstevel@tonic-gate         psp->lhsalias = x;
19917c478bd9Sstevel@tonic-gate         psp->state = LHS_ALIAS_2;
19927c478bd9Sstevel@tonic-gate       }else{
19937c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
19947c478bd9Sstevel@tonic-gate           "\"%s\" is not a valid alias for the LHS \"%s\"\n",
19957c478bd9Sstevel@tonic-gate           x,psp->lhs->name);
19967c478bd9Sstevel@tonic-gate         psp->errorcnt++;
19977c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_RULE_ERROR;
19987c478bd9Sstevel@tonic-gate       }
19997c478bd9Sstevel@tonic-gate       break;
20007c478bd9Sstevel@tonic-gate     case LHS_ALIAS_2:
20017c478bd9Sstevel@tonic-gate       if( x[0]==')' ){
20027c478bd9Sstevel@tonic-gate         psp->state = LHS_ALIAS_3;
20037c478bd9Sstevel@tonic-gate       }else{
20047c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
20057c478bd9Sstevel@tonic-gate           "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
20067c478bd9Sstevel@tonic-gate         psp->errorcnt++;
20077c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_RULE_ERROR;
20087c478bd9Sstevel@tonic-gate       }
20097c478bd9Sstevel@tonic-gate       break;
20107c478bd9Sstevel@tonic-gate     case LHS_ALIAS_3:
20117c478bd9Sstevel@tonic-gate       if( x[0]==':' && x[1]==':' && x[2]=='=' ){
20127c478bd9Sstevel@tonic-gate         psp->state = IN_RHS;
20137c478bd9Sstevel@tonic-gate       }else{
20147c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
20157c478bd9Sstevel@tonic-gate           "Missing \"->\" following: \"%s(%s)\".",
20167c478bd9Sstevel@tonic-gate            psp->lhs->name,psp->lhsalias);
20177c478bd9Sstevel@tonic-gate         psp->errorcnt++;
20187c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_RULE_ERROR;
20197c478bd9Sstevel@tonic-gate       }
20207c478bd9Sstevel@tonic-gate       break;
20217c478bd9Sstevel@tonic-gate     case IN_RHS:
20227c478bd9Sstevel@tonic-gate       if( x[0]=='.' ){
20237c478bd9Sstevel@tonic-gate         struct rule *rp;
2024*55fea89dSDan Cross         rp = (struct rule *)malloc( sizeof(struct rule) +
20257c478bd9Sstevel@tonic-gate              sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs );
20267c478bd9Sstevel@tonic-gate         if( rp==0 ){
20277c478bd9Sstevel@tonic-gate           ErrorMsg(psp->filename,psp->tokenlineno,
20287c478bd9Sstevel@tonic-gate             "Can't allocate enough memory for this rule.");
20297c478bd9Sstevel@tonic-gate           psp->errorcnt++;
20307c478bd9Sstevel@tonic-gate           psp->prevrule = 0;
20317c478bd9Sstevel@tonic-gate 	}else{
20327c478bd9Sstevel@tonic-gate           int i;
20337c478bd9Sstevel@tonic-gate           rp->ruleline = psp->tokenlineno;
20347c478bd9Sstevel@tonic-gate           rp->rhs = (struct symbol**)&rp[1];
20357c478bd9Sstevel@tonic-gate           rp->rhsalias = (char**)&(rp->rhs[psp->nrhs]);
20367c478bd9Sstevel@tonic-gate           for(i=0; i<psp->nrhs; i++){
20377c478bd9Sstevel@tonic-gate             rp->rhs[i] = psp->rhs[i];
20387c478bd9Sstevel@tonic-gate             rp->rhsalias[i] = psp->alias[i];
20397c478bd9Sstevel@tonic-gate 	  }
20407c478bd9Sstevel@tonic-gate           rp->lhs = psp->lhs;
20417c478bd9Sstevel@tonic-gate           rp->lhsalias = psp->lhsalias;
20427c478bd9Sstevel@tonic-gate           rp->nrhs = psp->nrhs;
20437c478bd9Sstevel@tonic-gate           rp->code = 0;
20447c478bd9Sstevel@tonic-gate           rp->precsym = 0;
20457c478bd9Sstevel@tonic-gate           rp->index = psp->gp->nrule++;
20467c478bd9Sstevel@tonic-gate           rp->nextlhs = rp->lhs->rule;
20477c478bd9Sstevel@tonic-gate           rp->lhs->rule = rp;
20487c478bd9Sstevel@tonic-gate           rp->next = 0;
20497c478bd9Sstevel@tonic-gate           if( psp->firstrule==0 ){
20507c478bd9Sstevel@tonic-gate             psp->firstrule = psp->lastrule = rp;
20517c478bd9Sstevel@tonic-gate 	  }else{
20527c478bd9Sstevel@tonic-gate             psp->lastrule->next = rp;
20537c478bd9Sstevel@tonic-gate             psp->lastrule = rp;
20547c478bd9Sstevel@tonic-gate 	  }
20557c478bd9Sstevel@tonic-gate           psp->prevrule = rp;
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate         psp->state = WAITING_FOR_DECL_OR_RULE;
20587c478bd9Sstevel@tonic-gate       }else if( isalpha(x[0]) ){
20597c478bd9Sstevel@tonic-gate         if( psp->nrhs>=MAXRHS ){
20607c478bd9Sstevel@tonic-gate           ErrorMsg(psp->filename,psp->tokenlineno,
20617c478bd9Sstevel@tonic-gate             "Too many symbol on RHS or rule beginning at \"%s\".",
20627c478bd9Sstevel@tonic-gate             x);
20637c478bd9Sstevel@tonic-gate           psp->errorcnt++;
20647c478bd9Sstevel@tonic-gate           psp->state = RESYNC_AFTER_RULE_ERROR;
20657c478bd9Sstevel@tonic-gate 	}else{
20667c478bd9Sstevel@tonic-gate           psp->rhs[psp->nrhs] = Symbol_new(x);
20677c478bd9Sstevel@tonic-gate           psp->alias[psp->nrhs] = 0;
20687c478bd9Sstevel@tonic-gate           psp->nrhs++;
20697c478bd9Sstevel@tonic-gate 	}
20707c478bd9Sstevel@tonic-gate       }else if( x[0]=='(' && psp->nrhs>0 ){
20717c478bd9Sstevel@tonic-gate         psp->state = RHS_ALIAS_1;
20727c478bd9Sstevel@tonic-gate       }else{
20737c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
20747c478bd9Sstevel@tonic-gate           "Illegal character on RHS of rule: \"%s\".",x);
20757c478bd9Sstevel@tonic-gate         psp->errorcnt++;
20767c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_RULE_ERROR;
20777c478bd9Sstevel@tonic-gate       }
20787c478bd9Sstevel@tonic-gate       break;
20797c478bd9Sstevel@tonic-gate     case RHS_ALIAS_1:
20807c478bd9Sstevel@tonic-gate       if( isalpha(x[0]) ){
20817c478bd9Sstevel@tonic-gate         psp->alias[psp->nrhs-1] = x;
20827c478bd9Sstevel@tonic-gate         psp->state = RHS_ALIAS_2;
20837c478bd9Sstevel@tonic-gate       }else{
20847c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
20857c478bd9Sstevel@tonic-gate           "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
20867c478bd9Sstevel@tonic-gate           x,psp->rhs[psp->nrhs-1]->name);
20877c478bd9Sstevel@tonic-gate         psp->errorcnt++;
20887c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_RULE_ERROR;
20897c478bd9Sstevel@tonic-gate       }
20907c478bd9Sstevel@tonic-gate       break;
20917c478bd9Sstevel@tonic-gate     case RHS_ALIAS_2:
20927c478bd9Sstevel@tonic-gate       if( x[0]==')' ){
20937c478bd9Sstevel@tonic-gate         psp->state = IN_RHS;
20947c478bd9Sstevel@tonic-gate       }else{
20957c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
20967c478bd9Sstevel@tonic-gate           "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
20977c478bd9Sstevel@tonic-gate         psp->errorcnt++;
20987c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_RULE_ERROR;
20997c478bd9Sstevel@tonic-gate       }
21007c478bd9Sstevel@tonic-gate       break;
21017c478bd9Sstevel@tonic-gate     case WAITING_FOR_DECL_KEYWORD:
21027c478bd9Sstevel@tonic-gate       if( isalpha(x[0]) ){
21037c478bd9Sstevel@tonic-gate         psp->declkeyword = x;
21047c478bd9Sstevel@tonic-gate         psp->declargslot = 0;
21057c478bd9Sstevel@tonic-gate         psp->decllnslot = 0;
21067c478bd9Sstevel@tonic-gate         psp->state = WAITING_FOR_DECL_ARG;
21077c478bd9Sstevel@tonic-gate         if( strcmp(x,"name")==0 ){
21087c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->name);
21097c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"include")==0 ){
21107c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->include);
21117c478bd9Sstevel@tonic-gate           psp->decllnslot = &psp->gp->includeln;
21127c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"code")==0 ){
21137c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->extracode);
21147c478bd9Sstevel@tonic-gate           psp->decllnslot = &psp->gp->extracodeln;
21157c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"token_destructor")==0 ){
21167c478bd9Sstevel@tonic-gate           psp->declargslot = &psp->gp->tokendest;
21177c478bd9Sstevel@tonic-gate           psp->decllnslot = &psp->gp->tokendestln;
21187c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"default_destructor")==0 ){
21197c478bd9Sstevel@tonic-gate           psp->declargslot = &psp->gp->vardest;
21207c478bd9Sstevel@tonic-gate           psp->decllnslot = &psp->gp->vardestln;
21217c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"token_prefix")==0 ){
21227c478bd9Sstevel@tonic-gate           psp->declargslot = &psp->gp->tokenprefix;
21237c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"syntax_error")==0 ){
21247c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->error);
21257c478bd9Sstevel@tonic-gate           psp->decllnslot = &psp->gp->errorln;
21267c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"parse_accept")==0 ){
21277c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->accept);
21287c478bd9Sstevel@tonic-gate           psp->decllnslot = &psp->gp->acceptln;
21297c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"parse_failure")==0 ){
21307c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->failure);
21317c478bd9Sstevel@tonic-gate           psp->decllnslot = &psp->gp->failureln;
21327c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"stack_overflow")==0 ){
21337c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->overflow);
21347c478bd9Sstevel@tonic-gate           psp->decllnslot = &psp->gp->overflowln;
21357c478bd9Sstevel@tonic-gate         }else if( strcmp(x,"extra_argument")==0 ){
21367c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->arg);
21377c478bd9Sstevel@tonic-gate         }else if( strcmp(x,"token_type")==0 ){
21387c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->tokentype);
21397c478bd9Sstevel@tonic-gate         }else if( strcmp(x,"default_type")==0 ){
21407c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->vartype);
21417c478bd9Sstevel@tonic-gate         }else if( strcmp(x,"stack_size")==0 ){
21427c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->stacksize);
21437c478bd9Sstevel@tonic-gate         }else if( strcmp(x,"start_symbol")==0 ){
21447c478bd9Sstevel@tonic-gate           psp->declargslot = &(psp->gp->start);
21457c478bd9Sstevel@tonic-gate         }else if( strcmp(x,"left")==0 ){
21467c478bd9Sstevel@tonic-gate           psp->preccounter++;
21477c478bd9Sstevel@tonic-gate           psp->declassoc = LEFT;
21487c478bd9Sstevel@tonic-gate           psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
21497c478bd9Sstevel@tonic-gate         }else if( strcmp(x,"right")==0 ){
21507c478bd9Sstevel@tonic-gate           psp->preccounter++;
21517c478bd9Sstevel@tonic-gate           psp->declassoc = RIGHT;
21527c478bd9Sstevel@tonic-gate           psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
21537c478bd9Sstevel@tonic-gate         }else if( strcmp(x,"nonassoc")==0 ){
21547c478bd9Sstevel@tonic-gate           psp->preccounter++;
21557c478bd9Sstevel@tonic-gate           psp->declassoc = NONE;
21567c478bd9Sstevel@tonic-gate           psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
21577c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"destructor")==0 ){
21587c478bd9Sstevel@tonic-gate           psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
21597c478bd9Sstevel@tonic-gate 	}else if( strcmp(x,"type")==0 ){
21607c478bd9Sstevel@tonic-gate           psp->state = WAITING_FOR_DATATYPE_SYMBOL;
21617c478bd9Sstevel@tonic-gate         }else if( strcmp(x,"fallback")==0 ){
21627c478bd9Sstevel@tonic-gate           psp->fallback = 0;
21637c478bd9Sstevel@tonic-gate           psp->state = WAITING_FOR_FALLBACK_ID;
21647c478bd9Sstevel@tonic-gate         }else{
21657c478bd9Sstevel@tonic-gate           ErrorMsg(psp->filename,psp->tokenlineno,
21667c478bd9Sstevel@tonic-gate             "Unknown declaration keyword: \"%%%s\".",x);
21677c478bd9Sstevel@tonic-gate           psp->errorcnt++;
21687c478bd9Sstevel@tonic-gate           psp->state = RESYNC_AFTER_DECL_ERROR;
21697c478bd9Sstevel@tonic-gate 	}
21707c478bd9Sstevel@tonic-gate       }else{
21717c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
21727c478bd9Sstevel@tonic-gate           "Illegal declaration keyword: \"%s\".",x);
21737c478bd9Sstevel@tonic-gate         psp->errorcnt++;
21747c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_DECL_ERROR;
21757c478bd9Sstevel@tonic-gate       }
21767c478bd9Sstevel@tonic-gate       break;
21777c478bd9Sstevel@tonic-gate     case WAITING_FOR_DESTRUCTOR_SYMBOL:
21787c478bd9Sstevel@tonic-gate       if( !isalpha(x[0]) ){
21797c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
21807c478bd9Sstevel@tonic-gate           "Symbol name missing after %destructor keyword");
21817c478bd9Sstevel@tonic-gate         psp->errorcnt++;
21827c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_DECL_ERROR;
21837c478bd9Sstevel@tonic-gate       }else{
21847c478bd9Sstevel@tonic-gate         struct symbol *sp = Symbol_new(x);
21857c478bd9Sstevel@tonic-gate         psp->declargslot = &sp->destructor;
21867c478bd9Sstevel@tonic-gate         psp->decllnslot = &sp->destructorln;
21877c478bd9Sstevel@tonic-gate         psp->state = WAITING_FOR_DECL_ARG;
21887c478bd9Sstevel@tonic-gate       }
21897c478bd9Sstevel@tonic-gate       break;
21907c478bd9Sstevel@tonic-gate     case WAITING_FOR_DATATYPE_SYMBOL:
21917c478bd9Sstevel@tonic-gate       if( !isalpha(x[0]) ){
21927c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
21937c478bd9Sstevel@tonic-gate           "Symbol name missing after %destructor keyword");
21947c478bd9Sstevel@tonic-gate         psp->errorcnt++;
21957c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_DECL_ERROR;
21967c478bd9Sstevel@tonic-gate       }else{
21977c478bd9Sstevel@tonic-gate         struct symbol *sp = Symbol_new(x);
21987c478bd9Sstevel@tonic-gate         psp->declargslot = &sp->datatype;
21997c478bd9Sstevel@tonic-gate         psp->decllnslot = 0;
22007c478bd9Sstevel@tonic-gate         psp->state = WAITING_FOR_DECL_ARG;
22017c478bd9Sstevel@tonic-gate       }
22027c478bd9Sstevel@tonic-gate       break;
22037c478bd9Sstevel@tonic-gate     case WAITING_FOR_PRECEDENCE_SYMBOL:
22047c478bd9Sstevel@tonic-gate       if( x[0]=='.' ){
22057c478bd9Sstevel@tonic-gate         psp->state = WAITING_FOR_DECL_OR_RULE;
22067c478bd9Sstevel@tonic-gate       }else if( isupper(x[0]) ){
22077c478bd9Sstevel@tonic-gate         struct symbol *sp;
22087c478bd9Sstevel@tonic-gate         sp = Symbol_new(x);
22097c478bd9Sstevel@tonic-gate         if( sp->prec>=0 ){
22107c478bd9Sstevel@tonic-gate           ErrorMsg(psp->filename,psp->tokenlineno,
22117c478bd9Sstevel@tonic-gate             "Symbol \"%s\" has already be given a precedence.",x);
22127c478bd9Sstevel@tonic-gate           psp->errorcnt++;
22137c478bd9Sstevel@tonic-gate 	}else{
22147c478bd9Sstevel@tonic-gate           sp->prec = psp->preccounter;
22157c478bd9Sstevel@tonic-gate           sp->assoc = psp->declassoc;
22167c478bd9Sstevel@tonic-gate 	}
22177c478bd9Sstevel@tonic-gate       }else{
22187c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
22197c478bd9Sstevel@tonic-gate           "Can't assign a precedence to \"%s\".",x);
22207c478bd9Sstevel@tonic-gate         psp->errorcnt++;
22217c478bd9Sstevel@tonic-gate       }
22227c478bd9Sstevel@tonic-gate       break;
22237c478bd9Sstevel@tonic-gate     case WAITING_FOR_DECL_ARG:
22247c478bd9Sstevel@tonic-gate       if( (x[0]=='{' || x[0]=='\"' || isalnum(x[0])) ){
22257c478bd9Sstevel@tonic-gate         if( *(psp->declargslot)!=0 ){
22267c478bd9Sstevel@tonic-gate           ErrorMsg(psp->filename,psp->tokenlineno,
22277c478bd9Sstevel@tonic-gate             "The argument \"%s\" to declaration \"%%%s\" is not the first.",
22287c478bd9Sstevel@tonic-gate             x[0]=='\"' ? &x[1] : x,psp->declkeyword);
22297c478bd9Sstevel@tonic-gate           psp->errorcnt++;
22307c478bd9Sstevel@tonic-gate           psp->state = RESYNC_AFTER_DECL_ERROR;
22317c478bd9Sstevel@tonic-gate 	}else{
22327c478bd9Sstevel@tonic-gate           *(psp->declargslot) = (x[0]=='\"' || x[0]=='{') ? &x[1] : x;
22337c478bd9Sstevel@tonic-gate           if( psp->decllnslot ) *psp->decllnslot = psp->tokenlineno;
22347c478bd9Sstevel@tonic-gate           psp->state = WAITING_FOR_DECL_OR_RULE;
22357c478bd9Sstevel@tonic-gate 	}
22367c478bd9Sstevel@tonic-gate       }else{
22377c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename,psp->tokenlineno,
22387c478bd9Sstevel@tonic-gate           "Illegal argument to %%%s: %s",psp->declkeyword,x);
22397c478bd9Sstevel@tonic-gate         psp->errorcnt++;
22407c478bd9Sstevel@tonic-gate         psp->state = RESYNC_AFTER_DECL_ERROR;
22417c478bd9Sstevel@tonic-gate       }
22427c478bd9Sstevel@tonic-gate       break;
22437c478bd9Sstevel@tonic-gate     case WAITING_FOR_FALLBACK_ID:
22447c478bd9Sstevel@tonic-gate       if( x[0]=='.' ){
22457c478bd9Sstevel@tonic-gate         psp->state = WAITING_FOR_DECL_OR_RULE;
22467c478bd9Sstevel@tonic-gate       }else if( !isupper(x[0]) ){
22477c478bd9Sstevel@tonic-gate         ErrorMsg(psp->filename, psp->tokenlineno,
22487c478bd9Sstevel@tonic-gate           "%%fallback argument \"%s\" should be a token", x);
22497c478bd9Sstevel@tonic-gate         psp->errorcnt++;
22507c478bd9Sstevel@tonic-gate       }else{
22517c478bd9Sstevel@tonic-gate         struct symbol *sp = Symbol_new(x);
22527c478bd9Sstevel@tonic-gate         if( psp->fallback==0 ){
22537c478bd9Sstevel@tonic-gate           psp->fallback = sp;
22547c478bd9Sstevel@tonic-gate         }else if( sp->fallback ){
22557c478bd9Sstevel@tonic-gate           ErrorMsg(psp->filename, psp->tokenlineno,
22567c478bd9Sstevel@tonic-gate             "More than one fallback assigned to token %s", x);
22577c478bd9Sstevel@tonic-gate           psp->errorcnt++;
22587c478bd9Sstevel@tonic-gate         }else{
22597c478bd9Sstevel@tonic-gate           sp->fallback = psp->fallback;
22607c478bd9Sstevel@tonic-gate           psp->gp->has_fallback = 1;
22617c478bd9Sstevel@tonic-gate         }
22627c478bd9Sstevel@tonic-gate       }
22637c478bd9Sstevel@tonic-gate       break;
22647c478bd9Sstevel@tonic-gate     case RESYNC_AFTER_RULE_ERROR:
22657c478bd9Sstevel@tonic-gate /*      if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
22667c478bd9Sstevel@tonic-gate **      break; */
22677c478bd9Sstevel@tonic-gate     case RESYNC_AFTER_DECL_ERROR:
22687c478bd9Sstevel@tonic-gate       if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
22697c478bd9Sstevel@tonic-gate       if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
22707c478bd9Sstevel@tonic-gate       break;
22717c478bd9Sstevel@tonic-gate   }
22727c478bd9Sstevel@tonic-gate }
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate /* In spite of its name, this function is really a scanner.  It read
22757c478bd9Sstevel@tonic-gate ** in the entire input file (all at once) then tokenizes it.  Each
22767c478bd9Sstevel@tonic-gate ** token is passed to the function "parseonetoken" which builds all
22777c478bd9Sstevel@tonic-gate ** the appropriate data structures in the global state vector "gp".
22787c478bd9Sstevel@tonic-gate */
Parse(gp)22797c478bd9Sstevel@tonic-gate void Parse(gp)
22807c478bd9Sstevel@tonic-gate struct lemon *gp;
22817c478bd9Sstevel@tonic-gate {
22827c478bd9Sstevel@tonic-gate   struct pstate ps;
22837c478bd9Sstevel@tonic-gate   FILE *fp;
22847c478bd9Sstevel@tonic-gate   char *filebuf;
22857c478bd9Sstevel@tonic-gate   int filesize;
22867c478bd9Sstevel@tonic-gate   int lineno;
22877c478bd9Sstevel@tonic-gate   int c;
22887c478bd9Sstevel@tonic-gate   char *cp, *nextcp;
22897c478bd9Sstevel@tonic-gate   int startline = 0;
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate   ps.gp = gp;
22927c478bd9Sstevel@tonic-gate   ps.filename = gp->filename;
22937c478bd9Sstevel@tonic-gate   ps.errorcnt = 0;
22947c478bd9Sstevel@tonic-gate   ps.state = INITIALIZE;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate   /* Begin by reading the input file */
22977c478bd9Sstevel@tonic-gate   fp = fopen(ps.filename,"rb");
22987c478bd9Sstevel@tonic-gate   if( fp==0 ){
22997c478bd9Sstevel@tonic-gate     ErrorMsg(ps.filename,0,"Can't open this file for reading.");
23007c478bd9Sstevel@tonic-gate     gp->errorcnt++;
23017c478bd9Sstevel@tonic-gate     return;
23027c478bd9Sstevel@tonic-gate   }
23037c478bd9Sstevel@tonic-gate   fseek(fp,0,2);
23047c478bd9Sstevel@tonic-gate   filesize = ftell(fp);
23057c478bd9Sstevel@tonic-gate   rewind(fp);
23067c478bd9Sstevel@tonic-gate   filebuf = (char *)malloc( filesize+1 );
23077c478bd9Sstevel@tonic-gate   if( filebuf==0 ){
23087c478bd9Sstevel@tonic-gate     ErrorMsg(ps.filename,0,"Can't allocate %d of memory to hold this file.",
23097c478bd9Sstevel@tonic-gate       filesize+1);
23107c478bd9Sstevel@tonic-gate     gp->errorcnt++;
23117c478bd9Sstevel@tonic-gate     return;
23127c478bd9Sstevel@tonic-gate   }
23137c478bd9Sstevel@tonic-gate   if( fread(filebuf,1,filesize,fp)!=filesize ){
23147c478bd9Sstevel@tonic-gate     ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
23157c478bd9Sstevel@tonic-gate       filesize);
23167c478bd9Sstevel@tonic-gate     free(filebuf);
23177c478bd9Sstevel@tonic-gate     gp->errorcnt++;
23187c478bd9Sstevel@tonic-gate     return;
23197c478bd9Sstevel@tonic-gate   }
23207c478bd9Sstevel@tonic-gate   fclose(fp);
23217c478bd9Sstevel@tonic-gate   filebuf[filesize] = 0;
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate   /* Now scan the text of the input file */
23247c478bd9Sstevel@tonic-gate   lineno = 1;
23257c478bd9Sstevel@tonic-gate   for(cp=filebuf; (c= *cp)!=0; ){
23267c478bd9Sstevel@tonic-gate     if( c=='\n' ) lineno++;              /* Keep track of the line number */
23277c478bd9Sstevel@tonic-gate     if( isspace(c) ){ cp++; continue; }  /* Skip all white space */
23287c478bd9Sstevel@tonic-gate     if( c=='/' && cp[1]=='/' ){          /* Skip C++ style comments */
23297c478bd9Sstevel@tonic-gate       cp+=2;
23307c478bd9Sstevel@tonic-gate       while( (c= *cp)!=0 && c!='\n' ) cp++;
23317c478bd9Sstevel@tonic-gate       continue;
23327c478bd9Sstevel@tonic-gate     }
23337c478bd9Sstevel@tonic-gate     if( c=='/' && cp[1]=='*' ){          /* Skip C style comments */
23347c478bd9Sstevel@tonic-gate       cp+=2;
23357c478bd9Sstevel@tonic-gate       while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
23367c478bd9Sstevel@tonic-gate         if( c=='\n' ) lineno++;
23377c478bd9Sstevel@tonic-gate         cp++;
23387c478bd9Sstevel@tonic-gate       }
23397c478bd9Sstevel@tonic-gate       if( c ) cp++;
23407c478bd9Sstevel@tonic-gate       continue;
23417c478bd9Sstevel@tonic-gate     }
23427c478bd9Sstevel@tonic-gate     ps.tokenstart = cp;                /* Mark the beginning of the token */
23437c478bd9Sstevel@tonic-gate     ps.tokenlineno = lineno;           /* Linenumber on which token begins */
23447c478bd9Sstevel@tonic-gate     if( c=='\"' ){                     /* String literals */
23457c478bd9Sstevel@tonic-gate       cp++;
23467c478bd9Sstevel@tonic-gate       while( (c= *cp)!=0 && c!='\"' ){
23477c478bd9Sstevel@tonic-gate         if( c=='\n' ) lineno++;
23487c478bd9Sstevel@tonic-gate         cp++;
23497c478bd9Sstevel@tonic-gate       }
23507c478bd9Sstevel@tonic-gate       if( c==0 ){
23517c478bd9Sstevel@tonic-gate         ErrorMsg(ps.filename,startline,
23527c478bd9Sstevel@tonic-gate "String starting on this line is not terminated before the end of the file.");
23537c478bd9Sstevel@tonic-gate         ps.errorcnt++;
23547c478bd9Sstevel@tonic-gate         nextcp = cp;
23557c478bd9Sstevel@tonic-gate       }else{
23567c478bd9Sstevel@tonic-gate         nextcp = cp+1;
23577c478bd9Sstevel@tonic-gate       }
23587c478bd9Sstevel@tonic-gate     }else if( c=='{' ){               /* A block of C code */
23597c478bd9Sstevel@tonic-gate       int level;
23607c478bd9Sstevel@tonic-gate       cp++;
23617c478bd9Sstevel@tonic-gate       for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
23627c478bd9Sstevel@tonic-gate         if( c=='\n' ) lineno++;
23637c478bd9Sstevel@tonic-gate         else if( c=='{' ) level++;
23647c478bd9Sstevel@tonic-gate         else if( c=='}' ) level--;
23657c478bd9Sstevel@tonic-gate         else if( c=='/' && cp[1]=='*' ){  /* Skip comments */
23667c478bd9Sstevel@tonic-gate           int prevc;
23677c478bd9Sstevel@tonic-gate           cp = &cp[2];
23687c478bd9Sstevel@tonic-gate           prevc = 0;
23697c478bd9Sstevel@tonic-gate           while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
23707c478bd9Sstevel@tonic-gate             if( c=='\n' ) lineno++;
23717c478bd9Sstevel@tonic-gate             prevc = c;
23727c478bd9Sstevel@tonic-gate             cp++;
23737c478bd9Sstevel@tonic-gate 	  }
23747c478bd9Sstevel@tonic-gate 	}else if( c=='/' && cp[1]=='/' ){  /* Skip C++ style comments too */
23757c478bd9Sstevel@tonic-gate           cp = &cp[2];
23767c478bd9Sstevel@tonic-gate           while( (c= *cp)!=0 && c!='\n' ) cp++;
23777c478bd9Sstevel@tonic-gate           if( c ) lineno++;
23787c478bd9Sstevel@tonic-gate 	}else if( c=='\'' || c=='\"' ){    /* String a character literals */
23797c478bd9Sstevel@tonic-gate           int startchar, prevc;
23807c478bd9Sstevel@tonic-gate           startchar = c;
23817c478bd9Sstevel@tonic-gate           prevc = 0;
23827c478bd9Sstevel@tonic-gate           for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
23837c478bd9Sstevel@tonic-gate             if( c=='\n' ) lineno++;
23847c478bd9Sstevel@tonic-gate             if( prevc=='\\' ) prevc = 0;
23857c478bd9Sstevel@tonic-gate             else              prevc = c;
23867c478bd9Sstevel@tonic-gate 	  }
23877c478bd9Sstevel@tonic-gate 	}
23887c478bd9Sstevel@tonic-gate       }
23897c478bd9Sstevel@tonic-gate       if( c==0 ){
23907c478bd9Sstevel@tonic-gate         ErrorMsg(ps.filename,ps.tokenlineno,
23917c478bd9Sstevel@tonic-gate "C code starting on this line is not terminated before the end of the file.");
23927c478bd9Sstevel@tonic-gate         ps.errorcnt++;
23937c478bd9Sstevel@tonic-gate         nextcp = cp;
23947c478bd9Sstevel@tonic-gate       }else{
23957c478bd9Sstevel@tonic-gate         nextcp = cp+1;
23967c478bd9Sstevel@tonic-gate       }
23977c478bd9Sstevel@tonic-gate     }else if( isalnum(c) ){          /* Identifiers */
23987c478bd9Sstevel@tonic-gate       while( (c= *cp)!=0 && (isalnum(c) || c=='_') ) cp++;
23997c478bd9Sstevel@tonic-gate       nextcp = cp;
24007c478bd9Sstevel@tonic-gate     }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
24017c478bd9Sstevel@tonic-gate       cp += 3;
24027c478bd9Sstevel@tonic-gate       nextcp = cp;
24037c478bd9Sstevel@tonic-gate     }else{                          /* All other (one character) operators */
24047c478bd9Sstevel@tonic-gate       cp++;
24057c478bd9Sstevel@tonic-gate       nextcp = cp;
24067c478bd9Sstevel@tonic-gate     }
24077c478bd9Sstevel@tonic-gate     c = *cp;
24087c478bd9Sstevel@tonic-gate     *cp = 0;                        /* Null terminate the token */
24097c478bd9Sstevel@tonic-gate     parseonetoken(&ps);             /* Parse the token */
24107c478bd9Sstevel@tonic-gate     *cp = c;                        /* Restore the buffer */
24117c478bd9Sstevel@tonic-gate     cp = nextcp;
24127c478bd9Sstevel@tonic-gate   }
24137c478bd9Sstevel@tonic-gate   free(filebuf);                    /* Release the buffer after parsing */
24147c478bd9Sstevel@tonic-gate   gp->rule = ps.firstrule;
24157c478bd9Sstevel@tonic-gate   gp->errorcnt = ps.errorcnt;
24167c478bd9Sstevel@tonic-gate }
24177c478bd9Sstevel@tonic-gate /*************************** From the file "plink.c" *********************/
24187c478bd9Sstevel@tonic-gate /*
24197c478bd9Sstevel@tonic-gate ** Routines processing configuration follow-set propagation links
24207c478bd9Sstevel@tonic-gate ** in the LEMON parser generator.
24217c478bd9Sstevel@tonic-gate */
24227c478bd9Sstevel@tonic-gate static struct plink *plink_freelist = 0;
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate /* Allocate a new plink */
Plink_new()24257c478bd9Sstevel@tonic-gate struct plink *Plink_new(){
24267c478bd9Sstevel@tonic-gate   struct plink *new;
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate   if( plink_freelist==0 ){
24297c478bd9Sstevel@tonic-gate     int i;
24307c478bd9Sstevel@tonic-gate     int amt = 100;
24317c478bd9Sstevel@tonic-gate     plink_freelist = (struct plink *)malloc( sizeof(struct plink)*amt );
24327c478bd9Sstevel@tonic-gate     if( plink_freelist==0 ){
24337c478bd9Sstevel@tonic-gate       fprintf(stderr,
24347c478bd9Sstevel@tonic-gate       "Unable to allocate memory for a new follow-set propagation link.\n");
24357c478bd9Sstevel@tonic-gate       exit(1);
24367c478bd9Sstevel@tonic-gate     }
24377c478bd9Sstevel@tonic-gate     for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
24387c478bd9Sstevel@tonic-gate     plink_freelist[amt-1].next = 0;
24397c478bd9Sstevel@tonic-gate   }
24407c478bd9Sstevel@tonic-gate   new = plink_freelist;
24417c478bd9Sstevel@tonic-gate   plink_freelist = plink_freelist->next;
24427c478bd9Sstevel@tonic-gate   return new;
24437c478bd9Sstevel@tonic-gate }
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate /* Add a plink to a plink list */
Plink_add(plpp,cfp)24467c478bd9Sstevel@tonic-gate void Plink_add(plpp,cfp)
24477c478bd9Sstevel@tonic-gate struct plink **plpp;
24487c478bd9Sstevel@tonic-gate struct config *cfp;
24497c478bd9Sstevel@tonic-gate {
24507c478bd9Sstevel@tonic-gate   struct plink *new;
24517c478bd9Sstevel@tonic-gate   new = Plink_new();
24527c478bd9Sstevel@tonic-gate   new->next = *plpp;
24537c478bd9Sstevel@tonic-gate   *plpp = new;
24547c478bd9Sstevel@tonic-gate   new->cfp = cfp;
24557c478bd9Sstevel@tonic-gate }
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate /* Transfer every plink on the list "from" to the list "to" */
Plink_copy(to,from)24587c478bd9Sstevel@tonic-gate void Plink_copy(to,from)
24597c478bd9Sstevel@tonic-gate struct plink **to;
24607c478bd9Sstevel@tonic-gate struct plink *from;
24617c478bd9Sstevel@tonic-gate {
24627c478bd9Sstevel@tonic-gate   struct plink *nextpl;
24637c478bd9Sstevel@tonic-gate   while( from ){
24647c478bd9Sstevel@tonic-gate     nextpl = from->next;
24657c478bd9Sstevel@tonic-gate     from->next = *to;
24667c478bd9Sstevel@tonic-gate     *to = from;
24677c478bd9Sstevel@tonic-gate     from = nextpl;
24687c478bd9Sstevel@tonic-gate   }
24697c478bd9Sstevel@tonic-gate }
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate /* Delete every plink on the list */
Plink_delete(plp)24727c478bd9Sstevel@tonic-gate void Plink_delete(plp)
24737c478bd9Sstevel@tonic-gate struct plink *plp;
24747c478bd9Sstevel@tonic-gate {
24757c478bd9Sstevel@tonic-gate   struct plink *nextpl;
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate   while( plp ){
24787c478bd9Sstevel@tonic-gate     nextpl = plp->next;
24797c478bd9Sstevel@tonic-gate     plp->next = plink_freelist;
24807c478bd9Sstevel@tonic-gate     plink_freelist = plp;
24817c478bd9Sstevel@tonic-gate     plp = nextpl;
24827c478bd9Sstevel@tonic-gate   }
24837c478bd9Sstevel@tonic-gate }
24847c478bd9Sstevel@tonic-gate /*********************** From the file "report.c" **************************/
24857c478bd9Sstevel@tonic-gate /*
24867c478bd9Sstevel@tonic-gate ** Procedures for generating reports and tables in the LEMON parser generator.
24877c478bd9Sstevel@tonic-gate */
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate /* Generate a filename with the given suffix.  Space to hold the
24907c478bd9Sstevel@tonic-gate ** name comes from malloc() and must be freed by the calling
24917c478bd9Sstevel@tonic-gate ** function.
24927c478bd9Sstevel@tonic-gate */
file_makename(lemp,suffix)24937c478bd9Sstevel@tonic-gate PRIVATE char *file_makename(lemp,suffix)
24947c478bd9Sstevel@tonic-gate struct lemon *lemp;
24957c478bd9Sstevel@tonic-gate char *suffix;
24967c478bd9Sstevel@tonic-gate {
24977c478bd9Sstevel@tonic-gate   char *name;
24987c478bd9Sstevel@tonic-gate   char *cp;
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate   name = malloc( strlen(lemp->filename) + strlen(suffix) + 5 );
25017c478bd9Sstevel@tonic-gate   if( name==0 ){
25027c478bd9Sstevel@tonic-gate     fprintf(stderr,"Can't allocate space for a filename.\n");
25037c478bd9Sstevel@tonic-gate     exit(1);
25047c478bd9Sstevel@tonic-gate   }
25057c478bd9Sstevel@tonic-gate   strcpy(name,lemp->filename);
25067c478bd9Sstevel@tonic-gate   cp = strrchr(name,'.');
25077c478bd9Sstevel@tonic-gate   if( cp ) *cp = 0;
25087c478bd9Sstevel@tonic-gate   strcat(name,suffix);
25097c478bd9Sstevel@tonic-gate   return name;
25107c478bd9Sstevel@tonic-gate }
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate /* Open a file with a name based on the name of the input file,
25137c478bd9Sstevel@tonic-gate ** but with a different (specified) suffix, and return a pointer
25147c478bd9Sstevel@tonic-gate ** to the stream */
file_open(lemp,suffix,mode)25157c478bd9Sstevel@tonic-gate PRIVATE FILE *file_open(lemp,suffix,mode)
25167c478bd9Sstevel@tonic-gate struct lemon *lemp;
25177c478bd9Sstevel@tonic-gate char *suffix;
25187c478bd9Sstevel@tonic-gate char *mode;
25197c478bd9Sstevel@tonic-gate {
25207c478bd9Sstevel@tonic-gate   FILE *fp;
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate   if( lemp->outname ) free(lemp->outname);
25237c478bd9Sstevel@tonic-gate   lemp->outname = file_makename(lemp, suffix);
25247c478bd9Sstevel@tonic-gate   fp = fopen(lemp->outname,mode);
25257c478bd9Sstevel@tonic-gate   if( fp==0 && *mode=='w' ){
25267c478bd9Sstevel@tonic-gate     fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
25277c478bd9Sstevel@tonic-gate     lemp->errorcnt++;
25287c478bd9Sstevel@tonic-gate     return 0;
25297c478bd9Sstevel@tonic-gate   }
25307c478bd9Sstevel@tonic-gate   return fp;
25317c478bd9Sstevel@tonic-gate }
25327c478bd9Sstevel@tonic-gate 
2533*55fea89dSDan Cross /* Duplicate the input file without comments and without actions
25347c478bd9Sstevel@tonic-gate ** on rules */
Reprint(lemp)25357c478bd9Sstevel@tonic-gate void Reprint(lemp)
25367c478bd9Sstevel@tonic-gate struct lemon *lemp;
25377c478bd9Sstevel@tonic-gate {
25387c478bd9Sstevel@tonic-gate   struct rule *rp;
25397c478bd9Sstevel@tonic-gate   struct symbol *sp;
25407c478bd9Sstevel@tonic-gate   int i, j, maxlen, len, ncolumns, skip;
25417c478bd9Sstevel@tonic-gate   printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
25427c478bd9Sstevel@tonic-gate   maxlen = 10;
25437c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nsymbol; i++){
25447c478bd9Sstevel@tonic-gate     sp = lemp->symbols[i];
25457c478bd9Sstevel@tonic-gate     len = strlen(sp->name);
25467c478bd9Sstevel@tonic-gate     if( len>maxlen ) maxlen = len;
25477c478bd9Sstevel@tonic-gate   }
25487c478bd9Sstevel@tonic-gate   ncolumns = 76/(maxlen+5);
25497c478bd9Sstevel@tonic-gate   if( ncolumns<1 ) ncolumns = 1;
25507c478bd9Sstevel@tonic-gate   skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
25517c478bd9Sstevel@tonic-gate   for(i=0; i<skip; i++){
25527c478bd9Sstevel@tonic-gate     printf("//");
25537c478bd9Sstevel@tonic-gate     for(j=i; j<lemp->nsymbol; j+=skip){
25547c478bd9Sstevel@tonic-gate       sp = lemp->symbols[j];
25557c478bd9Sstevel@tonic-gate       assert( sp->index==j );
25567c478bd9Sstevel@tonic-gate       printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
25577c478bd9Sstevel@tonic-gate     }
25587c478bd9Sstevel@tonic-gate     printf("\n");
25597c478bd9Sstevel@tonic-gate   }
25607c478bd9Sstevel@tonic-gate   for(rp=lemp->rule; rp; rp=rp->next){
25617c478bd9Sstevel@tonic-gate     printf("%s",rp->lhs->name);
25627c478bd9Sstevel@tonic-gate /*    if( rp->lhsalias ) printf("(%s)",rp->lhsalias); */
25637c478bd9Sstevel@tonic-gate     printf(" ::=");
25647c478bd9Sstevel@tonic-gate     for(i=0; i<rp->nrhs; i++){
25657c478bd9Sstevel@tonic-gate       printf(" %s",rp->rhs[i]->name);
25667c478bd9Sstevel@tonic-gate /*      if( rp->rhsalias[i] ) printf("(%s)",rp->rhsalias[i]); */
25677c478bd9Sstevel@tonic-gate     }
25687c478bd9Sstevel@tonic-gate     printf(".");
25697c478bd9Sstevel@tonic-gate     if( rp->precsym ) printf(" [%s]",rp->precsym->name);
25707c478bd9Sstevel@tonic-gate /*    if( rp->code ) printf("\n    %s",rp->code); */
25717c478bd9Sstevel@tonic-gate     printf("\n");
25727c478bd9Sstevel@tonic-gate   }
25737c478bd9Sstevel@tonic-gate }
25747c478bd9Sstevel@tonic-gate 
ConfigPrint(fp,cfp)25757c478bd9Sstevel@tonic-gate void ConfigPrint(fp,cfp)
25767c478bd9Sstevel@tonic-gate FILE *fp;
25777c478bd9Sstevel@tonic-gate struct config *cfp;
25787c478bd9Sstevel@tonic-gate {
25797c478bd9Sstevel@tonic-gate   struct rule *rp;
25807c478bd9Sstevel@tonic-gate   int i;
25817c478bd9Sstevel@tonic-gate   rp = cfp->rp;
25827c478bd9Sstevel@tonic-gate   fprintf(fp,"%s ::=",rp->lhs->name);
25837c478bd9Sstevel@tonic-gate   for(i=0; i<=rp->nrhs; i++){
25847c478bd9Sstevel@tonic-gate     if( i==cfp->dot ) fprintf(fp," *");
25857c478bd9Sstevel@tonic-gate     if( i==rp->nrhs ) break;
25867c478bd9Sstevel@tonic-gate     fprintf(fp," %s",rp->rhs[i]->name);
25877c478bd9Sstevel@tonic-gate   }
25887c478bd9Sstevel@tonic-gate }
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate /* #define TEST */
25917c478bd9Sstevel@tonic-gate #ifdef TEST
25927c478bd9Sstevel@tonic-gate /* Print a set */
SetPrint(out,set,lemp)25937c478bd9Sstevel@tonic-gate PRIVATE void SetPrint(out,set,lemp)
25947c478bd9Sstevel@tonic-gate FILE *out;
25957c478bd9Sstevel@tonic-gate char *set;
25967c478bd9Sstevel@tonic-gate struct lemon *lemp;
25977c478bd9Sstevel@tonic-gate {
25987c478bd9Sstevel@tonic-gate   int i;
25997c478bd9Sstevel@tonic-gate   char *spacer;
26007c478bd9Sstevel@tonic-gate   spacer = "";
26017c478bd9Sstevel@tonic-gate   fprintf(out,"%12s[","");
26027c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nterminal; i++){
26037c478bd9Sstevel@tonic-gate     if( SetFind(set,i) ){
26047c478bd9Sstevel@tonic-gate       fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
26057c478bd9Sstevel@tonic-gate       spacer = " ";
26067c478bd9Sstevel@tonic-gate     }
26077c478bd9Sstevel@tonic-gate   }
26087c478bd9Sstevel@tonic-gate   fprintf(out,"]\n");
26097c478bd9Sstevel@tonic-gate }
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate /* Print a plink chain */
PlinkPrint(out,plp,tag)26127c478bd9Sstevel@tonic-gate PRIVATE void PlinkPrint(out,plp,tag)
26137c478bd9Sstevel@tonic-gate FILE *out;
26147c478bd9Sstevel@tonic-gate struct plink *plp;
26157c478bd9Sstevel@tonic-gate char *tag;
26167c478bd9Sstevel@tonic-gate {
26177c478bd9Sstevel@tonic-gate   while( plp ){
26187c478bd9Sstevel@tonic-gate     fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->index);
26197c478bd9Sstevel@tonic-gate     ConfigPrint(out,plp->cfp);
26207c478bd9Sstevel@tonic-gate     fprintf(out,"\n");
26217c478bd9Sstevel@tonic-gate     plp = plp->next;
26227c478bd9Sstevel@tonic-gate   }
26237c478bd9Sstevel@tonic-gate }
26247c478bd9Sstevel@tonic-gate #endif
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate /* Print an action to the given file descriptor.  Return FALSE if
26277c478bd9Sstevel@tonic-gate ** nothing was actually printed.
26287c478bd9Sstevel@tonic-gate */
PrintAction(struct action * ap,FILE * fp,int indent)26297c478bd9Sstevel@tonic-gate int PrintAction(struct action *ap, FILE *fp, int indent){
26307c478bd9Sstevel@tonic-gate   int result = 1;
26317c478bd9Sstevel@tonic-gate   switch( ap->type ){
26327c478bd9Sstevel@tonic-gate     case SHIFT:
26337c478bd9Sstevel@tonic-gate       fprintf(fp,"%*s shift  %d",indent,ap->sp->name,ap->x.stp->index);
26347c478bd9Sstevel@tonic-gate       break;
26357c478bd9Sstevel@tonic-gate     case REDUCE:
26367c478bd9Sstevel@tonic-gate       fprintf(fp,"%*s reduce %d",indent,ap->sp->name,ap->x.rp->index);
26377c478bd9Sstevel@tonic-gate       break;
26387c478bd9Sstevel@tonic-gate     case ACCEPT:
26397c478bd9Sstevel@tonic-gate       fprintf(fp,"%*s accept",indent,ap->sp->name);
26407c478bd9Sstevel@tonic-gate       break;
26417c478bd9Sstevel@tonic-gate     case ERROR:
26427c478bd9Sstevel@tonic-gate       fprintf(fp,"%*s error",indent,ap->sp->name);
26437c478bd9Sstevel@tonic-gate       break;
26447c478bd9Sstevel@tonic-gate     case CONFLICT:
26457c478bd9Sstevel@tonic-gate       fprintf(fp,"%*s reduce %-3d ** Parsing conflict **",
26467c478bd9Sstevel@tonic-gate         indent,ap->sp->name,ap->x.rp->index);
26477c478bd9Sstevel@tonic-gate       break;
26487c478bd9Sstevel@tonic-gate     case SH_RESOLVED:
26497c478bd9Sstevel@tonic-gate     case RD_RESOLVED:
26507c478bd9Sstevel@tonic-gate     case NOT_USED:
26517c478bd9Sstevel@tonic-gate       result = 0;
26527c478bd9Sstevel@tonic-gate       break;
26537c478bd9Sstevel@tonic-gate   }
26547c478bd9Sstevel@tonic-gate   return result;
26557c478bd9Sstevel@tonic-gate }
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate /* Generate the "y.output" log file */
ReportOutput(lemp)26587c478bd9Sstevel@tonic-gate void ReportOutput(lemp)
26597c478bd9Sstevel@tonic-gate struct lemon *lemp;
26607c478bd9Sstevel@tonic-gate {
26617c478bd9Sstevel@tonic-gate   int i;
26627c478bd9Sstevel@tonic-gate   struct state *stp;
26637c478bd9Sstevel@tonic-gate   struct config *cfp;
26647c478bd9Sstevel@tonic-gate   struct action *ap;
26657c478bd9Sstevel@tonic-gate   FILE *fp;
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate   fp = file_open(lemp,".out","w");
26687c478bd9Sstevel@tonic-gate   if( fp==0 ) return;
26697c478bd9Sstevel@tonic-gate   fprintf(fp," \b");
26707c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate; i++){
26717c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
26727c478bd9Sstevel@tonic-gate     fprintf(fp,"State %d:\n",stp->index);
26737c478bd9Sstevel@tonic-gate     if( lemp->basisflag ) cfp=stp->bp;
26747c478bd9Sstevel@tonic-gate     else                  cfp=stp->cfp;
26757c478bd9Sstevel@tonic-gate     while( cfp ){
26767c478bd9Sstevel@tonic-gate       char buf[20];
26777c478bd9Sstevel@tonic-gate       if( cfp->dot==cfp->rp->nrhs ){
26787c478bd9Sstevel@tonic-gate         sprintf(buf,"(%d)",cfp->rp->index);
26797c478bd9Sstevel@tonic-gate         fprintf(fp,"    %5s ",buf);
26807c478bd9Sstevel@tonic-gate       }else{
26817c478bd9Sstevel@tonic-gate         fprintf(fp,"          ");
26827c478bd9Sstevel@tonic-gate       }
26837c478bd9Sstevel@tonic-gate       ConfigPrint(fp,cfp);
26847c478bd9Sstevel@tonic-gate       fprintf(fp,"\n");
26857c478bd9Sstevel@tonic-gate #ifdef TEST
26867c478bd9Sstevel@tonic-gate       SetPrint(fp,cfp->fws,lemp);
26877c478bd9Sstevel@tonic-gate       PlinkPrint(fp,cfp->fplp,"To  ");
26887c478bd9Sstevel@tonic-gate       PlinkPrint(fp,cfp->bplp,"From");
26897c478bd9Sstevel@tonic-gate #endif
26907c478bd9Sstevel@tonic-gate       if( lemp->basisflag ) cfp=cfp->bp;
26917c478bd9Sstevel@tonic-gate       else                  cfp=cfp->next;
26927c478bd9Sstevel@tonic-gate     }
26937c478bd9Sstevel@tonic-gate     fprintf(fp,"\n");
26947c478bd9Sstevel@tonic-gate     for(ap=stp->ap; ap; ap=ap->next){
26957c478bd9Sstevel@tonic-gate       if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
26967c478bd9Sstevel@tonic-gate     }
26977c478bd9Sstevel@tonic-gate     fprintf(fp,"\n");
26987c478bd9Sstevel@tonic-gate   }
26997c478bd9Sstevel@tonic-gate   fclose(fp);
27007c478bd9Sstevel@tonic-gate   return;
27017c478bd9Sstevel@tonic-gate }
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate /* Search for the file "name" which is in the same directory as
27047c478bd9Sstevel@tonic-gate ** the exacutable */
pathsearch(argv0,name,modemask)27057c478bd9Sstevel@tonic-gate PRIVATE char *pathsearch(argv0,name,modemask)
27067c478bd9Sstevel@tonic-gate char *argv0;
27077c478bd9Sstevel@tonic-gate char *name;
27087c478bd9Sstevel@tonic-gate int modemask;
27097c478bd9Sstevel@tonic-gate {
27107c478bd9Sstevel@tonic-gate   char *pathlist;
27117c478bd9Sstevel@tonic-gate   char *path,*cp;
27127c478bd9Sstevel@tonic-gate   char c;
27137c478bd9Sstevel@tonic-gate   extern int access();
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate #ifdef __WIN32__
27167c478bd9Sstevel@tonic-gate   cp = strrchr(argv0,'\\');
27177c478bd9Sstevel@tonic-gate #else
27187c478bd9Sstevel@tonic-gate   cp = strrchr(argv0,'/');
27197c478bd9Sstevel@tonic-gate #endif
27207c478bd9Sstevel@tonic-gate   if( cp ){
27217c478bd9Sstevel@tonic-gate     c = *cp;
27227c478bd9Sstevel@tonic-gate     *cp = 0;
27237c478bd9Sstevel@tonic-gate     path = (char *)malloc( strlen(argv0) + strlen(name) + 2 );
27247c478bd9Sstevel@tonic-gate     if( path ) sprintf(path,"%s/%s",argv0,name);
27257c478bd9Sstevel@tonic-gate     *cp = c;
27267c478bd9Sstevel@tonic-gate   }else{
27277c478bd9Sstevel@tonic-gate     extern char *getenv();
27287c478bd9Sstevel@tonic-gate     pathlist = getenv("PATH");
27297c478bd9Sstevel@tonic-gate     if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
27307c478bd9Sstevel@tonic-gate     path = (char *)malloc( strlen(pathlist)+strlen(name)+2 );
27317c478bd9Sstevel@tonic-gate     if( path!=0 ){
27327c478bd9Sstevel@tonic-gate       while( *pathlist ){
27337c478bd9Sstevel@tonic-gate         cp = strchr(pathlist,':');
27347c478bd9Sstevel@tonic-gate         if( cp==0 ) cp = &pathlist[strlen(pathlist)];
27357c478bd9Sstevel@tonic-gate         c = *cp;
27367c478bd9Sstevel@tonic-gate         *cp = 0;
27377c478bd9Sstevel@tonic-gate         sprintf(path,"%s/%s",pathlist,name);
27387c478bd9Sstevel@tonic-gate         *cp = c;
27397c478bd9Sstevel@tonic-gate         if( c==0 ) pathlist = "";
27407c478bd9Sstevel@tonic-gate         else pathlist = &cp[1];
27417c478bd9Sstevel@tonic-gate         if( access(path,modemask)==0 ) break;
27427c478bd9Sstevel@tonic-gate       }
27437c478bd9Sstevel@tonic-gate     }
27447c478bd9Sstevel@tonic-gate   }
27457c478bd9Sstevel@tonic-gate   return path;
27467c478bd9Sstevel@tonic-gate }
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate /* Given an action, compute the integer value for that action
27497c478bd9Sstevel@tonic-gate ** which is to be put in the action table of the generated machine.
27507c478bd9Sstevel@tonic-gate ** Return negative if no action should be generated.
27517c478bd9Sstevel@tonic-gate */
compute_action(lemp,ap)27527c478bd9Sstevel@tonic-gate PRIVATE int compute_action(lemp,ap)
27537c478bd9Sstevel@tonic-gate struct lemon *lemp;
27547c478bd9Sstevel@tonic-gate struct action *ap;
27557c478bd9Sstevel@tonic-gate {
27567c478bd9Sstevel@tonic-gate   int act;
27577c478bd9Sstevel@tonic-gate   switch( ap->type ){
27587c478bd9Sstevel@tonic-gate     case SHIFT:  act = ap->x.stp->index;               break;
27597c478bd9Sstevel@tonic-gate     case REDUCE: act = ap->x.rp->index + lemp->nstate; break;
27607c478bd9Sstevel@tonic-gate     case ERROR:  act = lemp->nstate + lemp->nrule;     break;
27617c478bd9Sstevel@tonic-gate     case ACCEPT: act = lemp->nstate + lemp->nrule + 1; break;
27627c478bd9Sstevel@tonic-gate     default:     act = -1; break;
27637c478bd9Sstevel@tonic-gate   }
27647c478bd9Sstevel@tonic-gate   return act;
27657c478bd9Sstevel@tonic-gate }
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate #define LINESIZE 1000
27687c478bd9Sstevel@tonic-gate /* The next cluster of routines are for reading the template file
27697c478bd9Sstevel@tonic-gate ** and writing the results to the generated parser */
27707c478bd9Sstevel@tonic-gate /* The first function transfers data from "in" to "out" until
27717c478bd9Sstevel@tonic-gate ** a line is seen which begins with "%%".  The line number is
27727c478bd9Sstevel@tonic-gate ** tracked.
27737c478bd9Sstevel@tonic-gate **
27747c478bd9Sstevel@tonic-gate ** if name!=0, then any word that begin with "Parse" is changed to
27757c478bd9Sstevel@tonic-gate ** begin with *name instead.
27767c478bd9Sstevel@tonic-gate */
tplt_xfer(name,in,out,lineno)27777c478bd9Sstevel@tonic-gate PRIVATE void tplt_xfer(name,in,out,lineno)
27787c478bd9Sstevel@tonic-gate char *name;
27797c478bd9Sstevel@tonic-gate FILE *in;
27807c478bd9Sstevel@tonic-gate FILE *out;
27817c478bd9Sstevel@tonic-gate int *lineno;
27827c478bd9Sstevel@tonic-gate {
27837c478bd9Sstevel@tonic-gate   int i, iStart;
27847c478bd9Sstevel@tonic-gate   char line[LINESIZE];
27857c478bd9Sstevel@tonic-gate   while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
27867c478bd9Sstevel@tonic-gate     (*lineno)++;
27877c478bd9Sstevel@tonic-gate     iStart = 0;
27887c478bd9Sstevel@tonic-gate     if( name ){
27897c478bd9Sstevel@tonic-gate       for(i=0; line[i]; i++){
27907c478bd9Sstevel@tonic-gate         if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
27917c478bd9Sstevel@tonic-gate           && (i==0 || !isalpha(line[i-1]))
27927c478bd9Sstevel@tonic-gate         ){
27937c478bd9Sstevel@tonic-gate           if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
27947c478bd9Sstevel@tonic-gate           fprintf(out,"%s",name);
27957c478bd9Sstevel@tonic-gate           i += 4;
27967c478bd9Sstevel@tonic-gate           iStart = i+1;
27977c478bd9Sstevel@tonic-gate         }
27987c478bd9Sstevel@tonic-gate       }
27997c478bd9Sstevel@tonic-gate     }
28007c478bd9Sstevel@tonic-gate     fprintf(out,"%s",&line[iStart]);
28017c478bd9Sstevel@tonic-gate   }
28027c478bd9Sstevel@tonic-gate }
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate /* The next function finds the template file and opens it, returning
28057c478bd9Sstevel@tonic-gate ** a pointer to the opened file. */
tplt_open(lemp)28067c478bd9Sstevel@tonic-gate PRIVATE FILE *tplt_open(lemp)
28077c478bd9Sstevel@tonic-gate struct lemon *lemp;
28087c478bd9Sstevel@tonic-gate {
28097c478bd9Sstevel@tonic-gate   static char templatename[] = "lempar.c";
28107c478bd9Sstevel@tonic-gate   char buf[1000];
28117c478bd9Sstevel@tonic-gate   FILE *in;
28127c478bd9Sstevel@tonic-gate   char *tpltname;
28137c478bd9Sstevel@tonic-gate   char *cp;
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate   cp = strrchr(lemp->filename,'.');
28167c478bd9Sstevel@tonic-gate   if( cp ){
28177c478bd9Sstevel@tonic-gate     sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
28187c478bd9Sstevel@tonic-gate   }else{
28197c478bd9Sstevel@tonic-gate     sprintf(buf,"%s.lt",lemp->filename);
28207c478bd9Sstevel@tonic-gate   }
28217c478bd9Sstevel@tonic-gate   if( access(buf,004)==0 ){
28227c478bd9Sstevel@tonic-gate     tpltname = buf;
28237c478bd9Sstevel@tonic-gate   }else if( access(templatename,004)==0 ){
28247c478bd9Sstevel@tonic-gate     tpltname = templatename;
28257c478bd9Sstevel@tonic-gate   }else{
28267c478bd9Sstevel@tonic-gate     tpltname = pathsearch(lemp->argv0,templatename,0);
28277c478bd9Sstevel@tonic-gate   }
28287c478bd9Sstevel@tonic-gate   if( tpltname==0 ){
28297c478bd9Sstevel@tonic-gate     fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
28307c478bd9Sstevel@tonic-gate     templatename);
28317c478bd9Sstevel@tonic-gate     lemp->errorcnt++;
28327c478bd9Sstevel@tonic-gate     return 0;
28337c478bd9Sstevel@tonic-gate   }
28347c478bd9Sstevel@tonic-gate   in = fopen(tpltname,"r");
28357c478bd9Sstevel@tonic-gate   if( in==0 ){
28367c478bd9Sstevel@tonic-gate     fprintf(stderr,"Can't open the template file \"%s\".\n",templatename);
28377c478bd9Sstevel@tonic-gate     lemp->errorcnt++;
28387c478bd9Sstevel@tonic-gate     return 0;
28397c478bd9Sstevel@tonic-gate   }
28407c478bd9Sstevel@tonic-gate   return in;
28417c478bd9Sstevel@tonic-gate }
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate /* Print a string to the file and keep the linenumber up to date */
tplt_print(out,lemp,str,strln,lineno)28447c478bd9Sstevel@tonic-gate PRIVATE void tplt_print(out,lemp,str,strln,lineno)
28457c478bd9Sstevel@tonic-gate FILE *out;
28467c478bd9Sstevel@tonic-gate struct lemon *lemp;
28477c478bd9Sstevel@tonic-gate char *str;
28487c478bd9Sstevel@tonic-gate int strln;
28497c478bd9Sstevel@tonic-gate int *lineno;
28507c478bd9Sstevel@tonic-gate {
28517c478bd9Sstevel@tonic-gate   if( str==0 ) return;
28527c478bd9Sstevel@tonic-gate   fprintf(out,"#line %d \"%s\"\n",strln,lemp->filename); (*lineno)++;
28537c478bd9Sstevel@tonic-gate   while( *str ){
28547c478bd9Sstevel@tonic-gate     if( *str=='\n' ) (*lineno)++;
28557c478bd9Sstevel@tonic-gate     putc(*str,out);
28567c478bd9Sstevel@tonic-gate     str++;
28577c478bd9Sstevel@tonic-gate   }
28587c478bd9Sstevel@tonic-gate   fprintf(out,"\n#line %d \"%s\"\n",*lineno+2,lemp->outname); (*lineno)+=2;
28597c478bd9Sstevel@tonic-gate   return;
28607c478bd9Sstevel@tonic-gate }
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate /*
28637c478bd9Sstevel@tonic-gate ** The following routine emits code for the destructor for the
28647c478bd9Sstevel@tonic-gate ** symbol sp
28657c478bd9Sstevel@tonic-gate */
emit_destructor_code(out,sp,lemp,lineno)28667c478bd9Sstevel@tonic-gate void emit_destructor_code(out,sp,lemp,lineno)
28677c478bd9Sstevel@tonic-gate FILE *out;
28687c478bd9Sstevel@tonic-gate struct symbol *sp;
28697c478bd9Sstevel@tonic-gate struct lemon *lemp;
28707c478bd9Sstevel@tonic-gate int *lineno;
28717c478bd9Sstevel@tonic-gate {
28727c478bd9Sstevel@tonic-gate  char *cp = 0;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate  int linecnt = 0;
28757c478bd9Sstevel@tonic-gate  if( sp->type==TERMINAL ){
28767c478bd9Sstevel@tonic-gate    cp = lemp->tokendest;
28777c478bd9Sstevel@tonic-gate    if( cp==0 ) return;
28787c478bd9Sstevel@tonic-gate    fprintf(out,"#line %d \"%s\"\n{",lemp->tokendestln,lemp->filename);
28797c478bd9Sstevel@tonic-gate  }else if( sp->destructor ){
28807c478bd9Sstevel@tonic-gate    cp = sp->destructor;
28817c478bd9Sstevel@tonic-gate    fprintf(out,"#line %d \"%s\"\n{",sp->destructorln,lemp->filename);
28827c478bd9Sstevel@tonic-gate  }else if( lemp->vardest ){
28837c478bd9Sstevel@tonic-gate    cp = lemp->vardest;
28847c478bd9Sstevel@tonic-gate    if( cp==0 ) return;
28857c478bd9Sstevel@tonic-gate    fprintf(out,"#line %d \"%s\"\n{",lemp->vardestln,lemp->filename);
28867c478bd9Sstevel@tonic-gate  }else{
28877c478bd9Sstevel@tonic-gate    assert( 0 );  /* Cannot happen */
28887c478bd9Sstevel@tonic-gate  }
28897c478bd9Sstevel@tonic-gate  for(; *cp; cp++){
28907c478bd9Sstevel@tonic-gate    if( *cp=='$' && cp[1]=='$' ){
28917c478bd9Sstevel@tonic-gate      fprintf(out,"(yypminor->yy%d)",sp->dtnum);
28927c478bd9Sstevel@tonic-gate      cp++;
28937c478bd9Sstevel@tonic-gate      continue;
28947c478bd9Sstevel@tonic-gate    }
28957c478bd9Sstevel@tonic-gate    if( *cp=='\n' ) linecnt++;
28967c478bd9Sstevel@tonic-gate    fputc(*cp,out);
28977c478bd9Sstevel@tonic-gate  }
28987c478bd9Sstevel@tonic-gate  (*lineno) += 3 + linecnt;
28997c478bd9Sstevel@tonic-gate  fprintf(out,"}\n#line %d \"%s\"\n",*lineno,lemp->outname);
29007c478bd9Sstevel@tonic-gate  return;
29017c478bd9Sstevel@tonic-gate }
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate /*
29047c478bd9Sstevel@tonic-gate ** Return TRUE (non-zero) if the given symbol has a destructor.
29057c478bd9Sstevel@tonic-gate */
has_destructor(sp,lemp)29067c478bd9Sstevel@tonic-gate int has_destructor(sp, lemp)
29077c478bd9Sstevel@tonic-gate struct symbol *sp;
29087c478bd9Sstevel@tonic-gate struct lemon *lemp;
29097c478bd9Sstevel@tonic-gate {
29107c478bd9Sstevel@tonic-gate   int ret;
29117c478bd9Sstevel@tonic-gate   if( sp->type==TERMINAL ){
29127c478bd9Sstevel@tonic-gate     ret = lemp->tokendest!=0;
29137c478bd9Sstevel@tonic-gate   }else{
29147c478bd9Sstevel@tonic-gate     ret = lemp->vardest!=0 || sp->destructor!=0;
29157c478bd9Sstevel@tonic-gate   }
29167c478bd9Sstevel@tonic-gate   return ret;
29177c478bd9Sstevel@tonic-gate }
29187c478bd9Sstevel@tonic-gate 
2919*55fea89dSDan Cross /*
29207c478bd9Sstevel@tonic-gate ** Generate code which executes when the rule "rp" is reduced.  Write
29217c478bd9Sstevel@tonic-gate ** the code to "out".  Make sure lineno stays up-to-date.
29227c478bd9Sstevel@tonic-gate */
emit_code(out,rp,lemp,lineno)29237c478bd9Sstevel@tonic-gate PRIVATE void emit_code(out,rp,lemp,lineno)
29247c478bd9Sstevel@tonic-gate FILE *out;
29257c478bd9Sstevel@tonic-gate struct rule *rp;
29267c478bd9Sstevel@tonic-gate struct lemon *lemp;
29277c478bd9Sstevel@tonic-gate int *lineno;
29287c478bd9Sstevel@tonic-gate {
29297c478bd9Sstevel@tonic-gate  char *cp, *xp;
29307c478bd9Sstevel@tonic-gate  int linecnt = 0;
29317c478bd9Sstevel@tonic-gate  int i;
29327c478bd9Sstevel@tonic-gate  char lhsused = 0;    /* True if the LHS element has been used */
29337c478bd9Sstevel@tonic-gate  char used[MAXRHS];   /* True for each RHS element which is used */
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate  for(i=0; i<rp->nrhs; i++) used[i] = 0;
29367c478bd9Sstevel@tonic-gate  lhsused = 0;
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate  /* Generate code to do the reduce action */
29397c478bd9Sstevel@tonic-gate  if( rp->code ){
29407c478bd9Sstevel@tonic-gate    fprintf(out,"#line %d \"%s\"\n{",rp->line,lemp->filename);
29417c478bd9Sstevel@tonic-gate    for(cp=rp->code; *cp; cp++){
29427c478bd9Sstevel@tonic-gate      if( isalpha(*cp) && (cp==rp->code || (!isalnum(cp[-1]) && cp[-1]!='_')) ){
29437c478bd9Sstevel@tonic-gate        char saved;
29447c478bd9Sstevel@tonic-gate        for(xp= &cp[1]; isalnum(*xp) || *xp=='_'; xp++);
29457c478bd9Sstevel@tonic-gate        saved = *xp;
29467c478bd9Sstevel@tonic-gate        *xp = 0;
29477c478bd9Sstevel@tonic-gate        if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
29487c478bd9Sstevel@tonic-gate          fprintf(out,"yygotominor.yy%d",rp->lhs->dtnum);
29497c478bd9Sstevel@tonic-gate          cp = xp;
29507c478bd9Sstevel@tonic-gate          lhsused = 1;
29517c478bd9Sstevel@tonic-gate        }else{
29527c478bd9Sstevel@tonic-gate          for(i=0; i<rp->nrhs; i++){
29537c478bd9Sstevel@tonic-gate            if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
29547c478bd9Sstevel@tonic-gate              fprintf(out,"yymsp[%d].minor.yy%d",i-rp->nrhs+1,rp->rhs[i]->dtnum);
29557c478bd9Sstevel@tonic-gate              cp = xp;
29567c478bd9Sstevel@tonic-gate              used[i] = 1;
29577c478bd9Sstevel@tonic-gate              break;
29587c478bd9Sstevel@tonic-gate            }
29597c478bd9Sstevel@tonic-gate          }
29607c478bd9Sstevel@tonic-gate        }
29617c478bd9Sstevel@tonic-gate        *xp = saved;
29627c478bd9Sstevel@tonic-gate      }
29637c478bd9Sstevel@tonic-gate      if( *cp=='\n' ) linecnt++;
29647c478bd9Sstevel@tonic-gate      fputc(*cp,out);
29657c478bd9Sstevel@tonic-gate    } /* End loop */
29667c478bd9Sstevel@tonic-gate    (*lineno) += 3 + linecnt;
29677c478bd9Sstevel@tonic-gate    fprintf(out,"}\n#line %d \"%s\"\n",*lineno,lemp->outname);
29687c478bd9Sstevel@tonic-gate  } /* End if( rp->code ) */
29697c478bd9Sstevel@tonic-gate 
29707c478bd9Sstevel@tonic-gate  /* Check to make sure the LHS has been used */
29717c478bd9Sstevel@tonic-gate  if( rp->lhsalias && !lhsused ){
29727c478bd9Sstevel@tonic-gate    ErrorMsg(lemp->filename,rp->ruleline,
29737c478bd9Sstevel@tonic-gate      "Label \"%s\" for \"%s(%s)\" is never used.",
29747c478bd9Sstevel@tonic-gate        rp->lhsalias,rp->lhs->name,rp->lhsalias);
29757c478bd9Sstevel@tonic-gate    lemp->errorcnt++;
29767c478bd9Sstevel@tonic-gate  }
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate  /* Generate destructor code for RHS symbols which are not used in the
29797c478bd9Sstevel@tonic-gate  ** reduce code */
29807c478bd9Sstevel@tonic-gate  for(i=0; i<rp->nrhs; i++){
29817c478bd9Sstevel@tonic-gate    if( rp->rhsalias[i] && !used[i] ){
29827c478bd9Sstevel@tonic-gate      ErrorMsg(lemp->filename,rp->ruleline,
29837c478bd9Sstevel@tonic-gate        "Label %s for \"%s(%s)\" is never used.",
29847c478bd9Sstevel@tonic-gate        rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
29857c478bd9Sstevel@tonic-gate      lemp->errorcnt++;
29867c478bd9Sstevel@tonic-gate    }else if( rp->rhsalias[i]==0 ){
29877c478bd9Sstevel@tonic-gate      if( has_destructor(rp->rhs[i],lemp) ){
29887c478bd9Sstevel@tonic-gate        fprintf(out,"  yy_destructor(%d,&yymsp[%d].minor);\n",
29897c478bd9Sstevel@tonic-gate           rp->rhs[i]->index,i-rp->nrhs+1); (*lineno)++;
29907c478bd9Sstevel@tonic-gate      }else{
29917c478bd9Sstevel@tonic-gate        fprintf(out,"        /* No destructor defined for %s */\n",
29927c478bd9Sstevel@tonic-gate         rp->rhs[i]->name);
29937c478bd9Sstevel@tonic-gate         (*lineno)++;
29947c478bd9Sstevel@tonic-gate      }
29957c478bd9Sstevel@tonic-gate    }
29967c478bd9Sstevel@tonic-gate  }
29977c478bd9Sstevel@tonic-gate  return;
29987c478bd9Sstevel@tonic-gate }
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate /*
30017c478bd9Sstevel@tonic-gate ** Print the definition of the union used for the parser's data stack.
30027c478bd9Sstevel@tonic-gate ** This union contains fields for every possible data type for tokens
30037c478bd9Sstevel@tonic-gate ** and nonterminals.  In the process of computing and printing this
30047c478bd9Sstevel@tonic-gate ** union, also set the ".dtnum" field of every terminal and nonterminal
30057c478bd9Sstevel@tonic-gate ** symbol.
30067c478bd9Sstevel@tonic-gate */
print_stack_union(out,lemp,plineno,mhflag)30077c478bd9Sstevel@tonic-gate void print_stack_union(out,lemp,plineno,mhflag)
30087c478bd9Sstevel@tonic-gate FILE *out;                  /* The output stream */
30097c478bd9Sstevel@tonic-gate struct lemon *lemp;         /* The main info structure for this parser */
30107c478bd9Sstevel@tonic-gate int *plineno;               /* Pointer to the line number */
30117c478bd9Sstevel@tonic-gate int mhflag;                 /* True if generating makeheaders output */
30127c478bd9Sstevel@tonic-gate {
30137c478bd9Sstevel@tonic-gate   int lineno = *plineno;    /* The line number of the output */
30147c478bd9Sstevel@tonic-gate   char **types;             /* A hash table of datatypes */
30157c478bd9Sstevel@tonic-gate   int arraysize;            /* Size of the "types" array */
30167c478bd9Sstevel@tonic-gate   int maxdtlength;          /* Maximum length of any ".datatype" field. */
30177c478bd9Sstevel@tonic-gate   char *stddt;              /* Standardized name for a datatype */
30187c478bd9Sstevel@tonic-gate   int i,j;                  /* Loop counters */
30197c478bd9Sstevel@tonic-gate   int hash;                 /* For hashing the name of a type */
30207c478bd9Sstevel@tonic-gate   char *name;               /* Name of the parser */
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate   /* Allocate and initialize types[] and allocate stddt[] */
30237c478bd9Sstevel@tonic-gate   arraysize = lemp->nsymbol * 2;
30247c478bd9Sstevel@tonic-gate   types = (char**)malloc( arraysize * sizeof(char*) );
30257c478bd9Sstevel@tonic-gate   for(i=0; i<arraysize; i++) types[i] = 0;
30267c478bd9Sstevel@tonic-gate   maxdtlength = 0;
30277c478bd9Sstevel@tonic-gate   if( lemp->vartype ){
30287c478bd9Sstevel@tonic-gate     maxdtlength = strlen(lemp->vartype);
30297c478bd9Sstevel@tonic-gate   }
30307c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nsymbol; i++){
30317c478bd9Sstevel@tonic-gate     int len;
30327c478bd9Sstevel@tonic-gate     struct symbol *sp = lemp->symbols[i];
30337c478bd9Sstevel@tonic-gate     if( sp->datatype==0 ) continue;
30347c478bd9Sstevel@tonic-gate     len = strlen(sp->datatype);
30357c478bd9Sstevel@tonic-gate     if( len>maxdtlength ) maxdtlength = len;
30367c478bd9Sstevel@tonic-gate   }
30377c478bd9Sstevel@tonic-gate   stddt = (char*)malloc( maxdtlength*2 + 1 );
30387c478bd9Sstevel@tonic-gate   if( types==0 || stddt==0 ){
30397c478bd9Sstevel@tonic-gate     fprintf(stderr,"Out of memory.\n");
30407c478bd9Sstevel@tonic-gate     exit(1);
30417c478bd9Sstevel@tonic-gate   }
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate   /* Build a hash table of datatypes. The ".dtnum" field of each symbol
30447c478bd9Sstevel@tonic-gate   ** is filled in with the hash index plus 1.  A ".dtnum" value of 0 is
30457c478bd9Sstevel@tonic-gate   ** used for terminal symbols.  If there is no %default_type defined then
30467c478bd9Sstevel@tonic-gate   ** 0 is also used as the .dtnum value for nonterminals which do not specify
30477c478bd9Sstevel@tonic-gate   ** a datatype using the %type directive.
30487c478bd9Sstevel@tonic-gate   */
30497c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nsymbol; i++){
30507c478bd9Sstevel@tonic-gate     struct symbol *sp = lemp->symbols[i];
30517c478bd9Sstevel@tonic-gate     char *cp;
30527c478bd9Sstevel@tonic-gate     if( sp==lemp->errsym ){
30537c478bd9Sstevel@tonic-gate       sp->dtnum = arraysize+1;
30547c478bd9Sstevel@tonic-gate       continue;
30557c478bd9Sstevel@tonic-gate     }
30567c478bd9Sstevel@tonic-gate     if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
30577c478bd9Sstevel@tonic-gate       sp->dtnum = 0;
30587c478bd9Sstevel@tonic-gate       continue;
30597c478bd9Sstevel@tonic-gate     }
30607c478bd9Sstevel@tonic-gate     cp = sp->datatype;
30617c478bd9Sstevel@tonic-gate     if( cp==0 ) cp = lemp->vartype;
30627c478bd9Sstevel@tonic-gate     j = 0;
30637c478bd9Sstevel@tonic-gate     while( isspace(*cp) ) cp++;
30647c478bd9Sstevel@tonic-gate     while( *cp ) stddt[j++] = *cp++;
30657c478bd9Sstevel@tonic-gate     while( j>0 && isspace(stddt[j-1]) ) j--;
30667c478bd9Sstevel@tonic-gate     stddt[j] = 0;
30677c478bd9Sstevel@tonic-gate     hash = 0;
30687c478bd9Sstevel@tonic-gate     for(j=0; stddt[j]; j++){
30697c478bd9Sstevel@tonic-gate       hash = hash*53 + stddt[j];
30707c478bd9Sstevel@tonic-gate     }
30717c478bd9Sstevel@tonic-gate     hash = (hash & 0x7fffffff)%arraysize;
30727c478bd9Sstevel@tonic-gate     while( types[hash] ){
30737c478bd9Sstevel@tonic-gate       if( strcmp(types[hash],stddt)==0 ){
30747c478bd9Sstevel@tonic-gate         sp->dtnum = hash + 1;
30757c478bd9Sstevel@tonic-gate         break;
30767c478bd9Sstevel@tonic-gate       }
30777c478bd9Sstevel@tonic-gate       hash++;
30787c478bd9Sstevel@tonic-gate       if( hash>=arraysize ) hash = 0;
30797c478bd9Sstevel@tonic-gate     }
30807c478bd9Sstevel@tonic-gate     if( types[hash]==0 ){
30817c478bd9Sstevel@tonic-gate       sp->dtnum = hash + 1;
30827c478bd9Sstevel@tonic-gate       types[hash] = (char*)malloc( strlen(stddt)+1 );
30837c478bd9Sstevel@tonic-gate       if( types[hash]==0 ){
30847c478bd9Sstevel@tonic-gate         fprintf(stderr,"Out of memory.\n");
30857c478bd9Sstevel@tonic-gate         exit(1);
30867c478bd9Sstevel@tonic-gate       }
30877c478bd9Sstevel@tonic-gate       strcpy(types[hash],stddt);
30887c478bd9Sstevel@tonic-gate     }
30897c478bd9Sstevel@tonic-gate   }
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate   /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
30927c478bd9Sstevel@tonic-gate   name = lemp->name ? lemp->name : "Parse";
30937c478bd9Sstevel@tonic-gate   lineno = *plineno;
30947c478bd9Sstevel@tonic-gate   if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
30957c478bd9Sstevel@tonic-gate   fprintf(out,"#define %sTOKENTYPE %s\n",name,
30967c478bd9Sstevel@tonic-gate     lemp->tokentype?lemp->tokentype:"void*");  lineno++;
30977c478bd9Sstevel@tonic-gate   if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
30987c478bd9Sstevel@tonic-gate   fprintf(out,"typedef union {\n"); lineno++;
30997c478bd9Sstevel@tonic-gate   fprintf(out,"  %sTOKENTYPE yy0;\n",name); lineno++;
31007c478bd9Sstevel@tonic-gate   for(i=0; i<arraysize; i++){
31017c478bd9Sstevel@tonic-gate     if( types[i]==0 ) continue;
31027c478bd9Sstevel@tonic-gate     fprintf(out,"  %s yy%d;\n",types[i],i+1); lineno++;
31037c478bd9Sstevel@tonic-gate     free(types[i]);
31047c478bd9Sstevel@tonic-gate   }
31057c478bd9Sstevel@tonic-gate   fprintf(out,"  int yy%d;\n",lemp->errsym->dtnum); lineno++;
31067c478bd9Sstevel@tonic-gate   free(stddt);
31077c478bd9Sstevel@tonic-gate   free(types);
31087c478bd9Sstevel@tonic-gate   fprintf(out,"} YYMINORTYPE;\n"); lineno++;
31097c478bd9Sstevel@tonic-gate   *plineno = lineno;
31107c478bd9Sstevel@tonic-gate }
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate /*
31137c478bd9Sstevel@tonic-gate ** Return the name of a C datatype able to represent values between
31147c478bd9Sstevel@tonic-gate ** lwr and upr, inclusive.
31157c478bd9Sstevel@tonic-gate */
minimum_size_type(int lwr,int upr)31167c478bd9Sstevel@tonic-gate static const char *minimum_size_type(int lwr, int upr){
31177c478bd9Sstevel@tonic-gate   if( lwr>=0 ){
31187c478bd9Sstevel@tonic-gate     if( upr<=255 ){
31197c478bd9Sstevel@tonic-gate       return "unsigned char";
31207c478bd9Sstevel@tonic-gate     }else if( upr<65535 ){
31217c478bd9Sstevel@tonic-gate       return "unsigned short int";
31227c478bd9Sstevel@tonic-gate     }else{
31237c478bd9Sstevel@tonic-gate       return "unsigned int";
31247c478bd9Sstevel@tonic-gate     }
31257c478bd9Sstevel@tonic-gate   }else if( lwr>=-127 && upr<=127 ){
31267c478bd9Sstevel@tonic-gate     return "signed char";
31277c478bd9Sstevel@tonic-gate   }else if( lwr>=-32767 && upr<32767 ){
31287c478bd9Sstevel@tonic-gate     return "short";
31297c478bd9Sstevel@tonic-gate   }else{
31307c478bd9Sstevel@tonic-gate     return "int";
31317c478bd9Sstevel@tonic-gate   }
31327c478bd9Sstevel@tonic-gate }
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate /*
31357c478bd9Sstevel@tonic-gate ** Each state contains a set of token transaction and a set of
31367c478bd9Sstevel@tonic-gate ** nonterminal transactions.  Each of these sets makes an instance
31377c478bd9Sstevel@tonic-gate ** of the following structure.  An array of these structures is used
31387c478bd9Sstevel@tonic-gate ** to order the creation of entries in the yy_action[] table.
31397c478bd9Sstevel@tonic-gate */
31407c478bd9Sstevel@tonic-gate struct axset {
31417c478bd9Sstevel@tonic-gate   struct state *stp;   /* A pointer to a state */
31427c478bd9Sstevel@tonic-gate   int isTkn;           /* True to use tokens.  False for non-terminals */
31437c478bd9Sstevel@tonic-gate   int nAction;         /* Number of actions */
31447c478bd9Sstevel@tonic-gate };
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate /*
31477c478bd9Sstevel@tonic-gate ** Compare to axset structures for sorting purposes
31487c478bd9Sstevel@tonic-gate */
axset_compare(const void * a,const void * b)31497c478bd9Sstevel@tonic-gate static int axset_compare(const void *a, const void *b){
31507c478bd9Sstevel@tonic-gate   struct axset *p1 = (struct axset*)a;
31517c478bd9Sstevel@tonic-gate   struct axset *p2 = (struct axset*)b;
31527c478bd9Sstevel@tonic-gate   return p2->nAction - p1->nAction;
31537c478bd9Sstevel@tonic-gate }
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate /* Generate C source code for the parser */
ReportTable(lemp,mhflag)31567c478bd9Sstevel@tonic-gate void ReportTable(lemp, mhflag)
31577c478bd9Sstevel@tonic-gate struct lemon *lemp;
31587c478bd9Sstevel@tonic-gate int mhflag;     /* Output in makeheaders format if true */
31597c478bd9Sstevel@tonic-gate {
31607c478bd9Sstevel@tonic-gate   FILE *out, *in;
31617c478bd9Sstevel@tonic-gate   char line[LINESIZE];
31627c478bd9Sstevel@tonic-gate   int  lineno;
31637c478bd9Sstevel@tonic-gate   struct state *stp;
31647c478bd9Sstevel@tonic-gate   struct action *ap;
31657c478bd9Sstevel@tonic-gate   struct rule *rp;
31667c478bd9Sstevel@tonic-gate   struct acttab *pActtab;
31677c478bd9Sstevel@tonic-gate   int i, j, n;
31687c478bd9Sstevel@tonic-gate   char *name;
31697c478bd9Sstevel@tonic-gate   int mnTknOfst, mxTknOfst;
31707c478bd9Sstevel@tonic-gate   int mnNtOfst, mxNtOfst;
31717c478bd9Sstevel@tonic-gate   struct axset *ax;
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate   in = tplt_open(lemp);
31747c478bd9Sstevel@tonic-gate   if( in==0 ) return;
31757c478bd9Sstevel@tonic-gate   out = file_open(lemp,".c","w");
31767c478bd9Sstevel@tonic-gate   if( out==0 ){
31777c478bd9Sstevel@tonic-gate     fclose(in);
31787c478bd9Sstevel@tonic-gate     return;
31797c478bd9Sstevel@tonic-gate   }
31807c478bd9Sstevel@tonic-gate   lineno = 1;
31817c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate   /* Generate the include code, if any */
31847c478bd9Sstevel@tonic-gate   tplt_print(out,lemp,lemp->include,lemp->includeln,&lineno);
31857c478bd9Sstevel@tonic-gate   if( mhflag ){
31867c478bd9Sstevel@tonic-gate     char *name = file_makename(lemp, ".h");
31877c478bd9Sstevel@tonic-gate     fprintf(out,"#include \"%s\"\n", name); lineno++;
31887c478bd9Sstevel@tonic-gate     free(name);
31897c478bd9Sstevel@tonic-gate   }
31907c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
31917c478bd9Sstevel@tonic-gate 
31927c478bd9Sstevel@tonic-gate   /* Generate #defines for all tokens */
31937c478bd9Sstevel@tonic-gate   if( mhflag ){
31947c478bd9Sstevel@tonic-gate     char *prefix;
31957c478bd9Sstevel@tonic-gate     fprintf(out,"#if INTERFACE\n"); lineno++;
31967c478bd9Sstevel@tonic-gate     if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
31977c478bd9Sstevel@tonic-gate     else                    prefix = "";
31987c478bd9Sstevel@tonic-gate     for(i=1; i<lemp->nterminal; i++){
31997c478bd9Sstevel@tonic-gate       fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
32007c478bd9Sstevel@tonic-gate       lineno++;
32017c478bd9Sstevel@tonic-gate     }
32027c478bd9Sstevel@tonic-gate     fprintf(out,"#endif\n"); lineno++;
32037c478bd9Sstevel@tonic-gate   }
32047c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate   /* Generate the defines */
32077c478bd9Sstevel@tonic-gate   fprintf(out,"/* \001 */\n");
32087c478bd9Sstevel@tonic-gate   fprintf(out,"#define YYCODETYPE %s\n",
32097c478bd9Sstevel@tonic-gate     minimum_size_type(0, lemp->nsymbol+5)); lineno++;
32107c478bd9Sstevel@tonic-gate   fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol+1);  lineno++;
32117c478bd9Sstevel@tonic-gate   fprintf(out,"#define YYACTIONTYPE %s\n",
32127c478bd9Sstevel@tonic-gate     minimum_size_type(0, lemp->nstate+lemp->nrule+5));  lineno++;
32137c478bd9Sstevel@tonic-gate   print_stack_union(out,lemp,&lineno,mhflag);
32147c478bd9Sstevel@tonic-gate   if( lemp->stacksize ){
32157c478bd9Sstevel@tonic-gate     if( atoi(lemp->stacksize)<=0 ){
32167c478bd9Sstevel@tonic-gate       ErrorMsg(lemp->filename,0,
32177c478bd9Sstevel@tonic-gate "Illegal stack size: [%s].  The stack size should be an integer constant.",
32187c478bd9Sstevel@tonic-gate         lemp->stacksize);
32197c478bd9Sstevel@tonic-gate       lemp->errorcnt++;
32207c478bd9Sstevel@tonic-gate       lemp->stacksize = "100";
32217c478bd9Sstevel@tonic-gate     }
32227c478bd9Sstevel@tonic-gate     fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize);  lineno++;
32237c478bd9Sstevel@tonic-gate   }else{
32247c478bd9Sstevel@tonic-gate     fprintf(out,"#define YYSTACKDEPTH 100\n");  lineno++;
32257c478bd9Sstevel@tonic-gate   }
32267c478bd9Sstevel@tonic-gate   if( mhflag ){
32277c478bd9Sstevel@tonic-gate     fprintf(out,"#if INTERFACE\n"); lineno++;
32287c478bd9Sstevel@tonic-gate   }
32297c478bd9Sstevel@tonic-gate   name = lemp->name ? lemp->name : "Parse";
32307c478bd9Sstevel@tonic-gate   if( lemp->arg && lemp->arg[0] ){
32317c478bd9Sstevel@tonic-gate     int i;
32327c478bd9Sstevel@tonic-gate     i = strlen(lemp->arg);
32337c478bd9Sstevel@tonic-gate     while( i>=1 && isspace(lemp->arg[i-1]) ) i--;
32347c478bd9Sstevel@tonic-gate     while( i>=1 && (isalnum(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
32357c478bd9Sstevel@tonic-gate     fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg);  lineno++;
32367c478bd9Sstevel@tonic-gate     fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg);  lineno++;
32377c478bd9Sstevel@tonic-gate     fprintf(out,"#define %sARG_FETCH %s = yypParser->%s\n",
32387c478bd9Sstevel@tonic-gate                  name,lemp->arg,&lemp->arg[i]);  lineno++;
32397c478bd9Sstevel@tonic-gate     fprintf(out,"#define %sARG_STORE yypParser->%s = %s\n",
32407c478bd9Sstevel@tonic-gate                  name,&lemp->arg[i],&lemp->arg[i]);  lineno++;
32417c478bd9Sstevel@tonic-gate   }else{
32427c478bd9Sstevel@tonic-gate     fprintf(out,"#define %sARG_SDECL\n",name);  lineno++;
32437c478bd9Sstevel@tonic-gate     fprintf(out,"#define %sARG_PDECL\n",name);  lineno++;
32447c478bd9Sstevel@tonic-gate     fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
32457c478bd9Sstevel@tonic-gate     fprintf(out,"#define %sARG_STORE\n",name); lineno++;
32467c478bd9Sstevel@tonic-gate   }
32477c478bd9Sstevel@tonic-gate   if( mhflag ){
32487c478bd9Sstevel@tonic-gate     fprintf(out,"#endif\n"); lineno++;
32497c478bd9Sstevel@tonic-gate   }
32507c478bd9Sstevel@tonic-gate   fprintf(out,"#define YYNSTATE %d\n",lemp->nstate);  lineno++;
32517c478bd9Sstevel@tonic-gate   fprintf(out,"#define YYNRULE %d\n",lemp->nrule);  lineno++;
32527c478bd9Sstevel@tonic-gate   fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index);  lineno++;
32537c478bd9Sstevel@tonic-gate   fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum);  lineno++;
32547c478bd9Sstevel@tonic-gate   if( lemp->has_fallback ){
32557c478bd9Sstevel@tonic-gate     fprintf(out,"#define YYFALLBACK 1\n");  lineno++;
32567c478bd9Sstevel@tonic-gate   }
32577c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate   /* Generate the action table and its associates:
32607c478bd9Sstevel@tonic-gate   **
32617c478bd9Sstevel@tonic-gate   **  yy_action[]        A single table containing all actions.
32627c478bd9Sstevel@tonic-gate   **  yy_lookahead[]     A table containing the lookahead for each entry in
32637c478bd9Sstevel@tonic-gate   **                     yy_action.  Used to detect hash collisions.
32647c478bd9Sstevel@tonic-gate   **  yy_shift_ofst[]    For each state, the offset into yy_action for
32657c478bd9Sstevel@tonic-gate   **                     shifting terminals.
32667c478bd9Sstevel@tonic-gate   **  yy_reduce_ofst[]   For each state, the offset into yy_action for
32677c478bd9Sstevel@tonic-gate   **                     shifting non-terminals after a reduce.
32687c478bd9Sstevel@tonic-gate   **  yy_default[]       Default action for each state.
32697c478bd9Sstevel@tonic-gate   */
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate   /* Compute the actions on all states and count them up */
32727c478bd9Sstevel@tonic-gate   ax = malloc( sizeof(ax[0])*lemp->nstate*2 );
32737c478bd9Sstevel@tonic-gate   if( ax==0 ){
32747c478bd9Sstevel@tonic-gate     fprintf(stderr,"malloc failed\n");
32757c478bd9Sstevel@tonic-gate     exit(1);
32767c478bd9Sstevel@tonic-gate   }
32777c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate; i++){
32787c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
32797c478bd9Sstevel@tonic-gate     stp->nTknAct = stp->nNtAct = 0;
32807c478bd9Sstevel@tonic-gate     stp->iDflt = lemp->nstate + lemp->nrule;
32817c478bd9Sstevel@tonic-gate     stp->iTknOfst = NO_OFFSET;
32827c478bd9Sstevel@tonic-gate     stp->iNtOfst = NO_OFFSET;
32837c478bd9Sstevel@tonic-gate     for(ap=stp->ap; ap; ap=ap->next){
32847c478bd9Sstevel@tonic-gate       if( compute_action(lemp,ap)>=0 ){
32857c478bd9Sstevel@tonic-gate         if( ap->sp->index<lemp->nterminal ){
32867c478bd9Sstevel@tonic-gate           stp->nTknAct++;
32877c478bd9Sstevel@tonic-gate         }else if( ap->sp->index<lemp->nsymbol ){
32887c478bd9Sstevel@tonic-gate           stp->nNtAct++;
32897c478bd9Sstevel@tonic-gate         }else{
32907c478bd9Sstevel@tonic-gate           stp->iDflt = compute_action(lemp, ap);
32917c478bd9Sstevel@tonic-gate         }
32927c478bd9Sstevel@tonic-gate       }
32937c478bd9Sstevel@tonic-gate     }
32947c478bd9Sstevel@tonic-gate     ax[i*2].stp = stp;
32957c478bd9Sstevel@tonic-gate     ax[i*2].isTkn = 1;
32967c478bd9Sstevel@tonic-gate     ax[i*2].nAction = stp->nTknAct;
32977c478bd9Sstevel@tonic-gate     ax[i*2+1].stp = stp;
32987c478bd9Sstevel@tonic-gate     ax[i*2+1].isTkn = 0;
32997c478bd9Sstevel@tonic-gate     ax[i*2+1].nAction = stp->nNtAct;
33007c478bd9Sstevel@tonic-gate   }
33017c478bd9Sstevel@tonic-gate   mxTknOfst = mnTknOfst = 0;
33027c478bd9Sstevel@tonic-gate   mxNtOfst = mnNtOfst = 0;
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate   /* Compute the action table.  In order to try to keep the size of the
33057c478bd9Sstevel@tonic-gate   ** action table to a minimum, the heuristic of placing the largest action
33067c478bd9Sstevel@tonic-gate   ** sets first is used.
33077c478bd9Sstevel@tonic-gate   */
33087c478bd9Sstevel@tonic-gate   qsort(ax, lemp->nstate*2, sizeof(ax[0]), axset_compare);
33097c478bd9Sstevel@tonic-gate   pActtab = acttab_alloc();
33107c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate*2 && ax[i].nAction>0; i++){
33117c478bd9Sstevel@tonic-gate     stp = ax[i].stp;
33127c478bd9Sstevel@tonic-gate     if( ax[i].isTkn ){
33137c478bd9Sstevel@tonic-gate       for(ap=stp->ap; ap; ap=ap->next){
33147c478bd9Sstevel@tonic-gate         int action;
33157c478bd9Sstevel@tonic-gate         if( ap->sp->index>=lemp->nterminal ) continue;
33167c478bd9Sstevel@tonic-gate         action = compute_action(lemp, ap);
33177c478bd9Sstevel@tonic-gate         if( action<0 ) continue;
33187c478bd9Sstevel@tonic-gate         acttab_action(pActtab, ap->sp->index, action);
33197c478bd9Sstevel@tonic-gate       }
33207c478bd9Sstevel@tonic-gate       stp->iTknOfst = acttab_insert(pActtab);
33217c478bd9Sstevel@tonic-gate       if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
33227c478bd9Sstevel@tonic-gate       if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
33237c478bd9Sstevel@tonic-gate     }else{
33247c478bd9Sstevel@tonic-gate       for(ap=stp->ap; ap; ap=ap->next){
33257c478bd9Sstevel@tonic-gate         int action;
33267c478bd9Sstevel@tonic-gate         if( ap->sp->index<lemp->nterminal ) continue;
33277c478bd9Sstevel@tonic-gate         if( ap->sp->index==lemp->nsymbol ) continue;
33287c478bd9Sstevel@tonic-gate         action = compute_action(lemp, ap);
33297c478bd9Sstevel@tonic-gate         if( action<0 ) continue;
33307c478bd9Sstevel@tonic-gate         acttab_action(pActtab, ap->sp->index, action);
33317c478bd9Sstevel@tonic-gate       }
33327c478bd9Sstevel@tonic-gate       stp->iNtOfst = acttab_insert(pActtab);
33337c478bd9Sstevel@tonic-gate       if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
33347c478bd9Sstevel@tonic-gate       if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
33357c478bd9Sstevel@tonic-gate     }
33367c478bd9Sstevel@tonic-gate   }
33377c478bd9Sstevel@tonic-gate   free(ax);
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate   /* Output the yy_action table */
33407c478bd9Sstevel@tonic-gate   fprintf(out,"static YYACTIONTYPE yy_action[] = {\n"); lineno++;
33417c478bd9Sstevel@tonic-gate   n = acttab_size(pActtab);
33427c478bd9Sstevel@tonic-gate   for(i=j=0; i<n; i++){
33437c478bd9Sstevel@tonic-gate     int action = acttab_yyaction(pActtab, i);
33447c478bd9Sstevel@tonic-gate     if( action<0 ) action = lemp->nsymbol + lemp->nrule + 2;
33457c478bd9Sstevel@tonic-gate     if( j==0 ) fprintf(out," /* %5d */ ", i);
33467c478bd9Sstevel@tonic-gate     fprintf(out, " %4d,", action);
33477c478bd9Sstevel@tonic-gate     if( j==9 || i==n-1 ){
33487c478bd9Sstevel@tonic-gate       fprintf(out, "\n"); lineno++;
33497c478bd9Sstevel@tonic-gate       j = 0;
33507c478bd9Sstevel@tonic-gate     }else{
33517c478bd9Sstevel@tonic-gate       j++;
33527c478bd9Sstevel@tonic-gate     }
33537c478bd9Sstevel@tonic-gate   }
33547c478bd9Sstevel@tonic-gate   fprintf(out, "};\n"); lineno++;
33557c478bd9Sstevel@tonic-gate 
33567c478bd9Sstevel@tonic-gate   /* Output the yy_lookahead table */
33577c478bd9Sstevel@tonic-gate   fprintf(out,"static YYCODETYPE yy_lookahead[] = {\n"); lineno++;
33587c478bd9Sstevel@tonic-gate   for(i=j=0; i<n; i++){
33597c478bd9Sstevel@tonic-gate     int la = acttab_yylookahead(pActtab, i);
33607c478bd9Sstevel@tonic-gate     if( la<0 ) la = lemp->nsymbol;
33617c478bd9Sstevel@tonic-gate     if( j==0 ) fprintf(out," /* %5d */ ", i);
33627c478bd9Sstevel@tonic-gate     fprintf(out, " %4d,", la);
33637c478bd9Sstevel@tonic-gate     if( j==9 || i==n-1 ){
33647c478bd9Sstevel@tonic-gate       fprintf(out, "\n"); lineno++;
33657c478bd9Sstevel@tonic-gate       j = 0;
33667c478bd9Sstevel@tonic-gate     }else{
33677c478bd9Sstevel@tonic-gate       j++;
33687c478bd9Sstevel@tonic-gate     }
33697c478bd9Sstevel@tonic-gate   }
33707c478bd9Sstevel@tonic-gate   fprintf(out, "};\n"); lineno++;
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate   /* Output the yy_shift_ofst[] table */
33737c478bd9Sstevel@tonic-gate   fprintf(out, "#define YY_SHIFT_USE_DFLT (%d)\n", mnTknOfst-1); lineno++;
3374*55fea89dSDan Cross   fprintf(out, "static %s yy_shift_ofst[] = {\n",
33757c478bd9Sstevel@tonic-gate           minimum_size_type(mnTknOfst-1, mxTknOfst)); lineno++;
33767c478bd9Sstevel@tonic-gate   n = lemp->nstate;
33777c478bd9Sstevel@tonic-gate   for(i=j=0; i<n; i++){
33787c478bd9Sstevel@tonic-gate     int ofst;
33797c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
33807c478bd9Sstevel@tonic-gate     ofst = stp->iTknOfst;
33817c478bd9Sstevel@tonic-gate     if( ofst==NO_OFFSET ) ofst = mnTknOfst - 1;
33827c478bd9Sstevel@tonic-gate     if( j==0 ) fprintf(out," /* %5d */ ", i);
33837c478bd9Sstevel@tonic-gate     fprintf(out, " %4d,", ofst);
33847c478bd9Sstevel@tonic-gate     if( j==9 || i==n-1 ){
33857c478bd9Sstevel@tonic-gate       fprintf(out, "\n"); lineno++;
33867c478bd9Sstevel@tonic-gate       j = 0;
33877c478bd9Sstevel@tonic-gate     }else{
33887c478bd9Sstevel@tonic-gate       j++;
33897c478bd9Sstevel@tonic-gate     }
33907c478bd9Sstevel@tonic-gate   }
33917c478bd9Sstevel@tonic-gate   fprintf(out, "};\n"); lineno++;
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate   /* Output the yy_reduce_ofst[] table */
33947c478bd9Sstevel@tonic-gate   fprintf(out, "#define YY_REDUCE_USE_DFLT (%d)\n", mnNtOfst-1); lineno++;
3395*55fea89dSDan Cross   fprintf(out, "static %s yy_reduce_ofst[] = {\n",
33967c478bd9Sstevel@tonic-gate           minimum_size_type(mnNtOfst-1, mxNtOfst)); lineno++;
33977c478bd9Sstevel@tonic-gate   n = lemp->nstate;
33987c478bd9Sstevel@tonic-gate   for(i=j=0; i<n; i++){
33997c478bd9Sstevel@tonic-gate     int ofst;
34007c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
34017c478bd9Sstevel@tonic-gate     ofst = stp->iNtOfst;
34027c478bd9Sstevel@tonic-gate     if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
34037c478bd9Sstevel@tonic-gate     if( j==0 ) fprintf(out," /* %5d */ ", i);
34047c478bd9Sstevel@tonic-gate     fprintf(out, " %4d,", ofst);
34057c478bd9Sstevel@tonic-gate     if( j==9 || i==n-1 ){
34067c478bd9Sstevel@tonic-gate       fprintf(out, "\n"); lineno++;
34077c478bd9Sstevel@tonic-gate       j = 0;
34087c478bd9Sstevel@tonic-gate     }else{
34097c478bd9Sstevel@tonic-gate       j++;
34107c478bd9Sstevel@tonic-gate     }
34117c478bd9Sstevel@tonic-gate   }
34127c478bd9Sstevel@tonic-gate   fprintf(out, "};\n"); lineno++;
34137c478bd9Sstevel@tonic-gate 
34147c478bd9Sstevel@tonic-gate   /* Output the default action table */
34157c478bd9Sstevel@tonic-gate   fprintf(out, "static YYACTIONTYPE yy_default[] = {\n"); lineno++;
34167c478bd9Sstevel@tonic-gate   n = lemp->nstate;
34177c478bd9Sstevel@tonic-gate   for(i=j=0; i<n; i++){
34187c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
34197c478bd9Sstevel@tonic-gate     if( j==0 ) fprintf(out," /* %5d */ ", i);
34207c478bd9Sstevel@tonic-gate     fprintf(out, " %4d,", stp->iDflt);
34217c478bd9Sstevel@tonic-gate     if( j==9 || i==n-1 ){
34227c478bd9Sstevel@tonic-gate       fprintf(out, "\n"); lineno++;
34237c478bd9Sstevel@tonic-gate       j = 0;
34247c478bd9Sstevel@tonic-gate     }else{
34257c478bd9Sstevel@tonic-gate       j++;
34267c478bd9Sstevel@tonic-gate     }
34277c478bd9Sstevel@tonic-gate   }
34287c478bd9Sstevel@tonic-gate   fprintf(out, "};\n"); lineno++;
34297c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate   /* Generate the table of fallback tokens.
34327c478bd9Sstevel@tonic-gate   */
34337c478bd9Sstevel@tonic-gate   if( lemp->has_fallback ){
34347c478bd9Sstevel@tonic-gate     for(i=0; i<lemp->nterminal; i++){
34357c478bd9Sstevel@tonic-gate       struct symbol *p = lemp->symbols[i];
34367c478bd9Sstevel@tonic-gate       if( p->fallback==0 ){
34377c478bd9Sstevel@tonic-gate         fprintf(out, "    0,  /* %10s => nothing */\n", p->name);
34387c478bd9Sstevel@tonic-gate       }else{
34397c478bd9Sstevel@tonic-gate         fprintf(out, "  %3d,  /* %10s => %s */\n", p->fallback->index,
34407c478bd9Sstevel@tonic-gate           p->name, p->fallback->name);
34417c478bd9Sstevel@tonic-gate       }
34427c478bd9Sstevel@tonic-gate       lineno++;
34437c478bd9Sstevel@tonic-gate     }
34447c478bd9Sstevel@tonic-gate   }
34457c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name, in, out, &lineno);
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate   /* Generate a table containing the symbolic name of every symbol
34487c478bd9Sstevel@tonic-gate   */
34497c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nsymbol; i++){
34507c478bd9Sstevel@tonic-gate     sprintf(line,"\"%s\",",lemp->symbols[i]->name);
34517c478bd9Sstevel@tonic-gate     fprintf(out,"  %-15s",line);
34527c478bd9Sstevel@tonic-gate     if( (i&3)==3 ){ fprintf(out,"\n"); lineno++; }
34537c478bd9Sstevel@tonic-gate   }
34547c478bd9Sstevel@tonic-gate   if( (i&3)!=0 ){ fprintf(out,"\n"); lineno++; }
34557c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate   /* Generate a table containing a text string that describes every
34587c478bd9Sstevel@tonic-gate   ** rule in the rule set of the grammer.  This information is used
34597c478bd9Sstevel@tonic-gate   ** when tracing REDUCE actions.
34607c478bd9Sstevel@tonic-gate   */
34617c478bd9Sstevel@tonic-gate   for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
34627c478bd9Sstevel@tonic-gate     assert( rp->index==i );
34637c478bd9Sstevel@tonic-gate     fprintf(out," /* %3d */ \"%s ::=", i, rp->lhs->name);
34647c478bd9Sstevel@tonic-gate     for(j=0; j<rp->nrhs; j++) fprintf(out," %s",rp->rhs[j]->name);
34657c478bd9Sstevel@tonic-gate     fprintf(out,"\",\n"); lineno++;
34667c478bd9Sstevel@tonic-gate   }
34677c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate   /* Generate code which executes every time a symbol is popped from
3470*55fea89dSDan Cross   ** the stack while processing errors or while destroying the parser.
34717c478bd9Sstevel@tonic-gate   ** (In other words, generate the %destructor actions)
34727c478bd9Sstevel@tonic-gate   */
34737c478bd9Sstevel@tonic-gate   if( lemp->tokendest ){
34747c478bd9Sstevel@tonic-gate     for(i=0; i<lemp->nsymbol; i++){
34757c478bd9Sstevel@tonic-gate       struct symbol *sp = lemp->symbols[i];
34767c478bd9Sstevel@tonic-gate       if( sp==0 || sp->type!=TERMINAL ) continue;
34777c478bd9Sstevel@tonic-gate       fprintf(out,"    case %d:\n",sp->index); lineno++;
34787c478bd9Sstevel@tonic-gate     }
34797c478bd9Sstevel@tonic-gate     for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
34807c478bd9Sstevel@tonic-gate     if( i<lemp->nsymbol ){
34817c478bd9Sstevel@tonic-gate       emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
34827c478bd9Sstevel@tonic-gate       fprintf(out,"      break;\n"); lineno++;
34837c478bd9Sstevel@tonic-gate     }
34847c478bd9Sstevel@tonic-gate   }
34857c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nsymbol; i++){
34867c478bd9Sstevel@tonic-gate     struct symbol *sp = lemp->symbols[i];
34877c478bd9Sstevel@tonic-gate     if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
34887c478bd9Sstevel@tonic-gate     fprintf(out,"    case %d:\n",sp->index); lineno++;
34897c478bd9Sstevel@tonic-gate     emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
34907c478bd9Sstevel@tonic-gate     fprintf(out,"      break;\n"); lineno++;
34917c478bd9Sstevel@tonic-gate   }
34927c478bd9Sstevel@tonic-gate   if( lemp->vardest ){
34937c478bd9Sstevel@tonic-gate     struct symbol *dflt_sp = 0;
34947c478bd9Sstevel@tonic-gate     for(i=0; i<lemp->nsymbol; i++){
34957c478bd9Sstevel@tonic-gate       struct symbol *sp = lemp->symbols[i];
34967c478bd9Sstevel@tonic-gate       if( sp==0 || sp->type==TERMINAL ||
34977c478bd9Sstevel@tonic-gate           sp->index<=0 || sp->destructor!=0 ) continue;
34987c478bd9Sstevel@tonic-gate       fprintf(out,"    case %d:\n",sp->index); lineno++;
34997c478bd9Sstevel@tonic-gate       dflt_sp = sp;
35007c478bd9Sstevel@tonic-gate     }
35017c478bd9Sstevel@tonic-gate     if( dflt_sp!=0 ){
35027c478bd9Sstevel@tonic-gate       emit_destructor_code(out,dflt_sp,lemp,&lineno);
35037c478bd9Sstevel@tonic-gate       fprintf(out,"      break;\n"); lineno++;
35047c478bd9Sstevel@tonic-gate     }
35057c478bd9Sstevel@tonic-gate   }
35067c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate   /* Generate code which executes whenever the parser stack overflows */
35097c478bd9Sstevel@tonic-gate   tplt_print(out,lemp,lemp->overflow,lemp->overflowln,&lineno);
35107c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
35117c478bd9Sstevel@tonic-gate 
3512*55fea89dSDan Cross   /* Generate the table of rule information
35137c478bd9Sstevel@tonic-gate   **
35147c478bd9Sstevel@tonic-gate   ** Note: This code depends on the fact that rules are number
35157c478bd9Sstevel@tonic-gate   ** sequentually beginning with 0.
35167c478bd9Sstevel@tonic-gate   */
35177c478bd9Sstevel@tonic-gate   for(rp=lemp->rule; rp; rp=rp->next){
35187c478bd9Sstevel@tonic-gate     fprintf(out,"  { %d, %d },\n",rp->lhs->index,rp->nrhs); lineno++;
35197c478bd9Sstevel@tonic-gate   }
35207c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate   /* Generate code which execution during each REDUCE action */
35237c478bd9Sstevel@tonic-gate   for(rp=lemp->rule; rp; rp=rp->next){
35247c478bd9Sstevel@tonic-gate     fprintf(out,"      case %d:\n",rp->index); lineno++;
35257c478bd9Sstevel@tonic-gate     emit_code(out,rp,lemp,&lineno);
35267c478bd9Sstevel@tonic-gate     fprintf(out,"        break;\n"); lineno++;
35277c478bd9Sstevel@tonic-gate   }
35287c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate   /* Generate code which executes if a parse fails */
35317c478bd9Sstevel@tonic-gate   tplt_print(out,lemp,lemp->failure,lemp->failureln,&lineno);
35327c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate   /* Generate code which executes when a syntax error occurs */
35357c478bd9Sstevel@tonic-gate   tplt_print(out,lemp,lemp->error,lemp->errorln,&lineno);
35367c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
35377c478bd9Sstevel@tonic-gate 
35387c478bd9Sstevel@tonic-gate   /* Generate code which executes when the parser accepts its input */
35397c478bd9Sstevel@tonic-gate   tplt_print(out,lemp,lemp->accept,lemp->acceptln,&lineno);
35407c478bd9Sstevel@tonic-gate   tplt_xfer(lemp->name,in,out,&lineno);
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate   /* Append any addition code the user desires */
35437c478bd9Sstevel@tonic-gate   tplt_print(out,lemp,lemp->extracode,lemp->extracodeln,&lineno);
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate   fclose(in);
35467c478bd9Sstevel@tonic-gate   fclose(out);
35477c478bd9Sstevel@tonic-gate   return;
35487c478bd9Sstevel@tonic-gate }
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate /* Generate a header file for the parser */
ReportHeader(lemp)35517c478bd9Sstevel@tonic-gate void ReportHeader(lemp)
35527c478bd9Sstevel@tonic-gate struct lemon *lemp;
35537c478bd9Sstevel@tonic-gate {
35547c478bd9Sstevel@tonic-gate   FILE *out, *in;
35557c478bd9Sstevel@tonic-gate   char *prefix;
35567c478bd9Sstevel@tonic-gate   char line[LINESIZE];
35577c478bd9Sstevel@tonic-gate   char pattern[LINESIZE];
35587c478bd9Sstevel@tonic-gate   int i;
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate   if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
35617c478bd9Sstevel@tonic-gate   else                    prefix = "";
35627c478bd9Sstevel@tonic-gate   in = file_open(lemp,".h","r");
35637c478bd9Sstevel@tonic-gate   if( in ){
35647c478bd9Sstevel@tonic-gate     for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
35657c478bd9Sstevel@tonic-gate       sprintf(pattern,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
35667c478bd9Sstevel@tonic-gate       if( strcmp(line,pattern) ) break;
35677c478bd9Sstevel@tonic-gate     }
35687c478bd9Sstevel@tonic-gate     fclose(in);
35697c478bd9Sstevel@tonic-gate     if( i==lemp->nterminal ){
35707c478bd9Sstevel@tonic-gate       /* No change in the file.  Don't rewrite it. */
35717c478bd9Sstevel@tonic-gate       return;
35727c478bd9Sstevel@tonic-gate     }
35737c478bd9Sstevel@tonic-gate   }
35747c478bd9Sstevel@tonic-gate   out = file_open(lemp,".h","w");
35757c478bd9Sstevel@tonic-gate   if( out ){
35767c478bd9Sstevel@tonic-gate     for(i=1; i<lemp->nterminal; i++){
35777c478bd9Sstevel@tonic-gate       fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
35787c478bd9Sstevel@tonic-gate     }
3579*55fea89dSDan Cross     fclose(out);
35807c478bd9Sstevel@tonic-gate   }
35817c478bd9Sstevel@tonic-gate   return;
35827c478bd9Sstevel@tonic-gate }
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate /* Reduce the size of the action tables, if possible, by making use
35857c478bd9Sstevel@tonic-gate ** of defaults.
35867c478bd9Sstevel@tonic-gate **
35877c478bd9Sstevel@tonic-gate ** In this version, we take the most frequent REDUCE action and make
35887c478bd9Sstevel@tonic-gate ** it the default.  Only default a reduce if there are more than one.
35897c478bd9Sstevel@tonic-gate */
CompressTables(lemp)35907c478bd9Sstevel@tonic-gate void CompressTables(lemp)
35917c478bd9Sstevel@tonic-gate struct lemon *lemp;
35927c478bd9Sstevel@tonic-gate {
35937c478bd9Sstevel@tonic-gate   struct state *stp;
35947c478bd9Sstevel@tonic-gate   struct action *ap, *ap2;
35957c478bd9Sstevel@tonic-gate   struct rule *rp, *rp2, *rbest;
35967c478bd9Sstevel@tonic-gate   int nbest, n;
35977c478bd9Sstevel@tonic-gate   int i;
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate   for(i=0; i<lemp->nstate; i++){
36007c478bd9Sstevel@tonic-gate     stp = lemp->sorted[i];
36017c478bd9Sstevel@tonic-gate     nbest = 0;
36027c478bd9Sstevel@tonic-gate     rbest = 0;
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate     for(ap=stp->ap; ap; ap=ap->next){
36057c478bd9Sstevel@tonic-gate       if( ap->type!=REDUCE ) continue;
36067c478bd9Sstevel@tonic-gate       rp = ap->x.rp;
36077c478bd9Sstevel@tonic-gate       if( rp==rbest ) continue;
36087c478bd9Sstevel@tonic-gate       n = 1;
36097c478bd9Sstevel@tonic-gate       for(ap2=ap->next; ap2; ap2=ap2->next){
36107c478bd9Sstevel@tonic-gate         if( ap2->type!=REDUCE ) continue;
36117c478bd9Sstevel@tonic-gate         rp2 = ap2->x.rp;
36127c478bd9Sstevel@tonic-gate         if( rp2==rbest ) continue;
36137c478bd9Sstevel@tonic-gate         if( rp2==rp ) n++;
36147c478bd9Sstevel@tonic-gate       }
36157c478bd9Sstevel@tonic-gate       if( n>nbest ){
36167c478bd9Sstevel@tonic-gate         nbest = n;
36177c478bd9Sstevel@tonic-gate         rbest = rp;
36187c478bd9Sstevel@tonic-gate       }
36197c478bd9Sstevel@tonic-gate     }
3620*55fea89dSDan Cross 
36217c478bd9Sstevel@tonic-gate     /* Do not make a default if the number of rules to default
36227c478bd9Sstevel@tonic-gate     ** is not at least 2 */
36237c478bd9Sstevel@tonic-gate     if( nbest<2 ) continue;
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate     /* Combine matching REDUCE actions into a single default */
36277c478bd9Sstevel@tonic-gate     for(ap=stp->ap; ap; ap=ap->next){
36287c478bd9Sstevel@tonic-gate       if( ap->type==REDUCE && ap->x.rp==rbest ) break;
36297c478bd9Sstevel@tonic-gate     }
36307c478bd9Sstevel@tonic-gate     assert( ap );
36317c478bd9Sstevel@tonic-gate     ap->sp = Symbol_new("{default}");
36327c478bd9Sstevel@tonic-gate     for(ap=ap->next; ap; ap=ap->next){
36337c478bd9Sstevel@tonic-gate       if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
36347c478bd9Sstevel@tonic-gate     }
36357c478bd9Sstevel@tonic-gate     stp->ap = Action_sort(stp->ap);
36367c478bd9Sstevel@tonic-gate   }
36377c478bd9Sstevel@tonic-gate }
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate /***************** From the file "set.c" ************************************/
36407c478bd9Sstevel@tonic-gate /*
36417c478bd9Sstevel@tonic-gate ** Set manipulation routines for the LEMON parser generator.
36427c478bd9Sstevel@tonic-gate */
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate static int size = 0;
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate /* Set the set size */
SetSize(n)36477c478bd9Sstevel@tonic-gate void SetSize(n)
36487c478bd9Sstevel@tonic-gate int n;
36497c478bd9Sstevel@tonic-gate {
36507c478bd9Sstevel@tonic-gate   size = n+1;
36517c478bd9Sstevel@tonic-gate }
36527c478bd9Sstevel@tonic-gate 
36537c478bd9Sstevel@tonic-gate /* Allocate a new set */
SetNew()36547c478bd9Sstevel@tonic-gate char *SetNew(){
36557c478bd9Sstevel@tonic-gate   char *s;
36567c478bd9Sstevel@tonic-gate   int i;
36577c478bd9Sstevel@tonic-gate   s = (char*)malloc( size );
36587c478bd9Sstevel@tonic-gate   if( s==0 ){
36597c478bd9Sstevel@tonic-gate     extern void memory_error();
36607c478bd9Sstevel@tonic-gate     memory_error();
36617c478bd9Sstevel@tonic-gate   }
36627c478bd9Sstevel@tonic-gate   for(i=0; i<size; i++) s[i] = 0;
36637c478bd9Sstevel@tonic-gate   return s;
36647c478bd9Sstevel@tonic-gate }
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate /* Deallocate a set */
SetFree(s)36677c478bd9Sstevel@tonic-gate void SetFree(s)
36687c478bd9Sstevel@tonic-gate char *s;
36697c478bd9Sstevel@tonic-gate {
36707c478bd9Sstevel@tonic-gate   free(s);
36717c478bd9Sstevel@tonic-gate }
36727c478bd9Sstevel@tonic-gate 
36737c478bd9Sstevel@tonic-gate /* Add a new element to the set.  Return TRUE if the element was added
36747c478bd9Sstevel@tonic-gate ** and FALSE if it was already there. */
SetAdd(s,e)36757c478bd9Sstevel@tonic-gate int SetAdd(s,e)
36767c478bd9Sstevel@tonic-gate char *s;
36777c478bd9Sstevel@tonic-gate int e;
36787c478bd9Sstevel@tonic-gate {
36797c478bd9Sstevel@tonic-gate   int rv;
36807c478bd9Sstevel@tonic-gate   rv = s[e];
36817c478bd9Sstevel@tonic-gate   s[e] = 1;
36827c478bd9Sstevel@tonic-gate   return !rv;
36837c478bd9Sstevel@tonic-gate }
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate /* Add every element of s2 to s1.  Return TRUE if s1 changes. */
SetUnion(s1,s2)36867c478bd9Sstevel@tonic-gate int SetUnion(s1,s2)
36877c478bd9Sstevel@tonic-gate char *s1;
36887c478bd9Sstevel@tonic-gate char *s2;
36897c478bd9Sstevel@tonic-gate {
36907c478bd9Sstevel@tonic-gate   int i, progress;
36917c478bd9Sstevel@tonic-gate   progress = 0;
36927c478bd9Sstevel@tonic-gate   for(i=0; i<size; i++){
36937c478bd9Sstevel@tonic-gate     if( s2[i]==0 ) continue;
36947c478bd9Sstevel@tonic-gate     if( s1[i]==0 ){
36957c478bd9Sstevel@tonic-gate       progress = 1;
36967c478bd9Sstevel@tonic-gate       s1[i] = 1;
36977c478bd9Sstevel@tonic-gate     }
36987c478bd9Sstevel@tonic-gate   }
36997c478bd9Sstevel@tonic-gate   return progress;
37007c478bd9Sstevel@tonic-gate }
37017c478bd9Sstevel@tonic-gate /********************** From the file "table.c" ****************************/
37027c478bd9Sstevel@tonic-gate /*
37037c478bd9Sstevel@tonic-gate ** All code in this file has been automatically generated
37047c478bd9Sstevel@tonic-gate ** from a specification in the file
37057c478bd9Sstevel@tonic-gate **              "table.q"
37067c478bd9Sstevel@tonic-gate ** by the associative array code building program "aagen".
37077c478bd9Sstevel@tonic-gate ** Do not edit this file!  Instead, edit the specification
37087c478bd9Sstevel@tonic-gate ** file, then rerun aagen.
37097c478bd9Sstevel@tonic-gate */
37107c478bd9Sstevel@tonic-gate /*
37117c478bd9Sstevel@tonic-gate ** Code for processing tables in the LEMON parser generator.
37127c478bd9Sstevel@tonic-gate */
37137c478bd9Sstevel@tonic-gate 
strhash(x)37147c478bd9Sstevel@tonic-gate PRIVATE int strhash(x)
37157c478bd9Sstevel@tonic-gate char *x;
37167c478bd9Sstevel@tonic-gate {
37177c478bd9Sstevel@tonic-gate   int h = 0;
37187c478bd9Sstevel@tonic-gate   while( *x) h = h*13 + *(x++);
37197c478bd9Sstevel@tonic-gate   return h;
37207c478bd9Sstevel@tonic-gate }
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate /* Works like strdup, sort of.  Save a string in malloced memory, but
37237c478bd9Sstevel@tonic-gate ** keep strings in a table so that the same string is not in more
37247c478bd9Sstevel@tonic-gate ** than one place.
37257c478bd9Sstevel@tonic-gate */
Strsafe(y)37267c478bd9Sstevel@tonic-gate char *Strsafe(y)
37277c478bd9Sstevel@tonic-gate char *y;
37287c478bd9Sstevel@tonic-gate {
37297c478bd9Sstevel@tonic-gate   char *z;
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate   z = Strsafe_find(y);
37327c478bd9Sstevel@tonic-gate   if( z==0 && (z=malloc( strlen(y)+1 ))!=0 ){
37337c478bd9Sstevel@tonic-gate     strcpy(z,y);
37347c478bd9Sstevel@tonic-gate     Strsafe_insert(z);
37357c478bd9Sstevel@tonic-gate   }
37367c478bd9Sstevel@tonic-gate   MemoryCheck(z);
37377c478bd9Sstevel@tonic-gate   return z;
37387c478bd9Sstevel@tonic-gate }
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate /* There is one instance of the following structure for each
37417c478bd9Sstevel@tonic-gate ** associative array of type "x1".
37427c478bd9Sstevel@tonic-gate */
37437c478bd9Sstevel@tonic-gate struct s_x1 {
37447c478bd9Sstevel@tonic-gate   int size;               /* The number of available slots. */
37457c478bd9Sstevel@tonic-gate                           /*   Must be a power of 2 greater than or */
37467c478bd9Sstevel@tonic-gate                           /*   equal to 1 */
37477c478bd9Sstevel@tonic-gate   int count;              /* Number of currently slots filled */
37487c478bd9Sstevel@tonic-gate   struct s_x1node *tbl;  /* The data stored here */
37497c478bd9Sstevel@tonic-gate   struct s_x1node **ht;  /* Hash table for lookups */
37507c478bd9Sstevel@tonic-gate };
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate /* There is one instance of this structure for every data element
37537c478bd9Sstevel@tonic-gate ** in an associative array of type "x1".
37547c478bd9Sstevel@tonic-gate */
37557c478bd9Sstevel@tonic-gate typedef struct s_x1node {
37567c478bd9Sstevel@tonic-gate   char *data;                  /* The data */
37577c478bd9Sstevel@tonic-gate   struct s_x1node *next;   /* Next entry with the same hash */
37587c478bd9Sstevel@tonic-gate   struct s_x1node **from;  /* Previous link */
37597c478bd9Sstevel@tonic-gate } x1node;
37607c478bd9Sstevel@tonic-gate 
37617c478bd9Sstevel@tonic-gate /* There is only one instance of the array, which is the following */
37627c478bd9Sstevel@tonic-gate static struct s_x1 *x1a;
37637c478bd9Sstevel@tonic-gate 
37647c478bd9Sstevel@tonic-gate /* Allocate a new associative array */
Strsafe_init()37657c478bd9Sstevel@tonic-gate void Strsafe_init(){
37667c478bd9Sstevel@tonic-gate   if( x1a ) return;
37677c478bd9Sstevel@tonic-gate   x1a = (struct s_x1*)malloc( sizeof(struct s_x1) );
37687c478bd9Sstevel@tonic-gate   if( x1a ){
37697c478bd9Sstevel@tonic-gate     x1a->size = 1024;
37707c478bd9Sstevel@tonic-gate     x1a->count = 0;
3771*55fea89dSDan Cross     x1a->tbl = (x1node*)malloc(
37727c478bd9Sstevel@tonic-gate       (sizeof(x1node) + sizeof(x1node*))*1024 );
37737c478bd9Sstevel@tonic-gate     if( x1a->tbl==0 ){
37747c478bd9Sstevel@tonic-gate       free(x1a);
37757c478bd9Sstevel@tonic-gate       x1a = 0;
37767c478bd9Sstevel@tonic-gate     }else{
37777c478bd9Sstevel@tonic-gate       int i;
37787c478bd9Sstevel@tonic-gate       x1a->ht = (x1node**)&(x1a->tbl[1024]);
37797c478bd9Sstevel@tonic-gate       for(i=0; i<1024; i++) x1a->ht[i] = 0;
37807c478bd9Sstevel@tonic-gate     }
37817c478bd9Sstevel@tonic-gate   }
37827c478bd9Sstevel@tonic-gate }
37837c478bd9Sstevel@tonic-gate /* Insert a new record into the array.  Return TRUE if successful.
37847c478bd9Sstevel@tonic-gate ** Prior data with the same key is NOT overwritten */
Strsafe_insert(data)37857c478bd9Sstevel@tonic-gate int Strsafe_insert(data)
37867c478bd9Sstevel@tonic-gate char *data;
37877c478bd9Sstevel@tonic-gate {
37887c478bd9Sstevel@tonic-gate   x1node *np;
37897c478bd9Sstevel@tonic-gate   int h;
37907c478bd9Sstevel@tonic-gate   int ph;
37917c478bd9Sstevel@tonic-gate 
37927c478bd9Sstevel@tonic-gate   if( x1a==0 ) return 0;
37937c478bd9Sstevel@tonic-gate   ph = strhash(data);
37947c478bd9Sstevel@tonic-gate   h = ph & (x1a->size-1);
37957c478bd9Sstevel@tonic-gate   np = x1a->ht[h];
37967c478bd9Sstevel@tonic-gate   while( np ){
37977c478bd9Sstevel@tonic-gate     if( strcmp(np->data,data)==0 ){
37987c478bd9Sstevel@tonic-gate       /* An existing entry with the same key is found. */
37997c478bd9Sstevel@tonic-gate       /* Fail because overwrite is not allows. */
38007c478bd9Sstevel@tonic-gate       return 0;
38017c478bd9Sstevel@tonic-gate     }
38027c478bd9Sstevel@tonic-gate     np = np->next;
38037c478bd9Sstevel@tonic-gate   }
38047c478bd9Sstevel@tonic-gate   if( x1a->count>=x1a->size ){
38057c478bd9Sstevel@tonic-gate     /* Need to make the hash table bigger */
38067c478bd9Sstevel@tonic-gate     int i,size;
38077c478bd9Sstevel@tonic-gate     struct s_x1 array;
38087c478bd9Sstevel@tonic-gate     array.size = size = x1a->size*2;
38097c478bd9Sstevel@tonic-gate     array.count = x1a->count;
38107c478bd9Sstevel@tonic-gate     array.tbl = (x1node*)malloc(
38117c478bd9Sstevel@tonic-gate       (sizeof(x1node) + sizeof(x1node*))*size );
38127c478bd9Sstevel@tonic-gate     if( array.tbl==0 ) return 0;  /* Fail due to malloc failure */
38137c478bd9Sstevel@tonic-gate     array.ht = (x1node**)&(array.tbl[size]);
38147c478bd9Sstevel@tonic-gate     for(i=0; i<size; i++) array.ht[i] = 0;
38157c478bd9Sstevel@tonic-gate     for(i=0; i<x1a->count; i++){
38167c478bd9Sstevel@tonic-gate       x1node *oldnp, *newnp;
38177c478bd9Sstevel@tonic-gate       oldnp = &(x1a->tbl[i]);
38187c478bd9Sstevel@tonic-gate       h = strhash(oldnp->data) & (size-1);
38197c478bd9Sstevel@tonic-gate       newnp = &(array.tbl[i]);
38207c478bd9Sstevel@tonic-gate       if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
38217c478bd9Sstevel@tonic-gate       newnp->next = array.ht[h];
38227c478bd9Sstevel@tonic-gate       newnp->data = oldnp->data;
38237c478bd9Sstevel@tonic-gate       newnp->from = &(array.ht[h]);
38247c478bd9Sstevel@tonic-gate       array.ht[h] = newnp;
38257c478bd9Sstevel@tonic-gate     }
38267c478bd9Sstevel@tonic-gate     free(x1a->tbl);
38277c478bd9Sstevel@tonic-gate     *x1a = array;
38287c478bd9Sstevel@tonic-gate   }
38297c478bd9Sstevel@tonic-gate   /* Insert the new data */
38307c478bd9Sstevel@tonic-gate   h = ph & (x1a->size-1);
38317c478bd9Sstevel@tonic-gate   np = &(x1a->tbl[x1a->count++]);
38327c478bd9Sstevel@tonic-gate   np->data = data;
38337c478bd9Sstevel@tonic-gate   if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
38347c478bd9Sstevel@tonic-gate   np->next = x1a->ht[h];
38357c478bd9Sstevel@tonic-gate   x1a->ht[h] = np;
38367c478bd9Sstevel@tonic-gate   np->from = &(x1a->ht[h]);
38377c478bd9Sstevel@tonic-gate   return 1;
38387c478bd9Sstevel@tonic-gate }
38397c478bd9Sstevel@tonic-gate 
38407c478bd9Sstevel@tonic-gate /* Return a pointer to data assigned to the given key.  Return NULL
38417c478bd9Sstevel@tonic-gate ** if no such key. */
Strsafe_find(key)38427c478bd9Sstevel@tonic-gate char *Strsafe_find(key)
38437c478bd9Sstevel@tonic-gate char *key;
38447c478bd9Sstevel@tonic-gate {
38457c478bd9Sstevel@tonic-gate   int h;
38467c478bd9Sstevel@tonic-gate   x1node *np;
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate   if( x1a==0 ) return 0;
38497c478bd9Sstevel@tonic-gate   h = strhash(key) & (x1a->size-1);
38507c478bd9Sstevel@tonic-gate   np = x1a->ht[h];
38517c478bd9Sstevel@tonic-gate   while( np ){
38527c478bd9Sstevel@tonic-gate     if( strcmp(np->data,key)==0 ) break;
38537c478bd9Sstevel@tonic-gate     np = np->next;
38547c478bd9Sstevel@tonic-gate   }
38557c478bd9Sstevel@tonic-gate   return np ? np->data : 0;
38567c478bd9Sstevel@tonic-gate }
38577c478bd9Sstevel@tonic-gate 
38587c478bd9Sstevel@tonic-gate /* Return a pointer to the (terminal or nonterminal) symbol "x".
38597c478bd9Sstevel@tonic-gate ** Create a new symbol if this is the first time "x" has been seen.
38607c478bd9Sstevel@tonic-gate */
Symbol_new(x)38617c478bd9Sstevel@tonic-gate struct symbol *Symbol_new(x)
38627c478bd9Sstevel@tonic-gate char *x;
38637c478bd9Sstevel@tonic-gate {
38647c478bd9Sstevel@tonic-gate   struct symbol *sp;
38657c478bd9Sstevel@tonic-gate 
38667c478bd9Sstevel@tonic-gate   sp = Symbol_find(x);
38677c478bd9Sstevel@tonic-gate   if( sp==0 ){
38687c478bd9Sstevel@tonic-gate     sp = (struct symbol *)malloc( sizeof(struct symbol) );
38697c478bd9Sstevel@tonic-gate     MemoryCheck(sp);
38707c478bd9Sstevel@tonic-gate     sp->name = Strsafe(x);
38717c478bd9Sstevel@tonic-gate     sp->type = isupper(*x) ? TERMINAL : NONTERMINAL;
38727c478bd9Sstevel@tonic-gate     sp->rule = 0;
38737c478bd9Sstevel@tonic-gate     sp->fallback = 0;
38747c478bd9Sstevel@tonic-gate     sp->prec = -1;
38757c478bd9Sstevel@tonic-gate     sp->assoc = UNK;
38767c478bd9Sstevel@tonic-gate     sp->firstset = 0;
38777c478bd9Sstevel@tonic-gate     sp->lambda = B_FALSE;
38787c478bd9Sstevel@tonic-gate     sp->destructor = 0;
38797c478bd9Sstevel@tonic-gate     sp->datatype = 0;
38807c478bd9Sstevel@tonic-gate     Symbol_insert(sp,sp->name);
38817c478bd9Sstevel@tonic-gate   }
38827c478bd9Sstevel@tonic-gate   return sp;
38837c478bd9Sstevel@tonic-gate }
38847c478bd9Sstevel@tonic-gate 
38857c478bd9Sstevel@tonic-gate /* Compare two symbols for working purposes
38867c478bd9Sstevel@tonic-gate **
38877c478bd9Sstevel@tonic-gate ** Symbols that begin with upper case letters (terminals or tokens)
38887c478bd9Sstevel@tonic-gate ** must sort before symbols that begin with lower case letters
38897c478bd9Sstevel@tonic-gate ** (non-terminals).  Other than that, the order does not matter.
38907c478bd9Sstevel@tonic-gate **
38917c478bd9Sstevel@tonic-gate ** We find experimentally that leaving the symbols in their original
38927c478bd9Sstevel@tonic-gate ** order (the order they appeared in the grammar file) gives the
38937c478bd9Sstevel@tonic-gate ** smallest parser tables in SQLite.
38947c478bd9Sstevel@tonic-gate */
Symbolcmpp(struct symbol ** a,struct symbol ** b)38957c478bd9Sstevel@tonic-gate int Symbolcmpp(struct symbol **a, struct symbol **b){
38967c478bd9Sstevel@tonic-gate   int i1 = (**a).index + 10000000*((**a).name[0]>'Z');
38977c478bd9Sstevel@tonic-gate   int i2 = (**b).index + 10000000*((**b).name[0]>'Z');
38987c478bd9Sstevel@tonic-gate   return i1-i2;
38997c478bd9Sstevel@tonic-gate }
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate /* There is one instance of the following structure for each
39027c478bd9Sstevel@tonic-gate ** associative array of type "x2".
39037c478bd9Sstevel@tonic-gate */
39047c478bd9Sstevel@tonic-gate struct s_x2 {
39057c478bd9Sstevel@tonic-gate   int size;               /* The number of available slots. */
39067c478bd9Sstevel@tonic-gate                           /*   Must be a power of 2 greater than or */
39077c478bd9Sstevel@tonic-gate                           /*   equal to 1 */
39087c478bd9Sstevel@tonic-gate   int count;              /* Number of currently slots filled */
39097c478bd9Sstevel@tonic-gate   struct s_x2node *tbl;  /* The data stored here */
39107c478bd9Sstevel@tonic-gate   struct s_x2node **ht;  /* Hash table for lookups */
39117c478bd9Sstevel@tonic-gate };
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate /* There is one instance of this structure for every data element
39147c478bd9Sstevel@tonic-gate ** in an associative array of type "x2".
39157c478bd9Sstevel@tonic-gate */
39167c478bd9Sstevel@tonic-gate typedef struct s_x2node {
39177c478bd9Sstevel@tonic-gate   struct symbol *data;                  /* The data */
39187c478bd9Sstevel@tonic-gate   char *key;                   /* The key */
39197c478bd9Sstevel@tonic-gate   struct s_x2node *next;   /* Next entry with the same hash */
39207c478bd9Sstevel@tonic-gate   struct s_x2node **from;  /* Previous link */
39217c478bd9Sstevel@tonic-gate } x2node;
39227c478bd9Sstevel@tonic-gate 
39237c478bd9Sstevel@tonic-gate /* There is only one instance of the array, which is the following */
39247c478bd9Sstevel@tonic-gate static struct s_x2 *x2a;
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate /* Allocate a new associative array */
Symbol_init()39277c478bd9Sstevel@tonic-gate void Symbol_init(){
39287c478bd9Sstevel@tonic-gate   if( x2a ) return;
39297c478bd9Sstevel@tonic-gate   x2a = (struct s_x2*)malloc( sizeof(struct s_x2) );
39307c478bd9Sstevel@tonic-gate   if( x2a ){
39317c478bd9Sstevel@tonic-gate     x2a->size = 128;
39327c478bd9Sstevel@tonic-gate     x2a->count = 0;
3933*55fea89dSDan Cross     x2a->tbl = (x2node*)malloc(
39347c478bd9Sstevel@tonic-gate       (sizeof(x2node) + sizeof(x2node*))*128 );
39357c478bd9Sstevel@tonic-gate     if( x2a->tbl==0 ){
39367c478bd9Sstevel@tonic-gate       free(x2a);
39377c478bd9Sstevel@tonic-gate       x2a = 0;
39387c478bd9Sstevel@tonic-gate     }else{
39397c478bd9Sstevel@tonic-gate       int i;
39407c478bd9Sstevel@tonic-gate       x2a->ht = (x2node**)&(x2a->tbl[128]);
39417c478bd9Sstevel@tonic-gate       for(i=0; i<128; i++) x2a->ht[i] = 0;
39427c478bd9Sstevel@tonic-gate     }
39437c478bd9Sstevel@tonic-gate   }
39447c478bd9Sstevel@tonic-gate }
39457c478bd9Sstevel@tonic-gate /* Insert a new record into the array.  Return TRUE if successful.
39467c478bd9Sstevel@tonic-gate ** Prior data with the same key is NOT overwritten */
Symbol_insert(data,key)39477c478bd9Sstevel@tonic-gate int Symbol_insert(data,key)
39487c478bd9Sstevel@tonic-gate struct symbol *data;
39497c478bd9Sstevel@tonic-gate char *key;
39507c478bd9Sstevel@tonic-gate {
39517c478bd9Sstevel@tonic-gate   x2node *np;
39527c478bd9Sstevel@tonic-gate   int h;
39537c478bd9Sstevel@tonic-gate   int ph;
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate   if( x2a==0 ) return 0;
39567c478bd9Sstevel@tonic-gate   ph = strhash(key);
39577c478bd9Sstevel@tonic-gate   h = ph & (x2a->size-1);
39587c478bd9Sstevel@tonic-gate   np = x2a->ht[h];
39597c478bd9Sstevel@tonic-gate   while( np ){
39607c478bd9Sstevel@tonic-gate     if( strcmp(np->key,key)==0 ){
39617c478bd9Sstevel@tonic-gate       /* An existing entry with the same key is found. */
39627c478bd9Sstevel@tonic-gate       /* Fail because overwrite is not allows. */
39637c478bd9Sstevel@tonic-gate       return 0;
39647c478bd9Sstevel@tonic-gate     }
39657c478bd9Sstevel@tonic-gate     np = np->next;
39667c478bd9Sstevel@tonic-gate   }
39677c478bd9Sstevel@tonic-gate   if( x2a->count>=x2a->size ){
39687c478bd9Sstevel@tonic-gate     /* Need to make the hash table bigger */
39697c478bd9Sstevel@tonic-gate     int i,size;
39707c478bd9Sstevel@tonic-gate     struct s_x2 array;
39717c478bd9Sstevel@tonic-gate     array.size = size = x2a->size*2;
39727c478bd9Sstevel@tonic-gate     array.count = x2a->count;
39737c478bd9Sstevel@tonic-gate     array.tbl = (x2node*)malloc(
39747c478bd9Sstevel@tonic-gate       (sizeof(x2node) + sizeof(x2node*))*size );
39757c478bd9Sstevel@tonic-gate     if( array.tbl==0 ) return 0;  /* Fail due to malloc failure */
39767c478bd9Sstevel@tonic-gate     array.ht = (x2node**)&(array.tbl[size]);
39777c478bd9Sstevel@tonic-gate     for(i=0; i<size; i++) array.ht[i] = 0;
39787c478bd9Sstevel@tonic-gate     for(i=0; i<x2a->count; i++){
39797c478bd9Sstevel@tonic-gate       x2node *oldnp, *newnp;
39807c478bd9Sstevel@tonic-gate       oldnp = &(x2a->tbl[i]);
39817c478bd9Sstevel@tonic-gate       h = strhash(oldnp->key) & (size-1);
39827c478bd9Sstevel@tonic-gate       newnp = &(array.tbl[i]);
39837c478bd9Sstevel@tonic-gate       if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
39847c478bd9Sstevel@tonic-gate       newnp->next = array.ht[h];
39857c478bd9Sstevel@tonic-gate       newnp->key = oldnp->key;
39867c478bd9Sstevel@tonic-gate       newnp->data = oldnp->data;
39877c478bd9Sstevel@tonic-gate       newnp->from = &(array.ht[h]);
39887c478bd9Sstevel@tonic-gate       array.ht[h] = newnp;
39897c478bd9Sstevel@tonic-gate     }
39907c478bd9Sstevel@tonic-gate     free(x2a->tbl);
39917c478bd9Sstevel@tonic-gate     *x2a = array;
39927c478bd9Sstevel@tonic-gate   }
39937c478bd9Sstevel@tonic-gate   /* Insert the new data */
39947c478bd9Sstevel@tonic-gate   h = ph & (x2a->size-1);
39957c478bd9Sstevel@tonic-gate   np = &(x2a->tbl[x2a->count++]);
39967c478bd9Sstevel@tonic-gate   np->key = key;
39977c478bd9Sstevel@tonic-gate   np->data = data;
39987c478bd9Sstevel@tonic-gate   if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
39997c478bd9Sstevel@tonic-gate   np->next = x2a->ht[h];
40007c478bd9Sstevel@tonic-gate   x2a->ht[h] = np;
40017c478bd9Sstevel@tonic-gate   np->from = &(x2a->ht[h]);
40027c478bd9Sstevel@tonic-gate   return 1;
40037c478bd9Sstevel@tonic-gate }
40047c478bd9Sstevel@tonic-gate 
40057c478bd9Sstevel@tonic-gate /* Return a pointer to data assigned to the given key.  Return NULL
40067c478bd9Sstevel@tonic-gate ** if no such key. */
Symbol_find(key)40077c478bd9Sstevel@tonic-gate struct symbol *Symbol_find(key)
40087c478bd9Sstevel@tonic-gate char *key;
40097c478bd9Sstevel@tonic-gate {
40107c478bd9Sstevel@tonic-gate   int h;
40117c478bd9Sstevel@tonic-gate   x2node *np;
40127c478bd9Sstevel@tonic-gate 
40137c478bd9Sstevel@tonic-gate   if( x2a==0 ) return 0;
40147c478bd9Sstevel@tonic-gate   h = strhash(key) & (x2a->size-1);
40157c478bd9Sstevel@tonic-gate   np = x2a->ht[h];
40167c478bd9Sstevel@tonic-gate   while( np ){
40177c478bd9Sstevel@tonic-gate     if( strcmp(np->key,key)==0 ) break;
40187c478bd9Sstevel@tonic-gate     np = np->next;
40197c478bd9Sstevel@tonic-gate   }
40207c478bd9Sstevel@tonic-gate   return np ? np->data : 0;
40217c478bd9Sstevel@tonic-gate }
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate /* Return the n-th data.  Return NULL if n is out of range. */
Symbol_Nth(n)40247c478bd9Sstevel@tonic-gate struct symbol *Symbol_Nth(n)
40257c478bd9Sstevel@tonic-gate int n;
40267c478bd9Sstevel@tonic-gate {
40277c478bd9Sstevel@tonic-gate   struct symbol *data;
40287c478bd9Sstevel@tonic-gate   if( x2a && n>0 && n<=x2a->count ){
40297c478bd9Sstevel@tonic-gate     data = x2a->tbl[n-1].data;
40307c478bd9Sstevel@tonic-gate   }else{
40317c478bd9Sstevel@tonic-gate     data = 0;
40327c478bd9Sstevel@tonic-gate   }
40337c478bd9Sstevel@tonic-gate   return data;
40347c478bd9Sstevel@tonic-gate }
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate /* Return the size of the array */
Symbol_count()40377c478bd9Sstevel@tonic-gate int Symbol_count()
40387c478bd9Sstevel@tonic-gate {
40397c478bd9Sstevel@tonic-gate   return x2a ? x2a->count : 0;
40407c478bd9Sstevel@tonic-gate }
40417c478bd9Sstevel@tonic-gate 
40427c478bd9Sstevel@tonic-gate /* Return an array of pointers to all data in the table.
40437c478bd9Sstevel@tonic-gate ** The array is obtained from malloc.  Return NULL if memory allocation
40447c478bd9Sstevel@tonic-gate ** problems, or if the array is empty. */
Symbol_arrayof()40457c478bd9Sstevel@tonic-gate struct symbol **Symbol_arrayof()
40467c478bd9Sstevel@tonic-gate {
40477c478bd9Sstevel@tonic-gate   struct symbol **array;
40487c478bd9Sstevel@tonic-gate   int i,size;
40497c478bd9Sstevel@tonic-gate   if( x2a==0 ) return 0;
40507c478bd9Sstevel@tonic-gate   size = x2a->count;
40517c478bd9Sstevel@tonic-gate   array = (struct symbol **)malloc( sizeof(struct symbol *)*size );
40527c478bd9Sstevel@tonic-gate   if( array ){
40537c478bd9Sstevel@tonic-gate     for(i=0; i<size; i++) array[i] = x2a->tbl[i].data;
40547c478bd9Sstevel@tonic-gate   }
40557c478bd9Sstevel@tonic-gate   return array;
40567c478bd9Sstevel@tonic-gate }
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate /* Compare two configurations */
Configcmp(a,b)40597c478bd9Sstevel@tonic-gate int Configcmp(a,b)
40607c478bd9Sstevel@tonic-gate struct config *a;
40617c478bd9Sstevel@tonic-gate struct config *b;
40627c478bd9Sstevel@tonic-gate {
40637c478bd9Sstevel@tonic-gate   int x;
40647c478bd9Sstevel@tonic-gate   x = a->rp->index - b->rp->index;
40657c478bd9Sstevel@tonic-gate   if( x==0 ) x = a->dot - b->dot;
40667c478bd9Sstevel@tonic-gate   return x;
40677c478bd9Sstevel@tonic-gate }
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate /* Compare two states */
statecmp(a,b)40707c478bd9Sstevel@tonic-gate PRIVATE int statecmp(a,b)
40717c478bd9Sstevel@tonic-gate struct config *a;
40727c478bd9Sstevel@tonic-gate struct config *b;
40737c478bd9Sstevel@tonic-gate {
40747c478bd9Sstevel@tonic-gate   int rc;
40757c478bd9Sstevel@tonic-gate   for(rc=0; rc==0 && a && b;  a=a->bp, b=b->bp){
40767c478bd9Sstevel@tonic-gate     rc = a->rp->index - b->rp->index;
40777c478bd9Sstevel@tonic-gate     if( rc==0 ) rc = a->dot - b->dot;
40787c478bd9Sstevel@tonic-gate   }
40797c478bd9Sstevel@tonic-gate   if( rc==0 ){
40807c478bd9Sstevel@tonic-gate     if( a ) rc = 1;
40817c478bd9Sstevel@tonic-gate     if( b ) rc = -1;
40827c478bd9Sstevel@tonic-gate   }
40837c478bd9Sstevel@tonic-gate   return rc;
40847c478bd9Sstevel@tonic-gate }
40857c478bd9Sstevel@tonic-gate 
40867c478bd9Sstevel@tonic-gate /* Hash a state */
statehash(a)40877c478bd9Sstevel@tonic-gate PRIVATE int statehash(a)
40887c478bd9Sstevel@tonic-gate struct config *a;
40897c478bd9Sstevel@tonic-gate {
40907c478bd9Sstevel@tonic-gate   int h=0;
40917c478bd9Sstevel@tonic-gate   while( a ){
40927c478bd9Sstevel@tonic-gate     h = h*571 + a->rp->index*37 + a->dot;
40937c478bd9Sstevel@tonic-gate     a = a->bp;
40947c478bd9Sstevel@tonic-gate   }
40957c478bd9Sstevel@tonic-gate   return h;
40967c478bd9Sstevel@tonic-gate }
40977c478bd9Sstevel@tonic-gate 
40987c478bd9Sstevel@tonic-gate /* Allocate a new state structure */
State_new()40997c478bd9Sstevel@tonic-gate struct state *State_new()
41007c478bd9Sstevel@tonic-gate {
41017c478bd9Sstevel@tonic-gate   struct state *new;
41027c478bd9Sstevel@tonic-gate   new = (struct state *)malloc( sizeof(struct state) );
41037c478bd9Sstevel@tonic-gate   MemoryCheck(new);
41047c478bd9Sstevel@tonic-gate   return new;
41057c478bd9Sstevel@tonic-gate }
41067c478bd9Sstevel@tonic-gate 
41077c478bd9Sstevel@tonic-gate /* There is one instance of the following structure for each
41087c478bd9Sstevel@tonic-gate ** associative array of type "x3".
41097c478bd9Sstevel@tonic-gate */
41107c478bd9Sstevel@tonic-gate struct s_x3 {
41117c478bd9Sstevel@tonic-gate   int size;               /* The number of available slots. */
41127c478bd9Sstevel@tonic-gate                           /*   Must be a power of 2 greater than or */
41137c478bd9Sstevel@tonic-gate                           /*   equal to 1 */
41147c478bd9Sstevel@tonic-gate   int count;              /* Number of currently slots filled */
41157c478bd9Sstevel@tonic-gate   struct s_x3node *tbl;  /* The data stored here */
41167c478bd9Sstevel@tonic-gate   struct s_x3node **ht;  /* Hash table for lookups */
41177c478bd9Sstevel@tonic-gate };
41187c478bd9Sstevel@tonic-gate 
41197c478bd9Sstevel@tonic-gate /* There is one instance of this structure for every data element
41207c478bd9Sstevel@tonic-gate ** in an associative array of type "x3".
41217c478bd9Sstevel@tonic-gate */
41227c478bd9Sstevel@tonic-gate typedef struct s_x3node {
41237c478bd9Sstevel@tonic-gate   struct state *data;                  /* The data */
41247c478bd9Sstevel@tonic-gate   struct config *key;                   /* The key */
41257c478bd9Sstevel@tonic-gate   struct s_x3node *next;   /* Next entry with the same hash */
41267c478bd9Sstevel@tonic-gate   struct s_x3node **from;  /* Previous link */
41277c478bd9Sstevel@tonic-gate } x3node;
41287c478bd9Sstevel@tonic-gate 
41297c478bd9Sstevel@tonic-gate /* There is only one instance of the array, which is the following */
41307c478bd9Sstevel@tonic-gate static struct s_x3 *x3a;
41317c478bd9Sstevel@tonic-gate 
41327c478bd9Sstevel@tonic-gate /* Allocate a new associative array */
State_init()41337c478bd9Sstevel@tonic-gate void State_init(){
41347c478bd9Sstevel@tonic-gate   if( x3a ) return;
41357c478bd9Sstevel@tonic-gate   x3a = (struct s_x3*)malloc( sizeof(struct s_x3) );
41367c478bd9Sstevel@tonic-gate   if( x3a ){
41377c478bd9Sstevel@tonic-gate     x3a->size = 128;
41387c478bd9Sstevel@tonic-gate     x3a->count = 0;
4139*55fea89dSDan Cross     x3a->tbl = (x3node*)malloc(
41407c478bd9Sstevel@tonic-gate       (sizeof(x3node) + sizeof(x3node*))*128 );
41417c478bd9Sstevel@tonic-gate     if( x3a->tbl==0 ){
41427c478bd9Sstevel@tonic-gate       free(x3a);
41437c478bd9Sstevel@tonic-gate       x3a = 0;
41447c478bd9Sstevel@tonic-gate     }else{
41457c478bd9Sstevel@tonic-gate       int i;
41467c478bd9Sstevel@tonic-gate       x3a->ht = (x3node**)&(x3a->tbl[128]);
41477c478bd9Sstevel@tonic-gate       for(i=0; i<128; i++) x3a->ht[i] = 0;
41487c478bd9Sstevel@tonic-gate     }
41497c478bd9Sstevel@tonic-gate   }
41507c478bd9Sstevel@tonic-gate }
41517c478bd9Sstevel@tonic-gate /* Insert a new record into the array.  Return TRUE if successful.
41527c478bd9Sstevel@tonic-gate ** Prior data with the same key is NOT overwritten */
State_insert(data,key)41537c478bd9Sstevel@tonic-gate int State_insert(data,key)
41547c478bd9Sstevel@tonic-gate struct state *data;
41557c478bd9Sstevel@tonic-gate struct config *key;
41567c478bd9Sstevel@tonic-gate {
41577c478bd9Sstevel@tonic-gate   x3node *np;
41587c478bd9Sstevel@tonic-gate   int h;
41597c478bd9Sstevel@tonic-gate   int ph;
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate   if( x3a==0 ) return 0;
41627c478bd9Sstevel@tonic-gate   ph = statehash(key);
41637c478bd9Sstevel@tonic-gate   h = ph & (x3a->size-1);
41647c478bd9Sstevel@tonic-gate   np = x3a->ht[h];
41657c478bd9Sstevel@tonic-gate   while( np ){
41667c478bd9Sstevel@tonic-gate     if( statecmp(np->key,key)==0 ){
41677c478bd9Sstevel@tonic-gate       /* An existing entry with the same key is found. */
41687c478bd9Sstevel@tonic-gate       /* Fail because overwrite is not allows. */
41697c478bd9Sstevel@tonic-gate       return 0;
41707c478bd9Sstevel@tonic-gate     }
41717c478bd9Sstevel@tonic-gate     np = np->next;
41727c478bd9Sstevel@tonic-gate   }
41737c478bd9Sstevel@tonic-gate   if( x3a->count>=x3a->size ){
41747c478bd9Sstevel@tonic-gate     /* Need to make the hash table bigger */
41757c478bd9Sstevel@tonic-gate     int i,size;
41767c478bd9Sstevel@tonic-gate     struct s_x3 array;
41777c478bd9Sstevel@tonic-gate     array.size = size = x3a->size*2;
41787c478bd9Sstevel@tonic-gate     array.count = x3a->count;
41797c478bd9Sstevel@tonic-gate     array.tbl = (x3node*)malloc(
41807c478bd9Sstevel@tonic-gate       (sizeof(x3node) + sizeof(x3node*))*size );
41817c478bd9Sstevel@tonic-gate     if( array.tbl==0 ) return 0;  /* Fail due to malloc failure */
41827c478bd9Sstevel@tonic-gate     array.ht = (x3node**)&(array.tbl[size]);
41837c478bd9Sstevel@tonic-gate     for(i=0; i<size; i++) array.ht[i] = 0;
41847c478bd9Sstevel@tonic-gate     for(i=0; i<x3a->count; i++){
41857c478bd9Sstevel@tonic-gate       x3node *oldnp, *newnp;
41867c478bd9Sstevel@tonic-gate       oldnp = &(x3a->tbl[i]);
41877c478bd9Sstevel@tonic-gate       h = statehash(oldnp->key) & (size-1);
41887c478bd9Sstevel@tonic-gate       newnp = &(array.tbl[i]);
41897c478bd9Sstevel@tonic-gate       if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
41907c478bd9Sstevel@tonic-gate       newnp->next = array.ht[h];
41917c478bd9Sstevel@tonic-gate       newnp->key = oldnp->key;
41927c478bd9Sstevel@tonic-gate       newnp->data = oldnp->data;
41937c478bd9Sstevel@tonic-gate       newnp->from = &(array.ht[h]);
41947c478bd9Sstevel@tonic-gate       array.ht[h] = newnp;
41957c478bd9Sstevel@tonic-gate     }
41967c478bd9Sstevel@tonic-gate     free(x3a->tbl);
41977c478bd9Sstevel@tonic-gate     *x3a = array;
41987c478bd9Sstevel@tonic-gate   }
41997c478bd9Sstevel@tonic-gate   /* Insert the new data */
42007c478bd9Sstevel@tonic-gate   h = ph & (x3a->size-1);
42017c478bd9Sstevel@tonic-gate   np = &(x3a->tbl[x3a->count++]);
42027c478bd9Sstevel@tonic-gate   np->key = key;
42037c478bd9Sstevel@tonic-gate   np->data = data;
42047c478bd9Sstevel@tonic-gate   if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
42057c478bd9Sstevel@tonic-gate   np->next = x3a->ht[h];
42067c478bd9Sstevel@tonic-gate   x3a->ht[h] = np;
42077c478bd9Sstevel@tonic-gate   np->from = &(x3a->ht[h]);
42087c478bd9Sstevel@tonic-gate   return 1;
42097c478bd9Sstevel@tonic-gate }
42107c478bd9Sstevel@tonic-gate 
42117c478bd9Sstevel@tonic-gate /* Return a pointer to data assigned to the given key.  Return NULL
42127c478bd9Sstevel@tonic-gate ** if no such key. */
State_find(key)42137c478bd9Sstevel@tonic-gate struct state *State_find(key)
42147c478bd9Sstevel@tonic-gate struct config *key;
42157c478bd9Sstevel@tonic-gate {
42167c478bd9Sstevel@tonic-gate   int h;
42177c478bd9Sstevel@tonic-gate   x3node *np;
42187c478bd9Sstevel@tonic-gate 
42197c478bd9Sstevel@tonic-gate   if( x3a==0 ) return 0;
42207c478bd9Sstevel@tonic-gate   h = statehash(key) & (x3a->size-1);
42217c478bd9Sstevel@tonic-gate   np = x3a->ht[h];
42227c478bd9Sstevel@tonic-gate   while( np ){
42237c478bd9Sstevel@tonic-gate     if( statecmp(np->key,key)==0 ) break;
42247c478bd9Sstevel@tonic-gate     np = np->next;
42257c478bd9Sstevel@tonic-gate   }
42267c478bd9Sstevel@tonic-gate   return np ? np->data : 0;
42277c478bd9Sstevel@tonic-gate }
42287c478bd9Sstevel@tonic-gate 
42297c478bd9Sstevel@tonic-gate /* Return an array of pointers to all data in the table.
42307c478bd9Sstevel@tonic-gate ** The array is obtained from malloc.  Return NULL if memory allocation
42317c478bd9Sstevel@tonic-gate ** problems, or if the array is empty. */
State_arrayof()42327c478bd9Sstevel@tonic-gate struct state **State_arrayof()
42337c478bd9Sstevel@tonic-gate {
42347c478bd9Sstevel@tonic-gate   struct state **array;
42357c478bd9Sstevel@tonic-gate   int i,size;
42367c478bd9Sstevel@tonic-gate   if( x3a==0 ) return 0;
42377c478bd9Sstevel@tonic-gate   size = x3a->count;
42387c478bd9Sstevel@tonic-gate   array = (struct state **)malloc( sizeof(struct state *)*size );
42397c478bd9Sstevel@tonic-gate   if( array ){
42407c478bd9Sstevel@tonic-gate     for(i=0; i<size; i++) array[i] = x3a->tbl[i].data;
42417c478bd9Sstevel@tonic-gate   }
42427c478bd9Sstevel@tonic-gate   return array;
42437c478bd9Sstevel@tonic-gate }
42447c478bd9Sstevel@tonic-gate 
42457c478bd9Sstevel@tonic-gate /* Hash a configuration */
confighash(a)42467c478bd9Sstevel@tonic-gate PRIVATE int confighash(a)
42477c478bd9Sstevel@tonic-gate struct config *a;
42487c478bd9Sstevel@tonic-gate {
42497c478bd9Sstevel@tonic-gate   int h=0;
42507c478bd9Sstevel@tonic-gate   h = h*571 + a->rp->index*37 + a->dot;
42517c478bd9Sstevel@tonic-gate   return h;
42527c478bd9Sstevel@tonic-gate }
42537c478bd9Sstevel@tonic-gate 
42547c478bd9Sstevel@tonic-gate /* There is one instance of the following structure for each
42557c478bd9Sstevel@tonic-gate ** associative array of type "x4".
42567c478bd9Sstevel@tonic-gate */
42577c478bd9Sstevel@tonic-gate struct s_x4 {
42587c478bd9Sstevel@tonic-gate   int size;               /* The number of available slots. */
42597c478bd9Sstevel@tonic-gate                           /*   Must be a power of 2 greater than or */
42607c478bd9Sstevel@tonic-gate                           /*   equal to 1 */
42617c478bd9Sstevel@tonic-gate   int count;              /* Number of currently slots filled */
42627c478bd9Sstevel@tonic-gate   struct s_x4node *tbl;  /* The data stored here */
42637c478bd9Sstevel@tonic-gate   struct s_x4node **ht;  /* Hash table for lookups */
42647c478bd9Sstevel@tonic-gate };
42657c478bd9Sstevel@tonic-gate 
42667c478bd9Sstevel@tonic-gate /* There is one instance of this structure for every data element
42677c478bd9Sstevel@tonic-gate ** in an associative array of type "x4".
42687c478bd9Sstevel@tonic-gate */
42697c478bd9Sstevel@tonic-gate typedef struct s_x4node {
42707c478bd9Sstevel@tonic-gate   struct config *data;                  /* The data */
42717c478bd9Sstevel@tonic-gate   struct s_x4node *next;   /* Next entry with the same hash */
42727c478bd9Sstevel@tonic-gate   struct s_x4node **from;  /* Previous link */
42737c478bd9Sstevel@tonic-gate } x4node;
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate /* There is only one instance of the array, which is the following */
42767c478bd9Sstevel@tonic-gate static struct s_x4 *x4a;
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate /* Allocate a new associative array */
Configtable_init()42797c478bd9Sstevel@tonic-gate void Configtable_init(){
42807c478bd9Sstevel@tonic-gate   if( x4a ) return;
42817c478bd9Sstevel@tonic-gate   x4a = (struct s_x4*)malloc( sizeof(struct s_x4) );
42827c478bd9Sstevel@tonic-gate   if( x4a ){
42837c478bd9Sstevel@tonic-gate     x4a->size = 64;
42847c478bd9Sstevel@tonic-gate     x4a->count = 0;
4285*55fea89dSDan Cross     x4a->tbl = (x4node*)malloc(
42867c478bd9Sstevel@tonic-gate       (sizeof(x4node) + sizeof(x4node*))*64 );
42877c478bd9Sstevel@tonic-gate     if( x4a->tbl==0 ){
42887c478bd9Sstevel@tonic-gate       free(x4a);
42897c478bd9Sstevel@tonic-gate       x4a = 0;
42907c478bd9Sstevel@tonic-gate     }else{
42917c478bd9Sstevel@tonic-gate       int i;
42927c478bd9Sstevel@tonic-gate       x4a->ht = (x4node**)&(x4a->tbl[64]);
42937c478bd9Sstevel@tonic-gate       for(i=0; i<64; i++) x4a->ht[i] = 0;
42947c478bd9Sstevel@tonic-gate     }
42957c478bd9Sstevel@tonic-gate   }
42967c478bd9Sstevel@tonic-gate }
42977c478bd9Sstevel@tonic-gate /* Insert a new record into the array.  Return TRUE if successful.
42987c478bd9Sstevel@tonic-gate ** Prior data with the same key is NOT overwritten */
Configtable_insert(data)42997c478bd9Sstevel@tonic-gate int Configtable_insert(data)
43007c478bd9Sstevel@tonic-gate struct config *data;
43017c478bd9Sstevel@tonic-gate {
43027c478bd9Sstevel@tonic-gate   x4node *np;
43037c478bd9Sstevel@tonic-gate   int h;
43047c478bd9Sstevel@tonic-gate   int ph;
43057c478bd9Sstevel@tonic-gate 
43067c478bd9Sstevel@tonic-gate   if( x4a==0 ) return 0;
43077c478bd9Sstevel@tonic-gate   ph = confighash(data);
43087c478bd9Sstevel@tonic-gate   h = ph & (x4a->size-1);
43097c478bd9Sstevel@tonic-gate   np = x4a->ht[h];
43107c478bd9Sstevel@tonic-gate   while( np ){
43117c478bd9Sstevel@tonic-gate     if( Configcmp(np->data,data)==0 ){
43127c478bd9Sstevel@tonic-gate       /* An existing entry with the same key is found. */
43137c478bd9Sstevel@tonic-gate       /* Fail because overwrite is not allows. */
43147c478bd9Sstevel@tonic-gate       return 0;
43157c478bd9Sstevel@tonic-gate     }
43167c478bd9Sstevel@tonic-gate     np = np->next;
43177c478bd9Sstevel@tonic-gate   }
43187c478bd9Sstevel@tonic-gate   if( x4a->count>=x4a->size ){
43197c478bd9Sstevel@tonic-gate     /* Need to make the hash table bigger */
43207c478bd9Sstevel@tonic-gate     int i,size;
43217c478bd9Sstevel@tonic-gate     struct s_x4 array;
43227c478bd9Sstevel@tonic-gate     array.size = size = x4a->size*2;
43237c478bd9Sstevel@tonic-gate     array.count = x4a->count;
43247c478bd9Sstevel@tonic-gate     array.tbl = (x4node*)malloc(
43257c478bd9Sstevel@tonic-gate       (sizeof(x4node) + sizeof(x4node*))*size );
43267c478bd9Sstevel@tonic-gate     if( array.tbl==0 ) return 0;  /* Fail due to malloc failure */
43277c478bd9Sstevel@tonic-gate     array.ht = (x4node**)&(array.tbl[size]);
43287c478bd9Sstevel@tonic-gate     for(i=0; i<size; i++) array.ht[i] = 0;
43297c478bd9Sstevel@tonic-gate     for(i=0; i<x4a->count; i++){
43307c478bd9Sstevel@tonic-gate       x4node *oldnp, *newnp;
43317c478bd9Sstevel@tonic-gate       oldnp = &(x4a->tbl[i]);
43327c478bd9Sstevel@tonic-gate       h = confighash(oldnp->data) & (size-1);
43337c478bd9Sstevel@tonic-gate       newnp = &(array.tbl[i]);
43347c478bd9Sstevel@tonic-gate       if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
43357c478bd9Sstevel@tonic-gate       newnp->next = array.ht[h];
43367c478bd9Sstevel@tonic-gate       newnp->data = oldnp->data;
43377c478bd9Sstevel@tonic-gate       newnp->from = &(array.ht[h]);
43387c478bd9Sstevel@tonic-gate       array.ht[h] = newnp;
43397c478bd9Sstevel@tonic-gate     }
43407c478bd9Sstevel@tonic-gate     free(x4a->tbl);
43417c478bd9Sstevel@tonic-gate     *x4a = array;
43427c478bd9Sstevel@tonic-gate   }
43437c478bd9Sstevel@tonic-gate   /* Insert the new data */
43447c478bd9Sstevel@tonic-gate   h = ph & (x4a->size-1);
43457c478bd9Sstevel@tonic-gate   np = &(x4a->tbl[x4a->count++]);
43467c478bd9Sstevel@tonic-gate   np->data = data;
43477c478bd9Sstevel@tonic-gate   if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
43487c478bd9Sstevel@tonic-gate   np->next = x4a->ht[h];
43497c478bd9Sstevel@tonic-gate   x4a->ht[h] = np;
43507c478bd9Sstevel@tonic-gate   np->from = &(x4a->ht[h]);
43517c478bd9Sstevel@tonic-gate   return 1;
43527c478bd9Sstevel@tonic-gate }
43537c478bd9Sstevel@tonic-gate 
43547c478bd9Sstevel@tonic-gate /* Return a pointer to data assigned to the given key.  Return NULL
43557c478bd9Sstevel@tonic-gate ** if no such key. */
Configtable_find(key)43567c478bd9Sstevel@tonic-gate struct config *Configtable_find(key)
43577c478bd9Sstevel@tonic-gate struct config *key;
43587c478bd9Sstevel@tonic-gate {
43597c478bd9Sstevel@tonic-gate   int h;
43607c478bd9Sstevel@tonic-gate   x4node *np;
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate   if( x4a==0 ) return 0;
43637c478bd9Sstevel@tonic-gate   h = confighash(key) & (x4a->size-1);
43647c478bd9Sstevel@tonic-gate   np = x4a->ht[h];
43657c478bd9Sstevel@tonic-gate   while( np ){
43667c478bd9Sstevel@tonic-gate     if( Configcmp(np->data,key)==0 ) break;
43677c478bd9Sstevel@tonic-gate     np = np->next;
43687c478bd9Sstevel@tonic-gate   }
43697c478bd9Sstevel@tonic-gate   return np ? np->data : 0;
43707c478bd9Sstevel@tonic-gate }
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate /* Remove all data from the table.  Pass each data to the function "f"
43737c478bd9Sstevel@tonic-gate ** as it is removed.  ("f" may be null to avoid this step.) */
43747c478bd9Sstevel@tonic-gate void Configtable_clear(f)
43757c478bd9Sstevel@tonic-gate int(*f)(/* struct config * */);
43767c478bd9Sstevel@tonic-gate {
43777c478bd9Sstevel@tonic-gate   int i;
43787c478bd9Sstevel@tonic-gate   if( x4a==0 || x4a->count==0 ) return;
43797c478bd9Sstevel@tonic-gate   if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
43807c478bd9Sstevel@tonic-gate   for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
43817c478bd9Sstevel@tonic-gate   x4a->count = 0;
43827c478bd9Sstevel@tonic-gate   return;
43837c478bd9Sstevel@tonic-gate }
4384