使 OperatorPrecedenceParser 解析 Optional (opt) 表达式

Making the OperatorPrecedenceParser parse an Optional (opt) expression

我正在实现一个解析器,它为我的一个项目实现特定领域的特定语言。

我遇到困难的一个方面是制作一个表达式(使用 FParsec 的 OperatorPrecedenceParser 实现)使得整个表达式是可选的。

我实现解析器 OPP 的方式与网络上的许多示例大致相同。我也试过在行尾注释中使用空格的地方移动。 None 我的尝试似乎在机器人案例中有效(表达式和行尾注释都是可选的)

具体来说(在下面的例子中实现),我试图让以下内容成功解析。

KeyValue: expression  # comment
KeyValue:
KeyValue:  # comment

其中表达式是可选的,表达式后面有一个可选的注释。 "KeyValue:" 在这个例子中是硬编码的,但在我的主要解析器代码中,它是一个标识符。

N.B。对于下面的示例。我已经实现了一个最小表达式,它解析一个简单的中缀加法 2 个浮点数。完整的解析器实现了更多。

N.B.2。表达式必须在 : 和表达式之间至少有 1 个空格。 # 注释字符根本不会出现在表达式中。

如何使表达式 "optional"。请参阅下面的 eKeyValue FParsec 解析器类型。

以下示例包含6个示例案例。所有的案例都应该有效。我试过将 wsBeforeEOL 添加到条款的末尾,使表达式可选 (opt),但似乎没有任何效果。看来OPP一直在消耗,从不失败。

下面示例程序的输出如下所示:

Test1 - Failure:
Error in Test1 - No Expression but comment: Ln: 1 Col: 15
  KeyName:    # No Expression but comment
              ^
Expecting: floating-point number or '('

Test2 - Success: Key (Some (Arithmetic (Number 2.0,Plus,Number 2.0)))
Test3 - Success: Key (Some (Arithmetic (Number 3.0,Plus,Number 3.0)))
Test4 - Success: Key (Some (Arithmetic (Number 3.0,Plus,Number 4.0)))
Test5 - Success: Key (Some (Arithmetic (Number 4.0,Plus,Number 4.0)))
Test6 - Success: Key null
Press any key to continue . . .

示例程序为(以上测试用例在Main()函数中:

open FParsec


    // Placeholder for state...
    type UserState = 
        {
            dummy: int            
        }
        with
            static member Create() = {dummy = -1}


    type Operator =
        | Plus

    type Expression =
        | Number of float
        | Arithmetic of Expression * Operator * Expression   // Composes 2 primatives

    type Statement =
        | Key of Expression option // Optional expression name


// very simple parsers which handles a simple string on one line.
// White space handling
let isBlank = fun c -> c = ' ' || c = '\t'
let ws1 = skipMany1SatisfyL isBlank "whitespace"
let ws = skipManySatisfy isBlank
let comment = pstring "#" >>. skipRestOfLine false
let wsBeforeEOL = skipManySatisfy isBlank >>. optional comment

// Parse a number
let sNumber = pfloat .>> wsBeforeEOL |>> Number // Test wsExpression ending

// The expression reference
let expression, expressionRef = createParserForwardedToRef()

let expressionFragment = choice [sNumber] //;sBool;sNull;sString;sIdentifier]
let bracketedExpressionFragment = between (pstring "(" .>> ws) (pstring ")" .>> ws) expression

// The parser for addition only
let oppa = new OperatorPrecedenceParser<Expression, unit, UserState>()

let parithmetic = oppa.ExpressionParser

//oppa.TermParser <- (expressionFragment .>> wsBeforeEOL)   <|> (bracketedExpressionFragment .>> wsBeforeEOL)
oppa.TermParser <- choice[expressionFragment;bracketedExpressionFragment]
oppa.AddOperator(InfixOperator("+", ws, 1, Associativity.Left, fun x y -> Arithmetic(x, Plus, y)))
expressionRef := oppa.ExpressionParser


// *** HERE: Define the Key, with optional expression, which must have at lease 1 WS,, then followed by wsBeforeEOL, which is multiple blanks and optional comment.
let eKeyValue = pstringCI "KeyName:" >>. (opt (ws1 >>. expression)) .>> wsBeforeEOL |>> Key

// Define the parser for the whole string...in this case a single line
let htmlProgramParser = spaces >>. eKeyValue .>> spaces .>> eof

// test harnes on a string
let parseHtmlProgramString programName str =
    runParserOnString htmlProgramParser (UserState.Create()) programName str //with

[<EntryPoint>]
let main argv = 
    printfn "%A" argv

    let test1 =
        "  KeyName:    # No Expression but comment"
        |> parseHtmlProgramString "Test1 - No Expression but comment"
        |> printfn "Test1 - %A"

    let test2 =
        "  KeyName:  2+2  # Expression and Comment"
        |> parseHtmlProgramString "Test2 - 2+2 # Expression and Comment"
        |> printfn "Test2 - %A"

    let test3 =
        "  KeyName:  3 + 3  # # Expression and Comment2"
        |> parseHtmlProgramString "Test3 - 3 + 3 # Expression and Comment2 (Spaces)"
        |> printfn "Test3 - %A"

    let test4 =
        "  KeyName:  (3 + 4)  # Bracketed Expression and Comment"
        |> parseHtmlProgramString "Test4 - (3 + 4) # Bracketed Expression and Comment"
        |> printfn "Test4 - %A"

    let test5 =
        "  KeyName:  (4 + 4)  "
        |> parseHtmlProgramString "Test5 - (4 + 4) # Expression + <no comment>"
        |> printfn "Test5 - %A"

    let test6 =
        "  KeyName:"
        |> parseHtmlProgramString "Test6 - <no expression> <no comment>"
        |> printfn "Test6 - %A"

    0 // return an integer exit code

如果我正确理解你的问题,问题似乎是 eKeyValue 中的 ws1 >>. expression 在消耗空格后可能会失败,从而阻止 opt 组合子成功。

您可以通过定义 eKeyValue

来解决这个问题
let eKeyValue = pstringCI "KeyName:" >>. (opt (ws1 >>? expression)) .>> wsBeforeEOL |>> Key

或像

一样重构它
let eKeyValue = pstringCI "KeyName:" >>. (ws1 >>. opt expression <|>% None) .>> wsBeforeEOL |>> Key