Wednesday, 13 September 2017

Intermediate code generation for sample language using LEX and YACC.

CL1_A5.l :

%{
   #include "y.tab.h"
   extern char yyval;
%}

%%
[0-9]+         {yylval.symbol=(char)yytext[0]; return NUMBER;}
[a-zA-Z]+   {yylval.symbol=(char)yytext[0];return LETTER;}

\n {return 0;}
.  {return yytext[0];}

%%


CL1_A5.y :

%{
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    void ThreeAddressCode();
    void Triple();
    void Qudraple();
    char AddtoTable(char ,char, char);

    int index1=0;
    char temp='A';
    struct expr
    {
        char operand1;
        char operand2;
        char operator;
      };
%}

%union
{
    char symbol;
}

%token <symbol> LETTER NUMBER
%type <symbol> exp
%left '-''+'
%right '*''/'

%%

statement: LETTER '=' exp ';' {AddtoTable((char)$1,(char)$3,'=');}
           | exp ';'
       ;

exp: exp '+' exp {$$ = AddtoTable((char)$1,(char)$3,'+');}
      | exp '-' exp {$$ = AddtoTable((char)$1,(char)$3,'-');}
      | exp '*' exp {$$ = AddtoTable((char)$1,(char)$3,'*');}
      | exp '/' exp {$$ = AddtoTable((char)$1,(char)$3,'/');}
      | '(' exp ')' {$$ = (char)$2;}
      | NUMBER {$$ = (char)$1;}
      | LETTER {$$ = (char)$1;}
      ;

%%

yyerror(char *s)
{
  printf("%s",s);
  exit(0);
}

struct expr arr[20];
int id=0;

char AddtoTable(char operand1,char operand2,char operator)
{
    arr[index1].operand1=operand1;
    arr[index1].operand2=operand2;
    arr[index1].operator=operator;
    index1++;
    temp++;
    return temp;
}

void ThreeAddressCode()
{
    int cnt=0;
    temp++;
    printf("\n\n\t THREE ADDRESS CODE\n\n");
    while(cnt<index1)
    {
        printf("%c : = \t",temp);  
        if(isalpha(arr[cnt].operand1))
            printf("%c\t",arr[cnt].operand1);
        else
            {printf("%c\t",temp);}
        printf("%c\t",arr[cnt].operator);
        if(isalpha(arr[cnt].operand2))
            printf("%c\t",arr[cnt].operand2);
        else
            {printf("%c\t",temp);}
        printf("\n");
        cnt++;
        temp++;
    }
}

void Quadraple()
{
    int cnt=0;
    temp++;
    printf("\n\n\t QUADRAPLE CODE\n\n");
    while(cnt<index1)
    {
        printf("%d",id);
        printf("\t");      
        printf("%c",arr[cnt].operator);
        printf("\t");   
            if(isalpha(arr[cnt].operand1))
                printf("%c\t",arr[cnt].operand1);
            else
                {printf("%c\t",temp);}  
            if(isalpha(arr[cnt].operand2))
                printf("%c\t",arr[cnt].operand2);
            else
                {printf("%c\t",temp);}      
            printf("%c",temp);
        printf("\n");
        cnt++;
        temp++;
        id++;  
    }
}

void Triple()
{
    int cnt=0,cnt1,id1=0;
    temp++;
    printf("\n\n\t TRIPLE CODE\n\n");
    while(cnt<index1)
    {
        if(id1==0)
        {
            printf("%d",id1);
            printf("\t");      
            printf("%c",arr[cnt].operator);
            printf("\t");   
            if(isalpha(arr[cnt].operand1))
                 printf("%c\t",arr[cnt].operand1);
            else
                {printf("%c\t",temp);}
            cnt1=cnt-1;
            if(isalpha(arr[cnt].operand2))
                printf("%c",arr[cnt].operand2);
            else
                {printf("%c\t",temp);}
        }
        else
        {
            printf("%d",id1);
            printf("\t");      
            printf("%c",arr[cnt].operator);
            printf("\t");   
            if(isalpha(arr[cnt].operand1))
                printf("%c\t",arr[cnt].operand1);
            else
                 {printf("%c\t",temp);}
            cnt1=cnt-1;
            if(isalpha(arr[cnt].operand2))
                printf("%d",id1-1);
            else
                {printf("%c\t",temp);}
        }
        printf("\n");
        cnt++;
        temp++;
        id1++; 
    }
}

main()
{
    printf("\nEnter the Expression: ");
    yyparse();
    temp='A';
    ThreeAddressCode();
    Quadraple();
    Triple();
}

yywrap()
{
    return 1;
}


OUTPUT :



Sunday, 10 September 2017

Implementation of any 2 uninformed search methods with some application.

CODE :

#include<iostream>
#include <list>

using namespace std;

class Graph
{
    int vertices; 
    list<int> *ptr;
    void DFSUtil(int vertex, bool visited[]);
public:
    Graph(int vertices); 
    void addEdge(int vertex1, int vertex2);
    void BFS(int start_vertex);
    void DFS(int start_vertex); 
};

Graph::Graph(int vertices)
{
    this->vertices = vertices;
    ptr = new list<int>[vertices];
}

void Graph::addEdge(int vertex1, int vertex2)
{
    ptr[vertex1].push_back(vertex2);
}

void Graph::BFS(int start_vertex)
{
    bool *visited = new bool[vertices];
    for(int i = 0; i < vertices; i++)
        visited[i] = false;

    list<int> queue;
   
    visited[start_vertex] = true;
    queue.push_back(start_vertex);

    list<int>::iterator itr;

    while(!queue.empty())
    {
        start_vertex = queue.front();
        cout << start_vertex << " ";
        queue.pop_front();

        for(itr = ptr[start_vertex].begin(); itr != ptr[start_vertex].end(); ++itr)
        {
            if(!visited[*itr])
            {
                visited[*itr] = true;
                queue.push_back(*itr);
            }
        }
    }
}

void Graph::DFSUtil(int start_vertex, bool visited[])
{
    visited[start_vertex] = true;
    cout << start_vertex << " ";

    list<int>::iterator itr;
    for (itr = ptr[start_vertex].begin(); itr != ptr[start_vertex].end(); ++itr)
        if (!visited[*itr])
            DFSUtil(*itr, visited);
}

void Graph::DFS(int start_vertex)
{
    bool *visited = new bool[vertices];
    for (int i = 0; i < vertices; i++)
        visited[i] = false;
    DFSUtil(start_vertex, visited);
}
 
 
int main()
{
    int no_vertices,start_vertex;
    cout<<"Enter the number of vertices"<<endl;
    cin>>no_vertices;
    int matrix[no_vertices][no_vertices];
    Graph gr(no_vertices);
    cout<<"\n\nEnter Adjacency Matrix of size ["<<no_vertices<<"x"<<no_vertices<<"]: \n"; 
    for(int i=0;i<no_vertices;i++) 
    { 
        for(int j=0; j<no_vertices; j++) 
        { 
            cin>>matrix[i][j]; 
        } 
    }
   
    for(int i=0; i<no_vertices; i++) 
    {            
        for(int j=0; j<no_vertices; j++) 
        { 
            if(matrix[i][j] == 1) 
            { 
                gr.addEdge(i,j); 
            } 
        } 
    }
     
    cout<<"Enter start vertex"<<endl;
    cin>>start_vertex;
    cout<<"BFS"<<endl;
    gr.BFS(start_vertex);
    cout<<"\nDFS"<<endl;
    gr.DFS(start_vertex);
    return 0;
}


OUTPUT :