[antlr-interest] Rewrite rule cardinality

Terence Parr parrt at cs.usfca.edu
Tue Nov 20 08:46:51 PST 2007


On Nov 20, 2007, at 8:38 AM, Mark Edgeworth wrote:

> 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.

Hi Medge, welcome :)

Well, ANTLR doesn't flatten anything for you so it gets a single  
element back from the list of regs, which it adds to the REGISTER  
root.  It only knows it got a single result back from the list of regs.

As you saw, i suggest moving them up to reg decl.

Ter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071120/2d077ff3/attachment-0001.html 


More information about the antlr-interest mailing list