Advertisement

Interpreter (computing)

From Academic Kids

An interpreter is a computer program that executes other programs. This is in contrast to a compiler which does not execute its input program (the source code) but translates it into executable machine code (also called object code) which is output to a file for later execution. It may be possible to execute the same source code either directly by an interpreter or by compiling it and then executing the machine code produced.

It takes longer to run a program under an interpreter than to run the compiled code but it can take less time to interpret it than the total time required to compile and run it. This is especially important when prototyping and testing code when an edit-interpret-debug cycle can often be much shorter than an edit-compile-run-debug cycle.

Interpreting code is slower than running the compiled code because the interpreter must analyse each statement in the program each time it is executed and then perform the desired action whereas the compiled code just performs the action. This run-time analysis is known as "interpretive overhead". Access to variables is also slower in an interpreter because the mapping of identifiers to storage locations must be done repeatedly at run-time rather than at compile time.

There are various compromises between the development speed when using an interpreter and the execution speed when using a compiler. Some systems (e.g. some LISPs) allow interpreted and compiled code to call each other and to share variables. This means that once a routine has been tested and debugged under the interpreter it can be compiled and thus benefit from faster execution while other routines are being developed. Many interpreters do not execute the source code as it stands but convert it into some more compact internal form. For example, some BASIC interpreters replace keywords with single byte tokens which can be used to index into a jump table. An interpreter might well use the same lexical analyzer and parser as the compiler and then interpret the resulting abstract syntax tree.

There is thus a spectrum of possibilities between interpreting and compiling, depending on the amount of analysis performed before the program is executed. For example Emacs Lisp is compiled to bytecode which is a highly compressed and optimised representation of the Lisp source but is not machine code (and therefore not tied to any particular hardware). This "compiled" code is then executed interpreted by a bytecode interpreter (itself written in C). The compiled code in this case is machine code for a virtual machine which is implemented not in hardware but in the byte-code interpreter. The same approach is used with the Forth code used in Open Firmware systems: the source language is compiled into "F code" (a bytecode) which is then interpreted by an architecture-independent virtual machine.

A technique that has gained attention in recent years is Just In Time compilation, which further blurs the distinction between interpreters, byte-code interpreters and compilation. JIT is available for both the .NET and Java platforms. The JIT technique is several decades old, appearing in languages such as Smalltalk in the 1980's.

Contents

Example of a simple interpreter

The following simple interpreter program is written using BASIC. When compiled using the QuickBASIC compiler it is a straightforward interpreter but when run on the QBASIC interpreter, it is an example of an interpreted interpreter.

DECLARE SUB SplitFirst (aFirst AS STRING, aRest AS STRING)
LET Q$ = "TESTPROG.TXT"
LET F = FREEFILE
OPEN Q$ FOR INPUT AS #F
DO WHILE NOT EOF(F)
  LINE INPUT #F, FileInput$
  LET FileInput$ = LTRIM$(FileInput$)
  SplitFirst KeyWord$, FileInput$
  SELECT CASE KeyWord$
  CASE "-"
    SplitFirst KeyWord$, FileInput$
    GOSUB InterpretKeyword
  END SELECT
LOOP
CLOSE #F
SYSTEM

InterpretKeyword:
  SELECT CASE UCASE$(KeyWord$)
  CASE "STORE"
    GOSUB AssignToValue
  CASE "ADD"
    GOSUB AddToValue
  CASE "MULTIPLYBY"
    GOSUB MultiplyWithValue
  CASE "PRINT"
    GOSUB PutOutput
  CASE "CLS"
    GOSUB ClearScreen
  CASE "NEWLINE"
    PRINT
  CASE ELSE
    PRINT
    PRINT "I don't know what "; KeyWord$; " "; FileInput$; " means."
  END SELECT
  RETURN

AssignToValue:
  GOSUB GetArg
  LET Value$ = Arg$
  RETURN

AddToValue:
  GOSUB GetArg
  LET Value$ = LTRIM$(STR$(VAL(Value$) + VAL(Arg$)))
  RETURN

MultiplyWithValue:
  GOSUB GetArg
  LET Value$ = LTRIM$(STR$(VAL(Value$) * VAL(Arg$)))
  RETURN

GetArg:
  Split KeyWord$, FileInput$
  SELECT CASE UCASE$(KeyWord$)
  CASE "INPUT"
    GOSUB GetInput
    LET Arg$ = UserInput$
  CASE "VALUE"
    LET Arg$ = Value$
  CASE "TEXT"
    LET Arg$ = FileInput$
  CASE ELSE
    LET Arg$ = KeyWord$ + " " + FileInput$
  END SELECT
  RETURN

GetInput:
  LINE INPUT "", UserInput$
  RETURN

PutOutput:
  GOSUB GetArg
  PRINT Arg$; " ";
  RETURN

ClearScreen:
  CLS
  RETURN

NewLine:
  PRINT
  RETURN

SUB SplitFirst (aFirst AS STRING, aRest AS STRING)

  DIM J AS INTEGER

  LET J = INSTR(aRest + " ", " ")
  LET aFirst = LTRIM$(LEFT$(aRest, J - 1))
  LET aRest = LTRIM$(MID$(aRest, J))

END SUB

There is a program to try with this interpreter, on the Literate programming article. If you want to do so you must save the interpreter as INTERP.BAS and then save the Literate programming article as TESTPROG.TXT in the same folder.

Punched card interpreter

The term "interpreter" often referred to a piece of unit record equipment that could read punched cards and print the characters in human-readable form along the top edge of the card. The IBM 550 Numeric Interpreter and IBM 557 Alphabetic Interpreter are typical examples from 1930 and 1954, respectively.

See also

External links

de:Interpreter et:Interpretaator es:Intrprete informtico fr:Interprteur ko:인터프리터 he:מפרש (תוכנה) lt:Interpretatorius nl:Interpreter ja:インタプリタ pl:Interpreter fi:Ohjelmointikielen tulkki sv:Interpretator

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools