xref: /illumos-gate/usr/src/lib/libsqlite/tool/lemon.c (revision 88e1588b)
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.
5737c478bd9Sstevel@tonic-gate **
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   }
8637c478bd9Sstevel@tonic-gate 
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 
8957c478bd9Sstevel@tonic-gate   /* 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{
10117c478bd9Sstevel@tonic-gate     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");
13437c478bd9Sstevel@tonic-gate      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(