In forward referencing language such as c#, how does the compiler handle this? What are the steps in which the compiler operate?
C#'s compiler design - forward referencing
1.4k views Asked by Shawn Mclean At
3
There are 3 answers
0
On
It does this by doing two passes of compilation. The first pass parses the code and collects all identifiers used. The second pass resolves all identifiers.
In a language with a single pass compiler, like Pascal, only backwards references can be used as the type of an identifier have to be known before it can be resolved.
The main difference between allowing forward reference or not is using a one pass compiler or a multi pass one. Of course to handle forward referencing you have to check symbols definitions and do typechecking AFTER having generated the full abstract syntax tree of the source you are compiling.
So there is no problem, when you first find a forward reference you just rely that it will be defined later (you can mark it as pending in symbol table) then when you find the actual definition you refine the symbol object in symbol table.
After you can typecheck it or check if some symbols are still pending (so there is no real definition, and you can raise a semantic error)..