-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.hpp
226 lines (184 loc) · 4.78 KB
/
parser.hpp
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
/**
* Grammar:
* expr : term ((PLUS | MINUS) term ) *
* term : factor ((MUL | DIV) factor ) *
* factor: PLUS factor | MINUS factor | NUM | variable | LPAREN expr RPAREN
* assignment_statement: variable ASSIGN expr
* variable: ID
*/
#include <string>
#include <cctype>
#include <stdexcept>
#include <iostream>
#include <sstream>
namespace lsbasi {
struct VisitorPrint {
virtual std::stringstream visit(class Num *ast, int deep) = 0;
virtual std::stringstream visit(class BinOp *ast, int deep) = 0;
virtual std::stringstream visit(class UnaryOp *ast, int deep) = 0;
virtual std::stringstream visit(class Id *ast, int deep) = 0;
virtual std::stringstream visit(class Assign *ast, int deep) = 0;
};
struct VisitorInterpreter {
virtual int visit(class Num *ast) = 0;
virtual int visit(class BinOp *ast) = 0;
virtual int visit(class UnaryOp *ast) = 0;
virtual int visit(class Id *ast) = 0;
virtual int visit(class Assign *ast) = 0;
};
struct AST {
virtual std::stringstream handler(VisitorPrint * v, int deep) = 0;
virtual int handler(VisitorInterpreter * v) = 0;
};
struct Num: public AST {
Token * token;
int value;
Num(Token * token):token(token){
value = std::stoi(token->value);
}
std::stringstream handler(VisitorPrint * v, int deep){
return v->visit(this, deep);
}
int handler(VisitorInterpreter * v){
return v->visit(this);
}
};
struct Id: public AST {
Token * token;
std::string value;
Id(Token * token):token(token),value(token->value){}
std::stringstream handler(VisitorPrint * v, int deep){
return v->visit(this, deep);
}
int handler(VisitorInterpreter * v){
return v->visit(this);
}
};
struct BinOp: public AST {
AST * left;
Token * token;
std::string op;
AST * right;
BinOp(Token * token, AST * left, AST * right): token(token), left(left), right(right), op(token->value){}
std::stringstream handler(VisitorPrint * v, int deep){
return v->visit(this, deep);
}
int handler(VisitorInterpreter * v){
return v->visit(this);
}
};
struct UnaryOp: public AST {
AST * fact;
Token * token;
std::string op;
UnaryOp(Token * token, AST * fact): token(token), fact(fact), op(token->value){}
std::stringstream handler(VisitorPrint * v, int deep){
return v->visit(this, deep);
}
int handler(VisitorInterpreter * v){
return v->visit(this);
}
};
struct Assign: public AST {
AST * id;
AST * expr;
Assign(AST * id, AST * expr): id(id), expr(expr){}
std::stringstream handler(VisitorPrint * v, int deep){
return v->visit(this, deep);
}
int handler(VisitorInterpreter * v){
return v->visit(this);
}
};
class Parser {
Token * current_token;
Lexer lexer;
int error () {
throw std::runtime_error("Invalid Syntax");
}
void eat (Token::TokenType type){
if (current_token->type == type)
current_token = lexer.get_next_token();
else
error();
}
AST * variable(){
AST * ast;
Token * tk;
tk = current_token;
ast = new Id(tk);
eat(Token::_ID);
return ast;
}
AST * factor(){
AST * ast;
Token * tk;
tk = current_token;
switch(current_token->type){
case Token::_INTEGER:
ast = new Num(current_token);
eat(Token::_INTEGER);
break;
case Token::_LPAREN:
eat(Token::_LPAREN);
ast = expr();
eat(Token::_RPAREN);
break;
case Token::_PLUS:
eat(Token::_PLUS);
ast = new UnaryOp(tk, factor());
break;
case Token::_MINUS:
eat(Token::_MINUS);
ast = new UnaryOp(tk, factor());
break;
default:
ast = variable();
break;
}
return ast;
}
AST * term(){
AST * ast = factor();
while((current_token->type == Token::_MUL) || (current_token->type == Token::_DIV)){
Token * tk;
tk = current_token;
if (current_token->type == Token::_MUL)
eat(Token::_MUL);
else if (current_token->type == Token::_DIV)
eat(Token::_DIV);
ast = new BinOp(tk, ast, factor());
}
return ast;
}
AST * expr(){
AST * ast = term();
while((current_token->type == Token::_PLUS) || (current_token->type == Token::_MINUS)){
Token * tk;
tk = current_token;
if (current_token->type == Token::_PLUS)
eat(Token::_PLUS);
else if (current_token->type == Token::_MINUS)
eat(Token::_MINUS);
ast = new BinOp(tk, ast, term());
}
return ast;
}
AST * assignment_statement(){
AST * var = variable();
eat(Token::_ASSIGN);
AST * ast = new Assign(var, expr());
return ast;
}
public:
Parser(lsbasi::Lexer& lexer): lexer(lexer) {
current_token = this->lexer.get_next_token();
}
AST * parse(){
AST * ast = assignment_statement();
if (current_token->type != Token::_EOF)
error();
return ast;
}
};
};