[antlr-interest] some advice on tracking location in parser rules

Jim Idle jimi at temporal-wave.com
Fri Aug 13 12:54:23 PDT 2010


Will,

 

The answer is 'probably' ;-) While what you ask for here can probably be
done in the parser, it some becomes complicated to manage (as you are
seeing). The next thing you know, you will be asked to track symbol usage,
then something else and so on. So generally, I would use an AST anyway.

 

However, you probably just need a global symbol table that gets a new entry
for each method you enter and any other scoping points that you need. The
current active symbol table is where you add references and increment counts
and so on. At the end of the parse (or better yet, tree walk),you will have
a set of objects that contain the information you want. I think that Ter has
some information on building symbol tables in the Wiki somewhere, as part of
the material for his University courses. yep:
http://www.antlr.org/wiki/display/CS652/Symbol+tables 

 

It will probably help you to see the wood for the trees. You can also pass
in parameters to rules which can pass on those parameters of course.

 

Jim

 

From: William Tribbey [mailto:will at tribbey.net] 
Sent: Friday, August 13, 2010 9:21 AM
To: Jim Idle
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] some advice on tracking location in parser
rules

 

Thanks, Jim.

 

Hhhhmmmm.

 

What I have to do is take a java file as input and extract this information

from it:

 

1) package, class, and import data,

2) each method's name and parameter types,

3) for each method, record the methods it uses, and

4) for each method, record the frequency of the keywords it uses.

 

I had been attacking this by putting actions into the parser rules.

Would these be better and more easily accomplished by walking 

an AST? 

 

 

 

Will

 

 

On Fri, Aug 13, 2010 at 12:06 PM, Jim Idle <jimi at temporal-wave.com> wrote:

Will,

It might be better to step back and ask what it is that you are doing that
makes you think you NEED to know this :-) Generally you will produce things
in your productions that are either impendent of what they are used for, or
are influenced by parameters to the rule, or just return their type (such as
an expression). However, you generally produce an AST first, then start to
walk the tree to do things like code generate or transform etc.

So, what is your task is possibly the better thing to deal with?

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of William Tribbey
> Sent: Friday, August 13, 2010 8:48 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] some advice on tracking location in parser rules
>
> Hi Everyone,
>
> As a newbie working through my first real antlr project I find myself
> wondering what is the better way to keep track where I have been as I make
> my way through the parser rules.
> I am using a Java.g file and when I wind up for instance, down in the
> statement rule, I would like to be able to figure out how I got there. I
have
> seen examples in the reference book that use boolean flags and scopes to
> do this. Is there a better way? Return values on the rules?
>
> Thank you,
>
> Will T.
>

> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

 



More information about the antlr-interest mailing list