ocaml menhir - 简单语法的流冲突结束

时间:2014-10-29 20:34:15

标签: ocaml parser-generator menhir

我正在尝试使用(+, - ,*,/运算符)生成一个非常简单的表达式解析器Menhir,但是我得到了流冲突的结束。这是语法:

%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 }

我得到的警告是:

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.

如何防止此警告?

这是menhir生成的.automaton file

1 个答案:

答案 0 :(得分:2)

看起来您需要在第二个子句中添加显式EOF。

 top_expr:
    | EOF
    { None }

    | r = expr EOF
    { Some r }