CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Similar documents
CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE410 aka CSE306 Software Quality

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CS 314 Principles of Programming Languages

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

LECTURE 3. Compiler Phases

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

Syntax. In Text: Chapter 3

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Compiler Code Generation COMP360

Semantic Analysis. Compiler Architecture

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

1 Lexical Considerations

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

Formal Languages and Compilers Lecture I: Introduction to Compilers

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CMPE 152 Compiler Design

CS/SE 153 Concepts of Compiler Design

Programming Languages: Lecture 12

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Lecture 3: C Programm

CMPE 152 Compiler Design

CSE 113 A. Announcements - Lab

Winter Compiler Construction Who. Mailing list and forum

Ruby: Introduction, Basics

Lecture08: Scope and Lexical Address

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

DEPARTMENT OF INFORMATION TECHNOLOGY AUTOMATA AND COMPILER DESIGN. B.Tech-IT, III Year -I Sem

CSE 307: Principles of Programming Languages

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Compilers. Computer Science 431

CMPE 152 Compiler Design

CSE 504: Compiler Design

Syntax. A. Bellaachia Page: 1

CSCE 314 Programming Languages

Final CSE 131B Spring 2004

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

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

Introduction to Lexical Analysis

EECS483 D1: Project 1 Overview

1. Consider the following program in a PCAT-like language.

Programming Languages

Programming Languages

CSE 3302 Programming Languages Lecture 2: Syntax

CSE 582 Autumn 2002 Exam 11/26/02

COSE212: Programming Languages. Lecture 0 Course Overview

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

COMP108 Algorithmic Foundations

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

CSE302: Compiler Design

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Lexical Analysis. Lecture 2-4

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

CSE3322 Programming Languages and Implementation

Compiler Construction

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

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

Syntax-Directed Translation. Lecture 14

Undergraduate Compilers in a Day

CS 415 Midterm Exam Spring SOLUTION

CSc 520 Principles of Programming Languages

MIDTERM EXAMINATION. CSE 130: Principles of Programming Languages. Professor Goguen. February 16, points total

CSE3322 Programming Languages and Implementation

4. An interpreter is a program that

UNIVERSITY OF CALIFORNIA

Chapter 3. Describing Syntax and Semantics

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

COS 320. Compiling Techniques

A programming language requires two major definitions A simple one pass compiler

EI326 ENGINEERING PRACTICE & TECHNICAL INNOVATION (III-G) Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

M.EC201 Programming language

CPS 506 Comparative Programming Languages. Syntax Specification

Compiler Construction

Principles of Compiler Construction ( )

The role of semantic analysis in a compiler

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

afewadminnotes CSC324 Formal Language Theory Dealing with Ambiguity: Precedence Example Office Hours: (in BA 4237) Monday 3 4pm Wednesdays 1 2pm

Semantic Analysis. Lecture 9. February 7, 2018

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

NOTE: Answer ANY FOUR of the following 6 sections:

A simple syntax-directed

CS/SE 153 Concepts of Compiler Design

Types and Programming Languages. Lecture 5. Extensions of simple types

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CS 314 Principles of Programming Languages

Transcription:

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall www.cse.buffalo. edu/faculty/alphonce/sp18/cse443 piazza.com/buffalo/spring2018/cse443

BUILD A COMPILER!

Assessment plan Homework - 5 assignments Project - 5 phases / checkpoints Examination - 3 hour final, based on homework/project

Learning outcomes Learning outcome Identify and describe the function of the major phases of a compiler. Define formally the grammars used in the front end of a compiler, their application in the front end, and techniques for parsing such grammars. Evaluate (compare and contrast) different intermediate representations. Explain the compiler s role in creating and managing run-time environments. Explain and evaluate (compare and contrast) different approaches to code generation. Identify and explain the applicability and operation of code optimizations. Build both the front and back ends of a compiler. Instructional methods Lecture-based instruction Hands-on activities in lecture and recitation Assessment HW, EX HW, EX PROJ

Grading INDIVIDUAL TEAM Homework 30% Project Exam 50% 20%

Teams & Recitations Form teams this week - I recommend teams of size 3. Recitations start this week. Recommend all team members attend same recitation, but not required (you can attend either recitation). For project, you may choose either C or SML - decide with your teammates (you must have a unanimous decision). We will discuss more in recitation this week.

Goal: build a compiler source program executable

Phases of a compiler source program executable Figure 1.6, page 5 of text

Why? Deeper understanding of languages Become a better programmer Learn how to build tools Build special-purpose languages (DSLs) Theory meets practice High-level meets low-level => CSE490 Computer Architecture collaboration

Deep understanding - ex 1 name vs identifier vs variable

name y.x variable identifier x refers to location in memory

Deep understanding - ex 1 void foo() { int x = 0; printf(x); } void bar() { double x = 3.8; printf(x); }

Deep understanding - ex 1 int func(int x) { if (x == 0) { return 1; } else { return x * func(x-1); } }

Deep understanding - ex 1 struct Pair { int x; int y; }; void bar() { } Pair r, s;

RUNTIME CODE variable variable identifier x variable variable variables in distinct scopes, variables in distinct records/objects, or variables in distinct function invocations

Deep understanding - ex 2 order of evaluation Does source code completely determine order of evaluation/ execution at machine language level?

Deep understanding - ex 2 a + b * c; What is the order of evaluation?

Deep understanding - ex 2 f() + g() * h(); What is the order of evaluation?

Deep understanding - ex 2 f() + f() * f(); What is the order of evaluation?

Deep understanding - ex 2 a + b * c; In most languages the result will be consistent with the evaluation of a + ( b * c )

Deep understanding - ex 2 a + b * c; Order of operations is important here, but order of evaluation of the variables a, b, and c is not (as long as they are evaluated before they are needed.

Deep understanding - ex 2 f() + g() * h(); What is the order of the function calls? Must g be called before f?

Deep understanding - ex 2 f() + f() * f(); How many times will f be called? Could it be just once? If it cannot be just once, is order important?

Deep understanding - ex 2 f() + f() * f(); If the value of f() depends on mutable persistent state, then the value returned by each call can be different.

Deep understanding - ex 2 f() + f() * f(); If f is known to be referentially transparent, then each call to f() will produce the same value. We can then compute f once, and use its value multiple times.

What determines program meaning? #include <stdio.h> int main() { int i = 0; int sum = 0; while (i <= 10) { sum = sum + i; printf("sum of integers from 0 to %d is %d.\n",i,sum); i = i + 1; } }

What determines program semantics? #include <stdio.h> int main() { int i = 0; int sum = 0; while (i <= 10) { sum = sum + i; printf("sum of integers from 0 to %d is %d.\n",i,sum); i = i + 1; } }

What is this? #include <stdio.h> int main() { int i = 0; int sum = 0; while (i <= 10) { sum = sum + i; printf("sum of integers from 0 to %d is %d.\n",i,sum); i = i + 1; } }

What is this? #include <stdio.h> int main() { int i = 0; int sum = 0; while (i <= 10) { sum = sum + i; printf("sum of integers from 0 to %d is %d.\n",i,sum); i = i + 1; } }

/*La suite de Syracuse est définie ainsi : - on part d'un entier ; - s'il est pair, on le divise par 2 ; - sinon, on le multiplie par 3 et on ajoute 1 ; - on recommence la même opération sur l'entier obtenu, et ainsi de suite ; - la suite s'arrête si on arrive à 1. */ syracuse : durée est un nombre e est un nombre début e prend 14 tant que e!= 1 lis durée prend durée + 1 si (e mod 2) = 0, e prend e / 2 sinon e prend e * 3 + 1 affiche e ferme affiche "durée = {durée}" TRY IT

/* The Syracuse sequence is defined as follows: - it starts with any natural number > 0 - if it is even, we divide by 2 - else we multiply by 3 and add 1 - the process is repeated on the result - the process ends when the result is 1 */ void syracuse() { int iterations; int e; } iterations = 0; e = 14; while (e!= 1) { iterations = iterations + 1; if ( (e % 2) == 0 ) e = e / 2; else e = e * 3 + 1; printf("%d\n",e); } printf("iterations = %d\n",iterations);

Linotte French keywords C English keywords syracuse : durée est un nombre e est un nombre début e prend 14 tant que e!= 1 lis durée prend durée + 1 si (e mod 2) = 0, e prend e / 2 sinon e prend e * 3 + 1 affiche e ferme affiche "durée = {durée}" void syracuse() { int iterations = 0; int e; } e = 14; while (e!= 1) { iterations = iterations + 1; if ( (e % 2) == 0 ) e = e / 2; else e = e * 3 + 1; printf("%d\n",e); } printf("iterations = %d\n",iterations);

Linotte French keywords C English keywords syracuse : durée est un nombre e est un nombre début e prend 14 tant que e!= 1 lis durée prend durée + 1 si (e mod 2) = 0, e prend e / 2 sinon e prend e * 3 + 1 affiche e ferme affiche "durée = {durée}" void syracuse() { int iterations = 0; int e; } e = 14; while (e!= 1) { iterations = iterations + 1; if ( (e % 2) == 0 ) e = e / 2; else e = e * 3 + 1; printf("%d\n",e); } printf("iterations = %d\n",iterations); Keywords have no inherent meaning. Program meaning is given by formal semantics. Compiler must preserve semantics of source program in translation to low level form.

Syntax and semantics Syntax: program structure Semantics: program meaning Semantics are determined (in part) by program structure.

Languages: the Chomsky hierarchy "On Certain Formal Properties of Grammars" published 1959 recursively enumerable context-sensitive context-free regular https://upload.wikimedia.org/wikipedia/commons/8/86/noam_chomsky.jpg

SOURCE: https://openi.nlm.nih.gov/detailedresult.php?img=pmc3367694_rstb20120103-g2&req=4 AUTHORS: Fitch WT, Friederici AD - Philos. Trans. R. Soc. Lond., B, Biol. Sci. (2012) LICENSE: http://creativecommons.org/licenses/by/3.0/

Syntactic structure Lexical structure SOURCE: https://openi.nlm.nih.gov/detailedresult.php?img=pmc3367694_rstb20120103-g2&req=4 AUTHORS: Fitch WT, Friederici AD - Philos. Trans. R. Soc. Lond., B, Biol. Sci. (2012) LICENSE: http://creativecommons.org/licenses/by/3.0/

Phases of a compiler Lexical structure Syntactic structure Figure 1.6, page 5 of text