Stages of compilation Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

When a compiler is used, high-level code goes through…

A

four stages before it is turned into
object code that is ready to be executed.

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

What are the 4 stages

A

Lexical Analysis
Syntax Analysis
Code Generation
Optimisation

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

Lexical Analysis

A

In the first stage of compilation, whitespace and comments are removed from the code.

The remaining code is analysed for keywords and names of variables and constants.

These are replaced with tokens and information about the token associated with each keyword or identifier is stored in a symbol table

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

Syntax Analysis

A

In this stage, tokens are analysed against the grammar and rules of the programming language.
Any tokens that break the rules of the programming language are flagged up as syntax errors and added to a list of errors.

An abstract syntax tree is produced, which is a representation of the source code in the form of a tree.
Further detail about identifiers is also added to the symbol table.

Semantic analysis is also carried out at the syntax analysis stage, where logic mistakes within the program are detected.

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

Examples of syntax errors:

A

undeclared variable type, incomplete set of brackets.

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

Examples of semantic errors:

A

multiple declaration,
undeclared identifiers.

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

Code Generation

A

The abstract syntax tree produced in the syntax analysis stage is used to produce machine code.

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

Optimisation

A

This stage of compilation searches through the code for areas it could be made more efficient, reducing execution time. Although this stage can significantly add to the overall time taken for compilation.

Insignificant, redundant parts of code are detected and removed.

Repeated sections of code may be grouped and replaced with a more efficient piece of code which produces the same result.

There is a danger, however, that excessive optimisation may alter the way in which the program behaves.

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