0% found this document useful (0 votes)
173 views5 pages

Compilers and Interpreters

The document discusses computer programs and systems. It defines a program as a set of instructions written in a language that a computer understands to perform tasks. It describes a computer system as including programs, documentation, and hardware designed to control the computer's operation and extend its capabilities. It distinguishes between application software designed for specific uses and system software that controls the computer's operation. It also provides details on compilers and interpreters, which translate programs between high-level languages for humans and low-level machine code.

Uploaded by

Teflon Peter Don
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views5 pages

Compilers and Interpreters

The document discusses computer programs and systems. It defines a program as a set of instructions written in a language that a computer understands to perform tasks. It describes a computer system as including programs, documentation, and hardware designed to control the computer's operation and extend its capabilities. It distinguishes between application software designed for specific uses and system software that controls the computer's operation. It also provides details on compilers and interpreters, which translate programs between high-level languages for humans and low-level machine code.

Uploaded by

Teflon Peter Don
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program- set of instructions written in the language that the computer understands, so as the computer

can run. They have to be installed into the computer for it to run

Set of instructions written in the language a computer understands for the computer to perform a
specific task. The computer interprets a program’s instructions if the program is stored within the
computer.

System includes programs with associated documentation to control the way the computer operates
and facilitates extending the general capabilities of the system. A system includes input, process and
output. A computer system may therefore refer to the group of hardware and associated software
designed and assembled 2gether to perform a specific task or a group of functions.

A computer is a programmable machine that receives input, process and produces output in a useful
format

Software refers to all various programs together with their associated documentation that may be used
on a computer system.

Application software is the software designed to be put into specific practical use. Includes;

A) Specialized application software:- Are programs with associated documentation designed to


perform specific particular tasks e.g. business software, educational software..
B) Application packages:- R suites of programs with associated documentation used for a particular
time of problem. Most packages can be used for a variety of similar problems e.g. ms office
suite

System software are programs and associated documentation that are designed to control the way the
computer operates and provides facilities extending the general capabilities of a computer. e.g.
operating systems, comp. utilities, compilers and interpreters.

Compilers and Interpreters

These are programs which translate computer programs from high-level languages
such as Pascal, C++, Java or JavaScript into the raw 1s and 0s which the computer can
understand, but the human programmers cannot:
You write this The computer translates it ... into this, which it can run

Compilers
Compilers were the first sort of translator program to be written. The idea is simple:
You write the program, then hand it to the compiler which translates it. Then you run
the result.

The compiler takes the file that you have written and produces another file from it. In
the case of Pascal programs, for instance, you might write a program called
myProg.pas and the Pascal compiler would translate it into the file myProg.exe which
you could then run. If you tried to examine the contents of myProg.exe using, say, a
text editor, then it would just appear as gobbledy-gook.

The compiler has another task apart from translating your program. It also checks it to
make sure that it is grammatically correct. Only when it is sure that there are no
grammatical errors does it do the translation. Any errors that the compiler detects are
called compile-time errors or syntax errors. If it finds so much as one syntax error, it
stops compiling and reports the error to you. Here is an example of the C++ compiler
reporting a whole list of errors:

Most "serious" languages are compiled, including Pascal, C++ and Ada.

Interpreters

An interpreter is also a program that translates a high-level language into a low-level


one, but it does it at the moment the program is run. You write the program using a
text editor or something similar, and then instruct the interpreter to run the program. It
takes the program, one line at a time, and translates each line before running it: It
translates the first line and runs it, then translates the second line and runs it etc. The
interpreter has no "memory" for the translated lines, so if it comes across lines of the
program within a loop, it must translate them afresh every time that particular line
runs. Consider this simple Basic program:
10 FOR COUNT = 1 TO 1000
20 PRINT COUNT * COUNT
30 NEXT COUNT

Line 20 of the program displays the square of the value stored in COUNT and this line
has to be carried out 1000 times. The interpreter must also translate that line 1000
times, which is clearly an inefficient process. However, interpreted languages do have
their uses, as we will see in a later section.

Examples of interpreted languages are Basic, JavaScript and LISP.

So which is better?

Well, that depends on how you want to write and run your program. The main
advantages of compilers are as follows:

 They produce programs which run quickly.


 They can spot syntax errors while the program is being compiled (i.e. you are
informed of any grammatical errors before you try to run the program).
However, this does not mean that a program that compiles correctly is error-
free!

The main advantages of interpreters are as follows:

 There is no lengthy "compile time", i.e. you do not have to wait between
writing a program and running it, for it to compile. As soon as you have written
a program, you can run it.
 They tend to be more "portable", which means that they will run on a greater
variety of machines. This is because each machine can have its own interpreter
for that language. For instance, the version of the BASIC interpreter for the
PDP series computers is different from the QBasic program for personal
computers, as they run on different pieces of hardware, but programs written in
BASIC are identical from the user's point of view.

Some computer systems try to get the best of both worlds. for instance, when I was at
Durham, we programmed in Pascal on the old PDP/11 machines. Running a Pascal
program on those machines was a two-stage process. Firstly, we ran a compiler
program (called pc) which compiled the program to a low-level version, and spotted
any grammatical errors in the process. We then ran an interpreter program which took
the output of pc and ran it. The fact that pc produced something that didn't have to run
directly as machine code made the program more portable. Different versions of the
low-level interpreter could be written for different machines in the PDP range, each
taking as its input the same output from pc

How does a compiler work?

Compiling a program takes several stages of processing, which I have outlined below.
The principles which are explained below also apply to interpreters, with the
exception that the interpreters translate each program one line at a time before running
it, and then moving on to the next line. The process may be summarised in this
diagram:
Tokenising  Syntax analysis  Semantic analysis 
Translation

Tokenising

This part of the process is also sometimes called Lexical Analysis. It involves turning
the program from a series of characters into a series of tokens that represent the
building blocks of a program. The tokens are keywords of the language i.e. important
words such as if, print or repeat), variable names and mathematical operators (+, *,
brackets etc.).

The tokeniser takes each of the characters in turn, and as soon as it recognises a
legitimate token, it reports it to the next stage of the process. Each token has a label
and a type, so a variable "count" in a program would have the label corresponding to
its name ("count") and the type "variable name". The tokeniser is also responsible for
ignoring comments in the program - these are words and phrases inserted purely for
the benefit of any human reading the program, and they have no function.

Syntax Analysis

Syntax means "grammar" and the syntax analyser in a compiler checks that the right
tokens appear in the right order to make grammatically correct instructions. For
instance, in C++, the instruction xyz++; is syntactically correct, but the instruction
+;xyz+ is not - the order of the tokens is wrong.

Semantic Analysis

The word "semantics" refers to meaning, and the semantic analyser checks the
meaning of the program. This refers to aspects such as whether the variables have
been declared, e.g. xyz++; may be syntactically correct, but if the variable xyz has
not been declared, then it is semantically incorrect!
The semantic analyser checks not only variable declarations and scope, but whether
the program has entered or left loops, or subroutines, whether classes are accessed
correctly etc.

Translation

You might also like