Cannot find Parsec modules in GHCi

I am working on question 67A out of 99 Haskell questions . The question arises of constructing a tree from a given string: "x(y,a(,b))" => Branch 'x' (Branch 'y' Empty Empty) (Branch 'a' Empty (Branch 'b' Empty Empty))
One solution using Parsec as follows:

 import Text.Parsec.String import Text.Parsec hiding (Empty) pTree :: Parser (Tree Char) pTree = do pBranch <|> pEmpty pBranch = do a <- letter char '(' t0 <- pTree char ',' t1 <- pTree char ')' return $ Branch a t0 t1 pEmpty = return Empty stringToTree str = case parse pTree "" str of Right t -> t Left e -> error (show e) 

However, my GHCi could not find Text.Parsec.String and Text.Parsec . Are these modules obsolete? My version of GHCi is 6.12.3

+6
source share
2 answers

Text.Parsec and Text.Parsec.String are modules in the parsec package version 3. The old parsec-2 interface is accessible from the compatibility modules with the traditional names Text.ParserCombinators.Parsec.* , But there is no *.String module new in parsec-3 . If you have installed parsec-2 or no parsec , I recommend installing parsec-3 with the canonical cabal install parsec .

Edit:

If you want to parse less rigid syntax for trees, supporting the input of your example,

 pBranch = do a <- letter do char '(' t0 <- pTree char ',' t1 <- pTree char ')' return $ Branch a t0 t1 <|> return (Branch a Empty Empty) 

By default, two empty children are set if the letter is not followed by an opening bracket.

+8
source

Parsec is in Text.ParserCombinators.Parsec instead of Text.Parsec .

0
source

Source: https://habr.com/ru/post/907191/


All Articles