Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jison global variables

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?

like image 965
Marcelo Camargo Avatar asked May 31 '15 20:05

Marcelo Camargo


1 Answers

As a suggestion for Govind Mantri, instead of using 'chars' in the 'if' you should use a variable than if it is used, for example 'cities'. The same thing happened to me with the concatenation problems, but with that I solved it.

if (!('chars' in yy)) { yy.cities = ["Austin","New_York","Chicago","Las_Vegas"];

=>

if (!('cities' in yy)) { yy.cities = ["Austin","New_York","Chicago","Las_Vegas"];

like image 129
Luis Espino Avatar answered Nov 02 '22 07:11

Luis Espino