of 2

Recursive Descent Parser

Published on February 2017 | Categories: Documents | Downloads: 6 | Comments: 0
45 views

Comments

Content


// IMPLEMENT RECURSIVE DESCENT PARSER FOR THE FOLLOWING GRAMMAR
// E -> E+T | T
// T -> T*F | F
// F -> (E) | id
// After eliminating left recursion, the grammar will become
// E->TE'
// E'->+TE'|e
// T->FT'
// T'->*FT'|e
// F->(E)|id
#include<stdio.h>
#include<conio.h>
#include<string.h>
char str[20];
int i;
void E();void T();void F();
void EPrime();void TPrime();
void error();
void main()
{
clrscr();
printf("Enter the string :");
scanf("%s",str);
E();
if(str[i]=='\0')
printf("\n String Accepted");
else
printf("\n String Rejected");
getch();
}
void E()
{
T();
EPrime();
}
void T()
{
F();
TPrime();
}
void F()
{
if(isalpha(str[i]))
i++;
else if(str[i]=='(')
{
i++;
E();
if(str[i]==')')
i++;
else
{
error();
}
}
else
error();
}
void EPrime()
{
if(str[i]=='+')
{
i++;
T();
EPrime();
}
}
void TPrime()
{
if(str[i]=='*')
{
i++;
F();
TPrime();
}
}
void error()
{
printf("\n String Rejected");
getch();
exit(0);
}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close