Try partial parsing and recovery of Menhir

Hello,

I made a very small calculator in Sedlex and Menhir: GitHub - chengtie/sedlex-menhir. Now, I would like to make the calculator 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?

Thank you

Also asked on Stack Overflow

Augmented summary of my comments:

  • [@recovery ...] is a construct specific to Merlin
  • As of now (13/02/2022) it is still possible to define your own error recovery by using the special error token like this:
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:

  • If the error token is used to survive an error and continue parsing, then the legacy strategy should be selected.

  • --strategy strategy. This switch selects an error-handling strategy, to be used by the code back-end, the table back-end, and the reference interpreter. The available strategies are legacy and simplified.

From OCaml weekly news:

  • Grammars that make more complex use of the error token, and therefore
    need the legacy strategy, cannot be compiled by the new code back-end.
    As a workaround, it is possible to switch to the table back-end (using
    –table --strategy legacy) or to the ancient code back-end (using
    –code-ancient). In the long run, we recommend abandoning the use of
    the error token. Support for the error token may be removed
    entirely at some point in the future.

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.