Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.

Slides:



Advertisements
Similar presentations
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Advertisements

CPSC Compiler Tutorial 9 Review of Compiler.
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Compilers Book: Crafting a Compiler with C
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
Compiler design Computer Science Rensselaer Polytechnic Lecture 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.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
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.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
CSC 338: Compiler design and implementation
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.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
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.
Homework #1 J. H. Wang Oct. 5, 2015.
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.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 1, 08/28/03 Prof. Roy Levow.
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.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM John.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
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.
Dr. Mohamed Ramadan Saady 314ALL CH1.1 Chapter 1: Introduction to Compiling.
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.
CSC 8505 Compiler Construction
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
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 TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Chapter 1. Introduction.
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
Programming Language Hierarchy, Phases of a Java Program
Compiler Construction (CS-636)
Chapter 1 Introduction.
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Lecture 2: General Structure of a Compiler
CSE401 Introduction to Compiler Construction
COP4020 Programming Languages
Introduction to Compiler Construction
Chapter 10: Compilers and Language Translation
Programming language translators
Introduction to Compiler Construction
Presentation transcript:

Textbook: Crafting a Compiler by Fischer, Cytron, and LeBlanc Pearson 2010 ISBN: 978-0-13-801785-9

Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000

Chapter 1 Introduction

Introduction A compiler is a program that accepts, as input, a program text in a certain programming language (source language), and produces, as output, a program text in an assembly language (target language), which will later be assembled by the assembler into machine language. This process is called compilation. (See Fig. 1.1).

Introduction (Cont.)

Front-end and Back-end Front-end performs the analysis of the source language code to build the internal representation, called intermediate representation (IR). Back-end performs the target language synthesis to generate the target code (assembly code).

Machine Code Generated by Compilers Pure machine code Compiler may generate code for a particular machine’s instruction set without assuming the existence of any operating system or library routines. Pure machine code is used in compilers for system implementation languages, which are for implementing operating systems or embedded applications.

Machine Code Generated by Compilers (Cont.) Augmented machine code Compilers generate code for a machine architecture that is augmented with: 1) operating system routines and 2) runtime language support routines.

Machine Code Generated by Compilers (Cont.) Virtual machine code Compilers generate virtual machine code that is composed entirely of virtual machine instructions. That code can run on any architecture for which a VM interpreter is available. For example, the VM for Java, Java virtual machine (JVM), has a JVM interpreter. Portability is achieved by writing just one virtual machine (VM) interpreter for all the target architectures.

Bootstrapping When the source language (e.g., L) is also the implementation language (e.g., L) and the source text to be compiled is actually a new compiler of the compiler itself, the process is called bootstrapping (see Fig. 1.2)

Compiler vs. Interpreter Compiler has compilation and execution phases: 1) The compilation phase generates target program from source program. 2) The execution phase executes the target program. Interpreter directly interprets (executes) the source program that reads inputs and writes outputs (Fig. 1.3).

Compiler versus Interpreter (Cont.)

Organization of a Compiler Compilers generally perform the following tasks: 1) Analysis of source program such as scanning and parsing. 2) Synthesis of target program such as code generation.

Organization of a Compiler (Cont.) Scanner The scanner begins the analysis of the source program by reading the input text (character by character) and grouping individual characters into tokens such as identifiers and integers. Parser The parser is based on a formal syntax specification such as context free grammar (CFG). The parser usually builds an abstract syntax tree (AST) as a concise representation of program structure.

Organization of a Compiler (Cont.) Type checker (semantic analyzer) The type checker checks and decorates the semantics of each AST code. It checks type correctness, reachability and termination, and exception handling.

Organization of a Compiler (Cont.) Translator If an AST node is semantically correct, it can be translated into IR code (such as byte code in Java) that correctly implements the meaning of the program.

Organization of a Compiler (Cont.) Optimizer improves IR code’s performance. The next example optimizes a matrix multiplication program A=B*C (Fig. 14.1) in 2 steps: 1) Fig.14.2 in-lines 2 methods (* and =) to avoid the overhead of method call. 2) Fig.14-3 fuses the outer two loops (for i , for j) and eliminates the “Result” matrix.

Organization of a Compiler (Cont.) Code generator Mapping the IR code (such as Java byte code) or abstract syntax tree into target code (assembly code).

Organization of a Compiler (Cont.) Source Program Scannerr Tokens Parser AST Type Checker Decorated AST Translator Intermediate Representation Symbol Tables Optimizer Intermediate Representation Code Generator Target Code Revised Figure 1.4: A syntax-directed compiler. AST denotes the Abstract Syntax Tree.

Homework 4. One of the most important tasks of a compiler is detecting incorrect or illegal program components. Some errors are detected at compiler-time. Thus the statement a=b/0; would be marked as containing an illegal divide operation. Other errors are detected at run-time. Thus give: c=0; a=b/c; an illegal division would be detected as the program executes. One difference between compiler-time and run-time errors is that run- time errors are only detected if the illegal code is actually executed. Compiler-time errors make an entire program illegal, even if the erroneous code is rarely or never reached. Which approach do you recommend? Should a program with compiler-time errors be allowed to execute, with termination only when illegal code is executed? Are there any advantages to making a whole program illegal if some illegal component of it might be executed?

No. 4 Solution 4. 1) Compile-time error is a better approach. 2) A program with compiler-time errors SHOULD NOT be allowed to execute. 3) The advantage is that it ensures all the source codes are syntactically correct.

Homework (Cont.) 8. Most programming languages, such as C and C++, are compiled directly into the machine language of a “real” microprocessor (for example, an Inter x86 or Sparc). Java takes a different approach. It is commonly compiled into the machine language of the JVM. The JVM is not implemented in its own microprocessor, but is instead interpreted on some existing processor. This allows Java to be run on a wide variety of machines, thereby making it highly platform independent. Explain why building an interpreter for a virtual machine like the JVM is easier and faster than building a complete Java compiler. What are the disadvantages of this virtual machine approach?

No. 8 Solution 8. Building an interpreter for a virtual machine is easier than building a complete Java compiler is that there are fewer phases in developing interpreters. The disadvantages of this virtual machine approach is that the source code must be compiled to byte code, and then a java interpreter can execute it. This takes more time.

Homework (Cont.) 11. C is sometimes called the universal assembly language in light of its ability to be very efficiently implemented on a wide variety of computer architecture. In light of this characterization, some compiler writers have chosen to generate C code as their output instead of a particular machine language. What are the advantages to this approach to compilation? Are there any disadvantages?

No. 11 Solution 11. Generating C code is more portable than generating an assembly code for a particular machine language. However, the source code will be translated into C code, and then to assembly code. This is time-consuming.