I am writing a compiler from Java 1.0 to x86. I have built the Abstract Syntax Tree (using lex + yacc) and the symbol table. Now, in order to do semantic analysis I plan on traversing the AST in a Depth-first order , type-checking each syntactic construct as I reach its node while also performing necessary type conversions. However, before proceeding to intermediate code generation, what more should be done in the semantic analysis phase.
Asked
Active
Viewed 130 times
1

Shashank Kumar
- 65
- 8
-
1It's up to you, really. Typical checks would include checks for missing or duplicate identifiers, range checks that can be performed at compile-time (arrays, constant assignment, and so on). This can also include live variable analysis (i.e. is a variable referenced before it's initialized, and so on), but this is not strictly required, just a nice-to-have. – 500 - Internal Server Error Apr 17 '20 at 08:47
-
@500-InternalServerError People have said that scope analysis is a part of semantic analysis. But since scope analysis I believe involves creation of global and local scopes and linking these scopes together to form a tree structure, I think it can be done while generating the syntax tree itself and it is not a part of semantic analysis since we are not doing anything meaning related here. – Shashank Kumar Apr 17 '20 at 08:58
-
@500-InternalServerError Also, what do you mean by missing or duplicate identifiers? – Shashank Kumar Apr 17 '20 at 08:59
-
Identifiers that are either not defined at all or defined twice in the same scope. – 500 - Internal Server Error Apr 17 '20 at 11:45
-
I don't know that there is a limit to what constitutes semantic analysis, but constant folding, elimination of redundant evaluation of common subexpressions and dead code elimination are pretty common. There are also a lot of analyses based on control flow analysis and symbolic execution. – rici Apr 17 '20 at 15:52