Skip to content

Parsing a C-like language #9

Description

@basus

This is not a bug, but 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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions