[antlr-interest] Confusion: Understanding gap or Tree Grammar Bug

Sarnath K - ERS, HCLTech k_sarnath at hcl.com
Fri Aug 20 01:53:09 PDT 2010


Grammar to parse the "Hi" statement of this e-mail:
---------------------------------------------------
all: cM | cF | 'There';
cM: 'Chitparan'; // Sanskrit word meaning "One who inspires the mind" - Male 
cF: 'Chitpari';  // Sanskrit word meaning "One who inspires the mind" - Female 
---------------------------------------------------

Hi "all",

We are new to ANTLR and we all love this tool! Thanks to Terrence!  \__/

We are trying to write a C2C converter. 
However, we are stuck with the handling of the "block statement" in the Tree Grammar part.
Though we have a workaround, we would like to know whether this is an ANTLR bug (or) a gap in our understanding.

I have written a concise grammar that captures the problem. Kindly see the attachment.

The problem is very simple:
A compound statement is a 'statement' that starts with '{', has zero or more variable declarations, followed by zero or more 'statements' and ends with '}'
In the grammar above, 'v' stands for variable declaration, 's' stands for a statement.

When I run this project , I get the following output.
-------------------------------
Console
-------------------------------
{vs{s}}
Tree = ({ v s ({ s }) })
Tree Grammar Output =  {vs{vs} }
--------------------------------

Now look at "Tree Grammar Output". It carries over the "v" from the previous level.
However if I re-write the grammar as below, I don't have this problem.

b->'{' '}'
  | '{' v '}'
  | '{' s '}'
  | '{' v+ s+ '}'

Kindly advice me on what is the right way to do things!
Are we missing something? (like... RTFM?)

Thanks,
Best Regards,
Sarnath
Sarnath Kannan 
Sr. Technical Manager 
HCL Technologies Ltd 
Shipara Building, Off Hosur Road, GB Palya, Bangalore
+91-80-41584417
www.hcltech.com 
www.hcl.in 


DISCLAIMER:
-----------------------------------------------------------------------------------------------------------------------

The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. 
It shall not attach any liability on the originator or HCL or its affiliates. Any views or opinions presented in 
this email are solely those of the author and may not necessarily reflect the opinions of HCL or its affiliates. 
Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of 
this message without the prior written consent of the author of this e-mail is strictly prohibited. If you have 
received this email in error please delete it and notify the sender immediately. Before opening any mail and 
attachments please check them for viruses and defect.

-----------------------------------------------------------------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: driver.java
Type: application/octet-stream
Size: 912 bytes
Desc: driver.java
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100820/bd0d277d/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: block.g
Type: application/octet-stream
Size: 422 bytes
Desc: block.g
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100820/bd0d277d/attachment-0001.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: blockTree.g
Type: application/octet-stream
Size: 435 bytes
Desc: blockTree.g
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20100820/bd0d277d/attachment-0002.obj 


More information about the antlr-interest mailing list