test profiler with generated code. dump profile information using toString as dump() no longer available.

This commit is contained in:
Terence Parr 2014-06-10 13:28:42 -07:00 committed by Sam Harwell
parent 4aa3556f1c
commit a1c0319b41
2 changed files with 29 additions and 1 deletions

View File

@ -1025,6 +1025,7 @@ public abstract class BaseTest {
"import org.antlr.v4.runtime.*;\n" +
"import org.antlr.v4.runtime.tree.*;\n" +
"import org.antlr.v4.runtime.atn.*;\n" +
"import java.util.Arrays;\n"+
"\n" +
"public class Test {\n" +
" public static void main(String[] args) throws Exception {\n" +
@ -1035,7 +1036,7 @@ public abstract class BaseTest {
" parser.setBuildParseTree(true);\n" +
" <profile>\n"+
" ParserRuleContext tree = parser.<parserStartRuleName>();\n" +
" <if(profile)>profiler.dump();<endif>\n" +
" <if(profile)>System.out.println(Arrays.toString(profiler.getDecisionInfo()));<endif>\n" +
" ParseTreeWalker.DEFAULT.walk(new TreeShapeListener(), tree);\n" +
" }\n" +
"\n" +

View File

@ -208,6 +208,7 @@ public class TestParserProfiler extends BaseTest {
// pred forces to
// ambig and ('+' e)* tail recursion forces lookahead to fall out of e
// any non-precedence predicates are always evaluated as true by the interpreter
DecisionInfo[] info = interpAndGetDecisionInfo(lg, g, "s", "a+b+c;");
// at "+b" it uses k=1 and enters loop then calls e for b...
// e matches and d=2 uses "+c;" for k=3
@ -220,6 +221,32 @@ public class TestParserProfiler extends BaseTest {
assertEquals(expecting, Arrays.toString(info));
}
@Test public void testProfilerGeneratedCode() throws Exception {
String grammar =
"grammar T;\n" +
"s : a+ ID EOF ;\n" +
"a : ID ';'{}\n" +
" | ID '.'\n" +
" ;\n"+
"WS : [ \\r\\t\\n]+ -> channel(HIDDEN) ;\n" +
"SEMI : ';' ;\n" +
"DOT : '.' ;\n" +
"ID : [a-zA-Z]+ ;\n" +
"INT : [0-9]+ ;\n" +
"PLUS : '+' ;\n" +
"MULT : '*' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s",
"xyz;abc;z.q", false, true);
String expecting =
"[{decision=0, contextSensitivities=0, errors=0, ambiguities=0, SLL_lookahead=6, SLL_ATNTransitions=4, " +
"SLL_DFATransitions=2, LL_Fallback=0, LL_lookahead=0, LL_ATNTransitions=0}," +
" {decision=1, contextSensitivities=0, errors=0, ambiguities=0, SLL_lookahead=6, " +
"SLL_ATNTransitions=3, SLL_DFATransitions=3, LL_Fallback=0, LL_lookahead=0, LL_ATNTransitions=0}]\n";
assertEquals(expecting, found);
assertEquals(null, stderrDuringParse);
}
public DecisionInfo[] interpAndGetDecisionInfo(
LexerGrammar lg, Grammar g,
String startRule, String... input)