lecture09

lecture09 - 1 Prof. Aiken CS 143 Lecture 9 1 Overview of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Prof. Aiken CS 143 Lecture 9 1 Overview of Semantic Analysis Lecture 9 Prof. Aiken CS 143 Lecture 9 2 Announcements WA2 due today Electronic hand-in Or after class Or under my office door by 5pm PA2 due tonight Electronic hand-in Regrades One deduction for each error But you have to prove it to us . . . Prof. Aiken CS 143 Lecture 9 3 Midterm Thursday In class SCPD students come to campus for the exam Material through lecture 8 Four sheets hand- or type-written notes Prof. Aiken CS 143 Lecture 9 4 Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Implementation: symbol tables Types Prof. Aiken CS 143 Lecture 9 5 The Compiler So Far Lexical analysis Detects inputs with illegal tokens Parsing Detects inputs with ill-formed parse trees Semantic analysis Last front end phase Catches all remaining errors Prof. Aiken CS 143 Lecture 9 6 Why a Separate Semantic Analysis? Parsing cannot catch some errors Some language constructs not context-free 2 Prof. Aiken CS 143 Lecture 9 7 What Does Semantic Analysis Do? Checks of many kinds . . . coolc checks: 1. All identifiers are declared 2. Types 3. Inheritance relationships 4. Classes defined only once 5. Methods in a class defined only once 6. Reserved identifiers are not misused And others . . . The requirements depend on the language Prof. Aiken CS 143 Lecture 9 8 Scope Matching identifier declarations with uses Important static analysis step in most languages Including COOL! Prof. Aiken CS 143 Lecture 9 9 Whats Wrong? Example 1 Let y: String abc in y + 3 Example 2 Let y: Int in x + 3 Note: An example property that is not context free. Prof. Aiken CS 143 Lecture 9 10 Scope (Cont.) The scope of an identifier is the portion of a program in which that identifier is accessible The same identifier may refer to different things in different parts of the program Different scopes for same name dont overlap An identifier may have restricted scope Prof. Aiken CS 143 Lecture 9 11 Static vs. Dynamic Scope Most languages have static scope Scope depends only on the program text, not run- time behavior Cool has static scope A few languages are dynamically scoped Lisp, SNOBOL Lisp has changed to mostly static scoping Scope depends on execution of the program Prof. Aiken CS 143 Lecture 9 12 Static Scoping Example let x: Int <- 0 in { x; let x: Int <- 1 in x; x; } 2 Prof. Aiken CS 143 Lecture 9 7 What Does Semantic Analysis Do? Checks of many kinds . . . coolc checks: 1. All identifiers are declared 2. Types 3. Inheritance relationships 4. Classes defined only once 5. Methods in a class defined only once 6. Reserved identifiers are not misused And others . . ....
View Full Document

Page1 / 13

lecture09 - 1 Prof. Aiken CS 143 Lecture 9 1 Overview of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online