-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexan.cxx
executable file
·288 lines (255 loc) · 6.81 KB
/
lexan.cxx
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/************************ lexan.cxx Sommersemester 2018 ********************/
/*************** Scanner *******************************/
#ifndef GLOBAL_H
#include "global.h"
#endif
#ifndef ERROR_H
#include "error.h"
#endif
#ifndef LEXAN_H
#include "lexan.h"
#endif
#include <climits>
#include <cmath>
#include <map>
using namespace token;
map<string, type_t> keywords = {
{ "const", CONST },
{ "var", VAR },
{ "procedure", PROCEDURE },
{ "call", CALL },
{ "begin", BEGIN },
{ "end", END },
{ "if", IF },
{ "then", THEN },
{ "else", ELSE },
{ "while", WHILE },
{ "do", DO },
{ "int", INT },
{ "real", REAL },
{ "fi", FI }
};
/**
* Initalize a new lexan_t entity starting at line number 1 and reading the
* first character from this->input
*/
lexan_t::lexan_t( ifstream& input, ostream& output, ostream& error )
: lineno( 1 ), input( input ), output( output ), err( error ) {
this->next();
}
void lexan_t::next() {
this->input.get( this->actchar );
}
/**
* nextsymbol returns the next symbol from the inputfile.
*
* IMPORTANT!!!: lexan must always be pointing to the next character after the last found symbol
*
* @param lex the used lexan instance with the current character and linenumber
* @return the found symbol
*/
token_t lexan_t::nextsymbol() {
while( !this->input.eof() ) {
if ( isspace( this->actchar ) && this->actchar != '\n' ) {
/* ignore whitespaces except for the newline */
this->input.get(this->actchar);
} else if ( this->actchar == '\n' ) {
/* found newline => increment linenumber of lexan_t */
this->input.get(this->actchar);
this->lineno++;
this->output << "line: " << this->lineno << endl;
} else if ( isdigit(this->actchar) ) {
/* found number. regex: [0-9]+\.?[0-9]* */
char zahl[BSIZE];
int b = 0;
bool isReal = false;
this->output.put( this->actchar );
zahl[b++] = this->actchar;
this->next();
while ( isdigit( this->actchar ) || ( !isReal && this->actchar == '.' ) ) {
if ( this->actchar == '.' ) {
isReal = true;
}
zahl[b++] = this->actchar;
this->output.put( this->actchar );
this->next();
if ( b > BSIZE ) {
this->error( error::NUMBER_TO_BIG );
}
}
zahl[b] = '\0';
if ( isReal ) {
double real = strtod( zahl, nullptr );
if ( errno == ERANGE && real == HUGE_VAL ) {
this->error( error::NUMBER_TO_BIG ); // To big number
}
return real;
}
long val = strtol( zahl, nullptr, 0 );
if ( errno == ERANGE && ( val == LONG_MIN || val == LONG_MAX ) ) {
this->error( error::NUMBER_TO_BIG ); // To big number
}
if ( val > INT32_MAX || val < INT32_MIN ) {
this->error( error::NUMBER_TO_BIG ); // To big number
}
return (int) val;
} else if ( isalpha(this->actchar) ) {
/* reg. Ausdruck letter (letter|digit)* erkennen ==>
* solange Buchstaben oder Ziffern folgen --> Identifikator
*/
string ident;
ident += this->actchar;
this->output.put( this->actchar );
while ( isalpha( this->actchar ) || isdigit( this->actchar ) ) {
this->next();
if ( isalpha( this->actchar ) || isdigit( this->actchar ) ) {
ident += this->actchar;
this->output.put( this->actchar );
}
if ( ident.length() > BSIZE ) {
this->error( error::IDENTFIER_TO_BIG );
}
}
if ( keywords.count( ident ) == 0 ) {
return ident;
}
return keywords[ ident ];
} else {
this->output.put(this->actchar); /* Zeichen in Ausgabedatei */
switch( this->actchar) {
case '=':
this->input.get(this->actchar);
return EQ;
case '<':
this->input.get( this->actchar);
switch ( this->actchar ) {
case '=':
this->output.put(this->actchar);
this->next();
return LE;
default:
return LT;
}
case '!':
this->next();
this->output.put( this->actchar );
if ( this->actchar != '=' ) {
this->error( error::EXPECTED_EQUAL );
}
this->next();
return NE;
case '>':
this->next();
switch ( this->actchar ) {
case '=':
this->next();
return GE;
default:
return GT;
}
case ':':
this->next();
switch ( this->actchar ) {
case '=':
this->output.put( this->actchar );
this->next();
return ASS;
default:
return COLON;
}
case ',':
this->next();
return KOMMA;
case ';':
this->next();
return SEMICOLON;
case '+':
this->next();
return PLUS;
case '-':
this->next();
return MINUS;
case '*':
this->next();
return MULT;
case '/':
this->next();
return DIV;
case '(':
this->next();
return KLAUF;
case ')':
this->next();
return KLZU;
case '$':
this->next();
return PROGEND;
default:
this->error( error::INVALID_CHARACTER );
break;
} /* end-switch */
} /* end-else */
}/* end while */
return DONE; /* EIngabe -Ende erreicht */
}
namespace token {
string toString( type_t type ) {
switch( type ) {
default:
case INVALID: return "INVALID";
case INTNUM: return "INTNUM";
case REALNUM: return "REALNUM";
case ID: return "ID";
case CONST: return "CONST";
case VAR: return "VAR";
case PROCEDURE: return "PROCEDURE";
case CALL: return "CALL";
case BEGIN: return "BEGIN";
case END: return "END";
case IF: return "IF";
case THEN: return "THEN";
case ELSE: return "ELSE";
case WHILE: return "WHILE";
case DO: return "DO";
case EQ: return "EQ";
case NE: return "NE";
case LT: return "LT";
case LE: return "LE";
case GT: return "GT";
case GE: return "GE";
case ASS: return "ASS";
case KOMMA: return "KOMMA";
case SEMICOLON: return "SEMICOLON";
case PLUS: return "PLUS";
case MINUS: return "MINUS";
case MULT: return "MULT";
case DIV: return "DIV";
case KLAUF: return "KLAUF";
case KLZU: return "KLZU";
case PROGEND: return "PROGEND";
case COLON: return "COLON";
case INT: return "INT";
case REAL: return "REAL";
case FI: return "FI";
case DONE: return "DONE";
}
}
}
std::ostream& operator<<( std::ostream& os, const token_t& symbol ) {
os << "symbol(" << token::toString(symbol.type);
switch ( symbol.type ) {
case INTNUM:
os << ", num = " << symbol.num << ")";
break;
case REALNUM:
os << ", real = " << symbol.realnum << ")";
break;
case ID:
os << ", name = " << symbol.idname << ")";
break;
default:
os << ")";
break;
}
return os;
}