Monday, April 16, 2012
end sem lab exam java declaration parser solution
/************PARSER BY RAJAT************/
#include
#include
#include
#include
#include
#include
FILE *f1;
int cc=0;
int ll=0;
int p=0;
char tokk[100];
struct node
{
char tok[100];
char lex[100];
int l;
}hash[100];
void lex()
{
char c;
c=getc(f1);
while(c!=EOF)
{
switch(c)
{
case '\n': ll++;
c=getc(f1);
break;
case ' ':
c=getc(f1);
break;
case'{': strcpy(hash[cc].lex,"{\0");
hash[cc].l=ll;
strcpy(hash[cc].tok,"LBRAC");
cc++;
c=getc(f1);
break;
case'}':
strcpy(hash[cc].lex,"}");
hash[cc].l=ll;
strcpy(hash[cc].tok,"RBRAC");
cc++;
c=getc(f1);
break;
case'(':
strcpy(hash[cc].lex,"(");
hash[cc].l=ll;
strcpy(hash[cc].tok,"LB");
cc++;
c=getc(f1);
break;
case')':
strcpy(hash[cc].lex,")");
hash[cc].l=ll;
strcpy(hash[cc].tok,"RB");
cc++;
c=getc(f1);
break;
case';':
strcpy(hash[cc].lex,";");
hash[cc].l=ll;
strcpy(hash[cc].tok,"semicol");
cc++;
c=getc(f1);
break;
case',':
strcpy(hash[cc].lex,",");
hash[cc].l=ll;
strcpy(hash[cc].tok,"comma");
cc++;
c=getc(f1);
break;
default:
if(isdigit(c))
{ tokk[0]='\0';
int x=0;
while(isalnum(c))
{ tokk[x++]=c;
c=getc(f1);
}
tokk[x]='\0';
strcpy(hash[cc].lex,tokk);
hash[cc].l=ll;
strcpy(hash[cc].tok,"iid");
cc++;
break;
}
if(isalpha(c))
{ tokk[0]='\0';
int x=0;
while(isalnum(c))
{ tokk[x++]=c;
c=getc(f1);
}
cout< tokk[x]='\0';
if(strcmp(tokk,"public")==0||strcmp(tokk,"void")==0||strcmp(tokk,"implements")==0||strcmp(tokk,"private")==0||strcmp(tokk,"protected")==0||strcmp(tokk,"class")==0||strcmp(tokk,"void")==0 ||strcmp(tokk,"extends")==0||strcmp(tokk,"Super")==0)
{ strcpy(hash[cc].lex,tokk);
hash[cc].l=ll;
strcpy(hash[cc].tok,"keyword");
cc++;
break;
}
else
{
strcpy(hash[cc].lex,tokk);
hash[cc].l=ll;
strcpy(hash[cc].tok,"id");
cc++;
break;
}
}
}
}
}
void modi()
{
while((strcmp("public",hash[p].lex)==0)||(strcmp(hash[p].lex,"private")==0)||(strcmp(hash[p].lex,"protected")==0))
{if(!((strcmp("public",hash[p].lex)==0)||(strcmp(hash[p].lex,"private")==0)||(strcmp(hash[p].lex,"protected")==0)))
cout< p++;
}
}
void dec()
{
if(!((strcmp(hash[p].lex,"void")==0)))
cout< p++;
if((strcmp(hash[p].tok,"iid")==0))
cout< else if(!(strcmp(hash[p].tok,"id")==0))
cout< p++;
if(!((strcmp(hash[p].lex,"(")==0)))
cout< p++;
if(!((strcmp(hash[p].lex,")")==0)))
cout< p++;
if(!((strcmp(hash[p].lex,";")==0)))
cout< p++;
}
void body()
{
if(!((strcmp(hash[p].lex,"{")==0)))
cout< p++;
if((strcmp(hash[p].lex,"}")==0))
exit(0);
//cout< else
dec();
if(!(strcmp(hash[p].lex,"}")==0))
cout<
}
void imp()
{
if(!((strcmp(hash[p].lex,"implements")==0)))
cout< p++;
if((strcmp(hash[p].tok,"iid")==0))
cout< else if(!(strcmp(hash[p].tok,"id")==0))
cout< p++;
body();
}
void sup()
{
if(!((strcmp(hash[p].lex,"extends")==0)))
cout< p++;
if((strcmp(hash[p].tok,"iid")==0))
cout< else if(!(strcmp(hash[p].tok,"id")==0))
cout< p++;
imp();
}
void decl()
{
if(!((strcmp("public",hash[p].lex)==0)||(strcmp(hash[p].lex,"private")==0)||(strcmp(hash[p].lex,"protected")==0)))
cout< p++;modi();
if(!((strcmp(hash[p].lex,"class")==0)))
cout< p++;
if((strcmp(hash[p].tok,"iid")==0))
cout< else if(!(strcmp(hash[p].tok,"id")==0))
cout< p++;
sup();
}
void main()
{
clrscr();
char str[100];
char c;
cout< gets(str);
f1=fopen(str,"r");
lex();
for(int s=0;s {
cout< //cout<
}
decl();
getche();
}
/****sample input file****/
/*
public private class ab extends bc implements xyz
{
void id();
}
or
private clas ab extends bc implements xyz
{
}
*/
#include
#include
#include
#include
#include
#include
FILE *f1;
int cc=0;
int ll=0;
int p=0;
char tokk[100];
struct node
{
char tok[100];
char lex[100];
int l;
}hash[100];
void lex()
{
char c;
c=getc(f1);
while(c!=EOF)
{
switch(c)
{
case '\n': ll++;
c=getc(f1);
break;
case ' ':
c=getc(f1);
break;
case'{': strcpy(hash[cc].lex,"{\0");
hash[cc].l=ll;
strcpy(hash[cc].tok,"LBRAC");
cc++;
c=getc(f1);
break;
case'}':
strcpy(hash[cc].lex,"}");
hash[cc].l=ll;
strcpy(hash[cc].tok,"RBRAC");
cc++;
c=getc(f1);
break;
case'(':
strcpy(hash[cc].lex,"(");
hash[cc].l=ll;
strcpy(hash[cc].tok,"LB");
cc++;
c=getc(f1);
break;
case')':
strcpy(hash[cc].lex,")");
hash[cc].l=ll;
strcpy(hash[cc].tok,"RB");
cc++;
c=getc(f1);
break;
case';':
strcpy(hash[cc].lex,";");
hash[cc].l=ll;
strcpy(hash[cc].tok,"semicol");
cc++;
c=getc(f1);
break;
case',':
strcpy(hash[cc].lex,",");
hash[cc].l=ll;
strcpy(hash[cc].tok,"comma");
cc++;
c=getc(f1);
break;
default:
if(isdigit(c))
{ tokk[0]='\0';
int x=0;
while(isalnum(c))
{ tokk[x++]=c;
c=getc(f1);
}
tokk[x]='\0';
strcpy(hash[cc].lex,tokk);
hash[cc].l=ll;
strcpy(hash[cc].tok,"iid");
cc++;
break;
}
if(isalpha(c))
{ tokk[0]='\0';
int x=0;
while(isalnum(c))
{ tokk[x++]=c;
c=getc(f1);
}
cout<
if(strcmp(tokk,"public")==0||strcmp(tokk,"void")==0||strcmp(tokk,"implements")==0||strcmp(tokk,"private")==0||strcmp(tokk,"protected")==0||strcmp(tokk,"class")==0||strcmp(tokk,"void")==0 ||strcmp(tokk,"extends")==0||strcmp(tokk,"Super")==0)
{ strcpy(hash[cc].lex,tokk);
hash[cc].l=ll;
strcpy(hash[cc].tok,"keyword");
cc++;
break;
}
else
{
strcpy(hash[cc].lex,tokk);
hash[cc].l=ll;
strcpy(hash[cc].tok,"id");
cc++;
break;
}
}
}
}
}
void modi()
{
while((strcmp("public",hash[p].lex)==0)||(strcmp(hash[p].lex,"private")==0)||(strcmp(hash[p].lex,"protected")==0))
{if(!((strcmp("public",hash[p].lex)==0)||(strcmp(hash[p].lex,"private")==0)||(strcmp(hash[p].lex,"protected")==0)))
cout<
}
}
void dec()
{
if(!((strcmp(hash[p].lex,"void")==0)))
cout<
if((strcmp(hash[p].tok,"iid")==0))
cout<
cout<
if(!((strcmp(hash[p].lex,"(")==0)))
cout<
if(!((strcmp(hash[p].lex,")")==0)))
cout<
if(!((strcmp(hash[p].lex,";")==0)))
cout<
}
void body()
{
if(!((strcmp(hash[p].lex,"{")==0)))
cout<
if((strcmp(hash[p].lex,"}")==0))
exit(0);
//cout<
dec();
if(!(strcmp(hash[p].lex,"}")==0))
cout<
}
void imp()
{
if(!((strcmp(hash[p].lex,"implements")==0)))
cout<
if((strcmp(hash[p].tok,"iid")==0))
cout<
cout<
body();
}
void sup()
{
if(!((strcmp(hash[p].lex,"extends")==0)))
cout<
if((strcmp(hash[p].tok,"iid")==0))
cout<
cout<
imp();
}
void decl()
{
if(!((strcmp("public",hash[p].lex)==0)||(strcmp(hash[p].lex,"private")==0)||(strcmp(hash[p].lex,"protected")==0)))
cout<
if(!((strcmp(hash[p].lex,"class")==0)))
cout<
if((strcmp(hash[p].tok,"iid")==0))
cout<
cout<
sup();
}
void main()
{
clrscr();
char str[100];
char c;
cout<
f1=fopen(str,"r");
lex();
for(int s=0;s
cout<
}
decl();
getche();
}
/****sample input file****/
/*
public private class ab extends bc implements xyz
{
void id();
}
or
private clas ab extends bc implements xyz
{
}
*/
Wednesday, August 17, 2011
DAA Assignment Questions
Question 1
Consider the algorithm :
Algorithm mystery(n)
//Input : A non negative integer
s<-0
for i<-1 to n do
s<-s+i
return s
a) What does this algorithm compute ?
b)What is its basic operation ?
c)How many times is the basic operation executed ?
d)What is the efficiency class of the algorithm ?
e)Suggest an improvement if possible.
Question 2
Consider the following recursive algorithm
ALGORITHM S(n)
//Input: A positive integer n
//Output: The sum of the first n cubes
if n = 1 return 1
else return S(n - 1) + n * n * n
a. Set up and solve a recurrence relation for the number of times the algorithm's basic operation is executed.
b. How does this algorithm compare with the straightforward non-recursive algorithm for computing this sum?
Question 3
How many comparisons will be made by the Brute Force technique for each of the following cases :
1) Binary text of 1000 zeros.
Patterns:
a) 00001 b) 10100 c) 10001
Question 4
Consider the problem of counting in a given text , the no. of substrings that start with 'a' and end with 'b'.Design a Brute Force algorithm for this problem and determine its efficiency class
Consider the algorithm :
Algorithm mystery(n)
//Input : A non negative integer
s<-0
for i<-1 to n do
s<-s+i
return s
a) What does this algorithm compute ?
b)What is its basic operation ?
c)How many times is the basic operation executed ?
d)What is the efficiency class of the algorithm ?
e)Suggest an improvement if possible.
Question 2
Consider the following recursive algorithm
ALGORITHM S(n)
//Input: A positive integer n
//Output: The sum of the first n cubes
if n = 1 return 1
else return S(n - 1) + n * n * n
a. Set up and solve a recurrence relation for the number of times the algorithm's basic operation is executed.
b. How does this algorithm compare with the straightforward non-recursive algorithm for computing this sum?
Question 3
How many comparisons will be made by the Brute Force technique for each of the following cases :
1) Binary text of 1000 zeros.
Patterns:
a) 00001 b) 10100 c) 10001
Question 4
Consider the problem of counting in a given text , the no. of substrings that start with 'a' and end with 'b'.Design a Brute Force algorithm for this problem and determine its efficiency class
Sunday, August 14, 2011
Subscribe to:
Posts (Atom)