I've been stuck for hours trying to solve what seems like a simple problem but I just can't figure it out :/
I'm working on defining a small javascript-like language in jison. The issue I'm facing is that both the Parameter rule and the AssignStatement rule can start with a CHAR_SEQUENCE, but it always selects the parameter rule. For example, even when the code is a = 5;
, it triggers the throw "reached parameter";
so it appears to interpret the a
as a Parameter instead of a = 5;
as an AssignStatement
Here's the relevant part of my grammar:
Parameter
: InlineVariable
{ $$ = $1; }
| CHAR_SEQUENCE
{ throw "reached parameter"; checkUndefined($1, @1); $$ = vars[$1]; }
;
InlineVariable
: NUMBER
{ $$ = new Runtime.Integer(parseInt($1)); }
| '"' CHAR_SEQUENCE '"'
{ $$ = new Runtime.String($2); }
| FUNCTION '(' ParameterList ')' Statement
{ $$ = new Container($5); }
;
AssignStatement
: CHAR_SEQUENCE AssignmentOperator Parameter ';'
{
$$ = function()
{
if((typeof vars[$1] == 'undefined' && $3 instanceof Runtime.Integer) || (vars[$1] instanceof Container && $3 instanceof Runtime.Integer))
vars[$1] = new Runtime.Integer(0, $1);
else if(typeof vars[$1] == 'undefined' || (vars[$1] instanceof Container && !($3 instanceof Container)))
vars[$1] = $3.constructor.call();
$2(vars[$1], vars[$3]);
}
}
| CHAR_SEQUENCE SingleAssignmentOperator ';'
{
$$ = function()
{
if(typeof vars[$1] == 'undefined')
vars[$1] = new Runtime.Integer(0, $1);
$2(vars[$1]);
};
}
;
You can find the full grammar at https://gist.github.com/M4GNV5/36c2550946c1a1f6ec91
Is there a solution to this problem? I've already tried using %left, %right, %assoc, and %precedence but it didn't work (or maybe I did something wrong?