include iostream include vector include fstream include string define

  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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#define IDENTIFICATOR 1
#define INTEGER 2
#define SPLITER 3
#define KEYWORD 4
#define COMMENTS 5
using namespace std;
struct twoNumbers {
unsigned numberOfThatThings;
unsigned numInTable;
};
char SpliterTable[2][9] = {{1, 2, 3, 4, 5, 6, 7, 8, 9}, {';', '+', '-', '*', '/', '(', ')', '{', '}' }};
string KeywordArray[2][3] = {{"1", "2", "3"}, {"for", "if", "else"}};
vector<twoNumbers> result;
vector<int> numbers;
vector<char> characters;
int indexSpliter;
unsigned state;
bool check_num(char*);
bool check_char(char *);
bool isKeyName(vector<char>);
int inputInTableOfConst(vector<int>);
int inputInTableOfSymName(vector<char>);
int inputInTableOfKeywords(vector<char>);
bool look(vector<char>);
int persing(char *);
int main(int argc, const char * argv[])
{
ifstream in;
state = 0;
char* buf;
twoNumbers temp;
in.open("/Users/nikita/Developer/Scanner/Scanner/intput.txt");
in >> buf;
while (!in.eof()) {
switch (state) {
case 0:
if (check_num(buf)) {
state = 1;
numbers.push_back(atoi(buf));
}
if (strcmp(buf, "/")) {
state = 2;
}
if (strcmp(buf, ";") || strcmp(buf, "+") || strcmp(buf, "-") || strcmp(buf, "*") || strcmp(buf, "(") || strcmp(buf, ")") || strcmp(buf, "{") || strcmp(buf, "}")) {
state = 3;
temp.numberOfThatThings = SPLITER;
temp.numInTable = persing(buf);
state = 0;
}
if (check_char(buf)) {
state = 4;
characters.push_back(buf[0]);
}
break;
case 1:
if (check_num(buf)) {
state = 1;
numbers.push_back(atoi(buf));
}
else {
state = 0;
temp.numberOfThatThings = INTEGER;
temp.numInTable = inputInTableOfConst(numbers);
result.push_back(temp);
numbers.clear();
}
case 2:
if (strcmp(buf, "/"))
state = 5;
else
state = 6;
break;
case 4:
if (check_char(buf) || check_num(buf)) {
state = 4;
characters.push_back(buf[0]);
}
else {
if (look(characters))
{
temp.numberOfThatThings = IDENTIFICATOR;
temp.numInTable = inputInTableOfSymName(characters);
result.push_back(temp);
}
else
{
temp.numberOfThatThings = KEYWORD;
temp.numInTable = inputInTableOfSymName(characters);
result.push_back(temp);
}
characters.clear();
state = 0;
}
break;
default:
break;
}
in >> buf;
}
in.close();
return 0;
}
bool check_num(char *ch)
{
if (ch[0] >= '0' && ch[0] <= '9')
return true;
return false;
}
int persing(char *ch)
{
for (int i = 0; i < 9; i++) {
if (ch[0] == SpliterTable[1][i])
return i + 1;
}
return 0;
}
int inputInTableOfKeywords(vector<char> chs)
{
for (int i = 0; i < 3; i++) {
if (ch[0] == KeywordArray[1][i])
return i + 1;
}
}
int look(char *ch)
{
return 0;
}
bool check_char(char *ch)
{
if ((ch[0] >= 'a' && ch[0] <= 'z') || (ch[0] >= 'A' && ch[0] <= 'Z'))
return true;
return false;
}