-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.cpp
253 lines (211 loc) · 10.3 KB
/
table.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
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
#include <iostream>
#include "table.h"
#include <iomanip>
void Table::begin(Token* token){
if (token->getType() == "IDENTIFIER") {
Entry* tempEntry = nullptr;
if (token->getSibling() != nullptr) {
return build(token->getSibling(), token, tempEntry);
} else if (token->getChild() != nullptr) {
token = token->getChild();
return build(token->getChild(), token, tempEntry);
} else {
return;
}
} else {
if (token->getSibling() != nullptr) {
begin(token->getSibling());
} else if (token->getChild() != nullptr) {
begin(token->getChild());
} else {
return;
}
}
}
void Table::build(Token* token, Token* prevToken, Entry* prevEntry) {
if (prevToken == nullptr || token == nullptr) {
return; // Ensure the tokens are valid before proceeding
}
Entry* entry = prevEntry; // Start with the previous entry
// Checks if function isn't paused, if prevToken is reserved, and token isn't {,},(,),[,]
if (!pause && contains(prevToken->getValue()) &&
token->getValue() != "{" && token->getValue() != "}" &&
token->getValue() != "(" && token->getValue() != ")" &&
token->getValue() != "[" && token->getValue() != "]") {
pause = true; // Pauses identifier creation until function parameters or array info is processed
if (prevToken->getValue() == "procedure" || prevToken->getValue() == "function") {
scope++;
inProcOrFuncScope = true;
if (prevToken->getValue() == "procedure") { // If prevToken is a procedure
exists(token, scope);
// New entry created for this procedure
entry = new Entry(token->getValue(), prevToken->getValue(), "NOT APPLICABLE", false, 0, scope);
if (prevEntry == nullptr){ //
head = entry;
}
}
else { // If prevToken is a function
exists(token, scope);
// New entry created for this function
entry = new Entry(token->getSibling()->getValue(), prevToken->getValue(), token->getValue(), false, 0, scope);
if (prevEntry == nullptr){ // If prevEntry is null, set head to entry
head = entry;
}
else {
prevEntry->setNext(entry); // Links entry to prevEntry
}
// Recursive call to process the next sibling
return build(token->getSibling()->getSibling(), token->getSibling(), entry);
}
}
else { // prevToken is not a procedure or function
if (inProcOrFuncScope){
exists(token, scope);
entry = new Entry(token->getValue(), "datatype", prevToken->getValue(), false, 0, scope);
}
else{
exists(token, 0);
entry = new Entry(token->getValue(), "datatype", prevToken->getValue(), false, 0, 0);
}
if (prevEntry == nullptr){
head = entry;
}
if (token->getSibling() != nullptr){ // If token has a sibling, process sibling token
if (token->getSibling()->getValue() == "["){ // Indicates start of an array
if (prevEntry != nullptr){
prevEntry->setNext(entry); // Links entry to prevEntry
}
return setArray(token->getSibling(), entry); // Handles the array
}
else if (token->getSibling()->getValue() == ","){ // Indicates an initalizer list for variables
if (prevEntry != nullptr){
prevEntry->setNext(entry); // Links entry to prevEntry
}
return handleInitList(prevToken->getValue(), token->getSibling()->getSibling(), entry); // Handles initializer list
}
}
}
if (prevEntry != nullptr){
prevEntry->setNext(entry); // Update the next pointer of prevEntry
}
}
else if (pause == true){ // If true, we are processing parameters for a function or procedure
if (contains(prevToken->getValue()) && token->getType() == "IDENTIFIER"){
exists(token, scope);
// Entry created for a parameter
Entry *newEntry = new Entry(token->getValue(), "parameter", prevToken->getValue(), false, 0, scope);
// Processes an array parameter
if (token->getSibling()->getValue() == "["){
token = token->getSibling()->getSibling();
newEntry->setIsArray();
newEntry->setArray(stoi(token->getValue()));
}
prevEntry->parameters.push_back(newEntry);
}
}
// Checks for the ending of a function or procedure, or a '}'
if (token->getValue() == "}") {
pause = false; // Resume normal processing
inProcOrFuncScope = false;
}
// Recursively traverse siblings or children
if (token->getSibling() != nullptr){
build(token->getSibling(), token, entry); // Traverse sibling
}
else if (token->getChild() != nullptr){
pause = false;
build(token->getChild(), token, entry); // Traverse child
}
}
// Checks if the token is a reserved word
bool Table::contains(std::string token){
for (int i = 0; i < reserved.size(); i++){
if (token == reserved.at(i)){
return true;
}
}
return false;
}
// Checks if the token exists in the symbol table
void Table::exists(Token* token, int scope){
auto temp = head;
while(temp != nullptr){
if (temp->getIDName() == token->getValue() && !contains(token->getValue()) ){
if (temp->getScope() == 0){
std::cerr << "Error on line: " << token->getLineNumber() << " variable \"" << token->getValue() << "\" already defined globally\n";
exit(1);
}
if (temp->getScope() == scope){
std::cerr << "Error on line: " << token->getLineNumber() << " variable \"" << token->getValue() << "\" already defined locally\n";
exit(1);
}
}
for (int i = 0; i < temp->parameters.size(); i++){
if (temp->parameters.at(i)->getIDName() == token->getValue() && !contains(token->getValue()) ){
if (temp->parameters.at(i)->getScope() == 0){
std::cerr << "Error on line: " << token->getLineNumber() << " variable \"" << token->getValue() << "\" already defined globally\n";
exit(1);
}
if (temp->parameters.at(i)->getScope() == scope){
std::cerr << "Error on line: " << token->getLineNumber() << " variable \"" << token->getValue() << "\" already defined locally\n";
exit(1);
}
}
}
temp = temp->getNext();
}
}
void Table::setArray(Token* token, Entry* entry){
entry->setArray(std::stoi(token->getSibling()->getValue()));
build(token->getSibling()->getSibling(), token, entry);
}
void Table::handleInitList(std::string type, Token* token, Entry* prevEntry){
Entry *entry;
if(inProcOrFuncScope){
entry = new Entry(token->getValue(), "datatype", type, false, 0, scope);
}
else{
entry = new Entry(token->getValue(), "datatype", type, false, 0, 0);
}
prevEntry->setNext(entry); // Update the next pointer of prevEntry
if (token->getSibling()->getValue() == ";"){
return build(token->getSibling()->getChild(), token->getSibling(), entry);
}
return handleInitList(type, token->getSibling()->getSibling(), entry);
}
void Table::printTable(){
const int colonWidth = 25;
//make temp head pointer
Entry* tempHead = this->head;
while(tempHead != nullptr){
std::cout << std::setw(colonWidth) << std::right << "IDENTIFIER_NAME: " << tempHead->getIDName() << std::endl;
std::cout << std::setw(colonWidth) << std::right << "IDENTIFIER_TYPE: " << tempHead->getIDType() << std::endl;
std::cout << std::setw(colonWidth) << std::right << "DATATYPE: " << tempHead->getDType() << std::endl;
std::cout << std::setw(colonWidth) << std::right << "DATATYPE_IS_ARRAY: " << (tempHead->getIsArray() ? "yes" : "no") << std::endl;
std::cout << std::setw(colonWidth) << std::right << "DATATYPE_ARRAY_SIZE: " << tempHead->getArraySize() << std::endl;
std::cout << std::setw(colonWidth) << std::right << "SCOPE: " << tempHead->getScope() << std::endl;
std::cout << std::endl;
tempHead = tempHead->getNext();
}
}
// Print function for any Entries that have parameters
void Table::printParameters(){
// make local head pointer
Entry* tempHead = this->head;
const int colonWidth = 25;
// Cycle through all entries in the table
while(tempHead != nullptr){
if (tempHead->parameters.size() > 0){ // If current entry has parameters...
std::cout << std::setw(colonWidth) << std::right << "PARAMETER LIST FOR: " << tempHead->getIDName() << std::endl;
for (int i = 0; i < tempHead->parameters.size(); i++) { // Print each parameter
std::cout << std::setw(colonWidth) << std::right << "IDENTIFIER_NAME: " << tempHead->parameters.at(i)->getIDName() << std::endl;
std::cout << std::setw(colonWidth) << std::right << "DATATYPE: " << tempHead->parameters.at(i)->getDType() << std::endl;
std::cout << std::setw(colonWidth) << std::right << "DATATYPE_IS_ARRAY: " << (tempHead->parameters.at(i)->getIsArray() ? "yes" : "no") << std::endl;
std::cout << std::setw(colonWidth) << std::right << "DATATYPE_ARRAY_SIZE: " << tempHead->parameters.at(i)->getArraySize() << std::endl;
std::cout << std::setw(colonWidth) << std::right << "SCOPE: " << tempHead->parameters.at(i)->getScope() << std::endl;
std::cout << std::endl;
}
}
tempHead = tempHead->getNext();
}
}