Using the syntax of C, write a recursive descent subprogram that corresponds to
ID: 3824796 • Letter: U
Question
Using the syntax of C, write a recursive descent subprogram that corresponds to the following EBNF production (taken from the specification of Java):
<local_variable_declaration_statement>
[ final ] <type> <variable_declarator> { , <variable_declarator> } ;
{, }, [, and ] are metasymbols. Assume that the token codes for final, the comma, and the semicolon are FINAL_CODE, COMMA_CODE, and SEMICOLON_CODE, respectively. Also assume that recursive-descent subprograms named type and variable_declarator already exist
Explanation / Answer
include <stringdio.h>
#include <conio.h>
char a[100];
char b[100][100];
int place=-1,l,string=-1;
char c,d;
void E();
void T();
void F();
void advance();
void Td();
void Ed();
void advance()
{
place++;
if(place<l)
{
if(a[place]>='0'&& a[place]<='9')
{
d=a[place];
c='';
}
if((a[place]>='a' || a[place]>='A')&&(a[place]<='z' || a[place]<='Z'))
{c=a[place];
d='';
}
}
}
void E()
{
strcpy(b[++string],"E->TE'");
T();
Ed();
}
void Ed()
{
int p=1;
if(a[place]=='+')
{
p=0;
strcpy(b[++string],"E'->+TE'");
advance();
T();
Ed();
}
if(a[place]=='-')
{ p=0;
strcpy(b[++string],"E'->-TE'");
advance();
T();
Ed();
}
// Recursive Descent Parser
if(p==1)
{
strcpy(b[++string],"E'->null");
}
}
void T()
{
strcpy(b[++string],"T->FT'");
F();
Td();
}
void Td()
{
int p=1;
if(a[place]=='*')
{
p=0;
strcpy(b[++string],"T'->*FT'");
advance();
F();
Td();
}
if(a[place]=='/')
{ p=0;
strcpy(b[++string],"T'->/FT'");
advance();
F();
Td();
}
if(p==1)
strcpy(b[++string],"T'->null");
}
void F()
{
if(a[place]==c) {
strcpy(b[++string],"F->c");
advance(); }
if(a[place]=='(')
{
strcpy(b[++string],"F->(E)");
advance();
E();
if(a[place]==')') {
//strcpy(b[++string],"F->(E)");
advance(); }
}
if(a[place]==d)
{
strcpy(b[++string],"F->d");
advance();
}
}
int main()
{
int i;
printf("Enter a stringring ");
scanf("%s",a);
l=stringrlen(a);
a[l]='$';
advance();
E();
if(place==l)
{
printf("stringring Accepted ");
for(i=0;i<=string;i++)
{
printf("%s ",b[i]);
}
}
else
{
printf("stringring rejected ");
}
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.