Menhir的部分解析和恢复

Partial parsing and recovery of Menhir

有一个很小calculator in Sedlex and Menhir。现在,我想让计算器能够解析像 1+ 这样的表达式。所以我将parser.mly修改为

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

但评估 1+ 仍然返回错误 Fatal error: exception Parser.MenhirBasics.Error

有人能帮忙吗?

我的评论的补充摘要:

  • [@recovery ...] 是 Merlin 特有的结构
  • 截至目前(13/02/2022),仍然可以通过使用特殊的 error 标记来定义您自己的错误恢复,如下所示:
main:
    expr EOL                {  }
;
expr [@recovery (E_int 0)]:
    INT                     { E_int  }
  | BOOL                    { E_bool  }
  | error                   { E_int 0 }

来自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.

来自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.

这种做事方式很可能在不久的将来行不通,但现在行得通,而且它似乎是执行 OP 要求的最简单方法。