[antlr-interest] found more bugs in ANTLRv3.g + howto add separator when parallel iterating 2 lists in stringtemplate

Jens Boeykens jens.boeykens at gmail.com
Mon Jul 7 02:44:06 PDT 2008


I think I found 2 more bugs in ANTLRv3.g http://www.antlr.org/grammar/ANTLR

Rule altlist:*

altList
@init {
    // must create root manually as it's used by invoked rules in real antlr
tool.
    // leave here to demonstrate use of {...} in rewrite rule
    // it's really BLOCK[firstToken,"BLOCK"]; set line/col to previous ( or
: token.
    CommonTree blkRoot =
(CommonTree)adaptor.create(BLOCK,input.LT(-1),"BLOCK");
}
    :   a1=alternative rewrite ( '|' a2=alternative rewrite )*
        -> ^( {blkRoot} (alternative rewrite)+ EOB["EOB"] )
    ;*

I removed the question mark after rewrite in the tree rewrite subrule. This
caused problems with parallel iteration of lists. (Only the first rewrite
appeared in the tree). Also see my post on the stringtemplate mailing list.


Rule block:

*block
    :   lp='('
        ( (opts=optionsSpec)? ':' )?
        a1=alternative rewrite ( '|' a2=alternative rewrite )*
        rp=')'
        -> ^( BLOCK[$lp,"BLOCK"] optionsSpec? (alternative rewrite)+
EOB[$rp,"EOB"] )
    ;*

I added "rewrite" to the tree rewrite subrule. Otherwise rewrite was missing
in the tree. Also changed block in ANTLRv3Tree.g

*block
    :   ^( BLOCK optOptionsSpec (a+=alternative r+=rewrite)+ EOB )
        -> block(blok={$BLOCK}, optionsSpec={$optOptionsSpec.st},
alternative={$a}, rewrite={$r},
        eob={$EOB})
    ;*

I now have a problem iterating parallel through 2 lists (alternative and
rewrite) in my stringtemplate:

*block(blok, optionsSpec, alternative, rewrite, eob) ::= <<
<alternative,rewrite: {alt,rw | <alt> <rw> }>
>>*

This should regenerate a rule like  *r: a -> int | b -> char;*
My output however is:  *r: a -> int b -> char; *
Thus no separator.

If I use template (added a '|'):
*
block(blok, optionsSpec, alternative, rewrite, eob) ::= <<
<alternative,rewrite: {alt,rw | <alt> <rw> | }>
>>*

I have one '|' too many in my output:
*r: a -> int | b -> char |; *
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080707/f1fad01f/attachment.html 


More information about the antlr-interest mailing list