-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.cpp
More file actions
114 lines (94 loc) · 1.78 KB
/
Copy pathtoken.cpp
File metadata and controls
114 lines (94 loc) · 1.78 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
#include <sstream>
#include "Token.h"
#include "Expression.h"
Token::Token() {
type = INVALID;
token = " ";
priority = -1;
}
Token::Token(string s) {
set(s);
}
void Token::set(string s) {
token = s;
priority = -1;
if ((s.at(0) == '=') && (s.size() == 1)){
type = EQ;
priority = -2;
}
else if ((s.at(0) == '(') && (s.size() == 1)){
type = Openbrace;
priority = 0;
}
else if ((s.at(0) == ')') && (s.size() == 1)){
type = Closebrace;
priority = -2;
}
else if ((s.at(0) == '+') ||(s.at(0) == '-') && (s.size() == 1)) {
type = OP;
priority = 1;
}
else if ((s.at(0) == '*') || (s.at(0) =='/') && (s.size() == 1)){
type = OP;
priority = 2;
}
else if (isalpha(s.at(0)) != 0) {
int i = 0;
while (((i < s.size()) && (isalpha(s.at(i)) != 0)) || ((i < s.size()) && (isdigit(s.at(i))!= 0))) {
i++;
}
if (i == s.size())
type = ID;
else {
type = INVALID;
priority = -2;
}
}
else if (isdigit(s.at(0)) != 0) {
int i = 0;
while ((i < s.size()) && (isdigit(s.at(i)) != 0)) {
i++;
}
if (i == s.size()) {
type = INT;
stringstream change (token);
int x = 0;
change >> x;
priority = x;
}
else {
type = INVALID;
priority = -2;
}
}
else {
type = INVALID;
priority = -2;
}
}
int Token::value() const{
if (type == INT) {
stringstream change (token);
int x = 0;
change >> x;
return x;
}
else if (type == ID){
return -1;
}
else {
return -2;
}
}
void Token::display() const {
cout << token << " ";
}
Token_type Token::get_type() const {
return type;
}
string Token::get_token() const {
return token;
}
int Token::get_priority() const {
return priority;
}