Created
October 20, 2020 04:07
-
-
Save blacksmithop/018f7f3c68576d452a27e2b2b4ab9eac to your computer and use it in GitHub Desktop.
Recursive Descent Parser in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
char input[100]; | |
int i; | |
int T(); | |
int F(); | |
int TP(); | |
int EP(); | |
int E() | |
{ | |
if(T()) | |
{ | |
if(EP()) | |
return(1); | |
else return(0); | |
} | |
else | |
return(0); | |
} | |
int EP() | |
{ | |
if(input[i]=='+') | |
{ | |
i++; | |
if(T()) | |
{ | |
if(EP()) | |
return(1); | |
else | |
return(0); | |
} | |
else | |
return(0); | |
} | |
else | |
return(1); | |
} | |
int T() | |
{ | |
if(F()) | |
{ | |
if(TP()) | |
return(1); | |
else | |
return(0); | |
} | |
else | |
return(0); | |
} | |
int TP() | |
{ | |
if(input[i]=='*') | |
{ | |
i++; | |
if(F()) | |
{ | |
if(TP()) | |
return(1); | |
else | |
return(0); | |
} | |
else | |
return(0); | |
} | |
else | |
return(1); | |
} | |
int F() | |
{ | |
if(input[i]=='(') | |
{ | |
i++; | |
if(E()) | |
{ | |
if(input[i]==')') | |
{ | |
i++; | |
return(1); | |
} | |
else | |
return(0); | |
} | |
else | |
return(0); | |
} | |
else | |
if(input[i]>='a'&&input[i]<='z'||input[i]>='A'&&input[i]<='Z') | |
{ | |
i++; | |
return(1); | |
} | |
else | |
return(0); | |
} | |
//Main Program | |
void main(){ | |
printf("Recursive Decent Parser\n"); | |
printf("E->TE'\nE'->TE'/@\nT->FT'\nT'->*FT'/@\nF->(E)/ID\n"); | |
gets(input); | |
if (E()){ | |
if (input[i+1]=='\0'){ | |
printf("String is accepted\n"); | |
} | |
else{ | |
printf("String is not accepted\n"); | |
} | |
} | |
else{ | |
printf("String is not accepted\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment