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

Wit*_*iko 2 haskell combinators parsec

我正在编写标准数学符号 - > 符合DC POSIX标准的格式转换器.它接受输入字符串,将其解析为中间数据类型,然后通过它将其转换为输出字符串show.

这是使用的数据类型.我没有数据类型 - >输出字符串转换的问题,它完美无缺:

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
Run Code Online (Sandbox Code Playgroud)

但是,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)
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*her 6

好吧,你不需要重写你的整个代码,但由于你的subexpression解析器根本没有优先考虑,你必须重写 - 实质上.

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

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)
Run Code Online (Sandbox Code Playgroud)

另一个选择是让图书馆使用优先级和关联性,并使用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
Run Code Online (Sandbox Code Playgroud)

(这需要使用bracketed parserconstant消耗使用过的令牌之后的空格).

  • 你必须重新学习一下,但是一旦你弄湿了,编写解析器就比正则表达式容易得多,甚至是稍微复杂的东西. (3认同)