Programming Languages
Programming Languages
1. Evolution
To write a program for a computer, we must use a computer language. A computer language is a set of predefined
words that are combined into a program according to predefined rules (syntax). Over the years, computer languages
have evolved from machine language to high-level languages.
Machine language is the only language understood by the computer hardware, which is made of electronic
switches with two states: off (representing 0) and on (representing (1).
HALT Stop
High-level languages are portable to many different computers, allowing the programmer to concentrate on the
application rather than the intricacies of the computer's organization. They are designed to relieve the
programmer from the details of assembly language.
High-level languages share one characteristic with symbolic languages: they must be converted to machine
language. This process is called interpretation or compilation (described later).
Over the years, various languages, most notably BASIC, COBOL, Pascal, Ada, C, C+ +, and Java, were developed.
Program 9.1 shows the code for adding two integers as it would appear in the C++ language. Although the
program looks longer, some of the lines are used to for documentation (comments).
Program 9-1 Addition program in C++
2. Translation
Programs today are normally written in one of the high-level languages. To run the program on a computer, the
program needs to be translated into the machine language of the computer on which it will run. The program in a
high-level language is called the source program. The translated program in machine language is called the object
program. Two methods are used for translation: compilation and interpretation.
2.1. Compilation
A compiler normally translates the whole source program into the object program.
2.2. Interpretation
Some computer languages use an interpreter to translate the source program into the object program.
Interpretation refers to the process of translating each line of the source program into the corresponding line of
the object program and executing the line. However, we need to be aware of two trends in interpretation: that
used by some languages before Java and the interpretation used by Java.
Compilation and interpretation differ in that the first translates the whole source code before executing it, while
the second translates and executes the source code a line at a time. Both methods, however, follow the same
translation process shown in Figure 9.1.
Figure 9.1 Source code translation process
Lexical Analyser
A lexical analyser reads the source code, symbol by symbol, and creates a list of tokens in the source language.
For example, the five symbols w, h, i, l, e are read and grouped together as the token while in the C, C++, or Java
languages.
Syntax Analyser
The syntax analyser parses a set of tokens to find instructions. For example, the token ‘x’, ‘ = ’, ‘0’ are used by
the syntax analyser to create the assignment statement in the C language ‘x = 0’.
Semantic Analyser
The semantic analyser checks the sentences created by the syntax analyser to be sure that they contain no
ambiguity. Computer languages are normally unambiguous, which means that this stage is either omitted in a
translator, or its duty is minimal.
Code Generator
After unambiguous instructions are created by the semantic analyser, each instruction is converted to a set of
machine language instructions for the computer on which the program will run. This is done by the code
generator.