Compiler Design
SDT & Runtime Environment
Two small but steady topics: attribute evaluation (synthesized vs inherited, S- vs L-attributed, what a given SDT prints) and the runtime environment (activation records, scope, parameter passing, call counts).
GATE CSE syntax-directed translation and runtime environments — attributes, S-attributed and L-attributed definitions, SDT output order, activation records, scoping, parameter passing, with sample questions, flashcards and drills.
Learning outcomes
- Distinguish synthesized and inherited attributes and classify S-attributed vs L-attributed definitions
- Evaluate a syntax-directed definition on a parse tree and predict the output of a SDT with print actions
- Describe run-time storage organisation and the fields of an activation record
- Solve static vs dynamic scoping, access-link and parameter-passing output questions, and count activations
Syntax-directed definitions and attributes
- A syntax-directed definition (SDD) attaches ATTRIBUTES to grammar symbols and SEMANTIC RULES to productions. A syntax-directed translation scheme (SDT) embeds program fragments (semantic actions) inside the right-hand sides to show WHEN they run.
- A SYNTHESIZED attribute of a non-terminal A at a parse-tree node is computed from the attributes of its CHILDREN (and of the node itself). An INHERITED attribute of a symbol is computed from attributes of its PARENT and SIBLINGS.
- Terminals have only synthesized attributes (e.g. digit.lexval supplied by the scanner). The start symbol has no inherited attributes.
- An annotated parse tree shows the attribute values at each node. Evaluating an SDD means finding an order for the semantic rules consistent with the DEPENDENCY GRAPH (edges from the attribute used to the attribute computed); a topological order exists iff the graph is acyclic.
- Typical uses: expression evaluation (val), building syntax trees, type checking (type), generating three-address code (code, place, begin/end labels), and storing declaration types in the symbol table (inherited type).
- Reading the annotated tree: every dependency-graph edge points from a child to its parent, so any post-order (bottom-up) evaluation works. For 3 * 5 + 4 n the tree has 9 internal nodes (L, two E, three T, three F), which equals the number of reductions and of semantic actions in an LR evaluation.
- Attribute kinds: lexval comes from the scanner for terminals; val, type, place and code belong to non-terminals. Rules that only copy a value (T.val = F.val) are COPY RULES; in an LR parser they need no real work if the value-stack slot is reused. Rules with side effects (print, symbol-table update) make the evaluation order matter.
- Left-recursive grammars are convenient for synthesized attributes and bottom-up evaluation; after removing left recursion for top-down parsing, left-to-right dependencies are handled with inherited attributes.
| Production | Semantic rule |
|---|---|
| L → E n | print(E.val) |
| E → E1 + T | E.val = E1.val + T.val |
| E → T | E.val = T.val |
| T → T1 * F | T.val = T1.val × F.val |
| T → F | T.val = F.val |
| F → ( E ) | F.val = E.val |
| F → digit | F.val = digit.lexval |
Worked example
Problem. Evaluate the SDD above on the input 3 * 5 + 4 n.
Solution. Leaves: F.val = 3, 5, 4. T.val for 3 * 5 = 3 × 5 = 15. E.val for 15 + 4 = 19. The root L prints 19. All attributes are synthesized, so the SDD is S-attributed and can be evaluated by an LR parser with a value stack.
S-attributed and L-attributed definitions
- S-ATTRIBUTED: only synthesized attributes. It can be evaluated in one bottom-up pass; with an LR parser the actions are performed at REDUCE time (each action is placed at the right end of the production — a postfix SDT).
- L-ATTRIBUTED: each inherited attribute of a symbol Xj on the RHS of A → X1 … Xn depends only on (a) inherited attributes of the parent A and (b) attributes of the symbols X1 … Xj−1 to the LEFT of Xj. No dependence on right siblings.
- Every S-attributed definition is L-attributed. L-attributed definitions can be evaluated in a single depth-first, left-to-right traversal of the parse tree, so they work with top-down (LL) parsers and also with bottom-up parsers using marker non-terminals.
- Rules for SDT with actions: an action that uses an inherited attribute of a symbol must be placed BEFORE that symbol; an action that uses a synthesized attribute of A must be placed at the END of the production for A. An action may use only attributes already computed.
- Declaration example: D → T L {L.inh = T.type}; T → int {T.type = integer}; L → L1 , id {L1.inh = L.inh; addType(id.entry, L.inh)}; L → id {addType(id.entry, L.inh)}. L.inh is INHERITED (it flows from T to the list), so the SDD is L-attributed but not S-attributed.
- Order of action execution: in a bottom-up (postfix) SDT the action runs at reduction time, i.e. in the order of the rightmost derivation reversed. In a top-down SDT the actions run when the traversal reaches their position in the RHS — i.e. in the order of a depth-first, left-to-right walk of the parse tree.
- L-attributed test: for every production A → X1 … Xn and every inherited attribute of Xj, list the attributes its rule uses. Each must be an inherited attribute of A, any attribute of X1 … Xj−1, or an attribute of Xj itself (without a cycle). One use of Xj+1 … Xn fails the test.
- Parser compatibility: an L-attributed SDD on an LL(1) grammar can be evaluated during predictive parsing; on an LR grammar only S-attributed rules are natural, and inherited values need MARKER non-terminals (A → X M Y ; M → ε {copy X.s}) whose actions read values lower on the stack.
- Postfix SDT means every action sits at the very end of its production; such a scheme is S-attributed and every action runs exactly at a reduction.
- Cycles: an SDD whose dependency graph has a cycle cannot be evaluated; checking this in general is expensive (exponential), which is why practical systems restrict to S- or L-attributed forms.
| Type | Attributes allowed | Evaluation | Parsers |
|---|---|---|---|
| S-attributed | synthesized only | bottom-up, at reduction | LR (and LL) |
| L-attributed | synthesized + inherited (from parent / left siblings) | one left-to-right DFS | LL, and LR with markers |
| General SDD | any acyclic dependencies | topological order of dependency graph | build tree, then evaluate |
Worked example
Problem. Is the rule A → B C {B.i = A.i; C.i = B.s} L-attributed? What about A → B C {B.i = C.s}?
Solution. First: B.i depends on the parent’s inherited A.i and C.i depends on the left sibling’s synthesized B.s — both allowed, so it is L-attributed. Second: B.i depends on C.s, a right sibling, so it is NOT L-attributed.
Worked example — SDT output order
- Infix to postfix: E → E + T {print “+”} | T ; T → T * F {print “*”} | F ; F → num {print num.val}. The actions are at the right end, so a bottom-up parser performs them at reduction, giving a postfix traversal of the tree.
- For the input 2 + 3 * 4 the output is 2 3 4 * + because the T → T * F reduction (printing *) happens before E → E + T (printing +). A top-down parse of the equivalent right-recursive grammar produces the same postfix order if the actions are placed after the recursive call.
- Rule of thumb for “what does this SDT print?”: write the parse tree; for each node execute the actions in the order in which a post-order (for actions at the end) or pre-order (for actions at the front) traversal reaches them.
- Example with two actions: S → a S {print 1} | b {print 2}. For aab the reductions are: b → S (prints 2), then a S → S twice (prints 1, 1), so the output is 2 1 1.
- If the action is placed at the FRONT, S → {print 1} a S | {print 2} b would output 1 1 2 in a top-down traversal, but with a bottom-up parser such mid-rule actions need marker non-terminals to run before the symbols are reduced.
- Counting: for postfix SDTs the number of actions executed equals the number of reductions, i.e. the number of internal nodes in the parse tree.
| Input | Action order (reductions) | Output |
|---|---|---|
| 2 + 3 | F→2 print 2, F→3 print 3, E→E+T print + | 2 3 + |
| 2 + 3 * 4 | prints 2, 3, 4, then T→T*F prints *, then E→E+T prints + | 2 3 4 * + |
| 2 * 3 + 4 | prints 2, 3, * then 4, then + | 2 3 * 4 + |
Worked example
Problem. What is printed for the input a a b by S → a S {print 1} | b {print 2} using a bottom-up parser?
Solution. Reductions in order: b → S (prints 2), a S → S (prints 1), a S → S (prints 1). Output: 211.
Inherited attributes after left-recursion removal
- Removing left recursion turns E → E + T | T into E → T E′ ; E′ → + T E′ | ε. The tree now leans right, but the VALUE must still be left-associative, so the partial result is carried DOWN as an inherited attribute E′.i and the final result is returned UP as a synthesized attribute E′.s.
- SDT: E → T { E′.i = T.val } E′ { E.val = E′.s } ; E′ → + T { E′1.i = E′.i + T.val } E′1 { E′.s = E′1.s } ; E′ → ε { E′.s = E′.i }. This is L-attributed: E′.i uses the parent’s inherited value and the left sibling T.val.
- Placement rule: an action that computes an inherited attribute of a symbol sits immediately BEFORE that symbol; an action that sets a synthesized attribute of the head sits at the END of the production.
- Trace for 2 + 3 + 4: E′.i = 2, then 2 + 3 = 5, then 5 + 4 = 9; the ε-production returns E′.s = 9 and the value is copied back up to E.val. Counting the actions: one per E and E′ node.
| Step | Node | Inherited i | Synthesized s |
|---|---|---|---|
| 1 | E′ (first) | 2 (= T.val) | 9 (copied up) |
| 2 | E′ after + 3 | 2 + 3 = 5 | 9 (copied up) |
| 3 | E′ after + 4 | 5 + 4 = 9 | 9 (copied up) |
| 4 | E′ → ε | 9 | s = i = 9 |
Worked example
Problem. Evaluate 9 − 4 − 3 with the same scheme (E′ → − T { E′1.i = E′.i − T.val } E′1 …).
Solution. E′.i starts at 9, becomes 9 − 4 = 5, then 5 − 3 = 2, and the ε-production returns 2, which is the correct left-associative value (9 − 4) − 3. Evaluating only synthesized attributes on the right-leaning tree would give 9 − (4 − 3) = 8, which is wrong.
Run-time storage organisation and activation records
- A running program has four memory areas: CODE (static size), STATIC data (globals, constants), STACK (activation records, grows and shrinks with calls), and HEAP (dynamic allocation: malloc/new, managed by free/garbage collection). Stack and heap grow towards each other.
- Each procedure call creates an ACTIVATION RECORD (frame). Typical fields: actual parameters, return value, control link (dynamic link: caller’s frame), access link (static link: enclosing scope’s frame), saved machine status (return address, saved registers), local data, temporaries.
- Procedure calls are properly nested (LIFO), so a stack suits them: on a call push a frame; on return pop it. Static allocation cannot support recursion because every activation of the same procedure needs its own copy of the locals.
- Activation tree: one node per activation; children are the calls made, in order. The control stack holds the path from the root to the current activation. The maximum stack depth equals the depth of the activation tree (the longest root-to-node path).
- For the recursive Fibonacci, calls C(n) = C(n−1) + C(n−2) + 1 with C(0) = C(1) = 1, so fib(4) makes 9 activations; the deepest chain is fib(4) → fib(3) → fib(2) → fib(1), i.e. 4 frames on the stack at once (excluding main).
- Heap storage: allocated and freed in any order; problems are dangling references (use after free) and memory leaks. Garbage collection (mark-and-sweep, reference counting) reclaims unreachable objects. Returning a pointer to a local variable creates a dangling pointer, since the frame is popped.
- Calling sequence: the caller evaluates and pushes the actual parameters and the return address; the callee sets the new frame pointer, saves registers and allocates locals. The return sequence reverses these steps and leaves the return value in the agreed slot.
- The CONTROL link points to the caller’s frame (the dynamic chain); the ACCESS link points to the frame of the lexically enclosing procedure (the static chain). Languages without nested procedures, such as C, need no access links.
- Activation tree of fact(4): a chain of 4 nodes, so 4 calls in total and a maximum of 4 frames on the stack.
- Local-variable access uses fixed offsets from the frame pointer; non-local variables in nested procedures need access links or a display.
| Area | Contents | Size known at compile time? |
|---|---|---|
| Code | instructions | Yes |
| Static | global and static variables, constants | Yes |
| Stack | activation records | No (grows with recursion depth) |
| Heap | dynamically allocated objects | No |
Worked example
Problem. How many activation records are on the stack at the deepest point of fact(4), where fact(n) = n * fact(n−1) and fact(1) = 1 (not counting main)?
Solution. fact(4) calls fact(3), which calls fact(2), which calls fact(1). At the deepest point all four activations are alive: 4 records.
Scoping, access links and parameter passing
- STATIC (lexical) scope: a name refers to the declaration in the nearest enclosing BLOCK of the source text — decided at compile time. DYNAMIC scope: a name refers to the most recent active declaration found by following the CALL chain (control links) — decided at run time.
- Nested procedures: a procedure at nesting depth n accessing a variable declared at depth m ≤ n follows (n − m) ACCESS LINKS. A DISPLAY (array of frame pointers indexed by depth) reaches the frame in one step.
- Call by VALUE: the actual is evaluated and copied to the formal; changes are not visible to the caller. Call by REFERENCE: the address of the actual is passed, so the formal is an alias; changes are visible. Copy-RESTORE (value-result): copy in on call, copy out on return.
- Call by NAME: each use of the formal re-evaluates the actual expression in the caller’s environment (macro-like substitution, implemented with a thunk). Assignments to a formal assign to the actual; subscripts such as a[i] are re-evaluated each time, so side effects matter.
- Aliasing example: void foo(a, b) { a = a + 1; b = b + a; } with int x = 2 and the call foo(x, x). By value: x stays 2. By reference: a and b both alias x, so x becomes 3 and then 3 + 3 = 6. By name: same as reference here (6). Copy-restore: a = 3, b = 5 locally; the final copy-out order decides (5 for left-to-right).
- Call by name swap: swap(x, y) { t = x; x = y; y = t; } with i = 1 and a[1] = 5, called as swap(i, a[i]). By reference: t = 1; i = 5; a[1] = 1 (correct swap). By name: t = i = 1; i = a[i] = a[1] = 5; a[i] = t sets a[5] = 1, so i = 5, a[1] stays 5 and a[5] becomes 1 — the swap fails.
- Type checking and semantic analysis use the symbol table and attribute rules: type expressions, structural vs name equivalence, coercions (int → float), overloading and polymorphism are the typical topics.
| Mechanism | What is passed | Caller sees changes? |
|---|---|---|
| Call by value | copy of value | No |
| Call by reference | address (alias) | Yes, immediately |
| Copy-restore | copy in, copy out on return | Yes, at return |
| Call by name | expression (thunk), re-evaluated at each use | Yes (via re-evaluation) |
Worked example
Problem. int x = 10; void f() { print(x); } void g() { int x = 20; f(); } main() { g(); } — what is printed under static and under dynamic scoping?
Solution. Static scoping: f’s x is the global x, so it prints 10. Dynamic scoping: f is called from g, whose x (20) is the most recent declaration on the call chain, so it prints 20.
Exam tips
- Synthesized = from children; inherited = from parent / left siblings. S-attributed ⊂ L-attributed.
- A bottom-up parser runs postfix actions at reduction time; follow the reductions to predict SDT output.
- Max activation records on the stack = depth of the activation tree; number of calls = nodes of the tree.
- Static scope = textual nesting; dynamic scope = call chain. Access links hops = depth difference.
- For call by name, substitute the actual expression literally at every use of the formal and re-evaluate subscripts.
10 GATE-style MCQs for SDT & Runtime Environment with full explanations. Attempt each question before revealing the answer.
A bottom-up parser executes the actions of S → a S {print 1} | b {print 2} at reduction time. For the input a a b the output is:
Show answer & explanation
Answer: B
First b is reduced (prints 2); then the two a S reductions print 1 and 1: 211.
The SDT E → E + T {print “+”} | T ; T → T * F {print “*”} | F ; F → num {print num.val} with input 2 + 3 * 4 (bottom-up evaluation) prints:
Show answer & explanation
Answer: B
The reduction T → T * F (print *) happens before E → E + T (print +), so the output is the postfix form 2 3 4 * +.
Which of the following can be evaluated in a single bottom-up pass during LR parsing?
Show answer & explanation
Answer: B
S-attributed definitions use only synthesized attributes, which are computed when a production is reduced.
In the declaration SDD D → T L {L.inh = T.type}, the attribute L.inh is:
Show answer & explanation
Answer: B
L.inh is computed from the sibling T, so it is inherited.
In the program int x = 10; void f(){ print x; } void g(){ int x = 20; f(); } main(){ g(); } the values printed under static scoping and dynamic scoping respectively are:
Show answer & explanation
Answer: C
Static scoping uses the global x (10); dynamic scoping uses the most recent x on the call chain (g’s x = 20).
Procedure P at nesting depth 4 accesses a variable declared in an enclosing procedure at nesting depth 2. The number of access links followed is:
Show answer & explanation
Answer: B
Follow (4 − 2) = 2 access links.
void foo(a, b) { a = a + 1; b = b + a; } int x = 2; foo(x, x) — the value of x after the call, with call by reference, is:
Show answer & explanation
Answer: D
a and b alias x: a = a + 1 makes x = 3; then b = b + a is x = 3 + 3 = 6.
The recursive function fib(n) = fib(n−1) + fib(n−2), with fib(0) = fib(1) = 1, is called as fib(4). The total number of activations (calls including the first) is:
Show answer & explanation
Answer: C
C(n) = C(n−1) + C(n−2) + 1: C(2) = 3, C(3) = 5, C(4) = 9.
The maximum number of activation records simultaneously on the stack for fib(4) (excluding main) is:
Show answer & explanation
Answer: B
The deepest path is fib(4) → fib(3) → fib(2) → fib(1): 4 frames.
Which of the following is NOT stored in an activation record?
Show answer & explanation
Answer: C
Global and static variables live in the static data area; the other three are fields of the frame.
Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.
For the desk-calculator SDD, the value printed for the input 2 * 3 + 4 n is:
Show answer & explanation
Answer: B) 10
2 × 3 = 6; 6 + 4 = 10.
Which rule makes a definition NOT L-attributed?
Show answer & explanation
Answer: C) an inherited attribute depends on a right sibling’s attribute
L-attributed definitions allow only parent and LEFT sibling dependencies.
Maximum number of activation records on the stack for fact(5) with base case fact(1) (not counting main):
Show answer & explanation
Answer: B) 5
fact(5), fact(4), fact(3), fact(2), fact(1) are all alive at the deepest point.
Using the swap(i, a[i]) example (i = 1, a[1] = 5, swap(x,y){t=x; x=y; y=t;}) with call by reference, the final value of a[1] is:
Show answer & explanation
Answer: B) 1
By reference x = i and y = a[1]: t = 1, i = 5, a[1] = 1. The swap works.
Call by value passes:
Show answer & explanation
Answer: B) a copy of the value of the actual parameter
Changes to the formal do not affect the actual.
A function returns the address of one of its local variables. This leads to:
Show answer & explanation
Answer: B) a dangling pointer
The frame holding the local variable is popped on return, so the address becomes invalid.
Which attribute type is used to pass the declared type from the type specifier to a list of identifiers?
Show answer & explanation
Answer: B) inherited
The type flows from T to the identifier list L, a parent/left-sibling dependency.
The total number of calls to fib in the activation tree of fib(3) (fib(0)=fib(1)=1) is:
Show answer & explanation
Answer: C) 5
C(3) = C(2) + C(1) + 1 = 3 + 1 + 1 = 5.
Topic FAQs
What is the difference between an SDD and an SDT?
An SDD is a high-level specification (attributes + semantic rules, no execution order). An SDT is an implementation scheme with the actions embedded in the productions, so the position of each action fixes when it executes.
Why are L-attributed definitions important?
They can be evaluated in a single left-to-right depth-first traversal, so they are compatible with one-pass compilers using top-down parsing (and bottom-up parsing with marker non-terminals).
What is the difference between the control link and the access link?
The control (dynamic) link points to the caller’s activation record and is used to pop the frame on return; the access (static) link points to the frame of the lexically enclosing procedure and is used to reach non-local variables under static scoping.
Can recursion work with static storage allocation?
No. Each recursive activation needs its own local variables, so frames must be allocated dynamically on a stack.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.