Translators: Interpreters, compilers and assemblers, stages of compilation (lexical analysis, syntax analysis, code generation and optimisation) Flashcards
What is a translator?
A translator is a program that converts high-level source code into low-level object code, which is then ready to be executed by a computer
What are 3 translators?
- Compilers
- Interpreters
- Assemblers
What are compilers?
Converts high-level source code into object code which can be saved and run without a translator
Features of compilers/compiled code?
- Different hardware platform will require different compilers, since the resulting object code will be hardware specific
- Once compiled does not need to go through a interpreter
- Takes longer to compile (translation) but execution is faster
What are interpreters?
Translates and executes source code line by line (high level language)
Features of interpreters?
- Does not produce code which can run without a translator so can be slower to run
- E.g. Using Idle python, executes code line by line
- Faster to compile but execution is slower
What are assemblers?
Converts low level assembly code into machine code instructions
Features of assemblers/assembly code?
- Only used on assembly code not on high level languages (e.g. python)
- Must be translated into the equivalent machine code, or an intermediate form called bytecode
- The output of the assembler (machine code) is called the object code (platform specific)
What happens during lexical analysis?
- The lexer starts by converting lexemes in the source code into a series of tokes
- The lexer reads (parsing) the source code, it scans the code letter by letter
- Encounters a white space, operator symbol or special symbol it decides that a word (lexeme) is complete
- Checks if the lexeme is valid using a predefined set of rules that allow every lexeme to be identified as a valid token (operator + is considered a token)
- White space and comments have been removed because they are simply passed over by the lexer
Why does the length of variable names and amount of comments in a program’s source makes no difference to the size of a compiled program?
- Comments only relevant in the source code
- There is no performance difference, since those names don’t matter at the machine level, a compiler will process those names away so it won’t make a difference to the program at all
What is meant by syntax/syntax analysis?
- Syntax is the set of rules a programming language requires code to follow
- Syntax analysis is making sure the code follows the rules
What happens during syntax analysis?
- It receives its inputs in the form of tokens from lexical analysis
- Analyses the syntactical structure of the input, checking if it’s in the correct syntax of the programming language it has been written in
Analyses the token stream against production rules to detect any errors in the code, accomplishes two tasks: - Checks for errors and reporting them
- Building an abstract syntax tree (parse tree)
What happens during code generation?
Code generation the compiler takes the parse tree and converts it into machine code
What happens during code optimisation?
Code optimisation attempts to reduce the execution time of the program by:
- Spotting redundant instructions and producing object code that achieves the same effect as the source program
- Removing subroutines that are never called
- Removing variables and constants that are never referenced
What is the aim of code optimisation?
The aim of optimisation is to make the code faster to execute, optimisation tweaks the code so it will run as quickly and use as little memory as possible