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 wa开发者_如何转开发y I have it now, it enters a and b as separate table entries.
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.
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.
精彩评论