CS307: Principles of Programming Languages LECTURE 12: NAMES, SCOPES, AND BINDINGS II LECTURE OUTLINE • INTRODUCTION • BINDINGS • LIFETIME AND STORAGE MANAGEMENT • SCOPE RULES • STATIC SCOPING • DYNAMIC SCOPING • STATIC VS DYNAMIC SCOPING • DEEP VS SHALLOW BINDING • POLYMORPHISM, DYNAMIC BINDING, GENERIC PROGRAMMING • METHOD MATCHING VS BINDING • BINDING OF REFERENCE ENVIRONMENTS • SEPARATE COMPILATION 2 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson SCOPE RULES • BINDING SCOPE IS THE TEXTUAL REGION OF THE PROGRAM IN WHICH A BINDING IS ACTIVE • A SCOPE IS A PROGRAM SECTION OF MAXIMAL SIZE WHERE • NO BINDINGS CHANGE • MINIMALLY: NO RE-DECLARATIONS ARE PERMITTED • SCOPING RULE EXAMPLE 1: DECLARATION BEFORE USE • CAN A NAME BE USED BEFORE IT IS DECLARED? • JAVA LOCAL VARS: NO • JAVA CLASS PROPERTIES AND METHODS: YES • THE SCOPE OF A BINDING IS DETERMINED STATICALLY OR DYNAMICALLY 3 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson SCOPING RULES • SCOPING RULE EXAMPLE 2: TWO USES OF A GIVEN NAME. DO THEY REFER TO THE SAME BINDING? a = 1 ... def f(): a = 2 b = a SCOPING RULES DETERMINE WHETHER OR NOT THIS IS THE CASE 4 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson SCOPE RULES • KEY IDEA: IN STATIC SCOPE RULES -> BINDINGS DEFINED BY THE PHYSICAL (LEXICAL) STRUCTURE OF THE PROGRAM • STATIC SCOPING (ALSO CALLED LEXICAL SCOPING) EXAMPLES: • ONE BIG SCOPE: (BASIC) • SCOPE OF A FUNCTION (VARIABLES LIVE THROUGH A FUNCTION EXECUTION) • BLOCK SCOPE • NESTED SUBROUTINES • VARIABLES ACTIVE IN ONE OR MORE SCOPES • CLOSEST NESTED SCOPE RULE APPLIES • LEXICAL/STATIC SCOPING WAS USED BY ALGOL 5 • PICKED UP IN MOST LANGUAGES SINCE: (PASCAL, C, JAVA, ETC) CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson SCOPE RULES • IN MOST LANGUAGES WITH SUBROUTINES: • OPEN A NEW SCOPE ON SUBROUTINE ENTRY: • CREATE BINDINGS FOR NEW LOCAL VARIABLES • DEACTIVATE BINDINGS FOR GLOBAL VARIABLES THAT ARE RE-DECLARED • THESE [GLOBAL] VARIABLES ARE SAID TO HAVE A "HOLE" IN THEIR SCOPE • ON SUBROUTINE EXIT: • DESTROY BINDINGS FOR LOCAL VARIABLES • REACTIVATE BINDINGS FOR DEACTIVATED GLOBAL VARIABLES 6 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson SCOPE RULES • ELABORATION • PROCESS OF CREATING BINDINGS WHEN ENTERING A SCOPE • STORAGE MAY BE ALLOCATED • TASKS STARTED • EXCEPTIONS MAY BE PROPAGATED AS A RESULT OF THE ELABORATION OF DECLARATIONS 7 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson STATIC SCOPING • STATIC (LEXICAL) SCOPE RULES • A SCOPE IS DEFINED IN TERMS OF THE PHYSICAL (LEXICAL) STRUCTURE OF THE PROGRAM: • SCOPES CAN BE DETERMINED BY COMPILER • ALL BINDINGS FOR IDENTIFIERS CAN BE RESOLVED BY EXAMINING PROGRAM • TYPICALLY, CHOOSE THE MOST RECENT, ACTIVE BINDING MADE AT COMPILE TIME • MOST COMPILED LANGUAGES, C, PASCAL, JAVA AND PYTHON INCLUDED, EMPLOY STATIC SCOPE RULES 8 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson STATIC SCOPING • NESTED BLOCKS: • CLASSICAL EXAMPLE OF STATIC SCOPE RULES: “MOST CLOSELY NESTED RULE”: • AN IDENTIFIER IS • KNOWN IN THE SCOPE IN WHICH IT’S DECLARED AND IN EACH ENCLOSED SCOPE • UNLESS RE-DECLARED IN AN ENCLOSED SCOPE (ORIGINAL IDENTIFIER IS HIDDEN) • TO RESOLVE A REFERENCE TO AN IDENTIFIER: EXAMINE LOCAL SCOPE AND STATICALLY ENCLOSING SCOPES UNTIL A BINDING IS FOUND • CLASSES (IN ABSTRACTION AND OO LANGUAGES) HAVE EVEN MORE SOPHISTICATED (STATIC) SCOPE RULES 9 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson ILLUSTRATION Pascal: CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione 10 (SUNY Korea) and Pearson STATIC SCOPING • SIMPLEST WAY TO FIND FRAMES OF SURROUNDING SCOPES • MAINTAIN A STATIC LINK IN EACH FRAME • POINTS TO THE ‘PARENT’ FRAME: THE FRAME OF MOST RECENT INVOCATION OF A LEXICALLY SURROUNDING SUBROUTINE • SUBROUTINE DECLARED AT OUTERMOST NESTING WILL HAVE A NULL STATIC LINK TO ‘PARENT’ AT RUN TIME • SUBROUTINE NESTED K LEVELS DEEP • FRAME’S STATIC ‘PARENT’ LINK AND THOSE OF GRANDPARENT, ETC. FORM A STATIC CHAIN OF LENGTH K AT RUN TIME 11 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson ILLUSTRATION Nesting of subroutines: During run time (with links) CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione 12 (SUNY Korea) and Pearson STATIC SCOPING • DECLARATION ORDER: • SOME EARLY LANGUAGES, (ALGOL 60, LISP, ETC), REQUIRED ALL DECLARATIONS APPEAR AT BEGINNING OF SCOPE • PASCAL REQUIREMENT: NAMES MUST BE DECLARED BEFORE USED • STILL USES A WHOLE-BLOCK SCOPE (ALSO IMPLEMENTED IN C#) • EXAMPLE (C#): DEFINING VARIABLE IN A BLOCK MAKES EVERY EXTERNAL DECLARATION HIDDEN • ➔ THE M=N ASSIGNMENT GENERATES A COMPILING ERROR! 13 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson DYNAMIC SCOPING • DYNAMIC SCOPE RULES • BINDINGS DEPEND ON CURRENT STATE OF PROGRAM EXECUTION • CANNOT BE RESOLVED BY EXAMINING PROGRAM SOURCE SINCE THEY’RE DEPENDENT ON CALLING SEQUENCES • BINDING MIGHT DEPEND ON HOW A FUNCTION IS CALLED • TO RESOLVE A REFERENCE: USE THE MOST RECENT, ACTIVE BINDING MADE AT RUN TIME 14 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson DYNAMIC SCOPING OF BINDINGS EXAMPLE: var total = 0 def add(): total += 1 def myfunc(): var total = 0 add() add() myfunc() print total VERY EASY TO IMPLEMENT (STACK OF NAMES) OFTEN HARD TO MAKE EFFICIENT CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione 15 (SUNY Korea) and Pearson SCOPE RULES • DYNAMIC SCOPE RULES USUALLY IN INTERPRETED LANGUAGES • EXAMPLES: LISP, PERL, RUBY, C#, THE SHELL LANGUAGES BASH, DASH, AND POWERSHELL • SUCH LANGUAGES DO NOT ALWAYS HAVE COMPILE TIME TYPE CHECKING • TYPE DETERMINATION NOT ALWAYS POSSIBLE WITH DYNAMIC SCOPE RULES 16 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson BINDING OF REFERENCE ENVIRONMENTS • REFERENCE ENVIRONMENT • ALL BINDINGS ACTIVE AT A GIVEN TIME • TO TAKE A REFERENCE TO A FUNCTION • NEED TO DECIDE WHICH REFERENCE ENVIRONMENT TO USE!! • DEEP BINDING BINDS ENVIRONMENT AT TIME PROCEDURE IS PASSED AS AN ARGUMENT • SHALLOW BINDING BINDS ENVIRONMENT AT THE TIME THE PROCEDURE IS ACTUALLY CALLED 17 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson EXAMPLE: STATIC VS. DYNAMIC SHALLOW VS. DYNAMIC DEEP CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione 18 (SUNY Korea) and Pearson BINDING OF REFERENCE ENVIRONMENTS x: integer := 1 y: integer := 2 -DYNAMIC SCOPING WITH DEEP BINDING: -WHEN ADD IS PASSED INTO SECOND procedure add ENVIRONMENT OF ADD IS x = 1, y = 3 x := x + y -THE x IS THE GLOBAL x SO IT WRITES 4 INTO THE GLOBAL x, procedure second(P:procedure) -GLOBAL x IS THE ONE PICKED UP x:integer := 2 BY THE write_integer P() -SHALLOW BINDING: procedure first -JUST TRAVERSE UP CALL CHAIN y:integer := 3 -IT FINDS THE NEAREST VARIABLE second(add) THAT CORRESPONS TO THE NAME (THE LOCAL x) first() -INCREMENTS LOCAL x SO write_integer(x) ANSWER IS 1 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione 19 (SUNY Korea) and Pearson SCOPE RULES EXAMPLE: STATIC VS. DYNAMIC program scopes (input, output ); • PROGRAM OUTPUT DEPENDS var a : integer; • ON SCOPE RULES procedure first(); begin • IN THE CASE OF DYNAMIC SCOPING, A VALUE READ AT RUN TIME a := 1; end; • STATIC SCOPING: THIS PROGRAM PRINTS A 1. procedure second(); • DYNAMIC SCOPING: var a : integer; • OUTPUT DEPENDS ON VALUE READ BY begin READ_INTEGER() AT RUN TIME: first(); • INPUT IS POSITIVE: THE PROGRAM PRINTS A 2 end; • OTHERWISE: PROGRAM PRINTS A 1 begin • THE ASSIGNMENT TO THE VARIABLE A IN FIRST() a := 2; REFERS EITHER if read_integer() > 0 second(); • TO GLOBAL VARIABLE DECLARED IN PROGRAM SCOPES OR else • TO THE LOCAL VARIABLE DECLARED INSIDE first(); SECOND() 20 write(a); end. CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson SCOPE RULES EXAMPLE: STATIC VS. DYNAMIC • STATIC SCOPE RULES REQUIRE REFERENCE TO RESOLVE TO THE MOST RECENT, COMPILE-TIME BINDING • GLOBAL VARIABLE A • DYNAMIC SCOPE RULES REQUIRE CHOICE OF THE MOST RECENT, ACTIVE BINDING AT RUN TIME • MOST COMMON USE OF DYNAMIC SCOPE RULES MAY BE TO PROVIDE IMPLICIT PARAMETERS TO SUBROUTINES • THIS IS CONSIDERED BAD PROGRAMMING PRACTICE TODAY (EXCEPT IN FUNCTIONAL LANGUAGES) • ALTERNATIVE MECHANISMS EXIST • STATIC VARIABLES THAT CAN BE MODIFIED BY AUXILIARY ROUTINES • DEFAULT AND OPTIONAL PARAMETERS 21 CS307 : Principles of Programming Languages - (c) Paul Fodor (CS Stony Brook), Tony Mione (SUNY Korea) and Pearson SCOPE RULES EXAMPLE: STATIC VS. DYNAMIC • AT RUN TIME: CREATE A BINDING FOR A WHEN ENTER MAIN PROGRAM IS ENTERED • CREATE ANOTHER BINDING FOR A WHEN CODE ENTERS SECOND() • MOST RECENT, ACTIVE BINDING WHEN FIRST() IS EXECUTED • SO VARIABLE LOCAL TO SECOND() IS MODIFIED, NOT GLOBAL VARIABLE • BUT, WRITE() PRINTS GLOBAL VARIABLE
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages38 Page
-
File Size-