-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexico.cs
More file actions
388 lines (374 loc) · 15.1 KB
/
Lexico.cs
File metadata and controls
388 lines (374 loc) · 15.1 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace Semantica
{
public class Lexico : Token, IDisposable // Interfaz para el metodo Dispose que es llamado por el GC
{
private StreamReader Archivo;
protected StreamWriter Log;
protected StreamWriter Asm;
private const int f = -1;
private const int e = -2;
private int[,] TRAND = new int[,] {
// arreglar comentario multilinea
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// WS L D . E + - LA = ; % { } & | ! > < " C / * \n
{ 0, 1, 2, 10, 1, 13, 14, 8, 11, 12, 16, 18, 19, 20, 21, 23, 24, 25, 28, 10, 30, 34, 0}, // 0
{ f, 1, 1, f, 1, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 1
{ f, f, 2, 3, 5, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 2
{ e, e, 4, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e}, // 3
{ f, f, 4, f, 5, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 4
{ e, e, 7, e, e, 6, 6, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e}, // 5
{ e, e, 7, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e}, // 6
{ f, f, 7, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 7
{ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}, // 8
{ e, e, e, e, e, e, e, 10, e, e, e, e, e, e, e, e, e, e, e, e, e, e, e}, // 9
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 10
{ f, f, f, f, f, f, f, f, 27, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 11
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 12
{ f, f, f, f, f, 15, f, f, 15, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 13
{ f, f, f, f, f, f, 15, f, 15, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 14
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 15
{ f, f, f, f, f, f, f, f, 17, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 16
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 17
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 18
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 19
{ f, f, f, f, f, f, f, f, f, f, f, f, f, 22, f, f, f, f, f, f, f, f, f}, // 20
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, 22, f, f, f, f, f, f, f, f}, // 21
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 22
{ f, f, f, f, f, f, f, f, 27, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 23
{ f, f, f, f, f, f, f, f, 27, f, f, f, f, f, f, f, 26, f, f, f, f, f, f}, // 24
{ f, f, f, f, f, f, f, f, 27, f, f, f, f, f, f, f, f, 26, f, f, f, f, f}, // 25
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 26
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 27
{28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 28, 28, 28, 28}, // 28
{ f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f, f}, // 29
{ f, f, f, f, f, f, f, f, 17, f, f, f, f, f, f, f, f, f, f, f, 31, 32, f}, // 30
{31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 0}, // 31
{32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 32}, // 32
{32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 0, 33, 32}, // 33
{ f, f, f, f, f, f, f, f, 17, f, f, f, f, f, f, f, f, f, f, f, f, f, f} // 34
// WS L D . E + - LA = ; % { } & | ! > < " C / * \n
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
};
protected int renglon;
protected int columna;
public Lexico()
{
Archivo = new StreamReader(@"/archivos/prueba.cs"); //Debe existir
Log = new StreamWriter(@"/archivos/prueba.log"); //Lo crea si no existe, si no, lo sobreescribe
Asm = new StreamWriter(@"/archivos/prueba.asm");
Cabecera(@"/archivos/prueba.cs");
columna = 1;
renglon = 1;
}
public Lexico(string nombre)
{
/*
validar parametro con extensión .cs, de lo contrario generar excepción critica
archivo log tenga el mismo nombre /ruta/nombre.log
ejemplo: suma.cs suma.log
*/
Log = new StreamWriter(Path.ChangeExtension(nombre, "log"));
Asm = new StreamWriter(Path.ChangeExtension(nombre, "asm"));
Cabecera(nombre);
try
{
if (Path.GetExtension(nombre).ToLower() != ".cs")
{
throw new FormatoNoSoportadoException(nombre);
}
Archivo = new StreamReader(nombre);
Archivo.Peek();
}
catch (UnauthorizedAccessException)
{
ImprimirError(new CompiladorException(nombre + "\n¡No se puede leer el archivo!"));
}
catch (FileNotFoundException)
{
ImprimirError(new CompiladorException(nombre + "\n¡No existe el archivo!"));
}
catch (CompiladorException ex)
{
ImprimirError(ex);
}
renglon = 1;
columna = 1;
}
~Lexico()
{
Dispose();
}
public void Dispose()
{
Archivo.Close();
Log.Close();
Asm.Close();
Console.WriteLine("Fin de analisis léxico");
}
private int Columna(char transicion)
{
if (transicion == '\n')
{
return 22;
}
else if (char.IsWhiteSpace(transicion))
{
return 0;
}
else if (char.ToUpper(transicion) == 'E')// && GETClasificacion() == numero)
{
return 4;
}
else if (char.IsLetter(transicion))
{
return 1;
}
else if (char.IsDigit(transicion))
{
return 2;
}
else if (transicion == '.')
{
return 3;
}
else if (transicion == '+')
{
return 5;
}
else if (transicion == '-')
{
return 6;
}
else if (transicion == '"')
{
return 18;
}
else if (transicion == '\'')
{
return 7;
}
else if (transicion == '=')
{
return 8;
}
else if (transicion == ';')
{
return 9;
}
else if (transicion == '%')
{
return 10;
}
else if (transicion == '{')
{
return 11;
}
else if (transicion == '}')
{
return 12;
}
else if (transicion == '&')
{
return 13;
}
else if (transicion == '|')
{
return 14;
}
else if (transicion == '!')
{
return 15;
}
else if (transicion == '>')
{
return 16;
}
else if (transicion == '<')
{
return 17;
}
else if (transicion == '/')
{
return 20;
}
else if (transicion == '*')
{
return 21;
}
else
{
return 19;
}
}
private int automata(int estado, char transicion)
{
int nuevoEstado = TRAND[estado, Columna(transicion)];
switch (nuevoEstado)
{
case 1:
SETClasificacion(identificador);
break;
case 2:
SETClasificacion(numero);
break;
case 11:
SETClasificacion(asignacion);
break;
case 12:
SETClasificacion(finSentencia);
break;
case 13:
SETClasificacion(operadorTermino);
break;
case 14:
SETClasificacion(operadorTermino);
break;
case 15:
SETClasificacion(incrementoTermino);
break;
case 16:
SETClasificacion(operadorFactor);
break;
case 17:
SETClasificacion(incrementoFactor);
break;
case 18:
SETClasificacion(inicioBloque);
break;
case 19:
SETClasificacion(finBloque);
break;
case 22:
SETClasificacion(operadorLogico);
break;
case 23:
SETClasificacion(operadorLogico);
break;
case 24:
SETClasificacion(operadorRelacional);
break;
case 25:
SETClasificacion(operadorRelacional);
break;
case 26:
SETClasificacion(flujo);
break;
case 27:
SETClasificacion(operadorRelacional);
break;
case 28:
SETClasificacion(cadena);
break;
case 8:
case 10:
case 20:
case 21:
SETClasificacion(caracter);
break;
case 30:
case 34:
SETClasificacion(operadorFactor);
break;
}
return nuevoEstado;
}
public void NetxToken()
{
char transicion;
string Buffer = "";
int estado = 0;
try
{
while (estado >= 0)
{
transicion = (char)Archivo.Peek(); // Lee, no avanza
estado = automata(estado, transicion);
if (Archivo.EndOfStream)
{
break;
}
if (estado > 0 && estado < 31 || estado == 34)
{
Buffer += transicion;
}
else if (estado == 31 || estado == 32)
{
Buffer = "";
}
if (estado >= 0)
{
columna++;
if (transicion == '\n')
{
renglon++;
columna = 1;
}
Archivo.Read(); // Lee y avanza en el archivo, se pierde al no asignar
}
}
if (estado == e)
{
throw new ErrorLexico(SAYClasificacion(GETClasificacion()), Buffer, renglon, columna);
}
else
{
SETContenido(Buffer);
if (GETClasificacion() == identificador)
{
if (Buffer == "private" || Buffer == "protected" || Buffer == "public")
{
SETClasificacion(zona);
}
else if (Buffer == "char" || Buffer == "int" || Buffer == "float")
{
SETClasificacion(tipoDato);
}
}
}
}
catch (ErrorLexico ELexico)
{
SETContenido("");
SETClasificacion(-1);
ImprimirError(ELexico);
}
}
protected void ImprimirError(CompiladorException ex)
{
ConsoleColor ColorActual = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine(ex.Message);
Console.WriteLine();
Console.ForegroundColor = ColorActual;
Log.WriteLine(ex.Message);
Log.Close();
Asm.Close();
Environment.Exit(1);
}
private void Cabecera(string nombre)
{
CultureInfo cultura = new CultureInfo("es-MX");
Log.WriteLine("Programador:\tAngel Leon <user@domain>");
Log.WriteLine("Compilando:\t" + nombre);
Log.WriteLine("Fecha:\t\t" + DateTime.Now.ToString("ddd dd MMM yyyy HH:mm:ss", cultura));
Asm.WriteLine("; Programador:\tAngel Leon <user@domain>");
Asm.WriteLine("; Compilando:\t" + nombre);
Asm.WriteLine("; Fecha:\t" + DateTime.Now.ToString("ddd dd MMM yyyy HH:mm:ss", cultura)); // escribir fecha de compilacion al archivo
/*
Asm.WriteLine("org 100h");
Asm.WriteLine(".MODEL small");
Asm.WriteLine(".STACK 64");
Asm.WriteLine(".DATA");
*/
Asm.WriteLine();
}
}
}