/
project.cpp
118 lines (99 loc) · 3.35 KB
/
project.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
//Use only the following three libraries:
#include "parserClasses.h"
#include <iostream>
#include <fstream>
using namespace std;
//Example Test code for interacting with your Token, TokenList, and Tokenizer classes
//Add your own code to further test the operation of your Token, TokenList, and Tokenizer classes
int main() {
ifstream sourceFile;
TokenList tokens;
//Lists for types of tokens
TokenList operatorTokens;
TokenList identifierTokens;
TokenList literalTokens;
TokenList commentBodyTokens;
TokenList otherTokens;
TokenList keywordTkoens;
Tokenizer tokenizer;
//Read in a file line-by-line and tokenize each line
sourceFile.open("/Users/arlene/Desktop/251Final/251Final/test.vhd");
if (!sourceFile.is_open()) {
cout << "Failed to open file" << endl;
return 1;
}
while(!sourceFile.eof()) {
string line;
getline(sourceFile, line);
tokenizer.setString(&line);
while(!tokenizer.isComplete()) {
tokens.append(tokenizer.getNextToken());
}
}
/*Test your tokenization of the file by traversing the tokens list and printing out the tokens*/
Token *t = tokens.getFirst();
while(t) {
cout << t->getStringRep() << " ";
t = t->getNext();
}
//--------------------------------------test code for part 1-------------------------------------------
/*Token *t = tokens.getFirst();
while(t){
tokens.findAndSetTokenDetails(t);
if (t->isOperator()){
Token *temp = new Token(*t);
temp->setNext(nullptr);
operatorTokens.append(temp);
}
if (t->isLiteral()){
Token *temp = new Token(*t);
temp->setNext(nullptr);
literalTokens.append(temp);
}
if (t->isKeyword()){
Token *temp = new Token(*t);
temp->setNext(nullptr);
keywordTkoens.append(temp);
}
if (t->isComment()){
Token *temp = new Token(*t);
temp->setNext(nullptr);
commentBodyTokens.append(temp);
}
if (t->isIdentifier()){
Token *temp = new Token(*t);
temp->setNext(nullptr);
identifierTokens.append(temp);
}
if (t->isOther()){
Token *temp = new Token(*t);
temp->setNext(nullptr);
otherTokens.append(temp);
}
t = t->getNext();
}
t = identifierTokens.getFirst();
while(t){
cout << t->getStringRep() << " ";
if (t->getTokenDetails() != nullptr){
cout << t->getTokenDetails()->width << " ";
cout << t->getTokenDetails()->type << " "<<endl;}
t = t->getNext();}*/
//---------------------------------------test remove Token Type-----------------------------------------
// cout << removeTokensOfType(tokens, T_CommentBody) <<endl;
//---------------------------------------test removeComments--------------------------------------------
/*removeComments(tokens);
t=tokens.getFirst();
while(t){
cout << t->getStringRep() << " ";
t = t->getNext();
}*/
//--------------------------------------------test part 2-----------------------------------------------
/* TokenList *p = findAllConditionalExpressions(tokens);
Token *t = p->getFirst();
while(t!= nullptr) {
cout << t->getStringRep() << " ";
t = t->getNext();
}*/
return 0;
}