Skip to main content

Write a program to perform constant propagation

  Write a program to perform constant propagation

Agenda-
1.Algorithm
2.Program for  Write a program to perform constant propagation
3.Input
4.Output

1.Algorithms

1. For all 4-tuples do 

   1.1 get next 4-tuple

   1.2 If the operator is "Label" then 

        1.2.1 free the constant table

        1.2.2 write the 4-tuple

 1.3 If either or both of the operands are in the constant table then

       1.3.1 substitute the constant value(s) for the operand(s) 

       1.3.2 if either of the operands are intermediate results then  

               1.3.2.1 remove those operand(s) from the constant table. 

1.4 if 4-tuple operand(s) are constant then 

      1.4.1 if operator is "JPCT" then 

              1.4.1.1 if the operand is true then 

                         1.4.1.1.1 replace the JPCT with JP 

                         1.4.1.1.2 write the new 4-tuple else

                         1.4.1.1.3 eliminate the 4-tuple 

1.4.2  if operator is "=" then 

               1.4.2.1 place the variable and constant in the constant table 

               1.4.2.2 write the 4-tuple with the constant operand 

1.4.3 else 

              1.4.3.1 Perform the operation.

              1.4.3.2 Place the intermediate result name and constant value in the constant table.Else

 1.4.4  write the 4-tuple Consider the following sequence.

 


 Program for  Write a program to perform constant propagation-

#include<stdio.h>

#include<string.h>

#include<ctype.h>

#include<conio.h>

void input();
void output();
void change(int p, char * res);
void constant();
struct expr {
  char op[2], op1[5], op2[5], res[5];
  int flag;
}
arr[10];
int n;
void main() {
  clrscr();
  input();
  constant();
  output();
  getch();
}
void input() {
  int i;
  printf("\n\nEnter the maximum number of  expressions : ");
  scanf("%d", & n);
  printf("\nEnter the input : \n");
  for (i = 0; i < n; i++) {
    scanf("%s", arr[i].op);
    scanf("%s", arr[i].op1);
    scanf("%s", arr[i].op2);
    scanf("%s", arr[i].res);
    arr[i].flag = 0;
  }
}
void constant() {
  int i;
  int op1, op2, res;
  char op, res1[5];
  for (i = 0; i < n; i++) {
    if (isdigit(arr[i].op1[0]) && isdigit(arr[i].op2[0]) || strcmp(arr[i].op, "=") == 0/*if both digits, store them in variables*/ {
      op1 = atoi(arr[i].op1);
      op2 = atoi(arr[i].op2);
      op = arr[i].op[0];
      switch (op) {
      case '+':
        res = op1 + op2;
        break;
      case '-':
        res = op1 - op2;
        break;
      case '*':
        res = op1 * op2;
        break;
      case '/':
        res = op1 / op2;
        break;
      case '=':
        res = op1;
        break;
      }
      sprintf(res1, "%d", res);
      arr[i].flag = 1/*eliminate expr and replace any operand below that uses result of this expr */
      change(i, res1);
    }
  }
}
void output() {
  int i = 0;
  printf("\nOptimized code is : ");
  for (i = 0; i < n; i++) {
    if (!arr[i].flag) {
      printf("\n%s %s %s %s", arr[i].op, arr[i].op1, arr[i].op2, arr[i].res);
    }
  }
}
void change(int p, char * res) {
  int i;
  for (i = p + 1; i < n; i++) {
    if (strcmp(arr[p].res, arr[i].op1) == 0)
      strcpy(arr[i].op1, res);
    else if (strcmp(arr[p].res, arr[i].op2) == 0)
      strcpy(arr[i].op2, res);
  }
}


Input-

Enter the maximum number of  expressions : 4

Enter the input :
= 3 - a
+ a b t1
+ a c t2
+ t1 t2 t3


Output-

Optimized code is :
+ 3 b t1
+ 3 c t2
+ t1 t2 t3

Comments

Popular posts from this blog

Write a code simulating ARP /RARP protocols

   Write a code simulating ARP /RARP protocols . Aim:        To write a java program for simulating ARP/RARP protocols ALGORITHM: server 1. Create a server socket and bind it to port. 2. Listen for new connection and when a connection arrives, accept it. 3. Send server ‟ s date and time to the client. 4. Read client ‟ s IP address sent by the client. 5. Display the client details. 6. Repeat steps 2-5 until the server is terminated. 7. Close all streams. 8. Close the server socket. 9. Stop. Client 1. Create a client socket and connect it to the server ‟ s port number. 2. Retrieve its own IP address using built-in function. 3. Send its address to the server. 4. Display the date & time sent by the server. 5. Close the input and output streams. 6. Close the client socket. 7. Stop. Program Program for Address Resolutuion Protocol (ARP) using TCP Client: import java.io.*; import java.net.*; impor...

Importants Links of Leetcode

  Here are all the important discussion posts which contain brief descriptions about a topic with its problem list. Recursion:- A.   https://leetcode.com/tag/recursion/discuss/1733447/Become-Master-In-Recursion Binary Tree and Binary Search Tree:- A.   https://leetcode.com/tag/binary-tree/discuss/1212004/Binary-Trees-study-guide B.   https://leetcode.com/tag/binary-tree/discuss/1820334/Become-Master-in-Tree C.   https://leetcode.com/tag/binary-tree/discuss/1094690/Views-and-Traversal-of-binary-tree-or-Important-topics-or-Must-Read-%3A- ) Dynamic Programming:- A.   https://leetcode.com/discuss/study-guide/458695/Dynamic-Programming-Patterns B.   https://leetcode.com/tag/dynamic-programming/discuss/1437879/Dynamic-Programming-Patterns C.   https://leetcode.com/tag/dynamic-programming/discuss/662866/DP-for-Beginners-Problems-or-Patterns-or-Sample-Solutions D.   https://leetcode.com/tag/dynamic-programming/discuss/1050391/Must-do-Dynamic-programm...

Write a JSP which insert the details of the 3 or 4 users who register with the web site by using registration form. Authenticate the user when he submits the login form using the user name and password from the database

Write a JSP which does the following job   Insert the details of the 3 or 4 users who register with the web site (week9) by using registration form. Authenticate the user when he submits the login form using the user name and password from      the database (similar to week8 instead of cookies). Description JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms: Expressions of the form  <%= expression %>  that are evaluated and inserted into the output, Scriptlets of the form  <% code %>  that are inserted into the servlet's  service  method, and Declarations of the form  <%! code %>  that are inserted into the body of the servlet class, outside of any existing methods. Each of these is described in more detail below. JSP Expressions A JSP  expression  is used to insert Java values directly...