Пример #1
0
 public override void ReportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, SimulatorState conflictState)
 {
     string format = "reportAttemptingFullContext d={0}, input='{1}'";
     string decision = GetDecisionDescription(recognizer, dfa);
     string text = ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex));
     string message = string.Format(format, decision, text);
     recognizer.NotifyErrorListeners(message);
 }
Пример #2
0
        public void And(BitSet set)
        {
            if (set == null)
                throw new ArgumentNullException("set");

            int length = Math.Min(_data.Length, set._data.Length);
            for (int i = 0; i < length; i++)
                _data[i] &= set._data[i];

            for (int i = length; i < _data.Length; i++)
                _data[i] = 0;
        }
Пример #3
0
 public override void ReportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, bool exact, BitSet ambigAlts, ATNConfigSet configs)
 {
     if (exactOnly && !exact)
     {
         return;
     }
     string format = "reportAmbiguity d={0}: ambigAlts={1}, input='{2}'";
     string decision = GetDecisionDescription(recognizer, dfa);
     BitSet conflictingAlts = GetConflictingAlts(ambigAlts, configs);
     string text = ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex));
     string message = string.Format(format, decision, conflictingAlts, text);
     recognizer.NotifyErrorListeners(message);
 }
Пример #4
0
 public ParserInterpreter(string grammarFileName, IEnumerable<string> tokenNames, IEnumerable<string> ruleNames, ATN atn, ITokenStream input)
     : base(input)
 {
     this.grammarFileName = grammarFileName;
     this.atn = atn;
     this.tokenNames = tokenNames.ToArray();
     this.ruleNames = ruleNames.ToArray();
     // identify the ATN states where pushNewRecursionContext must be called
     this.pushRecursionContextStates = new BitSet(atn.states.Count);
     foreach (ATNState state in atn.states)
     {
         if (!(state is StarLoopEntryState))
         {
             continue;
         }
         if (((StarLoopEntryState)state).precedenceRuleDecision)
         {
             this.pushRecursionContextStates.Set(state.stateNumber);
         }
     }
     // get atn simulator that knows how to do predictions
     Interpreter = new ParserATNSimulator(this, atn);
 }
Пример #5
0
 private static bool TestTailCall(ATN atn, RuleTransition transition, bool optimizedPath)
 {
     if (!optimizedPath && transition.tailCall)
     {
         return true;
     }
     if (optimizedPath && transition.optimizedTailCall)
     {
         return true;
     }
     BitSet reachable = new BitSet(atn.states.Count);
     Stack<ATNState> worklist = new Stack<ATNState>();
     worklist.Push(transition.followState);
     while (worklist.Count > 0)
     {
         ATNState state = worklist.Pop();
         if (reachable.Get(state.stateNumber))
         {
             continue;
         }
         if (state is RuleStopState)
         {
             continue;
         }
         if (!state.OnlyHasEpsilonTransitions)
         {
             return false;
         }
         IList<Transition> transitions = optimizedPath ? state.optimizedTransitions : state.transitions;
         foreach (Transition t in transitions)
         {
             if (t.TransitionType != TransitionType.EPSILON)
             {
                 return false;
             }
             worklist.Push(t.target);
         }
     }
     return true;
 }
Пример #6
0
        // configs that LL not SLL considered conflicting
        /** If context sensitive parsing, we know it's ambiguity not conflict */
        protected virtual void ReportAmbiguity(DFA dfa,
									   DFAState D, // the DFA state from execATN() that had SLL conflicts
									   int startIndex, int stopIndex,
									   bool exact,
									   BitSet ambigAlts,
									   ATNConfigSet configs)
        {
            if (debug || retry_debug)
            {
                Interval interval = Interval.Of(startIndex, stopIndex);
                Console.WriteLine("ReportAmbiguity " +
                                   ambigAlts + ":" + configs +
                                   ", input=" + parser.TokenStream.GetText(interval));
            }
            if (parser != null) parser.ErrorListenerDispatch.ReportAmbiguity(parser, dfa, startIndex, stopIndex,
                                                                                  exact, ambigAlts, configs);
        }
Пример #7
0
        protected PredPrediction[] GetPredicatePredictions(BitSet ambigAlts,
																 SemanticContext[] altToPred)
        {
            List<PredPrediction> pairs = new List<PredPrediction>();
            bool containsPredicate = false;
            for (int i = 1; i < altToPred.Length; i++)
            {
                SemanticContext pred = altToPred[i];

                // unpredicated is indicated by SemanticContext.NONE

                if (ambigAlts != null && ambigAlts[i])
                {
                    pairs.Add(new PredPrediction(pred, i));
                }
                if (pred != SemanticContext.NONE) containsPredicate = true;
            }

            if (!containsPredicate)
            {
                return null;
            }

            //		Console.WriteLine(Arrays.toString(altToPred)+"->"+pairs);
            return pairs.ToArray();
        }
Пример #8
0
        /** Look through a list of predicate/alt pairs, returning alts for the
         *  pairs that win. A {@code NONE} predicate indicates an alt containing an
         *  unpredicated config which behaves as "always true." If !complete
         *  then we stop at the first predicate that evaluates to true. This
         *  includes pairs with null predicates.
         */
        protected virtual BitSet EvalSemanticContext(PredPrediction[] predPredictions,
										  ParserRuleContext outerContext,
										  bool complete)
        {
            BitSet predictions = new BitSet();
            foreach (PredPrediction pair in predPredictions)
            {
                if (pair.pred == SemanticContext.NONE)
                {
                    predictions[pair.alt] = true;
                    if (!complete)
                    {
                        break;
                    }
                    continue;
                }

                bool fullCtx = false; // in dfa
                bool predicateEvaluationResult = EvalSemanticContext(pair.pred, outerContext, pair.alt, fullCtx);
                if (debug || dfa_debug)
                {
                    Console.WriteLine("eval pred " + pair + "=" + predicateEvaluationResult);
                }

                if (predicateEvaluationResult)
                {
                    if (debug || dfa_debug) Console.WriteLine("PREDICT " + pair.alt);
                    predictions[pair.alt] = true;
                    if (!complete)
                    {
                        break;
                    }
                }
            }

            return predictions;
        }
Пример #9
0
 public virtual void ReportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, SimulatorState conflictState)
 {
 }
Пример #10
0
        public void Or(BitSet set)
        {
            if (set == null)
                throw new ArgumentNullException("set");

            if (set._data.Length > _data.Length)
                Array.Resize(ref _data, set._data.Length);

            for (int i = 0; i < set._data.Length; i++)
                _data[i] |= set._data[i];
        }
Пример #11
0
 protected internal override void ReportAmbiguity(DFA dfa, DFAState D, int startIndex, int stopIndex, bool exact, BitSet ambigAlts, ATNConfigSet configs)
 {
     int prediction;
     if (ambigAlts != null)
     {
         prediction = ambigAlts.NextSetBit(0);
     }
     else
     {
         prediction = configs.RepresentedAlternatives.NextSetBit(0);
     }
     if (conflictingAltResolvedBySLL != ATN.InvalidAltNumber && prediction != conflictingAltResolvedBySLL)
     {
         // Even though this is an ambiguity we are reporting, we can
         // still detect some context sensitivities.  Both SLL and LL
         // are showing a conflict, hence an ambiguity, but if they resolve
         // to different minimum alternatives we have also identified a
         // context sensitivity.
         decisions[currentDecision].contextSensitivities.Add(new ContextSensitivityInfo(currentDecision, currentState, _input, startIndex, stopIndex));
     }
     decisions[currentDecision].ambiguities.Add(new AmbiguityInfo(currentDecision, currentState, ambigAlts, _input, startIndex, stopIndex));
     base.ReportAmbiguity(dfa, D, startIndex, stopIndex, exact, ambigAlts, configs);
 }
Пример #12
0
 protected internal override void ReportAttemptingFullContext(DFA dfa, BitSet conflictingAlts, SimulatorState conflictState, int startIndex, int stopIndex)
 {
     if (conflictingAlts != null)
     {
         conflictingAltResolvedBySLL = conflictingAlts.NextSetBit(0);
     }
     else
     {
         conflictingAltResolvedBySLL = conflictState.s0.configs.RepresentedAlternatives.NextSetBit(0);
     }
     decisions[currentDecision].LL_Fallback++;
     base.ReportAttemptingFullContext(dfa, conflictingAlts, conflictState, startIndex, stopIndex);
 }
Пример #13
0
 /// <summary>
 /// Compute set of tokens that can follow
 /// <code>s</code>
 /// in the ATN in the
 /// specified
 /// <code>ctx</code>
 /// .
 /// <p/>
 /// If
 /// <code>ctx</code>
 /// is
 /// <see cref="PredictionContext.EmptyLocal"/>
 /// and
 /// <code>stopState</code>
 /// or the end of the rule containing
 /// <code>s</code>
 /// is reached,
 /// <see cref="TokenConstants.Epsilon"/>
 /// is added to the result set. If
 /// <code>ctx</code>
 /// is not
 /// <see cref="PredictionContext.EmptyLocal"/>
 /// and
 /// <code>addEOF</code>
 /// is
 /// <code>true</code>
 /// and
 /// <code>stopState</code>
 /// or the end of the outermost rule is reached,
 /// <see cref="TokenConstants.Eof"/>
 /// is added to the result set.
 /// </summary>
 /// <param name="s">the ATN state.</param>
 /// <param name="stopState">
 /// the ATN state to stop at. This can be a
 /// <see cref="BlockEndState"/>
 /// to detect epsilon paths through a closure.
 /// </param>
 /// <param name="ctx">
 /// The outer context, or
 /// <see cref="PredictionContext.EmptyLocal"/>
 /// if
 /// the outer context should not be used.
 /// </param>
 /// <param name="look">The result lookahead set.</param>
 /// <param name="lookBusy">
 /// A set used for preventing epsilon closures in the ATN
 /// from causing a stack overflow. Outside code should pass
 /// <code>new HashSet&lt;ATNConfig&gt;</code>
 /// for this argument.
 /// </param>
 /// <param name="calledRuleStack">
 /// A set used for preventing left recursion in the
 /// ATN from causing a stack overflow. Outside code should pass
 /// <code>new BitSet()</code>
 /// for this argument.
 /// </param>
 /// <param name="seeThruPreds">
 /// 
 /// <code>true</code>
 /// to true semantic predicates as
 /// implicitly
 /// <code>true</code>
 /// and "see through them", otherwise
 /// <code>false</code>
 /// to treat semantic predicates as opaque and add
 /// <see cref="HitPred"/>
 /// to the
 /// result if one is encountered.
 /// </param>
 /// <param name="addEOF">
 /// Add
 /// <see cref="TokenConstants.Eof"/>
 /// to the result if the end of the
 /// outermost context is reached. This parameter has no effect if
 /// <code>ctx</code>
 /// is
 /// <see cref="PredictionContext.EmptyLocal"/>
 /// .
 /// </param>
 protected internal virtual void Look(ATNState s, ATNState stopState, PredictionContext ctx, IntervalSet look, HashSet<ATNConfig> lookBusy, BitSet calledRuleStack, bool seeThruPreds, bool addEOF)
 {
     //		System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx);
     ATNConfig c = ATNConfig.Create(s, 0, ctx);
     if (!lookBusy.Add(c))
     {
         return;
     }
     if (s == stopState)
     {
         if (PredictionContext.IsEmptyLocal(ctx))
         {
             look.Add(TokenConstants.Epsilon);
             return;
         }
         else
         {
             if (ctx.IsEmpty)
             {
                 if (addEOF)
                 {
                     look.Add(TokenConstants.Eof);
                 }
                 return;
             }
         }
     }
     if (s is RuleStopState)
     {
         if (ctx.IsEmpty && !PredictionContext.IsEmptyLocal(ctx))
         {
             if (addEOF)
             {
                 look.Add(TokenConstants.Eof);
             }
             return;
         }
         bool removed = calledRuleStack.Get(s.ruleIndex);
         try
         {
             calledRuleStack.Clear(s.ruleIndex);
             for (int i = 0; i < ctx.Size; i++)
             {
                 if (ctx.GetReturnState(i) == PredictionContext.EmptyFullStateKey)
                 {
                     continue;
                 }
                 ATNState returnState = atn.states[ctx.GetReturnState(i)];
                 //					System.out.println("popping back to "+retState);
                 Look(returnState, stopState, ctx.GetParent(i), look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
             }
         }
         finally
         {
             if (removed)
             {
                 calledRuleStack.Set(s.ruleIndex);
             }
         }
     }
     int n = s.NumberOfTransitions;
     for (int i_1 = 0; i_1 < n; i_1++)
     {
         Transition t = s.Transition(i_1);
         if (t is RuleTransition)
         {
             RuleTransition ruleTransition = (RuleTransition)t;
             if (calledRuleStack.Get(ruleTransition.ruleIndex))
             {
                 continue;
             }
             PredictionContext newContext = ctx.GetChild(ruleTransition.followState.stateNumber);
             try
             {
                 calledRuleStack.Set(ruleTransition.ruleIndex);
                 Look(t.target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
             }
             finally
             {
                 calledRuleStack.Clear(ruleTransition.ruleIndex);
             }
         }
         else
         {
             if (t is AbstractPredicateTransition)
             {
                 if (seeThruPreds)
                 {
                     Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
                 }
                 else
                 {
                     look.Add(HitPred);
                 }
             }
             else
             {
                 if (t.IsEpsilon)
                 {
                     Look(t.target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
                 }
                 else
                 {
                     if (t.GetType() == typeof(WildcardTransition))
                     {
                         look.AddAll(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType));
                     }
                     else
                     {
                         //				System.out.println("adding "+ t);
                         IntervalSet set = t.Label;
                         if (set != null)
                         {
                             if (t is NotSetTransition)
                             {
                                 set = set.Complement(IntervalSet.Of(TokenConstants.MinUserTokenType, atn.maxTokenType));
                             }
                             look.AddAll(set);
                         }
                     }
                 }
             }
         }
     }
 }
Пример #14
0
 public ConflictInfo(BitSet conflictedAlts, bool exact)
 {
     this.conflictedAlts = conflictedAlts;
     this.exact = exact;
 }
Пример #15
0
 protected override void ReportAttemptingFullContext(DFA dfa, BitSet conflictingAlts, ATNConfigSet configs, int startIndex, int stopIndex)
 {
     if (conflictingAlts != null)
     {
     conflictingAltResolvedBySLL = conflictingAlts.NextSetBit(0);
     }
     else {
         conflictingAltResolvedBySLL = configs.GetAlts().NextSetBit(0);
     }
     decisions[currentDecision].LL_Fallback++;
     base.ReportAttemptingFullContext(dfa, conflictingAlts, configs, startIndex, stopIndex);
 }
Пример #16
0
 /// <summary>
 /// Constructs a new instance of the
 /// <see cref="AmbiguityInfo"/>
 /// class with the
 /// specified detailed ambiguity information.
 /// </summary>
 /// <param name="decision">The decision number</param>
 /// <param name="state">
 /// The final simulator state identifying the ambiguous
 /// alternatives for the current input
 /// </param>
 /// <param name="ambigAlts">The set of alternatives in the decision that lead to a valid parse.</param>
 /// <param name="input">The input token stream</param>
 /// <param name="startIndex">The start index for the current prediction</param>
 /// <param name="stopIndex">
 /// The index at which the ambiguity was identified during
 /// prediction
 /// </param>
 public AmbiguityInfo(int decision, SimulatorState state, BitSet ambigAlts, ITokenStream input, int startIndex, int stopIndex)
     : base(decision, state, input, startIndex, stopIndex, state.useContext)
 {
     this.ambigAlts = ambigAlts;
 }
Пример #17
0
 public BitSet Clone()
 {
     BitSet result = new BitSet();
     result._data = (ulong[])_data.Clone();
     return result;
 }
Пример #18
0
 public BitSet GetDescendants(int rule)
 {
     BitSet descendants = new BitSet();
     descendants.Or(children[rule]);
     while (true)
     {
         int cardinality = descendants.Cardinality();
         for (int i = descendants.NextSetBit(0); i >= 0; i = descendants.NextSetBit(i + 1))
         {
             descendants.Or(children[i]);
         }
         if (descendants.Cardinality() == cardinality)
         {
             // nothing changed
             break;
         }
     }
     return descendants;
 }
Пример #19
0
 public virtual void ReportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, bool exact, BitSet ambigAlts, ATNConfigSet configs)
 {
 }
Пример #20
0
 private static void CheckDependencies(IList<Tuple<RuleDependencyAttribute, ICustomAttributeProvider>> dependencies, Type recognizerType)
 {
     string[] ruleNames = GetRuleNames(recognizerType);
     int[] ruleVersions = GetRuleVersions(recognizerType, ruleNames);
     RuleDependencyChecker.RuleRelations relations = ExtractRuleRelations(recognizerType);
     StringBuilder errors = new StringBuilder();
     foreach (Tuple<RuleDependencyAttribute, ICustomAttributeProvider> dependency in dependencies)
     {
         if (!dependency.Item1.Recognizer.IsAssignableFrom(recognizerType))
         {
             continue;
         }
         // this is the rule in the dependency set with the highest version number
         int effectiveRule = dependency.Item1.Rule;
         if (effectiveRule < 0 || effectiveRule >= ruleVersions.Length)
         {
             string message = string.Format("Rule dependency on unknown rule {0}@{1} in {2}", dependency.Item1.Rule, dependency.Item1.Version, dependency.Item1.Recognizer.ToString());
             errors.AppendLine(dependency.Item2.ToString());
             errors.AppendLine(message);
             continue;
         }
         Dependents dependents = Dependents.Self | dependency.Item1.Dependents;
         ReportUnimplementedDependents(errors, dependency, dependents);
         BitSet @checked = new BitSet();
         int highestRequiredDependency = CheckDependencyVersion(errors, dependency, ruleNames, ruleVersions, effectiveRule, null);
         if ((dependents & Dependents.Parents) != 0)
         {
             BitSet parents = relations.parents[dependency.Item1.Rule];
             for (int parent = parents.NextSetBit(0); parent >= 0; parent = parents.NextSetBit(parent + 1))
             {
                 if (parent < 0 || parent >= ruleVersions.Length || @checked.Get(parent))
                 {
                     continue;
                 }
                 @checked.Set(parent);
                 int required = CheckDependencyVersion(errors, dependency, ruleNames, ruleVersions, parent, "parent");
                 highestRequiredDependency = Math.Max(highestRequiredDependency, required);
             }
         }
         if ((dependents & Dependents.Children) != 0)
         {
             BitSet children = relations.children[dependency.Item1.Rule];
             for (int child = children.NextSetBit(0); child >= 0; child = children.NextSetBit(child + 1))
             {
                 if (child < 0 || child >= ruleVersions.Length || @checked.Get(child))
                 {
                     continue;
                 }
                 @checked.Set(child);
                 int required = CheckDependencyVersion(errors, dependency, ruleNames, ruleVersions, child, "child");
                 highestRequiredDependency = Math.Max(highestRequiredDependency, required);
             }
         }
         if ((dependents & Dependents.Ancestors) != 0)
         {
             BitSet ancestors = relations.GetAncestors(dependency.Item1.Rule);
             for (int ancestor = ancestors.NextSetBit(0); ancestor >= 0; ancestor = ancestors.NextSetBit(ancestor + 1))
             {
                 if (ancestor < 0 || ancestor >= ruleVersions.Length || @checked.Get(ancestor))
                 {
                     continue;
                 }
                 @checked.Set(ancestor);
                 int required = CheckDependencyVersion(errors, dependency, ruleNames, ruleVersions, ancestor, "ancestor");
                 highestRequiredDependency = Math.Max(highestRequiredDependency, required);
             }
         }
         if ((dependents & Dependents.Descendants) != 0)
         {
             BitSet descendants = relations.GetDescendants(dependency.Item1.Rule);
             for (int descendant = descendants.NextSetBit(0); descendant >= 0; descendant = descendants.NextSetBit(descendant + 1))
             {
                 if (descendant < 0 || descendant >= ruleVersions.Length || @checked.Get(descendant))
                 {
                     continue;
                 }
                 @checked.Set(descendant);
                 int required = CheckDependencyVersion(errors, dependency, ruleNames, ruleVersions, descendant, "descendant");
                 highestRequiredDependency = Math.Max(highestRequiredDependency, required);
             }
         }
         int declaredVersion = dependency.Item1.Version;
         if (declaredVersion > highestRequiredDependency)
         {
             string message = string.Format("Rule dependency version mismatch: {0} has maximum dependency version {1} (expected {2}) in {3}", ruleNames[dependency.Item1.Rule], highestRequiredDependency, declaredVersion, dependency.Item1.Recognizer.ToString());
             errors.AppendLine(dependency.Item2.ToString());
             errors.AppendLine(message);
         }
     }
     if (errors.Length > 0)
     {
         throw new InvalidOperationException(errors.ToString());
     }
 }
Пример #21
0
 protected internal virtual BitSet GetConflictingAlts(BitSet reportedAlts, ATNConfigSet configs)
 {
     if (reportedAlts != null)
     {
         return reportedAlts;
     }
     BitSet result = new BitSet();
     foreach (ATNConfig config in configs)
     {
         result.Set(config.Alt);
     }
     return result;
 }
Пример #22
0
 public RuleRelations(int ruleCount)
 {
     parents = new BitSet[ruleCount];
     for (int i = 0; i < ruleCount; i++)
     {
         parents[i] = new BitSet();
     }
     children = new BitSet[ruleCount];
     for (int i_1 = 0; i_1 < ruleCount; i_1++)
     {
         children[i_1] = new BitSet();
     }
 }
Пример #23
0
        /**
         Sam pointed out a problem with the previous definition, v3, of
         ambiguous states. If we have another state associated with conflicting
         alternatives, we should keep going. For example, the following grammar

         s : (ID | ID ID?) ';' ;

         When the ATN simulation reaches the state before ';', it has a DFA
         state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally
         12|1|[] and 12|2|[] conflict, but we cannot stop processing this node
         because alternative to has another way to continue, via [6|2|[]].
         The key is that we have a single state that has config's only associated
         with a single alternative, 2, and crucially the state transitions
         among the configurations are all non-epsilon transitions. That means
         we don't consider any conflicts that include alternative 2. So, we
         ignore the conflict between alts 1 and 2. We ignore a set of
         conflicting alts when there is an intersection with an alternative
         associated with a single alt state in the state&rarr;config-list map.

         It's also the case that we might have two conflicting configurations but
         also a 3rd nonconflicting configuration for a different alternative:
         [1|1|[], 1|2|[], 8|3|[]]. This can come about from grammar:

         a : A | A | A B ;

         After matching input A, we reach the stop state for rule A, state 1.
         State 8 is the state right before B. Clearly alternatives 1 and 2
         conflict and no amount of further lookahead will separate the two.
         However, alternative 3 will be able to continue and so we do not
         stop working on this state. In the previous example, we're concerned
         with states associated with the conflicting alternatives. Here alt
         3 is not associated with the conflicting configs, but since we can continue
         looking for input reasonably, I don't declare the state done. We
         ignore a set of conflicting alts when we have an alternative
         that we still need to pursue.
         */
        protected BitSet GetConflictingAltsOrUniqueAlt(ATNConfigSet configSet)
        {
            BitSet conflictingAlts;
            if (configSet.uniqueAlt != ATN.INVALID_ALT_NUMBER)
            {
                conflictingAlts = new BitSet();
                conflictingAlts[configSet.uniqueAlt] = true;
            }
            else {
                conflictingAlts = configSet.conflictingAlts;
            }
            return conflictingAlts;
        }
Пример #24
0
 public BitSet GetAncestors(int rule)
 {
     BitSet ancestors = new BitSet();
     ancestors.Or(parents[rule]);
     while (true)
     {
         int cardinality = ancestors.Cardinality();
         for (int i = ancestors.NextSetBit(0); i >= 0; i = ancestors.NextSetBit(i + 1))
         {
             ancestors.Or(parents[i]);
         }
         if (ancestors.Cardinality() == cardinality)
         {
             // nothing changed
             break;
         }
     }
     return ancestors;
 }
Пример #25
0
        protected SemanticContext[] GetPredsForAmbigAlts(BitSet ambigAlts,
														 ATNConfigSet configSet,
													  int nalts)
        {
            // REACH=[1|1|[]|0:0, 1|2|[]|0:1]
            /* altToPred starts as an array of all null contexts. The entry at index i
             * corresponds to alternative i. altToPred[i] may have one of three values:
             *   1. null: no ATNConfig c is found such that c.alt==i
             *   2. SemanticContext.NONE: At least one ATNConfig c exists such that
             *      c.alt==i and c.semanticContext==SemanticContext.NONE. In other words,
             *      alt i has at least one unpredicated config.
             *   3. Non-NONE Semantic Context: There exists at least one, and for all
             *      ATNConfig c such that c.alt==i, c.semanticContext!=SemanticContext.NONE.
             *
             * From this, it is clear that NONE||anything==NONE.
             */
            SemanticContext[] altToPred = new SemanticContext[nalts + 1];
            foreach (ATNConfig c in configSet.configs)
            {
                if (ambigAlts[c.alt])
                {
                    altToPred[c.alt] = SemanticContext.OrOp(altToPred[c.alt], c.semanticContext);
                }
            }

            int nPredAlts = 0;
            for (int i = 1; i <= nalts; i++)
            {
                if (altToPred[i] == null)
                {
                    altToPred[i] = SemanticContext.NONE;
                }
                else if (altToPred[i] != SemanticContext.NONE)
                {
                    nPredAlts++;
                }
            }

            //		// Optimize away p||p and p&&p TODO: optimize() was a no-op
            //		for (int i = 0; i < altToPred.length; i++) {
            //			altToPred[i] = altToPred[i].optimize();
            //		}

            // nonambig alts are null in altToPred
            if (nPredAlts == 0) altToPred = null;
            if (debug) Console.WriteLine("getPredsForAmbigAlts result " + Arrays.ToString(altToPred));
            return altToPred;
        }
Пример #26
0
 public override void ReportAmbiguity(Parser recognizer, Antlr4.Runtime.Dfa.DFA dfa, int startIndex, int stopIndex, bool exact, Antlr4.Runtime.Sharpen.BitSet ambigAlts, Antlr4.Runtime.Atn.ATNConfigSet configs)
 {
     if (_captureDiagnostics)
     {
         _warningMessages.Add(string.Format("reportAmbiguity d={0}: ambigAlts={1}, input='{2}'", GetDecisionDescription(recognizer, dfa), GetConflictingAlts(ambigAlts, configs), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex))));
         Debug.Log(string.Format("reportAmbiguity d={0}: ambigAlts={1}, input='{2}'", GetDecisionDescription(recognizer, dfa), GetConflictingAlts(ambigAlts, configs), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex))));
         MyConsole.main.AppendText(string.Format("reportAmbiguity d={0}: ambigAlts={1}, input='{2}'", GetDecisionDescription(recognizer, dfa), GetConflictingAlts(ambigAlts, configs), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex))));
     }
 }
Пример #27
0
 protected virtual void ReportAttemptingFullContext(DFA dfa, BitSet conflictingAlts, ATNConfigSet configs, int startIndex, int stopIndex)
 {
     if (debug || retry_debug)
     {
         Interval interval = Interval.Of(startIndex, stopIndex);
         Console.WriteLine("reportAttemptingFullContext decision=" + dfa.decision + ":" + configs +
                            ", input=" + parser.TokenStream.GetText(interval));
     }
     if (parser != null)
         parser.ErrorListenerDispatch.ReportAttemptingFullContext(parser, dfa, startIndex, stopIndex, conflictingAlts, null /*configs*/);
 }
Пример #28
0
 public override void ReportAttemptingFullContext(Parser recognizer, Antlr4.Runtime.Dfa.DFA dfa, int startIndex, int stopIndex, Antlr4.Runtime.Sharpen.BitSet conflictingAlts, Antlr4.Runtime.Atn.SimulatorState conflictState)
 {
     if (_captureDiagnostics)
     {
         _warningMessages.Add(string.Format("reportAttemptingFullContext d={0}, input='{1}'", GetDecisionDescription(recognizer, dfa), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex))));
         Debug.Log(string.Format("reportAttemptingFullContext d={0}, input='{1}'", GetDecisionDescription(recognizer, dfa), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex))));
         MyConsole.main.AppendText(string.Format("reportAttemptingFullContext d={0}, input='{1}'", GetDecisionDescription(recognizer, dfa), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex))));
     }
 }
Пример #29
0
 public virtual void SetContextSensitive(ATN atn)
 {
     lock (this)
     {
         System.Diagnostics.Debug.Assert(!configs.IsOutermostConfigSet);
         if (IsContextSensitive)
         {
             return;
         }
         contextSymbols = new BitSet();
         contextEdges = new SingletonEdgeMap<DFAState>(-1, atn.states.Count - 1);
     }
 }
Пример #30
0
 /// <summary>
 /// Given a start and stop index, return a
 /// <c>List</c>
 /// of all tokens in
 /// the token type
 /// <c>BitSet</c>
 /// .  Return
 /// <see langword="null"/>
 /// if no tokens were found.  This
 /// method looks at both on and off channel tokens.
 /// </summary>
 public virtual IList<IToken> GetTokens(int start, int stop, BitSet types)
 {
     LazyInit();
     if (start < 0 || stop >= tokens.Count || stop < 0 || start >= tokens.Count)
     {
         throw new ArgumentOutOfRangeException("start " + start + " or stop " + stop + " not in 0.." + (tokens.Count - 1));
     }
     if (start > stop)
     {
         return null;
     }
     // list = tokens[start:stop]:{T t, t.getType() in types}
     IList<IToken> filteredTokens = new List<IToken>();
     for (int i = start; i <= stop; i++)
     {
         IToken t = tokens[i];
         if (types == null || types.Get(t.Type))
         {
             filteredTokens.Add(t);
         }
     }
     if (filteredTokens.Count == 0)
     {
         filteredTokens = null;
     }
     return filteredTokens;
 }
Пример #31
0
 public virtual void SetContextSensitive(ATN atn)
 {
     System.Diagnostics.Debug.Assert(!configs.IsOutermostConfigSet);
     if (IsContextSensitive)
     {
         return;
     }
     lock (this)
     {
         if (contextSymbols == null)
         {
             contextSymbols = new BitSet();
         }
     }
 }
Пример #32
0
 public virtual IList<IToken> GetTokens(int start, int stop, int ttype)
 {
     BitSet s = new BitSet(ttype);
     s.Set(ttype);
     return GetTokens(start, stop, s);
 }