Home » Compiler Design » Compiler design miscellaneous » Question

Compiler design miscellaneous

Direction: Consider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production.

E → number E.val = number.val
| E ‘+’ E E(1).val = E(2).val + E(3).val
| E ‘×’ E E (1).val = E(2).val × E(3).val

  1. Assume the conflicts in Part (a) of this question are resolved and an LALR(1) parser is generated for parsing arithmetic expressions as per the given associativity properties does the generated parser realize?
    1. Equal precedence and left associativity; expression is evaluated to 7
    2. Equal precedence and left associativity; expression is evaluated to 9
    3. Precedence of ‘x’ is higher than that of ‘+’ and both operators are left associative; expression is evaluated to 7
    4. Precedence of ‘+’ is higher than that of ‘×’ and both operators are left associative; expression is evaluated to 9
Correct Option: B

The grammar has equal precedence and it is also ambiguous. Since LALR(1) parser prefer shift over reduce so + operation will be executed here before). 2 + 1 = 3 & 3 #3 = 9 also the operators are right associative. Hence (b) is correct option.



Your comments will be displayed only after manual approval.