Skip to content

Instantly share code, notes, and snippets.

@t0yv0
Created September 26, 2011 02:34
Show Gist options
  • Select an option

  • Save t0yv0/1241504 to your computer and use it in GitHub Desktop.

Select an option

Save t0yv0/1241504 to your computer and use it in GitHub Desktop.
{-# OPTIONS -XDoRec -XDeriveDataTypeable #-}
module Example where
import Text.Earley
import Data.Dynamic
import Control.Applicative
data E = Nat Int
| Add E E deriving (Show, Typeable)
grammar :: Grammar (Rule Char E)
grammar = do
nat <- rule "NAT"
[ fmap (\_ -> 0) (term '0')
, fmap (\_ -> 1) (term '1')
]
rec expr <- rule "EXPR"
[ fmap Nat $ var nat
, pure (\x _ y -> Add x y)
<*> var expr
<*> term '+'
<*> var expr
]
return expr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment