Next: Recursive Rules, Previous: Syntax of Grammar Rules, Up: Grammar Rules [Contents][Index]
A rule is said to be empty if its right-hand side (components) is empty. It means that result in the previous example can match the empty string. As another example, here is how to define an optional semicolon:
semicolon.opt: | ";";
It is easy not to see an empty rule, especially when |
is used. The
%empty
directive allows to make explicit that a rule is empty on
purpose:
semicolon.opt: %empty | ";" ;
Flagging a non-empty rule with %empty
is an error. If run with
-Wempty-rule, bison
will report empty rules without
%empty
. Using %empty
enables this warning, unless
-Wno-empty-rule was specified.
The %empty
directive is a Bison extension, it does not work with
Yacc. To remain compatible with POSIX Yacc, it is customary to write a
comment ‘/* empty */’ in each rule with no components:
semicolon.opt: /* empty */ | ";" ;