[Haskell-cafe] How do you test a parser?

Kevin Charter kcharter at gmail.com
Sat Jun 11 20:44:04 CEST 2011


On Sat, Jun 11, 2011 at 7:10 AM, Andrew Coppin
<andrewcoppin at btinternet.com>wrote:

>
> If you have a function that turns a parse tree back into text again, you
> can try verifying that a round-trip is the identity function. Except perhaps
> sometimes it isn't. Perhaps a given expression has more than one equivalent
> representation. A round-trip from string and back again is even less likely
> to be stable.
>

I find that this kind of testing works quite nicely in practice with
QuickCheck, in the sense that it does a good job of finding bugs in both the
unparser and the parser. It's possible, but unlikely, that you'll have a bug
in the parser and a bug in the unparser that cancel one another out.
Generate the ASTs; parse . unparse ought to be id, or perhaps t = t . parse
. unparse for some simple transformation 't'. For example, if the parser
annotates AST nodes with source positions, you may need 't' to strip them
out.

Kevin
-- 
Kevin Charter
kevin.charter at acm.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110611/2495788b/attachment.htm>


More information about the Haskell-Cafe mailing list