gate compiler design 93-2009

Download GATE Compiler Design 93-2009

If you can't read please download the document

Upload: singhmanish1997

Post on 29-Oct-2015

870 views

Category:

Documents


0 download

TRANSCRIPT

GATEFORUM-Computer Science & EngineeringCompiler Design-GATE QuestionGate Questions: - Compiler DesignGATE 19921. 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) The go to part of both tables may be different.(b) The shift entries are identical in both the tables.(c) The reduce entries in the tables may be different.(d) The error entries in the tables 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, XM1 X+1M2X.ENDC.ENDC.IF NE, X.IF NE, XWORD X.WORD X + 1.ENDC.ENDC.ENDM.ENDMGive 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 symbols5.(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.D TL;T intT realL L, idL id5 (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 < dGATE 19931. 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 above3. 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 aboveGATE 19941 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 programs2. 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 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 cannot always be used to replace a macro in an assembly language program.(b) A symbol declared as 'external' in assembly language is assigned an address outside the program by the assembler itself.Consider the following grammar:A id := EE E + T|TT (E)|idWrite a syntax directed translation to generate code using this grammar for the computer described above.GATE 19951. 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?(a) Object code(b) Relocation bits(c) Names and locations of all external symbols defined in the object module(d) Absolute addresses of internal symbols3. Construct the LL(1) table for the following grammar.1. Expr _Expr2. Expr (Expr)3. Expr Var Expr Tail4. ExprTail _Expr5. ExprTail 6. Var Id Var Tail7. 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 +AShow 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 (c) shareable code(d) interrupt6. The correct matching for the following pairs is(a) Activation record(1) Linking loader(b) Location counter(2) Garbage collection(c) Reference counts(3) Subroutine call(d) Address relocation(4) Assembler(a) A 3 B 4 C 1 D 2(b) A 4 B 3 C 1 D 2(c) A 4 B 3 C 2 D 1(d) A 3 B 4 C 2 D 1GATE 19971. In the following grammarWhich of the following is true?(a) is left associative while '*' is right associative(b) Both and '*' is left associative(c) is right associative while *' is left associative(d) None of the above2. 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 using static memory allocation can be written for L(b) A 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 above3. 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) 'op' is '+' or '*'(b) 'op' is or '*' (c) 'op' is ' or '+' (d) not possible to evaluate without storing5. The language L, defined by the following grammar, allows use of real or integer data in expressions and assignment statements. :: LHS := E E ::= E + T | T T ::= T * V | V V ::= id | (E) (LHS) :: idIt 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 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 19981. 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 power2. Type checking is normally done during(a) lexical analysis (b) syntax analysis (c) syntax directed translation (d) code optimization3. (a) An identifier in a programming language consists of up to six letters and digits of which the first character must be a letter. 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 0 I 1For example, an input 101.101 give S.val = 5.625Construct 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 (d) None of the above6. 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 above7. The number of tokens in the following C statement printf("i=%d, &i=%x", i, &i); is(a) 3 (b) 26 (c) 10 (d) 218. 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 reverse9. Given the following expression grammar:E E * F | F + E | FF F | idWhich of the following is true?(a) * has higher precedence than +(b) - has higher precedence than *(c) + and - have same precedence (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. val = F. val} F 2 {F. val =2} F 4 {F. val =4}(a) Using this SDTS, construct a parse tree for the expression 4 2 4 * 2and 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 19991. Which of the following is the most powerful parsing method?(a) LL(1) (b) Canonical LR (c) SLR (d) LALR2. The number of tokens in the Fortran statement DO 10 I = 1.25 is(a) 3 (b) 4(c) 5 (d) None of the aboveGATE 20011. The process of assigning load addresses to the various 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 resolution2. 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 k3. Consider the following grammar with terminal alphabetend start symbol E. The 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.4. The syntax of the repeat-until statement is given by the following grammar S repeat S1 until EWhere E stands for expressions, S and S1 stand for statement. The non-terminals S and S1 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 newlabel( ) that returns a distinct label for a 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 |Construct all possible parse trees for the string abab. Is the grammar ambiguous?GATE 20021. (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 20031. 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) Removing left recursion and factoring the grammar(d) None of the above2. 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(a) n1 is necessarily less than n2(b) n1 is necessarily equal to n2(c) n1 is necessarily greater than n2 (d) None of the above3. 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 evaluated4. Which of the following statements is FALSE?(a) In statically typed languages, each variable in a program has a fixed type(b) In un-typed languages, values do not have any types(c) In dynamically typed languages, 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 program5. Consider the grammar shown below S i E t S S ' | a S' e S | e E bIn 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 | dThis 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 28. Consider the syntax directed definition shown below. S id : = E {gen(id.place = E.place;);} E E1 + E2 {t = newtemp( ); gen(t = E1.place + E2.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 = t1(c) t1 = Y; t2 = t1 + Z; X = t2(d) t1 = Y; t2 = Z; t3 = t1 + t2; X = t3GATE 20041. Which of the following grammar rules violate the requirements of an operator grammar? 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) only2. Consider a program P that consists of two source modules M1 and M2 contained in two different files. If M1 contains a reference to a function defined in M2 the reference will be resolved at(a) Edit time(b) Compile time(c) Link time(d) Load time3. Consider the grammar rule E E1 E2 for arithmetic expressions. The code generated is targeted to a CPU having a single user register. The subtraction operation requires the first operand to be in the register. If E1 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 E1 and E2 should necessarily be interleaved(d) Order to evaluation of E1 and E2 is of no consequence4. Consider the grammar with the following translation rules and E as the start symbol. E E1 # T { E.value = E1.value * T.value } I T { E.value = T.value } T T1 & F { T.value = T1.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) 405. The grammar A AA | ( A) | is not suitable for predictive-parsing because the grammar is:(a) ambiguous (b) left-recursive(c) right-recursive (d) an operator-grammarGATE 20051. Consider the grammar: S (S) | aLet the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be n1, n2 and n3 respectively. The following relationship holds good:(a) n1 < n2 < n3(b) n1 = n3 < n2(c) n1 = n2 = n3(d) n1 > n3 > n2Statement for Linked Answer Questions 2a & 2bConsider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production.E number E.val = number.va| | E '+' E E(1).val = E(2).val + E(3).val | E E E(1).val = E{2).val E(3).val2(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(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 and associativity properties does the generated 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 9GATE 20061. Consider the following grammar. S S * E S E E F + E E F F idConsider the following LR(0) items corresponding to the grammar above.(i) S S * .E(ii) E F. + E(iii) E F + .EGiven the items above, which two of them will appear in the same set in the canonical sets-of-items for the grammar?(a) (i) and (ii) (b) (ii) and (iii)(c) (i) and (iii) (d) None of the above2. 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 work1S2: 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 true3. 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 it to 13, and pass the address of the cell swapS2: On execution the code will generate a runtime error on line L1S3: On execution the code will generate a runtime error on line L2S4: The program will print 13 and 8S5: The program will print 13 and -2Exactly the following set of statement(s) is correct:(a) S1 and S2(b) S1 and S4(c) S3 (d) S1 and S54. Consider this C code to swap two integers and these five statements: the codeS1: will generate a compilation errorS2: may generate a segmentation fault at runtime depending on the arguments passedS3: correctly implements the swap procedure for all input pointers referring to integers stored in memory locations accessible to the processS4: implements the swap procedure correctly for some but not all valid input pointersS5: may add or subtract integers and pointers.(a) S1(b) S2 and S3(c) S2 and S4 (d) S2 and S55. Consider the following grammar: S FR R *S| F idIn the predictive parser table, M, of the grammar the entries M[S,id] and M[R,$] 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