-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.java
79 lines (68 loc) · 1.87 KB
/
Token.java
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
/*
Laboratorio No. 3 - Recursive Descent Parsing
CC4 - Compiladores
Clase que sirve para representar un token
Actualizado: agosto de 2021, Luis Cu
*/
public final class Token {
// Tokens (?)
public static final int SEMI = 0; // ;
public static final int PLUS = 1; // +
public static final int MINUS = 2; // -
public static final int MULT = 3; // *
public static final int DIV = 4; // /
public static final int MOD = 5; // %
public static final int EXP = 6; // ^
public static final int LPAREN = 7; // (
public static final int RPAREN = 8; // )
public static final int NUMBER = 9; // number
public static final int ERROR = 10; // error
public static final int UNARY = 11; // ~ menos unario
// Esto puede ser bastante util
private static final String[] tokens = {
";",
"+",
"-",
"*",
"/",
"%",
"^",
"(",
")",
"NUMBER",
"ERROR",
"~"
};
private int id;
private String val;
// Constructor de la clase
public Token(int id, String val) {
this.id = id;
this.val = val;
}
// Constructor para tokens sin val
public Token(int id) {
this(id, null);
}
// Para tener el valor de un number
public double getVal() {
return Double.parseDouble(this.val);
}
// Para tener el id de un token
public int getId() {
return this.id;
}
// Para comparar, esto es muy util
public boolean equals(int id) {
return this.id == id;
}
// Para representar un Token en String
public String toString() {
if(this.id == Token.NUMBER) {
if (this.val != null) {
return Token.tokens[this.id] + " : " + this.val;
}
}
return Token.tokens[this.id];
}
}