Static & Dynamic Scoping | Compiler Design - Computer Science Engineering (CSE) PDF Download

Introduction

The scope of a variable x in the region of the program in which the use of x refers to its declaration. One of the basic reasons for scoping is to keep variables in different parts of the program distinct from one another. Since there are only a small number of short variable names, and programmers share habits about naming of variables (e.g., I for an array index), in any program of moderate size the same variable name will be used in multiple different scopes.

Scoping is generally divided into two classes: 

  • Static Scoping 
  • Dynamic Scoping

Static Scoping


  • Static scoping is also called lexical scoping. In this scoping, a variable always refers to its top-level environment. This is a property of the program text and is unrelated to the run-time call stack. Static scoping also makes it much easier to make a modular code as a programmer can figure out the scope just by looking at the code. In contrast, dynamic scope requires the programmer to anticipate all possible dynamic contexts.
  • In most programming languages including C, C++, and Java, variables are always statically (or lexically) scoped i.e., binding of a variable can be determined by program text and is independent of the run-time function call stack. 
  • For example, the output for the below program is 10, i.e., the value returned by f() is not dependent on who is calling it (Like g() calls it and has a x with value 20). f() always returns the value of global variable x.

C

// A C program to demonstrate static scoping.

#include<stdio.h>

int x = 10;

 

// Called by g()

int f()

{

   return x;

}

 

// g() has its own variable

// named as x and calls f()

int g()

{

   int x = 20;

   return f();

}

 

int main()

{

  printf("%d", g());

  printf("\n");

  return 0;

}

Output :

10

To sum up, in static scoping the compiler first searches in the current block, then in global variables, then in successively smaller scopes.

Dynamic Scoping


  • With dynamic scope, a global identifier refers to the identifier associated with the most recent environment and is uncommon in modern languages. In technical terms, this means that each identifier has a global stack of bindings and the occurrence of an identifier is searched in the most recent binding.
  • In simpler terms, in dynamic scoping, the compiler first searches the current block and then successively all the calling functions.

C

// Since dynamic scoping is very uncommon in

// the familiar languages, we consider the

// following pseudo code as our example. It

// prints 20 in a language that uses dynamic

// scoping.  

 

int x = 10;

 

// Called by g()

int f()

{

   return x;

}

 

// g() has its own variable

// named as x and calls f()

int g()

{

   int x = 20;

   return f();

}

 

main()

{

  printf(g());

}

Output in a language that uses Dynamic Scoping : 

20

Static Vs Dynamic Scoping 


  • In most programming languages static scoping is dominant. This is simply because in static scoping it’s easy to reason about and understand just by looking at code. We can see what variables are in the scope just by looking at the text in the editor.
  • Dynamic scoping does not care about how the code is written, but instead how it executes. Each time a new function is executed, a new scope is pushed onto the stack.
  • Perl supports both dynamic and static scoping. Perl’s keyword “my” defines a statically scoped local variable, while the keyword “local” defines a dynamically scoped local variable.

Perl

# A perl code to demonstrate dynamic scoping

$x = 10;

sub f

{

   return $x;

}

sub g

{

   # Since local is used, x uses

   # dynamic scoping.

   local $x = 20;

 

   return f();

}

print g()."\n";

Output:

20

The document Static & Dynamic Scoping | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Static & Dynamic Scoping - Compiler Design - Computer Science Engineering (CSE)

1. What is the difference between static and dynamic scoping in computer science engineering?
Ans. Static scoping is a scoping mechanism where the scope of a variable is determined at compile-time and remains the same throughout the program. On the other hand, dynamic scoping is a scoping mechanism where the scope of a variable is determined at runtime and can change during the execution of the program.
2. How does static scoping work in computer science engineering?
Ans. In static scoping, the scope of a variable is determined based on the program's structure. When a variable is referenced, the compiler looks for the variable in the nearest enclosing scope. If the variable is not found, the compiler continues to look in the outer scopes until the variable is found or an error is thrown.
3. What are the advantages of static scoping in computer science engineering?
Ans. Static scoping offers several advantages. It allows for better compile-time error detection as the compiler can verify the variable's existence and usage at compile-time. It also provides better code readability as the scope of variables is explicitly defined in the program structure. Furthermore, static scoping is generally more efficient as it avoids the runtime overhead of dynamically determining variable scopes.
4. How does dynamic scoping work in computer science engineering?
Ans. In dynamic scoping, the scope of a variable is determined based on the program's execution flow. When a variable is referenced, the interpreter or runtime environment looks for the variable in the most recent scope that called the current function. If the variable is not found, the search continues in the calling function's scope until the variable is found or an error occurs.
5. What are the advantages of dynamic scoping in computer science engineering?
Ans. Dynamic scoping offers flexibility in terms of variable access as it allows variables to be accessed from different scopes based on the program's execution flow. This can be useful in certain programming scenarios where the context of variable access changes dynamically. However, dynamic scoping can make code harder to reason about and debug, as the variable's scope is not explicitly defined in the program structure.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Objective type Questions

,

Exam

,

Static & Dynamic Scoping | Compiler Design - Computer Science Engineering (CSE)

,

MCQs

,

Summary

,

Static & Dynamic Scoping | Compiler Design - Computer Science Engineering (CSE)

,

Viva Questions

,

Previous Year Questions with Solutions

,

practice quizzes

,

shortcuts and tricks

,

Important questions

,

Static & Dynamic Scoping | Compiler Design - Computer Science Engineering (CSE)

,

mock tests for examination

,

pdf

,

study material

,

Extra Questions

,

video lectures

,

Semester Notes

,

past year papers

,

Free

,

ppt

,

Sample Paper

;