• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

Parsing and Syntax directed translation

Question 21

Consider the following expression grammar. The seman­tic rules for expression calculation 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
The above grammar and the semantic rules are fed to a yacc tool (which is an LALR (1) parser generator) for parsing and evaluating arithmetic expressions. Which one of the following is true about the action of yacc for the given grammar?
  • It detects recursion and eliminates recursion
  • It detects reduce-reduce conflict, and resolves
  • It detects shift-reduce conflict, and resolves the conflict in favor of a shift over a reduce action
  • It detects shift-reduce conflict, and resolves the conflict in favor of a reduce over a shift action

Question 22

Consider the following expression grammar. The seman­tic rules for expression calculation 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 
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 grammar. Consider an expression 3 × 2 + 1. What precedence and associativity properties does the generated parser realize?
  • Equal precedence and left associativity; ex­pression is evaluated to 7
  • Equal precedence and right associativity; ex­pression is evaluated to 9
  • Precedence of \'×\' is higher than that of \'+\', and both operators are left associative; expression is evaluated to 7
  • Precedence of \'+\' is higher than that of \'×\', and both operators are left associative; expression is evaluated to 9

Question 23

Which of the following grammar rules violate the requirements of an operator grammar ? P, Q, R are nonterminals, and r, s, t are terminals.
1.    P → Q R                    
2.    P → Q s R
3.    P → ε       
4.    P → Q t R r 
  • 1 only
  • 1 and 3 only
  • 2 and 3 only
  • 3 and 4 only

Question 24

Consider the grammar with the following translation rules and E as the start symbol.
E → E1 # T { E.value = E1.value * T.value }
         | T{ E.value = T.value }
T → T1 & F { T.value = T1.value + F.value }
          | F{ T.value = F.value }
F → num { F.value = num.value } 
Compute E.value for the root of the parse tree for the expression: 2 # 3 & 5 # 6 & 4.
  • 200
  • 180
  • 160
  • 40

Question 25

Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar?
  • Removing left recursion alone
  • Factoring the grammar alone
  • Removing left recursion and factoring the grammar
  • None of these

Question 26

Assume that the SLR parser for a grammar G has n1 states and the LALR parser for G has n2 states. The relationship between n1 and n2 is:
  • n1 is necessarily less than n2
  • n1 is necessarily equal to n2
  • n1 is necessarily greater than n2
  • none of these

Question 27

In a bottom-up evaluation of a syntax directed definition, inherited attributes can
  • always be evaluated
  • be evaluated only if the definition is L-­attributed
  • be evaluated only if the definition has synthesized attributes
  • never be evaluated

Question 28

Consider the grammar shown below
S → i E t S S\' | a
S\' → e S | ε
E → b 
In the predictive parse table. M, of this grammar, the entries M[S\', e] and M[S\', $] respectively are
  • {S\' → e S} and {S\' → e}
  • {S\' → e S} and {}
  • {S\' → ε} and {S\' → ε}
  • {S\' → e S, S\'→ ε} and {S\' → ε}

Question 29

Consider the grammar shown below.
S → C C
C → c C | d
The grammar is
  • LL(1)
  • SLR(1) but not LL(1)
  • LALR(1) but not SLR(1)
  • LR(1) but not LALR(1)

Question 30

Consider the translation scheme shown below
S → T R
R → + T {print (\'+\');} R | ε
T → num {print (num.val);} 
Here num is a token that represents an integer and num.val represents the corresponding integer value. For an input string \'9 + 5 + 2\', this translation scheme will print
  • 9 + 5 + 2
  • 9 5 + 2 +
  • 9 5 2 + +
  • + + 9 5 2

There are 85 questions to complete.

Last Updated :
Take a part in the ongoing discussion