Skip to content

Instantly share code, notes, and snippets.

@wyager
Created February 14, 2017 07:17
Show Gist options
  • Save wyager/741b8f73e6eda424c6f436ebe9cd8bbe to your computer and use it in GitHub Desktop.
Save wyager/741b8f73e6eda424c6f436ebe9cd8bbe to your computer and use it in GitHub Desktop.
import Text.Parsec
import Text.Parsec.String
data Expr = Mul Expr Expr | Add Expr Expr | Lit Int deriving Show
expr :: Parser Expr
expr = try lit <|> try mul <|> try add <|> parenthesized
where
mul = char '*' >> Mul <$> rec <*> rec
add = char '+' >> Add <$> rec <*> rec
lit = (Lit . read) <$> many1 digit
rec = many space >> expr
parenthesized = between (char '(') (char ')') rec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment