#line directive and real usages

443 views Asked by At

Many C books say very little about the #line directive.

Many of them say that this directive is used by yacc of bison tools but don't show any practical example.

Can anyone show me some real usage of this directive or give me a practical example of bison or yacc usage?

1

There are 1 answers

2
Basile Starynkevitch On BEST ANSWER

Any program or script which generates C (or C++) code from something else in some other file should also emit a lot of #line directives (to associate generated C lines to location in that other file).

This is mostly useful for diagnostic messages and for the debugger.

As an example, run GNU bison (a generator of parser code) on some example.y file, use ANTLR, and look into the generated C code, or develop then build, install your GCC plugin (it could modify source locations; see also the old -obsolete- GCC MELT project and perhaps Bismon), or look inside compilers translating to C like Bigloo.

BTW, by experience, when you develop a serious C or C++ code generator, you need to emit correct #line directives, and that is a significant burden in the code generator. It may require a significant amount of work.

In some cases, you may choose to not generate #line directives. In 2021, we are making such a choice in the RefPerSys project.