0% found this document useful (0 votes)
69 views29 pages

CD Lab Final (1-10)

This document contains code for implementing a predictive parser in C. It defines functions for pushing and popping items to/from a stack, converting integer items to characters, and defines the parsing table as a 2D array. It gets an input string from the user, converts each character to an integer code, and initializes the stack and input pointer. The main loop pops the top stack item, looks it up in the table with the current input item, and performs the action like shifting or reducing until the entire input is parsed or an error is encountered. It prints the stack and input at each step for debugging.

Uploaded by

Manu123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views29 pages

CD Lab Final (1-10)

This document contains code for implementing a predictive parser in C. It defines functions for pushing and popping items to/from a stack, converting integer items to characters, and defines the parsing table as a 2D array. It gets an input string from the user, converts each character to an integer code, and initializes the stack and input pointer. The main loop pops the top stack item, looks it up in the table with the current input item, and performs the action like shifting or reducing until the entire input is parsed or an error is encountered. It prints the stack and input at each step for debugging.

Uploaded by

Manu123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

/*LEXICAL ANALYZER*/

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char user_tokens[20][20],ch;
int i,k,n,temp;
char per_keys[32][10]={"auto","break","case","char","const","continue","default","do","double",
"else","enum","extern","float","for","goto","if","int","long","register","return","short","signed",
"sizeof","static","struct","typedef","union","unsigned","void","volatile","while"};
printf("\n enter the key:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n enter the token %d ",i);
scanf("%s",&user_tokens[i]);
}
printf("total tokens are %d",i-1);
for(i=1;i<=n;i++)
{
for(k=0;k<=32;k++)
{
temp=0;
if(strcmp(user_tokens[i],per_keys[k])==0)
{
printf("\n\n%s is a keyword",user_tokens[i]);
temp=1;
break;
}
}
if(temp)
continue;
k=user_tokens[i][0];
if(k>=48&&k<=57)
{
printf("\n\n %s is a digit",user_tokens[i]);
continue;
}
if(k==37||k==42||k==43||k==45||k==47)
{
printf("\n\n %s is an operator",user_tokens[i]);
continue;
}
if(k>=65&&k<=90||k>=97&&k<=122)
{
printf("\n\n %s is an identifier",user_tokens[i]);
continue;
}
else
{
printf("\n\n %s is a special character",user_tokens[i]);
continue;
}
}
printf("\n\n are you sure to continue[Y/N]");
scanf("%s",&ch);
if(ch=='y'||ch=='Y')
main();
else
getch();
}

OUTPUT:
/*FIRST OF GRAMMAR*/
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
main()
{
char p[10][10],n[10],first,v[16],temp[16];
int i=0,j,pro,a=0;
printf("\n enter no of productions:");
scanf("%d",&pro);
for(i=0;i<pro;i++)
{
printf("\n enter the left hand side:");
getchar();
scanf("%c",&n[i]);
printf("\n enter the right hand side:");
scanf("%s",&p[i]);
}
for(i=0;i<pro;i++)
{
printf("\n %c->%s\n",n[i],p[i]);
}
for(i=0;i<pro;i++)
{
first=n[i];
printf("\n FIRST(%c)={",first);
while(first!='$')
{
if(isupper(first))
{
for(j=0;n[j]!='\0';j++)
{
if(first==n[j])
{
if(isupper(p[j][0]))
{
v[a]=first;
first=p[j][0];
}
else
{
temp[j]=p[j][0];
printf("%c",temp[j]);
}
}
}
}
else if(temp[i]=='\0')
temp[j]=p[j][0];
first='$';
printf("}\n");
}
}
getch();
}
OUTPUT:
/*FOLLOW OF GRAMMAR*/
PROGRAM:
#include<stdio.h> 
#include<string.h>
int n,m=0,p,i=0,j=0;
char a[10][10],followResult[10];
void follow(char c);
void first(char c);
void addToResult(char);
int main()
{
int i;
int choice;
char c,ch;
printf("Enter the no.of productions: ");
scanf("%d", &n);
printf(" Enter %d productions\nProduction with multiple terms should be give as separate
productions \n", n);
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
// gets(a[i]);
do
{
m=0;
printf("Find FOLLOW of -->");
scanf(" %c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",followResult[i]);
printf(" }\n");
printf("Do you want to continue(Press 1 to continue....)?");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
}
void follow(char c)
{
if(a[0][0]==c)addToResult('$');
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')
first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
}
}
}
}
void first(char c)
{
int k;
if(!(isupper(c)))
//f[m++]=c;
addToResult(c);
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$') follow(a[i][0]);
else if(islower(a[k][2]))
//f[m++]=a[k][2];
addToResult(a[k][2]);
else first(a[k][2]);
}
}
}
void addToResult(char c)
{
int i;
for( i=0;i<=m;i++)
if(followResult[i]==c)
return;
followResult[m++]=c;
}
OUTPUT:
/*OPERATOR PRECEDENCE*/
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int scan(int);
int number(char);
int findingL();
int findingG();
int erase(char);
char opers[6]={'i','+','*','$','/','-'},input[50];
char table[6]
[6]={'=','>','>','>','>','>','<','>','<','>','>','>','<','>','>','>','>','>','<','<','<','=','<','<','<','>','<','>','<','>','
<','>'};
int scan(int position)
{
int i;
for(i=strlen(input);i>=position;i--)
input[i]=input[i-1];
return i;
}
int number(char operator)
{
int i;
for(i=0;i<sizeof(opers);i++)
if(opers[i]==operator)
return i;
return -1;
}
int findingG()
{
int i;
for(i=0;i<strlen(input);i++)
if(input[i]=='>')
return i;
return -1;
}
int findingL(int position)
{
int i;
for(i=position;i>0;i--)
if(input[i]=='<')
return i;
return -1;
}
int erase(char ch)
{
int i,j;
for(i=0;i<strlen(input);i++)
if(input[i]==ch)
for(j=i;j<strlen(input);j++)
input[j]=input[j+1];
return -1;
}
main()
{
int i,G,L;
printf("\n *** operator precedence parsing ***\n\n");
printf("enter the input:");
scanf("%s",&input);
for(i=1;i<strlen(input);i+=2)
{
scan(i);
input[i]=table[number(input[i])][number(input[i+1])];
}
printf("\n the parsed output is");
while(strcmp(input,"$$"))
{
G=findingG();
L=findingL(G);
input[L]='x';
input[L+1]=table[number(input[L-1])][number(input[G+1])];
input[G]='x';
erase('x');
erase('=');
printf("\n\n next stage:\n");
printf("\n %s",input);
}
getch();
}
OUTPUT:

/*LL(1) GRAMMAR*/
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int A();
void disp();
void error();
char s[20];
int i;
main()
{
printf("S->cAd\n");
printf("\n A->ab/a\n");
printf("\n Enter the string:\n");
scanf("%s",&s);
i=0;
if(s[i++]=='c'&&A())
{
if(s[++i]=='d'&& s[i+1]==NULL)
disp();
else
error();
}
else
error();
}
int A()
{
if(s[i++]=='a'&&s[i]=='b')
return(1);
else if(s[--i]=='a')
return(1);
else
return(0);
}
void disp()
{
printf("\n string is valid \n");
getch();
exit(0);
}
void error()
{
printf("\n string is invalid \n");
getch();
exit(0);
}

Output:
/*SHIFT REDUCE PARSER */
PROGRAM:
#include<stdio.h>
#include<stdlib.h
#include<conio.h>
#include<string.h>
char ip_sym[15],stack[15];
int ip_ptr=0,st_ptr=0,len,i;
char temp[2],temp2[2];
char act[15];
void check();
main()
{
printf("\n\t\t SHIFT REDUCE PARSER\n");
printf("\nGRAMMAR\n");
printf("\n E->E+E\n E->E/E");
printf("\n E->E*E\n E->a/b");
printf("\n enter the input symbol:\t");
gets(ip_sym);
printf("\n\t stack implementation table");
printf("\nstack \t\tinput symbol\t\taction");
printf("\n-----\t\t-----\t\t----\n");
printf("\n $\t\t%s$\t\t\t---",ip_sym);
strcpy(act,"shift");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
len=strlen(ip_sym);
for(i=0;i<=len-1;i++)
{
stack[st_ptr]=ip_sym[ip_ptr];
stack[st_ptr+1]='\0';
ip_sym[ip_ptr]=' ';
ip_ptr++;
printf("\n$%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
strcpy(act,"shift");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
check();
st_ptr++;
}
st_ptr++;
check();
}
void check()
{
int flag=0;
temp2[0]=stack[st_ptr];
temp2[1]='\0';
if((!strcmpi(temp2,"a"))||(!strcmpi(temp2,"b")))
{
stack[st_ptr]='E';
if(!strcmpi(temp2,"a"))
printf("\n$%s\t\t%s$\t\t\tE->a",stack,ip_sym);
else
printf("\n$%s\t\t%s$\t\t\tE->b",stack,ip_sym);
flag=1;
}
if((!strcmpi(temp2,"+"))||(strcmpi(temp2,"*"))||(!strcmpi(temp2,"/")))
{
flag=1;
}
if((!strcmpi(stack,"E+E"))||(!strcmpi(stack,"E/E"))||(!strcmpi(stack,"E*E")))
{
strcpy(stack,"E");
st_ptr=0;
if(!strcmpi(stack,"E+E"))
printf("\n$%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
else
if(!strcmpi(stack,"E/E"))
printf("\n$%s\t\t%s$\t\t\tE->E/E",stack,ip_sym);
else
printf("\n$%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
flag=1;
}
if(!strcmpi(stack,"E")&&ip_ptr==len)
{
printf("\n$%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
getch();
exit(0);
}
if(flag==0)
{
printf("\n$%s\t\t%s$\t\t\t REJECT",stack,ip_sym);
exit(0);
}
return;
}

Output:
/* RECURSIVE DESCENT PARSER*/
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int n,i=0;
char s[20];
void E(void);
void E1(void);
void T(void);
void T1(void);
void F();
void error(void);
main()
{
printf("\n the given grammar is:");
printf("\n E->TE1");
printf("\n E1->+TE1/#");
printf("\n T->FT1");
printf("\n T1->*FT1/#");
printf("\n F->(E)/d");
printf("\n enter length of string:");
scanf("%d",&n);
printf("\n enter the string u want to parse:");
scanf("%s",&s);
E();
if(s[i]==0)
printf("\n string is valid");
else
printf("\n string is invalid");
getch();
}
void E()
{
if(i<=n)
{
T();
E1();
}
}
void E1()
{
if(i<=n)
{
if(s[i]=='+')
{
i++;
T();
E1();
}
else if(s[i]=='#')
i++;
else;
}
else
error();
}
void T()
{
if(i<=n)
{
F();
T1();
}
}
void T1()
{
if(i<=n)
{
if(s[i]=='+')
{
i++;
F();
T1();
}
else if(s[i]=='#')
i++;
else;
}
else
error();
}
void F()
{
if(i<=n)
{
if(s[i]=='('||s[i]==')')
{
i++;
E();
}
else if(s[i]=='d')
i++;
else;
}
else
error();
}
void error()
{
printf("\n valid");
getch();
exit(0);
}

Output:
/* PREDICTIVE PARSER */
PROGRAM:
#include<stdio.h>
int stack[20],top=-1;
void push(int item)
{
if(top>=20)
{
printf("STACK OVERFLOW");
exit(1);
}
stack[++top]=item;
}
int pop()
{
int ch;
if(top<=-1)
{
printf("underflow");
exit(1);
}
ch=stack[top--];
return ch;
}
char convert(int item)
{
char ch;
switch(item)
{
case 0:return('E');
case 1:return('e');
case 2:return('T');
case 3:return('t');
case 4:return('F');
case 5:return('i');
case 6:return('+');
case 7:return('*');
case 8:return('(');
case 9:return(')');
case 10:return('$');
}
}
void main()
{
int m[10][10],i,j,k;
char ips[20];
int ip[10],a,b,t;
m[0][0]=m[0][3]=21;
m[1][1]=621;
m[1][4]=m[1][5]=-2;
m[2][0]=m[2][3]=43;
m[3][1]=m[3][4]=m[3][5]=-2;
m[3][2]=743;
m[4][0]=5;
m[4][3]=809;
printf("\n enter the input string:");
scanf("%s",ips);
for(i=0;ips[i];i++)
{
switch(ips[i])
{
case 'E':k=0;break;
case 'e':k=1;break;
case 'T':k=2;break;
case 't':k=3;break;
case 'F':k=4;break;
case 'i':k=5;break;
case '+':k=6;break;
case '*':k=7;break;
case '(':k=8;break;
case ')':k=9;break;
case '$':k=10;break;
}
ip[i]=k;
}
ip[i]=-1;
push(10);
push(0);
i=0;
printf("\tstack\t input \n");
while(1)
{
printf("\t");
for(j=0;j<=top;j++)
printf("%c",convert(stack[j]));
printf("\t\t");
for(k=i;ip[k]!=-1;k++)
printf("%c",convert(ip[k]));
printf("\n");
if(stack[top]==ip[i])

{
if(ip[i]==10)
{
printf("\t\t SUCCESS");
return;
}
else
{
top--;
i++;
}
}
else if(stack[top]<=4&&stack[top]>=0)
{
a=stack[top];
b=ip[i]-5;
t=m[a][b];
top--;
while(t>0)
{
push(t%10);
t=t/10;
}
}
else
{
printf("ERROR");
return;
}
}
getch();
}

OUTPUT:
/* LALR PARSER*/
PROGRAM:
#include<stdio.h>
int st[20],top=-1;
char input[20];
int encode(char ch)
{
switch(ch)
{
case 'i':return 0;
case '+':return 1;
case '*':return 2;
case '(':return 3;
case ')':return 4;
case '$':return 5;
case 'E':return 6;
case 'T':return 7;
case 'F':return 8;
}
return -1;
}
char decode(int n)
{
switch(n)
{
case 0:return('i');
case 1:return('+');
case 2:return('*');
case 3:return('(');
case 4:return(')');
case 5:return('$');
case 6:return('E');
case 7:return('T');
case 8:return('F');
}
return 'z';
}
void push(int n)
{
st[++top]=n;
}
int pop()
{
return(st[top--]);
}
void display(int p,char *ptr)
{
int l;
for(l=0;l<=top;l++)
{
if(l%2==1)
printf("%c",decode(st[l]));
else
printf("%d",st[l]);
}
printf("\t");
for(l=p;ptr[l];l++)
printf("%c",ptr[l]);
printf("\n");
}
void main()
{
char t1[20][20],pr[20][20],xy;
int inp[20],t2[20][20],gt[20][20];
int i,k,x,y,tx=0,ty=0,len;
clrscr();
strcpy(pr[1],"E E+T");
strcpy(pr[2],"E T");
strcpy(pr[3],"T T*F");
strcpy(pr[4],"T F");
strcpy(pr[5],"F (E)");
strcpy(pr[6],"F i");
t2[2][1]=t2[2][4]=t2[2][5]=2;
t2[3][1]=t2[3][2]=t2[3][4]=t2[3][5]=4;
t2[5][1]=t2[5][2]=t2[5][4]=t2[5][5]=6;
t2[9][1]=t2[9][4]=t2[9][5]=1;
t2[10][1]=t2[10][2]=t2[10][4]=t2[10][5]=3;
t2[11][2]=t2[11][1]=t2[11][4]=t2[11][5]=5;
t1[2][1]=t1[2][4]=t1[2][5]='r';
t1[3][1]=t1[3][2]=t1[3][4]='r';
t1[3][5]=t1[5][1]=t1[5][2]='r';
t1[5][4]=t1[5][5]=t1[9][1]=t1[9][4]='r';
t1[9][5]=t1[10][1]=t1[10][2]=t1[10][4]=t1[10][5]='r';
t1[11][1]=t1[11][4]=t1[11][2]=t1[11][5]='r';
t1[0][0]=t1[4][0]=t1[6][0]=t1[7][0]=t1[0][3]=t1[4][3]=t1[6][3]='s';
t1[2][2]=t1[9][2]=t1[8][4]=t1[1][1]=t1[8][1]=t1[7][3]='s';
t1[1][5]='a';
t2[0][0]=t2[4][0]=t2[6][0]=t2[7][0]=5;
t2[0][3]=t2[4][3]=t2[6][3]=t2[7][3]=4;
t2[2][2]=t2[9][2]=7;
t2[8][4]=11;
t2[1][1]=t2[8][1]=6;
gt[0][6]=1;
gt[0][7]=gt[4][7]=2;
gt[0][8]=gt[4][8]=gt[6][8]=3;
gt[4][6]=8;gt[6][7]=9;gt[7][8]=10;
printf("enter string:");
scanf("%s",input);
for(k=0;input[k];k++)
{
inp[k]=encode(input[k]);
if(input[k]<0||inp[k]>5)
printf("\n error in input");
}
push(0);
i=0;
while(1)
{
x=st[top];y=inp[i];
display(i,input);
if(t1[x][y]=='a')
{
printf("string is accepted \n");
exit(0);
}
else if(t1[x][y]=='s')
{
push(inp[i]) ;
push(t2[x][y]);
i++;
}
else if(t1[x][y]=='r')
{
len=strlen(pr[t2[x][y]])-2;
xy=pr[t2[x][y]][0];
ty=encode(xy);
for(k=1;k<=2*len;k++)
pop();
tx=st[top];
push(ty);
push(gt[tx][ty]);
}
else
printf("\n error in parsing");
}
getch();
}
OUTPUT:
/*CONSTANT PROPAGATION*/

PROGRAM:

#include<stdio.h>
#include<math.h>
#include<conio.h>
main()
{
printf("\n \n \n value of c using copy propagation is :%d",copypropagation());
copypropagation();
}
copypropagation()
{
int a = 30;
int b = 9-(a/5);
int c;
c = b*4;
if (c>10)
{
c=c+18;
}
getch();
return c*(60/a);
}
OUTPUT:

You might also like