I am in the middle of developing a compiler for a C like language and am having some difficulties in the semantic analysis and code generation phases. My questions are the following: 1) For an if statement, the following is the syntax:
if (expression) then
statement1;
statement2;
else
statement3;
end if;
Now, in my target code, it has to be a 3-address code with go to statements, so it should
look something like:
if (Rx) // Rx is the register where the expression is evaluated and stored
go to X1 //for if part
X2 // for else part;
So now, my question is, how do I generate the addresses for "go to" statements?
2) This question is about semantic analysis: I have been able to build and use a symbol table for a single function. What is the approach I should be using to build symbol tables for function calls? In other words for different lexical levels? I know this should somehow involve having multiple trees. One tree for one function. But what's the approach to point to a different tree from somewhere in the middle of a program?
I'm a beginner and thus any suggestions/thoughts would be greatly appreciated.
Are you generating the code immediately, without an "assembler" pass which will resolve symbolic labels? In this case you have to build a table of all the labels and branching instructions, and then backpatch your code once all the labels are generated.