1. The Need for Translation

THE NEED FOR TRANSLATION

How do computers read code?


Source code is a collection of statements written in a high-level language. These statements make no sense to the CPU; they must be translated into machine language.

A translator is used to carry out this translation process. Translators are themselves software applications whose purpose is to convert the source code of a particular high level language into the machine language instructions understood by a particular CPU. Different CPUs require different translators.

The translation process includes three main steps: lexical analysis, syntactical analysis
and code generation.
 

  • Lexical analysis examines each element of the source code to ensure it is a legitimate part of the high-level language. This is similar to checking the spelling in a written document. 
  • Syntactical analysis checks the grammar or syntax of the source code is correct. 
  • This is similar to checking a written document has correct grammar and punctuation. Finally the machine code is generated.

There are two main methods of translation; compilation and interpretation. During the development of a software product it is common to use both these methods. 

Interpreters are used during development to test code as it is being produced. Once the source code is complete compilation is usually used to generate the final executable files for distribution.

Compilation

The process of compilation involves translating the entire source code into object code. 

The object code is then combined with other linked files to create the final  executable files. 

These executable files can then be executed repeatedly at a later time without the services of the compiler. Compiled code executes fast as there is no translation required at run time. Most applications we use on computers have been compiled.

When using a compiled application it is difficult to determine the original high-level language used for its development. The compiled code is a series of machine language instructions. This provides protection for the software developer as it is very difficult to alter the code. Similarly, determining the nature of the original source code is very difficult to achieve from executable machine code.

Incremental Compilation

Compilation for a very large system, such as an operating system, can take a long time.  A variant of compilation is incremental compilation, where a program is compiled section by section. This can help minimise the time required to compile a very large system, as updated or altered sections can be compiled separately, rather than the whole program all at once.

Incremental compilers are designed so that only part of a program under development need be recompiled after a change has been made to its source code.

This can be effected in one of two ways:

  • by choosing a structure of the language and recompiling that whole structure whenever part of the structure is edited; or
  • by determining the smallest amount of recompilation required after each individual editing change and recompiling only that section of the source code.

Using the first method (generally) involves unnecessary recompilation, but determining what source code to recompile is trivial. The second method performs no unnecessary recompilation, but requires extra computation to determine what source code to recompile. 


Interpretation 

Interpreters translate source code statement by statement

After each statement has been translated into machine code it is immediately executed. This process of translation then execution continues until either an error in a line of source code is encountered or the application ends. 

The process of translating code at run time is a significant processing overhead resulting in poor performance. 

Apart from code that generates many web sites few applications are executed using interpreters; rather interpreters are used by programmers to test their code as it is being developed.

When running programs using an interpreter you must have a copy of the interpreter installed together with the original source code. To distribute a program in this way makes copying and alteration of the source code simple. For commercial software this is a definite problem.

The code behind many websites is interpreted. For example the popular PHP language is always interpreted. PHP code runs on a web server to create dynamic web sites. In this case the output from the execution of the code is transmitted to the user’s browser. The source code remains on the web server where it is not accessible to users. Some declarative languages do not use compilers, rather they must be interpreted. In these cases, the interpreter is one part of the programming environment. The environment provides much of the processing support for the application.



See Also

Comments