Question

In forward referencing language such as c#, how does the compiler handle this? What are the steps in which the compiler operate?

Was it helpful?

Solution

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)..

OTHER TIPS

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.

exactly the same way C++ handles it, I think, only difference: the syntax is simple enough that the compiler can construct the parse tree without needing you to tell what kind of syntactical object your yet undeclared symbols refer to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top