Friday, May 3, 2019

SS 6.B - YACC - RECOGNIZE VALID IDENTIFIER, OPERATORS AND KEYWORDS

6 - b) Write YACC program to recognize valid identifier, operators and keywords in the given text (C program) file.

lab6.l PROGRAM

%{
#include <stdio.h>
#include "y.tab.h"
extern yylval;
%}

%%
[ \t];
[+|-|*|/|=|<|>] {printf("operator is %s\n",yytext);return OP;}
[0-9]+ {yylval = atoi(yytext); printf("numbers is %d\n",yylval); return DIGIT;}
int|char|bool|float|void|for|do|while|if|else|return|void {printf("keyword is %s\n",yytext);return KEY;}
[a-zA-Z0-9]+ {printf("identifier is %s\n",yytext);return ID;}
. ;
%%

lab6.y PROGRAM

%{
#include <stdio.h>
#include <stdlib.h>
int id=0, dig=0, key=0, op=0;
%}
%token DIGIT ID KEY OP

%%
input:
DIGIT input { dig++; }
| ID input { id++; }
| KEY input { key++; }
| OP input {op++;}
| DIGIT { dig++; }
| ID { id++; }
| KEY { key++; }
| OP { op++;}
;
%%

#include <stdio.h>
extern int yylex();
extern int yyparse();
extern FILE *yyin;
main()
{
FILE *myfile = fopen("inputfile.c", "r");
if (!myfile)
{
printf("I can't open inputfile.c!");
return -1;
}
yyin = myfile;
do{
yyparse();
}while (!feof(yyin));
printf("numbers = %d\nKeywords = %d\nIdentifiers = %d\noperators = %d\n",dig, key,id, op);
}

void yyerror()
{
printf("EEK, parse error! Message: ");
exit(-1);
}

inputfile.txt FILE

#include<stdio.h>

int main()
{
int a ;
int b ;
a = 1 ;
b = 2 ;
a = a+b;
return 0 ;
}


OUTPUT :( click on image to zoom )


SS 7 - C - ROUND ROBIN (RR) SCHEDULING ALGORITHM

7 - Design, develop and implement a C/C++/Java program to simulate the working of Shortest remaining time and Round Robin (RR) scheduling algorithms. Experiment with different quantum sizes for RR algorithm.

lab7.c PROGRAM

// CPU Scheduling -Round Robin
#include<stdio.h>
struct process
{
char name;
int at,bt,wt,tt,rt;
int completed;
}p[10];

int n;
int q[10];  //queue
int front=-1,rear=-1;
void enqueue(int i)
{
    if(rear==10)
        printf("overflow");
    rear++;
    q[rear]=i;
    if(front==-1)
        front=0;
}

int dequeue()
{
    if(front==-1)
        printf("underflow");
    int temp=q[front];
    if(front==rear)
        front=rear=-1;
    else
        front++;
    return temp;
}

int isInQueue(int i)
{
    int k;
    for(k=front;k<=rear;k++)
    {
        if(q[k]==i)
        return 1;
    }
    return 0;
}

void sortByArrival()
{
    struct process temp;
    int i,j;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(p[i].at>p[j].at)
            {
                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
}

int main()
{
    int i,j,time=0,sum_bt=0,tq;
    char c;
    float avgwt=0;
     printf("Enter Number of Processes:\n");
     scanf("%d",&n);
     for(i=0,c='A';i<n;i++,c++)
     {
         p[i].name=c;
         printf("\nProcess %c\n",c);
         printf("\tArrival Time :");
         scanf("%d",&p[i].at);
         printf("\tBurst Time :");
         scanf("%d",&p[i].bt);
         p[i].rt=p[i].bt;
         p[i].completed=0;
         sum_bt+=p[i].bt;
    }
    printf("\nEnter the time quantum:");
    scanf("%d",&tq);
    sortByArrival();
    enqueue(0);
    printf("\nProcess execution order: ");
    for(time=p[0].at;time<sum_bt;)
    {
        i=dequeue();
        if(p[i].rt<=tq)
        {
            time+=p[i].rt;
            p[i].rt=0;
            p[i].completed=1;
            printf(" %c ",p[i].name);
            p[i].wt=time-p[i].at-p[i].bt;
            p[i].tt=time-p[i].at;
            for(j=0;j<n;j++)
            {
                if(p[j].at<=time && p[j].completed!=1&& isInQueue(j)!=1)
                {
                    enqueue(j);
                }
            }
        }
        else
        {
            time+=tq;
            p[i].rt-=tq;
            printf(" %c ",p[i].name);
            for(j=0;j<n;j++)
            {
                if(p[j].at<=time && p[j].completed!=1&&i!=j&& isInQueue(j)!=1)
                {
                    enqueue(j);
                }
            }
            enqueue(i);
        }
    }
    printf("\n\nName\tArrival Time\tBurst Time\tResponse Time\tTurnAround Time");
    for(i=0;i<n;i++)
    {
        avgwt+=p[i].wt;
        printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d",p[i].name,p[i].at,p[i].bt,p[i].wt,p[i].tt);
    }
    printf("\n\nAverage waiting time:%f",avgwt/n);
}


OUTPUT :( click on image to zoom )

SS 8 - C - BANKER’S ALGORITHM

8 - Design, develop and implement a C/C++/Java program to implement Banker’s algorithm. Assume suitable input required to demonstrate the results.

lab8.c PROGRAM

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

int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;

printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}

printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}

printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];

do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;

for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
} while(count != p && process != -1);

if(count == p)
{
printf("\nThe system is in a safe state!!\n"); printf("Safe Sequence : < "); for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}


OUTPUT :( click on image to zoom )



SS 9 - C - PAGE REPLACEMENT ALGORITHMS LRU AND FIFO

9 - Design, develop and implement a C/C++/Java program to implement page replacement algorithms LRU and FIFO. Assume suitable input required to demonstrate the results.

lab9.c PROGRAM

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

void FIFO(char [ ],char [ ],int,int);
void lru(char [ ],char [ ],int,int);
void opt(char [ ],char [ ],int,int);

int main()
{
    int ch,YN=1,i,l,f;
    char F[10],s[25];
    printf("\nEnter the no of empty frames: ");
    scanf("%d",&f);
    printf("\nEnter the length of the string: ");
    scanf("%d",&l);
    printf("\nEnter the string: ");
    scanf("%s",s);
    for(i=0;i<f;i++)
        F[i]=-1;

    do
    {
        printf("\n*********** MENU ***********");
        printf("\n1:FIFO\n2:LRU \n3:EXIT");
        printf("\nEnter your choice: ");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1: for(i=0;i<f;i++)
                        F[i]=-1;
                    FIFO(s,F,l,f);
                    break;

            case 2: for(i=0;i<f;i++)
                        F[i]=-1;
                    lru(s,F,l,f);
                    break;

            case 3: exit(0);
        }
        printf("\n\nDo u want to continue IF YES PRESS 1\nIF NO PRESS 0 : ");
        scanf("%d",&YN);
    } while(YN==1);
    return(0);
}


void FIFO(char s[],char F[],int l,int f)
{
    int i,j=0,k,flag=0,cnt=0;
    printf("\n\tPAGE\t FRAMES\t\t\t FAULTS");
    for(i=0;i<l;i++)
    {
        for(k=0;k<f;k++)
        {
            if(F[k]==s[i])
                flag=1;
        }

        if(flag==0)
        {
            printf("\n\t%c\t",s[i]);
            F[j]=s[i];
            j++;
            for(k=0;k<f;k++)
                printf(" %c",F[k]);
            printf("\tPage-fault%d",cnt);
            cnt++;
        }

        else
        {
            flag=0;
            printf("\n\t%c\t",s[i]);
            for(k=0;k<f;k++)
                printf(" %c",F[k]);
            printf("\tNo page-fault");
        }
        if(j==f)
            j=0;
    }
}


void lru(char s[],char F[],int l,int f)
{
    int i,j=0,k,m,flag=0,cnt=0,top=0;
    printf("\n\tPAGE\t FRAMES\t\t\t FAULTS");
    for(i=0;i<l;i++)
    {
        for(k=0;k<f;k++)
        {
            if(F[k]==s[i])
            {
                flag=1;
                break;
            }
        }
        printf("\n\t%c\t",s[i]);
        if(j!=f && flag!=1)
        {
            F[top]=s[i];
            j++;
            if(j!=f)
                top++;
        }

        else
        {
            if(flag!=1)
            {
                for(k=0;k<top;k++)
                    F[k]=F[k+1];
                F[top]=s[i];
            }

            if(flag==1)
            {
                for(m=k;m<top;m++)
                    F[m]=F[m+1];
                F[top]=s[i];
            }
        }

        for(k=0;k<f;k++)
            printf(" %c",F[k]);

        if(flag==0)
        {
            printf("\tPage-fault%d",cnt);
            cnt++;
        }
        else
            printf("\tNo page fault");
        flag=0;
    }
}


OUTPUT :( click on image to zoom )

SS 10.A - C - A NUMERICAL CALCULATOR

10 - a). Design, develop and implement a C/C++/Java program to simulate a numerical calculator.

lab10a.c PROGRAM

#include <stdio.h>

int main()
{
char operator;
float num1, num2, result;
printf("Simulation of a Simple Calculator\n\n");
printf("Enter two numbers \n");
scanf("%f %f", &num1,&num2);

fflush(stdin);
printf("\nEnter the operator [+,-,*,/] \n");
scanf("%s", &operator);

switch(operator)
{
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
default : printf("Error in operation");     break;
}
printf("\n%.2f %c %.2f = %.2f\n", num1, operator, num2, result);
}


OUTPUT :( click on image to zoom )

SS 10.B - C - PAGE REPLACEMENT TECHNIQUE

10 - b). Design, develop and implement a C/C++/Java program to simulate page replacement technique.

lab10b.c PROGRAM

#include<stdio.h>

int main()
{
      int reference_string[10], page_faults = 0, m, n, s, pages, frames;
      printf("\nEnter Total Number of Pages:\t");
      scanf("%d", &pages);
      printf("\nEnter values of Reference String:\n");
      for(m = 0; m < pages; m++)
      {
            printf("Value No. [%d]:\t", m + 1);
            scanf("%d", &reference_string[m]);
      }
      printf("\nEnter Total Number of Frames:\t");
      scanf("%d", &frames);
      int temp[frames];
      for(m = 0; m < frames; m++)
            temp[m] = -1;
      for(m = 0; m < pages; m++)
      {
            s = 0;
            for(n = 0; n < frames; n++)
            {
                  if(reference_string[m] == temp[n])
                  {
                        s++;
                        page_faults--;
                  }
            }
            page_faults++;
            if((page_faults <= frames) && (s == 0))
                  temp[m] = reference_string[m];
            else if(s == 0)
                  temp[(page_faults - 1) % frames] = reference_string[m];
            printf("\n");
            for(n = 0; n < frames; n++)
                  printf("%d\t", temp[n]);
      }
      printf("\nTotal Page Faults:\t%d\n", page_faults);
      return 0;
}


OUTPUT :( click on image to zoom )

SS 11 - LEX - SENTENCE IS SIMPLE/COMPOUND

11. Program to recognize whether a given sentence is simple or compound.

vi lab11.l

%{
int flag=0;
%}

%%
" and " |
" or " |
" but " |
" because " |
" than "|
" nevertheless " {flag=1;}
%%
int main()
{
printf("Enter the sentence:\n");
yylex();
if(flag==1)
printf("compound statement");
else
printf("simple statements\n");
}


OUTPUT :( click on image to zoom )


SS 12 - C++ EMULATE UNIX ln COMMAND

12. Write a C++ program to emulate the Unix ln command

lab12.cpp PROGRAM

#include<iostream>
#inculde<unistd.h>

int main(int argc, char* argv[])
{
using namespace std;
if(argc!=3)
{
cout<<"Usage ./a.out sourcefile destination file\n";
return 0;
}
if(link(argv[1],argv[2])==-1)
{
cout<<"cant link\n";
return 1;
}
else
{
cout<<"Files have been Linked\n";
}
return 0;
}

OUTPUT :( click on image to zoom )


Thursday, April 4, 2019

DBMS QUESTION BANK FOR ALL MODULES - GURU PRASAD SIR

CHAPTER 1 - Introduction: Databases and Database Users

CHAPTER 2 - Database System Concepts and Architecture

CHAPTER 3 - Data Modeling Using the Entity-Relationship (ER) Model

CHAPTER 4 - Enhanced Entity-Relationship (EER) Modeling

CHAPTER 5 - The Relational Data Model and Relational Database Constraints

CHAPTER 6 - The Relational Algebra and Calculus

CHAPTER 7 - Relational Database Design by ER- and EERR-to-Relational Mapping

CHAPTER 8 - SQL-99: Schema Definition, Constraints, and Queries and Views

CHAPTER 9 - Introduction to SQL Programming Techniques

CHAPTER 10 - Functional Dependencies and Normalization for Relational Databases

CHAPTER 11 - Relational Database Design Algorithms and Further Dependencies

CHAPTER 12 - Practical Database Design Methodology and Use of UML Diagrams

CHAPTER 17 - Introduction to Transaction Processing Concepts and Theory

CHAPTER 18 - Concurrency Control Techniques

CHAPTER 19 - Database Recovery Techniques

Wednesday, April 3, 2019

DBMS 1 - LIBRARY DATABASE

1 - Consider the following schema for a Library Database: BOOK(Book_id, Title, Publisher_Name, Pub_Year) BOOK_AUTHORS(Book_id, Author_Name) PUBLISHER(Name, Address, Phone) BOOK_COPIES(Book_id, Branch_id, No-of_Copies) BOOK_LENDING(Book_id, Branch_id, Card_No, Date_Out, Due_Date) LIBRARY_BRANCH(Branch_id, Branch_Name, Address)
Write SQL queries to
  1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of copies in each branch, etc.
  2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to Jun 2017.
  3. Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation operation.
  4. Partition the BOOK table based on year of publication. Demonstrate its working with a simple query.
  5. Create a view of all books and its number of copies that are currently available in the Library.

ENTITY RELATIONSHIP DIAGRAM




 SCHEMA DIAGRAM





CREATION OF TABLES


-- 1
CREATE TABLE PUBLISHER
(NAME VARCHAR(20) PRIMARY KEY,
PHONE INTEGER,
ADDRESS VARCHAR(20));

DESC PUBLISHER;



-- 2
CREATE TABLE BOOK
(BID INTEGER PRIMARY KEY,
TITLE VARCHAR(20),
PUBYEAR VARCHAR(20),
PUBNAME REFERENCES PUBLISHER(NAME) ON DELETE CASCADE);

DESC BOOK;

-- 3
CREATE TABLE BOOK_AUTHORS
(ANAME VARCHAR(20),
BID REFERENCES BOOK(BID) ON DELETE CASCADE,
PRIMARY KEY(BID, ANAME));

DESC BOOK_AUTHORS;

-- 4
CREATE TABLE LIBRARY_BRANCH
(BID INTEGER PRIMARY KEY,
BNAME VARCHAR(50),
ADDRESS VARCHAR(50));

DESC LIBRARY_BRANCH;

-- 5
CREATE TABLE BOOK_COPIES
(NOOFCOPIES INTEGER,
BID REFERENCES BOOK(BID) ON DELETE CASCADE,
BRANCHID REFERENCES LIBRARY_BRANCH(BRANCHID) ON DELETE CASCADE,
PRIMARY KEY (BID,BRANCHID));

DESC BOOK_COPIES;

-- 6
CREATE TABLE CARD
(CARDNO INTEGER PRIMARY KEY);

DESC CARD;

-- 7
CREATE TABLE BOOKLENDING
(DATEOUT DATE,
DUEDATE DATE,
BID REFERENCES BOOK(BID) ON DELETE CASCADE,
BRANCHID REFERENCES LIBRARY_BRANCH(BRANCHID) ON DELETE CASCADE,
CARDNO REFERENCES CARD(CARDNO) ON DELETE CASCADE,
PRIMARY KEY (BID,BRANCHID,CARDNO));

DESC BOOKLENDING;



INSERTION OF TABLES
-- 1
INSERT INTO PUBLISHER VALUES('MCGRAW-HILL',9191919191,'BANGALORE');
INSERT INTO PUBLISHER VALUES('PEARSON',8181818181,'NEWDELHI');
INSERT INTO PUBLISHER VALUES('RANDOM HOUSE',7171717171,'HYDERABAD');
INSERT INTO PUBLISHER VALUES('LIVRE',6161616161,'CHENNAI');
INSERT INTO PUBLISHER VALUES('PLANETA',5151515151,'BANGALORE');

SELECT * FROM PUBLISHER;


-- 2
INSERT INTO BOOK VALUES(1,'DBMS','JAN-2017','MCGRAW-HILL');
INSERT INTO BOOK VALUES(2,'ADBMS','JUN-2016','MCGRAW-HILL');
INSERT INTO BOOK VALUES(3,'CN','SEP-2016','PEARSON');
INSERT INTO BOOK VALUES(4,'CG','SEP-2015','PLANETA');
INSERT INTO BOOK VALUES(5,'OS','MAY-2016','PEARSON');

SELECT * FROM BOOK;

-- 3
INSERT INTO BOOK_AUTHORS VALUES('NAVATHE',1);
INSERT INTO BOOK_AUTHORS VALUES('NAVATHE',2);
INSERT INTO BOOK_AUTHORS VALUES('TANENBAUM',3);
INSERT INTO BOOK_AUTHORS VALUES('EDWARD ANGEL',4);
INSERT INTO BOOK_AUTHORS VALUES('GALVIN',5);

SELECT * FROM BOOK_AUTHORS;


-- 4
INSERT INTO LIBRARY_BRANCH VALUES(10,'RR NAGAR','BANGALORE');
INSERT INTO LIBRARY_BRANCH VALUES(11,'RNSIT','BANGALORE');
INSERT INTO LIBRARY_BRANCH VALUES(12,'RAJAJI NAGAR','BANGALORE');
INSERT INTO LIBRARY_BRANCH VALUES(13,'NITTE','MANGALORE');
INSERT INTO LIBRARY_BRANCH VALUES(14,'MANIPAL','UDUPI');

SELECT * FROM LIBRARY_BRANCH;


-- 5
INSERT INTO BOOK_COPIES VALUES(10,1,10);
INSERT INTO BOOK_COPIES VALUES(5,1,11);
INSERT INTO BOOK_COPIES VALUES(2,2,12);
INSERT INTO BOOK_COPIES VALUES(5,2,13);
INSERT INTO BOOK_COPIES VALUES(7,3,14);
INSERT INTO BOOK_COPIES VALUES(1,5,10);
INSERT INTO BOOK_COPIES VALUES(3,4,11);

SELECT * FROM BOOK_COPIES;


-- 6
INSERT INTO CARD VALUES(100);
INSERT INTO CARD VALUES(101);
INSERT INTO CARD VALUES(102);
INSERT INTO CARD VALUES(103);
INSERT INTO CARD VALUES(104);

SELECT * FROM CARD;



-- 7
INSERT INTO BOOKLENDING VALUES('01-JAN-17','01-JUN-17',1,10,101);
INSERT INTO BOOKLENDING VALUES('11-JAN-17','11-MAR-17',3,14,101);
INSERT INTO BOOKLENDING VALUES('21-FEB-17','21-APR-17',2,13,101);
INSERT INTO BOOKLENDING VALUES('15-MAR-17','15-JUL-17',4,11,101);
INSERT INTO BOOKLENDING VALUES('12-APR-17','12-MAY-17',1,11,104);

SELECT * FROM BOOKLENDING;



--QUERIES

-- QUERY 1

SELECT B.BID, B.TITLE, B.PUBNAME, A.ANAME,C.NOOFCOPIES,L.BRANCHID
FROM BOOK B, BOOK_AUTHORS A, BOOK_COPIES C, LIBRARY_BRANCH LIBRARY_BRANCH LIBRARY_BRANCH L
WHERE B.BID=A.BID
AND B.BID=C.BID
AND L.BRANCHID=C.BRANCHID;

 -- QUERY 2

SELECT CARDNO
FROM BOOKLENDING
WHERE DATEOUT BETWEEN '01-JAN-17' AND '01-JUN-17'
GROUP BY CARDNO
HAVING COUNT(*)>3;



-- QUERY 3

DELETE FROM BOOK
WHERE BID = 3;


-- QUERY 4

CREATE VIEW VPUBLICATION AS
SELECT PUBYEAR
FROM BOOK;

-- QUERY 5

CREATE VIEW VBOOKS AS
SELECTB.BID, B.TITLE, C.NOOFCOPIES
FROM
BOOK B, BOOK_COPIES C, LIBRARY_BRANCH L
WHEREB.BID=C.BID
AND C.BRANCHID=L.BRANCHID;