A recent thread on the St. Louis Lambda Lounge mailing list resurrected an old thread on Tony Morris’ blog, writing a parser for a simple grammar (balanced () and []) in different languages. The original author shows a dense Haskell implementation (not using Parsec), among others. Followup comments include a Parsec implementation.
Here is my attempt at a simple Haskell implementation. In some ways not as concise as the original Haskell version, it seems easy to read and extend.
-- Parser returns whether string is composed of balanced
-- brackets and parens (e.g., "[()]", "[]()", "", etc.)
-- returns false if not balanced or any other character found
pairs = [('[',']'), ('(',')')]
parse = match []
-- ss is stack of closing chars to match, cs is input string
match (s:ss) (c:cs) | c == s = match ss cs
match ss (c:cs) =
case lookup c pairs of
Nothing -> False
Just s -> match (s:ss) cs
match ss [] = null ss