-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
239 lines (195 loc) · 4.51 KB
/
parser.cpp
File metadata and controls
239 lines (195 loc) · 4.51 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
#include <iostream>
#include <string>
#include "parser.hpp"
#include "variable.hpp"
#include "function.hpp"
#include "global.hpp"
using namespace std;
//Create ast node tree and stack
vector<Node*> Parser::program(void){
vector<Node*> nodes;
nodes.push_back(binexpr(get_node()));
while(consume("\n")){
nodes.push_back(binexpr(get_node()));
}
return nodes;
}
//classify the token
Node* Parser::primary(void){
count++;
switch (token->token)
{
case T_IDENT:
return identifier();
case T_RESERVED:
return operation_eval();
case T_NUM:
return number_eval();
case T_EOF:
return 0;
default:
cerr << "Error : Unkown token "<< token->str<<endl;
exit(1);
}
}
//Create node leaf
Node* Parser::identifier(void)
{
Node* n;
if(T_IDENT !=token->token){
cerr << "Error : expect identifier"<<endl;
exit(1);
}
if(token[1].str == "="){
}
n = new Node(token->str);
token++;
return n;
}
//Evaluate operation node
Node* Parser::operation_eval(void)
{
Node* n;
if(token->str == "("){
expect("(");
n=binexpr(static_cast<int>(get_node()));
expect(")");
return n;
}
n = new Node(get_node());
token++;
return n;
}
//Create figure node leaf
Node* Parser:: number_eval(void)
{
Node* n;
if(T_NUM !=token->token){
cerr << "Error : expect number"<<endl;
exit(1);
}
try{
n = new Node(stoi(token->str));
}
catch(const invalid_argument& e){
cerr<<"Error : Invalid argument. "<<endl;
exit(1);
}
catch(const out_of_range& e){
cerr<<"Error : Out of range \""<<token->str<<"\""<<endl;
exit(1);
}
token++;
return n;
}
void Parser::expect(string str)
{
if(T_EOF == token->token || str != token->str ){
cerr << "Error : expect \""<< str<<"\""<<endl;
exit(1);
}else{
token++;
}
}
//List of nodes precedence
static int OpPrec[] =
{0,
5,5,
20,
30,30,
40,40,
50,50,50,50,
60,60,
70,70,
80,80,80,
90,
100,100,100,100,100,100,
};
//Get node precedence
static int op_precedence(int tokentype){
int prec;
prec = OpPrec[tokentype];
return prec;
}
//Get token and return node type
NodeKind Parser::get_node(void)
{
NodeKind nodetype;
int length = sizeof(symbols)/sizeof(*symbols); //length = 22
switch (token->token)
{
//Number node
case T_NUM:
return (NodeKind)2;
//Identifer node
case T_IDENT:
return (NodeKind)1;
//Operation node
case T_RESERVED:
for(int i=0;i < length;i++){
if(token->str == symbols[i]){
nodetype = (NodeKind)(i+3);
return nodetype;
}
}
return (NodeKind)0;
case T_EOF:
return (NodeKind)0;
default:
cerr << "Error : unexpected token \""<<token->token<<"\""<<endl;
exit(1);
}
}
//Create main ast tree
Node* Parser::binexpr(int ptp)
{
Node *left,*right;
NodeKind tokentype;
left = primary();
tokentype = get_node();
//Return if the tokentype is equal to ),],}
if(tokentype == ND_RPAREN || tokentype == ND_RBLOCK|| tokentype == ND_RDIC)
return left;
//if the current token is highly prioritized than the prior one.
while (op_precedence(static_cast<int>(tokentype)) > ptp)
{
token++;
//Get right and left node tree
right = binexpr(OpPrec[tokentype]);
left = new Node(tokentype,left,right);
tokentype = get_node();
//Return if the tokentype is equal to ),],}
if(tokentype == ND_RPAREN || tokentype == ND_RBLOCK|| tokentype == ND_RDIC)
return left;
}
return left;
}
//Evaluate if the program should keep creating ast tree or not
bool Parser::consume(string str)
{
if(T_EOF == token->token) return false;
if(str == token->str){
token++;
return true;
}
return false;
}
//Return ast trees if this program process tokens correctly
vector <Node*> Parser::parse(vector<Token> &tokens){
token = tokens.begin();
count = 0;
vector<Node*> nodes = program();
if(token->token != T_EOF){
cerr << "Failed to parse"<<endl;
exit(1);
}
/*
cout<< string(30,'-')<<endl;
for(Node* node:nodes){
node->print();
cout << endl;
}
cout<< string(30,'-')<<endl;
*/
return nodes;
}