[antlr-interest] AST problem
Jorge Scandaliaris
j_scandaliaris at yahoo.es
Thu Jun 12 15:06:30 PDT 2003
Hi,
I had a look at the generated code (I do it regularly, but I am
not yet familiar with ASTs) and I run the debugger but I still don't
understand. I also put some debug code in the rule to see what's going
on:
logical_name_list
{ string str, str1 = "1", str2 = "2";
int size;
}
: { cout << "ln1:logical_name : "; }
// renoved actions to see what where addSrc() was being
called
ln1:logical_name
( COMMA { cout << "ln2:logical_name : "; }
ln2:logical_name
)*
{ cout << "##->addSrc(str1 + str2); : ";
##->addSrc(str1 + str2);
size = ##->srcSize();
cout << "lnl source size: " << size << "\n";
for (int i = 0; i < size; i++) {
cout << "lnl linea " << i << ": " <<
##->getSrc(i) << "\n";
}
}
;
The parsed text is ieee, aaa, bbb (this is what I want as result of
logical_name_list rule)
Now what it seems weird to me is that I get this ( I added some debug
code to constructors, etc...):
inside VhdlAST()
inside initialize(RefToken)
ln1:logical_name : inside VhdlAST()
inside initialize(RefToken)
from within addSrc: ieee <-- call to addSrc()
inside VhdlAST()
inside initialize(RefToken)
ln2:logical_name : inside VhdlAST()
inside initialize(RefToken)
from within addSrc: aaa <-- call to addSrc()
inside VhdlAST()
inside initialize(RefToken)
ln2:logical_name : inside VhdlAST()
inside initialize(RefToken)
from within addSrc: bbb <-- call to addSrc
##->addSrc(str1 + str2); : from within addSrc: 12 <-- call to addSrc
lnl source size: 2 <-- WEIRD!!
lnl linea 0: ieee <-- where is this coming from?
lnl linea 1: 12 <-- This is what I want
shouldn't the first element have been added to logical_name node,
instead of to logical_name_list?
Why doesn't happen the same thing with the ( )* loop?
The generated code for ln1:logical_name seems the same as
ln2:logical_name, except that this is within a loop. Where is the
difference in behavior coming from?
Thanks in advance. It's probably not the best thing to "debug" in the
list, but I honestly run out of ideas now.
Jorge
> -----Mensaje original-----
> De: mzukowski at yci.com [mailto:mzukowski at yci.com]
> Enviado el: jueves, 12 de junio de 2003 17:22
> Para: antlr-interest at yahoogroups.com
> Asunto: RE: [antlr-interest] AST problem
>
> 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/
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list