I recently asked a question regarding writing a very basic English grammar parser. I thought it might be helpful to show some code (though very ugly) to specify where I need assistance in implementing the grammar. Here is the code. Comments indicate questions.
#include "std_lib_facilities.h"
string sentence();
string noun();
string verb();
string article()
{
string words = sentence(); // not sure how to check for words
if (words == "the")
words = sentence();
else return words;
}
string verb()
{
string words = sentence(); // not sure how to check for words
if (words == "rules" || words == "fly" || words == "swim")
words = sentence();
else return words;
}
string noun()
{
string words = sentence(); // not sure how to check for words
if (words == "birds" || words == "fish" || words == "C++")
words = sentence();
else return words;
}
string conjunction()
{
string words = sentence(); // not sure how to check for words
if (words == "and" || words == "but" || words == "or")
words = sentence();
else return words;
}
string sentence()
{
if (noun()){ // this fails to compile, not sure how to check this
// error message says could not convert noun to bool
if (verb())
cout << "OK.\n";
else cout << "Not OK.\n";}
else if (article()){
if (sentence()) // will this create a loop?
cout << "Ok.\n";
else cout << "Not Ok.\n";}
else if (conjunction()){
if (sentence()) // actually needs to be sentence conjunction sentence
cout << "Ok.\n";
else cout << "Not Ok.\n";
else cout << "Not OK.\n";
}
int main()
{
string words;
cout << "Enter sentence.\n";
while(cin >> words){
sentence();
}
keep_window_open(); // this function is part of the facilities library
}
The code is very incomplete, but is basically a framework for what needs to happen. The main problems are that I need to check if a function is true, and I need to know how to compare the user input to the called function (anything besides strcasecmp()? it hasn't been used in the book yet so there must be another way to it.) I'm also worried about there possibly being multiple outputs of "OK" or "Not OK", but I'll worry about that later.