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

UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

  • Upload
    lequynh

  • View
    1.731

  • Download
    53

Embed Size (px)

Citation preview

Page 1: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

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.

Page 2: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

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

Page 3: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

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.

Page 4: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

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

Page 5: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

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

Page 6: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

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

Page 7: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

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.

Page 8: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address
Page 9: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

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.

Page 10: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address

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

Page 11: UNIT IV INTERMEDIATE CODE GENERATION 1. …chettinadtech.ac.in/storage/13-11-07/13-11-07-12-17-51-2142... · Draw syntax tree for the expression a=b*-c+b*-c 2. ... Write a three address