soumik
soumik

Reputation: 359

ocaml menhir - end of stream conflict for simple grammar

I am trying out Menhir to generate a very simple expression parser with (+, -, *, / operators), but I get end of stream conflicts. Here's the grammar:

%token <int> INT
%token ADD
%token SUB
%token MUL
%token DIV
%token EOF
%token LPAREN
%token RPAREN

%start <Expr.ast option> top_expr

%%

top_expr:
    | EOF
    { None }

    | r = expr
    { Some r }
    ;

expr:
    | r = term
    { r }

    | l = expr; ADD; r = term
    { Expr.Add (l,r) }

    | l = expr; SUB; r = term
    { Expr.Sub (l,r) }
    ;

term:
    | r = atom
    { r }

    | l = term; MUL; r = atom
    { Expr.Mul (l,r) }

    | l = term; DIV; r = atom
    { Expr.Div (l,r) }
    ;

atom:
    | LPAREN; r = expr; RPAREN
    { r }

    | r = INT
    { Expr.INT r }

The warning I get is:

Warning: 9 states have an end-of-stream conflict.
File "expr_parser.mly", line 18, characters 6-19:
Warning: production top_expr -> expr is never reduced.

How to prevent this warning?

Here's the .automaton file generated by menhir.

Upvotes: 3

Views: 965

Answers (1)

ivg
ivg

Reputation: 35280

Looks like that you need to add explicit EOF to the second clause.

 top_expr:
    | EOF
    { None }

    | r = expr EOF
    { Some r }

Upvotes: 2

Related Questions