Module Name:    src
Committed By:   christos
Date:           Thu Oct 29 21:03:59 UTC 2009

Modified Files:
        src/external/bsd/byacc/dist: defs.h output.c reader.c skeleton.c

Log Message:
Add support for pure parsers yyparse and yylex params, similar to bison.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/external/bsd/byacc/dist/defs.h \
    src/external/bsd/byacc/dist/output.c src/external/bsd/byacc/dist/reader.c
cvs rdiff -u -r1.4 -r1.5 src/external/bsd/byacc/dist/skeleton.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/external/bsd/byacc/dist/defs.h
diff -u src/external/bsd/byacc/dist/defs.h:1.2 src/external/bsd/byacc/dist/defs.h:1.3
--- src/external/bsd/byacc/dist/defs.h:1.2	Wed Oct 28 20:56:19 2009
+++ src/external/bsd/byacc/dist/defs.h	Thu Oct 29 17:03:59 2009
@@ -96,6 +96,9 @@
 #define IDENT 9
 #define EXPECT 10
 #define EXPECT_RR 11
+#define PURE_PARSER 12
+#define PARSE_PARAM 13
+#define LEX_PARAM 14
 
 /*  symbol classes  */
 
@@ -182,6 +185,13 @@
     Value_t shift[1];
 };
 
+typedef struct param param;
+struct param {
+    struct param *next;
+    char *name;
+    char *type;
+};
+
 /*  the structure used to store reductions  */
 
 typedef struct reductions reductions;
@@ -223,6 +233,7 @@
 extern int lineno;
 extern int outline;
 extern int exit_code;
+extern int pure_parser;
 
 extern const char * const banner[];
 extern const char * const tables[];
@@ -307,6 +318,9 @@
 extern Value_t *itemsetend;
 extern unsigned *ruleset;
 
+extern param *lex_param;
+extern param *parse_param;
+
 /* global functions */
 
 extern bucket *lookup(const char *);
@@ -393,7 +407,7 @@
 extern void reader(void);
 
 /* skeleton.c */
-extern void write_section(const char * const section[]);
+extern void write_section(const char * const section[], int);
 
 /* verbose.c */
 extern void verbose(void);
Index: src/external/bsd/byacc/dist/output.c
diff -u src/external/bsd/byacc/dist/output.c:1.2 src/external/bsd/byacc/dist/output.c:1.3
--- src/external/bsd/byacc/dist/output.c:1.2	Wed Oct 28 20:56:20 2009
+++ src/external/bsd/byacc/dist/output.c	Thu Oct 29 17:03:59 2009
@@ -1,10 +1,10 @@
-/*	$NetBSD: output.c,v 1.2 2009/10/29 00:56:20 christos Exp $	*/
+/*	$NetBSD: output.c,v 1.3 2009/10/29 21:03:59 christos Exp $	*/
 /* Id: output.c,v 1.21 2009/10/27 10:55:05 tom Exp */
 
 #include "defs.h"
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: output.c,v 1.2 2009/10/29 00:56:20 christos Exp $");
+__RCSID("$NetBSD: output.c,v 1.3 2009/10/29 21:03:59 christos Exp $");
 
 static int nvectors;
 static int nentries;
@@ -55,6 +55,70 @@
 }
 
 static void
+output_yacc_decl(void)
+{
+    param *p;
+    ++outline;
+    fprintf(code_file, "/* compatibility with bison */\n");
+    ++outline;
+    fprintf(code_file, "#ifdef YYPARSE_PARAM\n");
+    ++outline;
+    fprintf(code_file, "/* compatibility with FreeBSD */\n");
+    ++outline;
+    fprintf(code_file, "# ifdef YYPARSE_PARAM_TYPE\n");
+    ++outline;
+    fprintf(code_file, "#  define YYPARSE_DECL() "
+	"yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
+    ++outline;
+    fprintf(code_file, "# else\n");
+    ++outline;
+    fprintf(code_file, "#  define YYPARSE_DECL() "
+	"yyparse(void *YYPARSE_PARAM)\n");
+    ++outline;
+    fprintf(code_file, "# endif\n");
+    ++outline;
+    fprintf(code_file, "#else\n");
+    ++outline;
+    fprintf(code_file, "# define YYPARSE_DECL() yyparse(");
+    if (!parse_param)
+	fprintf(code_file, "void");
+    else
+	for (p = lex_param; p; p = p->next)
+	    fprintf(code_file, "%s %s%s", p->type, p->name,
+		p->next ? ", " : "");
+    fprintf(code_file, ")\n");
+    outline += 2;
+    fprintf(code_file, "#endif\n\n");
+}
+
+static void
+output_lex_decl(void)
+{
+    param *p;
+    ++outline;
+    fprintf(code_file, "/* Pure parsers. */\n");
+    ++outline;
+    fprintf(code_file, "#define YYPURE %d\n", pure_parser);
+    ++outline;
+    fprintf(code_file, "#ifdef YYLEX_PARAM\n");
+    ++outline;
+    if (pure_parser)
+	fprintf(code_file, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
+    else
+	fprintf(code_file, "# define YYLEX yylex(YYLEX_PARAM)\n");
+    ++outline;
+    fprintf(code_file, "#else\n");
+    if (pure_parser)
+	fprintf(code_file, "# define YYLEX yylex(&yylval, ");
+    else
+	fprintf(code_file, "# define YYLEX yylex(");
+    for (p = lex_param; p; p = p->next)
+	fprintf(code_file, "%s%s", p->name, p->next ? ", " : "");
+    fprintf(code_file, ")\n");
+    outline += 2;
+    fprintf(code_file, "#endif\n\n");
+}
+static void
 output_prefix(void)
 {
     if (symbol_prefix == NULL)
@@ -837,7 +901,8 @@
 	rewind(union_file);
 	while ((c = getc(union_file)) != EOF)
 	    putc(c, defines_file);
-	fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
+	if (!pure_parser)
+	    fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
 		symbol_prefix);
     }
 }
@@ -1210,6 +1275,9 @@
     free_itemsets();
     free_shifts();
     free_reductions();
+    write_section(banner, 0);
+    output_yacc_decl();
+    output_lex_decl();
     output_prefix();
     output_stored_text();
     output_defines();
@@ -1220,12 +1288,12 @@
     output_debug();
     output_stype();
     if (rflag)
-	write_section(tables);
-    write_section(header);
+	write_section(tables, 0);
+    write_section(header, !pure_parser);
     output_trailing_text();
-    write_section(body);
+    write_section(body, pure_parser);
     output_semantic_actions();
-    write_section(trailer);
+    write_section(trailer, 0);
 }
 
 #ifdef NO_LEAKS
Index: src/external/bsd/byacc/dist/reader.c
diff -u src/external/bsd/byacc/dist/reader.c:1.2 src/external/bsd/byacc/dist/reader.c:1.3
--- src/external/bsd/byacc/dist/reader.c:1.2	Wed Oct 28 20:56:20 2009
+++ src/external/bsd/byacc/dist/reader.c	Thu Oct 29 17:03:59 2009
@@ -1,10 +1,10 @@
-/*	$NetBSD: reader.c,v 1.2 2009/10/29 00:56:20 christos Exp $	*/
+/*	$NetBSD: reader.c,v 1.3 2009/10/29 21:03:59 christos Exp $	*/
 /* Id: reader.c,v 1.18 2009/10/27 09:04:07 tom Exp */
 
 #include "defs.h"
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: reader.c,v 1.2 2009/10/29 00:56:20 christos Exp $");
+__RCSID("$NetBSD: reader.c,v 1.3 2009/10/29 21:03:59 christos Exp $");
 
 /*  The line size must be a positive integer.  One hundred was chosen	*/
 /*  because few lines in Yacc input grammars exceed 100 characters.	*/
@@ -45,6 +45,7 @@
 static char *name_pool;
 
 char line_format[] = "#line %d \"%s\"\n";
+int pure_parser;
 
 static void
 cachec(int c)
@@ -278,6 +279,12 @@
 	    return (EXPECT);
 	if (strcmp(cache, "expect-rr") == 0)
 	    return (EXPECT_RR);
+	if (strcmp(cache, "pure-parser") == 0)
+	    return (PURE_PARSER);
+	if (strcmp(cache, "parse-param") == 0)
+	    return (PARSE_PARAM);
+	if (strcmp(cache, "lex-param") == 0)
+	    return (LEX_PARAM);
     }
     else
     {
@@ -299,6 +306,90 @@
     /*NOTREACHED */
 }
 
+struct param *lex_param;
+struct param *parse_param;
+
+/*
+ * Keep a linked list of parameters
+ */
+static void
+copy_param(int k)
+{
+    char *buf;
+    int c;
+    param *head, *p;
+    int i;
+
+    c = nextc();
+    if (c == EOF)
+	unexpected_EOF();
+    if (c != '{')
+	goto out;
+    cptr++;
+
+    c = nextc();
+    if (c == EOF)
+	unexpected_EOF();
+    if (c == '}')
+	goto out;
+
+    buf = MALLOC(linesize);
+    if (buf == NULL)
+	goto nospace;
+
+    for (i = 0; (c = *cptr++) != '}'; i++) {
+	if (c == EOF)
+	    unexpected_EOF();
+	buf[i] = c;
+    }
+
+    if (i == 0)
+	goto out;
+
+    buf[i--] = '\0';
+    while (i >= 0 && isspace((unsigned char)buf[i]))
+	buf[i--] = '\0';
+    while (i >= 0 && isalnum((unsigned char)buf[i]))
+	i--;
+
+    if (!isspace((unsigned char)buf[i]) && buf[i] != '*')
+	goto out;
+
+    p = MALLOC(sizeof(*p));
+    if (p == NULL)
+	goto nospace;
+
+    p->name = strdup(buf + i + 1);
+    if (p->name == NULL)
+	goto nospace;
+
+    buf[i + 1] = '\0';
+    p->type = buf;
+
+    if (k == LEX_PARAM)
+	head = lex_param;
+    else
+	head = parse_param;
+
+    if (head != NULL) {
+	while (head->next)
+	    head = head->next;
+	head->next = p;
+    } else {
+	if (k == LEX_PARAM)
+	    lex_param = p;
+	else
+	    parse_param = p;
+    }
+    p->next = NULL;
+    return;
+	
+out:
+    syntax_error(lineno, line, cptr);
+nospace:
+    no_space();
+}
+
 static void
 copy_ident(void)
 {
@@ -481,9 +572,9 @@
     if (!lflag)
 	fprintf(text_file, line_format, lineno, input_file_name);
 
-    fprintf(text_file, "typedef union");
+    fprintf(text_file, "typedef union YYSTYPE");
     if (dflag)
-	fprintf(union_file, "typedef union");
+	fprintf(union_file, "typedef union YYSTYPE");
 
     depth = 0;
   loop:
@@ -1121,6 +1212,15 @@
 	    copy_union();
 	    break;
 
+	case PURE_PARSER:
+	    pure_parser = 1;
+	    break;
+
+	case LEX_PARAM:
+	case PARSE_PARAM:
+	    copy_param(k);
+	    break;
+
 	case TOKEN:
 	case LEFT:
 	case RIGHT:
@@ -2028,7 +2128,6 @@
 void
 reader(void)
 {
-    write_section(banner);
     create_symbol_table();
     read_declarations();
     read_grammar();

Index: src/external/bsd/byacc/dist/skeleton.c
diff -u src/external/bsd/byacc/dist/skeleton.c:1.4 src/external/bsd/byacc/dist/skeleton.c:1.5
--- src/external/bsd/byacc/dist/skeleton.c:1.4	Wed Oct 28 22:02:04 2009
+++ src/external/bsd/byacc/dist/skeleton.c	Thu Oct 29 17:03:59 2009
@@ -1,10 +1,10 @@
-/*	$NetBSD: skeleton.c,v 1.4 2009/10/29 02:02:04 christos Exp $	*/
+/*	$NetBSD: skeleton.c,v 1.5 2009/10/29 21:03:59 christos Exp $	*/
 /* Id: skeleton.c,v 1.19 2008/12/24 14:52:28 tom Exp */
 
 #include "defs.h"
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: skeleton.c,v 1.4 2009/10/29 02:02:04 christos Exp $");
+__RCSID("$NetBSD: skeleton.c,v 1.5 2009/10/29 21:03:59 christos Exp $");
 
 /*  The definition of yysccsid in the banner should be replaced with	*/
 /*  a #pragma ident directive if the target C compiler supports		*/
@@ -37,25 +37,11 @@
     CONCAT1("#define YYPATCH ", YYPATCH),
 #endif
     "",
-    "#define YYLEX yylex()",
     "#define YYEMPTY        (-1)",
     "#define yyclearin      (yychar = YYEMPTY)",
     "#define yyerrok        (yyerrflag = 0)",
     "#define YYRECOVERING() (yyerrflag != 0)",
     "",
-    "/* compatibility with bison */",
-    "#ifdef YYPARSE_PARAM",
-    "/* compatibility with FreeBSD */",
-    "#ifdef YYPARSE_PARAM_TYPE",
-    "#define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)",
-    "#else",
-    "#define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)",
-    "#endif",
-    "#else",
-    "#define YYPARSE_DECL() yyparse(void)",
-    "#endif /* YYPARSE_PARAM */",
-    "",
-    "static int yygrowstack(void);",
     0
 };
 
@@ -85,6 +71,8 @@
     "#endif",
     "",
     "extern int YYPARSE_DECL();",
+    "static int yygrowstack(short **, short **, short **,",
+    "    YYSTYPE **, YYSTYPE **, unsigned *);",
     "",
     "/* define the initial stack-sizes */",
     "#ifdef YYSTACKSIZE",
@@ -102,58 +90,49 @@
     "#define YYINITSTACKSIZE 500",
     "",
     "int      yydebug;",
-    "int      yynerrs;",
     "int      yyerrflag;",
-    "int      yychar;",
-    "short   *yyssp;",
-    "YYSTYPE *yyvsp;",
-    "YYSTYPE  yyval;",
-    "YYSTYPE  yylval;",
-    "",
-    "/* variables for the parser stack */",
-    "static short   *yyss;",
-    "static short   *yysslim;",
-    "static YYSTYPE *yyvs;",
-    "static unsigned yystacksize;",
+    "\003",
+    "",
     0
 };
 
 const char * const body[] =
 {
     "/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
-    "static int yygrowstack(void)",
+    "static int yygrowstack(short **yyss, short **yyssp, short **yysslim,",
+    "    YYSTYPE **yyvs, YYSTYPE **yyvsp, unsigned *yystacksize)",
     "{",
     "    int i;",
     "    unsigned newsize;",
     "    short *newss;",
     "    YYSTYPE *newvs;",
     "",
-    "    if ((newsize = yystacksize) == 0)",
+    "    if ((newsize = *yystacksize) == 0)",
     "        newsize = YYINITSTACKSIZE;",
     "    else if (newsize >= YYMAXDEPTH)",
     "        return -1;",
     "    else if ((newsize *= 2) > YYMAXDEPTH)",
     "        newsize = YYMAXDEPTH;",
     "",
-    "    i = yyssp - yyss;",
-    "    newss = (yyss != 0)",
-    "          ? (short *)realloc(yyss, newsize * sizeof(*newss))",
+    "    i = *yyssp - *yyss;",
+    "    newss = (*yyss != 0)",
+    "          ? (short *)realloc(*yyss, newsize * sizeof(*newss))",
     "          : (short *)malloc(newsize * sizeof(*newss));",
     "    if (newss == 0)",
     "        return -1;",
     "",
-    "    yyss  = newss;",
-    "    yyssp = newss + i;",
+    "    *yyss  = newss;",
+    "    *yyssp = newss + i;",
     "    newvs = (yyvs != 0)",
-    "          ? (YYSTYPE *)realloc(yyvs, newsize * sizeof(*newvs))",
+    "          ? (YYSTYPE *)realloc(*yyvs, newsize * sizeof(*newvs))",
     "          : (YYSTYPE *)malloc(newsize * sizeof(*newvs));",
     "    if (newvs == 0)",
     "        return -1;",
     "",
-    "    yyvs = newvs;",
-    "    yyvsp = newvs + i;",
-    "    yystacksize = newsize;",
-    "    yysslim = yyss + newsize - 1;",
+    "    *yyvs = newvs;",
+    "    *yyvsp = newvs + i;",
+    "    *yystacksize = newsize;",
+    "    *yysslim = *yyss + newsize - 1;",
     "    return 0;",
     "}",
     "",
@@ -166,6 +145,15 @@
     "YYPARSE_DECL()",
     "{",
     "    int yym, yyn, yystate;",
+    "\003",
+    "    YYSTYPE  yyval;",
+    "    /* variables for the parser stack */",
+    "    short   *yyssp;",
+    "    short   *yyss;",
+    "    short   *yysslim;",
+    "    YYSTYPE *yyvs;",
+    "    YYSTYPE *yyvsp;",
+    "    unsigned yystacksize;",
     "#if YYDEBUG",
     "    const char *yys;",
     "",
@@ -182,7 +170,11 @@
     "    yychar = YYEMPTY;",
     "    yystate = 0;",
     "",
-    "    if (yyss == NULL && yygrowstack()) goto yyoverflow;",
+    "    yystacksize = 0;",
+    "    yyvs = NULL;",
+    "    yyss = NULL;",
+    "    if (yygrowstack(&yyss, &yyssp, &yysslim, &yyvs, &yyvsp, &yystacksize))",
+    "        goto yyoverflow;",
     "    yyssp = yyss;",
     "    yyvsp = yyvs;",
     "    yystate = 0;",
@@ -192,7 +184,7 @@
     "    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;",
     "    if (yychar < 0)",
     "    {",
-    "        if ((yychar = yylex()) < 0) yychar = 0;",
+    "        if ((yychar = yylex(\002)) < 0) yychar = 0;",
     "#if YYDEBUG",
     "        if (yydebug)",
     "        {",
@@ -212,7 +204,8 @@
     "            printf(\"%sdebug: state %d, shifting to state %d\\n\",",
     "                    YYPREFIX, yystate, yytable[yyn]);",
     "#endif",
-    "        if (yyssp >= yysslim && yygrowstack())",
+    "        if (yyssp >= yysslim && yygrowstack(&yyss, &yyssp, &yysslim,",
+    "            &yyvs, &yyvsp, &yystacksize))",
     "        {",
     "            goto yyoverflow;",
     "        }",
@@ -231,7 +224,7 @@
     "    }",
     "    if (yyerrflag) goto yyinrecovery;",
     "",
-    "    yyerror(\"syntax error\");",
+    "    yyerror(\001\"syntax error\");",
     "",
     "    goto yyerrlab;",
     "",
@@ -252,7 +245,8 @@
     "                    printf(\"%sdebug: state %d, error recovery shifting\\",
     " to state %d\\n\", YYPREFIX, *yyssp, yytable[yyn]);",
     "#endif",
-    "                if (yyssp >= yysslim && yygrowstack())",
+    "                if (yyssp >= yysslim && yygrowstack(&yyss, &yyssp,",
+    "                    &yysslim, &yyvs, &yyvsp, &yystacksize))",
     "                {",
     "                    goto yyoverflow;",
     "                }",
@@ -328,7 +322,7 @@
     "        *++yyvsp = yyval;",
     "        if (yychar < 0)",
     "        {",
-    "            if ((yychar = yylex()) < 0) yychar = 0;",
+    "            if ((yychar = yylex(\002)) < 0) yychar = 0;",
     "#if YYDEBUG",
     "            if (yydebug)",
     "            {",
@@ -353,7 +347,8 @@
     "        printf(\"%sdebug: after reduction, shifting from state %d \\",
     "to state %d\\n\", YYPREFIX, *yyssp, yystate);",
     "#endif",
-    "    if (yyssp >= yysslim && yygrowstack())",
+    "    if (yyssp >= yysslim && yygrowstack(&yyss, &yyssp,",
+    "        &yysslim, &yyvs, &yyvsp, &yystacksize))",
     "    {",
     "        goto yyoverflow;",
     "    }",
@@ -362,23 +357,29 @@
     "    goto yyloop;",
     "",
     "yyoverflow:",
-    "    yyerror(\"yacc stack overflow\");",
+    "    yyerror(\001\"yacc stack overflow\");",
     "",
     "yyabort:",
+    "	 free(yyss);",
+    "	 free(yyvs);",
     "    return (1);",
     "",
     "yyaccept:",
+    "	 free(yyss);",
+    "	 free(yyvs);",
     "    return (0);",
     "}",
     0
 };
 
 void
-write_section(const char * const section[])
+write_section(const char * const section[], int dodecls)
 {
     int c;
     int i;
     const char *s;
+    const char *comma;
+    param *p;
     FILE *f;
 
     f = code_file;
@@ -387,7 +388,39 @@
 	++outline;
 	while ((c = *s) != 0)
 	{
-	    putc(c, f);
+	    switch (c) {
+	    case '\001':
+		p = parse_param;
+		for (; p != NULL; p = p->next)
+		    fprintf(f, "%s, ", p->name);
+		break;
+
+	    case '\002':
+		p = lex_param;
+		if (pure_parser) {
+		    fprintf(f, "&yylval");
+		    comma = ", ";
+		} else 
+		    comma = "";
+		for (; p != NULL; p = p->next) {
+		    fprintf(f, "%s%s", comma, p->name);
+		    comma = ", ";
+		}
+		break;
+
+	    case '\003':
+		if (!dodecls)
+		    break;
+		fprintf(f, 
+		    "    int      yynerrs;\n"
+		    "    int      yychar;\n"
+		    "    YYSTYPE  yylval;");
+		break;
+
+	    default:
+		putc(c, f);
+		break;
+	    }
 	    ++s;
 	}
 	putc('\n', f);

Reply via email to