misc/lemon.c
author Ethan Lee <flibitijibibo@flibitijibibo.com>
Tue, 25 Aug 2020 22:41:43 -0400
changeset 1300 f1cdc5187d53
parent 1111 7530b37979b8
permissions -rw-r--r--
Handle MOJOSHADER_USAGE_UNKNOWN in SPIR-V linker
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__
957
c5992f4bee6d More Visual Studio fixes for Lemon.
Ryan C. Gordon <icculus@icculus.org>
parents: 956
diff changeset
    29
#ifdef __cplusplus
c5992f4bee6d More Visual Studio fixes for Lemon.
Ryan C. Gordon <icculus@icculus.org>
parents: 956
diff changeset
    30
extern "C" {
c5992f4bee6d More Visual Studio fixes for Lemon.
Ryan C. Gordon <icculus@icculus.org>
parents: 956
diff changeset
    31
#endif
c5992f4bee6d More Visual Studio fixes for Lemon.
Ryan C. Gordon <icculus@icculus.org>
parents: 956
diff changeset
    32
extern int access(const char *path, int mode);
c5992f4bee6d More Visual Studio fixes for Lemon.
Ryan C. Gordon <icculus@icculus.org>
parents: 956
diff changeset
    33
#ifdef __cplusplus
c5992f4bee6d More Visual Studio fixes for Lemon.
Ryan C. Gordon <icculus@icculus.org>
parents: 956
diff changeset
    34
}
c5992f4bee6d More Visual Studio fixes for Lemon.
Ryan C. Gordon <icculus@icculus.org>
parents: 956
diff changeset
    35
#endif
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#else
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
#include <unistd.h>
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
#endif
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
/* #define PRIVATE static */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
#define PRIVATE
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
#ifdef TEST
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
#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
    45
#else
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
#define MAXRHS 1000
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
#endif
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
829
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    49
static const char **made_files = NULL;
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    50
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
    51
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
    52
static void LemonAtExit(void)
829
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
    /* 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
    55
    int i;
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    56
    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
    57
        if (!successful_exit) {
829
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    58
            remove(made_files[i]);
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
    }
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    61
    free(made_files);
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    62
    made_files_count = 0;
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    63
    made_files = NULL;
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    64
}
7b3c3ae9e5a9 Lemon now deletes its output on failure.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    65
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
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
    67
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
** 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
    70
** 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
    71
** 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
    72
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
#define lemonStrlen(X)   ((int)strlen(X))
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    75
/* a few forward declarations... */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    76
struct rule;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    77
struct lemon;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    78
struct action;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    79
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
static struct action *Action_new(void);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
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
    82
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
/********** From the file "build.h" ************************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
void FindRulePrecedences();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
void FindFirstSets();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
void FindStates();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
void FindLinks();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
void FindFollowSets();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
void FindActions();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
/********* 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
    92
void Configlist_init(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_add(struct rule *, int);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    94
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
    95
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
    96
void Configlist_sort(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    97
void Configlist_sortbasis(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
    98
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
    99
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
   100
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
   101
void Configlist_reset(void);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
/********* From the file "error.h" ***************************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
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
   105
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
/****** 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
   107
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
   108
         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
   109
struct s_options {
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   110
  enum option_type type;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   111
  const char *label;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
  char *arg;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   113
  const char *message;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
};
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   115
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
   116
int    OptNArgs(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   117
char  *OptArg(int);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   118
void   OptErr(int);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   119
void   OptPrint(void);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
/******** 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
   122
void Parse(struct lemon *lemp);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
/********* 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
   125
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
   126
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
   127
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
   128
void Plink_delete(struct plink *);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
/********** 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
   131
void Reprint(struct lemon *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   132
void ReportOutput(struct lemon *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   133
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
   134
void ReportHeader(struct lemon *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   135
void CompressTables(struct lemon *);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   136
void ResortStates(struct lemon *);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
/********** 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
   139
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
   140
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
   141
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
   142
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   143
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
   144
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
   145
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
   146
#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
   147
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
/********** From the file "struct.h" *************************************/
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
** 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
   151
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
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
   154
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
/* 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
   156
** in the following: */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   157
enum symbol_type {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   158
  TERMINAL,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   159
  NONTERMINAL,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   160
  MULTITERMINAL
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   161
};
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   162
enum e_assoc {
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    LEFT,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    RIGHT,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    NONE,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
    UNK
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   167
};
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   168
struct symbol {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   169
  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
   170
  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
   171
  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
   172
  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
   173
  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
   174
  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
   175
  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
   176
  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
   177
  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
   178
  int useCnt;              /* Number of times used */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
  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
   180
                           ** 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
   181
  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
   182
  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
   183
                           ** object. Only used if type==NONTERMINAL */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
  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
   185
                           ** 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
   186
                           ** 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
   187
  /* 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
   188
  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
   189
  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
   190
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
/* 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
   193
** structure.  */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
struct rule {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
  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
   196
  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
   197
  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
   198
  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
   199
  int nrhs;                /* Number of RHS symbols */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
  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
   201
  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
   202
  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
   203
  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
   204
  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
   205
  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
   206
  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
   207
  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
   208
  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
   209
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
/* 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
   212
** 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
   213
** 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
   214
** 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
   215
** 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
   216
enum cfgstatus {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   217
  COMPLETE,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   218
  INCOMPLETE
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   219
};
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
struct config {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
  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
   222
  int dot;                 /* The parse point */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
  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
   224
  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
   225
  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
   226
  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
   227
  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
   228
  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
   229
  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
   230
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   232
enum e_action {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   233
  SHIFT,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   234
  ACCEPT,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   235
  REDUCE,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   236
  ERROR,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   237
  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
   238
  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
   239
  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
   240
  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
   241
  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
   242
  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
   243
};
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   244
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
/* 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
   246
struct action {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
  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
   248
  enum e_action type;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
  union {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    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
   251
    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
   252
  } x;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
  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
   254
  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
   255
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
/* 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
   258
** 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
   259
struct state {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
  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
   261
  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
   262
  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
   263
  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
   264
  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
   265
  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
   266
  int iDflt;               /* Default action */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
#define NO_OFFSET (-2147483647)
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
/* 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
   271
** 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
   272
** the first changes. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
struct plink {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
  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
   275
  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
   276
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
/* 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
   279
** 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
   280
** 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
   281
** 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
   282
struct lemon {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
  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
   284
  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
   285
  int nstate;              /* Number of states */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
  int nrule;               /* Number of rules */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
  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
   288
  int nterminal;           /* Number of terminal symbols */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
  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
   290
  int errorcnt;            /* Number of errors */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
  struct symbol *errsym;   /* The error symbol */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
  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
   293
  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
   294
  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
   295
  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
   296
  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
   297
  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
   298
  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
   299
  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
   300
  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
   301
  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
   302
  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
   303
  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
   304
  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
   305
  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
   306
  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
   307
  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
   308
  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
   309
  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
   310
  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
   311
#if __MOJOSHADER__
786
795e7762da7f Modified Lemon to accept a specific amount of conflicts.
Ryan C. Gordon <icculus@icculus.org>
parents: 714
diff changeset
   312
  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
   313
#endif
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
  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
   315
  int basisflag;           /* Print only basis configurations */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
  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
   317
  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
   318
  char *argv0;             /* Name of the program */
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
#define MemoryCheck(X) if((X)==0){ \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
  extern void memory_error(); \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
  memory_error(); \
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
/**************** From the file "table.h" *********************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
** 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
   329
** from a specification in the file
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
**              "table.q"
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
** 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
   332
** 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
   333
** file, then rerun aagen.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
** 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
   337
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
/* Routines for handling a strings */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   340
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
   341
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   342
void Strsafe_init(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   343
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
   344
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
   345
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
/* 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
   347
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   348
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
   349
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
   350
void Symbol_init(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   351
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
   352
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
   353
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
   354
int Symbol_count(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   355
struct symbol **Symbol_arrayof(void);
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
/* Routines to manage the state table */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   359
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
   360
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
   361
void State_init(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   362
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
   363
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
   364
struct state **State_arrayof(/*  */);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
/* 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
   367
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   368
void Configtable_init(void);
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   369
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
   370
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
   371
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
   372
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
/****************** From the file "action.c" *******************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
** 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
   376
*/
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
/* Allocate a new parser action */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
static struct action *Action_new(void){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
  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
   381
  struct action *newaction;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
  if( freelist==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
    int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
    int amt = 100;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
    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
   387
    if( freelist==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
      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
   389
      exit(1);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
    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
   392
    freelist[amt-1].next = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
  }
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   394
  newaction = freelist;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
  freelist = freelist->next;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   396
  return newaction;
708
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
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
/* 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
   400
** 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
   401
** the first
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
static int actioncmp(
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
  struct action *ap1,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
  struct action *ap2
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
  int rc;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
  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
   409
  if( rc==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
    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
   411
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   412
  if( rc==0 && ap1->type==REDUCE ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
    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
   414
  }
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   415
  if( rc==0 ){
884
7d3aad1c2569 Merged some lemon diffs from sqlite's fossil repo: Visual Studio compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 833
diff changeset
   416
    rc = (int) (ap2 - ap1);
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   417
  }
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
  return rc;
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
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
/* Sort parser actions */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
static struct action *Action_sort(
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
  struct action *ap
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   424
){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   425
  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
   426
                              (int(*)(const char*,const char*))actioncmp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   427
  return ap;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   428
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   430
void Action_add(
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   431
  struct action **app,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   432
  enum e_action type,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   433
  struct symbol *sp,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   434
  char *arg
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   435
){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   436
  struct action *newaction;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   437
  newaction = Action_new();
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   438
  newaction->next = *app;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   439
  *app = newaction;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   440
  newaction->type = type;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   441
  newaction->sp = sp;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
  if( type==SHIFT ){
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   443
    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
   444
  }else{
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   445
    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
   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
/********************** 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
   449
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   450
** 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
   451
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   452
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   453
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   454
** 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
   455
** the following structure.
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   456
**
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   457
** 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
   458
** 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
   459
** 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
   460
** 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
   461
** 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
   462
** 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
   463
** 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
   464
** 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
   465
**
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   466
** 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
   467
** 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
   468
** 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
   469
** 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
   470
** 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
   471
** state number.
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   472
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   473
struct lookahead_action {
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   474
  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
   475
  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
   476
};
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
typedef struct acttab acttab;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
struct acttab {
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
  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
   480
  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
   481
  struct lookahead_action
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   482
    *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
   483
    *aLookahead;               /* A single new transaction set */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
  int mnLookahead;             /* Minimum aLookahead[].lookahead */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
  int mnAction;                /* Action associated with mnLookahead */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
  int mxLookahead;             /* Maximum aLookahead[].lookahead */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
  int nLookahead;              /* Used slots in aLookahead[] */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
  int nLookaheadAlloc;         /* Slots allocated in aLookahead[] */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
};
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
/* 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
   492
#define acttab_size(X) ((X)->nAction)
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
/* 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
   495
#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
   496
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
/* 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
   498
#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
   499
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
/* 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
   501
void acttab_free(acttab *p){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
  free( p->aAction );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
  free( p->aLookahead );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
  free( p );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
/* Allocate a new acttab structure */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
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
   509
  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
   510
  if( p==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   511
    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
   512
    exit(1);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   513
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
  memset(p, 0, sizeof(*p));
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
  return p;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   518
/* 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
   519
**
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   520
** 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
   521
** state.
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
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
   524
  if( p->nLookahead>=p->nLookaheadAlloc ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
    p->nLookaheadAlloc += 25;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   526
    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
   527
                             sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
    if( p->aLookahead==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
      fprintf(stderr,"malloc failed\n");
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
      exit(1);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
  if( p->nLookahead==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
    p->mxLookahead = 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
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
    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
   539
    if( p->mnLookahead>lookahead ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
      p->mnLookahead = lookahead;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
      p->mnAction = action;
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
  p->aLookahead[p->nLookahead].lookahead = lookahead;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
  p->aLookahead[p->nLookahead].action = action;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   546
  p->nLookahead++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   547
}
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
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
** 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
   551
** 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
   552
** 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
   553
**
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   554
** 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
   555
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   556
int acttab_insert(acttab *p){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   557
  int i, j, k, n;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   558
  assert( p->nLookahead>0 );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
  /* 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
   561
  ** 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
   562
  ** 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
   563
  */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
  n = p->mxLookahead + 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
  if( p->nAction + n >= p->nActionAlloc ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
    int oldAlloc = p->nActionAlloc;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
    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
   568
    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
   569
                          sizeof(p->aAction[0])*p->nActionAlloc);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
    if( p->aAction==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
      fprintf(stderr,"malloc failed\n");
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
      exit(1);
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
    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
   575
      p->aAction[i].lookahead = -1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
      p->aAction[i].action = -1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
    }
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
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   580
  /* 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
   581
  ** 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
   582
  ** 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
   583
  **
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
  ** 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
   585
  */
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   586
  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
   587
    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
   588
      /* 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
   589
      ** 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
   590
      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
   591
      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
   592
        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
   593
        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
   594
        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
   595
        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
   596
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
      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
   598
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   599
      /* 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
   600
      ** 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
   601
      n = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   602
      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
   603
        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
   604
        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
   605
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   606
      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
   607
        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
   608
      }
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   609
    }
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
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   612
  /* 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
   613
  ** 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
   614
  ** aLookahead[] transaction.
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   615
  */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   616
  if( i<0 ){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   617
    /* 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
   618
    ** 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
   619
    ** 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
   620
    ** transaction will be appended. */
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   621
    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
   622
      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
   623
        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
   624
          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
   625
          if( k<0 ) break;
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   626
          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
   627
        }
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   628
        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
   629
        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
   630
          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
   631
        }
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   632
        if( j==p->nAction ){
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   633
          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
   634
        }
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
  /* Insert transaction set at index i. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
  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
   640
    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
   641
    p->aAction[k] = p->aLookahead[j];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   642
    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
   643
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
  p->nLookahead = 0;
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
  /* 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
   647
  ** 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
   648
  return i - p->mnLookahead;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   649
}
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
/********************** From the file "build.c" *****************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   653
** 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
   654
** parser generator.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   655
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   656
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   657
/* 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
   658
** 
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   659
** 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
   660
** 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
   661
** 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
   662
** 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
   663
** 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
   664
** symbol field is left blank.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   665
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   666
void FindRulePrecedences(struct lemon *xp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   667
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   668
  struct rule *rp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
  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
   670
    if( rp->precsym==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
      int i, j;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   672
      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
   673
        struct symbol *sp = rp->rhs[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
        if( sp->type==MULTITERMINAL ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   675
          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
   676
            if( sp->subsym[j]->prec>=0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   677
              rp->precsym = sp->subsym[j];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   678
              break;
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
        }else if( sp->prec>=0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   682
          rp->precsym = rp->rhs[i];
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
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   687
  return;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   688
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   689
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   690
/* 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
   691
** 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
   692
** 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
   693
** a string generated by that nonterminal.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   694
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   695
void FindFirstSets(struct lemon *lemp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   696
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   697
  int i, j;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   698
  struct rule *rp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
  int progress;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
  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
   702
    lemp->symbols[i]->lambda = LEMON_FALSE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
  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
   705
    lemp->symbols[i]->firstset = SetNew();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   706
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   707
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
  /* First compute all lambdas */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
  do{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   710
    progress = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   711
    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
   712
      if( rp->lhs->lambda ) continue;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   713
      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
   714
         struct symbol *sp = rp->rhs[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   715
         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
   716
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   717
      if( i==rp->nrhs ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   718
        rp->lhs->lambda = LEMON_TRUE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   719
        progress = 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   721
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   722
  }while( progress );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   723
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   724
  /* Now compute all first sets */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   725
  do{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   726
    struct symbol *s1, *s2;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   727
    progress = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   728
    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
   729
      s1 = rp->lhs;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   730
      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
   731
        s2 = rp->rhs[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   732
        if( s2->type==TERMINAL ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   733
          progress += SetAdd(s1->firstset,s2->index);
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( s2->type==MULTITERMINAL ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   736
          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
   737
            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
   738
          }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   739
          break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   740
	}else if( s1==s2 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   741
          if( s1->lambda==LEMON_FALSE ) break;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
	}else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
          progress += SetUnion(s1->firstset,s2->firstset);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
          if( s2->lambda==LEMON_FALSE ) break;
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
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
  }while( progress );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
  return;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   750
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   751
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   752
/* 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
   753
** 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
   754
** can be computed later.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   755
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   756
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
   757
void FindStates(struct lemon *lemp)
708
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
  struct symbol *sp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   760
  struct rule *rp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   761
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   762
  Configlist_init();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   763
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   764
  /* Find the start symbol */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   765
  if( lemp->start ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   766
    sp = Symbol_find(lemp->start);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   767
    if( sp==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   768
      ErrorMsg(lemp->filename,0,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   769
"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
   770
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
   771
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
   772
      lemp->errorcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
      sp = lemp->rule->lhs;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   774
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   776
    sp = lemp->rule->lhs;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   777
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   778
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   779
  /* 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
   780
  ** 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
   781
  ** start symbol in this case.) */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   782
  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
   783
    int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   784
    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
   785
      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
   786
        ErrorMsg(lemp->filename,0,
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   787
"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
   788
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
   789
does not work properly.",sp->name);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   790
        lemp->errorcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   791
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   792
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   793
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   794
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   795
  /* 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
   796
  ** 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
   797
  ** left-hand side */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   798
  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
   799
    struct config *newcfp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   800
    rp->lhsStart = 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   801
    newcfp = Configlist_addbasis(rp,0);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   802
    SetAdd(newcfp->fws,0);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   803
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   804
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   805
  /* 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
   806
  ** 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
   807
  ** 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
   808
  (void)getstate(lemp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   809
  return;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   810
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   811
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   812
/* 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
   813
** 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
   814
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   815
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
   816
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
   817
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   818
  struct config *cfp, *bp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   819
  struct state *stp;
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
  /* 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
   822
  ** by prior calls to "Configlist_addbasis()". */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   823
  Configlist_sortbasis();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   824
  bp = Configlist_basis();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   825
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   826
  /* 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
   827
  stp = State_find(bp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   828
  if( stp ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   829
    /* 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
   830
    ** 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
   831
    ** 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
   832
    struct config *x, *y;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   833
    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
   834
      Plink_copy(&y->bplp,x->bplp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   835
      Plink_delete(x->fplp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   836
      x->fplp = x->bplp = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   837
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   838
    cfp = Configlist_return();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   839
    Configlist_eat(cfp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   840
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   841
    /* 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
   842
    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
   843
    Configlist_sort();           /* Sort the configuration closure */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   844
    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
   845
    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
   846
    MemoryCheck(stp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   847
    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
   848
    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
   849
    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
   850
    stp->ap = 0;                 /* No actions, yet. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   851
    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
   852
    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
   853
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   854
  return stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   855
}
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
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   858
** 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
   859
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   860
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
   861
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   862
  int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   863
  if( a==b ) return 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   864
  if( a->type!=MULTITERMINAL ) return 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   865
  if( b->type!=MULTITERMINAL ) return 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   866
  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
   867
  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
   868
    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
   869
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   870
  return 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   871
}
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
/* 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
   874
** 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
   875
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   876
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
   877
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   878
  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
   879
  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
   880
  struct config *newcfg;  /* */
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   881
  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
   882
  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
   883
  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
   884
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   885
  /* 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
   886
  ** state.  Initially, all configurations are incomplete */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   887
  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
   888
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   889
  /* 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
   890
  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
   891
    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
   892
    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
   893
    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
   894
    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
   895
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   896
    /* 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
   897
    ** 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
   898
    ** 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
   899
    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
   900
      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
   901
      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
   902
      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
   903
      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
   904
      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
   905
      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
   906
      Plink_add(&newcfg->bplp,bcfp);
708
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
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   909
    /* 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
   910
    ** constructed in the preceding loop */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   911
    newstp = getstate(lemp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   912
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   913
    /* 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
   914
    ** on the symbol "sp" */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   915
    if( sp->type==MULTITERMINAL ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   916
      int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   917
      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
   918
        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
   919
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   920
    }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   921
      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
   922
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   923
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   924
}
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
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   927
** Construct the propagation links
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   928
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   929
void FindLinks(struct lemon *lemp)
708
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
  int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   932
  struct config *cfp, *other;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   933
  struct state *stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   934
  struct plink *plp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   935
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   936
  /* Housekeeping detail:
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   937
  ** 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
   938
  ** which the link is attached. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   939
  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
   940
    stp = lemp->sorted[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   941
    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
   942
      cfp->stp = stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   943
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   944
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   945
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   946
  /* 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
   947
  ** 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
   948
  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
   949
    stp = lemp->sorted[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   950
    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
   951
      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
   952
        other = plp->cfp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   953
        Plink_add(&other->fplp,cfp);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   954
      }
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
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   957
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   958
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   959
/* Compute all followsets.
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
** 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
   962
** after a configuration.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   963
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   964
void FindFollowSets(struct lemon *lemp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   965
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   966
  int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   967
  struct config *cfp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   968
  struct plink *plp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   969
  int progress;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   970
  int change;
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
  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
   973
    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
   974
      cfp->status = INCOMPLETE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   975
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   976
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   977
  
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   978
  do{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   979
    progress = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   980
    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
   981
      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
   982
        if( cfp->status==COMPLETE ) continue;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   983
        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
   984
          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
   985
          if( change ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   986
            plp->cfp->status = INCOMPLETE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   987
            progress = 1;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   988
	  }
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
        cfp->status = COMPLETE;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   991
      }
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
  }while( progress );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   994
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   995
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
   996
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
   997
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   998
/* 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
   999
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1000
void FindActions(struct lemon *lemp)
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1001
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1002
  int i,j;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1003
  struct config *cfp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1004
  struct state *stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1005
  struct symbol *sp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1006
  struct rule *rp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1007
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1008
  /* Add all of the reduce actions 
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1009
  ** 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
  1010
  ** 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
  1011
  */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1012
  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
  1013
    stp = lemp->sorted[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1014
    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
  1015
      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
  1016
        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
  1017
          if( SetFind(cfp->fws,j) ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1018
            /* 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
  1019
            ** 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
  1020
            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
  1021
          }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1022
	}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1023
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1024
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1025
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1026
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1027
  /* Add the accepting token */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1028
  if( lemp->start ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1029
    sp = Symbol_find(lemp->start);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1030
    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
  1031
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1032
    sp = lemp->rule->lhs;
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
  /* 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
  1035
  ** 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
  1036
  ** start nonterminal.  */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1037
  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
  1038
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1039
  /* Resolve conflicts */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1040
  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
  1041
    struct action *ap, *nap;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1042
    struct state *stp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1043
    stp = lemp->sorted[i];
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1044
    /* assert( stp->ap ); */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1045
    stp->ap = Action_sort(stp->ap);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1046
    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
  1047
      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
  1048
         /* 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
  1049
         ** 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
  1050
         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
  1051
      }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1052
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1053
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1054
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1055
  /* 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
  1056
  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
  1057
  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
  1058
    struct action *ap;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1059
    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
  1060
      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
  1061
    }
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
  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
  1064
    if( rp->canReduce ) continue;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1065
    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
  1066
    lemp->errorcnt++;
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
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1069
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1070
/* 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
  1071
** 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
  1072
**
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1073
** NO LONGER TRUE:
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1074
**   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
  1075
**   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
  1076
**   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
  1077
**   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
  1078
**   use precedence to resolve the conflict.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1079
**
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1080
** 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
  1081
** 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
  1082
*/
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1083
static int resolve_conflict(
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1084
  struct action *apx,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1085
  struct action *apy,
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1086
  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
  1087
){
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1088
  struct symbol *spx, *spy;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1089
  int errcnt = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1090
  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
  1091
  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
  1092
    apy->type = SSCONFLICT;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1093
    errcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1094
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1095
  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
  1096
    spx = apx->sp;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1097
    spy = apy->x.rp->precsym;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1098
    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
  1099
      /* Not enough precedence information. */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1100
      apy->type = SRCONFLICT;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1101
      errcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1102
    }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
  1103
      apy->type = RD_RESOLVED;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1104
    }else if( spx->prec<spy->prec ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1105
      apx->type = SH_RESOLVED;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1106
    }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
  1107
      apy->type = RD_RESOLVED;                             /* associativity */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1108
    }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
  1109
      apx->type = SH_RESOLVED;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1110
    }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1111
      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
  1112
      apy->type = SRCONFLICT;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1113
      errcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1114
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1115
  }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
  1116
    spx = apx->x.rp->precsym;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1117
    spy = apy->x.rp->precsym;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1118
    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
  1119
    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
  1120
      apy->type = RRCONFLICT;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1121
      errcnt++;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1122
    }else if( spx->prec>spy->prec ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1123
      apy->type = RD_RESOLVED;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1124
    }else if( spx->prec<spy->prec ){
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
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1127
  }else{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1128
    assert( 
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1129
      apx->type==SH_RESOLVED ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1130
      apx->type==RD_RESOLVED ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1131
      apx->type==SSCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1132
      apx->type==SRCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1133
      apx->type==RRCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1134
      apy->type==SH_RESOLVED ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1135
      apy->type==RD_RESOLVED ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1136
      apy->type==SSCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1137
      apy->type==SRCONFLICT ||
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1138
      apy->type==RRCONFLICT
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1139
    );
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1140
    /* 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
  1141
    ** 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
  1142
    ** 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
  1143
  }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1144
  return errcnt;
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
/********************* From the file "configlist.c" *************************/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1147
/*
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1148
** 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
  1149
** in the LEMON parser generator.
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1150
*/
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1151
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1152
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
  1153
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
  1154
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
  1155
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
  1156
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
  1157
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1158
/* 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
  1159
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
  1160
  struct config *newcfg;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1161
  if( freelist==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1162
    int i;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1163
    int amt = 3;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1164
    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
  1165
    if( freelist==0 ){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1166
      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
  1167
      exit(1);
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1168
    }
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1169
    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
  1170
    freelist[amt-1].next = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1171
  }
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1172
  newcfg = freelist;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1173
  freelist = freelist->next;
831
80e47b096ecd Updated lemon to the latest in my fossil repo.
Ryan C. Gordon <icculus@icculus.org>
parents: 830
diff changeset
  1174
  return newcfg;
708
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1175
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1176
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1177
/* 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
  1178
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
  1179
{
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1180
  old->next = freelist;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1181
  freelist = old;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1182
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1183
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1184
/* Initialized the configuration list builder */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1185
void Configlist_init(){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1186
  current = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1187
  currentend = &current;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1188
  basis = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1189
  basisend = &basis;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1190
  Configtable_init();
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1191
  return;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1192
}
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1193
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1194
/* Initialized the configuration list builder */
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1195
void Configlist_reset(){
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1196
  current = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1197
  currentend = &current;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1198
  basis = 0;
d29c193900b0 Added lemon and lempar sources to revision control.
Ryan C. Gordon