[antlr-interest] philosophy about translation

Andy Tripp antlr at jazillian.com
Sun Oct 8 16:51:23 PDT 2006


Sohail Somani wrote:

>On Sat, 2006-10-07 at 13:40 -0400, Andy Tripp wrote:
>  
>
>>The getContainingFunction() method is real. 
>>It looks backwards
>>(while balancing matching braces) for something that looks like a 
>>function declaration: a "{"
>>preceeded by a ")", with a matching "(" that's preceeded by an ID.
>>
>>Crazy, I know :)
>>    
>>
>
>Yikes! For me its something like:
>
>functionDefn : <function_defn_syntax>
>{setCurrentFunction(function_name);}
>
>Why you need to parse the thing umpteen times, I don't know, but you
>might have a valid reason!
>  
>
I have a function that tells me which function I'm in, rather than 
setting a variable
while walking the code, because...

a) I have hundreds of "rules"/"phases", and only a couple need to know 
what function I'm in.
Given that it's not trivial to know when I'm at a function declaration 
(because I'm "walking"
token streams rather than ASTs), it would be a huge waste to keep track 
of that. Basically,
for each token in each file, I'd be checking to figure out if it's a 
function declaration.

b) It's not actually clear, in COBOL, what a function *is*. There are 
paragraphs, which
typically map to a function, but there can also be "stray code" at the 
top of a file that's
not in a paragraph but needs to be in a function.

c) I have a feeling there might be a problem if I move code around. I 
can't think of a specific
example right now, but that's my general thinking for avoiding symbol 
table use if I can - better
to have a single data structure (in my case a token stream) rather than 
two (a token stream
and a symbol table) that need to be kept in sync.

But I agree with your general point: if you really often need to "know 
where you are" then
an AST helps alot. I've found that I rarely need to know "where I am" in 
the source.

Andy

>Sohail
>
>  
>



More information about the antlr-interest mailing list