0

I am trying to implement a feature in my language where you can execute a statement after returning in a function by doing return someVal then someCode().

I have the following grammar for return statements:

ReturnStatement:
    'RETURN'                                                { $$ = new yy.ReturnStatement(null).pos(@$)}
|   'RETURN' Expression                                     { $$ = new yy.ReturnStatement($2).pos(@$)}
|   'RETURN' Expression 'THEN' Statement  %prec 'AFTER'     { $$ = new yy.ReturnStatement($2, $4).pos(@$)}
;

In the header of the Jison file I have the following precedence rules:

%nonassoc 'NAME'

%right ':'

%nonassoc 'IF' 
%nonassoc 'ENDLN'
%nonassoc 'ELSE'
%nonassoc 'DEF'

%right 'ASSIGN' ''
%left 'YIELD'
%left 'OR'
%left 'AND'
%left '==' '!=' '<' '>' '<=' '>='
%left '+' '-' '&'
%left '*' '//' '%' '/'
%right 'UMIN' 'UPLUS'
%left '^'
%left 'AWAIT'
%right 'TYPEOF'
%right 'UB_FUNC' 'B_FUNC'

%left 'ACCESS'

%nonassoc 'INDEX_LEFT' 'INDEX_RIGHT'

%left 'CALL_LEFT' 'CALL_RIGHT'

%right 'NEW'

%nonassoc 'AFTER'

%%

I am aware that Jison will shift by default when a conflict exists, but I would like to set up precedence rules such that I do not get shift/reduce conflict warnings in Jison. I thougt setting precedence to the last rule over all others would do that, but I get this instead:

Conflict in grammar: multiple actions possible when lookahead token is - in state 53
- reduce by rule: Continuation -> YIELD
- shift token (then go to state 63)
Conflict in grammar: multiple actions possible when lookahead token is + in state 53
- reduce by rule: Continuation -> YIELD
- shift token (then go to state 64)
Conflict in grammar: multiple actions possible when lookahead token is - in state 54
- reduce by rule: Continuation -> AWAIT
- shift token (then go to state 63)
Conflict in grammar: multiple actions possible when lookahead token is + in state 54
- reduce by rule: Continuation -> AWAIT
- shift token (then go to state 64)
Conflict in grammar: multiple actions possible when lookahead token is CALL_LEFT in state 131
- reduce by rule: Expression -> MemberExpression
- shift token (then go to state 91)
Conflict in grammar: multiple actions possible when lookahead token is CALL_LEFT in state 132
- reduce by rule: Expression -> Identifier
- shift token (then go to state 91)
Conflict in grammar: multiple actions possible when lookahead token is % in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 87)
Conflict in grammar: multiple actions possible when lookahead token is // in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 86)
Conflict in grammar: multiple actions possible when lookahead token is / in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 85)
Conflict in grammar: multiple actions possible when lookahead token is * in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 84)
Conflict in grammar: multiple actions possible when lookahead token is - in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 83)
Conflict in grammar: multiple actions possible when lookahead token is + in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 82)
Conflict in grammar: multiple actions possible when lookahead token is & in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 81)
Conflict in grammar: multiple actions possible when lookahead token is >= in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 80)
Conflict in grammar: multiple actions possible when lookahead token is <= in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 79)
Conflict in grammar: multiple actions possible when lookahead token is > in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 78)
Conflict in grammar: multiple actions possible when lookahead token is < in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 77)
Conflict in grammar: multiple actions possible when lookahead token is != in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 76)
Conflict in grammar: multiple actions possible when lookahead token is == in state 134
- reduce by rule: Continuation -> YIELD Expression
- shift token (then go to state 75)
Conflict in grammar: multiple actions possible when lookahead token is % in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 87)
Conflict in grammar: multiple actions possible when lookahead token is // in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 86)
Conflict in grammar: multiple actions possible when lookahead token is / in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 85)
Conflict in grammar: multiple actions possible when lookahead token is * in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 84)
Conflict in grammar: multiple actions possible when lookahead token is - in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 83)
Conflict in grammar: multiple actions possible when lookahead token is + in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 82)
Conflict in grammar: multiple actions possible when lookahead token is & in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 81)
Conflict in grammar: multiple actions possible when lookahead token is >= in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 80)
Conflict in grammar: multiple actions possible when lookahead token is <= in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 79)
Conflict in grammar: multiple actions possible when lookahead token is > in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 78)
Conflict in grammar: multiple actions possible when lookahead token is < in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 77)
Conflict in grammar: multiple actions possible when lookahead token is != in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 76)
Conflict in grammar: multiple actions possible when lookahead token is == in state 135
- reduce by rule: Continuation -> AWAIT Expression
- shift token (then go to state 75)
Conflict in grammar: multiple actions possible when lookahead token is == in state 151
- reduce by rule: UnaryExpression -> - Expression
- shift token (then go to state 75)
Conflict in grammar: multiple actions possible when lookahead token is != in state 151
- reduce by rule: UnaryExpression -> - Expression
- shift token (then go to state 76)
Conflict in grammar: multiple actions possible when lookahead token is < in state 151
- reduce by rule: UnaryExpression -> - Expression
- shift token (then go to state 77)
Conflict in grammar: multiple actions possible when lookahead token is > in state 151
- reduce by rule: UnaryExpression -> - Expression
- shift token (then go to state 78)
Conflict in grammar: multiple actions possible when lookahead token is <= in state 151
- reduce by rule: UnaryExpression -> - Expression
- shift token (then go to state 79)
Conflict in grammar: multiple actions possible when lookahead token is >= in state 151
- reduce by rule: UnaryExpression -> - Expression
- shift token (then go to state 80)
... 

Any help on solving this through precedence rules will be greatly appreciated.

Thank you in advance!

Gabriel Ratener
  • 595
  • 5
  • 20
  • Why do you think the problem is related to the return statement? – rici Oct 18 '15 at 00:33
  • The problem started when I added the last pattern to the return statement rules. – Gabriel Ratener Oct 18 '15 at 01:38
  • Fair enough but it would be easier if we could see enough of the grammar to put the reported conflicts in context. Otherwise it is pure speculation. – rici Oct 18 '15 at 02:01
  • Here is a link to the jison file: https://drive.google.com/file/d/0BynmczlsUDdPYTBmVEJNenhTb2M/view?usp=sharing – Gabriel Ratener Oct 18 '15 at 04:27
  • So, unless I've misread your grammar, you can write `return 0 then return 1;`? What does that mean? Do you really want to have that in your grammar? – Thomas Padron-McCarthy Oct 18 '15 at 08:16
  • The grammar does allow that because what is after the `then` is a statement, however that shouldn't be allowed. I would like to ensure that any statement except a `return x?` is used after the `then` in the grammar, as that could potentially cause issues. However I don't see why that would be the source of the shift reduce conflicts that are occuring. – Gabriel Ratener Oct 18 '15 at 18:41
  • That's a very large grammar and it needs some cleanup. But I really don't think the conflicts are related to the return production. It is possible that adding the return production (or some apparently trivial change you made at the same time) _revealed_ the conflicts by enabling a previously unreachable non-terminal. (Jison doesn't warn you about useless productions and non-terminals.) – rici Oct 19 '15 at 04:01
  • One obvious problem is that `yield` and `await` should have the same precedence. But also `yield -2`: does it yield -2 and then resume ignoring the value sent into the resume? Or does it yield nothing and then subtract 2 from the resumed value? You can hide the s/r conflict with precedence, but can you predict which parse you will get? For a grammar that complicated, I'd suggest explicit productions rather than using precedence. – rici Oct 19 '15 at 04:02
  • I don't want `await` to have the same precedence as `yield` because the argument to an await is always an object which cannot result from operations, that's why it has precedence over all the numerical and logical operators. I hadn't thought of the `yield -2` issue, I will look into that. Thank you for your help! – Gabriel Ratener Oct 19 '15 at 04:47

0 Answers0