I’m new to Menhir and I’m trying to implement a calculator using it. I’m aiming to include negative integers in my calculator as well. But I’m having trouble with one specific kind of expression. My parser fails when it encounters inputs like “43-1” when there is no space between the ‘-’ character and the positive integer operand to it’s right. Myparser.mly
is as follows:
%token EOF
%token LPAREN
%token RPAREN
%token <int> INT
%token PLUS
%token TIMES
%token MINUS
%token DIV
%token MOD
%token POW
%start <Ast.expr> prog
%%
prog:
| e = expr; EOF { e }
;
expr:
| term { $1 }
| expr PLUS term { Ast.Binop(Add, $1, $3) }
| expr MINUS term { Ast.Binop(Sub,$1, $3) }
;
term:
| powterm { $1 }
| expr TIMES powterm { Ast.Binop(Mul, $1, $3) }
| expr DIV powterm { Ast.Binop(Div, $1, $3) }
| expr MOD powterm { Ast.Binop(Mod, $1, $3) }
;
powterm:
| factor { $1 }
| factor POW powterm { Ast.Binop(Pow, $1, $3) }
factor:
| INT { Int($1) }
| MINUS INT { Int(-1 * $2) }
| LPAREN expr RPAREN { $2 }
;
What mistake have I made in my parser? Any help is much appreciated.