-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGivenParserPart.cpp
More file actions
111 lines (85 loc) · 2.01 KB
/
GivenParserPart.cpp
File metadata and controls
111 lines (85 loc) · 2.01 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
/* Implementation of Recursive-Descent Parser
* for Mini C-Like Language
* Programming Assignment 2
* Fall 2024
*/
#include "parser.h"
map<string, bool> defVar;
map<string, Token> SymTable;
namespace Parser {
bool pushed_back = false;
LexItem pushed_token;
static LexItem GetNextToken(istream& in, int& line) {
if( pushed_back ) {
pushed_back = false;
return pushed_token;
}
return getNextToken(in, line);
}
static void PushBackToken(LexItem & t) {
if( pushed_back ) {
abort();
}
pushed_back = true;
pushed_token = t;
}
}
static int error_count = 0;
int ErrCount()
{
return error_count;
}
void ParseError(int line, string msg)
{
++error_count;
cout << line << ": " << msg << endl;
}
bool IdentList(istream& in, int& line);
//PrintStmt:= PRINT (ExpreList)
bool PrintStmt(istream& in, int& line) {
LexItem t;
//cout << "in PrintStmt" << endl;
t = Parser::GetNextToken(in, line);
if( t != LPAREN ) {
ParseError(line, "Missing Left Parenthesis");
return false;
}
bool ex = ExprList(in, line);
if( !ex ) {
ParseError(line, "Missing expression list after Print");
return false;
}
t = Parser::GetNextToken(in, line);
if(t != RPAREN ) {
ParseError(line, "Missing Right Parenthesis");
return false;
}
//Evaluate: print out the list of expressions values
return true;
}//End of PrintStmt
//ExprList:= Expr {,Expr}
bool ExprList(istream& in, int& line) {
bool status = false;
//cout << "in ExprList and before calling Expr" << endl;
status = Expr(in, line);
if(!status){
ParseError(line, "Missing Expression");
return false;
}
LexItem tok = Parser::GetNextToken(in, line);
if (tok == COMMA) {
//cout << "before calling ExprList" << endl;
status = ExprList(in, line);
//cout << "after calling ExprList" << endl;
}
else if(tok.GetToken() == ERR){
ParseError(line, "Unrecognized Input Pattern");
cout << "(" << tok.GetLexeme() << ")" << endl;
return false;
}
else{
Parser::PushBackToken(tok);
return true;
}
return status;
}//End of ExprList