Monday, September 27, 2010

Project 5 - Interpreters and Compilers

In this project we'll be looking at interpreters and compilers for languages such as BASIC, PASCAL, C and C++. What they are, how to get one and how to use one.

Compilers take your source code, that is the code that you have written, and translate it into machine language so that the computer can understand it. It checks to make sure everything is grammatically correct first. Should it find something wrong it will spit out syntax errors and stop compiling. Should everything check out, it will create a separate file with the machine code translation and run it. Compiling large amounts of code will take some time, which is why interpreters are often used during the creation process, however, compiled code usually runs much faster than interpreted code.

So the main advantages of compilers is that they:
  • produce programs which will run very quickly.
  • can spot syntax errors whilst the program is being compiled (however this does not necessarily mean the program is error free if it completely compiles, just that there are no grammatical errors)
Interpreters take your high-level language and translates it to a low-level one. You write the program than instruct the interpreter to run it. It takes the program, one line at a time and translates each line before running it. Something worth noting is that it has no memory for translated lines, so in order to run a loop it will re-translate every line when it comes to it. Using this is a much more effecient way to check your program as you're writing it since you don't have to wait for the compilation to finish. However, it is usually much slower overall, and for this reason interpreters are primarily used to check code. Most software you buy was most likely interpreted whilst the designers were making it, however what you usually get is the compiled code. Since this is machine code opening up with a text-editor will just get you a whole bunch of meaningless gobbly-gook.

So the main advantages of interpreters is that they:
  • have no lengthy compile time, i.e. you do not have to wait between writing a program and running it. As soon as you have written the program you may run it, which is great for checking larger programs.
  • tend to be more portable, that is an interpreter will run on a greater variety of machines, because each machine can have it's own interpreter for that language.

Finding interpreters and compilers for these languages is typically a very simple process. A simple Google search will turn up many interpreters and compilers that you could choose from. Often these are largely similar with a few key or minor differences between them in how they run and what they output. For example: a Google search of "interpreter and compiler for C" on the first page gives you 6 different possibilities for a download, mostly free software. The other 4 are pages with explanations of interpreters and compilers. It is crucial that you read the page you find it on and find out what exactly that particular interpreter or compiler will do. For many languages the developers will have a website with their compiler program for the language on it, such as Sun Microsystems with their Java language or Microsoft with their C# language.

No comments:

Post a Comment