Parsing a C-like language using MParser

mparser
#1

I’m wondering how I can use MParser to write a parser for a C-like language? The main issue I’m running into is when defining the different kinds statements which need to be mutually recursive:


  let rec while_ : (stmt, bytes list) MParser.t =
    while' >> parens (cond) >>= fun cond ->
    (braces block) >>= fun bl -> return (While(cond, bl))
  and block : (block, bytes list) MParser.t =
    braces (many statement)
  and statement : (stmt, bytes list) MParser.t =
    attempt assign <|>
    attempt call <|>
    attempt while_

Here assign and call have been defined separately and work fine. However, while_ needs to be mutually recursive with block and statement, but this definition violates OCaml’s rules for mutually recursive definitions.

Is there some other way to define rules like this using MParser?