Friday 6 March 2015

COMPILER DESIGN LAB.

                   COMPILER DESIGN LAB. 
                                                                                                                                                           

Program 1 :-  Write a program to implement a Finite Automation . The program should read an F.A through a file and should check whether a string given from the console is acceptable by the given automation or not ?

Program 2 :-  Write a program to implement  Mealy & Moore machines . The program should read the machine from a file and should generate the corresponding output for a string given from the console .An error must be generated for the case where no valid transition is available.

Program 3 :-  Write a program to implement the conversion of an NFA to a DFA . The program should read an NFA through a file and should generate the corresponding tabular DFA for the same .

Program 4 :-  Write a program to implement a Regular Grammar . The program should read an R.G. through a file and should check whether a string given from the console is acceptable by the given R.G. or not ?

Program 5 :-  Write a program to find out the FIRST & FOLLOW values for a given Context Free Grammar . The program should read the CFG from a file .

Program 6 :-  Write a program to find the Leaders and Basic Blocks for a Three Address Code given through a file .

Program 7 :-  Write a program to find the Flow Graph and the Dominator nodes in a Three Address Code given through a file .

*******************************************************************************
                                                   SOLUTION                                                                                                                                                                      
*******************************************************************************


Program 1 :-  Write a program to implement a Finite Automation . The program should read an F.A through a file and should check whether a string given from the console is acceptable by the given automation or not ?

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
   FILE *fp;
   char str[50],k;
   int states=0,fcount=0,i,j,flag=1,new_line=1;
   int in,final_state[25],col , cur_state ,terminal=0, st_table[25][25];
   clrscr();
   fp = fopen("input.txt", "r");
   for(i=0;i<25;i++)
   {
     for(j=0;j<25;j++)
     {
                     st_table[i][j]=-1;
     }
   }
   if( fp != NULL )
   {
    while(fscanf(fp,"%d",&in) != EOF )
    {
                fscanf(fp,"%c",&k);
                if(new_line == 1)
                                cur_state = in;

                if(new_line == 2)
                {
                    final_state[fcount] = in;
                    fcount++;
                }
                if(new_line >= 3)
                {
                                states=new_line-3;
                                st_table[states][terminal]=in;
                                terminal++;
                }

                if(k=='\n')
                {
                    terminal = 0;
                    new_line++;
                }
    }
      fclose(fp);
   }
   printf("\nCurrent state = Q%d\n",cur_state);
  printf("Final States are :");
   for(i=0; i < fcount; i++)
    {
                  printf(" Q%d\t",final_state[i]);
    }
  for(i=0;  i<= states; i++)
   {
      printf("\n");
      for(j=0; j<= terminal-1; j++)
     {
                  printf("%d\t",st_table[i][j]);
      }
   }
   printf("\nEnter the input string in form of a and b : \n");
   gets(str);
   printf("Enterd String =  %s\n",str);
   col = 0;
   while(str[col]!='\0')
   {
                cur_state = st_table[cur_state][str[col]-97];
                if(cur_state==-1)
                {
                                flag=3;
                                break;
                }
                col++;
   }
    for(i=0; i < fcount; i++)
    {
                  if(final_state[i]==cur_state)
                                flag=0;
    }
    if(flag==0)
                printf("String Accepted.");
    else
                printf("String not Accepted.");
   getch();
   return 0;
}



input.txt
0
0  1  2
0  1
0  2
0  -1



Output :-






Program 2 :-  Write a program to implement  Mealy & Moore machines . The program should read the machine from a file and should generate the corresponding output for a string given from the console .An error must be generated for the case where no valid transition is available.

//Implementation of Moore Machine

#include<stdio.h>
#include<conio.h>
#include<string.h>

int main()
{
   FILE *fp;
   int cur_state,in,fcount=0, i,j,col,st_table[26][26];
   int state_output[26],count=0;
   int match_output[50],new_line=1,states=0,terminal=0,flag=1;
   char k,str[50];
   clrscr();
   for(i=0; i<26; i++)
            for(j=0; j<26; j++)
                        st_table[i][j]=-1;
   fp = fopen("input.txt", "r");

   if( fp != NULL )
   {
    while(fscanf(fp,"%d",&in) != EOF )
    {
            fscanf(fp,"%c",&k);
            if(new_line == 1)
                        cur_state = in;

            if(new_line == 2)
            {
                state_output[fcount] = in;
                fcount++;
            }
            if(new_line >= 3)
            {
                        states=new_line-3;
                        st_table[states][terminal]=in;
                        terminal++;
            }
            if(k=='\n')
            {
                terminal = 0;
                new_line++;
            }
    }
      fclose(fp);
   }
   printf("\nCurrent state = Q%d\n",cur_state);
  printf("States output are :\n");
   for(i=0; i < fcount; i++)
    {
              printf("At Q%d = %d\n",i,state_output[i]);
    }
    printf("\nState Table : ");
  for(i=0; i <= states; i++)
   {
      printf("\n");
      for(j = 0; j < terminal; j++)
     {
              printf("%d\t",st_table[i][j]);
      }
   }
   printf("\nEnter the input string in the form of a and b :\n");
   gets(str);
   printf("Enterd String : %s\n",str);
   col = 0;
   printf("Outputs are :\t");
   match_output[0]=state_output[cur_state];
   while(str[col]!='\0')
   {
            cur_state = st_table[cur_state][str[col]-97];
            if(cur_state==-1)
            {
                        flag=3;
                        break;
            }
            match_output[col+1]=state_output[cur_state];
            if(match_output[col+1]==1)
                        count++;
            col++;
   }
   for(i=0; i<=col+1; i++)
   {    flag=3;
            if(match_output[i]==1)
            {
                        flag=1;
                        break;
            }
   }
    if(str[0]!='\0')
    {
            for(i=0; i <= col; i++)
                        printf("%d",match_output[i]);
                                    printf("\nNumber of pattern found = %d",count);
                        if(flag==1)
                                    printf("\nString is accepted by Moore Machine .");
                        else
                                    printf("\nString is not accepted by Moore Machine .");
    }
   getch();
   return 0;
}


input.txt
0
0  0  0  1
1  0
1  2
3  0
1  2


Output :-







// Implementation of Mealy Machine

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
   FILE *fp;
   int cur_state,new_state,in ,i,j,count=0,state_table[25][50];
   int col,match_output[50];
   int terminal=0,flag=1,new_line=1,states=0;
   char k,str[50],*numberi;
   clrscr();
   for(i=0; i<25; i++)
            for(j=0; j<50; j++)
                        state_table[i][j]=-1;
      fp = fopen("input.txt", "r");
   if( fp != NULL )
   {
    while(fscanf(fp,"%d",&in) != EOF )
    {
            fscanf(fp,"%c",&k);
            if(new_line == 1)
                        cur_state = in;

            if(new_line >= 2)
            {
                        states=new_line-2;
                                    state_table[states][terminal]=in;
                                    terminal++;
            }
            if(k=='\n')
            {
                terminal = 0;
                new_line++;
            }
    }
      fclose(fp);
   }
   printf("\nCurrent state = q%d\n",cur_state);
   printf("\nFirst part corresponds to Input 0 and the Second to Input 1 .");
  for(i=0; i<=states; i++)
   {
      printf("\n");
      for(j=0; j<terminal; j+=2)
     {
              printf("\t");
              printf("At State q%d (q%d , %d)",i,state_table[i][j],state_table[i][j+1]);
      }
   }
   printf("\nEnter the input string in form of 0 and 1 : \n");
   gets(str);
   printf("Enterd String : %s\n",str);
   col = 0;
   while(str[col]!='\0')
   {
            *numberi=str[col];
            new_state = state_table[cur_state][atoi(numberi)*2];
            if(new_state==-1)
            {
                        flag=3;
                        break;
            }
            match_output[col]=state_table[cur_state][atoi(numberi)*2+1];
            if(match_output[col]==1)
                        count++;
            cur_state=new_state;
            col++;
   }
    for(i=0;i<col;i++)
    {
            flag=3;
            if(match_output[i]==1)
            {
                        flag=1;
                        break;
            }
    }
    if(str[0]!='\0')
    {
            printf("Outputs are :\t");
            for(i=0; i< col; i++)
                        printf("%d",match_output[i]);
                        printf("\nNumber of pattern found : %d",count);
                          if(flag==1)
                                    printf("\nString is accepted by Mealy machine .");
                          else
                                    printf("\nString is not accepted by Mealy machine .");
   }
   getch();
   return 0;
}

input.txt

0
1  0  0  0
1  0  2  0
3  1  0  0
1  0  2  0

Output :-






Program 3 :-  Write a program to implement the conversion of an NFA to a DFA . The program should read an NFA through a file and should generate the corresponding tabular DFA for the same .

#include<stdio.h>
#include<string.h>
#include<conio.h>

int input=0;
int cur_state, st_table[26][26][26];
int new_state[26][26],tmp[26],final_state[26];

int new_state_fn();
int search_state();
int sort_array();
int state_array();
int clr_temp();

int main()
{
   FILE *fp;
   char k,str[50];
   int row,col,finc=0,iloop=0, in,ki,srt,stt,fcount=0,oloop=0;
   int terminal=0,nstates=0,flag=1,new_line=1,states=0;
   clrscr();
   for(row=0; row<26; row++)
   {
            final_state[row]=-1;
            for(col=0; col<26; col++)
                        new_state[row][col]=-1;
   }

   for(row=0; row<26; row++)
            for(col=0; col<26; col++)
                        for(ki=0; ki<26; ki++)
                                    st_table[row][col][ki]=-1;

   fp = fopen("nfa2dfa.txt", "r");
   if( fp != NULL )
   {
    while(fscanf(fp,"%d",&in) != EOF )
    {
            fscanf(fp,"%c",&k);
            if(new_line == 1)
                        cur_state = in;

            if(new_line == 2)
                final_state[in] = 1;

            if(new_line == 3)
                input = in;

            if(new_line >= 4)
            {
                        states=new_line-4;
                        new_state[states][0]=states;
                        st_table[states][terminal][nstates]=in;
                        if(k==',')
                                    nstates++;
                        else
                        {
                                    nstates=0;
                                    terminal++;
                        }
            }
            if(k=='\n')
            {
                terminal = nstates = 0;
                new_line++;
            }
    }
      fclose(fp);
   }
   sort_array();
   state_array();

   printf("\nInitial state = Q%d",cur_state);
   printf("\nFinal States are :\n");
   for(finc=0; finc<26; finc++)
   {
            if(final_state[finc]!=-1)
            {
                        for(row=0; row<26; row++)
                        {
                                    if(new_state[finc][row]!=-1)
                                                printf("%d",new_state[finc][row]);
                        }
                        printf("\t");
            }

   }
   printf("\nTypes of inputs can be = %d",input);
   printf("\nTotal number of states are :");
   for(finc=0; finc<26; finc++)
   {
            col=0;
            for(row=0; row<26; row++)
                        if(new_state[finc][row]!=-1)
                        {
                                    col=1;
                                    printf("%d",new_state[finc][row]);
                        }
            if(col==1)
                        printf("\t");
   }
   printf("\nNew State Table is :\n");
   for(finc=0; finc<26; finc++)
   {
            if(new_state[finc][0] != -1)
            {
                        for(row=0; row<input; row++)
                        {
                                    for(col=0; col<26; col++)
                                    {
                                                printf("%d",st_table[finc][row][col]);
                                                if(st_table[finc][row][col+1]==-1)
                                                break;
                                    }
                                    printf("\t");
                        }
                        printf("\n");
            }
   }

   printf("\nEnter the input string in form of a,b and c\n");
   gets(str);
   printf("Enterd String : %s\n",str);
   col = 0;
   while(str[col]!='\0')
   {
            for(row=0; row<26; row++)
            {
                        tmp[row] = st_table[cur_state][str[col]-97][row];
                        if(tmp[row]==-1 && row==0)
                        {
                                    cur_state=-1;
                                    break;
                        }
            }
            if(cur_state==-1)
            {
                        flag=3;
                        break;
            }
            else
              cur_state=search_state();
            clr_temp();
            col++;
   }
    if(flag!=3 && flag==1 && final_state[cur_state]==1)
            flag=0;

    if(flag==0)
            printf("String Accepted");
    else
            printf("String not accepted");
   getch();
   return 0;
}
int sort_array()
{
            int si,sj,sk,temp;
            for(sk=0; sk<26; sk++)
            {
                        if(new_state[sk][0]==-1)
                                    return 0;
                        for(si=0; si<26; si++)
                                    for(sj=0; sj < 26-si-1; sj++)
                                    {
                                                if(new_state[sk][sj] <= new_state[sk][sj+1])
                                                {
                                                            temp = new_state[sk][sj];
                                                            new_state[sk][sj] = new_state[sk][sj+1];
                                                            new_state[sk][sj+1] = temp;
                                                }
                                    }
            }
            return 0;
}
int state_array()
{
      int row=0,mid=0,col=0,kl;
      int inp=input;

      for(row=0; row<26; row++)
                        for(mid=0; mid < inp; mid++)
                        {
                                    clr_temp();
                                    for(col=0; col<26; col++)
                                    {
                                                if(st_table[row][mid][col]==-1)
                                                            break;
                                                tmp[col]=st_table[row][mid][col];
                                    }
                                    if(col>=2)
                                    {
                                                new_state_fn();
                                                sort_array();
                                    }
                        }
            return 0;
}
int new_state_fn()
{
            int row=0,mid=0,col=0,find_state=-1,inl=0,iinl=0;
            int inp=input;
            find_state = search_state();
            if(find_state == -1)
            {
                        for(row=0; row<26; row++)
                        {
                                    if(new_state[row][0]==-1)
                                    {
                                                for(mid=0; mid<26; mid++)
                                                {
                                                            new_state[row][mid]=tmp[mid];
                                                            col=0;
                                                            while(col<inp && tmp[mid]!=-1)
                                                            {
                                                                        for(inl=0; inl<=mid; inl++)
                                                                        {
                                                                                    for(iinl=0; iinl<26; iinl++)
                                                                                                if(st_table[row][col][iinl]==-1)
                                                                                                {
                                                                                                            st_table[row][col][iinl]=st_table[tmp[mid]][col][inl];
                                                                                                            //printf("%d=%d=%d=%d\t",row,final_state[st_table[tmp[mid]][col][inl]],tmp[mid],st_table[tmp[mid]][col][inl]);
                                                                                                            if(final_state[tmp[mid]]==1)
                                                                                                                        final_state[row]=1;

                                                                                                            break;
                                                                                                }
                                                                        }
                                                                        col++;
                                                            }
                                                }
                                                return 1;
                                    }
                        }

            }
            return 0;
}
int search_state()
{
            int row=0,col=0,kl=0;
            for(row=0; row<7; row++)
            {
                        for(col=0; col< 26; col++)
                        {
                                    if(tmp[col]==new_state[row][col])
                                                kl=1;
                                    else
                                    {
                                                kl=0;
                                                break;
                                    }
                        }
                        if(kl==1)
                                    return row;
            }
            return -1;

}
int clr_temp()
{
            int clr;
            for(clr=0; clr<26; clr++)
                        tmp[clr]=-1;
            return 0;
}

Nfa2dfa.txt:-
0
2
2
1  -1
-1  2,0
-1  -1

Output:-




Program 4 :-  Write a program to implement a Regular Grammar . The program should read an R.G. through a file and should check whether a string given from the console is acceptable by the given R.G. or not ?

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
   FILE *fp;
   char k,k1,str[50],stk[26];
   char grammar_table[26][26],cur_symbol;
   int i,j,symbol=0,col=0,strnull=0,flag=1,stkp=25,new_line=1;
   clrscr();
   for(i=0; i<26; i++)
   {
      stk[i]=' ';
      for(j=0; j<26; j++)
            grammar_table[i][j]=' ';
   }
   for(i=0; i<50; i++)
            str[i]=' ';

   printf("\n****Implementation of a Regular Grammar****");
   fp = fopen("rg.txt", "r");
   if( fp != NULL )
   {
    while(k != '#')
    {
            fscanf(fp,"%c",&k);
            fscanf(fp,"%c",&k1);
            if(new_line == 1)
                        cur_symbol = k;

            if(new_line >= 2)
            {
                        symbol=new_line-2;
                        if(k != '#')
                        {
                                    grammar_table[symbol][col]=k;
                                    col++;
                        }
            }
            if(k1=='\n')
            {
                col=0;
                new_line++;
            }
    }
      fclose(fp);
   }
   printf("\nStarting state = %c\n",cur_symbol);
   printf("Productions  are :");
   for(i=0; i <= symbol; i++)
   {
      printf("\n");
      for(j = 0; j < 26; j++)
      {

              if(grammar_table[i][j]=='#')
                        grammar_table[i][j]=' ';
              printf("%c",grammar_table[i][j]);
              if(j==0)
                        printf("->");
              if(grammar_table[i][j]==' ')
                        break;
      }
   }
   printf("\nEnter the input string in form of a and b : ");
   gets(str);
   printf("Enterd String : %s\n",str);
   col = 0;
   while(str[col]!='\0')
   {
            for(i=0;i<=symbol;i++)
            {
                if(grammar_table[i][0]==cur_symbol && grammar_table[i][1]==str[col])
                {
                        stkp=25;
                        flag=3;
                        printf("%c->",cur_symbol);
                        for(j=25; j>=1; j--)
                        {
                                    if(grammar_table[i][j]!=' ' && grammar_table[i][j]!='#')
                                    {
                                                stk[stkp]=grammar_table[i][j];
                                                printf("%c",grammar_table[i][j]);
                                                stkp--;
                                    }
                        }
                        printf("\n");
                        break;  
                }
            }
            if(flag==1)
                        break;
            if(stk[stkp+1]==str[col])
                        stk[stkp+1]=' '; 
            str[col]=' ';            
            if(stkp+2==26)
            {
                        cur_symbol=' ';
                        break;
            }
            cur_symbol=stk[stkp+2];
            if(cur_symbol==' ')
            {
                        flag=1;
                        break;
            }
            stk[stkp+2]=' ';   //pop the stack again
            col++;
   }

   for(j=0; j<50; j++)
            if(str[j]!='\0' && str[j]!=' ')
                        strnull=1;

    if(stk[25]==' ' && strnull==0 && flag==3 && cur_symbol==' ')
            printf("String is accepted by Regular Grammar.");
    else
            printf("String is not accepted by Regular Grammar.");
   getch();
   return 0;
}

rg.txt
E
E-b B
E-a E
B-a A
B-b
A-a E
A-a #

Output :-






Program 5 :-  Write a program to find out the FIRST & FOLLOW values for a given Context Free Grammar . The program should read the CFG from a file .

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>

char prodn[50][50],fw[10],first[10];
int n=0,m=0,i=0,j=0,symbol;
void FOLLOW(char c);
void frst_folw(char c);
void FIRST(char c);
int main()
{
            FILE *fp;
            int i,choice,z,col,p,q;
            int new_line=1;
            char c,ch,k,cur_symbol;
            clrscr();
            for(i=0; i<26; i++)
            {
                        for(j=0; j<26; j++)
                                    prodn[i][j]=' ';
            }

            fp = fopen("ff_CFG.txt", "r");
   if( fp != NULL )
   {
    while(k != '#')
    {
            fscanf(fp,"%c",&k);
            if(new_line == 1)
                        if(k!='\n')
                        cur_symbol = k;


            if(new_line >= 2)
            {
                        symbol=new_line-2;
                        if(k != '#')
                        {
                                    prodn[symbol][col]=k;
                                    col++;
                        }
            }
            if(k=='\n')
            {
                col=0;
                new_line++;
            }
    }
      fclose(fp);
   }
  
   printf("\nStarting Symbol is : %c\n",cur_symbol);
   printf("\nProductions  are :");
   for(p=0; p <= symbol; p++)
   {
      printf("\n");
      for(q = 0; q < 26; q++)
      {

              if(prodn[p][q]=='#')
                        prodn[p][q]=' ';
              printf("%c",prodn[p][q]);
              if(prodn[p][q]==' ')
                        break;
      }
   }
             do
             {
                                       n=0;
                                     printf("\nEnter the element whose FIRST & FOLLOW is to be found:");
                                       scanf("%c",&c);
                                       FIRST(c);
                                       printf("\n FIRST(%c)= { ",c);
                                       for(i=0;i<n;i++)  
                                                printf("%c ",first[i]);
                                                printf("}\n");

                                       if(!(islower(c)) || (c!='+'|| c!='*' || c!='-' || c!='/' || c!='(' || c!=')') )
                                        {
                                        m=0;
                                       FOLLOW(c);
                                       printf("FOLLOW(%c) = { ",c);
                                       for(i=0;i<m;i++)
                                                printf("%c ",fw[i]);
                                                printf(" }\n");
                                       }
                        printf("Do you want to continue(0/1)?");
                        scanf("%d%c",&z,&ch);

       }
       while(z==1);
            return 0;
}
void FOLLOW(char c)
{

            if(prodn[0][0]==c)
                        fw[m++]='$';
            for(i=0;i<=symbol;i++) 
            {
                        for(j=2;j<strlen(prodn[i]);j++)
                        {
                                    if(prodn[i][j]==c)
                                    {
                                                if(prodn[i][j+1]!='\0')  
                                                            frst_folw(prodn[i][j+1]);
                                                if(prodn[i][j+1]=='\0'&&c!=prodn[i][0])
                                                            FOLLOW(prodn[i][0]);

                                    }
                        }
            }
}
void frst_folw(char c)
{
      int k1;
                         if(!(isupper(c)))
                                    fw[m++]=c;
                         for(k1=0;k1<=symbol;k1++) 
                         {
                                    if(prodn[k1][0]==c)
                                    {
                                                 if(prodn[k1][2]=='$')
                                                            FOLLOW(prodn[i][0]);
                                                 else if(islower(prodn[k1][2]))
                                                            fw[m++]=prodn[k1][2];
                                                 else frst_folw(prodn[k1][2]);
                                    }
                         }

}
void FIRST(char c)
{
            int k;
            if(!(isupper(c)))
                        first[n++]=c; 
            for(k=0;k<=symbol;k++)      
            {
                        if(prodn[k][0]==c)
                        {
                                    if(prodn[k][2]=='$')
                                                first[n++]='$'; 
                                    else if(islower(prodn[k][2]))
                                                first[n++]=prodn[k][2];
                                    else FIRST(prodn[k][2]);
                        }
            }
}

ff_CFG.txt
E
E=TP
P=+TP
P=$
T=FQ
Q=*FQ
Q=$
F=(E)
F=b#

Output :-








Program 6 :-  Write a program to find the Leaders and Basic Blocks for a Three Address Code given through a file .


#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>

int new_line=1,n=30;
int leader[10];
int search_leader(int);
void sort_leader();
int main()
{
   FILE *fp;
   char buffer[255],k,*num;
   int i,in,indx=0,col,first=0,last=0;
   clrscr();
   for(i=0; i<n; i++)
            leader[i]=999;

   printf("\n****Program to find the Leaders and Basic Blocks for a TAC****\n");
   printf("\nTAC stored in a file : \n");
   fp = fopen("block.txt", "r");
   while(fscanf(fp,"%s",buffer)!=EOF)
   {
            printf("%s\n",buffer);
   }
   fclose(fp);

   fp = fopen("tac.txt", "r");
   if( fp != NULL )
   {
    while(k != '#')
    {
            fscanf(fp,"%c",&k);
            if(new_line==1)
            {       if(search_leader(1)==0)
                        {
                        leader[indx]=new_line;
                        indx++;
                        }
            }
            if(k=='g')
            {
                        fscanf(fp,"%c",&k);
                        if(k=='o')
                        {
                                    fscanf(fp,"%c",&k);
                                    if(k=='t')
                                    {
                                                fscanf(fp,"%c",&k);
                                                if(k=='o')
                                                {
                                                   fscanf(fp,"%c",&k);
                                                   fscanf(fp,"%d",&in);
                                                   if(search_leader(in)==0)
                                                   {
                                                            leader[indx]=in;
                                                            indx++;
                                                            if(search_leader(new_line+1)==0)
                                                            {
                                                                        fscanf(fp,"%c",&k);
                                                                        if(k!='#')
                                                                        {
                                                                                    leader[indx]=new_line+1;
                                                                                    indx++;
                                                                        }
                                                            }
                                                   }
                                                }

                                    }
                        }
            }
            if(k=='\n')
                new_line++;
    }
      fclose(fp);
   }
   sort_leader();
   printf("\nLeader lines are -->\n");
   for(i=0; i<=new_line; i++)
            if(leader[i]!=999)
                        printf("Line : %d\n",leader[i]);
   for(indx=0; indx<=new_line; indx++)
   {
            if(leader[indx]==999)
                        break;
            first=leader[indx];
            if(leader[indx+1]!=999)
                        last=leader[indx+1]-1;
            else
                        last=new_line;
            printf("Block B%d is from line number %d to %d\n",indx+1,first,last);
   }
   getch();
   return 0;
}
int search_leader(int a)
{
            int i;
            for(i=0; i<new_line; i++)
                        if(leader[i]==a)
                                    return 1;
            return 0;
}
void sort_leader()
{
            int i,j,temp;
            for(i=0; i<new_line; i++)
            {
                        for(j=0; j<new_line-i-1; j++)
                        {
                                    if(leader[j]>=leader[j+1])
                                    {
                                                temp=leader[j];
                                                leader[j]=leader[j+1];
                                                leader[j+1]=temp;
                                    }
                        }
            }
}

block.txt

 prod=0
i=1
t1=4*i
t2=a[t1]
t3=4*i
t4=b[t3]
t5=t2*t4
t6=prod+t5
prod=t6
t7=i+1
i=t7
if  i<=10 goto 3

Output :-




Program 7 :-  Write a program to find the Flow Graph and the Dominator nodes in a Three Address Code given through a file .

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
int leader[10],leader_line[10][2],pred[10][10];
int leader_search(int);
void leader_sort();
int search_block(int),blockflow[10][10];
//int calculate_predecessor(int node);
int search_block(int);
int main()
{
   FILE *fp;
   char k,*num;
   int z,j,varline=0,in,row,col,new_line=1,ifarray[10],gotoarray[10][2],index=0,first=0,last=0;
   clrscr();
   for(row=0; row<10; row++)
   {
            ifarray[row+1]=gotoarray[row+1][0]=gotoarray[row+1][1]=leader[row]=leader_line[row][0]=leader_line[row][1]=777;
            for(col=0; col<10; col++)
                        blockflow[row][col]=pred[row][col]=0;
   }

   fp = fopen("tac.txt", "r");
   if( fp != NULL )
   {
    while(k != '#')
    {
            fscanf(fp,"%c",&k);
            if(new_line==1)
            {       if(leader_search(1)==0)
                        {
                        leader[index]=new_line;
                        index++;
                        }
            }
            if(k=='i')
            {
                        fscanf(fp,"%c",&k);
                        if(k=='f')
                        {
                                    ifarray[new_line]=1;
                        }
            }
            if(k=='g')
            {
                        fscanf(fp,"%c",&k);
                        if(k=='o')
                        {
                                    fscanf(fp,"%c",&k);
                                    if(k=='t')
                                    {
                                                fscanf(fp,"%c",&k);
                                                if(k=='o')
                                                {
                                                   gotoarray[new_line][0]=1;
                                                   fscanf(fp,"%c",&k);
                                                   fscanf(fp,"%d",&in);
                                                   if(leader_search(in)==0)
                                                   {
                                                            leader[index]=in;
                                                            gotoarray[new_line][1]=in;
                                                            index++;
                                                            if(leader_search(new_line+1)==0)
                                                            {
                                                                        leader[index]=new_line+1;
                                                                        index++;
                                                            }
                                                   }
                                                }

                                    }
                        }
            }
            if(k=='\n')
                new_line++;
    }
      fclose(fp);
   }
   leader_sort();
   printf("\nLeader lines are\n");
   for(row=0; row<10; row++)
            if(leader[row]!=777)
                        printf("Line : %d\n",leader[row]);
   for(index=0; index<10; index++)
   {
            if(leader[index]==777)
                        break;
            first=leader[index];
            if(leader[index+1]!=777)
                        last=leader[index+1]-1;
            else
                        last=new_line;
            leader_line[index][0]=first;
            leader_line[index][1]=last;
            printf("Block %d is from line number %d to %d\n",index+1,first,last);
   }
   for(row=0; row<10; row++)
   {
            for(col=0; col<10; col++)
                        if(blockflow[row][col]==777)
                                    blockflow[row][col]=0;
   }
   for(index=0; index<10; index++)
   {
            varline=0;
            last=-1;
            if(leader[index]==777)
                        break;
            if(ifarray[leader_line[index][0]]==1)
                        last=varline=gotoarray[leader_line[index][0]][1];
            else if(gotoarray[leader_line[index][0]][0]==1)
                        varline=gotoarray[leader_line[index][0]][1];
            else if(ifarray[leader_line[index][1]]==1)
                        last=varline=gotoarray[leader_line[index][1]][1];
            else if(gotoarray[leader_line[index][1]][0]==1)
                        varline=gotoarray[leader_line[index][1]][1];
            else
                        blockflow[index][index+1]=1;

            if(varline!=0)
            {
                   first = search_block(varline);
                   if(first!=-1)
                        blockflow[index][first]=1;
            }
            if(last!=-1)
                        blockflow[index][index+1]=1;

   }
   printf("\nAdjacency Matrix for Control Flow will be : \n\n");
   printf(" \tB1\tB2\tB3\tB4\tB5\tB6\n\n");
   for(row=0; row<10; row++)
   {
            if(leader[row]==777)
                        break;
            printf(" B%d\t",row+1);
            for(col=0; col<10; col++)
            {
                        if(leader[col]==777)
                                    break;
                        first=blockflow[row][col];
                        printf("%d\t",first);
            }
            printf("\n\n");
   }


   printf("\nDominators are:\n");
   printf("B1 dominates \tB1 B2\n");
   for(row=1; row<10; row++)
   {
            if(leader[row]==777)
                        break;
                        countone=-1;
                        nextblock=-1;
                        printf("B%d dominates for\t",row+1);
                        for(col=0;col<10; col++)
                        {
                                    if(leader[col]==777)
                                                break;
                                    first=blockflow[row][col];
                                    if(first==1)
                                    {
                                                for(index=1; index<10; index++)
                                                {
                                                            if(row!=index)
                                                            {
                                                                        if(leader[index]==777)
                                                                                    break;
                                                                        nextblock=blockflow[index][col];
                                                                        if(nextblock==1 && col>index && col!=row)
                                                                                    countone=index;
                                                            }
                                                }
                                                if(countone==-1)
                                                printf("B%d ",col+1);
                                    }
                        }
            printf("\n");
   }
  
   getch();
   return 0;
}
int leader_search(int a)
{
            int i;
            for(i=0; i<10; i++)
                        if(leader[i]==a)
                                    return 1;
            return 0;
}
void leader_sort()
{
            int i,j,tmp;
            for(i=0; i<10; i++)
            {
                        for(j=0; j<10-i-1; j++)
                        {
                                    if(leader[j]>=leader[j+1])
                                    {
                                                tmp=leader[j];
                                                leader[j]=leader[j+1];
                                                leader[j+1]=tmp;
                                    }
                        }
            }
}
int search_block(int ln)
{
            int i=0;
            for(i=0; i<10; i++)
                        if(leader_line[i][0]==ln)
                                    return i;
            return -1;
}


tac.txt

prod=0
i=1
t1=4*i
t2=a[t1]
t3=4*i
t4=b[t3]
t5=t2*t4
t6=prod+t5
prod=t6
t7=i+1
i=t7
if  i<=10 goto 3

Output:-




Click here to download Lubna_compiler_lab.pdf

No comments:

Post a Comment


We’re eager to see your comment. However, Please Keep in mind that all comments are moderated manually by our human reviewers according to our comment policy. Let’s enjoy a personal and evocative conversation.