Пример #1
0
        public ParserInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable <string> ruleNames, ATN atn, ITokenStream input)
            : base(input)
        {
            this._grammarFileName = grammarFileName;
            this._atn             = atn;
            this._ruleNames       = ruleNames.ToArray();
            this.vocabulary       = vocabulary;
            // 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).isPrecedenceDecision)
                {
                    this.pushRecursionContextStates.Set(state.stateNumber);
                }
            }

            //init decision DFA
            int numberofDecisions = atn.NumberOfDecisions;

            this._decisionToDFA = new Dfa.DFA[numberofDecisions];
            for (int i = 0; i < numberofDecisions; i++)
            {
                DecisionState decisionState = atn.GetDecisionState(i);
                _decisionToDFA[i] = new Dfa.DFA(decisionState, i);
            }
            // get atn simulator that knows how to do predictions
            Interpreter = new ParserATNSimulator(this, atn, _decisionToDFA, null);
        }
Пример #2
0
 public DFASerializer(DFA dfa, IVocabulary vocabulary, string[] ruleNames, ATN atn)
 {
     this.dfa = dfa;
     this.vocabulary = vocabulary;
     this.ruleNames = ruleNames;
     this.atn = atn;
 }
Пример #3
0
 static ConfigReferenceLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #4
0
 static FormulaGrammerLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #5
0
 static SpeakParser()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
 static ControlScriptLanguageLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #7
0
 static HighLevelAssemblyLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #8
0
 static ExpressionLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #9
0
 static TypescriptCodeLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #10
0
 static SquareList()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
 public override void ReportAmbiguity(Parser recognizer, DFA dfa, int startIndex, 
     int stopIndex, BitSet ambigAlts, ATNConfigSet configs)
 {
     string format = "reportAmbiguity d={0}: ambigAlts={1}, input='{2}'";
     recognizer.NotifyErrorListeners(string.Format(format, GetDecisionDescription(recognizer
         , dfa.decision), ambigAlts, ((ITokenStream)recognizer.InputStream).GetText(Interval
         .Of(startIndex, stopIndex))));
 }
 public override void ReportContextSensitivity(Parser recognizer, DFA dfa, int startIndex
     , int stopIndex, SimulatorState acceptState)
 {
     string format = "reportContextSensitivity d={0}, input='{1}'";
     recognizer.NotifyErrorListeners(string.Format(format, GetDecisionDescription(recognizer
         , dfa.decision), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex
         , stopIndex))));
 }
Пример #13
0
        public LexerATNSimulator(Lexer recog, ATN atn,
								 DFA[] decisionToDFA,
								 PredictionContextCache sharedContextCache)
            : base(atn, sharedContextCache)
        {
            this.decisionToDFA = decisionToDFA;
            this.recog = recog;
        }
Пример #14
0
 static ComponentPointLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #15
0
 static QueryFilteringLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #16
0
 static FilterTranslator()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #17
0
 static UnicodeClasses()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #18
0
 static PlatformerMapLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #19
0
 static PropositionalLogicGrammarParser()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
 static InvertedPolishCalculatorParser()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #21
0
 static mnemonic_interpreterLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #22
0
 static DataPrivilegeLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #23
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);
 }
 static CsharpSubsetLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
 static CurrencyComputerLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #26
0
 static RealtyTypeAndOwnType()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #27
0
 static GamaPreprocessorLexer()
 {
     decisionToDFA = new DFA[_ATN.NumberOfDecisions];
     for (int i = 0; i < _ATN.NumberOfDecisions; i++)
     {
         decisionToDFA[i] = new DFA(_ATN.GetDecisionState(i), i);
     }
 }
Пример #28
0
 public override void ReportContextSensitivity(Parser recognizer, Antlr4.Runtime.Dfa.DFA dfa, int startIndex, int stopIndex, int prediction, Antlr4.Runtime.Atn.SimulatorState acceptState)
 {
     if (_captureDiagnostics)
     {
         _warningMessages.Add(string.Format("reportContextSensitivity d={0}, input='{1}'", GetDecisionDescription(recognizer, dfa), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex))));
         Debug.Log(string.Format("reportContextSensitivity d={0}, input='{1}'", GetDecisionDescription(recognizer, dfa), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex))));
         MyConsole.main.AppendText(string.Format("reportContextSensitivity d={0}, input='{1}'", GetDecisionDescription(recognizer, dfa), ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex))));
     }
 }
Пример #29
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))));
     }
 }
 protected override SimulatorState ComputeStartState(DFA dfa, ParserRuleContext globalContext, bool useContext)
 {
     _computingStartState = true;
     try
     {
         return base.ComputeStartState(dfa, globalContext, useContext);
     }
     finally
     {
         _computingStartState = false;
     }
 }
Пример #31
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);
 }
Пример #32
0
 public LexerInterpreter(string grammarFileName, IVocabulary vocabulary, IEnumerable<string> ruleNames, IEnumerable<string> modeNames, ATN atn, ICharStream input)
     : base(input)
 {
     if (atn.grammarType != ATNType.Lexer)
     {
         throw new ArgumentException("The ATN must be a lexer ATN.");
     }
     this.grammarFileName = grammarFileName;
     this.atn = atn;
     this.ruleNames = ruleNames.ToArray();
     this.modeNames = modeNames.ToArray();
     this.vocabulary = vocabulary;
     this.decisionToDFA = new DFA[atn.NumberOfDecisions];
     for (int i = 0; i < decisionToDFA.Length; i++)
     {
         decisionToDFA[i] = new DFA(atn.GetDecisionState(i), i);
     }
     this.Interpreter = new LexerATNSimulator(this, atn, decisionToDFA, sharedContextCache);
 }
        protected override DFAState CreateDFAState(DFA dfa, ATNConfigSet configs)
        {
            int t = _input.La(1);
            if (t == AntlrV4.CaretToken.CaretTokenType && !_computingStartState)
            {
                _caretToken = (ICaretToken)_input.Lt(1);
                throw NoViableAlt(_input, _outerContext, configs, _startIndex);
            }

            return base.CreateDFAState(dfa, configs);
        }
Пример #34
0
 public LexerDFASerializer(DFA dfa)
     : base(dfa, (string[])null)
 {
 }
Пример #35
0
 protected internal virtual string GetDecisionDescription(Parser recognizer, DFA
      dfa)
 {
     int decision = dfa.decision;
     int ruleIndex = dfa.atnStartState.ruleIndex;
     string[] ruleNames = recognizer.RuleNames;
     if (ruleIndex < 0 || ruleIndex >= ruleNames.Length)
     {
         return decision.ToString();
     }
     string ruleName = ruleNames[ruleIndex];
     if (string.IsNullOrEmpty(ruleName))
     {
         return decision.ToString();
     }
     return string.Format("{0} ({1})", decision, ruleName);
 }
Пример #36
0
 public LexerDFASerializer(DFA dfa)
     : base(dfa, Vocabulary.EmptyVocabulary)
 {
 }
Пример #37
0
 public DFAState(DFA dfa, ATNConfigSet configs)
     : this(dfa.EmptyEdgeMap, dfa.EmptyContextEdgeMap, configs)
 {
 }
Пример #38
0
 public virtual void ReportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex
     , int stopIndex, BitSet conflictingAlts, SimulatorState conflictState)
 {
 }
Пример #39
0
 protected virtual void ReportContextSensitivity(DFA dfa, int prediction, ATNConfigSet configs, int startIndex, int stopIndex)
 {
     if (debug || retry_debug)
     {
         Interval interval = Interval.Of(startIndex, stopIndex);
         Console.WriteLine("ReportContextSensitivity decision=" + dfa.decision + ":" + configs +
                            ", input=" + parser.TokenStream.GetText(interval));
     }
     if (parser != null) parser.ErrorListenerDispatch.ReportContextSensitivity(parser, dfa, startIndex, stopIndex, prediction, null /*configs*/);
 }
Пример #40
0
        /**
         * Add state {@code D} to the DFA if it is not already present, and return
         * the actual instance stored in the DFA. If a state equivalent to {@code D}
         * is already in the DFA, the existing state is returned. Otherwise this
         * method returns {@code D} after adding it to the DFA.
         *
         * <p>If {@code D} is {@link #ERROR}, this method returns {@link #ERROR} and
         * does not change the DFA.</p>
         *
         * @param dfa The dfa
         * @param D The DFA state to add
         * @return The state stored in the DFA. This will be either the existing
         * state if {@code D} is already in the DFA, or {@code D} itself if the
         * state was not already present.
         */
        protected DFAState AddDFAState(DFA dfa, DFAState D)
        {
            if (D == ERROR)
            {
                return D;
            }

            lock (dfa.states)
            {
                DFAState existing = dfa.states.Get(D);
                if (existing != null) return existing;

                D.stateNumber = dfa.states.Count;
                if (!D.configSet.IsReadOnly)
                {
                    D.configSet.OptimizeConfigs(this);
                    D.configSet.IsReadOnly = true;
                }
                dfa.states.Put(D, D);
                if (debug) Console.WriteLine("adding new DFA state: " + D);
                return D;
            }
        }
Пример #41
0
        /**
         * Add an edge to the DFA, if possible. This method calls
         * {@link #addDFAState} to ensure the {@code to} state is present in the
         * DFA. If {@code from} is {@code null}, or if {@code t} is outside the
         * range of edges that can be represented in the DFA tables, this method
         * returns without adding the edge to the DFA.
         *
         * <p>If {@code to} is {@code null}, this method returns {@code null}.
         * Otherwise, this method returns the {@link DFAState} returned by calling
         * {@link #addDFAState} for the {@code to} state.</p>
         *
         * @param dfa The DFA
         * @param from The source state for the edge
         * @param t The input symbol
         * @param to The target state for the edge
         *
         * @return If {@code to} is {@code null}, this method returns {@code null};
         * otherwise this method returns the result of calling {@link #addDFAState}
         * on {@code to}
         */
        protected DFAState AddDFAEdge(DFA dfa,
									  DFAState from,
									  int t,
									  DFAState to)
        {
            if (debug)
            {
                Console.WriteLine("EDGE " + from + " -> " + to + " upon " + GetTokenName(t));
            }

            if (to == null)
            {
                return null;
            }

            to = AddDFAState(dfa, to); // used existing if possible not incoming
            if (from == null || t < -1 || t > atn.maxTokenType)
            {
                return to;
            }

            lock (from)
            {
                if (from.edges == null)
                {
                    from.edges = new DFAState[atn.maxTokenType + 1 + 1];
                }

                from.edges[t + 1] = to; // connect
            }

            if (debug)
            {
                Console.WriteLine("DFA=\n" + dfa.ToString(parser != null ? parser.Vocabulary : Vocabulary.EmptyVocabulary));
            }

            return to;
        }
Пример #42
0
 public override void ClearDFA()
 {
     for (int d = 0; d < decisionToDFA.Length; d++)
     {
         decisionToDFA[d] = new DFA(atn.GetDecisionState(d), d);
     }
 }
Пример #43
0
        public virtual int AdaptivePredict(ITokenStream input, int decision,
								   ParserRuleContext outerContext)
        {
            if (debug || debug_list_atn_decisions)
            {
                Console.WriteLine("adaptivePredict decision " + decision +
                                       " exec LA(1)==" + GetLookaheadName(input) +
                                  " line " + input.LT(1).Line + ":" + input.LT(1).Column);
            }

            this.input = input;
            startIndex = input.Index;
            context = outerContext;
            DFA dfa = decisionToDFA[decision];
            thisDfa = dfa;

            int m = input.Mark();
            int index = startIndex;

            // Now we are certain to have a specific decision's DFA
            // But, do we still need an initial state?
            try
            {
                DFAState s0;
                if (dfa.IsPrecedenceDfa)
                {
                    // the start state for a precedence DFA depends on the current
                    // parser precedence, and is provided by a DFA method.
                    s0 = dfa.GetPrecedenceStartState(parser.Precedence);
                }
                else {
                    // the start state for a "regular" DFA is just s0
                    s0 = dfa.s0;
                }

                if (s0 == null)
                {
                    if (outerContext == null) outerContext = ParserRuleContext.EmptyContext;
                    if (debug || debug_list_atn_decisions)
                    {
                        Console.WriteLine("predictATN decision " + dfa.decision +
                                           " exec LA(1)==" + GetLookaheadName(input) +
                                           ", outerContext=" + outerContext.ToString(parser));
                    }

                    bool fullCtx = false;
                    ATNConfigSet s0_closure =
                        ComputeStartState(dfa.atnStartState,
                                          ParserRuleContext.EmptyContext,
                                          fullCtx);

                    if (dfa.IsPrecedenceDfa)
                    {
                        /* If this is a precedence DFA, we use applyPrecedenceFilter
                         * to convert the computed start state to a precedence start
                         * state. We then use DFA.setPrecedenceStartState to set the
                         * appropriate start state for the precedence level rather
                         * than simply setting DFA.s0.
                         */
                        dfa.s0.configSet = s0_closure; // not used for prediction but useful to know start configs anyway
                        s0_closure = ApplyPrecedenceFilter(s0_closure);
                        s0 = AddDFAState(dfa, new DFAState(s0_closure));
                        dfa.SetPrecedenceStartState(parser.Precedence, s0);
                    }
                    else {
                        s0 = AddDFAState(dfa, new DFAState(s0_closure));
                        dfa.s0 = s0;
                    }
                }

                int alt = ExecATN(dfa, s0, input, index, outerContext);
                if (debug)
                    Console.WriteLine("DFA after predictATN: " + dfa.ToString(parser.Vocabulary));
                return alt;
            }
            finally
            {
                mergeCache = null; // wack cache after each prediction
                thisDfa = null;
                input.Seek(index);
                input.Release(m);
            }
        }
Пример #44
0
        public ParserATNSimulator(Parser parser, ATN atn,
								  DFA[] decisionToDFA,
								  PredictionContextCache sharedContextCache)
            : base(atn, sharedContextCache)
        {
            this.parser = parser;
            this.decisionToDFA = decisionToDFA;
            //		DOTGenerator dot = new DOTGenerator(null);
            //		Console.WriteLine(dot.getDOT(atn.rules.get(0), parser.getRuleNames()));
            //		Console.WriteLine(dot.getDOT(atn.rules.get(1), parser.getRuleNames()));
        }
Пример #45
0
        /** Testing only! */
        public ParserATNSimulator(ATN atn, DFA[] decisionToDFA,
								  PredictionContextCache sharedContextCache)
            : this(null, atn, decisionToDFA, sharedContextCache)
        {
        }
Пример #46
0
 public LexerDFASerializer(DFA dfa)
     : base(dfa, Vocabulary.EmptyVocabulary)
 {
 }
Пример #47
0
 public virtual void ReportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex
     , int stopIndex, SimulatorState initialState)
 {
 }
Пример #48
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*/);
 }
Пример #49
0
 public void ClearDFA()
 {
     decisionToDFA = new DFA[decisionToState.Count];
     for (int i = 0; i < decisionToDFA.Length; i++)
     {
         decisionToDFA[i] = new DFA(decisionToState[i], i);
     }
     modeToDFA = new DFA[modeToStartState.Count];
     for (int i_1 = 0; i_1 < modeToDFA.Length; i_1++)
     {
         modeToDFA[i_1] = new DFA(modeToStartState[i_1]);
     }
     contextCache.Clear();
     LL1Table.Clear();
 }
Пример #50
0
 public DFASerializer(DFA dfa, IRecognizer parser)
     : this(dfa, parser != null ? parser.TokenNames : null, parser != null ? parser.RuleNames : null, parser != null ? parser.Atn : null)
 {
 }
Пример #51
0
 public DFASerializer(DFA dfa, string[] tokenNames)
     : this(dfa, tokenNames, null, null)
 {
 }
Пример #52
0
 public DFAState([NotNull] DFA dfa, [NotNull] ATNConfigSet configs)
     : this(dfa.EmptyEdgeMap, dfa.EmptyContextEdgeMap, configs)
 {
 }
Пример #53
0
 public virtual void ReportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int
      stopIndex, BitSet ambigAlts, ATNConfigSet configs)
 {
 }
Пример #54
0
        /**
         * Compute a target state for an edge in the DFA, and attempt to add the
         * computed state and corresponding edge to the DFA.
         *
         * @param dfa The DFA
         * @param previousD The current DFA state
         * @param t The next input symbol
         *
         * @return The computed target DFA state for the given input symbol
         * {@code t}. If {@code t} does not lead to a valid DFA state, this method
         * returns {@link #ERROR}.
         */
        protected virtual DFAState ComputeTargetState(DFA dfa, DFAState previousD, int t)
        {
            ATNConfigSet reach = ComputeReachSet(previousD.configSet, t, false);
            if (reach == null)
            {
                AddDFAEdge(dfa, previousD, t, ERROR);
                return ERROR;
            }

            // create new target state; we'll add to DFA after it's complete
            DFAState D = new DFAState(reach);

            int predictedAlt = GetUniqueAlt(reach);

            if (debug)
            {
                ICollection<BitSet> altSubSets = PredictionMode.GetConflictingAltSubsets(reach.configs);
                Console.WriteLine("SLL altSubSets=" + altSubSets +
                                   ", configs=" + reach +
                                   ", predict=" + predictedAlt + ", allSubsetsConflict=" +
                                       PredictionMode.AllSubsetsConflict(altSubSets) + ", conflictingAlts=" +
                                   GetConflictingAlts(reach));
            }

            if (predictedAlt != ATN.INVALID_ALT_NUMBER)
            {
                // NO CONFLICT, UNIQUELY PREDICTED ALT
                D.isAcceptState = true;
                D.configSet.uniqueAlt = predictedAlt;
                D.prediction = predictedAlt;
            }
            else if (PredictionMode.HasSLLConflictTerminatingPrediction(mode, reach))
            {
                // MORE THAN ONE VIABLE ALTERNATIVE
                D.configSet.conflictingAlts = GetConflictingAlts(reach);
                D.requiresFullContext = true;
                // in SLL-only mode, we will stop at this state and return the minimum alt
                D.isAcceptState = true;
                D.prediction = D.configSet.conflictingAlts.NextSetBit(0);
            }

            if (D.isAcceptState && D.configSet.hasSemanticContext)
            {
                PredicateDFAState(D, atn.GetDecisionState(dfa.decision));
                if (D.predicates != null)
                {
                    D.prediction = ATN.INVALID_ALT_NUMBER;
                }
            }

            // all adds to dfa are done after we've created full D state
            D = AddDFAEdge(dfa, previousD, t, D);
            return D;
        }
Пример #55
0
 public virtual void ReportContextSensitivity(Parser recognizer, DFA dfa, int startIndex
     , int stopIndex, SimulatorState acceptState)
 {
 }
Пример #56
0
        /** Performs ATN simulation to compute a predicted alternative based
         *  upon the remaining input, but also updates the DFA cache to avoid
         *  having to traverse the ATN again for the same input sequence.

         There are some key conditions we're looking for after computing a new
         set of ATN configs (proposed DFA state):
               * if the set is empty, there is no viable alternative for current symbol
               * does the state uniquely predict an alternative?
               * does the state have a conflict that would prevent us from
                 putting it on the work list?

         We also have some key operations to do:
               * add an edge from previous DFA state to potentially new DFA state, D,
                 upon current symbol but only if adding to work list, which means in all
                 cases except no viable alternative (and possibly non-greedy decisions?)
               * collecting predicates and adding semantic context to DFA accept states
               * adding rule context to context-sensitive DFA accept states
               * consuming an input symbol
               * reporting a conflict
               * reporting an ambiguity
               * reporting a context sensitivity
               * reporting insufficient predicates

         cover these cases:
            dead end
            single alt
            single alt + preds
            conflict
            conflict + preds
         */
        protected int ExecATN(DFA dfa, DFAState s0,
						   ITokenStream input, int startIndex,
						   ParserRuleContext outerContext)
        {
            if (debug || debug_list_atn_decisions)
            {
                Console.WriteLine("execATN decision " + dfa.decision +
                                   " exec LA(1)==" + GetLookaheadName(input) +
                                   " line " + input.LT(1).Line + ":" + input.LT(1).Column);
            }

            DFAState previousD = s0;

            if (debug) Console.WriteLine("s0 = " + s0);

            int t = input.LA(1);

            while (true)
            { // while more work
                DFAState D = GetExistingTargetState(previousD, t);
                if (D == null)
                {
                    D = ComputeTargetState(dfa, previousD, t);
                }

                if (D == ERROR)
                {
                    // if any configs in previous dipped into outer context, that
                    // means that input up to t actually finished entry rule
                    // at least for SLL decision. Full LL doesn't dip into outer
                    // so don't need special case.
                    // We will get an error no matter what so delay until after
                    // decision; better error message. Also, no reachable target
                    // ATN states in SLL implies LL will also get nowhere.
                    // If conflict in states that dip out, choose min since we
                    // will get error no matter what.
                    NoViableAltException e = NoViableAlt(input, outerContext, previousD.configSet, startIndex);
                    input.Seek(startIndex);
                    int alt = GetSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previousD.configSet, outerContext);
                    if (alt != ATN.INVALID_ALT_NUMBER)
                    {
                        return alt;
                    }
                    throw e;
                }

                if (D.requiresFullContext && mode != PredictionMode.SLL)
                {
                    // IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
                    BitSet conflictingAlts = D.configSet.conflictingAlts;
                    if (D.predicates != null)
                    {
                        if (debug) Console.WriteLine("DFA state has preds in DFA sim LL failover");
                        int conflictIndex = input.Index;
                        if (conflictIndex != startIndex)
                        {
                            input.Seek(startIndex);
                        }

                        conflictingAlts = EvalSemanticContext(D.predicates, outerContext, true);
                        if (conflictingAlts.Cardinality() == 1)
                        {
                            if (debug) Console.WriteLine("Full LL avoided");
                            return conflictingAlts.NextSetBit(0);
                        }

                        if (conflictIndex != startIndex)
                        {
                            // restore the index so reporting the fallback to full
                            // context occurs with the index at the correct spot
                            input.Seek(conflictIndex);
                        }
                    }

                    if (dfa_debug) Console.WriteLine("ctx sensitive state " + outerContext + " in " + D);
                    bool fullCtx = true;
                    ATNConfigSet s0_closure =
                        ComputeStartState(dfa.atnStartState, outerContext, fullCtx);
                    ReportAttemptingFullContext(dfa, conflictingAlts, D.configSet, startIndex, input.Index);
                    int alt = ExecATNWithFullContext(dfa, D, s0_closure,
                                                     input, startIndex,
                                                     outerContext);
                    return alt;
                }

                if (D.isAcceptState)
                {
                    if (D.predicates == null)
                    {
                        return D.prediction;
                    }

                    int stopIndex = input.Index;
                    input.Seek(startIndex);
                    BitSet alts = EvalSemanticContext(D.predicates, outerContext, true);
                    switch (alts.Cardinality())
                    {
                        case 0:
                            throw NoViableAlt(input, outerContext, D.configSet, startIndex);

                        case 1:
                            return alts.NextSetBit(0);

                        default:
                            // report ambiguity after predicate evaluation to make sure the correct
                            // set of ambig alts is reported.
                            ReportAmbiguity(dfa, D, startIndex, stopIndex, false, alts, D.configSet);
                            return alts.NextSetBit(0);
                    }
                }

                previousD = D;

                if (t != IntStreamConstants.EOF)
                {
                    input.Consume();
                    t = input.LA(1);
                }
            }
        }
Пример #57
0
 public LexerDFASerializer(DFA dfa) : base(dfa, (string[])null)
 {
 }
Пример #58
0
        // comes back with reach.UniqueAlt set to a valid alt
        protected int ExecATNWithFullContext(DFA dfa,
											 DFAState D, // how far we got in SLL DFA before failing over
											 ATNConfigSet s0,
											 ITokenStream input, int startIndex,
											 ParserRuleContext outerContext)
        {
            if (debug || debug_list_atn_decisions)
            {
                Console.WriteLine("execATNWithFullContext " + s0);
            }
            bool fullCtx = true;
            bool foundExactAmbig = false;
            ATNConfigSet reach = null;
            ATNConfigSet previous = s0;
            input.Seek(startIndex);
            int t = input.LA(1);
            int predictedAlt;
            while (true)
            { // while more work
              //			Console.WriteLine("LL REACH "+GetLookaheadName(input)+
              //							   " from configs.size="+previous.size()+
              //							   " line "+input.LT(1)Line+":"+input.LT(1).Column);
                reach = ComputeReachSet(previous, t, fullCtx);
                if (reach == null)
                {
                    // if any configs in previous dipped into outer context, that
                    // means that input up to t actually finished entry rule
                    // at least for LL decision. Full LL doesn't dip into outer
                    // so don't need special case.
                    // We will get an error no matter what so delay until after
                    // decision; better error message. Also, no reachable target
                    // ATN states in SLL implies LL will also get nowhere.
                    // If conflict in states that dip out, choose min since we
                    // will get error no matter what.
                    NoViableAltException e = NoViableAlt(input, outerContext, previous, startIndex);
                    input.Seek(startIndex);
                    int alt = GetSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previous, outerContext);
                    if (alt != ATN.INVALID_ALT_NUMBER)
                    {
                        return alt;
                    }
                    throw e;
                }

                ICollection<BitSet> altSubSets = PredictionMode.GetConflictingAltSubsets(reach.configs);
                if (debug)
                {
                    Console.WriteLine("LL altSubSets=" + altSubSets +
                                       ", predict=" + PredictionMode.GetUniqueAlt(altSubSets) +
                                       ", ResolvesToJustOneViableAlt=" +
                                           PredictionMode.ResolvesToJustOneViableAlt(altSubSets));
                }

                //			Console.WriteLine("altSubSets: "+altSubSets);
                //			System.err.println("reach="+reach+", "+reach.conflictingAlts);
                reach.uniqueAlt = GetUniqueAlt(reach);
                // unique prediction?
                if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER)
                {
                    predictedAlt = reach.uniqueAlt;
                    break;
                }
                if (mode != PredictionMode.LL_EXACT_AMBIG_DETECTION)
                {
                    predictedAlt = PredictionMode.ResolvesToJustOneViableAlt(altSubSets);
                    if (predictedAlt != ATN.INVALID_ALT_NUMBER)
                    {
                        break;
                    }
                }
                else {
                    // In exact ambiguity mode, we never try to terminate early.
                    // Just keeps scarfing until we know what the conflict is
                    if (PredictionMode.AllSubsetsConflict(altSubSets) &&
                         PredictionMode.AllSubsetsEqual(altSubSets))
                    {
                        foundExactAmbig = true;
                        predictedAlt = PredictionMode.GetSingleViableAlt(altSubSets);
                        break;
                    }
                    // else there are multiple non-conflicting subsets or
                    // we're not sure what the ambiguity is yet.
                    // So, keep going.
                }

                previous = reach;
                if (t != IntStreamConstants.EOF)
                {
                    input.Consume();
                    t = input.LA(1);
                }
            }

            // If the configuration set uniquely predicts an alternative,
            // without conflict, then we know that it's a full LL decision
            // not SLL.
            if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER)
            {
                ReportContextSensitivity(dfa, predictedAlt, reach, startIndex, input.Index);
                return predictedAlt;
            }

            // We do not check predicates here because we have checked them
            // on-the-fly when doing full context prediction.

            /*
            In non-exact ambiguity detection mode, we might	actually be able to
            detect an exact ambiguity, but I'm not going to spend the cycles
            needed to check. We only emit ambiguity warnings in exact ambiguity
            mode.

            For example, we might know that we have conflicting configurations.
            But, that does not mean that there is no way forward without a
            conflict. It's possible to have nonconflicting alt subsets as in:

               LL altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}]

            from

               [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]),
                (13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])]

            In this case, (17,1,[5 $]) indicates there is some next sequence that
            would resolve this without conflict to alternative 1. Any other viable
            next sequence, however, is associated with a conflict.  We stop
            looking for input because no amount of further lookahead will alter
            the fact that we should predict alternative 1.  We just can't say for
            sure that there is an ambiguity without looking further.
            */
            ReportAmbiguity(dfa, D, startIndex, input.Index, foundExactAmbig, reach.GetAlts(), reach);

            return predictedAlt;
        }
Пример #59
0
 public DFAState(DFA dfa, ATNConfigSet configs)
     : this(dfa.EmptyEdgeMap, dfa.EmptyContextEdgeMap, configs)
 {
 }
Пример #60
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);
        }