What does #line mean?

Luchian Grigore picture Luchian Grigore · Feb 5, 2012 · Viewed 7.7k times · Source

What does the following line do?

#line 25 "CSSGrammar.y"

And what's with the extension?

Answer

Seth Carnegie picture Seth Carnegie · Feb 5, 2012

According to the Standard:

§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.

§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:

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).