Course supervisor: Lubna Siddiqui

Slides:



Advertisements
Similar presentations
CS 31003: Compilers Introduction to Phases of Compiler.
Advertisements

Compilers and Language Translation
Compiler Construction by Muhammad Bilal Zafar (AP)
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
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.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
Invitation to Computer Science 5th Edition
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.
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.
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.
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.
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.
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.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Compiler Construction Composed By, Muhammad Bilal Qureshi.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
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.
CSC 4181 Compiler Construction
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
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)
Lecture 12 Intermediate Code Generation Translating Expressions
COMP 3002: Compiler Construction Pat Morin School of Computer Science.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
Advanced Computer Systems
Component 1.6.
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
Chapter 1 Introduction.
A Simple Syntax-Directed Translator
Constructing Precedence Table
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
Introduction CI612 Compiler Design CI612 Compiler Design.
Compilers B V Sai Aravind (11CS10008).
CMPE 152: Compiler Design August 21/23 Lab
Compiler design.
CS416 Compiler Design lec00-outline February 23, 2019
Introduction to Compiler Construction
CISC 7120X Programming Languages and Compilers
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction Chapter : Introduction.
Compiler Structures 1. Overview Objective
Introduction to Compiler Construction
Presentation transcript:

Course supervisor: Lubna Siddiqui COMPILER CONSTRUCTION FALL 2014 BSCS 6 Course supervisor: Lubna Siddiqui

Course Objectives Introduce and Describe the Fundamentals of Compilation Techniques Assist you in Writing your Own Compiler (or a part of it) Overview & Basic Concepts Lexical Analysis Syntax Analysis / Syntax-Directed Translation Semantic Analysis Intermediate Code Generation.

Course Textbooks Compilers – Principles, Techniques and Tools (Second Edition) Authors: Alfred V. Aho Monica S. Lam Ravi Sethi Jeffrey D. Ullman

Grade Distribution Quizzes: 10% Assignments: 20% Mid-Term : 20% Final: 50%

What is a Compiler? Programming languages: Notations for describing computations to people and to machines E.g. Java, C#, C++, Perl, Prolog etc. A program written in any language must be translated into a form that is understood by the computer This form is typically known as the Machine Language (ML), Machine Code, or Object Code Consists of streams of 0’s and 1’s The program that carries out the translation activity is called the compiler.

Basic Concept The language to be translated: Source language Input code is called the Source code The language produced: Target language Output code is called Target code A compiler is a program that translates source code written in one language into the target code of another language. Java Machine Language

Compiler vs. Interpreter Interpreter: A program that doesn’t produce the target executable. It can do three things: In a line-by-line fashion, it directly executes the source code by using the given inputs, and producing the desired outputs May translate source code into some intermediate language and then execute that immediately, e.g., Perl, Python May also execute previously stored pre-compiled code, made by a compiler that is part of the interpreter system, e.g., Java and Pascal

Structure of a Compiler Character Stream Machine-Independent Code Optimizer Lexical Analyzer Token Stream Intermediate Representation Syntax Analyzer Machine Code Generator Syntax Tree Target Machine Code Semantic Analyzer Machine-Dependent Code Optimizer Semantically-correct Syntax Tree Optimized Target Machine Code Intermediate Code Generator Intermediate Representation SYMBOL TABLE

Compilation – Analysis Part Phases involved: Lexical Analysis Syntax Analysis Semantic Analysis Determines the operations implied by the source program which are recorded in a tree structure called the Syntax Tree Breaks up the source code into constituent pieces, while storing information in the symbol table Imposes a grammatical structure on these pieces.

Compilation – Synthesis Part Phases involved: Intermediate Code Generation Intermediate Code Optimization Machine Code Generator Machine Dependent Code Optimization Constructs the target code from the syntax tree, and from the information in the symbol table Optimization is another important activity Both the intermediate and machine codes are optimized in order to achieve an efficient translation, with the least possible use of computing resources.

Front End & Back End The analysis part often called front end. The synthesis part is called back end.

Lexical Analysis Read the character stream of the source code and break it up into meaningful sequences called lexemes (a.k.a Scanning) Each lexeme is represented as a token <token_name, attribute value> Single, atomic unit of the language E.g., force = mass * 60 Lexeme force: token <id,1> Lexeme =: token <=> Lexeme mass: token <id,2> Lexeme *: token <*> Lexeme 60:token <60>. Output: <id,1>=<id,2>*60 1 force .. 2 mass SYMBOL TABLE

Syntax Analysis Syntax analysis: Parsing the token stream to identify the grammatical structure of the stream (a.k.a parsing) Typically builds a parse tree, which replaces the linear sequence of tokens with a tree structure The tree is built according to the rules of a formal grammar which define the language's syntax It is analyzed, augmented, and transformed by later phases of compilation. Syntax tree for <id,1>=<id,2>*60 = * <id,1> <id,2> 60

More Examples

Semantic Analysis Semantic analysis: Adding semantic information to the parse tree  Performs semantic checks, e.g., type checking (checking for type errors),  object binding (associating variable and function references with their definitions), rejecting incorrect programs or issuing warnings Suppose force and mass are floats and acceleration is integer: type casting required = * <id,1> Semantically correct syntax tree for <id,1>=<id,2>*60 <id,2> inttofloat 60

Example: English Language Ahmad told us that Faisal was going to his place Who does his refer to? In programming language, such ambiguities are avoided { int Ahmad = 10; int Ahmad = 15; System.out.println(Ahmad); } 15 is printed

Intermediate Code Generator Intermediate Code Generation: After verifying the semantics of the source code, we convert it into a machine-like intermediate representation i.e., like a program for a machine Typically, an assembly language-like form is used, because it is easy to generate and debug Three address code: 3 operands/instruction Suppose t1 and t2 are registers t1=inttofloat(60) t2=id2*t1 id1=t2. = * <id,1> <id,2> inttofloat 60

Machine-Independent Optimizer Machine-Independent Optimizer: Optimize the intermediate code, so that a better target program can be generated Faster, smaller code that consumes less computation power E.g., X = Y * 0 is the same as X = 0 Example: t1=inttofloat(60) t2=id2*t1 t1=id2*60.0 id1=t2 id1=t1 Eliminating the one time use of register t2, and converting 60 to a float

Machine Code Generation Machine Code Generator: Converts the optimized intermediate code into the target code (typically the Machine Code) This is done through the Assembly Language In case of Machine Code, registers (memory locations) are selected for each variable Then, intermediate instructions are translated into sequences of machine code instructions that perform the same task Example: id1=id2*60.0 LDF R2,id2 MULF R1, R2, #60 Transfer id2 into R2, multiply and assign to R1

Machine-Dependent Optimizer Machine-Dependent Optimizer: In this phase, after the Machine Code has been generated, it is optimized further (if needed) This completes the compilation process Important: Optimization is an optional activity in compilation One or both of the optimization phases might be missing.