©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Symbol Table.
Intermediate Code Generation
Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 Compiler Construction Intermediate Code Generation.
The Assembly Language Level
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Chapter 9: Subprogram Control
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CS 2104 Prog. Lang. Concepts Subprograms
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 4 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
ISBN Chapter 10 Implementing Subprograms.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 321 Programming Languages and Compilers Names, Scopes, and Bindings.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Implementing Subprograms
Lecture 9 Symbol Table and Attributed Grammars
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Names and Attributes Names are a key programming language feature
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS 326 Programming Languages, Concepts and Implementation
Structure of Programming Languages
Implementing Subprograms
Implementing Subprograms
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Course Overview PART I: overview material PART II: inside a compiler
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Presentation transcript:

©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones

Lecture 3©2004 Joel Jones 2 Outline Reading & Questions from Last Class Scopes Symbol Tables Reading for Next Class

Lecture 3©2004 Joel Jones 3 Question from Last Class How many different scopes does C++ have? Pair Up: Decide on answers to the questions and the justification

Lecture 3©2004 Joel Jones 4 Scope rules The region of the program in which a binding is active is its scope. Most languages today are lexically scoped

Lecture 3©2004 Joel Jones 5 Static (Lexical) Scopes Global scope (basic, original awk) Program unit (main program, subroutines, functions) in FORTRAN, C, C++. Blocks (C, C++, Java, Pascal, etc.) Objects (structs, unions) (C++, Java, etc.) Enumerations (C, C++) File scope (C, C++)

Lecture 3©2004 Joel Jones 6 Understanding Scopes in C++ Pair Up: Given the following code, what is the sequence of scopes that are searched to find the declaration of “x”? Foo.c++: … class Foo::Bar { … void f() {… int i; if (aBool) {… i = x; } }; }

Lecture 3©2004 Joel Jones 7 Static Scope in FORTRAN We will discuss two classes of Fortran objects variables common blocks Common blocks are blocks of storage that can be shared by several program units.

Lecture 3©2004 Joel Jones 8 Static Scope (Cont.) Common block example subroutine first real b(2) logical flag complex c type coordinates sequence real x, y logical z_0 end type coordinates type (coordinates) p common /reuse/ b,c,flag,p … subroutine second integer I(8) common /reuse/ i z_0yxflag i(8)i(7)i(6)i(5)i(4)i(3) c i(2) b(2) i(1) b(1)

Lecture 3©2004 Joel Jones 9 Static Scope (Cont.) Lifetime of statically allocated variables and common blocks is the duration of the program. Most Fortran 77 implementations do all allocations statically. If default allocation is not static, variables and common blocks ca be saved (I.e. declared as save: save /reuse/,w,r) to force their static allocation. Variables can only be saved in the program unit where they are declared. If a common block is saved, it has to be saved in all program units where it appears.

Lecture 3©2004 Joel Jones 10 Static Scope (Cont.) The default is that common blocks can go away when there are no active program units that access them. Saved variables and saved common blocks may cause collisions in parallel executions, but private entities enable the creation of new copies with each invocation.

Lecture 3©2004 Joel Jones 11 Symbol Tables Symbol tables are used to keep track of scope and binding information about names. The symbol table is searched every time a name is encountered in the source text. Changes occur when a new name or new information about a name is discovered. The abstract syntax tree will contain pointers to the symbol table rather than the actual names used for objects in the source text.

Lecture 3©2004 Joel Jones 12 Symbol Tables (Cont.) Each symbol table entry contains the symbol name, its category (scalar variable, array, constant, type, procedure, field name, parameter, etc.) scope number, type (a pointer to another symbol table entry), and additional, category specific fields (e.g. rank and shape for arrays) To keep symbol table records uniform, it may be convenient for some of the information about a name to be kept outside the table entry, with only a pointer to this information stored in the entry.

Lecture 3©2004 Joel Jones 13 Symbol Tables (Cont.) The symbol table may contain the keywords at the beginning if the lexical scanner searches the symbol table for each name. Alternatively, the lexical scanner can identify keywords using a separate table or by creating a separate final state for each keyword.

Lecture 3©2004 Joel Jones 14 Symbol Tables (Cont.) One of the important issues is handling static scope. A simple solution is to create a symbol table for each scope and attach it to the node in the abstract syntax tree corresponding to the scope. An alter native is to use a additional data structure to keep track of the scope. This structure would resemble a stack:

Symbol Tables (Cont.) procedure new_id(id) for index = top to scope_marker[LL - 1] by -1 if id == symbol_table[additional[index]].name then error() k = new_symbol_table_index() symbol_table[k].name = id additional[top++] = k procedure old_id(id) for index= top to 0 by -1 if id == symbol_table[additional[index]].name then return additional[index] error() procedure scope_entry () scope_marker[LL++] = top procedure scope_exit() top = scope_marker[--LL] A C A B additional 0 2 scope_marker 4 top 2 LL symbol table

Lecture 3©2004 Joel Jones 16 Symbol Tables for C++ Pair Up: Given the number of different scopes in C++, does the previous representation provide much guidance for implementing a C++ compiler? Why or why not? What problems are involved?

Lecture 3©2004 Joel Jones 17 Reading for Next Class Skim the following, paying particular attention to how to print the addresses of variables: Debugging with GDB: The GNU Source Level Debugger GDB Quick Reference Card