Write A C Program To Simulate Lexical Analyzer To Validating A Given Input String.
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;
OUTPUT:
#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.
#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