[antlr-interest] Java.g -> AST == exception

Pedro Francisco pedrogfrancisco at gmail.com
Thu Aug 30 08:55:54 PDT 2012


On ANTLR's site's Java.g (OpenJDK version) we find the following rule
(sligthly adapted here):

normalParameterDecl
    :   variableModifiers type IDENTIFIER
        dblSquareBrckt*
    ;

dblSquareBrckt : '[' ']' ;

I tried applying a simple rewrite rule (on a variant of Java.g) by doing:
normalParameterDecl
    :   variableModifiers type IDENTIFIER dblSquareBrckt* ->
variableModifiers type IDENTIFIER dblSquareBrckt*
    ;

Though it compiles, when I run it on a Java file I get:
Exception in thread "main"
org.antlr.runtime.tree.RewriteEmptyStreamException: rule
variableModifiers
	at org.antlr.runtime.tree.RewriteRuleElementStream._next(RewriteRuleElementStream.java:158)
	at org.antlr.runtime.tree.RewriteRuleElementStream.nextTree(RewriteRuleElementStream.java:145)
	at com.example.javap.JavaPParser.normalParameterDecl(JavaPParser.java:9832)
        (...)

The workaround is below: (notice the '?' in variableModifiers after the ->)
normalParameterDecl
    :   variableModifiers type IDENTIFIER dblSquareBrckt* ->
variableModifiers? type IDENTIFIER dblSquareBrckt*
    ;

Though I was able to workaround it, I don't understand what is
happening. Is it expected? (I didn't check with a vanilla Java.g but I
don't touch variableModifiers rule so I thought it should work...).
So, what is going on here?

Thanks in Advance,
-- 
Pedro


More information about the antlr-interest mailing list