Skip to main content

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).

~5 hrs ~0.5–1 marks · High 10 samples · 8 practice · 10 cards

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.
ProductionSemantic rule
L → E nprint(E.val)
E → E1 + TE.val = E1.val + T.val
E → TE.val = T.val
T → T1 * FT.val = T1.val × F.val
T → FT.val = F.val
F → ( E )F.val = E.val
F → digitF.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.

Annotated parse tree for 3 * 5 + 4 n with val attributes at every node and the nine step bottom-up evaluation order
Annotated parse tree of 3 * 5 + 4 n: all val attributes are synthesized, so a bottom-up order gives E.val = 19.

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.
TypeAttributes allowedEvaluationParsers
S-attributedsynthesized onlybottom-up, at reductionLR (and LL)
L-attributedsynthesized + inherited (from parent / left siblings)one left-to-right DFSLL, and LR with markers
General SDDany acyclic dependenciestopological order of dependency graphbuild 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.

Left: synthesized attribute flowing up from children to parent. Right: inherited attribute flowing from T to L and down to L1
Synthesized attributes flow up (S-attributed); inherited attributes flow from the parent or left siblings (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.
InputAction order (reductions)Output
2 + 3F→2 print 2, F→3 print 3, E→E+T print +2 3 +
2 + 3 * 4prints 2, 3, 4, then T→T*F prints *, then E→E+T prints +2 3 4 * +
2 * 3 + 4prints 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.
StepNodeInherited iSynthesized s
1E′ (first)2 (= T.val)9 (copied up)
2E′ after + 32 + 3 = 59 (copied up)
3E′ after + 45 + 4 = 99 (copied up)
4E′ → ε9s = 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.
AreaContentsSize known at compile time?
CodeinstructionsYes
Staticglobal and static variables, constantsYes
Stackactivation recordsNo (grows with recursion depth)
Heapdynamically allocated objectsNo

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.

Fields of an activation record from actual parameters to temporaries next to the run-time memory areas code, static, heap and stack
An activation record and the run-time memory areas: heap and stack grow towards each other.

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.
MechanismWhat is passedCaller sees changes?
Call by valuecopy of valueNo
Call by referenceaddress (alias)Yes, immediately
Copy-restorecopy in, copy out on returnYes, at return
Call by nameexpression (thunk), re-evaluated at each useYes (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.
Exam pattern · 10 questions

GATE sample questions & answers

10 GATE-style MCQs for SDT & Runtime Environment with full explanations. Attempt each question before revealing the answer.

Q1

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:

  • A) 112
  • B) 211
  • C) 121
  • D) 221
Show answer & explanation

Answer: B

First b is reduced (prints 2); then the two a S reductions print 1 and 1: 211.

Q2

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:

  • A) + 2 * 3 4
  • B) 2 3 4 * +
  • C) 2 3 + 4 *
  • D) 2 + 3 * 4
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 * +.

Q3

Which of the following can be evaluated in a single bottom-up pass during LR parsing?

  • A) L-attributed definitions with inherited attributes in general
  • B) S-attributed definitions
  • C) definitions with cycles
  • D) only inherited attributes
Show answer & explanation

Answer: B

S-attributed definitions use only synthesized attributes, which are computed when a production is reduced.

Q4

In the declaration SDD D → T L {L.inh = T.type}, the attribute L.inh is:

  • A) synthesized
  • B) inherited
  • C) global
  • D) both synthesized and inherited
Show answer & explanation

Answer: B

L.inh is computed from the sibling T, so it is inherited.

Q5

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:

  • A) 10 and 10
  • B) 20 and 20
  • C) 10 and 20
  • D) 20 and 10
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).

Q6

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:

  • A) 1
  • B) 2
  • C) 3
  • D) 4
Show answer & explanation

Answer: B

Follow (4 − 2) = 2 access links.

Q7

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:

  • A) 2
  • B) 3
  • C) 5
  • D) 6
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.

Q8

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:

  • A) 5
  • B) 7
  • C) 9
  • D) 15
Show answer & explanation

Answer: C

C(n) = C(n−1) + C(n−2) + 1: C(2) = 3, C(3) = 5, C(4) = 9.

Q9

The maximum number of activation records simultaneously on the stack for fib(4) (excluding main) is:

  • A) 3
  • B) 4
  • C) 5
  • D) 9
Show answer & explanation

Answer: B

The deepest path is fib(4) → fib(3) → fib(2) → fib(1): 4 frames.

Q10

Which of the following is NOT stored in an activation record?

  • A) local variables
  • B) return address
  • C) global (static) variables
  • D) control link
Show answer & explanation

Answer: C

Global and static variables live in the static data area; the other three are fields of the frame.

Topic drill · 8 questions

8 practice questions — SDT & Runtime Environment

Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.

P1

For the desk-calculator SDD, the value printed for the input 2 * 3 + 4 n is:

  • A) 14
  • B) 10
  • C) 24
  • D) 9
Show answer & explanation

Answer: B) 10

2 × 3 = 6; 6 + 4 = 10.

P2

Which rule makes a definition NOT L-attributed?

  • A) an inherited attribute of a symbol depends on the parent’s inherited attribute
  • B) an inherited attribute depends on a left sibling’s attribute
  • C) an inherited attribute depends on a right sibling’s attribute
  • D) a synthesized attribute depends on children
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.

P3

Maximum number of activation records on the stack for fact(5) with base case fact(1) (not counting main):

  • A) 4
  • B) 5
  • C) 6
  • D) 1
Show answer & explanation

Answer: B) 5

fact(5), fact(4), fact(3), fact(2), fact(1) are all alive at the deepest point.

P4

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:

  • A) 5
  • B) 1
  • C) 0
  • D) unchanged and i = 1
Show answer & explanation

Answer: B) 1

By reference x = i and y = a[1]: t = 1, i = 5, a[1] = 1. The swap works.

P5

Call by value passes:

  • A) the address of the actual parameter
  • B) a copy of the value of the actual parameter
  • C) the expression as text
  • D) a pointer to the caller frame
Show answer & explanation

Answer: B) a copy of the value of the actual parameter

Changes to the formal do not affect the actual.

P6

A function returns the address of one of its local variables. This leads to:

  • A) a memory leak
  • B) a dangling pointer
  • C) a stack underflow
  • D) a compile-time error always
Show answer & explanation

Answer: B) a dangling pointer

The frame holding the local variable is popped on return, so the address becomes invalid.

P7

Which attribute type is used to pass the declared type from the type specifier to a list of identifiers?

  • A) synthesized
  • B) inherited
  • C) static
  • D) none
Show answer & explanation

Answer: B) inherited

The type flows from T to the identifier list L, a parent/left-sibling dependency.

P8

The total number of calls to fib in the activation tree of fib(3) (fib(0)=fib(1)=1) is:

  • A) 3
  • B) 4
  • C) 5
  • D) 7
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.

Quick revision · 10 cards

10 flashcards — SDT & Runtime Environment

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 What is a synthesized attribute?
Q2 What is an inherited attribute?
Q3 Define S-attributed and L-attributed.
Q4 When does a bottom-up parser run a postfix SDT action?
Q5 List the fields of an activation record.
Q6 Control link vs access link?
Q7 Static vs dynamic scoping?
Q8 How many access links for a variable at depth m accessed from depth n?
Q9 What is call by name?
Q10 Why is a stack used for activation records?