Saturday, November 3, 2007

First Parser

Program :



#include
#include
#include
#include

void main() //main function
{
char gramar[10][10];
int ch=1,n=0;
char temp[10];
char first[23];
int m;
int j=0,i=0,k=3,flag=0;

char small[30]={'a','b','c','d','e','f','g','h',
'i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x',
'y','z','*','+','/','-','(',')'}; //check terminals

int p,h=1;
char sa[10];
char pa[10];
char temp1[10];
clrscr();

do
{
printf("\nenter grammar");
scanf("%s",gramar[n]); //get the grammar
printf("\ncontinue( 0 or 1 )");
scanf("\n%d",&ch);
n++;
}


while(ch==1);

printf("\n\nTHE ENTERED GRAMMAR IS");
for(i=0;iprintf("\n%s",gramar[i]);
do
{
printf("\nenter the grammar symbol whose first has to be found");
scanf("%s",sa); //accept symbol
label:

for(i=0;i{
strcpy(temp,gramar[i]);
if(sa[0]==temp[0])
{
strcpy(temp1,temp); //copy to temp1
break;
}
}

k=2;
j=0;

for(i=0;i<35;i++) //check if terminal
{
if(temp1[2]==small[i])
{
flag=1;
first[j]=temp1[k];
}
}
j++;

while(k{
if(temp1[k]=='|')
{
for(i=0;i<35;i++) //check if terminal
{
if(temp1[k+1]==small[i])
{
flag=1;
first[j]=temp1[k+1];
}
}
j++;
goto l1;
}
k++;
}
l1:
if(flag==0) //if no terminal is found
{
sa[0]=temp1[2];
goto label;
}
first[j]='\0';
printf("\n { ");
for(i=0;i{
if(i+1==strlen(first))
printf("%c",first[i]);
else
printf("%c , ",first[i]);
}
printf(" } ");
printf("\n do u want to continue ?? yes(1) or no(0) ");
scanf("\n%d",&h);
}
while(h!=0);

getch();
}

0 comments: