#line 是什么意思?
以下行的作用是什么?
#line 25 "CSSGrammar.y"
扩展名又是什么呢?
What does the following line do?
#line 25 "CSSGrammar.y"
And what's with the extension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据标准:
§16.4.3:
§16.4.4:
§16.4.5:
.y
扩展名正是作者选择使用的,也许是为了表明它是一个 YACC 文件(“语法”一词也指出了这一点,尽管这只是一个猜测)。According to the Standard:
§16.4.3:
§16.4.4:
§16.4.5:
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).它只是指出当前代码行源自 CSSGrammar.y 的第 25 行,这是一个 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.This can be used by debuggers to step into the grammar itself as opposed to the generated code.
#line
指令修改编译器的报告位置,并由代码生成软件使用来帮助程序员识别原始源中的问题。任何人都可以使用它来帮助重定向错误报告以提供更多信息。例如,您的解析器生成一个 CSSGrammar.cpp 文件,该文件由 c++ 编译器编译,并且其中包含 c++ 代码片段,
#line 25 "CSSGrammar.y"
指令告诉 c++ 编译器将文件中的该特定点视为 CSSGrammar.y 中的第 25 行。编译器将继续解析后续行并在该指令的初始条件下报告错误。
因此,如果 3 行之后发生错误,它将报告 CSSGrammar.y 中的第 28 行发生错误。
请注意,单个源文件可以包含来自多个部分的源;并且该指令可以非常有效地用于指示错误情况。
通常,您会看到一路上有多个
#line
指令;它们只是用来解释一路上的各种注入(如果您愿意的话,可以重置报告插入符号)。请注意,#line 指令可以由任何生成器(包括您自己的生成器)使用,并且不限于任何解析器生成器。
#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. 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.yThe 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
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; 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.
它是一个指令,让编译器相信以下行是文件
CSSGrammar.y
中的第 25 行。然后,如果编译器在下第二行检测到错误,则会将其报告为来自CSSGrammar.y
生成 C 文件的程序的第 26 行,例如
bison
或yacc
或flex
, 或 ANTLR,甚至是(过时的)MELT 经常使用这种可能性。如果生成了调试信息(例如使用
gcc -g
),它将指向示例中的CSSGrammar.y
文件。It a directive for the compiler to believe that the following line is the line number 25 in file
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 ofCSSGrammar.y
Programs generating C files, like
bison
, oryacc
, orflex
, or ANTLR, or even the (obsolete) MELT use that possibility a lot.If debugging information is generated (e.g. with
gcc -g
), it will point to theCSSGrammar.y
file in your example.“yacc”解析器生成器使用以 .y 结尾的文件,并发出包含 c 或 c++ 的文件。它添加这些 #line 行以允许调试器返回到旧的原始源代码,不接受替代品。
The 'yacc' parser generator consumes files that end in .y, and emits files that contain c or c++. It adds these #line lines to allow a debugger to get back to ye olde original source, accept no substitutes.
这是 ac 预处理器选项。它告诉 c 解析器假装删除源文件的行数,即第 25 行。
有了这些信息,您就可以更轻松地调试源文件。 yacc 文件将被转换为 c 源代码,其中这是假装的源代码行。
it's a c preprocessor option. It tells the c-parser to drop it's line count of the source file an pretend, that this is line #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.
使用 #line 会迫使编译器忘记它正在编译什么文件以及它在哪一行,并加载新数据。
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.