KU Fall 2019 Drew Davidson Lecture 13 Scope

  • Slides: 17
Download presentation
KU | Fall 2019 | Drew Davidson Lecture 13 Scope 1

KU | Fall 2019 | Drew Davidson Lecture 13 Scope 1

Check-in #14 Lecture 14 – Scope 2

Check-in #14 Lecture 14 – Scope 2

Announcements Lecture 14 – Scope Quiz 2 – Next Friday • You can take

Announcements Lecture 14 – Scope Quiz 2 – Next Friday • You can take it one week from next Friday if you want Monday’s Lecture in HAW Live Assignments Project 2 Homework 5 3

Last Time SLR Parsing LR Parser Operation • Another way to derive the parse

Last Time SLR Parsing LR Parser Operation • Another way to derive the parse tree Parsing 4

COMPILER Code Generation Execution Runtime Environment Optimization Intermediate Representation SDD Parsing Semantics Lexical Analysis

COMPILER Code Generation Execution Runtime Environment Optimization Intermediate Representation SDD Parsing Semantics Lexical Analysis Syntactic Definiton 5

Today’s Outline Lecture 13 – Scope Semantics • Program meaning Scope • Name analysis

Today’s Outline Lecture 13 – Scope Semantics • Program meaning Scope • Name analysis Parsing 6

Syntax. Semantic vs Semantics Analysis Program Syntax • Does the program have a valid

Syntax. Semantic vs Semantics Analysis Program Syntax • Does the program have a valid structure? Program Semantics • Does the program have a valid meaning? 7

Goals Semantic Analysis Error Checking • Is the program’s meaning sensible? Program “Understanding” •

Goals Semantic Analysis Error Checking • Is the program’s meaning sensible? Program “Understanding” • To what does an ID refer? • To what operator does a program refer? Example Program Snippet a + b Is this addition? String concatenation? User-defined operation? 8

Respecting Semantic Program Semantics Analysis Compiler must facilitate language semantics • Prerequisite: Infer the

Respecting Semantic Program Semantics Analysis Compiler must facilitate language semantics • Prerequisite: Infer the intended program behavior w. r. t. semantics • Approach: Take multiple passes over the completed AST One example: scope 9

Scope Semantic Analysis • A central issue in name analysis is to determine the

Scope Semantic Analysis • A central issue in name analysis is to determine the lifetime of a variable, function, etc. • Scope definition: the block of code in which a name is visible/valid 10

Scope: A Language Feature Semantic Analysis • Some languages have NO notion of scope

Scope: A Language Feature Semantic Analysis • Some languages have NO notion of scope • Assembly / FORTRAN • Most familiar: static / most deeply nested • C / C++ / Java There are several decisions to make, we’ll overview a couple of them 11

Kinds of Scope Decisions • Static Scope – Most deeply nested w. r. t.

Kinds of Scope Decisions • Static Scope – Most deeply nested w. r. t. syntactic block (determined at compile time) • Dynamic Scope – Most deeply nested w. r. t. calling context (determined at runtime) 12

Forward Reference Scope Decisions • Do we allow use before name is (lexically) defined?

Forward Reference Scope Decisions • Do we allow use before name is (lexically) defined? void country() { western(); } void western() { country(); } • Requires 2 passes over the program • 1 to fill symbol table • 1 pass to use symbols 13

Variable Shadowing Scope Decisions • Do we allow names to be re-used? • What

Variable Shadowing Scope Decisions • Do we allow names to be re-used? • What about when the kinds are different? void smooth. Jazz(int a){ int a; if (a){ int a; } } } void hard. Rock(int a){ int hard. Rock; } 14

Scope Kind & Shadowing Scope Decisions int a = 1; int hop(){ return a;

Scope Kind & Shadowing Scope Decisions int a = 1; int hop(){ return a; } int hip(){ int a = 2; return hop(); } int hippo(){ return hip(); } 15

Overloading Scope Decisions • Do we allow same names, same scope, different types? int

Overloading Scope Decisions • Do we allow same names, same scope, different types? int techno(int a){ … } bool techno(bool a, bool b){ } 16

OUR Scope Decisions • What scoping rules will we employ? • What info does

OUR Scope Decisions • What scoping rules will we employ? • What info does the compiler need? 17