Attributes and SDT Types MCQs: 12 Solved Questions with Explanations

Test attribute direction, S-attributed and L-attributed definitions, evaluation order and declaration typing across 12 solved MCQs, six of them GATE previous-year questions.

KnowledgeGate Team

Exam prep & CS education

10 Aug 20269 min read

Memorising the words synthesized and inherited is not enough when a semantic rule quietly reads a right sibling. These 12 MCQs, six of them GATE previous-year questions from 2003 to 2026, all turn on one move: find the symbol being assigned, then find where its value comes from.

Attributes and SDT types: use dependency direction before attempting an MCQ

A synthesized attribute reads the node's children; an inherited attribute reads its parent or a sibling. A right-sibling read is still inherited, but the definition stops being L-attributed: a left-to-right pass cannot resolve it.

S-attributed means synthesized only. L-attributed also permits parent and left-sibling sources, so S-attributed implies L-attributed, not conversely. See Compiler Design MCQs and GATE CS Exam Preparation.

Synthesized and inherited attribute MCQs

Question 1

Consider the following statements:

S1: The value of a synthesized attribute at a node is computed from the value of the attribute at the children of that node.

S2: The value of inherited attribute at a node is computed from the value of the attribute at the children of that node.

Which of the following is true?

  • A. S1 is true

  • B. S2 is true

  • C. S1 is false

  • D. S1 and S2 are false

Answer: A.

S1 correctly defines a synthesized attribute as one computed from the children. S2 is false: an inherited attribute reads the parent or siblings, never the children. A is correct.

Question 2

Consider the SDD

P -> Q R { Q.val = R.val }

A -> * B C { A.val = B.val × C.val }

Which of the following is true?

  • A. Q.val is synthesized attribute and A.val is inherited attribute.

  • B. Q.val is synthesized attribute but A.val is not an inherited attribute.

  • C. Q.val is inherited attribute but A.val is not a synthesized attribute.

  • D. Q.val is inherited attribute and A.val is synthesized attribute.

Answer: D.

Q.val is assigned from its sibling R, so it is inherited, ruling out A and B; A.val is computed from its children, so it is synthesized. Only D fits.

Question 3

Consider the following productions with semantic actions:

S -> A + B { S.val = A.val + B.val }

A -> a { A.val = a }

B -> b { B.val = b }

Select the wrong option.

  • A. The given SDT has synthesized attributes.

  • B. The given SDT is L-attributed.

  • C. The given SDT can be evaluated by a bottom-up parser.

  • D. The given SDT has inherited attributes.

Answer: D.

Every action assigns the left-hand-side attribute from its own children or terminals, so the definition is S-attributed, hence L-attributed and bottom-up evaluable. A, B and C are true; D is the wrong statement.

S-attributed and L-attributed classification MCQs

Question 4

Consider the SDT.

S1: P -> Q + R { P.val = Q.val + R.val }

S2: P -> Q . R { P.val = Q.val * R.val and Q.val = R.val }

Which of the given option is correct w.r.t the above SDT?

  • A. S1 is L attributed but S2 is not L-attributed.

  • B. S1 is S attributed and S2 is L-attributed.

  • C. Both S1 and S2 are S attributed.

  • D. None of the above

Answer: A.

S1 computes P.val from its children, so it is S-attributed and hence L-attributed. In S2, Q.val depends on its right sibling R, so S2 is not L-attributed: exactly option A.

Question 5

Consider the following SDD:

T -> F T₁ { F.val = T₁.val }

T₂ -> * A T₃ { T₂.val = T₃.val × A.val }

Which of the following is true?

  • A. This SDD follows S-attributed definition.

  • B. This SDD follows both S-attributed as well as L-attributed definition.

  • C. This SDD follows L-attributed definition.

  • D. None of the above

Answer: D.

F.val is set from T₁, F's right sibling, so it is inherited from the right. The SDD is neither S-attributed nor L-attributed, so A, B and C all fail, leaving D.

Question 6

Consider the SDT.

S -> P Q { S.a = P.a + Q.a }

P -> Q R { Q.a = R.a }

R -> id { R.a = id }

Which of the given option is correct:

  • A. Given SDT is L-attributed

  • B. Attribute “a” is inherited only

  • C. Attribute “a” is synthesized only

  • D. None of these

Answer: D.

S.a and R.a are synthesized, but Q.a = R.a makes Q depend on its right sibling R. So a is neither synthesized-only nor inherited-only, and that dependency also breaks L-attribution, leaving D.

Evaluation order and declaration-type PYQs

Review Syntax-Directed Translation and Code Optimization before attempting these evaluation-order PYQs.

Question 7

GATE 2003

In a bottom-up evaluation of a syntax directed definition, inherited attributes can

  • A. always be evaluated

  • B. be evaluated only if the definition is L-attributed

  • C. be evaluated only if the definition has synthesized attributes

  • D. never be evaluated

Answer: B.

Reductions compute synthesized attributes naturally, but inherited attributes need parent or left-sibling values first, which a bottom-up evaluator can honour only for an L-attributed definition. A and D are false absolutes, and C names the wrong condition.

Question 8

GATE 2019

Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let X₁, X₂, X₃, X₄, X₅, and X₆ be the placeholders for the non-terminals D, T, L or L₁ in the following table:

Production rule

Semantic action

D -> T L

X₁.type = X₂.type

T -> int

T.type = int

T -> float

T.type = float

L -> L₁ , id

X₃.type = X₄.type; addType(id.entry, X₅.type)

L -> id

addType(id.entry, X₆.type)

Which one of the following are the appropriate choices for X₁, X₂, X₃ and X₄?

  • A. X₁ = L , X₂ = T, X₃ = L₁, X₄ = L

  • B. X₁ = T , X₂ = L, X₃ = L₁, X₄ = T

  • C. X₁ = L , X₂ = L, X₃ = L₁, X₄ = T

  • D. X₁ = T , X₂ = L, X₃ = T, X₄ = L₁

Answer: A.

In D -> T L the type flows from T into the list, so X₁ = L and X₂ = T. Down the list, X₃ = L₁ and X₄ = L copy the type along, and addType records each identifier: for float x, y both get float, option A.

Question 9

GATE 2020

Consider the productions A -> P Q and A -> X Y. Each of the five non-terminals A, P, Q, X, and Y has two attributes: s is a synthesized attribute, and i is an inherited attribute. Consider the following rules.

Rule 1 : P.i = A.i + 2, Q.i = P.i + A.i, and A.s = P.s + Q.s

Rule 2 : X.i = A.i + Y.s, and Y.i = X.s + A.i

Which one of the following is TRUE ?

  • A. Both Rule 1 and Rule 2 are L - attributed

  • B. Only Rule 1 is L - attributed

  • C. Only Rule 2 is L - attributed

  • D. Neither Rule 1 nor Rule 2 is L - attributed

Answer: B.

In Rule 1 every value flows from the parent or a left sibling. Take A.i = 4: P.i = 4 + 2 = 6, and Q.i = 6 + 4 = 10, so each input exists before it is needed. Rule 2 makes X.i depend on the right-sibling value Y.s, which a left-to-right pass cannot supply, so only Rule 1 qualifies.

Advanced attribute-grammar classification PYQs

Question 10

GATE 2024 Set 2

Which of the following statements is/are FALSE?

  • A. An attribute grammar is a syntax-directed definition (SDD) in which the functions in the semantic rules have no side effects

  • B. The attributes in a L-attributed definition cannot always be evaluated in a depth-first order

  • C. Synthesized attributes can be evaluated by a bottom-up parser as the input is parsed

  • D. All L-attributed definitions based on LR(1) grammar can be evaluated using a bottom-up parsing strategy

Answers: B and D.

A and C are true: semantic rules have no side effects, and bottom-up parsers compute synthesized attributes while parsing. B is false because an L-attributed definition always allows a left-to-right depth-first evaluation; D is false because LR(1) does not guarantee bottom-up evaluation of inherited attributes.

Question 11

GATE 2025 Set 2

Given the following syntax directed translation rules:

Rule 1: R -> A B { B.i = R.i - 1; A.i = B.i; R.i = A.i + 1; }

Rule 2: P -> C D { P.i = C.i + D.i; D.i = C.i + 2; }

Rule 3: Q -> E F { Q.i = E.i + F.i; }

Which ONE is the CORRECT option among the following?

  • A. Rule 1 is S-attributed and L-attributed; Rule 2 is S-attributed and not L-attributed; Rule 3 is neither S-attributed nor L-attributed

  • B. Rule 1 is neither S-attributed nor L-attributed; Rule 2 is S-attributed and L-attributed; Rule 3 is S-attributed and L-attributed

  • C. Rule 1 is neither S-attributed nor L-attributed; Rule 2 is not S-attributed and is L-attributed; Rule 3 is S-attributed and L-attributed

  • D. Rule 1 is S-attributed and not L-attributed; Rule 2 is not S-attributed and is L-attributed; Rule 3 is S-attributed and L-attributed

Answer: C.

Rule 1 makes A.i depend on its right sibling B: neither S-attributed nor L-attributed. Rule 2 gives D.i only a left-sibling dependency: L-attributed, not S-attributed. Rule 3 computes Q.i from children alone: both, so C is correct.

Question 12

GATE 2026 Set 1

Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations.

SDD1 grammar G1

SDD1 semantic rules

SDD2 grammar G2

SDD2 semantic rules

D -> T V

D.type = T.type; V.type = T.type

D -> D₁ id

D.type = D₁.type; put(id.entry, D₁.type)

T -> int

T.type = int

D -> T id

D.type = T.type; put(id.entry, T.type)

T -> float

T.type = float

T -> int

T.type = int

V -> V₁ id

V₁.type = V.type; put(id.entry, V.type)

T -> float

T.type = float

V -> id

put(id.entry, V.type)

D is the start symbol, and int, float and id are the three terminals. The non-terminal V₁ is the same as V and the non-terminal D₁ is the same as D. Here, the subscript is used to differentiate the grammar symbols on the two sides of a production. The function put updates the symbol table with the type information for an identifier. Let P and Q be the languages specified by grammars G1 and G2, respectively.

Which of the following statements is/are true?

  • A. The languages P and Q are the same

  • B. SDD2 is S-attributed and contains only synthesized attributes

  • C. SDD1 is L-attributed and contains only inherited attributes

  • D. The specifications of SDD1 and SDD2 are such that the same entries get added to the symbol table

Answers: A, B and D.

Both grammars describe the same declarations, so P and Q match (A) and identical entries reach the symbol table (D), and SDD2 uses synthesized attributes only (B). C fails: SDD1 is L-attributed but not limited to inherited attributes.

Attributes and SDT Types MCQ traps and exam patterns

Question pattern

Fast test

Questions

Direction

Inspect the assigned symbol

Q1-Q3

S versus L

Find right-side assignments and right-sibling dependencies

Q4-Q6, Q11

Evaluation

Check when each value exists

Q7, Q9, Q10

Declarations

Follow one type

Q8, Q12

Three traps produce most of the wrong answers here. A name classifies nothing: the attribute called a in Question 6 is synthesized in two rules and inherited in a third, so both “inherited only” and “synthesized only” fail. S-attributed implies L-attributed and never the reverse, so an option reading “S-attributed and not L-attributed” (Question 11, option D) can be struck unread. And a right-sibling read breaks L-attribution while remaining a legal SDD, which is why Questions 5 and 6 land on “None of the above”. Redo Questions 4, 9 and 11 with dependency arrows drawn on the parse tree.

Attributes and SDT Types: the short version and next step

The short version: classify by the assigned symbol, hunt right-sibling dependencies, check when each value exists, and follow one declaration's type down the chain. Use GATE Guidance by Sanchit Sir for concepts, or GATE Test Series for timed practice.