public
Created

New parser for Hatt with support for associativity and operator precedence

  • Download Gist
hatt-parser2.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.