Compilers and Interpreters

Authors Avatar

Functions & Purposes of translators _                                                                            James Leong Mook Seng

Compilers and Interpreters

There are two types of translator for converting a high-level language such as Pascal into machine code; compilers and interpreters. These relieve programmers of the tedium and complexity of low-level programming and hence make programming considerably easier and less error prone.

Compiler
This type of translator takes the whole source code and compiles it into object code. The object code (sometimes called machine code) can then be run. Pascal is an example of a programming language that uses compilation.

Interpreter
This type of translator takes the source code and translates the first line of the program, and then executes it. It then does the second line, and the third line, until it gets to the end of the code. BASIC, LISP, Prolog and APL are examples of programming languages that uses interpretation.

Compilers plus Interpreters.

Some programs written in languages such as JAVA are both compiled and interpreted! A program is firstly compiled into an 'intermediate code'. It is then distributed to users who use a wide range of computers such as Mac or PCs. These computers then run their own 'interpreter' to convert the compiled code into a code they can use. Languages such as JAVA are said to be platform-independent, because any program written in that language can run on any machine. These types of languages are ideal for use on the Internet, where you don't know anything about the machine that will be running your code.

Compilation and interpretation compared
  • Compilation is much faster than interpretation. Once the compilation process has been completed, the object code will run much faster than the same interpreted code. And the program only has to compile the code once.
  • Object code does not need the compiler to actually run it, only to convert it from source code to object code. Therefore, the object code produced by a compiler can be distributed to other computers without the compiler. One could write a program and then sell the object code on CD to anyone who wants to run the application.
  • Object code is difficult to 'read' so that it can be distributed without actually revealing the code. This helps protect intellectual rights.
  • Source code can be compiled in sections to produce object code in sections. You can't do this with interpreters. If you have a problem with memory space, therefore, compilation may be more useful.
  • If there is an error in interpretation, the program will run successfully up to the point an error occurs. Then it will stop. All of the variables are available for inspection at that point. Because of this, debugging is easier and quicker using interpretation compared to compilation.
  • Interpreters are far simpler to write than compilers. This is why JAVA works - because it is a relatively easy job to write an interpreter for a particular computer or application.

Compiler

A compiler takes a computer program that is written by the programmer (the source program) and translates it into machine code (the target program). This process comprises three main steps, as illustrated below

Lexical Analysis

The main purpose of the lexical analysis is to convert the source program into a series of tokens that are passed on to the syntax analyzer. The functions that the lexical analyser might perform include the following:

  1. Removing white space. White space is regarded as all the program code that is superfluous to the meaning of the programs and includes comments, spaces, tabs and new-line characters
  2. Identify the individual words, operators, etc (known as syntactic units) in the program
  3. Create a symbol table. The symbol table will contain details of each symbol used in the program. A symbol might be a function name, a variable name, etc. the symbol table will be used by the later stages of the compiler. It will contain the symbol name plus information about the item, e.g an integer, a procedure, etc

  1. Some languages allow the use of include files. These are files of code that can be added to the program before compilation starts. The lexical analyzer has to load these files.
  2. Each reserved word, operator, etc will be converted into a token to be passed to the syntax analyzer.
  3. Some simple error checking is performed. For example:
  1. If the language imposes a limit on the length of identifiers or string constants, the lexical analyzer will determine if the identifier is too long
  2. An illegal identifier such as 2ndyeazr will be flagged as an error
  3. An attempt to assign an illegal value to a constant such as
Join now!

Const Name = Hello World

         Error: The  words ‘Hello World’ should be enclosed in quotes

Once you have lexically analysed every line in a program, you end up with a very long string of tokens. This is passed to the syntax analysis stage.

Syntax analysis

The syntax analyzer attempts to make sense of the program. There are two parts to this process:

  • Syntax analysis
  • Semantic analysis

Syntax Analsis

It is the process of determining whether the sequence of input characters, symbols, items or tokens form a valid sentence in the language. The examination of ...

This is a preview of the whole essay

Here's what a teacher thought of this essay

Avatar

***** A great piece of work! With questions and answers too!