In previous versions of Jison, it was possible to have a Flex-like feature that allowed defining variables accessible in both the lexer and parser contexts, such as:
%{
var chars = 0;
var words = 0;
var lines = 0;
%}
%lex
%options flex
%%
\s
[^ \t\n\r\f\v]+ { words++; chars+= yytext.length; }
. { chars++; }
\n { chars++; lines++ }
/lex
%%
E : { console.log(lines + "\t" + words + "\t" + chars) ; };
Ref.: Flex like features?
Although, in the latest version of Jison, this isn't valid. chars
, words
and lines
cannot be reached from the parser context, generating an error.
Searching more about the new version, I found that it should be possible by defining output, on parser's context, inside of %{ ... %}
, but it doesn't work, although it is used for multi-line statements. I'm generating code from a source to a target language and I'll prettify this code, applying the correct indentation, controlled by the scope and generating directly from parser, without building an AST.
How do global definitions currently work in Jison?
$x
can store all the value across all E. jsfiddle.net/Lnukko75/1 – Readable