GATE Compiler Design 93-2009
Short Description
Download GATE Compiler Design 93-2009...
Description
GATEFORUM-Computer GATEFORUM-Computer Science & Engineering Compiler Design-GATE Question Gate Questions: - Compiler Design GATE 1992
1. Fill in the blanks: (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 an assembler is ________ 2. For a context-free grammar, FOLLOW(A) is the set of terminals that can appear immediately to 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 sentential" and "right most sentential" respectively in the definition of FOLLOW(A). Which of the following statements is/are true? (a) FOLLOW(A) & FOLLOW (A) may be different (b) FOLLOW(A) & FOLLOW (A) are always the same. (c) All the three sets are identical. (d) All the three sets are different. 3. Consider the SLR(1) and LALR (1) parsing tables for a context free grammar. Which of the following statements is/are true? (a) (a) The The go to part part of both both tabl tables es may may be be dif diffe fere rent nt.. (b) (b) The The shi shift ft entr entrie ies s are are iden identi tica call in in bot both h the tables. (c) The reduce reduce entrie entries s in the the tabl tables es may may be dif differ ferent ent.. (d) The error error entri entries es in the tables tables may may be different. 4. Write short answers to the following: (i) Which of the following macros can put a macro assembler into an infinite loop? .MACRI M1,X .MACRO M2, X ..IF EQ,X .IF EQ, X M1 X+1 M2X .ENDC .ENDC .IF NE, X .IF NE, X WORD X .WORD X + 1 .ENDC .ENDC .ENDM .ENDM Give an example of a call that does so. (ii) Mention the pass number for each of the following activities that occur in a two pass assembler (a) Object code generation (b) Literals added literal table (c) Listing printed (d) Address resolution of local symbols 5.(a). Write syntax directed definitions (semantic rules) for the following grammar to add the type of each identifier to its entry in the symbol table during semantic analysis. Rewriting the grammar is not permitted and semantic rules are to be added to the ends of productions only. only. D TL; T int T real L L, id L id 5 (b). Write 3-address intermediate code (quadruples) for the following boolean expression in the sequence as it would be generated by a compiler. Partial evaluation of Boolean expressions is not permitted. Assume the usual rules or precedence of the operators.
(a + b) > (c + d) or a > c and b < d GATE 1993
1. A simple two-pass assembler does the following in the first pass: (a) It allocates space for the literals. (b) It computes the total length of the program (c) It builds the symbol table for the symbols and their values. (d) It generates code for all the load and store register instructions. (e) None of the above.
2. For the program segment given below, which of the following are true? program main (output); type link = ^data; data = record d : real; n : link end; var ptr : link; begin new (ptr); ptr :=nil; ptr^.d:=5.2; write In(ptr) end. (a) The program leads to compile time error (b) The program leads to run time error (c) The program outputs 5.2 (d) The program produces error relating to nil pointer dereferencing (e) None of the above 3. A part of the system software, which under all circumstances must reside in the main memory, is: (a) text editor (b) assembler (c) linker (d) loader (e) none of the above GATE 1994
1 Generation of intermediate code based on an abstract machine model is useful in compilers because (a) It makes implementation of lexical analysis and syntax analysis easier (b) Syntax-directed translations can be written for intermediate code generation (c) It enhances the portability of the front end of the compiler (d) It is not possible to generate code for real machines directly from high level language programs 2. State whether the following statements are True or False with reasons for your answer: (a) Coroutine is just another name for a subroutine. (b) A two two pass assembler uses its machine opcode table in the first pass of assembly. 3. State whether the following statements are True or False with reasons for your answer (a) A subroutine subroutine cannot always be used to replace a macro in an assembly language program. (b) A symbol symbol declared as 'external' in assembly language is assigned an address outside the program by the assembler itself.
Consider the following grammar: A id := E E E + T|T T (E)|id Write a syntax directed translation to generate code using this grammar for the computer described above. GATE 1995
1. In some programming languages, an identifier is permitted to be a letter following by any number of letters or digits. If L and D denote the sets of letters and digits respectively, which of the following expressions defines an identifier? (a) (L D)+ (b) L(L D)* (c) (L.D)* (d) L.(L.D)* 2. A linker is given object modules for a set of programs that were compiled separately. What information need to be included in an object module? m odule? (a) Object code (b) Relocation bits (c) Names and locations of all external symbols defined in the object module (d) Absolute addresses of internal symbols 3. Construct the LL(1) table for the following grammar. 1. Expr _Expr 2. Expr (Expr) 3. Expr Var Expr Tail Tail 4. ExprTail _Expr 5. ExprTail 6. Var Id Var Tail 7. VarTail VarTail (Expr) 8. VarTail 9. Goal Expr$ 4. (a) Translate the arithmetic expression a* – (b + c) into syntax tree. (b) A grammar is said to have cycles if it is the case that A + A Show that no grammar that has cycles can be LL(I). 5. Relative mode of addressing is most relevant to writing (a) coroutines (b) position - independent code interrupt 6. The correct matching for the following pairs is (a) (a) Acti Activa vati tion on reco record rd (1) (1) Lin Linki king ng load loader er (b) (b) Loca Locati tio on cou counter nter (2) (2) Garba arbag ge collection (c) (c) Ref Refer eren ence ce coun counts ts (3) (3) Sub Subro rout utin ine e cal calll (d) Address (4) Assembler relocation (a) A – 3 B – 4 C – 1 D – 2 (c) A – 4 B – 3 C – 2 D – 1 GATE 1997
1. In the following grammar X: := X ⊕
Y Y
(c) shareable code
(b) A – 4 B – 3 C – 1 D – 2 (d) A – 3 B – 4 C – 2 D – 1
(d)
Y: := Z *
Y Z
Z: : = id
Which of the following is true? (a) (a) ‘’‘’ is left left asso associ ciat ativ ive e whi while le '*' '*' is is rig right ht asso associ ciat ative ive (c) (c) ‘’‘’ is righ rightt ass assoc ocia iati tive ve whil while e ‘*' ‘*' is left left asso associ ciat ativ ive e
(b) (b) Bot Both h ‘’‘’ and and '*' '*' is left left asso associ ciat ative ive (d) (d) Non None e of of the the abov above e
2. A language L allows declaration of arrays whose sizes are not known during compilation. It is required to make efficient use of memory. Which one of the following is true? (a) A compiler compiler using static memory allocation can be written for L (b) A compiler compiler cannot be written for L; an interpreter must be used. (c) A compiler using dynamic memory allocation can be written for L (d) None of the above 3. The conditional expansion facility of macro processor is provided to (a) test a condition during the execution of the expanded program (b) to expand certain model statements depending upon the value of a condition during the execution of the expanded program (c) to implement recursion (d) to expand certain model statements depending upon the value of a condition during the process of macro expansion. 4. The expression (a * b) * c op.... where 'op' is one of '+', '*' and '’ (exponentiation) can be evaluated on a CPU with a single register without storing the value of (a * b) if (a) (a) 'op' 'op' is '+' '+' or '*' '*' (b) (b) 'op' 'op' is ‘’ or '*' '*' (c) (c) 'op' 'op' is ‘' or '+' '+' (d) (d) not not poss possib ible le to eval evalua uate te without storing
5. The language L, defined by the following grammar, allows use of real or integer data i n expressions and assignment statements. :: LHS := E E ::= E + T | T T ::= T * V | V V ::= id | (E) (LHS) :: id It is required to convert expression and assignment strings of L into postfix strings that use the type-specific operations (+,i) (+,r), (*,i), (*,r), (:=,i) and (:=,r) Write a syntax directed translation scheme to convert expression and assignment strings into the post-fix form. You You may assume that the name and type of a variable can be obtained by making the function calls' give type (id) and give-name (id)* respectively. GATE 1998
1. Which of the following statements is true? (a) SLR parser is more powerful than LALR (b) LALR parser is more powerful than Canonical LR parser (c) Canonical LR parser is more powerful than LALR parser. (d) The parsers SLR, Canonical CR, and LALR have the same power 2. Type Type checking is normally done during (a) lexical analysis (b) syntax analysis
(c) syntax directed translation
(d)
code optimization 3. (a) An identifier in a programming language consists of up to six letters and digits of which the first character must be a letter l etter.. Derive a regular expression for the identifier. (b) Build an LL (1) parsing table for the language defined by the LL(1) grammar with productions Program begin d semi X end X d semi X | sY Y semi s Y | 4. Let the attribute ‘val’ give the value of a binary number generated by S in the following grammar: S L.L | L L LB | B B 0I1 For example, an input 101.101 give S.val = 5.625 Construct a syntax directed translation scheme using only synthesized attributes, to determine S.val. 5. The number of tokens in the Fortran statement DO 10 I = 1.25 is (a) 3 (b) 4 (c) 5 above
(d) None of the
6. A grammar that is both left and right recursive for a non-terminal, is (a) Ambiguous (b) Unambiguous (c) Information is not sufficient to decide whether it is ambiguous or unambiguous (d) None of the above 7. The number of tokens in the following C statement printf("i=%d, &i=%x", i, &i); is (a) 3 (b) 26
(c) 10
(d) 21
8. Which of the following derivations does a top-down parser use while parsing an input string? The input is assumed to be scanned in left to right order. (a) Leftmost derivation (b) Leftmost derivation traced out in reverse (c) Rightmost derivation (d) Rightmost derivation traced out in reverse 9. Given the following expression grammar: E E * F| F +E | F F F – | id Which of the following is true? (a) * has higher precedence than + (c) + and - have same precedence
(b) - has higher precedence than * (d) + has higher precedence than *
10. Consider the syntax directed translation scheme (SDTS) given in the following. Assume attribute evaluation with bottom-up parsing, i.e., attributes are evaluated immediately after a reduction. E E1 * T {E.val = E1. val * T. val} E T {E. val = T. val}
T F – T1 {T.val = F.val – T1. val} T F {T. {T. val = F. F. val} F 2 {F. {F. val =2} F 4 {F. {F. val =4} (a) Using this SDTS, cconstruct onstruct a parse tree for the expression 4–2–4*2 and also compute its E.val. (b) It is required to compute the total number of reductions performed to parse a given input. Using synthesized attributes only, modify the SDTS given, without changing the grammar, to find E.red, the number of reductions performed while reducing an input to E. GATE 1999
1. Which of the following is the most powerful parsing method? (a) LL(1) (b) Canonical LR (c) SLR
(d) LALR
2. The number of tokens in the Fortran statement DO 10 I = 1.25 is (a) 3 (b) 4 (c) 5 above
(d) None of the
GATE 2001
1. The process of assigning load addresses to the various v arious parts of the program and adjusting the code and date in the program to reflect the assigned addresses is called (a) Assembly (b) Parsing (c) Relocation (d) Symbol resolution 2. Which of the following statements is false? (a) An unambiguous grammar has same leftmost and rightmost derivation (b) An LL(1) parser is a top-down parser (c) LALR is more powerful than SLR (d) An ambiguous grammar can never be LR(k) for any k 3. Consider the following grammar with terminal alphabet ∑ production rules of the grammar are: E aA E (E) A +E A *E A (a) Compute the FIRST and FOLLOW sets for E and A. (b) Complete the LL(1) parse table for the grammar.
{ a, ( ,) , + ,* }
end start symbol E. The
4. The syntax of the repeat-until statement is given by the following grammar S repeat S 1 until E Where E stands for expressions, S and S 1 stand for statement. The non-terminals S and S 1 have 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 a distinct variable, and the attribute varName contains the name of the 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. Give a syntax-directed definition to generate three-address code for the repeat-until statement. Assume that you can call a function function newlabel( ) that returns returns a distinct label for a statement. statement. Use 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 following grammar: S Sa| Sb | a | b (b) Consider the following grammar: S aSbS | bSaS bSaS | Construct all possible parse trees for the string abab. Is the grammar ambiguous? GATE 2002
1. (a) Construct all the parse trees corresponding to i + j * k for the grammar E E+E E E*E E id (b) In this grammar, what is the precedence of the two operators * and +? (c) If only one parse tree is desired for any string in the same language, what changes are to be made so that the resulting LALR(1) grammar is non-ambiguous? GATE 2003
1. Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar? (a) Removing left recursion alone (b) Factoring the grammar alone (c) Removi Removing ng left left recurs recursion ion and and factor factoring ing the the grammar grammar (d) None None of the the above above 2. Assume that the SLR parser for a grammar G has n 1 states and the LALR parser for G has n 2 states. The relationship between n 1 and n2 is (a) n1 is necessarily less than n 2 (b) n1 is necessarily equal to n 2 (c) n1 is necessarily greater than n 2 (d) None of the above 3. In a bottom-up evaluation of a syntax directed definition, inherited attributes can (a) Always be evaluated (b) Be evaluated only if the definition is L-attributed (c) Be evaluated only if the definition has synthesized attributes (d) Never be evaluated 4. Which of the following statements is FALSE? (a) In statically typed languages, each variable in i n a program has a fixed type (b) In un-typed languages, values do not have any types (c) In dynamically typed languages, l anguages, variables have no types (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 5. Consider the grammar shown below S i E t S S' | a S' e S | e E b In the predictive parse table, M, of this grammar, the entries M[S', e] and M[S',$] respectively are (a) {S' e S} and {S' } (b) {S' e S} and { } (c) {S' } and {S' } (d) {S' e S, S' } and {S’ } 6. Consider the grammar shown below. S CC C cC | d This grammar is (a) LL(1) (b) SLR(1) but not LL(1)
(c) LALR(1) but not SLR(1)
(d) LR(1) but
not LALR(1) 7. Consider the translation scheme shown below. S TR 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 (a) 9 + 5 + 2 (b) 9 5 + 2 + (c) 9 5 2 + + (d) + + 9 5 2 8. Consider the syntax directed definition shown below. S id : = E {gen( id.place = E.place;);} E E1 + E2 {t = newtemp( ); gen(t = E 1.place + E 2.place;); E.place = t;} E id {E.place = id.place;} Here, gen is a function that generates the output code, and newtemp is a function that returns the name of a new temporary variable on every call. Assume that ti's are the temporary variable names generated by newtemp. For the statement 'X : = Y + Z', the 3-address code sequence generated by this definition is (a) X = Y + Z (b) t1 = Y + Z; X = t 1 (c) t1 = Y; t2 = t1 + Z; X = t 2 (d) t1 = Y; t2 = Z; t 3 = t1 + t2; X = t 3 GATE 2004
1. Which of the following grammar rules violate the requirements of an operator grammar? P, P, Q, R are nonterminals, and r,s,t are terminals. (i ) P Q R (ii) P Q s R (iii) P (iv) P Q t R r (a) (i) only (b) (i) and (iii) only (c) (ii) and (iii) only (d) (iii) and (iv) only 2. Consider a program P that consists of two source modules M 1 and M2 contained in two different files. If M1 contains a reference to a function defined in M 2 the reference will be resolved at (a) Edit time (b) Compile time (c) Link time (d) Load time 3. Consider the grammar rule E E 1 – E2 for arithmetic expressions. The code generated is targeted to a CPU having a single user register. The subtraction operation operation requires the first operand to be in the register. If E 1 and E2 do not have nay-common sub-expression, in order to get the shortest possible code (a) E1 should be evaluated first (b) E2 should be evaluated first (c) Evaluation of E 1 and E2 should necessarily be interleaved (d) Order to evaluation of E 1 and E2 is of no consequence 4. Consider the grammar with the following translation rules and E as the start symbol. E E1 # T { E.value = E 1.value * T.value } I T { E.value = T.value } T T1 & F { T.value = T 1.value * F.value } I F { T.value = F.value } F num { F.value = num.value } Compute E.value for the root of the parse tree for the expressions: 2 # 3 & 5 # 6 & 4. (a) 200 (b) 180 (c) 160 (d) 40 5. The grammar A AA | ( A) | is not suitable for predictive-parsing because because the grammar is:
(a) (a) ambi ambig guous uous grammar
(b) (b) left left-r -rec ecu ursiv rsive e
(c) (c) rig right-r ht-rec ecu ursiv rsive e
(d) (d) an oper operat ator or--
GATE 2005
1. Consider the grammar: S (S) | a Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be n 1, n2 and n3 respectively. respectively. The following relationship holds good: (a) n1 < n2 < n3 (b) n1 = n3 < n2 (c) n1 = n2 = n3 (d) n1 > n3 > n2 Statement for Linked Answer Questions 2a & 2b
Consider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production. E number | E '+' E | E ’’ E
E.val = number.va| number.va| (1) E .val = E (2).val + E (3).val E(1).val = E{2).val E(3).val
2(A). 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? (a) It detects recursion and eliminates recursion recursi on (b) It detects reduce-reduce conflict, and resolves (c) It detects shift-reduce conflict, and resolves the conflict in favor of a shift over a reduce action. (d) It detects shift-reduce conflict, and resolves the conflict in favor of a reduce over a shift action. 2(B). 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 precedence and associativity properties does the generated parser parser realize? (a) Equal precedence and left associativity; expression is evaluated to 7 (b) Equal precedence and right associativity; expression is evaluated to 9 (c) Precedence of ‘x’ is higher than that of ‘+’, and both operators are left associative; expression is evaluated to 7 (d) Precedence of ’+’ is higher than that of ‘x’, arid both operators are left associative; expression is evaluated to 9 GATE 2006
1. Consider the following grammar. S S*E S E E F+E E F F id Consider the following LR(0) items corresponding to the grammar above. (i) S S * .E (ii) E F. + E (iii) E F + .E Given the items above, which two of them will appear in the same s ame set in the canonical sets-ofitems for the grammar? (a) (i) and (ii) (b) (ii) and (iii) (c) (i) and (iii) (d) None of the above 2. Consider these two functions and two statements S1 and S2 about them.
S1: The transformation form work1 to work2 is valid, i.e., for any program state and input arguments, work2 will compute the same output and have the same effect on program state as work1 S2: All the transformations applied to work1 to get work2 will always improve the performance (i.e reduce CPU time) of work2 compared to work1 (a) S1 is false and S2 is false (b) S1 is false and S2 is true (c) S1 is true and S2 is false (d) S1 is true and S2 is true 3. Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code.
S1: The compiler will generate code to allocate a temporary nameless cell, initialize initializ e it to 13, and pass the address of the cell swap S2: On execution the code will generate a runtime error on line L1 S3: On execution the code will generate a runtime error on line L2 S4: The program will print 13 and 8 S5: The program will print 13 and -2 Exactly the following set of statement(s) is correct: (a) S1 and S2 (b) S1 and S4 (c) S3 (d) S1 and S5 4. Consider this C code to swap s wap two integers and these five statements: the code
S1: will generate a compilation error S2: may generate a segmentation fault at runtime depending on the arguments passed S3: correctly implements the swap procedure for all input pointers referring to integers stored in memory locations accessible to the process S4: implements the swap procedure correctly for some but not all valid input pointers S5: may add or subtract integers and pointers. (a) S1 (b) S2 and S3 (c) S2 and S4 (d) S2 and S5
5. Consider the following grammar: S FR R *S| F id In the predictive parser table, M, of the grammar the entries M[S,id] and M[R,$] respectively. respectively. (a) {S FR} and {R } (b) {S FK} and { } (c) {S FA} and {R *S} (d) {F id} and {R } 6. Consider the following translation scheme. S ER R *E {print (‘*’);} R| E F + E {print(‘+’);} |F F (S)|id {print(id.value);} Here id is a token that represents an integer and id.value represents the corresponding integer value. For an input ‘2 * 3 + 4’, this translation scheme prints (a) 2 * 3 + 4 (b) 2 * + 3 4 (c) 2 3 * 4 + (d) 2 3 4 + * 7. Consider the following C code segment for (i - 0, i
View more...
Comments