-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHello.g4
315 lines (278 loc) · 8.63 KB
/
Hello.g4
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
/**
* Define a grammar called Hello
*/
grammar Hello;
@header{
import java.util.ArrayList;
import java.util.List;
import symbols.DataType;
import java.util.Stack;
import symbols.Identifier;
import symbols.SymbolTable;
import expressions.*;
import ast.*;
import java.util.HashMap;
import java.util.Map;
}
@members{
private SymbolTable symbolTable = new SymbolTable();
private DataType currentType;
private AbstractExpression expression;
private String operator;
private String unaryOperator;
private DataType leftDT;
private DataType rightDT;
private String idDeclarado, idAtribuido;
private boolean isExprParenthesisOpened = false;
private boolean condicaoEnquanto = false;
private List<Identifier> declaredIds;
private String text;
private Program program = new Program();
private Stack<List<AbstractCommand>> stack = new Stack<List<AbstractCommand>>();
private Map<String, Boolean> mapaUsoVariaveis = new HashMap<>();
private Map<String, Boolean> mapaVarsInicializadas = new HashMap<>();
public void init(){
program.setSymbolTable(symbolTable);
stack.push(new ArrayList<AbstractCommand>());
}
public void exibirID(){
symbolTable.getSymbols().values().stream().forEach((id)->System.out.println(id));
}
public void generateObjectCode(){
program.generateTarget();
}
}
programa : 'programa' decl+ cmd+ 'fimprog.'
{
for (Map.Entry<String, Boolean> entry : mapaUsoVariaveis.entrySet()) {
if(entry.getValue() == false){
System.out.println("A variavel: " + entry.getKey() + " não foi utilizada.");
}
}
System.out.println();
program.setComandos(stack.pop());
}
;
decl : tipo lista_var PF {
Declaration _decl = new Declaration(declaredIds);
stack.peek().add(_decl);
}
;
tipo : 'INTEGER' { currentType = DataType.INTEGER; }
| 'REAL' { currentType = DataType.REAL; }
;
lista_var : ID {
declaredIds = new ArrayList<>();
idDeclarado = _input.LT(-1).getText();
if (symbolTable.get(idDeclarado) != null) {
throw new RuntimeException("Variable " + idDeclarado + " already has been declared");
}
mapaUsoVariaveis.put(idDeclarado,false);
mapaVarsInicializadas.put(idDeclarado, false);
symbolTable.add(idDeclarado, new Identifier(idDeclarado, currentType));
declaredIds.add(symbolTable.get(idDeclarado));
}
(VIRG
ID {
idDeclarado = _input.LT(-1).getText();
if (symbolTable.get(idDeclarado) != null) {
throw new RuntimeException("Variable \"" + idDeclarado + "\" already has been declared");
}
mapaUsoVariaveis.put(idDeclarado,false);
mapaVarsInicializadas.put(idDeclarado, false);
symbolTable.add(idDeclarado, new Identifier(idDeclarado, currentType));
declaredIds.add(symbolTable.get(idDeclarado));
}
)*
;
cmd : cmdAttr | cmdRead | cmdWrite | cmdIf | cmdWhile
;
cmdWhile : 'enquanto'{
stack.push(new ArrayList<AbstractCommand>());
BinaryExpression _relExpr = new BinaryExpression();
CmdWhile _cmdWhile = new CmdWhile();
}
AP expr {
_relExpr.setLeftSide(expression);
}
OPREL {
_relExpr.setOperator(_input.LT(-1).getText());
}
expr {
_relExpr.setRightSide(expression);
_cmdWhile.setExpr(_relExpr);
// limpando operadores para validacoes de comparacoes futuras
leftDT = null;
rightDT =null;
}
FP 'realize' cmd+ {
_cmdWhile.setCommands(stack.pop());
}
'fimenquanto' PF{
stack.peek().add(_cmdWhile);
}
;
cmdIf : 'se' {
stack.push(new ArrayList<AbstractCommand>());
BinaryExpression _relExpr = new BinaryExpression();
CmdIf _cmdIf = new CmdIf();
}
AP expr {
_relExpr.setLeftSide(expression);
}
OPREL {
_relExpr.setOperator(_input.LT(-1).getText());
}
expr {
_relExpr.setRightSide(expression);
_cmdIf.setExpr(_relExpr);
// limpando operadores para validacoes de comparacoes futuras
leftDT = null;
rightDT =null;
} FP 'entao' cmd+
{
_cmdIf.setListaTrue(stack.pop());
}
('senao' {
stack.push(new ArrayList<AbstractCommand>());
}
cmd+ {
_cmdIf.setListaFalse(stack.pop());
})?
'fimse' PF {
stack.peek().add(_cmdIf);
}
;
cmdRead : 'leia' AP ID {
Identifier id = symbolTable.get(_input.LT(-1).getText());
if (id == null){
throw new RuntimeException("Undeclared Variable");
}
mapaVarsInicializadas.put(id.getText(), true);
CmdRead _read = new CmdRead(id);
stack.peek().add(_read);
}
FP PF
;
cmdWrite : 'escreva' AP (
ID {
Identifier id = symbolTable.get(_input.LT(-1).getText());
if (id == null){
throw new RuntimeException("Undeclared Variable");
}
CmdWrite _write = new CmdWrite(id);
stack.peek().add(_write);
}
|
TEXT {
CmdWrite _write = new CmdWrite(_input.LT(-1).getText());
stack.peek().add(_write);
}
) FP PF
;
cmdAttr : ID {
idAtribuido = _input.LT(-1).getText();
if (!symbolTable.exists(_input.LT(-1).getText())){
throw new RuntimeException("Semantic ERROR - Undeclared Identifier");
}
mapaVarsInicializadas.put(idAtribuido, true);
leftDT = symbolTable.get(_input.LT(-1).getText()).getType();
rightDT = null;
}
ATTR expr PF
{
// logica para atribuir o valor da expressao no identificador
Identifier id = symbolTable.get(idAtribuido);
symbolTable.add(idAtribuido, id);
CmdAttrib _attr = new CmdAttrib(id, expression);
stack.peek().add(_attr);
expression = null;
leftDT = null;
rightDT = null;
}
;
expr : termo exprl*
;
termo : NUMBER
{
expression = new NumberExpression(Integer.parseInt(_input.LT(-1).getText()));
}
|
DECIMAL_NUMBER{
expression = new NumberDecimalExpression(Float.parseFloat(_input.LT(-1).getText()));
}
|
ID {
// checks for existence in symbolTable; throws RuntimeError if not present
if (!symbolTable.exists(_input.LT(-1).getText())){
throw new RuntimeException("Semantic ERROR - Undeclared Identifier: "+_input.LT(-1).getText());
}
// checks for left-hand side and right-hand side type equality; throws if mismatch
rightDT = symbolTable.get(_input.LT(-1).getText()).getType();
//como a expressão é reusada, usamos o if abaixo para atribuicao de tipo do lado esquerdo da expressao. Apos isso ele ira ler o valor do lado direito.
if(leftDT == null){
leftDT = symbolTable.get(_input.LT(-1).getText()).getType();
};
if (leftDT != rightDT){
throw new RuntimeException("Semantic ERROR - Type Mismatching "+leftDT+ "-"+rightDT);
}
// checks for initialization; if initialized generate new NumberExpression, throws otherwise
Identifier id = symbolTable.get(_input.LT(-1).getText());
if (mapaVarsInicializadas.get(id.getText())){ //nome - v se inicializado, false caso contrario
mapaUsoVariaveis.put(id.getText(),true);
expression = new IDExpression(id);
}
else{
throw new RuntimeException("Semantic ERROR - Unassigned variable: " + id.getText());
}
}
| AP expr FP {
expression = new ParenthesisExpression(expression);
}
| (SUM | SUB) {
unaryOperator = _input.LT(-1).getText();
} expr {
expression = new UnaryExpression(unaryOperator, expression);
}
;
exprl : (SUM | SUB | MULT | DIV) {
operator = _input.LT(-1).getText();
BinaryExpression _exprADD = new BinaryExpression(operator);
_exprADD.setLeftSide(expression);
}
termo
{
_exprADD.setRightSide(expression);
expression = _exprADD;
}
;
NUMBER : [0-9]+
;
DECIMAL_NUMBER : [0-9]+('.' [0-9]+)?
;
TEXT : '"' (~["\\\r\n])* '"' // tudo entre aspas menos " \ \r \n
;
ATTR : ':='
;
SUM : '+'
;
SUB : '-'
;
MULT : '*'
;
DIV : '/'
;
OPREL : '>' | '>=' | '<' | '<=' | '==' | '!='
;
ID : [a-z] ([a-z]|[A-Z]|[0-9])*
;
VIRG : ','
;
PF : '.'
;
AP : '('
;
FP : ')'
;
BLANK : (' '| '\t' | '\n' | '\r') -> skip
;