You are on page 1of 14

LANGUAGE PROCESSOR

Program 1: Generate Lexemes.


#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 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)
{

printf("\t-------> Its a Keyword");


}
else if (strcmp(s[j],",")==0||strcmp(s[j],".")==0|| strcmp(s[j],";")==0||strcmp(s[j],"(")==0||
strcmp(s[j],")")==0||strcmp(s[j],"{")==0||strcmp(s[j],"}")==0||strcmp(s[j],"[")==0||
strcmp(s[j],"]")==0||strcmp(s[j],":")==0 )
{
printf("\t-------> Its a Delimiter");
}
else if (strcmp(s[j],"+")==0||strcmp(s[j],"-")==0|| strcmp(s[j],"*")==0||strcmp(s[j],"/")==0||
strcmp(s[j],"=")==0||strcmp(s[j],"!")==0||strcmp(s[j],"<")==0||strcmp(s[j],">")==0||strcmp(s[j],"+
+")==0||strcmp(s[j],"+=")==0||strcmp(s[j],"--")==0||strcmp(s[j],"-=")==0||strcmp(s[j],"**")==0||
strcmp(s[j],"*=")==0||strcmp(s[j],"//")==0||strcmp(s[j],"/=")==0||strcmp(s[j],"<<")==0||
strcmp(s[j],">>")==0||strcmp(s[j],"<=")==0||strcmp(s[j],">=")==0||strcmp(s[j],"!=")==0||
strcmp(s[j],"==")==0)
{
printf("\t-------> Its a Operator");
}
else if ((s[j][0]=='_') || (s[j][0]>='a' && s[j][0]<='z'))
{
printf("\t-------> Its a Identifier");
}
else if (s[j][0]>='0' && s[j][0]<='9')
{
printf("\t-------> Its a Number");
}
else
{
printf("\t-------> Its an Unknown ");
}
}
}
void main()
{
clrscr();
printf("Enter any string : ");
gets(a);
strcat(a,b);
lexems();
tokens();
getch();
}
Program 3 : Implement LL(1) Grammar & Check whether String is valid or not.

#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;

if(ch==ip && ch=='$' && ip=='$')


{
cout<<endl<<"Sucessful Search !!!"<<endl;
break;
}
else if(ch==ip && ch!='$' && ip!='$')
{
ind++;
ip=inp[ind];
}
else if(ch!=ip)
{
if(asc>64 && asc<91)
{
for(x=0;x<5;x++)
{
if (ch==nt[x])
{
xl=x;
}
}
for(x=0;x<6;x++)
{
if (ip==op[x])
{
yl=x;
}
}
rno=tab[xl][yl];
len=strlen(rul[rno]);
for(y=len-1;y>=0;y--)
{
ch1=rul[rno][y];
if(ch1!='=')
{
s1.push(ch1);
}
else
{
break;
}
}
}
}
else
{

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[1][5]= 'Z'; //Z means Accept

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[7][8]= 'Y';//Y means 10

table[8][1]= '6';

table[8][4]= 'X';//X means 11

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][2]= 'c'; table[10][4]= 'c';

table[10][5]= 'c';

table[11][1]= 'e'; table[11][2]= 'e'; table[11][4]= 'e';


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;

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();
}

You might also like