-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.c
More file actions
50 lines (40 loc) · 1.39 KB
/
Copy pathscanner.c
File metadata and controls
50 lines (40 loc) · 1.39 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
#include <stdio.h>
#include <ctype.h>
#include "scanner.h"
typedef enum{ESPACIO, ID, NUMERO, OTRO, FINAL, ESTADOFINAL = 10, ESTADOIDENTIFICADOR, ESTADOCONSTANTE, ESTADOERROR} estados_t;
static char _matrizDeEstados[4][5] = {
{ESPACIO, ID, NUMERO, OTRO, ESTADOFINAL},
{ESTADOIDENTIFICADOR, ID, ID, ESTADOIDENTIFICADOR, ESTADOIDENTIFICADOR},
{ESTADOCONSTANTE, ESTADOCONSTANTE, NUMERO, ESTADOCONSTANTE, ESTADOCONSTANTE},
{ESTADOERROR, ESTADOERROR, ESTADOERROR, OTRO, ESTADOERROR}
};
estados_t _tipoCaracter(char c);
int _estadoConCentinela(estados_t estado);
lexicalCategory_t _estadoToToken(estados_t estado);
/* Devuelve el lexema al que pertenece el caracter*/
estados_t _tipoCaracter(char c){
int tipoChar = OTRO;
if (isspace(c)) tipoChar = ESPACIO;
if (islower(c)) tipoChar = ID;
if (isdigit(c)) tipoChar = NUMERO;
if (c == EOF) tipoChar = FINAL;
return tipoChar;
}
lexicalCategory_t readToken(){
estados_t estadoActual = 0;
char c;
while(estadoActual < ESTADOSACEPTORES){ //lee cada token
c = getchar();
estadoActual = _matrizDeEstados[estadoActual][_tipoCaracter(c)];
}
if (_estadoConCentinela(estadoActual)){
ungetc(c, stdin);
}
return _estadoToToken(estadoActual);
}
int _estadoConCentinela(estados_t estado){
return estado - ESTADOSACEPTORES;
}
lexicalCategory_t _estadoToToken(estados_t estado){
return estado - ESTADOSACEPTORES;
}