Presentation is loading. Please wait.

Presentation is loading. Please wait.

Running & Testing Programs :: Translators

Similar presentations


Presentation on theme: "Running & Testing Programs :: Translators"— Presentation transcript:

1 Running & Testing Programs :: Translators
Last modified: 26th August 2019

2 www.drfrostmaths.com ? Everything is completely free.
Why not register? Registering on the DrFrostMaths platform allows you to save all the code and progress in the various Computer Science mini-tasks. It also gives you access to the maths platform allowing you to practise GCSE and A Level questions from Edexcel, OCR and AQA. With Computer Science questions by: Your code on any mini-tasks will be preserved. Note: The Tiffin/DFM Computer Science course uses JavaScript as its core language. Most code examples are therefore in JavaScript. Using these slides: Green question boxes can be clicked while in Presentation mode to reveal. Slides are intentionally designed to double up as revision notes for students, while being optimised for classroom usage. The Mini-Tasks on the DFM platform are purposely ordered to correspond to these slides, giving your flexibility over your lesson structure. ?

3 Learning Objectives Directly from the OCR GCSE specification:
This stuff

4 Overview of High vs Low Level Languages
You write some code… How does it wind up being run by the CPU? Assembly code instructions Line: 1 2 3 4 5 6 7 8 ASSIGN i  1 PUT i ON STACK GO TO LINE 78 REMOVE TOP OF STACK, MAKE t OUTPUT t ASSIGN i  i + 1 IF NOT(i<= n) GO TO LINE 9 GO TO LINE 2 Original code: function factorial(n) { var a = 1; for(var i=1; i<=n; i++) { a*= i; } return a; // MAIN CODE for(var i=1; i<=10; i++) { console.log(factorial(i)); ... Your code is often converted to finer-grained ‘low level’ instructions by a compiler/interpreter. Machine code run on CPU An assembler then converts the assembly code instruction into binary code (known as machine code) that the CPU can understand. Earlier in the course we saw how the Control Unit of the CPU works, which ‘customises’ the CPU circuit in such a way that allows the instruction to be run.

5 Overview of High vs Low Level Languages
High Level Language Low Level Language function factorial(n) { var a = 1; for(var i=1; i<=n; i++) { a*= i; } return a; // MAIN CODE for(var i=1; i<=10; i++) { console.log(factorial(i)); ... ASSIGN i  1 PUT i ON STACK GO TO LINE 78 REMOVE TOP OF STACK, MAKE t OUTPUT t ASSIGN i  i + 1 IF NOT(i<= n) GO TO LINE 9 GO TO LINE 2 Examples: Java, JavaScript, C++, Python Each line of code could end up as multiple low level instructions. While the low level language might be specific to a particular CPU architecture, we can write the same code, which will be converted appropriately by the specific ‘translator’. It abstracts away specific details of how parts of the code operates: we can store variables without needing to know the machine’s memory structure. The drawback of this is that we don’t have as much control over the CPU in some languages, so some programs may be less efficient. Must be translated/interpreted before it can be run. One assembly code construction becomes one line of machine code, doing one specific action. Some languages (e.g. ‘BASIC’) allow the programmer to write these low-level instructions themselves. But often assembly code is an ‘intermediate’ language outputted by compilers. Assembly code instructions specific to a particular machine/CPU. Programmer needs to know about specific operations of the CPU, e.g. how memory is managed. Difficult to manage, read or update code. Can be executed directly without needing to translate/interpret. More control, therefore can be more efficient.

6 Test Your Understanding So Far
? It’s helpful to know what is not accepted as part of your answer.

7 Different types of translator
We’ve seen that a compiler/interpreter converts a high-level language (e.g. Java or Python) into assembly code instructions, which can be more readily turned into machine instructions that a CPU can understand. Compilers and interpreters are examples of translators, which in general change instructions from one language to another. Translator Assembler Compiler Interpreter Turns assembly language into machine code. ADD $3, $2, $1 Both turn high-level languages into low-level ones (assembly languages), or sometimes directly into machine code. We’ll see the differences on the next slide… As machine code is specific to particular CPU architectures, the assembler is also CPU specific.

8 Compilers vs translators
Turns source code into a file that can be executed (e.g. an .exe file, or .class in the case of Java files). No executable file created. All code translated at the same time when compiling. Translates and runs source code one instruction at a time. Compiling only has to be done once (until source code modified), saving time when the program is run. Program must be translated every time it is run. The compiler can identify some problems with the code (‘compiler errors’), potentially identifying problems before program is run. Since no compilation, there are no ‘compiler errors’, only ‘runtime errors’. Is a separate file produced? ? When is code translated? ? ? How often do we have to translate code? ? How are code errors handled? In summary, with compilers we ‘compile’ the program prior to running it, producing a separate ‘compiled’ file. This saves us the time of converting the program to machine instructions each time the program is run.

9 Examples of interpreters
JavaScript is an ‘interpreted’ language – the browser runs JavaScript code within a webpage without needing to compile it first.

10 Examples of interpreters
During my PhD I wrote my own interpreter for an invented language called ‘HURDLE’ which was used for coding behaviour in robots, to enable it to converse with humans and interact with the robot’s functionality. The code was translated to Java. So it should be noted that not all interpreters turn high-level languages to low-level ones (or machine code), but sometimes high-level to high-level!

11 (not in the GCSE syllabus)
Why writing compilers/interpreters is difficult… (not in the GCSE syllabus) Writing the ‘grammar’ rules that allow the compiler to read the language. 1 In maths you may be familiar with the idea of ‘order of operations’ (sometimes known as ‘BIDMAS’). Compilers/interpreters must also be able to understand this idea of precedence, but this also extends from mathematical operators to other programming constructs. var x = i > 0 ? * 2 : 3

12 function factorial(n) {
var a = 1; for(var i=1; i<=n; i++) { a*= i; } return a; // MAIN CODE for(var i=1; i<=10; i++) { console.log(factorial(i)); ... Line: 1 2 3 4 5 6 7 8 78 79 80 81 82 83 84 ASSIGN i  1 PUT I ON STACK GO TO LINE 78 REMOVE TOP OF STACK, MAKE t OUTPUT t ASSIGN i  i + 1 IF NOT(i<= n) GO TO LINE 9 GO TO LINE 2 Working out program flow with if, while and for constructs as well as procedures/functions. 2 Machine instructions are read sequentially one after another, but instructions can ask the CPU to ‘skip’ to another line of code. This enables us for example to accommodate calls to functions, as we need the CPU to move to the part of the code needed to execute the function that was called. REMOVE TOP OF STACK, MAKE n ASSIGN a  1 ASSIGN i  1 ASSIGN a  a × I ASSIGN i  i + 1 IF NOT(i <= N) PUT a ON STACK, GO BACK TO SOURCE GO TO LINE 79

13 (not in the GCSE syllabus)
Why writing compilers/interpreters is difficult… (not in the GCSE syllabus) 3 ‘Tokenising’ In English, before we use our ‘grammar’ rules to understand the structure of a sentence, the assumption is that we’ve already split the sequence of characters into distinct ‘words’. This is easy in English because words are separated by spaces. In compiling/interpreting, this process of identifying the ‘words’ in code (which might be construct words such as ‘if’, characters like {, numbers like or strings such as “bob”) is known as tokenising. if(i > 3.5) { print “hello” } Tokenise if, (, i, >, 3.5, ), {, print, “hello”

14 Test Your Understanding
?


Download ppt "Running & Testing Programs :: Translators"

Similar presentations


Ads by Google