我想在Haskell中创建一个解析器来解析逻辑中的boolean expresionns。我陷入了困境。我使用的解析器无法解析“Not”符号,除非它位于表达式的前面。
这有效:
parse exprparser "" "~x^y|-z"
但这不是:
parse exprparse "" "x^y|-~y"
它不解析not("~"
)符号后的任何内容。我怎样才能解决这个问题?
这是我正在处理的代码:
exprparser :: Parser Expr
exprparser = buildExpressionParser table term <?> "expression"
table = [ [Prefix (m_reservedOp "~" >> return (Uno Not))]
, [Infix (m_reservedOp "," >> return (Duo Virgula)) AssocLeft]
, [Infix (m_reservedOp "&" >> return (Duo And)) AssocLeft]
, [Infix (m_reservedOp "^" >> return (Duo Or)) AssocLeft]
, [Infix (m_reservedOp "|-" >> return (Duo Dem)) AssocLeft]
]
term = m_parens exprparser
<|> fmap Var m_identifier
<|> (m_reserved "true" >> return (Con True))
<|> (m_reserved "false" >> return (Con False))