According to the dragon book, one of the first steps the source code of a progra
ID: 654533 • Letter: A
Question
According to the dragon book, one of the first steps the source code of a program goes through during compilation is the Preprocessor.
Assuming a language like C++ where there is no underlying virtual machine to abstract away the underlying platform, is there a way to deal with cross platform compilation without requiring the use of the Preprocessor?
Another use case would be code to remove code that should only be executed in "Debug Builds".
P.S: I'm not mentioning other uses of the preprocessor like defining a value for PI as these can easily be replaced with the use const global variables.
Explanation / Answer
Yes, it's entirely possible.
For example, you can define the language as requiring at least a minimal degree of dead-code elimination, so (for example) something like:
const bool debug = false;
if (debug)
whatever();
...requires that not only is whatever() not called, but that it produce code that does not refer to whatever at all.
This gives roughly the same effect as something like:
#if debug
whatever();
#endif
...would in C or C++. The only obvious difference is that by including the capability in the language, you typically require that the contents of the condition be syntactically correct for the target programming language (whereas C and C++ allow an #ifdef block to contain code that isn't syntactically correct C or C++, as long as the preprocessor can still detect the matching #endif).
Now, in some cases it's easy to require things that nobody knows how to do, thus producing a specification that nobody can implement. That would not be the case here though: compilers that can do the optimization above already exist.
Ada, for one example, includes a great deal of ability to tailor code to a specific platform without using a preprocessor like C and C++ do. In fact, it (Ada) arguably supports quite a bit more than C and C++ do in this regard.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.