Questions tagged [parsec]

Parsec is an industrial-strength, monadic parser combinator library for Haskell.

Parsec is an industrial strength, monadic parser combinator library for Haskell. Parsec lets you construct parsers by combining higher-order Combinators to create larger expressions. Combinator parsers are written and used within the same programming language as the rest of the program. The parsers are first-class citizens of the language , unlike Happy parsers, which must be generated via a preprocessor.

More information about Parsec, including usage examples, can be found on the Parsec website.

609 questions
10
votes
1 answer

Parsec and custom parsing error type

Is it possible somehow to get parse error of some custom type? It would be cool to get more information about parsing context from error for example. And it seems not very convenient to have error info just in the form of text message.
sergeyz
  • 1,308
  • 2
  • 14
  • 23
9
votes
3 answers

Parsec debugging

I've been working with parsec and I have trouble debugging my code. For example, I can set a breakpoint in ghci, but I'm not sure how to see how much of the input has been consumed, or things like that. Are there tools / guidelines to help with…
Xodarap
  • 11,581
  • 11
  • 56
  • 94
9
votes
3 answers

Parsec - error "combinator 'many' is applied to a parser that accepts an empty string"

I'm trying to write a parser using Parsec that will parse literate Haskell files, such as the following: The classic 'Hello, world' program. \begin{code} main = putStrLn "Hello, world" \end{code} More text. I've written the following,…
stusmith
  • 14,003
  • 7
  • 56
  • 89
9
votes
1 answer

Haskell parsec: `many` combinator inside an `optional` combinator

I'd like to implement this grammar rule using Haskell's parsec library: ((a | b | c)* (a | b))? Which is a parser rule that accepts an optional (i.e. potentially empty) string. If the string it acccepts is not empty, then it can be consumed by…
Rob Stewart
  • 1,812
  • 1
  • 12
  • 25
9
votes
2 answers

Why does it seem that the Parsec Choice operator depends on order of the parsers?

I am trying to parse a very simple language that consists of only decimal or binary numbers. For example, here are some valid inputs: #b1 #d1 #b0101 #d1234 I am having a problem using Parsec's choice operator: <|>. According to the tutorial: Write…
mandark
  • 762
  • 5
  • 21
9
votes
2 answers

parsec: is there an easy way to allow comments/whitespace everywhere in the grammar?

How do you deal with whitespace and comments? The fragments that are usually removed during syntactic analysis stage? I want to enable comments everywhere in my document that I am parsing. Is adding these in every elementary parser that I define the…
akonsu
  • 28,824
  • 33
  • 119
  • 194
9
votes
4 answers

Parsec how to find "matches" within a string

How can I use parsec to parse all matched input in a string and discard the rest? Example: I have a simple number parser, and I can find all the numbers if I know what separates them: num :: Parser Int num = read <$> many digit parse (num `sepBy`…
Sean Clark Hess
  • 15,859
  • 12
  • 52
  • 100
9
votes
4 answers

Generalized Bottom up Parser Combinators in Haskell

I am wondered why there is no generalized parser combinators for Bottom-up parsing in Haskell like a Parsec combinators for top down parsing. ( I could find some research work went during 2004 but nothing after…
Panini
  • 91
  • 6
9
votes
1 answer

Haskell Parsec, adapting oneOf to [String]

I'm going through the Write yourself a scheme in 48 hours tutorial. symbol :: Parser Char symbol = oneOf "!#$%&|*+-/:<=>?@^_~" This is great for symbols, but what if I have a list of keywords? (i.e. struct, int) can oneOf be adapted to lists?…
The Internet
  • 7,959
  • 10
  • 54
  • 89
9
votes
1 answer

Testing Parsec parsers by generating inputs with QuickCheck

I'd like to write tests for a suite of Parsec parsers. Here's a simple example of a parser I want to test with QuickCheck: identifier = do c <- letter cs <- many (alphaNum <|> oneOf identSymbols) skipSpaces return $ Ident $ c:cs So,…
Derek Thurn
  • 14,953
  • 9
  • 42
  • 64
8
votes
2 answers

Using Parsec to parse regular expressions

I'm trying to learn Parsec by implementing a small regular expression parser. In BNF, my grammar looks something like: EXP : EXP * | LIT EXP | LIT I've tried to implement this in Haskell as: expr = try star <|> try litE <|>…
Xodarap
  • 11,581
  • 11
  • 56
  • 94
8
votes
1 answer

How to parse ternary expression using Parsec?

buildExpressionParser only deals with unary and binary operators. Can it handle ternary operators like ?:? There are some discussions here and here, but none is conclusive.
sinoTrinity
  • 1,125
  • 2
  • 15
  • 27
8
votes
1 answer

Indentation using Megaparsec

I would like to parse a basic indented language using Megaparsec. Originally I was using Parsec which I managed to get working correctly with indentation but now I'm having quite some trouble. I've been following a tutorial here and here's the code…
Michael
  • 3,411
  • 4
  • 25
  • 56
8
votes
0 answers

Elegant way to parse "line splices" (backslashes followed by a newline) in megaparsec

for a small compiler project we are currently working on implementing a compiler for a subset of C for which we decided to use Haskell and megaparsec. Overall we made good progress but there are still some corner cases that we cannot correctly…
Chirs
  • 567
  • 2
  • 15
8
votes
2 answers

Why does ParsecT type have 'u' argument?

Documentation for the parsec package states that u argument is used to carry some user state through monadic computation. But the same functionality can be achieved by basing ParsecT monad transformer on State monad. So if my parser is not stateful,…
arrowd
  • 33,231
  • 8
  • 79
  • 110