parsingocamlmenhirmerlin

Partial parsing and recovery of Menhir


There is a very small calculator in Sedlex and Menhir. Now, I would like to make the calculator to be able to parse expressions like 1+. So I modified parser.mly to

... ...
main:
    expr EOL                { $1 }
;
expr [@recovery (E_int 0)]:
    INT                     { E_int $1 }
  | BOOL                    { E_bool $1 }
... ...

But evaluating 1+ still returned an error Fatal error: exception Parser.MenhirBasics.Error.

Could anyone help?


Solution

  • Augmented summary of my comments:

    main:
        expr EOL                { $1 }
    ;
    expr [@recovery (E_int 0)]:
        INT                     { E_int $1 }
      | BOOL                    { E_bool $1 }
      | error                   { E_int 0 }
    

    From menhir's manual:

    From OCaml weekly news:

    This way of doing will most likely not work in a near future but it does for now and it appeared to be the simplest way of doing what OP asked.