Re: [9fans] all you yacc experts

2011-11-15 Thread David Leimbach
Pretty cool!

On Tue, Nov 15, 2011 at 1:10 PM, Anthony Martin  wrote:

> Attached is a modified version of p9p yacc that
> supports the Go grammar.  I'll be sending a
> version of Plan 9 yacc later today.
>
> The following is a description of the changes.
>
>  1. The %error-verbose directive is ignored.
>
>  2. A description of the final grammar is
> printed before the state descriptions
> in y.output.
>
>  3. The 'x' format for character literals is
> now used instead of prefixing with a space.
>
>  4. The YYEMPTY define is now used to clear
> the lookahead token (instead of an explicit
> negative one).
>
>  5. Make yychar and yystate globals so they
> can be inspected by external code.
>
>  5. Support C++ style // comments in actions.
>
>  6. Add a usage message.
>
>  7. Fix a few uses of sprint and strcpy.
>
>
> I've also sent out a changeset to the Go
> development list which adds support for
> using Plan 9 yacc to generate the special
> errors.
>
> One tiny nit is that Plan 9 uses the name
> yytoknames for debugging where Bison uses
> yytname.  I've just used sed for this.
>
> Any questions?
>   Anthony
>


Re: [9fans] all you yacc experts

2011-11-15 Thread Anthony Martin
Attached is a modified version of p9p yacc that
supports the Go grammar.  I'll be sending a
version of Plan 9 yacc later today.

The following is a description of the changes.

  1. The %error-verbose directive is ignored.

  2. A description of the final grammar is
 printed before the state descriptions
 in y.output.

  3. The 'x' format for character literals is
 now used instead of prefixing with a space.

  4. The YYEMPTY define is now used to clear
 the lookahead token (instead of an explicit
 negative one).

  5. Make yychar and yystate globals so they
 can be inspected by external code.

  5. Support C++ style // comments in actions.

  6. Add a usage message.

  7. Fix a few uses of sprint and strcpy.


I've also sent out a changeset to the Go
development list which adds support for
using Plan 9 yacc to generate the special
errors.

One tiny nit is that Plan 9 uses the name
yytoknames for debugging where Bison uses
yytname.  I've just used sed for this.

Any questions?
  Anthony
diff -r 44a7194d00cf lib/yaccpar
--- a/lib/yaccpar   Sat Nov 12 11:52:10 2011 -0800
+++ b/lib/yaccpar   Tue Nov 15 13:10:13 2011 -0800
@@ -2,7 +2,7 @@
 #define YYERRORgoto yyerrlab
 #define YYACCEPT   return(0)
 #define YYABORTreturn(1)
-#defineyyclearin   yychar = -1
+#defineyyclearin   yychar = YYEMPTY
 #defineyyerrok yyerrflag = 0
 
 #ifdef yydebug
@@ -51,6 +51,8 @@
return x;
 }
 
+long yychar;
+
 static long
 #ifdef YYARG
 yylex1(struct Yyarg *yyarg)
@@ -58,7 +60,6 @@
 yylex1(void)
 #endif
 {
-   long yychar;
const long *t3p;
int c;
 
@@ -68,6 +69,7 @@
yychar = yylex();
 #endif
if(yychar <= 0) {
+   yychar = 0;
c = yytok1[0];
goto out;
}
@@ -99,6 +101,8 @@
return c;
 }
 
+int yystate;
+
 int
 #ifdef YYARG
 yyparse(struct Yyarg *yyarg)
@@ -112,8 +116,8 @@
int yys;
} yys[YYMAXDEPTH], *yyp, *yypt;
const short *yyxi;
-   int yyj, yym, yystate, yyn, yyg;
-   long yychar;
+   int yyj, yym, yyn, yyg;
+   long yyc;
 #ifndef YYARG
YYSTYPE save1, save2;
int save3, save4;
@@ -125,7 +129,8 @@
 #endif
 
yystate = 0;
-   yychar = -1;
+   yychar = YYEMPTY;
+   yyc = YYEMPTY;
yynerrs = 0;
yyerrflag = 0;
yyp = &yys[-1];
@@ -151,7 +156,7 @@
 yystack:
/* put a state and value onto the stack */
if(yydebug >= 4)
-   fprint(2, "char %s in %s", yytokname(yychar), 
yystatname(yystate));
+   fprint(2, "char %s in %s", yytokname(yyc), yystatname(yystate));
 
yyp++;
if(yyp >= &yys[YYMAXDEPTH]) {
@@ -165,18 +170,19 @@
yyn = yypact[yystate];
if(yyn <= YYFLAG)
goto yydefault; /* simple state */
-   if(yychar < 0)
+   if(yyc < 0)
 #ifdef YYARG
-   yychar = yylex1(yyarg);
+   yyc = yylex1(yyarg);
 #else
-   yychar = yylex1();
+   yyc = yylex1();
 #endif
-   yyn += yychar;
+   yyn += yyc;
if(yyn < 0 || yyn >= YYLAST)
goto yydefault;
yyn = yyact[yyn];
-   if(yychk[yyn] == yychar) { /* valid shift */
-   yychar = -1;
+   if(yychk[yyn] == yyc) { /* valid shift */
+   yyc = YYEMPTY;
+   yychar = YYEMPTY;
yyval = yylval;
yystate = yyn;
if(yyerrflag > 0)
@@ -188,11 +194,11 @@
/* default state action */
yyn = yydef[yystate];
if(yyn == -2) {
-   if(yychar < 0)
+   if(yyc < 0)
 #ifdef YYARG
-   yychar = yylex1(yyarg);
+   yyc = yylex1(yyarg);
 #else
-   yychar = yylex1();
+   yyc = yylex1();
 #endif
 
/* look through exception table */
@@ -201,21 +207,24 @@
break;
for(yyxi += 2;; yyxi += 2) {
yyn = yyxi[0];
-   if(yyn < 0 || yyn == yychar)
+   if(yyn < 0 || yyn == yyc)
break;
}
yyn = yyxi[1];
-   if(yyn < 0)
+   if(yyn < 0) {
+   yyc = YYEMPTY;
+   yychar = YYEMPTY;
goto ret0;
+   }
}
if(yyn == 0) {
/* error ... attempt to resume parsing */
switch(yyerrflag) {
case 0:   /* brand new error */
yyerror("syntax error");
-   if(yydebug >= 1) {
+   if(yydebug >= 2) {
fprint(2, "%s", yystatname(yystate));
-   fprint(2, "saw %s\n", yytokname(yychar));
+   fprint(2,