Search code examples
ocamlocamlyacc

ocamlyacc parse error: what token?


I'm using ocamlyacc and ocamllex. I have an error production in my grammar that signals a custom exception. So far, I can get it to report the error position:

| error { raise (Parse_failure (string_of_position (symbol_start_pos ()))) }

But, I also want to know which token was read. There must be a way---anyone know?

Thanks.


Solution

  • Tokens are generated by lexer, hence you can use the current lexer token when error occurs :

      let parse_buf_exn lexbuf =
        try
          T.input T.rule lexbuf
        with exn ->
          begin
            let curr = lexbuf.Lexing.lex_curr_p in
            let line = curr.Lexing.pos_lnum in
            let cnum = curr.Lexing.pos_cnum - curr.Lexing.pos_bol in
            let tok = Lexing.lexeme lexbuf in
            let tail = Sql_lexer.ruleTail "" lexbuf in
            raise (Error (exn,(line,cnum,tok,tail)))
          end
    

    Lexing.lexeme lexbuf is what you need. Other parts are not necessary but useful. ruleTail will concat all remaining tokens into string for the user to easily locate error position. lexbuf.Lexing.lex_curr_p should be updated in the lexer to contain correct positions. (source)