Logout

4.3.5

Outline the need for a translation process from a higher level language to machine executable code.

 

Teaching Note:

For example, compiler, interpreter, virtual machine.

 

Sample Question FORMER CURRICULUM:

Outline one difference between the functioning of an interpreter and a compiler. [2 marks]

 

*** Note at this point that you may not have to know much beyond explaining why the need exists, and mentioning the three examples cited in the Teaching Note. The fact is that there certainly is a need to for translation between higher level languages to lower level languages, because we cannot read machine code, and machines cannot directly read languages such as Java. So, what are three things which can facilitate this translation?: "compilers", "interpreters" and "virtual machines". So that answers why the need, and how it is addressed. Never-the-less, you probably should understand a bit more on each:

But befor going on, here's a straight-forward "answer" to the assessment statement taken at face value: Because of the difference between higher (human) and lower (machine) level languages there need to exist interpreter programs or compiler programs which will take our higher level language code and convert it, ultimately, into machine code.


Interpreted vs Compiled Programs

- An interpreted programming language is one which is translated one line at a time by a translator program on the computer running the program.

- A compiled programming language is converted into machine code all at once by the programmer's computer, and then distributed to the users, so it can be used directly by the machine. Though note that working this way, you have to complie separate versions of your program for the various operating systems it will be used on (and .exe file for Window machines, an .app file for Mac OS and so on).

Because the interpreted program needs to be translated one instruction at a time, it is slower when used. But its one main advantage is because each line can be inspected as run, errors can be caught and handled by the interpreter. This also aids debugging during the development process.

In terms of the advantages of compiled programs, not only do they run faster, but they don't require a specific interpreter to be installed on the user's machine.

In theory any language can be compiled or interpreted, and many modern languages support both options.

Here is a great YouTube video (from 1983, my High School graduation year!!) about the differences between the way a compiler and an interpreter works.


Java as Partially Compiled, & the use of a Vitrual Machine

- Java is actually unique in that it is "partially" compiled then compiled further and/or interpreted on the user's machine. In terms of the particulars, a Java program is first partially compiled by the programmer's IDE into a JAR file - which is a compressed file containing several .class binary files. But then a "Java Virtual Machine" on the user's computer does the final interpretation of the Java code, for the specific machine on which the program runs. This way one Java JAR file can be run on any operating system which has a "Java Virtual Machine" installed, making Java a platform-independent language.

Here is a good YouTube video on how Java works in a platform independent way with the Java Virtual Machine.

**Again, note that all this information goes a bit beyond what is required by this assessment statement. If you want even more information, read on, below. But for now, do go back to the assessment statement itself and recall that the main thing you need to be aware of is the need for compilers and interpreters.

 

JSR Notes FORMER CURRICULUM - 3.1.2 Describe the function of high-level language translators.:

- The translators should be limited to interpreters and compilers.

- Most important is compiling versus Interpreting, during the translation of high level languages into low level machine code.

Most of the JSR Notes in this case are just re-stating what is generally well done in the yellow and red text book:

And though I only infrequently copy and paste from webopedia or wikipedia, in this case, it's hard to state it better. So from Wikepedia.org:

Interpreted language

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Interpreted language is a programming language in which programs are 'indirectly' executed ("interpreted") by an interpreter program. This can be contrasted with a compiled language which is converted into machine code and then 'directly' executed by the host CPU. Theoretically, any language may be compiled or interpreted, so this designation is applied purely because of common implementation practice and not some essential property of a language. Indeed, for some programming languages, there is little performance difference between an interpretive- or compiled-based approach to their implementation.

Many languages have been implemented using both compilers and interpreters, including BASIC, C, Lisp, Pascal, and Python. While Java is translated to a form that is intended to be interpreted, just-in-time compilation is often used to generate machine code.

 

Note that translation is "parsing", or changing from one form (human words written in a language like Java) to, ultimately - through various steps - the machine code of 0s and 1s specific for a specific kind of computer architecture. The key difference between compiled languages and interpreted languages is that with compiling, the "parsing" is done ahead of time to the whole program all at once, down to the actual machine code which is ready to be executed, whereas with interpretation, the parsing and execution are done by the interpreter when the program is run, one instruction at a time.

Also note that an interpreted program needs an interpreter on any computer that wishes to run it, whereas the compiled file is a complete, totally stand-alone file, like an .exe Windows file, not requiring a specific interpreter.

One interesting thing not mentioned in class is that Java can be both interpreted and compiled. How is this possible? You can refer to the Morelli text, pages 45-49, but basically, Java (along with the even newer C# - pronounced "C-sharp") are not languages that compile all the way to machine code native to a particular kind of computer, as C and C++ do; rather they compile to general byte code, which is then, in turn, is interpreted in a specific way by their respective Java Virtual Machines (VM). So, in this case, you'll note that the Java VM is the "interpreter".

(If I get a chance, I'll do a "Hello World" applet, and insert it here: The fact that I can, and you'll all be able to run it on your computer - no matter what kind of a computer you have - demonstrates this distinct advantage of Java: the ability to do the final interpretation stage in a specific way on each specific computer - assuming that you have the Java Virtual Machine running on your computer, which you almost certainly do.) (I did, but will have to do it again, as it is causing freezing in Firefox.... will do another one some time.)

 

Linking: This is worth expanding upon a bit. When you compile a Java file, after it's been parsed to "object code" (simplified, compacted code accomplished though the "lexical analysis" stages), it then needs to have all of the Java libraries you've taken advantage of linked to the file. So things like System.out.println, and Math.random need to be included in the overall file.

So for Java, the various stages (all the way from the code you write to the actual executable file on a specific computer) are:

1. Writing the source code - what you write(in NotePad, Netbeans, or whatever)
2A. Compiling your code - when you compile the program in Netbeans or whatever, the compiler checks for syntax correctness, and then makes tokens etc out of the statement you have written.
2B. Linking libraries to your code - in Netbeans, this happens when you run the program; both your lines and the lines of needed external libraries are compiled to an intermediary stage of byte-code. With Java a compressed format .jar file is produced.
3. Interpretation of the byte-code by a specific JVM of a particular computer. (That also happens when you run in Netbeans, but it happens slightly differently on each platform the program runs on.)

 

JSR To Do: Better yet would be a link to a Java file working through a GlassFish server that will not only allow Java files to be executed on any computer, but to be done so with the capability of storing information on a server, just like Stroodle does with php and a MySQL database. I’m not quite sure how the GlassFish server works, but you can imagine my delight in getting as far as I did in just half an hour or so; this opens up a whole new world of possibilities for what your programs can do: :8080/WebApplication3/ ...Actually, not sure about this one; it's working when I have Netbeans open; it has to "deploy"; I'm a little nervous to keep it deployed until I know a bit more about it - particularly security issues. So I'll show you in class; it's pretty interesting.