gpt4 book ai didi

haskell - megaparsec 中的运算符优先级

转载 作者:行者123 更新时间:2023-12-01 12:10:19 26 4
gpt4 key购买 nike

我在使用 Megaparsec 6 的 makeExprParser 时遇到问题 helper 。我似乎无法弄清楚如何绑定(bind)两个二进制 ^和一元 -在我期望的优先级。

使用这个makeExprParser表达式解析器:

expressionParser :: Parser Expression
expressionParser =
makeExprParser termParser
[
[InfixR $ BinOp BinaryExp <$ symbol "^"],
[
Prefix $ MonOp MonoMinus <$ symbol "-",
Prefix $ MonOp MonoPlus <$ symbol "+"
],
[
InfixL $ BinOp BinaryMult <$ symbol "*",
InfixL $ BinOp BinaryDiv <$ symbol "/"
],
[
InfixL $ BinOp BinaryPlus <$ symbol "+",
InfixL $ BinOp BinaryMinus <$ symbol "-"
]
]

我希望这些测试能够通过:
testEqual expressionParser "1^2" "(1)^(2)"
testEqual expressionParser "-1^2" "-(1^2)"
testEqual expressionParser "1^-2" "1^(-2)"
testEqual expressionParser "-1^-2" "-(1^(-2))"

也就是说, -1^-2应该解析为与 -(1^(-2)) 相同的东西.这就是例如Python 解析它:
>>> 2**-2
0.25
>>> -2**-2
-0.25
>>> -2**2
-4

和 ruby :
irb(main):004:0> 2**-2
=> (1/4)
irb(main):005:0> -2**-2
=> (-1/4)
irb(main):006:0> -2**2
=> -4

但是这个 Megaparsec 解析器却无法解析 1^-2根本没有,而是给了我有用的错误:
(TrivialError (SourcePos {sourceName = \"test.txt\", sourceLine = Pos 1, sourceColumn = Pos 3} :| []) (Just (Tokens ('-' :| \"\"))) (fromList [Tokens ('(' :| \"\"),Label ('i' :| \"nteger\")]))")

我读到它说“我可以在这里使用这些字符中的任何一个,但是 - 让我感到困惑”。

如果我像这样调整运算符表的某些优先级(在一元后移动指数 - ):
expressionParser =
makeExprParser termParser
[
[
Prefix $ MonOp MonoMinus <$ symbol "-",
Prefix $ MonOp MonoPlus <$ symbol "+"
],
[InfixR $ BinOp BinaryExp <$ symbol "^"],
[
InfixL $ BinOp BinaryMult <$ symbol "*",
InfixL $ BinOp BinaryDiv <$ symbol "/"
],
[
InfixL $ BinOp BinaryPlus <$ symbol "+",
InfixL $ BinOp BinaryMinus <$ symbol "-"
]
]

然后我不再遇到解析失败,而是 -1^2错误地解析为 (-1)^2 (而不是正确的 -(1^2) )。

这是一个完整的自包含解析器来显示问题(它需要 HUnit,当然还有 megaparsec):
module Hascas.Minimal where

import Data.Void (Void)
import Test.HUnit hiding (test)
import Text.Megaparsec hiding (ParseError)
import Text.Megaparsec.Char
import Text.Megaparsec.Expr
import qualified Text.Megaparsec as MP
import qualified Text.Megaparsec.Char.Lexer as L

data Expression
= Literal Integer
| MonOp MonoOperator Expression
| BinOp BinaryOperator Expression Expression
deriving (Read, Show, Eq, Ord)

data BinaryOperator
= BinaryPlus
| BinaryMinus
| BinaryDiv
| BinaryMult
| BinaryExp
deriving (Read, Show, Eq, Ord)

data MonoOperator
= MonoPlus
| MonoMinus
deriving (Read, Show, Eq, Ord)

type Parser a = Parsec Void String a
type ParseError = MP.ParseError (Token String) Void

spaceConsumer :: Parser ()
spaceConsumer = L.space space1 lineComment blockComment
where
lineComment = L.skipLineComment "//"
blockComment = L.skipBlockComment "/*" "*/"

lexeme :: Parser a -> Parser a
lexeme = L.lexeme spaceConsumer

symbol :: String -> Parser String
symbol = L.symbol spaceConsumer

expressionParser :: Parser Expression
expressionParser =
makeExprParser termParser
[
[InfixR $ BinOp BinaryExp <$ symbol "^"],
[
Prefix $ MonOp MonoMinus <$ symbol "-",
Prefix $ MonOp MonoPlus <$ symbol "+"
],
[
InfixL $ BinOp BinaryMult <$ symbol "*",
InfixL $ BinOp BinaryDiv <$ symbol "/"
],
[
InfixL $ BinOp BinaryPlus <$ symbol "+",
InfixL $ BinOp BinaryMinus <$ symbol "-"
]
]

termParser :: Parser Expression
termParser = (
(try $ Literal <$> L.decimal)
<|> (try $ parens expressionParser))

parens :: Parser a -> Parser a
parens x = between (symbol "(") (symbol ")") x

main :: IO ()
main = do
-- just to show that it does work in the + case:
test expressionParser "1+(-2)" $
BinOp BinaryPlus (Literal 1) (MonOp MonoMinus $ Literal 2)
test expressionParser "1+-2" $
BinOp BinaryPlus (Literal 1 ) (MonOp MonoMinus $ Literal 2)

-- but not in the ^ case
test expressionParser "1^-2" $
BinOp BinaryExp (Literal 1) (MonOp MonoMinus $ Literal 2)
test expressionParser "-1^2" $
MonOp MonoMinus $ BinOp BinaryExp (Literal 1) (Literal 2)
test expressionParser "-1^-2" $
MonOp MonoMinus $ BinOp BinaryExp (Literal 1) (MonOp MonoMinus $ Literal 2)

-- exponent precedence is weird
testEqual expressionParser "1^2" "(1)^(2)"
testEqual expressionParser "-1^2" "-(1^2)"
testEqual expressionParser "1^-2" "1^(-2)"
testEqual expressionParser "-1^-2" "-(1^(-2))"
testEqual expressionParser "1^2^3^4" "1^(2^(3^(4))))"
where
test :: (Eq a, Show a) => Parser a -> String -> a -> IO ()
test parser input expected = do
assertEqual input (Right expected) $ parse (spaceConsumer >> parser <* eof) "test.txt" input

testEqual :: (Eq a, Show a) => Parser a -> String -> String -> IO ()
testEqual parser input expected = do
assertEqual input (p expected) (p input)
where
p i = parse (spaceConsumer >> parser <* eof) "test.txt" i

是否有可能让 Megaparsec 以其他语言的优先级解析这些运算符?

最佳答案

makeExprParser termParser [precN, ..., prec1]将产生一个表达式解析器,它的工作方式使得每个优先级都调用下一个更高级别的优先级。所以如果你手动定义它,你会有一个中缀规则 +- ,它使用 mult-and-div 规则作为操作数。这反过来将使用前缀规则作为操作数,并且将使用 ^规则作为操作数。最后是^规则使用 termParser为操作数。

这里要注意的重要一点是 ^规则(或更一般地说:任何优先级高于前缀运算符的规则)调用一个在开头不接受前缀运算符的解析器。因此前缀运算符不能出现在此类运算符的右侧(括号内除外)。

这基本上意味着 makeExprParser 不支持您的用例。 .

要解决此问题,您可以使用 makeExprParser只处理优先级低于前缀运算符的中缀运算符,然后处理前缀运算符和^手动,使^的右操作数将“循环回”到前缀运算符。像这样的东西:

expressionParser =
makeExprParser prefixParser
[
[
InfixL $ BinOp BinaryMult <$ symbol "*",
InfixL $ BinOp BinaryDiv <$ symbol "/"
],
[
InfixL $ BinOp BinaryPlus <$ symbol "+",
InfixL $ BinOp BinaryMinus <$ symbol "-"
]
]

prefixParser =
do
prefixOps <- many prefixOp
exp <- exponentiationParser
return $ foldr ($) exp prefixOps
where
prefixOp = MonOp MonoMinus <$ symbol "-" <|> MonOp MonoPlus <$ symbol "+"

exponentiationParser =
do
lhs <- termParser
-- Loop back up to prefix instead of going down to term
rhs <- optional (symbol "^" >> prefixParser)
return $ maybe lhs (BinOp BinaryExp lhs) rhs

请注意,与 makeExprParser 不同。 ,这也允许多个连续的前缀运算符(如 --x 用于双重否定)。如果您不希望这样,请替换 manyoptionalprefixParser 的定义中.

关于haskell - megaparsec 中的运算符优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52107488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com