47
DEPARTMENT OF COMPUTER SIENCE AND ENGINEERING 2 Marks Question and Answer Subject Code & Name: PRINCIPLES OF COMPILER DESIGN UNIT I – LEXICAL ANALYSIS 1. Define compiler? A compiler is a program that reads a program written in one language (source language) and translates it into an equivalent program in another language (target language) and the compiler reports to its user the presence of errors in the source program. 2. What are the classifications of compiler? i) Single pass compiler ii) Multi pass compiler iii) Load-and-go compiler iv) Debugging or optimizing compiler 3. What are the phases of compiler? i) Lexical analyzer ii) Syntax analyzer iii) Semantic analyzer iv) Intermediate code generation v) Code generation vi) Code optimization vii) Symbol table manager. 4. Define preprocessor & what are the functions of preprocessor? Preprocessor produce input to the compilers (i.e.) the program will be divided in to the modules. They may perform the following functions. i) Macro processing ii) File inclusion iii) Rational preprocessor iv) Language extension 5. What are the tools available in analysis phase? i) Structure editors ii) Pretty printer iii) Static checkers iv) Interpreters. 6. Define pretty printers?

Principles of Compiler Design

Embed Size (px)

DESCRIPTION

2 marks Question and Answers

Citation preview

Page 1: Principles of Compiler Design

DEPARTMENT OF COMPUTER SIENCE AND ENGINEERING2 Marks Question and Answer

Subject Code & Name: PRINCIPLES OF COMPILER DESIGN

UNIT I – LEXICAL ANALYSIS

1. Define compiler?A compiler is a program that reads a program written in one language (source language) and translates it into an equivalent program in another language (target language) and the compiler reports to its user the presence of errors in the source program.

2. What are the classifications of compiler?i) Single pass compilerii) Multi pass compileriii) Load-and-go compileriv) Debugging or optimizing compiler

3. What are the phases of compiler?i) Lexical analyzerii) Syntax analyzeriii) Semantic analyzeriv) Intermediate code generationv) Code generationvi) Code optimization vii) Symbol table manager.

4. Define preprocessor & what are the functions of preprocessor?Preprocessor produce input to the compilers (i.e.) the program will be divided in to the modules.

They may perform the following functions.i) Macro processingii) File inclusioniii) Rational preprocessoriv) Language extension5. What are the tools available in analysis phase?

i) Structure editorsii) Pretty printeriii) Static checkersiv) Interpreters.

6. Define pretty printers?A pretty printer analyzes a program and prints it in such a way that the structure of the program

becomes clearly visible. For the comments may appear with an amount of indentation proportional to the depth of their nesting in the hierarchical organization of the statements.

7. Define assembler and its types?It is defined by the low level language is assembly language and high level language is machine

language is called assembler.One pass assemblerTwo pass assembler

Page 2: Principles of Compiler Design

8. Give the types of a language processing system?i. Preprocessors

ii. Compilersiii. Assembleriv. Loaders and link editors

9. What are the functions performed in analysis phase?a) Lexical analysis or Linear analysisb) Syntax analysis or hierarchical analysisc) Semantic analysis

10. What are the functions performed in synthesis phase?a) Intermediate code generationb) Code generationc) Code optimization

11. Give the classification of processing performed by the semantic analysis?a) Processing of declarative statements.b) Processing of executable statements.

12. Give the properties of intermediate representation?a. It should be easy to produce.b. It should be easy to translate into the target program.

13. What are the two different parts of compilation?a)     Analysis phases of compilationb)    Synthesis phases of compilation

14. What is meant by lexical analysis?It reads the characters in the program and groups them into tokens that are sequences of characters

having a collective meaning Such as an identifier, a keyword, a punctuation, character or a multi-character operator like ++.

15. What is meant by syntax analysis?It processes the string of descriptors, synthesized by the lexical analyzer, to determine the syntactic

structure of an input statement. This process is known as parsing. Output of the parsing step is a representation of the syntactic structure of a statement. It is represented in the form of syntax tree.

16. What is meant by intermediate code generation?After syntax and semantic analysis, some compilers generate an explicit intermediate representation

of the source program. It can have a variety of forms. This form called three-address code. It consists of sequence of instructions, each of which has at most three operands.

17. What is meant by semantic analysis?This phase checks the source program for semantic errors and gathers type of information for the

subsequent phase.

18. What do you meant by interpreter?Certain other translators transform a programming language into a simplified language called

intermediate code, which can directly executed using a program called an interpreter.

Page 3: Principles of Compiler Design

19. What do you meant by phases?Each of which transforms the source program one representation to another. A phase is a logically

cohesive operation that takes as input one representation of the source program and produces as output another representation

20. Write short notes on symbol table manager?The table management or bookkeeping portion of the compiler keeps track of the names used by

program and records essential information about each, such as its type (int, real etc.,) the data structure used to record this information is called a symbol table manger.

21. Write short notes on error handler?The error handler is invoked when a flaw in the source program is detected. It must warn the

programmer by issuing a diagnostic, and adjust the information being passed from phase to phase so that each phase can proceed. So that as many errors as possible can be detected in one compilation.

22. Mention some of the cousins of the compiler?a) Preprocessorsb) Assemblersc) Two pass assemblyd) Loaders and Linker-editors.

23. What is front end and back end?The phases are collected into a front end and a back end. The front end consists of those phases or

parts of phases, that depends primarily on the source language and is largely independent of the target machine. The back ends that depend on the target machine and generally these portions do not depend on the source language.

24. What do you meant by passes?A pass reads the source program or the output of the previous pass, makes the transformations

specified by its phases and writes output into an intermediate file, which may then be read by a subsequent pass. In an implementation of a compiler, portions of one or more phases are combined into a module called pass.

25. List some compiler construction tools?a) Parser generatorsb) Scanner generatorsc) Syntax-directed translation engined) Automatic code generatorse) Data-flow engine.

26. Explain any one compiler construction tool?Scanner generators, these automatically generate lexical analyzers normally from a specification

based on regular expressions. The resulting of lexical analyzer is in effect of finite automata.

27. What are issues available in lexical analysis?a) Simpler designb) Compiler efficiency is improved by specialized buffering techniques for reading input characters

and processing tokens and significantly speeds up the performance of a compiler.c) Compiler portability is enhanced.

Page 4: Principles of Compiler Design

28. Define patterns/lexeme/tokens?A set of strings in the input for which the same token is produced as output. This set of strings

described by a rule called pattern associated with the token.A lexeme is a sequence of characters in the source program that is matched by the pattern for a token.Token is a sequence of character that can be treated as a single logical entity.

29. Give the algebraic properties of regular expression? AXIOM DESCRIPTIONi)                   r/s = s/r / is commutativeii)                r/(s/t)=(r/s)/t / is associativeiii)              (rs)t=r(st) concatenation is associativeiv)              r(s/t)=rs/rt concatenation distributes over /v)                r**=r* * is idempotent

30. What are the systems referred to data flow engine?i) Compiler-compilersii) Compiler-generatorsiii) Translator writing systems.

31. Give the parts of a string?Prefix of s, suffix of s, substring of s, proper prefix, proper suffix, proper substring and subsequence

of s.32. What are the operations on language?·        Union·        Concatenation·        Kleene closure or star closure and ·        Star closure.

33. Give the error recovery actions in lexical errors?i) Deleting an extraneous characterii) Inserting a missing characteriii) Replacing an incorrect character by a correct character.

34. What are the implementations of lexical analyzer?a) Use a lexical analyzer generator, such as Lex compiler, to produce the lexical analyzer from a regular expression based specificationb) Write the lexical analyzer in a conventional systems-programming language using the I/O facilities of that language to read the input.c) Write the lexical analyzer in assembly language and explicitly manage the reading of input.

35. Define regular expression?It is built up out of simpler regular expression using a set of defining rules. Each regular expression

‘r’ denotes a language L(r). The defining rules specify how L(r) is formed by combining in various ways the languages denoted by the sub expressions of r.

36. Give the precedence of regular expression operator?i) The unary operator * has the highest precedence and is left associative.ii) Concatenation has the second highest precedence and is left associative.iii) / has the lowest precedence and is left associative.

Page 5: Principles of Compiler Design

37. Define the length of a string?It is the number of occurrences of symbols in string,‘s’ denoted by |s|.

Example: s=abc, |s| =3.

38. Give the rules in regular expression?1)   € is a regular expression that denotes {€}, that is the set containing the empty string.2)   If ‘a’ is a symbol in ∑ , then a is a regular expression that denoted {a} , i.e., the set containing the string a.3)    Suppose r and s are regular expression denoting the languages L( r) and L(s) .

39. Define regular set? A language denoted by a regular expression is said to be a regular set.

40. Give the types of notational shorthand’s of RE?·        Zero or more instance·        One or more instance·        Character classes·        Character not in a given set and unary operator? 41. Define kleene closure or star closure and positive closure? Star closure of L, denoted L*, is the set of those strings that can be formed by taking any number of strings from L and concatenating all of them.

L* = Li

Positive closure of L ,denoted L+, is the set of those strings that can beFormed by one or more concatenations of LL+ = Li

42. Define character class with example. The notation [abc] where a, b, c are alphabet symbols denotes the regular expression a/b/c. Example: [A-z] = a | b | c | -------| z Regular expression for identifiers using character classes [a – z A – Z] [A – Z a – z 0 – 9] *

43. Give the error recovery strategies in lexical analyzer.a. Deleting an extraneous characterb. Inserting a missing characterc. Replacing an incorrect character by a correct characterd. Transposing of two adjacent character

44. Write the R.E. for the following language.      i.Set of statements over {a,b,c} that contain no two consecutive b’s (B/c) (A/c/ab/cb) *     ii. Set of statements over {a,b,c} that contain an even no of a’s ((b/c)* a (b/c) * a)* (b/c)*

45. Describe the language denoted by the following R.E.       i. (0/1)*0(0/1)(0/1) The set of all strings of 0’s and 1’s with the third symbol from the right end is 0.       ii. 0(0/1)*0

Page 6: Principles of Compiler Design

The set of all strings of 0’s and 1’s starting and ending with 0.       iii. (00/11)*((01/10)(00/11)*(01/10)(00/11)*) The set of all strings of 0’s and 1’s with even number of 0’s and 1’s46. Define                          i.      L.L* = L+

                        ii.      1.1* = 1+

47. What are the tasks in lexical analyzer?a.One task is stripping out from the source program comments and white space in the form of blank,

tab, new line characters.b. Another task is correlating error messages from the compiler with the source program.

48. Define finite automata and its types with eg.A recognizer for a language is a program that takes as input a string x and answers “yes” if x is a

sentence of the language and “no” otherwise.A better way to convert a regular expression to a recognizer is to construct a generalized transition diagram from the expression. This diagram is called a finite automation.1.     Deterministic (DFA)2.     Non-deterministic (NFA)

49. What are the three parts of lexical program?·        Declarations %%·        Translation rules %%·        Auxiliary procedures

50. What are the four functions of regular expression to DFA? Nullable (n) Firstpos (n) Last (n) Followpos (n)

51. What are the models of LEX compiler? Lexeme FA simulator Transition table

Page 7: Principles of Compiler Design

UNIT III – SYNTAX ANALYSIS and RUN TIME ENVIRONMENT

1.  What do u meant by parser and its types?A parser for grammar G is a program that takes as input a string ‘w’ and produces as output either a

parse tree for’w’, if ‘w’ is a sentence of G, or an error message indicating that w is not a sentence of G. it obtains a string of tokens from the lexical analyzer, verifies that the string generated by the grammar for the source language.

1. Top down parsing2. Bottom up parsing

2.  What are the different levels of syntax error handler?a)     Lexical, such as misspelling an identifier, keyword, or operator.b)    Syntactic, such as an arithmetic expression with unbalanced parenthesesc)     Semantic, such as operator applied to an incompatible operandd)    Logical, such as an infinitely recursive call.

3. What are the goals of error handler in a parser?a. It should report the presence of errors clearly and accuratelyb. It should recover from each error quickly enough to be able to detect subsequent errorsc. It should not significantly slow down the processing of correct programs.

4. What are error recovery strategies in parser? a)     Panic mode b)    Phrase level c)     Error productions d)    Global corrections

5. Define CFG?Many programming language has rules that prescribe the syntactic structure of well-formed

programs. The syntax of programming language constructs can be described by CFG. Conditional statement defined by a rule such as; If S1 and S2 are statements and E is an expression, then“If E then S1 else S2” is a statement.

6. Define derivations. Give an example and its types?We apply the productions of a CFG to infer that certain strings are in the language of a certain

variable. There are two approaches (a) derivations (b) recursive inference or reduction. Then derivation uses the production from head to body. A replacement according to a production is known as as derivationi) Left most derivationii) Right most derivation or canonical derivations Eà E+Eàid+Eàid+(E) à id+(E+E)àid+(id*E) à id+(id*id)

7. Define ambiguity?A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

8. Define sentential form?If G = (V, T, P, S) is a CFG, then any string ‘α’ in (VUT)* such that Sà* α is a sentential form.

9. Define yield of the string?A string that is derived from the root variable is called the yield of the tree.

Page 8: Principles of Compiler Design

10. Give the several reasons for writing a grammar?i) The lexical rules of a language are frequently quite simple and to describe them we do not need a notation as powerful as grammars.ii) R.E generally provides a more concise and easier to understand notation for token than grammars.iii) More efficient lexical analyzers can be constructed automatically from R.E than from arbitrary grammars.iv) Separating the syntactic structure of a language into lexical and non lexical parts provide a convenient way of modularizing the front end of a compiler into two manageable-sized components.

11. Define left factoring?The process of factoring out of common prefixes of alternates is called as left factoring.

12. What are the difficulties with top down parsing?a)     Left recursionb)    Backtrackingc)     The order in which alternates are tried can affect the language acceptedd)    When failure is reported. We have very little idea where the error actually occurred.

13. What is meant by recursive-descent parser?A parser that uses a set of recursive procedures to recognize its input with no backtracking is called

a recursive-descent parser. To avoid the necessity of a recursive language, we shall also consider a tabular implementation of recursive descent called predictive parsing.

14. Define top down parsing? It can be viewed as an attempt to find the left most derivation for an input string. It can be viewed as attempting to construct a parse tree for the input starting from the root and creating the nodes of the parse tree in preorder.

15. Define LL (1) grammar? A grammar G is LL (1) if and only if, whenever Aà α / β are two distinct productions of G of the following conditionsa)     For no terminal ‘a’ do both α and β derive strings beginning with α.b)    At most one of α and β can derive the empty string.c)     If β*è e then α does not derive any string beginning with a terminal in FOLLOW (A).

16. What are the possibilities of non-recursive predictive parsing?a)    If X = a = $ the parser halts and announces successful completion of parsingb)    If X = a = $ the parser pops X off the stack and advances the input pointer to the next symbolc)     If X is a nonterminal, the program consults entry M[X,a] of the parsing table M. this entry will be either an X-production of the grammar or an error entry.

17. What are the actions available in shift reduce parser?a)     Shiftb)    Reducec)     Acceptd)    Error

18. Define handle? A handle of a string is a substring that matches the right side of a production, and whose reduction

to the non-terminal on the left side of the production represents one step along the reverse of a right most derivation.

Page 9: Principles of Compiler Design

19. Define viable prefixes? The set of prefixes of right sentential forms that can appear on the stack of a shift reduce parser are called viable prefixes.

20. What are the two common ways of determining precedence relations should hold between a pair of terminals?a)     Based on associative and precedence of operators.b)    Construct an unambiguous grammar for the language, a grammar that reflects the correct associativity and precedence in its parse tree.

21. Define operator grammar? A grammar with the property that no production right side is £ or has two adjacent non-terminals are called an operator grammar.

22. Define LR parser?LR parsers can be used to parse a large class of context free grammars. The technique is called LR

(K) parsing.“L” denotes that input sequence is processed from left to right“R” denotes that the right most derivation is performed“K” denotes that at most K symbols of the sequence are used to make a decision.

23. What are the drawbacks of LR parser?a)     Parsing tables are too complicated to be generated by hand, need an automated parser generator.b)    Cannot handle ambiguous grammar without special tricks.

24. Give the reasons for LR parser?a)     LR parsers can handle a large class of CF languagesb)    An LR parser can detect syntax errors as soon as they occurc)     The LR parsing method is the most general non-back tracking shift reduce parsing methodd)    LR parsers can handle all language recognizable by LL(1).

25. What are the techniques for producing LR parsing Table?1)    Shift s, where s is a state2)    Reduce by a grammar production Aà β3)    Accept and4)    Error

26. Define augmented grammar? If G is a grammar with start symbol S, then G’ the augmented grammar for G, is G with a new start symbol S’ and production S’àS. the purpose of this new starting production is to indicate to the parser when it should stop parsing and announce acceptance of the input.

27. Define LR (0) items? LR (0) item for a grammar G is a production of G with a dot at some position of the right side. Thus production AàXYZ yields the four items.

Aà.XYZ, AàX.YZ, AàXY.Z, AàXYZ.28. What are the two functions of LR parsing algorithm?a)     Action function b)    GOTO function

Page 10: Principles of Compiler Design

29. What are the three types of YACC?a)     Declarations b)    Translation rulesc)     Supporting c-routines

30. Give note about semantic actions?It is a sequence of c statements. In a semantic action, the symbol $$ refers to the attribute value

associated with the NT on the left, and $i refers to the value associated with the i th grammar symbol on the right. It is called semantic action.

31. Define SLR parser?The parsing table consisting of the parsing action and goto function determined by constructing an

SLR parsing table algorithm is called SLR(1) table. An LR parser using the SLR (1) table is called SLR (1) parser. A grammar having an SLR (1) parsing table is called SLR (1) grammar.

32. Give the two optional sections in the declarations parts? a)  Ordinary ‘c’ declarations Delimited by % {and %}, a temporary variables used by the translation rules or procedures. b)    Declarations of grammar tokens.

33. What are two classes of items in LR parser? a) Kernel items, which include the initial item, S’à.S, and all items whose dots are not at the left end. b)    Non-Kernel items, which have their dots at the left end.

34. What are the three techniques for constructing LR parsing table?a)     SLR (simple LR)b)    Canonical LRc)     LALR (Look ahead LR)

35. Define bottom up parsing?It attempts to construct a parse tree for an input string is beginning at leaves and working up

towards the root (i.e.) reducing a string ‘w’ to the start symbol of a grammar. At each reduction step, a particular substring matching the right side of a production is replaced by the symbol on the left of that production. It is a rightmost derivation and it’s also known as shifts reduce parsing.

36. Define LALR grammar?Last parser construction method, the LALR technique. This method is often used in practice

because the tables obtained by it are considerably smaller than the canonical LR tables, yet most common syntactic constructs of programming language can be expressed conveniently by an LALR grammar. If there are no parsing action conflicts, then the given grammar is said to be an LALR (1) grammar. The collection of sets of items constructed is called LALR (1) collections.

37. Define operator precedence grammar?It is an £-free operator grammar in which the precedence relations <., =., and .> constructed as

above are disjoint. That is for any pair of terminals a and b, never more than one of the relations a<.b, a=.b, and a.>b is true.

38. Define handle pruning? Reducing β to A in αβw is called handle pruning, i.e., removing the children of A from the parse tree.

Page 11: Principles of Compiler Design

39. Eliminate left recursion from the grammar. Sà Aa / b, AàAc / Sd/e?

Replaced as AàbdA’ / A’ A’àcA’/ adA’/ £

Finally we obtain SàAa / b, AàbdA’ / A’, A’àcA’ / adA’ / £

40. Define left recursion. Give an example?A grammar is left recursive if it has a nonterminal ‘A’ such that there is a derivation Aà+ Aα for

some strings α.Example: Pair of productions AàAα/β. Replaced by nonterminal productions AàβA’ and AàαA’€E41. What are the two standard storage allocation strategies?The two standard allocation strategies are1. Static allocation.2. Stack allocation

42.Discuss about static allocation.In static allocation the position of an activation record in memory is fixed at run time.

43. Write short notes on activation tree. Nov/Dec 2007A tree which depicts the way of control enters and leaves activations.

In an activation treeEach node represents an activation of an procedure The root represents the activation of the main program.The node for a is the parent of the node for b , if and only if control flows from activation a to bNode for a is to the left of the node for b, if and only if the lifetime of a occurs before the lifetime of b.

44. Define control stack.A stack which is used to keep track of live procedure actions is known as control stack.

45. Define heap.A separate area of run-time memory which holds all other information is called a heap.

46. Give the structure of general activation record

Returned value

Actual parameters

Optional control link

Page 12: Principles of Compiler Design

Optional access link

Saved machine status

Local data

Temporaries

47. Discuss about stack allocation.In stack allocation a new activation record is pushed on to the stack for each execution of a

procedure. The record is popped when the activation ends.

48. What are the 2 approaches to implement dynamic scope?Deep accessShallow access

49. What is padding?Space left unused due to alignment consideration is referred to as padding.

50. What are the 3 areas used by storage allocation strategies?Static allocationStack allocationHeap allocation

51. What are the limitations of using static allocation?The size of a data object and constraints on its position in memory must be known at compile time.

Recursive procedures are restricted, because all activations of a procedure use the same bindings for local nameData structures cannot be created dynamically since there is no mechanism for storage allocation at run time

52. Define calling sequence and return sequence.A call sequence allocates an activation record and enters information into its fields

A return sequence restores the state of the machine so that calling procedure can continue execution.

53. When dangling reference occurs?A dangling reference occurs when there is storage that has been deallocated.

It is logical error to use dangling references, since the value of deallocated storage is undefined according to the semantics of most languages.

54. Define static scope rule and dynamic rule1. Lexical or static scope rule determines the declaration that applies to a name by a examining the program text alone.2. Dynamic scope rule determines the declaration applicable to name at runtime, by considering the current activations.

Page 13: Principles of Compiler Design

55. What is block? Give its syntax.A block is a statement containing its own data declaration.

Syntax:{Declaration statements}

56. What is access link?An access link is a pointer to each activation record which obtains a direct implementation of

lexical scope for nested procedure.

57. What is known as environment and state?The term environment refers to a function that maps a name to a storage location.

The term state refers to a function that maps a storage location to the value held there.

58. How the run-time memory is sub-divided?1. Generated target code2. Data objects3. A counterpart of the control stack to keep track of procedure activations.

Page 14: Principles of Compiler Design

UNIT III– INTERMEDIATE CODE GENERATION

1. What are the benefits of using machine-independent intermediate form? Retargeting is facilitated; a compiler for a different machine can be created by attaching a

back end for the new machine to an existing front end. A machine-independent code optimizer can be applied to the intermediate representation.

2. List the three kinds of intermediate representation.The three kinds of intermediate representations are

i. Syntax treesii. Postfix notation

iii. Three address code

3. How can you generate three-address code?The three-address code is generated using semantic rules that are similar to those for

constructing syntax trees for generating postfix notation.

4. What is a syntax tree? Draw the syntax tree for the assignment statement a := b * -c + b * -c.

A syntax tree depicts the natural hierarchical structure of a source program. Syntax tree:

assign

a +

* *

b uminus b uminus

c c

5. What is postfix notation?A Postfix notation is a linearized representation of a syntax tree. It is a list of nodes of the

tree in which a node appears immediately after its children.

6. What is the usage of syntax directed definition.Syntax trees for assignment statement are produced by the syntax directed definition.

Page 15: Principles of Compiler Design

7. Why “Three address code” is named so?The reason for the term “Three address code” is that each usually contains three addresses,

two for operands and one for the result.

8. Define three-address code. Three-address code is a sequence of statements of the general form

x := y op z

where x, y and z are names, constants, or compiler-generated temporaries; op stands for any operator, such as fixed or floating-point arithmetic operator, or a logical operator on boolean-valued data.

Three-address code is a linearized representation of a syntax tree or a dag in which explicit names correspond to the interior nodes of the graph.

9. State quadruple

A quadruple is a record structure with four fields, which we call op, arg1, arg2 and result.10. What is called an abstract or syntax tree?

A tree in which each leaf represents an operand and each interior node an operator is called as abstract or syntax tree.

11. Construct Three address code for the followingposition := initial + rate * 60

Ans: temp1 := inttoreal(60)

temp2 := id3 * temp1 temp3 := id2 + temp2

id1 := temp3

12. What are triples? The fields arg1,and arg2 for the arguments of op, are either pointers to thesymbol table or pointers into the triple structure then the three fields used in the intermediate code format are called triples. In other words the intermediate code format is known as triples.

13. Draw the DAG for a := b * -c + b * -c assign

a +

*

b uminus

c

Page 16: Principles of Compiler Design

14. List the types of three address statements.The types of three address statements are

a. Assignment statementsb. Assignment Instructionsc. Copy statementsd. Unconditional Jumpse. Conditional jumpsf. Indexed assignmentsg. Address and pointer assignmentsh. Procedure calls and return

15. What are the various methods of implementing three-address statements?i. Quadruples

ii. Triplesiii. Indirect triples

16. What is meant by declaration?The process of declaring keywords, procedures, functions, variables, and statements with proper

syntax is called declaration.

17. How semantic rules are defined?The semantic rules are defined by the following waysa. mktable(previous)b. enter(table,name,type,offset)c. addwith(table,width)d. enterproc(table,name,newtable)e.

18. What are the two primary purposes of Boolean Expressions? They are used to compute logical values They are used as conditional expressions in statements that alter the flow of control, such as

if-then, if-then-else, or while-do statements.

19. Define Boolean Expression.Expressions which are composed of the Boolean operators (and, or, and not) applied to elements

that are Boolean variables or relational expressions are known as Boolean expressions

20. What are the two methods to represent the value of a Boolean expression?i. The first method is to encode true and false numerically and to evaluate a Boolean

expression analogously to an arithmetic expression.ii. The second principal method of implementing Boolean expression is by flow of control

that is representing the value of a Boolean expression by a position reached in a program.

21. What do you mean by viable prefixes. Nov/Dec 2004Viable prefixes are the set of prefixes of right sentinels forms that can appear on the stack of shift/reduce parser are called viable prefixes. It is always possible to add terminal symbols to the end of the viable prefix to obtain a right sentential form.

22. What is meant by Shot-Circuit or jumping code?We can also translate a 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 called “short-circuit” or “jumping” code.

Page 17: Principles of Compiler Design

23. What is known as calling sequence?A sequence of actions taken on entry to and exit from each procedure is known as calling

sequence.

24. What is the intermediate code representation for the expression a or b and not c? (Or) Translate a or b and not c into three address code.Three-address sequence is

t1 := not c

t2 := b and t1

t3 := a or t2

25. Translate the conditional statement if a<b then 1 else 0 into three address code.Three-address sequence is

100: if a < b goto 103

101: t := 0

102: goto 104

103: t := 1

104:

26. Explain the following functions:i) makelist(i) ii) merge(p1,p2) iii) backpatch(p,i)

i. makelist(i) creates a new list containing only I, an index into the array of quadruples; makelist returns a pointer to the list it has made.

ii. merge(p1,p2) concatenates the lists pointed to by p1 and p2 , and returns a pointer to the concatenated list.

iii. backpatch(p,i) inserts i as the target label for each of the statements on the list pointed to by p.

27. Define back patching. May/June 2007 & Nov/Dec 2007Back patching is the activity of filling up unspecified information of labels using appropriate semantic actions in during the code generation process.

28. What are the methods of representing a syntax tree?i. Each node is represented as a record with a field for its operator and additional fields for

pointers to its children.ii. Nodes are allocated from an array of records and the index or position of the node serves as the

pointer to the node.

29. Give the syntax directed definition for if-else statement.Ans:

Production Semantic rule

S if E then S1 else S2 E.true := newlabel;

Page 18: Principles of Compiler Design

E.false := newlabel;

S1.next := S.next

S2.next := S.next

S.code := E.code || gen(E.true ‘:’) || S1.code ||

gen(‘goto’ S.next) || gen(E.false ‘:’) ||

S2.code

30. What are the advantages of generating an intermediate representation?a. Ease of conversion from the source program to the intermediate code.b. Ease with which subsequent processing can be performed from the intermediate code.

31.  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.

32.  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.

33. 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.

34. 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

35. Define syntax tree. Give an example?An (abstract) syntax tree is a condensed form of parse tree useful for representing language

constructs.Example: syntax tree for a – 4 + c

36. 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)

Page 19: Principles of Compiler Design

37. 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.

38. Define DAG. Give an example?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)

39. 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. b)   low-of –control. Represent the value of a Boolean expression by a position reached in a program.

40. 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.

41. 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

42. Give the advantages of quadruples?i) Can perform peephole optimization.ii) The contents of field’s arg1, arg2 and result are normally pointersiii) The symbol-table entries for the names represented by these fields.iv) If So, temporary names must be entered into the symbol table as they Are created.

43.  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.

44. 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.

45. 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.

Page 20: Principles of Compiler Design

46. What are the three address code for a or b and not c?The three address sequence is

T1:= not cT2:= b and T1T3:= a or T2.

47. Write a three address code for the expression a < b or c < d?100: if a<b goto 103101: t1:=0102: goto 104103: t1:=1104: if c<d goto 107105: t2:=0106: goto 108107: t2:=1108: t3:=t1 or t2

48. What are the parameter transmission mechanisms?1.     Call by value2.     Call by value-result3.     Call by reference4.     Call by name

49. Construct a DAG for the expression I: = I + 10?:=

+

I 1050. What are the various data structure used for implementing the symbol table?1.     Linear list2.     Binary tree3.     Hash table

51. 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.

52. What are the three functions of backpatching?Make list (i) – create a new list.Merge (p1, p2) – concatenates the lists pointed to by p1 and p2.Backpatch (p,i) – insert i as the target label for the statements pointed to by p.

53. 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.

54. 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.

Page 21: Principles of Compiler Design

Main ( ){ Print (5); } Int

Void print (int n){ printf (“%d”, n); }

55. 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.

56. What are the semantic rules are defined in the declarations operations?1)    Mktable(previous)2)    Enter(table, name,type,offset)3)    Addwidth(table,width)4)    Enterproc(table,namenewtable)

57. 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.

58. Give the syntax of case statements?Switch expression

BeginCase value: statementCase value: statement -------Case value: statementDefault : statement

End

59. Give the 2 attributes of syntax directed translation into 3-addr code?i)   E.place, the name that will hold the value of E .ii)  E.code, the sequence of 3-addr statements evaluating E.

60. 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.

61. 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.

62. Write the grammar for flow-of-control statements?The following grammar generates the flow-of-control statements, if-then, if-then-else, and while-do

statements.

Page 22: Principles of Compiler Design

S à if E then S1 | If E then S1 else S2

| While E do S1.63. Write the 3-addr code for the statements a =b*-c + b*-c? Three address codes are: a=b*-c + b*-cT1 = -cT2 = b*T1T3 = -cT4 = b*T3T5 = T2+T4a:= T5.

Page 23: Principles of Compiler Design

UNIT IV – CODE GENERATION1. How the quality of object program is measured?

The quality of an object program is measured by its Size or its running time. For large computation running time is particularly important. For small computations size may be as important or even more.

2. What is the more accurate term for code optimization?The more accurate term for code optimization would be “code improvement”

3. Explain the principle sources of optimization.Code optimization techniques are generally applied after syntax analysis, usually both before and

during code generation. The techniques consist of detecting patterns in the program and replacing these patterns by equivalent and more efficient constructs.

4. What are the patterns used for code optimization?The patterns may be local or global and replacement strategy may be a machine dependent or

independent

5. What are the 3 areas of code optimization? Local optimization Loop optimization Data flow analysis

6. Define local optimization.The optimization performed within a block of code is called a local optimization.

7. Define constant folding.Deducing at compile time that the value of an expression is a constant and using the constant instead is

known as constant folding.

8. What do you mean by inner loops?The most heavily traveled parts of a program, the inner loops, are an obvious target for optimization.

Typical loop optimizations are the removal of loop invariant computations and the elimination of induction variables.

9. What is code motion? April/May 2004, May/June 2007, April/May-2008Code motion is an important modification that decreases the amount of code in a loop.

Page 24: Principles of Compiler Design

Code optimizer

Control flow analysis

Data Flow analysis

Transformations

Front end Code generator

10. What are the properties of optimizing compilers? Transformation must preserve the meaning of programs. Transformation must, on the average, speed up the programs by a measurable amount A Transformation must be worth the effort.

11. Give the block diagram of organization of code optimizer.

12. What are the advantages of the organization of code optimizer?a. The operations needed to implement high level constructs are made explicit in the intermediate

code, so it is possible to optimize them.b. The intermediate code can be independent of the target machine, so the optimizer does not have

to change much if the code generator is replaced by one for a different machine

13. Define Local transformation & Global Transformation.A transformation of a program is called Local, if it can be performed by looking only at the

statements in a basic block otherwise it is called global.

14. Give examples for function preserving transformations. Common subexpression elimination Copy propagation Dead – code elimination Constant folding

15. What is meant by Common Subexpressions?An occurrence of an expression E is called a common subexpression, if E was previously computed,

and the values of variables in E have not changed since the previous computation.

16. What is meant by Dead Code?A variable is live at a point in a program if its value can be used subsequently otherwise, it is dead

at that point. The statement that computes values that never get used is known Dead code or useless code.

Page 25: Principles of Compiler Design

17. What are the techniques used for loop optimization?i) Code motionii) Induction variable eliminationiii) Reduction in strength

18. What is meant by Reduction in strength?Reduction in strength is the one which replaces an expensive operation by a cheaper one such as a

multiplication by an addition.

19. What is meant by loop invariant computation?An expression that yields the same result independent of the number of times the loop is executed is

known as loop invariant computation.

20. Define data flow equations.A typical equation has the form

Out[S] = gen[S] U (In[S] – kill[S])

and can be read as, “ the information at the end of a statement is either generated within the statement, or enters at the beginning and is not killed as control flows through the statement”. Such equations are called data flow equations.

1. What are the two standard storage allocation strategies?The two standard allocation strategies are

1. Static allocation.2. Stack allocation

2. Discuss about static allocation.In static allocation the position of an activation record in memory is fixed at run time.

3. Write short notes on activation tree. Nov/Dec 20071) A tree which depicts the way of control enters and leaves activations.2) In an activation tree

i. Each node represents an activation of an procedure ii. The root represents the activation of the main program.

iii. The node for a is the parent of the node for b , if and only if control flows from activation a to b

iv. Node for a is to the left of the node for b, if and only if the lifetime of a occurs before the lifetime of b.

4. Define control stack.

Page 26: Principles of Compiler Design

A stack which is used to keep track of live procedure actions is known as control stack.

5. Define heap.A separate area of run-time memory which holds all other information is called a heap.

6. Give the structure of general activation record

Returned value

Actual parameters

Optional control link

Optional access link

Saved machine status

Local data

Temporaries

7. Discuss about stack allocation.In stack allocation a new activation record is pushed on to the stack for each execution of a procedure.

The record is popped when the activation ends.

8. What are the 2 approaches to implement dynamic scope?1) Deep access2) Shallow access

9. What is padding?Space left unused due to alignment consideration is referred to as padding.

10. What are the 3 areas used by storage allocation strategies?1) Static allocation

Page 27: Principles of Compiler Design

2) Stack allocation3) Heap allocation

11. What are the limitations of using static allocation?1) The size of a data object and constraints on its position in memory must be known at compile time.2) Recursive procedure are restricted, because all activations of a procedure use the same bindings

for local name3) Data structures cannot be created dynamically since there is no mechanism for storage allocation

at run time

12. Define calling sequence and return sequence.1) A call sequence allocates an activation record and enters information into its fields 2) A return sequence restores the state of the machine so that calling procedure can continue

execution.

13. When dangling reference occurs?1) A dangling reference occurs when there is storage that has been deallocated.2) It is logical error to use dangling references, since the value of deallocated storage is undefined

according to the semantics of most languages.

14. Define static scope rule and dynamic rule1) Lexical or static scope rule determines the declaration that applies to a name by a examining the

program text alone.2) Dynamic scope rule determines the declaration applicable to name at runtime, by considering the

current activations.

15. What is block? Give its syntax.1) A block is a statement containing its own data declaration.2) Syntax:

{

Declaration statements

}

16. What is access link?1) An access link is a pointer to each activation record which obtains a direct implementation of lexical

scope for nested procedure.

17. What is known as environment and state?1) The term environment refers to a function that maps a name to a storage location.2) The term state refers to a function that maps a storage location to the value held there.

18. How the run-time memory is sub-divided?1) Generated target code2) Data objects

Page 28: Principles of Compiler Design

3) A counterpart of the control stack to keep track of procedure activations.

1. Define code generations with ex?It is the final phase in compiler model and it takes as an input an intermediate representation of the

source program and output produces as equivalent target programs. Then intermediate instructions are each translated into a sequence of machine instructions that perform the same task.

2. What are the issues in the design of code generator?a. Input to the generatorb. Target programsc. Memory managementd. Instruction selectione. Register allocationf. Choice of evaluation order g. Approaches to code generation.

3. Give the variety of forms in target program.a. Absolute machine language.b. Relocatable machine language.c. Assembly language.

4. Give the factors of instruction selections.a. Uniformity and completeness of the instruction setsb. Instruction speed and machine idiomsc. Size of the instruction sets.

5. What are the sub problems in register allocation strategies? During register allocation, we select the set of variables that will reside in register at a point in the

program. During a subsequent register assignment phase, we pick the specific register that a variable reside

in.

6. Give the standard storage allocation strategies. Static allocation Stack allocation.

7. Define static allocations and stack allocationsStatic allocation is defined as lays out for all data objects at compile time.

Names are bound to storage as a program is compiled, so there is no need for a Run time support package.

Stack allocation is defined as process in which manages the run time as a Stack. It is based on the idea of a control stack; storage is organized as a stack, And activation records are pushed and popped as activations begin and end.

8. Write the addressing mode and associated costs in the target machine.

MODE FORM ADDRESS ADDED COSTAbsolute M M 1Register R R 0Indexed c(R) c+contents(R) 1

Page 29: Principles of Compiler Design

Indirect register *R contents(R) 0Indirect indexed *c(R) contents(c+contents(R)) 1

9. Define basic block and flow graph.A basic block is a sequence of consecutive statements in which flow of

Control enters at the beginning and leaves at the end without halt or possibility Of branching except at the end.

A flow graph is defined as the adding of flow of control information to the Set of basic blocks making up a program by constructing a directed graph.

10. Write the step to partition a sequence of 3 address statements into basic blocks.1. First determine the set of leaders, the first statement of basic blocks.·        The rules we can use are the following.·        The first statement is a leader.·        Any statement that is the target of a conditional or unconditional goto is a leader.·        Any statement that immediately follows a goto or conditional goto statement is a leader. 2. For each leader, its basic blocks consists of the leader and all statements Up to but not including the next leader or the end of the program.

11. Give the important classes of local transformations on basic blocks Structure preservation transformations Algebraic transformations.

12. Describe algebraic transformations. It can be used to change the set of expressions computed by a basic blocks into A algebraically equivalent sets. The useful ones are those that simplify the expressions place expensive operations by cheaper ones.

X = X+ 0X = X * 1

13. What is meant by register descriptors and address descriptors? A register descriptor keeps track of what is currently in each register. It is consulted whenever a new

register is needed. An address descriptor keeps track of the location where ever the current Value of the name can be

found at run time. The location might be a register, Stack location, a memory address,

14. What are the actions to perform the code generation algorithms? Invoke a function get reg to determine the location L. Consult the address descriptor for y to determine y’, the current location of y. If the current values of y and/or z have no next uses, are not live on exit from the block, and are in

register, alter the register descriptor.

15. Write the code sequence for the d :=(a-b)+(a-c)+(a-c).Statement Code generation Register descriptor Address

descriptort:=a-b MOV a,R0

SUB b,R0R0 contains t t in R0

u:=a-c MOV a,R1SUB c,R1

R0 contains tR1 contains u

t in R0u in R1

v:=t+u ADD R1,R0 R0 contains vR1 contains u

u in R1v in R0

d:=v+u ADD R1,R0 R0 contains d d in R0

Page 30: Principles of Compiler Design

MOV R0,d d in R0 and memory

16. Write the labels on nodes in DAG.A DAG for a basic block is a directed acyclic graph with the following

Labels on nodes: Leaves are labeled by unique identifiers, either variable names or constants. Interior nodes are labeled by an operator symbol. Nodes are also optionally given a sequence of identifiers for labels.

17. Give the applications of DAG. Automatically detect the common sub expressions Determine which identifiers have their values used in the block. Determine which statements compute values that could be used outside the blocks.

18. What are basic blocks?A sequence of consecutive statements which may be entered only at the beginning and when

entered are executed in sequence without halt or possibility of branch , are called basic blocks.

19. What is a flow graph?1) The basic block and their successor relationships shown by a directed graph is called a flow graph.2) The nodes of a flow graph are the basic blocks.

20. What are the advantages and disadvantages of register allocation and assignments?Advantages:

i.It simplifies the design of a compiler Disadvantages:

i. It is applied too strictly.ii. It uses registers in efficiently. Certain registers may go unused over substantial portions of the code,

while unnecessary load and stores are generated.

21. What is meant by virtual machine?An intermediate language as a model assembly language, optimized for a non-existent but ideal

computer called a virtual machine.

22. Discuss back-end and front end? Back-end

Intermediate to binary translation is usually done by a separate compilation pass called back end. Front end

There are several back ends for different target machines, all of which use the same parser and code generator called front end.

23. Define relocatable object module.The unpatched binary image is usually called a relocatable object module.

24. What is meant by multiregister operations?We can modify our labeling algorithm to handle operations like multiplication, division, or function

calls which normally requires more than one register to perform. Hence this operation is called multiregister operations.

25. List the types of addressing modes:-i) Intermediate mode

Page 31: Principles of Compiler Design

ii) Direct modeiii) Indirect mode

iv) Effective address mode26. What is input to code generator?

The input to code generator consists of the intermediate representation of the source program produced by the front end together with information in the symbol table that is used to determine the run time addresses of the data objects denoted by the names in the intermediate representation.

27. How the use of registers is subdivided into 2 sub-problems?1) During register allocation we select the set of variables that will reside in registers at a point in the

program.2) During a subsequent register assignment phase, we pick the specific register that a variable will

reside in.28. How would you calculate the cost of an instruction?

The cost of an instruction to be one plus the costs associated with the source and destination address modes. This cost corresponds to the length of the instruction.

Address modes involving registers have cost zero, while those with a memory location or literal in them have cost one.

29. Give some examples for 3 address statements.1. Call2. Return3. Halt4. Action5. Use of machine idioms

30. What is a recursive procedure?A procedure is recursive a new activation can begin before an earlier activation of the same

procedure has ended.

31. What are the common methods for associating actual and formal parameters?1) Call-by-value2) Call-by-reference3) Copy-restore4) Call-by-name5) Macro-expansion

32. Define DAG. Nov/Dec 2007A DAG for a basic block is a directed acyclic graph with the following labels on nodes:

1) Leaves are labeled by unique identifiers, either variable names or constants.2) Interior nodes are labeled by an operator symbol.3) Nodes are also optionally given a sequence of identifiers for labels.

33. What are the issues in the design of code generators? Nov/Dec 2007a.Input to the code generatorb. Target programsc.Memory managementd. Instruction selectione.Register allocationf. Choice of evaluation orderg. Approaches to code generation

34. What are the various forms of target programs? Absolute machine language

Page 32: Principles of Compiler Design

Relocatable machine language Assembly language

35. What is memory management? Mapping names in the source program to addresses of data object in run time memory done comparatively by the front end and the code generator is called memory management

36. What are the rules to determine the leaders of basic blocks?1. The first statement is a leader2. Any statement that is the target of a conditional or unconditional goto is a leader3. Any statement that immediately follows a goto or conditional goto statement is a leader.

37. What is the use of algebraic transformation?Algebraic transformation can be used to change the set of expressions computed by a basic blocks

into an algebraically equivalent set.

38. What is meant by loop? A loop is a collection of nodes in a flow graph such that

1. All nodes in the collection are strongly connected i.e., from any node in the loop to any other, there is a path of length one or more, wholly within the loop

2. The collection of nodes has a unique entry, i.e. a node in the loop such that the only way to reach a node of the loop from a node outside the loop is to first go through the entry.

39. What is register descriptor and address descriptor? A register descriptor keeps track of what is currently in each register. An address descriptor keeps track of the location where the current value of the name can be found at run time.

40. What are the primary structure preserving transformations on basic blocks?1. Common sub-expression elimination2. Dead-code elimination3. Renaming of temporary variable4. Interchange of 2 independent adjacent statements.

Page 33: Principles of Compiler Design

Code optimizer

Control flow analysis

Data Flow analysis

Transformations

Front end Code generator

UNIT –V CODE OPTIMIZATION21. How the quality of object program is measured?

The quality of an object program is measured by its Size or its running time. For large computation running time is particularly important. For small computations size may be as important or even more.

22. What is the more accurate term for code optimization?The more accurate term for code optimization would be “code improvement”

23. Explain the principle sources of optimization.Code optimization techniques are generally applied after syntax analysis, usually both before and

during code generation. The techniques consist of detecting patterns in the program and replacing these patterns by equivalent and more efficient constructs.

24. What are the patterns used for code optimization?The patterns may be local or global and replacement strategy may be a machine dependent or

independent

25. What are the 3 areas of code optimization? Local optimization Loop optimization Data flow analysis26. Define local optimization.

The optimization performed within a block of code is called a local optimization.

27. Define constant folding.Deducing at compile time that the value of an expression is a constant and using the constant instead is

known as constant folding.

8. What do you mean by inner loops?The most heavily traveled parts of a program, the inner loops, are an obvious target for optimization.

Typical loop optimizations are the removal of loop invariant computations and the elimination of induction variables.

28. What is code motion? April/May 2004, May/June 2007, April/May-2008Code motion is an important modification that decreases the amount of code in a loop.

29. What are the properties of optimizing compilers? Transformation must preserve the meaning of programs. Transformation must, on the average, speed up the programs by a measurable amount A Transformation must be worth the effort.30. Give the block diagram of organization of code optimizer.

Page 34: Principles of Compiler Design

31. What are the advantages of the organization of code optimizer?c. The operations needed to implement high level constructs are made explicit in the intermediate code, so it is possible to optimize them.d. The intermediate code can be independent of the target machine, so the optimizer does not have to change much if the code generator is replaced by one for a different machine

32. Define Local transformation & Global Transformation.A transformation of a program is called Local, if it can be performed by looking only at the statements

in a basic block otherwise it is called global.

33. Give examples for function preserving transformations. Common sub expression elimination Copy propagation Dead – code elimination Constant folding

34. What is meant by Common Subexpressions?An occurrence of an expression E is called a common subexpression, if E was previously computed,

and the values of variables in E have not changed since the previous computation.

35. What is meant by Dead Code?A variable is live at a point in a program if its value can be used subsequently otherwise, it is dead at

that point. The statement that computes values that never get used is known Dead code or useless code.

36. What are the techniques used for loop optimization?iv) Code motionv) Induction variable eliminationvi) Reduction in strength

37. What is meant by Reduction in strength?Reduction in strength is the one which replaces an expensive operation by a cheaper one such as a

multiplication by an addition.

38. What is meant by loop invariant computation?An expression that yields the same result independent of the number of times the loop is executed is

known as loop invariant computation.

39. Define data flow equations.A typical equation has the form

Out[S] = gen[S] U (In[S] – kill[S])

and can be read as, “the information at the end of a statement is either generated within the statement, or enters at the beginning and is not killed as control flows through the statement”. Such equations are called data flow equations.

21. Define Peephole optimization.A Statement by statement code generation strategy often produces target code that contains

redundant instructions and suboptimal constructs. “Optimizing” is misleading because there is no guarantee that the resulting code is optimal. It is a method for trying to improve the performance of the target

Page 35: Principles of Compiler Design

program by examining the short sequence of target instructions and replacing this instructions by shorter or faster sequence.

22. Write the characteristics of peephole optimization? Redundant-instruction elimination Flow-of-control optimizations. Algebraic simplifications Use of machine idioms

23. What are the structure preserving transformations on basic blocks? Common sub-expression elimination Dead-code elimination Renaming of temporary variables Interchange of two independent adjacent statement

24. Define Common sub-expression elimination with ex.It is defined as the process in which eliminate the statements which has the same expressions.

Hence this basic block may be transformed into the equivalent Block.Ex:

a : =b + cb :=a - dc :=b + c

After elimination: a : =b + cb :=a - dc :=a

25. Define Dead-code elimination with ex.It is defined as the process in which the statement x=y+z appear in a basic

block, where x is a dead that is never subsequently used. Then this statement may be safely removed without changing the value of basic blocks.

23. Define Renaming of temporary variables with ex.We have the statement u:=b + c ,where u is a new temporary variable, and change all uses of this

instance of t to u, then the value of the basic block is not changed.

26. Define reduction in strength with ex.Reduction in strength replaces expensive operations by equivalent cheaper ones on the target machines.

Certain machine instructions are cheaper than others and can often be used as special cases of more expensive operators.Ex:

X^2 is invariably cheaper to implement as x*x than as a call to an exponentiation routine.

25. Define use of machine idioms.The target machine may have harder instructions to implement certain specific operations efficiently.

Detecting situations that permit the use of these instructions can reduce execution time significantly.

27. Define code optimization and optimizing compilerThe term code-optimization refers to techniques a compiler can employ in an attempt to produce a

better object language program than the most obvious for a given source program.Compilers that apply code-improving transformations are called Optimizing-compilers.

Page 36: Principles of Compiler Design