0% found this document useful (0 votes)
20 views

Programming Languages

Programming languages have evolved from machine languages composed of binary code to high-level languages that are easier for humans to read and write. Early assembly languages used mnemonic codes that were translated to machine language by assemblers, while modern high-level languages are translated to machine code through compilation or interpretation. The translation process involves lexical analysis, syntax analysis, semantic analysis, and code generation to convert the high-level language code into executable machine language instructions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Programming Languages

Programming languages have evolved from machine languages composed of binary code to high-level languages that are easier for humans to read and write. Early assembly languages used mnemonic codes that were translated to machine language by assemblers, while modern high-level languages are translated to machine code through compilation or interpretation. The translation process involves lexical analysis, syntax analysis, semantic analysis, and code generation to convert the high-level language code into executable machine language instructions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming Languages

Sources: Forouzan, B. Foundations of Computer Science, 3rd edition.

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.

1.1. Machine Languages


In the earliest days of computers, the only programming languages available were machine languages. Each
computer had its own machine language, which was made of streams of 0s and 1s. In Chapter 5 we showed that
in a primitive hypothetical computer, we need to use 11 lines of code to read two integers, add them, and print
the result. These lines of code, when written in machine language, make 11 lines of binary code, each of 16 bits,
as shown in Table 9.1.

Table 9.1 Code in machine language to add two integers

Hexadecimal Code in machine language

(1FEF)16 0001 1111 1110 1111

(240F)16 0010 0100 0000 1111

(1FEF)16 0001 1111 1110 1111

(241F)16 0010 0100 0001 1111

(1040)16 0001 0000 0100 0000

(1141)16 0001 0001 0100 0001

(3201)16 0011 0010 0000 0001

(2422)16 0010 0100 0010 0010

(1F42)16 0001 1111 0100 0010

(2FFF)16 0010 1111 1111 1111

(0000)16 0000 0000 0000 0000

Table 9.1 Code in machine language to add two integers

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).

The only language understood by a computer is machine language.


Although a program written in machine language truly represents how data is manipulated by the computer, it
has at least two drawbacks. First, it is machine-dependent. The machine language of one computer is different
than the machine language of another computer if they use different hardware. Second, it is very tedious to
write programs in this language and very difficult to find errors. The era of machine language is now is referred
to as the first generation of programming languages.

1.2. Assembly Languages


The next evolution in programming came with the idea of replacing binary code for instruction and addresses
with symbols or mnemonics. Because they used symbols, these languages were first known as symbolic
languages. The set of these mnemonic languages were later referred to as assembly languages. The assembly
language for our hypothetical computer to replace the machine language in Table 9.2 is shown in Program 9.1.
A special program called an assembler is used to translate code in assembly language into machine language.

Table 9.2 Code in assembly language to add two integers

Code in assembly language Description

LOAD RF Keyboard Load from keyboard controller to register F

STORE Number 1 RF Store register F into Number1

LOAD RF Keyboard Load from keyboard controller to register F

STORE Number2 RF Store register F into Number2

LOAD R0 Number1 Load Number1 into register 0

LOAD R1 Number2 Load Number2 into register 1

ADDI R2 R0 R1 Add registers 0 and 1 with result in register 2

STORE Result R2 Store register 2 into Result

LOAD RF Result Load Result into register F

STORE Monitor RF Store register F into monitor controller

HALT Stop

Table 9.2 Code in assembly language to add two integers

1.3. High-Level Languages


Although assembly languages greatly improved programming efficiency, they still required programmers to
concentrate on the hardware they were using. Working with symbolic languages was also very tedious, because
each machine instruction had to be individually coded. The desire to improve programmer efficiency and to
change the focus from the computer to the problem being solved led to the development of high-level
languages.

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.

First Approach to Interpretation


Some interpreted languages prior to Java (such as BASIC and APL) used a kind of interpretation process that we
refer to as the first approach to interpretation, for the lack of any other name. In this type of interpretation, each
line of the source program is translated into the machine language of the computer being used and executed
immediately. If there are any error in translation and execution, the process displays a message and the rest of
the process is aborted. The program needs to be corrected and be interpreted and executed again from the
beginning. This first approach was considered to be a slow process, which is why most languages use compilation
instead of interpretation.

Second Approach to Interpretation


With the advent of Java, a new kind of interpretation process was introduced. The Java language is designed to
be portable to any computer. To achieve portability, the translation of the source program to object program is
done in two steps: compilation and interpretation. A Java source program is first compiled to create
Java bytecode, which looks like code in a machine language, but is not the object code for any specific computer:
it is the object code for a virtual machine, called the Java Virtual Machine or JVM. The byte code then can be
compiled or interpreted by any computer that runs a JVM emulator—that is, the computer that runs the
bytecode needs only a JVM emulator, not the Java compiler.

2.3. Translation process

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.

You might also like