5

FParsec を使用して F# で Mathematica 言語のパーサーを作成しようとしています。

f x y = (f(x))(y)関数適用の優先順位が高い構文をサポートする MiniML 用に作成しました。ここで、同じ構文を意味に使用する必要があるf*x*yため、乗算と同じ優先順位が必要です。特にx y + 2 = x*y + 2x y ^ 2 = x * y^2.

これはどのように達成できますか?

4

1 に答える 1

7

Stephan がコメントで指摘したように、演算子パーサーを 2 つの個別のパーサーに分割し、スペースで区切られた式の中間に独自のパーサーを配置できます。次のコードはこれを示しています。

#I "../packages/FParsec.1.0.1/lib/net40-client"
#r "FParsec"
#r "FParsecCS"

open FParsec
open System.Numerics

type Expr =
  | Int of BigInteger
  | Add of Expr * Expr
  | Mul of Expr * Expr
  | Pow of Expr * Expr

let str s = pstring s >>. spaces
let pInt : Parser<_, unit> = many1Satisfy isDigit |>> BigInteger.Parse .>> spaces
let high = OperatorPrecedenceParser<Expr,unit,unit>()
let low = OperatorPrecedenceParser<Expr,unit,unit>()
let pHighExpr = high.ExpressionParser .>> spaces
let pLowExpr = low.ExpressionParser .>> spaces

high.TermParser <-
  choice
    [ pInt |>> Int
      between (str "(") (str ")") pLowExpr ]

low.TermParser <-
  many1 pHighExpr |>> (function [f] -> f | fs -> List.reduce (fun f g -> Mul(f, g)) fs) .>> spaces

low.AddOperator(InfixOperator("+", spaces, 10, Associativity.Left, fun f g -> Add(f, g)))
high.AddOperator(InfixOperator("^", spaces, 20, Associativity.Right, fun f g -> Pow(f, g)))

run (spaces >>. pLowExpr .>> eof) "1 2 + 3 4 ^ 5 6"

出力は次のとおりです。

Add (Mul (Int 1,Int 2),Mul (Mul (Int 3,Pow (Int 4,Int 5)),Int 6))

これは1 * 2 + 3 * 4^5 * 6期待どおりです。

于 2015-03-29T20:10:38.097 に答える