--- /dev/null
+/* Automata conversion functions for DLG
+ *
+ * SOFTWARE RIGHTS
+ *
+ * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
+ * Set (PCCTS) -- PCCTS is in the public domain. An individual or
+ * company may do whatever they wish with source code distributed with
+ * PCCTS or the code generated by PCCTS, including the incorporation of
+ * PCCTS, or its output, into commerical software.
+ *
+ * We encourage users to develop software with PCCTS. However, we do ask
+ * that credit is given to us for developing PCCTS. By "credit",
+ * we mean that if you incorporate our source code into one of your
+ * programs (commercial product, research project, or otherwise) that you
+ * acknowledge this fact somewhere in the documentation, research report,
+ * etc... If you like PCCTS and have developed a nice tool with the
+ * output, please mention that you developed it using PCCTS. In
+ * addition, we ask that this header remain intact in our source code.
+ * As long as these guidelines are kept, we expect to continue enhancing
+ * this system and expect to make other tools available as they are
+ * completed.
+ *
+ * DLG 1.33
+ * Will Cohen
+ * With mods by Terence Parr; AHPCRC, University of Minnesota
+ * 1989-1995
+ */
+
+#include <stdio.h>
+#include "dlg.h"
+#ifdef MEMCHK
+#include "trax.h"
+#else
+#ifdef __STDC__
+#include <stdlib.h>
+#else
+#include <malloc.h>
+#endif /* __STDC__ */
+#endif
+
+#define hash_list struct _hash_list_
+hash_list{
+ hash_list *next; /* next thing in list */
+ dfa_node *node;
+};
+
+int dfa_allocated = 0; /* keeps track of number of dfa nodes */
+dfa_node **dfa_array; /* root of binary tree that stores dfa array */
+dfa_node *dfa_model_node;
+hash_list *dfa_hash[HASH_SIZE]; /* used to quickly find */
+ /* desired dfa node */
+
+void
+make_dfa_model_node(width)
+int width;
+{
+ register int i;
+ dfa_model_node = (dfa_node*) malloc(sizeof(dfa_node)
+ + sizeof(int)*width);
+ dfa_model_node->node_no = -1; /* impossible value for real dfa node */
+ dfa_model_node->dfa_set = 0;
+ dfa_model_node->alternatives = FALSE;
+ dfa_model_node->done = FALSE;
+ dfa_model_node->nfa_states = empty;
+ for(i = 0; i<width; i++){
+ dfa_model_node->trans[i] = NIL_INDEX;
+ }
+}
+
+
+/* adds a new nfa to the binary tree and returns a pointer to it */
+dfa_node *new_dfa_node(nfa_states)
+set nfa_states;
+{
+ register int j;
+ register dfa_node *t;
+ static int dfa_size=0; /* elements dfa_array[] can hold */
+
+ ++dfa_allocated;
+ if (dfa_size<=dfa_allocated){
+ /* need to redo array */
+ if (!dfa_array){
+ /* need some to do inital allocation */
+ dfa_size=dfa_allocated+DFA_MIN;
+ dfa_array=(dfa_node **) malloc(sizeof(dfa_node*)*
+ dfa_size);
+ }else{
+ /* need more space */
+ dfa_size=2*(dfa_allocated+1);
+ dfa_array=(dfa_node **) realloc(dfa_array,
+ sizeof(dfa_node*)*dfa_size);
+ }
+ }
+ /* fill out entry in array */
+ t = (dfa_node*) malloc(sizeof(nfa_node)+sizeof(int)*class_no);
+ *t = *dfa_model_node;
+ for (j=0; j<class_no; ++j)
+ t->trans[j] = NIL_INDEX;
+ t->node_no = dfa_allocated;
+ t->nfa_states = set_dup(nfa_states);
+ dfa_array[dfa_allocated] = t;
+ return t;
+}
+
+
+/* past a pointer to the start start of the nfa graph
+ * nfa_to_dfa convers this graph to dfa. The function returns
+ * a pointer to the first dfa state.
+ * NOTE: The function that prints out the table will have to figure out how
+ * to find the other dfa states given the first dfa_state and the number of dfa
+ * nodes allocated
+ */
+dfa_node **nfa_to_dfa(start)
+nfa_node *start;
+{
+ register dfa_node *d_state, *trans_d_state;
+ register int a;
+ set t;
+ int last_done;
+ unsigned *nfa_list;
+ unsigned *reach_list;
+
+ reach_list = (unsigned *) malloc((2+nfa_allocated)*sizeof(unsigned));
+ if (!start) return NULL;
+ t = set_of(NFA_NO(start));
+ _set_pdq(t,reach_list);
+ closure(&t,reach_list);
+ /* Make t a dfa state */
+ d_state = dfastate(t);
+ last_done = DFA_NO(d_state);
+
+ do {
+ /* Mark dfa state x as "done" */
+ d_state->done = TRUE;
+ nfa_list = set_pdq(d_state->nfa_states);
+ for (a = 0; a<class_no; ++a) {
+ /* Add NFA states reached by a from d_state */
+ reach(nfa_list,a,reach_list);
+ /* Were any states found? */
+ if ((*reach_list)!=nil) {
+ /* was t=empty; */
+ set_free(t);
+ /* yes, compute closure */
+ closure(&t,reach_list);
+ /* Make DFA state of it ... */
+ trans_d_state = dfastate(t);
+ /* And make transition x->t, labeled with a */
+ d_state->trans[a] = DFA_NO(trans_d_state);
+ d_state->alternatives = TRUE;
+ }
+ }
+ free(nfa_list);
+ ++last_done; /* move forward in queue */
+ /* And so forth until nothing isn't done */
+ d_state = DFA(last_done);
+ } while (last_done<=dfa_allocated);
+
+ free(reach_list);
+ set_free(t);
+
+ /* returns pointer to the array that holds the automaton */
+ return dfa_array;
+}
+
+clear_hash()
+{
+ register int i;
+
+ for(i=0; i<HASH_SIZE; ++i)
+ dfa_hash[i] = 0;
+}
+
+#if HASH_STAT
+fprint_hash_stats(f)
+FILE *f;
+{
+ register hash_list *p;
+ register int i,j;
+ register total;
+
+ total=0;
+ for(i=0; i<HASH_SIZE; ++i){
+ j=0;
+ p = dfa_hash[i];
+ while(p){
+ ++j;
+ p = p->next;
+ }
+ total+=j;
+ fprintf(f,"bin[%d] has %d\n",i,j);
+ }
+ fprintf(f,"total = %d\n",total);
+}
+#endif
+
+/* Returns a pointer to a dfa node that has the same nfa nodes in it.
+ * This may or maynot be a newly created node.
+ */
+dfa_node *dfastate(nfa_states)
+set nfa_states; /* list of nfa states it could be */
+{
+ register hash_list *p;
+ int bin;
+
+ /* hash using set and see if it exists */
+ bin = set_hash(nfa_states,HASH_SIZE);
+ p = dfa_hash[bin];
+ while(p && !set_equ(nfa_states,(p->node)->nfa_states)){
+ p = p->next;
+ }
+ if(!p){
+ /* next state to add to hash table */
+ p = (hash_list*)malloc(sizeof(hash_list));
+ p->node = new_dfa_node(nfa_states);
+ p->next = dfa_hash[bin];
+ dfa_hash[bin] = p;
+ }
+ return (p->node);
+}
+
+
+/* this reach assumes the closure has been done already on set */
+int reach(nfa_list,a,reach_list)
+unsigned *nfa_list;
+register int a;
+unsigned *reach_list;
+{
+ register unsigned *e;
+ register nfa_node *node;
+ int t=0;
+
+ e = nfa_list;
+ if (e){
+ while (*e != nil){
+ node = NFA(*e);
+ if (set_el(a,node->label)){
+ t=1;
+ *reach_list=NFA_NO(node->trans[0]);
+ ++reach_list;
+ }
+ ++e;
+ }
+ }
+ *reach_list=nil;
+ return t;
+}
+
+/* finds all the nodes that can be reached by epsilon transitions
+ from the set of a nodes and returns puts them back in set b */
+set closure(b,reach_list)
+set *b;
+unsigned *reach_list;
+{
+ register nfa_node *node,*n; /* current node being examined */
+ register unsigned *t,*e;
+
+ ++operation_no;
+#if 0
+ t = e = set_pdq(*b);
+#else
+ e=reach_list;
+#endif
+ while (*e != nil){
+ node = NFA(*e);
+ set_orel(NFA_NO(node),b);
+ /* mark it done */
+ node->nfa_set = operation_no;
+ if ((n=node->trans[0]) != NIL_INDEX && set_nil(node->label) &&
+ (n->nfa_set != operation_no)){
+ /* put in b */
+ set_orel(NFA_NO(n),b);
+ close1(n,operation_no,b);
+ }
+ if ((n=node->trans[1]) != NIL_INDEX &&
+ (n->nfa_set != operation_no)){
+ /* put in b */
+ set_orel(NFA_NO(node->trans[1]),b);
+ close1(n,operation_no,b);
+ }
+ ++e;
+ }
+#if 0
+ free(t);
+#endif
+ return *b;
+}
+
+close1(node,o,b)
+nfa_node *node;
+int o; /* marker to avoid cycles */
+set *b;
+{
+ register nfa_node *n; /* current node being examined */
+
+ /* mark it done */
+ node->nfa_set = o;
+ if ((n=node->trans[0]) != NIL_INDEX && set_nil(node->label) &&
+ (n->nfa_set != o)){
+ /* put in b */
+ set_orel(NFA_NO(n),b);
+ close1(n,o,b);
+ }
+ if ((n=node->trans[1]) != NIL_INDEX &&
+ (n->nfa_set != o)){
+ /* put in b */
+ set_orel(NFA_NO(node->trans[1]),b);
+ close1(n,o,b);
+ }
+}
--- /dev/null
+/* dlg header file
+ *
+ * SOFTWARE RIGHTS
+ *
+ * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
+ * Set (PCCTS) -- PCCTS is in the public domain. An individual or
+ * company may do whatever they wish with source code distributed with
+ * PCCTS or the code generated by PCCTS, including the incorporation of
+ * PCCTS, or its output, into commerical software.
+ *
+ * We encourage users to develop software with PCCTS. However, we do ask
+ * that credit is given to us for developing PCCTS. By "credit",
+ * we mean that if you incorporate our source code into one of your
+ * programs (commercial product, research project, or otherwise) that you
+ * acknowledge this fact somewhere in the documentation, research report,
+ * etc... If you like PCCTS and have developed a nice tool with the
+ * output, please mention that you developed it using PCCTS. In
+ * addition, we ask that this header remain intact in our source code.
+ * As long as these guidelines are kept, we expect to continue enhancing
+ * this system and expect to make other tools available as they are
+ * completed.
+ *
+ * DLG 1.33
+ * Will Cohen
+ * With mods by Terence Parr; AHPCRC, University of Minnesota
+ * 1989-1995
+ */
+#include "set.h"
+
+#define TRUE 1
+#define FALSE 0
+
+/***** output related stuff *******************/
+#define IN input_stream
+#define OUT output_stream
+
+#define MAX_MODES 50 /* number of %%names allowed */
+#define MAX_ON_LINE 10
+
+#define NFA_MIN 64 /* minimum nfa_array size */
+#define DFA_MIN 64 /* minimum dfa_array size */
+
+#define DEFAULT_CLASSNAME "DLGLexer"
+
+/* these macros allow the size of the character set to be easily changed */
+/* NOTE: do NOT change MIN_CHAR since EOF is the lowest char, -1 */
+#define MIN_CHAR (-1) /* lowest possible character possible on input */
+#define MAX_CHAR 255 /* highest possible character possible on input */
+#define CHAR_RANGE (1+(MAX_CHAR) - (MIN_CHAR))
+
+/* indicates that the not an "array" reference */
+#define NIL_INDEX 0
+
+/* size of hash table used to find dfa_states quickly */
+#define HASH_SIZE 211
+
+#define nfa_node struct _nfa_node
+nfa_node {
+ int node_no;
+ int nfa_set;
+ int accept; /* what case to use */
+ nfa_node *trans[2];
+ set label; /* one arc always labelled with epsilon */
+};
+
+#define dfa_node struct _dfa_node
+dfa_node {
+ int node_no;
+ int dfa_set;
+ int alternatives; /* used for interactive mode */
+ /* are more characters needed */
+ int done;
+ set nfa_states;
+ int trans[1];/* size of transition table depends on
+ * number of classes required for automata.
+ */
+
+
+};
+
+/******** macros for accessing the NFA and DFA nodes ****/
+#define NFA(x) (nfa_array[x])
+#define DFA(x) (dfa_array[x])
+#define DFA_NO(x) ( (x) ? (x)->node_no : NIL_INDEX)
+#define NFA_NO(x) ( (x) ? (x)->node_no : NIL_INDEX)
+
+/******** wrapper for memory checking ***/
+/*#define malloc(x) dlg_malloc((x),__FILE__,__LINE__)*/
+
+/*#define calloc(x,y) dlg_calloc((x),(y),__FILE__,__LINE__)*/
+
+/******** antlr attributes *************/
+typedef struct {
+ unsigned char letter;
+ nfa_node *l,*r;
+ set label;
+ } Attrib;
+
+#define zzcr_attr(attr, token, text) { \
+ (attr)->letter = text[0]; (attr)->l = NULL; \
+ (attr)->r = NULL; (attr)->label = empty; \
+}
+#define zzd_attr(a) set_free((a)->label);
+
+/******************** Variable ******************************/
+extern char program[]; /* tells what program this is */
+extern char version[]; /* tells what version this is */
+extern char *file_str[]; /* file names being used */
+extern int err_found; /* flag to indicate error occured */
+extern int action_no; /* last action function printed */
+extern int func_action; /* should actions be turned into functions?*/
+extern set used_chars; /* used to label trans. arcs */
+extern set used_classes; /* classes or chars used to label trans. arcs */
+extern int class_no; /* number of classes used */
+extern set class_sets[]; /* shows char. in each class */
+extern set normal_chars; /* mask off unused portion of set */
+extern int comp_level; /* what compression level to use */
+extern int interactive; /* interactive scanner (avoid lookahead)*/
+extern int mode_counter; /* keeps track of the number of %%name */
+extern int dfa_basep[]; /* start of each group of dfa */
+extern int dfa_class_nop[];/* number of transistion arcs in */
+ /* each dfa in each mode */
+extern int nfa_allocated;
+extern int dfa_allocated;
+extern nfa_node **nfa_array; /* start of nfa "array" */
+extern dfa_node **dfa_array; /* start of dfa "array" */
+extern int operation_no; /* unique number for each operation */
+extern FILE *input_stream; /* where description read from */
+extern FILE *output_stream; /* where to put the output */
+extern FILE *mode_stream; /* where to put the mode output */
+extern FILE *class_stream;
+extern char *mode_file; /* name of file for mode output */
+extern int gen_ansi; /* produce ansi compatible code */
+extern int case_insensitive;/* ignore case of input spec. */
+extern int warn_ambig; /* show if regular expressions ambiguous */
+extern int gen_cpp;
+extern char *cl_file_str;
+extern char *outdir;
+
+/******************** Functions ******************************/
+#ifdef __STDC__
+extern char *dlg_malloc(int, char *, int); /* wrapper malloc */
+extern char *dlg_calloc(int, int, char *, int); /* wrapper calloc */
+extern int reach(unsigned *, register int, unsigned *);
+extern set closure(set *, unsigned *);
+extern dfa_node *new_dfa_node(set);
+extern nfa_node *new_nfa_node(void);
+extern dfa_node *dfastate(set);
+extern dfa_node **nfa_to_dfa(nfa_node *);
+extern internal_error(char *, char *, int);
+extern FILE *read_stream(char *); /* opens file for reading */
+extern FILE *write_stream(char *); /* opens file for writing */
+extern void make_nfa_model_node(void);
+extern void make_dfa_model_node(int);
+extern char *ClassName(char *);
+extern char *OutMetaName(char *);
+extern void error(char*, int);
+extern void warning(char*, int);
+#else
+extern char *dlg_malloc(); /* wrapper malloc */
+extern char *dlg_calloc(); /* wrapper calloc */
+extern int reach();
+extern set closure();
+extern dfa_node *new_dfa_node();
+extern nfa_node *new_nfa_node();
+extern dfa_node *dfastate();
+extern dfa_node **nfa_to_dfa();
+extern internal_error();
+extern FILE *read_stream(); /* opens file for reading */
+extern FILE *write_stream(); /* opens file for writing */
+extern void make_nfa_model_node();
+extern void make_dfa_model_node();
+extern char *ClassName();
+extern char *OutMetaName();
+extern void error();
+extern void warning();
+#endif
+
+#include "config.h"
--- /dev/null
+#
+# Makefile for DLG 1.33
+# Terence Parr
+# Purdue University, U of MN, Parr Research Corporation
+# 1989-1994
+#
+# Ported to IBM C-Set/2 and Microsoft 6.0 by
+# Ed Harfmann
+# Micro Data Base Systems
+# Lafayette, Indiana
+#
+SET=../support/set
+PCCTS_H=../h
+
+##
+## Uncomment the appropriate section to build
+##
+
+#
+# OS/2 & DOS 16 bit using MSC 6.0
+#
+#CC=cl
+#ANTLR=..\bin\antlr
+#DLG=..\bin\dlg
+#CFLAGS= -I. -I$(SET) -I$(PCCTS_H) /AL /Za /W3 -DPC -DUSER_ZZSYN
+#OUT_OBJ = -Fo
+#LIBS=/NOD:LLIBCE LLIBCEP
+#OBJ_EXT = obj
+#
+#dlg.exe : dlg_p.obj dlg_a.obj main.obj err.obj set.obj support.obj \
+# output.obj relabel.obj automata.obj
+# link @<<
+#$** /NOI
+#$@ /STACK:16384
+#
+#$(LIBS: = +^
+#)
+#$(DEF_FILE) $(LFLAGS) ;
+#<<
+# bind $@ c:\os2\doscalls.lib
+# copy *.exe ..\bin
+#
+
+#
+# Borland C++ for DOS
+#
+#CC=bcc
+#ANTLR=..\bin\antlr
+#DLG=..\bin\dlg
+#CFLAGS= -I. -I$(SET) -I$(PCCTS_H) -ml -ff- -w- -DPC -DUSER_ZZSYN
+#OUT_OBJ = -o
+#LIBS= emu mathl cl
+#OBJ_EXT = obj
+#
+#dlg.exe : dlg_p.obj dlg_a.obj main.obj err.obj set.obj support.obj \
+# output.obj relabel.obj automata.obj
+# tlink @&&|
+#C0L $**
+#$@ /Tde /c
+#
+#$(LIBS)
+#$(DEF_FILE) $(LFLAGS) ;
+#|
+# copy *.exe ..\bin
+#
+
+#
+# C-Set/2 for OS/2
+#
+#CC=icc
+#CFLAGS= -I. -I$(SET) -I$(PCCTS_H) /Sa /W3 /DUSER_ZZSYN
+#OUT_OBJ = -Fo
+#LIBS=
+#ANTLR=..\bin\antlr
+#DLG=..\bin\dlg
+#OBJ_EXT=obj
+#
+#dlg.exe : dlg_p.obj dlg_a.obj main.obj err.obj set.obj support.obj \
+# output.obj relabel.obj automata.obj
+# link386 @<<
+#$** /NOI
+#$@ /STACK:32768
+#
+#$(LIBS: = +^
+#)
+#$(DEF_FILE) $(LFLAGS) ;
+#<<
+# copy *.exe ..\bin
+#
+
+#
+# Borland C++ for OS/2
+#
+#CC=bcc
+#CFLAGS= -I. -I$(SET) -I$(PCCTS_H) -w- -DUSER_ZZSYN
+#OUT_OBJ = -o
+#LIBS= c2 os2
+#
+#ANTLR=..\bin\antlr
+#DLG=..\bin\dlg
+#OBJ_EXT = obj
+#dlg.exe : dlg_p.obj dlg_a.obj main.obj err.obj set.obj support.obj \
+# output.obj relabel.obj automata.obj
+# tlink @&&|
+#c02 $** -c
+#dlg.exe
+#
+#C2 os2
+#
+#|
+# copy *.exe ..\bin
+#
+
+#
+# UNIX
+#
+CC=cc
+ANTLR=../bin/antlr
+DLG=../bin/dlg
+CFLAGS= -O -I. -I$(SET) -I$(PCCTS_H) -DUSER_ZZSYN
+OBJ_EXT=o
+OUT_OBJ = -o
+OBJ = dlg_p.o dlg_a.o main.o err.o set.o support.o output.o \
+ relabel.o automata.o
+
+dlg : $(OBJ) $(SRC)
+ $(CC) $(CFLAGS) -o dlg $(OBJ)
+ mv dlg ../bin
+
+SRC = dlg_p.c dlg_a.c main.c err.c $(SET)/set.c support.c output.c \
+ relabel.c automata.c
+
+#dlg_p.c parser.dlg err.c tokens.h : dlg_p.g
+# $(ANTLR) dlg_p.g
+
+#dlg_a.c mode.h : parser.dlg
+# $(DLG) -C2 parser.dlg dlg_a.c
+
+dlg_p.$(OBJ_EXT) : dlg_p.c dlg.h tokens.h mode.h
+ $(CC) $(CFLAGS) -c dlg_p.c
+
+dlg_a.$(OBJ_EXT) : dlg_a.c dlg.h tokens.h mode.h
+ $(CC) $(CFLAGS) -c dlg_a.c
+
+main.$(OBJ_EXT) : main.c dlg.h
+ $(CC) $(CFLAGS) -c main.c
+
+set.$(OBJ_EXT) : $(SET)/set.c
+ $(CC) -c $(CFLAGS) $(SET)/set.c
+
+lint:
+ lint *.c
+
+#clean up all the intermediate files
+clean:
+ rm -f *.$(OBJ_EXT) core
--- /dev/null
+/* output.c, output generator for dlg
+ *
+ * Output Notes:
+ *
+ * DfaStates == number of dfa nodes in automaton (just a #define)
+ * DfaState == type large enough to index every node in automaton
+ * <256 unsigned char, <65536 unsigned short, etc.
+ *
+ * Thus, the elements in each of the automaton states (st%d) are type DfaState
+ * and are size appropriately, since they must be able to index the next
+ * automaton state.
+ *
+ * dfa[] == a linear array that points to all the automaton states (st%d)
+ * (dfa_base[] should be the same, but isn't right now)
+ *
+ * accepts[] == Taking a closer look at this one, it probably shouldn't be type
+ * DfaState because there is no real requirement that the number of
+ * accepts states is less than the number of dfa state. However, if
+ * the number of accept states was more than the number of DFA states
+ * then the lexical specification would be really ambiguous.
+ *
+ * Another note. Is that is should be possible to fold accepts[] and
+ * actions[] together. If this is done, I would suggest get rid of
+ * accept[] and make actions[] have an entry for each state (st%d) in
+ * the automaton.
+ *
+ * dfa_base[] == starting location for each lexical mode. This should be
+ * Dfastate type (but isn't right now), since it points to the states
+ * in the automaton.
+ *
+ * dfa_class_no[] == indicates the number of columns each lexical mode has.
+ *
+ * b_class_no[] == pointer to the start of the translation array used to
+ * convert from input character to character class. This could cause
+ * problems if there are more than 256 classes
+ *
+ * shift%d[] == the actual translation arrays that convert the input character
+ * into the character class. These will have to change if there are
+ * more than 256 character classes.
+ *
+ * SOFTWARE RIGHTS
+ *
+ * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
+ * Set (PCCTS) -- PCCTS is in the public domain. An individual or
+ * company may do whatever they wish with source code distributed with
+ * PCCTS or the code generated by PCCTS, including the incorporation of
+ * PCCTS, or its output, into commerical software.
+ *
+ * We encourage users to develop software with PCCTS. However, we do ask
+ * that credit is given to us for developing PCCTS. By "credit",
+ * we mean that if you incorporate our source code into one of your
+ * programs (commercial product, research project, or otherwise) that you
+ * acknowledge this fact somewhere in the documentation, research report,
+ * etc... If you like PCCTS and have developed a nice tool with the
+ * output, please mention that you developed it using PCCTS. In
+ * addition, we ask that this header remain intact in our source code.
+ * As long as these guidelines are kept, we expect to continue enhancing
+ * this system and expect to make other tools available as they are
+ * completed.
+ *
+ * DLG 1.33
+ * Will Cohen
+ * With mods by Terence Parr; AHPCRC, University of Minnesota
+ * 1989-1995
+ */
+
+#include <stdio.h>
+#include "dlg.h"
+#ifdef MEMCHK
+#include "trax.h"
+#else
+#ifdef __STDC__
+#include <stdlib.h>
+#else
+#include <malloc.h>
+#endif /* __STDC__ */
+#endif
+
+static char *mode_name[MAX_MODES];
+static int mode_number[MAX_MODES];
+static int cur_mode=0;
+
+int operation_no = 0; /* used to mark nodes so that infinite loops avoided */
+int dfa_basep[MAX_MODES]; /* start of each group of states */
+int dfa_class_nop[MAX_MODES]; /* number of elements in each group of states*/
+
+int gen_ansi = FALSE; /* allows ansi code to be generated */
+
+FILE *input_stream; /* where to read description from */
+FILE *output_stream; /* where to put the output */
+FILE *mode_stream; /* where to put the mode.h stuff */
+FILE *class_stream; /* where to put the scan.h stuff (if gen_cpp) */
+
+/* NOTE: This section is MACHINE DEPENDENT */
+#define DIF_SIZE 4
+#if defined(PC) && !defined(PC32)
+long typesize[DIF_SIZE] = { 0x7f, 0x7fff, 0x7fff, 0x7fffffff };
+char t0[] = "unsigned char";
+char t1[] = "unsigned short";
+char t2[] = "unsigned int";
+char t3[] = "unsigned long";
+char *typevar[DIF_SIZE] = { t0, t1, t2, t3};
+#else
+long typesize[DIF_SIZE] = { 0x7f, 0x7fff, 0x7fffffff, 0x7fffffff };
+char t0[] = "unsigned char";
+char t1[] = "unsigned short";
+char t2[] = "unsigned int";
+char t3[] = "unsigned long";
+char *typevar[DIF_SIZE] = { t0, t1, t2, t3};
+#endif
+
+#ifdef __STDC__
+char *minsize(int);
+#else
+char *minsize();
+#endif
+
+/* Added by TJP August 1994 */
+/* Take in MyLexer and return MyLexer_h */
+static char *
+#ifdef __USE_PROTOS
+gate_symbol(char *name)
+#else
+gate_symbol(name)
+char *name;
+#endif
+{
+ static char buf[100];
+ sprintf(buf, "%s_h", name);
+ return buf;
+}
+
+/* Added by TJP August 1994 */
+static char *
+#ifdef __USE_PROTOS
+mystrdup(char *s)
+#else
+mystrdup(s)
+char *s;
+#endif
+{
+ char *p = (char *)malloc(strlen(s)+1);
+ strcpy(p, s);
+ return p;
+}
+
+p_class_hdr()
+{
+ if ( class_stream == NULL ) return;
+ fprintf(class_stream, "#ifndef %s\n", gate_symbol(ClassName("")));
+ fprintf(class_stream, "#define %s\n", gate_symbol(ClassName("")));
+ fprintf(class_stream, "/*\n");
+ fprintf(class_stream, " * D L G L e x e r C l a s s D e f i n i t i o n\n");
+ fprintf(class_stream, " *\n");
+ fprintf(class_stream, " * Generated from:");
+ fprintf(class_stream, " %s", file_str[0]);
+ fprintf(class_stream, "\n");
+ fprintf(class_stream, " *\n");
+ fprintf(class_stream, " * 1989-1994 by Will Cohen, Terence Parr, and Hank Dietz\n");
+ fprintf(class_stream, " * Purdue University Electrical Engineering\n");
+ fprintf(class_stream, " * DLG Version %s\n", version);
+ fprintf(class_stream, " */\n\n");
+}
+
+p_class_def()
+{
+ int i, m;
+
+ if ( class_stream == NULL ) return;
+ fprintf(class_stream, "\n");
+ fprintf(class_stream, "#include \"%s\"\n\n", DLEXERBASE_H);
+ fprintf(class_stream, "class %s : public DLGLexerBase {\n", ClassName(""));
+ fprintf(class_stream, "public:\n");
+ fprintf(class_stream, "\tstatic const int MAX_MODE;\n");
+ fprintf(class_stream, "\tstatic const int DfaStates;\n");
+ for (i=0; i<cur_mode; i++) {
+ fprintf(class_stream, "\tstatic const int %s;\n", mode_name[i]);
+ }
+
+ fprintf(class_stream, "\ttypedef %s DfaState;\n\n", minsize(dfa_allocated));
+ fprintf(class_stream, "\t%s(DLGInputStream *in,\n",ClassName(""));
+ fprintf(class_stream, "\t\tunsigned bufsize=2000)\n");
+ fprintf(class_stream, "\t\t: DLGLexerBase(in, bufsize, %d)\n", interactive);
+ fprintf(class_stream, "\t{\n");
+ fprintf(class_stream, "\t;\n");
+ fprintf(class_stream, "\t}\n");
+ fprintf(class_stream, "\tvoid mode(int);\n");
+ fprintf(class_stream, "\tANTLRTokenType nextTokenType(void);\n");
+ fprintf(class_stream, "\tvoid advance(void);\n");
+
+ fprintf(class_stream, "protected:\n");
+ for (i=1; i<=action_no; ++i) {
+ fprintf(class_stream, "\tANTLRTokenType act%d();\n", i);
+ }
+
+ for(m=0; m<(mode_counter-1); ++m){
+ for(i=dfa_basep[m]; i<dfa_basep[m+1]; ++i)
+ fprintf(class_stream, "\tstatic DfaState st%d[%d];\n", i-1, dfa_class_nop[m]+1);
+ }
+ for(i=dfa_basep[m]; i<=dfa_allocated; ++i)
+ fprintf(class_stream, "\tstatic DfaState st%d[%d];\n", i-1, dfa_class_nop[m]+1);
+
+ fprintf(class_stream, "\tstatic DfaState *dfa[%d];\n", dfa_allocated);
+ fprintf(class_stream, "\tstatic DfaState dfa_base[];\n");
+/* fprintf(class_stream, "\tstatic int dfa_base_no[];\n"); */
+ fprintf(class_stream, "\tstatic unsigned char *b_class_no[];\n");
+ fprintf(class_stream, "\tstatic DfaState accepts[%d];\n",dfa_allocated+1);
+ fprintf(class_stream, "\tstatic DLGChar alternatives[%d];\n",dfa_allocated+1);
+ /* WARNING: should be ANTLRTokenType for action table, but g++ 2.5.6 is hosed */
+ fprintf(class_stream, "\tstatic ANTLRTokenType (%s::*actions[%d])();\n", ClassName(""), action_no+1);
+ for(m=0; m<mode_counter; ++m) {
+ fprintf(class_stream, "\tstatic unsigned char shift%d[%d];\n",
+ m, CHAR_RANGE);
+ }
+ if (comp_level)
+ fprintf(class_stream, "\tint ZZSHIFT(int c) { return b_class_no[automaton][1+c]; }\n");
+ else
+ fprintf(class_stream, "\tint ZZSHIFT(int c) { return 1+c; }\n");
+
+ fprintf(class_stream, "};\n");
+
+ fprintf(class_stream, "typedef ANTLRTokenType (%s::*Ptr%sMemberFunc)();\n",
+ ClassName(""), ClassName(""));
+
+ fprintf(class_stream, "#endif\n");
+}
+
+/* generate required header on output */
+
+p_head()
+{
+ fprintf(OUT, "/*\n");
+ fprintf(OUT, " * D L G tables\n");
+ fprintf(OUT, " *\n");
+ fprintf(OUT, " * Generated from:");
+ fprintf(OUT, " %s", file_str[0]);
+ fprintf(OUT, "\n");
+ fprintf(OUT, " *\n");
+ fprintf(OUT, " * 1989-1994 by Will Cohen, Terence Parr, and Hank Dietz\n");
+ fprintf(OUT, " * Purdue University Electrical Engineering\n");
+ fprintf(OUT, " * DLG Version %s\n", version);
+ fprintf(OUT, " */\n\n");
+ if ( gen_cpp ) fprintf(OUT, "#include <stdio.h>\n");
+ if ( !gen_cpp ) fprintf(OUT, "#include \"%s\"\n\n", mode_file);
+ fprintf(OUT,"\n");
+}
+
+p_includes()
+{
+ int i;
+
+ fprintf(OUT, "#include \"%s\"\n", APARSER_H);
+ fprintf(OUT, "#include \"%s\"\n", DLEXERBASE_H);
+ fprintf(OUT, "#include \"%s\"\n", ClassName(".h"));
+}
+
+/* generate code to tie up any loose ends */
+
+p_tail()
+{
+ if ( gen_cpp ) {
+ if ( strcmp(ClassName(""), DEFAULT_CLASSNAME)!=0 )
+ fprintf(OUT, "#define DLGLexer %s\n", ClassName(""));
+ fprintf(OUT, "#include \"%s\"\n", DLEXER_C);
+ return;
+ }
+ fprintf(OUT, "\n");
+ fprintf(OUT, "\n");
+ if (comp_level)
+ fprintf(OUT, "#define ZZSHIFT(c) (b_class_no[zzauto][1+c])\n");
+ else
+ fprintf(OUT, "#define ZZSHIFT(c) (1+c)\n");
+ if ( !gen_cpp ) fprintf(OUT, "#define MAX_MODE %d\n",mode_counter);
+ fprintf(OUT, "#include \"dlgauto.h\"\n");
+}
+
+
+/* output the table of DFA for general use */
+
+p_tables()
+{
+ char *minsize();
+
+ if ( !gen_cpp ) {
+ fprintf(OUT, "#define DfaStates\t%d\n", dfa_allocated);
+ fprintf(OUT, "typedef %s DfaState;\n\n", minsize(dfa_allocated));
+ }
+
+ if ( gen_cpp ) {
+ int i;
+ fprintf(OUT, "\n");
+ fprintf(OUT, "const int %s::MAX_MODE=%d;\n",
+ ClassName(""),
+ mode_counter);
+ fprintf(OUT, "const int %s::DfaStates=%d;\n",
+ ClassName(""),
+ dfa_allocated);
+ for (i=0; i<cur_mode; i++) {
+ fprintf(OUT, "const int %s::%s=%d;\n",
+ ClassName(""), mode_name[i], mode_number[i]);
+ }
+ fprintf(OUT, "\n");
+ }
+
+ p_node_table();
+ p_dfa_table();
+ p_accept_table();
+ p_action_table();
+ p_base_table();
+ p_class_table();
+ if (comp_level)
+ p_bshift_table();
+ if (interactive || gen_cpp )
+ p_alternative_table();
+}
+
+
+/* figures out the smallest variable type that will hold the transitions
+ */
+char *minsize(elements)
+int elements;
+{
+ int i = 0;
+
+ while (elements > typesize[i])
+ ++i;
+ return typevar[i];
+}
+
+
+p_node_table()
+{
+ register int i;
+ register int m = 0;
+
+ for(m=0; m<(mode_counter-1); ++m){
+ for(i=dfa_basep[m]; i<dfa_basep[m+1]; ++i)
+ p_single_node(i,dfa_class_nop[m]);
+ }
+ for(i=dfa_basep[m]; i<=dfa_allocated; ++i)
+ p_single_node(i,dfa_class_nop[m]);
+}
+
+
+p_single_node(i,classes)
+int i,classes;
+{
+ register int j;
+ register int trans, items_on_line;
+
+#if 1
+ /* extra state (classes+1) for invalid characters */
+ fprintf(OUT, "%sDfaState %sst%d[%d] = {\n ",
+ gen_cpp?ClassName("::"):"static ",
+ gen_cpp?ClassName("::"):"",(i-1), (classes+1));
+#else
+ fprintf(OUT, "static DfaState st%d[%d] = {\n ", (i-1), classes);
+#endif
+ items_on_line = MAX_ON_LINE;
+ for(j=0; j<classes; ++j){
+ DAWDLE;
+ trans = DFA(i)->trans[j];
+ if (trans == NIL_INDEX)
+ trans = dfa_allocated+1;
+ /* all of DFA moved down one in array */
+ fprintf(OUT, "%d", trans-1);
+ fprintf(OUT, ", ");
+ if (!(--items_on_line)){
+ fprintf(OUT, "\n ");
+ items_on_line = MAX_ON_LINE;
+ }
+ }
+#if 1
+ /* put in jump to error state */
+ fprintf(OUT, "%d\n};\n\n", dfa_allocated);
+#else
+ fprintf(OUT, "\n};\n\n");
+#endif
+}
+
+
+p_dfa_table()
+{
+ register int i;
+
+ fprintf(OUT, "\n%sDfaState *%sdfa[%d] = {\n",
+ gen_cpp?ClassName("::"):"",gen_cpp?ClassName("::"):"", dfa_allocated);
+ for (i=0; i<(dfa_allocated-1); ++i){
+ fprintf(OUT, "\tst%d,\n", i);
+ }
+ fprintf(OUT, "\tst%d\n", i);
+ fprintf(OUT, "};\n\n");
+}
+
+
+p_accept_table()
+{
+ register int i = 1;
+ register int items_on_line = 0;
+ int true_interactive = TRUE;
+
+ /* make sure element for one past (zzerraction) -WEC 12/16/92 */
+ fprintf(OUT,"\n%sDfaState %saccepts[%d] = {\n ",
+ gen_cpp?ClassName("::"):"",
+ gen_cpp?ClassName("::"):"",
+ dfa_allocated+1);
+ /* don't do anything if no dfa nodes */
+ if (i>dfa_allocated) goto skip_accepts;
+ while (TRUE){
+ int accept;
+ set accept_set;
+ set nfa_states;
+ unsigned int *t, *nfa_i;
+ unsigned int *q, *regular_expr;
+
+ accept_set = empty;
+ nfa_states = DFA(i)->nfa_states;
+ t = nfa_i = set_pdq(nfa_states);
+ /* NOTE: picks lowest accept because accepts monotonic */
+ /* with respect to nfa node numbers and set_pdq */
+ /* returns in that order */
+ while((*nfa_i != nil) && (!(accept = NFA(*nfa_i)->accept))){
+ nfa_i++;
+ }
+
+ /* figure out if more than one accept state there */
+ if (warn_ambig ){
+ set_orel(accept, &accept_set);
+ while(*nfa_i != nil){
+ set_orel(NFA(*nfa_i)->accept, &accept_set);
+ nfa_i++;
+ }
+ /* remove error action from consideration */
+ set_rm(0, accept_set);
+
+ if( set_deg(accept_set)>1){
+ fprintf(stderr, "dlg warning: ambiguous regular expression ");
+ q = regular_expr = set_pdq(accept_set);
+ while(*regular_expr != nil){
+ fprintf(stderr," %d ", *regular_expr);
+ ++regular_expr;
+ }
+ fprintf(stderr, "\n");
+ free(q);
+ }
+ }
+
+ if ((DFA(i)->alternatives) && (accept != 0)){
+ true_interactive = FALSE;
+ }
+ fprintf(OUT, "%d, ", accept);
+
+ /* free up memory before we "break" below -ATG 4/6/95 */
+ free(t);
+ set_free(accept_set);
+
+ if ((++i)>dfa_allocated)
+ break;
+ if ((++items_on_line)>=MAX_ON_LINE){
+ fprintf(OUT,"\n ");
+ items_on_line = 0;
+ }
+/*
+ free(t);
+ set_free(accept_set);
+*/
+ }
+ /* make sure element for one past (zzerraction) -WEC 12/16/92 */
+skip_accepts:
+ fprintf(OUT, "0\n};\n\n");
+}
+
+
+p_action_table()
+{
+ register int i;
+ char* className = ClassName("");
+
+ if ( gen_cpp )
+ fprintf(OUT, "Ptr%sMemberFunc %s::actions[%d] = {\n", className,
+ className, action_no+1);
+ else
+ fprintf(OUT, "void (*actions[%d])() = {\n", action_no+1);
+ if ( gen_cpp )
+/* fprintf(OUT, "\t(Ptr%sMemberFunc)&%s::erraction,\n", className, className);*/
+ fprintf(OUT, "\t&%s::erraction,\n", className);
+ else
+ fprintf(OUT, "\tzzerraction,\n");
+ for (i=1; i<action_no; ++i) {
+ if ( gen_cpp )
+/* fprintf(OUT,"\t(Ptr%sMemberFunc)&%s::act%d,\n", className, className, i);*/
+ fprintf(OUT,"\t&%s::act%d,\n", className, i);
+ else
+ fprintf(OUT,"\tact%d,\n", i);
+ DAWDLE;
+ }
+ if ( gen_cpp )
+/* fprintf(OUT,"\t(Ptr%sMemberFunc)&%s::act%d\n", className, className, i);*/
+ fprintf(OUT,"\t&%s::act%d\n", className, i);
+ else
+ fprintf(OUT,"\tact%d\n", i);
+ fprintf(OUT, "};\n\n");
+}
+
+
+p_shift_table(m)
+int m;
+{
+ register int i = 0, j;
+ register int items_on_line = 0;
+
+ fprintf(OUT, "%s unsigned char %sshift%d[%d] = {\n ",
+ gen_cpp?"":"static",
+ gen_cpp?ClassName("::"):"", m, CHAR_RANGE);
+ while (TRUE){
+ /* find which partition character i is in */
+ for (j=0; j<dfa_class_nop[mode_counter]; ++j){
+ if (set_el(i,class_sets[j]))
+ break;
+ }
+ fprintf(OUT,"%d",j);
+ if ((++i)>=CHAR_RANGE)
+ break;
+ fprintf(OUT,", ");
+ if ((++items_on_line)>=MAX_ON_LINE){
+ fprintf(OUT,"\n ");
+ items_on_line = 0;
+ }
+ }
+ fprintf(OUT, "\n};\n\n");
+}
+
+
+p_base_table()
+{
+ register int m;
+
+ fprintf(OUT, "%sDfaState %sdfa_base[] = {\n",
+ gen_cpp?ClassName("::"):"static ",
+ gen_cpp?ClassName("::"):"");
+ for(m=0; m<(mode_counter-1); ++m)
+ fprintf(OUT, "\t%d,\n", dfa_basep[m]-1);
+ fprintf(OUT, "\t%d\n};\n\n", dfa_basep[m]-1);
+}
+
+
+p_class_table()
+{
+#if 0
+ register int m;
+
+ fprintf(OUT,"%s int %sdfa_class_no[] = {\n",
+ gen_cpp?"":"static",
+ gen_cpp?ClassName("::"):"");
+ for(m=0; m<(mode_counter-1); ++m)
+ fprintf(OUT,"\t%d,\n", dfa_class_nop[m]);
+ fprintf(OUT,"\t%d\n};\n\n", dfa_class_nop[m]);
+#endif
+}
+
+
+p_bshift_table()
+{
+ register int m;
+
+ fprintf(OUT,"%s unsigned char *%sb_class_no[] = {\n",
+ gen_cpp?"":"static",
+ gen_cpp?ClassName("::"):"");
+ for(m=0; m<(mode_counter-1); ++m)
+ fprintf(OUT, "\tshift%d,\n", m);
+ fprintf(OUT, "\tshift%d\n};\n\n", m);
+}
+
+
+p_alternative_table()
+{
+ register int i;
+
+ if ( !gen_cpp ) fprintf(OUT, "#define ZZINTERACTIVE\n\n");
+ if ( gen_cpp )
+ fprintf(OUT, "DLGChar %salternatives[%sDfaStates+1] = {\n",
+ ClassName("::"),
+ ClassName("::"));
+ else
+ fprintf(OUT, "static %s zzalternatives[DfaStates+1] = {\n",
+ minsize(dfa_allocated));
+
+ for(i=1; i<=dfa_allocated; ++i)
+ fprintf(OUT, "\t%d,\n", DFA(i)->alternatives);
+ fprintf(OUT, "/* must have 0 for zzalternatives[DfaStates] */\n");
+ fprintf(OUT, "\t0\n};\n\n");
+}
+
+
+p_mode_def(s,m)
+char *s;
+int m;
+{
+ if ( gen_cpp )
+ {
+ mode_name[cur_mode] = mystrdup(s);
+ mode_number[cur_mode] = m;
+ cur_mode++;
+ }
+ else
+ fprintf(mode_stream, "#define %s %d\n", s, m);
+}
+
+char *
+ClassName(suffix)
+char *suffix;
+{
+ static char buf[200];
+ extern char *class_name;
+
+ sprintf(buf, "%s%s", class_name, suffix);
+ return buf;
+}
+
+#ifdef DEBUG
+/* print out a particular nfa node that is pointed to by p */
+p_nfa_node(p)
+nfa_node *p;
+{
+ register nfa_node *t;
+
+ if (p != NIL_INDEX){
+ printf("NFA state : %d\naccept state : %d\n",
+ NFA_NO(p),p->accept);
+ if (p->trans[0] != NIL_INDEX){
+ printf("trans[0] => %d on ", NFA_NO(p->trans[0]));
+ p_set(p->label);
+ printf("\n");
+ }
+ else
+ printf("trans[0] => nil\n");
+ if (p->trans[1] != NIL_INDEX)
+ printf("trans[1] => %d on epsilon\n",
+ NFA_NO(p->trans[1]));
+ else
+ printf("trans[1] => nil\n");
+ printf("\n");
+ }
+}
+#endif
+
+#ifdef DEBUG
+/* code to print out special structures when using a debugger */
+
+p_nfa(p)
+nfa_node *p; /* state number also index into array */
+{
+/* each node has a marker on it so it only gets printed once */
+
+ operation_no++; /* get new number */
+ s_p_nfa(p);
+}
+
+s_p_nfa(p)
+nfa_node *p; /* state number also index into array */
+{
+ if ((p != NIL_INDEX) && (p->nfa_set != operation_no)){
+ /* so it is only printed once */
+ p->nfa_set = operation_no;
+ p_nfa_node(p);
+ s_p_nfa(p->trans[0]);
+ s_p_nfa(p->trans[1]);
+ }
+}
+
+p_dfa_node(p)
+dfa_node *p;
+{
+ int i;
+
+ if (p != NIL_INDEX){
+ printf("DFA state :%d\n",NFA_NO(p));
+ if (p->done)
+ printf("done\n");
+ else
+ printf("undone\n");
+ printf("from nfa states : ");
+ p_set(p->nfa_states);
+ printf("\n");
+ /* NOTE: trans arcs stored as ints rather than pointer*/
+ for (i=0; i<class_no; i++){
+ printf("%d ",p->trans[i]);
+ }
+ printf("\n\n");
+ }
+}
+
+p_dfa()
+{
+/* prints out all the dfa nodes actually allocated */
+
+ int i;
+
+ for (i = 1; i<=dfa_allocated; i++)
+ p_dfa_node(NFA(i));
+}
+
+
+/* print out numbers in the set label */
+p_set(label)
+set label;
+{
+ unsigned *t, *e;
+
+ if (set_nil(label)){
+ printf("epsilon\n");
+ }else{
+ t = e = set_pdq(label);
+ while(*e != nil){
+ printf("%d ", (*e+MIN_CHAR));
+ e++;
+ }
+ printf("\n");
+ free(t);
+ }
+
+}
+#endif
+
--- /dev/null
+/* This group of functions does the character class compression.
+ It goes over the dfa and relabels the arcs with the partitions
+ of characters in the NFA. The partitions are stored in the
+ array class.
+
+ *
+ * SOFTWARE RIGHTS
+ *
+ * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
+ * Set (PCCTS) -- PCCTS is in the public domain. An individual or
+ * company may do whatever they wish with source code distributed with
+ * PCCTS or the code generated by PCCTS, including the incorporation of
+ * PCCTS, or its output, into commerical software.
+ *
+ * We encourage users to develop software with PCCTS. However, we do ask
+ * that credit is given to us for developing PCCTS. By "credit",
+ * we mean that if you incorporate our source code into one of your
+ * programs (commercial product, research project, or otherwise) that you
+ * acknowledge this fact somewhere in the documentation, research report,
+ * etc... If you like PCCTS and have developed a nice tool with the
+ * output, please mention that you developed it using PCCTS. In
+ * addition, we ask that this header remain intact in our source code.
+ * As long as these guidelines are kept, we expect to continue enhancing
+ * this system and expect to make other tools available as they are
+ * completed.
+ *
+ * DLG 1.33
+ * Will Cohen
+ * With mods by Terence Parr; AHPCRC, University of Minnesota
+ * 1989-1995
+ */
+
+#include <stdio.h>
+#include "dlg.h"
+#ifdef MEMCHK
+#include "trax.h"
+#else
+#ifdef __STDC__
+#include <stdlib.h>
+#else
+#include <malloc.h>
+#endif /* __STDC__ */
+#endif
+
+int class_no = CHAR_RANGE; /* number of classes for labels */
+int first_el[CHAR_RANGE]; /* first element in each class partition */
+set class_sets[CHAR_RANGE]; /* array holds partitions from class */
+ /* compression */
+
+/* goes through labels on NFA graph and partitions the characters into
+ * character classes. This reduces the amount of space required for each
+ * dfa node, since only one arc is required each class instead of one arc
+ * for each character
+ * level:
+ * 0 no compression done
+ * 1 remove unused characters from classes
+ * 2 compress equivalent characters into same class
+ *
+ * returns the number of character classes required
+ */
+int relabel(start,level)
+int level;
+nfa_node *start;
+{
+ if (level){
+ set_free(used_classes);
+ partition(start,level);
+ label_with_classes(start);
+ }else{
+ /* classes equivalent to all characters in alphabet */
+ class_no = CHAR_RANGE;
+ }
+ return class_no;
+}
+
+/* makes character class sets for new labels */
+partition(start,level)
+nfa_node *start; /* beginning of nfa graph */
+int level; /* compression level to uses */
+{
+ set current_class;
+ set unpart_chars;
+ set temp;
+
+ unpart_chars = set_dup(used_chars);
+#if 0
+ /* EOF (-1+1) alway in class 0 */
+ class_sets[0] = set_of(0);
+ first_el[0] = 0;
+ used_classes = set_of(0);
+ temp = set_dif(unpart_chars, class_sets[0]);
+ set_free(unpart_chars);
+ unpart_chars = temp;
+ class_no = 1;
+#else
+ class_no = 0;
+#endif
+ while (!set_nil(unpart_chars)){
+ /* don't look for equivalent labels if c <= 1 */
+ if (level <= 1){
+ current_class = set_of(set_int(unpart_chars));
+ }else{
+ current_class = set_dup(unpart_chars);
+ intersect_nfa_labels(start,¤t_class);
+ }
+ set_orel(class_no,&used_classes);
+ first_el[class_no] = set_int(current_class);
+ class_sets[class_no] = current_class;
+ temp = set_dif(unpart_chars,current_class);
+ set_free(unpart_chars);
+ unpart_chars = temp;
+ ++class_no;
+ }
+
+ /* free unpart_chars -ATG 5/6/95 */
+ set_free(unpart_chars);
+
+#if 0
+ /* group all the other unused characters into a class */
+ set_orel(class_no,&used_classes);
+ first_el[class_no] = set_int(current_class);
+ class_sets[class_no] = set_dif(normal_chars,used_chars);
+ ++class_no;
+#endif
+}
+
+
+/* given pointer to beginning of graph and recursively walks it trying
+ * to find a maximal partition. This partion in returned in maximal_class
+ */
+intersect_nfa_labels(start,maximal_class)
+nfa_node *start;
+set *maximal_class;
+{
+ /* pick a new operation number */
+ ++operation_no;
+ r_intersect(start,maximal_class);
+}
+
+r_intersect(start,maximal_class)
+nfa_node *start;
+set * maximal_class;
+{
+ set temp;
+
+ if(start && start->nfa_set != operation_no)
+ {
+ start->nfa_set = operation_no;
+ temp = set_and(*maximal_class,start->label);
+ if (!set_nil(temp))
+ {
+ set_free(*maximal_class);
+ *maximal_class = temp;
+ }else{
+ set_free(temp);
+ }
+ r_intersect(start->trans[0],maximal_class);
+ r_intersect(start->trans[1],maximal_class);
+ }
+}
+
+
+/* puts class labels in place of old character labels */
+label_with_classes(start)
+nfa_node *start;
+{
+ ++operation_no;
+ label_node(start);
+}
+
+label_node(start)
+nfa_node *start;
+{
+ set new_label;
+ register int i;
+
+ /* only do node if it hasn't been done before */
+ if (start && start->nfa_set != operation_no){
+ start->nfa_set = operation_no;
+ new_label = empty;
+ for (i = 0; i<class_no; ++i){
+ /* if one element of class in old_label,
+ all elements are. */
+ if (set_el(first_el[i],start->label))
+ set_orel(i,&new_label);
+ }
+ set_free(start->label);
+ start->label = new_label;
+ /* do any nodes that can be reached from this one */
+ label_node(start->trans[0]);
+ label_node(start->trans[1]);
+ }
+}
--- /dev/null
+#ifndef STDPCCTS_H
+#define STDPCCTS_H
+/*
+ * stdpccts.h -- P C C T S I n c l u d e
+ *
+ * Terence Parr, Will Cohen, and Hank Dietz: 1989-1994
+ * Purdue University Electrical Engineering
+ * With AHPCRC, University of Minnesota
+ * ANTLR Version 1.32
+ */
+#include <stdio.h>
+#define ANTLR_VERSION 132
+
+#include <ctype.h>
+#include "dlg.h"
+#ifdef MEMCHK
+#include "trax.h"
+#endif
+#define zzSET_SIZE 8
+#include "antlr.h"
+#include "tokens.h"
+#include "dlgdef.h"
+#include "mode.h"
+#endif
--- /dev/null
+/*
+ * SOFTWARE RIGHTS
+ *
+ * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
+ * Set (PCCTS) -- PCCTS is in the public domain. An individual or
+ * company may do whatever they wish with source code distributed with
+ * PCCTS or the code generated by PCCTS, including the incorporation of
+ * PCCTS, or its output, into commerical software.
+ *
+ * We encourage users to develop software with PCCTS. However, we do ask
+ * that credit is given to us for developing PCCTS. By "credit",
+ * we mean that if you incorporate our source code into one of your
+ * programs (commercial product, research project, or otherwise) that you
+ * acknowledge this fact somewhere in the documentation, research report,
+ * etc... If you like PCCTS and have developed a nice tool with the
+ * output, please mention that you developed it using PCCTS. In
+ * addition, we ask that this header remain intact in our source code.
+ * As long as these guidelines are kept, we expect to continue enhancing
+ * this system and expect to make other tools available as they are
+ * completed.
+ *
+ * DLG 1.33
+ * Will Cohen
+ * With mods by Terence Parr; AHPCRC, University of Minnesota
+ * 1989-1995
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "dlg.h"
+#ifdef MEMCHK
+#include "trax.h"
+#else
+#ifdef __STDC__
+#include <stdlib.h>
+#else
+#include <malloc.h>
+#endif /* __STDC__ */
+#endif
+
+int err_found = 0; /* indicates whether problem found */
+
+internal_error(s,file,line)
+char *s,*file;
+int line;
+{
+ fprintf(stderr,s,file,line);
+ exit(PCCTS_EXIT_FAILURE);
+}
+
+char *dlg_malloc(bytes,file,line)
+int bytes;
+char *file;
+int line;
+{
+ char *t;
+
+ t = (char *) malloc(bytes);
+ if (!t){
+ /* error */
+ internal_error("%s(%d): unable to allocate memory\n",
+ file,line);
+ }
+ return t;
+}
+
+
+char *dlg_calloc(n,bytes,file,line)
+int n,bytes;
+char *file;
+int line;
+{
+ char *t;
+
+ t = (char *) calloc(n,bytes);
+ if (!t){
+ /* error */
+ internal_error("%s(%d): unable to allocate memory\n",
+ file,line);
+ }
+ return t;
+}
+
+
+FILE *read_stream(name)
+char *name;
+{
+ FILE *f;
+
+ if (name){
+ if (name[0] == '-') {
+ fprintf(stderr, "dlg: invalid option: '%s'\n", name);
+ f = NULL;
+ }else{
+ f = fopen(name, "r");
+ if (f == NULL){
+ /* couldn't open file */
+ fprintf(stderr,
+ "dlg: Warning: Can't read file %s.\n",
+ name);
+ }
+ }
+ }else{
+ /* open stdin if nothing there */
+ f = stdin;
+ }
+ return f;
+}
+
+FILE *write_stream(name)
+char *name;
+{
+ FILE *f;
+
+ if (name){
+ if (name[0] == '-') {
+ fprintf(stderr, "dlg: invalid option: '%s'\n", name);
+ f = NULL;
+ }else{
+ f = fopen(OutMetaName(name), "w");
+ if (f == NULL){
+ /* couldn't open file */
+ fprintf(stderr,
+ "dlg: Warning: Can't write to file %s.\n",
+ name);
+ }
+ else
+ special_fopen_actions(OutMetaName(name));
+ }
+ }else{
+ /* open stdout if nothing there */
+ f = stdout;
+ }
+ return f;
+}
+
+
+void fatal(message,line_no)
+char *message;
+int line_no;
+{
+ fprintf(stderr,ErrHdr,
+ (file_str[0] ? file_str[0] : "stdin"), line_no);
+ fprintf(stderr, " Fatal: %s\n", message);
+ exit(PCCTS_EXIT_FAILURE);
+}
+
+void error(message,line_no)
+char *message;
+int line_no;
+{
+ fprintf(stderr,ErrHdr,
+ (file_str[0] ? file_str[0] : "stdin"), line_no);
+ fprintf(stderr, " Error: %s\n", message);
+ err_found = 1;
+}
+
+void warning(message,line_no)
+char *message;
+int line_no;
+{
+ fprintf(stderr,ErrHdr,
+ (file_str[0] ? file_str[0] : "stdin"), line_no);
+ fprintf(stderr, " Warning: %s\n", message);
+}
+
+char *
+#ifdef __STDC__
+OutMetaName(char *n)
+#else
+OutMetaName(n)
+char *n;
+#endif
+{
+ static char buf[200+1];
+
+ if ( strcmp(outdir,TopDirectory)==0 ) return n;
+ strcpy(buf, outdir);
+ if ( strcmp(&buf[strlen(buf) - 1], DirectorySymbol ) )
+ strcat(buf, DirectorySymbol);
+ strcat(buf, n);
+ return buf;
+}
--- /dev/null
+#ifndef tokens_h
+#define tokens_h
+/* tokens.h -- List of labelled tokens and stuff
+ *
+ * Generated from: dlg_p.g
+ *
+ * Terence Parr, Will Cohen, and Hank Dietz: 1989-1994
+ * Purdue University Electrical Engineering
+ * ANTLR Version 1.32
+ */
+#define zzEOF_TOKEN 1
+#define L_EOF 4
+#define PER_PER 5
+#define NAME_PER_PER 6
+#define ACTION 7
+#define GREAT_GREAT 8
+#define L_BRACE 9
+#define R_BRACE 10
+#define L_PAR 11
+#define R_PAR 12
+#define L_BRACK 13
+#define R_BRACK 14
+#define ZERO_MORE 15
+#define ONE_MORE 16
+#define OR 17
+#define RANGE 18
+#define NOT 19
+#define OCTAL_VALUE 20
+#define HEX_VALUE 21
+#define DEC_VALUE 22
+#define TAB 23
+#define NL 24
+#define CR 25
+#define BS 26
+#define LIT 27
+#define REGCHAR 28
+
+#ifdef __STDC__
+void grammar(void);
+#else
+extern void grammar();
+#endif
+
+#ifdef __STDC__
+void start_states(void);
+#else
+extern void start_states();
+#endif
+
+#ifdef __STDC__
+void do_conversion(void);
+#else
+extern void do_conversion();
+#endif
+
+#ifdef __STDC__
+void rule_list(void);
+#else
+extern void rule_list();
+#endif
+
+#ifdef __STDC__
+void rule(void);
+#else
+extern void rule();
+#endif
+
+#ifdef __STDC__
+void reg_expr(void);
+#else
+extern void reg_expr();
+#endif
+
+#ifdef __STDC__
+void and_expr(void);
+#else
+extern void and_expr();
+#endif
+
+#ifdef __STDC__
+void repeat_expr(void);
+#else
+extern void repeat_expr();
+#endif
+
+#ifdef __STDC__
+void expr(void);
+#else
+extern void expr();
+#endif
+
+#ifdef __STDC__
+void atom_list(void);
+#else
+extern void atom_list();
+#endif
+
+#ifdef __STDC__
+void near_atom(void);
+#else
+extern void near_atom();
+#endif
+
+#ifdef __STDC__
+void atom(void);
+#else
+extern void atom();
+#endif
+
+#ifdef __STDC__
+void anychar(void);
+#else
+extern void anychar();
+#endif
+
+#endif
+extern SetWordType zzerr1[];
+extern SetWordType zzerr2[];
+extern SetWordType zzerr3[];
+extern SetWordType setwd1[];
+extern SetWordType zzerr4[];
+extern SetWordType zzerr5[];
+extern SetWordType setwd2[];
+extern SetWordType zzerr6[];
+extern SetWordType setwd3[];