Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)



Similar documents
Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Lecture 9. Semantic Analysis Scoping and Symbol Table

Semantic Analysis: Types and Type Checking

CSCI 3136 Principles of Programming Languages

Compiler Construction

Chapter 5 Names, Bindings, Type Checking, and Scopes

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Moving from CS 61A Scheme to CS 61B Java

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 2: Elements of Java

The C Programming Language course syllabus associate level

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

C Compiler Targeting the Java Virtual Machine

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Compiler I: Syntax Analysis Human Thought

Symbol Tables. Introduction

CS143 Handout 18 Summer 2012 July 16 th, 2012 Semantic Analysis

Semester Review. CSC 301, Fall 2015

Install Java Development Kit (JDK) 1.8

Theory of Compilation

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Compiler Construction

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

Java Interview Questions and Answers

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Lecture 1: Introduction

Chapter 13 Storage classes

The previous chapter provided a definition of the semantics of a programming

Introduction to Object-Oriented Programming

AP Computer Science Java Subset

CA Compiler Construction

03 - Lexical Analysis

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Programming Languages

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Programming Languages

Technical paper review. Program visualization and explanation for novice C programmers by Matthew Heinsen Egan and Chris McDonald.

Top 72 Perl Interview Questions and Answers

Lab Experience 17. Programming Language Translation

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Programming Languages CIS 443

CSC Software II: Principles of Programming Languages

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

1/20/2016 INTRODUCTION

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Arrays in Java. Working with Arrays

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Sources: On the Web: Slides will be available on:

Java Application Developer Certificate Program Competencies

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Antlr ANother TutoRiaL

An Incomplete C++ Primer. University of Wyoming MA 5310

Object Oriented Software Design

ANTLR - Introduction. Overview of Lecture 4. ANTLR Introduction (1) ANTLR Introduction (2) Introduction

Explain the relationship between a class and an object. Which is general and which is specific?

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Introduction to Java

Basics of Java Programming Input and the Scanner class

Object Oriented Software Design

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Object Oriented Software Design II

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Programming Project 1: Lexical Analyzer (Scanner)

Some Scanner Class Methods

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Introduction to Java. CS 3: Computer Programming in Java

Lecture 5: Java Fundamentals III

Computer Programming I

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Stacks. Linear data structures

arrays C Programming Language - Arrays

Example of a Java program

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Introduction to Python

Syntaktická analýza. Ján Šturc. Zima 208

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Java (12 Weeks) Introduction to Java Programming Language

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

C++FA 5.1 PRACTICE MID-TERM EXAM

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Introduction to Programming

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

COMPUTER SCIENCE. 1. Computer Fundamentals and Applications

Transcription:

Semantic Analysis Scoping (Readings 7.1,7.4,7.6) Static Dynamic Parameter passing methods (7.5) Building symbol tables (7.6) How to use them to find multiply-declared and undeclared variables Type checking (6.1-6.6) The Compiler So Far Lexical analysis Detects inputs with illegal tokens e.g.: main$ (); Parsing Detects inputs with ill-formed parse trees e.g.: missing semicolons Semantic analysis Last front end phase Catches all remaining errors

Classes of Errors Lexical detected by scanner Examples: Illegal character in input Illegal comments Unterminated string constants Syntactic detected by the parser Input is not a legal program, because it cannot be parsed by CFG Example: a = * 5 Classes of Errors Static Semantic - can be detected in parser or in separate semantic analysis passes Input can be parsed by CFG but some non context-free error Examples Multiply declared variables Undeclared variables Function call with wrong number of arguments Type mismatches

Classes of Errors Semantic - may be detected at compile time, may also add checks in code for runtime Examples Division by 0 Array index out-of-bounds Dereference of NULL pointer Can include code to check for conditions and not allow them to occur Advantage: error message instead of error behavior Disadvantage: object code is longer and slower Classes of Errors Semantic - may be detected at compile time, may also add checks in code for runtime Examples Division by 0 Array index out-of-bounds Dereference of NULL pointer Can include code to check for conditions and not allow them to occur Advantage: error message instead of error behavior Disadvantage: object code is longer and slower

Classes of Errors Logical hardest to detect Program is syntactically and semantically correct but does not do the correct thing Compiler can sometimes detect problem things if (x = 0) result = 1; result = 2; Assignment in the body of the if is useless immediately overwritten by next assignment Static Semantic Errors multiple declarations: a variable should be declared (in the same scope) at most once undeclared variable: a variable should not be used before being declared. type mismatch: type of the left-hand side of an assignment should match the type of the right-hand side wrong arguments: methods should be called with the right number and types of arguments

An sample semantic analyzer works in two phases i.e., it traverses the AST created by the parser: 1. For each scope in the program: process the declarations = add new entries to the symbol table and report any variables that are multiply declared process the statements = find uses of undeclared variables, and update the "ID" nodes of the AST to point to the appropriate symbol-table entry. 2. Process all of the statements in program again, use the symbol-table information to determine the type of each expression, and to find type errors Symbol Table = set of entries purpose: keep track of names declared in the program names of variables, classes, fields, methods, symbol table entry: associates a name with a set of attributes, e.g.: kind of name (variable, class, field, method, etc) type (int, float, etc) nesting level memory location (i.e., where will it be found at runtime).

Scoping symbol table design influenced by what kind of scoping is used by the compiled language In most languages, the same name can be declared multiple times if its declarations occur in different scopes, and/or involve different kinds of names Scoping: example Java: can use same name for a class, field of the class, a method of the class, and a local variable of the method legal Java program: class Test { int Test; void Test( ) { double Test;

Scoping: overloading Java and C++ (but not in Pascal or C): can use the same name for more than one method as long as the number and/or types of parameters are unique. int add(int a, int b); float add(float a, float b); Scoping: general rules The scope rules of a language: determine which declaration of a named object corresponds to each use of the object i.e., scoping rules map uses of objects to their declarations C++ and Java use static scoping: mapping from uses to declarations is made at compile time. C++ uses the "most closely nested" rule a use of variable x matches the declaration in the most closely enclosing scope such that the declaration precedes the use

Scope levels Each function has two or more scopes: one for the parameters, one for the function body, and possibly additional scopes in the function for each for loop and each nested block (delimited by curly braces) Example void f( int k ) { int k = 0; while (k) { int k = 1; // k is a parameter // also a local variable // another local variable, in a loop the outmost scope includes just the name "f", and function f itself has three (nested) scopes: 1. The outer scope for f just includes parameter k 2. The next scope is for the body of f, and includes the variable k that is initialized to 0 3. The innermost scope is for the body of the while loop, and includes the variable k that is initialized to 1

Example Match uses and declarations int k=10, x=20; void foo(int k) { int a = x; int x = k; int b = x; while (...) { int x; if (x == k) { int k, y; k = y = x; if (x == k) { int x = y; Dynamic Scoping Not all languages use static scoping Lisp, APL, and Snobol use dynamic scoping Dynamic scoping: A use of a variable that has no corresponding declaration in the same function corresponds to the declaration in the most-recently-called still active function

Example For example, consider the following code: void main() { f1(); f2(); void f1() { int x = 10; g(); void f2() { String x = "hello"; f3(); g(); void f3() { double x = 30.5; void g() { print(x); Example: Dynamic Scoping Assuming dynamic scoping is used, what is output by this program? void main() { int x = 0; f1(); g(); f2(); void f1() { int x = 10; g(); void f2() { int x = 20; f1(); g(); void g() { print(x);

Scoping Comparison Static vs dynamic scoping generally, dynamic scoping is a bad idea can make a program difficult to understand a single use of a variable can correspond to many different declarations with different types! Can a name be used before they are defined? Java: a method or field name can be used before the definition appears, no true for a variable! Example class Test { void f() { val = 0; // field val has not yet been declared -- OK g(); // method g has not yet been declared -- OK x = 1; // var x has not yet been declared -- ERROR! int x; void g() { int val;