Have read in Compiler textbook that type inference is context sensitive. Can anyone explain why is it so? This means that we need context sensitive grammar in semantic analysis phase of a compiler with this feature? How is it done in popular programming languages like C?
The context that is needed tells us the types of known variables. Suppose We want to infer the type of
To make sense of this we need to
To answer these three questions, we look up information about variables in a typing context, which is a mapping from variables to their types. Furthermore, the result is context-sensitive, as the type of the whole expression is the type of
Now you are also mentioning grammars. These are not really relevant here, because we are talking about a stage of compilation which comes after parsing. I think you just got confused by the word "context" being used outside of the subject of grammars. Or you are reading a very strange book. By the way, there is a reason scientists want other scientists to give precise references to their sources. So instead of saying "I read a compiler book", you should have given the author and the title. Now I am just guessing.