Reduce the differences between yaccpar and yaccpars.

Add support for passing an argument through yyparse to yylex.
This commit is contained in:
wkj 2005-02-14 20:27:13 +00:00
parent 5ddc97fc3e
commit cae9bfe9da
4 changed files with 146 additions and 38 deletions

View file

@ -1,4 +1,7 @@
#define YYFLAG -1000 #define YYFLAG -1000
#define YYERROR goto yyerrlab
#define YYACCEPT return(0)
#define YYABORT return(1)
#define yyclearin yychar = -1 #define yyclearin yychar = -1
#define yyerrok yyerrflag = 0 #define yyerrok yyerrflag = 0
@ -6,19 +9,25 @@
#include "y.debug" #include "y.debug"
#else #else
#define yydebug 0 #define yydebug 0
char* yytoknames[1]; /* for debugging */ static const char* yytoknames[1]; /* for debugging */
char* yystates[1]; /* for debugging */ static const char* yystates[1]; /* for debugging */
#endif #endif
/* parser for yacc output */ /* parser for yacc output */
#ifdef YYARG
#define yynerrs yyarg->yynerrs
#define yyerrflag yyarg->yyerrflag
#define yyval yyarg->yyval
#define yylval yyarg->yylval
#else
int yynerrs = 0; /* number of errors */ int yynerrs = 0; /* number of errors */
int yyerrflag = 0; /* error recovery flag */ int yyerrflag = 0; /* error recovery flag */
#endif
extern int fprint(int, char*, ...); extern int fprint(int, char*, ...);
extern int sprint(char*, char*, ...); extern int sprint(char*, char*, ...);
char* static const char*
yytokname(int yyc) yytokname(int yyc)
{ {
static char x[10]; static char x[10];
@ -30,7 +39,7 @@ yytokname(int yyc)
return x; return x;
} }
char* static const char*
yystatname(int yys) yystatname(int yys)
{ {
static char x[10]; static char x[10];
@ -42,14 +51,22 @@ yystatname(int yys)
return x; return x;
} }
long static long
#ifdef YYARG
yylex1(struct Yyarg *yyarg)
#else
yylex1(void) yylex1(void)
#endif
{ {
long yychar; long yychar;
long *t3p; const long *t3p;
int c; int c;
#ifdef YYARG
yychar = yylex(yyarg);
#else
yychar = yylex(); yychar = yylex();
#endif
if(yychar <= 0) { if(yychar <= 0) {
c = yytok1[0]; c = yytok1[0];
goto out; goto out;
@ -83,16 +100,21 @@ out:
} }
int int
#ifdef YYARG
yyparse(struct Yyarg *yyarg)
#else
yyparse(void) yyparse(void)
#endif
{ {
struct struct
{ {
YYSTYPE yyv; YYSTYPE yyv;
int yys; int yys;
} yys[YYMAXDEPTH], *yyp, *yypt; } yys[YYMAXDEPTH], *yyp, *yypt;
short *yyxi; const short *yyxi;
int yyj, yym, yystate, yyn, yyg; int yyj, yym, yystate, yyn, yyg;
long yychar; long yychar;
#ifndef YYARG
YYSTYPE save1, save2; YYSTYPE save1, save2;
int save3, save4; int save3, save4;
@ -100,6 +122,7 @@ yyparse(void)
save2 = yyval; save2 = yyval;
save3 = yynerrs; save3 = yynerrs;
save4 = yyerrflag; save4 = yyerrflag;
#endif
yystate = 0; yystate = 0;
yychar = -1; yychar = -1;
@ -117,10 +140,12 @@ ret1:
goto ret; goto ret;
ret: ret:
#ifndef YYARG
yylval = save1; yylval = save1;
yyval = save2; yyval = save2;
yynerrs = save3; yynerrs = save3;
yyerrflag = save4; yyerrflag = save4;
#endif
return yyn; return yyn;
yystack: yystack:
@ -141,7 +166,11 @@ yynewstate:
if(yyn <= YYFLAG) if(yyn <= YYFLAG)
goto yydefault; /* simple state */ goto yydefault; /* simple state */
if(yychar < 0) if(yychar < 0)
#ifdef YYARG
yychar = yylex1(yyarg);
#else
yychar = yylex1(); yychar = yylex1();
#endif
yyn += yychar; yyn += yychar;
if(yyn < 0 || yyn >= YYLAST) if(yyn < 0 || yyn >= YYLAST)
goto yydefault; goto yydefault;
@ -160,7 +189,11 @@ yydefault:
yyn = yydef[yystate]; yyn = yydef[yystate];
if(yyn == -2) { if(yyn == -2) {
if(yychar < 0) if(yychar < 0)
yychar = yylex1(); #ifdef YYARG
yychar = yylex1(yyarg);
#else
yychar = yylex1();
#endif
/* look through exception table */ /* look through exception table */
for(yyxi=yyexca;; yyxi+=2) for(yyxi=yyexca;; yyxi+=2)
@ -180,11 +213,12 @@ yydefault:
switch(yyerrflag) { switch(yyerrflag) {
case 0: /* brand new error */ case 0: /* brand new error */
yyerror("syntax error"); yyerror("syntax error");
yynerrs++;
if(yydebug >= 1) { if(yydebug >= 1) {
fprint(2, "%s", yystatname(yystate)); fprint(2, "%s", yystatname(yystate));
fprint(2, "saw %s\n", yytokname(yychar)); fprint(2, "saw %s\n", yytokname(yychar));
} }
yyerrlab:
yynerrs++;
case 1: case 1:
case 2: /* incompletely recovered error ... try again */ case 2: /* incompletely recovered error ... try again */

View file

@ -9,16 +9,22 @@
#include "y.debug" #include "y.debug"
#else #else
#define yydebug 0 #define yydebug 0
char* yytoknames[1]; /* for debugging */ static const char* yytoknames[1]; /* for debugging */
char* yystates[1]; /* for debugging */ static const char* yystates[1]; /* for debugging */
#endif #endif
/* parser for yacc output */ /* parser for yacc output */
#ifdef YYARG
#define yynerrs yyarg->yynerrs
#define yyerrflag yyarg->yyerrflag
#define yyval yyarg->yyval
#define yylval yyarg->yylval
#else
int yynerrs = 0; /* number of errors */ int yynerrs = 0; /* number of errors */
int yyerrflag = 0; /* error recovery flag */ int yyerrflag = 0; /* error recovery flag */
#endif
char* static const char*
yytokname(int yyc) yytokname(int yyc)
{ {
static char x[10]; static char x[10];
@ -30,7 +36,7 @@ yytokname(int yyc)
return x; return x;
} }
char* static const char*
yystatname(int yys) yystatname(int yys)
{ {
static char x[10]; static char x[10];
@ -42,14 +48,22 @@ yystatname(int yys)
return x; return x;
} }
long static long
#ifdef YYARG
yylex1(struct Yyarg *yyarg)
#else
yylex1(void) yylex1(void)
#endif
{ {
long yychar; long yychar;
long *t3p; const long *t3p;
int c; int c;
#ifdef YYARG
yychar = yylex(yyarg);
#else
yychar = yylex(); yychar = yylex();
#endif
if(yychar <= 0) { if(yychar <= 0) {
c = yytok1[0]; c = yytok1[0];
goto out; goto out;
@ -83,23 +97,29 @@ out:
} }
int int
#ifdef YYARG
yyparse(struct Yyarg *yyarg)
#else
yyparse(void) yyparse(void)
#endif
{ {
struct struct
{ {
YYSTYPE yyv; YYSTYPE yyv;
int yys; int yys;
} yys[YYMAXDEPTH], *yyp, *yypt; } yys[YYMAXDEPTH], *yyp, *yypt;
short *yyxi; const short *yyxi;
int yyj, yym, yystate, yyn, yyg; int yyj, yym, yystate, yyn, yyg;
long yychar;
#ifndef YYARG
YYSTYPE save1, save2; YYSTYPE save1, save2;
int save3, save4; int save3, save4;
long yychar;
save1 = yylval; save1 = yylval;
save2 = yyval; save2 = yyval;
save3 = yynerrs; save3 = yynerrs;
save4 = yyerrflag; save4 = yyerrflag;
#endif
yystate = 0; yystate = 0;
yychar = -1; yychar = -1;
@ -117,10 +137,12 @@ ret1:
goto ret; goto ret;
ret: ret:
#ifndef YYARG
yylval = save1; yylval = save1;
yyval = save2; yyval = save2;
yynerrs = save3; yynerrs = save3;
yyerrflag = save4; yyerrflag = save4;
#endif
return yyn; return yyn;
yystack: yystack:
@ -141,7 +163,11 @@ yynewstate:
if(yyn <= YYFLAG) if(yyn <= YYFLAG)
goto yydefault; /* simple state */ goto yydefault; /* simple state */
if(yychar < 0) if(yychar < 0)
#ifdef YYARG
yychar = yylex1(yyarg);
#else
yychar = yylex1(); yychar = yylex1();
#endif
yyn += yychar; yyn += yychar;
if(yyn < 0 || yyn >= YYLAST) if(yyn < 0 || yyn >= YYLAST)
goto yydefault; goto yydefault;
@ -160,7 +186,11 @@ yydefault:
yyn = yydef[yystate]; yyn = yydef[yystate];
if(yyn == -2) { if(yyn == -2) {
if(yychar < 0) if(yychar < 0)
yychar = yylex1(); #ifdef YYARG
yychar = yylex1(yyarg);
#else
yychar = yylex1();
#endif
/* look through exception table */ /* look through exception table */
for(yyxi=yyexca;; yyxi+=2) for(yyxi=yyexca;; yyxi+=2)

View file

@ -86,6 +86,15 @@ Stdio
instead of the instead of the
.B print .B print
routines in libc. routines in libc.
.TP
.BI -l
Disable #line directives in the generated parser.
.TP
.BI -a
Generate a parser that takes an argument of type Yyarg
and passes this argument to each invocation of the lexer
function, yylex. Yyarg contains per-instance state
and a single user-visible member, arg, of type void*.
.PP .PP
The specification of The specification of
.I yacc .I yacc

View file

@ -15,6 +15,7 @@
char *PARSER = "#9/lib/yaccpar"; char *PARSER = "#9/lib/yaccpar";
char *PARSERS = "#9/lib/yaccpars"; char *PARSERS = "#9/lib/yaccpars";
#define TEMPNAME "y.tmp.XXXXXX" #define TEMPNAME "y.tmp.XXXXXX"
#define ACTNAME "y.acts.XXXXXX" #define ACTNAME "y.acts.XXXXXX"
#define OFILE "tab.c" #define OFILE "tab.c"
@ -185,6 +186,8 @@ char ttempname[] = TEMPNAME;
char tactname[] = ACTNAME; char tactname[] = ACTNAME;
char* parser; char* parser;
char* yydebug; char* yydebug;
int yyarg;
int yyline = 1;
/* storage of types */ /* storage of types */
int ntypes; /* number of types defined */ int ntypes; /* number of types defined */
@ -459,7 +462,7 @@ others(void)
warray("yytok2", temp1, c+1); warray("yytok2", temp1, c+1);
/* table 3 has everything else */ /* table 3 has everything else */
Bprint(ftable, "long yytok3[] =\n{\n"); Bprint(ftable, "static\tconst\tlong yytok3[] =\n{\n");
c = 0; c = 0;
TLOOP(i) { TLOOP(i) {
j = tokset[i].value; j = tokset[i].value;
@ -1176,6 +1179,7 @@ setup(int argc, char *argv[])
int i, j, fd, lev, ty, ytab, *p; int i, j, fd, lev, ty, ytab, *p;
int vflag, dflag, stem; int vflag, dflag, stem;
char actnm[8], *stemc, *s, dirbuf[128]; char actnm[8], *stemc, *s, dirbuf[128];
Biobuf *fout;
ytab = 0; ytab = 0;
vflag = 0; vflag = 0;
@ -1193,9 +1197,15 @@ setup(int argc, char *argv[])
case 'D': case 'D':
yydebug = ARGF(); yydebug = ARGF();
break; break;
case 'a':
yyarg = 1;
break;
case 'd': case 'd':
dflag++; dflag++;
break; break;
case 'l':
yyline = 0;
break;
case 'o': case 'o':
ytab++; ytab++;
ytabc = ARGF(); ytabc = ARGF();
@ -1211,7 +1221,10 @@ setup(int argc, char *argv[])
error("illegal option: %c", ARGC()); error("illegal option: %c", ARGC());
}ARGEND }ARGEND
openup(stemc, dflag, vflag, ytab, ytabc); openup(stemc, dflag, vflag, ytab, ytabc);
fout = dflag?fdefine:ftable;
if(yyarg){
Bprint(fdefine, "#define\tYYARG\t1\n\n");
}
if((fd = mkstemp(ttempname)) >= 0){ if((fd = mkstemp(ttempname)) >= 0){
tempname = ttempname; tempname = ttempname;
ftemp = Bfdopen(fd, OWRITE); ftemp = Bfdopen(fd, OWRITE);
@ -1369,7 +1382,8 @@ setup(int argc, char *argv[])
error("unexpected EOF before %%"); error("unexpected EOF before %%");
/* t is MARK */ /* t is MARK */
Bprint(ftable, "extern int yyerrflag;\n"); if(!yyarg)
Bprint(ftable, "extern int yyerrflag;\n");
Bprint(ftable, "#ifndef YYMAXDEPTH\n"); Bprint(ftable, "#ifndef YYMAXDEPTH\n");
Bprint(ftable, "#define YYMAXDEPTH 150\n"); Bprint(ftable, "#define YYMAXDEPTH 150\n");
Bprint(ftable, "#endif\n" ); Bprint(ftable, "#endif\n" );
@ -1378,9 +1392,20 @@ setup(int argc, char *argv[])
Bprint(ftable, "#define YYSTYPE int\n"); Bprint(ftable, "#define YYSTYPE int\n");
Bprint(ftable, "#endif\n"); Bprint(ftable, "#endif\n");
} }
Bprint(ftable, "YYSTYPE yylval;\n"); if(!yyarg){
Bprint(ftable, "YYSTYPE yyval;\n"); Bprint(ftable, "YYSTYPE yylval;\n");
Bprint(ftable, "YYSTYPE yyval;\n");
}else{
if(dflag)
Bprint(ftable, "#include \"%s.%s\"\n\n", stemc, FILED);
Bprint(fout, "struct Yyarg {\n");
Bprint(fout, "\tint\tyynerrs;\n");
Bprint(fout, "\tint\tyyerrflag;\n");
Bprint(fout, "\tvoid*\targ;\n");
Bprint(fout, "\tYYSTYPE\tyyval;\n");
Bprint(fout, "\tYYSTYPE\tyylval;\n");
Bprint(fout, "};\n\n");
}
prdptr[0] = mem; prdptr[0] = mem;
/* added production */ /* added production */
@ -1508,7 +1533,9 @@ setup(int argc, char *argv[])
finact(); finact();
if(t == MARK) { if(t == MARK) {
Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile); Bprint(ftable, "\n");
if(yyline)
Bprint(ftable, "#line\t%d\t\"%s\"\n", lineno, infile);
while((c=Bgetrune(finput)) != Beof) while((c=Bgetrune(finput)) != Beof)
Bputrune(ftable, c); Bputrune(ftable, c);
} }
@ -1625,7 +1652,7 @@ defout(int last)
} }
ndefout = ntokens+1; ndefout = ntokens+1;
if(last && fdebug) { if(last && fdebug) {
Bprint(fdebug, "char* yytoknames[] =\n{\n"); Bprint(fdebug, "static char* yytoknames[] =\n{\n");
TLOOP(i) { TLOOP(i) {
if(tokset[i].name) { if(tokset[i].name) {
chcopy(sar, tokset[i].name); chcopy(sar, tokset[i].name);
@ -1844,7 +1871,9 @@ cpyunion(void)
long c; long c;
int level; int level;
Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile); Bprint(ftable, "\n");
if(yyline)
Bprint(ftable, "#line\t%d\t\"%s\"\n", lineno, infile);
Bprint(ftable, "typedef union "); Bprint(ftable, "typedef union ");
if(fdefine != 0) if(fdefine != 0)
Bprint(fdefine, "\ntypedef union "); Bprint(fdefine, "\ntypedef union ");
@ -1869,8 +1898,11 @@ cpyunion(void)
/* we are finished copying */ /* we are finished copying */
if(level == 0) { if(level == 0) {
Bprint(ftable, " YYSTYPE;\n"); Bprint(ftable, " YYSTYPE;\n");
if(fdefine != 0) if(fdefine != 0){
Bprint(fdefine, "\tYYSTYPE;\nextern\tYYSTYPE\tyylval;\n"); Bprint(fdefine, "\tYYSTYPE;\n");
if(!yyarg)
Bprint(fdefine, "extern\tYYSTYPE\tyylval;\n");
}
return; return;
} }
} }
@ -1883,7 +1915,6 @@ cpyunion(void)
void void
cpycode(void) cpycode(void)
{ {
long c; long c;
c = Bgetrune(finput); c = Bgetrune(finput);
@ -1891,7 +1922,9 @@ cpycode(void)
c = Bgetrune(finput); c = Bgetrune(finput);
lineno++; lineno++;
} }
Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile); Bprint(ftable, "\n");
if(yyline)
Bprint(ftable, "#line\t%d\t\"%s\"\n", lineno, infile);
while(c != Beof) { while(c != Beof) {
if(c == '\\') { if(c == '\\') {
if((c=Bgetrune(finput)) == '}') if((c=Bgetrune(finput)) == '}')
@ -1947,7 +1980,9 @@ cpyact(int offset)
long c; long c;
int brac, match, j, s, fnd, tok; int brac, match, j, s, fnd, tok;
Bprint(faction, "\n#line\t%d\t\"%s\"\n", lineno, infile); Bprint(faction, "\n");
if(yyline)
Bprint(faction, "#line\t%d\t\"%s\"\n", lineno, infile);
brac = 0; brac = 0;
loop: loop:
@ -2157,9 +2192,9 @@ output(void)
int i, k, c; int i, k, c;
Wset *u, *v; Wset *u, *v;
Bprint(ftable, "short yyexca[] =\n{"); Bprint(ftable, "static\tconst\tshort yyexca[] =\n{");
if(fdebug) if(fdebug)
Bprint(fdebug, "char* yystates[] =\n{\n"); Bprint(fdebug, "static\tconst\tchar* yystates[] =\n{\n");
/* output the stuff for state i */ /* output the stuff for state i */
SLOOP(i) { SLOOP(i) {
@ -2570,7 +2605,7 @@ warray(char *s, int *v, int n)
{ {
int i; int i;
Bprint(ftable, "short %s[] =\n{", s); Bprint(ftable, "static\tconst\tshort %s[] =\n{", s);
for(i=0;;) { for(i=0;;) {
if(i%10 == 0) if(i%10 == 0)
Bprint(ftable, "\n"); Bprint(ftable, "\n");
@ -2900,7 +2935,7 @@ arout(char *s, int *v, int n)
{ {
int i; int i;
Bprint(ftable, "short %s[] =\n{", s); Bprint(ftable, "static\tconst\tshort %s[] =\n{", s);
for(i = 0; i < n;) { for(i = 0; i < n;) {
if(i%10 == 0) if(i%10 == 0)
Bprint(ftable, "\n"); Bprint(ftable, "\n");