1.2.2 Flashcards
Give two advantages of using compiled code over interpreted code
- Faster to execute
- Does not require compiler to run
- safer as can’t be reverse engineered
Give two advantages of using interpreted code over compiled code
- It is platform independent
- Runs instantly without time waiting for compilation
What are the stages of compilation?
- Lexical analysis
- Syntax analysis
- Code generation
- Optimisation
What happens during syntax analysis
- Tokens are compared to the rules of the programming language
- Syntax errors are identified and reported as a list
- Symbol table is updated with more detail
- Semantic analysis
What does a compiler do
Translates high level language source code into machine code which is stored in an object file. One or more object files are then combined to form the final executable code.
Same source code can be run through different compilers to generate machine code for different CPUs
What does an interpreter do
An interpreter translates source code one line at a time, once translated the machine code is executed immediately.
What does an assembler do
Converts assembly language source code into machine code. The assembler is usually designed to produce machine code for a specific CPU.
When is assembly language usually used
When the program needs to interact very closely with the details of the hardware, for example device drivers.
It is also used when code needs to be high speed and efficient
What happens during lexical analysis
- The compiler breaks down the ASCII into its component parts called lexemes
- Compiler scans the source code letter by letter looking for keywords (IF, WHILE)
- Removes redundant characters such as tabs, whitespace and comments
- lexemes are converted into binary sequences of fixed length called tokens
What happens during code generation
- Tokens are converted into machine code instructions
- Allocates memory locations to tokens that were noted as variables
- Works out relative addresses for jumping around within the program (IF)
- Results stored as an object file
- Tries to optimise the code (makes it more efficient and fast, removes redundant code)
What is intermediate code
simplified code that is between high level & machine code
It is produced by compiler
and runs on any computer
Benefits of intermediate code
- Allows portability between machines
- Allows sections of code to be written in different languages by different programmers
What is a virtual machine
A theoretical computer which provides an environment in which a translator is available
Uses an interpreter to run the intermediate code
Issue with virtual machines
Security, the current version of the VM may have security vulnerabilities that allow rogue code to take control of the host computer. This is why VMs are patched frequently.
Advantages of using an interpreter when writing code
Errors are reported as they occur rather than together and are easier to correct
Programs can be restarted from any point after correcting an error
Breakpoints (for debugging) can be inserted in the code to halt the program at any point at which point the values of any variables used can be displayed
It is possible to set variable values while running the program
Less memory is needed as only the object code from a single source code instruction in use is held and interpreters are usually smaller programs than compilers