Reputation: 456
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?
Upvotes: 2
Views: 445
Reputation: 349
Try:
%start program
%% /* language grammar */
program
: EOF
{ return []; }
| statements EOF
{ return $1; }
;
statements
: statement
{ $$ = [$1]; }
| statements statement
{ $1.push($2); $$ = $1; }
;
Also, replace the returns with "$$ = "
statement
: assign NL
{ $$ = $1; }
;
assign
: IDENTIFIER ASSIGN expression
{ $$ = ['assign', $1, $3]; }
| IDENTIFIER REACT expression
{ $$ = ['react', $1, $2, $3]; }
;
expression
: NUMBER
{ $$ = $1; }
| IDENTIFIER
{/*add something here like $$ = $1 to keep the original value*/}
;
Upvotes: 2
Reputation: 241861
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 $$
.
Upvotes: 3