![haskell parsec haskell parsec](https://www.haskell.org/communities/11-2015/html/ihaskell-diagrams.png)
Main = forever $ do putStrLn "Enter an integer: " This makes things quite simple: import Control.Monad
#HASKELL PARSEC CODE#
Creating a full-blown parser for this might seem like overkill, but this will allow us to create fast, readable code with good error messages, all while remaining short and concise.įor our first go at creating a parser for integers, we'll ignore the optional leading signs. To start things simple, we'll create a parser for integers. Not only does this result in code that is short and easy to understand, it provides good performance and helpful error messages for free. Through libraries such as Parsec it's possible to build up complex parsers by using simple parsers and parser combinators. Luckily, parsing is a task that Haskell is especially well-suited for. The biggest problem with writing parsers by hand, however, is that it's simply a lot more work. Writing a parser by hand can alleviate these problems, but often lead to brittle implementations.
![haskell parsec haskell parsec](https://freesoff.com/uploads/default/optimized/2X/8/87e292e87515e07f823f7323530e108ce2ac120d_2_1035x535.png)
Regular expressions don't support much in the way of error handling, they require learning and understanding a completely different language, the exact syntax and features can vary between implementations, they only support regular languages (or if they do support languages that aren't regular it's through non-standard extensions), and even people with a lot of experience with regular expressions can have trouble understanding complex ones. Writing a parser by hand is much less convenient, but allows fine-grain error reporting and can support languages that are not regular.
![haskell parsec haskell parsec](https://i.stack.imgur.com/s0Cin.png)
Regular expressions are convenient for many reasons: they're concise, they're fast, and for simple parsing they are fairly readable for those that understand the syntax. In many languages, when we want to write a simple parser the usual choices are either to use regular expressions or write a complete parser ourselves.