How does an interpreter/compiler work

0 votes
asked Mar 4, 2010 by developer

How does an interpreter/compiler work? What is the difference between interpreter and compiler.

5 Answers

0 votes
answered Mar 4, 2010 by adriaan-stander

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.

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.

Compiler characteristics:

  • spends a lot of time analyzing and processing the program
  • the resulting executable is some form of machine- specific binary code
  • the computer hardware interprets (executes) the resulting code
  • program execution is fast

Interpreter characteristics:

  • relatively little time is spent analyzing and processing the program
  • the resulting code is some sort of intermediate code
  • the resulting code is interpreted by another program
  • program execution is relatively slow
0 votes
answered Mar 4, 2014 by martynas

What is a translator?

An S -> T translator accepts code expressed in source language S, and translates it to equivalent code expressed in another (target) language T.

Examples of translators:

  • Compilers - translates high level code to low level code, e.g. Java -> JVM
  • Assemblers - translates assembly language code to machine code, e.g. x86as -> x86
  • High-level translators - translates code from one PL to another, e.g. Java -> C
  • Decompilers - translates low-level code to high-level code, e.g. Java JVM bytecode -> Java

What is an interpreter?

An S interpreter accepts code expressed in language S, and immediately executes that code. It works by fetching, analysing, and executing one instruction at a time.

Great when user is entering instructions interactively (think Python) and would like to get the output before putting in the next instruction. Also useful when the program is to be executed only once or requires to be portable.

  • Interpreting a program is much slower than executing native machine code
  • Interpreting a high-level language is ~100 times slower
  • Interpreting an intermediate-level (such as JVM bytecode) language is ~10 slower
  • If an instruction is called repeatedly, it will be analysed repeatedly - time-consuming!
  • No need to compile code

Differences

Behaviour

  • A compiler translates source code to machine code, but does not execute the source or object code.

  • An interpreter executes source code one instruction at a time, but does not translate the source code.

Performance

  • A compiler takes quite a long time to translate the source program to native machine code, but subsequent execution is fast
  • An interpreter starts executing the source program immediately, but execution is slow

Interpretive compilers

An interpretive compiler is a good compromise between compilers and interpreters. It translates source program into virtual machine code, which is then interpreted.

An interpretive compiler combines fast translation with moderately fast execution, provided that:

  • VM code is lower than the source language, but higher than native machine code
  • VM instructions have simple formats (can be quickly analysed by an interpreter)

Example: JDK provides an interpretive compiler for Java.

0 votes
answered Mar 21, 2015 by premraj

What is the difference between interpreter and compiler?

enter image description here

The Compiler translates the entire program before it is run.

The Interpreters translates one statement into machine language, executes it, and proceeds to next statement.

How does an interpreter/compiler work?

enter image description here

  • Unlike compiled languages which are translated into machine language ahead of time (right).
  • Interpreted languages are translated at runtime.
  • dBASE and BASIC interpreters (middle) translate the original source code.
  • Java and Visual Basic (left) interpreters translate bytecode, which is an intermediate language compiled from the original source code.

Source

0 votes
answered Mar 27, 2015 by krishna

Compiler - A compiler is a computer program that transforms (translates) source code of a programming language (the source language) into another computer language (the target language). In most cases compilers are used to transform source code into executable program, i.e. they translate code from high-level programming languages into low (or lower) level languages, mostly assembly or machine code.

Interpreter - An interpreter is a computer program that executes instructions written in a programming language. It can either execute the source code directly or translates the source code in a first step into a more efficient representation and executes this code

0 votes
answered Sep 15, 2017 by dmitry-ponyatov

Look at PLAI book, it's the best intro to dymanic language realization I found ever:

Programming Languages: Application and Interpretation (c) Shriram Krishnamurthi

This book centers on writing interpreter for dynamic language in Scheme (dr.Racket), using it you can write your own interpreter for any language, and add some tips on OOP from

and SmallTalk and SOM: Simple Object Machine:

All modern interpreters include compiler inside: compile highlevel elements into low-level but portable byte-code, or use JIT for compiling into machine code into RAM.

PS: If anybody wants to write SmallTalk system on Python, please note me.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...