IAE Academy

GATE 2024 CS2 Q.29: solution and answer

GATE 2024 · CS2Q.29 · 1 mark · MSQ

Which of the following statements is/are FALSE?

  1. (A)

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

  2. (B)

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

  3. (C)

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

  4. (D)

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

See the question in the paper

Answer and solution

ANSWER

The false statements are (B) and (D).

Official answer: B, DSource: Official answer key, GATE 2024 CS2 (IISc Bengaluru)

  1. We need the FALSE statements, so test each option against the definitions.

  2. (A) An attribute grammar is an SDD whose semantic rules are functions with no side effects. This is the standard definition, so (A) is TRUE.

  3. (B) In an L-attributed definition, an inherited attribute of a symbol depends only on the inherited attributes of its parent and on attributes of symbols to its left. A left-to-right depth-first traversal of the parse tree therefore always finds every value it needs already computed. So the attributes can always be evaluated in depth-first order. The claim that they cannot always be is FALSE.

  4. (C) A bottom-up parser can compute a synthesized attribute at each reduction from the attributes of the right-hand-side symbols already on the stack. This is how S-attributed definitions work, so (C) is TRUE.

  5. (D) Evaluating an L-attributed definition bottom-up needs rewriting: each inherited attribute needs a marker nonterminal inserted before the symbol. This rewriting is guaranteed to work only for LL(1) grammars. For an LR(1) grammar the markers can introduce conflicts. Example: take , , , which is LR(1), and suppose and each have an inherited attribute. After inserting markers, with and . After shifting with lookahead , the parser must reduce by or by and cannot decide. This is a reduce-reduce conflict. So not all L-attributed definitions on LR(1) grammars can be evaluated bottom-up, and (D) is FALSE.

  6. The false statements are (B) and (D).

KEY IDEA

L-attributed definitions can always be evaluated in one left-to-right depth-first pass. Evaluating them bottom-up is guaranteed only for LL(1) grammars, because the marker nonterminals that inherited attributes need can break an LR(1) grammar.

WATCH OUT FOR

Reading (B) as true because bottom-up parsing cannot evaluate inherited attributes directly. The question there is depth-first evaluation order, which L-attribution always allows. Another slip is assuming that the LL(1) result for bottom-up evaluation extends to every LR(1) grammar.

From the GATE 2024 CS2 paper. Every question of this paper · Download the full paper · All 7 Syntax-directed translation questions, with notes

More Syntax-directed translation questions