In which step of compilation are comments removed?

2019-01-12 13:15发布

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

2条回答
叛逆
2楼-- · 2019-01-12 13:34

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:

printf("Hello "); // *************\
printf("World");  // I like boxes!\
printf("!\n);     // ^^^^^^^^^^^^^\

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

查看更多
孤傲高冷的网名
3楼-- · 2019-01-12 13:41
  • Preprocessing is a phase of its own with its own scanning and parsing, which precede lexical analysis.
  • I'm a compiler writer and I've never heard of 'line reconstruction'. Compilers don't process lines: they process token streams. Your citation specifically says this is a special case for a few odd languages.
  • You've left out flow analysis, optimization, register allocation, and code generation, and a few more.
  • Comments are ignored, not removed, during lexical analysis, which is sometimes conceptually described as 'screening' and 'scanning', in which case you can say comments are screened out, like white space.
查看更多
登录 后发表回答