My language allows anonymous function declaration like (x: int):int => { return x * x - x }
. To call this simply write (expression)
. But I also have to enclose an expression within parentheses for precedence, for example x*(y+z)
.
Now bison raises shift/reduce conflict for anonymous function-call with single argument.
Here is a part of the grammar section:
expr: ID %prec LOW
| ID '=' expr %prec LOW
| ID '(' ')'
| ID '(' call_args ')'
| '(' call_args ')' // anonymous function call
| '(' ')' // anonymous function call without arg
| '(' expr ')' // expression enclosed, like '(y+z)' in 'x*(y+z)'
| expr op_arithematic expr %prec HIGH
| expr op_comparison expr %prec HIGH
| numeric
call_args: expr
| call_args ',' expr
The conflicted output is:
State 31
31 expr: '(' expr . ')'
32 | expr . op_arithematic expr
33 | expr . op_comparison expr
37 call_args: expr .
...
...
...
')' shift, and go to state 61
')' [reduce using rule 37 (call_args)]
$default reduce using rule 37 (call_args)
I have also added %prec
with no luck:
expr: ID %prec LOW
| ID '=' expr %prec LOW
| ID '(' ')'
| ID '(' call_args ')'
| '(' call_args ')' %prec HIGH // anonymous function call
| '(' ')' // anonymous function call without arg
| '(' expr ')' %prec LOW // expression enclosed, like '(y+z)' in 'x*(y+z)'
| expr op_arithematic expr %prec HIGH
| expr op_comparison expr %prec HIGH
| numeric
call_args: expr
| call_args ',' expr
It is painful to me to allow calling an anonymous function like this, but I cannot avoid it's implementation because, it is my lab-assignment. I found no way to resolve this issue.