Professional Documents
Culture Documents
c++;
s[r][c]='\0';
flag=1;
}
}
}
void display()
{
printf("\nLexemes are...\n");
for(k=0;k<r;k++)
{
printf("\n");
for (l=0;s[k][l]!='\0';l++)
{
printf("%c",s[k][l]);
}
}
}
void main()
{
clrscr();
printf("Enter any string : \n");
gets(a);
strcat(a,b);
lexems();
display();
getch();
}
Program 2 : Generates Tokens.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int i=0,j=0,k=0,l=0;
int flag=0,r=0,c=0;
char a[25],s[25][25];
char b[2]={' ','\0'};
void lexems()
{
for(i=0;i<=strlen(a);i++)
{
if(a[i]== '+' || a[i]== '-'|| a[i]== '*'|| a[i]== '/'|| a[i]=='='|| a[i]==';'|| a[i]==','|| a[i]=='<' || a[i]=='>' ||
a[i]=='('|| a[i]==')' )
{
if (flag==1)
{
r++;
c=0;
}
s[r][0]=a[i];
s[r][1]='\0';
c=0;
r++;
flag=0;
}
else if(a[i]==' ')
{
if (flag==1)
{
s[r][c]='\0';
r++;
c=0;
flag=0;
}
}
else
{
s[r][c]=a[i];
c++;
s[r][c]='\0';
flag=1;
}
}
}
void tokens()
{
printf("\n\n\n");
printf("\nLexemes:");
printf("\t\tTokens:\n");
for (j=0;j<r;j++)
{
printf("\n%s",s[j]);
if(strcmp(s[j],"int")==0||strcmp(s[j],"float")==0||strcmp(s[j],"char")==0||strcmp(s[j],"for")==0||
strcmp(s[j],"while")==0||strcmp(s[j],"double")==0||strcmp(s[j],"long")==0)
{
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class stack
{
char data[20];
int top;
public:
stack()
{
top=-1;
}
void push(char a)
{
top++;
data[top]=a;
}
char pop()
{
char ch1;
ch1=data[top];
data[top]='\0';
top--;
return ch1;
}
};
void main()
{
int x,y,xl,yl,rno,len,flag=0;
char rul[8][10];
stack s1;
char inp[20];
char ch='y',ip,ch1;
int asc,ind=0;
char nt[5];
char op[6];
clrscr();
nt[0]='E';
nt[1]='A';
op[0]='i';
op[1]='*';
strcpy(rul[0],"E=TA");
strcpy(rul[3],"T=FB");
nt[2]='T';
nt[3]='B';
op[2]='+';
op[3]='(';
strcpy(rul[1],"A=+TA");
strcpy(rul[4],"B=*FB");
nt[4]='F';
op[4]=')';
op[5]='$';
strcpy(rul[2],"A=");
strcpy(rul[5],"B=");
strcpy(rul[6],"F=(E)");
strcpy(rul[7],"F=i");
int tab[5][6];
tab[0][0]=0;
tab[2][0]=3;
tab[3][5]=5;
tab[0][3]=0;
tab[2][3]=3;
tab[4][0]=7;
tab[1][2]=1;
tab[3][1]=4;
tab[4][3]=6;
tab[1][4]=2;
tab[3][2]=5;
while(ch=='y')
{
ag: cout<<endl<<"Enter the input string(Should end with $):-";
gets(inp);
len=strlen(inp);
for(x=0;x<len;x++)
{
for(y=0;y<6;y++)
{
if(inp[x]!=op[y])
{
flag=1;
}
else
{
flag=0;
break;
}
}
if (flag==1)
{
break;
}
}
if (flag==0)
{
if(inp[len-1]!='$')
{
cout<<endl<<"The String should end with $"<<endl;
goto ag;
}
s1.push('$');
s1.push(rul[0][0]);
while(1)
{
ip=inp[ind];
ch=s1.pop();
asc=(int)ch;
tab[1][5]=2;
tab[3][4]=5;
break;
}
}
}
else
{
cout<<endl<<"Enter proper String"<<endl;
}
cout<<endl<<"Do You want to continue(y/n):";
cin>>ch;
}
}
Program 4 : Implement SLR / LR(0) Parser & check whether string is valid or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<stdlib.h>
class stack
{
public:
char data[20];
int top;
stack()
{
top=-1;
}
void push(char a)
{
top++;
data[top]=a;
}
char pop()
{
char ch1;
ch1=data[top];
data[top]='\0';
top--;
return ch1;
}
};
void main()
{
char rule[6][5],table[12][12],terminals[9];
stack s1;
char str[20];
clrscr();
strcpy(rule[0],"E=E+T"); //a
strcpy(rule[1],"E=T"); //b
strcpy(rule[2],"T=T*F"); //c
strcpy(rule[3],"T=F"); //d
strcpy(rule[4],"F=(E)"); //e
strcpy(rule[5],"F=i"); //f
terminals[0]='i'; terminals[1]='+'; terminals[2]='*'; terminals[3]='('; terminals[4]=')';
terminals[5]='$'; terminals[6]='E'; terminals[7]='T'; terminals[8]='F';
table[0][0]= '5';
table[0][3]= '4';
table[1][1]= '6';
table[2][1]= 'b';
table[2][2]= '7';
table[2][4]= 'b';
table[2][5]= 'b';
table[3][1]= 'd';
table[3][2]= 'd';
table[3][4]= 'd';
table[3][5]= 'd';
table[4][0]= '5';
table[4][3]= '4';
table[4][6]= '8';
table[4][7]= '2';
table[5][1]= 'f';
table[5][2]= 'f';
table[6][0]= '5';
table[6][3]= '4';
table[6][7]= '9';
table[7][0]= '5';
table[7][3]= '4';
table[8][1]= '6';
table[9][1]= 'a';
table[9][2]= '7';
table[10][1]= 'c';
table[0][6]= '1';
table[5][4]= 'f';
table[0][7]= '2';
table[0][8]= '3';
table[4][8]= '3';
table[5][5]= 'f';
table[9][4]= 'a';
table[6][8]= '3';
table[9][5]= 'a';
table[10][5]= 'c';
table[11][5]= 'e';
do
{
for(int i=0;i<9;i++)
{
if(terminals[i]==str[s])
{
x=i;
}
}
char ch=s1.pop();
s1.push(ch);
r=(int)ch;
table[r][x]=ac;
if((ac!='a')||(ac!='b')||(ac!='c')||(ac!='d')||(ac!='e')||(ac!='f')||(ac!='Z'))
{
s1.push(ac);
}
else if(ac=='f')
{
do
{
s++;
ac=s1.pop();
}while(s1.data[s1.top]=='i');
s1.push(rule[5][0]);
}
else if(ac=='e')
{
do
{
s++;
ac=s1.pop();
}while(s1.data[s1.top]==')');
s1.push(rule[4][0]);
}
else if(ac=='d')
{
do
{
s++;
ac=s1.pop();
}while(s1.data[s1.top]=='F');
s1.push(rule[3][0]);
}
else if(ac=='c')
{
do
{
s++;
ac= s1.pop();
}while(s1.data[s1.top]=='F');
s1.push(rule[2][0]);
}
else if(ac=='b')
{
do
{
s++;
ac=s1.pop();
}while(s1.data[s1.top]=='T');
s1.push(rule[1][0]);
}
else if(ac=='a')
{
do
{
s++;
ac= s1.pop();
}while(s1.data[s1.top]=='T');
s1.push(rule[0][0]);
}
for(i=0;i<strlen(str)-2;i++)
{
if((str[i]=='i') || (str[i]=='+') || (str[i]=='(')|| (str[i]==')'))
{
flag=0;
}
else
{
flag=1;
}
}
s++;
}while(str[s]!='^');
if(flag==1)
cout<<"Invalid string";
else
cout<<"Valid string";
getch();
}
Program 5 : Implement CLR / LR(1) Parser & check whether string is valid or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<stdlib.h>
class stack
{
public:
char data[20];
int top;
stack()
{
top=-1;
}
void push(char a)
{
top++;
data[top]=a;
}
char pop()
{
char ch1;
ch1=data[top];
data[top]='\0';
top--;
return ch1;
}
};
void main()
{
char rule[3][5],table[12][12],terminals[9];
stack s1;
char str[20];
clrscr();
strcpy(rule[0],"S=CC"); //a
strcpy(rule[1],"C=xC"); //b
strcpy(rule[2],"C=y"); //c
terminals[0]='x'; terminals[1]='y'; terminals[2]='$'; terminals[3]='S'; terminals[4]='C';
table[0][0]= '3'; table[0][1]= '4';
table[1][2]= 'Z'; //Z means Accept
table[0][3]= '1';
table[2][0]= '6';
table[2][4]= '8';
table[2][1]= '7';
table[0][4]= '2';
table[3][0]= '3';
table[4][0]= 'c';
table[5][2]= 'a';
table[6][0]= '6';
table[7][2]= 'c';
table[8][0]= 'b';
table[9][2]= 'b';
table[3][1]= '4';
table[4][1]= 'c';
table[3][4]= '8';
table[6][1]= '7';
table[6][4]= '9';
table[8][1]= 'b';
s1.push(0);
cout<<"Enter the string to be parsed : ";
cin>>str;
strcat(str,"^");
s1.push(str[0]);
int x;
int s=0,r=0;
char ac;
int flag=0;
do
{
for(int i=0;i<9;i++)
{
if(terminals[i]==str[s])
{
x=i;
}
}
char ch=s1.pop();
s1.push(ch);
r=(int)ch;
table[r][x]=ac;
if((ac!='a')||(ac!='b')||(ac!='c')||(ac!='Z'))
{
s1.push(ac);
}
else if(ac=='c')
{
do
{
s++;
ac= s1.pop();
}while(s1.data[s1.top]=='d');
s1.push(rule[2][0]);
}
else if(ac=='b')
{
do
{
s++;
ac=s1.pop();
}while(s1.data[s1.top]=='C');
s1.push(rule[1][0]);
}
else if(ac=='a')
{
do
{
s++;
ac= s1.pop();
}while(s1.data[s1.top]=='C');
s1.push(rule[0][0]);
}
for(i=0;i<strlen(str)-2;i++)
{
if((str[i]=='x') || (str[i]=='y'))
{
flag=0;
}
else
{
flag=1;
}
}
s++;
}while(str[s]!='^');
if(flag==1)
cout<<"Invalid string!!!";
else
cout<<"Valid string!!!";
getch();
}