Problem Solving and Python Programming: UNIT II: Data Types, Expressions, Statements

Concept of Python Interpreter and Compiler

The Python interpreter is a program that reads and executes Python code.

Concept of Interpreter and Compiler


Definition of translator : A translator is one kind of program that takes one form of program as input and converts it into another form. The input program is called source language and the output program is called target language.


Fig. 2.3.1 Translator

The source language can be low level language like assembly language or a high level language like C, C++, FORTRAN.

The target language can be a low level language or a machine language.


Types of translator

There are two types of translators compiler and assembler.


Compiler

Compiler is a program which takes one language (source program) as input and translates it into an equivalent another language (target program). Refer Fig. 2.3.2.


Fig. 2.3.2 Compiler

During this process of translation if some errors are encountered then compiler  displays them as error messages. The basic model of Compiler can be represented as follows.

The compiler takes a source program as higher level languages such as C, PASCAL, FORTRAN and converts it into low level language or a machine level language such as assembly language.

Major functions done by compiler

1. The compilers translate high level source program to machine program.

2. It raises error messages if any, during the process of comilation.

3. The translation of source language to machine language must be done efficiently.

4. While translating, the compiler preserves the meaning of the code.


Interpreter

Definition : An interpreter is a kind of translator which produces the result directly when the source language and data is given to it as input

• It does not produce the object code rather each time the program needs execution.

• The model for interpreter is as shown Fig. 2.3.3 Interpreter in


Fig. 2.3.3.

• Languages such as BASIC, SNOBOL, LISP can be translated using interpreters. JAVA also uses interpreter.

• The process of interpretation can be carried out in following phases.

1. Lexical analysis

2. Syntax analysis

3. Semantic analysis

4. Direct execution.

Advantages :

•  Modification of user program can be easily made and implemented as execution proceeds.

•  Type of object that denotes a variable may change dynamically.

•  Debugging a program and finding errors is simplified task for a program used for interpretation.

• The interpreter for the language makes it machine independent.

Disadvantages :

• The execution of the program is slower.

• Memory consumption is more.


Difference between Compiler and Interpreter

The analysis phase of interpreter and compiler is same i.e. in both lexical, syntactic and semantic analysis is performed.


Interpreter

1. Demerit: The source program gets interpreted every time it is to be executed, and every time the source program is analyzed. Hence interpretation is less efficient than compiler.

2. The interpreters do not produce object code.

3. Merit:

The interpreters can be made portal because they do not produce object code.

4. Merit:

Interpreters are simpler and give us improved debugging environment.

5. An interpreter is a kind on translator which produces the results directly when the source language and data is given to it as input.


6. Examples of interpreter :

A UPS debugger is basically a graphical source level debugger but it contains built in C interpreter which can handle multiple source files.


Compiler

1. Merit: in the process of compilation the program is analyzed only once and then the code is generated. Hence compiler is efficient than interpreter,

2. The compilers produce object code.

3. Demerit :

The compilers has to be present on the host machine when particular program needs to be compiled.

4. Demerit : The compiler is a complex program and it requires large amount of memory.

5. An compiler is a kind of translator which takes only source program as input and converts it into object code


Then loader performs loading and link editing and prepares an executable code. Compiler takes this executable code and data as input and produces output


6. Example of compiler : Borland C compiler or Turbo C compiler

compiles the programs written in Cor C++.

 

Python Interpreter

AU : Dec.-19, Marks 2

• The Python interpreter is a program that reads and executes Python code.

• On Linux, the python interpreter is installed as,

/usr/local/bin/python_version_number

•  On Windows machines, the Python installation is usually placed I C:\Python_folder.

• Depending on your environment, you might start the interpreter by clicking on an icon, or by typing python on a command line.

•  When it starts, you should see output like this:


The last line is a prompt that indicates that the interpreter is ready for you to enter code. If you type a line of code and hit enter, the interpreter displays the result. For example -


Review Question

1. Compare interpreter and compiler. What type of translator is used for Python ?

AU : Dec.-19, Marks 2

Problem Solving and Python Programming: UNIT II: Data Types, Expressions, Statements : Tag: Engineering Python : - Concept of Python Interpreter and Compiler