Search code examples
javascriptparsingjison

Jison recursion


I'm trying to get a hang of Jison. I'm having a bit of a trouble though. The following parser always returns [], no matter what you give it.

%lex
%%

"data"\s*             return 'DATA'
[A-Za-z][A-Za-z0-9_]* return 'IDENTIFIER'
[0-9]+("."[0-9]+)?\b  return 'NUMBER'
"="                   return 'ASSIGN'
("<-"|"<+-")          return 'REACT'
"+"                   return '+'
"-"                   return '-'
"*"                   return '*'
"/"                   return '/'
"^"                   return '^'
\n+                   return 'NL'
<<EOF>>               return 'EOF'
.                     return 'INVALID'

/lex

%token NL

/* operator associations and precedence */

%left ASSIGN
%left REACT
%left '+' '-'
%left '*' '/'
%left '^'
%left UMINUS

%start program

%% /* language grammar */

program
    :
        {return [];}
    | program statement
        {return $1.concat([$2]);}
    | program statement EOF
        {return $1.concat([$2]);}
    ;

statement
    : assign NL
        {return $1;}
    ;

assign
    : IDENTIFIER ASSIGN expression
        {return ['assign', $1, $3];}
    | IDENTIFIER REACT expression
        {return ['react', $1, $2, $3];}
    ;

expression
    : NUMBER
        {return +$1;}
    | IDENTIFIER
    ;

The problem is obviously in my definition of the non-terminal program. What would be the proper way to declare it?


Solution

  • As Aadit M. Shah points out in a comment, the problem is that you cannot return in a jison grammar action before the parse is complete. If a parser rule executes a return, the parser itself will return. You need to assign the semantic value to $$.