Google Search

Wednesday, October 5, 2011

compiler design lab manual programs


#include<stdio.h>
#include<conio.h>
int n,i=0;
char s[20];
void E(void);
void E1(void);
void T(void);
void T1(void);
void F(void);
void error(void);
void main()
{
clrscr();
printf("The given grammar is\n");
printf("E -> TE1\n");
printf("E1 -> +TE1/#\n");
printf("T -> FT1\n");
printf("T1 -> *FT1/#\nF -> (E)/d\n");
printf("Enter the string you want to parse:\n");
scanf("%s",&s);
E();
if(s[i]==NULL)
printf("string is valid\n");
else
printf("string is invalid\n");
getch();
}
void E()
{
T();
E1();
}
void E1()
{
if(s[i]=='+')
{
i++;
T();
E1();
}
else if(s[i]=='#')
i++;
else
error();
}
void T()
{
F();
T1();
}
void T1()
{
if(s[i]=='*')
{
i++;
F();
T1();
}
else if(s[i]=='#')
i++;
else
error();
}
void F()
{
if(s[i]=='(')
{
i++;
E();
if(s[i]==')')
i++;
else
error();
}
else if(s[i]=='d')
i++;
else
error();
}
void error()
{
printf("string is invalid\n");
getch();
exit(0);
}

0 comments:

Post a Comment