[antlr-interest] philosophy about translation
Sohail Somani
sohail at taggedtype.net
Sun Oct 8 20:44:02 PDT 2006
On Sun, 2006-10-08 at 19:51 -0400, Andy Tripp wrote:
> Sohail Somani wrote:
>
> 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.
Its weird that you're walking token streams, but what it seems it that
you're implementing the recursive descent parser by hand...
> 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.
I believe there is an unspoken rule that all bets are off with COBOL?
> 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.
Well, in your case you're managing both. In my case, I just worry about
telling antlr the grammar and managing the symbol table appropriately.
For my compiler, I needed to spit out lots of warnings since it was
basically a cfront type deal, albeit not for C++ (thankfully!) I found
that having a crude symbol table was very easy and natural. I couldn't
imagine re-parsing the token stream just to determine the type of a
variable, something I might need to do more than once. For example, if
you see:
a.b();
You might need to know if a is a class (this making b a static function
call) or an object. Once you have decided that a is one or the other, I
don't see why you'd do it again...
Since the compiler has been in regular use for about 7-8 months, there
have been a few bug fixes and lots of feature requests. I've found that
having a symbol table available is a great help.
> 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.
Hmm... I guess I understand.
Thanks,
Sohail
More information about the antlr-interest
mailing list