compilationcompiler-constructionpreprocessorlexical-analysissemantic-analysis

Compiler design preprocessing and compiling


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"?


Solution

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