-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
181 lines (134 loc) · 3.02 KB
/
parser.cpp
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
#include "parser.h"
#include "calcex.h"
#include <string>
#include <sstream>
Parser::Parser(istream* in) {
scan = new Scanner(in);
}
Parser::~Parser() {
try {
delete scan;
} catch (...) {}
}
AST* Parser::parse() {
return prog();
}
AST* Parser::prog() {
AST* result = expr();
Token* t = scan->getToken();
if (t->getType() != eof) {
cout << "* parser error" << endl;
throw ParseError;
}
return result;
}
AST* Parser::expr() {
return restExpr(term());
}
AST* Parser::restExpr(AST* e) {
Token* t = scan->getToken();
if (t->getType() == add) {
return restExpr(new AddNode(e,term()));
}
if (t->getType() == sub)
return restExpr(new SubNode(e,term()));
scan->putBackToken();
return e;
}
AST* Parser::term() {
return restTerm(storable()); //restTerm llama a storable
}
AST* Parser::restTerm(AST* e) {
Token* t = scan->getToken(); //obtiene el token
AST* result = nullptr;
switch(t->getType()) {
case times:
result = restTerm(new TimesNode(e, storable()));
break;
case divide:
result = restTerm(new DivideNode(e, storable()));
break;
case module:
result = restTerm(new ModuleNode(e, storable()));
break;
default:
scan->putBackToken();
result = e;
break;
}
return result;
}
AST* Parser::storable() {
return memOperation(factor());
}
AST* Parser::memOperation(AST* e) {
AST* result = e;
Token *t = scan->getToken();
if (t->getType() == keyword){
if(t->getLex() == "S") {
return new StoreNode(result);
}
else if(t->getLex() == "P") {
return new PlusNode(result);
}
else if(t->getLex() == "M") {
return new MinusNode(result);
}
else {
cout << "* parse error" << endl;
throw ParseError;
}
}
else {
scan->putBackToken();
}
return result;
}
AST* Parser::factor() {
AST* result;
Token* t = scan->getToken();
if (t->getType() == identifier) {
return new IdNode(t->getLex());
}
if (t->getType() == number) {
istringstream in(t->getLex());
int val;
in >> val;
return new NumNode(val);
}
if (t->getType() == keyword)
if(t->getLex() == "-v")
return InitVar();
else if(t->getLex() == "R")
return new RecallNode();
else if(t->getLex() == "C") {
return new ClearNode();
}
else {
cout << "* Parse error" << endl;
throw ParseError;
}
if (t->getType() == lparen) {
result = expr();
t = scan->getToken();
if (t->getType() == rparen) {
return result;
}
else{
cout << "* parser error" << endl;
throw ParseError;
}
}
cerr << "* Parse error 5" << endl;
throw ParseError;
}
AST* Parser::InitVar() {
Token* t = scan->getToken();
if(t->getType() == identifier) {
Token* t1 = scan->getToken();
if(t1->getType() == eol)
return new InitVarNode(t->getLex(), expr());
}
cout << "* parser error 6" << endl;
throw ParseError;
}