Menhir obscure error

Hello. I am currently writing a parser for some kind of simplified assembly language, and I am running into an obscure error spat out by menhir:

Uncaught exception:
  Dune__exe__Parser.MenhirBasics.Error
Raised at file "src/parser.ml", line 965, characters 8-18
[...]

I am not used to reading this kind of errors, as it is not describing a shift/reduce conflict that I could fix in my parser.mly file. Moreover the parser.ml mentioned in the error is rather unreadable (it is generated by the library, thus not meant to be read or edited by humans). I have no clue what could have caused the problem. Here is my build configuration:

dune-project:

(lang dune 2.4)
(using menhir 2.0)

dune:

(ocamllex
  (modules lexer))

(menhir
  (modules parser))

(executables
  (names assembler vm)
  (libraries core))

NB: At the moment, vm.ml is empty, and assembler.ml only runs the parser, having as arguments the lexer (generated from a lexer.mll file) and a lexbuf variable generated from an input channel (In_channel.t).

Any idea how to fix this kind of errors? Thanks in advance.

It seems that your parser has been correctly compiled by menhir but detects a syntax error in the file you are trying to parse with. Quoting the documentation: When no state that can act on error is found on the automaton’s stack, the parser stops and raises the exception Error.
You can catch this exception in your code and print the lexbuf position to know where an error has been detected.

1 Like

The error location gave me a hint, and I made a debug parser rule that prints all the lexemes. It allowed me to see that the origin of the failure is in the lexer. However, it is an error case I have handled. It should have raised a custom LexingError exception.

I guess the parser caught everything and threw a generic error with no information instead, which is not what I expected, and made the debugging process much longer. One should probably test the lexer before even starting to write the parser. Thank you very much for the help!

The parser should not catch lexer exceptions: I think it is worth debugging why the lexer did not raise the excepted exception, or why this exception has been caught, because it may hide another problem.