CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - - PowerPoint PPT Presentation

cse443 compilers
SMART_READER_LITE
LIVE PREVIEW

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - - PowerPoint PPT Presentation

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http:/ /www.cse.buffalo.edu/faculty/alphonce/SP17 /CSE443/index.php https:/ /piazza.com/class/iybn4ndqa1s3ei BUILD A COMPILER! Learning outcomes Instructional


slide-1
SLIDE 1

CSE443 Compilers

  • Dr. Carl Alphonce

alphonce@buffalo.edu 343 Davis Hall

http:/ /www.cse.buffalo.edu/faculty/alphonce/SP17 /CSE443/index.php https:/ /piazza.com/class/iybn4ndqa1s3ei

slide-2
SLIDE 2

BUILD A COMPILER!

slide-3
SLIDE 3

Learning outcomes

Learning outcome Instructional methods Assessment

Identify and describe the function of the major phases of a compiler. Lecture-based instruction Hands-on activities in lecture and recitation HW, EX 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. PRE, HW, EX Identify and explain the applicability and

  • peration of code optimizations.

Build both the front and back ends of a compiler. PROJ

slide-4
SLIDE 4

Assessment

Homework - 5-7 assignments Project - 5-7 phases Presentation - 1-2 per team Examination - 3 hour final, based

  • n homework/project
slide-5
SLIDE 5

Grading

INDIVIDUAL TEAM Homework 30% Project 40% Exam 20% Presentation 10%

slide-6
SLIDE 6

Teams & Recitations

Form teams this week - I recommend teams

  • f size 3-4.

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.

slide-7
SLIDE 7

Goal: build a compiler

source program executable

slide-8
SLIDE 8

Phases of a compiler

Figure 1.6, page 5 of text

source program executable

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Deep understanding - ex 1

name vs identifier vs variable

slide-11
SLIDE 11

void foo() { int x = 0; printf(x); }

Deep understanding - ex 1

slide-12
SLIDE 12

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

Deep understanding - ex 1

slide-13
SLIDE 13

struct Pair { int x; int y; }; void bar() { Pair r, s; }

Deep understanding - ex 1

slide-14
SLIDE 14

name y.x identifier x variable location in memory

refers to

slide-15
SLIDE 15

identifier x variable

variables in distinct scopes, variables in distinct records/objects, or variables in distinct function invocations

CODE RUNTIME

variable variable variable

slide-16
SLIDE 16
  • rder of evaluation

Deep understanding - ex 2

slide-17
SLIDE 17

a + b * c;

Deep understanding - ex 2

slide-18
SLIDE 18

a + b * c; f() + g() * h();

Deep understanding - ex 2

slide-19
SLIDE 19

a + b * c; f() + g() * h(); f() + f() * f();

Deep understanding - ex 2

slide-20
SLIDE 20

Languages: the Chomsky hierarchy

"On Certain Formal Properties of Grammars"

recursively enumerable context-sensitive context-free

regular

https:/ /upload.wikimedia.org/wikipedia/commons/8/86/Noam_chomsky.jpg

slide-21
SLIDE 21

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/

slide-22
SLIDE 22

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/

Lexical structure Syntactic structure

slide-23
SLIDE 23

Phases of a compiler

Figure 1.6, page 5 of text

Lexical structure Syntactic structure

slide-24
SLIDE 24

Lexical Structure

int main(){

slide-25
SLIDE 25

Lexical Structure

int main(){

character stream

i n t m a i n ( ) {

slide-26
SLIDE 26

Lexical Structure

int main(){

character stream -> token stream

i n t m a i n ( ) { id(“int”) id(“main”) LPAR RPAR LBRACE

slide-27
SLIDE 27

Lexical Structure

tokens

keywords (e.g. static, for, while, struct)

  • perators (e.g. <, >, <=, =, ==, +, -, &, .)

identifiers (e.g. foo, bar, sum, mystery) literals (e.g. -17, 34.52E-45, true, ’e’, “Serenity”) punctuation (e.g. { , } , ( , ) , ; )

slide-28
SLIDE 28

meta vs object language

  • bject language: the language we

are describing meta language: the language we use to describe the object language

slide-29
SLIDE 29

meta vs object language

use quotes (meta vs ‘object’) punctuation (e.g. ‘{’ , ‘}’ , ‘(’ , ‘)’ , ‘;’ ) use font or font property (meta vs object) punctuation (e.g. { , } , ( , ) , ; )

slide-30
SLIDE 30

languages & grammars

Formally, a language is a set of strings

  • ver some alphabet
  • Ex. {00, 01, 10, 11} is the set of all

strings of length 2 over the alphabet {0, 1}

  • Ex. {00, 11} is the set of all even parity

strings of length 2 over the alphabet {0, 1}

slide-31
SLIDE 31

Formally, a grammar is defined by 4 items:

  • 1. N, a set of non-terminals
  • 2. ∑, a set of terminals
  • 3. P, a set of productions
  • 4. S, a start symbol

G = (N, ∑, P, S)

languages & grammars

slide-32
SLIDE 32

N, a set of non-terminals ∑, a set of terminals (alphabet) N ∩ ∑ = {} P, a set of productions of the form (right linear) X -> a X -> aY X -> ℇ X ∈ N, Y ∈ N, a ∈ ∑, ℇ denotes the empty string S, a start symbol S ∈ N

languages & grammars

slide-33
SLIDE 33

Given a string αΑ, where α ∈ ∑

* and Α ∈ N,

and a production Α -> β ∈ P we write αΑ => αβ to indicate that αΑ derives αβ in one step. =>

k and => * can be used to indicate k or

arbitrarily many derivation steps, respectively.

languages & grammars

slide-34
SLIDE 34

𝓜(G) is the set of all strings derivable from G starting with the start symbol; i.e. it denotes the language of G.

languages & grammars

slide-35
SLIDE 35

Given a grammar G the language it generates, 𝓜(G), is unique. Given a language L there are many grammars H such that 𝓜(H) = L.

languages & grammars