English 中文(简体)
Question about building a symbol table with a yacc parser
原标题:

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.

问题回答

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.





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签