What is it

Peggy is a parser generator for Haskell. It supports parsing expression grammer (PEG) which is simple and expressive. It can generate efficient packrat parser.

Quick Start

You can install Peggy from Hackage:

$ cabal update
$ cabal install Peggy

Github repository is here.

Easy to use

Syntax of Peggy is very simple and easy to understand, so you can start writing practical parser immediately.


Parsing Expression Grammer

Peggy supports parsing expresion grammer (PEG), which is:

  • No shift/reduce conflict
  • Simple and Expressive
  • Unlimited look-ahead
  • Linear time parser

And Peggy extends PEG:

  • Separated list
  • Easy to handle source location
  • Support left recursion

Modern

Peggy generates modern Haskell code, which is:

Embeded DSL

Peggy is an embeded DSL for Haskell. You can embeded your parser in Haskell source code directly using Template Haskell and Quasiquotation looks like below. The code is checked by Haskell’s type-checker and you need no more separated grammer file.

{-# Language TemplateHaskell, QuasiQuotes, FlexibleContexts #-}

import Text.Peggy

[peggy|
top :: Double = expr !.

expr :: Double
  = expr "+" fact { $1 + $2 }
  / expr "-" fact { $1 - $2 }
  / fact

fact :: Double
  = fact "*" term { $1 * $2 }
  / fact "/" term { $1 / $2 }
  / term

term :: Double
  = "(" expr ")"
  / number

number ::: Double
  = [1-9] [0-9]* { read ($1 : $2) }
|]

main :: IO ()
main = print . parseString top "<stdin>" =<< getContents

Learn More

You can find more document about Peggy syntax here. Haddock documentation is here.