1 year ago
#377218

ibi0tux
LALR Grammar desambiguation for function calls statements (as in Python grammar)
I'm trying to write a simple LALR parser for a custom DSL. My grammar looks like this :
main : stmts
stmts : expr stmts
|
expr : arithmetics
| function_call
| VARNAME
| INT
arithmetics : expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| '(' expr ')'
function_call : VARNAME '(' arg_list ')'
arg_list : expr ',' arg_list
| expr
I can write simple arithmetics and function calls, but I'm facing an ambiguation problem.
For instance a (2)
could be either seen as the function a
called with parameter 2
.
Or it could be seen as two distinct statements : first a
, then the number 2
surrounded by parenthesis. I'm facing shift/reduce conflicts when '(' is encountered as it can be either used in arithmetics operation and to make function calls.
Here are two different ways to produce a (2)
:
stmts -> expr -> function_call -> VARNAME ( arg_list ) -> VARNAME ( expr ) -> a ( 2 )
stmts -> expr stmts -> expr expr -> VARNAME arithmetics -> VARNAME ( expr ) -> a ( 2 )
How can I desambiguate such a grammar ?
I noticed that the Python grammar allows to write such things without ambiguity :
def a(x):
return x+2
a
(5)
(2)
a
a
a
(8)
print(
a
(9)
)
How can I make my grammar change to make it work similarly ? Thanks.
python
parsing
grammar
lalr
0 Answers
Your Answer