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

Paul Johnson paul at cogito.org.uk
Sat Jun 11 17:18:17 CEST 2011

On 11/06/11 14:10, Andrew Coppin wrote:
> OK, so suppose you sit down and write a complicated string parser. Now 
> how do you test that it works correctly?
> 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.
> So what's the best way to attack this problem?
> _

If your parse tree has a "show" instance (or better yet, a pretty-print 
function) then you can generate random parse trees, print them, and then 
show that the parse returns an equal tree.

However if you want to have useful error messages or a wider range of 
representations than just those generated by "show" then you will need 
to write a QuickCheck variant on the "show" function that emits all 
these variations, which is likely to be rather more work.

More information about the Haskell-Cafe mailing list