Compiler needs to know?
. Whether a variable has been declared?
. Are there variables which have not been declared?
. What is the type of the variable?
. Whether a variable is a scalar, an array, or a function?
. What declaration of the variable does each reference use?
. If an expression is type consistent?
. If an array use like A[i,j,k] is consistent with the declaration? Does it have three dimensions?
What does a compiler need to know during semantic analysis?
For example, we have the third question from the above list, i.e., what is the type of a variable and we have a statement like
int a, b , c;
Then we see that syntax analyzer cannot alone handle this situation. We actually need to traverse the parse trees to find out the type of identifier and this is all done in semantic analysis phase. Purpose of listing out the questions is that unless we have answers to these questions we will not be able to write a semantic analyzer. This becomes a feedback mechanism.
|