The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Similar documents
Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

The role of semantic analysis in a compiler

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Semantic Analysis. Lecture 9. February 7, 2018

Overview of Semantic Analysis. Lecture 9

Scoping and Type Checking

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

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

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

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Syntax Errors; Static Semantics

Compiler Theory. (Semantic Analysis and Run-Time Environments)

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

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

Semantic Analysis and Type Checking

Programming Languages Third Edition. Chapter 7 Basic Semantics

Context Analysis. Mooly Sagiv. html://

Semantic Analysis. Compiler Architecture

Lecture 16: Static Semantics Overview 1

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

CMSC 330: Organization of Programming Languages

Type systems. Static typing

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

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

Introduction to Programming Using Java (98-388)

5. Semantic Analysis!

The compilation process is driven by the syntactic structure of the program as discovered by the parser

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Lecture Outline. Type systems and their expressiveness. Type Checking in COOL (II) Type checking with SELF_TYPE in COOL

Project Compiler. CS031 TA Help Session November 28, 2011

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Semantic actions for declarations and expressions

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Semantic actions for declarations and expressions. Monday, September 28, 15

Run-time Environments

Run-time Environments

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

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

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Today's Topics. CISC 458 Winter J.R. Cordy

CS558 Programming Languages

CS4215 Programming Language Implementation

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Syntax-Directed Translation. Lecture 14

Lecture 14 Sections Mon, Mar 2, 2009

CS558 Programming Languages

Lexical Considerations

1 Lexical Considerations

Semantic actions for declarations and expressions

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CSC 533: Organization of Programming Languages. Spring 2005

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

Semantic actions for expressions

Run-time Environments - 2

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

G Programming Languages - Fall 2012

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Concepts Introduced in Chapter 6

The Decaf Language. 1 Lexical considerations

CA Compiler Construction

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

LECTURE 14. Names, Scopes, and Bindings: Scopes

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

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Operational Semantics of Cool

CS558 Programming Languages

Lexical Considerations

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Programming Assignment IV

CSCE 314 Programming Languages. Type System

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Operational Semantics. One-Slide Summary. Lecture Outline

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

A Short Summary of Javali

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Programming Assignment IV Due Monday, November 8 (with an automatic extension until Friday, November 12, noon)

Ambiguity and Errors Syntax-Directed Translation

Informatica 3 Syntax and Semantics

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

PL Revision overview

CA Compiler Construction

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

The SPL Programming Language Reference Manual

COMP-520 GoLite Tutorial

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Transcription:

The Compiler So Far Overview of Semantic Analysis Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) 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 CS780(Prasad) L19SA 1 CS780(Prasad) L19SA 2 Why a Separate Semantic Analysis? Parsing cannot catch some errors. Some language constructs are not context-free. Static Check: Identifier declaration and use An abstract version of the problem is: { wcw w (a + b) * The 1st w represents a declaration; the 2nd w represents a use. Dynamic Check: Array bounds check Null pointer dereference check What Does Semantic Analysis Do? Checks of many kinds... coolc checks: 1. All identifiers are declared. 2. Types compatibility. 3. Inheritance relationships. 4. Classes defined only once. 5. Methods in a class defined only once. 6. Reserved identifiers are not misused. CS780(Prasad) L19SA 3 CS780(Prasad) L19SA 4 More on Semantic Checks Establish that a program conforms to language definition. (Requirements language dependent) Flow of control checks Declaration of a variable should be before use. Each exit path returns a value of the correct type. Uniqueness Checks No identifier can be used for two different definitions in the same scope. Type checks Number of arguments matches the number of formals and the corresponding types are equivalent. Each access of a variable should match the declaration (arrays, structures etc.). Identifiers in an expression should be evaluatable. LHS of an assignment should be assignable. In an expression, all the types of variables, method return types and operators should be compatible. CS780(Prasad) L19SA 5 CS780(Prasad) L19SA 6 1

Scope Matching identifier declarations with uses. Important static analysis step in most languages. Including COOL! What s Wrong? Example 1 Let y: String abc in y + 3 Example 2 Let y: Int in x + 3 CS780(Prasad) L19SA 7 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. An identifier may have restricted scope. CS780(Prasad) L19SA 8 Static vs. Dynamic Scope Most languages have static scope. Scope depends only on the program text, not runtime 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. Static Scoping Example let x: Int <- 0 in { x; let x: Int <- 1 in x; x; Uses of x refer to closest enclosing definition. CS780(Prasad) L19SA 9 CS780(Prasad) L19SA 10 Dynamic Scope A dynamically-scoped variable refers to the closest enclosing binding in the execution of the program. Example g(y) = let a 4 in f(3); f(x) = a; Scope in Cool Cool identifier bindings are introduced by Class declarations (introduce class names) Method definitions (introduce method names) Let expressions (introduce object id s) Formal parameters (introduce object id s) Attribute definitions in a class (introduce object id s) Case expressions (introduce object id s) CS780(Prasad) L19SA 11 CS780(Prasad) L19SA 12 2

Scope in Cool (Cont.) Not all kinds of identifiers follow the mostclosely nested rule. For example, class definitions in Cool Cannot be nested. Are globally visible throughout the program. In other words, a class name can be used before it is defined. CS780(Prasad) L19SA 13 Example: Use Before Definition Class Foo {... let y: Bar in... ; Class Bar {... ; CS780(Prasad) L19SA 14 More Scope in Cool Attribute names are global within the method in which they are defined. Class Foo { f(): Int { a ; a: Int 0; More Scope (Cont.) Method and attribute names have complex rules. A method need not be defined in the class in which it is used, but in some parent class. Methods may also be redefined (overridden). CS780(Prasad) L19SA 15 CS780(Prasad) L19SA 16 Implementing the Most-Closely Nested Rule Much of semantic analysis can be expressed as a recursive descent of an AST. Process an AST node n Process the children of n Finish processing the AST node n Implementing... (Cont.) Example: the scope of let bindings is one subtree let x: Int 0 in e When performing semantic analysis on a portion of the AST, we need to know which identifiers are defined. x is defined in subtree e. CS780(Prasad) L19SA 17 CS780(Prasad) L19SA 18 3

Symbol Tables Consider again: let x: Int 0 in e Idea: Before processing e, add definition of x to current definitions, overriding any other definition of x After processing e, remove definition of x and restore old definition of x A symbol table is a data structure that tracks the current bindings of identifiers. A Simple Symbol Table Implementation Structure is a stack. Operations add_symbol(x) push x and associated info, such as x s type, on the stack find_symbol(x) search stack, starting from top, for x. Return first x found or NULL if none found remove_symbol() pop the stack Why does this work? CS780(Prasad) L19SA 19 CS780(Prasad) L19SA 20 Limitations The simple symbol table works for let Symbols added one at a time. Declarations are perfectly nested. Doesn t work for foo(x: Int, x: String); A Fancier Symbol Table enter_scope() start a new nested scope find_symbol(x) finds current x (or null) add_symbol(x) add a symbol x to the table check_scope(x) true if x defined in current scope exit_scope() exit current scope Other problems? CS780(Prasad) L19SA 21 CS780(Prasad) L19SA 22 Class Definitions Class names can be used before being defined. We can t check that using a symbol table, or even in one pass. Solution Pass 1: Gather all class names. Pass 2: Do the checking later. Semantic analysis requires multiple passes. Probably more than two. Types What is a type? The notion varies from language to language. Consensus A set of values. A set of operations on those values. Classes are one instantiation of the modern notion of type. CS780(Prasad) L19SA 23 CS780(Prasad) L19SA 24 4

Why Do We Need Type Systems? Consider the assembly language fragment addi $r1, $r2, $r3 What are the types of $r1, $r2, $r3? Types and Operations Certain operations are legal for values of each type. It doesn t make sense to add a function pointer and an integer in C. It does make sense to add two integers. But both have the same assembly language implementation! CS780(Prasad) L19SA 25 CS780(Prasad) L19SA 26 Type Systems A language s type system specifies which operations are valid for which types. The goal of type checking is to ensure that operations are used with the correct types. Enforces intended interpretation of values, because nothing else will! Type Checking Overview Three kinds of languages: Statically typed: All or almost all checking of types is done as part of compilation (C, Java, Cool). Dynamically typed: Almost all checking of types is done as part of program execution (Scheme). Untyped: No type checking (machine code). CS780(Prasad) L19SA 27 CS780(Prasad) L19SA 28 The Type Wars Competing views on static vs. dynamic typing. Static typing proponents say: Static checking catches many programming errors at compile time. Avoids overhead of runtime type checks. Dynamic typing proponents say: Static type systems are restrictive. Rapid prototyping difficult within a static type system. The Type Wars (Cont.) In practice, most code is written in statically typed languages with an escape mechanism Unsafe casts in C, Java. It s debatable whether this compromise represents the best or worst of both worlds. CS780(Prasad) L19SA 29 CS780(Prasad) L19SA 30 5

A simple typed language A language that has a sequence of declarations followed by a single expression P D; E D D; D id : T T char integer array [ num ] of T E literal num id E + E E [ E ] Example Program var: integer; var + 1023 A simple typed language A language that has a sequence of declarations followed by a single expression P D; E D D; D id : T T char integer array [ num ] of T E literal num id E + E E [ E ] What are the semantic rules of this language? CS780(Prasad) L19SA 31 CS780(Prasad) L19SA 32 23 Parser actions P D; E D D; D D id : T { addtype(id.entry, T.type); T char { T.type = char; T integer { T.type = integer; T array [ num ] of T 1 { T.type = array(t 1.type, num.val); Parser actions E literal { E.type = char; E num { E.type = integer; E id { E.type = lookup_type(id.name); E E 1 + E 2 { if E 1.type == integer and E 2.type == integer then E.type = integer else E.type = type_error CS780(Prasad) L19SA 33 CS780(Prasad) L19SA 34 Parser actions E E 1 [E 2 ] { if E 2.type == integer and E 1.type == array(s, t) then E.type = s else E.type = type_error CS780(Prasad) L19SA 35 6