这是这个问题的演变。
我需要用 megaparsec 解析一个数据结构,比如
data Foo =
Simple String
Dotted Foo String
Paren String Foo
我想解析它的字符串
foo ::= alphanum
| foo "." alphanum
| alphanum "(" foo ")"
例如 a 字符串"a(b.c).d"
应该被解析为Dotted (Paren "a" (Dotted (Simple "b") "c")) "d"
.
我遇到的问题是这同时是左右递归的。
我为第一种和第三种情况编写解析器没有问题:
parser :: Parser Foo
parser
= try (do
prefix <- alphanum
constant "("
content <- parser
constant ")"
pure $ Paren prefix content
)
<|> Simple alphanum
但我无法将第二种情况的解析器放在一起。我试图用sepBy1
或用来接近它,makeExprParser
但我做错了