[antlr-interest] AST problem

j_scandaliaris at yahoo.es j_scandaliaris at yahoo.es
Thu Jun 12 07:50:59 PDT 2003


Hi,
	I have derived a custom AST class from commonAST, mostly following examples in the antlr distribution and tutorials. I am not sure I have done everything the right way though, as I don’t fully understand the mechanisms behind all this. So this post is about two things:
       1.- Try to understand what is the right way to do it: I found the doc in the antlr distribution difficult to understand (maybe ‘cause I am not that strong in c++ ?). What is that is necessary to add/modify and why? I get particularly lost with the factory stuff :(
       2.- Identify if the following behaviour in my parser is “normal” or induced (to put it in a nice way :) )by the way I created the custom AST, and hopefully understand how to fix it!

logical_name_list 
	{  string str1, str2;  
   	}
   	:  ln1:logical_name 
   	{  str1 = #ln1->getSrc(/* int lineNumber=0 */);
   	}
   	(  COMMA ln2:logical_name
   	{  str2 += ", " + #ln2->getSrc();
   	}
   	)*
   	{  ##->addSrc(str1 + str2);
   	}
	;

logical_name
	:  id:IDENTIFIER
   	{  ##->addSrc(id->getText()); 
   	}
   	;

addSrc() and getSrc() are member functions of MyAST handling a vector<string>. addSrc() makes a push_back, getSrc() gets the first string out of the vector by default (other elements available by passing an argument).
The key of the problem is the addSrc() call. As I understand it (maybe that is where the problem lies :) ) in logical_name_list I push_back ONCE (a getSrc() call in logical_name_list) with the text I am interested (comma separated list of IDs). Further up I read the text out of logical_name_list node assuming the text of interest is the first element of the vector. Instead, it seems that I am doing a push_back every time I get an identifier, so I end with the text of interest at the end of the vector, and the beginning filled with garbage. I would have thought that in logical_name rule I was applying the addSrc() to a logical_name node. 
Is this the right behaviour? Could it be due to an incorrect MyAST definition?
I attached the AST definition, in case anyone cares to see if there is anything wrong there. It is just a copy paste of what I found on the heteroAST example with additions. I looked also in Peter Morling's tutorial.
	To finish I just mention that this example is probably the simplest case in my grammar (which would be easier to solve in another way), and that the interest is to find the right way to go so I can apply this to the more difficult cases.
	

Jorge


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: VhdlAST.h
Type: application/octet-stream
Size: 4676 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20030612/1f73c81d/VhdlAST.obj


More information about the antlr-interest mailing list