Questions tagged [fparsec]

A parser combinator library for F#.

FParsec is a parser combinator library for F#.

FParsec’s features include:

  • support for context‐sensitive, infinite look‐ahead grammars,
  • automatically generated, highly readable error messages,
  • Unicode support,
  • efficient support for very large files,
  • an embeddable, runtime‐configurable operator‐precedence parser component,
  • a simple, efficient and easily extensible API,
  • an implementation thoroughly optimized for performance,
  • comprehensive documentation,
  • a permissive open source license.

FParsec is an F# adaptation of Parsec, the popular parser combinator library for Haskell.

FParsec is optimized for an applicative programming style, but it also supports a monadic syntax similar to Parsec's.

See http://www.quanttec.com/fparsec/ for more information.

191 questions
6
votes
2 answers

FParsec: how to omit `many` parser failures from error messages

Consider this parser that converts digit strings to ints: let toInt (s:string) = match Int32.TryParse(s) with | (true, n) -> preturn n | _ -> fail "Number must be below 2147483648" let naturalNum = many1Chars digit >>= toInt…
Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65
6
votes
3 answers

FParsec: backtracking `sepBy`

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…
Carl Patenaude Poulin
  • 6,238
  • 5
  • 24
  • 46
6
votes
2 answers

Using ParserResult

The example code below appears to work nicely: open FParsec let capitalized : Parser =(asciiUpper >>. many asciiLower >>. eof) let inverted : Parser =(asciiLower >>. many asciiUpper >>. eof) let capsOrInvert =choice…
Carbon
  • 3,828
  • 3
  • 24
  • 51
6
votes
1 answer

Use FParsec to parse float or int*float

I've just started out playing around with FParsec, and I'm now trying to parse strings on the following format 10*0.5 0.25 0.75 3*0.1 0.9 I want 3*0.1, for example, to be expanded into 0.1 0.1 0.1 What I have so far is the following type UserState…
larsjr
  • 665
  • 7
  • 17
6
votes
1 answer

FParsec choice behaves in unexpected ways

I plan to use FParsec for the prototype of a larger project of mine. So I decided to get my first experiences with this library by means of the test program listed below. But it seems that the combination of my basic parsers (which seem to work) by…
BitTickler
  • 10,905
  • 5
  • 32
  • 53
6
votes
1 answer

Picking blocks out of largely free-form text with FParsec

I'm trying to parse some information out of largely free-form text. I attempted an implementation in FParsec, but I haven't used it before and I'm not sure if I'm doing it wrong, or even if it is well-suited to this particular problem. Problem…
David Tchepak
  • 9,826
  • 2
  • 56
  • 68
5
votes
2 answers

FParsec identifiers vs keywords

For languages with keywords, some special trickery needs to happen to prevent for example "if" from being interpreted as an identifier and "ifSomeVariableName" from becoming keyword "if" followed by identifier "SomeVariableName" in the token…
Hans
  • 2,230
  • 23
  • 23
5
votes
1 answer

Using preprocessing function with identifier parser in FParsec?

I am using the identifier parser from FParsec to parse the names of variables and functions, which are normally a mixture of Unicode and ASCII characters. But sometimes I have escaped Unicode characters in the beginning (like \u03C0) or within the…
Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115
5
votes
2 answers

Can someone give an example of using chainl1 in FParsec?

This is the most puzzling combinator in all of FParsec... http://www.quanttec.com/fparsec/reference/primitives.html#members.chainl1 ...but there is no example on how to use it in the documentation or, AFAIK, on any web pages on the internet. I have…
Bryan Edds
  • 1,696
  • 12
  • 28
5
votes
1 answer

Chunked Parsing with FParsec

Is it possible to submit input to an FParsec parser in chunks, as from a socket? If not, is it possible to retrieve the current result and unparsed portion of an input stream so that I might accomplish this? I'm trying to run the chunks of input…
user29439
5
votes
1 answer

How to resolve FParsec error "The combinator 'many' was applied to a parser that succeeds without consuming..."

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 { …
Bryan Edds
  • 1,696
  • 12
  • 28
5
votes
2 answers

Parsing simple types in FParsec

I'm trying to parse standard simple types (in the sense of lambda calculus) using FParsec, but I've having difficulty going from a Lex/Yacc style to that used in FParsec, specifically with respect to recursive definitions. Examples of the types I am…
rneatherway
  • 553
  • 3
  • 11
5
votes
2 answers

How to parse seq of words separated by double spaces using fparsec?

Given the input: alpha beta gamma one two three How could I parse this into the below? [["alpha"; "beta"; "gamma"]; ["one"; "two"; "three"]] I can write this when there is a better separator (e.g.__), as then sepBy (sepBy word (pchar ' '))…
Simon P
  • 1,196
  • 1
  • 12
  • 26
5
votes
1 answer

Parsing separated lists with FParsec

I am trying to parse something that may be a list of items, or which may be just one item. I want to put the results into a DU (Thing below). The way I'm approaching this is as below, but it gives me a list of things even when there is only one…
Sean Kearon
  • 10,987
  • 13
  • 77
  • 93
5
votes
2 answers

Parsing in to a recursive data structure

I wish to parse a string in to a recursive data structure using F#. In this question I'm going to present a simplified example that cuts to the core of what I want to do. I want to parse a string of nested square brackets in to the record…
Lawrence
  • 3,287
  • 19
  • 32
1
2
3
12 13