title: A notation for empty right-hand sides of rules
body: <p>When writing a ("theoretical") grammar with a rule with an empty right-hand side, one always use a symbol such as ε (or 1) to make this emptiness explicit:</p>
  
  <pre><code>A → ε | a A
  </code></pre>
  
  <p>Such a grammar in Yacc and others would then look like</p>
  
  <pre><code>a: | 'a' a
  </code></pre>
  
  <p>or "worse"</p>
  
  <pre><code>a:       { $$ = new_list(); }
   | a 'a' { $$ = $1; $$-&gt;append($1); }
   ;
  </code></pre>
  
  <p>The fact that in "real world grammars" (Yacc, Bison, etc.) this empty right-hand side part of the rule is not explicitly marked as empty troubles me: it is easy to miss the fact that an rhs is empty, or worse: to forget to insert <code>|</code> and actually use a mid-rule action:</p>
  
  <pre><code>a:       { $$ = new_list(); }
     a 'a' { $$ = $1; $$-&gt;append($1); }
   ;
  </code></pre>
  
  <p>1) I don't know of any tool that provides a means to make empty rhs explicit.  Are there any?</p>
  
  <p>Future versions of Bison might support a dedicated symbol, with errors when used in a non-empty rhs, and warnings when a implicitly empty rhs is left.</p>
  
  <p>2) Do people consider this useful?</p>
  
  <p>3) What would be the notation you'd suggest?</p>
  
  <p>Currently, the candidate is <code>$empty</code>:</p>
  
  <pre><code>a: $empty { $$ = new_list(); }
   | a 'a'  { $$ = $1; $$-&gt;append($1); }
   ;
  </code></pre>
  
  <h1>EDIT</h1>
  
  <p>The chosen syntax is <code>%empty</code>:</p>
  
  <pre><code>a: %empty { $$ = new_list(); }
   | a 'a'  { $$ = $1; $$-&gt;append($1); }
   ;
  </code></pre>
  
  <p>Indeed <code>$empty</code> looks like a pseudo-symbol, such as <code>$accept</code> that Bison generates for the initial rule, or the <code>$@n</code> pseudo-symbols for mid-rule actions, or <code>$eof</code> for, well, end-of-file.  But it's definitely not a symbol, it is precisely the absence of symbols.</p>
  
  <p>On the other hand <code>%</code> clearly denotes a directive (some kind of attribute/metadata), like <code>%pred</code>.</p>
  
  <p>So it's a minor difference of syntax, but it's more consistent with the overall syntax.  Credit goes to Joel E. Denny.</p>