简体   繁体   English

c/c++程序中的print(而不是printf())由编译器的哪一部分检测

[英]print(instead of printf()) in c/c++ program is detected by which part of compiler

Suppose I have written ac program and I have written print instead of printf .假设我编写了 ac 程序,并且编写了print而不是printf
Now my question is which part of compiler will detect this ?现在我的问题是编译器的哪一部分会检测到这一点?

I'm assuming OP means which part of the compiler internally , such as the lexer, parser, type analyzer, name analyzer, code generator, etc.我假设 OP 表示编译器内部的哪一部分,例如词法分析器、解析器、类型分析器、名称分析器、代码生成器等。

Without knowing specifically about gcc/llvm, I would assume that it's the Name Analyzer (more specifically, this is a part of the "Semantic Analyzer" generally, which also does Type Analysis), as that wouldn't be able to match "print" to anything that exists name wise.在不特别了解 gcc/llvm 的情况下,我会假设它是名称分析器(更具体地说,这通常是“语义分析器”的一部分,它也进行类型分析),因为它无法匹配“打印" 任何存在的名字明智的。 This is the same thing that prevents things such as:这与防止以下情况的事情相同:

x = 5;

When x does not exist previously.x以前不存在时。

Strictly speaking, assume that print will be represented by token in the form:严格来说,假设print将由以下形式的令牌表示:

{ token type = Identifier, token value = 'print' }

This transformation from source characters in tokens is done by lexical analyzer.这种从标记中的源字符转换是由词法分析器完成的。 Lets say you have function get_token , it reads source file characters and returns token (in the form of above structure).假设您有函数get_token ,它读取源文件字符并返回令牌(以上述结构的形式)。 We can say that source file is viewed as a sequence of such tokens.我们可以说源文件被视为这样的标记序列。

To do higher-level job we call lower-level routines, so assume now that you have function parse_declaration that uses get_token .为了完成更高级别的工作,我们称其为低级例程,因此假设您现在拥有使用get_token parse_declaration函数。 parse_declaration is responsible to recognize declaration in your program (it is done using parsing algorithm, eg recursive descent ) If declaration is recognized it will save token value in symbol table, with type information and attributes. parse_declaration负责识别程序中的声明(它是使用解析算法完成的,例如recursive descent )如果声明被识别,它将在符号表中保存token value ,以及类型信息和属性。

Now, assume you have function parse_expression , it will call get_token , and if token type is Identifier it will perform name lookup .现在,假设您有函数parse_expression ,它将调用get_token ,如果token typeIdentifier ,它将执行name lookup This means that it will search for token value in the symbol table.这意味着它将在符号表中搜索token value If search is unsuccessful it will print error message (something like "token value : undeclared identifier" ).如果搜索不成功,它将打印错误消息(类似于"token value : undeclared identifier" )。

Of course this concept is simplified.当然,这个概念被简化了。 In practice there is rather sophisticated logic for lexical analysis, parsing, semantics (how language 'behaves', name lookup is part of language semantics), and this logic should be as independent (separate) on one another as possible.在实践中,词法分析、解析、语义(语言如何“行为”、名称查找是语言语义的一部分)有相当复杂的逻辑,并且这种逻辑应该尽可能相互独立(分离)。

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

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