Aim: Write down program to implement the
predictive parsing algorithm.
#include<stdio.h>
#include<conio.h>
#include<String.h>
void match();
void simple(char t[50][50]);
void type(char t[50][50]);
char t[50][50];
int
l=0,m=0,la=0,flag=0;
void main()
{
char str[100];
int i,j,k;
clrscr();
printf("\n============Pridictive
Parsing===============\n");
printf("\n\n\tProduction::");
printf("\n\n\ttype -> simple
| ^id | array [ simple ] of type");
printf("\n\n\tsimple ->
integer | char | num dotdot num");
printf("\n\n\n\tEnter the
string ===> ");
gets(str);
for(i=0;str[i]!=NULL;i++)
{
if(str[i]!=' ')
{
t[l][m]=str[i];
m++;
}
else
{
t[l][m]=NULL;
l++;
m=0;
}
}
t[l][m]=NULL;
for(i=0;i<=l;i++)
{
if(strcmp(t[i],"array")==0 || strcmp(t[i],"^id")==0)
{
type(t);
}
else if(strcmp(t[i],"integer")==0
|| strcmp(t[i],"char")==0 || strcmp(t[i],"num")==0)
{
simple(t);
}
else
{
break;
}
}
printf("\n\n\n");
if(la==l+1 && flag==0)
{
printf("\n\n\tGrammar MATCHED....Congrats!!!!");
}
else
{
printf("\n\n\tGrammar
UNMATCHED.....Try again:-(");
}
getch();
}
void type(char t[50][50])
{
if((strcmp(t[la],"integer")==0) || (strcmp(t[la],"char")==0) || (strcmp(t[la],"num")==0))
{
simple(t);
}
if(strcmp(t[la],"array")==0)
{
match("array");
match("[");
simple(t);
match("]");
match("of");
type(t);
}
if(strcmp(t[la],"^id")==0)
{
match("^id");
}
}
void match(char str[20])
{
if(strcmp(t[la],str)==0)
{
la++;
}
else
{
flag=1;
}
}
void simple(char t[50][50])
{
if(strcmp(t[la],"integer")==0)
{
match("integer");
}
else if(strcmp(t[la],"char")==0)
{
match("char");
}
else if(strcmp(t[la],"num")==0)
{
match("num");
match("dotdot");
match("num");
}
}
No comments:
Post a Comment