NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

Similar documents
UNIT IV INTERMEDIATE CODE GENERATION

Syntax-Directed Translation

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

Module 25 Control Flow statements and Boolean Expressions

CMPSC 160 Translation of Programming Languages. Three-Address Code

Chapter 6 Intermediate Code Generation

Dixita Kagathara Page 1

Formal Languages and Compilers Lecture X Intermediate Code Generation

Intermediate Code Generation

Intermediate Code Generation

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

PRINCIPLES OF COMPILER DESIGN

VIVA QUESTIONS WITH ANSWERS

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

Module 26 Backpatching and Procedures

CS2210: Compiler Construction. Code Generation

A Simple Syntax-Directed Translator

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

UNIT I INTRODUCTION TO COMPILING

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Intermediate Code Generation

intermediate-code Generation

Intermediate Code Generation

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

COP5621 Exam 3 - Spring 2005

THEORY OF COMPILATION

Question Bank. 10CS63:Compiler Design

Concepts Introduced in Chapter 6

Time : 1 Hour Max Marks : 30

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Syntax Directed Translation

Concepts Introduced in Chapter 6

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Module 27 Switch-case statements and Run-time storage management

CSc 453 Intermediate Code Generation

Intermediate Representations

Intermediate Code Generation

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

Principles of Compiler Design

CSCI Compiler Design

Compiler. --- Intermediate Code Generation. Zhang Zhizheng.

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Intermediate Representations

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

Syntax-Directed Translation

LECTURE 17. Expressions and Assignment

VALLIAMMAI ENGINEERING COLLEGE

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Languages and Compiler Design II IR Code Generation I

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Syntax-Directed Translation Part II

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

Alternatives for semantic processing

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Functional Programming. Pure Functional Programming

Intermediate representation

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Intermediate Code Generation

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Semantic Analysis and Intermediate Code Generation

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Intermediate-Code Generat ion

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Midterm Solutions COMS W4115 Programming Languages and Translators Wednesday, March 25, :10-5:25pm, 309 Havemeyer

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

CSCI565 Compiler Design

5. Syntax-Directed Definitions & Type Analysis

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Code No: R Set No. 1

ECS 142 Project: Code generation hints (2)

Intermediate Code Generation

Intermediate Code Generation

2.2 Syntax Definition

SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT I - LEXICAL ANALYSIS PART - A

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018

CS415 Compilers. Intermediate Represeation & Code Generation

VETRI VINAYAHA COLLEGE OF ENGINEERING AND TECHNOLOGY

Intermediate Code Generation

Intermediate Representations

Syntax-Directed Translation

COMPILER DESIGN. Intermediate representations Introduction to code generation. COMP 442/6421 Compiler Design

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Intermediate Representa.on

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

CS5363 Final Review. cs5363 1

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES

Transcription:

SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT III INTERMEDIATE CODE GENERATION PART A 1. What are the benefits of intermediate code generation? (A.U May 2008) A Compiler for different machines can be created by attaching different back end to the existing front ends of each machine. A Compiler for different source languages can be created by proving different front ends for corresponding source languages t existing back end. A machine independent code optimizer can be applied to intermediate code in order to optimize the code generation. 2. What are the various types of intermediate code representation? (A.U Nov/Dec 2007) There are mainly three types of intermediate code representations. Syntax tree Postfix Three address code 3. Define back patching. (A.U Nov/Dec 2009) (A.U Nov/Dec 2010)NOV DEC 2012 Back patching is the activity of filling up unspecified information of labels using appropriate semantic actions in during the code generation process. In the semantic actions the functions used are mklist (i), merge list(p1,p2) and back patch(p,i) 4.Mention the functions that are used in back patching. (A.U Nov/Dec 2007) (i)creates the new list. The index iis passed as an argument to this function where I is an index to the array of quadruple. (ii)merge list (p1, p2) this function concatenates two lists pointed by p1 and p2. It returns the pointer to the concatenated list. (iii)back patch (p, i) inserts i as target label for the statement pointed by pointer p. 5. What is the intermediate code representation for the expression a or b And not c? The intermediate code representation for the expression a or b and not c is the three address sequence t1 := not c t2 := b and t1 t3 := a or t2 6. What are the various methods of implementing three address statements? The three address statements can be implemented using the following methods. Quadruple: a structure with at most four fields such as operator (OP), arg1, arg2, result. Triples: the use of temporary variables is avoided by referring the pointersin the symbol table. Indirect triples: the listing of triples has been done and listing pointers are used instead of using statements. 7. Give the syntax-directed definition for if-else statement. (A.U May/june 2008) 1. S if E then S1 E.true := new_label() Page 1

E.false :=S.next S1.next :=S.next S.code :=E.code gen_code(e.true : ) S1.code 2. S if E then S1 else S2 E.true := new_label() E.false := new_label() S1.next :=S.next S2.next :=S.next S.code :=E.code gen_code(e.true : ) S1.code gen_code( go to,s.next) gen_code(e.false : ) S2.code 8. What are the methods to represent the value of a Boolean expression? Numerical representation Encode true and false. Numerically and to evaluate a Boolean expression analogously to an arithmetic expression. Flow of Control Implementing Boolean expression is by flow of control i.e.,representing the value of a Boolean expression by a positionreached in a program. 9. Define short-circuit code. (A.U May/June 2008) It translates a Boolean expression into three address code without generatingcode for any of the Boolean operators and without having the code necessarilyevaluates the entire expression. This type of evaluation is called short circuit orjumping code. 10. Define Boolean expression. Boolean expressions are composed of the Boolean operators (and,or, not) applied to elements. Those are Boolean variables or relational expressions. 11. Define three address codes. (A.U Nov/Dec 2010) Three address code is a sequence of statements of the general form x: = y op z where x,y,z are operand and op is operator. 12. Write the properties of intermediate languages. The intermediate language is an easy form of source language which Can be generated efficiently by the compiler. The generation of intermediate language should lead to efficient codegeneration. The intermediate language should act as effective mediator between Front and back end. The intermediate language should be flexible enough so that optimized Code can be generated. 13. Generate the three address code for an expression x: = a + b * c +d ; Assuming x and a as real and b,c and d are integer. t1 := b int * c t2 := t1 int + d t3 := inttoreal t2 t4 := a real + t3 x := t4 Page 2

14. Generate the three address code for while (i<10) {x := 0;i := i + 1; } The three address code can be generated as follows 100 L1 : if i<10 goto L2 101 GotoLnext 102 L2 : x = 0 103 i = i + 1 104 goto L1 105 Lnext 15. Write the translation scheme for while (i<10) {x := 0;i := i + 1; }S.begin = new_label() = L1 E.true = new_label() = L2 E.code = if i<10 goto E.false = S.next = Lnext S1.code = x =0; i = i + 1 Generate the three address code for switch (ch) {case 1: c := a + b; break; case 2: c := a b; break; }The three address code can be generated as follows 100 ifch = 1 goto L1 101 ifch = 2 goto L2 102 L1: t1 := a + b 103 c := t1 104 goto Last 105 L2 : t1 : = a - b 106 c := t1 107 Page 3

goto Last 108 Last: 16. Write the translation scheme for switch (ch) {case 1: c := a + b; break; case 2: c := a b; break; }Evaluate E into t such that t = E Goto check L1 : code for s1 goto last L2 : code for s2 goto last check : if c = a + b goto L1 if c = a b goto L2 last: 17. Write the semantic action for the production rule of E E1 OR M E2. {Backpatch (E1.Flist, M.state); E.Tlist := merge ( E1.Tlist,E2.Tlist); E.Flist := E2.Flist } 18. Write the semantic action for the production rule of S call id (L). {for each item p in queue do append ( param p); append ( call id.place } 19. Write the semantic action for the production rule of L. L, E. { inserte.place in the queue } 20. Write the semantic action for the production rule of L. E(A.U Nov/Dec 2007) {initialize queue and insert E.place in the queue } 21. What are the advantages of generating an intermediate representation? i) Ease of conversion from the source program to the intermediate code. ii) Ease with which subsequent processing can be performed from the intermediate code. 22. Define a syntax-directed translation? Syntax-directed translation specifies the translation of a construct in terms of Attributes associated with its syntactic components. Syntax-directed translation uses a context free grammar to specify the syntactic structure of the input. It is an input- output mapping. 23. Define an attribute. Give the types of an attribute? An attribute may represent any quantity, with each grammar symbol, it associates a set of attributes and with each production, a set of semantic rules for computing values of the attributes associated with the symbols appearing in that production. Page 4

Example: a type, a value, a memory location etc., i) Synthesized attributes. ii) Inherited attributes. 24. Define annotated parse tree? A parse tree showing the values of attributes at each node is called an annotated parse tree. The process of computing an attribute values at the nodes is called annotating parse tree. Example: an annotated parse tree for the input 3*5+4n. 25. Define dependency graph? The interdependencies among the inherited and synthesized attributes at the nodes in a parse tree can be depicted by a directed graph is called a dependency graph. Example: Production E_ E1 + E2 Semantic Rule E.val:= E1.va; + E2.val 26. Define syntax tree. Give an example? APRIL MAY 2011 An (abstract) syntax tree is a condensed form of parse tree useful for representing language constructs. Example: syntax tree for a 4 + c + - c a 4 27. What are the functions used to create the nodes of syntax trees? i) Mknode (op, left, right) SNSCT Department of Compute Science and Engineering Page 12 ii) Mkleaf (id,entry) iii) Mkleaf (num, val) 28. What are the functions for constructing syntax trees for expressions? i) The construction of a syntax tree for an expression is similar to the translation of the expression into postfix form. ii) Each node in a syntax tree can be implemented as a record with several fields. 29. Define DAG. Give an example? NOV DEC 2011 DAG is a directed acyclic graph for an expression identifies the common sub expression in the expression. Example: DAG for the expression a- 4 *c P1 = mkleaf(id,a) P2 = mknum(num,4) P3 = mkleaf(id,c) P4 = mknode( *,p2,p3) P5 = mknode( -,p1,p4) 30. What are the three kinds of intermediate representations? i) Syntax trees. ii) Postfix notation. iii) Three address code. 31. Define postfix notation? Postfix notation is a linearized representation of a syntax tree. It is a list of the nodes of the tree in which a node appears immediately after its children. The syntax tree is, a: = b*-c The postfix notation for the syntax tree is, abc-*c 32. Define three-address code? Three address code is a sequence of statements of the form x: = y op z. where x, y, z are names, constants, or compiler generated temporaries, op stand for any type of operator. Since a statement involves not more than three references it is called three-address statement, and hence a sequence of such statement is called three address codes. Page 5

33. What are the types of three address statements? NOV DEC 2011 Assignment statements, assignment instruction, copy statements, conditional jump, address-address statements, indexed assignment statements, address and pointer statements. 34. What are the three types of implementations of three-address statements? i) Quadruples ii) Triples iii) Indirect Triples. 35. What are the methods of translating Boolean expressions? There are two principal methods of representing the value of a Boolean expression. a) Encode true and false numerically and to evaluate a Boolean expression analogous to an arithmetic expression. SNSCT Department of Compute Science and Engineering Page 13 b) Flow-of control. Represent the value of a Boolean expression by a position reached in a program. 36. What are the two purposes of Boolean expressions? a) They are used to compute logical expressions. b) Often they are used as condition expression in statements that alter the flow of control, such as if-then, if-then-else, or while-do statements. 37. Define quadruple. Give an example? A quadruple is a record structure with four fields: op, arg1, arg2 and result. The op field contains an internal code for the operator. Example: x: =y op z 38. Give the advantages of quadruples? i) Can perform peephole optimization. ii) The contents of field s arg1, arg2 and result are normally pointers to The symbol-table entries for the names represented by these fields. If So, temporary names must be entered into the symbol table as they Are created. 39. Define triple. Give an example? Triple is a record structure with three fields: op, arg1 and arg2. The fields arg1 and arg2 are either pointes to the symbol-table or pointers into the triple structure. This method is used to avoid temporary names into the symbol table. 40. Define indirect triples. Give the advantage? Listing pointers to triples rather than listing the triples themselves are called indirect triples. Advantages: it can save some space compared with quadruples, if the same temporary value is used more than once. 41. Define translation scheme? A translation scheme is a CFG in which program fragments called semantic action are embedded within the right sides of productions. A translation scheme is like a syntax-directed definition, except that the order of evaluation of the semantic rules is explicitly shown. 42. What are the three address code for a or b and not c? The three address sequence is T1:= not c T2:= b and T1 T3:= a or T2. 43. Write a three address code for the expression a < b or c < d? 100: if a<b goto 103 101: t1:=0 Page 6

102: goto 104 103: t1:=1 104: if c<d goto 107 SNSCT Department of Compute Science and Engineering Page 14 105: t2:=0 106: goto 108 107: t2:=1 108: t3:=t1 or t2 44. What are the parameter transmission mechanisms? 1. Call by value 2. Call by value-result 3. Call by reference 4. Call by name 45. Construct a DAG for the expression I: = I + 10? := + I 10 46. What are the various data structure used for implementing the symbol table? 1. Linear list 2. Binary tree 3. Hash table 47. What is the purpose of DAG? i) A label for each node. For leaves the label is an identifier and for interior nodes, an operator symbol ii) For each node a list of attached identifiers. 48. What are the three functions of backpatching? i) Makelist(i) create a new list. ii) Merge(p1,p2) concatenates the lists pointed to by p1 and p2. iii) Backpatch(p,i) insert i as the target label for the statements pointed to by p. 49. Give short note about call-by-name? Call by name, at every reference to a formal parameter in a procedure body the name of the corresponding actual parameter is evaluated. Access is then made to the effective parameter. 50. How parameters are passed to procedures in call-by-value method? This mechanism transmits values of the parameters of call to the called program. The transfer is one way only and therefore the only way to returned can be the value of a function. Main ( ) { print (5); } Int SNSCT Department of Compute Science and Engineering Page 15 Void print (int n) { printf ( %d, n); } 51. Define symbol table? A compiler uses a symbol-table to keep track of scope and binding information about names. It is searched every time a name is encountered in the source text changes to the table occur, if a new name or new information about an existing name is discovered. 52. What are the semantic rules are defined in the declarations operations? 1) Mktable(previous) 2) Enter(table, name,type,offset) Page 7

3) Addwidth(table,width) 4) Enterproc(table,namenewtable) 53. Give the syntax of case statements? Switch expression Begin Case value: statement Case value: statement ------- Case value: statement Default : statement End 54. Give the 2 attributes of syntax directed translation into 3-addr code? i) E.place, the name that will hold the value of E and ii) E.code, the sequence of 3-addr statements evaluating E. 55. Write a short note on declarations? Declarations in a procedure, for each local name, we create a symbol table entry with information like the type and the relative address of the storage for the name. The relative address consists of an offset from the base of the static data area or the field for local data in an activation record. The procedure enter (name, type, offset) create a symbol table entry. 56. Give the two parts of basic hashing scheme? 1) A hash table consisting of a fixed array of m pointers to table entries. 2) Table entries organized into m separate linked lists, called buckets. Each record in the symbol table appears on exactly one of these lists. 57. Write the grammar for flow-of-control statements? The following grammar generates the flow-of-control statements, if-then, if-thenelse, and while-do statements. S _ if E then S1 If E then S1 else S2 While E do S1. SNSCT Department of Compute Science and Engineering Page 16 58. Write the 3-addr code for the statements a =b*-c + b*-c? Three address codes are: a=b*-c + b*-c T1 = -c T2 = b*t1 T3 = -c T4 = b*t3 T5 = T2+T4 a:= T5. Page 8

59. What. is a syntax tree? Draw the syntax tree for the following statement:a:=b - c+b*-c. APRIL MAY 2012 Syntax tree: A syntax tree depicts the natural hierarchical structure of a source program. A dag (Directed Acyclic Graph) gives the same information but in a more compact way because common subexpressions are identified. A syntax tree and dag for the assignment statement a : =b * - c + b * - c are as follows: 60. Page 9