parsec

Parsing non binary operators with Parsec

﹥>﹥吖頭↗ 提交于 2021-02-08 03:28:07
问题 Traditionally, arithmetic operators are considered to be binary (left or right associative), thus most tools are dealing only with binary operators. Is there an easy way to parse arithmetic operators with Parsec, which can have an arbitrary number of arguments? For example, the following expression should be parsed into the tree (a + b) + c + d * e + f 回答1: Yes! The key is to first solve a simpler problem, which is to model + and * as tree nodes with only two children. To add four things, we

Parsing non binary operators with Parsec

半城伤御伤魂 提交于 2021-02-08 03:28:01
问题 Traditionally, arithmetic operators are considered to be binary (left or right associative), thus most tools are dealing only with binary operators. Is there an easy way to parse arithmetic operators with Parsec, which can have an arbitrary number of arguments? For example, the following expression should be parsed into the tree (a + b) + c + d * e + f 回答1: Yes! The key is to first solve a simpler problem, which is to model + and * as tree nodes with only two children. To add four things, we

Why Parsec's sepBy stops and does not parse all elements?

爷,独闯天下 提交于 2021-02-08 02:10:21
问题 I am trying to parse some comma separated string which may or may not contain a string with image dimensions. For example "hello world, 300x300, good bye world" . I've written the following little program: import Text.Parsec import qualified Text.Parsec.Text as PS parseTestString :: Text -> [Maybe (Int, Int)] parseTestString s = case parse dimensStringParser "" s of Left _ -> [Nothing] Right dimens -> dimens dimensStringParser :: PS.Parser [Maybe (Int, Int)] dimensStringParser = (optionMaybe

Why Parsec's sepBy stops and does not parse all elements?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 02:05:47
问题 I am trying to parse some comma separated string which may or may not contain a string with image dimensions. For example "hello world, 300x300, good bye world" . I've written the following little program: import Text.Parsec import qualified Text.Parsec.Text as PS parseTestString :: Text -> [Maybe (Int, Int)] parseTestString s = case parse dimensStringParser "" s of Left _ -> [Nothing] Right dimens -> dimens dimensStringParser :: PS.Parser [Maybe (Int, Int)] dimensStringParser = (optionMaybe

FParsec: how to parse date in fparsec (newbie)

▼魔方 西西 提交于 2020-01-14 19:06:48
问题 I am using the Bill Casarin post on how to parse delimited files with fparsec, I am dumbing the logic down to get an understanding of how the code works. I am parsing a multi row delimited document into Cell list list structure (for now) where a Cell is a string or a float. I am a complete newbie on this. I am having issues parsing the floats - in a typical case (a cell delimitted by tabs, containing a numeric) it works. However when a cell happens to be a string that starts with a number -

FParsec: how to parse date in fparsec (newbie)

旧时模样 提交于 2020-01-14 19:06:25
问题 I am using the Bill Casarin post on how to parse delimited files with fparsec, I am dumbing the logic down to get an understanding of how the code works. I am parsing a multi row delimited document into Cell list list structure (for now) where a Cell is a string or a float. I am a complete newbie on this. I am having issues parsing the floats - in a typical case (a cell delimitted by tabs, containing a numeric) it works. However when a cell happens to be a string that starts with a number -

Parsec3 blocked by parsec, what ever I do

烂漫一生 提交于 2020-01-06 03:16:26
问题 using a cabal file looking like this (the relevant library part): build-depends: base >=4.8 && < 4.9, filepath >=1.4 && <1.5, time >=1.5 && <1.6, bytestring >=0.10 && <0.11, unix >=2.7 && <2.8, cryptohash >=0.11 && <0.12, process >=1.2 && <1.3, transformers >= 0.4 && < 0.5, text >= 1.2 && <= 1.3, base16-bytestring >= 0.1.1 && < 1.1.2, utf8-string >= 1 && < 1.1, directory >=1.2 && <1.3, regex-base >= 0.9 && < 1.0, regex-pcre >= 0.94 && < 0.95, regex-base >= 0.93 && < 0.94, direct-sqlite >=2.3

Why doesn't Parsec backtrack when one part of the parser succeeds and the rest fails?

假如想象 提交于 2020-01-04 13:11:31
问题 I have this parsec parser : a = optionMaybe $ do {try $ spaceornull *> string "hello";string "No"} Where spaceornull is ((:[]) <$> try space) <|> string "" When I test a with input " " I get : Left (line 1, column 2): unexpected end of input expecting "hello" I don't understand this, spaceornull *> string "hello" should fail because there is no "hello", then with try parsec backtracks and now there is no consumed input but try fails anyway so the parser passed to optionMaybe (the one inside

Why doesn't Parsec backtrack when one part of the parser succeeds and the rest fails?

丶灬走出姿态 提交于 2020-01-04 13:11:07
问题 I have this parsec parser : a = optionMaybe $ do {try $ spaceornull *> string "hello";string "No"} Where spaceornull is ((:[]) <$> try space) <|> string "" When I test a with input " " I get : Left (line 1, column 2): unexpected end of input expecting "hello" I don't understand this, spaceornull *> string "hello" should fail because there is no "hello", then with try parsec backtracks and now there is no consumed input but try fails anyway so the parser passed to optionMaybe (the one inside

heap usage for CPS vs non-CPS parsers in Haskell's parsec

試著忘記壹切 提交于 2020-01-02 13:56:25
问题 I'm trying to write the following parser using parsec: manyLength :: forall s u m a. Monad m => ParsecT s u m a -> ParsecT s u m Int manyLength p = go 0 where go :: Int -> ParsecT s u m Int go !i = (p *> go (i + 1)) <|> pure i This is like the many function, but instead of returning [a] , it returns the number of times Parser a succeeds. This works, but I can't seem to make it run in constant heap space. This makes sense, since the recursive call to go is not in the tail-call position. If