[antlr-interest] Bugreport: ANTLR 3.4 with problems of duplicating nodes and trees in my grammar

Stefan Mätje Stefan.Maetje at esd-electronics.com
Mon Dec 19 09:47:37 PST 2011


Hi,

I think I've found a bug in ANTLR 3.4 (from antlr-3.4-complete.jar) with  
respect
to the duplication of nodes and trees as described in the ANTLR book in
chapter 7, page 167 (P4.0 printing).

I try to build an AST from my input language and split the multi ID lines of a
structure definition from my input in multiple "FIELD_DCL" subtrees for later
evaluation. My input example looks like this:
-------------8<-------------8<-------------8<-------------8<-------------8<----
MODULE ( structsmpl );

PROBLEM;
     DCL toast   STRUCT [
         (zing, zang, zong)  FIXED,
         (ring, rang)  CLOCK
     ] GLOBAL;
MODEND;
-------------8<-------------8<-------------8<-------------8<-------------8<----


The simplified rule for a single structure component looks like this (modifier
list MOD_LIST commented out):
-------------8<-------------8<-------------8<-------------8<-------------8<----
structComponent
	:	( ID | '(' ID (',' ID )* ')' ) /*i='INV'?*/ simpleType
		-> ^(FIELD_DCL simpleType /* ^(MOD_LIST $i?)*/ ID)+
	;
-------------8<-------------8<-------------8<-------------8<-------------8<----


Here follows the somewhat pretty printed output of the AST's toString() method:
-------------8<-------------8<-------------8<-------------8<-------------8<----
(MODULE structsmpl
     (PROBLEM
         (VAR_DCL
             (STRUCT
                 (FIELD_DCL FIXED 15 zing)
                 (FIELD_DCL FIXED 15 zing zang)
                 (FIELD_DCL FIXED 15 zing zong)
                 (FIELD_DCL CLOCK ring)
                 (FIELD_DCL CLOCK rang)
             )
             (MOD_LIST GLOBAL)
             toast
         )
     )
)
-------------8<-------------8<-------------8<-------------8<-------------8<----

What you can see is that the "zing" component of the structure is repeated for
all three variables if the "simpleType" rule returns something like "FIXED 15".
Astonishingly it works correct if "simpleType" only returns a tree like  
"CLOCK".
See the output I would _expect_ to be generated by that rule below:
-------------8<-------------8<-------------8<-------------8<-------------8<----
(MODULE structsmpl
     (PROBLEM
         (VAR_DCL
             (STRUCT
                 (FIELD_DCL FIXED 15 zing)
                 (FIELD_DCL FIXED 15 zang)
                 (FIELD_DCL FIXED 15 zong)
                 (FIELD_DCL CLOCK ring)
                 (FIELD_DCL CLOCK rang)
             )
             (MOD_LIST GLOBAL)
             toast
         )
     )
)
-------------8<-------------8<-------------8<-------------8<-------------8<----

After some trial and error I found a version of the "structComponent" rule that
is working some part more like I expect:
-------------8<-------------8<-------------8<-------------8<-------------8<----
structComponent
	:	( ID | '(' ID (',' ID )* ')' ) /*i='INV'?*/ simpleType
		-> ^(FIELD_DCL ID simpleType /* ^(MOD_LIST $i?)*/)+
	;
-------------8<-------------8<-------------8<-------------8<-------------8<----

Moving the "ID" element from the end to directly after "FIELD_DCL" seems to fix
this behaviour. In the following output generated with the changed
"structComponent" rule every component shows up only once as expected.
-------------8<-------------8<-------------8<-------------8<-------------8<----
(MODULE structsmpl
     (PROBLEM
         (VAR_DCL
             (STRUCT
                 (FIELD_DCL zing FIXED 15)
                 (FIELD_DCL zang FIXED 15)
                 (FIELD_DCL zong FIXED 15)
                 (FIELD_DCL ring CLOCK)
                 (FIELD_DCL rang CLOCK)
             )
             (MOD_LIST GLOBAL)
             toast
         )
     )
)
-------------8<-------------8<-------------8<-------------8<-------------8<----

Please find a simplified grammar Pearl90.g and a test rig Pearl90Test.java
attached to this report in a zip file.

Any comments and bug fixes appreciated.

Greetings,
	Stefan Mätje
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Pearl90RewriteBug.zip
Type: application/zip
Size: 7848 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20111219/8a03ba8a/attachment.zip 


More information about the antlr-interest mailing list