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

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

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

CSE450. Translation of Programming Languages. Lecture 14: Arrays!

The role of semantic analysis in a compiler

Semantic Analysis. Lecture 9. February 7, 2018

1 Lexical Considerations

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

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

CSE450. Translation of Programming Languages. Lecture 21: Functions: Definitions and the Call Stack

Introduction to Programming Using Java (98-388)

Semantic actions for declarations and expressions

Semantic actions for expressions

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

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

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

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

Lexical Considerations

LECTURE 3. Compiler Phases

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

Lexical Considerations

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

Syntax Errors; Static Semantics

Exam for CSE 450 (2016)

Intermediate Code Generation

Semantic actions for declarations and expressions

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

CMSC 330: Organization of Programming Languages

Writing an Interpreter Thoughts on Assignment 6

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

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

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

KU Compilerbau - Programming Assignment

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

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

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

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Static Type Checking (Introducing Micro-Haskell)

CSI33 Data Structures

Primary Exam for CSE 450 (2016)

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

5. Syntax-Directed Definitions & Type Analysis

Semantic Analysis. Compiler Architecture

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Declaration and Memory

CS558 Programming Languages

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Scoping and Type Checking

Semantics of programming languages

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

CS112 Lecture: Primitive Types, Operators, Strings

Lecture 14 Sections Mon, Mar 2, 2009

Compilers. Lecture 2 Overview. (original slides by Sam

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

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

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

CSE450. Translation of Programming Languages. Lecture 10: Flow Control and Lexical Scope

CSE P 501 Exam 8/5/04

Language Reference Manual simplicity

Review for Test 1 (Chapter 1-5)

Programming Languages Third Edition. Chapter 7 Basic Semantics

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

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

CSE 431S Type Checking. Washington University Spring 2013

Syntax and Grammars 1 / 21

CSCE 314 Programming Languages. Type System

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Lecture 12: Data Types (and Some Leftover ML)

Array. Array Declaration:

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

B.V. Patel Institute of BMC & IT, UTU 2014

The SPL Programming Language Reference Manual

Semantics of programming languages

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

CS 360 Programming Languages Interpreters

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

Syntax-Directed Translation. Lecture 14

Compiler Design (40-414)

Working of the Compilers

CSE 12 Abstract Syntax Trees

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSE 401 Midterm Exam Sample Solution 2/11/15

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

A Simple Syntax-Directed Translator

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

CSE 582 Autumn 2002 Exam 11/26/02

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

CSC 467 Lecture 3: Regular Expressions

Decaf Language Reference

Project Compiler. CS031 TA Help Session November 28, 2011

G Programming Languages - Fall 2012

Transcription:

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

Structure Project 1 - of a Project 2 - Compiler Today! Project 3 - Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator Front End Intermediate Code Code Optimizer Target Code Generator Back End Target Language

Importance of Semantic Analysis Parsing cannot catch all possible errors. Parsing assumes that we are working with a context-free grammar. Example language constructs that require context: Have variables been declared? Is a variable available in the current scope? Are the operands of an expression valid types? Is an assignment using legal types? Are the arguments to a function of the correct type?

Types Why do we need to worry about type checking? Consider the Tube-IC fragment: add s12 s20 s34 What types are s12, s20, and s34? They can be anything! Likewise, processors treat registers generically. This makes their operations flexible and reusable, but not type safe.

Types and Operations Legal operations can vary depending on the type of a value. It typically does not make sense to add a function pointer to an integer in C++ It does makes sense to add integers Both of these operations can potentially have the same implementation in assembly. As far as the processor is concerned, an integer and a pointer look the same.

Type Systems A language type system specifies which types are available, and what operations can be used on those types. The goal of type checking is to ensure that only "sensible" operations are allowed to be performed. Type checking also can provide the ability to have different operations performed depending on the types involved.

Three basic kinds of Type Checking Statically Typed Almost all type checking happens at compile time Each variable is limited to a single type Language examples include C/C++, Java, Tubular Dynamically Typed Almost all type checking occurs at runtime Variables can typically contain any type of value Most scripting languages do this (Javascript, Python, Ruby, Scheme, etc.) Untyped No checking is done, such as in assembly

Static vs. Dynamic Typing In practice, most languages use some statically typed and dynamically typed elements. Provide escape mechanisms (casting) to allow static elements to be used as needed. There are three basic kinds of type checking systems: Static typing Many errors can be caught at compile time Optimizations can be easier to perform Runtime environment can be faster, type decisions have already been performed Dynamically Typed Less restrictive, easier to express operations, faster development Programs can be more modular, extensible, and adaptive More runtime machinery required, can be slower during execution

Types in Tubular We will have two basic types in Project 4: val char - floating point quantity (already implemented) - a single ascii char And one meta type will be added for Project 5: array A consecutive grouping of a basic type array(char) can also be referred to as string

Tubular Type Checking For Tubular, we will be using static typing. Simpler to implement the runtime environment. Four basic scenarios where types will need to be checked: Variable Assignments: type of RHS must match variable Mathematical Operations: type must be val for + - * / % && and! Comparison Operators: types must both be val or both be char Generic commands, like print: any type accepted Function calls (coming in Project 7): arguments must match

Variable Assignments val x; char y; assignment: var_any '=' expression x = 1; y = 'b'; = = x 1 y 'b' x = 'a'; y = 2; = = x 'a' y 2

Mathematical Operations val x = 1; char y; x = x + 2; y = 'c'; x = y + 3; y = x; y = 'a' + 'b'; expr: expr '+' expr + x 2 + y 3 'a' + 'b'

Functions and Commands The print command can take anything. Type information is used to determine what operation to perform. If the type of the argument is val, use out_val If the type of the argument is char, use out_char Starting with Project 5, If the type of the argument is an array print out each element of that array with the internal type Other commands and functions may have particular type requirements, depending on argument position.

Type Checking Char versus Val

The char Type Like val variables can be declared type char. val x = 0; char y = a ; Char variables are single characters between single quotes. The symbol table must keep track of type to ensure that no illegal operations take place.

Escape Characters char a = \n ; char b = \t ; char c = \ ; char d = \\ ; The 4 escape characters are preceded by a backslash. No other escape characters should be implemented.

Special Note - # is a normal character char a = # ; The comment character # is allowed between single quotes and doesn t denote a comment.

Type Checking - Assignment char a = x ; val b; b = a; # ERROR You cannot assign a variable of one type to another. With static type checking, we know the type of every variable at compile time and can ensure correctness.

Type Checking - Relationship Operators char a = x ; char b = y ; b > a; val c = 0; You can compare (==,!=, >, >=, <, <=) two variables of the same type. But you cannot compare two different types. a!= c; # ERROR

Type Checking - Mathematical Operators char a = x ; char b = y ; a + b; # ERROR a && b; # ERROR The char type cannot be used by math operators (+, +=, -, -=, *, *=, /, /=), nor boolean operators (&&,,!).

Type Checking - Boolean Evaluation char a = x ; if (a) { # ERROR a = b ; } The char type cannot be used where a boolean result is needed (conditions for if and while statements).

Type Checking - Type Specific Commands char a = x ; random(a); # ERROR print(a); The random command only takes the type val, giving it anything else is an error. The print command happily takes type char as an argument.

Hold up the colors that are legal. #1 val x = 1; x = a ; #3 char x = a ; char y = b ; x!= y; #2 char x = a ; char y = b ; char z = x + y; #4 char x = a ; if (x == b ) { x = b ; }

Char Implementation - val_copy char a = \n ; becomes val_copy \n s1 val_copy s1 s2 Tube Intermediate Code handles char s just like val s. Escape characters are treated identically to Tubular (original source).

Char Implementation - other ops a > b ; becomes val_copy a s1 val_copy b s2 test_gtr s1 s2 s3 The other TubeIC operators behave with char like val.

Char Implementation - out_char print(1, a ) becomes val_copy 1 s1 val_copy a s2 out_val s1 out_char s2 out_char \n You ve already been using the one char specific TubeIC instruction.

How to keep track of TYPE Every variable (temporary or named) needs to know its type. You can use the symbol table to store this information. For this class, there will only be a finite number of types (val, char, and a few others introduced in future projects).

Implementing char type 1. Make the lexer include escape characters 2. Make the parser allow type char in variable declarations 3. Make the symbol table store type of every variable used 4. Make the abstract syntax tree include a node for literal char values 5. For each node in the AST, make sure that the types of its children are legal or raise an error if not. This can be done at the creation of the node.

Scope Refresher Symbol Tables and Decrementing Scope

Implementing Scoping Scoping can be implemented right within your symbol table(s). When a variable is declared: Check that it has not been previously defined within this scope (but lower scopes are allowed) Add it to the table, recording its name, type, etc., along with the scope in which it was created. When leaving a scope, simply deactivate symbols that are no longer accessible. They can t be used again in the source program. (But you will need to reference them when outputting your intermediate code!)

Stack of SymbolTables Given: val a = 123; val b = 44; if (a == 123) { char a = 'x'; print(a); } print(a); SymbolTable[0]: val a val b SymbolTable[1]: char a