OperatorPrecedenceParser 抛出关于我没有的负优先级的异常

OperatorPrecedenceParser throw exception about negative priority which I don't have

我正在为基于 lambda 演算的编程语言创建解析器。我添加了一个中缀运算符及其优先级,但解析器因负优先级错误而崩溃。我可以手动解析运算符,但似乎无法获得正确的优先级。所以我想我不妨学习使用 OperatorPrecedenceParser。

我会展示代码,因为我不知道它为什么会崩溃,因为我没有任何负面优先级。

语言 AST

module MiniML
type Exp =
            | C of Cst 
            | Id of Id 
            | Lam of Id * Exp 
            | App of Exp * Exp 
            | Let of Id * Exp * Exp
            | Pair of Exp * Exp
            | If of Exp * Exp * Exp
and Cst  = I of int | B of bool | Unit | Nil
and Id   = string;;

let op = ["+"; 
      "-";
      "*"; 
      "/"; 
      "="; 
      "<"; 
      ">"; 
      "@"; 
      "and"; 
      "or"; 
      ","; 
      "::"
    ]

这是解析器本身。这是我第一次使用解析器组合器(和解析),所以如果有什么严重错误,我想知道。否则,只要知道它崩溃的原因就足够了。

open MiniML
open FParsec

let ws = spaces

let operator : Parser<MiniML.Id,unit> = op |> List.map pstring |> choice

let keyword : Parser<string,unit> = ["false";"true";"let";"end";"in";"if";"then";"else";"lam"] |> List.map pstring |> choice

let fstId = asciiLetter <|> pchar '_'

let restId = fstId <|> digit <|> pchar '''

let betweenPar p = between (pchar '(' .>> ws) (pchar ')' .>> ws) p

let cstB = (stringReturn "true"  (B true)) <|> (stringReturn "false" (B false))

let cstI = puint32 |>> (int >> I)

let cstU = stringReturn "()" Unit

let cstN = stringReturn "[]" Nil

let expC : Parser<Exp,unit> = cstB <|> cstI <|> cstU <|> cstN  |>> C

let expIdStr = notFollowedByL keyword "Cannot use keyword as variable" >>. 
                    notFollowedByL operator "Cannot use operator as variable" >>. 
                        many1CharsTill2 fstId restId (notFollowedBy restId)

let expId  : Parser<Exp,unit> = expIdStr |>> (MiniML.Exp.Id)

let exp, expRef = createParserForwardedToRef<Exp, unit>()

let expApp, expAppRef = createParserForwardedToRef<Exp, unit>()

let expLam : Parser<Exp,unit> = (pstring "lam" >>. ws >>. expIdStr .>> ws .>> pchar '.') .>> ws .>>. exp |>> Lam

let expLet = tuple3 (pstring "let" >>. ws >>. expIdStr .>> ws .>> pchar '=' .>> ws) (exp .>> ws .>> pstring "in" .>> ws) (exp .>> ws .>> pstring "end") |>> Let

let expIf = tuple3 (pstring "if" >>. ws >>. exp .>> ws) (pstring "then" >>. ws >>. exp .>> ws) (pstring "else" >>. ws >>. exp) |>> If

let closeEXP, closeEXPRef = createParserForwardedToRef<Exp, unit>()

let expBang = (pstring "!" >>% MiniML.Id "!") .>>. closeEXP |>> App

let buildList (el,ef)  = 
    let rec go l = match l with 
                       | (e::es) -> App(MiniML.Id "cons", Pair(e,go es))
                       | [] -> C Nil
    go (el @ [ef])

let expList = between (pchar '[' .>> ws) (pchar ']') (many (exp .>>? (ws .>> pchar ';' .>> ws)) .>>. exp .>> ws 
                |>> buildList )

do closeEXPRef := choice [expC ; expId ; expBang ; betweenPar exp ; expList]  .>> ws

do expAppRef := many1 closeEXP |>> (function (x::xs) -> List.fold (fun x y -> App(x,y)) x xs | [] -> failwith "Impossible")

let opOpp : InfixOperator<Exp,unit,unit> list = 
        [
          InfixOperator("*", ws, 6, Associativity.Left, fun x y -> App(MiniML.Id "*",Pair(x,y))); 
          InfixOperator("/", ws, 6, Associativity.Left, fun x y -> App(MiniML.Id "/",Pair(x,y)));
          InfixOperator("+", ws, 5, Associativity.Left, fun x y -> App(MiniML.Id "+",Pair(x,y))); 
          InfixOperator("-", ws, 5, Associativity.Left, fun x y -> App(MiniML.Id "-",Pair(x,y)));
          InfixOperator("::", ws,4, Associativity.Right, fun x y -> App(MiniML.Id "cons",Pair(x,y)));
          InfixOperator("=", ws, 3, Associativity.Left, fun x y -> App(MiniML.Id "=",Pair(x,y)));  
          InfixOperator("<", ws, 3, Associativity.Left, fun x y -> App(MiniML.Id "<",Pair(x,y)));  
          InfixOperator(">", ws, 3, Associativity.Left, fun x y -> App(MiniML.Id ">",Pair(x,y)));  
          InfixOperator("and", ws, 2, Associativity.Right, fun x y -> App(MiniML.Id "and",Pair(x,y))); 
          InfixOperator("or", ws, 1, Associativity.Right, fun x y -> App(MiniML.Id "or",Pair(x,y))); 
          InfixOperator(",", ws,0, Associativity.None, fun x y -> Pair(x,y) )
        ]

let opp = new OperatorPrecedenceParser<Exp,unit,unit>()
let expr = opp.ExpressionParser
let term = exp <|> betweenPar expr
opp.TermParser <- term
List.iter (fun x -> opp.AddOperator(x)) opOpp

do expRef := [expLam;expIf;expLet;expApp] |>  choice |> (fun p -> p .>>. opt (expOp operator) |>> binOp )

let mainExp = expr .>> eof

您的示例代码似乎不完整,因为未包含 expOpbinOp。当我 运行 你的代码没有最后两行时,OPP 在添加逗号运算符时抛出一个 ArgumentOutOfRangeException 和消息 "The operator precedence must be greater than 0." 。问题是您将 0 指定为逗号运算符的优先级。

当您将 IDE 与 Visual Studio 等完全集成的调试器一起使用时,此类问题更容易诊断。