Monday, April 16, 2012

end sem lab exam java declaration parser solution

end sem lab exam java declaration parser solution

lp lab manual

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
{
}


*/