GATE / CS ARCHIVE

GATE CSE 2020 / QUESTION 24

GATE CSE 2020 Q24

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 2020 paper

GATE 2020, Q24 1 mark · NAT
Report issue

Parsing & Syntax Analysis

Question 24, GATE 2020: Grammar: S→aSB | d; B→b. How many reductions does a bottom-up parser perform to accept aaadbbb?
Question 24, GATE 2020: Grammar: S→aSB | d; B→b. How many reductions does a bottom-up parser perform to accept aaadbbb?
Tap to enlarge
Answer & solution

Official answer: 7

Step through it (16 steps)

Worked explanation

An explicit reduction sequence is aaadbbb ⇒ aaaSbbb ⇒ aaaSBbb ⇒ aaSbb ⇒ aaSBb ⇒ aSb ⇒ aSB ⇒ S. The first step uses d→S; each pair thereafter uses b→B followed by aSB→S. Thus there are 1+3+3=7 reductions. The corresponding derivation tree has four S-production nodes and three B-production nodes. A bottom-up parser may also shift seven terminals and eventually perform an accept action; neither shifts nor accept are additional grammar reductions.

Derivation and answer

The derivation uses S→aSB three times, S→d once, and B→b three times. A bottom-up parse reverses these seven production applications, so it performs 7 reductions, excluding the separate parser accept action.

Original paper, page 9 · Answer key