0% found this document useful (0 votes)
14 views

66fe65b5746f9CCWeek-02Lecture03

Cc lecture 2 part 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

66fe65b5746f9CCWeek-02Lecture03

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

Compiler Construction

(CSC-320)
Lecture # 03
Course Instructor: M. Ramzan Shahid Khan

Department of Computer Science,


Namal University Mianwali
Fall Semester, 2024
Topics
• Phases of Compiler

2
What are the Phases of Compiler Design?
• Compiler Operates in various phases

• Each phase transforms the source program from one representation


to another.

• Every phase takes inputs from its previous stage and feeds its output
to the next phase of the compiler.

3
The Phases of a Compiler
Phase Output Sample
Programmer Source string A=B+C;
Scanner (performs lexical Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
analysis) And symbol table for identifiers
;
Parser (performs syntax Parse tree or abstract syntax tree |
analysis based on the grammar =
/ \
of the programming language) A +
/ \
B C

Semantic analyzer (type Parse tree or abstract syntax tree


checking, etc)
Intermediate code generator Three-address code, quads, or int2fp B t1
RTL (Register Transfer Level) + t1 C t2
:= t2 A
Optimizer Three-address code, quads, or int2fp B t1
RTL (Register Transfer Level) + t1 #2.3 A
Code generator Assembly code MOVF #2.3,r1
ADDF2 r1,r2
MOVF r2,A
Peephole optimizer Assembly code ADDF2 #2.3,r2
MOVF r2,A
4
The Grouping of Phases

• Compiler front and back ends:


• Analysis (machine independent front end)
• Synthesis (machine dependent back end)
• Passes
• A collection of phases may be repeated only once (single pass) or
multiple times (multi pass)
• Single pass: usually requires everything to be defined before being used
in source program
• Multi pass: compiler may have to keep entire program representation in
memory

5
The Analysis-Synthesis Model of Compilation

•There are two parts to compilation:


•Analysis determines the operations implied by the source program
which are recorded in a tree structure

•Synthesis takes the tree structure and translates the operations therein
into the target program

6
Other Tools that Use the Analysis-Synthesis
Model
• Editors (syntax highlighting)
• Pretty printers (e.g. doxygen)
• Static checkers (e.g. lint and splint)
• Interpreters
• Text formatters (e.g. TeX and LaTeX)
• Silicon compilers (e.g. VHDL)
• Query interpreters/compilers (Databases)

7
Compiler-Construction Tools
1.Parser generators that automatically produce syntax analyzers from a
grammatical description of a programming language.
2.Scanner generators that produce lexical analyzers from a regular-expression
description of the tokens of a language.
3.Syntax-directed translation engines that produce collections of routines for
walking a parse tree and generating intermediate code.
4.Code-generator generators that produce a code generator from a collection
of rules for translating each operation of the intermediate language into the
machine language for a target machine.
5.Data-flow analysis engines that facilitate the gathering of information about
how values are transmitted from one part of a program to each other part.
Data-flow analysis is a key part of code optimization.
6.Compiler-construction toolkits that provide an integrated set of routines for
constructing various phases of a compiler. 8
Compiler-Construction Tools
Software development tools are available to implement one or
more compiler phases:
• Scanner generators (JFlex)
https://www.jflex.de/#:~:text=JFlex%20is%20designed%20to%20
work,or%20as%20a%20standalone%20tool.
• Parser generators
• Syntax-directed translation engines
• Automatic code generators
• Data-flow engines

9
Phases of Compiler
• There are 6 phases involved in the construction of a Compiler.

• Each of this phase help in converting the high-level language into the machine code.

• The phases of compiler are:


1. Lexical Analysis (Also Called Scanner)
2. Syntax Analysis
3. Semantic Analysis
4. Intermediate Code Generator
5. Code Optimizer
6. Code Generator

10
Phases of Compiler
Symbol Table

High Level Lexical Syntax Intermediate Code Code Machine


Language Analyser Analyser Code Generator Optimizer Generator Code

Error Handler

11
Phases of Compiler
• All these phases convert the Source Code by

• Dividing into Tokens,

• Creating Parse Trees, and

• Optimizing the Source Code by different Phases

12
Phase 1: Lexical Analysis
• Lexical Analysis is the first phase when compiler scans the source
code.

• This process can be


• Left to right
• Character by character, and
• Group these characters into tokens.

13
Phase 1: Lexical Analysis
• Here, the character stream from the source program is grouped in
meaningful sequences by identifying the tokens.

• It makes the entry of the corresponding tickets into the symbol table
and passes that token to next phase.

14
Phase 1: Lexical Analysis
The primary functions of this phase are:

• Identify the lexical units in a source code

• Classify lexical units into classes like


• Constants,
• Reserved Words, and
• Enter them in different tables
• It will ignore comments in the source program
• Identify token which is not a part of the language

15
Phase 1: Lexical Analysis - Example
X = Y + 10

Tokens:

X Identifier
= Assignment Operator
Y Identifier
+ Addition Operator
10 Number

16
Phase 2: Syntax Analysis
• Syntax Analysis is all about discovering structure in code.

• It determines whether or not a text follows the expected format.

• The main aim of this phase is to make sure that the source code was
written by the programmer is correct or not.

17
Phase 2: Syntax Analysis
• Syntax analysis is based on the rules based on the specific
programming language by constructing the Parse Tree with the help
of tokens.

• It also determines the structure of source language and grammar or


syntax of the language.

18
Phase 2: Syntax Analysis
Here, is a list of tasks performed in this phase:

• Obtain tokens from the lexical analyser

• Check if the expression is syntactically correct or not

• Report all syntax errors

• Construct a hierarchical structure which is known as a Parse Tree

19
Phase 2: Syntax Analysis - Example
Any identifier/ number is an expression

If X is an identifier and Y + 10 is an expression,

Then

X = Y + 10 is a statement.

20
Phase 2: Syntax Analysis - Example
Consider Parse Tree for the Following Example:

(a+b)*c
*

+ c

a b
21
Phase 2: Syntax Analysis - Example
In Parse Tree

• Interior Node: Record with an operator filed and two files for children

• Leaf: Records with 2/ more fields; One for Token and Other Information about the Token

• Ensure that the components of the program fit together meaningfully

• Gathers type information and checks for type compatibility

• Checks operands are permitted by the source language

22
Phase 3: Semantic Analysis
• Semantic analysis checks the semantic consistency of the code.

• It uses the syntax tree of the previous phase along with the symbol
table to verify that the given source code is semantically consistent.

• It also checks whether the code is conveying an appropriate


meaning.

23
Phase 3: Semantic Analysis
• Semantic Analyser will check for

• Type Mismatches,

• Incompatible Operands,

• A function called with improper arguments,

• An undeclared variable, etc.

24
Phase 3: Semantic Analysis
Functions of Semantic Analyses Phase are:

• Helps you to store type information gathered and save it in symbol


table or syntax tree

• Allows you to perform type checking

• In the case of Type Mismatch, where there are no exact type


correction rules which satisfy the desired operation a Semantic Error
is shown
25
Phase 3: Semantic Analysis
• Collects type information and checks for type compatibility

• Checks if the source language permits the operands or not

26
Phase 3: Semantic Analysis - Example
float X = 20.2;
float Y = X * 30;

In the above code, the semantic analyse will typecast the integer 30 to
float 30.0 before multiplication.

27
Phase 4: Intermediate Code Generation
• Once the semantic analysis phase is over the compiler, generates
intermediate code for the target machine.

• It represents a Program for some Abstract Machine.

• Intermediate Code is between the High-Level and Machine Level


Language.

• This intermediate code needs to be generated in such a manner that


makes it easy to translate it into the target machine code.
28
Phase 4: Intermediate Code Generation
Functions on Intermediate Code Generation:

• It should be generated from the semantic representation of the


source program

• Holds the values computed during the process of translation

• Helps you to translate the intermediate code into target language

29
Phase 4: Intermediate Code Generation
• Allows you to maintain precedence ordering of the source language

• It holds the correct number of Operands of the instruction

30
Phase 4: Intermediate Code Generation –
Example
total = count + rate * 5

Intermediate code with the help of address code method is:

t1 := int_to_float(5)
t2 := rate * t1
t3 := count + t2
total := t3

31
Phase 5: Code Optimization
• The next phase is Code Optimization

• This phase removes unnecessary code line and arranges the


sequence of statements to speed up the execution of the program
without wasting resources.

• The main goal of this phase is to improve on the intermediate code


to generate a code that runs faster and occupies less space.

32
Phase 5: Code Optimization
The primary functions of this phase are:

• It helps you to establish a trade-off between execution and


compilation speed

• Improves the running time of the target program

• Generates streamlined code still in intermediate representation

33
Phase 5: Code Optimization
• Removing unreachable code and getting rid of unused variables

• Removing statements which are not altered from the loop

34
Phase 5: Code Optimization - Example
a = int_to_float(10)
b=c*a
d=e+b
f=d

Can become

b = c * 10.0
f=e+b

35
Phase 6: Code Generation
• Code Generation is the last and final phase of a compiler.

• It gets inputs from code optimization phases and produces the page
code or object code as a result.

• The objective of this phase is to allocate storage and generate


relocatable machine code.

36
Phase 6: Code Generation
• It also allocates memory locations for the variable.

• The instructions in the intermediate code are converted into


machine instructions.

• This phase converts the optimize or intermediate code into the target
language.

37
Phase 6: Code Generation
• The target language is the machine code.

• Therefore, all the memory locations and registers are also selected
and allotted during this phase.

• The code generated by this phase is executed to take inputs and


generate expected outputs.

38
Phase 6: Code Generation - Example
a = b + 60.0

Would be possibly translated to registers.

MOVF a, R1
MULF #60.0, R2
ADDF R1, R2

39
Symbol Table Management
• A Symbol Table contains a record for each identifier with fields for the
attributes of the identifier.

• This component makes it easier for the compiler to search the


identifier record and retrieve it quickly.

• The symbol table also helps you for the scope management.

• The symbol table and error handler interact with all the phases and
symbol table update correspondingly.
40
Error Handling Routine
In the compiler design process error may occur in all the below-given
phases:

• Lexical Analyser: Wrongly Spelled Tokens

• Syntax Analyser: Missing Parenthesis

• Intermediate Code Generator: Mismatched Operands for an Operator

41
Error Handling Routine
• Code Optimizer: When the statement is not reachable

• Code Generator: When the memory is full or proper registers are not
allocated

• Symbol Tables: Error of Multiple Declared Identifiers

42
Error Handling Routine
• Most common errors are:

• Invalid character sequence in scanning,

• Invalid token sequences in type,

• Scope error, and

• Parsing in semantic analysis

43
Error Handling Routine
• The error may be encountered in any of the above phases.

• After finding errors, the phase needs to deal with the errors to
continue with the compilation process.

• These errors need to be reported to the error handler which handles


the error to perform the compilation process.

• Generally, the errors are reported in the form of message.


44
Summary
• Compiler operates in various phases each phase transforms the
source program from one representation to another

• Six phases of Compiler Design are


1. Lexical Analysis
2. Syntax Analysis
3. Semantic Analysis
4. Intermediate Code Generator
5. Code Optimizer
6. Code Generator

45
Summary
• Lexical Analysis is the first phase when compiler scans the source
code.

• Syntax Analysis is all about discovering structure in text.

• Semantic Analysis checks the semantic consistency of the code

• Once the semantic analysis phase is over the compiler, generate


intermediate code for the target machine
46
Summary
• Code Optimization Phase removes unnecessary code line and
arranges the sequence of statements

• Code Generation Phase gets inputs from code optimisation phase and
produces the page code or object code as a result

• A Symbol Table contains a record for each identifier with fields for the
attributes of the identifier

• Error Handling routine handles error and reports during many phases
47

You might also like