bison-patches
[Top][All Lists]
Advanced

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

Purity of yyerror etc.


From: Paul Eggert
Subject: Purity of yyerror etc.
Date: Fri, 29 Nov 2002 00:57:04 -0800 (PST)

> From: Akim Demaille <address@hidden>
> Date: 04 Nov 2002 08:29:53 +0100

> | This syntax avoids the need for duplicating the declared identifier
> | ("ast" in this case), and it makes the %parse-param directive easier
> | to explain and read.

> Yes, that's sure.  Well, if you really prefer the lighter syntax,
> let's go for it.

OK, I finally got around to it, by installing this patch.

As a side effect, this patch removes the "," token (since it's no
longer needed), and is compatible with the byacc behavior of treating
"," as white space.  I am becoming more and more of a fan of being
compatible with Yacc, as I am being bitten by incompatibilities
in practice (the latest being with Mawk; the Mawk grammar isn't
acceptable to Bison, but that's a different matter).

2002-11-29  Paul Eggert  <address@hidden>

        %parse-param and %lex-param now take just one argument, the
        declaration; the argument name is deduced from the declaration.

        * doc/bison.texinfo (Parser Function, Pure Calling, Error
        Reporting, Table of Symbols): Document this.
        * src/parse-gram.y (add_param): New function.
        (COMMA): Remove.
        (declaration): Implement new rule for %parse-param and %lex-param.
        * src/scan-gram.l: "," now elicits a warning, rather than being
        a token; this is more compatible with byacc.
        * tests/calc.at (Simple LALR Calculator): Adopt new convention.

Index: doc/bison.texinfo
===================================================================
RCS file: /cvsroot/bison/bison/doc/bison.texinfo,v
retrieving revision 1.86
diff -p -u -r1.86 bison.texinfo
--- doc/bison.texinfo   27 Nov 2002 18:32:12 -0000      1.86
+++ doc/bison.texinfo   29 Nov 2002 08:30:42 -0000
@@ -3847,19 +3847,20 @@ If you use a reentrant parser, you can o
 parameter information to it in a reentrant way.  To do so, use the
 declaration @code{%parse-param}:
 
address@hidden {Directive} %parse-param @address@hidden@}, @address@hidden@}
address@hidden {Directive} %parse-param @address@hidden@}
 @findex %parse-param
-Declare that @code{argument-name} is an additional @code{yyparse}
-argument.  This argument is also passed to @code{yyerror}.  The
address@hidden is used when declaring functions or
-prototypes.
+Declare that an argument declared by @code{argument-declaration} is an
+additional @code{yyparse} argument.  This argument is also passed to
address@hidden  The @var{argument-declaration} is used when declaring
+functions or prototypes.  The last identifier in
address@hidden must be the argument name.
 @end deffn
 
 Here's an example.  Write this in the parser:
 
 @example
-%parse-param @{int address@hidden,  @address@hidden
-%parse-param @{int address@hidden, @address@hidden
+%parse-param @{int address@hidden
+%parse-param @{int address@hidden
 @end example
 
 @noindent
@@ -4090,18 +4091,18 @@ If you wish to pass the additional param
 @code{%lex-param} just like @code{%parse-param} (@pxref{Parser
 Function}).
 
address@hidden {Directive} lex-param @address@hidden@}, @address@hidden@}
address@hidden {Directive} lex-param @address@hidden@}
 @findex %lex-param
-Declare that @code{argument-name} is an additional @code{yylex}
-argument.
+Declare that @code{argument-declaration} is an additional @code{yylex}
+argument declaration.
 @end deffn
 
 For instance:
 
 @example
-%parse-param @{int address@hidden,  @address@hidden
-%lex-param   @{int address@hidden,  @address@hidden
-%parse-param @{int address@hidden, @address@hidden
+%parse-param @{int address@hidden
+%lex-param   @{int address@hidden
+%parse-param @{int address@hidden
 @end example
 
 @noindent
@@ -4190,7 +4191,7 @@ void yyerror (const char *msg);         
 void yyerror (YYLTYPE *locp, const char *msg);  /* GLR parsers.   */
 @end example
 
-If @samp{%parse-param @{int address@hidden, @address@hidden is used, then:
+If @samp{%parse-param @{int address@hidden is used, then:
 
 @example
 void yyerror (int *randomness, const char *msg);  /* Yacc parsers.  */
@@ -4207,10 +4208,10 @@ convention of @code{yylex} @emph{and} th
 %locations
 /* Pure yylex.  */
 %pure-parser
-%lex-param   @{int address@hidden,  @address@hidden
+%lex-param   @{int address@hidden
 /* Pure yyparse.  */
-%parse-param @{int address@hidden,  @address@hidden
-%parse-param @{int address@hidden, @address@hidden
+%parse-param @{int address@hidden
+%parse-param @{int address@hidden
 @end example
 
 @noindent
@@ -6498,7 +6499,7 @@ Bison declaration to assign left associa
 @xref{Precedence Decl, ,Operator Precedence}.
 @end deffn
 
address@hidden {Directive} %lex-param @address@hidden@}. @address@hidden"@}
address@hidden {Directive} %lex-param @address@hidden@}
 Bison declaration to specifying an additional parameter that
 @code{yylex} should accept.  @xref{Pure Calling,, Calling Conventions
 for Pure Parsers}.
@@ -6530,7 +6531,7 @@ Bison declaration to set the name of the
 Summary}.
 @end deffn
 
address@hidden {Directive} %parse-param @address@hidden@}, @address@hidden@}
address@hidden {Directive} %parse-param @address@hidden@}
 Bison declaration to specifying an additional parameter that
 @code{yyparse} should accept.  @xref{Parser Function,, The Parser
 Function @code{yyparse}}.
Index: src/parse-gram.y
===================================================================
RCS file: /cvsroot/bison/bison/src/parse-gram.y,v
retrieving revision 1.31
diff -p -u -r1.31 parse-gram.y
--- src/parse-gram.y    27 Nov 2002 18:33:18 -0000      1.31
+++ src/parse-gram.y    29 Nov 2002 08:30:43 -0000
@@ -67,6 +67,8 @@ do {                                                  \
        print_token_value (File, Type, &Value)
 static void print_token_value (FILE *file, int type, YYSTYPE const *value);
 
+static void add_param (char const *, char const *, location_t);
+
 symbol_class current_class = unknown_sym;
 struniq_t current_type = 0;
 symbol_t *current_lhs;
@@ -141,7 +143,6 @@ braced_code_t current_braced_code = acti
 %token EQUAL           "="
 %token SEMICOLON       ";"
 %token COLON           ":"
-%token COMMA           ","
 %token PIPE            "|"
 %token ID              "identifier"
 %token PERCENT_PERCENT "%%"
@@ -184,20 +185,12 @@ declaration:
 | "%expect" INT                            { expected_conflicts = $2; }
 | "%file-prefix" "=" string_content        { spec_file_prefix = $3; }
 | "%glr-parser"                           { glr_parser = 1; }
-| "%lex-param" code_content "," code_content
-                           {
-                            muscle_pair_list_grow ("lex_param", $2, $4);
-                            scanner_last_string_free ();
-                          }
+| "%lex-param" code_content               { add_param ("lex_param", $2, @2); }
 | "%locations"                             { locations_flag = 1; }
 | "%name-prefix" "=" string_content        { spec_name_prefix = $3; }
 | "%no-lines"                              { no_lines_flag = 1; }
 | "%output" "=" string_content             { spec_outfile = $3; }
-| "%parse-param" code_content "," code_content
-                           {
-                            muscle_pair_list_grow ("parse_param", $2, $4);
-                            scanner_last_string_free ();
-                          }
+| "%parse-param" code_content          { add_param ("parse_param", $2, @2); }
 | "%pure-parser"                           { pure_parser = 1; }
 | "%skeleton" string_content               { skeleton = $2; }
 | "%token-table"                           { token_table_flag = 1; }
@@ -435,6 +428,44 @@ semi_colon.opt:
 | ";"
 ;
 %%
+static void
+add_param (char const *type, char const *decl, location_t loc)
+{
+  static char const alphanum[] =
+    "0123456789"
+    "abcdefghijklmnopqrstuvwxyz"
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+    "_";
+  char const *alpha = alphanum + 10;
+  char const *name_start = NULL;
+  char const *p;
+
+  for (p = decl; *p; p++)
+    if ((p == decl || ! strchr (alphanum, p[-1])) && strchr (alpha, p[0]))
+      name_start = p;
+
+  if (! name_start)
+    complain_at (loc, _("missing identifier in parameter declaration"));
+  else
+    {
+      char *name;
+      size_t name_len;
+
+      for (name_len = 1;
+          name_start[name_len] && strchr (alphanum, name_start[name_len]);
+          name_len++)
+       continue;
+
+      name = xmalloc (name_len + 1);
+      memcpy (name, name_start, name_len);
+      name[name_len] = '\0';
+      muscle_pair_list_grow (type, decl, name);
+      free (name);
+    }
+
+  scanner_last_string_free ();
+}
+
 /*------------------------------------------------------------------.
 | When debugging the parser, display tokens' locations and values.  |
 `------------------------------------------------------------------*/
Index: src/scan-gram.l
===================================================================
RCS file: /cvsroot/bison/bison/src/scan-gram.l,v
retrieving revision 1.44
diff -p -u -r1.44 scan-gram.l
--- src/scan-gram.l     27 Nov 2002 18:34:14 -0000      1.44
+++ src/scan-gram.l     29 Nov 2002 08:30:43 -0000
@@ -266,10 +266,14 @@ splice     (\\[ \f\t\v]*\n)*
   "="                     return EQUAL;
   ":"                     rule_length = 0; return COLON;
   "|"                     rule_length = 0; return PIPE;
-  ","                     return COMMA;
   ";"                     return SEMICOLON;
 
   [ \f\n\t\v]  STEP;
+
+  "," {
+    warn_at (*loc, _("stray `,' treated as white space"));
+    STEP;
+  }
 
   {id}        {
     val->symbol = symbol_get (yytext, *loc);
Index: tests/calc.at
===================================================================
RCS file: /cvsroot/bison/bison/tests/calc.at,v
retrieving revision 1.47
diff -p -u -r1.47 calc.at
--- tests/calc.at       21 Nov 2002 05:28:09 -0000      1.47
+++ tests/calc.at       29 Nov 2002 08:30:43 -0000
@@ -562,7 +562,7 @@ AT_CHECK_CALC_LALR([%error-verbose %debu
 
 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines 
%name-prefix="calc" %verbose %yacc])
 
-AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines 
%name-prefix="calc" %verbose %yacc %parse-param {value_t *result}, {result} 
%parse-param {int *count}, {count}])
+AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines 
%name-prefix="calc" %verbose %yacc %parse-param {value_t *result} %parse-param 
{int *count}])
 
 
 # ----------------------- #
@@ -597,4 +597,4 @@ AT_CHECK_CALC_GLR([%error-verbose %debug
 
 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines 
%name-prefix="calc" %verbose %yacc])
 
-AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines 
%name-prefix="calc" %verbose %yacc %parse-param {value_t *result}, {result} 
%parse-param {int *count}, {count}])
+AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines 
%name-prefix="calc" %verbose %yacc %parse-param {value_t *result} %parse-param 
{int *count}])





reply via email to

[Prev in Thread] Current Thread [Next in Thread]