简体   繁体   English

#line是什么意思?

[英]What does #line mean?

What does the following line do? 以下几行有什么作用?

#line 25 "CSSGrammar.y"

And what's with the extension? 扩展有什么用?

According to the Standard: 根据标准:

§16.4.3: §16.4.3:

A preprocessing directive of the form 表单的预处理指令

 # line digit-sequence new-line 

causes the implementation to behave as if the following sequence of source lines begins with a source line that has a line number as specified by the digit sequence (interpreted as a decimal integer). 导致实现的行为就好像以下源行序列以一个源行开头,该源行具有由数字序列指定的行号(解释为十进制整数)。 If the digit sequence specifies zero or a number greater than 2147483647, the behavior is undefined. 如果数字序列指定零或大于2147483647的数字,则行为未定义。

§16.4.4: §16.4.4:

A preprocessing directive of the form 表单的预处理指令

 # line digit-sequence " s-char-sequenceopt" new-line 

sets the presumed line number similarly and changes the presumed name of the source file to be the contents of the character string literal. 类似地设置假定的行号,并将源文件的假定名称更改为字符串文字的内容。

§16.4.5: §16.4.5:

A preprocessing directive of the form 表单的预处理指令

 # line pp-tokens new-line 

(that does not match one of the two previous forms) is permitted. (允许与前两种形式中的一种不匹配)。 The preprocessing tokens after line on the directive are processed just as in normal text (each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens). 指令行后的预处理标记的处理方式与普通文本一样(当前定义为宏名称的每个标识符都由其预处理标记的替换列表替换)。 If the directive resulting after all replacements does not match one of the two previous forms, the behavior is undefined; 如果在所有替换后得到的指令与前两个表单中的一个不匹配,则行为未定义; otherwise, the result is processed as appropriate. 否则,结果将被适当处理。

The .y extension is just what the author chose to use, perhaps to make it apparent that it was a YACC file (the word "grammar" also points to that though it's just a guess). .y扩展名正是作者选择使用的,也许是为了表明它是一个YACC文件(“语法”这个词也指向它,虽然它只是一个猜测)。

It simply states that the current line of code is sourced from line 25 of CSSGrammar.y , a YACC-style grammar file which is where this code was generated. 它只是声明当前代码行来自CSSGrammar.y第25 CSSGrammar.y ,这是一个YACC样式的语法文件,它是生成此代码的地方。

This can be used by debuggers to step into the grammar itself as opposed to the generated code. 调试器可以使用它来进入语法本身,而不是生成的代码。

#line directive modifies the reporting position for the compiler, and is used by code generating software to help the programmer identify the issue in the original source. #line指令修改编译器的报告位置,并由代码生成软件用于帮助程序员识别原始源中的问题。 It can be used by anyone to help redirect error reporting to be more informative. 任何人都可以使用它来帮助重定向错误报告以提供更多信息。

So for instance your parser generates a CSSGrammar.cpp file say, which is compiled by the c++ compiler, and has c++ snippets in it, a #line 25 "CSSGrammar.y" directive tells the c++ compiler to treat that particular point in the file as if it is line number 25 from CSSGrammar.y 因此,例如,您的解析器生成一个CSSGrammar.cpp文件,由c ++编译器编译,并且其中包含c ++片段, #line 25 "CSSGrammar.y"指令告诉c ++编译器处理文件中的特定点好像它是来自CSSGrammar.y的第25行

The compiler will continue to parse subsequent lines and report errors under the initial conditions of that directive. 编译器将继续解析后续行并在该指令的初始条件下报告错误。

So if an error occurs 3 lines later it would report that an error occurred on line 28 in CSSGrammar.y 因此,如果3行之后发生错误,则会报告CSSGrammar.y中第28行发生错误

Note that a single source file can have sources coming in from multiple parts; 请注意,单个源文件可以包含来自多个部分的源; and that this directive can be used quite effectively to indicate error conditions. 并且该指令可以非常有效地用于指示错误条件。

Typically you'll see that there are multiple #line directives along the way; 通常你会发现沿途有多个#line指令; they are just there to account for various injections along the way (to reset the reporting caret if you will). 他们只是在那里考虑各种注射(如果你愿意,重置报告插入符号)。

Note that #line directive can be used by ANY generator including your own, and is not limited to in anyway parser generators. 请注意,#line指令可以由包含您自己的任何生成器使用,并且不限于无论如何解析器生成器。

The 'yacc' parser generator consumes files that end in .y, and emits files that contain c or c++. 'yacc'解析器生成器使用以.y结尾的文件,并发出包含c或c ++的文件。 It adds these #line lines to allow a debugger to get back to ye olde original source, accept no substitutes. 它添加了这些#line行以允许调试器返回原始源代码,不接受任何替代。

It a directive for the compiler to believe that the following line is the line number 25 in file CSSGrammar.y . 它是编译器相信以下行是文件CSSGrammar.y第25行的CSSGrammar.y Then, if an error is detected by the compiler on the 2nd next line, it would be reported as coming from line 26 of CSSGrammar.y 然后,如果编译器在第二行的下一行检测到错误,则会报告它来自CSSGrammar.y第26行。

Programs generating C files, like bison , or yacc , or flex , or ANTLR , or even the (obsolete) MELT use that possibility a lot. 生成C文件的程序,如bisonyaccflex ,或ANTLR ,甚至(过时的) MELT都会使用这种可能性。

If debugging information is generated (eg with gcc -g ), it will point to the CSSGrammar.y file in your example. 如果生成调试信息(例如使用gcc -g ),它将指向示例中的CSSGrammar.y文件。

it's ac preprocessor option. 它是交流预处理器选项。 It tells the c-parser to drop it's line count of the source file an pretend, that this is line #25. 它告诉c-parser丢弃它的源文件的行数假装,这是第25行。

With this information it's easier for you to debug the the source file. 有了这些信息,您就可以更轻松地调试源文件。 The yacc file will be translated into a c-source, where this is the pretended source line. yacc文件将被转换为c-source,这是假装的源代码行。

Using #line forces the compiler to experience amnesia about what file it's compiling and what line it's on, and loads in the new data. 使用#line会强制编译器体验它正在编译的文件以及它所在的行,并加载新数据。

Note: The compiler still compiles from the line it was on. 注意:编译器仍然从它所在的行编译。

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

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