【问题标题】:How to parse prefix function such as Pow() with multiple parameters using FParsec如何使用 FParsec 解析具有多个参数的 Pow() 等前缀函数
【发布时间】:2016-11-01 19:35:02
【问题描述】:

我尝试使用 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)"

【问题讨论】:

  • 见这里:stackoverflow.com/questions/9197687/…(以及相关问题)。
  • 简而言之 - 将函数应用程序建模为 TermParser 的一部分而不是一元运算符是一个更好的主意。
  • 为什么你不想接受上面写的建议?
  • 看了看...代码相当混乱。我建议你看看MathNet.Symbolics 中的解析器是如何实现的——代码干净易懂。
  • 对于初学者来说,最好将与解析相关的所有内容都放在一个单独的模块中。

标签: parsing f# fparsec


【解决方案1】:

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

我已更改以下代码

let argList = sepBy primitive (str_ws ",")

到下面

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2012-02-07
    相关资源
    最近更新 更多