Compiler Construction

Slides:



Advertisements
Similar presentations
CS 31003: Compilers Introduction to Phases of Compiler.
Advertisements

CPSC Compiler Tutorial 9 Review of Compiler.
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
Invitation to Computer Science 5th Edition
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
COP4020 Programming Languages
Chapter 1. Introduction.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 History of compiler development 1953 IBM develops the 701 EDPM (Electronic Data Processing Machine), the first general purpose computer, built as a “defense.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Component 1.6.
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
A Simple Syntax-Directed Translator
Compiler Construction (CS-636)
Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
-by Nisarg Vasavada (Compiled*)
课程名 编译原理 Compiling Techniques
History of compiler development
Chapter 1: Introduction to Compiling (Cont.)
CS416 Compiler Design lec00-outline September 19, 2018
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Front End vs Back End of a Compilers
Introduction CI612 Compiler Design CI612 Compiler Design.
Compiler Construction
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compilers B V Sai Aravind (11CS10008).
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compiler design.
CS416 Compiler Design lec00-outline February 23, 2019
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler 薛智文 M 2 3 4, DTH Spring.
Compiler Structures 1. Overview Objective
Introduction to Compiler Construction
Faculty of Computer Science and Information System
Presentation transcript:

Compiler Construction CSC441 Lecture No. 3 Muhammad Farhan

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…

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.

Structure of a Compiler Source Language ? Structure of a Compiler Target Language

Structure of a Compiler Source Language Front End Structure of a Compiler Intermediate Code Back End Target Language

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

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

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

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

Lexical Analysis (cont’d) The blanks separating the characters of these tokens would normally be eliminated during lexical analysis.

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.

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.

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

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.

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.

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

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

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.

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.

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.

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.

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

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.

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

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

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

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

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

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

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

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

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

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

Phases of Compiler

THANKS