COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Similar documents
PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Formal Languages and Compilers Lecture I: Introduction to Compilers

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

CS 4201 Compilers 2014/2015 Handout: Lab 1


Introduction to Compiler Design

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

Compiler Design (40-414)

Introduction. Compilers and Interpreters

CST-402(T): Language Processors

COMPILER DESIGN. For COMPUTER SCIENCE

Compiler Design. Subject Code: 6CS63/06IS662. Part A UNIT 1. Chapter Introduction. 1.1 Language Processors

COMPILER DESIGN LECTURE NOTES

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

Principles of Programming Languages. Lecture Outline

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Compiling Regular Expressions COMP360

Introduction to Compiler Construction

Introduction to Compiler Construction

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

Introduction to Compiler Construction


Concepts of Programming Languages

CS131: Programming Languages and Compilers. Spring 2017

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Compilers. Prerequisites

Introduction to Compiler Construction

Overview of Compiler. A. Introduction

SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT I - LEXICAL ANALYSIS PART - A

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Lexical Scanning COMP360

Why are there so many programming languages?

Life Cycle of Source Program - Compiler Design

COSE312: Compilers. Lecture 1 Overview of Compilers

Advanced Programming & C++ Language

Introduction to Compiler Construction

Chapter 1. Preliminaries

Design & Implementation Overview

Chapter 1 Preliminaries

Compilation I. Hwansoo Han

LANGUAGE TRANSLATORS

CS426 Compiler Construction Fall 2006

Chapter 1. Preview. Reason for Studying OPL. Language Evaluation Criteria. Programming Domains

Pioneering Compiler Design

Compilers. Pierre Geurts

Programming Languages

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

Introduction to Compilers

Compiler Design Overview. Compiler Design 1

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Compiler, Assembler, and Linker

Dixita Kagathara Page 1

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

CS101 Introduction to Programming Languages and Compilers

UNIT I- LEXICAL ANALYSIS. 1.Interpreter: It is one of the translators that translate high level language to low level language.

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

CMPT 379 Compilers. Anoop Sarkar.

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

LECTURE 1. Overview and History

Undergraduate Compilers in a Day

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Chapter 1. Preliminaries

Introduction to Compiler

Software II: Principles of Programming Languages

Compiler Design. Dr. Chengwei Lei CEECS California State University, Bakersfield

Working of the Compilers

CSCI 334: Principles of Programming Languages. Lecture 2: Lisp Wrapup & Fundamentals. Higher-Order Functions. Activity

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

1. INTRODUCTION TO LANGUAGE PROCESSING The Language Processing System can be represented as shown figure below.

Advanced Topics in MNIT. Lecture 1 (27 Aug 2015) CADSL

Compilers and Interpreters

Crafting a Compiler with C (II) Compiler V. S. Interpreter

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Chapter 1: Why Program? Main Hardware Component Categories 8/23/2014. Main Hardware Component Categories: Why Program?

names names identifiers variables subroutines constants

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

Lecture 09. Ada to Software Engineering. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

CS 415 Midterm Exam Spring 2002

Compiler Design IIIT Kalyani, West Bengal 1. Introduction. Goutam Biswas. Lect 1

The role of semantic analysis in a compiler

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

LECTURE 3. Compiler Phases

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Introduction to Programming Languages. CSE 307 Principles of Programming Languages Stony Brook University

Compiler Theory Introduction and Course Outline Sandro Spina Department of Computer Science

Compiling Techniques

Chapter 1: Introduction to Computers and Programming

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CS 3304 Comparative Languages. Lecture 1: Introduction

Compiler Code Generation COMP360

G Compiler Construction Lecture 4: Lexical Analysis. Mohamed Zahran (aka Z)

Transcription:

COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou

Administrative! Next time reading assignment [ALSU07] Chapters 1,2 [ALSU07] Sections 1.1-1.5 (cover in class) [ALSU07] Section 1.6 (read on your own) Programming language basics that you should be familiar Email me questions (if any) before next class [ALSU07] We will cover selected parts from all sections Copyright (c) 2012 Ioanna Dionysiou 2

Lecture Outline! Chapter 1 - Introduction Definition Structure of Compiler Compilation parts Analysis, Synthesis Phases of a compiler An Example: Translation of a statement Evolution of Programming Languages Building a Compiler and Applications of Compiler Technology Copyright (c) 2012 Ioanna Dionysiou 3

Compiler: Simple Definition! A compiler is a program that reads a program written in one language and translates it into an equivalent program in another language source program Compiler target program error messages input Target Program output Copyright (c) 2012 Ioanna Dionysiou 4

The context of a compiler! In addition to a compiler, several other programs may be required to create an executable target program Preprocessor Assembler Linker/Loader These are the cousins of the compilers! Copyright (c) 2012 Ioanna Dionysiou 5

Cousins of the compiler! Preprocessors Macro processing File inclusion! Assemblers Some compilers produce assembly code that is passed to an assembler for further processing The assembler then produces machine code! Linker/loader Applies to large programs in multiple files Linker resolves the address (location) of variables Loader combines all the pieces into an executable Copyright (c) 2012 Ioanna Dionysiou 6

Lecture Outline! Chapter 1 - Introduction Definition Structure of Compiler Compilation parts Analysis, Synthesis Phases of a compiler An Example: Translation of a statement Evolution of Programming Languages Building a Compiler and Applications of Compiler Technology Copyright (c) 2012 Ioanna Dionysiou 7

Compilation Process! There are two parts to compilation Analysis Break up the source program into pieces Create an intermediate representation of the source program Synthesis Construct the desired target program from the intermediate representation source program Compiler target program Copyright (c) 2012 Ioanna Dionysiou 8

Analysis of Source Program! It consists of three phases (front-end) Lexical analysis Linear analysis, scanning Stream of characters are read and grouped into tokens (sequence of characters with a collective meaning) Syntax analysis Hierarchical analysis, parsing Tokens are grouped hierarchically into nested collections with collective meaning Semantic analysis Check to ensure that the components of a program have a meaning Copyright (c) 2012 Ioanna Dionysiou 9

Synthesis of Target Program! It consists of four phases (back-end) Intermediate Code Generator Explicit low-level or machine-like intermediate representation Machine-Independent Code Optimizer Improve the intermediate code (faster, shorter code, etc) Code Generator Translate intermediate instructions into sequence of machine instructions Machine-Dependent Code Optimizer Improve the generated code Copyright (c) 2012 Ioanna Dionysiou 10

Phases of a compiler Source Program Lexical analyzer Syntax analyzer Symbol-table manager Semantic analyzer Intermediate code generator Error Handler MI Code optimizer Code generator MD Code optimizer Target Program Copyright (c) 2012 Ioanna Dionysiou 11

The phases of a compiler Source Program Lexical analyzer Syntax analyzer Symbol-table manager Semantic analyzer Intermediate code generator Error Handler MI Code optimizer! Lexical analyzer Stream of characters are read in and grouped into tokens Code generator MD Code optimizer Target Program Copyright (c) 2012 Ioanna Dionysiou 12

The phases of a compiler Source Program Lexical analyzer Syntax analyzer Symbol-table manager Semantic analyzer Intermediate code generator Error Handler MI Code optimizer! Syntax analyzer Group tokens into grammatical phrases Code generator MD Code optimizer Target Program Copyright (c) 2012 Ioanna Dionysiou 13

The phases of a compiler Source Program Lexical analyzer Syntax analyzer Symbol-table manager Semantic analyzer Intermediate code generator Error Handler MI Code optimizer! Semantic analyzer Checks the source program for semantic errors and gathers type Code generator MD Code optimizer checking information Target Program E.g.use a real number as index to array elements Copyright (c) 2012 Ioanna Dionysiou 14

The phases of a compiler Source Program Lexical analyzer Syntax analyzer Symbol-table manager Semantic analyzer Intermediate code generator Error Handler MI Code optimizer! Intermediate Code Generator Generate explicit intermediate representation of the source program E.g. Three-address code Code generator MD Code optimizer Target Program Copyright (c) 2012 Ioanna Dionysiou 15

The phases of a compiler Source Program Lexical analyzer Syntax analyzer Symbol-table manager Semantic analyzer Intermediate code generator Error Handler MI Code optimizer! Machine-Independent Code Optimizer Attempts to improve the intermediate code, faster performance results Code generator MD Code optimizer Target Program Copyright (c) 2012 Ioanna Dionysiou 16

The phases of a compiler Source Program Lexical analyzer Syntax analyzer Symbol-table manager Semantic analyzer Intermediate code generator Error Handler! Code Generator Consists normally of relocatable machine code or assembly code Machine instructions, assignment of variables to registers MI Code optimizer Code generator MD Code optimizer Target Program Copyright (c) 2012 Ioanna Dionysiou 17

The phases of a compiler Source Program Lexical analyzer Syntax analyzer Symbol-table manager Semantic analyzer Intermediate code generator Error Handler MI Code optimizer! Machine-Dependent Code Optimizer Attempts to improve the generated code, faster performance results Code generator MD Code optimizer Target Program Copyright (c) 2012 Ioanna Dionysiou 18

Lecture Outline! Chapter 1 - Introduction Definition Structure of Compiler Compilation parts Analysis, Synthesis Phases of a compiler An Example: Translation of a statement Evolution of Programming Languages Building a Compiler and Applications of Compiler Technology Copyright (c) 2012 Ioanna Dionysiou 19

Example: Translation of a statement SYMBOL TABLE position initial rate position = initial + rate * 60 Lexical analyzer <id,1> <=> <id,2> <+> <id,3> <*> <60> Copyright (c) 2012 Ioanna Dionysiou 20

Example: Translation of a statement <id,1> <=> <id,2> <+> <id,3> <*> <60> Syntax analyzer <id,1> = + <id,2> <id,3> * 60 Copyright (c) 2012 Ioanna Dionysiou 21

Example: Translation of a statement = <id,1> + <id,2> * <id,3> 60 Semantic analyzer SYMBOL TABLE position FLOAT initial FLOAT rate FLOAT <id,1> = + <id,2> <id,3> * inttofloat 60 Copyright (c) 2012 Ioanna Dionysiou 22

Example: Translation of a statement <id,1> = + <id,2> <id,3> * inttofloat 60 Intermediate Code Generator temp1 = inttofloat(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Copyright (c) 2012 Ioanna Dionysiou 23

Example: Translation of a statement temp1 = inttoreal(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 Code Optimizer temp1 = id3 * 60.0 id1 = id2 + temp1 Copyright (c) 2012 Ioanna Dionysiou 24

Example: Translation of a statement temp1 = id3 * 60.0 id1 = id2 + temp1 Code Generator MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1 Copyright (c) 2012 Ioanna Dionysiou 25

Lecture Outline! Chapter 1 - Introduction Definition Structure of Compiler Compilation parts Analysis, Synthesis Phases of a compiler An Example: Translation of a statement Evolution of Programming Languages Building a Compiler and Applications of Compiler Technology Copyright (c) 2012 Ioanna Dionysiou 26

First Computers! Appeared in 1940 s! Programmed in machine language 0 s and 1 s! Low-level operations Move data, add contents of registers! Programming problems Slow, tedious, error prone Very hard to understand and modify Copyright (c) 2012 Ioanna Dionysiou 27

Move to Higher-level languages! Early 50 s Assembly languages! Mid-late 50 s Fortran, Cobol, Lisp! Today Thousands of programming languages Various classifications Generation Imperative vs. declarative Object oriented Scripting Copyright (c) 2012 Ioanna Dionysiou 28

More on Higher-Level Languages! Generation 1st : machine languages 2nd : assembly languages 3rd : higher-level languages (Cobol, C, C++, Java) 4th : application-specific languages (SQL) 5th : logic-based languages (Prolog)! Imperative vs. Declarative How a computation is done: C, C++, Java What computation is to be done: Prolog! Object-Oriented Java, C#! Scripting JavaScript, PHP Copyright (c) 2012 Ioanna Dionysiou 29

Lecture Outline! Chapter 1 - Introduction Definition Structure of Compiler Compilation parts Analysis, Synthesis Phases of a compiler An Example: Translation of a statement Evolution of Programming Languages Building a Compiler and Applications of Compiler Technology Copyright (c) 2012 Ioanna Dionysiou 30

Building a Compiler! Compiler development is challenging A compiler must accept ALL source programs that conform to the language specification Set of source programs -> Infinite! Millions of lines of code An transformation performed by the compiler must preserve the meaning of the source program Copyright (c) 2012 Ioanna Dionysiou 31

Modeling in Compiler Design! Study of compilers How do we design the right mathematical models? How do we choose the right algorithms?! Fundamental models Finite-state machines Regular expressions Context-free grammars Trees Copyright (c) 2012 Ioanna Dionysiou 32

Compiler-Construction Tools! Luckily, there are tools available Parser generators Scanner generators Syntax-directed translation engines Code-generator generators Data-flow analysis engines Compiler-construction toolkits Copyright (c) 2012 Ioanna Dionysiou 33

Science of Code Optimization! Optimization Attempts that a compiler makes to produce more efficient code! Compiler Optimizations must meet the following design objectives Optimization must be correct Optimization must improve the performance of many programs Compilation time must be kept reasonable Engineering effort required must be manageable Copyright (c) 2012 Ioanna Dionysiou 34

More on Optimization! Correct optimization Generate fast code that is correct!!!! Improve performance Normally means reducing speed of program execution Minimizing size of generated code (embedded applications) Minimizing power consumption (mobile devices)! Short compilation time To support rapid development and debugging cycle! Keep it simple Compiler is a complex system - Keep system simple so that engineering and maintenance costs of the compiler are manageable Prioritize optimizations, implement those that lead to greatest benefits Copyright (c) 2012 Ioanna Dionysiou 35

Compiler Technology Apps.! Optimizations for Computer Architectures Parallelism at the instruction level and processor level Compiler techniques are developed to generate code automatically for such machines from sequential programs Copyright (c) 2012 Ioanna Dionysiou 36