bison-patches
[Top][All Lists]
Advanced

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

[PATCH 01/12] grammar: introduce %empty


From: Akim Demaille
Subject: [PATCH 01/12] grammar: introduce %empty
Date: Sat, 16 Feb 2013 14:55:37 +0100

Provide a means to explicitly denote empty right-hand sides of rules:
instead of

  exp:  { ... }

allow

  exp: %empty { ... }

Make sure that %empty is properly used.

With help from Joel E. Denny and Gabriel Rassoul.
http://lists.gnu.org/archive/html/bison-patches/2013-01/msg00142.html

* src/reader.h, src/reader.c (grammar_current_rule_empty_set): New.
* src/parse-gram.y (%empty): New token.
Use it.
* src/scan-gram.l (%empty): Scan it.
* src/reader.c (grammar_rule_check): Check that %empty is properly used.
* tests/actions.at (Invalid uses of %empty, Valid uses of %empty): New.
---
 THANKS           |  1 +
 src/parse-gram.y |  3 +++
 src/reader.c     | 17 +++++++++++++++
 src/reader.h     |  2 ++
 src/scan-gram.l  |  1 +
 src/symlist.c    |  1 +
 src/symlist.h    |  4 ++++
 tests/actions.at | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 94 insertions(+)

diff --git a/THANKS b/THANKS
index 59e591d..f30602c 100644
--- a/THANKS
+++ b/THANKS
@@ -44,6 +44,7 @@ Fabrice Bauzac            address@hidden
 Florian Krohm             address@hidden
 Frank Heckenbach          address@hidden
 Frans Englich             address@hidden
+Gabriel Rassoul           address@hidden
 Georg Sauthoff            address@hidden
 George Neuner             address@hidden
 Gilles Espinasse          address@hidden
diff --git a/src/parse-gram.y b/src/parse-gram.y
index e889eb5..7ab214a 100644
--- a/src/parse-gram.y
+++ b/src/parse-gram.y
@@ -605,6 +605,7 @@ rhses.1:
 | rhses.1 ";"
 ;
 
+%token PERCENT_EMPTY "%empty";
 rhs:
   /* Nothing.  */
     { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location,
@@ -615,6 +616,8 @@ rhs:
     { grammar_current_rule_action_append ($2, @2, $3, false); }
 | rhs "%?{...}"
     { grammar_current_rule_action_append ($2, @2, NULL, true); }
+| rhs "%empty"
+    { grammar_current_rule_empty_set (@2); }
 | rhs "%prec" symbol
     { grammar_current_rule_prec_set ($3, @3); }
 | rhs "%dprec" INT
diff --git a/src/reader.c b/src/reader.c
index 9ef993c..a83f11c 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -333,6 +333,12 @@ grammar_rule_check (const symbol_list *r)
       }
   }
 
+  /* Check that %empty => empty rule.  */
+  if (r->percent_empty_loc.start.file
+      && r->next && r->next->content.sym)
+    complain (&r->percent_empty_loc, complaint,
+              _("%%empty on non-empty rule"));
+
   /* See comments in grammar_current_rule_prec_set for how POSIX
      mandates this complaint.  It's only for identifiers, so skip
      it for char literals and strings, which are always tokens.  */
@@ -434,6 +440,17 @@ grammar_current_rule_prec_set (symbol *precsym, location 
loc)
   current_rule->ruleprec = precsym;
 }
 
+/* Set %empty for the current rule. */
+
+void
+grammar_current_rule_empty_set (location loc)
+{
+  if (current_rule->percent_empty_loc.start.file)
+    complain (&loc, complaint, _("only one %s allowed per rule"), "%empty");
+  else
+    current_rule->percent_empty_loc = loc;
+}
+
 /* Attach dynamic precedence DPREC to the current rule. */
 
 void
diff --git a/src/reader.h b/src/reader.h
index c15544e..ba6ffe6 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -47,6 +47,8 @@ void grammar_current_rule_begin (symbol *lhs, location loc,
                                  named_ref *lhs_named_ref);
 void grammar_current_rule_end (location loc);
 void grammar_midrule_action (void);
+/* Apply %empty to the current rule.  */
+void grammar_current_rule_empty_set (location loc);
 void grammar_current_rule_prec_set (symbol *precsym, location loc);
 void grammar_current_rule_dprec_set (int dprec, location loc);
 void grammar_current_rule_merge_set (uniqstr name, location loc);
diff --git a/src/scan-gram.l b/src/scan-gram.l
index 2ed6d2f..7ce3b4b 100644
--- a/src/scan-gram.l
+++ b/src/scan-gram.l
@@ -217,6 +217,7 @@ eqopt    ([[:space:]]*=)?
   "%defines"                        return PERCENT_DEFINES;
   "%destructor"                     return PERCENT_DESTRUCTOR;
   "%dprec"                          return PERCENT_DPREC;
+  "%empty"                          return PERCENT_EMPTY;
   "%error-verbose"                  return PERCENT_ERROR_VERBOSE;
   "%expect"                         return PERCENT_EXPECT;
   "%expect-rr"                      return PERCENT_EXPECT_RR;
diff --git a/src/symlist.c b/src/symlist.c
index d43591a..005d808 100644
--- a/src/symlist.c
+++ b/src/symlist.c
@@ -44,6 +44,7 @@ symbol_list_sym_new (symbol *sym, location loc)
 
   /* Members used for LHS only.  */
   res->ruleprec = NULL;
+  res->percent_empty_loc = empty_location;
   code_props_none_init (&res->action_props);
   res->dprec = 0;
   res->merger = 0;
diff --git a/src/symlist.h b/src/symlist.h
index 657efac..9b0f117 100644
--- a/src/symlist.h
+++ b/src/symlist.h
@@ -78,6 +78,10 @@ typedef struct symbol_list
    * each RHS are also stored here.  */
   code_props action_props;
 
+  /* The location of the first %empty for this rule, or \a
+     empty_location.  */
+  location percent_empty_loc;
+
   int dprec;
   int merger;
   location merger_declaration_location;
diff --git a/tests/actions.at b/tests/actions.at
index 9473f6e..473c79f 100644
--- a/tests/actions.at
+++ b/tests/actions.at
@@ -64,6 +64,71 @@ AT_PARSER_CHECK([./input], 0,
 
 AT_CLEANUP
 
+## ------------------------ ##
+## Invalid uses of %empty.  ##
+## ------------------------ ##
+
+AT_SETUP([Invalid uses of %empty])
+
+AT_DATA_GRAMMAR([[one.y]],
+[[%%
+exp:
+  %empty %empty {}
+;
+]])
+
+AT_BISON_CHECK([-fcaret one.y], [1], [],
+[[one.y:11.10-15: error: only one %empty allowed per rule
+   %empty %empty {}
+          ^^^^^^
+]])
+
+AT_DATA_GRAMMAR([[two.y]],
+[[%%
+exp:
+  'a' %empty    {}
+| %empty 'a'    {}
+;
+]])
+
+AT_BISON_CHECK([-fcaret two.y], [1], [],
+[[two.y:11.7-12: error: %empty on non-empty rule
+   'a' %empty    {}
+       ^^^^^^
+two.y:12.3-8: error: %empty on non-empty rule
+ | %empty 'a'    {}
+   ^^^^^^
+]])
+
+AT_CLEANUP
+
+## ---------------------- ##
+## Valid uses of %empty.  ##
+## ---------------------- ##
+
+AT_SETUP([Valid uses of %empty])
+
+AT_BISON_OPTION_PUSHDEFS
+AT_DATA_GRAMMAR([[input.y]],
+[[
+%debug
+%code
+{
+]AT_YYERROR_DECLARE[
+]AT_YYLEX_DECLARE[
+}
+%%
+exp: %empty {}
+%%
+]AT_YYERROR_DEFINE[
+]AT_YYLEX_DEFINE[
+]AT_MAIN_DEFINE[
+]])
+
+AT_FULL_COMPILE([input])
+AT_PARSER_CHECK([./input])
+AT_BISON_OPTION_POPDEFS
+AT_CLEANUP
 
 ## ------------------ ##
 ## Initial location.  ##
-- 
1.8.1.3




reply via email to

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