使用 OperatorPrecedenceParser 通过 FParsec 解析函数应用程序?

发布于 2025-01-04 05:53:49 字数 1660 浏览 5 评论 0原文

这个问题类似于这个,但我想用函数应用程序解析表达式使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不念旧人 2025-01-11 05:53:49

您可以将参数列表解析为标识符的可选后缀表达式

let argListInParens = between (str_ws "(") (str_ws ")") argList
let identWithOptArgs = 
    pipe2 ident (opt argListInParens) 
          (fun id optArgs -> match optArgs with
                             | Some args -> FunctionCall(id, args)
                             | None -> Variable(id))

,然后定义 expr 如下

let expr = (number |>> Float) <|> identWithOptArgs

You could parse the argument list as an optional postfix expression of an identifier

let argListInParens = between (str_ws "(") (str_ws ")") argList
let identWithOptArgs = 
    pipe2 ident (opt argListInParens) 
          (fun id optArgs -> match optArgs with
                             | Some args -> FunctionCall(id, args)
                             | None -> Variable(id))

and then define expr like

let expr = (number |>> Float) <|> identWithOptArgs
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文