parsec

Full parser examples with parsec?

时光毁灭记忆、已成空白 提交于 2019-12-02 16:18:52
I'm trying to make a parser for a simple functional language, a bit like Caml, but I seem to be stuck with the simplest things. So I'd like to know if there are some more complete examples of parsec parsers, something that goes beyond "this is how you parse 2 + 3". Especially function calls in terms and suchlike. And I've read "Write you a Scheme", but the syntax of scheme is quite simple and not really helping for learning. The most problems I have is how to use try , <|> and choice properly, because I really don't get why parsec never seems to parse a(6) as a function call using this parser:

Should I use a lexer when using a parser combinator library like Parsec?

前提是你 提交于 2019-12-02 15:29:06
When writing a parser in a parser combinator library like Haskell's Parsec, you usually have 2 choices: Write a lexer to split your String input into tokens, then perform parsing on [Token] Directly write parser combinators on String The first method often seems to make sense given that many parsing inputs can be understood as tokens separated by whitespace. In other places, I have seen people recommend against tokenizing (or scanning or lexing , how some call it), with simplicity being quoted as the main reason. What are general trade-offs between lexing and not doing it? nh2 The most

How to parse a Tuple (String,Int) in Haskell using parsec

时光怂恿深爱的人放手 提交于 2019-12-02 13:04:41
I think i already managed to parse strings to strings and strings to Ints but I also need to parse a (String,Int) type as userRatings is in order to read from a textFile correctly and I am using Parsec This is the Parsing along with the import import Text.Parsec ( Parsec, ParseError, parse -- Types and parser , between, noneOf, sepBy, many1 -- Combinators , char, spaces, digit, newline -- Simple parsers ) -- Parse a string to a string stringLit :: Parsec String u String stringLit = between (char '"') (char '"') $ many1 $ noneOf "\"\n" -- Parse a string to a list of strings listOfStrings ::

How do I get Parsec to let me call `read` :: Int?

痴心易碎 提交于 2019-12-02 06:54:19
问题 I've got the following, which type-checks: p_int = liftA read (many (char ' ') *> many1 digit <* many (char ' ')) Now, as the function name implies, I want it to give me an Int. But if I do this: p_int = liftA read (many (char ' ') *> many1 digit <* many (char ' ')) :: Int I get this type error: Couldn't match expected type `Int' with actual type `f0 b0' In the return type of a call of `liftA' In the expression: liftA read (many (char ' ') *> many1 digit <* many (char ' ')) :: Int In an

Parsec <|> choice in parser, Error throws but does not go to next parser

本秂侑毒 提交于 2019-12-02 02:15:45
I am learning haskell with Write yourself a scheme . I am currently trying to implement a char recognition in scheme. A char is #\<character> or #\<character-name> like #\a or #\ or #\space . So i wrote the following code : -- .. some code .. data LispVal = Atom String | List [LispVal] | DottedList [LispVal] LispVal | String String | Number Integer | Bool Bool | Char Char deriving Show -- .... More code ... parseChar :: Parser LispVal parseChar = liftM Char (parseSingleChar <|> parseSpecialCharNotation) parseSingleChar :: Parser Char parseSingleChar = do string "#\\" x <- letter return x

Parsec how to find “matches” within a string

末鹿安然 提交于 2019-12-01 15:37:57
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` space) "" "111 4 22" But what if I don't know what is between the numbers? "I will live to be 111 years <b>old</b> if I work out 4 days a week starting at 22." many anyChar doesn't work as a separator, because it consumes everything. So how can I get things that match an arbitrary parser surrounded by things I want to ignore? EDIT : Note that in the

parsec: string choice parser with useful error messages

橙三吉。 提交于 2019-12-01 09:18:58
Let's have following parser: parser :: GenParser Char st String parser = choice (fmap (try . string) ["head", "tail", "tales"] <?> "expected one of ['head', 'tail', 'tales']") When we parse the malformed input "ta" it will return the defined error but because of backtracking it will also talk about unexpected "t" at first position instead of unexpected " " at position 3. Is there an easy (or built-in) way of matching one of multiple expected strings that produces good error messages? I am talking about showing the correct position and in this case something like expected "tail" or "tales"

Parsing Karva notation in haskell

旧时模样 提交于 2019-12-01 08:28:59
Karva notation is used in Gene Expression Programming to represent mathematical expressions. See here http://www.gene-expression-programming.com/Tutorial002.asp You create an expression tree by reading the off the gene and filling in nodes from left to right, top to bottom. So for example using the operators ( +, * ) and terminals (1,2,3,4,5,6) in "+*+1+2*3456" would evaluate to 39. How would I do this in haskell using attoparsec (or parsec)? karvaParser :: Parser Int karvaParser = ???????????? Prelude> parse karvaParser "+*+1+2*3456" Done 39 AndrewC (I've proved this is a linear time

Parsing Karva notation in haskell

[亡魂溺海] 提交于 2019-12-01 07:06:06
问题 Karva notation is used in Gene Expression Programming to represent mathematical expressions. See here http://www.gene-expression-programming.com/Tutorial002.asp You create an expression tree by reading the off the gene and filling in nodes from left to right, top to bottom. So for example using the operators ( +, * ) and terminals (1,2,3,4,5,6) in "+*+1+2*3456" would evaluate to 39. How would I do this in haskell using attoparsec (or parsec)? karvaParser :: Parser Int karvaParser = ??????????

parsec: string choice parser with useful error messages

那年仲夏 提交于 2019-12-01 06:20:29
问题 Let's have following parser: parser :: GenParser Char st String parser = choice (fmap (try . string) ["head", "tail", "tales"] <?> "expected one of ['head', 'tail', 'tales']") When we parse the malformed input "ta" it will return the defined error but because of backtracking it will also talk about unexpected "t" at first position instead of unexpected " " at position 3. Is there an easy (or built-in) way of matching one of multiple expected strings that produces good error messages? I am