5.2 Language Translators

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

1

5.2 Language
translators

Zain Merchant
2

Zain Merchant
3
Pre-reading questions

1.
A. Name two types of language translator.
B. Identify a method, other than using a translator, of executing a program written in
a high-level language.
2. 2 Most modern language translators offer an Integrated Development Environment (IDE)
for program development.
A. Which IDE are you using?
B. Describe ve features offered by the IDE you use.
C. Which feature do you nd most useful?Why is it useful to you?

Zain Merchant
fi
fi
4

Translation and execution of


programs
Instructions in a program can only be executed when written in machine code and
loaded into the main memory of a computer. Programming instructions written in
any programming language other than machine code must be translated before
they can be used. The systems software used to translate
a source program written in any language other than machine code are translators.
There are three types of translator available, each translator performs a different
role.

Assemblers
Programs written in assembly language are translated into machine code by an
assembler program. Assemblers either store the program directly in main memory,
ready for execution, as it is translated, or they store the translated program on a
storage medium to be used later. If stored for later use, then
a loader program is also needed to load the stored translated program into main
memory before it can be executed. The stored translated program can be
executed many times without being re-translated.
Every different type of computer/chip has its own machine code and assembly
language. For example, MASM is an assembler that is used for the X86 family of
chips, while PIC and GENIE are used for microcontrollers. Assembly language
programs are machine dependent; they are not portable from one type of
computer/chip to another.
Here is a short sample PIC assembly program:

Assembly language programs are often written for tasks that need to be speedily
executed, for example, parts of an operating system, central heating system or
controlling a robot.

Compilers and interpreters


Programs written in a high-level language can be either translated into machine
code by a compiler program, or directly executed line-by-line using an interpreter
program.
Compilers usually store the translated program (object program) on a storage
medium ready to be executed later. A loader program is needed to load the stored
translated program into main memory before it can be executed.

Zain Merchant
5
The stored translated program can be executed many times without being
retranslated. The program will only need to be retranslated when changes are
made to the source code.
With an interpreter, no translated program is generated in main memory or stored
for later use. Every line in a program is interpreted then executed each time the
program is run.
High-level language programs are machine independent, portable and can be run
on any type of computer/chip, provided there is a compiler or interpreter
available. For example, Java, Python and Visual Basic® (VB) are high-level
languages often used for teaching programming.

Zain Merchant
6

Pros and cons of compiling or


interpreting a program
Both compilers and interpreters are used for programs written in high-level
languages. Some integrated development environments (IDEs) have both
available for programmers, since interpreters are most useful in the early stages of
development and compilers produce a stand-alone program that can be executed
many times without needing the compiler.

Zain Merchant
7

Zain Merchant
8

Partial compiling and


interpreting
In order to achieve shorter execution times, many high-level languages programs
use a system that is partially compilation and partially interpretation. The source
code is checked and translated by a compiler into object code.
The compiled object code is a low-level machine independent code, called
intermediate code, p-code or bytecode. To execute the program, the object code
can be interpreted by an interpreter or compiled using a compiler.
For example, Java and Python programs can be translated by a compiler into a set
of instructions for a virtual machine. These instructions, called bytecode, are then
interpreted by an interpreter.
Below are examples of Java and Python intermediate code (bytecode):

Zain Merchant
9

Zain Merchant
10

Integrated development
environment (IDE)
An integrated development environment (IDE) is used by programmers to
aid the writing and development of programs. There are many different IDEs
available; some just support one programming language, others can be used for
several different programming languages. NetBeans®, PyCharm®, Visual Studio®
and SharpDevelop are all IDEs currently in use.
Dynamic syntax checking nds possible syntax errors as the program code
is being typed in to the source code editor and alerts the programmer at the time,
before the source code is interpreted. Many errors can therefore be found and
corrected during program writing and editing before the program is run. Logic
errors can only be found when the program is run.
For larger programs that have more than one code block, some code blocks can
be collapsed to a single line in the editor allowing the programmer to just see the
code blocks that are currently being developed.

Compilers and interpreters


Most IDEs usually provide a compiler and/or an interpreter to run the program.
The interpreter is often used for developing the program and the compiler to
produce the nal version of the object code.
With PyCharm there can be more than one interpreter available for different
versions of the Python language. The program results are shown using the run-
time environment provided.

A run-time environment with a debugger


A debugger is a program that runs the program under development and aids the
process of debugging. It allows the programmer to single step through the
program a line at a time (single stepping) or to set a breakpoint to stop the
execution of the program at a certain point in the source code. A report window
then shows the contents of the variables and expressions evaluated at that point in
the program. This allows the programmer to see if there are any logic errors in the
program and check that the program works as intended.
Each variable used is shown in the report window together with the type and the
contents of the variable at that point in the program. The top variable shown is the
last one that was used.
Answers to calculations and other expressions can also be shown.

Auto-documenter
Most IDEs usually provide an auto-documenter to explain the function and
purpose of programming code.

Zain Merchant
fi
fi

You might also like