[antlr-interest] philosophy about translation

Jim Idle jimi at intersystems.com
Wed Nov 1 15:21:19 PST 2006


To be honest it is this sort of question (and the answer) that we need to have in the book. The problem is that I would not have thought of this question in a million years (and that is no slight on Andy at all).

For things like this though, remember that ANTLR tries to generate the minimum of code that is required to do the parse and does not automatically include things for stuff you MIGHT want to do as part of the parse. However, it makes it very easy for you to put your own bits of code in there. 

So, think of the grammar like a code template and 'feel' how it works its way through the syntax or the tree. Then where to put a simple counter or a symbol table will just come naturally (honestly, it will!)

The tree grammar says "You will find this bit of tree at these places", then your counter just needs to be higher up the food chain than that to work, and then high enough up the chain to be in scope when you want to use it for something. Recognize something, reach back and count it/register it, move on through the tree.

Jim

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Terence Parr
Sent: Wednesday, November 01, 2006 12:05 PM
To: ANTLR Interest
Subject: Re: [antlr-interest] philosophy about translation


On Nov 1, 2006, at 11:45 AM, Andy Tripp wrote:
>> Try feeding Java code or an ANTLR grammar to a C++ compiler. ;-)
>>
> Of course you need to know the input syntax and symantics (a  
> little). That's different from
> knowing the details of what the tool is doing. When talking to Ter,  
> at one point I said
> "and how do I know how many times that pattern was matched?" and he  
> said "just check
> 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. ;)

Ter

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

-- 
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