Compilation Flashcards
Stages of Compilation
when a compiler is used, high level code goes through 4 stages before its turned into object code ready to be executed:
- lexical analysis
- syntax analysis
- code generation
- optimisation
Lexical Analysis
first stage of compilation
whitespace and comments are removed from code
remaining code is analysed for keywords and names of variables and constants
these are replaced with tokens and infromation about the token associated with each keyword identifier is stored in a symbol table
Lexical Analysis process (exam question)
- source code program used as input
- series of tokens creatted from the individual symbols and reserved words/keywords in the program
- each token is a fixed length string of binary digits
- variable names are loaded in look-up/symbol table which stores infromation about variables and subroutines
- redundant characters e.g. white spaces are removed
- comments are removed
- error diagnostics are given
- prepares code for syntax analysis
Syntax Analysis
tokens are analysed against the grammar rules of the programming language
any tokens that break the rules of programming languages are flagged as syntax errors and added to list of errors
examples include:
- undeclared variable type
- incomplete set of brackets
abstract syntax tree is produced which is a representation fo the source code in the form of a tree
further detail about identifiers are added to the symbol table
semantic analysis is carried out at this stage, where logic mistakes within the program are detected for example multiple declaration, undeclared identifiers
Syntax Analysis process (exam question)
- recieves and accepts output from lexical analysis
- compiler checks statements, arithmetic expressions and tokens in the program are syntactically correct against the grammar rules about the structure of the programming language e.g. matching brackets
- abstract syntax tree is built
- errors are reported as a list at end of compilation
- error diagnostics are given
- further detail is added to the symbol table e,g, data type, scope and address
- no errors = passes code to code generation
Code Generation
abstract syntax tree produced in previous stage is used to produce machine code
Code Generation Process (exam question)
- last phase of compilation
- abstract syntax tree converted into object/machine code
- produces machine code program/executeable code/intemediate code which is equivalent to the source program
- variables and constants are given addresses
- relative addresses are calculated
Optimisation
searches through code for areas that need to be more efficient
aim of it is to make code faster to execute although stage can significantly add to overall time taken for compilation
insignificant/
redundant parts of code are detected removed
repeated code replaced wiht mroe efficient piece of code which produces same result
excess optimisations causes danger as program behaviour can be altered
Optimation (exam question)
- occurs during code generation
- object is checked and made efficient as possible
- increases processing speed
- reduces number fo instructions
- programmer can choose between speed and size