$n with n zero or negative is allowed for reference to tokens and groupings on the stack before those that match the current rule.
This is a very risky practice, and to use it reliably you must be certain of the context in which the rule is applied. Here is a case in which you can use this reliably:
foo:
expr bar '+' expr { … }
| expr bar '-' expr { … }
;
bar:
%empty { previous_expr = $0; }
;
As long as bar is used only in the fashion shown here, $0 always refers to the expr which precedes bar in the definition of foo.
As bison/jison/jison-gho support location references as well, format @n or @label, the above implies @-1 is a legal location ref.
bison+jison-gho support
$n
for n <= 0:Quoting the bison manual at https://www.gnu.org/software/bison/manual/html_node/Actions.html (bold emphasis mine):
As bison/jison/jison-gho support location references as well, format
@n
or@label
, the above implies@-1
is a legal location ref.That FAILS since 0.6.0-??? when we switched to parsing / validating action code chunks via a patched recast library: https://github.com/GerHobbelt/recast :: https://www.npmjs.com/package/@gerhobbelt/recast which recognizes all jison-gho supported reference types:
$n
,@n
,#n
,##n
,#label#
... but still barfs on@-1
.Note
Given this, I expect my patched recast/esprima also barfs on
#-1
and##-1
...