Custom Format Parser


#1

I’m thinking about writing a parser for my own file format. I’m imagining a cross between toml and markdown. I figured this would be a good project for learning Reason. The problem is I have no idea where to start. Anyone able to give me some direction or resources on at least a high level of how this should be done? I believe you would want to convert it to an AST, but I am pretty naive about that as well.

Thanks! Any information at all would be greatly appreciated.


#2

@dardub I’m also in the process of learning Reason by writing my own parser for math expressions. It uses the shunting yard algorithm. https://github.com/kevinbarabash/re-math-parser

Another common type of parser is recursive descent parser. I find these are probably the easiest to understand and implement by hand. https://en.wikipedia.org/wiki/Recursive_descent_parser

There are also lots of tools for generating parsers. One common set of tools is lex and yacc. OCaml has bindings for these tools and Reason has runtimes for the lexers and parsers generated by these tools. See https://reasonml.github.io/api/Lexing.html and https://reasonml.github.io/api/Parsing.html. I have not used either of these modules.


#3

Great. Thank you for the info! I’ll take a look.


#4

Real World OCaml has a nice section on parsing with OCamllex and Menhir.