[antlr-interest] philosophy about translation

Jim Idle jimi at intersystems.com
Wed Nov 1 15:45:05 PST 2006



-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Andy Tripp

>> n < 4". How was I supposed to know that ANTLR generated some code  
>> that included
>> a variable called n?
>
>
> You aren't.  You need to generate int n=0 and n++ code so you know  
> what's up. ;)

Or better yet, just call some sort of astNode.getChildCount() method 
while walking the AST :)


Andy, as per the previous post, ANTLR does not generate code for things that you might like to do, or before too long you would get a parser that would be slower than reading the input text out loud ;-).

I hate to think what astNode.getChildCount() would have to do to be a generic thing in any tree parser, if it could even be done. There is a count of child nodes for any node available one way or another, but that is not context sensitive for your purpose, other than it falls out of the tree construct. However:


stuff
	: { int n = 0; }
		( otherStuff { n++; } )+
	;


Seems pretty obvious, and is just one way to do it, subject to scoping rules.


stuff
	scope { int stuffCount; }
	@init { $stuff::stuffCount = 0; }
	: stuffbits+
		{ 
			System.out.println("There are " + $stuff::stuffCount 
							+ " things!");
		}

stuffbits
	: ^(SUMMAT grand) // This line for Yorkshire folk
	{
		$stuff:stuffCount++;
	}
	;

Is another...



So, to sum up...when I have
...( statement )+...
Not only do I not want to have to figure out and then generate the 
"n-type-code" is that I need to
embedd in the ANTLR-generated code, I don't even want to walk the tree 
and call astNode.getChildCount()!

I just want to say something like:
int n = statementList.getStatementList().size()
In other words, I want an ANTLR-generated Java API that mirrors my grammar.
I want my parser's output to be a Java API (and implementation), not an 
AST like ANTLR produces
or even Java code that implements a treewalk like javacc produces.

Guess I'm just being difficult :)


:-) There may be some merit in this request I think, but I am not sure if what you are asking for is what you want ;-). I would suspect that the API that is generated would require more detailed knowledge of what is going on than the generic knowledge required to use action code neatly, and would be less obvious in use.
 
Jim

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.409 / Virus Database: 268.13.21/510 - Release Date: 11/1/2006
 


More information about the antlr-interest mailing list