3

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

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

我以与网络上的许多示例大致相同的方式实现了我的解析器 OPP。我还尝试改变行尾注释消耗空格的位置。我的尝试似乎都不适用于机器人案例(表达式和行尾注释都是可选的)

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

KeyValue: expression  # comment
KeyValue:
KeyValue:  # comment

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

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

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

如何使表达式“可选”。请参阅下面的 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
4

1 回答 1

1

如果我正确理解您的问题,问题似乎是ws1 >>. expressionineKeyValue消耗空格后可能会失败,这会阻止opt组合器成功。

例如,您可以通过定义eKeyValue类似来解决此问题

let eKeyValue = pstringCI "KeyName:" >>. (opt (ws1 >>? expression)) .>> wsBeforeEOL |>> Key

或像重构它一样

let eKeyValue = pstringCI "KeyName:" >>. (ws1 >>. opt expression <|>% None) .>> wsBeforeEOL |>> Key
于 2016-06-13T10:58:41.163 回答