[antlr-interest] parser, tree grammar and scopes

Patrick Masselink patrick at patmas.net
Tue Jun 5 14:01:42 PDT 2007


Hi,

First, let me say hello to you all, since I'm new to the list.
I'm working on a Jasmin bytecode generator, similar to the one discussed in 
the excellent book "The Definitive ANTLR Reference". Though the examples in 
the book have proven very useful, the project i'm working on is more complex, 
it can be tought of as a combination of the Cminus example and the Expression 
bytecode generator, and then some. I understand the individual grammars but 
I'm having trouble combining the various things in my own grammar. As I'm new 
to antlr, jasmin and don't have much experience to parser construction in 
general either, keeping track of things is proving quite difficult.
My approach thus far has been to use a parser grammar that outputs an AST, 
then use a tree grammar and StringTemplate to emit Jasmin bytecode.
Specific questions I have:
Using dynamic attribute scopes I plan to keep track of variables so I can tell 
if a var is defined in the current scope. There is to be a global (file) 
scope and local function, loop (for, while) and codeblock ({...}) scopes. 
Should I discard the tokens for a variable that isn't defined in the scope, 
so they don't end up in the tree, or should I just add everything to the tree 
and figure things out in the tree parser? I think I should process the input 
as much as possible in the input parser so I end up with a optimized tree 
right? Secondly I have a problem with where to add a new scope to the 'scopes 
stack' Take for instance a function definition like this:
int foo(int i, char j) {
	int k;
	....
	{
		int k;
	}
}
The function arguments and the variables declared inside the function are 
local to the function. So I need to define a new scope in the function rule 
and add the arguments to that scope. Then there's a code block, but variables 
in there should be added to the function scope instead of a new scope. But 
the codeblock inside the function needs a new scope. In short, I need to know 
what rules preceeds the codeblock rule, if it is the function rule use the 
function scope, if not add a new scope. Is this at all possible or should I 
change my grammar rules? Up until now I have simple rules for functions, 
loops etc, and refer to the codeblock rule for stuff inside them.

Hopefully above text isn't too confusing ;). Finally I'd like to ask if 
someone can point me to any advanced parser + tree grammars that contain lots 
of actions, scopes etc, just to see how it's supposed to be glued together. I 
found out the best way I learn is by example. I've seen most stuff on the 
antlr website/wiki, there're definitely some complex grammars but I haven't 
found one that builds an AST and includes the tree grammar and uses complex 
actions and other more exotic antlr features.

Regards, Patrick.


More information about the antlr-interest mailing list