forked from codehoper/c_test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
160 lines (140 loc) · 4.39 KB
/
test.cpp
File metadata and controls
160 lines (140 loc) · 4.39 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
#include<iostream>
#include<vector>
using namespace std;
//pass by value
typedef vector <int> IntVector;
string rev_char(string str);
string rev_string(string str);
enum token_types_t {
IDENT, // a sequence of non-space characters not starting with "
STRING, // sequence of characters between " ", no escape
ENDTOK, // end of string/file, no more token
ERRTOK // unrecognized token
};
struct Token {
token_types_t type;
std::string value;
// constructor for Token
Token(token_types_t tt=ENDTOK, std::string val="") : type(tt), value(val) {}
};
class Lexer {
public:
// constructor
Lexer(std::string str="") : input_str(str), cur_pos(0), in_err(false),
separators(" \r\t\n") { }
void set_input(std::string); // set a new input,
void restart(); // move cursor to the beginning, restart
Token next_token(); // returns the next token
bool has_more_token(); // are there more token(s)?
private:
std::string input_str; // the input string to be scanned
size_t cur_pos; // current position in the input string
bool in_err; // are we in the error state?
std::string separators; // set of separators; *not* the best option!
};
/**
* -----------------------------------------------------------------------------
* scan and return the next token
* cur_pos then points to one position right past the token
* the token type is set to ERRTOK on error, at that point the global state
* variable err will be set to true
* -----------------------------------------------------------------------------
*/
Token Lexer::next_token() {
Token ret;
size_t last;
if (in_err) {
ret.type = ERRTOK;
ret.value = "";
return ret;
}
// if not in error state, the default token is the ENDTOK
ret.type = ENDTOK;
ret.value = "";
if (has_more_token()) {
last = cur_pos; // input_str[last] is a non-space char
if (input_str[cur_pos] == '"') {
cur_pos++;
while (cur_pos < input_str.length() && input_str[cur_pos] != '"')
cur_pos++;
if (cur_pos < input_str.length()) {
ret.type = STRING;
ret.value = input_str.substr(last+1, cur_pos-last-1);
cur_pos++; // move past the closing "
} else {
in_err = true;
ret.type = ERRTOK;
ret.value = "";
}
} else {
while (cur_pos < input_str.length() &&
separators.find(input_str[cur_pos]) == string::npos &&
input_str[cur_pos] != '"') {
cur_pos++;
}
ret.type = IDENT;
ret.value = input_str.substr(last, cur_pos-last);
}
}
return ret;
}
/**
* -----------------------------------------------------------------------------
* set a new input string, restart
* -----------------------------------------------------------------------------
*/
void Lexer::set_input(string str) {
input_str = str;
restart();
}
/**
* -----------------------------------------------------------------------------
* is there more token from the current position?
* -----------------------------------------------------------------------------
*/
bool Lexer::has_more_token() {
while (cur_pos < input_str.length() &&
separators.find(input_str[cur_pos]) != string::npos) {
cur_pos++;
}
return (cur_pos < input_str.length());
}
/**
* -----------------------------------------------------------------------------
* restart from the beginning, reset error states
* -----------------------------------------------------------------------------
*/
void Lexer::restart() {
cur_pos = 0;
in_err = false;
}
int main() {
string hi = "hi";
string world = "world";
string bye = "bye";
bool is_true = true;
if(is_true) {
cout << hi << " " << world << endl;
}else {
cout << bye << world<<"\n";
}
IntVector i;
int inside_trading(int);
int inside_trading(int);
inside_trading(12);
rev_char("Dev");
Lexer lex("Hi hello \n test RGGB");
lex.set_input("Test \n test\n \t");
}
string rev_char(string str) {
string rev = "";
for(int i=str.length() -1;i>=0;i--) {
rev.push_back(str[i]);
}
//cout << "reverse string " << rev << endl;
return rev;
}
int inside_trading(int tax) {
cout << "Inside tax value " << tax << endl;
return tax;
}