These are the directions:
Recursive Descent Parsing
Consider the following BNF grammar:
E -> T + E | T - E | T
T -> P * T | P / T | P
P -> I | (E)
I -> a | b | ... | y | z
Using the technique described in class implement a recursive descent parser that recognizes strings in this language. Input should be from a file called input.dat and output should be to the console. An example session might look like this:
String read from file: a+b-c*d
The string "a+b-c*d" is in the language.
String read from file: a**b++c
The string "a**b++c" is not in the language.
You must implement the project in BOTH Java and C++! Implementations that do not include a solution in both languages will, at best, receive half credit. To simplify things you will not have to handle whitespace when parsing the string, i.e. " " and similiar are illegal characters in this language.
My output right now is saying everything is out of the language.
\My input data file is
a+b/c-d
a-b*c+d
1+a
a+1
aa
aa+1
a+-c
Please Dan Web help. I did a google search and found nothing to help me.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string c;
bool term();
bool paremeter();
bool identifier();
bool expression();
int place;
int main()
{
ifstream infile;
ofstream outfile;
infile.open("input.dat");
while (infile)
{
infile >> c;
place = 0;
if ( expression() )
{
cout <<"The sentence " << c << " is in the language" << endl << endl;
}//end of if
else
cout <<"The sentence " << c << " is not in the language" << endl<<endl;;
c = '0';
} //end of while
infile.close();
system("pause");
return 0;
} //end of main
/*
E -> T + E | T - E | T
T -> P * T | P / T | P
P -> I | (E)
I -> a | b | ... | y | z
*/
//E -> T + E | T - E | T
bool expression()
{
/* if (term() )
{if (c[place] == '+' || c[place] == '-' )
{place++; }
return true;
*/
if (term() )
{
if ( c[place] == '+' || c[place] == '-' )
{
if (term() )
{
return true; } } }
else
return false; }
//T -> P * T | P / T | P
bool term()
{
/* if (paremeter() )
{if (c[place] == '*' || c[place] == '/' )
{place++; }
return true;
}
*/
if (paremeter() )
{if (c[place] == '*' || c[place] == '/' )
{ if (paremeter() )
{place++; return true;
} } }
else
return false;
}
//P -> I | (E)
bool paremeter()
{
if (identifier() )
{ return true;}
if (c[place] == '(' )
{
place++;
if (expression() )
{
if (c[place] == ')' )
{ place++; return true;
}}}
return false;
}
//I -> a | b | ... | y | z
bool identifier()
{
if ( c[place] >= 'a' && c[place] <= 'z' )
{ place++; return true; }
return false;
}