Question

When reading this document, at the end of it, there is one sentence:

Historically, compilers for many languages, including C++ and Fortran, have been implemented as “preprocessors” which emit another high level language such as C.

Have no idea about preprocessors, any document? Does it mean all these languages will be translated into C source codes?

Was it helpful?

Solution

I think it would have been better to use the term source-to-source translator instead of "preprocessors" which makes it ambiguous in meaning , but it ain't any wrong to use it either.

Basically , A compiler is a computer program translates source code from a high-level programming language to a lower level language (e.g., assembly language or machine code).But the document in the question says :

Historically, compilers for many languages, including C++ and Fortran, have been implemented as “preprocessors” which emit another high level language such as C.

As per this description , it can be said that earlier , the compilers were implemented as source-to-source translator . A translator is also a form of preprocessor but its different from the preprocessor used in a program.

A translator is a computer program that translates a program written in a given programming language into a functionally equivalent program in a different language.

Now, coming to preprocessor used in a program , lets take an example :

#include <stdio.h>// a PREPROCESSOR directive

A preprocessor is a program that processes a source file before the main compilation takes place,( similar to a translator ) but the difference lies in the fact that HERE it handles directives whose names begin with #.

Here #include is a directive. This directive causes the preprocessor to add the contents of the stdio.h file to your program.This is a typical preprocessor action: adding or replacing text in the source code before it’s compiled.

OTHER TIPS

Some languages have been implemented by having the compiler generate C code which is then compiled by the C compiler. Notable examples include:

  • C++ in the earliest days (and C with Classes before that) — cfront generated C code from the C++ code. It ceased to be practical once C++ supported exceptions (read Stroustrup The Design and Evolution of C++ for more information), but not all C++ compilers used the technique (in fact, I don't know of any other compiler than cfront that did it).
  • Yacc is compiled to C code. Bison can be compiled to C or C++ code.
  • Lex is compiled to C code. Flex can be compiled to C or C++ code, I believe.
  • Informix ESQL/C converts Embedded SQL into pure C.
  • Informix 4GL converts I4GL source into ESQL/C, and then uses the ESQL/C compiler to create C code (and the C compiler to create object code and executables), so it has a multi-stage compiler (and I'm simplifying a bit).

The phrase "preprocessor" now has a totally different meaning, and is confusing to be used here. But, yes, here it means some compilers emits its source to another language.

It should be called source to source compiler. One of the examples is Cfront (designed by Bjarne Stroustrup himself), which converted C++ to C.

For the normal meaning of the phrase "preprocessor" in C++, see here.

No. Not necessarily. Many C++ compilers, LIKE THE GCC DOCUMENT SAID, (but not gcc/g++) produce C code output. Why do they do this? So they can piggyback on all the backend executable code that C compilers can compile to (X86, AMD, etc.) By having C as their destination code,they save alot of low end coding on the back end. Such compilers include the original Cfront and Comeau C/C++.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top