misc/lemon.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 17 Feb 2010 15:34:43 -0500
changeset 832 375ac4fcdc51
parent 831 80e47b096ecd
child 833 cd7c278acf02
permissions -rw-r--r--
Lemon fix: incorrect printf format strings.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
710
cbcba3ea6a05 Hacks to lemon parser generator to fit my needs.
Ryan C. Gordon <icculus@icculus.org>
parents: 708
diff changeset
     1
/*
cbcba3ea6a05 Hacks to lemon parser generator to fit my needs.
Ryan C. Gordon <icculus@icculus.org>
parents: 708
diff changeset
     2
 * My changes over the original lemon.c from SQLite are encased in
cbcba3ea6a05 Hacks to lemon parser generator to fit my needs.
Ryan C. Gordon <icculus@icculus.org>
parents: 708
diff changeset
     3
 *  #if __MOJOSHADER__ blocks.  --ryan.
cbcba3ea6a05 Hacks to lemon parser generator to fit my needs.
Ryan C. Gordon <icculus@icculus.org>
parents: 708
diff changeset
     4
 */
cbcba3ea6a05 Hacks to lemon parser generator to fit my needs.
Ryan C. Gordon <icculus@icculus.org>
parents: 708
diff changeset
     5
#define __MOJOSHADER__ 1
cbcba3ea6a05 Hacks to lemon parser generator to fit my needs.
Ryan C. Gordon <icculus@icculus.org>
parents: 708
diff changeset
     6
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
** This file contains all sources (including headers) to the LEMON
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
** LALR(1) parser generator.  The sources have been combined into a
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
** single file to make it easy to include LEMON in the source tree
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
** and Makefile of another program.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
**
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
** The author of this program disclaims copyright.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
#include <stdio.h>
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
#include <stdarg.h>
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
#include <string.h>
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
#include <ctype.h>
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
#include <stdlib.h>
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
#include <assert.h>
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
#ifndef __WIN32__
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
#   if defined(_WIN32) || defined(WIN32)
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
#	define __WIN32__
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
#   endif
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
#endif
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#ifdef __WIN32__
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
extern int access();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#else
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
#include <unistd.h>
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#endif
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
/* #define PRIVATE static */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
#define PRIVATE
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#ifdef TEST
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#define MAXRHS 5       /* Set low to exercise exception code */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
#else
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
#define MAXRHS 1000
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#endif
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
829
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    43
static const char **made_files = NULL;
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    44
static int made_files_count = 0;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    45
static int successful_exit = 0;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    46
static void LemonAtExit(void)
829
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    47
{
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    48
    /* if we failed, delete (most) files we made, to unconfuse build tools. */
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    49
    int i;
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    50
    for (i = 0; i < made_files_count; i++) {
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    51
        if (!successful_exit) {
829
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    52
            remove(made_files[i]);
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    53
        }
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    54
        free((void *) made_files[i]);
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    55
    }
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    56
    free(made_files);
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    57
    made_files_count = 0;
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    58
    made_files = NULL;
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    59
}
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    60
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
static char *msort(char*,char**,int(*)(const char*,const char*));
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
** Compilers are getting increasingly pedantic about type conversions
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
** as C evolves ever closer to Ada....  To work around the latest problems
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
** we have to define the following variant of strlen().
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
#define lemonStrlen(X)   ((int)strlen(X))
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    70
/* a few forward declarations... */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    71
struct rule;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    72
struct lemon;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    73
struct action;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    74
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
static struct action *Action_new(void);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
static struct action *Action_sort(struct action *);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
/********** From the file "build.h" ************************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
void FindRulePrecedences();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
void FindFirstSets();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
void FindStates();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
void FindLinks();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
void FindFollowSets();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
void FindActions();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
/********* From the file "configlist.h" *********************************/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    87
void Configlist_init(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    88
struct config *Configlist_add(struct rule *, int);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    89
struct config *Configlist_addbasis(struct rule *, int);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    90
void Configlist_closure(struct lemon *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    91
void Configlist_sort(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    92
void Configlist_sortbasis(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    93
struct config *Configlist_return(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    94
struct config *Configlist_basis(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    95
void Configlist_eat(struct config *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    96
void Configlist_reset(void);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
/********* From the file "error.h" ***************************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
void ErrorMsg(const char *, int,const char *, ...);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
/****** From the file "option.h" ******************************************/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   102
enum option_type { OPT_FLAG=1,  OPT_INT,  OPT_DBL,  OPT_STR,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   103
         OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR};
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
struct s_options {
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   105
  enum option_type type;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   106
  const char *label;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
  char *arg;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   108
  const char *message;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
};
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   110
int    OptInit(char**,struct s_options*,FILE*);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   111
int    OptNArgs(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   112
char  *OptArg(int);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   113
void   OptErr(int);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   114
void   OptPrint(void);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
/******** From the file "parse.h" *****************************************/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   117
void Parse(struct lemon *lemp);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
/********* From the file "plink.h" ***************************************/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   120
struct plink *Plink_new(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   121
void Plink_add(struct plink **, struct config *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   122
void Plink_copy(struct plink **, struct plink *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   123
void Plink_delete(struct plink *);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
/********** From the file "report.h" *************************************/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   126
void Reprint(struct lemon *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   127
void ReportOutput(struct lemon *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   128
void ReportTable(struct lemon *, int);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   129
void ReportHeader(struct lemon *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   130
void CompressTables(struct lemon *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   131
void ResortStates(struct lemon *);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
/********** From the file "set.h" ****************************************/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   134
void  SetSize(int);             /* All sets will be of size N */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   135
char *SetNew(void);               /* A new set for element 0..N */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   136
void  SetFree(char*);             /* Deallocate a set */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   137
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   138
char *SetNew(void);               /* A new set for element 0..N */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   139
int SetAdd(char*,int);            /* Add element to a set */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   140
int SetUnion(char *,char *);    /* A <- A U B, thru element N */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
#define SetFind(X,Y) (X[Y])       /* True if Y is in set X */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
/********** From the file "struct.h" *************************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
** Principal data structures for the LEMON parser generator.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
/* Symbols (terminals and nonterminals) of the grammar are stored
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
** in the following: */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   152
enum symbol_type {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   153
  TERMINAL,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   154
  NONTERMINAL,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   155
  MULTITERMINAL
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   156
};
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   157
enum e_assoc {
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    LEFT,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    RIGHT,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    NONE,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    UNK
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   162
};
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   163
struct symbol {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   164
  const char *name;        /* Name of the symbol */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   165
  int index;               /* Index number for this symbol */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   166
  enum symbol_type type;   /* Symbols are all either TERMINALS or NTs */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   167
  struct rule *rule;       /* Linked list of rules of this (if an NT) */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   168
  struct symbol *fallback; /* fallback token in case this token doesn't parse */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   169
  int prec;                /* Precedence if defined (-1 otherwise) */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   170
  enum e_assoc assoc;      /* Associativity if precedence is defined */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
  char *firstset;          /* First-set for all rules of this symbol */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
  Boolean lambda;          /* True if NT and can generate an empty string */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
  int useCnt;              /* Number of times used */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
  char *destructor;        /* Code which executes whenever this symbol is
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
                           ** popped from the stack during error processing */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
  int destLineno;          /* Line number for start of destructor */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
  char *datatype;          /* The data type of information held by this
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
                           ** object. Only used if type==NONTERMINAL */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
  int dtnum;               /* The data type number.  In the parser, the value
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
                           ** stack is a union.  The .yy%d element of this
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
                           ** union is the correct data type for this object */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
  /* The following fields are used by MULTITERMINALs only */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
  int nsubsym;             /* Number of constituent symbols in the MULTI */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
  struct symbol **subsym;  /* Array of constituent symbols */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
/* Each production rule in the grammar is stored in the following
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
** structure.  */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
struct rule {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
  struct symbol *lhs;      /* Left-hand side of the rule */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   191
  const char *lhsalias;    /* Alias for the LHS (NULL if none) */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
  int lhsStart;            /* True if left-hand side is the start symbol */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
  int ruleline;            /* Line number for the rule */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
  int nrhs;                /* Number of RHS symbols */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
  struct symbol **rhs;     /* The RHS symbols */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   196
  const char **rhsalias;   /* An alias for each RHS symbol (NULL if none) */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
  int line;                /* Line number at which code begins */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   198
  const char *code;        /* The code executed when this rule is reduced */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
  struct symbol *precsym;  /* Precedence symbol for this rule */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
  int index;               /* An index number for this rule */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
  Boolean canReduce;       /* True if this rule is ever reduced */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
  struct rule *nextlhs;    /* Next rule with the same LHS */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
  struct rule *next;       /* Next rule in the global list */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
/* A configuration is a production rule of the grammar together with
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
** a mark (dot) showing how much of that rule has been processed so far.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
** Configurations also contain a follow-set which is a list of terminal
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
** symbols which are allowed to immediately follow the end of the rule.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
** Every configuration is recorded as an instance of the following: */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   211
enum cfgstatus {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   212
  COMPLETE,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   213
  INCOMPLETE
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   214
};
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
struct config {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
  struct rule *rp;         /* The rule upon which the configuration is based */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
  int dot;                 /* The parse point */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
  char *fws;               /* Follow-set for this configuration only */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
  struct plink *fplp;      /* Follow-set forward propagation links */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
  struct plink *bplp;      /* Follow-set backwards propagation links */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
  struct state *stp;       /* Pointer to state which contains this */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   222
  enum cfgstatus status;   /* used during followset and shift computations */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
  struct config *next;     /* Next configuration in the state */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
  struct config *bp;       /* The next basis configuration */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   227
enum e_action {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   228
  SHIFT,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   229
  ACCEPT,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   230
  REDUCE,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   231
  ERROR,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   232
  SSCONFLICT,              /* A shift/shift conflict */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   233
  SRCONFLICT,              /* Was a reduce, but part of a conflict */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   234
  RRCONFLICT,              /* Was a reduce, but part of a conflict */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   235
  SH_RESOLVED,             /* Was a shift.  Precedence resolved conflict */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   236
  RD_RESOLVED,             /* Was reduce.  Precedence resolved conflict */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   237
  NOT_USED                 /* Deleted by compression */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   238
};
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   239
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
/* Every shift or reduce operation is stored as one of the following */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
struct action {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
  struct symbol *sp;       /* The look-ahead symbol */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   243
  enum e_action type;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
  union {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    struct state *stp;     /* The new state, if a shift */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    struct rule *rp;       /* The rule, if a reduce */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
  } x;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
  struct action *next;     /* Next action for this state */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
  struct action *collide;  /* Next action with the same hash */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
/* Each state of the generated parser's finite state machine
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
** is encoded as an instance of the following structure. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
struct state {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
  struct config *bp;       /* The basis configurations for this state */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
  struct config *cfp;      /* All configurations in this set */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
  int statenum;            /* Sequential number for this state */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
  struct action *ap;       /* Array of actions for this state */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
  int nTknAct, nNtAct;     /* Number of actions on terminals and nonterminals */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
  int iTknOfst, iNtOfst;   /* yy_action[] offset for terminals and nonterms */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
  int iDflt;               /* Default action */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
#define NO_OFFSET (-2147483647)
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
/* A followset propagation link indicates that the contents of one
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
** configuration followset should be propagated to another whenever
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
** the first changes. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
struct plink {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
  struct config *cfp;      /* The configuration to which linked */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
  struct plink *next;      /* The next propagate link */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
/* The state vector for the entire parser generator is recorded as
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
** follows.  (LEMON uses no global variables and makes little use of
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
** static variables.  Fields in the following structure can be thought
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
** of as begin global variables in the program.) */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
struct lemon {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
  struct state **sorted;   /* Table of states sorted by state number */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
  struct rule *rule;       /* List of all rules */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
  int nstate;              /* Number of states */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
  int nrule;               /* Number of rules */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
  int nsymbol;             /* Number of terminal and nonterminal symbols */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
  int nterminal;           /* Number of terminal symbols */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
  struct symbol **symbols; /* Sorted array of pointers to symbols */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
  int errorcnt;            /* Number of errors */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
  struct symbol *errsym;   /* The error symbol */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
  struct symbol *wildcard; /* Token that matches anything */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
  char *name;              /* Name of the generated parser */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
  char *arg;               /* Declaration of the 3th argument to parser */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
  char *tokentype;         /* Type of terminal symbols in the parser stack */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
  char *vartype;           /* The default type of non-terminal symbols */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
  char *start;             /* Name of the start symbol for the grammar */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
  char *stacksize;         /* Size of the parser stack */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
  char *include;           /* Code to put at the start of the C file */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
  char *error;             /* Code to execute when an error is seen */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
  char *overflow;          /* Code to execute on a stack overflow */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
  char *failure;           /* Code to execute on parser failure */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
  char *accept;            /* Code to execute when the parser excepts */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
  char *extracode;         /* Code appended to the generated file */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
  char *tokendest;         /* Code to execute to destroy token data */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
  char *vardest;           /* Code for the default non-terminal destructor */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
  char *filename;          /* Name of the input file */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
  char *outname;           /* Name of the current output file */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
  char *tokenprefix;       /* A prefix added to token names in the .h file */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
  int nconflict;           /* Number of parsing conflicts */
809
d0fcadcc93b4 Forgot to wrap this in a #if __MOJOSHADER__.
Ryan C. Gordon <icculus@icculus.org>
parents: 786
diff changeset
   306
#if __MOJOSHADER__
786
795e7762da7f Modified Lemon to accept a specific amount of conflicts.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
   307
  int nexpected;           /* Number of expected parsing conflicts */
809
d0fcadcc93b4 Forgot to wrap this in a #if __MOJOSHADER__.
Ryan C. Gordon <icculus@icculus.org>
parents: 786
diff changeset
   308
#endif
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
  int tablesize;           /* Size of the parse tables */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
  int basisflag;           /* Print only basis configurations */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
  int has_fallback;        /* True if any %fallback is seen in the grammar */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
  int nolinenosflag;       /* True if #line statements should not be printed */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
  char *argv0;             /* Name of the program */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
#define MemoryCheck(X) if((X)==0){ \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
  extern void memory_error(); \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
  memory_error(); \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
/**************** From the file "table.h" *********************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
** All code in this file has been automatically generated
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
** from a specification in the file
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
**              "table.q"
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
** by the associative array code building program "aagen".
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
** Do not edit this file!  Instead, edit the specification
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
** file, then rerun aagen.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
** Code for processing tables in the LEMON parser generator.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
/* Routines for handling a strings */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   335
const char *Strsafe(const char *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   336
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   337
void Strsafe_init(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   338
int Strsafe_insert(const char *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   339
const char *Strsafe_find(const char *);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
/* Routines for handling symbols of the grammar */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   343
struct symbol *Symbol_new(const char *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   344
int Symbolcmpp(const void *, const void *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   345
void Symbol_init(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   346
int Symbol_insert(struct symbol *, const char *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   347
struct symbol *Symbol_find(const char *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   348
struct symbol *Symbol_Nth(int);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   349
int Symbol_count(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   350
struct symbol **Symbol_arrayof(void);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
/* Routines to manage the state table */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   354
int Configcmp(const char *, const char *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   355
struct state *State_new(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   356
void State_init(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   357
int State_insert(struct state *, struct config *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   358
struct state *State_find(struct config *);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
struct state **State_arrayof(/*  */);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
/* Routines used for efficiency in Configlist_add */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   363
void Configtable_init(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   364
int Configtable_insert(struct config *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   365
struct config *Configtable_find(struct config *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   366
void Configtable_clear(int(*)(struct config *));
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   367
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
/****************** From the file "action.c" *******************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
** Routines processing parser actions in the LEMON parser generator.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
/* Allocate a new parser action */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
static struct action *Action_new(void){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
  static struct action *freelist = 0;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   376
  struct action *newaction;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
  if( freelist==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
    int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
    int amt = 100;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
    freelist = (struct action *)calloc(amt, sizeof(struct action));
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
    if( freelist==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
      fprintf(stderr,"Unable to allocate memory for a new parser action.");
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
      exit(1);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
    for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    freelist[amt-1].next = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
  }
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   389
  newaction = freelist;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
  freelist = freelist->next;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   391
  return newaction;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
/* Compare two actions for sorting purposes.  Return negative, zero, or
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
** positive if the first action is less than, equal to, or greater than
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
** the first
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
static int actioncmp(
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
  struct action *ap1,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
  struct action *ap2
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
  int rc;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
  rc = ap1->sp->index - ap2->sp->index;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
  if( rc==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
    rc = (int)ap1->type - (int)ap2->type;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
  if( rc==0 && ap1->type==REDUCE ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
    rc = ap1->x.rp->index - ap2->x.rp->index;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
  }
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   410
  if( rc==0 ){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   411
    rc = ap2 - ap1;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   412
  }
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
  return rc;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   415
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
/* Sort parser actions */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
static struct action *Action_sort(
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
  struct action *ap
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
  ap = (struct action *)msort((char *)ap,(char **)&ap->next,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
                              (int(*)(const char*,const char*))actioncmp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
  return ap;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   425
void Action_add(
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   426
  struct action **app,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   427
  enum e_action type,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   428
  struct symbol *sp,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   429
  char *arg
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   430
){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   431
  struct action *newaction;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   432
  newaction = Action_new();
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   433
  newaction->next = *app;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   434
  *app = newaction;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   435
  newaction->type = type;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   436
  newaction->sp = sp;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
  if( type==SHIFT ){
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   438
    newaction->x.stp = (struct state *)arg;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
  }else{
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   440
    newaction->x.rp = (struct rule *)arg;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
/********************** New code to implement the "acttab" module ***********/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
** This module implements routines use to construct the yy_action[] table.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   446
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   447
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   448
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   449
** The state of the yy_action table under construction is an instance of
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   450
** the following structure.
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   451
**
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   452
** The yy_action table maps the pair (state_number, lookahead) into an
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   453
** action_number.  The table is an array of integers pairs.  The state_number
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   454
** determines an initial offset into the yy_action array.  The lookahead
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   455
** value is then added to this initial offset to get an index X into the
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   456
** yy_action array. If the aAction[X].lookahead equals the value of the
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   457
** of the lookahead input, then the value of the action_number output is
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   458
** aAction[X].action.  If the lookaheads do not match then the
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   459
** default action for the state_number is returned.
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   460
**
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   461
** All actions associated with a single state_number are first entered
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   462
** into aLookahead[] using multiple calls to acttab_action().  Then the 
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   463
** actions for that single state_number are placed into the aAction[] 
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   464
** array with a single call to acttab_insert().  The acttab_insert() call
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   465
** also resets the aLookahead[] array in preparation for the next
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   466
** state number.
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   468
struct lookahead_action {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   469
  int lookahead;             /* Value of the lookahead token */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   470
  int action;                /* Action to take on the given lookahead */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   471
};
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
typedef struct acttab acttab;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   473
struct acttab {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   474
  int nAction;                 /* Number of used slots in aAction[] */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
  int nActionAlloc;            /* Slots allocated for aAction[] */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   476
  struct lookahead_action
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   477
    *aAction,                  /* The yy_action[] table under construction */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
    *aLookahead;               /* A single new transaction set */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
  int mnLookahead;             /* Minimum aLookahead[].lookahead */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
  int mnAction;                /* Action associated with mnLookahead */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
  int mxLookahead;             /* Maximum aLookahead[].lookahead */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
  int nLookahead;              /* Used slots in aLookahead[] */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
  int nLookaheadAlloc;         /* Slots allocated in aLookahead[] */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
/* Return the number of entries in the yy_action table */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
#define acttab_size(X) ((X)->nAction)
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
/* The value for the N-th entry in yy_action */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
#define acttab_yyaction(X,N)  ((X)->aAction[N].action)
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
/* The value for the N-th entry in yy_lookahead */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
#define acttab_yylookahead(X,N)  ((X)->aAction[N].lookahead)
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
/* Free all memory associated with the given acttab */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
void acttab_free(acttab *p){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
  free( p->aAction );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
  free( p->aLookahead );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
  free( p );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
/* Allocate a new acttab structure */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
acttab *acttab_alloc(void){
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   504
  acttab *p = (acttab *) calloc( 1, sizeof(*p) );
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
  if( p==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
    fprintf(stderr,"Unable to allocate memory for a new acttab.");
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
    exit(1);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509
  memset(p, 0, sizeof(*p));
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   510
  return p;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   512
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   513
/* Add a new action to the current transaction set.  
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   514
**
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   515
** This routine is called once for each lookahead for a particular
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   516
** state.
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
void acttab_action(acttab *p, int lookahead, int action){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
  if( p->nLookahead>=p->nLookaheadAlloc ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
    p->nLookaheadAlloc += 25;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   521
    p->aLookahead = (struct lookahead_action *) realloc( p->aLookahead,
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
                             sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
    if( p->aLookahead==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   524
      fprintf(stderr,"malloc failed\n");
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
      exit(1);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
  if( p->nLookahead==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
    p->mxLookahead = lookahead;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
    p->mnLookahead = lookahead;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
    p->mnAction = action;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
    if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
    if( p->mnLookahead>lookahead ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
      p->mnLookahead = lookahead;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
      p->mnAction = action;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
  p->aLookahead[p->nLookahead].lookahead = lookahead;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
  p->aLookahead[p->nLookahead].action = action;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
  p->nLookahead++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   544
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
** Add the transaction set built up with prior calls to acttab_action()
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
** into the current action table.  Then reset the transaction set back
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
** to an empty set in preparation for a new round of acttab_action() calls.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
**
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
** Return the offset into the action table of the new transaction.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
int acttab_insert(acttab *p){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   552
  int i, j, k, n;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   553
  assert( p->nLookahead>0 );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   555
  /* Make sure we have enough space to hold the expanded action table
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
  ** in the worst case.  The worst case occurs if the transaction set
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
  ** must be appended to the current action table
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
  */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
  n = p->mxLookahead + 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
  if( p->nAction + n >= p->nActionAlloc ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
    int oldAlloc = p->nActionAlloc;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
    p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   563
    p->aAction = (struct lookahead_action *) realloc( p->aAction,
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
                          sizeof(p->aAction[0])*p->nActionAlloc);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
    if( p->aAction==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
      fprintf(stderr,"malloc failed\n");
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
      exit(1);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
    for(i=oldAlloc; i<p->nActionAlloc; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
      p->aAction[i].lookahead = -1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
      p->aAction[i].action = -1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   575
  /* Scan the existing action table looking for an offset that is a 
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   576
  ** duplicate of the current transaction set.  Fall out of the loop
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   577
  ** if and when the duplicate is found.
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
  **
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
  ** i is the index in p->aAction[] where p->mnLookahead is inserted.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
  */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   581
  for(i=p->nAction-1; i>=0; i--){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   582
    if( p->aAction[i].lookahead==p->mnLookahead ){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   583
      /* All lookaheads and actions in the aLookahead[] transaction
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   584
      ** must match against the candidate aAction[i] entry. */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
      if( p->aAction[i].action!=p->mnAction ) continue;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
      for(j=0; j<p->nLookahead; j++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
        k = p->aLookahead[j].lookahead - p->mnLookahead + i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
        if( k<0 || k>=p->nAction ) break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
        if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
        if( p->aLookahead[j].action!=p->aAction[k].action ) break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
      if( j<p->nLookahead ) continue;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   593
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   594
      /* No possible lookahead value that is not in the aLookahead[]
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   595
      ** transaction is allowed to match aAction[i] */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
      n = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
      for(j=0; j<p->nAction; j++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
        if( p->aAction[j].lookahead<0 ) continue;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
        if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
      if( n==p->nLookahead ){
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   602
        break;  /* An exact match is found at offset i */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   603
      }
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   604
    }
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   605
  }
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   606
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   607
  /* If no existing offsets exactly match the current transaction, find an
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   608
  ** an empty offset in the aAction[] table in which we can add the
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   609
  ** aLookahead[] transaction.
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   610
  */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   611
  if( i<0 ){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   612
    /* Look for holes in the aAction[] table that fit the current
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   613
    ** aLookahead[] transaction.  Leave i set to the offset of the hole.
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   614
    ** If no holes are found, i is left at p->nAction, which means the
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   615
    ** transaction will be appended. */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   616
    for(i=0; i<p->nActionAlloc - p->mxLookahead; i++){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   617
      if( p->aAction[i].lookahead<0 ){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   618
        for(j=0; j<p->nLookahead; j++){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   619
          k = p->aLookahead[j].lookahead - p->mnLookahead + i;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   620
          if( k<0 ) break;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   621
          if( p->aAction[k].lookahead>=0 ) break;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   622
        }
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   623
        if( j<p->nLookahead ) continue;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   624
        for(j=0; j<p->nAction; j++){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   625
          if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   626
        }
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   627
        if( j==p->nAction ){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   628
          break;  /* Fits in empty slots */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   629
        }
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   630
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   633
  /* Insert transaction set at index i. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   634
  for(j=0; j<p->nLookahead; j++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
    k = p->aLookahead[j].lookahead - p->mnLookahead + i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
    p->aAction[k] = p->aLookahead[j];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
    if( k>=p->nAction ) p->nAction = k+1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
  p->nLookahead = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   640
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
  /* Return the offset that is added to the lookahead in order to get the
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
  ** index into yy_action of the action */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   643
  return i - p->mnLookahead;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   645
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   646
/********************** From the file "build.c" *****************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   647
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   648
** Routines to construction the finite state machine for the LEMON
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
** parser generator.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
/* Find a precedence symbol of every rule in the grammar.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
** 
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   654
** Those rules which have a precedence symbol coded in the input
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
** grammar using the "[symbol]" construct will already have the
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
** rp->precsym field filled.  Other rules take as their precedence
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
** symbol the first RHS symbol with a defined precedence.  If there
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   658
** are not RHS symbols with a defined precedence, the precedence
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
** symbol field is left blank.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   660
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   661
void FindRulePrecedences(struct lemon *xp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   662
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   663
  struct rule *rp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   664
  for(rp=xp->rule; rp; rp=rp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
    if( rp->precsym==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   666
      int i, j;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
      for(i=0; i<rp->nrhs && rp->precsym==0; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
        struct symbol *sp = rp->rhs[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
        if( sp->type==MULTITERMINAL ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
          for(j=0; j<sp->nsubsym; j++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
            if( sp->subsym[j]->prec>=0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
              rp->precsym = sp->subsym[j];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   673
              break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
            }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
          }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   676
        }else if( sp->prec>=0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
          rp->precsym = rp->rhs[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
	}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   679
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   680
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   681
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
  return;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   684
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   685
/* Find all nonterminals which will generate the empty string.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
** Then go back and compute the first sets of every nonterminal.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
** The first set is the set of all terminal symbols which can begin
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
** a string generated by that nonterminal.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   689
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   690
void FindFirstSets(struct lemon *lemp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   691
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   692
  int i, j;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   693
  struct rule *rp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   694
  int progress;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   695
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   696
  for(i=0; i<lemp->nsymbol; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   697
    lemp->symbols[i]->lambda = LEMON_FALSE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   698
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
  for(i=lemp->nterminal; i<lemp->nsymbol; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
    lemp->symbols[i]->firstset = SetNew();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   702
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
  /* First compute all lambdas */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
  do{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   705
    progress = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
    for(rp=lemp->rule; rp; rp=rp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
      if( rp->lhs->lambda ) continue;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
      for(i=0; i<rp->nrhs; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
         struct symbol *sp = rp->rhs[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
         if( sp->type!=TERMINAL || sp->lambda==LEMON_FALSE ) break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   712
      if( i==rp->nrhs ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
        rp->lhs->lambda = LEMON_TRUE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   714
        progress = 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   716
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
  }while( progress );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   718
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   719
  /* Now compute all first sets */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
  do{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   721
    struct symbol *s1, *s2;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   722
    progress = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   723
    for(rp=lemp->rule; rp; rp=rp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   724
      s1 = rp->lhs;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   725
      for(i=0; i<rp->nrhs; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   726
        s2 = rp->rhs[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   727
        if( s2->type==TERMINAL ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
          progress += SetAdd(s1->firstset,s2->index);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   729
          break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
        }else if( s2->type==MULTITERMINAL ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   731
          for(j=0; j<s2->nsubsym; j++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   732
            progress += SetAdd(s1->firstset,s2->subsym[j]->index);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   733
          }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   734
          break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   735
	}else if( s1==s2 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   736
          if( s1->lambda==LEMON_FALSE ) break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   737
	}else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   738
          progress += SetUnion(s1->firstset,s2->firstset);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
          if( s2->lambda==LEMON_FALSE ) break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
	}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   741
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
  }while( progress );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
  return;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   746
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
/* Compute all LR(0) states for the grammar.  Links
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
** are added to between some states so that the LR(1) follow sets
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
** can be computed later.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   750
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   751
PRIVATE struct state *getstate(struct lemon *);  /* forward reference */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   752
void FindStates(struct lemon *lemp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   753
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   754
  struct symbol *sp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   755
  struct rule *rp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   756
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   757
  Configlist_init();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   758
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   759
  /* Find the start symbol */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   760
  if( lemp->start ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   761
    sp = Symbol_find(lemp->start);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   762
    if( sp==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   763
      ErrorMsg(lemp->filename,0,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   764
"The specified start symbol \"%s\" is not \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   765
in a nonterminal of the grammar.  \"%s\" will be used as the start \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   766
symbol instead.",lemp->start,lemp->rule->lhs->name);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   767
      lemp->errorcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   768
      sp = lemp->rule->lhs;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   769
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   770
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   771
    sp = lemp->rule->lhs;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   772
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   774
  /* Make sure the start symbol doesn't occur on the right-hand side of
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
  ** any rule.  Report an error if it does.  (YACC would generate a new
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   776
  ** start symbol in this case.) */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   777
  for(rp=lemp->rule; rp; rp=rp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   778
    int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   779
    for(i=0; i<rp->nrhs; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   780
      if( rp->rhs[i]==sp ){   /* FIX ME:  Deal with multiterminals */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   781
        ErrorMsg(lemp->filename,0,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   782
"The start symbol \"%s\" occurs on the \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   783
right-hand side of a rule. This will result in a parser which \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   784
does not work properly.",sp->name);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   785
        lemp->errorcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   786
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   787
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   788
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   789
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   790
  /* The basis configuration set for the first state
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   791
  ** is all rules which have the start symbol as their
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   792
  ** left-hand side */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   793
  for(rp=sp->rule; rp; rp=rp->nextlhs){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   794
    struct config *newcfp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   795
    rp->lhsStart = 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   796
    newcfp = Configlist_addbasis(rp,0);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   797
    SetAdd(newcfp->fws,0);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   798
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   799
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   800
  /* Compute the first state.  All other states will be
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   801
  ** computed automatically during the computation of the first one.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   802
  ** The returned pointer to the first state is not used. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   803
  (void)getstate(lemp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   804
  return;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   805
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   806
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   807
/* Return a pointer to a state which is described by the configuration
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   808
** list which has been built from calls to Configlist_add.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   809
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   810
PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   811
PRIVATE struct state *getstate(struct lemon *lemp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   812
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   813
  struct config *cfp, *bp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   814
  struct state *stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   815
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   816
  /* Extract the sorted basis of the new state.  The basis was constructed
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   817
  ** by prior calls to "Configlist_addbasis()". */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   818
  Configlist_sortbasis();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   819
  bp = Configlist_basis();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   820
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   821
  /* Get a state with the same basis */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   822
  stp = State_find(bp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   823
  if( stp ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   824
    /* A state with the same basis already exists!  Copy all the follow-set
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   825
    ** propagation links from the state under construction into the
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   826
    ** preexisting state, then return a pointer to the preexisting state */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   827
    struct config *x, *y;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   828
    for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   829
      Plink_copy(&y->bplp,x->bplp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   830
      Plink_delete(x->fplp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   831
      x->fplp = x->bplp = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   832
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   833
    cfp = Configlist_return();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   834
    Configlist_eat(cfp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   835
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   836
    /* This really is a new state.  Construct all the details */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   837
    Configlist_closure(lemp);    /* Compute the configuration closure */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   838
    Configlist_sort();           /* Sort the configuration closure */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   839
    cfp = Configlist_return();   /* Get a pointer to the config list */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   840
    stp = State_new();           /* A new state structure */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   841
    MemoryCheck(stp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   842
    stp->bp = bp;                /* Remember the configuration basis */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   843
    stp->cfp = cfp;              /* Remember the configuration closure */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   844
    stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   845
    stp->ap = 0;                 /* No actions, yet. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   846
    State_insert(stp,stp->bp);   /* Add to the state table */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   847
    buildshifts(lemp,stp);       /* Recursively compute successor states */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   848
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   849
  return stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   850
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   851
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   852
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   853
** Return true if two symbols are the same.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   854
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   855
int same_symbol(struct symbol *a, struct symbol *b)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   856
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   857
  int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   858
  if( a==b ) return 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   859
  if( a->type!=MULTITERMINAL ) return 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   860
  if( b->type!=MULTITERMINAL ) return 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   861
  if( a->nsubsym!=b->nsubsym ) return 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   862
  for(i=0; i<a->nsubsym; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   863
    if( a->subsym[i]!=b->subsym[i] ) return 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   864
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   865
  return 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   866
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   867
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   868
/* Construct all successor states to the given state.  A "successor"
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   869
** state is any state which can be reached by a shift action.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   870
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   871
PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   872
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   873
  struct config *cfp;  /* For looping thru the config closure of "stp" */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   874
  struct config *bcfp; /* For the inner loop on config closure of "stp" */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   875
  struct config *newcfg;  /* */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   876
  struct symbol *sp;   /* Symbol following the dot in configuration "cfp" */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   877
  struct symbol *bsp;  /* Symbol following the dot in configuration "bcfp" */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   878
  struct state *newstp; /* A pointer to a successor state */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   879
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   880
  /* Each configuration becomes complete after it contibutes to a successor
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   881
  ** state.  Initially, all configurations are incomplete */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   882
  for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   883
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   884
  /* Loop through all configurations of the state "stp" */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   885
  for(cfp=stp->cfp; cfp; cfp=cfp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   886
    if( cfp->status==COMPLETE ) continue;    /* Already used by inner loop */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   887
    if( cfp->dot>=cfp->rp->nrhs ) continue;  /* Can't shift this config */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   888
    Configlist_reset();                      /* Reset the new config set */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   889
    sp = cfp->rp->rhs[cfp->dot];             /* Symbol after the dot */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   890
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   891
    /* For every configuration in the state "stp" which has the symbol "sp"
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   892
    ** following its dot, add the same configuration to the basis set under
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   893
    ** construction but with the dot shifted one symbol to the right. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   894
    for(bcfp=cfp; bcfp; bcfp=bcfp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   895
      if( bcfp->status==COMPLETE ) continue;    /* Already used */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   896
      if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   897
      bsp = bcfp->rp->rhs[bcfp->dot];           /* Get symbol after dot */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   898
      if( !same_symbol(bsp,sp) ) continue;      /* Must be same as for "cfp" */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   899
      bcfp->status = COMPLETE;                  /* Mark this config as used */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   900
      newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   901
      Plink_add(&newcfg->bplp,bcfp);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   902
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   903
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   904
    /* Get a pointer to the state described by the basis configuration set
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   905
    ** constructed in the preceding loop */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   906
    newstp = getstate(lemp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   907
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   908
    /* The state "newstp" is reached from the state "stp" by a shift action
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   909
    ** on the symbol "sp" */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   910
    if( sp->type==MULTITERMINAL ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   911
      int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   912
      for(i=0; i<sp->nsubsym; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   913
        Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   914
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   915
    }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   916
      Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   917
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   918
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   919
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   920
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   921
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   922
** Construct the propagation links
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   923
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   924
void FindLinks(struct lemon *lemp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   925
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   926
  int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   927
  struct config *cfp, *other;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   928
  struct state *stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   929
  struct plink *plp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   930
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   931
  /* Housekeeping detail:
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   932
  ** Add to every propagate link a pointer back to the state to
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   933
  ** which the link is attached. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   934
  for(i=0; i<lemp->nstate; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   935
    stp = lemp->sorted[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   936
    for(cfp=stp->cfp; cfp; cfp=cfp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   937
      cfp->stp = stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   938
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   939
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   940
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   941
  /* Convert all backlinks into forward links.  Only the forward
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   942
  ** links are used in the follow-set computation. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   943
  for(i=0; i<lemp->nstate; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   944
    stp = lemp->sorted[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   945
    for(cfp=stp->cfp; cfp; cfp=cfp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   946
      for(plp=cfp->bplp; plp; plp=plp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   947
        other = plp->cfp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   948
        Plink_add(&other->fplp,cfp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   949
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   950
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   951
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   952
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   953
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   954
/* Compute all followsets.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   955
**
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   956
** A followset is the set of all symbols which can come immediately
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   957
** after a configuration.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   958
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   959
void FindFollowSets(struct lemon *lemp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   960
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   961
  int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   962
  struct config *cfp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   963
  struct plink *plp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   964
  int progress;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   965
  int change;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   966
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   967
  for(i=0; i<lemp->nstate; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   968
    for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   969
      cfp->status = INCOMPLETE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   970
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   971
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   972
  
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   973
  do{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   974
    progress = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   975
    for(i=0; i<lemp->nstate; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   976
      for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   977
        if( cfp->status==COMPLETE ) continue;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   978
        for(plp=cfp->fplp; plp; plp=plp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   979
          change = SetUnion(plp->cfp->fws,cfp->fws);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   980
          if( change ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   981
            plp->cfp->status = INCOMPLETE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   982
            progress = 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   983
	  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   984
	}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   985
        cfp->status = COMPLETE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   986
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   987
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   988
  }while( progress );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   989
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   990
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   991
static int resolve_conflict(struct action *,struct action *, struct symbol *);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   992
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   993
/* Compute the reduce actions, and resolve conflicts.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   994
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   995
void FindActions(struct lemon *lemp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   996
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   997
  int i,j;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   998
  struct config *cfp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   999
  struct state *stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1000
  struct symbol *sp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1001
  struct rule *rp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1002
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1003
  /* Add all of the reduce actions 
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1004
  ** A reduce action is added for each element of the followset of
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1005
  ** a configuration which has its dot at the extreme right.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1006
  */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1007
  for(i=0; i<lemp->nstate; i++){   /* Loop over all states */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1008
    stp = lemp->sorted[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1009
    for(cfp=stp->cfp; cfp; cfp=cfp->next){  /* Loop over all configurations */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1010
      if( cfp->rp->nrhs==cfp->dot ){        /* Is dot at extreme right? */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1011
        for(j=0; j<lemp->nterminal; j++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1012
          if( SetFind(cfp->fws,j) ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1013
            /* Add a reduce action to the state "stp" which will reduce by the
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1014
            ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1015
            Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1016
          }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1017
	}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1018
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1019
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1020
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1021
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1022
  /* Add the accepting token */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1023
  if( lemp->start ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1024
    sp = Symbol_find(lemp->start);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1025
    if( sp==0 ) sp = lemp->rule->lhs;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1026
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1027
    sp = lemp->rule->lhs;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1028
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1029
  /* Add to the first state (which is always the starting state of the
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1030
  ** finite state machine) an action to ACCEPT if the lookahead is the
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1031
  ** start nonterminal.  */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1032
  Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1033
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1034
  /* Resolve conflicts */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1035
  for(i=0; i<lemp->nstate; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1036
    struct action *ap, *nap;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1037
    struct state *stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1038
    stp = lemp->sorted[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1039
    /* assert( stp->ap ); */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1040
    stp->ap = Action_sort(stp->ap);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1041
    for(ap=stp->ap; ap && ap->next; ap=ap->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1042
      for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1043
         /* The two actions "ap" and "nap" have the same lookahead.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1044
         ** Figure out which one should be used */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1045
         lemp->nconflict += resolve_conflict(ap,nap,lemp->errsym);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1046
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1047
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1048
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1049
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1050
  /* Report an error for each rule that can never be reduced. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1051
  for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1052
  for(i=0; i<lemp->nstate; i++){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1053
    struct action *ap;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1054
    for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1055
      if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1056
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1057
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1058
  for(rp=lemp->rule; rp; rp=rp->next){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1059
    if( rp->canReduce ) continue;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1060
    ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1061
    lemp->errorcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1062
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1063
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1064
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1065
/* Resolve a conflict between the two given actions.  If the
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1066
** conflict can't be resolved, return non-zero.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1067
**
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1068
** NO LONGER TRUE:
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1069
**   To resolve a conflict, first look to see if either action
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1070
**   is on an error rule.  In that case, take the action which
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1071
**   is not associated with the error rule.  If neither or both
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1072
**   actions are associated with an error rule, then try to
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1073
**   use precedence to resolve the conflict.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1074
**
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1075
** If either action is a SHIFT, then it must be apx.  This
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1076
** function won't work if apx->type==REDUCE and apy->type==SHIFT.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1077
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1078
static int resolve_conflict(
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1079
  struct action *apx,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1080
  struct action *apy,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1081
  struct symbol *errsym   /* The error symbol (if defined.  NULL otherwise) */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1082
){
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1083
  struct symbol *spx, *spy;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1084
  int errcnt = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1085
  assert( apx->sp==apy->sp );  /* Otherwise there would be no conflict */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1086
  if( apx->type==SHIFT && apy->type==SHIFT ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1087
    apy->type = SSCONFLICT;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1088
    errcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1089
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1090
  if( apx->type==SHIFT && apy->type==REDUCE ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1091
    spx = apx->sp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1092
    spy = apy->x.rp->precsym;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1093
    if( spy==0 || spx->prec<0 || spy->prec<0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1094
      /* Not enough precedence information. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1095
      apy->type = SRCONFLICT;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1096
      errcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1097
    }else if( spx->prec>spy->prec ){    /* Lower precedence wins */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1098
      apy->type = RD_RESOLVED;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1099
    }else if( spx->prec<spy->prec ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1100
      apx->type = SH_RESOLVED;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1101
    }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1102
      apy->type = RD_RESOLVED;                             /* associativity */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1103
    }else if( spx->prec==spy->prec && spx->assoc==LEFT ){  /* to break tie */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1104
      apx->type = SH_RESOLVED;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1105
    }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1106
      assert( spx->prec==spy->prec && spx->assoc==NONE );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1107
      apy->type = SRCONFLICT;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1108
      errcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1109
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1110
  }else if( apx->type==REDUCE && apy->type==REDUCE ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1111
    spx = apx->x.rp->precsym;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1112
    spy = apy->x.rp->precsym;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1113
    if( spx==0 || spy==0 || spx->prec<0 ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1114
    spy->prec<0 || spx->prec==spy->prec ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1115
      apy->type = RRCONFLICT;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1116
      errcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1117
    }else if( spx->prec>spy->prec ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1118
      apy->type = RD_RESOLVED;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1119
    }else if( spx->prec<spy->prec ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1120
      apx->type = RD_RESOLVED;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1121
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1122
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1123
    assert( 
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1124
      apx->type==SH_RESOLVED ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1125
      apx->type==RD_RESOLVED ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1126
      apx->type==SSCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1127
      apx->type==SRCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1128
      apx->type==RRCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1129
      apy->type==SH_RESOLVED ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1130
      apy->type==RD_RESOLVED ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1131
      apy->type==SSCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1132
      apy->type==SRCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1133
      apy->type==RRCONFLICT
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1134
    );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1135
    /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1136
    ** REDUCEs on the list.  If we reach this point it must be because
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1137
    ** the parser conflict had already been resolved. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1138
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1139
  return errcnt;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1140
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1141
/********************* From the file "configlist.c" *************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1142
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1143
** Routines to processing a configuration list and building a state
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1144
** in the LEMON parser generator.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1145
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1146
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1147
static struct config *freelist = 0;      /* List of free configurations */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1148
static struct config *current = 0;       /* Top of list of configurations */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1149
static struct config **currentend = 0;   /* Last on list of configs */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1150
static struct config *basis = 0;         /* Top of list of basis configs */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1151
static struct config **basisend = 0;     /* End of list of basis configs */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1152
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1153
/* Return a pointer to a new configuration */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1154
PRIVATE struct config *newconfig(){
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1155
  struct config *newcfg;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1156
  if( freelist==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1157
    int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1158
    int amt = 3;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1159
    freelist = (struct config *)calloc( amt, sizeof(struct config) );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1160
    if( freelist==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1161
      fprintf(stderr,"Unable to allocate memory for a new configuration.");
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1162
      exit(1);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1163
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1164
    for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1165
    freelist[amt-1].next = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1166
  }
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1167
  newcfg = freelist;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1168
  freelist = freelist->next;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1169
  return newcfg;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1170
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1171
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1172
/* The configuration "old" is no longer used */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1173
PRIVATE void deleteconfig(struct config *old)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1174
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1175
  old->next = freelist;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1176
  freelist = old;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1177
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1178
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1179
/* Initialized the configuration list builder */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1180
void Configlist_init(){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1181
  current = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1182
  currentend = &current;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1183
  basis = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1184
  basisend = &basis;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1185
  Configtable_init();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1186
  return;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1187
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1188
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1189
/* Initialized the configuration list builder */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1190
void Configlist_reset(){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1191
  current = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1192
  currentend = &current;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1193
  basis = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1194
  basisend = &basis;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1195
  Configtable_clear(0);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1196
  return;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1197
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1198
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1199
/* Add another configuration to the configuration list */