compiler construction - Jison global variables -


in previous versions of jison, possible have flex-like feature allowed defining variables accessible in both lexer , 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 features?

although, in latest version of jison, isn't valid. chars, words , lines cannot reached parser context, generating error.

searching more new version, found should possible defining output, on parser's context, inside of %{ ... %}, doesn't work, although used multi-line statements. i'm generating code source target language , i'll prettify code, applying correct indentation, controlled scope , generating directly parser, without building ast.

how global definitions work in jison?

the current version of jison has variable named yy purpose allow sharing of data between lexical actions, semantic actions, , other modules. code sample can work if store variables in yy follows:

%lex %options flex  %{ if (!('chars' in yy)) {   yy.chars = 0;   yy.words = 0;   yy.lines = 1; } %}  %% [^ \t\n\r\f\v]+ { yy.words++; yy.chars += yytext.length; } . { yy.chars++; } \n { yy.chars++; yy.lines++ } /lex  %% e : { console.log( yy.lines + "\t" + yy.words + "\t" + yy.chars); }; 

the above code tested using jison 0.4.13 on jison's try page.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -