Search This Blog

Friday, 7 October 2016

recursive_descent_parsing

#include<stdio.h>
#include<string.h>
char in[50];
int j=0,f=0;
void PE1();
void PT();
void PT1();
void PF();
void match(char c);
void PE()
{
//printf("Enter PE\n");
PT();
PE1();
printf("E->TE1\n");
}
void PE1()
{
//printf("Enter PE1\n");
char c=in[j];
if(c=='+')
{
match('+');
PT();
PE1();
}
else
printf("E1->NULL\n");
printf("E1->+TE1 | ^\n");
}
void PT()
{
//printf("Enter PT\n");
PF();
PT1();
printf("T->FT1\n");
}
void PT1()
{
//printf("Enter PT1\n");
int f=0;
char c=in[j];
if(c=='*')
{
match('*');
PF();
PT1();
}
else
printf("T1->NULL\n");
printf("T1->*FT1 | ^\n");
}
void PF()
{
//printf("Enter PF\n");
char c=in[j];
if(c=='(')
{
match('(');
PE();
match(')');
}
else if((c>='a' && c<='z')|| (c>='A' && c<='Z'))
match(c);
printf("F->(E)|i\n");
}
void match(char c)
{
if(j<=strlen(in))
{
if(in[j]==c)
{
printf("matched %c\n",c);
j++;
}
else
printf("Error\n");
}
}
int main()
{
scanf("%s",in);
PE();
if(in[j]=='\0')
printf("correct\n");
else
printf("Syntax error\nUnidentified symbol %c\n",in[j]);
return 0;
}


- Jinay Shah

No comments:

Post a Comment