编译器中的语义分析
编译器(通常)如何进行语义分析?
我在上次考试时必须回答这个问题,这对教授来说还不够。
我在回答中加入了 BNF(带有示例)和语法卡,他问我:“当编译器找到像 int i;
这样的语句时会发生什么?”
How is the semantic analysis done by a compiler (generally)?
I had to answer to this question during my last exam, it wasn't enough for the professor.
I included BNF (with an example) and syntactic cards in my answer, to which he asked me: "What happens when the compiler finds a statement like int i;
?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是时候仔细阅读 Aho&Ullman/Dragon 的书了。
语义分析是编译器确定各种值的类型、这些类型如何在表达式中交互以及这些交互在语义上是否合理的活动。例如,您无法合理地将字符串乘以类名,尽管没有编辑器会阻止您编写
为此,编译器必须首先识别声明和范围,并且通常将这一步的结果记录在一组符号表中。这告诉它特定标识符在特定上下文中的含义。它还必须确定各种文字常量的类型; “abc”是与 12.2e-5 不同的类型。
然后,它必须访问使用标识符和文字的所有位置,并验证标识符/文字的使用以及计算的结果是否与语言定义兼容(如上例所示)。
至于如何完成这一点:通常会解析源代码,构造程序的某种表示(语法树非常流行),然后逐个元素地遍历(“访问”)该表示以收集/验证语义信息。符号表通常只是一组与表示范围的语法树关联的哈希表,从标识符哈希到包含类型声明的结构。
Time to read Aho&Ullman/Dragon book carefully.
Semantic analysis is the activity of a compiler to determine what the types of various values are, how those types interact in expressions, and whether those interactions are semantically reasonable. For instance, you can't reasonably multiply a string by class name, although no editor will stop you from writing
To do this, the compiler must first identify declarations and scopes, and typically records the result of this step in a set of symbol tables. This tells it what specific identifiers means in specific contexts. It must also determine the types of various literal constants; "abc" is a different type than 12.2e-5.
Then it must visit all locations where identifiers and literals are used, and verify that the use of the identifier/literal, and the results computed, are compatible with the language definition (as in the above example).
As to how this is done: typically the source code is parsed, some representation of the program is constructed (syntax trees are very popular), and that representation is walked ("visited") element by element to collect/validate the semantic information. The symbol table is usually just a set of hash tables associated with the syntax tree representing a scope, hashing from identifiers to structures containing type declarations.