简体   繁体   English

C#的编译器设计 - 前向引用

[英]C#'s compiler design - forward referencing

In forward referencing language such as c#, how does the compiler handle this? 在前向引用语言(如c#)中,编译器如何处理此问题? What are the steps in which the compiler operate? 编译器的操作步骤是什么?

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).. 你可以检查它或检查一些符号是否仍然未决(因此没有真正的定义,你可以引发语义错误)..

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. 在具有单通道编译器的语言(如Pascal)中,只能使用向后引用,因为必须先知道标识符的类型才能解析它。

与C ++处理它的方式完全相同,我认为,唯一的区别是:语法很简单,编译器可以构造解析树,而无需告诉您尚未声明的符号所引用的语法对象类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM