[antlr-interest] Re: Own AST : initializing extra private members
Kristoff Tempelaere
kristoff_tempelaere at yahoo.com.au
Fri Aug 20 16:44:54 PDT 2004
#---------- the class for the ASTNodes -------
class MyAST : public antlr::CommonAST {
private:
int _x;
public:
void setx(const int x) {_x = x;}
//others
};
#------- a (simplified) parser rule ------------------
column :
(lbl1:ID PERIOD!) (lbl2:ID)
{#lbl1->setx(_Tablename_);} //_Tablename_ is a int-constant
{#lbl2->setx(_Columnname_);} //same
;
#---- Information----------
A column can be specified in SQL by "a.b" where "a" is a tablename and "b" is a columnname. I want to store this information in the AST, and I do this right now using semantic actions But I wondered if this can't be done while the node is created... avoiding the semantic actions.
So instead of calling MyFactory.create(ID) to create the node, I would like to call MyFactory.create(ID,_Tablename_), so the x-value is set when creating the node. Ofcourse I need a way to pass the xvalue in the grammar... ID<_x=_Tablename_> or something like that?
The difference with the example on http://www.imada.sdu.dk/~morling/ is that the x-value is passed in the parsergrammar, and isn't derivable from the data in the token.
I hope this makes it a bit more clear... thxs for your efforts !
Kristoff
Eric Mahurin <eric_mahurin at yahoo.com> wrote:
You should give a more concrete example that shows exactly what you
are doing - especially where xvalue is coming from. At least a
complete rule, if not a mini-grammar (parser).
--- In antlr-interest at yahoogroups.com, Kristoff Tempelaere
wrote:
> Where does xvalue come from? Is it constant or does it rarely change?
> ---- It changes a lot :( I'll make it a bit more concrete : I'm
working on an SQL-grammar, and I need to store some extra information
in the AST. For example, to group all keywords...
>
> Because sometimes I just need to all keywords in the query. I could
do this by searching the AST for every keyword, based on the _ttype.
But thanx to the x-value I can do this in just one traversal, which
saves ofcourse a lot of time... If there are 100 keywords, then this
would sava 99 traversals...
>
> I saw a similar example, with a private member "line". The
difference is, the information for this member is in the token, so
overwriting create(token) {.... line = token->getLine(); ... } did the
trick there. But in my case, the x-value can't be derived based on
the token, it must be passed in the grammar. A bunch of semantic
actions { setx(value);} does the job, but it looks awfull... and
messes up the grammar a bit... so I was wondering if there was a
better way, like "ID" or something... So, somehow
to force the parser call MyFactory.create(token, xvalue) and passing
the xvalue in the grammar...
>
> More general : setting private members of an ASTnode, which can NOT
be derived from the token... is there another way than using semantic
actions?
>
> Thx for your proposal anyway! But it won't work in my case :(
>
> Kristoff
>
>
> Eric Mahurin wrote:
> --- In antlr-interest at yahoogroups.com, Kristoff Tempelaere
> wrote:
>
> > Can this be done in a better way? Instead of using
> myfactory.create(token), i would like the parser calls
> MyFactory.create(token,xvalue)
>
> Where does xvalue come from? Is it constant or does it rarely change?
> If so, you could make your own factory class that has a static xvalue
> variable that you would assign whenever needed and then your new
> create method would use this static value.
>
> Eric
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
> ---------------------------------
> Find local movie times and trailers on Yahoo! Movies.
Yahoo! Groups Links
---------------------------------
Find local movie times and trailers on Yahoo! Movies.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20040821/83a0d111/attachment.html
More information about the antlr-interest
mailing list