Joh*_*iss 5 parsing haskell parsec recursive-descent uu-parsinglib
作为真实语言解析器的简化子问题,我试图为虚构语言的表达式实现一个解析器,它看起来类似于标准命令式语言(如Python,JavaScript等).其语法具有以下构造:
[a-zA-Z]+)+和*和括号的算术表达式.(例如foo.bar.buz)(1, foo, bar.buz))(删除歧义一元组写成(x,))foo(1, bar, buz()))foo()(),因为foo()可能返回函数是合法的)所以这个语言中的一个相当复杂的程序是
(1+2*3, f(4,5,6)(bar) + qux.quux()().quuux)
Run Code Online (Sandbox Code Playgroud)
相关性应该是
( (1+(2*3)), ( ((f(4,5,6))(bar)) + ((((qux.quux)())()).quuux) ) )
Run Code Online (Sandbox Code Playgroud)
我目前正在使用非常好的uu-parsinglib应用解析器组合器库.
第一个问题显然是直观的表达式语法(expr -> identifier | number | expr * expr | expr + expr | (expr)左递归.但我可以使用pChainl组合器解决这个问题(参见parseExpr下面的例子).
剩下的问题(因此这个问题)是函数应用程序,其函数返回其他函数(f()()).同样,语法是递归的expr -> fun-call | ...; fun-call -> expr ( parameter-list ).我有什么想法可以优雅地解决这个问题uu-parsinglib吗?(问题应该直接适用于parsec,attoparsec以及我猜的其他解析器组合器).
请参阅下面我当前的程序版本.它运行良好,但函数应用程序只处理标识符以删除左递归:
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
module TestExprGrammar
(
) where
import Data.Foldable (asum)
import Data.List (intercalate)
import Text.ParserCombinators.UU
import Text.ParserCombinators.UU.Utils
import Text.ParserCombinators.UU.BasicInstances
data Node =
NumberLiteral Integer
| Identifier String
| Tuple [Node]
| MemberAccess Node Node
| FunctionCall Node [Node]
| BinaryOperation String Node Node
parseFunctionCall :: Parser Node
parseFunctionCall =
FunctionCall <$>
parseIdentifier {- `parseExpr' would be correct but left-recursive -}
<*> parseParenthesisedNodeList 0
operators :: [[(Char, Node -> Node -> Node)]]
operators = [ [('+', BinaryOperation "+")]
, [('*' , BinaryOperation "*")]
, [('.', MemberAccess)]
]
samePrio :: [(Char, Node -> Node -> Node)] -> Parser (Node -> Node -> Node)
samePrio ops = asum [op <$ pSym c <* pSpaces | (c, op) <- ops]
parseExpr :: Parser Node
parseExpr =
foldr pChainl
(parseIdentifier
<|> parseNumber
<|> parseTuple
<|> parseFunctionCall
<|> pParens parseExpr
)
(map samePrio operators)
parseNodeList :: Int -> Parser [Node]
parseNodeList n =
case n of
_ | n < 0 -> parseNodeList 0
0 -> pListSep (pSymbol ",") parseExpr
n -> (:) <$>
parseExpr
<* pSymbol ","
<*> parseNodeList (n-1)
parseParenthesisedNodeList :: Int -> Parser [Node]
parseParenthesisedNodeList n = pParens (parseNodeList n)
parseIdentifier :: Parser Node
parseIdentifier = Identifier <$> pSome pLetter <* pSpaces
parseNumber :: Parser Node
parseNumber = NumberLiteral <$> pNatural
parseTuple :: Parser Node
parseTuple =
Tuple <$> parseParenthesisedNodeList 1
<|> Tuple [] <$ pSymbol "()"
instance Show Node where
show n =
let showNodeList ns = intercalate ", " (map show ns)
showParenthesisedNodeList ns = "(" ++ showNodeList ns ++ ")"
in case n of
Identifier i -> i
Tuple ns -> showParenthesisedNodeList ns
NumberLiteral n -> show n
FunctionCall f args -> show f ++ showParenthesisedNodeList args
MemberAccess f g -> show f ++ "." ++ show g
BinaryOperation op l r -> "(" ++ show l ++ op ++ show r ++ ")"
Run Code Online (Sandbox Code Playgroud)
简要地看一下(我更熟悉)的类似列表的组合器,我认为您可以通过折叠组合器的结果来解决这个问题:uu-parsinglibparsecpSome
parseFunctionCall :: Parser Node
parseFunctionCall =
foldl' FunctionCall <$>
parseIdentifier {- `parseExpr' would be correct but left-recursive -}
<*> pSome (parseParenthesisedNodeList 0)
Run Code Online (Sandbox Code Playgroud)
这也相当于Alternative some组合器,它确实应该适用于您提到的其他解析库。