Looking to enhance the functionality of the Jison calculator example by introducing some basic functions. As someone new to parsing and bison/jison, here's a glimpse of my progress so far:
/* lexical grammar */
%lex
%{
var funcs = {
pow: function(a, b) { return Math.pow(a, b); },
test: function(a) { return a*2; }
}
%}
%%
\s+ /* skipping whitespace */
[0-9]+("."[0-9]+)?\b return 'NUMBER'
[a-zA-Z]+ return 'NAME'
"," return ','
"*" return '*'
"(" return '('
")" return ')'
<<EOF>> return 'EOF'
. return 'INVALID'
/lex
%start expressions
%% /* language grammar */
expressions
: e EOF
{ return $1; }
;
expression_list
: expression_list ',' e
| e
;
e
: e '*' e
{$$ = $1*$3;}
| '(' e ')'
{$$ = $2;}
| NUMBER
{$$ = Number(yytext);}
| NAME '(' expression_list ')'
{$$ = funcs[$NAME]($expression_list);}
;
Encountering an issue where functions are only receiving a single argument. For instance:
test(2) -> 4
pow(2,3) -> null
Upon console.log
of arguments for pow
, it seems that b
is not properly defined. The question remains why the full expression list isn't being parsed before passing it to the function?