How does compiler work




















The preprocessor searches for any preprocessor directives lines of code starting with a. Next, the compiler works through the preprocessed code line by line translating each line into the appropriate machine language instruction. This will also uncover any syntax errors that are present in your source code and will throw an error to the command line.

Finally, if no errors are present, the compiler creates an object file with the machine language binary necessary to run on your machine. There is a final important step to reach an executable program. In order for you to have a final executable program, another utility known as the linker must combine your object files with the library functions necessary to run the code.

Think of it as having all the necessary blocks to build a house. The compiler then invokes the linker , which combines the object files, and any library functions used, in an executable file.

Figure illustrates the process of compiling and linking a program from several source files and libraries. The executable file also contains any information that the target operating system needs to load and start it.

Figure From source code to executable file. The compiling process takes place in eight logical steps. A given compiler may combine several of these steps, as long as the results are not affected. The steps are:. Characters are read from the source file and converted, if necessary, into the characters of the source character set. The end-of-line indicators in the source file, if different from the new line character, are replaced.

Likewise, any trigraph sequences are replaced with the single characters they represent. Digraphs, however are left alone; they are not converted into their single-character equivalents. This is a very different process from what takes place for an interpreted programming language.

This is somewhat simplified as many modern programs that are created using compiled languages makes use of dynamic linked libraries or shared libraries. Therefore, the executable file may require these dynamic linked libraries Windows or shared libraries Linux, Unix to run. The process is different for an interpreted language. Instead of translating the source code into machine language before the executable file is created, an interpreter converts the source code into machine language at the same time the program runs.

This is illustrated below:. Interpreted languages use a special program called an interpreter that converts the source code, combines with runtime libraries, and executes the resulting machine instructions all during runtime.

Unlike a compiled language, there is no precompiled program to run. The conversion process and combination with runtime libraries takes place every time an interpreted language program is run. This is why programs written in compiled languages tend to run faster than comparable programs written in interpreted languages.

Click Start to run the simulation of an interpreted program. Click Restart if you want to run the simulation again. Each time an interpreted program is run, the interpreter must convert source code into machine code and also pull in the runtime libraries.

This conversion process makes the program run slower than a comparable program written in a compiled language. Because an interpreter performs the conversion from source to machine language during the running of the program, interpreted languages usually result in programs that execute more slowly than compiled programs. But what is often gained in return is that interpreted languages are often platform independent because a different interpreter can be used for each different operating system.

The Java programming language does not fit into either the compiled language or interpreted language models. This is illustrated in the figure below. The Java compiler javac converts the source code into bytecode. Bytecode is a kind of average machine language.

However, I am sure you had a basic high-level understanding of what a compiler is or how it works before you took your compiler course.

Show 4 more comments. Active Oldest Votes. Lexing - break up the text of the program into "tokens". The tokens are the "words" of the programming language, such as identifiers keywords, variable names, function names, etc. Parsing - convert the sequence of tokens into a parse tree, which is a data structure representing various language constructs: type declarations, variable declarations, function definitions, loops, conditionals, expressions, etc.

Optimization - evaluate constant expressions, optimize away unused variables or unreachable code, unroll loops if possible, etc. Translate the parse tree into machine instructions or JVM byte code. Improve this answer. Dima Dima Actually, it transform a language into another.

The same goes for Vala compiler. I would argue that C-front was not a compiler but a front-end to the C compiler. Or a stage in the compilation process, if you will. Virtual machines blur the boundary between "executable" and "non-executable", of course. Here I would simply consider executable code to be whatever goes into the virtual machine, like the byte code, and abstract away whatever goes on inside the VM, like JIT.

Dima, it doesn't have to be from non-executable code to executable code.



0コメント

  • 1000 / 1000