Programming Language Translators

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Overview

CraigNDave

https://www.youtube.com/watch?v=qHM4rtpYuxE&list=PLCiOXwirraUDJYD-VxmztyhsDP0QM2BHW&index=3

Computer Science Tutor

https://www.youtube.com/watch?v=eXu0c5vZnhU&list=PL04uZ7242_M6O_6ITD6ncf7EonVHyBeCm&index=16

TRC Video

https://www.youtube.com/watch?v=Z01WWoqO-Kw

Assembler

An assembler translates assembly language into machine code. One line of assembler will create a single line of machine code, because the assembler mnemonic is converted into the binary instruction.

Compiler

A compiler translates code written in a high level programming language into object code, the machine code of a particular machine. It 'compiles' the entire source code before translating it. Remember this compiles the whole program at the same time, and any syntax error within the program will cause the compiler to fail. Also remember that once compiled you only need to recompile if the program is changed.

Interpreter

An interpreter also translates code written in a programming language but unlike a compiler it does this one line of code at a time, and it doesn't create any object code which means the program is interpreted every time it runs. This makes it slower to run than a compiler, but interpreters are useful for partially testing or debugging programs. Especially because the interpreter will only find errors if it makes it to the syntax error.

One key issue with interpreters, is iteration (loops). Each run through the loop will be interpreted again even though its the same code as before, a compiler just compiles once.

Compiler vs Interpreter

Compiler over Interpreter

  • Object code is saved, so you only need to compile once so no need to re-compile. Obviously if the code is changed it will need to be re-compiled.
  • Executes faster than interpreted code, because interpreted code is interpreted and execute one at a time. Compiled code is simply executed.
  • Object code can be distributed (without the compiler).
  • Secure because the source code is hidden, the compiler generated object code is not easily reverse engineered.
  • Compilers can be more efficient, for example an interpreter must interpret and then execute each time it passes through a loop. Compiled code can just execute each time through the loop.

Interpreter over Compiler

  • Easier to partially test or debug programs, it will run until an error is reached. This will therefore allow you to test code that you haven't completely finished.

Types of Code

Source Code

The code written by the programmer.

Object Code

The code generated by compiling your source code. This object code can then be executed without the need to recompile.

Byte Code

Compilers & Interpreters would suggest there are clear divides between the languages which are interpreted and the languages that are compiled. This is not the case, some languages use both. The source code is first compiled into byte code, and then when run the byte code is then interpreted.

Languages such as Java work in this way. Java code is compiled into byte code which can then be distributed onto any platform. This is because the byte code is an instruction set than can be executed in a virtual machine. The virtual machine can then emulate the architecture of a computer allowing the byte code to be executed on any platform.

https://www.youtube.com/watch?v=gWKoW_Z5Liw&list=PLCiOXwirraUDJYD-VxmztyhsDP0QM2BHW&index=4

Quiz

1. What code is generated from a compiler and can be run natively on a device?

Source code
Byte Code
Object code

2. What code is written by the user and can easily be edited by the user?

Source code
Byte Code
Object code

3. What code is generated from a compiler and can be run within a virtual machine on a device?

Source code
Byte Code
Object code

4. Which of these is not a characteristic of using a compiler?

Produces object code that can be distributed.
Incorrect.
Executes code line by line.
Correct.
Source code is hidden.
Incorrect.
Code does not need to be recompiled.
Incorrect.

5. Which of these are the benefits of a compiler?

Once compiled, the code does not need to be compiled again
The program can start running straight away
Program is faster at runtime
Source code is hidden and can't easily be decompiled
Interpreted
nah fam
Compiled
nah fam
Both
ayyy

6. Which of the following is a disadvantage of Compiled Programs?

They're Faster at Runtime
Incorrect, Thats an advantage
They mask the source code from any one attempting to modify the progran
Incorrect, Thats an advantage
They cant be modified directly and must be recompiled
Correct

7. Which of these are benefits of interpreted languages?

The program runs straight away from source code, with no need for compiling
The program is converted to machine instructions line by line as it is required
The whole program is converted to object code at once
This is how compilers work
The program does not have to be finished to be executed
The program can have unfinished sections of code and still be run
Interpreted languages run faster than compiled languages
Compiled languages run faster than interpreted languages

Your score is 0 / 0