On 29 December 2011 09:04, John Leuner
<[email protected]> wrote:
>>> I think the next step I would take (if that wasn't sufficient) is to
>>> create a test suite that tests each grammar rule independently,
>>> successively building up to the complex input that is failing.
>>
>> I'd been doing this for terminals, but it seems like non-terminals get
>> harder to write as unit tests, parser state and all. I suppose integration
>> tests could work, but it seems the more complex the grammatical structure,
>> the more I experience diminishing returns with tests. I just end up with
>> tests that fail mysteriously which is my original problem. Do you have any
>> examples about? I wonder if maybe there's something essential I'm failing
>> to understand. Maybe looking at your tests might send me along to an a-ha
>> moment.
>
> I don't have any tests to show you but I will try to create an example of
> what I mean. (I assume that the parser will fail if it cannot parse the
> entire input).
>
> Given the following grammar rules:
>
> identifier = letter (letter | digit)*
>
> method-call = identifier (ws+ argument)+
>
> (ws matches white space)
>
> argument = number-literal | identifier
>
> number-literal = digit+
>
>
> I would write tests something like this:
>
> test(number-literal, "1234")
> test(identifier, "foo")
> test(argument, "5")
> test(argument, "bar")
> test(method-call, "method a b c 3")
>
>
> Obviously this is a made-up example but I hope it shows you what I mean.

For some real-world examples you might want to check the PetitParser
image. The plain source code for the tests of the grammars for the
following languages is also visible here:

   Smalltalk: 
http://www.lukas-renggli.ch/dropbox/petitparser/grammars/PetitSmalltalk-Tests.st
   JSON: 
http://www.lukas-renggli.ch/dropbox/petitparser/grammars/PetitJson-Tests.st
   XML: 
http://www.lukas-renggli.ch/dropbox/petitparser/grammars/PetitXml-Tests.st

The Smalltalk one is probably the most systematic, testing every
idividual production of the grammar.

Also interesting in this context might be our work on grammar test
coverage: See Section 4.3 of
http://scg.unibe.ch/archive/papers/Berg11b-Profiling.pdf.

Lukas

-- 
Lukas Renggli
www.lukas-renggli.ch

_______________________________________________
fonc mailing list
[email protected]
http://vpri.org/mailman/listinfo/fonc

Reply via email to