Download presentation
Presentation is loading. Please wait.
1
Compiler Construction
CSC441 Lecture No. 3 Muhammad Farhan
2
Modern Compilers Compilers have not changed a great deal since the days of Backus. They still consist of two main components: The front-end reads in the program in the source languages, makes sense of it, and stores it in an internal representation…
3
And the back-end, which converts the internal representation into the target language, perhaps with optimizations. The target language used is typically an assembly language, but it is often easier to use a more established, higher-level language.
4
Structure of a Compiler
Source Language ? Structure of a Compiler Target Language
5
Structure of a Compiler
Source Language Front End Structure of a Compiler Intermediate Code Back End Target Language
6
Structure of a Compiler
Source Language Lexical Analyzer Front End Syntax Analyzer Semantic Analyzer Structure of a Compiler Int. Code Generator Intermediate Code Back End Target Language
7
Structure of a Compiler
Source Language Lexical Analyzer Front End Syntax Analyzer Semantic Analyzer Structure of a Compiler Int. Code Generator Intermediate Code Code Optimizer Back End Target Code Generator Target Language
8
Lexical Analysis In a compiler linear analysis is called lexical analysis or scanning. For example in lexical analysis the characters in the assignment statement. Position = initial + Rate*60
9
Lexical Analysis (cont’d)
Would be grouped into the following tokens. The identification position The assignment symbol:= The identifier initial The plus sign The identifier rate The multiplication sign The number 60
10
Lexical Analysis (cont’d)
The blanks separating the characters of these tokens would normally be eliminated during lexical analysis.
11
Syntax Analysis Hierarchical analysis is called parsing or syntax analysis. It involves grouping the token of the source program into grammatical phrases that are used by the compiler to synthesize the output. Usually the grammatical phrases of the source program are represented by a parse tree.
12
Semantic Analysis The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent code generation phase. It uses the hierarchical structure determined by the syntax analysis phase to identify the operators and operands of expressions and statements.
13
Semantic Analysis (cont’d)
An important component of semantic analysis is type checking hence the compiler checks that each operator has operands that are permitted by the source language specification. For example many programming language definitions require a compiler to report an error every time a real number is used to index an array
14
Intermediate Code Generation
After syntax and semantic analysis some compilers generate an explicit intermediate representation of the source programme. This intermediate representation should have two important properties it should be easy to produce and easy to translate into the target programme.
15
Intermediate Code Generation (cont’d)
We consider an intermediate form called three address code which is like the assembly language for a machine in which every memory location can act like a register. These address code consists of a sequence of instructions each of which has at most three operands.
16
Intermediate Code Generation (cont’d)
The source programme in three address code is: Temp1 = Into real (60) Temp2 =Id3 * Temp1 Temp3 = Id2+ Temp2 Id1 = Temp3
17
Intermediate Code Generation (cont’d)
This intermediate form has several properties; Each three address instruction has at most one operator in addition to the assignment thus when generating these instructions the compiler has to decide on the order in which operations are to be done; the multiplication precedes the addition in source programme
18
Intermediate Code Generation (cont’d)
The compiler must generate a temporary name to hold the value computed by each instruction. Some “Three address” instruction have fewer than three operands e.g. the first and last instruction.
19
Code Optimization The code optimization function phase attempts to improve the intermediate code so that faster running machine code will result. Optimization is a very broad term. In general it implies modifying the system to make some of its aspect to work more efficiently or use fewer resources or be more robust. Optimization is a very broad term. In general it implies modifying the system to make some of its aspect to work more efficiently or use fewer resources or be more robust. For example, a computer program may be optimized so that it will execute faster or use less memory or disk storage or be more responsive in terms of UI.
20
Code Optimization (cont’d)
For example, a computer program may be optimized so that it will execute faster or use less memory or disk storage or be more responsive in terms of UI. In computing and computer science, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the amount of memory occupied. The growth of portable computers has created a market for minimizing the power consumed by a program. Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output program that uses fewer resources.
21
Code Generation The final phase of the compiler is the generation of target code constructing normally of relocatable m/c code or assembly code. Memory locations are selected for each variables used by the program. A crucial aspect is the assignment of variables to registers.
22
Code Generation (cont’d)
For example using registers 1 and 2 the translation of the code might become MOVF id 3, R2 MULF # 60.0,R2 MOVF id2,R1 ADDF R2,R1 MOVF R1, ID 1
23
Code Generation (cont’d)
The first and the 2nd operands of each instruction specify a source and destination respectively . The F in each instruction tells us that instruction deal with floating point numbers.
24
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Source Code: Position = Initial + Rate * 60 Syntax Analyzer Semantic Analyzer Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
25
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Semantic Analyzer Source Code: Position = Initial + Rate * 60 Lexical Analysis: ID(1) ASSIGN ID(2) ADD ID(3) MULT INT(60) Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
26
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Semantic Analyzer Source Code: Position = Initial + Rate * 60 Lexical Analysis: ID(1) ASSIGN ID(2) ADD ID(3) MULT INT(60) Syntax Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) INT(60) Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
27
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Syntax Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) INT(60) Sematic Analysis: ID(3) int2float INT(60) Semantic Analyzer Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
28
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Sematic Analysis: ASSIGN ID(1) ADD ID(2) MULT ID(3) int2float INT(60) Intermediate Code: temp1 = int2float(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Semantic Analyzer Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
29
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Semantic Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 0): temp1 = int2float(60) Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
30
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Semantic Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 1): temp1 = 60.0 Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
31
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Semantic Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 2): temp2 = id3 * 60.0 Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
32
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Semantic Analyzer Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code (step 3): temp2 = id3 * 60.0 id1 = id2 + temp2 Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
33
Example Compilation Source Language Lexical Analyzer Syntax Analyzer
Intermediate Code: temp1 = int2real(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Optimized Code: temp1 = id3 * 60.0 id1 = id2 + temp1 Target Code: MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1 Semantic Analyzer Int. Code Generator Intermediate Code Code Optimizer Target Code Generator Target Language
34
Phases of Compiler
35
THANKS
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.