Write a grammars program

I'm in a beginning programming class and I'm really struggling with understanding how to write this grammars program. It's supposed to translate english statements into grammar.

The program is supposed to make sure (1) all statements end in a period, (2) all if-then statements start with if and end with then, (3) "and" and "or" group together smaller phrases, (4) The words "If" "then" "and" "or" and not" will not be part of any valid phrase, with the exception that a "not" might be included, and (5) there should be a precedence of options: "not" takes precedence over "and", which takes precedence over an "or" which takes precedence of "If...then..." So the statement "It is dry or it is not raining and it is cloudy." would be read as "(It is dry) or ((it is (not) raining) and (it is cloudy))"

It should also prompt the user for input/output filenames, read data from the input file, and write parsing results to the output file.

For example if the input is "If it is raining then it is cloudy." then the output should be "A -> B". Or if the input is "It is cloudy and I am dry." then the output would be "! A"

What i want to do is use "!" to indicated "not, "&&" for "and" "||" for "or, and "->" for an if-then statement. For each statement the output is supposed to be a boolean expression corresponding to the input. & each predicate should be assigned a letter and capitalization should be ignored.

I'm just so confused on even where to start with this. Any help or ideas on how to get started would be really appreciated. I have no experience with programming and I'm having a lot of trouble with this. Thank you so much for taking the time to read this.
give us something to start with and we will most likely take over the whole topic with our posts and arguments about how to do it better. All you gotta do is start us off. I recommend starting with this:
1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    return 0;
}
Getting proper grammar is a pain in the butt, and it is an endless task actually. This is just the beginning of the grammar library I have in my current program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
inline std::string capitalize (std::string& word) {
	word[0] = std::toupper(word[0]);
	return word;
}

inline bool isVowel (char letter) {
	static const std::string vowels = "aeiou";
	return vowels.find(letter) != std::string::npos;
}

inline std::string aAn (const std::string& word, Capitalized capital) {  // capital = NonCapital by default
	std::string result = isVowel(word[0]) ? "an" : "a";
	return (capital == NonCapital) ? result : capitalize(result);
}

inline std::string aAn (const Entity* entity, Capitalized capital) {  // capital = NonCapital by default
	return aAn (entity->getName(), capital);
}

inline std::string numToWord (int num, Capitalized capital) {  // capital = NonCapital by default
	std::string result;
	switch (num) {
		case 1  : result = "one";  break;
		case 2  : result = "two";  break;
	 	case 3  : result = "three";  break;
	 	case 4  : result = "four";  break;
	 	case 5  : result = "five";  break;
	 	case 6  : result = "six";  break;
	 	case 7  : result = "seven";  break;
	 	case 8  : result = "eight";  break;
	 	case 9  : result = "nine";  break;
	 	case 10 : result = "ten";  break;
		case 11 : result = "eleven";  break;
	 	case 12 : result = "twelve";  break;
		default : return toString(num);
	}
	return (capital == NonCapital) ? result : capitalize(result);
}

inline std::string ordinal (int num, Capitalized capital) {  // capital = NonCapital by default
	std::string result;
	switch (num) {
		case 1  : result = "first";  break;
		case 2  : result = "second";  break;
	 	case 3  : result = "third";  break;
	 	case 4  : result = "fourth";  break;
	 	case 5  : result = "fifth";  break;
	 	case 6  : result = "sixth";  break;
	 	case 7  : result = "seventh";  break;
	 	case 8  : result = "eighth";  break;
	 	case 9  : result = "ninth";  break;
	 	case 10 : result = "tenth";  break;
		case 11 : result = "eleventh";  break;
	 	case 12 : result = "twelfth";  break;
		default : result = toString(num) + "th";
	}
	return (capital == NonCapital) ? result : capitalize(result);
}

inline std::string firstWord (const std::string& str) {
	std::string word;
	std::stringstream ss(str);
	ss >> word;
	return word;
}

inline std::string plural (const std::string& word) {
	switch (word[word.length()-1]) {
		case 'x': case 'h': case 's':  return word + "es";
		case 'y': {
			const std::size_t secondLast = word.length() - 2;
			const char secondLastLetter = word.at(secondLast);
			if (isVowel (secondLastLetter))
				return word + "s";
			return word.substr (0, secondLast) + "ies";
		}
		default: return word + "s";
	}
}

template <typename T>  // T = int by default
inline std::string plural (T num, const std::string& word) {
	return (num == 1) ? word : plural(word);
}

template <typename T>  // T = int by default
inline std::string numObjects (T num, const std::string& object, NumberType numberType) {  // numberType = NumAsNum by default
	return (numberType == NumAsNum) ? toString(num) + " " + plural(num, object) : numToWord(num) + " " + plural(num, object);
}

inline std::string heShe (const LivingBeing* being, Capitalized capital) {  // capital = NonCapital by default
	std::string result = ((being->getGender() == Male) ? "he" : (being->getGender() == Female) ? "she" : "it");
	return (capital == NonCapital) ? result : capitalize(result);
}

inline std::string heShe (const LivingBeingProxy* being, Capitalized capital) {  // capital = NonCapital by default
	return heShe (being->getActual(), capital);
}

inline std::string hisHer (const LivingBeing* being, Capitalized capital) {  // capital = NonCapital by default
	std::string result = ((being->getGender() == Male) ? "his" : (being->getGender() == Female) ? "her" : "its");
	return (capital == NonCapital) ? result : capitalize(result);
}

inline std::string hisHer (const LivingBeingProxy* being, Capitalized capital) {  // capital = NonCapital by default
	return hisHer (being->getActual(), capital);
}

inline std::string himHer (const LivingBeing* being) {
	return (being->getGender() == Male) ? "him" : (being->getGender() == Female) ? "her" : "it";
}

inline std::string himHer (const LivingBeingProxy* being) {
	return himHer (being->getActual());
}

inline std::string hisHers (const LivingBeing* being) {
	return (being->getGender() == Male) ? "his" : (being->getGender() == Female) ? "hers" : "it";
}

inline std::string hisHers (const LivingBeingProxy* being) {
	return hisHers (being->getActual());
}
well then... this ought to be fun...
Topic archived. No new replies allowed.