[antlr-interest] Unit testing grammar productions and injecting EOF

Gavin Lambert antlr at mirality.co.nz
Wed Oct 7 13:33:36 PDT 2009


At 09:37 6/10/2009, Kaleb Pederson wrote:
 >After some investigation, I found that the reason for the
 >failure was that EOF wasn't in the expected follow set,
 >which makes sense when I'm parsing the whole language.
 >
 >As a workaround, I changed my production, adding EOF as an
 >optional terminal as follows:
 >
 >objectMember : functionExpression EOF? | ID EOF?;
 >
 >Everything now works but I'm curious if a better solution for
 >testing individual grammar productions exists.  Suggestions or
 >alternatives?

Well, for starters, that rule above is better expressed like this:

objectMember : (functionExpression | ID) EOF? ;

Also, I haven't tested this, but you could try leaving the 
original rule unmodified and then adding another rule like so (not 
called by anything):

test : (objectMember | everyOtherRuleToTest) EOF ;

Then just call the original method (objectMember) in your unit 
tests.  That might do the trick.



More information about the antlr-interest mailing list