I unfortunately was doing a little code archeology today (while refactoring out some old dangerous code) and found a little fossil like this:
# line 7 "foo.y"
I was completely flabbergasted to find such an archaic treasure in there. I read up on it on a website for C programming. However it didn’t explain WHY anyone would want to use it. I was left to myself therefore to surmise that the programmer put it in purely for the sheer joy of lying to the compiler.
Note:
(Mind you the fossil was actually on line 3 of the cpp file) (Oh, and the file was indeed pointing to a .y file that was almost identical to this file.
Does anyone have any idea why such a directive would be needed? Or what it could be used for?
It’s generally used by automated code generation tools (like
yaccorbison) to set the line number to the value of the line in the actual source file rather than theCsource file.That way, when you get an error that says:
you can look at line 15 of the actual source file to see the problem.
Otherwise, it says something ridiculous like
No such identifier 'xyz' on line 1723 of foo.cand you have to manually correlate that line in your auto-generatedCfile with the equivalent in your real file. Trust me, unless you want to get deeply involved in the internals of lexical and semantic analysis (or you want a brain haemorrhage), you don’t want to go through the code generated byyacc(bisonmay generate nicer code, I don’t know but nor do I really care since I write the higher level code).It has two forms as per the C99 standard:
The first sets just the reported line number, the second changes the reported filename as well, so you can get an error in line 27 of
foo.yinstead offoo.c.As to “the programmer put it in purely for the sheer joy of lying to the compiler”, no. We may be bent and twisted but we’re not usually malevolent 🙂 That line was put there by
yaccorbisonitself to do you a favour.