Compiler Design (40-414)

Similar documents
PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

CST-402(T): Language Processors

Formal Languages and Compilers Lecture I: Introduction to Compilers

Introduction to Compiler Construction

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Introduction to Compiler Design

CS 4201 Compilers 2014/2015 Handout: Lab 1

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Introduction to Compiler Construction

CS131: Programming Languages and Compilers. Spring 2017

CS 132 Compiler Construction

CS426 Compiler Construction Fall 2006

Compiler Design Overview. Compiler Design 1

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

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

Working of the Compilers

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Compiler Construction


Introduction to Compiler

Introduction. Compilers and Interpreters

An Overview of Compilation

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Alternatives for semantic processing

CA Compiler Construction

Compilers and Code Optimization EDOARDO FUSELLA

CS 352: Compilers: Principles and Practice

COMPILER DESIGN. For COMPUTER SCIENCE

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

Formats of Translated Programs

Undergraduate Compilers in a Day


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

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

What do Compilers Produce?

A Simple Syntax-Directed Translator

COMPILER DESIGN LECTURE NOTES

Compilers and Interpreters

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

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

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

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Introduction to Compiler Construction

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Introduction to Compiler Construction

G.PULLAIH COLLEGE OF ENGINEERING & TECHNOLOGY

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

Programming Language Processor Theory

Introduction to Compiler Construction

COMP3131/9102: Programming Languages and Compilers

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Compiling Regular Expressions COMP360

LECTURE 3. Compiler Phases

Compiler construction

CS Compiler Construction West Virginia fall semester 2014 August 18, 2014 syllabus 1.0

CS606- compiler instruction Solved MCQS From Midterm Papers

Compiler Code Generation COMP360

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

Compilers. Pierre Geurts

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

Pioneering Compiler Design

A programming language requires two major definitions A simple one pass compiler

INF5110 Compiler Construction

Front End. Hwansoo Han

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

The Structure of a Syntax-Directed Compiler

Compiler Construction LECTURE # 1

ECE573 Introduction to Compilers & Translators

Dixita Kagathara Page 1

Compilers Crash Course

The Structure of a Compiler

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

Computer Hardware and System Software Concepts

INTRODUCTION TO COMPILER AND ITS PHASES

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

CS 415 Midterm Exam Spring SOLUTION

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division

CJT^jL rafting Cm ompiler

HOLY ANGEL UNIVERSITY COLLEGE OF INFORMATION AND COMMUNICATIONS TECHNOLOGY COMPILER THEORY COURSE SYLLABUS

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Name of chapter & details

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF CSE COURSE PLAN

COMPILER CONSTRUCTION Seminar 02 TDDB44

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

TDDD55 - Compilers and Interpreters Lesson 3

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

A simple syntax-directed

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

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

SLIDE 2. At the beginning of the lecture, we answer question: On what platform the system will work when discussing this subject?

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila

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

LANGUAGE TRANSLATORS

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

Intermediate Code Generation

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Lexical Analysis. Introduction

Compiler Structure. Lexical. Scanning/ Screening. Analysis. Syntax. Parsing. Analysis. Semantic. Context Analysis. Analysis.

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY

Transcription:

Compiler Design (40-414) Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007 Evaluation: Midterm Exam 35% Final Exam 35% Assignments and Quizzes 10% Project 20% 1

Compiler learning Isn t it an old discipline? Yes, it is a well-established discipline Algorithms, methods and techniques were developed in early stages of computer science There are many compilers around, and many tools to generate them automatically So, why we need to learn it? Although you may never write a full compiler But the techniques we learn is useful in many tasks like: writing an interpreter for a scripting language, validation checking for forms, and so on 2

Terminology Compiler: a program that translates an executable program in a source language (usually high level) into an equivalent executable program in a target language (usually low level) Interpreter: a program that reads an executable program and produces the results of running that program usually, this involves executing the source program in some fashion Our course is mainly about compilers but many of the same issues arise in interpreters 3

A Compiler Source Program Compiler Target Program Errors Input Target Progtam Output 4

An Interpreter Source Program Input Interpreter Output Translates line by line Executes each translated line immediately Execution is slower because translation is repeated But, usually give better error diagnostics than a compiler 5

A Hybrid Compiler Source Program Translator Errors Intermediate Program Input Virtual Machine Output 6

Classifications of Compilers There are different types of Compilers: Single Pass Construction Multiple Pass Absolute (e.g., *.com) Relocateable (e.g., *.exe) Type of produced code 7

The Many Phases of a Compiler Source Program 1 Lexical analyzer 2 Syntax Analyzer Analyses 3 Semantic Analyzer Symbol-table Manager 4 Intermediate Code Generator Error Handler 5 Code Optimizer 6 Code Generator Syntheses 1, 2, 3, 4, 5 : Front-End 6, 7 : Back-End 7 Peephole Optimization Target Program 8

Front-end, Back-end division Source code Front end IR Back end Machine code errors Front end maps legal code into IR Back end maps IR onto target machine Simplifies retargeting Allows multiple front ends 9

Front end Source code Scanner tokens Parser Parse Tree errors Scanner: Maps characters into tokens the basic unit of syntax x = x + y becomes <id, x> = <id, x> + <id, y> Typical tokens: number, id, +, -, *, /, do, end Eliminate white space (tabs, blanks, comments) A key issue is speed so instead of using a tool like LEX it sometimes needed to write your own scanner 10

Front end Source code Scanner tokens Parser Parse Tree errors Parser: Recognize context-free syntax Guide context-sensitive analysis Construct IR Produce meaningful error messages Attempt error correction There are parser generators like YACC which automates much of the work 11

Front end Context free grammars are used to represent programming language syntaxes: <expr> ::= <expr> <op> <term> <term> <term> ::= <number> <id> <op> ::= + - 12

Front end A parser tries to map a program to the syntactic elements defined in the grammar A parse can be represented by a tree called a parse or syntax tree 13

Front end A parse tree can be represented more compactly referred to as Abstract Syntax Tree (AST) AST can be used as IR between front end and back end 14

Back end IR Instruction selection Register Allocation Machine code errors Translate IR into target machine code Choose instructions for each IR operation Decide what to keep in registers at each point 15

Back end IR Code Generation Peephole Optimization Machine code errors Produce compact fast code Use available addressing modes 16

Back end IR Code Generation Peephole Optimization Machine code errors Limited resources Optimal allocation is difficult 17

The Analysis Task For Compilation Three Phases: Lexical Analysis: Left-to-right Scan to Identify Tokens token: sequence of chars having a collective meaning Syntax Analysis: Grouping of Tokens Into Meaningful Collection Semantic Analysis: Checking to ensure Correctness of Components 18

Phase 1. Lexical Analysis Easiest Analysis - Identify tokens which are the basic building blocks For Example: Position := initial + rate * 60 ; All are tokens Blanks, Line breaks, etc. are scanned out 19

identifier position Phase 2. Syntax Analysis or Parsing assignment statement := expression identifier initial For previous example, we would have Parse Tree: expression + expression * expression expression identifier rate number 60 Nodes of tree are constructed using a grammar for the language 20

Phase 3. Semantic Analysis Finds Semantic Errors position := initial + rate * 60 := position + initial * rate inttoreal Syntax Tree Conversion Action 60 One of the Most Important Activity in This Phase: Type Checking - Legality of Operands 21

Supporting Phases/ Activities for Analysis Symbol Table Creation / Maintenance Contains Info (storage, type, scope, args) on Each Meaningful Token, Typically Identifiers Data Structure Created / Initialized During Lexical Analysis Utilized / Updated During Later Analysis & Synthesis Error Handling Detection of Different Errors Which Correspond to All Phases What Happens When an Error Is Found? 22

The Synthesis Task For Compilation Intermediate Code Generation Abstract Machine Version of Code - Independent of Architecture Easy to Produce and Do Final, Machine Dependent Code Generation Code Optimization Find More Efficient Ways to Execute Code Replace Code With More Optimal Statements Final Code Generation Generate Relocatable Machine Dependent Code Peephole Optimization With a Very Limited View Improves Produced Final Code 23

Reviewing the Entire Process position := initial + rate * 60 lexical analyzer id1 := id2 + id3 * 60 Symbol Table position... initial. rate. syntax analyzer := id1 + id2 * id3 60 semantic analyzer := id1 + id2 * id3 inttoreal 60 intermediate code generator Errors 24

Reviewing the Entire Process Symbol Table position... initial. rate. intermediate code generator t1 := inttoreal(60) t2 := id3 * t1 temp3 := id2 + t2 id1 := t3 t1 := id3 * 60.0 id1 := id2 + t1 code optimizer final code generator MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R1, R2 MOVF R1, id1 3 address code Errors 25