[antlr-interest] AST problem

mzukowski at yci.com mzukowski at yci.com
Thu Jun 12 08:22:06 PDT 2003


This is an excellent time to look at your generated code.  Inspect the code
in the parser and notice the loop for your closure ()*.  ANTLR's source code
is readable and close to what you would write by hand.  The key to
understanding ANTLR is to read the generated code.

Monty

-----Original Message-----
From: j_scandaliaris at yahoo.es [mailto:j_scandaliaris at yahoo.es] 
Sent: Thursday, June 12, 2003 7:51 AM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] AST problem


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/ 


 

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




More information about the antlr-interest mailing list