Skip to content

[Feat] Parser Combinator #42

Description

@brightprogrammer

Definition

A parser combinator is a parser that ingests multiple parsers and returns a new parser, covering languages of the ingested parsers based on the combining operation used, that either accepts or rejects strings of the combined language.

Example

Consider a parser combinator implementation that provides following higher order combining operations ($\Pi$ being the set of all parsers) :

  • $\mathrm{Seq}(P_1, P_2, \ldots, P_n) \mapsto \Pi, \quad P_1, P_2, \ldots, P_n \in \Pi$ : Accept only if string matches each parser in sequence, if any single one fails, reject, without checking whether the following parsers match or not
  • $\mathrm{Alt}(P_1, P_2, \ldots, P_n) \mapsto \Pi, \quad P_1, P_2, \ldots, P_n \in \Pi$ : Accept if any of the parsers match and return early
  • $\mathrm{MatchAtLeastOne}(P) \mapsto \Pi, \quad P \in \Pi$ : Matches at least one string that matches $P$
  • $\mathrm{MatchAny}(P) \mapsto \Pi, \quad P \in \Pi$ : Matches any number of ocurrences of a string matching $P$. This includes no ocurrence at all.

You can write a parser like $\mathrm{Seq}\big(\mathrm{Lit}(\text{'a'}),; \mathrm{MatchAtLeastOne}(\mathrm{Alt}(\mathrm{Lit}(\text{'1'}),, \mathrm{Lit}(\text{'2'})))\big)$ to accept a string of $a$ followed by at least one occurence of either $1$ or $2$.

Motivation

This will help writing the syntactic analyzer tools. I envision MisraStdC to provide a wide set of parsers out of the box, including different file formats and languages. This will also end up facilitating #41 in it's path to write a syntactic analyzer and a semantic analyzer, given both stages require a parsed source code.

End Goal

is to have a decent parser combinator engine in MisraStdC that facilitates easy writing of language parsers, and creation of a generic parse tree out of the box.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions