-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tokenlib.h
209 lines (186 loc) · 3.71 KB
/
Tokenlib.h
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
#ifndef TOKENLIB_H
#define TOKENLIB_H
#include <cstring>
#include <vector>
#include <string>
#include <stack>
using namespace std;
enum type_of_lexem{
LEX_NULL, //0
LEX_AND, //1
LEX_BREAK, //2
LEX_CASE, //3
LEX_CATCH, //4
LEX_CONST, //5
LEX_CONTINUE, //6
LEX_DEBUGGER, //7
LEX_DEFAULT, //8
LEX_DELETE, //9
LEX_DO, //10
LEX_ELSE, //11
LEX_FINALLY, //12
LEX_FOR, //13
LEX_FUNCTION, //14
LEX_IF, //15
LEX_IN, //16
LEX_INSTANCEOF, //17
LEX_LET, //18
LEX_NEW, //19
LEX_OR, //20
LEX_RETURN, //21
LEX_SWITCH, //22
LEX_THROW, //23
LEX_TRY, //24
LEX_TYPEOF, //25
LEX_VAR, //26
LEX_VOID, //27
LEX_WHILE, //28
LEX_WITH, //29
LEX_TRUE,//30
LEX_FALSE,//31
LEX_BOOL, //32
LEX_SEMICOLON, //33
LEX_COMMA, //34
LEX_COLON, //35
LEX_LPAREN, //36
LEX_RPAREN, //37
LEX_LSS, //38
LEX_EQ, //39
LEX_DEQ,//40
LEX_TEQ, //41
LEX_GTR, //42
LEX_PLUS, //43
LEX_MINUS, //44
LEX_TIMES, //45
LEX_SLASH, //46
LEX_LEQ, //47
LEX_NEQ, //48
LEX_GEQ, //49
LEX_NUM, //50
LEX_ID, //51
POLIZ_LABEL, //52
POLIZ_ADDRESS, //53
POLIZ_GO, //54
POLIZ_FGO, //55
LEX_FIN, //56
LEX_LBRACE, //57
LEX_RBRACE, //58
LEX_STRING, //59
LEX_UNOMINUS,//60
LEX_UNOPLUS, //61
LEX_ALERT,//62
LEX_READ,//63
POLIZ_CALL//64
};
/* Lexem is (type of a lexem, value of lexem)*/
class Lexem{
type_of_lexem type_lex;
int value_lex;
public:
Lexem(type_of_lexem t = LEX_NULL, int v=0){
type_lex = t;
value_lex = v;
}
type_of_lexem get_type(){ return type_lex; }
int get_value(){ return value_lex; }
friend ostream& operator<< (ostream &s, Lexem l){
s << '(' << l.type_lex << " - " << l.value_lex << "); ";
return s;
}
};
/* Here identificators described */
class Identificator{
char *name;
bool declare;
type_of_lexem type;
bool assign;
int value;
int address;
bool is_func;
public:
Identificator(){
declare = false;
assign = false;
is_func = false;
}
char* get_name(){ return name;}
void set_name(const char *s){
name = new char[strlen(s)+1];
strcpy(name,s);
}
bool get_declare(){ return declare; }
void set_declare(){ declare = true; }
void undeclare() { declare = false; }
type_of_lexem get_type(){ return type; }
void set_type(type_of_lexem t){ type = t; }
bool get_assign(){ return assign; }
void set_assign(){ assign = true; }
int get_value(){ return value; }
void set_value(int k){ value = k; }
void set_address(int k){ address = k; }
int get_address(){ return address; }
void set_is_func(){ is_func=true;}
bool get_is_func(){return is_func;}
};
class table_identificators{
Identificator *p;
int size;
int top;
public:
table_identificators(int max_size){
p = new Identificator[size=max_size];
//p[0]= Identificator();
top = 1;
}
~table_identificators(){
delete []p;
}
Identificator& operator[](int k){ return p[k]; }
int put( const char *buf );
};
static table_identificators TID(1000);
class Scanner{
enum state { H , IDENT , NUMB , COM1 , COM2 , ALE , COMP , DELIM , NEQ , STRING , ERROR};
static const char *TW[];
static type_of_lexem words[];
static const char *TD[];
static type_of_lexem dlms[];
//static char* TS[];
state current_state;
FILE *fp;
char c;
char buf[80];
int buf_top;
void clear(){
buf_top = 0;
for(int j=0; j<80; ++j)
buf[j]='\0';
}
void add(){
buf[buf_top++]=c;
}
int look(const char *buf, const char **list){
int i = 0;
while(list[i]){
if(!strcmp(buf,list[i]))
return i;
++i;
}
return 0;
}
void get_char(){
c = fgetc(fp);
}
public:
/* set up tables of identificators */
static vector<string> TS;
Scanner (const char *program){
if( !(fp = fopen(program,"r")) )
throw invalid_argument("no such file");
current_state = H;
clear();
get_char();
}
Lexem get_lex(int *n);
};
#endif