CSE - Compiler design - Questions and solutions-1
Question:-
Option (A)
n_{1} is necessarily less than n_{2}
Option (B)
n_{1} is necessarily equal to n_{2}
Option (C)
n_{1} is necessarily greater than n_{2}
Option (D)
None of the above
Correct Option:
B
Question Solution:
SLR parser has n_{1} states for a grammar G LALR parser has n_{2} states for a grammar G. The states of SLR and LALR parser are the states of corresponding states in a deterministic finite automata which recognizes the viable prefixes and both deterministic finite automate contains the equal number of states so n_{1} = n_{2}.
Question:-
Option (A)
Always be evaluated
Option (B)
Be evaluated only if the definition is L-attributed
Option (C)
Be evaluated only if the definition has synthesized attributes
Option (D)
Never be evaluated
Correct Option:
C
Question Solution:
Every S(Synthesized) - attributed definition is L - attributed. For implementing inherited attributed during bottom-up parsing , extends to some, but not LR grammars. Consider the following example
Production | Semantic Rule |
S ® L | L. count: = 0 |
L ® L_{1}1 | L. count: = L. count: +1 |
L ® E | print (L. count) |
In the example above the no terminal L in L
® E inherits the count of the number of 1’s generated by S. Since the production L
® E is the first that a bottom-up burser would reduce by, the translator at the time can’t know the number of 1’s in the input. So in a bottom-up evaluation of a syntax directed definition, inherits attributed can’t be evaluated if the definition is L-attributed in the given example. So we can say that L-attributed definition is based on simple LR(1) grammar, but it can’t be implemented always but inherited attributes can be evaluated only if the definition has synthesized attributes.
Question:-
Option (A)
LL (1)
Option (B)
SLR (1) but not LL (1)
Option (C)
LALR (1) but not SLR (1)
Option (D)
LR (1) but not LALR (1)
Correct Option:
D
Question Solution:
Consider the grammar
S ® CC
C ® cCld
The given grammar is LR(1) grammar. Every SLR(1) grammar is an LR(1) grammar but not every LALR(1) grammar is LR(1) grammar. The given grammar is canonical LR(1) grammar and ever canonical LR(1) grammar is LR(1) grammar.
Question:-
Option (A)
{S’ ® e S} and {S’ ® e}
Option (B)
{S’ ® eS} and { }
Option (C)
{S’ ® e} and {S’ ® e}
Option (D)
{S’ ® e S, S’ ® e} and {S’ ® e}
Correct Option:
D
Question Solution:
The grammar is
S
® iEtSS’|a
S’
® eS |
Î
E
® b
The predictive parser table M is
Non terminal | a | b | e | i | t | $ |
S | s ® a | | | S ® iEiSS’ | | |
S’ | | | S’ ® Î | | | S’ ® Î |
| | | S’ ® es | | | |
E | | E ® b | | | | |
So M[S’, e] = {S’
® Î, S’
® eS}
M[S’, $] = S
® Î
Question:-
Option (A)
In statically typed language, each variable in a program has a fixed type
Option (B)
In un-type languages, values do not have any types
Option (C)
In dynamically typed languages, variables have no types
Option (D)
In all statically typed languages, each variable in a program is associated with values of only a single type during the execution of the program
Correct Option:
C
Question Solution:
This grammar is SLR LR, LALR all the type but it is not LL(1).
Question:-
Option (A)
Removing left recursion alone
Option (B)
Factoring the grammar alone
Option (C)
Removing left recursion and factoring the grammar
Option (D)
None of this
Correct Option:
C
Question Solution:
To convert an arbitrary CFG to an LL(1) grammar we must remove left recursion and left factoring if we can’t remove these then grammar becomes ambiguous and LL(1) parsers can’t parse it.