Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does #line mean?

What does the following line do?

#line 25 "CSSGrammar.y"

And what's with the extension?

like image 586
Luchian Grigore Avatar asked Feb 05 '12 20:02

Luchian Grigore


People also ask

What does the fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

How can I find a song by the sound?

On your phone, touch and hold the Home button or say "Hey Google." Ask "What's this song?" Play a song or hum, whistle, or sing the melody of a song. Hum, whistle, or sing: Google Assistant will identify potential matches for the song.

What does the fox say for real?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.


3 Answers

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

like image 146
Seth Carnegie Avatar answered Sep 29 '22 16:09

Seth Carnegie


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.

like image 30
user7116 Avatar answered Sep 29 '22 16:09

user7116


#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.y

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

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.

like image 32
Ahmed Masud Avatar answered Sep 29 '22 14:09

Ahmed Masud