Docsity

Understanding Scope Rules in Programming Languages: Static vs. Dynamic, Study notes of Programming Languages

Partial preview of the text

Download Understanding Scope Rules in Programming Languages: Static vs. Dynamic and more Study notes Programming Languages in PDF only on Docsity!

CS 403

Organization of

Programming Languages

Class 9– September 20,

Scope

Def: The

(^) scope

(^) of a variable is the range

of statements over which it is visible.

Def: The

(^) nonlocal

(^) variables of a program

declared there.unit are those that are visible but not

with variables.how references to names are associatedThe scope rules of a language determine

NestingStatic Scope – Procedure

X = 3; X = 7 X = 5;

Static Scope Hierarchy

P
P
P
P
F

of the program on the preceding slide.This hierarchy represents the nesting structure

Scope Nesting in C++

for (int i = 1; i <= 50; i++){

for (int j = 1; j <= 50; j++){x = 1;int x;

cout << x << endl;

//outer x

x = j + 3;

Dynamic Scope

versus spatial).units, not their textual layout (temporalBased on calling sequences of program

execution to this point.the chain of subprogram calls that forceddeclarations by searching back throughReferences to variables are connected to

Behavior DifferencesStatic vs. Dynamic Scope:

Dynamic scope output: ?? Static scope output: ??

Exam Review

^ Topics:

(^) Compilation vs. interpretation (1.6)

 (^) Specifying language syntax; BNF (2.1)

 (^) Binding time (3.1)

 (3.2)^ Storage management: Static, stack, heap

 (^) Pointers and heap management (3.2, 7.7)

 (^) Scope rules (3.3.1, 3.3.2)

guaranteed to be exhaustive)Some Question Types (not

some concept, etc. Short answer, true/false (with justification), explainneither.contains a dangling reference, memory leak, both or Indicate whether a particular segment of codeleak. Write code with a dangling reference or memoryin a program’s execution Draw pictures of the run-time stack at various pointsprogram subset Write the assembly pseudocode for a particularconsistent with a particular BNF specification Identify whether a particular program subset is Write the BNF for a particular language subset