How to display location info from a parser?

How can I display the location information when a Stream.Error occurs inside a parser? For example:

let p = parser
  | [< '1; '2 ?? "1 expected at line L, column C" >] -> ()

how to get the location in the stream? Is there a way to use Stream.count without having to count the newline characters manually?

Just as an aside: you’re clearly using the old camlp4 parsing extension for new work. I recommend against that; camlp4 is now quite obsolete.

Disclaimer: I’m new to the language.

Could you please point me to the up-to-date extension? I learned about this in the LLVM tutorial, which I guess is using the outdated extension(?). If I would like to develop a lexer+parser for a programming language, what’s the right tool in OCaml?


The LLVM tutorial is indeed extremely obsolete. I suggest using either ocamllex or sedlex for lexing, and menhir for parsing.

Thanks for the suggestions. Part of my intention was a hand-written lexer+parser, for educational purposes, rather than using generators for both.
Is camlp5 also considered obsolete? I see the manual updated in 2017.

camlp4 and camlp5 are still used by some historic code and still updated, but you should probably not use them on new projects. If you wish to extend OCaml’s syntax, the modern mechanism to do that is the PPX facility.