What does #line mean?

What does the next line do?

#line 25 "CSSGrammar.y" 

What about the extension?

+11
c ++ c-preprocessor preprocessor-directive
source share
7 answers

In accordance with the standard:

§16.4.3:

Form preprocessing directive

 # line digit-sequence new-line 

leads to the fact that the implementation behaves as if the next line of source lines started with the original line that has a line number, as indicated in a numerical sequence (interpreted as a decimal integer). If the digit sequence specifies zero or a number greater than 2147483647, the behavior is undefined.

§16.4.4:

Form preprocessing directive

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

sets the estimated line number in the same way and changes the estimated name of the source file to the contents of the character string literal.

§16.4.5:

Form preprocessing directive

 # line pp-tokens new-line 

(which does not correspond to one of the two previous forms). The preprocessing characters after the line in the directive are processed in the same way as in plain text (each identifier currently defined as the macro name is replaced by its list of notes for preprocessing tokens). If the directive received after all replacements does not match one of the two previous forms, the behavior is undefined; otherwise, the result is processed if necessary.

The .y is what the author decided to use, perhaps it should be obvious that this is a YACC file (the word “grammar” also points to this, although this is just an assumption).

+12
source share

It simply states that the current line of code is derived from line 25 of the CSSGrammar.y file, a YACC-style grammar file in which this code was generated.

This can be used by debuggers to enter the grammar, rather than the generated code.

+8
source share
Directive

#line changes the position of the report for the compiler and is used by code generation software to help the programmer identify the problem at the source. It can be used by anyone to help redirect the error report to be more informative.

So, for example, your parser generates a CSSGrammar.cpp file that is compiled by the C ++ compiler and has C ++ fragments in it, the directive #line 25 "CSSGrammar.y" tells the C ++ compiler to process this specific point in the file, as if it's line number 25 from CSSGrammar.y

The compiler will continue to analyze subsequent lines and report errors in the initial conditions of this directive.

So, if an error occurs 3 lines later, it will report that the error occurred on line 28 in CSSGrammar.y

Please note that in one source file there may be sources coming from several parts; and that this directive can be used efficiently enough to indicate error conditions.

You will usually see that there are several #line directives in the #line ; they just have to consider the different injections on the way (before resetting the reporting carriages if you want).

Please note that the #line directive can be used by the ANY generator, including your own, and is not limited to them in parsers.

+4
source share

The yacc parser generator consumes files that end in .y and emits files containing c or C ++. He adds these #line lines to allow the debugger to return to its original source code without accepting any replacements.

+2
source share

For the compiler, the directive is to assume that the next line is line number 25 in the CSSGrammar.y file. Then, if the compiler detects an error in the 2nd next line, it will be reported as line 26 from CSSGrammar.y

Programs that generate C files, such as bison , or yacc , or flex , or ANTLR , or even (deprecated) MELT, often use this feature.

If debugging information is generated (for example, using gcc -g ), it will point to the CSSGrammar.y file in your example.

+2
source share

this is a preprocessor option c. It tells c-parser to omit its number of lines in the source file, pretending to be line # 25.

Using this information makes it easier for you to debug the source file. The yacc file will be broadcast to the c-source, where is the source string you are looking for.

+1
source share

Using #line causes the compiler to experience amnesia regarding which file it compiles and on which line it loads, and loads new data.

Note. The compiler is still compiling from the line it was on.

0
source share

All Articles