如何使用 FParsec 解析具有多个参数的前缀函数,例如 Pow()

How to parse prefix function such as Pow() with multiple parameters using FParsec

我尝试使用 FParsec 解析前缀函数,例如 Pow(3+2,2)。我阅读了示例文件中的计算器教程,如下所示。例子都是一元前缀函数。我想知道如何使用 FParsec.OperatorPrecedenceParser.

实现具有多个输入的前缀函数

http://www.quanttec.com/fparsec/reference/operatorprecedenceparser.html#members.PrefixOperator

let number = pfloat .>> ws

let opp = new OperatorPrecedenceParser<float,unit,unit>()
let expr = opp.ExpressionParser
opp.TermParser <- number <|> between (str_ws "(") (str_ws ")") expr


opp.AddOperator(InfixOperator("+", ws, 1, Associativity.Left, (+)))
opp.AddOperator(InfixOperator("-", ws, 1, Associativity.Left, (-)))
opp.AddOperator(InfixOperator("*", ws, 2, Associativity.Left, (*)))
opp.AddOperator(InfixOperator("/", ws, 2, Associativity.Left, (/)))
opp.AddOperator(InfixOperator("^", ws, 3, Associativity.Right, fun x y -> System.Math.Pow(x, y)))
opp.AddOperator(PrefixOperator("-", ws, 4, true, fun x -> -x))

let ws1 = nextCharSatisfiesNot isLetter >>. ws
opp.AddOperator(PrefixOperator("log", ws1, 4, true, System.Math.Log))
opp.AddOperator(PrefixOperator("exp", ws1, 4, true, System.Math.Exp))

更新 1

我已经根据字符串后解析器示例编写了一个快速脚本,因为实际应用程序需要字符串后解析器 http://www.quanttec.com/fparsec/users-guide/tips-and-tricks.html#parsing-f-infix-operators

abs(pow(1,2))可以解析但是pow(abs(1),2)无法解析.我对如何使用前缀函数作为 identWithArgs 输入的一部分感到困惑。

#I @"..\packages\FParsec.1.0.2\lib\net40-client"
#r "FParsecCS.dll"
#r "FParsec.dll"

open FParsec

type PrefixFunc = POW
type Expr =
  | InfixOpExpr of string * Expr * Expr
  | PrefixOpExpr of string * Expr
  | PrefixFuncExpr of PrefixFunc * Expr list 
  | Number of int

let ws = spaces
let ws1 = spaces1
let str s = pstring s
let str_ws s = ws >>. str s .>> ws
let strci s = pstringCI s
let strci_ws s = ws >>. strci s .>> ws
let strciret_ws s x = ws >>. strci s .>> ws >>% x

let isSymbolicOperatorChar = isAnyOf "!%&*+-./<=>@^|~?"
let remainingOpChars_ws = manySatisfy isSymbolicOperatorChar .>> ws

let primitive = pint32 .>> ws |>> Number
let argList = sepBy primitive (str_ws ",")
let argListInParens = between (str_ws "(") (str_ws ")") argList
let prefixFunc = strciret_ws "pow" POW 
let identWithArgs =
    pipe2 prefixFunc argListInParens (fun funcId args -> PrefixFuncExpr(funcId, args))

let opp = new OperatorPrecedenceParser<Expr, string, unit>()
opp.TermParser <-
  primitive <|>
  identWithArgs <|>
  between (pstring "(") (pstring ")") opp.ExpressionParser

// a helper function for adding infix operators to opp
let addSymbolicInfixOperators prefix precedence associativity =
    let op = InfixOperator(prefix, remainingOpChars_ws,
                           precedence, associativity, (),
                           fun remOpChars expr1 expr2 ->
                               InfixOpExpr(prefix + remOpChars, expr1, expr2))
    opp.AddOperator(op)

// the operator definitions:
addSymbolicInfixOperators "*"  10 Associativity.Left
addSymbolicInfixOperators "**" 20 Associativity.Right

opp.AddOperator(PrefixOperator("abs",remainingOpChars_ws,3,true,(),fun remOpChars expr -> PrefixOpExpr("abs", expr)))
opp.AddOperator(PrefixOperator("log",remainingOpChars_ws,3,true,(),fun remOpChars expr -> PrefixOpExpr("log", expr)))

run opp.ExpressionParser "abs(pow(1,2))"
run opp.ExpressionParser "pow(abs(1),2)"

一年后开始复习问题,终于意识到问题所在

我更改了以下代码

let argList = sepBy primitive (str_ws ",")

以下

let opp = new OperatorPrecedenceParser<Expr, string, unit>()
let argList = sepBy opp.ExpressionParser (str_ws ",")

我将 OperatorPrecedenceParser 带到了代码的开头。然后我通过直接将它放入argList来实现递归调用opp.ExpressionParser。

我刚刚意识到 OperatorPrecedenceParser 与 createParserForwardedToRef 非常相似。它首先创建一个解析器,直到后来才写下实现。 FParsec 必须以这种方式实现递归。与其 JSON 示例解析器类似。

改完之后,abs(pow(1,2))和pow(abs(1),2)都可以解析了。希望这可以帮助遇到此问题的其他人。