#include<iostream>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
using namespace std;
class recursive
{
public:
char in[20];
int j,error;
void E();
void F();
};
main()
{ recursive obj;
obj.j=0;
obj.error=0;
cout<<"Enter an arithmetic expression : ";
cin>>obj.in;
obj.E();
if(obj.in[obj.j]=='$' && obj.in[obj.j+1]=='\0')
cout<<"\n String Accepted. . !!\n";
else
cout<<"\n String Rejected. . !!\n";
}
void recursive::E()
{
if(isalpha(in[j]))
{ j++;
F();
}
}
void recursive::F()
{ if(in[j]=='+' && in[j+1]!='$')
{ j++;
if(isalpha(in[j]))
{
j++;
F();
}
}
}
Explaination :-
Grammar - E -> iF
F -> +iF / ∈ ∈ ∈
i is any alphabet from a to z.
Accepted Strings - i+i$ , i+i+i$ , . .etc
Rejected Strings - i++, (i), ++i . . etc
In recursive descent parser, for every variable, we are gonna write a function. In function E(), if first symbol is alphabet then increment pointer to next location and call F().
In function F(), if first symbol is +(plus) then increment pointer to next location and if next symbol is again alphabet then increment pointer and call F().
#include<string.h>
#include<ctype.h>
#include<stdio.h>
using namespace std;
class recursive
{
public:
char in[20];
int j,error;
void E();
void F();
};
main()
{ recursive obj;
obj.j=0;
obj.error=0;
cout<<"Enter an arithmetic expression : ";
cin>>obj.in;
obj.E();
if(obj.in[obj.j]=='$' && obj.in[obj.j+1]=='\0')
cout<<"\n String Accepted. . !!\n";
else
cout<<"\n String Rejected. . !!\n";
}
void recursive::E()
{
if(isalpha(in[j]))
{ j++;
F();
}
}
void recursive::F()
{ if(in[j]=='+' && in[j+1]!='$')
{ j++;
if(isalpha(in[j]))
{
j++;
F();
}
}
}
Explaination :-
Grammar - E -> iF
F -> +iF /
i is any alphabet from a to z.
Accepted Strings - i+i$ , i+i+i$ , . .etc
Rejected Strings - i++, (i), ++i . . etc
In recursive descent parser, for every variable, we are gonna write a function. In function E(), if first symbol is alphabet then increment pointer to next location and call F().
In function F(), if first symbol is +(plus) then increment pointer to next location and if next symbol is again alphabet then increment pointer and call F().
No comments:
Post a Comment