fparsec

How do I test for exactly 2 characters with fparsec?

北城余情 提交于 2020-01-02 05:38:09
问题 I have the following program that runs. It takes a line of text and splits it into two parts, the first is an identifier and the second is the remainder of the line. My parser for the identifier (factID) takes any string of characters as the identifier, which is not (quite) what I want. What I want is a parser that only succeeds when it encounters two consecutive upper case letters. So for example "AA" should succeed while "A", "A1" or "AAA" should not. What I can't figure out is how

FParsec: how to combine parsers so that they will be matched in arbitrary order

霸气de小男生 提交于 2019-12-31 03:19:25
问题 The task is find particular key-value pairs and parse them. The pairs can occur in any order. My partially working attempt: open FParsec type Parser<'a> = Parser<'a, unit> type Status = Running | Done type Job = { Id: int Status: Status Count: int } let ws = spaces let jobId: Parser<int> = ws >>. skipStringCI "Job id" >>. ws >>. skipChar '=' >>. ws >>. pint32 let status: Parser<Status> = ws >>. skipStringCI "Status" >>. ws >>. skipChar '=' >>. ws >>. ( (skipStringCI "Running" >>% Running) <|>

Tail-recursion in FParsec

谁说胖子不能爱 提交于 2019-12-22 10:36:24
问题 I have encounter a problem with parsers having two branches of recursion. To demonstrate the problem easier, I use a simple grammar of a lambda calculus from the article written by Luca Bolognese as the example: <expression> ::= <name> | <function> | <application> <name> ::= non­blank character sequence <function> ::= \ <name> . <body> <body> ::= <expression> <application> ::= ( <function expression> <argument expression> ) <function expression> ::= <expression> <argument expression> ::=

How to resolve FParsec error “The combinator 'many' was applied to a parser that succeeds without consuming…”

故事扮演 提交于 2019-12-22 06:56:37
问题 I have a parser that seems straight-forward enough. I added this sub-parser to the end to give info about general parsing errors since all the other sub-parsers failed - /// Read the rest of a line as an error. let readError = parse { let! restOfLineStr = restOfLine true return makeViolation ("Read error on: " + restOfLineStr + ".") } /// Read an expression. do readExprRef := choice [attempt readBoolean attempt readCharacter attempt readString attempt readInt attempt readError] // just now

Parsing “x y z” with the precedence of multiply

非 Y 不嫁゛ 提交于 2019-12-21 16:52:45
问题 I'm trying to write a parser for the Mathematica language in F# using FParsec. I have written one for a MiniML that supports the syntax f x y = (f(x))(y) with high precedence for function application. Now I need to use the same syntax to mean f*x*y and, therefore, have the same precedence as multiply. In particular, x y + 2 = x*y + 2 whereas x y ^ 2 = x * y^2 . How can this be accomplished? 回答1: As Stephan pointed out in a comment you can split the operator parser into two separate parsers

F#, FParsec, and Calling a Stream Parser Recursively

家住魔仙堡 提交于 2019-12-20 04:51:47
问题 I'm developing a multi-part MIME parser using F# and FParsec. I'm developing iteratively, and so this is highly unrefined, brittle code--it only solves my first immediate problem. Red, Green, Refactor. I'm required to parse a stream rather than a string, which is really throwing me for a loop. Given that constraint, to the best of my understanding, I need to call a parser recursively. How to do that is beyond my ken, at least with the way I've proceeded thus far. namespace MultipartMIMEParser

Fparsec recursive grammatics throw StackOverflowException

假装没事ソ 提交于 2019-12-19 11:04:15
问题 I've got this code type Exprs = | Val of float | Mult of Exprs * Exprs | Plus of Exprs * Exprs let pexpr, exprRef = createParserForwardedToRef<Exprs, unit>() let pval = pfloat |>> Val let binaryOp s = (ws >>. pexpr.>> ws) .>>. (ws >>. str s >>. ws >>. pexpr) let pplus = binaryOp "+" |>> Plus let pmuil = binaryOp "*" |>> Mult do exprRef := choice [ attempt pmuil pplus pval ] let expression = ws >>. pexpr .>> ws When it evaluated it throws StackoverflowExcpetion. So the question is how can i

Recursive grammars in FParsec

限于喜欢 提交于 2019-12-18 12:24:52
问题 I've decided to check out FParsec, and tried to write a parser for λ expressions. As it turns out, eagerness makes recursive parsing difficult. How can I solve this? Code: open FParsec type λExpr = | Variable of char | Application of λExpr * λExpr | Lambda of char * λExpr let rec FV = function | Variable v -> Set.singleton v | Application (f, x) -> FV f + FV x | Lambda (x, m) -> FV m - Set.singleton x let Λ0 = FV >> (=) Set.empty let apply f p = parse { let! v = p return f v } let λ e = let

FParsec: backtracking `sepBy`

怎甘沉沦 提交于 2019-12-12 10:54:36
问题 Consider the following toy grammar and parser: (* in EBNF: ap = "a", { "ba" } bp = ap, "bc" *) let ap = sepBy1 (pstring "a") (pstring "b") let bp = ap .>> (pstring "bc") let test = run bp "abababc" I get the following output: Error in Ln: 1 Col: 7 abababc ^ Expecting: 'a' Clearly sepBy1 sees the last b and expects it to lead into another a , failing when it doesn't find one. Is there a variant of sepBy1 which would backtrack over b and make this parse succeed? Is there any reason why I

FParsec - How to parse from standard input stream

强颜欢笑 提交于 2019-12-12 03:56:29
问题 I can't seem to successfully parse from standard input stream with FParsec. I reduced my case to this very simple code : match (runParserOnStream (pstring "test" .>> FParsec.CharParsers.newline) () "stdin" (Console.OpenStandardInput ()) Console.InputEncoding) with | Success(result, _, _) -> printfn "Success: %A" result | Failure(errorMsg, perr, _) -> printfn "Failure: %s" errorMsg But when i run the program, enter the string test, and then press Enter, it hangs there, and i can't seem to