0

It is written here that the one among many jobs of preprocessor is :

Inclusion of header files. These are files of declarations that can be substituted into your program.

Does the contents in header files goes through lexical analyser, syntax analyzer or semantic analyzer before it is used in our code? If not how did the compiler report any errors in header file before reporting errors in the "programmer written code"?

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55

2 Answers2

1

No. The pre-processor just inserts the included file into your code and then does lexical analysis etc on the resulting "big blob".

In reality the pre-processor probably doesn't create the whole "big blob", it just does lexical analysis on it to create a stream of tokens which can be fed to later stages of compile.

Most compilers will let you generate the "big blob" (gcc uses the -E option) - this will create a file with all your code and all included code and possibly some extra "cpp added" markers like line numbers from the original file etc to help with error reporting.

The reason errors from included files appear first is because they are encountered first by the compiler.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • If not how did the compiler report any errors in header file before reporting errors in the "programmer written code"? – Hari Krishnan Apr 09 '19 at 06:27
  • So did the preprocessor include every content in headerfile into my code? It will make the whole content big right? – Hari Krishnan Apr 09 '19 at 06:31
  • @john: if you don't ask for CPP output, gcc doesn't generate it. Internally, the result of preprocessing is a stream of tokens, not a character stream. The compiler most certainly does not create a temporary file for the preprocessor output. – rici Apr 10 '19 at 00:03
  • You can see what your preprocessor does by calling `gcc` with an option `-E`. You'll notice that besides including all the content verbatim, preprocessor annotates the line numbers and source file paths, so the next stages (lexing, parsing, semantic analysis, etc.) would still report correct locations. – SK-logic Apr 25 '19 at 10:09
0

the preprocessor will also intrepret preprocessor directives to determine which files to include and which parts of that file, then pass on the big blob.

Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80