Headline

Parsing in Haskell with Parsec

Motivation

The implementation demonstrates parsing in Haskell with the Parsec library of parser combinators. A concrete textual syntax for companies is assumed. Parse trees are constructed in accordance to an abstract syntax defined in terms of algebraic data types. We set up basic parsers for quoted strings and floating-point numbers. Further, we compose parsers for companies, departments, and employees using appropriate parser combinators for sequences, alternatives, and optionality. By design, the parser is kept simple in terms of leveraged programming technique; in particular, monadic style and applicative functors are avoided to the extent possible.

Illustration

See Contribution:haskellAcceptor for a basic illustrationof Parsec-based parsing. The present contribution is more complex in that it constructs proper parse trees.

parseDepartment :: Parser Department
parseDepartment = Department
  <$> (parseString "department"
  >>  parseLiteral)
  <*  parseString "{"
  <*> parseEmployee "manager"
  <*> many parseSubUnit
  <*  parseString "}"

To this end, we use a parser type that is still parametric in the type of parse trees. Thus:

-- Shorthand for the parser type
type Parser = Parsec String ()

Relationships

Architecture

There are these modules:

  • Main: parser test
  • Company/Parser: the actual parser
  • Company/Data: the abstract syntax definition
  • Company/Sample: a baseline for testing at the level of abstract syntax
The input is parsed from a file "sampleCompany.txt".

Usage

See https://github.com/101companies/101haskell/blob/master/README.md.


Ralf Lämmel edited this article at Sat, 20 Jun 2020 16:23:45 +0200
Compare revisions Compare revisions

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.