0% found this document useful (0 votes)
986 views

Write A C Program To Simulate Lexical Analyzer To Validating A Given Input String.

Uploaded by

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

Write A C Program To Simulate Lexical Analyzer To Validating A Given Input String.

Uploaded by

StephenKarunakar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

3. Write a C program to Simulate Lexical Analyzer to validating a given input String.

#include<ctype.h>
#include<stdio.h>
#include<string.h>
void main()
{
char s[20],c;
int state=0,i=0;
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();
}

OUTPUT:

Enter a String: aaaabbbbb


aaaabbbbb is accepted under rule 'a*b+'

Enter a string: cdgs


cdgs is not recognized
4. Write a C program to implement the Brute force technique of Top down Parsing.

#include<ctype.h>
#include<stdio.h>
#include<string.h>

char s[10],*p;
void s1();
main()
{
printf("Enter the string:");
gets(s);
p=s;
s1();
}
void s1()
{
int a();
if(*p=='c')
{
p++;
if(a()==0)
{
if(*p=='d')
{
p++;
if(*p=='\0')
printf("string parsed successfully.\n");
}
else
printf("sting not parsed.\n");
}
else
printf("string not parsed.\n");
}
else
printf("string not paresd.\n ");
}
int a()
{
char *i;
i=p;
if(*p=='a')
{
p++;
if(*p=='b')
{
p++;
return 0;
}
}
p=i;
if(*p=='a')
{
p++;
return 0;
}
}

OUTPUT:
Enter the string:cad
string parsed successfully.

Enter the string:cabd


string parsed successfully.

Enter the string:cbd


string not parsed.
5. Write a C program to implement a Recursive Descent Parser.

#include<ctype.h>
#include<stdio.h>
#include<string.h>
char input[100];
int i,l;
void main()
{
printf("\nRecursive Descent Parsing for the following Grammar:\n");
printf("E->TE'\nE'->+TE'|@\nT->FT'\nT'->*FT'|@\nF->(E)|id\n");
printf("Enter the string to be checked:");
gets(input);
if(E())
{
if(input[i+1]=='\0')
printf("\nString is accepted");
else
printf("\nString is not accepted");
}
else
printf("\nString not accepted");
//getch();
}
E()
{
if(T())
{
if(EP())
return(1);
else
return(0);
}
else
return(0);
}

EP()
{
if(input[i]=='+')
{
i++;
if(T())
{
if(EP())
return(1);
else
return(0);
}
else
return(0);
}
else
return(1);
}
T()
{
if(F())
{
if(TP())
return(1);
else
return(0);
}
else
return(0);
}

TP()
{
if(input[i]=='*')
{
i++;

if(F())
{
if(TP())
return(1);
else
return(0);
}
else
return(0);
}
else
return(1);
}
F()
{
if(input[i]=='(')
{
i++;
if(E())
{
if(input[i]==')')
{
i++;
return(1);
}
else
return(0);
}
else
return(0);
}
else if(input[i]=='a')
{
i++;
return(1);
}
else
return(0);
}

OUTPUT:
Recursive Descent Parsing for the following Grammar:
E->TE'
E'->+TE'|@
T->FT'
T'->*FT'|@
F->(E)|id

Enter the string to be checked:a+a


String is accepted

Enter the string to be checked:a*a+a


String is accepted

Enter the string to be checked:a*+a


String not accepted

You might also like