-
Notifications
You must be signed in to change notification settings - Fork 1
/
gramatica.g
318 lines (271 loc) · 6.96 KB
/
gramatica.g
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
316
317
318
{
import br.edu.compilador.commands.*;
}
class JujuParser extends Parser;
{
private Program prog;
private Variable<?> actualVar;
private Expression actualExpression;
private LogicExpression actualLogic;
public String convertProgram()
{
return prog.convert();
}
public void init(){
prog = new Program();
}
private void atrib(int actualType) throws RecognitionException
{
if (actualVar == null) {
throw new RecognitionException("Variavel nao declarada, impossivel atribuir");
} else {
if (actualType == T_msg && (actualExpression instanceof StringExpression)) {
if (actualVar instanceof StringVariable){
prog.addCommand(new CommandAttribution(actualVar, actualExpression));
}
else
throw new RecognitionException("Ish ta atribuindo errado isso ae, verifica que tem texto nos numero");
} else if (actualType == T_num && (actualExpression instanceof MathExpression)) {
if (actualVar instanceof IntegerVariable) {
prog.addCommand(new CommandAttribution(actualVar, actualExpression));
} else {
throw new RecognitionException("Ish ta atribuindo errado isso ae, verifica que tem numero nos texto");
}
}
}
}
private Integer toInt(String actualValue)
{
if (actualValue.contains("."))
return Math.round(Float.parseFloat(actualValue));
else
return Integer.parseInt(actualValue);
}
private Object logicExpressTerm (String text, int type) throws RecognitionException
{
Object expressionTerm = null;
if(type == T_id)
{
IntegerVariable intVar = (IntegerVariable) prog.getVariable(text);
if (intVar == null)
throw new RecognitionException("Erro loco na sua expressao logica, esse bagulho nao ecxiste. Lembra que so comparo inteiro por conta de preguica mesmo.");
expressionTerm = intVar;
} else
{
expressionTerm = toInt(text);
}
return expressionTerm;
}
}
programStart: (declara)* (function)*
;
function : "function" T_id
{
prog.addCommand(new CommandFunction(LT(0).getText()));
}
(comando)+ "end"
{
prog.addCommand(new CommandReturn());
prog.addCommand(new CommandEnd());
}
;
comando : cmdLeitura
| cmdEscrita
| declara
| comandoIfElse
| comandoWhile
| atrib
;
declara : T_tipo
{
String tipo = LT(0).getText();
}
T_id
{
if (tipo.equals("Int")) {
prog.addCommand(new CommandNew (new IntegerVariable(LT(0).getText())));
} else if (tipo.equals("String")) {
prog.addCommand(new CommandNew (new StringVariable(LT(0).getText())));
}
}
T_pv
;
atrib : T_id
{
actualVar = prog.getVariable(LT(0).getText());
}
T_atrib value
{
atrib(LT(0).getType());
}
T_pv
;
value : T_id
{
Variable<?> var = (Variable<?>) prog.getVariable(LT(0).getText());
if (var == null)
throw new RecognitionException("Essa variavel ae nao existe nao");
else {
if (var instanceof IntegerVariable) {
actualExpression = new MathExpression((IntegerVariable) var);
op_math_end();
}
else {
actualExpression = new StringExpression((StringVariable) var);
op_text_end();
}
}
}
| op_math | op_text | (T_ap value T_fp)
;
comandoIfElse : "if" exprif "then" (comando)+ "end" {prog.addCommand(new CommandEnd());}
("else" {prog.addCommand(new CommandElse()); } "begin" (comando)+ "end" {prog.addCommand(new CommandEnd());})?
;
exprif : expr
{
CommandIf cmdif = new CommandIf(actualLogic);
prog.addCommand(cmdif);
}
;
comandoWhile : "while" exprwhile "then" (comando)+ "end" {prog.addCommand(new CommandEnd());}
;
exprwhile : expr
{
CommandWhile cmdwhile = new CommandWhile(actualLogic);
prog.addCommand(cmdwhile);
}
;
expr :
expressTerm
{
Object leftTerm = logicExpressTerm(LT(0).getText(), LT(0).getType());
}
operator
{
int operator = LT(0).getType();
}
expressTerm
{
Object rightTerm = logicExpressTerm(LT(0).getText(), LT(0).getType());
actualLogic = new LogicExpression(leftTerm, rightTerm, operator);
}
;
expressTerm : (T_id | T_num)
;
operator : T_or
|
T_and
|
T_eq
|
T_neq
|
T_gt
|
T_lt
;
op_math :
(
T_num {actualExpression = new MathExpression(toInt(LT(0).getText()));}
op_math_end
)
;
op_math_end :
( {int type = 0;}
(T_plus {type = LT(0).getType();}
|T_minus {type = LT(0).getType();}
|T_times {type = LT(0).getType();}
|T_div {type = LT(0).getType();}
)
(T_num {((MathExpression) actualExpression).add(toInt(LT(0).getText()), type);}
|T_id |
{
IntegerVariable intVar = (IntegerVariable) prog.getVariable(LT(0).getText());
if (intVar == null)
throw new RecognitionException("Vamo la, vc ta somando algo errado ai, vai com calma campeao.");
else
((MathExpression) actualExpression).add(new MathExpression(intVar));
}
)
)*
;
op_text :
(
T_msg {actualExpression = new StringExpression(LT(0).getText());}
op_text_end
)
;
op_text_end :
( {int type = 0;}
T_plus {type = LT(0).getType();}
T_msg {((StringExpression) actualExpression).add(LT(0).getText(), type);}
)*
;
cmdLeitura : "input" T_ap T_id
{
Variable var = prog.getVariable(LT(0).getText());
if (var == null){
System.err.println("Variavel nao declarada");
}
}
T_fp T_pv
{
prog.addCommand(new CommandRead(var));
}
;
cmdEscrita : "output" T_ap
value
{
prog.addCommand(new CommandWrite(actualExpression));
}
T_fp T_pv
;
class JujuLexer extends Lexer;
options {
k=2;
}
T_tipo : "Int" | "String"
;
T_id : ('a'..'z') (('a'..'z') | ('0'..'9'))*
;
T_vir : ','
;
T_pv : ';'
;
T_ap : '('
;
T_fp : ')'
;
T_atrib : '='
;
T_msg : '\"' ( ('a'..'z')
| ('0'..'9')
| ('A'..'Z')
| (' ')
)*
'\"'
;
T_eq : "=="
;
T_or : "||"
;
T_and : "&&"
;
T_neq : "!="
;
T_gt : ">"
;
T_lt : "<"
;
T_gteq : ">="
;
T_lteq : "<="
;
T_plus : '+' ;
T_minus : '-' ;
T_div : '/' ;
T_times : '*' ;
T_num : ('0'..'9')+ ('.' ('0'..'9')+)?
;
T_ws : ('\n' | '\r' | '\t' | ' ') { $setType(Token.SKIP); }
;