Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/byacc/dist Add support for pure parsers yyparse...



details:   https://anonhg.NetBSD.org/src/rev/0a79ac0f5d15
branches:  trunk
changeset: 748648:0a79ac0f5d15
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Oct 29 21:03:59 2009 +0000

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

diffstat:

 external/bsd/byacc/dist/defs.h     |   16 ++++-
 external/bsd/byacc/dist/output.c   |   82 +++++++++++++++++++++-
 external/bsd/byacc/dist/reader.c   |  109 +++++++++++++++++++++++++++++-
 external/bsd/byacc/dist/skeleton.c |  131 +++++++++++++++++++++++-------------
 4 files changed, 276 insertions(+), 62 deletions(-)

diffs (truncated from 625 to 300 lines):

diff -r 8ee485b78e39 -r 0a79ac0f5d15 external/bsd/byacc/dist/defs.h
--- a/external/bsd/byacc/dist/defs.h    Thu Oct 29 18:20:11 2009 +0000
+++ b/external/bsd/byacc/dist/defs.h    Thu Oct 29 21:03:59 2009 +0000
@@ -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);
diff -r 8ee485b78e39 -r 0a79ac0f5d15 external/bsd/byacc/dist/output.c
--- a/external/bsd/byacc/dist/output.c  Thu Oct 29 18:20:11 2009 +0000
+++ b/external/bsd/byacc/dist/output.c  Thu Oct 29 21:03:59 2009 +0000
@@ -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
diff -r 8ee485b78e39 -r 0a79ac0f5d15 external/bsd/byacc/dist/reader.c
--- a/external/bsd/byacc/dist/reader.c  Thu Oct 29 18:20:11 2009 +0000
+++ b/external/bsd/byacc/dist/reader.c  Thu Oct 29 21:03:59 2009 +0000
@@ -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:



Home | Main Index | Thread Index | Old Index