diff --git a/src/trgen/templates/CSharp/st.MyParser.cs b/src/trgen/templates/CSharp/st.MyParser.cs index c4f8b064..5fb36aae 100644 --- a/src/trgen/templates/CSharp/st.MyParser.cs +++ b/src/trgen/templates/CSharp/st.MyParser.cs @@ -20,12 +20,6 @@ public MyParser(ITokenStream input) input); } - public ParserRuleContext Doit() - { - _ParserInterpreter.Interpreter.PredictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION; - return _ParserInterpreter.Parse(1); - } - public List\ getAllPossibleParseTrees( int decision, BitSet alts, @@ -33,6 +27,7 @@ public ParserRuleContext Doit() int stopIndex, int startRuleIndex) { + _ParserInterpreter.Interpreter.PredictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION; var trees = new List\(); if ( stopIndex>=(_tokens.Size - 1) ) { // if we are pointing at EOF token @@ -50,12 +45,6 @@ public ParserRuleContext Doit() _ParserInterpreter.Reset(); _ParserInterpreter.AddDecisionOverride(decision, startIndex, alt); ParserRuleContext t = _ParserInterpreter.Parse(startRuleIndex); -// var ambigSubTree = -// Trees.GetRootOfSubtreeEnclosingRegion(t, startIndex, stopIndex); - // Use higher of overridden decision tree or tree enclosing all tokens -// if ( Trees.IsAncestorOf(_ParserInterpreter.GetOverrideDecisionRoot(), ambigSubTree) ) { -// ambigSubTree = _ParserInterpreter.GetOverrideDecisionRoot(); -// } trees.Add(t); alt = alts.NextSetBit(alt+1); } diff --git a/src/trgen/templates/CSharp/st.Test.cs b/src/trgen/templates/CSharp/st.Test.cs index dd3c5cb8..1f0a95c1 100644 --- a/src/trgen/templates/CSharp/st.Test.cs +++ b/src/trgen/templates/CSharp/st.Test.cs @@ -75,11 +75,10 @@ public static IParseTree Parse2() var parser_alts = ai.ambigAlts; var parser_startIndex = ai.startIndex; var parser_stopIndex = ai.stopIndex; - var dfa_state = Parser.Atn.states[parser_decision]; var p = Parser.RuleNames.Select((value, index) => new { value, index }) .Where(pair => (pair.value == "start_")) .Select(pair => pair.index).First(); - var parser_startRuleIndex = p; //dfa_state.ruleIndex; + var parser_startRuleIndex = p; var parser_trees = ((MyParser)Parser).getAllPossibleParseTrees( parser_decision, parser_alts, @@ -355,21 +354,25 @@ static void DoParse(ICharStream str, string input_name, int row_number) } if (show_ambig) { - var am = parser.ParseInfo.getDecisionInfo().Where(d => - d.ambiguities.Any()).Select(d => d.ambiguities).FirstOrDefault(); - if (am != null) + var decs = parser.ParseInfo.getDecisionInfo().Where(d => + d.ambiguities.Any()).Select(d => d.ambiguities).ToList(); + foreach (var decision in decs) { - foreach (AmbiguityInfo ai in am) + foreach (var ai in decision) { var parser_decision = ai.decision; var parser_alts = ai.ambigAlts; var parser_startIndex = ai.startIndex; var parser_stopIndex = ai.stopIndex; - var dfa_state = parser.Atn.states[parser_decision]; + var nfa_state = parser.Atn.states.Where(s => + { + if (s is BasicBlockStartState s2) return s2.decision == parser_decision; + else return false; + }).ToList(); var p = parser.RuleNames.Select((value, index) => new { value, index }) .Where(pair => (pair.value == "")) .Select(pair => pair.index).First(); - var parser_startRuleIndex = p; //dfa_state.ruleIndex; + var parser_startRuleIndex = p; var parser_trees = parser.getAllPossibleParseTrees( parser_decision, parser_alts, diff --git a/src/trparse/Grun.cs b/src/trparse/Grun.cs index 47f95c36..63852184 100644 --- a/src/trparse/Grun.cs +++ b/src/trparse/Grun.cs @@ -327,9 +327,9 @@ int DoParse(string parser_type, string txt, string prefix, string input_name, in else { var tuples = res2 as List>>; - var list_of_trees = new List(); foreach (var tt in tuples) { + var list_of_trees = new List(); foreach (var tree in tt.Item2) { var t2 = tree as ParserRuleContext;