Bird
Raised Fist0
Compiler Designknowledge~10 mins

Scope resolution in Compiler Design - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Scope resolution
Start: Identifier Used
↓
Check Local Scope
↓
Use Local Value
No↓
Check Enclosing Scopes
↓
Use Enclosing Value
No↓
Check Global Scope
↓
Use Global Value
No↓
Error: Identifier Not Found
When an identifier is used, the compiler checks scopes from local to global to find its definition, using the closest one found.
Execution Sample
Compiler Design
int x = 5;
void func() {
  int x = 10;
  print(x);
}
This code declares a global x and a local x inside func; printing x inside func uses the local x.
Analysis Table
StepScope CheckedIdentifier Found?Value UsedAction
1Local scope of funcYes10Use local x = 10
2Enclosing scopesN/AN/ANot checked because found locally
3Global scopeN/AN/ANot checked because found locally
4EndN/AN/AIdentifier resolved successfully
💡 Identifier 'x' found in local scope at step 1, so no further scopes checked.
State Tracker
VariableGlobal ScopeLocal Scope in funcValue Used
x51010
Key Insights - 2 Insights
Why does the local variable x hide the global x inside the function?
Because the compiler finds the local x first (see execution_table step 1), it uses that value and stops searching further.
What happens if an identifier is not found in any scope?
The compiler raises an error after checking all scopes without finding the identifier (not shown in this example but implied after step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the identifier 'x' found?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Check the 'Identifier Found?' column in execution_table rows.
According to variable_tracker, what value of x is used inside the function?
A5
B10
CUndefined
D0
💡 Hint
Look at the 'Value Used' column for variable x in variable_tracker.
If the local x was removed, what would happen in the scope resolution?
AGlobal x would be used
BError would occur immediately
CEnclosing scope would be checked
DLocal x would still be used
💡 Hint
Refer to concept_flow showing the order of scope checking.
Concept Snapshot
Scope resolution means finding where an identifier is defined.
The compiler checks scopes from local to global.
The closest definition is used.
If not found, an error occurs.
Local variables hide outer ones with the same name.
Full Transcript
Scope resolution is the process the compiler uses to find the meaning of an identifier like a variable or function name. When the compiler sees an identifier, it first looks in the local scope, which is the current function or block. If it finds the identifier there, it uses that value and stops searching. If not found locally, it checks enclosing scopes, such as outer functions or blocks. If still not found, it checks the global scope. If the identifier is not found in any scope, the compiler reports an error. For example, if a variable x is declared globally as 5 and locally inside a function as 10, the function uses the local x with value 10. This is because the local scope hides the global one. This step-by-step checking ensures the correct value is used depending on where the identifier is declared.

Practice

(1/5)
1. What is the main purpose of the :: operator in scope resolution?
easy
A. To specify which scope a variable or function belongs to
B. To declare a new variable
C. To perform arithmetic operations
D. To create a loop

Solution

  1. Step 1: Understand the role of scope resolution

    The :: operator is used to access variables or functions that belong to a specific scope, especially when multiple scopes have the same name.
  2. Step 2: Identify the correct use of ::

    It helps avoid confusion by explicitly stating which scope to use, such as a global variable or a class member.
  3. Final Answer:

    To specify which scope a variable or function belongs to -> Option A
  4. Quick Check:

    Scope resolution operator = specify scope [OK]
Hint: Remember :: means 'from this scope' [OK]
Common Mistakes:
  • Confusing :: with assignment operator
  • Thinking it declares variables
  • Using it for arithmetic
2. Which of the following is the correct syntax to access a global variable named x inside a function when a local variable with the same name exists?
easy
A. global x
B. ::x
C. global::x
D. ::x

Solution

  1. Step 1: Identify the correct scope resolution syntax

    In many languages like C++, the global scope resolution operator is :: placed before the variable name to access the global variable.
  2. Step 2: Choose the syntax that correctly uses the operator

    The correct syntax is ::x to access the global variable x inside a function where a local x exists.
  3. Final Answer:

    ::x -> Option B
  4. Quick Check:

    Global variable access = ::x [OK]
Hint: Use :: before variable for global access [OK]
Common Mistakes:
  • Using 'global x' which is not scope resolution
  • Writing 'global::x' which is invalid syntax
  • Omitting the operator
3. Consider the following code snippet:
int x = 10;
void func() {
  int x = 20;
  print(::x);
}

What will be printed when func() is called?
medium
A. 20
B. 0
C. 10
D. Error

Solution

  1. Step 1: Identify the scopes of variable x

    There is a global x with value 10 and a local x inside func() with value 20.
  2. Step 2: Understand ::x usage in print(::x)

    The ::x accesses the global x, ignoring the local one.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Scope resolution prints global x = 10 [OK]
Hint: <::x> always means global variable [OK]
Common Mistakes:
  • Printing local x instead of global
  • Confusing syntax causing error
  • Assuming default prints local variable
4. Identify the error in the following code snippet:
int x = 5;
void func() {
  int x = 10;
  print(x::);
}
medium
A. Incorrect use of scope resolution operator
B. Variable redeclaration error
C. Missing semicolon
D. No error

Solution

  1. Step 1: Analyze the syntax of scope resolution operator

    The scope resolution operator :: must precede the variable or function name, not follow it.
  2. Step 2: Identify the incorrect usage in print(x::);

    Here, x:: is invalid syntax; it should be ::x to access global scope or just x for local.
  3. Final Answer:

    Incorrect use of scope resolution operator -> Option A
  4. Quick Check:

    Operator must be before variable [OK]
Hint: Scope operator goes before name, not after [OK]
Common Mistakes:
  • Placing :: after variable name
  • Confusing with member access operator
  • Ignoring syntax rules
5. Given two nested namespaces:
namespace Outer {
  int value = 100;
  namespace Inner {
    int value = 200;
    int getValue() {
      return Outer::value + value;
    }
  }
}

What will Outer::Inner::getValue() return?
hard
A. Compilation error
B. 200
C. 100
D. 300

Solution

  1. Step 1: Identify values in each namespace

    Outer::value is 100, and Inner::value is 200.
  2. Step 2: Calculate the return value of getValue()

    The function returns Outer::value + value, where value refers to Inner::value. So, 100 + 200 = 300.
  3. Final Answer:

    300 -> Option D
  4. Quick Check:

    Outer + Inner values = 300 [OK]
Hint: Add outer and inner values using scope operator [OK]
Common Mistakes:
  • Assuming only inner value is returned
  • Confusing namespaces and scopes
  • Expecting compilation error due to nested namespaces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard