[antlr-interest] Rewrite rule cardinality

Mark Edgeworth mark.edgeworth at scarletcode.co.uk
Tue Nov 20 08:38:31 PST 2007


Hi,

 

First post to the list, so be gentle :-)

 

I am parsing a language called verilog in which appears constructs such as
the following:

 

reg a, b, c;

 

The grammar that I am using specifies this as something like:

 

==================================

reg_declaration

            :           'reg' range? list_of_register_variables SEMI

            -> ^(REGISTER range? list_of_register_variables)+ 

    ;

    

list_of_register_variables

            :           register_variable ( COMMA! register_variable )*

            ;

            

range

            :           '[' n1=NUMBER ':' n2=NUMBER ']'

                                    -> ^(RANGE n1 n2)

            ;

            

register_variable: IDENTIFIER;

==================================

 

(REGISTER and RANGE are declared as virtual tokens earlier in the file)

 

Now, I was rather hoping to see something like the following in the tree
(using the input above):

 

( (REGISTER a) (REGISTER b) (REGISTER c) )

 

Because the cardinality of the 'list_of_register_variables' item in the
reg_declaration is > 1 I would expect the tree to be expanded (via the '+'
on the reg_declaration rewrite rule).  This looks quite straightforward in
the book (p172 if anyone is following me!)

 

However, I actually see (REGISTER a b c), which means I have more work to do
in the tree walker to break this out.

 

The weird thing is that if I expand the 'list_of_register_variables' rule
inside the 'reg_declaration' rule then all is as expected.  Like so:

 

reg_declaration

            :           'reg' range? register_variable ( COMMA!
register_variable )* SEMI

            -> ^(REGISTER range? register_variable)+ 

    ;

 

...but I'd rather keep this part factored-out as it is used in lots of
places...

 

Hmmm.  I love ANTLR but it does make me think... perhaps this is why I love
it.  Any of you experts out there able to spot where I'm going wrong?
Please?

 

Ta,

 

Mark Edgeworth  (AKA Medge)

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071120/85f4b1cf/attachment.html 


More information about the antlr-interest mailing list