Created
December 3, 2012 00:33
-
-
Save beastaugh/4191806 to your computer and use it in GitHub Desktop.
New parser for Hatt with support for associativity and operator precedence
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
module Data.Logic.Propositional.Parser2 where | |
import Data.Logic.Propositional | |
import Text.Parsec.Prim ((<|>), (<?>), runPT) | |
import Text.Parsec.Char (char, letter, spaces, string) | |
import Text.Parsec.Expr | |
parseExpr' = runPT expr () | |
expr = buildExpressionParser operators term | |
<?> "compound expression" | |
term = parens expr | |
<|> variable | |
<?> "expression" | |
variable = do c <- letter | |
return $ Variable (Var c) | |
<?> "variable" | |
parens p = do char '(' | |
spaces | |
x <- p | |
spaces | |
char ')' | |
return x | |
<?> "parens" | |
operators = [ [Prefix (string "~" >> return Negation)] | |
, [binary "&" Conjunction] | |
, [binary "|" Disjunction] | |
, [binary "->" Conditional] | |
, [binary "<->" Biconditional] | |
] | |
binary n c = Infix (spaces >> string n >> spaces >> return c) AssocRight |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment