Implementation of Recursive Descent Parser Algorithm Source Code C Programming

CS342 Compiler Lab Source code for Recursive Descent Parser
#include<stdio.h>
#include<conio.h>
char ip_sym[15],ip_ptr=0;
void e_prime();
void e();
void t();
void t_prime();
void f();
void advance();
void e()
{
printf("\n\t\tE->TE'");
t();
e_prime();
}
void e_prime()
{
if(ip_sym[ip_ptr]=='+')
{
printf("\n\t\tE'->+TE'");
advance();
t();
e_prime();
}
else
printf("\n\t\tE'->e");
}
void t()
{
printf("\n\t\tT->FT'");
f();
t_prime();
}
void t_prime()
{
if(ip_sym[ip_ptr]=='*')
{
printf("\n\t\tT'->*FT'");
advance();
f();
t_prime();
}
else
printf("\n\t\tT'->e");
}
void f()
{
if((ip_sym[ip_ptr]=='i')||(ip_sym[ip_ptr]=='I'))
{
printf("\n\t\tF->i");
advance();
}
else
{
if(ip_sym[ip_ptr]=='(')
{
printf("\n\t\tF->(E)");
advance();
e();
if(ip_sym[ip_ptr]==')')
{
advance();
}
}
else
{
printf("\n\t\tSYNTAX ERROR");
getch();
exit(1);
}
}
}
void advance()
{
ip_ptr++;
}
void main()
{
int i;
clrscr();
printf("\n\t GRAMMAR WITHOUT LEFT RECURSION");
printf("\n\t\tE->TE'\n\t\tE'->+TE'|e\n\t\tT->FT'");
printf("\n\t\tT'->*FT'|e\n\t\tF->(E)|i");
printf("\n Enter the input expression:\n");
gets(ip_sym);
printf("\n Sequence of production rules:");
e();
for(i=0;i<strlen(ip_sym);i++)
{
if(ip_sym[i]!='+'&&ip_sym[i]!='*'&&ip_sym[i]!='('&&ip_sym[i]!=')'&&
ip_sym[i]!='i'&& ip_sym[i]!='I')
{
printf("\n SYNTAX ERROR");
break;
}
}
getch();
}
This is the Source Code C Programming for Recursive Descent Parser

Post a Comment

0 Comments