1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.

Slides:



Advertisements
Similar presentations
Introduction to Compiler Construction
Advertisements

Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Chapter 1: Introduction to Compiling
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Invitation to Computer Science 5th Edition
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
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.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
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.
RIT 08/11/47Chapter 11 Chapter 1: Introduction to Compiling Dr. Winai Wichaipanitch Rajamangala Institute of Technology Klong 6 Thanyaburi Pathumthani.
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
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.
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,
Introduction to Compiling
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
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.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
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.
CH1.1 CSE244 Chapter 1: Introduction to Compiling Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut.
Dr. Mohamed Ramadan Saady 314ALL CH1.1 Chapter 1: Introduction to Compiling.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
©SoftMoore ConsultingSlide 1 Structure of Compilers.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
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.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
CS 3304 Comparative Languages
Introduction.
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Compiler Construction
CS416 Compiler Design lec00-outline September 19, 2018
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Lecture 2: General Structure of a Compiler
Introduction CI612 Compiler Design CI612 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 Structures 1. Overview Objective
Introduction to Compiler Construction
Presentation transcript:

1 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%

2 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

3Terminology  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

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

5 An Interpreter Interpreter Input 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 Source Program

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

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

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

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

10 Front end  Scanner:  Maps characters into tokens – the basic unit of syntax  x = x + y becomes = +  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 Scanner Source code Parse Tree errors tokens Parser

11 Front end  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 Scanner Source code Parse Tree errors tokens Parser

12 Front end  Context free grammars are used to represent programming language syntaxes: ::= | ::= | ::= + | - ::= + | -

13 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

14 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

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

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

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

18  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 The Analysis Task For Compilation

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

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

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

22 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?

23 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

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

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