diff --git a/runtime/Java/src/org/antlr/v4/runtime/Parser.java b/runtime/Java/src/org/antlr/v4/runtime/Parser.java index 3401e0208..87fe5740a 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Parser.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Parser.java @@ -751,7 +751,7 @@ public abstract class Parser extends Recognizer { boolean seenOne = false; for (int d = 0; d < _interp.decisionToDFA.length; d++) { DFA dfa = _interp.decisionToDFA[d]; - if ( dfa!=null ) { + if ( !dfa.states.isEmpty() ) { if ( seenOne ) System.out.println(); System.out.println("Decision " + dfa.decision + ":"); System.out.print(dfa.toString(getTokenNames())); diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java index 52ec341fc..3665d1e92 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java @@ -119,15 +119,6 @@ public class LexerATNSimulator extends ATNSimulator { { super(atn,sharedContextCache); this.decisionToDFA = decisionToDFA; - if ( decisionToDFA[Lexer.DEFAULT_MODE]==null ) { // create all mode dfa - synchronized (this.decisionToDFA) { - if ( decisionToDFA[Lexer.DEFAULT_MODE]==null ) { // create all mode dfa - for (int i=0; i diff --git a/tool/src/org/antlr/v4/tool/interp/LexerInterpreter.java b/tool/src/org/antlr/v4/tool/interp/LexerInterpreter.java index 5870dfff9..aca6a42ce 100644 --- a/tool/src/org/antlr/v4/tool/interp/LexerInterpreter.java +++ b/tool/src/org/antlr/v4/tool/interp/LexerInterpreter.java @@ -53,7 +53,7 @@ public class LexerInterpreter implements TokenSource { /** How to create token objects */ protected TokenFactory _factory = CommonTokenFactory.DEFAULT; - protected final DFA[] _decisionToDFA = new DFA[1]; + protected final DFA[] _decisionToDFA; protected final PredictionContextCache _sharedContextCache = new PredictionContextCache(); @@ -65,6 +65,10 @@ public class LexerInterpreter implements TokenSource { public LexerInterpreter(LexerGrammar g) { Tool antlr = new Tool(); antlr.process(g,false); + _decisionToDFA = new DFA[g.atn.getNumberOfDecisions()]; + for (int i = 0; i < _decisionToDFA.length; i++) { + _decisionToDFA[i] = new DFA(g.atn.getDecisionState(i), i); + } interp = new LexerATNSimulator(g.atn,_decisionToDFA,_sharedContextCache); } diff --git a/tool/src/org/antlr/v4/tool/interp/ParserInterpreter.java b/tool/src/org/antlr/v4/tool/interp/ParserInterpreter.java index c94f52406..7221a0f83 100644 --- a/tool/src/org/antlr/v4/tool/interp/ParserInterpreter.java +++ b/tool/src/org/antlr/v4/tool/interp/ParserInterpreter.java @@ -46,15 +46,20 @@ import org.antlr.v4.tool.Grammar; public class ParserInterpreter { public static class DummyParser extends Parser { + public final ATN atn; public final DFA[] decisionToDFA; // not shared for interp public final PredictionContextCache sharedContextCache = new PredictionContextCache(); public Grammar g; - public DummyParser(Grammar g, TokenStream input) { + public DummyParser(Grammar g, ATN atn, TokenStream input) { super(input); this.g = g; - decisionToDFA = new DFA[100]; + this.atn = atn; + this.decisionToDFA = new DFA[atn.getNumberOfDecisions()]; + for (int i = 0; i < decisionToDFA.length; i++) { + decisionToDFA[i] = new DFA(atn.getDecisionState(i), i); + } } @Override @@ -74,9 +79,7 @@ public class ParserInterpreter { @Override public ATN getATN() { - return null; - } - static { + return atn; } } @@ -92,7 +95,7 @@ public class ParserInterpreter { public ParserInterpreter(@NotNull Grammar g, @NotNull TokenStream input) { Tool antlr = new Tool(); antlr.process(g,false); - parser = new DummyParser(g, input); + parser = new DummyParser(g, g.atn, input); atnSimulator = new ParserATNSimulator(parser, g.atn, parser.decisionToDFA, parser.sharedContextCache); diff --git a/tool/test/org/antlr/v4/test/BaseTest.java b/tool/test/org/antlr/v4/test/BaseTest.java index ae8913edd..fa0e5f877 100644 --- a/tool/test/org/antlr/v4/test/BaseTest.java +++ b/tool/test/org/antlr/v4/test/BaseTest.java @@ -236,7 +236,7 @@ public abstract class BaseTest { ATN atn, CharStream input) { - LexerATNSimulator interp = new LexerATNSimulator(atn,new DFA[1],null); + LexerATNSimulator interp = new LexerATNSimulator(atn,new DFA[] { new DFA(atn.modeToStartState.get(Lexer.DEFAULT_MODE)) },null); List tokenTypes = new ArrayList(); int ttype; boolean hitEOF = false; diff --git a/tool/test/org/antlr/v4/test/TestATNInterpreter.java b/tool/test/org/antlr/v4/test/TestATNInterpreter.java index 95b55d7b5..38a7f5c9b 100644 --- a/tool/test/org/antlr/v4/test/TestATNInterpreter.java +++ b/tool/test/org/antlr/v4/test/TestATNInterpreter.java @@ -31,6 +31,7 @@ package org.antlr.v4.test; import org.antlr.v4.automata.ParserATNFactory; +import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.NoViableAltException; import org.antlr.v4.runtime.atn.ATN; import org.antlr.v4.runtime.atn.ATNState; @@ -381,7 +382,7 @@ public class TestATNInterpreter extends BaseTest { int expected) { ATN lexatn = createATN(lg, true); - LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn,new DFA[1],null); + LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn,new DFA[] { new DFA(lexatn.modeToStartState.get(Lexer.DEFAULT_MODE)) },null); IntegerList types = getTokenTypesViaATN(inputString, lexInterp); System.out.println(types); diff --git a/tool/test/org/antlr/v4/test/TestATNParserPrediction.java b/tool/test/org/antlr/v4/test/TestATNParserPrediction.java index dcfcaa539..1839df6e2 100644 --- a/tool/test/org/antlr/v4/test/TestATNParserPrediction.java +++ b/tool/test/org/antlr/v4/test/TestATNParserPrediction.java @@ -32,6 +32,7 @@ package org.antlr.v4.test; import org.antlr.v4.Tool; import org.antlr.v4.automata.ParserATNFactory; +import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.NoViableAltException; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.TokenStream; @@ -460,7 +461,7 @@ public class TestATNParserPrediction extends BaseTest { Tool.internalOption_ShowATNConfigsInDFA = true; ATN lexatn = createATN(lg, true); LexerATNSimulator lexInterp = - new LexerATNSimulator(lexatn,new DFA[1],new PredictionContextCache()); + new LexerATNSimulator(lexatn,new DFA[] { new DFA(lexatn.modeToStartState.get(Lexer.DEFAULT_MODE)) },new PredictionContextCache()); IntegerList types = getTokenTypesViaATN(inputString, lexInterp); System.out.println(types); @@ -550,7 +551,7 @@ public class TestATNParserPrediction extends BaseTest { // Tool.internalOption_ShowATNConfigsInDFA = true; ATN lexatn = createATN(lg, true); LexerATNSimulator lexInterp = - new LexerATNSimulator(lexatn,new DFA[1], new PredictionContextCache()); + new LexerATNSimulator(lexatn,new DFA[] { new DFA(lexatn.getDecisionState(Lexer.DEFAULT_MODE)) }, new PredictionContextCache()); semanticProcess(lg); g.importVocab(lg);