Skip to content

Commit 3154fa5

Browse files
author
oitoh
committed
Main: Melhorando detecção de erros
1 parent 594652d commit 3154fa5

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

src/main/java/br/usp/sistemasreativos/Main.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import br.usp.sistemasreativos.grammar.CTLLexer;
44
import br.usp.sistemasreativos.grammar.CTLParser;
5-
import org.antlr.v4.runtime.ANTLRInputStream;
6-
import org.antlr.v4.runtime.CommonTokenStream;
5+
import org.antlr.v4.runtime.*;
6+
import org.antlr.v4.runtime.misc.ParseCancellationException;
77
import org.antlr.v4.runtime.tree.ParseTree;
88
import org.antlr.v4.runtime.tree.ParseTreeWalker;
99

@@ -14,6 +14,17 @@
1414
import java.util.Iterator;
1515
import java.util.List;
1616

17+
class ThrowingErrorListener extends BaseErrorListener {
18+
19+
public static final ThrowingErrorListener INSTANCE = new ThrowingErrorListener();
20+
21+
@Override
22+
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e)
23+
throws ParseCancellationException {
24+
throw new ParseCancellationException("line " + line + ":" + charPositionInLine + " " + msg);
25+
}
26+
}
27+
1728
public class Main {
1829
public static void main(String[] args) throws IOException {
1930
// NormalizedCTLEmitter.testAppMain(args);
@@ -49,8 +60,17 @@ public static void defaultMain(String[] args) throws IOException {
4960
CTLParser parser = new CTLParser(
5061
new CommonTokenStream(new CTLLexer(new ANTLRInputStream(CTLExpression)))
5162
);
63+
parser.removeErrorListeners();
64+
parser.addErrorListener(ThrowingErrorListener.INSTANCE);
5265
parser.setBuildParseTree(true);
53-
ParseTree expressionTree = parser.expr();
66+
ParseTree expressionTree = null;
67+
try {
68+
expressionTree = parser.expr();
69+
} catch (Exception e) {
70+
System.out.println(e.getMessage());
71+
return;
72+
}
73+
5474
NormalizedCTLEmitter converter = new NormalizedCTLEmitter();
5575
walker.walk(converter, expressionTree);
5676

src/main/java/br/usp/sistemasreativos/StateMachine.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public StateMachine(int size) {
8282
}
8383

8484
public boolean addProperty(int stateId, String propertyName) {
85-
System.out.println("stateId = " + stateId + " propertyName = " + propertyName);
85+
System.err.println("stateId = " + stateId + " propertyName = " + propertyName);
8686
return stateList.get(stateId).addProperty(propertyName);
8787
}
8888

@@ -91,7 +91,7 @@ public boolean addNextState(int stateId, int idNextState) {
9191
return false;
9292
}
9393
try {
94-
System.out.println("stateId = " + stateId + " nextState = " + idNextState);
94+
System.err.println("stateId = " + stateId + " nextState = " + idNextState);
9595
return stateList.get(stateId).addNextState(stateList.get(idNextState));
9696
} catch (Exception e) {
9797
return false;
@@ -178,7 +178,7 @@ public boolean ex(int idLabel, int label) {
178178
// rotula com idlabel todos que tem um dos próximos estados o rótulo label
179179
if (stateList.get(i).verifyNextStateLabel(label)) {
180180
stateList.get(i).addLabel(idLabel);
181-
//System.out.println("label "+idLabel +" adicionado no estado "+i);
181+
//System.err.println("label "+idLabel +" adicionado no estado "+i);
182182
}
183183
}
184184
return true;
@@ -267,7 +267,7 @@ public static StateMachine buildKISSFormat(BufferedReader bufferedReader) throws
267267
//vectorPointer: ponteiro do vetor words
268268

269269
if (idLinha != Integer.parseInt(words[0])) {
270-
System.out.println("Nome do estado incorreto, esperado " + idLinha + ", recebido " + words[0]);
270+
System.err.println("Nome do estado incorreto, esperado " + idLinha + ", recebido " + words[0]);
271271
return null;
272272
}
273273
int propVectorPointer = Integer.parseInt(words[1]) + 2;
@@ -279,8 +279,8 @@ public static StateMachine buildKISSFormat(BufferedReader bufferedReader) throws
279279
for (vectorPointer = propVectorPointer + 1; vectorPointer < maxVectorPointer; vectorPointer++) {
280280
//adiciona proximos estados ao estado idLinha
281281
if (!stateMachine.addNextState(idLinha, Integer.parseInt(words[vectorPointer]))) {
282-
System.out.println("Estado <" + Integer.parseInt(words[vectorPointer]) + "> inexistente");
283-
System.out.println("nao foi possivel adicionar estado <" + Integer.parseInt(words[vectorPointer]) + "> ao estado <" + idLinha + ">");
282+
System.err.println("Estado <" + Integer.parseInt(words[vectorPointer]) + "> inexistente");
283+
System.err.println("nao foi possivel adicionar estado <" + Integer.parseInt(words[vectorPointer]) + "> ao estado <" + idLinha + ">");
284284
return null;
285285
}
286286
}
@@ -289,8 +289,8 @@ public static StateMachine buildKISSFormat(BufferedReader bufferedReader) throws
289289

290290
return stateMachine;
291291
} catch (NumberFormatException ex) {
292-
System.out.println("Máquina de estado mal formado, espera-se integer nos seguintes valores");
293-
System.out.println("Qtd de linhas, Qtd de estados, Nome dos estados, Qtd de propriedades");
292+
System.err.println("Máquina de estado mal formado, espera-se integer nos seguintes valores");
293+
System.err.println("Qtd de linhas, Qtd de estados, Nome dos estados, Qtd de propriedades");
294294
return null;
295295
}
296296
}
@@ -299,7 +299,7 @@ public static void testMainApp(String[] args) {
299299
StateMachine stateMachine;
300300
String fileName;
301301
BufferedReader bufferedReader;
302-
System.out.println("Digite o arquivo da maquina de estado");
302+
System.err.println("Digite o arquivo da maquina de estado");
303303
Scanner sc = new Scanner(System.in);
304304
fileName = sc.nextLine();
305305
sc.close();
@@ -324,42 +324,42 @@ public static void testMainApp(String[] args) {
324324
List<Integer> labelList = new ArrayList<Integer>();
325325
labelList = stateMachine.getStateLabel(1);
326326
for (int stateLabel:labelList){
327-
System.out.println("stados p "+stateLabel);
327+
System.err.println("stados p "+stateLabel);
328328
}
329329
labelList = stateMachine.getStateLabel(2);
330330
for (int stateLabel:labelList){
331-
System.out.println("stados q "+stateLabel);
331+
System.err.println("stados q "+stateLabel);
332332
}
333333
labelList = stateMachine.getStateLabel(3);
334334
for (int stateLabel:labelList){
335-
System.out.println("stados s "+stateLabel);
335+
System.err.println("stados s "+stateLabel);
336336
}
337337
labelList = stateMachine.getStateLabel(4);
338338
for (int stateLabel:labelList){
339-
System.out.println("stados r "+stateLabel);
339+
System.err.println("stados r "+stateLabel);
340340
}
341341
labelList = stateMachine.getStateLabel(5);
342342
for (int stateLabel:labelList){
343-
System.out.println("stados eu(p,q) "+stateLabel);
343+
System.err.println("stados eu(p,q) "+stateLabel);
344344
}
345345
labelList = stateMachine.getStateLabel(6);
346346
for (int stateLabel:labelList){
347-
System.out.println("stados and(p,q) "+stateLabel);
347+
System.err.println("stados and(p,q) "+stateLabel);
348348
}
349349
labelList = stateMachine.getStateLabel(7);
350350
for (int stateLabel:labelList){
351-
System.out.println("stados or(p,q) "+stateLabel);
351+
System.err.println("stados or(p,q) "+stateLabel);
352352
}
353353
labelList = stateMachine.getStateLabel(8);
354354
for (int stateLabel:labelList){
355-
System.out.println("stados not(p) "+stateLabel);
355+
System.err.println("stados not(p) "+stateLabel);
356356
}
357357
//*/
358358

359359
} catch (NumberFormatException ex) {
360-
System.out.println(ex);
360+
System.err.println(ex);
361361
} catch (IOException ex) {
362-
System.out.println(ex);
362+
System.err.println(ex);
363363
}
364364
}
365365

testes/valida

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
2 2 p r 2 2 4
44
3 2 q r 1 1
55
4 1 q 1 4
6-
( AF ( EX ( q ) -> AF ( p ) ) )
6+
( AUU(q, p) )

0 commit comments

Comments
 (0)