Given parser combinators as defined by libraries such as Parsec, Attoparsec or various other functional implementations, is it possible to parse languages such as C or Haskell themselves?
Here is an example of what I have in mind:
-- constructor defined by its name, and a list of arguments
data Constructor = Constructor String [Type]
-- type definition contains a type name, list of type variables, and a list of constructors
data Type = Type String [Char] [Constructor]
In this very simplified example, parsing of a type could be:
typeParser :: Parser Type
typeParser = do
string "data"
spaces
name <- takeWhile letters
spaces
typeVars <- many1 letter
...
I noticed that the package http://hackage.haskell.org/package/haskell-src-1.0.3.1 parses the Haskell 98 language, but it does not depend on any of the parser combinator libraries.
language-c
for parsing C. Bothlanguage-c
andhaskell-src
use parser generators for the actual parsing though. When your grammer grows to be large, a parser combinator is useful to help reason about ambiguities. - Alec