UNIT IV INTERMEDIATE CODE GENERATION

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

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

Chapter 6 Intermediate Code Generation

Intermediate Code Generation

Syntax-Directed Translation

Dixita Kagathara Page 1

A Simple Syntax-Directed Translator

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

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

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

Syntax-Directed Translation

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

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

PRINCIPLES OF COMPILER DESIGN

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

Formal Languages and Compilers Lecture X Intermediate Code Generation

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

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

COP5621 Exam 3 - Spring 2005

LECTURE 17. Expressions and Assignment

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

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

Concepts Introduced in Chapter 6

Alternatives for semantic processing

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

VIVA QUESTIONS WITH ANSWERS

Concepts Introduced in Chapter 6

Question Bank. 10CS63:Compiler Design

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

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

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER

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

UNIT I INTRODUCTION TO COMPILING

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Time : 1 Hour Max Marks : 30

Intermediate Code Generation

Intermediate Code Generation

CS2210: Compiler Construction. Code Generation

CMPSC 160 Translation of Programming Languages. Three-Address Code

THEORY OF COMPILATION

Intermediate Code Generation

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

Intermediate Code Generation

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

intermediate-code Generation

More On Syntax Directed Translation

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

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

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

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

Figure 28.1 Position of the Code generator

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

Chapter 4 :: Semantic Analysis

Programming Languages

[Syntax Directed Translation] Bikash Balami

CS6660-COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS PART A

Syntax Directed Translation

A programming language requires two major definitions A simple one pass compiler

Intermediate Representations

Syntax-Directed Translation Part II

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

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

Intermediate Code Generation

CST-402(T): Language Processors

2.2 Syntax Definition

Syntax-Directed Translation

Roll No. :... Invigilator's Signature :. CS/B.Tech(CSE)/SEM-7/CS-701/ LANGUAGE PROCESSOR. Time Allotted : 3 Hours Full Marks : 70

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Module 26 Backpatching and Procedures

Intermediate Representations

Page No 1 (Please look at the next page )

Chapter 4. Action Routines

Semantic Analysis and Intermediate Code Generation

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

CSc 453 Intermediate Code Generation

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

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

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Syntax-Directed Translation

5. Syntax-Directed Definitions & Type Analysis

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Intermediate Representa.on

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Compiler Design Aug 1996

VETRI VINAYAHA COLLEGE OF ENGINEERING AND TECHNOLOGY

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

Intermediate Code Generation

CSCI565 Compiler Design

Intermediate Representations

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

QUESTIONS RELATED TO UNIT I, II And III

Intermediate Representations

Intermediate Code Generation

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

Chapter 3: Operators, Expressions and Type Conversion

INSTITUTE OF AERONAUTICAL ENGINEERING

Lecture 14 Sections Mon, Mar 2, 2009

Transcription:

UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of the syntax tree in which a node appears immediately after its children. Eg, for the above syntax tree, a b c uminus * b c uminus * + = 3. What are the different types of three address statements? 4. In compiler how three address statements can be represented as records with fields for the operator and operands. Quadruples Triples Indirect triples.

5. Define quadruple and give one example. A quadruple is a data structure with 4 fields like operator, argument-1, argument- 2 and result. 6. Define triple and give one example. A triple is a data structure with 3 fields like operator, argument-1, argument-2. Example: a=b*-c 7. 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. 8. 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. 9. 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. Example: a type, a value, a memory location etc., i) Synthesized attributes. ii) Inherited attributes. 10. 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. 11. 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.val + E2.val 12. What are the functions used to create the nodes of syntax trees? i) Mknode (op, left, right) ii) Mkleaf (id,entry) iii) Mkleaf (num, val) 13. What are the three kinds of intermediate representations? i) Syntax trees. ii) Postfix notation. iii) Three address code. 14. 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 15. 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. 16. What are the types of three address statements? Assignment statements, assignment instruction, copy statements, conditional jump, address-address statements, indexed assignment statements, address and pointer statements. 17. Write the properties of intermediate language. Intermediate codes are machine independent codes, but they are close to machine instructions. The given program in a source language is converted to an equivalent program in an intermediate language by the intermediate code generator. Intermediate language can be many different languages, and the designer of the compiler decides this intermediate language. syntax trees can be used as an intermediate language.

postfix notation can be used as an intermediate language. three-address code (Quadruples, triples and indirect triples) can be used as an intermediate language 18. What is back patching? 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 backpatch(p,i). 19. List out the three functions that are used to manipulate the list of labels in back patching. mklist(i) creates the new list. The index i is passed as an argument to this function where I is an index to the array of quadruple. merge_list(p1,p2) this function concatenates two lists pointed by p1 and p2. It returns the pointer to the concatenated list. backpatch(p,i) inserts i as target label for the statement pointed by pointer p. 20. Define marker non-terminals with an example. 21. Why are quadruples preferred over triples in an optimizing Complier? 22. Give the triple representation of a ternary operation x:= y[i] 23. Give the Semantic rules for the production S while E do S1.

24. 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. 25. 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. 26. 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. 27. 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. 28. Write a three address code for the expression a < b or c < d? 100: if a<b goto 103 101: t1:=0 102: goto 104 103: t1:=1 104: if c<d goto 107 105: t2:=0 106: goto 108 107: t2:=1 108: t3:=t1 or t2 29. What are the parameter transmission mechanisms? 1. Call by value 2. Call by value-result 3. Call by reference 4. Call by name 30. Define short circuit code? Translate the Boolean expression into three-address code without generating code for any of the Boolean operators and without having the code necessarily

evaluate the entire expression. This style of evaluation is sometimes is called short-circuit or jumping code. 31. Give the syntax of case statements? Switch expression Begin Case value: statement Case value: statement ------- Case value: statement Default : statement End 32. When procedure call occurs, what are the steps to be takes placed? State of the calling procedure must be saved, so that it can resume after completion of procedure. Return address is saved, in this location called routine must transfer after completion of procedure 33. What are the demerits in generating 3-address code for procedure calls? Consider, b=abc (I, J).It is very difficult to identify whether it is array reference or it is a call to the procedure abc. 16 Marks 1. Write about implementation of three addressing statements. It is one of the intermediate representations. It is a sequence of statements of the form x:= y op z, where x, y, and z are names, constants or compiler-generated temporaries and op is an operator which can be arithmetic or a logical operator. E.g. x+y*z is translated as t1=y*z and t2=x+t1. Reason for the term three-address code is that each statement usually contains three addresses, two for the operands and one for the result. Implementation: Quadruples Record with four fields, op, arg1, arg2 and result Triples Record with three fields, op, arg1, arg2 to avoid entering temporary names into symbol table. Here, refer the temporary value by the position of the statement

that computes it. Indirect triples List the pointers to triples rather than listing the triples For a: = b* -c + b * -c Quadruples 2. Give the syntax-directed definition for flow of control statements.

3. How back patching can be used to generate code for Boolean expressions and flow of control statements. Hints: Write syntax directed translation schemes Explain with an example. 4. Write short notes on procedure calls.

5. Let A be a 10 * 20 array with low1 = low2 =1. Let w=4. Draw an annotated parse tree for the assignment statement X:=A[y,z]. Give the sequence of three address statement generated