Compiler Construction LECTURE # 1

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

Introduction to Compiler

High-level View of a Compiler

Compiling Techniques

Formal Languages and Compilers Lecture I: Introduction to Compilers

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

CA Compiler Construction

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

Compilers Crash Course

The View from 35,000 Feet

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

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

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

understanding recursive data types, recursive functions to compute over them, and structural induction to prove things about them

Overview. Overview. Programming problems are easier to solve in high-level languages

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Finding People and Information (1) G53CMP: Lecture 1. Aims and Motivation (1) Finding People and Information (2)

Front End. Hwansoo Han

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

CS 314 Principles of Programming Languages

Compiler Design (40-414)

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Introduction & First Content Comp 412

Parsing II Top-down parsing. Comp 412

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

Introduction to Parsing

EECS 6083 Intro to Parsing Context Free Grammars

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

Compilers. Lecture 2 Overview. (original slides by Sam

Overview of a Compiler

COMP 3002: Compiler Construction. Pat Morin School of Computer Science

Compiler Construction 1. Introduction. Oscar Nierstrasz

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

G.PULLAIH COLLEGE OF ENGINEERING & TECHNOLOGY

G53CMP: Lecture 1. Administrative Details 2016 and Introduction to Compiler Construction. Henrik Nilsson. University of Nottingham, UK

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

CS 132 Compiler Construction

Compiler Construction

CS415 Compilers. Lexical Analysis

LECTURE 3. Compiler Phases

CSE 582 Autumn 2002 Exam 11/26/02

CSE 401/M501 Compilers

Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

Compiler Construction 2010 (6 credits)

Lecture 1: Introduction

Compilers for Modern Architectures Course Syllabus, Spring 2015

Lexical Analysis. Introduction

Intermediate Representations

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Overview of a Compiler

CS 352: Compilers: Principles and Practice

Code Optimization. What is code optimization?

CSCI 565 Compiler Design and Implementation Spring 2014

Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

Lexical Analysis. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Philadelphia University Faculty of Information Technology Department of Computer Science --- Semester, 2007/2008. Course Syllabus

Assembly I: Basic Operations. Jo, Heeseung

Formats of Translated Programs

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Compiler Design Overview. Compiler Design 1

CS 432 Fall Mike Lam, Professor. Compilers. Advanced Systems Elective

Compiler Construction D7011E

Overview of a Compiler

Compiler Construction

Compiler Construction 1. Introduction. Oscar Nierstrasz

CSEP 501 Compilers. Overview and Administrivia Hal Perkins Winter /8/ Hal Perkins & UW CSE A-1

Working of the Compilers

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

CS 406/534 Compiler Construction Parsing Part I

Compilers and Interpreters

Name of chapter & details

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

CST-402(T): Language Processors

Compiler I (dt. Übersetzer I) Prof. Dr. Uwe Kastens Winter 2001/2002

2001 bei Prof. Dr. Uwe Kastens

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Compilers. Pierre Geurts

Compilers. Introduction. Yannis Smaragdakis, U. Athens (original slides by Sam

Week 3: Compilers and Interpreters

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CSE 582 Autumn 2002 Exam Sample Solution

Compiler Construction

General Course Information. Catalogue Description. Objectives

CS 432 Fall Mike Lam, Professor. Compilers. Advanced Systems Elective

What do Compilers Produce?

CS , Fall 2001 Exam 1

CS 406/534 Compiler Construction Putting It All Together

Compiler Construction D7011E

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

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

Machine Programming 1: Introduction

Instruction Set Architecture

CISC 360 Instruction Set Architecture

Compiler Construction

CS131: Programming Languages and Compilers. Spring 2017

Chapter 2 A Quick Tour

Transcription:

Compiler Construction AN OVERVIEW LECTURE # 1

The Course Course Code: CS-4141 Course Title: Compiler Construction Instructor: JAWAD AHMAD Email Address: jawadahmad@uoslahore.edu.pk Web Address: http://csandituoslahore.weebly.com/cc.html Term (Semester): FALL 2017 Duration: 15/16 Weeks

Text and Reference Material 1. Compilers: Principles, Techniques, and Tools By Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Contributor Jeffrey D. Ullman, Prentice Hall; 2ndEdition (2006). ISBN-10: 0321486811 2. Modern Compiler Design, By Dick Grune, Henri E. Bal, Ceriel J. H. Jacobs, Koen G. Langendoen, Springer; 2ndEdition. (2012). ISBN- 10: 1461446988 3. Engineering a Compiler, Second Edition by Keith Cooper and Linda Torczon, Morgan Kaufmann; 2ndEdition (February 21, 2011). ISBN-10: 012088478X

Grading Following is the division of marks: Mid-Term Exam 30 Assignments and Quizzes 15 Attendance 05 Final Exams. 50 Marks division might change during the semester

Project Implementation language: subset of java Generated code: Intel x86 assembly Implementation language: C++ Eight programming assignments 5

Why Take this Course Understand compilers and languages Understand the code structure Understand language semantics Understand relation between source code and generated machine code Become a better programmer Theory mathematical models: regular expressions, automata, grammars, graphs algorithms that use these models 6

What are Compilers Translate information from one representation to another Usually information = program 7

Examples Typical Compilers: VC, VC++, GCC, JavaC FORTRAN, Pascal, VB(?) Translators Word to PDF PDF to Postscript 8

In This Course We will study typical compilation: from programs written in high-level languages to low-level object code and machine code 9

Typical Compilation High-level source code Compiler Low-level machine code 10

Source Code int expr( int n ) { int d; d = 4*n*n*(n+1)*(n+1); return d; } 11

Source Code Optimized for human readability Matches human notions of grammar Uses named constructs such as variables and procedures 12

Assembly Code.globl _expr _expr: pushl %ebp movl %esp,%ebp subl $24,%esp movl 8(%ebp),%eax movl %eax,%edx leal 0(,%edx,4),%eax movl %eax,%edx imull 8(%ebp),%edx movl 8(%ebp),%eax incl %eax imull %eax,%edx movl 8(%ebp),%eax incl %eax imull %eax,%edx movl %edx,-4(%ebp) movl -4(%ebp),%edx movl %edx,%eax jmp L2.align 4 L2: leave ret 13

Assembly Code Optimized for hardware Consists of machine instructions Uses registers and unnamed memory locations Much harder to understand by humans 14

How to Translate Correctness: the generated machine code must execute precisely the same computation as the source code 15

How to Translate Is there a unique translation? No! Is there an algorithm for an ideal translation? No! 16

How to Translate Translation is a complex process source language and generated code are very different Need to structure the translation 17

Two-pass Compiler source code Front End IR Back End machine code errors 18

Two-pass Compiler Use an intermediate representation (IR) Front end maps legal source code into IR Back end maps IR into target machine code Admits multiple front ends & multiple passes Front end is O(n) or O(n log n) Back end is NP-Complete (NPC) 19

Front End Recognizes legal (& illegal) programs Report errors in a useful way Produce IR & preliminary storage map 20

The Front-End source code scanner tokens parser IR Modules errors Scanner Parser 21

Scanner source code scanner tokens parser IR errors 22

Scanner Maps character stream into words basic unit of syntax Produces pairs a word and its part of speech 23

Scanner Example x = x + y becomes <id,x> <assign,=> <id,x> <op,+> <id,y> <id,x> token type word 24

Scanner we call the pair <token type, word> a token typical tokens: number, identifier, +, -, new, while, if 25

Parser source code scanner tokens parser IR errors 26

Parser Recognizes context-free syntax and reports errors Guides context-sensitive ( semantic ) analysis Builds IR for source program 27

Context-Free Grammars Context-free syntax is specified with a grammar G=(S,N,T,P) S is the start symbol N is a set of non-terminal symbols T is set of terminal symbols or words P is a set of productions or rewrite rules 28

Context-Free Grammars Grammar for expressions 1. goal expr 2. expr expr op term 3. term 4. term number 5. id 6. op + 7. - 29

The Front End For this CFG S = goal T = { number, id, +, -} N = { goal, expr, term, op} P = { 1, 2, 3, 4, 5, 6, 7} 30

Context-Free Grammars Given a CFG, we can derive sentences by repeated substitution Consider the sentence (expression) x + 2 y 31

Derivation Production Result goal 1 expr 2 expr op term 5 expr op y 7 expr y 2 expr op term y 4 expr op 2 y 6 expr + 2 y 3 term + 2 y 5 x + 2 y 32

The Front End To recognize a valid sentence in some CFG, we reverse this process and build up a parse A parse can be represented by a tree: parse tree or syntax tree 33

Parse Production Result goal 1 expr 2 expr op term 5 expr op y 7 expr y 2 expr op term y 4 expr op 2 y 6 expr + 2 y 3 term + 2 y 5 x + 2 y 34

Syntax Tree x+2-y expr goal expr op term expr op term <id,y> term + <number, 2> <id,x> 35

Abstract Syntax Trees The parse tree contains a lot of unneeded information. Compilers often use an abstract syntax tree (AST). 36

Abstract Syntax Trees + <id,y> <id,x> <number,2> This is much more concise 37

Abstract Syntax Trees + <id,y> <id,x> <number,2> AST summarizes grammatical structure without the details of derivation 38

Abstract Syntax Trees + <id,y> <id,x> <number,2> ASTs are one kind of intermediate representation (IR) 39