bug-bison
[Top][All Lists]
Advanced

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

Re: %nterm directive incorrectly accepts character literals and quoted s


From: Akim Demaille
Subject: Re: %nterm directive incorrectly accepts character literals and quoted strings
Date: Tue, 27 Nov 2018 07:18:21 +0100

The following two patches make %nterm safe to use.

commit c4778646608e3aa6680639e1823479b2999d5c6c
Author: Akim Demaille <address@hidden>
Date:   Sun Nov 25 14:01:10 2018 +0100

    %nterm: do not accept numbers nor string alias
    
    Reported by Rici Lake.
    http://lists.gnu.org/archive/html/bug-bison/2018-10/msg00000.html
    
    * src/parse-gram.y (symbol_def): Refuse string aliases and numbers
    for non-terminals.
    (prologue_declaration): Recover from errors ended with ';'.
    * tests/input.at (Invalid %nterm uses): New.

diff --git a/src/parse-gram.y b/src/parse-gram.y
index 2d0fb460..73eda17b 100644
--- a/src/parse-gram.y
+++ b/src/parse-gram.y
@@ -317,6 +317,7 @@ prologue_declaration:
 | "%token-table"                { token_table_flag = true; }
 | "%verbose"                    { report_flag |= report_states; }
 | "%yacc"                       { yacc_flag = true; }
+| error ";"                     { yyerrok; }
 | /*FIXME: Err?  What is this horror doing here? */ ";"
 ;
 
@@ -495,7 +496,7 @@ tag:
 | "<>"  { $$ = uniqstr_new (""); }
 ;
 
-/* One token definition.  */
+/* One symbol (token or nterm depending on current_class) definition.  */
 symbol_def:
   TAG
     {
@@ -509,18 +510,38 @@ symbol_def:
     }
 | id INT
     {
+      if (current_class != token_sym)
+        {
+          gram_error (&@2,
+                      "non-terminals cannot be given an explicit number");
+          YYERROR;
+        }
       symbol_class_set ($1, current_class, @1, true);
       symbol_type_set ($1, current_type, @1);
       symbol_user_token_number_set ($1, $2, @2);
     }
 | id string_as_id
     {
+      if (current_class != token_sym)
+        {
+          gram_error (&@2,
+                      "non-terminals cannot be given a string alias");
+          YYERROR;
+        }
       symbol_class_set ($1, current_class, @1, true);
       symbol_type_set ($1, current_type, @1);
       symbol_make_alias ($1, $2, @$);
     }
 | id INT string_as_id
     {
+      if (current_class != token_sym)
+        {
+          gram_error (&@2,
+                      "non-terminals cannot be given an explicit number");
+          gram_error (&@3,
+                      "non-terminals cannot be given a string alias");
+          YYERROR;
+        }
       symbol_class_set ($1, current_class, @1, true);
       symbol_type_set ($1, current_type, @1);
       symbol_user_token_number_set ($1, $2, @2);
diff --git a/tests/input.at b/tests/input.at
index 175928d0..84fd0944 100644
--- a/tests/input.at
+++ b/tests/input.at
@@ -130,6 +130,48 @@ AT_BISON_CHECK([input.y], [1], [],
 AT_CLEANUP
 
 
+## --------------------- ##
+## Invalid %nterm uses.  ##
+## --------------------- ##
+
+AT_SETUP([Invalid %nterm uses])
+
+AT_DATA([input.y],
+[[%nterm expr "expression";
+%nterm term 123;
+%nterm fact 124 "factor";
+%%
+expr: expr '+' term | term;
+term: term '*' fact | fact;
+fact: '0';
+]])
+
+AT_BISON_CHECK([-fcaret input.y], [1], [],
+[[input.y:1.1-6: warning: deprecated directive, use '%type' [-Wdeprecated]
+ %nterm expr "expression";
+ ^^^^^^
+input.y:1.13-24: error: non-terminals cannot be given a string alias
+ %nterm expr "expression";
+             ^^^^^^^^^^^^
+input.y:2.1-6: warning: deprecated directive, use '%type' [-Wdeprecated]
+ %nterm term 123;
+ ^^^^^^
+input.y:2.13-15: error: non-terminals cannot be given an explicit number
+ %nterm term 123;
+             ^^^
+input.y:3.1-6: warning: deprecated directive, use '%type' [-Wdeprecated]
+ %nterm fact 124 "factor";
+ ^^^^^^
+input.y:3.13-15: error: non-terminals cannot be given an explicit number
+ %nterm fact 124 "factor";
+             ^^^
+input.y:3.17-24: error: non-terminals cannot be given a string alias
+ %nterm fact 124 "factor";
+                 ^^^^^^^^
+]])
+
+AT_CLEANUP
+
 
 ## ------------ ##
 ## Invalid $n.  ##




reply via email to

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