-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswift.cup
154 lines (134 loc) · 5.51 KB
/
swift.cup
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
import java_cup.runtime.*;
import java.util.*;
action code {:
Stack<String> identificadores = new Stack<String>();
public Boolean esFor = false;
public void inicializarVariables(Object valor, Integer tipo, int profundidad, Integer esGlobal, boolean asignado) {
for (String identificador : identificadores) {
TablaSimbolos.add(identificador, valor, tipo, profundidad, esGlobal, asignado);
}
identificadores.clear();
}
public void imprimir(Object e){
if (esFor && e instanceof ArrayList ){
for (Object o : (ArrayList)e){
System.out.println(o);
}
}else{
System.out.println(e);
}
esFor = false;
}
:}
/* TERMINALES */
terminal IF, ELSE, DO, WHILE, FOR, PRINT; // Funciones del lenguaje
terminal EQ, NE, LT, LE, GT, GE, NO, AND, OR; // Operadores lógicos
terminal SUM, RES, MUL, DIV, NEG, IGUAL; // Operadores matemáticos
terminal AP, CP, AC, CC, AL, CL; // Aperturas y cierres
terminal PYC, PP, COMA; // Fin de línea
terminal EOL; // Comentarios
terminal String IDENT;
terminal Integer NUM;
terminal Double DOUBLE;
terminal VAR,LET;
terminal TRUE,FALSE;
terminal DOUBLE_TYPE, INT_TYPE, BOOL_TYPE;
terminal IN; //Para el bucle for
non terminal lineas;
non terminal linea;
non terminal expresion;
non terminal sentencia;
non terminal endline;
non terminal tipo;
non terminal listaIdent;
non terminal varLet;
non terminal ArrayList<Object> array;
non terminal objetos_finales;
non terminal sentencia_for, for_header;
non terminal uwu;
precedence left SUM, RES;
precedence left MUL, DIV;
precedence left EQ, LE, GE;
precedence left NE, LT, GT;
precedence left NO, AND, OR;
precedence nonassoc AP, CP;
precedence nonassoc AC, CC;
precedence nonassoc AL, CL;
start with lineas;
lineas ::= linea endline lineas
|linea endline
|linea;
endline ::= endline EOL
| endline PYC EOL {:System.out.println("error"); System.exit(0);:}
| EOL
;
linea ::= sentencia PYC
| sentencia
| sentencia_for
;
sentencia ::= sentencia PYC expresion
| expresion
;
expresion ::= PRINT AP expresion:e CP {:imprimir(e);:}
| expresion:e SUM expresion:e2 {:RESULT = operations.sumar(e, e2);:}
| expresion:e RES expresion:e2 {:RESULT = operations.restar(e, e2);:}
| expresion:e MUL expresion:e2 {:RESULT = operations.multiplicar(e, e2);:}
| expresion:e DIV expresion:e2 {:RESULT = operations.dividir(e, e2);:}
| expresion:e OR expresion:e2 {:RESULT = ((boolean)e || (boolean)e2);:}
| expresion:e AND expresion:e2 {:RESULT = ((boolean)e && (boolean)e2);:}
| expresion:e EQ expresion:e2 {:RESULT = operations.eq(e, e2);:}
| expresion:e NE expresion:e2 {:RESULT = (e != e2);:}
| expresion:e LT expresion:e2 {:RESULT = operations.lt(e, e2);:}
| expresion:e LE expresion:e2 {:RESULT = operations.le(e, e2);:}
| expresion:e GT expresion:e2 {:RESULT = operations.gt(e, e2);:}
| expresion:e GE expresion:e2 {:RESULT = operations.ge(e, e2);:}
| varLet:g listaIdent tipo:v IGUAL expresion:e2 {: inicializarVariables(e2,0,v!=null?(Integer)v:0,(Integer)g, true);:}
| varLet:g listaIdent tipo:v {: inicializarVariables(null,0,v!=null?(Integer)v:0,(Integer)g, false);:}
| IDENT:e IGUAL expresion:e2 {: TablaSimbolos.replace(e, e2,0,true);:}
| AP expresion:e CP {: RESULT = e;:}
| IDENT:e {:RESULT=TablaSimbolos.get(e);:}
| NO IDENT:e {:RESULT=!(boolean)TablaSimbolos.get(e);:}
| AC array:a CC {:RESULT=a;:}
| IDENT:e AC NUM:n CC {:ArrayList<Object> a = (ArrayList)TablaSimbolos.get(e); RESULT=a.get(n);:}
| IDENT:e AC NUM:n CC IGUAL expresion:o {:TablaSimbolos.modifyArray(e, n, o);:}
| objetos_finales:e {:RESULT=e;:}
;
objetos_finales ::= NUM:e {:RESULT=e;:}
| DOUBLE:e {:RESULT=e;:}
| RES NUM:e {:RESULT=-e;:}
| RES DOUBLE:e {:RESULT=-e;:}
| FALSE {:RESULT=false;:}
| TRUE {:RESULT=true;:}
;
tipo ::= PP DOUBLE_TYPE {:RESULT=1;:}
|PP INT_TYPE {:RESULT=2;:}
|PP BOOL_TYPE {:RESULT=3;:}
|PP AC DOUBLE_TYPE CC {:RESULT=1;:}
|PP AC INT_TYPE CC {:RESULT=2;:}
|PP AC BOOL_TYPE CC {:RESULT=3;:}
|
;
listaIdent ::= listaIdent COMA IDENT:e {:identificadores.push(e);:}
| IDENT:e {:RESULT=e;identificadores.push(e);:}
;
varLet ::= VAR {:RESULT=0;:}
| LET {:RESULT=1;:}
;
array ::= objetos_finales:e COMA array:a {:ArrayList<Object> aux =new ArrayList<Object>();aux.add(e);aux.addAll(a);RESULT=aux;:}
| objetos_finales:e {:ArrayList<Object> aux =new ArrayList<Object>();aux.add(e);RESULT=aux;:}
;
sentencia_for ::= for_header:i
uwu
AL
uwu
linea
uwu
CL {:TablaSimbolos.borrar((String)i);:}
;
for_header ::= FOR {: esFor = true;:}
IDENT:i
IN IDENT:a {:RESULT=i;TablaSimbolos.add(i,(ArrayList)TablaSimbolos.get(a),0,99,0,true);:}
;
uwu ::= EOL
|
;