haskell - 如何使 Parsec chainl1 函数遵循运算符优先级规则

标签 haskell parsec combinators

我正在编写标准数学符号 -> DC POSIX 兼容的格式转换器。它获取输入字符串,将其解析为中间数据类型,然后通过显示将其转换为输出字符串。

这是使用的数据类型。我对数据类型 -> 输出字符串转换没有任何问题,它工作得很好:

data Expression = Expression :+ Expression
                | Expression :- Expression
                | Expression :* Expression
                | Expression :/ Expression
                | Expression :^ Expression
                | Cons String

infixr 0 :+
infixr 0 :-
infixr 1 :*
infixr 1 :/
infixr 2 :^

instance Show Expression where
  show (x :+ y) = unwords [show x, show y, "+"]
  show (x :- y) = unwords [show x, show y, "-"] 
  show (x :* y) = unwords [show x, show y, "*"]
  show (x :/ y) = unwords [show x, show y, "/"]
  show (x :^ y) = unwords [show x, show y, "^"]
  show (Cons y) = y

但是,Parsec 解析器部分拒绝遵守定义的运算符优先级规则。显然是因为 chainl1subexpression 解析器定义中使用的方式:

expression :: Parser Expression
expression = do
  spaces
  x <- subexpression
  spaces >> eof >> return x

subexpression :: Parser Expression
subexpression = (
    (bracketed subexpression) <|>
    constant
  ) `chainl1` (
    try addition              <|>
    try substraction          <|>
    try multiplication        <|>
    try division              <|>
    try exponentiation
  )

addition       = operator '+' (:+)
substraction   = operator '-' (:-)
multiplication = operator '*' (:*)
division       = operator '/' (:/)
exponentiation = operator '^' (:^)

operator :: Char -> (a -> a -> a) -> Parser (a -> a -> a)
operator c op = do
  spaces >> char c >> spaces
  return op

bracketed :: Parser a -> Parser a
bracketed parser = do
  char '('
  x <- parser
  char ')'
  return x

constant :: Parser Expression
constant = do
  parity <- optionMaybe $ oneOf "-+"
  constant <- many1 (digit <|> char '.')
  return (if parity == Just '-'
    then (Cons $ '_':constant)
    else  Cons       constant)

有没有办法让解析器考虑运算符优先级规则,而不必重写我的全部代码?

最佳答案

好吧,您不需要重写您的整个代码,但是由于您的子表达式 解析器根本没有考虑优先级,您必须重写- 实质上。

一种可能性是从具有相同优先级的顶级运算符的子表达式的解析器构建它,

atom :: Parser Expression
atom = bracketed subexpression <|> constant

-- highest precedence operator is exponentiation, usually that's
-- right-associative, hence I use chainr1 here
powers :: Parser Expression
powers = atom `chainr1` try exponentiation

-- a multiplicative expression is a product or quotient of powers,
-- left-associative
multis :: Parser Expression
multis = powers `chainl1` (try multiplication <|> try division)

-- a subexpression is a sum (or difference) of multiplicative expressions
subexpression :: Parser Expression
subexpression = multis `chainl1` (try addition <|> try substraction)

另一种选择是让库处理优先级和关联性并使用 Text.Parsec.Expr , 即 buildExpressionParser :

table = [ [binary "^" (:^) AssocRight]
        , [binary "*" (:*) AssocLeft, binary "/" (:/) AssocLeft]
        , [binary "+" (:+) AssocLeft, binary "-" (:-) AssocLeft]
        ]

binary  name fun assoc = Infix (do{ string name; spaces; return fun }) assoc

subexpression = buildExpressionParser table atom

(这要求方括号解析器常量 消耗已用标记后的空格)。

关于haskell - 如何使 Parsec chainl1 函数遵循运算符优先级规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16367150/

相关文章:

parsing - haskell 秒差距 : Undo a failed many

uu-parsinglib 的性能与 Parsec 中的 "try"相比

haskell - (=<<) 组合子的鸟名?

parsing - 解析器中的 Parsec <|> 选择,错误抛出但不转到下一个解析器

lisp - elisp 中的 Y 组合器

functional-programming - worker 用组合器的解释

string - Data.ByteString.Char8中 `isInfixOf`的时间复杂度是O(m * n)吗?

haskell - 创建完全依赖的串联

json - 有条件地将字段添加到 JSON 输出

haskell - (l,r)之前的~是什么意思