[antlr-interest] AST losing leaves in v3.3

Sam Harwell sharwell at pixelminegames.com
Mon Dec 6 06:12:42 PST 2010


The problem you're looking for is your qualifier rule, which should be (in
the rewrite syntax):

qualifier : ':' limits -> limits;

When you write ^(limits), it assumes you want the result of the limits rule
to be the root of your tree. Since the root of the tree can only be a single
node, it discards the children. Hopefully the Tool will get an update that
issues a warning when you use a tree as the root of a rewrite rule.

That said, here are some other comments:

1. The rewrite syntax is slower than the other syntax. The following rules
should be written this way:

qualifier : ':'! limits;
limit_val : ID;

2. To improve the ability to trace tree nodes back to the source, you should
always provide a token when constructing tokens in your AST. This guarantees
proper line/column info for every node in the tree.

var : ID -> ^(NAME[$ID, "var"] ID);
sfield : field_name '-' var qualifier* -> ^(FIELD[$field_name] field_name
var qualifier*);
limits : first=limit_var '-' second=limit_val -> ^(NAME[$first, "limits"]
$first $second);

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Mike Allbright
Sent: Sunday, December 05, 2010 1:20 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] AST losing leaves in v3.3

With v2.7.7, the AST has all the expected nodes, but with v3.3, the
limit_val nodes get dropped.  Am I overlooking some obvious mistake here?


Regards,
--Mike
 
Here's my input:
----------------------
mm-T_minute:bbc-xyzzy
 
And the v3.3 output:
------------------------
(FIELD mm (var T_minute) limits)
 
Here's the grammar
---------------------------
grammar sfield;
 
options { output=AST; ASTLabelType=CommonTree; }
tokens { NAME; FIELD; }
               
sfield : field_name '-' var qualifier* -> ^(FIELD field_name var qualifier*)
; 
field_name : ID;
var : ID -> ^(NAME["var"] ID);
qualifier : ':' limits -> ^(limits);
limits : limit_val '-' limit_val -> ^(NAME["limits"] limit_val+);
limit_val : ID -> ID;
 
ID: (LETTER|'_')(LETTER|'_'|DIGIT)*;
 
fragment LETTER: LOWER | UPPER;
fragment LOWER: 'a'..'z';
fragment UPPER: 'A'..'Z';
fragment DIGIT: '0'..'9';
fragment SPACE: ' ' | '\t';
fragment SIGN: '+' | '-';
NEWLINE: ('\r'? '\n')+ { $channel = HIDDEN; };
WHITESPACE: SPACE+ { $channel = HIDDEN; };
 
OTHER : . ;



More information about the antlr-interest mailing list