-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.h
201 lines (185 loc) · 6.12 KB
/
Scanner.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
#ifndef PASCAL_SCANNER_H
#define PASCAL_SCANNER_H
#include <iostream>
#include <string>
#include <cstdio>
#include <ctype.h>
#include <cstdlib>
#include <vector>
#include <stack>
#include <algorithm>
#include "Lex.h"
#include "Ident.h"
using namespace std;
/*==========================================================================
* Таблица идентификаторов TID
*==========================================================================
*/
vector <Ident> TID;
/*==========================================================================
* Занести лексему в таблицу TID. Если она уже описана, вернуть ее номер
*==========================================================================
*/
int put ( const string & buf ) {
vector<Ident>::iterator k;
if ( ( k = find ( TID.begin (), TID.end (), buf ) ) != TID.end () )
return k - TID.begin();
TID.push_back ( Ident(buf) );
return TID.size () - 1;
}
/*==========================================================================
*------------------------------Класс Scanner-------------------------------
*==========================================================================
* Выделение лексем с помощью диаграммы состояний лексического анализатора
*==========================================================================
*/
class Scanner {
FILE * fp;
char c;
int look ( const string buf, const char ** list ) {
int i = 0;
while ( list[i] ) {
if ( buf == list[i] )
return i;
++i;
}
return 0;
}
void gc () {
c = fgetc (fp);
}
public:
static const char * TW [], * TD [];
Scanner ( const char * program ) {
if ( !(fp = fopen ( program, "r" )) )
throw "can't open file" ;
}
Lex get_lex ();
};
/*==========================================================================
* Выделение лексем
*==========================================================================
*/
Lex Scanner::get_lex () {
enum state { H, IDENT, NUMB, COM, ALE, NEQ };
int d, j;
string buf;
state CS = H;
do {
gc ();
switch ( CS ) {
case H:
if ( c==' ' || c == '\n' || c== '\r' || c == '\t' );
else if ( isalpha (c) ) { // если буква
buf.push_back (c);
CS = IDENT;
}
else if ( isdigit (c) ) { // если цифра
d = c - '0';
CS = NUMB;
}
else if ( c== '{' ) {
CS = COM;
}
else if ( c == ':' || c == '<' || c == '>' ) {
buf.push_back (c);
CS = ALE;
}
else if (c == '.')
return Lex ( LEX_FIN );
else if (c == '!') {
buf.push_back (c);
CS = NEQ;
}
else {
buf.push_back (c);
if ( ( j = look ( buf, TD) ) ){
return Lex ( (type_of_lex)( j + (int) LEX_FIN ), j );
}
else
throw c;
}
break;
case IDENT:
if ( isalpha (c) || isdigit (c) ) {
buf.push_back (c);
}
else {
ungetc ( c, fp );
if ( (j = look ( buf, TW) ) ) {
return Lex ( (type_of_lex) j, j );
}
else {
j = put ( buf ); // записываем в таблицу и возвр-ем номер
return Lex ( LEX_ID, j );
}
}
break;
case NUMB:
if ( isdigit (c) ) {
d = d * 10 + ( c - '0' );
}
else {
ungetc ( c, fp );
return Lex ( LEX_NUM, d );
}
break;
case COM:
if ( c == '}' ) {
CS = H;
}
else if ( c == '.' || c == '{' )
throw c;
break;
case ALE:
if ( c == '=' ) {
buf.push_back ( c );
j = look ( buf, TD );
return Lex ( (type_of_lex) ( j + (int) LEX_FIN ), j );
}
else {
ungetc ( c, fp );
j = look ( buf, TD );
return Lex ( (type_of_lex) ( j + (int) LEX_FIN ), j );
}
break;
case NEQ:
if ( c == '=' ) {
buf.push_back(c);
j = look ( buf, TD );
return Lex ( LEX_NEQ, j );
}
else
throw '!';
break;
} //end switch
} while (true);
}
/*==========================================================================
* Перегрузка операции вывода
*==========================================================================
*/
ostream & operator<< ( ostream &s, Lex l ) {
string t;
if ( l.t_lex <= LEX_WRITE )
t = Scanner::TW[l.t_lex];
else if ( l.t_lex >= LEX_FIN && l.t_lex <= LEX_GEQ )
t = Scanner::TD[ l.t_lex - LEX_FIN ];
else if ( l.t_lex == LEX_NUM )
t = "NUMB";
else if ( l.t_lex == LEX_ID )
t = TID[l.v_lex].get_name ();
else if ( l.t_lex == POLIZ_LABEL )
t = "Label";
else if ( l.t_lex == POLIZ_ADDRESS )
t = "Addr";
else if ( l.t_lex == POLIZ_GO )
t = "!";
else if ( l.t_lex == POLIZ_FGO )
t = "!F";
else
throw l;
s << '(' << t << ',' << l.v_lex << ");" << endl;
return s;
}
#endif //PASCAL_SCANNER_H