-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.cpp
More file actions
121 lines (99 loc) · 2.17 KB
/
Copy pathparse.cpp
File metadata and controls
121 lines (99 loc) · 2.17 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
/* Incomplete Implementation of Recursive-Descent Parser
* parse.cpp
* Programming Assignment 2
* Fall 2021
*/
#include "parse.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;
}
//Decl = Type IdentList
//Type = INTEGER | REAL | CHAR
bool DeclStmt(istream& in, int& line) {
bool status = false;
LexItem tok;
//cout << "in Decl" << endl;
LexItem t = Parser::GetNextToken(in, line);
if(t == INT || t == FLOAT ) {
status = IdentList(in, line, t);
//cout<< "returning from IdentList" << " " << (status? 1: 0) << endl;
if (!status)
{
ParseError(line, "Incorrect variable in Declaration Statement.");
return status;
}
}
else{
Parser::PushBackToken(t);
ParseError(line, "Incorrect Type.");
return false;
}
return true;
}
bool Stmt(istream& in, int& line){
bool status=true;
//cout << "in Stmt" << endl;
LexItem t = Parser::GetNextToken(in, line);
switch( t.GetToken() ) {
case INT: case FLOAT:
Parser::PushBackToken(t);
status = DeclStmt(in, line);
if(!status)
{
ParseError(line, "Incorrect Declaration Statement.");
return status;
}
break;
case IF: case WRITE: case IDENT:
Parser::PushBackToken(t);
status = ControlStmt(in, line);
if(!status)
{
ParseError(line, "Incorrect control Statement.");
return status;
}
break;
default:
Parser::PushBackToken(t);
}
return status;
}
//WriteStmt:= wi, ExpreList
bool WriteStmt(istream& in, int& line) {
LexItem t;
//cout << "in WriteStmt" << endl;
bool ex = ExprList(in, line);
if( !ex ) {
ParseError(line, "Missing expression after Write");
return false;
}
//Evaluate: print out the list of expressions values
return ex;
}