mirror of
https://github.com/9fans/plan9port.git
synced 2025-01-12 11:10:07 +00:00
Reduce the differences between yaccpar and yaccpars.
Add support for passing an argument through yyparse to yylex.
This commit is contained in:
parent
5ddc97fc3e
commit
cae9bfe9da
4 changed files with 146 additions and 38 deletions
54
lib/yaccpar
54
lib/yaccpar
|
@ -1,4 +1,7 @@
|
|||
#define YYFLAG -1000
|
||||
#define YYERROR goto yyerrlab
|
||||
#define YYACCEPT return(0)
|
||||
#define YYABORT return(1)
|
||||
#define yyclearin yychar = -1
|
||||
#define yyerrok yyerrflag = 0
|
||||
|
||||
|
@ -6,19 +9,25 @@
|
|||
#include "y.debug"
|
||||
#else
|
||||
#define yydebug 0
|
||||
char* yytoknames[1]; /* for debugging */
|
||||
char* yystates[1]; /* for debugging */
|
||||
static const char* yytoknames[1]; /* for debugging */
|
||||
static const char* yystates[1]; /* for debugging */
|
||||
#endif
|
||||
|
||||
/* 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 yyerrflag = 0; /* error recovery flag */
|
||||
#endif
|
||||
|
||||
extern int fprint(int, char*, ...);
|
||||
extern int sprint(char*, char*, ...);
|
||||
|
||||
char*
|
||||
static const char*
|
||||
yytokname(int yyc)
|
||||
{
|
||||
static char x[10];
|
||||
|
@ -30,7 +39,7 @@ yytokname(int yyc)
|
|||
return x;
|
||||
}
|
||||
|
||||
char*
|
||||
static const char*
|
||||
yystatname(int yys)
|
||||
{
|
||||
static char x[10];
|
||||
|
@ -42,14 +51,22 @@ yystatname(int yys)
|
|||
return x;
|
||||
}
|
||||
|
||||
long
|
||||
static long
|
||||
#ifdef YYARG
|
||||
yylex1(struct Yyarg *yyarg)
|
||||
#else
|
||||
yylex1(void)
|
||||
#endif
|
||||
{
|
||||
long yychar;
|
||||
long *t3p;
|
||||
const long *t3p;
|
||||
int c;
|
||||
|
||||
#ifdef YYARG
|
||||
yychar = yylex(yyarg);
|
||||
#else
|
||||
yychar = yylex();
|
||||
#endif
|
||||
if(yychar <= 0) {
|
||||
c = yytok1[0];
|
||||
goto out;
|
||||
|
@ -83,16 +100,21 @@ out:
|
|||
}
|
||||
|
||||
int
|
||||
#ifdef YYARG
|
||||
yyparse(struct Yyarg *yyarg)
|
||||
#else
|
||||
yyparse(void)
|
||||
#endif
|
||||
{
|
||||
struct
|
||||
{
|
||||
YYSTYPE yyv;
|
||||
int yys;
|
||||
} yys[YYMAXDEPTH], *yyp, *yypt;
|
||||
short *yyxi;
|
||||
const short *yyxi;
|
||||
int yyj, yym, yystate, yyn, yyg;
|
||||
long yychar;
|
||||
#ifndef YYARG
|
||||
YYSTYPE save1, save2;
|
||||
int save3, save4;
|
||||
|
||||
|
@ -100,6 +122,7 @@ yyparse(void)
|
|||
save2 = yyval;
|
||||
save3 = yynerrs;
|
||||
save4 = yyerrflag;
|
||||
#endif
|
||||
|
||||
yystate = 0;
|
||||
yychar = -1;
|
||||
|
@ -117,10 +140,12 @@ ret1:
|
|||
goto ret;
|
||||
|
||||
ret:
|
||||
#ifndef YYARG
|
||||
yylval = save1;
|
||||
yyval = save2;
|
||||
yynerrs = save3;
|
||||
yyerrflag = save4;
|
||||
#endif
|
||||
return yyn;
|
||||
|
||||
yystack:
|
||||
|
@ -141,7 +166,11 @@ yynewstate:
|
|||
if(yyn <= YYFLAG)
|
||||
goto yydefault; /* simple state */
|
||||
if(yychar < 0)
|
||||
#ifdef YYARG
|
||||
yychar = yylex1(yyarg);
|
||||
#else
|
||||
yychar = yylex1();
|
||||
#endif
|
||||
yyn += yychar;
|
||||
if(yyn < 0 || yyn >= YYLAST)
|
||||
goto yydefault;
|
||||
|
@ -160,7 +189,11 @@ yydefault:
|
|||
yyn = yydef[yystate];
|
||||
if(yyn == -2) {
|
||||
if(yychar < 0)
|
||||
yychar = yylex1();
|
||||
#ifdef YYARG
|
||||
yychar = yylex1(yyarg);
|
||||
#else
|
||||
yychar = yylex1();
|
||||
#endif
|
||||
|
||||
/* look through exception table */
|
||||
for(yyxi=yyexca;; yyxi+=2)
|
||||
|
@ -180,11 +213,12 @@ yydefault:
|
|||
switch(yyerrflag) {
|
||||
case 0: /* brand new error */
|
||||
yyerror("syntax error");
|
||||
yynerrs++;
|
||||
if(yydebug >= 1) {
|
||||
fprint(2, "%s", yystatname(yystate));
|
||||
fprint(2, "saw %s\n", yytokname(yychar));
|
||||
}
|
||||
yyerrlab:
|
||||
yynerrs++;
|
||||
|
||||
case 1:
|
||||
case 2: /* incompletely recovered error ... try again */
|
||||
|
|
50
lib/yaccpars
50
lib/yaccpars
|
@ -9,16 +9,22 @@
|
|||
#include "y.debug"
|
||||
#else
|
||||
#define yydebug 0
|
||||
char* yytoknames[1]; /* for debugging */
|
||||
char* yystates[1]; /* for debugging */
|
||||
static const char* yytoknames[1]; /* for debugging */
|
||||
static const char* yystates[1]; /* for debugging */
|
||||
#endif
|
||||
|
||||
/* 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 yyerrflag = 0; /* error recovery flag */
|
||||
#endif
|
||||
|
||||
char*
|
||||
static const char*
|
||||
yytokname(int yyc)
|
||||
{
|
||||
static char x[10];
|
||||
|
@ -30,7 +36,7 @@ yytokname(int yyc)
|
|||
return x;
|
||||
}
|
||||
|
||||
char*
|
||||
static const char*
|
||||
yystatname(int yys)
|
||||
{
|
||||
static char x[10];
|
||||
|
@ -42,14 +48,22 @@ yystatname(int yys)
|
|||
return x;
|
||||
}
|
||||
|
||||
long
|
||||
static long
|
||||
#ifdef YYARG
|
||||
yylex1(struct Yyarg *yyarg)
|
||||
#else
|
||||
yylex1(void)
|
||||
#endif
|
||||
{
|
||||
long yychar;
|
||||
long *t3p;
|
||||
const long *t3p;
|
||||
int c;
|
||||
|
||||
#ifdef YYARG
|
||||
yychar = yylex(yyarg);
|
||||
#else
|
||||
yychar = yylex();
|
||||
#endif
|
||||
if(yychar <= 0) {
|
||||
c = yytok1[0];
|
||||
goto out;
|
||||
|
@ -83,23 +97,29 @@ out:
|
|||
}
|
||||
|
||||
int
|
||||
#ifdef YYARG
|
||||
yyparse(struct Yyarg *yyarg)
|
||||
#else
|
||||
yyparse(void)
|
||||
#endif
|
||||
{
|
||||
struct
|
||||
{
|
||||
YYSTYPE yyv;
|
||||
int yys;
|
||||
} yys[YYMAXDEPTH], *yyp, *yypt;
|
||||
short *yyxi;
|
||||
const short *yyxi;
|
||||
int yyj, yym, yystate, yyn, yyg;
|
||||
long yychar;
|
||||
#ifndef YYARG
|
||||
YYSTYPE save1, save2;
|
||||
int save3, save4;
|
||||
long yychar;
|
||||
|
||||
save1 = yylval;
|
||||
save2 = yyval;
|
||||
save3 = yynerrs;
|
||||
save4 = yyerrflag;
|
||||
#endif
|
||||
|
||||
yystate = 0;
|
||||
yychar = -1;
|
||||
|
@ -117,10 +137,12 @@ ret1:
|
|||
goto ret;
|
||||
|
||||
ret:
|
||||
#ifndef YYARG
|
||||
yylval = save1;
|
||||
yyval = save2;
|
||||
yynerrs = save3;
|
||||
yyerrflag = save4;
|
||||
#endif
|
||||
return yyn;
|
||||
|
||||
yystack:
|
||||
|
@ -141,7 +163,11 @@ yynewstate:
|
|||
if(yyn <= YYFLAG)
|
||||
goto yydefault; /* simple state */
|
||||
if(yychar < 0)
|
||||
#ifdef YYARG
|
||||
yychar = yylex1(yyarg);
|
||||
#else
|
||||
yychar = yylex1();
|
||||
#endif
|
||||
yyn += yychar;
|
||||
if(yyn < 0 || yyn >= YYLAST)
|
||||
goto yydefault;
|
||||
|
@ -160,7 +186,11 @@ yydefault:
|
|||
yyn = yydef[yystate];
|
||||
if(yyn == -2) {
|
||||
if(yychar < 0)
|
||||
yychar = yylex1();
|
||||
#ifdef YYARG
|
||||
yychar = yylex1(yyarg);
|
||||
#else
|
||||
yychar = yylex1();
|
||||
#endif
|
||||
|
||||
/* look through exception table */
|
||||
for(yyxi=yyexca;; yyxi+=2)
|
||||
|
|
|
@ -86,6 +86,15 @@ Stdio
|
|||
instead of the
|
||||
.B print
|
||||
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
|
||||
The specification of
|
||||
.I yacc
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
char *PARSER = "#9/lib/yaccpar";
|
||||
char *PARSERS = "#9/lib/yaccpars";
|
||||
|
||||
#define TEMPNAME "y.tmp.XXXXXX"
|
||||
#define ACTNAME "y.acts.XXXXXX"
|
||||
#define OFILE "tab.c"
|
||||
|
@ -185,6 +186,8 @@ char ttempname[] = TEMPNAME;
|
|||
char tactname[] = ACTNAME;
|
||||
char* parser;
|
||||
char* yydebug;
|
||||
int yyarg;
|
||||
int yyline = 1;
|
||||
|
||||
/* storage of types */
|
||||
int ntypes; /* number of types defined */
|
||||
|
@ -459,7 +462,7 @@ others(void)
|
|||
warray("yytok2", temp1, c+1);
|
||||
|
||||
/* table 3 has everything else */
|
||||
Bprint(ftable, "long yytok3[] =\n{\n");
|
||||
Bprint(ftable, "static\tconst\tlong yytok3[] =\n{\n");
|
||||
c = 0;
|
||||
TLOOP(i) {
|
||||
j = tokset[i].value;
|
||||
|
@ -1176,6 +1179,7 @@ setup(int argc, char *argv[])
|
|||
int i, j, fd, lev, ty, ytab, *p;
|
||||
int vflag, dflag, stem;
|
||||
char actnm[8], *stemc, *s, dirbuf[128];
|
||||
Biobuf *fout;
|
||||
|
||||
ytab = 0;
|
||||
vflag = 0;
|
||||
|
@ -1193,9 +1197,15 @@ setup(int argc, char *argv[])
|
|||
case 'D':
|
||||
yydebug = ARGF();
|
||||
break;
|
||||
case 'a':
|
||||
yyarg = 1;
|
||||
break;
|
||||
case 'd':
|
||||
dflag++;
|
||||
break;
|
||||
case 'l':
|
||||
yyline = 0;
|
||||
break;
|
||||
case 'o':
|
||||
ytab++;
|
||||
ytabc = ARGF();
|
||||
|
@ -1211,7 +1221,10 @@ setup(int argc, char *argv[])
|
|||
error("illegal option: %c", ARGC());
|
||||
}ARGEND
|
||||
openup(stemc, dflag, vflag, ytab, ytabc);
|
||||
|
||||
fout = dflag?fdefine:ftable;
|
||||
if(yyarg){
|
||||
Bprint(fdefine, "#define\tYYARG\t1\n\n");
|
||||
}
|
||||
if((fd = mkstemp(ttempname)) >= 0){
|
||||
tempname = ttempname;
|
||||
ftemp = Bfdopen(fd, OWRITE);
|
||||
|
@ -1369,7 +1382,8 @@ setup(int argc, char *argv[])
|
|||
error("unexpected EOF before %%");
|
||||
|
||||
/* t is MARK */
|
||||
Bprint(ftable, "extern int yyerrflag;\n");
|
||||
if(!yyarg)
|
||||
Bprint(ftable, "extern int yyerrflag;\n");
|
||||
Bprint(ftable, "#ifndef YYMAXDEPTH\n");
|
||||
Bprint(ftable, "#define YYMAXDEPTH 150\n");
|
||||
Bprint(ftable, "#endif\n" );
|
||||
|
@ -1378,9 +1392,20 @@ setup(int argc, char *argv[])
|
|||
Bprint(ftable, "#define YYSTYPE int\n");
|
||||
Bprint(ftable, "#endif\n");
|
||||
}
|
||||
Bprint(ftable, "YYSTYPE yylval;\n");
|
||||
Bprint(ftable, "YYSTYPE yyval;\n");
|
||||
|
||||
if(!yyarg){
|
||||
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;
|
||||
|
||||
/* added production */
|
||||
|
@ -1508,7 +1533,9 @@ setup(int argc, char *argv[])
|
|||
|
||||
finact();
|
||||
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)
|
||||
Bputrune(ftable, c);
|
||||
}
|
||||
|
@ -1625,7 +1652,7 @@ defout(int last)
|
|||
}
|
||||
ndefout = ntokens+1;
|
||||
if(last && fdebug) {
|
||||
Bprint(fdebug, "char* yytoknames[] =\n{\n");
|
||||
Bprint(fdebug, "static char* yytoknames[] =\n{\n");
|
||||
TLOOP(i) {
|
||||
if(tokset[i].name) {
|
||||
chcopy(sar, tokset[i].name);
|
||||
|
@ -1844,7 +1871,9 @@ cpyunion(void)
|
|||
long c;
|
||||
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 ");
|
||||
if(fdefine != 0)
|
||||
Bprint(fdefine, "\ntypedef union ");
|
||||
|
@ -1869,8 +1898,11 @@ cpyunion(void)
|
|||
/* we are finished copying */
|
||||
if(level == 0) {
|
||||
Bprint(ftable, " YYSTYPE;\n");
|
||||
if(fdefine != 0)
|
||||
Bprint(fdefine, "\tYYSTYPE;\nextern\tYYSTYPE\tyylval;\n");
|
||||
if(fdefine != 0){
|
||||
Bprint(fdefine, "\tYYSTYPE;\n");
|
||||
if(!yyarg)
|
||||
Bprint(fdefine, "extern\tYYSTYPE\tyylval;\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1883,7 +1915,6 @@ cpyunion(void)
|
|||
void
|
||||
cpycode(void)
|
||||
{
|
||||
|
||||
long c;
|
||||
|
||||
c = Bgetrune(finput);
|
||||
|
@ -1891,7 +1922,9 @@ cpycode(void)
|
|||
c = Bgetrune(finput);
|
||||
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) {
|
||||
if(c == '\\') {
|
||||
if((c=Bgetrune(finput)) == '}')
|
||||
|
@ -1947,7 +1980,9 @@ cpyact(int offset)
|
|||
long c;
|
||||
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;
|
||||
|
||||
loop:
|
||||
|
@ -2157,9 +2192,9 @@ output(void)
|
|||
int i, k, c;
|
||||
Wset *u, *v;
|
||||
|
||||
Bprint(ftable, "short yyexca[] =\n{");
|
||||
Bprint(ftable, "static\tconst\tshort yyexca[] =\n{");
|
||||
if(fdebug)
|
||||
Bprint(fdebug, "char* yystates[] =\n{\n");
|
||||
Bprint(fdebug, "static\tconst\tchar* yystates[] =\n{\n");
|
||||
|
||||
/* output the stuff for state i */
|
||||
SLOOP(i) {
|
||||
|
@ -2570,7 +2605,7 @@ warray(char *s, int *v, int n)
|
|||
{
|
||||
int i;
|
||||
|
||||
Bprint(ftable, "short %s[] =\n{", s);
|
||||
Bprint(ftable, "static\tconst\tshort %s[] =\n{", s);
|
||||
for(i=0;;) {
|
||||
if(i%10 == 0)
|
||||
Bprint(ftable, "\n");
|
||||
|
@ -2900,7 +2935,7 @@ arout(char *s, int *v, int n)
|
|||
{
|
||||
int i;
|
||||
|
||||
Bprint(ftable, "short %s[] =\n{", s);
|
||||
Bprint(ftable, "static\tconst\tshort %s[] =\n{", s);
|
||||
for(i = 0; i < n;) {
|
||||
if(i%10 == 0)
|
||||
Bprint(ftable, "\n");
|
||||
|
|
Loading…
Reference in a new issue