Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
438 views

Compiler Lab

This C program recognizes strings based on three grammar rules: 'a*', 'a*b+', and 'abb'. It uses a state machine approach with states 0-6 to check each character of the input string against the rules. It prints whether the string is recognized or not. A second problem performs shift-reduce parsing on the input string "32423" based on grammar rules: E->2E2, E->3E3, E->4. It initializes stacks and strings, shifts characters, calls a check function to reduce if possible, and prints the stack and action at each step. If the final stack is "E", it accepts the string, otherwise it rejects it.

Uploaded by

Raksha Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
438 views

Compiler Lab

This C program recognizes strings based on three grammar rules: 'a*', 'a*b+', and 'abb'. It uses a state machine approach with states 0-6 to check each character of the input string against the rules. It prints whether the string is recognized or not. A second problem performs shift-reduce parsing on the input string "32423" based on grammar rules: E->2E2, E->3E3, E->4. It initializes stacks and strings, shifts characters, calls a check function to reduce if possible, and prints the stack and action at each step. If the final stack is "E", it accepts the string, otherwise it rejects it.

Uploaded by

Raksha Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Problem: Write a C program to recognize strings under 'a*', 'a*b+', 'abb'

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char s[20],c;
int state=0,i=0;
clrscr();
printf("\n Enter a string:");
gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else if(c=='b')
state=2;
else
state=6;
break;
case 1: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;
case 2: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;

else if(c=='b')
state=5;
else
state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not recognised.",s);
exit(0);
}
}
If(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}

Use-cases:
INPUT & OUTPUT:
Input :

1. Enter a String: aaaabbbbb //2 Marks output: aaaabbbbb is accepted under rule 'a*b+'
2. Enter a string: cdgs // 2 Marks output: cdgs is not recognized
3. Enter a string: aaaaa // 2 Marks output: aaaaa is accepted under rule ‘a’
4. Enter a string: aabaaa // 2 Marks output: aabaaa is not recognized
5. Enter a string: aaaaac // 2 Marks output: aaaaac is not recognized
Problem-2:
Consider the grammar
        E –> 2E2
        E –> 3E3
        E –> 4
Perform Shift Reduce parsing for input string “32423”.
//Including Libraries
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//Global Variables
int z = 0, i = 0, j = 0, c = 0;

// Modify array size to increase


// length of string to be parsed
char a[16], ac[20], stk[15], act[10];

// This Function will check whether


// the stack contain a production rule
// which is to be Reduce.
// Rules can be E->2E2 , E->3E3 , E->4
void check()
{
// Coping string to be printed as action
strcpy(ac,"REDUCE TO E -> ");

// c=length of input string


for(z = 0; z < c; z++)
{
//checking for producing rule E->4
if(stk[z] == '4')
{
printf("%s4", ac);
stk[z] = 'E';
stk[z + 1] = '\0';

//pinting action
printf("\n$%s\t%s$\t", stk, a);
}
}

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


{
//checking for another production
if(stk[z] == '2' && stk[z + 1] == 'E' &&
stk[z + 2] == '2')
{
printf("%s2E2", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}

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


{
//checking for E->3E3
if(stk[z] == '3' && stk[z + 1] == 'E' &&
stk[z + 2] == '3')
{
printf("%s3E3", ac);
stk[z]='E';
stk[z + 1]='\0';
stk[z + 1]='\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}
}
return ; //return to main
}

//Driver Function
int main()
{
printf("GRAMMAR is -\nE->2E2 \nE->3E3 \nE->4\n");

// a is input string
strcpy(a,"32423");

// strlen(a) will return the length of a to c


c=strlen(a);

// "SHIFT" is copied to act to be printed


strcpy(act,"SHIFT");

// This will print Lables (column name)


printf("\nstack \t input \t action");

// This will print the initial


// values of stack and input
printf("\n$\t%s$\t", a);

// This will Run upto length of input string


for(i = 0; j < c; i++, j++)
{
// Printing action
printf("%s", act);

// Pushing into stack


stk[i] = a[j];
stk[i + 1] = '\0';
// Moving the pointer
a[j]=' ';

// Printing action
printf("\n$%s\t%s$\t", stk, a);

// Call check function ..which will


// check the stack whether its contain
// any production or not
check();
}

// Rechecking last time if contain


// any valid production then it will
// replace otherwise invalid
check();

// if top of the stack is E(starting symbol)


// then it will accept the input
if(stk[0] == 'E' && stk[1] == '\0')
printf("Accept\n");
else //else reject
printf("Reject\n");
}

You might also like