Compilation Flashcards

1
Q

Stages of Compilation

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Lexical Analysis

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Lexical Analysis process (exam question)

A
  1. source code program used as input
  2. series of tokens creatted from the individual symbols and reserved words/keywords in the program
  3. each token is a fixed length string of binary digits
  4. variable names are loaded in look-up/symbol table which stores infromation about variables and subroutines
  5. redundant characters e.g. white spaces are removed
  6. comments are removed
  7. error diagnostics are given
  8. prepares code for syntax analysis
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Syntax Analysis

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Syntax Analysis process (exam question)

A
  1. recieves and accepts output from lexical analysis
  2. 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
  3. abstract syntax tree is built
  4. errors are reported as a list at end of compilation
  5. error diagnostics are given
  6. further detail is added to the symbol table e,g, data type, scope and address
  7. no errors = passes code to code generation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Code Generation

A

abstract syntax tree produced in previous stage is used to produce machine code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Code Generation Process (exam question)

A
  1. last phase of compilation
  2. abstract syntax tree converted into object/machine code
  3. produces machine code program/executeable code/intemediate code which is equivalent to the source program
  4. variables and constants are given addresses
  5. relative addresses are calculated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Optimisation

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Optimation (exam question)

A
  1. occurs during code generation
  2. object is checked and made efficient as possible
  3. increases processing speed
  4. reduces number fo instructions
  5. programmer can choose between speed and size
How well did you know this?
1
Not at all
2
3
4
5
Perfectly