GA
GATEFORUM-Computer Science & TEFORUM-Computer Science & EngineeringEngineering Compiler Design-GATE Question
Compiler Design-GATE Question Gate Questions: - Compiler Design Gate Questions: - Compiler Design GATE 1992
GATE 1992
1. Fill in the blanks: 1. Fill in the blanks:
(i) Macro expansion is done in pass one instead of pass two in a pass macro assembler because (i) Macro expansion is done in pass one instead of pass two in a pass macro assembler because __________
__________
(ii) The purpose of instruction location counter in
(ii) The purpose of instruction location counter in an assembler is ________ an assembler is ________ 2. For a context-free grammar, FOLLOW(A) is the set of terminals
2. For a context-free grammar, FOLLOW(A) is the set of terminals that can appear immediately tothat can appear immediately to the right of non-terminal A in some "sentential" form. We define two sets LFOLLOW(A) and
the right of non-terminal A in some "sentential" form. We define two sets LFOLLOW(A) and RFOLLOW(A) by replacing the word "sentential" by "left most
RFOLLOW(A) by replacing the word "sentential" by "left most sentential" and "right mostsentential" and "right most sentential" respectively in the definition of FOLLOW(A).
sentential" respectively in the definition of FOLLOW(A). Which of the following statements is/are true?
Which of the following statements is/are true? (a)
(a) FOLLOW(A) FOLLOW(A) & & FOLLOW FOLLOW (A) (A) may may be be different different (b) (b) FOLLOW(A) FOLLOW(A) & & FOLLOW FOLLOW (A) (A) are are alwaysalways the same.
the same. (c)
(c) All All the the three three sets sets are are identical. identical. (d) (d) All All the the three three sets sets are are different.different. 3. Consider the SLR(1) and LALR (1) parsing
3. Consider the SLR(1) and LALR (1) parsing tables for a context free grammar. Which of thetables for a context free grammar. Which of the following statements is/are true?
following statements is/are true? (a
(a) T) The he go go to to papart rt of of boboth th tatablbles es mamay by be de dififfefererentnt.. (b(b) ) ThThe se shihift ft enentrtrieies as are re ididenentiticacal il in bn botothh the tables.
the tables. (c)
(c) The The redreduce uce ententrieries in ts in the the tablables maes may be dy be dififferferentent.. (d) (d) The The errerror enor entritries in es in the the tabtables mles mayay be different.
be different.
4. Write short answers to the following: 4. Write short answers to the following:
(i) Which of the following macros can put a macro assembler into an infinite loop? (i) Which of the following macros can put a macro assembler into an infinite loop?
..MMAACCRRI I MM11,,XX ..MMAACCRRO O MM22, , XX ....IIF F EEQQ,,XX ..IIF F EEQQ, , XX M M1 1 XX++11 MM22XX ..EENNDDCC ..EENNDDCC ..IIF F NNEE, , XX ..IIF F NNEE, , XX W WOORRD D XX ..WWOORRD D X X + + 11 ..EENNDDCC ..EENNDDCC ..EENNDDMM ..EENNDDMM Give an example of a call that does so.
Give an example of a call that does so. (ii) Mention the pass number for
(ii) Mention the pass number for each of the following activities that occur each of the following activities that occur in a two passin a two pass assembler
assembler
((aa) ) OObbjjeecct t ccoodde e ggeenneerraattiioonn ((bb) ) LLiitteerraalls s aaddddeed d lliitteerraal l ttaabbllee
((cc) ) LLiissttiinng g pprriinntteedd ((dd) ) AAddddrreesss s rreessoolluuttiioon n oof f llooccaal l ssyymmbboollss 5.(a). Write syntax directed definitions (semantic rules)
5.(a). Write syntax directed definitions (semantic rules) for the following grammar to add the typefor the following grammar to add the type of each identifier to its entry
of each identifier to its entry in the symbol table during semantic in the symbol table during semantic analysis. Rewriting the grammar analysis. Rewriting the grammar is not permitted and semantic rules
is not permitted and semantic rules are to be added to the ends of are to be added to the ends of productions onlyproductions only.. D TL; D TL; T int T int T real T real L L L, L, idid L L idid
5 (b). Write 3-address intermediate code (quadruples) for the following boolean expression in 5 (b). Write 3-address intermediate code (quadruples) for the following boolean expression in thethe sequence as it would be generated by a compiler. Partial evaluation of Boolean expressions sequence as it would be generated by a compiler. Partial evaluation of Boolean expressions isis not permitted. Assume the usual rules or precedence of the operators.
(a + b) > (c + d) or a > c and b < d (a + b) > (c + d) or a > c and b < d GATE 1993
GATE 1993 1. A
1. A simple two-pass assembler does the following in simple two-pass assembler does the following in the first pass:the first pass: (a) It allocates space for the literals.
(a) It allocates space for the literals. (b) It computes the total length of the
(b) It computes the total length of the programprogram
(c) It builds the symbol table for the symbols and their values. (c) It builds the symbol table for the symbols and their values. (d) It generates code for all
(d) It generates code for all the load and store register instructions.the load and store register instructions. (e) None of the above.
(e) None of the above.
2. For the program segment given below, which of the following are true? 2. For the program segment given below, which of the following are true?
program main (output); program main (output); type link = ^data;
type link = ^data; data = record data = record d : real; d : real; n : link n : link end; end; var ptr : link; var ptr : link; begin begin new (ptr); new (ptr); ptr :=nil; ptr :=nil; ptr^.d:=5.2; ptr^.d:=5.2; write In(ptr) write In(ptr) end. end.
(a) The program leads to compile time (a) The program leads to compile time error error (b) The program leads to run time error (b) The program leads to run time error (c) The program outputs 5.2
(c) The program outputs 5.2
(d) The program produces error relating to nil
(d) The program produces error relating to nil pointer dereferencingpointer dereferencing (e) None of the above
(e) None of the above 3. A
3. A part of the system software, which under all part of the system software, which under all circumstances must reside in circumstances must reside in the main memory,the main memory, is:
is:
((aa) ) tteexxt t eeddiittoorr ((bb) ) aasssseemmbblleer r ((cc) ) lliinnkkeer r ((dd) ) llooaaddeerr ((ee) ) nnoonne e oof f tthhe e aabboovvee
GATE 1994 GATE 1994
1 Generation of intermediate code based on an abstract machine model is
1 Generation of intermediate code based on an abstract machine model is useful in compilersuseful in compilers because
because
(a) It makes implementation of lexical analysis and syntax analysis easier (a) It makes implementation of lexical analysis and syntax analysis easier (b) Syntax-directed translations can be written for intermediate code generation (b) Syntax-directed translations can be written for intermediate code generation (c) It enhances the portability of the front
(c) It enhances the portability of the front end of the compiler end of the compiler (d) It is not possible
(d) It is not possible to generate code for real machines directly to generate code for real machines directly from high level languagefrom high level language programs
programs
2. State whether the following statements are True or False with reasons for your
2. State whether the following statements are True or False with reasons for your answer:answer: (a) Coroutine is just another name for
(a) Coroutine is just another name for a subroutine.a subroutine. (b) A tw
(b) A two pass assembler uses its o pass assembler uses its machine opcode table in the first pass machine opcode table in the first pass of assembly.of assembly. 3. State whether the following statements are True or False with reasons for your
3. State whether the following statements are True or False with reasons for your answer answer (a) A s
(a) A subroutine cannot always be used to replace a ubroutine cannot always be used to replace a macro in an assembly language program.macro in an assembly language program. (b) A symbo
(b) A symbol declared as 'external' in l declared as 'external' in assembly language is assigned an address outside theassembly language is assigned an address outside the program by the assembler itself.
Consider the following grammar: Consider the following grammar: A
A id := Eid := E E E E + T|TE + T|T T T (E)|id(E)|id Write a syntax directed translation to generate code using this
Write a syntax directed translation to generate code using this grammar for the computer grammar for the computer described above.
described above. GATE 1995 GATE 1995
1. In some programming languages, an identifier is
1. In some programming languages, an identifier is permitted to be a letter following by permitted to be a letter following by anyany number of letters or digits. If
number of letters or digits. If L and D denote the sets of letters and digits respectively, which of L and D denote the sets of letters and digits respectively, which of the following expressions defines an identifier?
the following expressions defines an identifier? (a)
(a) (L (L D)D)++ ((bb) ) LL((L L DD))** ((cc) ) ((LL..DD))** ((dd) ) LL..((LL..DD))**
2. A
2. A linker is given object linker is given object modules for a set of programs modules for a set of programs that were compiled separately. Whatthat were compiled separately. What information need to be included in an object m
information need to be included in an object module?odule? (a) Object code
(a) Object code (b) Relocation bits (b) Relocation bits
(c) Names and locations of all
(c) Names and locations of all external symbols defined in the object moduleexternal symbols defined in the object module (d) Absolute addresses of internal symbols
(d) Absolute addresses of internal symbols 3. Construct the LL(1)
3. Construct the LL(1) table for the following table for the following grammar.grammar. 1.
1. Expr Expr _Expr _Expr 2.
2. Expr Expr (Expr)(Expr) 3. Expr
3. Expr Var Var Expr TExpr Tailail 4.
4. ExprTail ExprTail _Expr _Expr 5. ExprTail
5. ExprTail 6. Var
6. Var Id Var Id Var TTailail 7.
7. VarTVarTail ail (Expr)(Expr) 8. VarTail
8. VarTail 9.
9. Goal Goal Expr$Expr$
4. (a) Translate the arithmetic expression a* – (b +
4. (a) Translate the arithmetic expression a* – (b + c) into syntax tree.c) into syntax tree. (b) A grammar is said to have cycles if
(b) A grammar is said to have cycles if it is the case thatit is the case that A
A ++ A A
Show that no grammar that has cycles
Show that no grammar that has cycles can be LL(I).can be LL(I). 5. Relative mode of addressing is
5. Relative mode of addressing is most relevant to writingmost relevant to writing
((aa) ) ccoorroouuttiinneess ((bb) ) ppoossiittiioon n - - iinnddeeppeennddeennt t ccoodde e ((cc) ) sshhaarreeaabblle e ccooddee ((dd)) interrupt
interrupt
6. The correct matching for the following pairs 6. The correct matching for the following pairs isis (a
(a) ) AcActitivavatition on rerecocordrd (1(1) L) Lininkiking ng loloadader er (b
(b) ) LoLocacatitioon n cocouuntnterer (2(2) ) GGararbabaggee collection collection (c
(c) R) Refefererenence ce cocoununtsts (3(3) S) Sububroroututinine ce calalll (d) Address (d) Address relocation relocation (4) Assembler (4) Assembler ((aa) ) A A – – 3 B 3 B – – 4 C 4 C – – 1 D 1 D – – 22 ((bb) ) A A – – 4 B 4 B – – 3 C 3 C – – 1 D 1 D – – 22 ((cc) ) A A – – 4 B 4 B – – 3 C 3 C – – 2 D 2 D – – 11 ((dd) ) A A – – 3 B 3 B – – 4 C 4 C – – 2 D 2 D – – 11 GATE 1997 GATE 1997
1. In the following grammar 1. In the following grammar
Y Y Y Y X X :: :: X X == ⊕⊕
Z Z Y Y * * Z Z :: :: Y Y == id id :: :: Z Z ==
Which of the following is true? Which of the following is true? (a
(a) ‘) ‘’ ’ is is leleft ft asassosociciatativive we whihile le '*'*' i' is rs rigight ht asassosociciatativeive (b(b) B) Bototh ‘h ‘’ ’ anand 'd '*' *' is is leleft ft asassosociciatativeive (c
(c) ‘) ‘’ ’ is is ririghght at assssocociaiatitive ve whwhilile ‘e ‘*' *' is is leleft ft asassosociciatativivee (d(d) N) Nonone oe of tf the he ababovovee 2. A
2. A language L allows declaration of arrays whose sizes are not known language L allows declaration of arrays whose sizes are not known during compilation. It isduring compilation. It is required to make efficient use of memory. Which one of the following is
required to make efficient use of memory. Which one of the following is true?true? (a) A c
(a) A compiler using static memory ompiler using static memory allocation can be written for Lallocation can be written for L (b) A c
(b) A compiler cannot be written for L; ompiler cannot be written for L; an interpreter must be used.an interpreter must be used. (c) A compiler using dynamic memory allocation can be written for L (c) A compiler using dynamic memory allocation can be written for L (d) None of the above
(d) None of the above
3. The conditional expansion facility of macro processor
3. The conditional expansion facility of macro processor is provided tois provided to (a) test a condition during the execution of the
(a) test a condition during the execution of the expanded programexpanded program (b) to expand certain model statements depending upon the value of a
(b) to expand certain model statements depending upon the value of a condition during thecondition during the execution of the expanded program
execution of the expanded program (c) to implement recursion
(c) to implement recursion
(d) to expand certain model statements depending upon the value of a
(d) to expand certain model statements depending upon the value of a condition during thecondition during the process of macro expansion.
process of macro expansion. 4. The expression (a * b) * c 4. The expression (a * b) * c op....op....
where 'op' is one of '+', '*'
where 'op' is one of '+', '*' and '’ (exponentiation) can be evaluated on a CPU with a singleand '’ (exponentiation) can be evaluated on a CPU with a single register without storing the value of (a *
register without storing the value of (a * b) if b) if (a
(a) ) 'o'op' p' is is '+'+' ' or or '*'*'' (b) (b) 'o'op' p' is is ‘’ ‘’ or or '*'*' ' (c(c) ) 'o'op' p' is is ‘' ‘' or or '+'+' ' (d(d) ) nonot t popossssibible le to to evevalaluauatete without storing
without storing
5. The language L, defined by the following grammar, allows use of real or integer data i 5. The language L, defined by the following grammar, allows use of real or integer data i nn expressions and assignment statements.
expressions and assignment statements.
<assign-stmt> :: LHS := E <assign-stmt> :: LHS := E E ::= E + T | T E ::= E + T | T T ::= T * V | V T ::= T * V | V V ::= id | (E) V ::= id | (E) (LHS) :: id (LHS) :: id It is required to convert expression
It is required to convert expression and assignment strings of L into postfix strings that use and assignment strings of L into postfix strings that use thethe type-specific operations (+,i) (+,r), (*,i), (*,r),
type-specific operations (+,i) (+,r), (*,i), (*,r), (:=,i) and (:=,r)(:=,i) and (:=,r)
Write a syntax directed translation scheme to convert expression
Write a syntax directed translation scheme to convert expression and assignment strings into theand assignment strings into the post-fix form. Y
post-fix form. You may assume that the name and type ou may assume that the name and type of a variable can be obtained by of a variable can be obtained by makingmaking the function calls' give type (id)
the function calls' give type (id) and give-name (id)* respectively.and give-name (id)* respectively. GATE 1998
GATE 1998
1. Which of the following statements is 1. Which of the following statements is true?true? (a) SLR parser is
(a) SLR parser is more powerful than LALRmore powerful than LALR (b) LALR parser is more
(b) LALR parser is more powerful than Canonical LR parser powerful than Canonical LR parser (c) Canonical LR parser is
(c) Canonical LR parser is more powerful than LALR parser.more powerful than LALR parser. (d) The parsers SLR, Canonical CR, and LALR have the
(d) The parsers SLR, Canonical CR, and LALR have the same power same power 2. Typ
2. Type checking is normally e checking is normally done duringdone during (a)
code optimization code optimization
3. (a) An identifier in a programming language consists of up
3. (a) An identifier in a programming language consists of up to six letters and digits to six letters and digits of which theof which the first character must be a l
first character must be a letteretter. Derive a regular expression . Derive a regular expression for the identifier.for the identifier. (b) Build an LL (1) parsing table for
(b) Build an LL (1) parsing table for the language defined by the LL(1) grammar with productionsthe language defined by the LL(1) grammar with productions Program
Program begin d begin d semi X semi X endend X
X d semi d semi X | X | sYsY Y
Y semi s semi s Y |Y |
4. Let the attribute ‘val’ give the value of a
4. Let the attribute ‘val’ give the value of a binary number generated by S in the binary number generated by S in the followingfollowing grammar: grammar: S S L.L L.L | | LL L L LB LB | | BB B B 0 0 I I 11
For example, an input 101.101 give S.val =
For example, an input 101.101 give S.val = 5.6255.625
Construct a syntax directed translation scheme using only
Construct a syntax directed translation scheme using only synthesized attributes, to determinesynthesized attributes, to determine S.val.
S.val.
5. The number of tokens in the Fortran statement DO 10
5. The number of tokens in the Fortran statement DO 10 I = 1.25 isI = 1.25 is
((aa) ) 3 3 ((bb) ) 44 ((cc) ) 5 5 ((dd) ) NNoonne e oof f tthhee above
above 6. A
6. A grammar that is both left and grammar that is both left and right recursive for a non-terminal, isright recursive for a non-terminal, is (a) Ambiguous
(a) Ambiguous (b) Unambiguous (b) Unambiguous
(c) Information is not sufficient to decide whether it
(c) Information is not sufficient to decide whether it is ambiguous or unambiguousis ambiguous or unambiguous (d) None of the above
(d) None of the above
7. The number of tokens in the following C
7. The number of tokens in the following C statementstatement printf("i=%d, &i=%x", i, &i);
printf("i=%d, &i=%x", i, &i); is
is (a)
(a) 3 3 (b) (b) 26 26 (c) (c) 10 10 (d) (d) 2121 8. Which of the following derivations does a
8. Which of the following derivations does a top-down parser use while parsing an input string?top-down parser use while parsing an input string? The input is assumed to be
The input is assumed to be scanned in left to right order.scanned in left to right order.
((aa) ) LLeeffttmmoosst t ddeerriivvaattiioonn ((bb) ) LLeeffttmmoosst t ddeerriivvaattiioon n ttrraacceed d oouut t iinn reverse
reverse
((cc) ) RRiigghhttmmoosst t ddeerriivvaattiioonn ((dd) ) RRiigghhttmmoosst t ddeerriivvaattiioon n ttrraacceed d oouut t iinn reverse
reverse
9. Given the following expression grammar: 9. Given the following expression grammar:
E
E E * F E * F | F + | F + E | FE | F F
F F F – – | id| id Which of the following is true?
Which of the following is true?
((aa) ) * * hhaas s hhiigghheer r pprreecceeddeenncce e tthhaan n ++ ((bb) ) - - hhaas s hhiigghheer r pprreecceeddeenncce e tthhaan n ** (c)
(c) + + and and - - have have same same precedence precedence (d) (d) + + has has higher higher precedence precedence than than ** 10. Consider the syntax directed translation scheme (SDTS)
10. Consider the syntax directed translation scheme (SDTS) given in the following. Assumegiven in the following. Assume attribute evaluation with bottom-up parsing, i.e., attributes are evaluated immediately after a attribute evaluation with bottom-up parsing, i.e., attributes are evaluated immediately after a reduction.
reduction. E
E EE11* T {E.val = E* T {E.val = E11. val * T. val}. val * T. val}
E
T
T F F – – TT11{T.val = F.val – T{T.val = F.val – T11. val}. val}
T
T F {TF {T. val = F. val = F. val}. val} F
F 2 {F2 {F. val . val =2}=2} F
F 4 {F4 {F. val . val =4}=4} (a) Using this SDTS, c
(a) Using this SDTS, construct a parse tree for the expressiononstruct a parse tree for the expression 4 – 2 – 4 * 2
4 – 2 – 4 * 2
and also compute its E.val. and also compute its E.val. (b) It is required to
(b) It is required to compute the total number of reductions performed to parse a given compute the total number of reductions performed to parse a given input.input. Using synthesized attributes only, modify the SDTS
Using synthesized attributes only, modify the SDTS given, without changing the grammar, to findgiven, without changing the grammar, to find E.red, the number of reductions performed while reducing an input to
E.red, the number of reductions performed while reducing an input to E.E. GATE 1999
GATE 1999
1. Which of the following is
1. Which of the following is the most powerful parsing method?the most powerful parsing method? (a)
(a) LL(1) LL(1) (b) (b) Canonical Canonical LR LR (c) (c) SLR SLR (d) (d) LALRLALR 2. The number of tokens in the Fortran statement DO 10
2. The number of tokens in the Fortran statement DO 10 I = 1.25 isI = 1.25 is
((aa) ) 3 3 ((bb) ) 44 ((cc) ) 5 5 ((dd) ) NNoonne e oof f tthhee above
above GATE 2001 GATE 2001
1. The process of assigning load addresses to the v
1. The process of assigning load addresses to the various parts of the program and adjusting thearious parts of the program and adjusting the code and date in the program to reflect the
code and date in the program to reflect the assigned addresses is calledassigned addresses is called
((aa) ) AAsssseemmbblly y ((bb) ) PPaarrssiinngg ((cc) ) RReellooccaattiioon n ((dd) ) SSyymmbbooll resolution
resolution
2. Which of the following statements is
2. Which of the following statements is false?false?
(a) An unambiguous grammar has same leftmost and rightmost derivation (a) An unambiguous grammar has same leftmost and rightmost derivation (b) An LL(1) parser is a top-down parser
(b) An LL(1) parser is a top-down parser (c) LALR is more powerful than SLR (c) LALR is more powerful than SLR
(d) An ambiguous grammar can never be LR(k) for any (d) An ambiguous grammar can never be LR(k) for any kk
3. Consider the following grammar with terminal
3. Consider the following grammar with terminal alphabetalphabet
∑
∑
{ {
aa,,( ( ))
,, ,,+ *+,,*}}
end start symbol E. Theend start symbol E. The production rules of the grammar are:production rules of the grammar are: E E aAaA E (E) E (E) A A +E+E A A *E*E A A
(a) Compute the FIRST and FOLLOW sets for E
(a) Compute the FIRST and FOLLOW sets for E and A.and A. (b) Complete the LL(1) parse table for the grammar. (b) Complete the LL(1) parse table for the grammar. 4. The syntax of the repeat-until statement is given by
4. The syntax of the repeat-until statement is given by the following grammar the following grammar S
S repeat repeat SS11 until Euntil E
Where E stands for expressions, S and S
Where E stands for expressions, S and S11 stand for statement. The non-terminals S and Sstand for statement. The non-terminals S and S11 havehave
an attribute code that represents generated code. The non-terminal E has two attributes. The an attribute code that represents generated code. The non-terminal E has two attributes. The attribute code represents generated code to evaluate the expression and store its truth value in attribute code represents generated code to evaluate the expression and store its truth value in aa distinct variable, and the attribute varName contains the name of
distinct variable, and the attribute varName contains the name of the variable in which the truththe variable in which the truth value is stored? The truth-value stored in the variable is 1 if E is true, 0 if E is false.
value is stored? The truth-value stored in the variable is 1 if E is true, 0 if E is false.
Give a syntax-directed definition to generate three-address code for the repeat-until statement. Give a syntax-directed definition to generate three-address code for the repeat-until statement. Assume that you can call a fun
Assume that you can call a function newlabel( ) that retction newlabel( ) that returns a distinct label for a statemenurns a distinct label for a statement. Uset. Use the operator ‘\\’ to concatenate two strings and the function gen(s) to generate a line containing the operator ‘\\’ to concatenate two strings and the function gen(s) to generate a line containing the string s.
5. (a) Remove left-recursion from the
5. (a) Remove left-recursion from the following grammar:following grammar: S
S Sa| Sb Sa| Sb | a | a | b| b
(b) Consider the following grammar: (b) Consider the following grammar:
S
S aSbS aSbS | b| bSaS |SaS | Construct all possible parse trees for
Construct all possible parse trees for the string abab. Is the grammar ambiguous?the string abab. Is the grammar ambiguous? GATE 2002
GATE 2002
1. (a) Construct all the parse trees corresponding to i + j * k for the grammar 1. (a) Construct all the parse trees corresponding to i + j * k for the grammar
E E E E + + EE E E*E E E*E E E idid
(b) In this grammar, what is the precedence of the two operators * and (b) In this grammar, what is the precedence of the two operators * and +?+? (c) If only one parse tree
(c) If only one parse tree is desired for any string is desired for any string in the same language, what changes are to bein the same language, what changes are to be made so that the resulting LALR(1) grammar is
made so that the resulting LALR(1) grammar is non-ambiguous?non-ambiguous? GATE 2003
GATE 2003
1. Which of the following suffices to convert
1. Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar?an arbitrary CFG to an LL(1) grammar?
((aa) ) RReemmoovviinng g lleefft t rreeccuurrssiioon n aalloonnee ((bb) ) FFaaccttoorriinng g tthhe e ggrraammmmaar r aalloonnee (c) R
(c) Removiemoving lng left reft recursecursion aion and fnd factoractoring ting the ghe grammarrammar (d) N(d) None one of tof the ahe abovebove 2. Assume that the SLR parser for a grammar G
2. Assume that the SLR parser for a grammar G has nhas n11 states and the LALR parser for G states and the LALR parser for G has nhas n22
states. The relationship between n
states. The relationship between n11 and nand n22 isis
(a) n
(a) n11is necessarily less than nis necessarily less than n22 (b) n(b) n11is necessarily equal to nis necessarily equal to n22
(c) n
(c) n11is necessarily greater than nis necessarily greater than n22 (d) None of the above(d) None of the above
3. In a bottom-up evaluation of a syntax directed
3. In a bottom-up evaluation of a syntax directed definition, inherited attributes candefinition, inherited attributes can (a) Always be evaluated
(a) Always be evaluated (b) Be evaluated only if the
(b) Be evaluated only if the definition is L-attributeddefinition is L-attributed (c) Be evaluated only if
(c) Be evaluated only if the definition has synthesized attributesthe definition has synthesized attributes (d) Never be evaluated
(d) Never be evaluated
4. Which of the following statements is
4. Which of the following statements is FALSE?FALSE? (a) In statically typed languages, each variable i
(a) In statically typed languages, each variable in a program has a fixed n a program has a fixed typetype (b) In un-typed languages, values do not have any types
(b) In un-typed languages, values do not have any types (c) In dynamically typed l
(c) In dynamically typed languages, variables have no typesanguages, variables have no types
(d) In all statically typed languages, each variable in a program is associated with values of only a (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
single type during the execution of the program 5. Consider the grammar shown below
5. Consider the grammar shown below S i S i E t E t S S S S ' | ' | aa S' e S | e S' e S | e E E bb
In the predictive parse table, M, of
In the predictive parse table, M, of this grammar, the entries M[S', e] and M[S',$] respectively arethis grammar, the entries M[S', e] and M[S',$] respectively are ((aa) ) {{SS' ' e e S} S} aannd d {{SS' ' }} ((bb) ) {{SS' ' e e SS} } aannd d { { }}
((cc) ) {{SS' ' } } aannd d {{SS' ' }} ((dd) ) {{SS' ' e e SS, , SS' ' } } aannd d {{SS’ ’ }} 6. Consider the grammar
6. Consider the grammar shown below.shown below. S S CCCC C C cC cC | | dd This grammar is This grammar is ((aa) ) LLLL((11)) ((bb) ) SSLLRR((11) ) bbuut t nnoot t LLLL((11)) ((cc) ) LLAALLRR((11) ) bbuut t nnoot t SSLLRR((11)) ((dd) ) LLRR((11) ) bbuutt
not LALR(1) not LALR(1)
7. Consider the translation
7. Consider the translation scheme shown below.scheme shown below. S
S TRTR R
R + T + T {print(‘+’);} R {print(‘+’);} R || T
T numnum {print(num.val);}{print(num.val);} Here num is a token
Here num is a token that represents an integer andthat represents an integer and numnum.val represents the corresponding integer .val represents the corresponding integer value. For an input string '9 + 5 + 2', this translation scheme will print
value. For an input string '9 + 5 + 2', this translation scheme will print (a)
(a) 9 9 + + 5 5 + + 2 2 (b) (b) 9 9 5 5 + + 2 2 + + (c) (c) 9 9 5 5 2 2 + + + + (d) (d) + + + + 9 9 5 5 22 8. Consider the syntax
8. Consider the syntax directed definition shown below.directed definition shown below. S
S idid : : = = E E {gen({gen(id.id.place = E.place;);}place = E.place;);} E
E EE11 + E+ E22 {t = newtemp( );{t = newtemp( );
gen(t = E
gen(t = E11.place + E.place + E22.place;);.place;);
E.place = t;} E.place = t;} E
E id id {E.place ={E.place = id.id.place;}place;}
Here, gen is a function that generates the output code, and newtemp is
Here, gen is a function that generates the output code, and newtemp is a function that returns thea function that returns the name of a new temporary variable on every
name of a new temporary variable on every call. Assume that ti's are the temporary variablecall. Assume that ti's are the temporary variable names generated by newtemp. For the statement 'X : = Y +
names generated by newtemp. For the statement 'X : = Y + Z', the 3-address code sequenceZ', the 3-address code sequence generated by this definition is
generated by this definition is
((aa))XX==YY ++ ZZ ((bb))tt11 = Y + Z; X = t= Y + Z; X = t11
(c) t
(c) t11 = Y; t= Y; t22 = t= t11 + Z; X = t+ Z; X = t22 (d) t(d) t11 = Y; t= Y; t22= Z; t= Z; t33= t= t11 + t+ t22; X = t; X = t33
GATE 2004 GATE 2004
1. Which of the following grammar rules
1. Which of the following grammar rules violate the requirements of an operator grammar? Pviolate the requirements of an operator grammar? P, Q,, Q, R are nonterminals, and r,s,t are terminals.
R are nonterminals, and r,s,t are terminals.
((ii) ) P P Q Q RR ((iiii) ) P P Q Q s s RR ((iiiiii) ) P P ((iivv) ) P P Q Q t t R R r r ((aa) ) ((ii) ) oonnllyy ((bb) ) ((ii) ) aannd d ((iiiiii) ) oonnllyy ((cc) ) ((iiii) ) aannd d ((iiiiii) ) oonnlly y ((dd) ) ((iiiiii) ) aannd d ((iivv)) only
only
2. Consider a program P that consists
2. Consider a program P that consists of two source modules Mof two source modules M11 and Mand M22 contained in two differentcontained in two different
files. If M
files. If M11contains a reference to a function defined in Mcontains a reference to a function defined in M22the reference will be resolved atthe reference will be resolved at
((aa) ) EEddiit t ttiimmee ((bb) ) CCoommppiille e ttiimmee ((cc) ) LLiinnk k ttiimmee ((dd) ) LLooaad d ttiimmee 3. Consider
3. Consider the grammar the grammar rule E rule E EE11 – E– E22 for arithmetic expressions. The code generated isfor arithmetic expressions. The code generated is
targeted to a CPU having a single
targeted to a CPU having a single user register. The subtraction operauser register. The subtraction operation requires the firsttion requires the first operand to be in the register. If E
operand to be in the register. If E11 and Eand E22 do not have nay-common sub-expression, in order todo not have nay-common sub-expression, in order to
get the shortest possible code get the shortest possible code (a) E
(a) E11 should be evaluated firstshould be evaluated first
(b) E
(b) E22 should be evaluated firstshould be evaluated first
(c) Evaluation of E
(c) Evaluation of E11 and Eand E22 should necessarily be interleavedshould necessarily be interleaved
(d) Order to evaluation of E
(d) Order to evaluation of E11 and Eand E22 is of no consequenceis of no consequence
4. Consider the grammar with the following translation rules
4. Consider the grammar with the following translation rules and E as the start and E as the start symbol.symbol. E
E EE11 # # T T { { E.value E.value = = EE11.value * T.value }.value * T.value }
I
I T T { { E.value E.value = = TT.value .value }} T
T TT11 & & F F { T{ T.value .value = = TT11.value * F.value }.value * F.value }
I
I F F { { TT.value .value = = FF.value .value }} F
F num num { { FF.value .value = = num.value num.value }} Compute
Compute E.value E.value for for the the root root of of the the parse parse tree tree for for the the expressions: expressions: 2 2 # # 3 3 & & 5 5 # # 6 6 & & 4.4. (a)
(a) 200 200 (b) (b) 180 180 (c) (c) 160 160 (d) (d) 4040 5. The grammar
(a
(a) ) amambibigguouous us (b(b) ) leleftft-r-rececuursrsivivee (c) (c) ririgghtht-r-rececuursrsivive e (d(d) ) an an opopereratator or--grammar
grammar GATE 2005 GATE 2005
1. Consider the grammar: 1. Consider the grammar:
S
S (S) (S) | | aa
Let the number of states in SLR
Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar (1), LR(1) and LALR(1) parsers for the grammar be nbe n11, n, n22and nand n33
respectively
respectively. The following . The following relationship holds good:relationship holds good: (a) n
(a) n11< n< n22 < n< n33 (b) n(b) n11 = n= n33 < n< n22 (c) n(c) n11= n= n22 = n= n33 (d) n(d) n11 > n> n33 > n> n22
Statement for Linked Answer Questions 2a & Statement for Linked Answer Questions 2a & 2b2b
Consider the following expression grammar. The semantic rules for expression evaluation are Consider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production.
stated next to each grammar production. E
E number number E.val = E.val = numbernumber.va|.va| |
| E E '+' '+' E E EE(1)(1).val = E.val = E(2)(2).val + E.val + E(3)(3).val.val
|
| E E ’’ ’’ E E EE(1)(1).val = E.val = E{2){2).val E.val E(3)(3).val.val
2(A). The above grammar and the semantic rules are
2(A). The above grammar and the semantic rules are fed to a yacc tool (which fed to a yacc tool (which is an LALR(1)is an LALR(1) parser generator) for parsing and evaluating arithmetic expressions. Which one of
parser generator) for parsing and evaluating arithmetic expressions. Which one of the following isthe following is true about the action of yacc for the
true about the action of yacc for the given grammar?given grammar? (a) It detects recursion and eliminates recursi
(a) It detects recursion and eliminates recursionon (b) It detects reduce-reduce conflict, and resolves (b) It detects reduce-reduce conflict, and resolves (c) It detects shift-reduce conflict, and resolves
(c) It detects shift-reduce conflict, and resolves the conflict in favor of the conflict in favor of a shift over a reduce action.a shift over a reduce action. (d) It detects shift-reduce conflict, and resolves
(d) It detects shift-reduce conflict, and resolves the conflict in favor of a the conflict in favor of a reduce over a shift action.reduce over a shift action. 2(B). Assume the conflicts in Part (a) of
2(B). Assume the conflicts in Part (a) of this question are resolved and an LALR(1) parser this question are resolved and an LALR(1) parser isis generated for parsing arithmetic expressions as per the given
generated for parsing arithmetic expressions as per the given grammar. Consider an expressiongrammar. Consider an expression 3
3 2 + 1. What preced2 + 1. What precedence and associativity properties does ence and associativity properties does the generated pthe generated parser realize?arser realize? (a) Equal precedence and left associativity; expression is
(a) Equal precedence and left associativity; expression is evaluated to 7evaluated to 7 (b) Equal precedence and right associativity; expression is
(b) Equal precedence and right associativity; expression is evaluated to 9evaluated to 9 (c) Precedence of ‘x’ is higher than that
(c) Precedence of ‘x’ is higher than that of ‘+’, and both operators are left associative; of ‘+’, and both operators are left associative; expressionexpression is evaluated to 7
is evaluated to 7
(d) Precedence of ’+’ is higher than that of
(d) Precedence of ’+’ is higher than that of ‘x’, arid both operators are left associative; ‘x’, arid both operators are left associative; expressionexpression is evaluated to 9
is evaluated to 9 GATE 2006 GATE 2006
1. Consider the following
1. Consider the following grammar.grammar. S S S S * * EE S S EE E E F F + + EE E E FF F F idid
Consider the following LR(0) items corresponding to the grammar
Consider the following LR(0) items corresponding to the grammar above.above. ((ii) ) S S S S * * ..EE ((iiii) ) E E FF. . + + EE ((iiiiii) ) E E F F + + ..EE Given the items above, which two of
Given the items above, which two of them will appear in the sthem will appear in the same set in the canonical sets-of-ame set in the canonical sets-of-items for the grammar?
items for the grammar?
((aa) ) ((ii) ) aannd d ((iiii) ) ((bb) ) ((iiii) ) aannd d ((iiiiii)) ((cc) ) ((ii) ) aannd d ((iiiiii) ) ((dd) ) NNoonne e oof f tthhee above
above
2. Consider these two functions and two statements S1 and S2
S1: The transformation form work1 to work2 is
S1: The transformation form work1 to work2 is valid, i.e., for any program state valid, i.e., for any program state and inputand input arguments, work2 will compute the same output and have the
arguments, work2 will compute the same output and have the same effect on program state assame effect on program state as work1
work1
S2: All the transformations applied to work1 to get work2 will
S2: All the transformations applied to work1 to get work2 will always improve the performance (i.ealways improve the performance (i.e reduce CPU time) of work2 compared to
reduce CPU time) of work2 compared to work1work1
((aa) ) SS1 1 iis s ffaallsse e aannd d SS2 2 iis s ffaallssee ((bb) ) SS1 1 iis s ffaallsse e aannd d SS2 2 iis s ttrruuee ((cc) ) SS1 1 iis s ttrruue e aannd d SS2 2 iis s ffaallssee ((dd) ) SS1 1 iis s ttrruue e aannd d SS2 2 iis s ttrruuee 3. Consider the following code written in a
3. Consider the following code written in a pass-by-reference language like FORTRAN and thesepass-by-reference language like FORTRAN and these statements about the code.
statements about the code.
S1: The compiler will generate code to
S1: The compiler will generate code to allocate a temporary nameless cell, initializallocate a temporary nameless cell, initialize it to 13, ande it to 13, and pass the address of the cell
pass the address of the cell swapswap S2: On execution the code will
S2: On execution the code will generate a runtime error on line L1generate a runtime error on line L1 S3: On execution the code will
S3: On execution the code will generate a runtime error on line L2generate a runtime error on line L2 S4: The program will print 13 and
S4: The program will print 13 and 88 S5: The program will print 13 and S5: The program will print 13 and -2-2
Exactly the following set of statement(s) is correct: Exactly the following set of statement(s) is correct:
((aa) ) SS1 1 aannd d SS22 ((bb) ) SS1 1 aannd d SS44 ((cc) ) SS3 3 ((dd) ) SS1 1 aannd d SS55 4. Consider this C code to s
4. Consider this C code to swap two integers and these five statements: the codewap two integers and these five statements: the code
S1: will generate a compilation error S1: will generate a compilation error
S2: may generate a segmentation fault at runtime depending on the arguments passed S2: may generate a segmentation fault at runtime depending on the arguments passed S3: correctly implements the swap procedure for
S3: correctly implements the swap procedure for all input pointers referring to integers stored inall input pointers referring to integers stored in memory locations accessible to the process
memory locations accessible to the process S4: implements the swap procedure correctly for
S4: implements the swap procedure correctly for some but not all valid some but not all valid input pointersinput pointers S5: may add or subtract integers and pointers.
S5: may add or subtract integers and pointers.
5. Consider the following grammar: 5. Consider the following grammar:
S S FRFR R *S| R *S| F F idid
In the predictive parser table, M, of
In the predictive parser table, M, of the grammar the entries M[S,id] and M[R,$] the grammar the entries M[S,id] and M[R,$] respectivelyrespectively.. ((aa) ) {{S S FFRR} } aannd d {{R R }} ((bb) ) {{S S FFKK} } aannd d { { }}
((cc) ) {{S S FFAA} } aannd d {{R R **SS}} ((dd) ) {{F F iidd} } aannd d {{R R }} 6. Consider the following translation scheme.
6. Consider the following translation scheme. S S ERER R R *E *E {print {print (‘*’);} R|(‘*’);} R| E E F + F + E {print(‘+’);} E {print(‘+’);} |F|F F
F (S)|id (S)|id {print(id.value);}{print(id.value);} Here id is a
Here id is a token that represents an integer and id.value represents the corresponding integer token that represents an integer and id.value represents the corresponding integer value. For an input ‘2 * 3 + 4’, this translation scheme prints
value. For an input ‘2 * 3 + 4’, this translation scheme prints
((aa) ) 2 2 * * 3 3 + + 44 ((bb) ) 2 2 * * + + 3 3 44 ((cc) ) 2 2 3 3 * * 4 4 ++ ((dd) ) 2 2 3 3 4 4 + + ** 7. Consider the following C code segment
7. Consider the following C code segment for (i - 0, i<n; i++) {
for (i - 0, i<n; i++) { for (j=0; j<n; j++) { for (j=0; j<n; j++) { if (i%2) { if (i%2) { x += (4*j + 5*i); x += (4*j + 5*i); y += (7 + 4*j); y += (7 + 4*j); }} }} }}
Which one of the following is
Which one of the following is false?false?
(a) The code contains loop invariant computation (a) The code contains loop invariant computation (b) There is scope of common
(b) There is scope of common sub-expression elimination in this codesub-expression elimination in this code (c) There is scope of strength reduction in
(c) There is scope of strength reduction in this codethis code (d) There is scope of dead code elim
(d) There is scope of dead code elimination in this codeination in this code
GATE 2007 GATE 2007
1. Which one of the following is
1. Which one of the following is a top-down parser?a top-down parser? (a)
(a) Recursive Recursive descent descent parser. parser. (b) (b) Operator Operator precedence precedence parserparser.. (c)
(c) An An LR(k) LR(k) parser. parser. (d) (d) An An LALR(k) LALR(k) parser.parser. 2. Consider the grammar with non-terminals N
2. Consider the grammar with non-terminals N ={S,C,S={S,C,S11}, terminals T = {a,b,i,t,e}, with S }, terminals T = {a,b,i,t,e}, with S as theas the
start symbol, and the following set of start symbol, and the following set of rules:rules:
S iCtSS S iCtSS11|a|a S S11 eS |eS | C C bb
The grammar is NOT LL(1) because: The grammar is NOT LL(1) because:
((aa) ) iit t iis s lleefft t rreeccuurrssiivvee ((bb) ) iit t iis s rriigghht t rreeccuurrssiivvee ((cc) ) iit t iis s aammbbiigguuoouuss ((dd) ) iit t iis s nnoott context-free.
context-free.
3. Consider the following two statements: 3. Consider the following two statements:
P: Every regular grammar is LL(1) P: Every regular grammar is LL(1)
Q: Every regular set has a LR(1) grammar Q: Every regular set has a LR(1) grammar Which of the following is TRUE?
Which of the following is TRUE?
((cc) ) P P iis s ffaallsse e aannd d Q Q iis s ttrruuee ((dd) ) BBootth h P P aannd d Q Q aarre e ffaallssee GATE 2008
GATE 2008
1. Which of the following describes a
1. Which of the following describes a handle (as applicable to LR-parsing) appropriately?handle (as applicable to LR-parsing) appropriately? (a) It is the position
(a) It is the position in a sentential form where the next sin a sentential form where the next shift or reduce operation will occur hift or reduce operation will occur (b) It is non-terminal whose production will
(b) It is non-terminal whose production will be used for reduction in the next be used for reduction in the next stepstep (c) It is
(c) It is a production that may be used for reduction in a production that may be used for reduction in a future step along with a position ia future step along with a position in then the sentential form where the next shift or reduce operation will
sentential form where the next shift or reduce operation will occur occur (d) It is the production p
(d) It is the production p that will be used for reduction in that will be used for reduction in the next step along with a position in the next step along with a position in thethe sentential form where the right hand side of the
sentential form where the right hand side of the production may be foundproduction may be found 2. Some code optimizations are carried
2. Some code optimizations are carried out on the intermediate code becauseout on the intermediate code because (a) They enhance the portability of the compiler to
(a) They enhance the portability of the compiler to other target processorsother target processors (b) Program analysis is
(b) Program analysis is more accurate on intermediate code than on machine codemore accurate on intermediate code than on machine code (c) The information from dataflow analysis cannot otherwise be used
(c) The information from dataflow analysis cannot otherwise be used for optimizationfor optimization (d) The information from the front end cannot otherwise be used for
(d) The information from the front end cannot otherwise be used for optimizationoptimization 3. An LALR(1) parser for a grammar G can have
3. An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if shift-reduce (S-R) conflicts if and only if and only if (a) The SLR(1) parser for G has S-R conflicts
(a) The SLR(1) parser for G has S-R conflicts (b) The LR(1) parser for G has
(b) The LR(1) parser for G has S-R conflictsS-R conflicts (c) The LR(0) parser for G has S-R conflicts (c) The LR(0) parser for G has S-R conflicts (d) The LALR(1) parser for G has