Semantic Analysis · Compiler Design

Scope

1. What Scope Means

Scope = program region where a binding (name → entity) is visible. Controls name resolution, lifetime, protection against clashes.

2. Scope Kinds

Kind Rule Example
Lexical (static) bound by source nesting C, Java: inner {int x} shadows outer
Dynamic bound by call chain at run time Lisp, shell: free var = most recent caller binding
Block {...} delimited C locals
Function whole function JS var, labels
File/global translation unit C static vs extern
Class/namespace qualified A::x, pkg.y

Most compiled languages use lexical scope (decidable at compile time).

3. Implementation (access)

Lexical nonlocal access via access links / display: level difference d → follow d links from fp. Example Pascal nested f→g→h using x from f: h follows 2 access links.

4. Common Exam Traps

  • Shadowing: inner decl hides outer (legal).
  • Hole: same-scope redeclare = error.
  • Dynamic vs lexical: same program prints different values — trace call stack vs source nesting.

5. Exam Points

  • Define scope + kinds with examples — 5 marks, very frequent.
  • Lexical vs dynamic with trace — 5-7 marks, frequent.
  • Access links for nonlocal use — 3-5 marks, frequent.
Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.

← All Compiler Design modules