1
$\begingroup$

I am writing a basic compiler that compiles a language down into custom bytecode instructions. The language allows for variable shadowing (this is a cause of one of the main problems I am running into). During Semantic Analysis the scopes of the symbol table are being pushed/popped and because variables can belong to different scopes, it is not hard to differentiate shadowed variables. The AST nodes store type and name as strings, so after Semantic Analysis how would the code generator be able to know which variable it needs to access if there are multiple variables with the same name?

Is it common to simply store the symbols on the AST instead, so that after semantic analysis there wouldn't be a need for the symbol table at all because all of its contents are directly connected to associated AST nodes?

$\endgroup$

1 Answer 1

1
$\begingroup$

I am not an expert in this area, but I believe one standard solution is to use alpha renaming to rename the variables so they all have a unique name.

An alternative is to identify variables by a unique identifier -- for example, have one object in memory (one symbol) per distinct variable, and refer to them by reference to the corresponding object (symbol) rather than by string, or pick a unique ID for each memory and refer to them by this ID rather than by a string. This is very similar to alpha renaming.

Possibly related: https://stackoverflow.com/q/5836910/781723

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.