Skip to content

Commit

Permalink
Clean up templates for ambig parses. Fix problem in ambig trees reset…
Browse files Browse the repository at this point in the history
… per decision in trparse.
  • Loading branch information
kaby76 committed Sep 15, 2024
1 parent 1835dde commit 4b20beb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
13 changes: 1 addition & 12 deletions src/trgen/templates/CSharp/st.MyParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@ public MyParser(ITokenStream input)
input);
}

public ParserRuleContext Doit()
{
_ParserInterpreter.Interpreter.PredictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION;
return _ParserInterpreter.Parse(1);
}

public List\<ParserRuleContext> getAllPossibleParseTrees(
int decision,
BitSet alts,
int startIndex,
int stopIndex,
int startRuleIndex)
{
_ParserInterpreter.Interpreter.PredictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION;
var trees = new List\<ParserRuleContext>();

if ( stopIndex>=(_tokens.Size - 1) ) { // if we are pointing at EOF token
Expand All @@ -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);
}
Expand Down
19 changes: 11 additions & 8 deletions src/trgen/templates/CSharp/st.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 == "<start_symbol>"))
.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,
Expand Down
2 changes: 1 addition & 1 deletion src/trparse/Grun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ int DoParse(string parser_type, string txt, string prefix, string input_name, in
else
{
var tuples = res2 as List<Tuple<int, List<IParseTree>>>;
var list_of_trees = new List<UnvParseTreeNode>();
foreach (var tt in tuples)
{
var list_of_trees = new List<UnvParseTreeNode>();
foreach (var tree in tt.Item2)
{
var t2 = tree as ParserRuleContext;
Expand Down

0 comments on commit 4b20beb

Please sign in to comment.