-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalizadorLexico.h
157 lines (111 loc) · 5.62 KB
/
AnalizadorLexico.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
#ifndef __Compilador__AnalizadorLexico__
#define __Compilador__AnalizadorLexico__
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <list>
#include <Qlist>
#include <QMap>
#include <QString>
#include "EntradaTS.h"
#include "TabladeSimbolos.h"
#include "y.tab.h"
using namespace std;
#define F -2 //ESTADO FINAL
#define FST -3 //ESTADO FINAL SIN TOKEN
#define TOKEN_INVALIDO -3
#define FINARCHIVO 19
static const int matrizTE [20][21] ={
/* @ l dig L +- * = < > () / \b \n 'e' 'E' _ 'i' . ' $ OTRO
,; \t (EOF)
{} */
{ 1 , 2 , 3 , 5 , 6 , 6 , 7 , 8 , 9 ,10 ,11 , 0 , 0 , 2 , 5 ,FST, 2 ,19 ,18 ,FST,FST},
{FST, 2 ,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST, 2 ,FST,FST, 2 ,FST,FST,FST,FST},
{ 2 , 2 , 2 , F , F , F , F , F , F , F , F , F , F , 2 , F , F , 2 , F , F , F , F },
{FST,FST, 3 ,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,15 ,15 , 4 ,FST,14 ,FST,FST,FST},
{FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST, F ,FST,FST,FST,FST},
{ F , F , F , 5 , F , F , F , F , F , F , F , F , F , F , 5 , F , F , F , F , F , F },
{ F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F },
{ F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F },
{ F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F },
{ F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F },
{ F , F , F , F , F , F , F , F , 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 , F , F , F , F , F },
{12 ,12 ,12 ,12 ,12 ,12 ,12 ,12 ,12 ,12 ,13 ,12 ,12 ,12 ,12 ,12 ,12 ,12 ,12 ,FST,12 },
{12 ,12 ,12 ,12 ,12 ,12 ,12 ,12 ,12 ,12 ,FST,12 ,12 ,12 ,12 ,12 ,12 ,12 ,12 ,FST,12 },
{ F , F ,14 , F , F , F , F , F , F , F , F , F , F ,15 ,15 , F , F , F , F , F , F },
{FST,FST,16 ,FST,17 ,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST},
{ F , F ,16 , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F ,FST, F },
{FST,FST,16 ,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST},
{18 ,18 ,18 ,18 ,18 ,18 ,18 ,18 ,18 ,18 ,18 ,18 ,FST,18 ,18 ,18 ,18 ,18 , F ,FST,18 },
{FST,FST,14 ,FST,FST,FST,FST,FST,FST,FST,FST,FST,FST,15 ,15 ,FST,FST,FST,FST,FST,FST}
};
class AnalizadorLexico {
private:
int lineas;
list<char> entrada;
list<QString> * warnings;
list<QString> * errores;
QMap <QString,int> palabrasReservadas;
TablaDeSimbolos * tablaDeSimbolos;
int (AnalizadorLexico::*matrizAS [20][21])(QString*, char);
//solo para facilitar la impresión final de los tokens
list<QString> tokensReconocidos;
void imprimirToken (QString * buffer, int devolucion);
/** Otros métodos privados **/
void error(string str);
void inicializarMatrizAs();
void inicializarPR ();
float toFloat (QString * buffer);
//Devuelve la columna correspondiente al caracter leído
int columna (char c);
/** Acciones Semánticas **/
// Reconoce un identificador. Devuelve ultimo char a la entrada y chequea longitud.
int accionS1(QString * buffer, char c);
// Sólo reconoce un identificador. Se utiliza al llegar a EOF.
int accionS1F(QString * buffer, char c);
// Reconoce palabra reservada. Devuelve ultimo char a la entrada.
int accionS2(QString * buffer, char c);
// Sólo reconoce palabra reservada. Se utiliza al llegar a EOF.
int accionS2F(QString * buffer, char c);
// Reconoce una constante
int accionS3(QString * buffer, char c);
// Reconoce operadores dobles (<>, <=, ==, etc).
int accionS4(QString * buffer, char c);
// Acción semantica al reconocer operadores simples (=, +, -, "(", ")", etc)
int accionS5(QString * buffer, char c);
// Reconoce operadores simples cuando se llega a EOF.
int accionS5F(QString * buffer, char c);
// Reconoce y verifica constantes flotantes.
int accionS6(QString * buffer, char c);
// Reconoce y verifica constantes flotantes cuando se llega a EOF. /*COMPLETAR/
int accionS6F(QString * buffer, char c);
// Reconoce y verifica una cadena. Consume ultimo char y actualiza la tabla de símbolos
int accionS7(QString * buffer, char c);
// Acción semántica vacía
int accionSV(QString * buffer, char c);
// Sólo suma las lineas leidas
int accionSL(QString * buffer, char c);
// Sólo almacena el char leído al buffer. Para pasos intermedios
int accionSA(QString * buffer, char c);
// Reconoce error en una cadena.
int accionSEC(QString * buffer, char c);
// Acción que anuncia error de token invalido.
int accionSTI(QString * buffer, char c);
// Acción que anuncia error de identificador.
int accionSEI (QString *buffer, char c);
// Acción que anuncia error de constante.
int accionSECTE (QString *buffer, char c);
// Acción que anuncia error de constante flotante.
int accionSEF (QString *buffer, char c);
// Acción que anuncia error de comentario.
int accionSECOM (QString *buffer, char c);
public:
~AnalizadorLexico();
AnalizadorLexico (const char * file, list<QString>* warnings, list<QString>* errores, TablaDeSimbolos * ts);
int lineaActual();
int yylex(void);
/*Los tokens se piden de a uno. El siguiente metodo es solo para imprimirlos todos al final*/
list<QString> getTokens();
};
#endif /* defined(__Compilador__AnalizadorLexico__) */