-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
331 lines (289 loc) · 11.1 KB
/
parser.cpp
File metadata and controls
331 lines (289 loc) · 11.1 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <iostream>
#include <fstream>
#include <string>
#include "scanner.cpp"
typedef tokentype token_type;
using namespace std;
/* INSTRUCTION: Complete all ** parts.
You may use any method to connect this file to scanner.cpp
that you had written.
e.g. You can copy scanner.cpp here by:
cp ../ScannerFiles/scanner.cpp .
and then append the two files into one:
cat scanner.cpp parser.cpp > myparser.cpp
*/
//=================================================
// File parser.cpp written by Group Number: 12
//=================================================
// ----- Four Utility Functions and Globals -----------------------------------
// ifstream fin; // Removed duplicate definition
string saved_lexeme;
token_type saved_token;
bool token_available = false;
ofstream errorFile("errors.txt"); // Global file stream for error messages
bool tracing = true; // Set to true to enable tracing, false to disable
// ** Need syntaxerror1 and syntaxerror2 functions (each takes 2 args)
// to display syntax error messages as specified by me.
void syntaxerror1(token_type expected, string lexeme);
void syntaxerror2(string function_name, string lexeme);
// ** Need the updated match and next_token with 2 global vars
// saved_token and saved_lexeme
token_type next_token();
// Type of error: Expected
// Done by: Eduardo Rocha and Alexander Nicholas
void syntaxerror1(token_type expected, string lexeme)
{
// Construct the error message
string errorMessage = "SYNTAX ERROR: Expected " + tokenName[expected] + " but found " + lexeme + "\n";
// Print and log the error
cout << errorMessage;
if (errorFile.is_open())
errorFile << errorMessage;
exit(1); // Exit on syntax error
}
// Type of error: Unexpected
// Done by: Eduardo Rocha and Alexander Nicholas
void syntaxerror2(string function_name, string lexeme)
{
// Construct the error message
string errorMessage = "SYNTAX ERROR: Unexpected " + lexeme + " found in " + function_name + "\n";
// Print to the console
cout << errorMessage;
// Write to the error file
if (errorFile.is_open())
{
errorFile << errorMessage;
}
exit(1); // Exit on syntax error
}
// ** Need the updated match and next_token with 2 global vars
// saved_token and saved_lexeme
// Purpose: Will grab and update any new tokens and continue to do so sequentially.
// Done by: Eduardo Rocha
token_type next_token()
{
if (!token_available)
{ // if there is no saved token yet
scanner(saved_token, saved_lexeme); // call scanner to grab a new token
token_available = true; // mark the fact that you have saved it
}
return saved_token; // return the saved token
}
// Purpose: Will track any matched token and save them.
// Done by: Eduardo Rocha
bool match(tokentype expected)
{
if (next_token() != expected)
{ // mismatch has occurred with the next token
syntaxerror1(expected, saved_lexeme); // call syntax error function
return false;
}
else
{ // match has occurred
cout << "Matched " << tokenName[expected] << endl; // trace the matched token
token_available = false; // eat up the token
return true;
}
}
// ----- RDP functions - one per non-term -------------------
// ** Make each non-terminal into a function here
// ** Be sure to put the corresponding grammar rule above each function
// ** Be sure to put the name of the programmer above each function
void story();
void s();
void noun();
void verb();
void be();
void tense();
// Grammar: <story> ::= <s> { <s> }
// Done by: Alexander Nicholas
void story()
{
if (tracing)
cout << "Processing <story>" << endl;
s(); // process one <s>
while (next_token() == CONNECTOR || next_token() == WORD1 || next_token() == PRONOUN)
{
s(); // process additional <s> as long as start of <s> is seen
}
}
// Grammar rule: <s> ::= [CONNECTOR] <noun> SUBJECT <verb> <tense> PERIOD
// Done by: Eduardo Rocha
void s()
{
if (tracing)
cout << "Processing <s>" << endl; // Print tracing message if tracing is enabled
if (next_token() == CONNECTOR)
{
match(CONNECTOR); // Match optional CONNECTOR if the next token is CONNECTOR
}
noun(); // Process <noun>
match(SUBJECT); // Match SUBJECT token
cout << "Processing <afterSubject>" << endl;
if (next_token() == WORD2)
{
verb(); // Process <verb> if the next token is WORD2
tense(); // Process <tense>
match(PERIOD); // Match PERIOD token
}
else if (next_token() == WORD1 || next_token() == PRONOUN)
{
noun(); // Process <noun> if the next token is WORD1 or PRONOUN
cout << "Processing <afterNoun>" << endl;
if (next_token() == IS || next_token() == WAS)
{
be(); // Process <be> if the next token is IS or WAS
match(PERIOD); // Match PERIOD token
}
else if (next_token() == DESTINATION)
{
match(DESTINATION); // Match DESTINATION token
verb(); // Process <verb>
tense(); // Process <tense>
match(PERIOD); // Match PERIOD token
}
else if (next_token() == OBJECT)
{
match(OBJECT); // Match OBJECT token
if (next_token() == WORD2)
{
verb(); // Process <verb> if the next token is WORD2
tense(); // Process <tense>
match(PERIOD); // Match PERIOD token
}
else if (next_token() == WORD1 || next_token() == PRONOUN)
{
noun(); // Process <noun> if the next token is WORD1 or PRONOUN
match(DESTINATION); // Match DESTINATION token
verb(); // Process <verb>
tense(); // Process <tense>
match(PERIOD); // Match PERIOD token
}
}
else
{
syntaxerror2("afterNoun", saved_lexeme); // Catch unexpected tokens
}
}
else
{
syntaxerror2("afterSubject", saved_lexeme); // Catch unexpected tokens
}
}
// Grammar rule: <noun> ::= WORD1 | PRONOUN
// Done by: Minh Tran
void noun()
{
if (tracing)
cout << "Processing <noun>" << endl; // Print tracing message if tracing is enabled
if (next_token() == WORD1)
{
match(WORD1); // Match WORD1 if the next token is WORD1
}
else if (next_token() == PRONOUN)
{
match(PRONOUN); // Match PRONOUN if the next token is PRONOUN
}
else
{
syntaxerror2("noun", saved_lexeme); // Call syntax error function if no match is found
}
}
// Grammar rule: <verb> ::= WORD2
// Done by: Eduardo Rocha
void verb()
{
if (tracing) // If tracing is enabled
cout << "Processing <verb>" << endl; // Print tracing message
match(WORD2); // Match WORD2 token
}
// Grammar rule: <be> ::= IS | WAS
// Done by: Minh Tran
void be()
{
if (tracing) // If tracing is enabled
cout << "Processing <be>" << endl; // Print tracing message
if (next_token() == IS) // Check if the next token is IS
{
match(IS); // Match IS token
}
else if (next_token() == WAS) // Check if the next token is WAS
{
match(WAS); // Match WAS token
}
else // If the next token is neither IS nor WAS
{
syntaxerror2("be", saved_lexeme); // Call syntax error function
}
}
// Grammar rule: <tense> ::= VERBPAST | VERBPASTNEG | VERB | VERBNEG
// Done by: Alexander Nicholas
void tense()
{
if (tracing) // If tracing is enabled
cout << "Processing <tense>" << endl; // Print tracing message
if (next_token() == VERBPAST) // Check if the next token is VERBPAST
{
match(VERBPAST); // Match VERBPAST token
}
else if (next_token() == VERBPASTNEG) // Check if the next token is VERBPASTNEG
{
match(VERBPASTNEG); // Match VERBPASTNEG token
}
else if (next_token() == VERB) // Check if the next token is VERB
{
match(VERB); // Match VERB token
}
else if (next_token() == VERBNEG) // Check if the next token is VERBNEG
{
match(VERBNEG); // Match VERBNEG token
}
else // If the next token is none of the above
{
syntaxerror2("tense", saved_lexeme); // Call syntax error function
}
}
string filename;
//----------- Driver ---------------------------
// The new test driver to start the parser
// Done by: Alexander Nicholas
int main()
{
cout << "Group 12 - Eduardo Rocha, Alexander Nicholas, and Minh Tran \n"; // Prompt the user to enter the input file name
char traceInput; // Variable to store user input for tracing option
while (true) // Infinite loop to get valid tracing input from the user
{
cout << "Enable tracing? (y/n): "; // Prompt the user to enable tracing
cin >> traceInput; // Read the user input
if (traceInput == 'y' || traceInput == 'Y' || traceInput == 'n' || traceInput == 'N')
{
tracing = (traceInput == 'y' || traceInput == 'Y'); // Enable tracing if user inputs 'y'
break; // Exit the loop if the input is valid
}
else
{
cerr << "Invalid input for tracing option. Please enter 'y' or 'n'." << endl; // Error message for invalid input
}
}
cout << "Enter the input file name: "; // Prompt the user to enter the input file name
cin >> filename; // Read the file name from the user
fin.open(filename.c_str()); // Open the input file
if (!fin) // Check if the file was opened successfully
{
cerr << "Error opening file " << filename << endl; // Error message if the file cannot be opened
return 1; // Exit if the file cannot be opened
}
// Calls the <story> function to start parsing
story();
// Closes the input file and the error file
fin.close(); // Close the input file
if (errorFile.is_open()) // Check if the error file is open
{
errorFile.close(); // Close the error file
}
cout << "Parsing completed." << endl; // Message indicating parsing is completed
return 0; // Return 0 to indicate successful execution
} // end
//** require no other input files!
//** syntax error EC requires producing errors.txt of error messages
//** tracing On/Off EC requires sending a flag to trace message output functions