Archive for Engineering

System Software Lab Programs ( Lex and Yaac Programs ) 6a

// June 14th, 2009 // View Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

6) Program to recognize the grammar (anb, n>= 10).

6.l

/* 6.l */
%{
#include<stdio.h>
#include “y.tab.h”
%}
%%
[aA] return A;
[bB] return B;
.|\n return yytext[0];
%%

—————————————–

6.y

/* 6.y */
%{
#include<stdio.h>
%}
%token A B
%%
stmt: S ‘\n’ {printf(“\nValid expression”);exit(1);}
;
S: X B
X: X A | A A A A A A A A A A
%%
main()
{
printf(“\nEnter the expression: “);
if(yyparse())
{
printf(“\nValid expression”);
exit(0);
}
}
int yyerror()
{
printf(“\nInvalid expression\n”);
return 1;
}

int yywrap()
{
return 1;
}

File Structure ( FS ) Lab Program 6 ( 6th Semester Information Science) – Alternative 2

// June 13th, 2009 // View Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 6 : Write a C++ program to implement index on secondary key, the name, for a file of student objects. Implement add ( ), search ( ), delete ( ) using the secondary index

This includes tow files

1.

//scindex.h
class SCIndex
{
public:

int  Insert (const char  *skey,const char  *pkey);
int  Remove (const char * key);
int Search (const char * key) const;
void Print (ostream &);
void Write (ostream &);
void Read(istream &);
void Init (int maxKeys);
private:
int MaxKeys;
int NumKeys;
char  **sKeys;
char  **pKeys;
int Find (const char * key) const;

};

int SCIndex :: Insert (const char *skey,const char *pkey)
{
int i=0,j=0;
if (NumKeys == MaxKeys) return 0; //no room for another key

for (i = NumKeys-1; i >=0; i–)
{

if(strcmp(skey,sKeys[i]) > 0)
break; // insert into location i+1
else if(strcmp(skey,sKeys[i]) == 0)
break; // insert into location i+1

else
{
sKeys[i+1] = sKeys[i];
pKeys[i+1] = pKeys[i];
}
}
j=i;

for(j=i; j>=0; j–)

//   while(strcmp(skey,sKeys[j]) == 0)
{
if(strcmp(pkey,pKeys[j]) > 0)
break;
sKeys[j+1] = sKeys[j];
pKeys[j+1] = pKeys[j];

}
sKeys[j+1] = strdup(skey);
pKeys[j+1] = strdup(pkey);
NumKeys ++;
return 1;
}

void SCIndex::Write(ostream &stream)
{    int i=0;
stream<<NumKeys<<”|”;
for (i = 0; i <NumKeys; i++)
{

stream<<sKeys[i];
stream<<”|”;
stream<<pKeys[i];
stream<<”|”;

}
}

int SCIndex :: Remove (const char * key)
{
int index = Find (key);
if (index < 0) return 0; // key not in index
for (int i = index; i < NumKeys; i++)
{
sKeys[i] = sKeys[i+1];
pKeys[i] = pKeys[i+1];
}
NumKeys –;
return 1;
}

int  SCIndex :: Search (const char * key) const
{
int i=0,fnd=0;
for(i=0;i<NumKeys;i++)
{
if (strcmp(sKeys[i], key)==0)
{
fnd=1;
cout<<endl<<sKeys[i]<<”             “<<pKeys[i];
}
}
if (fnd==0)
return -1;
return 1;

}

void SCIndex :: Print (ostream & stream)
{
stream << “Secondary Index :  Max keys = “<<MaxKeys
<<”    Number of  keys = “<<NumKeys<<endl;
for (int i = 0; i<NumKeys; i++)
stream <<”\tKey["<<i<<"] “<<sKeys[i]
<<”  : Primary key =”<<pKeys[i]<<endl;
}

int SCIndex :: Find (const char * key) const
{
for (int i = 0; i < NumKeys; i++)
if (strcmp(sKeys[i], key)==0)
return i;// key found
else if (strcmp(sKeys[i], key)>0) return -1;// not found
return -1;// not found
}

void SCIndex :: Init (int maxKeys)
{

if (maxKeys <= 0)
{
MaxKeys = 0;
cout<<endl<<”No Records : “<<endl;
}
MaxKeys = maxKeys;
sKeys =new char *[maxKeys];
pKeys = new char *[maxKeys];
NumKeys=0;

}

void SCIndex::Read(istream &stream)
{
int i=0;
/*
stream.read(NumKeys,4);
for (i = 0; i <NumKeys; i++)
{

stream.read(sKeys[i],10);
stream.read(pKeys[i],10);
}
*/
}

—————————————————————————————————————–

2. //lab6.cpp

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include “student.h”
#include “varbuf.h”
#include “index.h”
#include “scindex.h”

main()
{

//char buffer[500];

student s;
VariableLengthBuffer b;
TextIndex ind;
SCIndex scind;
fstream datafile,indexp,indexs;
int choice=0,k=0,k1=0,n=0;
int recaddr,i;
char delkey[20],skey[20],srchp[10];
datafile.open(“student.txt”,ios::out|ios::in);
indexp.open(“pstudent.ind”, ios::out);
indexs.open(“sstudent.ind”, ios::out);

b.Init(65);
ind.Init(50);
scind.Init(50);
b.Clear();
cout<<endl<<”Enter number of students : “;
cin>>n;

for(i=1;i<=n;i++)
{
cout<<endl<<” Record : “<<i;
s.clear();
s.readdata();
b.Clear();
b.Pack(s.usn);
b.Pack(s.name);
b.Pack(s.address);
b.Pack(s.sem);
recaddr=datafile.tellg();
k=ind.Insert(s.usn,recaddr);
if( k != 1)
cout<<endl<<” Duplicate Primary key : Record Not Inserted “;
else
{
b.Write(datafile);
k=scind.Insert(s.name,s.usn);
}
}

do
{
cout<<endl;
cout<<endl<<”1.Insert Record “;
cout<<endl<<”2.Delete Record “;
cout<<endl<<”3.Search Record “;
cout<<endl<<”4.Display Primary Index”;
cout<<endl<<”5.Display Secondary Index”;
cout<<endl<<”6.Exit “;
cout<<endl;

cout<<” Enter your choice “;
cin>>choice;
switch(choice)
{
case 1:
cout<<endl;
s.readdata();
datafile.seekg(0,ios::end);
recaddr=datafile.tellg();
k=ind.Insert(s.usn,recaddr);
if(k!=1)
cout<<endl<<”Record not inserted : Duplicate Primary Key “;

else
{
k=scind.Insert(s.name,s.usn);
b.Clear();
b.Pack(s.usn);
b.Pack(s.name);
b.Pack(s.address);
b.Pack(s.sem);
b.Write(datafile);
cout<<endl<<”Record Inserted “;
}
break;

case 2:
cout<<endl;
cout<<” Enter Name of the Record to be deleted : ” ;
cin>>delkey;
cout<<endl<<”Following Record/s Found for : “<<delkey<<endl;
cout<<endl<<”Secondary Key   Primary Key”;
cout<<endl<<”————–  ————-”;
k1=scind.Search(delkey);
if(k1 !=1)
{
cout<<endl<<” Record not found “;
break;
}
else
{
cout<<endl<<” Enter USN : “;
cin>>srchp;

k=scind.Remove(delkey);
k1=ind.Remove(srchp);
if(k!=1 || k1!=1)
cout<<endl<<”Record not Found “;
else
cout<<endl<<”Record of “<<delkey<<” USN “<<srchp<<” Deleted”;

}
break;

case 3:
cout<<endl;
cout<<” Enter name of the student to be Searched : ” ;
cin>>skey;
cout<<endl<<”Following Record/s Found for : “<<skey<<endl;
cout<<endl<<”Secondary Key    Primary Key”;
cout<<endl<<”————–   ————”;

k1=scind.Search(skey);
if(k1 !=1)
{
cout<<endl<<” Record not found “;
break;
}
else
{
cout<<endl<<” Enter USN : “;
cin>>srchp;
recaddr=ind.Search(srchp);
if(recaddr<0)
cout<<endl<<”Record does not exist”;
else
{
cout<<endl<<” Record found “;
cout<<endl<<”Record reference of USN “<<srchp<<” is “<<recaddr<<endl;
datafile.seekg(recaddr,ios::beg);
b.Clear();
b.Read(datafile);
b.bufdisplay();
s.clear();
b.Unpack(s.usn);
b.Unpack(s.name);
b.Unpack(s.address);
b.Unpack(s.sem);
s.display();
s.clear();
datafile.seekg(0,ios::end);
}
}
break;

case 4:
ind.Print(cout);
break;

case 5:
scind.Print(cout);
break;
//case 5: exit(1);
}
}   while(choice < 6);

ind.Write(indexp);
scind.Write(indexs);

indexp.close();
indexs.close();
datafile.close();

}

File Structure ( FS ) Lab Program 6 ( 6th Semester Information Science)- Alternative 1

// June 13th, 2009 // View Comments // Educational, Engineering, File Structure (F.S.) Lab

Program 6 : Write a C++ program to implement index on secondary key, the name, for a file of student objects. Implement add ( ), search ( ), delete ( ) using the secondary index

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
class secondary_index{

public:
string Name_list[100];
int Address_list[100];
int count;
void create_index();
void insert() ;
void remove(string);
void delete_from_file(int);
void search(string);
int search_index(string);
void read_from_file(int);
string extract_Name(string);
void sort_index();

};

void secondary_index::create_index()
{
fstream file;
int pos;
string buffer,name;
count=-1;
file.open(“1.txt”,ios::in);
while(!file.eof())
{
pos=file.tellg();
buffer.erase();
getline(file,buffer);
if(buffer.empty())
break;
//imp since it leads the last \n and goes to new line
name=extract_Name(buffer);
Name_list[++count]=name;
Address_list[count]=pos;
}
file.close();
sort_index();

buffer.erase();
}

string secondary_index::extract_Name(string buffer)
{string USN,Name;
int i=0;
USN.erase();
while(buffer[i]!=’|')
USN+=buffer[i++];
Name.erase();
i++;
while(buffer[i]!=’|')
Name+=buffer[i++];
return Name;
}

void secondary_index::sort_index()
{
int i,j,temp_Address;
string temp_Name;
for(int i=0;i<count;i++)
{
for(int j=i+1;j<=count;j++)
{
if(Name_list[i]>Name_list[j])
{
temp_Name=Name_list[i];
Name_list[i]=Name_list[j];
Name_list[j]=temp_Name;
temp_Address=Address_list[i];
Address_list[i]=Address_list[j];
Address_list[j]=temp_Address;

}

}
}
}

void secondary_index::insert()
{
string   USN,Name,Branch,sem,buffer;
int semester,pos;
fstream file ;
cout<< “\n USN:”;
cin>>USN;
cout<< “\n Name:”;
cin>>Name;
cout<< “\nBranch:” ;
cin>>Branch;
cout<< “\nSEMESTER:”;
cin>>semester;
stringstream out;
out<<semester;
sem=out.str();
buffer=USN+’|'+Name+’|'+Branch+’|'+sem+’$'+’\n’;
file.open(“1.txt”,ios::out|ios::app);
pos=file.tellp();
file<<buffer;
file.close();
Name_list[++count]=Name;

Address_list[count]=pos;
sort_index();
}
int secondary_index::search_index(string key){
int low=0,high=count,mid=0,flag=0;
while(low<=high){
mid=(low+high)/2;
if(Name_list[mid]==key){
flag=1;
break;
}
if(Name_list[mid]>key)
high=mid-1;
else
low=mid+1;
}
if(flag){
return mid;
}
else
return -1;
}
void secondary_index::search(string key){
int pos=0,t;
string buffer;
buffer.erase();
pos=search_index(key);
if(pos>=0){
read_from_file(pos);
t=pos;
while(Name_list[++t]==key)
read_from_file(t);
t=pos;
while(Name_list[--t]==key)
read_from_file(t);
}
else
cout<<”\n”<<”Not found”;
}
void secondary_index::read_from_file(int pos){
int address;
string buffer;
/*for(int i=pos;i<count;i++){
Name_list[i]=Name_list[i+1];
Address_list[i]=Address_list[i+1];
}    */
fstream file;
file.open(“1.txt”);
address=Address_list[pos];
file.seekp(address,ios::beg);
getline(file,buffer);
cout<<endl<<”Found the record:”<<buffer;
file.close();
}
void secondary_index::remove(string key){
int pos=0,t,ch;
string buffer;
buffer.erase();
pos=search_index(key);
if(pos>=0){
read_from_file(pos);
cout<<endl<<”delete ?”;
cin>>ch;
if(ch)
delete_from_file(pos);
t=pos;
while(Name_list[++t]==key){
read_from_file(t);
cout<<endl<<”Delete?”;
cin>>ch;
if(ch)
delete_from_file(t);
}
t=pos;
while(Name_list[--t]==key){
read_from_file(t);
cout<<endl<<”Delete?”;
cin>>ch;
if(ch)
delete_from_file(t);
}
}
else
cout<<”\n\nNot found\n”;
}
void secondary_index::delete_from_file(int pos){
char del_ch=’*';
int i,address;
if(pos>=0){
fstream file;
file.open(“1.txt”);
address=Address_list[pos];
file.seekp(address,ios::beg);
file.put(del_ch);
cout<<endl<<”\n\nRecord deleted:”;
file.close();
}
for(int i=pos;i<count;i++){
Name_list[i]=Name_list[i+1];
Address_list[i]=Address_list[i+1];
}

count–;
}
int main(){
int ch;
string key;
secondary_index i1;
i1.create_index();
while(1){
cout<<”\nMain Menu\n1:Add()\n2:Search()\n3:Delete()\n4:Exit()\nEnter the choice”;
cin>>ch;
switch(ch){
case 1:cout<<”Data \n”;
i1.insert();
break;
case 2:cout<<”Enter the name\n”;
cin>>key;
i1.search(key);
break;
case 3:cout<<”Enter the Name”;
cin>>key;
i1.remove(key);
break;
case 4: return 0;
default:cout<<”Wrong Choice!!!!!!!\n\n”;

}
}
}

System Software Lab Programs ( Lex and Yaac Programs ) 5b

// June 13th, 2009 // View Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

5b. Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using the grammar (anbn, n>= 0).

5b.l

%{
#include<stdio.h>
#include “y.tab.h”
%}

%%
[a] return A;
[b] return B;
. return yytext[0];
\n return yytext[0];
%%
——————————————

5b.y

%{
#include<stdio.h>
%}
%token A B
%%
input:expr ‘\n’ {return 0;}
expr: X
X: A X B|;
%%

main()
{
printf(“\nEnter string: “);
if(!yyparse())
{
printf(“\nValid”);
exit(0);
}
}
int yyerror()
{
printf(“\nInvalid”);
return 1;
}
int yywrap()
{
return 1;
}

System Software Lab Programs ( Lex and Yaac Programs ) 5a

// June 12th, 2009 // View Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

5) a. Program to evaluate an arithmetic expression involving operators +, -, * and /.

5a.l

%{
#include “y.tab.h”
extern int yylval;
%}

%%
[0-9]+ { yylval=atoi(yytext);return NUM;}
[ \t];
\n return 0;
. return yytext[0];
%%

————————————————-

5a.y
/* 5a.y */
%{
#include<stdio.h>
%}
%token NUM
%left ‘-”+’
%left ‘*”/’

%%
start: expr    {printf(“\nValid expression.\nValue: %d”,$1);}

expr: expr’+'expr   {$$=$1+$3;}
|expr’-'expr    {$$=$1-$3;}
|expr’*'expr    {$$=$1*$3;}
|expr’/'expr     {if($3==0)
yyerror(“Divide by zero”);
else
$$=$1/$3;
}
|’(‘ expr ‘)’  {$$=$2;}
|NUM
;
%%

main()
{
printf(“\nEnter an expression: “);
yyparse();
return(0);
}

int yyerror(char *msg)
{
printf(“\n%s”,msg);
printf(“\nInvalid expression”);
exit(0);
}

System Software Lab Programs ( Lex and Yaac Programs ) 4a

// June 11th, 2009 // View Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

4) a. Program to recognize a valid arithmetic expression that uses operators+, -, * and /.

4a.l

%{
#include “y.tab.h”
extern int yylval;
%}

%%
[0-9]+ {yylval=atoi(yytext); return NUM; }
[ \t] ;
\n return 0;
. return yytext[0];
%%

———————————————–

4a.y

%{
#include<stdio.h>
%}

%%

%token NUM
%left ‘+’ ‘-’
%left ‘+’ ‘-’
%nonassoc UMINUS

%%

e:e’+'e |
e’-'e |
e’*'e |
e’/'e {if($3==0)  {printf(“\n DIVIDE BY ZERO ERROR”); exit(0);}} |
‘(‘e’)’ |
‘-’e %prec UMINUS |
NUM
;
%%

yyerror()
{
printf(“invalid exppression\n”);
exit(0);
}

int main()
{
printf(“enter an expression:”);
yyparse();
printf(“valid expression”);
return 0;
}



System Software Lab Programs ( Lex and Yaac Programs ) 3.l

// June 10th, 2009 // View Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

3) Program to recognize and count the number of identifiers in a given
input file.

%{
int id=0;
%}

ID [_a-zA-Z][a-zA-Z0-9]*
DECLN “int”|”float”|”char”|”short”|”double”|”long”|”unsigned”
%x DEFN

%%
{DECLN}    {BEGIN DEFN;}
<DEFN>{ID}\, id++;
<DEFN>{ID}\; id++;
<*>\n ;
<*>. ;
%%

main(int argc, char **argv)
{
if(argc==2)
{
yyin=fopen(argv[1],”r”);
yylex();
printf(“\n Number of identifiers : %d\n”,id);
}
else
printf(“\n Usage :%s <file>\n”,argv[0]);
}

System Software Lab Programs ( Lex and Yaac Programs )2b.l

// June 9th, 2009 // View Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

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

%{
#include<stdio.h>
int F0=0,F1=0,F2=0,error=0,l1=0,l2=0;
%}

verb am|run|sit|did|study|is|large|go|come
subject [a-zA-Z]+
compnd “and”|”but”|”also”|”either”|”neither”|”yet”|”still”|”consequences”

%%
{verb} { if(F2==1)
l2=1;
F2=1;
if(F1==0)
error=1;
}

{compnd} { F0=1; }
{subject} { if(F1!=0)
l1=1;
F1++;
}
%%

main()
{
printf(“\n Enter a sentence: “);
yylex();

if(error==1 || F2==0 || F1==0)
{
printf(“\n Invalid sentence”);
exit(0);
}

if(F0==1 && l1==1 && l2==1)
printf(“\n Compound sentence\n”);
else
printf(” \nSimple sentence\n”);
}

System Software Lab Programs ( Lex and Yaac Programs )2a.l

// June 9th, 2009 // View Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

2) a. Program to recognize a valid arithmetic expression and to recognize
the identifiers and operators present. Print them separately.

%{
#include<stdio.h>
#include<string.h>
#define max 20
int flag,i,j,k,top,b;
char stack[max],ident[max],oper[max],brac[max];
%}

%%
[a-zA-Z0-9] {j++;strcat(ident,yytext);}
[a-zA-Z0-9]+ {flag=1;}
“+” {oper[k++]=’+';}
“-” {oper[k++]=’-';}
“*” {oper[k++]=’*';}
“/” {oper[k++]=’*';}
“$” {oper[k++]=’$';}
“^” {oper[k++]=’^';}
“%” {oper[k++]=’%';}
“(” { stack[++top]=’(‘;brac[b++]=’(‘;}
“)” { if (stack[top]==’(‘ && top!=-1) top–;flag=0;brac[b++]=’)';}
%%

int  main()
{
int i=j=k=b=flag=0;
top=-1;

printf(“\nEnter the Expression : “);
yylex();
printf(“\nThe identifiers are :  “);

for(i=0;i<j;i++)
printf(“\t%c”,ident[i]);

printf(“\nNo.of identifiers are : %d”,j);
printf(“\nThe operators are :\n”);

for(i=0;i<k;i++)
printf(“\t%c”,oper[i]);

printf(“\nNo. of operators are :%d”,k);
if(flag==0 && top==-1 && j==k+1)
printf(“\nValid expression”);
else
printf(“\nInvalid expression”);

return 0;
}

System Software Lab Programs ( Lex and Yaac Programs )1b

// June 8th, 2009 // View Comments // Educational, Engineering, Unix and System Software Lab

Subject : System Software Laboratory

Branch : Information Science & Engineering

Semester : 6

University : VTU

………………………………………………………………………………………………………………………….
PART – A

………………………………………………………………………………………………………………………….

1b. Program to count the numbers of comment lines in a given C program. Also eliminate them and copy the resulting program into separate file.

%{
int c=0,state=1;
%}

%%
“/*” { state=0;}
“*/” { c++; if (!state) state=1;}
{ if (state==1)
fprintf(yyout,”%s”,yytext);
}
%%

FILE * fp;
main(int argc,char ** argv)
{
if(argc<=1)
{
printf(“\nNo file”);
exit(1);
}

fp=fopen(argv[1],”w”);

if(!fp)
{
printf(“\nNo output file”);
exit(1);
}

yyout=fp;

fp=fopen(argv[1],”r”);

if(!fp)
{
printf(“\nNo inpput file”);
exit(1);
}

yyin=fp;
yylex();
printf(“\nNumber of comment lines : %d”,c);
}

yywrap()
{
if(state==0)
{
printf(“\nUnterminated commennt”);
return 1;
}
}

Top Commentators

Get Adobe Flash playerPlugin by wpburn.com wordpress themes