GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Infer operator binding from expression productions

5 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Inspect production nesting and recursion direction to establish precedence, associativity or their absence. Operators and alternatives differ; explicit precedence tables and yacc defaults are separate.

1 mark · MCQ
Report issue

Parsing & Syntax Analysis

Question 17, GATE 2014: Grammar: S→T*P; T→U or T*U; P→Q+P or Q; Q→Id; U→Id. Which associativity statement is true? A + left, * right; B + right, * left; C both right; D both left.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. Set up the reasoning. Inspect the recursive side of each operator production. T recurs on the left of *, while P recurs on the right of +.

2. Derive the result. The rule T→T*U is left-recursive and groups multiplication to the left. P→Q+P is right-recursive and groups addition to the right. Thus + is right associative and * left associative, B.

3. Check the interpretation. Expanding T twice gives (Id*Id)*Id, whereas expanding P twice gives Id+(Id+Id). Associativity concerns grouping of repeated operators, not which operator has greater precedence.

Original paper, page 33 · Answer key

Descriptive
Report issue

Operator precedence in the ambiguous expression grammar

Parsing & Syntax Analysis

Question 22(b), GATE 2002: Grammar E→E+E | E*E | id. Original22(b): What precedence, if any, does this grammar impose on * and +?
Question 22(b), GATE 2002: Grammar E→E+E | E*E | id. Original22(b): What precedence, if any, does this grammar impose on * and +?
Question 22(b), GATE 2002: Grammar E→E+E | E*E | id. Original22(b): What precedence, if any, does this grammar impose on * and +?
Tap to enlarge
Answer & solution

Answer: No relative precedence is specified or enforced by this grammar. (no official key was published)

Both + and * are produced by the same nonterminal E, and the grammar allows both i+(j*k) and (i+j)*k for the same token string. It therefore does not enforce multiplication-before-addition or addition-before-multiplication. Saying merely that the operators have equal precedence is also incomplete: the grammar does not enforce associativity, so it does not define a unique parse even under that description. External parser precedence declarations could resolve conflicts, but they are not part of the supplied grammar.

Original paper, page 10

2 marks · MCQ
Report issue

Precedence encoded in grammar levels

Parsing & Syntax Analysis

Question 2.21, GATE 2000: Given E→E*F|F+E|F and F→F−F|id, which operator-precedence claim is true: * above +; − above *; + and − equal; + above *?
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step 1: The only grammar productions introducing minus are inside F. Any operand F used by multiplication or addition is therefore formed before the surrounding E-level operation.

Step 2: For example id*id−id must have the minus within the right F, giving id*(id−id), rather than treating minus as an outer E-level operation. Thus minus binds more tightly than multiplication, B.

The E rules do not give a clean unambiguous ordering between + and *; some mixed expressions have multiple parses. F→F−F is itself associative-ambiguous. These ambiguities do not invalidate the separation placing minus at the tighter grammar level.

Original paper, page 6

1 mark · MCQ
Report issue

Associativity encoded by recursion

Parsing & Syntax Analysis

Question 1.6, GATE 1997: Grammar X→X⊕Y|Y, Y→Z⊙Y|Z, Z→id. Determine associativity of⊕ and⊙.
Tap to enlarge
Your answer
Answer & solution

Answer: A: ⊕ left, ⊙ right (no official key was published)

In X→X⊕Y, additional⊕ operators are introduced inside the left X. Thus id⊕id⊕id groups as (id⊕id)⊕id. In Y→Z⊙Y, additional⊙ operators occur in the right Y, grouping id⊙id⊙id as id⊙(id⊙id). Therefore⊕ is left associative and⊙ right associative, A. The lower nonterminal level also makes⊙ bind more tightly, but precedence and associativity are distinct properties.

Original paper, page 1

2 marks · Descriptive
Report issue

Expression grammar precedence and ambiguity

Parsing & Syntax Analysis

Question 10(a), GATE 1991: For arithmetic expressions using binary, mathematically nonassociative operators minus and division, the start symbol is E and the grammar is E→E−T | T; T→T/F | F; F→(E) | id. Is it unambiguous? If so state relative precedence; otherwise provide an unambiguous grammar giving division higher precedence.
Question 10(a), GATE 1991: For arithmetic expressions using binary, mathematically nonassociative operators minus and division, the start symbol is E and the grammar is E→E−T | T; T→T/F | F; F→(E) | id. Is it unambiguous? If so state relative precedence; otherwise provide an unambiguous grammar giving division higher precedence.
Tap to enlarge
Answer & solution

Answer: Unambiguous; / has higher precedence than −, and each operator is parsed left-associatively. (no official key was published)

The grammar separates expression levels: E combines terms with minus, while T combines factors with division. Thus id−id/id parses as id−(id/id), never as (id−id)/id. Division has higher precedence than subtraction.

Both recursive productions are left-recursive, so unparenthesized chains group to the left. For example id−id−id has structure (id−id)−id, and id/id/id has structure (id/id)/id. This is a syntactic associativity convention; it does not claim the arithmetic operators obey an associative law.

For uniqueness, consider an expression outside its matched parentheses. If it has a top-level minus, the root must be E→E−T, split at its last such minus because T cannot contain an unparenthesized minus. If there is no such minus, use E→T. Similarly a top-level slash in a term forces T→T/F split at its last top-level slash, since F cannot contain an unparenthesized slash. At the factor level, either the token is id or the uniquely matched outer parentheses contain an expression, to which the same argument applies. Induction on expression length gives a unique parse. Redundant parentheses are allowed by the grammar, but do not create two parses for the same token string.

Original paper, page 7