Showing posts with label Compiler. Show all posts
Showing posts with label Compiler. Show all posts

Oct 9, 2009

Operator precedence parsing algorithm


//This is operator precedence parsing algorithm using C

char pre[10][10],ss[30],rs[30],stk[10][2];
int i,j,k,n,p,sp=0,br=0;
void push();
void pop();
char chk_pre();
void reduce_ss();
void main()
{
clrscr();
strcpy(pre[0]," +*()]");
strcpy(pre[1],"+><<>>");
strcpy(pre[2],"*>><>>");
strcpy(pre[3],"(<<<=e");
strcpy(pre[4],")>>e>>");
strcpy(pre[5],"[<<<e=");
n=6;
scanf ("%s",ss, printf("Enter the source string : "));
strcpy(rs,"[");
strcat(rs,ss);
strcat(rs,"]");
strcpy(ss,rs);
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<strlen(pre[i]);j++)
printf("%c\t",pre[i][j]);
printf("\n");}
while(ss[2]!=']' && br==0)
{
sp=0;
for(i=0;i<strlen(ss);i++)
{
if( (ss[i]>='a' && ss[i]<='z') || (ss[i]>='A' && ss[i]<='Z') )
continue;
push();
if(stk[sp-1][1]=='e' || ss[1]==']' || ss[0]==']')
{ printf("\n\nInvalid string.");
br=1;
break; }
if((stk[sp-1][1]=='>' && stk[sp-2][1]=='<') || stk[sp-1][1]=='=')
{ reduce_ss();
pop(); } } }
if(ss[2]==']')
printf("\nValid String.");
getch();
}
void push()
{ stk[sp][0]=ss[i];
stk[sp][1]=chk_pre();
sp++; }
void pop()
{ char tmp,tmp1;
sp--;
tmp=stk[sp][0];
tmp1=stk[sp][1];
sp--;
stk[sp][0]=tmp;
stk[sp][1]=tmp1;
sp++; }
char chk_pre()
{ char tmp;
if(i==0)
tmp=' ';
else
{ for(j=0;j<n;j++)
for(k=0;k<strlen(pre[j]);k++)
if(stk[sp-1][0]==pre[j][0] && stk[sp][0]==pre[0][k])
tmp=(pre[j][k]); }
return tmp; }
void reduce_ss()
{  char t1[30],t3[30];
int tmp;
if(stk[sp-1][0]==')' && stk[sp-2][0]=='(')
tmp=i-2;
else
tmp=i-3;
for(j=0;j<tmp;j++)
{  t1[j]=ss[j];  }
t1[j]='\0';
tmp=tmp+3;
k=0;
for(j=tmp;j<strlen(ss);j++)
{  t3[k]=ss[j];
k++;  }
t3[k]='\0';
strcpy(ss,"");
strcpy(ss,t1);
strcat(ss,"i");
strcat(ss,t3);
}

Sep 10, 2009

system programming-quardple


void main()
{
char str[50],b,c,op[20],op1[20],op2[20];
int a=49,i,j,k=1,opi=0,op1i=0,op2i=0;
clrscr();
printf("enter string\n");
gets(str);
while(strlen(str)>=3)
{
i=0;
while(1)
{
if(str[i]>=33&&str[i]<=47)
{
op1[op1i++]=str[i-2];
str[i-2]=a++;
op[opi++]=str[i];
op2[op2i++]=str[i-1];
for(k=i;k<=strlen(str);k++)
str[k]=str[k+1];
str[k]=NULL;
for(k=i-1;k<=strlen(str);k++)
str[k]=str[k+1];
str[k]=NULL;
break;
}
else
{
i++;
}
}
}
op[opi]=NULL;
op1[op1i]=NULL;
op2[op2i]=NULL;
printf("\nTriple no. operator operator1 oprator2\n");
for(i=0;i<strlen(op);i++)
printf("%d\t\t%c\t%c\t%c\n",i+1,op[i],op1[i],op2[i]);
getch();
}

lexical analysis

//copyright this program is made by Jayesh Amin....
//This program is used to perform lexical Analysis....


//function for checking duplicate values

int check(char a[],char x,int z)
{
int y;
for(y=0;y<z;y++)
{
if(a[y]==x)
return 1;
}
return 0;
}

//function for printing lexical analysis

int print(char a[],char x1)
{
int q=0;
while(a[q]!=x1)
{
q++;
}
return q;
}

//main program starts

void main()
{

//variable declaration

int no,i,p,opi=0,keyi=0,idi=0,keyj=0,idp,opp,j,c,keyflag=1;
char stat[10][30],key[50],op[50],id[50],temp;
clrscr();

/* entering statements */

printf("\nHow many statements you want to enter???\n");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("\nenter statement%d\n",i+1);
scanf("%s",stat[i]);
}
clrscr();
printf("\nyour statements are:\n\n");
for(i=0;i<no;i++)
{
printf("%s\n",stat[i]);
}
printf("\n");

//logic of the program starts

printf("your answer of lexical analysis is:\n\n");
for(i=0;i<no;i++)/*loop for no. of statements*/
{
for(j=0;j<strlen(stat[i]);j++)/*loop for statement length*/
{
/*if 1st char is identifier and after that ':' is there then do this*/
if((stat[i][j]>=97 && stat[i][j]<=122) && (stat[i][j+1]==':'))
{

if(check(id,temp,idi)==0)
{
id[idi]=stat[i][j];
idi++;
}
temp=stat[i][j];
idp=print(id,temp);
printf("(id#%d)",idp+1);
j++;
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
j++;
while(stat[i][j]!=';')
{
key[keyi]=stat[i][j];
keyi++;
j++;
}
key[keyi]=' ';
keyi++;
printf("(key#%d)",keyflag);
keyflag++;
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
break;
}

/*if 1st char is identifier and after that ',' is there then do this*/

else if((stat[i][j]>=97 && stat[i][j]<=122) && (stat[i][j+1]==','))
{
if(check(id,temp,idi)==0)
{
id[idi]=stat[i][j];
idi++;
}
temp=stat[i][j];
idp=print(id,temp);
printf("(id#%d)",idp+1);
j++;
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
}

/*if 1st char is identifier and after that '=' is there then do this*/

else if((stat[i][j]>=97 && stat[i][j]<=122) && (stat[i][j+1]=='='))
{
temp=stat[i][j];
idp=print(id,temp);
printf("(id#%d)",idp+1);
j++;
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
j++;
while(stat[i][j]!=';')
{
c=(int)stat[i][j];
if((c>=48 && c<=57)||(c>=65 && c<=90)||(c>=97 && c<=122))
{
temp=c;
idp=print(id,temp);
printf("(id#%d)",idp+1);
}
else
{
temp=stat[i][j];
if(check(op,temp,opi)==0)
{
op[opi]=stat[i][j];
opi++;
}
opp=print(op,temp);
printf("(op#%d)",opp+1);
}
j++;
}
temp=stat[i][j];
opp=print(op,temp);
printf("(op#%d)",opp+1);
break;
}
else
{
}
}
printf("\n");
}

op[opi]=NULL;
key[keyi]=NULL;
id[idi]=NULL;

/* To print identifiers,operators and keywords seperately */

printf("\nidentifiers are--->%s\n",id);
printf("\noperators are----->%s\n",op);
printf("\nkeywords are------>%s\n",key);
getch();
}


system programming-teminal and non terminal


void main()
{
/* variable declaration */

int no,i,p,m=0,n=0,j,flag;
char prod[10][20],ts[50],nts[50],c;
ts[0]=NULL,nts[0]=NULL;
clrscr();

/* entering production */

printf("\nhow many production you want to enter\n");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("\nenter production%d\n",i+1);
scanf("%s",prod[i]);
}
for(i=0;i<no;i++)/*loop for no. of productions*/
{
for(j=0;j<strlen(prod[i]);j++)/*loop for production length*/
{
c=prod[i][j];
if(c>=97 && c<=122)/*check for terminal symbols*/
{
for(p=m;p>=0;p--)
{
if(ts[p]==c)
{
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
{
ts[m]=c;
m++;
}
}
else if(c>=65 && c<=90)/*check for non terminal symbols*/
{
for(p=n;p>=0;p--)
{
if(nts[p]==c)
{
flag=1;
break;
}
else
flag=0;
}
if(flag==0)
{
nts[n]=c;
n++;
}
}
else
{
}
}
ts[m]=NULL;
nts[n]=NULL;
}

/*display final result*/

printf("\nterminl symbols are %s\n",ts);
printf("\nnon terminal symbols are %s",nts);
getch();
}