1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.

Slides:



Advertisements
Similar presentations
Compilers and Language Translation
Advertisements

Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
CPSC Compiler Tutorial 9 Review of Compiler.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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 Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CSC 338: Compiler design and implementation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
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.
1 Chapter 1 Introduction. 2 Outlines 1.1 Overview and History 1.2 What Do Compilers Do? 1.3 The Structure of a Compiler 1.4 The Syntax and Semantics of.
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 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.
Introduction to Code Generation and Intermediate Representations
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.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
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
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
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.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CSC 8505 Compiler Construction
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Sung-Dong Kim Dept. of Computer Engineering, Hansung University
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
Compiler Construction (CS-636)
Introduction.
Short introduction to compilers
-by Nisarg Vasavada (Compiled*)
Compiler Lecture 1 CS510.
Introduction to Compiler Construction
Compilers B V Sai Aravind (11CS10008).
CMPE 152: Compiler Design August 21/23 Lab
System Programming and administration
Introduction Chapter : Introduction.
Presentation transcript:

1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana

2 What is a Compiler? A compiler is a computer program that translates a program in a source language into an equivalent program in a target language. A source program/code is a program/code written in the source language, which is usually a high-level language. A target program/code is a program/code written in the target language, which often is a machine language or an intermediate code. compiler Source program Target program Error message

3 Process of Compiling scanner parser Semantic analyzer Intermediate code generator Code optimization Code generator Code optimization Stream of characters Stream of tokens Parse/syntax tree Annotated tree Intermediate code Target code

4 Some Data Structures Symbol table Literal table Parse tree

5 Symbol Table Identifiers are names of variables, constants, functions, data types, etc. Store information associated with identifiers –Information associated with different types of identifiers can be different Information associated with variables are name, type, address,size (for array), etc. Information associated with functions are name, type of return value, parameters, address, etc.

6 Symbol Table (cont..) Accessed in every phase of compilers –The scanner, parser, and semantic analyzer put names of identifiers in symbol table. –The semantic analyzer stores more information (e.g. data types) in the table. –The intermediate code generator, code optimizer and code generator use information in symbol table to generate appropriate code. Mostly use hash table for efficiency.

7 Literal table Store constants and strings used in program –reduce the memory size by reusing constants and strings Can be combined with symbol table

8 Parse tree Dynamically-allocated, pointer-based structure Information for different data types related to parse trees need to be stored somewhere. –Nodes are variant records, storing information for different types of data –Nodes store pointers to information stored in other data structure, e.g. symbol table

9 Scanning A scanner reads a stream of characters and puts them together into some meaningful (with respect to the source language) units called tokens. It produces a stream of tokens for the next phase of compiler.

10 Parsing A parser gets a stream of tokens from the scanner, and determines if the syntax (structure) of the program is correct according to the (context-free) grammar of the source language. Then, it produces a data structure, called a parse tree or an abstract syntax tree, which describes the syntactic structure of the program.

11 Semantic analysis It gets the parse tree from the parser together with information about some syntactic elements It determines if the semantics or meaning of the program is correct. This part deals with static semantic. –semantic of programs that can be checked by reading off from the program only. –syntax of the language which cannot be described in context-free grammar. Mostly, a semantic analyzer does type checking. It modifies the parse tree in order to get that (static) semantically correct code.

12 Intermediate code generation An intermediate code generator –takes a parse tree from the semantic analyzer –generates a program in the intermediate language. In some compilers, a source program is translated into an intermediate code first and then the intermediate code is translated into the target language. In other compilers, a source program is translated directly into the target language.

13 Intermediate code generation (cont..) Using intermediate code is beneficial when compilers which translates a single source language to many target languages are required. –The front-end of a compiler – scanner to intermediate code generator – can be used for every compilers. –Different back-ends – code optimizer and code generator– is required for each target language. One of the popular intermediate code is three- address code. A three-address code instruction is in the form of x = y op z.

14 Code optimization Replacing an inefficient sequence of instructions with a better sequence of instructions. Sometimes called code improvement. Code optimization can be done: –after semantic analyzing performed on a parse tree –after intermediate code generation performed on a intermediate code – after code generation performed on a target code

15 Code generation A code generator –takes either an intermediate code or a parse tree –produces a target program.

16 Error Handling Error can be found in every phase of compilation. –Errors found during compilation are called static (or compile-time) errors. –Errors found during execution are called dynamic (or run-time) errors Compilers need to detect, report, and recover from error found in source programs Error handlers are different in different phases of compiler.

17 A compiler which generates target code for a different machine from one on which the compiler runs. A host language is a language in which the compiler is written. –T-diagram Cross compilers are used very often in practice. Cross Compiler S H T

18 Cross Compilers (cont’d) If we want a compiler from language A to language B on a machine with language E, –write one with E –write one with D if you have a compiler from D to E on some machine It is better than the former approach if D is a high-level language but E is a machine language –write one from G to B with E if we have a compiler from A to G written in E A E B D ? E A D B G E B A E G

19 Porting Porting: construct a compiler between a source and a target language using one host language from another host language A A K A H HA H KA A KA H K A K K

20 Bootstrapping If we have to implement, from scratch, a compiler from a high-level language A to a machine, which is also a host, language, –direct method –bootstrapping A H H A A1A1 H A1A1 A2A2 H A2A2 A3A3 H A3A3 H H

21 Cousins of Compilers Linkers Loaders Interpreters Assemblers