playing with tests.

This commit is contained in:
Terence Parr 2012-09-24 08:54:32 -07:00
parent f43cd15dd9
commit 4bde79a666
2 changed files with 29 additions and 3 deletions

View File

@ -1,6 +1,7 @@
grammar T;
s : INT { System.out.println($start.getText());} ;
s : INT+ ;
INT : [0-9]+ {$x.type = 3; String x = $text; $channel, $mode} ;
ID : [a-z]+ ;
INT : [0-9]+ ;
WS : [ \t\n]+ -> skip ;

View File

@ -1,8 +1,12 @@
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenFactory;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ConsoleErrorListener;
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.UnbufferedCharStream;
import java.io.FileInputStream;
@ -26,10 +30,31 @@ public class TestT {
parser.addErrorListener(new DiagnosticErrorListener());
ParserRuleContext tree = null;
parser.getInterpreter().setSLL(true); // try with just SLL(*)
// no errors messages or recovery wanted during first try
parser.removeErrorListeners();
parser.setErrorHandler(new BailErrorStrategy());
try {
tree = parser.s();
}
catch (RuntimeException ex) {
if (ex.getClass() == RuntimeException.class &&
ex.getCause() instanceof RecognitionException)
{
System.out.println("trying with LL(*)");
tokens.reset(); // rewind
// back to standard listeners/handlers
parser.addErrorListener(ConsoleErrorListener.INSTANCE);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.getInterpreter().setSLL(false); // try full LL(*)
tree = parser.s();
}
}
// parser.getInterpreter().setSLL(true);
// parser.setTrace(true);
ParserRuleContext tree = parser.s();
System.out.println(tree.toStringTree(parser));
// tree.save(parser, "/tmp/t.ps");
}