0
0
Compiler Designknowledge~10 mins

Activation records and call stack in Compiler Design - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Activation records and call stack
Function Call
Create Activation Record
Push Record on Call Stack
Execute Function Body
Function Returns
Pop Activation Record from Stack
Resume Caller Execution
When a function is called, a new activation record is created and pushed onto the call stack. The function runs, then its record is popped off when it returns, resuming the caller.
Execution Sample
Compiler Design
def main():
    print('Start')
    greet('Alice')
    print('End')

def greet(name):
    print('Hello,', name)
This code calls greet from main, showing how activation records are created and removed on the call stack.
Analysis Table
StepActionCall Stack StateCurrent FunctionOutput
1main() called[main]main
2print('Start')[main]mainStart
3greet('Alice') called[main, greet]greet
4print('Hello, Alice')[main, greet]greetHello, Alice
5greet returns[main]main
6print('End')[main]mainEnd
7main returns[]none
💡 main function finishes, call stack is empty, program ends
State Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
Call Stack[][main][main, greet][main][]
Current Functionnonemaingreetmainnone
Key Insights - 3 Insights
Why does the call stack grow when a function is called?
Each function call creates a new activation record pushed onto the stack to keep track of that call's data and return point, as shown in steps 1 and 3 of the execution table.
What happens to the activation record when a function finishes?
The activation record is popped off the call stack to free space and return control to the caller, as seen in steps 5 and 7.
Why is the call stack empty at the end?
Because all functions have returned and their activation records removed, leaving no active calls, shown in the final step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What is the call stack state?
A[]
B[main]
C[main, greet]
D[greet]
💡 Hint
Check the 'Call Stack State' column at step 3 in the execution table.
At which step does the function greet return and its activation record get removed?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for the 'greet returns' action in the execution table.
If main called another function before printing 'End', how would the call stack change at step 6?
AIt would have two activation records
BIt would be empty
CIt would have only main
DIt would have three activation records
💡 Hint
Refer to the call stack growth pattern in the variable tracker and execution table.
Concept Snapshot
Activation records store function call info.
Each call pushes a record on the call stack.
Function runs using its record.
Return pops the record off.
Call stack tracks active calls.
Empty stack means program end.
Full Transcript
When a function is called, the program creates an activation record that holds information like parameters, local variables, and return address. This record is pushed onto the call stack, which keeps track of all active function calls. The function executes using its activation record. When the function finishes, its activation record is popped off the stack, and control returns to the caller. This process repeats for every function call. The call stack grows with each call and shrinks as functions return. When the main function returns, the call stack is empty, and the program ends.