module Parser
import Text.Parsec hiding (parse)
import qualified Text.Parsec as Parsec
Copyright By PowCoder代写 加微信 powcoder
import Prop
— The string parser we use in this file
type Parser = Parsec String ()
— Variable declaration
var :: Parser (Prop String)
v <- many1 letter
return $ Lit v
-- Unary symbols (in this case negation)
unary :: Parser (Prop String)
unary = do
_ <- oneOf "~!-¬"
p <- factor
return $ Neg p
-- Parenthesized expression
paren :: Parser a -> Parser a
paren p = between (char ‘(‘) (char ‘)’) p
— A factor is either:
— – paren expr
— – unary
factor :: Parser (Prop String)
factor = do
f <- paren expr <|> unary <|> var
— A term, in this case a conjunction
term :: Parser (Prop String)
(t:ts) <- factor `sepBy1` (oneOf "&∧")
return $ foldl (:&:) t ts
-- An expression, in this case a disjunction
expr :: Parser (Prop String)
(e:es) <- term `sepBy1` (oneOf "|∨")
return $ foldl (:|:) e es
-- A full propositional expression, does not
-- accept garbage after the expression
prop :: Parser (Prop String)
parse :: String -> Prop String
parse raw = case Parsec.parse prop “haskell” raw of
Right p -> p
Left x -> error . show $ x
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com