Search code examples
javascriptparsingparser-generatorjison

Adding declarations to JISON


I have here an only slightly modified version of the JISON calculator example:

/* description: Parses end executes mathematical expressions. */

/* lexical grammar */
%lex
%%

\s+                   /* skip whitespace */
[0-9]+("."[0-9]+)?\b  return 'NUMBER'
"*"                   return '*'
"/"                   return '/'
"-"                   return '-'
"+"                   return '+'
"^"                   return '^'
"!"                   return '!'
"%"                   return '%'
"("                   return '('
")"                   return ')'
"PI"                  return 'PI'
"E"                   return 'E'
<<EOF>>               return 'EOF'
.                     return 'INVALID'

/lex

/* operator associations and precedence */

%left '+' '-'
%left '*' '/'
%left '^'
%right '!'
%right '%'
%left UMINUS

%start expressions

%% /* language grammar */

expressions
    : e EOF
        { typeof console !== 'undefined' ? console.log($1) : print($1);
          return $1; }
    ;

e
    : e '+' e
        {$$ = $1+$3;}
    | e '-' e
        {$$ = $1-$3;}
    | e '*' e
        {$$ = $1*$3;}
    | e '/' e
        {$$ = $1/$3;}
    | e '^' e
        {$$ = Math.pow($1, $3);}
    | e '!'
        {{
          $$ = fact($1);
        }}
    | e '%'
        {$$ = $1/100;}
    | '-' e %prec UMINUS
        {$$ = -$2;}
    | '(' e ')'
        {$$ = $2;}
    | NUMBER
        {$$ = Number(yytext);}
    | E
        {$$ = Math.E;}
    | PI
        {$$ = Math.PI;}
    ;

%%
/*why doesn't this work at runtime?
I see other examples defining declarations this way but I must be doing something wrong
I couldn't find a syntactically valid way of putting this declaration anywhere but here,
which is probably the issue*/
function fact(n) {
  var tot=1;
  for(var i=2;i<=n;++i) {
    tot*=i;
  }
  return tot;
}

Note the slight differences in the ! operator's definition. I'm trying to externally define the fact function rather than doing it inline.

As of now, it tells me at runtime fact is not defined. How can I fix this? Also, why does the calculator example use two braces around the factorial definition, {{ /*like so*/ }}?


Solution

  • To call a function defined under your productions (such as fact), you can use the mod-brace notation %{ and %} for multiline semantic actions:

    e
        : e '+' e
    
        ...
    
        | e '!'
            %{
                // the %{ tells jison this is a multi-line js eval statement
                $$ = fact($1);
            %}
        ;
    

    As a final solution, try this:

    /* lexical grammar */
    %lex
    %%
    
    \s+                   /* skip whitespace */
    [0-9]+("."[0-9]+)?\b  return 'NUMBER'
    "*"                   return '*'
    "/"                   return '/'
    "-"                   return '-'
    "+"                   return '+'
    "^"                   return '^'
    "!"                   return '!'
    "%"                   return '%'
    "("                   return '('
    ")"                   return ')'
    "PI"                  return 'PI'
    "E"                   return 'E'
    <<EOF>>               return 'EOF'
    .                     return 'INVALID'
    
    /lex
    
    /* operator associations and precedence */
    
    %left '+' '-'
    %left '*' '/'
    %left '^'
    %right '!'
    %right '%'
    %left UMINUS
    
    %start expressions
    
    %% /* language grammar */
    
    expressions
        : e EOF
            %{
                typeof console !== 'undefined' ? console.log($1) : print($1);
                return $1;
            %}
        ;
    
    e
        : e '+' e
            {$$ = $1+$3;}
        | e '-' e
            {$$ = $1-$3;}
        | e '*' e
            {$$ = $1*$3;}
        | e '/' e
            {$$ = $1/$3;}
        | e '^' e
            {$$ = Math.pow($1, $3);}
        | e '!'
            %{
              $$ = fact($1);
            %}
        | e '%'
            {$$ = $1/100;}
        | '-' e %prec UMINUS
            {$$ = -$2;}
        | '(' e ')'
            {$$ = $2;}
        | NUMBER
            {$$ = Number(yytext);}
        | E
            {$$ = Math.E;}
        | PI
            {$$ = Math.PI;}
        ;
    
    %%
    
    function fact(n) {
      var tot=1;
      for(var i=2;i<=n;++i) {
        tot*=i;
      }
      return tot;
    }