There are several steps in compilation of a program:
- line reconstruction
- lexical analysis
- Preprocessing.
- syntax analysis
- semantic analysis
Is removal of comments in the first step "line reconstruction"?
There are several steps in compilation of a program:
Is removal of comments in the first step "line reconstruction"?
I'm going to answer keeping a C compiler in mind. The following is usually the case in most compilers, but the examples I'm going to give would be for a C compiler.
The comments are removed after the line reconstruction phase and typically ignored during the lexical analysis phase. A quick verification can be done this way. Consider the following code:
When the C compiler finds a backslash immediately followed by a new-line, the line-reconstruction phase would make that into a single line.
You can figure out what the above code would result in!
The design choice is because it is consistent with the behaviour that we expect, that a backslash followed by a new-line must be concatenated always.
However, the lexical analysis phase involves tokenising. This stage can conveniently ignore the comments when tokenising the code for further processing. So by the time the next phase is called, the comments would've been already lost!
Hope this clarifies! :)
P.S.: Sources!
That line-reconstruction takes place before comments are even analysed
That comments are ignored during lexical analysis