[antlr-interest] Rewrite rule cardinality
mark.edgeworth at scarletcode.co.uk
Tue Nov 20 08:38:31 PST 2007
First post to the list, so be gentle :-)
I am parsing a language called verilog in which appears constructs such as
reg a, b, c;
The grammar that I am using specifies this as something like:
: 'reg' range? list_of_register_variables SEMI
-> ^(REGISTER range? list_of_register_variables)+
: register_variable ( COMMA! register_variable )*
: '[' n1=NUMBER ':' n2=NUMBER ']'
-> ^(RANGE n1 n2)
(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' 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
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?
Mark Edgeworth (AKA Medge)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the antlr-interest