0% found this document useful (0 votes)
38 views2 pages

Stages of Compilation

The document discusses the stages of compilation that source code undergoes when being converted to machine code by a compiler. It describes 4 main stages: 1) Lexical analysis where the source code is broken down into tokens, 2) Syntax analysis where the tokens are checked for correct syntax, 3) Code generation where machine code is initially generated from the syntax tree, and 4) Optimization where the machine code is optimized to improve efficiency. Each stage performs different parsing and preparation of the code for the next stage until machine code is produced.

Uploaded by

gcoopsey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views2 pages

Stages of Compilation

The document discusses the stages of compilation that source code undergoes when being converted to machine code by a compiler. It describes 4 main stages: 1) Lexical analysis where the source code is broken down into tokens, 2) Syntax analysis where the tokens are checked for correct syntax, 3) Code generation where machine code is initially generated from the syntax tree, and 4) Optimization where the machine code is optimized to improve efficiency. Each stage performs different parsing and preparation of the code for the next stage until machine code is produced.

Uploaded by

gcoopsey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Stages of Compilation:

What is the Compiler?


 As programmers, we write source code to be:
o Close to English
o Easily read, modified, and interpreted by humans.
 A processor cannot run source code – it needs to be turned into machine code.
 During the compilation process, a compiler runs through a series of parses.
 With each parse, the compiler performs different actions on the source code.
 Each parse is designed to carry out set tasks and prepare the code for the next stage of
compilation.

Stage 1: Lexical Analysis:


 The lexer starts by converting lexemes in the source code into a series of tokens
 As the lexer reads the source code, it scans the code letter by letter
 When it encounters a white space, operator symbol or special symbol, it decides that a
word (lexeme) is complete
 It then checks if the lexeme is valid using a predefined set of rules that allows every
lexeme to be identified as a valid token
 Keywords, constants, identifiers, strings, numbers, operators, and punctuation
symbols are all considered tokens

Stage 2: Syntax Analysis:


 Syntax analysis is the second phase of the compiler design process and comes after
lexical analysis.
 It receives its inputs in the form of tokens from lexical analysers.
 It analyses the syntactical structure of the input, checking if it is in the correct syntax
of the programming language it has been written in.
 It does this by analysing the token stream against production rules to detect any errors
in the code.
 Doing this accomplishes two tasks:
o Checking for errors and reporting them
o Building an abstract syntax tree (parse tree)
 We already have our token stream created from lexemes in the original source code.
 The lexer couldn’t tell us if the tokens were valid within the syntax of the language –
but the syntax analyser does.
Stage 3 and 4: Code Generation and Optimisation:
 In these final phases, the machine code is generated.
 Code optimisation attempts to reduce the execution time of the program by:
o Spotting redundant instructions and producing object code that achieves the
same effect as the source program – but not necessarily by the same means.
o Removing subroutines that are never called,
o Removing variables and constants that are never referenced.
 Code optimisations can considerably decrease compilation time for a program

You might also like