Created
October 11, 2009 17:22
-
-
Save SilverTab/207761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ParsecExpr | |
expr :: Parser Integer | |
expr = buildExpressionParser table factor | |
<?> "expression" | |
table = [[op "*" (*) AssocLeft, op "/" div AssocLeft] | |
,[op "+" (+) AssocLeft, op "-" (-) AssocLeft] | |
] | |
where | |
op s f assoc | |
= Infix (do{ string s; return f}) assoc | |
factor = do{ char '(' | |
; x <- expr | |
; char ')' | |
; return x | |
} | |
<|> number | |
<?> "simple expression" | |
number :: Parser Integer | |
number = do{ ds <- many1 digit | |
; return (read ds) | |
} | |
<?> "number" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment