1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
8 #include "antlr4-common.h"
14 /// A DFA state represents a set of possible ATN configurations.
15 /// As Aho, Sethi, Ullman p. 117 says "The DFA uses its state
16 /// to keep track of all possible states the ATN can be in after
17 /// reading each input symbol. That is to say, after reading
18 /// input a1a2..an, the DFA is in a state that represents the
19 /// subset T of the states of the ATN that are reachable from the
20 /// ATN's start state along some path labeled a1a2..an."
21 /// In conventional NFA->DFA conversion, therefore, the subset T
22 /// would be a bitset representing the set of states the
23 /// ATN could be in. We need to track the alt predicted by each
24 /// state as well, however. More importantly, we need to maintain
25 /// a stack of states, tracking the closure operations as they
26 /// jump from rule to rule, emulating rule invocations (method calls).
27 /// I have to add a stack to simulate the proper lookahead sequences for
28 /// the underlying LL grammar from which the ATN was derived.
30 /// I use a set of ATNConfig objects not simple states. An ATNConfig
31 /// is both a state (ala normal conversion) and a RuleContext describing
32 /// the chain of rules (if any) followed to arrive at that state.
34 /// A DFA state may have multiple references to a particular state,
35 /// but with different ATN contexts (with same or different alts)
36 /// meaning that state was reached via a different set of rule invocations.
38 class ANTLR4CPP_PUBLIC DFAState {
40 class PredPrediction {
42 Ref<atn::SemanticContext> pred; // never null; at least SemanticContext.NONE
45 PredPrediction(const Ref<atn::SemanticContext> &pred, int alt);
46 virtual ~PredPrediction();
48 virtual std::string toString();
51 void InitializeInstanceFields();
56 std::unique_ptr<atn::ATNConfigSet> configs;
58 /// {@code edges[symbol]} points to target of symbol. Shift up by 1 so (-1)
59 /// <seealso cref="Token#EOF"/> maps to {@code edges[0]}.
60 // ml: this is a sparse list, so we use a map instead of a vector.
61 // Watch out: we no longer have the -1 offset, as it isn't needed anymore.
62 std::unordered_map<size_t, DFAState *> edges;
66 /// if accept state, what ttype do we match or alt do we predict?
67 /// This is set to <seealso cref="ATN#INVALID_ALT_NUMBER"/> when <seealso cref="#predicates"/>{@code !=null} or
68 /// <seealso cref="#requiresFullContext"/>.
71 Ref<atn::LexerActionExecutor> lexerActionExecutor;
74 /// Indicates that this state was created during SLL prediction that
75 /// discovered a conflict between the configurations in the state. Future
76 /// <seealso cref="ParserATNSimulator#execATN"/> invocations immediately jumped doing
77 /// full context prediction if this field is true.
79 bool requiresFullContext;
82 /// During SLL parsing, this is a list of predicates associated with the
83 /// ATN configurations of the DFA state. When we have predicates,
84 /// <seealso cref="#requiresFullContext"/> is {@code false} since full context prediction evaluates predicates
85 /// on-the-fly. If this is not null, then <seealso cref="#prediction"/> is
86 /// <seealso cref="ATN#INVALID_ALT_NUMBER"/>.
88 /// We only use these for non-<seealso cref="#requiresFullContext"/> but conflicting states. That
89 /// means we know from the context (it's $ or we don't dip into outer
90 /// context) that it's an ambiguity not a conflict.
92 /// This list is computed by <seealso cref="ParserATNSimulator#predicateDFAState"/>.
94 std::vector<PredPrediction *> predicates;
96 /// Map a predicate to a predicted alternative.
99 DFAState(std::unique_ptr<atn::ATNConfigSet> configs);
103 /// Get the set of all alts mentioned by all ATN configurations in this
106 virtual std::set<size_t> getAltSet();
108 virtual size_t hashCode() const;
110 /// Two DFAState instances are equal if their ATN configuration sets
111 /// are the same. This method is used to see if a state already exists.
113 /// Because the number of alternatives and number of ATN configurations are
114 /// finite, there is a finite number of DFA states that can be processed.
115 /// This is necessary to show that the algorithm terminates.
117 /// Cannot test the DFA state numbers here because in
118 /// ParserATNSimulator#addDFAState we need to know if any other state
119 /// exists that has this exact set of ATN configurations. The
120 /// stateNumber is irrelevant.
121 bool operator == (const DFAState &o) const;
123 virtual std::string toString();
127 size_t operator()(DFAState *k) const {
128 return k->hashCode();
133 bool operator()(DFAState *lhs, DFAState *rhs) const
140 void InitializeInstanceFields();
144 } // namespace antlr4