0% found this document useful (0 votes)
18 views19 pages

Experiment 8 To 10

The document contains three separate experiments related to parsing techniques in programming. Experiment 8 focuses on simulating the First and Follow sets of a given grammar, Experiment 9 constructs a recursive descent parser for expressions, and Experiment 10 implements a Shift Reduce parser for a specified language. Each experiment includes code snippets and explanations of the parsing process.

Uploaded by

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

Experiment 8 To 10

The document contains three separate experiments related to parsing techniques in programming. Experiment 8 focuses on simulating the First and Follow sets of a given grammar, Experiment 9 constructs a recursive descent parser for expressions, and Experiment 10 implements a Shift Reduce parser for a specified language. Each experiment includes code snippets and explanations of the parsing process.

Uploaded by

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

EXPERIMENT NO-8

Aim: Write program to find Simulate First and Follow of any given grammar.

Program:

#include<stdio.h>

#include<ctype.h>

#include<string.h>

void followfirst(char, int, int);

void follow(char c);

void findfirst(char, int, int);

int count, n = 0;

char calc_first[10][100];

char calc_follow[10][100];

int m = 0;

char production[10][10];

char f[10], first[10];

int k;

char ck;

int e;

int main(int argc, char **argv)

int jm = 0;

int km = 0;

int i, choice;

char c, ch;

count = 8;

strcpy(production[0], "E=TR");
strcpy(production[1], "R=+TR");

strcpy(production[2], "R=#");

strcpy(production[3], "T=FY");

strcpy(production[4], "Y=*FY");

strcpy(production[5], "Y=#");

strcpy(production[6], "F=(E)");

strcpy(production[7], "F=i");

int kay;

char done[count];

int ptr = -1;

for(k = 0; k < count; k++) {

for(kay = 0; kay < 100; kay++) {

calc_first[k][kay] = '!';

int point1 = 0, point2, xxx;

for(k = 0; k < count; k++)

c = production[k][0];

point2 = 0;

xxx = 0;

for(kay = 0; kay <= ptr; kay++)

if(c == done[kay])

xxx = 1;

if (xxx == 1)

continue;
findfirst(c, 0, 0);

ptr += 1;

done[ptr] = c;

printf("\n First(%c) = { ", c);

calc_first[point1][point2++] = c;

for(i = 0 + jm; i < n; i++) {

int lark = 0, chk = 0;

for(lark = 0; lark < point2; lark++)

if (first[i] == calc_first[point1][lark])

chk = 1;

break;

if(chk == 0)

printf("%c, ", first[i]);

calc_first[point1][point2++] = first[i];

printf("}\n");

jm = n;

point1++;

printf("\n");
printf("-----------------------------------------------\n\n");

char donee[count];

ptr = -1;

for(k = 0; k < count; k++) {

for(kay = 0; kay < 100; kay++) {

calc_follow[k][kay] = '!';

point1 = 0;

int land = 0;

for(e = 0; e < count; e++)

ck = production[e][0];

point2 = 0;

xxx = 0;

for(kay = 0; kay <= ptr; kay++)

if(ck == donee[kay])

xxx = 1;

if (xxx == 1)

continue;

land += 1;

follow(ck);

ptr += 1;

donee[ptr] = ck;

printf(" Follow(%c) = { ", ck);

calc_follow[point1][point2++] = ck;
for(i = 0 + km; i < m; i++) {

int lark = 0, chk = 0;

for(lark = 0; lark < point2; lark++)

if (f[i] == calc_follow[point1][lark])

chk = 1;

break;

if(chk == 0)

printf("%c, ", f[i]);

calc_follow[point1][point2++] = f[i];

printf(" }\n\n");

km = m;

point1++;

void follow(char c)

int i, j;

if(production[0][0] == c) {

f[m++] = '$';
}

for(i = 0; i < 10; i++)

for(j = 2;j < 10; j++)

if(production[i][j] == c)

if(production[i][j+1] != '\0')

followfirst(production[i][j+1], i, (j+2));

if(production[i][j+1]=='\0'&& c!=production[i][0])

follow(production[i][0]);

void findfirst(char c, int q1, int q2)

int j;

if(!(isupper(c))) {

first[n++] = c;
}

for(j = 0; j < count; j++)

if(production[j][0] == c)

if(production[j][2] == '#')

if(production[q1][q2] == '\0')

first[n++] = '#';

else if(production[q1][q2] != '\0'

&& (q1 != 0 || q2 != 0))

findfirst(production[q1][q2], q1, (q2+1));

else

first[n++] = '#';

else if(!isupper(production[j][2]))

first[n++] = production[j][2];

else

findfirst(production[j][2], j, 3);

}
}

void followfirst(char c, int c1, int c2)

int k;

if(!(isupper(c)))

f[m++] = c;

else

int i = 0, j = 1;

for(i = 0; i < count; i++)

if(calc_first[i][0] == c)

break;

while(calc_first[i][j] != '!')

if(calc_first[i][j] != '#')

f[m++] = calc_first[i][j];

else

if(production[c1][c2] == '\0')

{
follow(production[c1][0]);

else

followfirst(production[c1][c2], c1, c2+1);

j++;

Output:
EXPERIMENT NO-9

Aim: Construct a recursive descent parser for an expression.

Program:

#include<stdio.h>

#include<string.h>

int E(),Edash(),T(),Tdash(),F();

char *ip;

char string[50];

int main()

printf("Enter the string\n");

scanf("%s",string);

ip=string;

printf("\n\nInput\tAction\n-------------------------------- \n");

if(E() && ip=="\0"){

printf("\n--------------------------------\n");

printf("\n String is successfully parsed\n");

else{

printf("\n--------------------------------\n");

printf("Error in parsing String\n");

int E()

printf("%s\tE->TE' \n",ip);
if(T())

if(Edash())

return 1;

else

return 0;

else

return 0;

int Edash()

if(*ip=='+')

printf("%s\tE'->+TE' \n",ip);

ip++;

if(T())

if(Edash())

return 1;

else

return 0;
}

else

return 0;

else

printf("%s\tE'->^ \n",ip);

return 1;

int T()

printf("%s\tT->FT' \n",ip);

if(F())

if(Tdash())

return 1;

else

return 0;

else

return 0;

int Tdash()
{

if(*ip=='*')

printf("%s\tT'->*FT' \n",ip);

ip++;

if(F())

if(Tdash())

return 1;

else

return 0;

else

return 0;

else

printf("%s\tT'->^ \n",ip);

return 1;

int F()

if(*ip=='(')
{

printf("%s\tF->(E) \n",ip);

ip++;

if(E())

if(*ip==')')

ip++;

return 0;

else

return 0;

else

return 0;

else if(*ip=='i')

ip++;

printf("%s\tF->id \n",ip);

return 1;

else

return 0;

}
Output:
EXPERIMENT NO-10

Aim:Construct a Shift Reduce Parser for a given. Language.

Program:

#include<stdio.h>

#include<conio.h>

#include<string.h>

int k=0,z=0,i=0,j=0,c=0;

char a[16],ac[20],stk[15],act[10];

void check();

int main()

puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id");

puts("enter input string ");

gets(a);

c=strlen(a);

strcpy(act,"SHIFT->");

puts("stack \t input \t action");

for(k=0,i=0; j<c; k++,i++,j++)

if(a[j]=='i'&& a[j+1]=='d')

stk[i]=a[j];

stk[i+1]=a[j+1];

stk[i+2]='\0';

a[j]='';

a[j+1]='';
printf("\n$%s\t%s$\t%sid",stk,a,act);

check();

else

stk[i]=a[j];

stk[i+1]='\0';

a[j]='';

printf("\n$%s\t%s$\t%ssymbols",stk,a,act);

check();

getch();

void check()

strcpy(ac,"REDUCE TO E");

for(z=0; z<c; z++)

if(stk[z]=='i'&& stk[z+1]=='d')

stk[z]='E';

stk[z+1]='\0';

printf("\n$%s\t%s$\t%s",stk,a,ac);

j++;

for(z=0; z<c; z++)


if(stk[z]=='E'&& stk[z+1]=='+'&& stk[z+2]=='E')

stk[z]='E';

stk[z+1]='\0';

stk[z+2]='\0';

printf("\n$%s\t%s$\t%s",stk,a,ac);

i=i-2;

for(z=0; z<c; z++)

if(stk[z]=='E'&& stk[z+1]=='*'&& stk[z+2]=='E')

stk[z]='E';

stk[z+1]='\0';

stk[z+1]='\0';

printf("\n$%s\t%s$\t%s",stk,a,ac);

i=i-2;

for(z=0; z<c; z++)

if(stk[z]=='('&& stk[z+1]=='E'&& stk[z+2]==')')

stk[z]='E';

stk[z+1]='\0';

stk[z+1]='\0';

printf("\n$%s\t%s$\t%s",stk,a,ac);

i=i-2;

}
}

Output:

You might also like