使用 OperatorPrecedenceParser 通过 FParsec 解析函数应用程序?
这个问题类似于这个,但我想用函数应用程序解析表达式使用FParsec
中的OperatorPrecedenceParser
。
这是我的 AST:
type Expression =
| Float of float
| Variable of VarIdentifier
| BinaryOperation of Operator * Expression * Expression
| FunctionCall of VarIdentifier (*fun name*) * Expression list (*arguments*)
我有以下输入:
board→create_obstacle(4, 4, 450, 0, fric)
这是解析器代码:
let expr = (number |>> Float) <|> (ident |>> Variable)
let parenexpr = between (str_ws "(") (str_ws ")") expr
let opp = new OperatorPrecedenceParser<_,_,_>()
opp.TermParser <- expr <|> parenexpr
opp.AddOperator(InfixOperator("→", ws,
10, Associativity.Right,
fun left right -> BinaryOperation(Arrow, left, right)))
我的问题是函数参数也是表达式(它们可以包括运算符、变量等),并且我不知道如何扩展我的 < code>expr 解析器将参数列表解析为表达式列表。我在这里构建了一个解析器,但我不知道如何将它与现有的解析器结合起来:
let primitive = expr <|> parenexpr
let argList = sepBy primitive (str_ws ",")
let fcall = tuple2 ident (between (str_ws "(") (str_ws ")") argList)
我的解析器当前有以下输出:
Success: Expression (BinaryOperation
(Arrow,Variable "board",Variable "create_obstacle"))
我想要的是得到以下内容:
Success: Expression
(BinaryOperation
(Arrow,
Variable "board",
Function (VarIdentifier "create_obstacle",
[Float 4, Float 4, Float 450, Float 0, Variable "fric"]))
The question is similar to this one, but I want to parse an expression with function application using the OperatorPrecedenceParser
in FParsec
.
Here is my AST:
type Expression =
| Float of float
| Variable of VarIdentifier
| BinaryOperation of Operator * Expression * Expression
| FunctionCall of VarIdentifier (*fun name*) * Expression list (*arguments*)
I have the following input:
board→create_obstacle(4, 4, 450, 0, fric)
And here is the parser code:
let expr = (number |>> Float) <|> (ident |>> Variable)
let parenexpr = between (str_ws "(") (str_ws ")") expr
let opp = new OperatorPrecedenceParser<_,_,_>()
opp.TermParser <- expr <|> parenexpr
opp.AddOperator(InfixOperator("→", ws,
10, Associativity.Right,
fun left right -> BinaryOperation(Arrow, left, right)))
My problem here is that the function arguments are expressions as well (they can include operators, variables etc) and I don't know how to extend my expr
parser to parse the argument list as a list of expression. I built a parser here, but I don't know how to combine it with my existing parser:
let primitive = expr <|> parenexpr
let argList = sepBy primitive (str_ws ",")
let fcall = tuple2 ident (between (str_ws "(") (str_ws ")") argList)
I currently have the following output from my parser:
Success: Expression (BinaryOperation
(Arrow,Variable "board",Variable "create_obstacle"))
What I want is to get the following:
Success: Expression
(BinaryOperation
(Arrow,
Variable "board",
Function (VarIdentifier "create_obstacle",
[Float 4, Float 4, Float 450, Float 0, Variable "fric"]))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将参数列表解析为标识符的可选后缀表达式
,然后定义
expr
如下You could parse the argument list as an optional postfix expression of an identifier
and then define
expr
like