Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov 2014 Compiler Design (170701) Question Bank / Assignment Unit 1: INTRODUCTION TO COMPILING 1. What is a compiler? Explain the various phases of compiler in detail, with a neat sketch. 2. What is a pass in a compiler? What is the effect of reducing the number of passes? 3. Explain the analysis synthesis model of compilation. List the factors that affect the design of compiler. Also List major functions done by compiler. 4. Find the Regular Expression corresponding to given statement, subset of {0,1}* a. The Language of all strings containing at least one 0 and at least one. b. The Language of all strings containing 0 s and 1 s both are even. c. The Language of all strings containing at most one pair of consecutive 1 s. d. The Language of all strings that do not end with 01. 5. What does the linker do? What does the loader do? What does the preprocess do? Explain their role(s) in compilation process. 6. Find errors and identify the phase of compiler detecting them for following C program segment. Justify your answers. int fi( int); char a[10], * cptr; int k = 1 ; int j = 2; float f; cptr = a; if (k); fi(k); fi( j ) ++k; *(cptr + 1 ) = 0 ; ++ a; n + *k ; 7. What is a symbol table? Discuss any two data structures suitable for it & compare their merits / demerits. Also compare one pass & two pass compilers. 8. Define compiler,interpreter,tokens, Patterns and lexemes,symbol table. 9. What are the possible error recovery actions in lexical Analyzer? 10. What are the three general approaches to the implementation of a Lexical Analyzer?
11. Describe briefly rational preprocessors with an example. 12. What are the reasons for separating the analysis phase of compiling into Lexical analysis and parsing? 13. Give the diagrammatic representation of a language processing system. 14. Explain briefly the producer consumer pair of a lexical analyzer and parser. 15. Mention the issues in a lexical analyzer and some of the cousins of the compiler. Unit 2: LEXICAL ANALYZER 1. Write a regular definition for the language of all strings of 0 s and 1 s with an even number of 0 s and odd number of 1 s. 2. Convert the following regular expression into deterministic finite automata. (a+b)*abb(a+b)* 3. Write the two methods used in lexical analyzer for buffering the input. Which technique is used for speeding up the lexical analyzer? 4. Convert the following NFA-^into equivalent NFA. Here ε is a ^-transition 5. Construct a DFA for a given regular expression (010+00)*(10)* 6. How do the parser and scanner communicate? Explain with the block diagram communication between them. Also explain: What is input buffering? 7. What is the difference between parse tree and syntax tree? Draw the parse tree for following expression: a= a + a * b + a * b * c a / b + a * b and write three address code for it. 8. Perform the Left factoring of following Grammar A ad a ab abc b 9. (a) Left factor the following grammar: E-> int int + E int - E E -(E) (b) Eliminate left-recursion from the following grammar: A -> A + B B B ->int (A) 10. Explain: Error Recovery Strategies in Compiler in brief 11. Explain lexical analysis phase of a compiler and, for a statement given below, write output of all phases (except of an optimization phase) of a complier. Assume a, b and c of type float a = a + b * c * 2;
12. Construct a DFA without constructing NFA for following regular expression. Find minimized DFA. a*b*a(a b)*b*a# 13. Construct a NFA for following regular expression using Thompson s notation and then convert it into DFA. aa*(b c) a*c# 14. Describe language denoted by the following regular expression:0(0/1)*0, (0/1)*0(0/1)(0/1),(00/11)*((01/10)(00/11)*(01/10)(01/10)(00/11)*)*, : 0*a0*a0*a0* 15. Write the regular definition for the following language: "All strings of digits with no repeated digits". 16. Give an example for ambiguous grammar and justify it. 17. Write regular definition for a number which can have fraction or exponent part as optional. 18. For the following grammar: list --> list+digit / list-digit / digit digit --> 0/1/.../9 Derive the sentence (9+5-4) using left-most derivation. 19. Write the algorithm for moving forward pointer in "input buffering" scheme using sentinels. Unit 3:PARSING THEORY 1. How can panic mode and phrase level recovery be implemented in LR parsers? 2. Explain how type checking & error reporting is performed in a compiler. 3. Consider the expression grammar. E E + E E * E (E) id 4. Prepare the SLR parsing table with error detection and recovery routines. 5. Write an algorithm for eliminating left recursion. 6. Is the following grammar suitable for LL(1) parsing? If not make it suitable for LL(1) parsing. Compute FIRST and FOLLOW sets. Generate the parsing table. S AB A Ca ϵ B BaAC c C b ϵ 7. Compute the operator precedence matrix and precedence function for the following grammar if it exists. +,*,-,/,id,num,( and ) are terminal symbols. G E E E+T E-T T T T*F T/F/F F num id (E) 8. Consider the following grammar E E+T T T TF F F F* a b
1) Construct the SLR parsing table for this grammar. 2) Construct the LALR parsing table. 9. Construct predictive parsing table for following. S -> A A -> ab Ad B -> bbc f C -> g 10. Eliminate left recursion from the following grammar and rewrite the Grammar. S -> Aa b A -> Ac Sd ε 11. Find out FIRST & FOLLOW set for all the Non-terminals S AcB cbb Ba A da BC B g ε C h ε 12. Explain non-recursive predictive parsers. Draw the block diagram of it. 13. Eliminate left recursion from the following grammar A->Ac/Aad/bd/c. 14. Test whether the following grammar is LL (1) or not. Construct predictive parsing table for it. S 1AB ε A 1AC 0C B 0S C 1 15. Construct the canonical parsing table for the following Grammar S S S CC C cc d 16. Generate the SLR parsing table for the following Grammar S Aa bac bba A d B d 17. Write unambiguous grammar for producing arithmetic expression consisting of symbols id, +, -, /, $. Find first & follow of non terminal symbols of the grammar for non recursive predictive parser. Construct parse table and parse following string. id - id + id - id $ 18. Consider the grammar S -> SS+ SS* a 19. Show that the string aa+a* can be generated by the grammar. 20. Construct the parse tree for it. Is the grammar ambiguous? Justify. 21. Write unambiguous production rules for producing arithmetic expression consisting of symbols id, *, -, ( ) and ^, where ^ represents exponent. Parse following string using shift reduce parser: id id * id ^ id * (id ^ id ) ^ id 22. Explain various conflicts of a shift reduce parser.
23. A robot is to be moved to a unit step in a direction specified as a command given to it. The robot moves in the direction North, South, East, West on receiving N, S, E, W command respectively & in the direction North-East, North-West, South-East, South-West on receiving A, B, C, D commands respectively. The current position of the robot is initialized to (0,0) Cartesian coordinates on receiving command Start. Write production rules for producing sequence of commands and semantic rules for knowing position of a robot after receiving a sequence of commands. Draw annotated parse tree for following sequence: Start N N A A C C N 24. Explain SLR parser in detail with the help of an example. 25. Draw transition diagrams corresponding to production rules for operators +, -, *, / and id for a predictive parser. Explain how parsing takes place for it. 26. Explain operator precedence parser by giving example for constructing a precedence graph and table. 27. Write ambiguous and unambiguous production rules for if then else construct. Illustrate parsing using both types of rules by giving an example. Also explain left factoring and its use. 28. What do you mean by Handle Pruning? 29. Define LR (0) items, viable prefixes,handle. 30. What is phrase level error recovery? 31. Construct Predictive Parsing table for the following grammar: S -> (L)/ a L -> L, S/S and check whether the following sentences belong to that grammar or not. (i) (a,a) (ii) (a, (a, a)) (iii) (a,((a,a),(a,a))) 32. Check whether the following grammar is SLR (1) or not. Explain your answer with reasons. S-> L+R S->R L->*R L->id R->L 33. For the grammar given below, calculate the operator precedence relation and the precedence functions. E->E+E E E E*E E/E E^E (E) E id 34. Check whether the following grammar is a LL(1) grammar S -> iets ietses a E-> b Also define the FIRST and FOLLOW. 35. Explain LALR parsing, justify how it is efficient over SLR parsing. 36. Draw the transition diagrams for predictive parsers for the following grammar. E TE E +TE ϵ T FT T *FT ϵ F (E) id
Unit 5:INTERMEDIATE CODE GENERATION 1. What is attributed grammar? Which phase of the compilation process does it facilitate? Explain with example. 2. Write syntax directed Defination for translating following grammar for postfix notation. Also draw annotated parse tree for 9-5+2. expr -> expr + term expr -> expr term term -> 0 1.. 9 3. Write a syntax directed definition for desk calculator. Justify whether this is an S-attributed definition or L-attributed definition. Using this definition draw annotated parse tree for 3*5+4n. 4. What is inherited attribute? Write syntax directed definition with inherited attributes for type declaration for list of identifiers. Show annotated parse tree for the sentence real id1,id2,id3. 5. What is Intermediate form of the code? What are the advantages of it? What are generally used intermediate forms? Write N-Tuple notation for: (a+b)*(c+d)-(a+b+c) 6. Give the Syntax directed definition of if else statement. 7. What are the various methods of implementing three address statements? 8. How would you generate the intermediate code for the flow of control statements? Explain with example. 9. What is back patching? 10. Translate the expression (a+b)*(c+d)+(a+b+c) into 1. Quadruples 2. Triples 3. Indirect triples 11. Convert the exp into three address code & Quadruple. S=(a+b)/(c-d)*(e+f) 12. Translate the arithmetic expression a*-(b+c) into 1. Syntax tree 2. Postfix notation 3. Three address code Unit 6:. RUN TIME MEMORY MANAGEMENT 1. Suggest a Suitable approach for completing hash function 2. Explain activation tree and control stack. 3. What are the limitations of static storage allocation? Explain the problem of dangling references. 4. For what purpose compiler uses symbol table? How characters of a name are stored in symbol table?
5. Explain the static scope rule and dynamic scope rule. 6. Explain the structure of an activation record with all its components 7. Explain: Symbol Table Management. How symbol table differs from other data structures? 8. Compare: Static v/s Dynamic Memory Allocation 9. Explain activation record. How is task divided between calling & called program for stack updating? 10. Explain various parameter passing methods. 11. Explain heap, dynamic storage allocation techniques and synthesized attributes 12. How Back patching can be used the generate code for Boolean expressions and flow of control statements. Unit 7: CODE OPTIMIZATION 1. Why are quadruples preferred over triples in an optimizing Complier? 2. Write an algorithm for global common subexpression elimination. 3. Explain peephole optimization. 4. Explain any three types of optimization techniques. Unit 8: CODE GENERATION 1. Write the generic issues in the design of code generators 2. Discuss the factors affecting the target code generation. 3. What are the applications of DAG? 4. How would you represent the following equation using the DAG, a. a: =b*c + b*c. What is the purpose of DAG? 5. Draw syntax tree and DAG for following statement. Write three address codes from both. 6. a = (a + b * c) ^ (b * c) + b * c -----------------------------------------------------------------------------------------------------------------