Compiler Construction

Similar documents
EDAN65: Compilers Introduc8on and Overview. Görel Hedin Revised:

Compiling Regular Expressions COMP360

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

Undergraduate Compilers in a Day

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

CMPE 152 Compiler Design

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Compiling Techniques

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

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

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

General Course Information. Catalogue Description. Objectives

CA Compiler Construction

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

Compilers. Computer Science 431

CMPE 152 Compiler Design

Chapter 2 A Quick Tour

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

Course introduction. Advanced Compiler Construction Michel Schinz

CMPE 152 Compiler Design

Compiler Design (40-414)

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

Compilers and Code Optimization EDOARDO FUSELLA

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 and Interpreters

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Introduction to Compiler

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

CS/SE 153 Concepts of Compiler Design

CS/SE 153 Concepts of Compiler Design

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

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

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

CPSC 411: Introduction to Compiler Construction

LECTURE 3. Compiler Phases

Introduction to Compiler Design

Compilers Crash Course

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

EDA180: Compiler Construc6on. More Top- Down Parsing Abstract Syntax Trees Görel Hedin Revised:

CS 4201 Compilers 2014/2015 Handout: Lab 1

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

Compilers for Modern Architectures Course Syllabus, Spring 2015

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Formal Languages and Compilers Lecture I: Introduction to Compilers

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

LL(k) Compiler Construction. Choice points in EBNF grammar. Left recursive grammar

LL(k) Compiler Construction. Top-down Parsing. LL(1) parsing engine. LL engine ID, $ S 0 E 1 T 2 3

List of Figures. About the Authors. Acknowledgments

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

Overview of the Class

Compiling Techniques

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

JastAdd an aspect-oriented compiler construction system

15-411/ Compiler Design

Time : 1 Hour Max Marks : 30

Lexical Scanning COMP360

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

Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers

Compiler Construction 1. Introduction. Oscar Nierstrasz

Compilers. Pierre Geurts

ECE573 Introduction to Compilers & Translators

Programming Assignment 2 LALR Parsing and Building ASTs

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

INF5110 Compiler Construction

Compiler Construction Lecture 1: Introduction Winter Semester 2018/19 Thomas Noll Software Modeling and Verification Group RWTH Aachen University

Overview of the Class

Compiler Design. Lecture 1

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

COMPILER DESIGN. For COMPUTER SCIENCE

Compiler Construction

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

Introduction to Compiler Construction

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

CSE 582 Autumn 2002 Exam 11/26/02


.jj file with actions

CS453 Compiler Construction

CSC 111 Introduction to Computer Science (Section C)

Chapter 4. Abstract Syntax

Principles of Compiler Construction ( )

CSCI 334: Principles of Programming Languages. Lecture 2: Lisp Wrapup & Fundamentals. Higher-Order Functions. Activity

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

Introduction to Compiler Construction

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

CS426 Compiler Construction Fall 2006

Programming Assignment 2 LALR Parsing and Building ASTs

CS 415 Midterm Exam Spring 2002

Compiler construction. Course info. Today. Lecture 1: Introduction and project overview. Compiler Construction Why learn to write a compiler?

GUJARAT TECHNOLOGICAL UNIVERSITY

Theory and Compiling COMP360

Welcome to CSE131b: Compiler Construction

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

LL parsing Nullable, FIRST, and FOLLOW

Compiler construction

A Simple Syntax-Directed Translator

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

Compiler Construction Lecture 1: Introduction Summer Semester 2017 Thomas Noll Software Modeling and Verification Group RWTH Aachen University

CSCI 565 Compiler Design and Implementation Spring 2014

Transcription:

Compiler Construction Introduction and overview Görel Hedin Reviderad 2013-01-22 2013 Compiler Construction 2013 F01-1

Agenda Course registration, structure, etc. Course overview Compiler Construction 2013 F01-2

Course registration You need to Confirm registration by signing the Registration Form To unregister Email me Compiler Construction 2013 F01-3

Prerequisites Object-oriented programming and Java Algorithms and Data structures (recursion, trees, lists, hash tables,... ) Compiler Construction 2013 F01-4

Course information Web page: http://cs.lth.se/eda180 will be updated during the course Literature Course material, will be made available on the web site. Lectures, seminars, labs, project, articles Not handed out - print yourself. Textbook A.W. Appel: Modern Compiler Implementation in Java, 2nd Edition, Cambridge University Press, 2002, ISBN: 0-521-82060-X. Available as an e-book via http://www.lub.lu.se/en/search/lubsearch.html Compiler Construction 2013 F01-5

Course structure 14 lectures Tuesday 15-17, MA:020 Wednesday 10-12, E:A 5 seminars (give extra points on exam) Thursday 10-12, MA:026 Start this week 6 computer assignments / lab sessions (mandatory) Thursday 13-15 or Friday 10-12 (E:Hacke) Sign up by Thursday Jan 24 (see course web) Start next week Must be completed before exam and course project Written exam, March 13 Course project, VT2 Compiler Construction 2013 F01-6

People helping with the course Lectures: Görel Hedin Emma Söderberg Guest lectures: Roger Henriksson (automatic memory management) Jonas Skeppstedt (code optimization) Seminars Emma Söderberg Programming assignments and lab sessions Niklas Fors Jesper Öqvist Compiler Construction 2013 F01-7

Seminars Active participation gives extra points at the exam Before the seminar Try to solve the problems at home. Write down your solutions, bring them with you, be prepared to present them at the seminar. At the seminar Mark the problems you are willing to present solutions for. The seminar leader selects some students to present their solutions. Discussion of the solutions. At the written exam (Note! Only for exams this year) Your markings will give you a maximum of 10% extra points at the written exam. Compiler Construction 2013 F01-8

Programming assignments / Lab sessions Work in pairs Use the lecture break to form pairs! Make the preparations before each lab session. If you complete the assignment in advance, you must anyway go to the lab session to get it approved. Compiler Construction 2013 F01-9

Examination Exams take place Wednesday, March 13, 8-13, Sparta:D Friday, August 30, 8-13, Victoriastadion 1A (1 week advance registration required) Prerequisites Completed programming assignments Compiler Construction 2013 F01-10

Project Standard project Design of a small procedural language Implementation of a compiler from source text to Intel assembly code Work in pairs Deadlines Intermediate deadlines given later Final deadline for completed and approved project: May 3rd Compiler Construction 2013 F01-11

Project outcome source code compiler assembly code Compiler Construction 2013 F01-12

Project outcome source code csum = a + b + 1; compiler assembly code movl a, %eax addl b, %eax addl $1, %eax movl %eax, csum Compiler Construction 2013 F01-12

What happens after compilation? source code assembler object code library object code compiler assembly code linker machine code loader machine memory Compiler Construction 2013 F01-13

A closer look at the compiler source code lexical analysis intermediate code generation syntactic analysis optimization semantic analysis machine code generation machine code Compiler Construction 2013 F01-14

Intermediate representations source code lexical analysis tokens intermediate code generation intermediate code syntactic analysis attributed AST optimization AST semantic analysis analysis intermediate code machine code generation synthesis machine code Compiler Construction 2013 F01-15

Front and back end source code lexical analysis intermediate code generation syntactic analysis optimization semantic analysis front end machine code generation back end machine code Compiler Construction 2013 F01-16

Intermediate code FrontEnd L intermediate code BackEnd Intel Compiler Construction 2013 F01-17

Several front and back ends FrontEnd L FrontEnd C FrontEnd PL0 intermediate code BackEnd Intel BackEnd MIPS Interpreter Compiler Construction 2013 F01-18

Why? It is more rational to implement m front ends + n back ends than m n compilers. Many optimizations are best performed on intermediate code. It may be easier to debug the front end using an interpreter than a target machine. Compiler Construction 2013 F01-19

Compilation and Interpretation A compiler translates a high level program to low level/machine code. An interpreter executes a high/low level program by calling one procedure for each program construct. An interpreter may use a JIT ( just in time ) compiler to compile all or parts of the the program into machine code during execution. Compiler Construction 2013 F01-20

Program representations source code lexical analysis tokens intermediate code generation intermediate code syntactic analysis attributed AST optimization AST semantic analysis intermediate code machine code generation machine code Compiler Construction 2013 F01-21

Lexical analysis (scanning) Source text while (k<=n) { sum=sum+k; k=k+1; } Tokens A token is a symbolic name, sometimes with an attribute. A lexeme is a string corresponding to a token. Compiler Construction 2013 F01-22

Lexical analysis (scanning) Source text Tokens while (k<=n) { sum=sum+k; k=k+1; } WHILE LPAR ID(k) LEQ ID(n) RPAR LBRA A token is a symbolic name, sometimes with an attribute. A lexeme is a string corresponding to a token. Compiler Construction 2013 F01-23

Lexical analysis (scanning) Source text Tokens while (k<=n) { WHILE LPAR ID(k) LEQ ID(n) RPAR LBRA sum=sum+k; ID(sum) EQ ID(sum) PLUS ID(k) SEMI k=k+1; ID(k) EQ ID(k) PLUS INT(1) SEMI } RBRA Compiler Construction 2013 F01-24

Syntactic analysis (parsing) Compiler Construction 2013 F01-25

Syntactic analysis (parsing) Compiler Construction 2013 F01-26

Abstract Syntax Tree (AST) Compiler Construction 2013 F01-27

Abstract Syntax Tree (AST) used for program representation inside tools very similar to the parse tree, but contains only essential tokens has a simpler more natural structure often represented by a typed object-oriented model abstract classes (statements, expressions,...) concrete classes (while, if, add, subtract,...) Compiler Construction 2013 F01-27

Parse tree spans all tokens Compiler Construction 2013 F01-28

Abstract syntax tree only essential structure and tokens Compiler Construction 2013 F01-29

AST class hierarchy Create class hierarchies for statements and expressions! Invent names for suitable abstract classes! Which methods are required to traverse the AST? Compiler Construction 2013 F01-30

Draw the class hierarchy Compiler Construction 2013 F01-31

Draw the class hierarchy Stmt WhileStmt getexpr() getstmt() Assignment getid() getexpr() CompoundStmt getnrofstmts() getstmt(int) Compiler Construction 2013 F01-31

Draw the class hierarchy Stmt Expr WhileStmt getexpr() getstmt() Assignment getid() getexpr() Add getexpr1() getexpr2() LessEqual getexpr1() getexpr2() CompoundStmt getnrofstmts() getstmt(int) Id getid() Int getint() Compiler Construction 2013 F01-31

Semantic analysis Analyze the AST, e.g. Which variable corresponds to which declaration? What is the type of an expression? Are there compile time errors in the program? Compiler Construction 2013 F01-32

Formalisms we will cover Regular expressions for defining tokens automatic generation of scanners Context-free grammars for defining concrete syntax trees automatic generation of parsers Abstract Grammars for defining abstract syntax trees automatic generation of Java classes Attribute Grammars for defining properties of AST nodes automatic evaluation of the attributes Aspect Modules for defining fields, methods, and attributes in separate modules automatic weaving into Java classes Compiler Construction 2013 F01-33

Compiler tools we will use JavaCC (Sun/Open source) Scanner and parser generator JJTree (Sun/Open source) adds AST building to JavaCC implements the Visitor pattern for ASTs JastAdd (LTH/Open source) generates Java classes supports static aspect oriented programming supports attribute grammars as (GNU/Open source) translates assembly code to machine code Compiler Construction 2013 F01-34

Other tools we will use Ant (Apache/Open source) Software system builder JUnit (Object Mentor/Open source) testing framework Gdb (GNU/Open source) debugger Compiler Construction 2013 F01-35

Synthesis Runtime systems How are variables accessed and procedures called? How are objects and classes represented? How is memory reused? Intermediate code generation Straight-forward mapping from AST Use unlimited number of registers (temporaries) Optimization Only brief overview (see EDA230 for detailed treatment) Machine code generation Instruction selection Register allocation Compiler Construction 2013 F01-36

Paradigms Imperative programming Compiler Construction 2013 F01-37

Paradigms Imperative programming Procedural Object oriented Compiler Construction 2013 F01-37

Paradigms Imperative programming Procedural Object oriented Declarative programming Compiler Construction 2013 F01-37

Paradigms Imperative programming Procedural Object oriented Declarative programming Functional Logical Constraint Regular expressions Context-free grammars Attribute grammars Compiler Construction 2013 F01-37

Paradigms Imperative programming Procedural Object oriented Declarative programming Functional Logical Constraint Regular expressions Context-free grammars Attribute grammars Hybrid languages Compiler Construction 2013 F01-37

Paradigms Imperative programming Procedural Object oriented Declarative programming Functional Logical Constraint Regular expressions Context-free grammars Attribute grammars Hybrid languages JastAdd Scala... Compiler Construction 2013 F01-37

Applications of compiler construction Compiler Construction 2013 F01-38

Applications of compiler construction Traditional compilers from source to assembly Source-to-source translators, preprocessors Interpreters and virtual machines Integrated programming environments Analysis tools Refactoring tools Domain-specific languages Compiler Construction 2013 F01-38

Related research at LTH Extensible compiler tools (Görel Hedin) Real-time garbage collection (Roger Henriksson) Code optimization for multiprocessors (Jonas Skeppstedt) Natural language processing (Pierre Nugues) Constraint solvers (Krzysztof Kuchcinski) Data-flow languages (Jörn Janneck) Languages for pervasive systems (Boris Magnusson) Languages for physical modeling (Johan Åkesson) Compiler Construction 2013 F01-39

Course goals After this course... You should be able to use regular expressions context-free grammars abstract grammars You should be able describe attribute grammars runtime systems and garbage collection some code optimizations You should be able to build a compiler, where you use a parser generator make semantic analysis do code generation You should be able to program, using static aspect oriented programming the visitor pattern Compiler Construction 2013 F01-40

Readings F1: Introduction Appel, chapter 1-1.2 F2: Regular expressions Appel, chapter 2 Appel, recommended exercises: 2.1 2.8 Try solve the problems in Seminar 1 Compiler Construction 2013 F01-41

Review questions Which are the major compilation phases? What is the difference between the analysis and synthesis phases? Why do we use intermediate code? What is the advantage of separating the front and back ends? What is a lexeme, a token, a parse tree, an abstract syntax tree, intermediate code, assembly code? Compiler Construction 2013 F01-42