javascript - Adding functions to bison/jison calculator language -
javascript - Adding functions to bison/jison calculator language -
i'm trying expand jison calculator example simple functions. i'm rather new parsing , bison/jison, bit of have far:
/* lexical grammar */ %lex %{ var funcs = { pow: function(a, b) { homecoming math.pow(a, b); }, test: function(a) { homecoming a*2; } } %} %% \s+ /* skip whitespace */ [0-9]+("."[0-9]+)?\b homecoming 'number' [a-za-z]+ homecoming 'name' "," homecoming ',' "*" homecoming '*' "(" homecoming '(' ")" homecoming ')' <<eof>> homecoming 'eof' . homecoming 'invalid' /lex %start expressions %% /* language grammar */ expressions : e eof { homecoming $1; } ; expression_list : expression_list ',' e | e ; e : e '*' e {$$ = $1*$3;} | '(' e ')' {$$ = $2;} | number {$$ = number(yytext);} | name '(' expression_list ')' {$$ = funcs[$name]($expression_list);} ;
the problem functions getting 1 argument passed them. example:
test(2) -> 4 pow(2,3) -> null
in fact, if console.log
arguments of pow
, appears b
isn't defined. why isn't parsing whole look list before sending function?
you need action in first production expression_list
. default action copies $1
$$
, means appended value discarded.
javascript parsing bison jison
Comments
Post a Comment