Arlen Cox

Ramblings on my hobbies

Location tracking in OCaml parsers

leave a comment »

I guess that I have been so assimilated into the functional programming world that I was having a very basic issue when developing parsers in OCaml.  I had this issue with all of the parser generators (ocamlyacc, Menhir, Dypgen and Aurochs): I could never get a simple error location when there was a parse error.  They all raise an exception called something like Parse_error, but they give no location information with that exception.

I found this unbelievable.  I mean Antlr gives location information and Parsec does too.  What makes OCaml so obtuse?

As it turns out, OCaml is not obtuse; I am.  The lexers generated by these tools are stateful. Therefore, when you get a parse error, you can simply query the lexer for the current location after exception has been raised.  For example, using Dypgen:

let lexbuf = Dyp.from_channel (StrParse.pp ()) stdin in
try
  (* do parse *)
with Dyp.Syntax_error ->
 let (startpos,endpos) = Dyp.dyplex_lexbuf_position lexbuf in
 (* print error message  - startpos and endpos are of standard Lexing.position type *)

This of course doesn’t provide error recovery where multiple errors could be provided and it doesn’t provide intelligent error messages, but it does give a little bit of information at least on the error. This is what many research tools provide, since developing good error handling is a time consuming task for code that will be quickly disposed of.

A very similar process can be followed both ocamlyacc and Menhir to good effect, though now that I have discovered the power of Dypgen, I doubt I will go back.

Advertisements

Written by arlencox

February 22, 2013 at 2:16 am

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: