views:

88

answers:

2

If my yacc parser encounters the following code:

int foo(int a, int b)

should it add int a and int b as attributes of foo? The way I have it now, it enters a and b as separate table entries.

A: 

Your question is pretty vague. It all depends on what and how you'll use the data later on.

You can use them as separate nodes in your AST, or, as you mentioned, you could add them as attributes of foo(). The choice in this matter is, I believe, yours.

Ivan Vučica
Could I add them both as separate nodes and as attributes, or should I pick only one?
Phenom
This really depends on you. If it will assist your algorithms in any way, then certainly add it in both places.
Ivan Vučica
A: 

I would add to the symbol table as separate entries, but have a scoping clause on the foo node. This will allow you to be able to report shadowing/masking of variables. So for C you many define a module global int a at the top, then having an a as a parameter will mask the global. This is a helpful hint you can give as a warning to users verse a deal breaker of redeclaration of the a symbol in the same scope.

There are other case where scope rules may need to block the same symbol being redeclared, like nested for loops, where the iterator has the same name.

As Ivan says, you need to make this call, based on what you want/need to detect, and the simplest way to that checking.

Simeon Pilgrim