-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLParser.cpp
More file actions
285 lines (248 loc) · 6.45 KB
/
Copy pathLLParser.cpp
File metadata and controls
285 lines (248 loc) · 6.45 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "LLParser.h"
#include <iostream>
#include <vector>
using namespace std;
map<string, int> symbolTable;
int initalMemory = 5000;
//Checks The symbol table and inserts data if needed
void symbolTableCheck(Token t) {
if(symbolTable[t.tokenName]) {
cout << "ERROR: Redeclaration of variable " << t.tokenName << endl;
exit(1);
}
symbolTable[t.tokenName] = initalMemory++;
}
template<typename K, typename V>
void print_map(std::map<K,V> const &m)
{
for (auto const& pair: m) {
std::cout << "{" << pair.first << ": " << pair.second << "}\n";
}
}
LLParser::LLParser(const std::string& expression)
{
m_expression = expression + "$";
//Use our previously made lexer function to get the tokens
m_tokens = lexer(expression);
//SET UP our parse Table
m_table[NTS_E][TS_ID] = 1;
m_table[NTS_E][TS_L_PR] = 1;
m_table[NTS_EP][TS_PLUS] = 2;
m_table[NTS_EP][TS_MINUS] = 3;
m_table[NTS_EP][TS_EOS] = 4;
m_table[NTS_T][TS_ID] = 5;
m_table[NTS_T][TS_L_PR] = 5;
m_table[NTS_TP][TS_PLUS] = 8;
m_table[NTS_TP][TS_MINUS] = 8;
m_table[NTS_TP][TS_MUL] = 6;
m_table[NTS_TP][TS_DIV] = 7;
m_table[NTS_TP][TS_R_PR] = 8;
m_table[NTS_TP][TS_EOS] = 8;
m_table[NTS_F][TS_ID] = 10;
m_table[NTS_F][TS_L_PR] = 9;
m_table[NTS_S][TS_TYPE] = 11;
m_table[NTS_D][TS_TYPE]= 12;
m_table[NTS_TYPE][TS_TYPE] = 13;
m_table[NTS_ID][TS_ID] = 14;
m_table[NTS_S][TS_ID] = 15;
m_table[NTS_A][TS_ID] = 16;
m_table[NTS_E][TS_NUM] = 1;
m_table[NTS_T][TS_NUM] = 5;
m_table[NTS_F][TS_NUM] = 17;
//Current index is used to get the next token in vector
m_index = -1;
//Call next to go to index 0
next();
//push the $ to the stack
m_ss.push(TS_EOS);
if(m_current.lexemeName == "Keyword") {
//IF IT IS KEYWORD TREAT IT AS declaration and PUSH S TO STACK S -> D
m_ss.push(NTS_S);
symbolTableCheck(m_tokens.at(1));
}
else if (m_expression.find('=') != std::string::npos) {
//IF IT CONTAINS = TREAT AS ASSIGNMENT AND PUSH S TO STACK S -> A
m_ss.push(NTS_S);
}
else {
//TREAT AS EXPRESSION AND PUSH E TO STACK
m_ss.push(NTS_E);
}
}
void LLParser::analyzer() {
if(m_current.tokenName == "$") {
return;
}
cout << "=================================================" << endl;
cout << endl;
for(unsigned x = 0; x < m_tokens.size(); ++x)
{
cout<<m_tokens[x].lexemeName<<": "
<<m_tokens[x].tokenName<<endl;
}
while(m_ss.size() > 0) {
if(lex(m_current) == m_ss.top()) {
cout << "Symbols matched: " << m_current.tokenName << endl;
next();
m_ss.pop();
}
else {
// cout << "TOP OF STACK IS "<< m_ss.top() << endl;
// cout << "CURRENT LEXEME IS " << lex(m_current) << endl;
// cout << "Tokens are: " << m_current.tokenName << " " << m_current.lexemeName << endl;
// cout << m_table[m_ss.top()][lex(m_current)] << endl;
switch (m_table[m_ss.top()][lex(m_current)]) {
case 1:
cout << "E -> TE'" << endl;
m_ss.pop();
m_ss.push(NTS_EP);
m_ss.push(NTS_T);
break;
case 2:
cout << "E' -> +TE'" << endl;
m_ss.pop();
m_ss.push(NTS_EP);
m_ss.push(NTS_T);
m_ss.push(TS_PLUS);
break;
case 3:
cout << "E' -> -TE'" << endl;
m_ss.pop();
m_ss.push(NTS_EP);
m_ss.push(NTS_T);
m_ss.push(TS_MINUS);
break;
case 4:
cout << "E' -> Empty" << endl;
m_ss.pop();
break;
case 5:
cout << "T -> FT'" << endl;
m_ss.pop();
m_ss.push(NTS_TP);
m_ss.push(NTS_F);
break;
case 6:
cout << "T' -> *FT'" << endl;
m_ss.pop();
m_ss.push(NTS_TP);
m_ss.push(NTS_F);
m_ss.push(TS_MUL);
break;
case 7:
cout << "T' -> /FT'" << endl;
m_ss.pop();
m_ss.push(NTS_TP);
m_ss.push(NTS_F);
m_ss.push(TS_DIV);
break;
case 8:
cout << "T' -> Empty" << endl;
m_ss.pop();
break;
case 9:
cout << "F -> ( E )" << endl;
m_ss.pop();
m_ss.push(TS_R_PR);
m_ss.push(NTS_E);
m_ss.push(TS_L_PR);
break;
case 10:
cout << "F -> id" << endl;
m_ss.pop();
m_ss.push(TS_ID);
break;
case 11:
cout << "S -> D" << endl;
m_ss.pop();
m_ss.push(NTS_D);
break;
case 12:
cout << "D -> <TYPE> <ID>" << endl;
m_ss.pop();
m_ss.push(NTS_ID);
m_ss.push(NTS_TYPE);
break;
case 13:
cout << "TYPE -> type" << endl;
m_ss.pop();
m_ss.push(TS_TYPE);
break;
case 14:
cout << "ID -> id" << endl;
m_ss.pop();
m_ss.push(TS_ID);
break;
case 15:
cout << "S -> A" << endl;
m_ss.pop();
m_ss.push(NTS_A);
break;
case 16:
cout << "A -> <ID> = <E>" << endl;
m_ss.pop();
m_ss.push(NTS_E);
m_ss.push(TS_EQUALS);
m_ss.push(NTS_ID);
break;
case 17:
cout << "F -> Number" << endl;
m_ss.pop();
m_ss.push(TS_NUM);
break;
default:
cout << "Parser defaulted." << endl;
exit(0);
}
}
}
cout << "SYMBOL TABLE: " << endl;
cout << "------------------------" << endl;
print_map(symbolTable);
cout << "------------------------" << endl;
}
Symbols LLParser::lex(Token t) {
if(t.lexemeName == "Identifier") {
return TS_ID;
}
if(t.lexemeName == "Keyword") {
return TS_TYPE;
}
if(t.lexemeName == "") {
return TS_EMPTY;
}
if(t.tokenName == "+") {
return TS_PLUS;
}
if(t.tokenName == "-") {
return TS_MINUS;
}
if(t.tokenName == "/") {
return TS_DIV;
}
if(t.tokenName == "(") {
return TS_L_PR;
}
if(t.tokenName == ")") {
return TS_R_PR;
}
if(t.tokenName == "$") {
return TS_EOS;
}
if(t.tokenName == "=") {
return TS_EQUALS;
}
if(t.lexemeName == "Integer") {
return TS_NUM;
}
return TS_EMPTY;
}
void LLParser::next() {
m_index++;
if(m_index >= m_tokens.size()) {
m_current = {"$", "$"};
}
else {
m_current = m_tokens.at(m_index);
}
}