Reindent yacc sources.

This commit is contained in:
Serge
2022-06-18 22:00:26 -07:00
parent 82817c9308
commit 72d7321b45
11 changed files with 2732 additions and 2474 deletions

View File

@@ -25,7 +25,7 @@ clean:
install: all
install yacc $(DESTDIR)/bin/
y1.o: y1.c dextern
y2.o: y2.c dextern
y3.o: y3.c dextern
y4.o: y4.c dextern
y1.o: y1.c dextern.h
y2.o: y2.c dextern.h
y3.o: y3.c dextern.h
y4.o: y4.c dextern.h

View File

@@ -1,275 +0,0 @@
/*
* @(#)dextern 4.2 (Berkeley) 3/21/86
*/
# include <stdio.h>
# include <ctype.h>
# include "files"
/* MANIFEST CONSTANT DEFINITIONS */
/* base of nonterminal internal numbers */
# define NTBASE 010000
/* internal codes for error and accept actions */
# define ERRCODE 8190
# define ACCEPTCODE 8191
/* sizes and limits */
# ifdef HUGE
# define ACTSIZE 12000
# define MEMSIZE 24000
# define NSTATES 750
# define NTERMS 300
# define NPROD 600
# define NNONTERM 300
# define TEMPSIZE 1200
# define CNAMSZ 5000
# define LSETSIZE 600
# define WSETSIZE 350
# endif
# ifdef MEDIUM
# define ACTSIZE 4000
# define MEMSIZE 5200
# define NSTATES 750
# define NTERMS 127
# define NPROD 400
# define NNONTERM 200
# define TEMPSIZE 800
# define CNAMSZ 4000
# define LSETSIZE 450
# define WSETSIZE 250
# endif
# ifdef TINY
# define ACTSIZE 1000
# define MEMSIZE 1300
# define NSTATES 750
# define NTERMS 127
# define NPROD 200
# define NNONTERM 50
# define TEMPSIZE 200
# define CNAMSZ 1000
# define LSETSIZE 250
# define WSETSIZE 100
# endif
# define NAMESIZE 50
# define NTYPES 63
# ifdef WORD32
# define TBITSET ((32+NTERMS)/32)
/* bit packing macros (may be machine dependent) */
# define BIT(a,i) ((a)[(i)>>5] & (1<<((i)&037)))
# define SETBIT(a,i) ((a)[(i)>>5] |= (1<<((i)&037)))
/* number of words needed to hold n+1 bits */
# define NWORDS(n) (((n)+32)/32)
# else
# define TBITSET ((16+NTERMS)/16)
/* bit packing macros (may be machine dependent) */
# define BIT(a,i) ((a)[(i)>>4] & (1<<((i)&017)))
# define SETBIT(a,i) ((a)[(i)>>4] |= (1<<((i)&017)))
/* number of words needed to hold n+1 bits */
# define NWORDS(n) (((n)+16)/16)
# endif
/* relationships which must hold:
TBITSET ints must hold NTERMS+1 bits...
WSETSIZE >= NNONTERM
LSETSIZE >= NNONTERM
TEMPSIZE >= NTERMS + NNONTERMs + 1
TEMPSIZE >= NSTATES
*/
/* associativities */
# define NOASC 0 /* no assoc. */
# define LASC 1 /* left assoc. */
# define RASC 2 /* right assoc. */
# define BASC 3 /* binary assoc. */
/* flags for state generation */
# define DONE 0
# define MUSTDO 1
# define MUSTLOOKAHEAD 2
/* flags for a rule having an action, and being reduced */
# define ACTFLAG 04
# define REDFLAG 010
/* output parser flags */
# define YYFLAG1 (-1000)
/* macros for getting associativity and precedence levels */
# define ASSOC(i) ((i)&03)
# define PLEVEL(i) (((i)>>4)&077)
# define TYPE(i) ((i>>10)&077)
/* macros for setting associativity and precedence levels */
# define SETASC(i,j) i|=j
# define SETPLEV(i,j) i |= (j<<4)
# define SETTYPE(i,j) i |= (j<<10)
/* looping macros */
# define TLOOP(i) for(i=1;i<=ntokens;++i)
# define NTLOOP(i) for(i=0;i<=nnonter;++i)
# define PLOOP(s,i) for(i=s;i<nprod;++i)
# define SLOOP(i) for(i=0;i<nstate;++i)
# define WSBUMP(x) ++x
# define WSLOOP(s,j) for(j=s;j<cwp;++j)
# define ITMLOOP(i,p,q) q=pstate[i+1];for(p=pstate[i];p<q;++p)
# define SETLOOP(i) for(i=0;i<tbitset;++i)
/* I/O descriptors */
extern FILE * finput; /* input file */
extern FILE * faction; /* file for saving actions */
extern FILE *fdefine; /* file for # defines */
extern FILE * ftable; /* y.tab.c file */
extern FILE * ftemp; /* tempfile to pass 2 */
extern FILE * foutput; /* y.output file */
/* structure declarations */
struct looksets {
int lset[TBITSET];
};
struct item {
int *pitem;
struct looksets *look;
};
struct toksymb {
char *name;
int value;
};
struct ntsymb {
char *name;
int tvalue;
};
struct wset {
int *pitem;
int flag;
struct looksets ws;
};
/* token information */
extern int ntokens ; /* number of tokens */
extern struct toksymb tokset[];
extern int toklev[]; /* vector with the precedence of the terminals */
/* nonterminal information */
extern int nnonter ; /* the number of nonterminals */
extern struct ntsymb nontrst[];
/* grammar rule information */
extern int nprod ; /* number of productions */
extern int *prdptr[]; /* pointers to descriptions of productions */
extern int levprd[] ; /* contains production levels to break conflicts */
/* state information */
extern int nstate ; /* number of states */
extern struct item *pstate[]; /* pointers to the descriptions of the states */
extern int tystate[]; /* contains type information about the states */
extern int defact[]; /* the default action of the state */
extern int tstates[]; /* the states deriving each token */
extern int ntstates[]; /* the states deriving each nonterminal */
extern int mstates[]; /* the continuation of the chains begun in tstates and ntstates */
/* lookahead set information */
extern struct looksets lkst[];
extern int nolook; /* flag to turn off lookahead computations */
/* working set information */
extern struct wset wsets[];
extern struct wset *cwp;
/* storage for productions */
extern int mem0[];
extern int *mem;
/* storage for action table */
extern int amem[]; /* action table storage */
extern int *memp ; /* next free action table position */
extern int indgo[]; /* index to the stored goto table */
/* temporary vector, indexable by states, terms, or ntokens */
extern int temp1[];
extern int lineno; /* current line number */
/* statistics collection variables */
extern int zzgoent ;
extern int zzgobest ;
extern int zzacent ;
extern int zzexcp ;
extern int zzclose ;
extern int zzrrconf ;
extern int zzsrconf ;
/* define functions with strange types... */
extern char *cstash();
extern struct looksets *flset();
extern char *symnam();
extern char *writem();
/* default settings for a number of macros */
/* name of yacc tempfiles */
# ifndef TEMPNAME
# define TEMPNAME "yacc.tmp"
# endif
# ifndef ACTNAME
# define ACTNAME "yacc.acts"
# endif
/* output file name */
# ifndef OFILE
# define OFILE "y.tab.c"
# endif
/* user output file name */
# ifndef FILEU
# define FILEU "y.output"
# endif
/* output file for # defines */
# ifndef FILED
# define FILED "y.tab.h"
# endif
/* command to clobber tempfiles after use */
# ifndef ZAPFILE
# define ZAPFILE(x) unlink(x)
# endif

278
src/cmd/yacc/dextern.h Normal file
View File

@@ -0,0 +1,278 @@
/*
* @(#)dextern 4.2 (Berkeley) 3/21/86
*/
#include <ctype.h>
#include <stdio.h>
#include "files.h"
/* MANIFEST CONSTANT DEFINITIONS */
/* base of nonterminal internal numbers */
#define NTBASE 010000
/* internal codes for error and accept actions */
#define ERRCODE 8190
#define ACCEPTCODE 8191
/* sizes and limits */
#ifdef HUGE
#define ACTSIZE 12000
#define MEMSIZE 24000
#define NSTATES 750
#define NTERMS 300
#define NPROD 600
#define NNONTERM 300
#define TEMPSIZE 1200
#define CNAMSZ 5000
#define LSETSIZE 600
#define WSETSIZE 350
#endif
#ifdef MEDIUM
#define ACTSIZE 4000
#define MEMSIZE 5200
#define NSTATES 750
#define NTERMS 127
#define NPROD 400
#define NNONTERM 200
#define TEMPSIZE 800
#define CNAMSZ 4000
#define LSETSIZE 450
#define WSETSIZE 250
#endif
#ifdef TINY
#define ACTSIZE 1000
#define MEMSIZE 1300
#define NSTATES 750
#define NTERMS 127
#define NPROD 200
#define NNONTERM 50
#define TEMPSIZE 200
#define CNAMSZ 1000
#define LSETSIZE 250
#define WSETSIZE 100
#endif
#define NAMESIZE 50
#define NTYPES 63
#ifdef WORD32
#define TBITSET ((32 + NTERMS) / 32)
/* bit packing macros (may be machine dependent) */
#define BIT(a, i) ((a)[(i) >> 5] & (1 << ((i)&037)))
#define SETBIT(a, i) ((a)[(i) >> 5] |= (1 << ((i)&037)))
/* number of words needed to hold n+1 bits */
#define NWORDS(n) (((n) + 32) / 32)
#else
#define TBITSET ((16 + NTERMS) / 16)
/* bit packing macros (may be machine dependent) */
#define BIT(a, i) ((a)[(i) >> 4] & (1 << ((i)&017)))
#define SETBIT(a, i) ((a)[(i) >> 4] |= (1 << ((i)&017)))
/* number of words needed to hold n+1 bits */
#define NWORDS(n) (((n) + 16) / 16)
#endif
/* relationships which must hold:
TBITSET ints must hold NTERMS+1 bits...
WSETSIZE >= NNONTERM
LSETSIZE >= NNONTERM
TEMPSIZE >= NTERMS + NNONTERMs + 1
TEMPSIZE >= NSTATES
*/
/* associativities */
#define NOASC 0 /* no assoc. */
#define LASC 1 /* left assoc. */
#define RASC 2 /* right assoc. */
#define BASC 3 /* binary assoc. */
/* flags for state generation */
#define DONE 0
#define MUSTDO 1
#define MUSTLOOKAHEAD 2
/* flags for a rule having an action, and being reduced */
#define ACTFLAG 04
#define REDFLAG 010
/* output parser flags */
#define YYFLAG1 (-1000)
/* macros for getting associativity and precedence levels */
#define ASSOC(i) ((i)&03)
#define PLEVEL(i) (((i) >> 4) & 077)
#define TYPE(i) ((i >> 10) & 077)
/* macros for setting associativity and precedence levels */
#define SETASC(i, j) i |= j
#define SETPLEV(i, j) i |= (j << 4)
#define SETTYPE(i, j) i |= (j << 10)
/* looping macros */
#define TLOOP(i) for (i = 1; i <= ntokens; ++i)
#define NTLOOP(i) for (i = 0; i <= nnonter; ++i)
#define PLOOP(s, i) for (i = s; i < nprod; ++i)
#define SLOOP(i) for (i = 0; i < nstate; ++i)
#define WSBUMP(x) ++x
#define WSLOOP(s, j) for (j = s; j < cwp; ++j)
#define ITMLOOP(i, p, q) \
q = pstate[i + 1]; \
for (p = pstate[i]; p < q; ++p)
#define SETLOOP(i) for (i = 0; i < tbitset; ++i)
/* I/O descriptors */
extern FILE *finput; /* input file */
extern FILE *faction; /* file for saving actions */
extern FILE *fdefine; /* file for # defines */
extern FILE *ftable; /* y.tab.c file */
extern FILE *ftemp; /* tempfile to pass 2 */
extern FILE *foutput; /* y.output file */
/* structure declarations */
struct looksets {
int lset[TBITSET];
};
struct item {
int *pitem;
struct looksets *look;
};
struct toksymb {
char *name;
int value;
};
struct ntsymb {
char *name;
int tvalue;
};
struct wset {
int *pitem;
int flag;
struct looksets ws;
};
/* token information */
extern int ntokens; /* number of tokens */
extern struct toksymb tokset[];
extern int toklev[]; /* vector with the precedence of the terminals */
/* nonterminal information */
extern int nnonter; /* the number of nonterminals */
extern struct ntsymb nontrst[];
/* grammar rule information */
extern int nprod; /* number of productions */
extern int *prdptr[]; /* pointers to descriptions of productions */
extern int levprd[]; /* contains production levels to break conflicts */
/* state information */
extern int nstate; /* number of states */
extern struct item *pstate[]; /* pointers to the descriptions of the states */
extern int tystate[]; /* contains type information about the states */
extern int defact[]; /* the default action of the state */
extern int tstates[]; /* the states deriving each token */
extern int ntstates[]; /* the states deriving each nonterminal */
extern int mstates[]; /* the continuation of the chains begun in tstates and ntstates */
/* lookahead set information */
extern struct looksets lkst[];
extern int nolook; /* flag to turn off lookahead computations */
/* working set information */
extern struct wset wsets[];
extern struct wset *cwp;
/* storage for productions */
extern int mem0[];
extern int *mem;
/* storage for action table */
extern int amem[]; /* action table storage */
extern int *memp; /* next free action table position */
extern int indgo[]; /* index to the stored goto table */
/* temporary vector, indexable by states, terms, or ntokens */
extern int temp1[];
extern int lineno; /* current line number */
/* statistics collection variables */
extern int zzgoent;
extern int zzgobest;
extern int zzacent;
extern int zzexcp;
extern int zzclose;
extern int zzrrconf;
extern int zzsrconf;
/* define functions with strange types... */
extern char *cstash();
extern struct looksets *flset();
extern char *symnam();
extern char *writem();
/* default settings for a number of macros */
/* name of yacc tempfiles */
#ifndef TEMPNAME
#define TEMPNAME "yacc.tmp"
#endif
#ifndef ACTNAME
#define ACTNAME "yacc.acts"
#endif
/* output file name */
#ifndef OFILE
#define OFILE "y.tab.c"
#endif
/* user output file name */
#ifndef FILEU
#define FILEU "y.output"
#endif
/* output file for # defines */
#ifndef FILED
#define FILED "y.tab.h"
#endif
/* command to clobber tempfiles after use */
#ifndef ZAPFILE
#define ZAPFILE(x) unlink(x)
#endif

View File

@@ -1,21 +0,0 @@
/*
* @(#)files 4.1.1 (2.11BSD) 1996/10/24
*/
/* this file has the location of the parser, and the size of the progam desired */
/* It may also contain definitions to override various defaults: for example,
/* WORD32 tells yacc that there are at least 32 bits per int */
/* on some systems, notably IBM, the names for the output files and tempfiles must
/* also be changed */
/*
* #ifndef WORD32
* #define WORD32
* #endif
*/
/* location of the parser text file */
# define PARSER "/usr/share/misc/yaccpar"
/* basic size of the Yacc implementation */
/* # define HUGE */
/* #define MEDIUM */
#define TINY

21
src/cmd/yacc/files.h Normal file
View File

@@ -0,0 +1,21 @@
/*
* @(#)files 4.1.1 (2.11BSD) 1996/10/24
*/
/* this file has the location of the parser, and the size of the progam desired */
/* It may also contain definitions to override various defaults: for example,
* WORD32 tells yacc that there are at least 32 bits per int */
/* on some systems, notably IBM, the names for the output files and tempfiles must
* also be changed */
/*
* #ifndef WORD32
* #define WORD32
* #endif
*/
/* location of the parser text file */
#define PARSER "/usr/share/misc/yaccpar.c"
/* basic size of the Yacc implementation */
/* # define HUGE */
/* #define MEDIUM */
#define TINY

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,413 +1,454 @@
#if !defined(lint) && defined(DOSCCS)
static char sccsid[] = "@(#)y3.c 4.1.1 (2.11BSD) 1995/05/11";
#endif
#include "dextern.h"
# include "dextern"
/* important local variables */
int lastred; /* the number of the last reduction of a state */
int defact[NSTATES]; /* the default actions of states */
/* important local variables */
int lastred; /* the number of the last reduction of a state */
int defact[NSTATES]; /* the default actions of states */
output()
{ /* print the output for the states */
output(){ /* print the output for the states */
int i, k;
register int c;
register struct wset *u, *v;
int i, k;
register int c;
register struct wset *u, *v;
fprintf(ftable, "short yyexca[] ={\n");
fprintf( ftable, "short yyexca[] ={\n" );
SLOOP(i)
{ /* output the stuff for state i */
nolook = !(tystate[i] == MUSTLOOKAHEAD);
closure(i);
/* output actions */
nolook = 1;
aryfil(temp1, ntokens + nnonter + 1, 0);
WSLOOP(wsets, u)
{
c = *(u->pitem);
if (c > 1 && c < NTBASE && temp1[c] == 0) {
WSLOOP(u, v)
{
if (c == *(v->pitem))
putitem(v->pitem + 1, (struct looksets *)0);
}
temp1[c] = state(c);
} else if (c > NTBASE && temp1[(c -= NTBASE) + ntokens] == 0) {
temp1[c + ntokens] = amem[indgo[i] + c];
}
}
SLOOP(i) { /* output the stuff for state i */
nolook = !(tystate[i]==MUSTLOOKAHEAD);
closure(i);
/* output actions */
nolook = 1;
aryfil( temp1, ntokens+nnonter+1, 0 );
WSLOOP(wsets,u){
c = *( u->pitem );
if( c>1 && c<NTBASE && temp1[c]==0 ) {
WSLOOP(u,v){
if( c == *(v->pitem) ) putitem( v->pitem+1, (struct looksets *)0 );
}
temp1[c] = state(c);
}
else if( c > NTBASE && temp1[ (c -= NTBASE) + ntokens ] == 0 ){
temp1[ c+ntokens ] = amem[indgo[i]+c];
}
}
if (i == 1)
temp1[1] = ACCEPTCODE;
if( i == 1 ) temp1[1] = ACCEPTCODE;
/* now, we have the shifts; look at the reductions */
/* now, we have the shifts; look at the reductions */
lastred = 0;
WSLOOP(wsets, u)
{
c = *(u->pitem);
if (c <= 0) { /* reduction */
lastred = -c;
TLOOP(k)
{
if (BIT(u->ws.lset, k)) {
if (temp1[k] == 0)
temp1[k] = c;
else if (temp1[k] < 0) { /* reduce/reduce conflict */
if (foutput != NULL)
fprintf(foutput,
"\n%d: reduce/reduce conflict (red'ns %d and %d ) on %s", i,
-temp1[k], lastred, symnam(k));
if (-temp1[k] > lastred)
temp1[k] = -lastred;
++zzrrconf;
} else { /* potential shift/reduce conflict */
precftn(lastred, k, i);
}
}
}
}
}
wract(i);
}
lastred = 0;
WSLOOP(wsets,u){
c = *( u->pitem );
if( c<=0 ){ /* reduction */
lastred = -c;
TLOOP(k){
if( BIT(u->ws.lset,k) ){
if( temp1[k] == 0 ) temp1[k] = c;
else if( temp1[k]<0 ){ /* reduce/reduce conflict */
if( foutput!=NULL )
fprintf( foutput,
"\n%d: reduce/reduce conflict (red'ns %d and %d ) on %s",
i, -temp1[k], lastred, symnam(k) );
if( -temp1[k] > lastred ) temp1[k] = -lastred;
++zzrrconf;
}
else { /* potential shift/reduce conflict */
precftn( lastred, k, i );
}
}
}
}
}
wract(i);
}
fprintf(ftable, "\t};\n");
fprintf( ftable, "\t};\n" );
wdef( "YYNPROD", nprod );
}
wdef("YYNPROD", nprod);
}
int pkdebug = 0;
apack(p, n ) int *p;{ /* pack state i from temp1 into amem */
int off;
register *pp, *qq, *rr;
int *q, *r;
apack(p, n) int *p;
{ /* pack state i from temp1 into amem */
int off;
register *pp, *qq, *rr;
int *q, *r;
/* we don't need to worry about checking because we
we will only look entries known to be there... */
/* we don't need to worry about checking because we
we will only look entries known to be there... */
/* eliminate leading and trailing 0's */
/* eliminate leading and trailing 0's */
q = p+n;
for( pp=p,off=0 ; *pp==0 && pp<=q; ++pp,--off ) /* VOID */ ;
if( pp > q ) return(0); /* no actions */
p = pp;
q = p + n;
for (pp = p, off = 0; *pp == 0 && pp <= q; ++pp, --off) /* VOID */
;
if (pp > q)
return (0); /* no actions */
p = pp;
/* now, find a place for the elements from p to q, inclusive */
/* now, find a place for the elements from p to q, inclusive */
r = &amem[ACTSIZE-1];
for( rr=amem; rr<=r; ++rr,++off ){ /* try rr */
for( qq=rr,pp=p ; pp<=q ; ++pp,++qq){
if( *pp != 0 ){
if( *pp != *qq && *qq != 0 ) goto nextk;
}
}
r = &amem[ACTSIZE - 1];
for (rr = amem; rr <= r; ++rr, ++off) { /* try rr */
for (qq = rr, pp = p; pp <= q; ++pp, ++qq) {
if (*pp != 0) {
if (*pp != *qq && *qq != 0)
goto nextk;
}
}
/* we have found an acceptable k */
/* we have found an acceptable k */
if( pkdebug && foutput!=NULL ) fprintf( foutput, "off = %d, k = %d\n", off, rr-amem );
if (pkdebug && foutput != NULL)
fprintf(foutput, "off = %d, k = %d\n", off, rr - amem);
for( qq=rr,pp=p; pp<=q; ++pp,++qq ){
if( *pp ){
if( qq > r ) error( "action table overflow" );
if( qq>memp ) memp = qq;
*qq = *pp;
}
}
if( pkdebug && foutput!=NULL ){
for( pp=amem; pp<= memp; pp+=10 ){
fprintf( foutput, "\t");
for( qq=pp; qq<=pp+9; ++qq ) fprintf( foutput, "%d ", *qq );
fprintf( foutput, "\n");
}
}
return( off );
for (qq = rr, pp = p; pp <= q; ++pp, ++qq) {
if (*pp) {
if (qq > r)
error("action table overflow");
if (qq > memp)
memp = qq;
*qq = *pp;
}
}
if (pkdebug && foutput != NULL) {
for (pp = amem; pp <= memp; pp += 10) {
fprintf(foutput, "\t");
for (qq = pp; qq <= pp + 9; ++qq)
fprintf(foutput, "%d ", *qq);
fprintf(foutput, "\n");
}
}
return (off);
nextk: ;
}
error("no space in action table" );
/* NOTREACHED */
}
nextk:;
}
error("no space in action table");
/* NOTREACHED */
}
go2out(){ /* output the gotos for the nontermninals */
register int i, j, k;
int best, count, cbest, times;
go2out()
{ /* output the gotos for the nontermninals */
register int i, j, k;
int best, count, cbest, times;
fprintf( ftemp, "$\n" ); /* mark begining of gotos */
fprintf(ftemp, "$\n"); /* mark begining of gotos */
for( i=1; i<=nnonter; ++i ) {
go2gen(i);
for (i = 1; i <= nnonter; ++i) {
go2gen(i);
/* find the best one to make default */
/* find the best one to make default */
best = -1;
times = 0;
best = -1;
times = 0;
for( j=0; j<=nstate; ++j ){ /* is j the most frequent */
if( tystate[j] == 0 ) continue;
if( tystate[j] == best ) continue;
for (j = 0; j <= nstate; ++j) { /* is j the most frequent */
if (tystate[j] == 0)
continue;
if (tystate[j] == best)
continue;
/* is tystate[j] the most frequent */
/* is tystate[j] the most frequent */
count = 0;
cbest = tystate[j];
count = 0;
cbest = tystate[j];
for( k=j; k<=nstate; ++k ) if( tystate[k]==cbest ) ++count;
for (k = j; k <= nstate; ++k)
if (tystate[k] == cbest)
++count;
if( count > times ){
best = cbest;
times = count;
}
}
if (count > times) {
best = cbest;
times = count;
}
}
/* best is now the default entry */
/* best is now the default entry */
zzgobest += (times-1);
for( j=0; j<=nstate; ++j ){
if( tystate[j] != 0 && tystate[j]!=best ){
fprintf( ftemp, "%d,%d,", j, tystate[j] );
zzgoent += 1;
}
}
zzgobest += (times - 1);
for (j = 0; j <= nstate; ++j) {
if (tystate[j] != 0 && tystate[j] != best) {
fprintf(ftemp, "%d,%d,", j, tystate[j]);
zzgoent += 1;
}
}
/* now, the default */
/* now, the default */
zzgoent += 1;
fprintf( ftemp, "%d\n", best );
}
}
zzgoent += 1;
fprintf(ftemp, "%d\n", best);
}
}
int g2debug = 0;
go2gen(c){ /* output the gotos for nonterminal c */
go2gen(c)
{ /* output the gotos for nonterminal c */
register int i, cc;
int work;
struct item *p, *q;
register int i, cc;
int work;
struct item *p, *q;
/* first, find nonterminals with gotos on c */
/* first, find nonterminals with gotos on c */
aryfil(temp1, nnonter + 1, 0);
temp1[c] = 1;
aryfil( temp1, nnonter+1, 0 );
temp1[c] = 1;
work = 1;
while (work) {
work = 0;
PLOOP(0, i)
{
if ((cc = prdptr[i][1] - NTBASE) >= 0) { /* cc is a nonterminal */
if (temp1[cc] != 0) { /* cc has a goto on c */
cc = *prdptr[i] - NTBASE; /* thus, the left side of production i does too */
if (temp1[cc] == 0) {
work = 1;
temp1[cc] = 1;
}
}
}
}
}
work = 1;
while( work ){
work = 0;
PLOOP(0,i){
if( (cc=prdptr[i][1]-NTBASE) >= 0 ){ /* cc is a nonterminal */
if( temp1[cc] != 0 ){ /* cc has a goto on c */
cc = *prdptr[i]-NTBASE; /* thus, the left side of production i does too */
if( temp1[cc] == 0 ){
work = 1;
temp1[cc] = 1;
}
}
}
}
}
/* now, we have temp1[c] = 1 if a goto on c in closure of cc */
/* now, we have temp1[c] = 1 if a goto on c in closure of cc */
if (g2debug && foutput != NULL) {
fprintf(foutput, "%s: gotos on ", nontrst[c].name);
NTLOOP(i) if (temp1[i]) fprintf(foutput, "%s ", nontrst[i].name);
fprintf(foutput, "\n");
}
if( g2debug && foutput!=NULL ){
fprintf( foutput, "%s: gotos on ", nontrst[c].name );
NTLOOP(i) if( temp1[i] ) fprintf( foutput, "%s ", nontrst[i].name);
fprintf( foutput, "\n");
}
/* now, go through and put gotos into tystate */
/* now, go through and put gotos into tystate */
aryfil(tystate, nstate, 0);
SLOOP(i)
{
ITMLOOP(i, p, q)
{
if ((cc = *p->pitem) >= NTBASE) {
if (temp1[cc -= NTBASE]) { /* goto on c is possible */
tystate[i] = amem[indgo[i] + c];
break;
}
}
}
}
}
aryfil( tystate, nstate, 0 );
SLOOP(i){
ITMLOOP(i,p,q){
if( (cc= *p->pitem) >= NTBASE ){
if( temp1[cc -= NTBASE] ){ /* goto on c is possible */
tystate[i] = amem[indgo[i]+c];
break;
}
}
}
}
}
precftn(r, t, s) register int t;
{ /* decide a shift/reduce conflict by precedence.
/* r is a rule number, t a token number */
/* the conflict is in state s */
/* temp1[t] is changed to reflect the action */
precftn(r,t,s) register int t;
{ /* decide a shift/reduce conflict by precedence.
/* r is a rule number, t a token number */
/* the conflict is in state s */
/* temp1[t] is changed to reflect the action */
int lp, lt, action;
int lp,lt, action;
lp = levprd[r];
lt = toklev[t];
if (PLEVEL(lt) == 0 || PLEVEL(lp) == 0) {
/* conflict */
if (foutput != NULL)
fprintf(foutput, "\n%d: shift/reduce conflict (shift %d, red'n %d) on %s", s, temp1[t],
r, symnam(t));
++zzsrconf;
return;
}
if (PLEVEL(lt) == PLEVEL(lp))
action = ASSOC(lt);
else if (PLEVEL(lt) > PLEVEL(lp))
action = RASC; /* shift */
else
action = LASC; /* reduce */
lp = levprd[r];
lt = toklev[t];
if( PLEVEL(lt) == 0 || PLEVEL(lp) == 0 ) {
/* conflict */
if( foutput != NULL ) fprintf( foutput, "\n%d: shift/reduce conflict (shift %d, red'n %d) on %s",
s, temp1[t], r, symnam(t) );
++zzsrconf;
return;
}
if( PLEVEL(lt) == PLEVEL(lp) ) action = ASSOC(lt);
else if( PLEVEL(lt) > PLEVEL(lp) ) action = RASC; /* shift */
else action = LASC; /* reduce */
switch (action) {
case BASC: /* error action */
temp1[t] = ERRCODE;
return;
switch( action ){
case LASC: /* reduce */
temp1[t] = -r;
return;
}
}
case BASC: /* error action */
temp1[t] = ERRCODE;
return;
wract(i) register int i;
{ /* output state i */
/* temp1 has the actions, lastred the default */
int p, p0, p1;
int ntimes, tred, count;
register int j;
int flag;
case LASC: /* reduce */
temp1[t] = -r;
return;
/* find the best choice for lastred */
}
}
lastred = 0;
ntimes = 0;
TLOOP(j)
{
if (temp1[j] >= 0)
continue;
if (temp1[j] + lastred == 0)
continue;
/* count the number of appearances of temp1[j] */
count = 0;
tred = -temp1[j];
levprd[tred] |= REDFLAG;
TLOOP(p)
{
if (temp1[p] + tred == 0)
++count;
}
if (count > ntimes) {
lastred = tred;
ntimes = count;
}
}
wract(i) register int i; { /* output state i */
/* temp1 has the actions, lastred the default */
int p, p0, p1;
int ntimes, tred, count;
register int j;
int flag;
/* for error recovery, arrange that, if there is a shift on the
/* error recovery token, `error', that the default be the error action */
if (temp1[1] > 0)
lastred = 0;
/* find the best choice for lastred */
/* clear out entries in temp1 which equal lastred */
TLOOP(p) if (temp1[p] + lastred == 0) temp1[p] = 0;
lastred = 0;
ntimes = 0;
TLOOP(j){
if( temp1[j] >= 0 ) continue;
if( temp1[j]+lastred == 0 ) continue;
/* count the number of appearances of temp1[j] */
count = 0;
tred = -temp1[j];
levprd[tred] |= REDFLAG;
TLOOP(p){
if( temp1[p]+tred == 0 ) ++count;
}
if( count >ntimes ){
lastred = tred;
ntimes = count;
}
}
wrstate(i);
defact[i] = lastred;
/* for error recovery, arrange that, if there is a shift on the
/* error recovery token, `error', that the default be the error action */
if( temp1[1] > 0 ) lastred = 0;
flag = 0;
TLOOP(p0)
{
if ((p1 = temp1[p0]) != 0) {
if (p1 < 0) {
p1 = -p1;
goto exc;
} else if (p1 == ACCEPTCODE) {
p1 = -1;
goto exc;
} else if (p1 == ERRCODE) {
p1 = 0;
goto exc;
exc:
if (flag++ == 0)
fprintf(ftable, "-1, %d,\n", i);
fprintf(ftable, "\t%d, %d,\n", tokset[p0].value, p1);
++zzexcp;
} else {
fprintf(ftemp, "%d,%d,", tokset[p0].value, p1);
++zzacent;
}
}
}
if (flag) {
defact[i] = -2;
fprintf(ftable, "\t-2, %d,\n", lastred);
}
fprintf(ftemp, "\n");
return;
}
/* clear out entries in temp1 which equal lastred */
TLOOP(p) if( temp1[p]+lastred == 0 )temp1[p]=0;
wrstate(i)
{ /* writes state i */
register j0, j1;
register struct item *pp, *qq;
register struct wset *u;
wrstate(i);
defact[i] = lastred;
if (foutput == NULL)
return;
fprintf(foutput, "\nstate %d\n", i);
ITMLOOP(i, pp, qq) fprintf(foutput, "\t%s\n", writem(pp->pitem));
if (tystate[i] == MUSTLOOKAHEAD) {
/* print out empty productions in closure */
WSLOOP(wsets + (pstate[i + 1] - pstate[i]), u)
{
if (*(u->pitem) < 0)
fprintf(foutput, "\t%s\n", writem(u->pitem));
}
}
flag = 0;
TLOOP(p0){
if( (p1=temp1[p0])!=0 ) {
if( p1 < 0 ){
p1 = -p1;
goto exc;
}
else if( p1 == ACCEPTCODE ) {
p1 = -1;
goto exc;
}
else if( p1 == ERRCODE ) {
p1 = 0;
goto exc;
exc:
if( flag++ == 0 ) fprintf( ftable, "-1, %d,\n", i );
fprintf( ftable, "\t%d, %d,\n", tokset[p0].value, p1 );
++zzexcp;
}
else {
fprintf( ftemp, "%d,%d,", tokset[p0].value, p1 );
++zzacent;
}
}
}
if( flag ) {
defact[i] = -2;
fprintf( ftable, "\t-2, %d,\n", lastred );
}
fprintf( ftemp, "\n" );
return;
}
/* check for state equal to another */
wrstate(i){ /* writes state i */
register j0,j1;
register struct item *pp, *qq;
register struct wset *u;
TLOOP(j0) if ((j1 = temp1[j0]) != 0)
{
fprintf(foutput, "\n\t%s ", symnam(j0));
if (j1 > 0) { /* shift, error, or accept */
if (j1 == ACCEPTCODE)
fprintf(foutput, "accept");
else if (j1 == ERRCODE)
fprintf(foutput, "error");
else
fprintf(foutput, "shift %d", j1);
} else
fprintf(foutput, "reduce %d", -j1);
}
if( foutput == NULL ) return;
fprintf( foutput, "\nstate %d\n",i);
ITMLOOP(i,pp,qq) fprintf( foutput, "\t%s\n", writem(pp->pitem));
if( tystate[i] == MUSTLOOKAHEAD ){
/* print out empty productions in closure */
WSLOOP( wsets+(pstate[i+1]-pstate[i]), u ){
if( *(u->pitem) < 0 ) fprintf( foutput, "\t%s\n", writem(u->pitem) );
}
}
/* output the final production */
/* check for state equal to another */
if (lastred)
fprintf(foutput, "\n\t. reduce %d\n\n", lastred);
else
fprintf(foutput, "\n\t. error\n\n");
TLOOP(j0) if( (j1=temp1[j0]) != 0 ){
fprintf( foutput, "\n\t%s ", symnam(j0) );
if( j1>0 ){ /* shift, error, or accept */
if( j1 == ACCEPTCODE ) fprintf( foutput, "accept" );
else if( j1 == ERRCODE ) fprintf( foutput, "error" );
else fprintf( foutput, "shift %d", j1 );
}
else fprintf( foutput, "reduce %d",-j1 );
}
/* now, output nonterminal actions */
/* output the final production */
j1 = ntokens;
for (j0 = 1; j0 <= nnonter; ++j0) {
if (temp1[++j1])
fprintf(foutput, "\t%s goto %d\n", symnam(j0 + NTBASE), temp1[j1]);
}
}
if( lastred ) fprintf( foutput, "\n\t. reduce %d\n\n", lastred );
else fprintf( foutput, "\n\t. error\n\n" );
wdef(s, n) char *s;
{ /* output a definition of s to the value n */
fprintf(ftable, "# define %s %d\n", s, n);
}
/* now, output nonterminal actions */
warray(s, v, n) char *s;
int *v, n;
{
register i;
j1 = ntokens;
for( j0 = 1; j0 <= nnonter; ++j0 ){
if( temp1[++j1] ) fprintf( foutput, "\t%s goto %d\n", symnam( j0+NTBASE), temp1[j1] );
}
fprintf(ftable, "short %s[]={\n", s);
for (i = 0; i < n;) {
if (i % 10 == 0)
fprintf(ftable, "\n");
fprintf(ftable, "%4d", v[i]);
if (++i == n)
fprintf(ftable, " };\n");
else
fprintf(ftable, ",");
}
}
}
hideprod()
{
/* in order to free up the mem and amem arrays for the optimizer,
/* and still be able to output yyr1, etc., after the sizes of
/* the action array is known, we hide the nonterminals
/* derived by productions in levprd.
*/
wdef( s, n ) char *s; { /* output a definition of s to the value n */
fprintf( ftable, "# define %s %d\n", s, n );
}
register i, j;
warray( s, v, n ) char *s; int *v, n; {
register i;
fprintf( ftable, "short %s[]={\n", s );
for( i=0; i<n; ){
if( i%10 == 0 ) fprintf( ftable, "\n" );
fprintf( ftable, "%4d", v[i] );
if( ++i == n ) fprintf( ftable, " };\n" );
else fprintf( ftable, "," );
}
}
hideprod(){
/* in order to free up the mem and amem arrays for the optimizer,
/* and still be able to output yyr1, etc., after the sizes of
/* the action array is known, we hide the nonterminals
/* derived by productions in levprd.
*/
register i, j;
j = 0;
levprd[0] = 0;
PLOOP(1,i){
if( !(levprd[i] & REDFLAG) ){
++j;
if( foutput != NULL ){
fprintf( foutput, "Rule not reduced: %s\n", writem( prdptr[i] ) );
}
}
levprd[i] = *prdptr[i] - NTBASE;
}
if( j ) fprintf( stdout, "%d rules never reduced\n", j );
}
j = 0;
levprd[0] = 0;
PLOOP(1, i)
{
if (!(levprd[i] & REDFLAG)) {
++j;
if (foutput != NULL) {
fprintf(foutput, "Rule not reduced: %s\n", writem(prdptr[i]));
}
}
levprd[i] = *prdptr[i] - NTBASE;
}
if (j)
fprintf(stdout, "%d rules never reduced\n", j);
}

View File

@@ -1,329 +1,354 @@
#if !defined(lint) && defined(DOSCCS)
static char sccsid[] = "@(#)y4.c 4.1.1 (2.11BSD) 1995/05/11";
#endif
#include "dextern.h"
# include "dextern"
#define a amem
#define mem mem0
#define pa indgo
#define yypact temp1
#define greed tystate
# define a amem
# define mem mem0
# define pa indgo
# define yypact temp1
# define greed tystate
int * ggreed = lkst[0].lset;
int * pgo = wsets[0].ws.lset;
int *ggreed = lkst[0].lset;
int *pgo = wsets[0].ws.lset;
int *yypgo = &nontrst[0].tvalue;
int maxspr = 0; /* maximum spread of any entry */
int maxoff = 0; /* maximum offset into a array */
int maxspr = 0; /* maximum spread of any entry */
int maxoff = 0; /* maximum offset into a array */
int *pmem = mem;
int *maxa;
# define NOMORE -1000
#define NOMORE -1000
int nxdb = 0;
int adb = 0;
callopt(){
register i, *p, j, k, *q;
/* read the arrays from tempfile and set parameters */
if( (finput=fopen(TEMPNAME,"r")) == NULL ) error( "optimizer cannot open tempfile" );
pgo[0] = 0;
yypact[0] = 0;
nstate = 0;
nnonter = 0;
for(;;){
switch( gtnm() ){
case '\n':
yypact[++nstate] = (--pmem) - mem;
case ',':
continue;
case '$':
break;
default:
error( "bad tempfile" );
}
break;
}
yypact[nstate] = yypgo[0] = (--pmem) - mem;
for(;;){
switch( gtnm() ){
case '\n':
yypgo[++nnonter]= pmem-mem;
case ',':
continue;
case EOF:
break;
default:
error( "bad tempfile" );
}
break;
}
yypgo[nnonter--] = (--pmem) - mem;
for( i=0; i<nstate; ++i ){
k = 32000;
j = 0;
q = mem + yypact[i+1];
for( p = mem + yypact[i]; p<q ; p += 2 ){
if( *p > j ) j = *p;
if( *p < k ) k = *p;
}
if( k <= j ){ /* nontrivial situation */
/* temporarily, kill this for compatibility
j -= k; /* j is now the range */
if( k > maxoff ) maxoff = k;
}
greed[i] = (yypact[i+1]-yypact[i]) + 2*j;
if( j > maxspr ) maxspr = j;
}
/* initialize ggreed table */
for( i=1; i<=nnonter; ++i ){
ggreed[i] = 1;
j = 0;
/* minimum entry index is always 0 */
q = mem + yypgo[i+1] -1;
for( p = mem+yypgo[i]; p<q ; p += 2 ) {
ggreed[i] += 2;
if( *p > j ) j = *p;
}
ggreed[i] = ggreed[i] + 2*j;
if( j > maxoff ) maxoff = j;
}
/* now, prepare to put the shift actions into the a array */
for( i=0; i<ACTSIZE; ++i ) a[i] = 0;
maxa = a;
for( i=0; i<nstate; ++i ) {
if( greed[i]==0 && adb>1 ) fprintf( ftable, "State %d: null\n", i );
pa[i] = YYFLAG1;
}
while( (i = nxti()) != NOMORE ) {
if( i >= 0 ) stin(i);
else gin(-i);
}
if( adb>2 ){ /* print a array */
for( p=a; p <= maxa; p += 10){
fprintf( ftable, "%4d ", p-a );
for( i=0; i<10; ++i ) fprintf( ftable, "%4d ", p[i] );
fprintf( ftable, "\n" );
}
}
/* write out the output appropriate to the language */
aoutput();
osummary();
ZAPFILE(TEMPNAME);
}
gin(i){
register *p, *r, *s, *q1, *q2;
/* enter gotos on nonterminal i into array a */
ggreed[i] = 0;
q2 = mem+ yypgo[i+1] - 1;
q1 = mem + yypgo[i];
/* now, find a place for it */
for( p=a; p < &a[ACTSIZE]; ++p ){
if( *p ) continue;
for( r=q1; r<q2; r+=2 ){
s = p + *r +1;
if( *s ) goto nextgp;
if( s > maxa ){
if( (maxa=s) > &a[ACTSIZE] ) error( "a array overflow" );
}
}
/* we have found a spot */
*p = *q2;
if( p > maxa ){
if( (maxa=p) > &a[ACTSIZE] ) error( "a array overflow" );
}
for( r=q1; r<q2; r+=2 ){
s = p + *r + 1;
*s = r[1];
}
pgo[i] = p-a;
if( adb>1 ) fprintf( ftable, "Nonterminal %d, entry at %d\n" , i, pgo[i] );
goto nextgi;
nextgp: ;
}
error( "cannot place goto %d\n", i );
nextgi: ;
}
stin(i){
register *r, *s, n, flag, j, *q1, *q2;
greed[i] = 0;
/* enter state i into the a array */
q2 = mem+yypact[i+1];
q1 = mem+yypact[i];
/* find an acceptable place */
for( n= -maxoff; n<ACTSIZE; ++n ){
flag = 0;
for( r = q1; r < q2; r += 2 ){
if( (s = *r + n + a ) < a ) goto nextn;
if( *s == 0 ) ++flag;
else if( *s != r[1] ) goto nextn;
}
/* check that the position equals another only if the states are identical */
for( j=0; j<nstate; ++j ){
if( pa[j] == n ) {
if( flag ) goto nextn; /* we have some disagreement */
if( yypact[j+1] + yypact[i] == yypact[j] + yypact[i+1] ){
/* states are equal */
pa[i] = n;
if( adb>1 ) fprintf( ftable, "State %d: entry at %d equals state %d\n",
i, n, j );
return;
}
goto nextn; /* we have some disagreement */
}
}
for( r = q1; r < q2; r += 2 ){
if( (s = *r + n + a ) >= &a[ACTSIZE] ) error( "out of space in optimizer a array" );
if( s > maxa ) maxa = s;
if( *s != 0 && *s != r[1] ) error( "clobber of a array, pos'n %d, by %d", s-a, r[1] );
*s = r[1];
}
pa[i] = n;
if( adb>1 ) fprintf( ftable, "State %d: entry at %d\n", i, pa[i] );
return;
nextn: ;
}
error( "Error; failure to place state %d\n", i );
}
nxti(){ /* finds the next i */
register i, max, maxi;
max = 0;
for( i=1; i<= nnonter; ++i ) if( ggreed[i] >= max ){
max = ggreed[i];
maxi = -i;
}
for( i=0; i<nstate; ++i ) if( greed[i] >= max ){
max = greed[i];
maxi = i;
}
if( nxdb ) fprintf( ftable, "nxti = %d, max = %d\n", maxi, max );
if( max==0 ) return( NOMORE );
else return( maxi );
}
osummary(){
/* write summary */
register i, *p;
if( foutput == NULL ) return;
i=0;
for( p=maxa; p>=a; --p ) {
if( *p == 0 ) ++i;
}
fprintf( foutput, "Optimizer space used: input %d/%d, output %d/%d\n",
pmem-mem+1, MEMSIZE, maxa-a+1, ACTSIZE );
fprintf( foutput, "%d table entries, %d zero\n", (maxa-a)+1, i );
fprintf( foutput, "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff );
}
aoutput(){ /* this version is for C */
/* write out the optimized parser */
fprintf( ftable, "# define YYLAST %d\n", maxa-a+1 );
arout( "yyact", a, (maxa-a)+1 );
arout( "yypact", pa, nstate );
arout( "yypgo", pgo, nnonter+1 );
}
arout( s, v, n ) char *s; int *v, n; {
register i;
fprintf( ftable, "short %s[]={\n", s );
for( i=0; i<n; ){
if( i%10 == 0 ) fprintf( ftable, "\n" );
fprintf( ftable, "%4d", v[i] );
if( ++i == n ) fprintf( ftable, " };\n" );
else fprintf( ftable, "," );
}
}
gtnm(){
register s, val, c;
/* read and convert an integer from the standard input */
/* return the terminating character */
/* blanks, tabs, and newlines are ignored */
s = 1;
val = 0;
while( (c=getc(finput)) != EOF ){
if( isdigit(c) ){
val = val * 10 + c - '0';
}
else if ( c == '-' ) s = -1;
else break;
}
*pmem++ = s*val;
if( pmem > &mem[MEMSIZE] ) error( "out of space" );
return( c );
}
callopt()
{
register i, *p, j, k, *q;
/* read the arrays from tempfile and set parameters */
if ((finput = fopen(TEMPNAME, "r")) == NULL)
error("optimizer cannot open tempfile");
pgo[0] = 0;
yypact[0] = 0;
nstate = 0;
nnonter = 0;
for (;;) {
switch (gtnm()) {
case '\n':
yypact[++nstate] = (--pmem) - mem;
case ',':
continue;
case '$':
break;
default:
error("bad tempfile");
}
break;
}
yypact[nstate] = yypgo[0] = (--pmem) - mem;
for (;;) {
switch (gtnm()) {
case '\n':
yypgo[++nnonter] = pmem - mem;
case ',':
continue;
case EOF:
break;
default:
error("bad tempfile");
}
break;
}
yypgo[nnonter--] = (--pmem) - mem;
for (i = 0; i < nstate; ++i) {
k = 32000;
j = 0;
q = mem + yypact[i + 1];
for (p = mem + yypact[i]; p < q; p += 2) {
if (*p > j)
j = *p;
if (*p < k)
k = *p;
}
if (k <= j) { /* nontrivial situation */
/* temporarily, kill this for compatibility
j -= k; /* j is now the range */
if (k > maxoff)
maxoff = k;
}
greed[i] = (yypact[i + 1] - yypact[i]) + 2 * j;
if (j > maxspr)
maxspr = j;
}
/* initialize ggreed table */
for (i = 1; i <= nnonter; ++i) {
ggreed[i] = 1;
j = 0;
/* minimum entry index is always 0 */
q = mem + yypgo[i + 1] - 1;
for (p = mem + yypgo[i]; p < q; p += 2) {
ggreed[i] += 2;
if (*p > j)
j = *p;
}
ggreed[i] = ggreed[i] + 2 * j;
if (j > maxoff)
maxoff = j;
}
/* now, prepare to put the shift actions into the a array */
for (i = 0; i < ACTSIZE; ++i)
a[i] = 0;
maxa = a;
for (i = 0; i < nstate; ++i) {
if (greed[i] == 0 && adb > 1)
fprintf(ftable, "State %d: null\n", i);
pa[i] = YYFLAG1;
}
while ((i = nxti()) != NOMORE) {
if (i >= 0)
stin(i);
else
gin(-i);
}
if (adb > 2) { /* print a array */
for (p = a; p <= maxa; p += 10) {
fprintf(ftable, "%4d ", p - a);
for (i = 0; i < 10; ++i)
fprintf(ftable, "%4d ", p[i]);
fprintf(ftable, "\n");
}
}
/* write out the output appropriate to the language */
aoutput();
osummary();
ZAPFILE(TEMPNAME);
}
gin(i)
{
register *p, *r, *s, *q1, *q2;
/* enter gotos on nonterminal i into array a */
ggreed[i] = 0;
q2 = mem + yypgo[i + 1] - 1;
q1 = mem + yypgo[i];
/* now, find a place for it */
for (p = a; p < &a[ACTSIZE]; ++p) {
if (*p)
continue;
for (r = q1; r < q2; r += 2) {
s = p + *r + 1;
if (*s)
goto nextgp;
if (s > maxa) {
if ((maxa = s) > &a[ACTSIZE])
error("a array overflow");
}
}
/* we have found a spot */
*p = *q2;
if (p > maxa) {
if ((maxa = p) > &a[ACTSIZE])
error("a array overflow");
}
for (r = q1; r < q2; r += 2) {
s = p + *r + 1;
*s = r[1];
}
pgo[i] = p - a;
if (adb > 1)
fprintf(ftable, "Nonterminal %d, entry at %d\n", i, pgo[i]);
goto nextgi;
nextgp:;
}
error("cannot place goto %d\n", i);
nextgi:;
}
stin(i)
{
register *r, *s, n, flag, j, *q1, *q2;
greed[i] = 0;
/* enter state i into the a array */
q2 = mem + yypact[i + 1];
q1 = mem + yypact[i];
/* find an acceptable place */
for (n = -maxoff; n < ACTSIZE; ++n) {
flag = 0;
for (r = q1; r < q2; r += 2) {
if ((s = *r + n + a) < a)
goto nextn;
if (*s == 0)
++flag;
else if (*s != r[1])
goto nextn;
}
/* check that the position equals another only if the states are identical */
for (j = 0; j < nstate; ++j) {
if (pa[j] == n) {
if (flag)
goto nextn; /* we have some disagreement */
if (yypact[j + 1] + yypact[i] == yypact[j] + yypact[i + 1]) {
/* states are equal */
pa[i] = n;
if (adb > 1)
fprintf(ftable, "State %d: entry at %d equals state %d\n", i, n, j);
return;
}
goto nextn; /* we have some disagreement */
}
}
for (r = q1; r < q2; r += 2) {
if ((s = *r + n + a) >= &a[ACTSIZE])
error("out of space in optimizer a array");
if (s > maxa)
maxa = s;
if (*s != 0 && *s != r[1])
error("clobber of a array, pos'n %d, by %d", s - a, r[1]);
*s = r[1];
}
pa[i] = n;
if (adb > 1)
fprintf(ftable, "State %d: entry at %d\n", i, pa[i]);
return;
nextn:;
}
error("Error; failure to place state %d\n", i);
}
nxti()
{ /* finds the next i */
register i, max, maxi;
max = 0;
for (i = 1; i <= nnonter; ++i)
if (ggreed[i] >= max) {
max = ggreed[i];
maxi = -i;
}
for (i = 0; i < nstate; ++i)
if (greed[i] >= max) {
max = greed[i];
maxi = i;
}
if (nxdb)
fprintf(ftable, "nxti = %d, max = %d\n", maxi, max);
if (max == 0)
return (NOMORE);
else
return (maxi);
}
osummary()
{
/* write summary */
register i, *p;
if (foutput == NULL)
return;
i = 0;
for (p = maxa; p >= a; --p) {
if (*p == 0)
++i;
}
fprintf(foutput, "Optimizer space used: input %d/%d, output %d/%d\n", pmem - mem + 1, MEMSIZE,
maxa - a + 1, ACTSIZE);
fprintf(foutput, "%d table entries, %d zero\n", (maxa - a) + 1, i);
fprintf(foutput, "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
}
aoutput()
{ /* this version is for C */
/* write out the optimized parser */
fprintf(ftable, "# define YYLAST %d\n", maxa - a + 1);
arout("yyact", a, (maxa - a) + 1);
arout("yypact", pa, nstate);
arout("yypgo", pgo, nnonter + 1);
}
arout(s, v, n) char *s;
int *v, n;
{
register i;
fprintf(ftable, "short %s[]={\n", s);
for (i = 0; i < n;) {
if (i % 10 == 0)
fprintf(ftable, "\n");
fprintf(ftable, "%4d", v[i]);
if (++i == n)
fprintf(ftable, " };\n");
else
fprintf(ftable, ",");
}
}
gtnm()
{
register s, val, c;
/* read and convert an integer from the standard input */
/* return the terminating character */
/* blanks, tabs, and newlines are ignored */
s = 1;
val = 0;
while ((c = getc(finput)) != EOF) {
if (isdigit(c)) {
val = val * 10 + c - '0';
} else if (c == '-')
s = -1;
else
break;
}
*pmem++ = s * val;
if (pmem > &mem[MEMSIZE])
error("out of space");
return (c);
}

View File

@@ -1,153 +0,0 @@
#if !defined(lint) && defined(DOSCCS)
static char yaccpar_sccsid[] = "@(#)yaccpar 4.1 (Berkeley) 2/11/83";
#endif
#
# define YYFLAG -1000
# define YYERROR goto yyerrlab
# define YYACCEPT return(0)
# define YYABORT return(1)
/* parser for yacc output */
#ifdef YYDEBUG
int yydebug = 0; /* 1 for debugging */
#endif
YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
int yychar = -1; /* current input token number */
int yynerrs = 0; /* number of errors */
short yyerrflag = 0; /* error recovery flag */
yyparse() {
short yys[YYMAXDEPTH];
short yyj, yym;
register YYSTYPE *yypvt;
register short yystate, *yyps, yyn;
register YYSTYPE *yypv;
register short *yyxi;
yystate = 0;
yychar = -1;
yynerrs = 0;
yyerrflag = 0;
yyps= &yys[-1];
yypv= &yyv[-1];
yystack: /* put a state and value onto the stack */
#ifdef YYDEBUG
if( yydebug ) printf( "state %d, char 0%o\n", yystate, yychar );
#endif
if( ++yyps> &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); }
*yyps = yystate;
++yypv;
*yypv = yyval;
yynewstate:
yyn = yypact[yystate];
if( yyn<= YYFLAG ) goto yydefault; /* simple state */
if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;
if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
yychar = -1;
yyval = yylval;
yystate = yyn;
if( yyerrflag > 0 ) --yyerrflag;
goto yystack;
}
yydefault:
/* default state action */
if( (yyn=yydef[yystate]) == -2 ) {
if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
/* look through exception table */
for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */
while( *(yyxi+=2) >= 0 ){
if( *yyxi == yychar ) break;
}
if( (yyn = yyxi[1]) < 0 ) return(0); /* accept */
}
if( yyn == 0 ){ /* error */
/* error ... attempt to resume parsing */
switch( yyerrflag ){
case 0: /* brand new error */
yyerror( "syntax error" );
yyerrlab:
++yynerrs;
case 1:
case 2: /* incompletely recovered error ... try again */
yyerrflag = 3;
/* find a state where "error" is a legal shift action */
while ( yyps >= yys ) {
yyn = yypact[*yyps] + YYERRCODE;
if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
yystate = yyact[yyn]; /* simulate a shift of "error" */
goto yystack;
}
yyn = yypact[*yyps];
/* the current yyps has no shift onn "error", pop stack */
#ifdef YYDEBUG
if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
#endif
--yyps;
--yypv;
}
/* there is no state on the stack with an error shift ... abort */
yyabort:
return(1);
case 3: /* no shift yet; clobber input char */
#ifdef YYDEBUG
if( yydebug ) printf( "error recovery discards char %d\n", yychar );
#endif
if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
yychar = -1;
goto yynewstate; /* try again in the same state */
}
}
/* reduction by production yyn */
#ifdef YYDEBUG
if( yydebug ) printf("reduce %d\n",yyn);
#endif
yyps -= yyr2[yyn];
yypvt = yypv;
yypv -= yyr2[yyn];
yyval = yypv[1];
yym=yyn;
/* consult goto table to find next state */
yyn = yyr1[yyn];
yyj = yypgo[yyn] + *yyps + 1;
if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
switch(yym){
$A
}
goto yystack; /* stack new state and value */
}

163
src/cmd/yacc/yaccpar.c Normal file
View File

@@ -0,0 +1,163 @@
/* parser for yacc output */
#define YYFLAG -1000
#define YYERROR goto yyerrlab
#define YYACCEPT return (0)
#define YYABORT return (1)
#ifdef YYDEBUG
int yydebug = 0; /* 1 for debugging */
#endif
YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
int yychar = -1; /* current input token number */
int yynerrs = 0; /* number of errors */
short yyerrflag = 0; /* error recovery flag */
yyparse()
{
short yys[YYMAXDEPTH];
short yyj, yym;
register YYSTYPE *yypvt;
register short yystate, *yyps, yyn;
register YYSTYPE *yypv;
register short *yyxi;
yystate = 0;
yychar = -1;
yynerrs = 0;
yyerrflag = 0;
yyps = &yys[-1];
yypv = &yyv[-1];
yystack: /* put a state and value onto the stack */
#ifdef YYDEBUG
if (yydebug)
printf("state %d, char 0%o\n", yystate, yychar);
#endif
if (++yyps > &yys[YYMAXDEPTH]) {
yyerror("yacc stack overflow");
return (1);
}
*yyps = yystate;
++yypv;
*yypv = yyval;
yynewstate:
yyn = yypact[yystate];
if (yyn <= YYFLAG)
goto yydefault; /* simple state */
if (yychar < 0)
if ((yychar = yylex()) < 0)
yychar = 0;
if ((yyn += yychar) < 0 || yyn >= YYLAST)
goto yydefault;
if (yychk[yyn = yyact[yyn]] == yychar) { /* valid shift */
yychar = -1;
yyval = yylval;
yystate = yyn;
if (yyerrflag > 0)
--yyerrflag;
goto yystack;
}
yydefault:
/* default state action */
if ((yyn = yydef[yystate]) == -2) {
if (yychar < 0)
if ((yychar = yylex()) < 0)
yychar = 0;
/* look through exception table */
for (yyxi = yyexca; (*yyxi != (-1)) || (yyxi[1] != yystate); yyxi += 2)
; /* VOID */
while (*(yyxi += 2) >= 0) {
if (*yyxi == yychar)
break;
}
if ((yyn = yyxi[1]) < 0)
return (0); /* accept */
}
if (yyn == 0) { /* error */
/* error ... attempt to resume parsing */
switch (yyerrflag) {
case 0: /* brand new error */
yyerror("syntax error");
yyerrlab:
++yynerrs;
case 1:
case 2: /* incompletely recovered error ... try again */
yyerrflag = 3;
/* find a state where "error" is a legal shift action */
while (yyps >= yys) {
yyn = yypact[*yyps] + YYERRCODE;
if (yyn >= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE) {
yystate = yyact[yyn]; /* simulate a shift of "error" */
goto yystack;
}
yyn = yypact[*yyps];
/* the current yyps has no shift onn "error", pop stack */
#ifdef YYDEBUG
if (yydebug)
printf("error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1]);
#endif
--yyps;
--yypv;
}
/* there is no state on the stack with an error shift ... abort */
yyabort:
return (1);
case 3: /* no shift yet; clobber input char */
#ifdef YYDEBUG
if (yydebug)
printf("error recovery discards char %d\n", yychar);
#endif
if (yychar == 0)
goto yyabort; /* don't discard EOF, quit */
yychar = -1;
goto yynewstate; /* try again in the same state */
}
}
/* reduction by production yyn */
#ifdef YYDEBUG
if (yydebug)
printf("reduce %d\n", yyn);
#endif
yyps -= yyr2[yyn];
yypvt = yypv;
yypv -= yyr2[yyn];
yyval = yypv[1];
yym = yyn;
/* consult goto table to find next state */
yyn = yyr1[yyn];
yyj = yypgo[yyn] + *yyps + 1;
if (yyj >= YYLAST || yychk[yystate = yyact[yyj]] != -yyn)
yystate = yyact[yypgo[yyn]];
switch (yym) {
$A
}
goto yystack; /* stack new state and value */
}