Warning: Undefined array key "amp-addthis" in /home/tgagmvup/onlinestudy.guru/wp-content/plugins/addthis/backend/AddThisSharingButtonsFeature.php on line 101
lang="en-US"> COMPILER DESIGN (MAY-2017) UNIT-III - onlinestudy.guru
Site icon onlinestudy.guru

COMPILER DESIGN (MAY-2017) UNIT-III

Question:-  6(a) Explain quadruples and triples with example. Write three address  code of expression: a+a*(b+c)+(b-c)*d

Answer: –

Quadruples

Each instruction in quadruples presentation is divided into four fields: operator, arg1, arg2, and result. The above example is represented below in quadruples format:

 Quadruple Representation :

 

 

Triples

Each instruction in triples presentation has three fields : op, arg1, and arg2.The results of respective sub-expressions are denoted by the position of expression. Triples represent similarity with DAG and syntax tree. They are equivalent to DAG while representing expressions.

Triples face the problem of code immovability while optimization, as the results are positional and changing the order or position of an expression may cause problems.

 Triple Representation

Indirect Triple Representation

This representation is an enhancement over triples representation. It uses pointers instead of position to store results. This enables the optimizers to freely re-position the sub-expression to produce an optimized code.

Comparison




Question: -6(b) Explain Various run time storage allocation strategies.

Answer: –Storage Allocation

Runtime environment manages runtime memory requirements for the following entities:

 

STORAGE ALLOCATION STRATEGIES

The different storage allocation strategies are :

  1. Static allocation – lays out storage for all data objects at compile time
  2. Stack allocation – manages the run-time storage as a stack.
  3. Heap allocation – allocates and deallocates storage as needed at run time from a data area known as heap.
Static Allocation

In this allocation scheme, the compilation data is bound to a fixed location in the memory and it does not change when the program executes. As the memory requirement and storage locations are known in advance, runtime support package for memory allocation and de-allocation is not required.

Stack Allocation

Procedure calls and their activations are managed by means of stack memory allocation. It works in last-in-first-out (LIFO) method and this allocation strategy is very useful for recursive procedure calls.

Heap Allocation

Variables local to a procedure are allocated and de-allocated only at runtime. Heap allocation is used to dynamically allocate memory to the variables and claim it back when the variables are no more required.

Except statically allocated memory area, both stack and heap memory can grow and shrink dynamically and unexpectedly. Therefore, they cannot be provided with a fixed amount of memory in the system.




Question: -7(a) Explain various data structure used for implementing symbol table and compare them.

Answer: –

Symbol Table in Compiler

Symbol Table is an important data structure created and maintained by the compiler in order to keep track of semantics of variable i.e. it stores information about scope and binding information about names, information about instances of various entities such as variable and function names, classes, objects, etc.

Symbol Table entries – Each entry in symbol table is associated with attributes that support compiler in different phases.
Items stored in Symbol table:

Information used by compiler from Symbol table:

Operations of Symbol table – The basic operations defined on a symbol table include:

 

 

Implementation of Symbol table –
Following are commonly used data structure for implementing symbol table :-

  1. List – 
    • In this method, an array is used to store names and associated information.
    • A pointer“available” is maintained at end of all stored records and new names are added in the order as they arrive
    • To search for a name we start from beginning of list till available pointer and if not found we get an error “use of undeclared name”
    • While inserting a new name we must ensure that it is not already present otherwise error occurs i.e. “Multiple defined name”
    • Insertion is fast O(1), but lookup is slow for large tables – O(n) on average
    • Advantage is that it takes minimum amount of space.
  2. Linked List–
    • This implementation is using linked list. A link field is added to each record.
    • Searching of names is done in order pointed by link of link field.
    • A pointer “First”is maintained to point to first record of symbol table.
    • Insertion is fast O(1), but lookup is slow for large tables – O(n) on average
  3. Hash Table–
    • In hashing scheme two tables are maintained – a hash table and symbol table and is the most commonly used method to implement symbol tables..
    • A hash table is an array with index range: 0 to tablesize – 1.These entries are pointer pointing to names of symbol table.
    • To search for a name we use hash function that will result in any integer between 0 to tablesize – 1.
    • Insertion and lookup can be made very fast – O(1).
    • Advantage is wuick search is possible and disadvantage is that hashing is complicated to implement.
  4. Binary Search Tree–
    • Another approach to oimplement symbol table is to use binary search tree i.e. we add two link fields i.e. left and right child.
    • All names are created as child of root node that always follow the property of binary search tree.
    • Insertion and lookup are O(log2n) on average.



Question: – 7(b) Give the general  structures of activation record. Explain the purpose or each component.

Answer: – The general  structures of activation record

Activation Records

Return value
Parameters
Local Data
Temporary Data
Links(optional)
Status

Activate Record

Local Data

Links

Status

Temporaries Stores temporary and intermediate values of an expression.
Local Data Stores local data of the called procedure.
Machine Status Stores machine status such as Registers, Program Counter etc., before the procedure is called.
Control Link Stores the address of activation record of the caller procedure.
Access Link Stores the information of data which is outside the local scope.
Actual Parameters Stores actual parameters, i.e., parameters which are used to send input to the called procedure.
Return Value Stores return values.

To understand this concept, we take a piece of code as an example:

. . .

Printf (“Enter Your Name: “);

Scanf (“%s”, username);

Show_data (username);

printf (“Press any key to continue…”);

. . .

int show_data (char *user)

{

Printf (“Your name is %s”, username);

return 0;

}

. . .




This article is contributed by Tarun Jangra. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




click here for solved UNIT-IV

Exit mobile version