antlr/antlr4 · error · Exception
This ATN simulator does not support clearing the DFA.
Error message
This ATN simulator does not support clearing the DFA.
What it means
ATNSimulator.ClearDFA() is a virtual method whose base implementation always throws, following the Java original's UnsupportedOperationException contract: only simulators that actually own an adaptable DFA cache (notably ParserATNSimulator) may clear it. Calling it on a simulator type that did not override the method is treated as an unsupported operation rather than a silent no-op, so mistakes surface immediately.
Source
Thrown at runtime/CSharp/src/Atn/ATNSimulator.cs:75
this.sharedContextCache = sharedContextCache;
}
public abstract void Reset();
/**
* Clear the DFA cache used by the current instance. Since the DFA cache may
* be shared by multiple ATN simulators, this method may affect the
* performance (but not accuracy) of other parsers which are being used
* concurrently.
*
* @throws UnsupportedOperationException if the current instance does not
* support clearing the DFA.
*
* @since 4.3
*/
public virtual void ClearDFA()
{
throw new Exception("This ATN simulator does not support clearing the DFA.");
}
public PredictionContextCache getSharedContextCache()
{
return sharedContextCache;
}
public PredictionContext getCachedContext(PredictionContext context)
{
if (sharedContextCache == null) return context;
lock (sharedContextCache)
{
PredictionContext.IdentityHashMap visited =
new PredictionContext.IdentityHashMap();
return PredictionContext.GetCachedContext(context,
sharedContextCache,
visited);View on GitHub (pinned to 7d5770395b)
Solutions
- Call ClearDFA() only on a concrete ParserATNSimulator reference (or your own subclass that overrides ClearDFA)
- If you own the subclass, override ClearDFA() and implement cache reset for your DFA storage
- Instead of clearing, rebuild the simulator and its DFA array (recreate DFA[decision] entries), which works for any simulator type
Example fix
// before
ATNSimulator sim = GetSimulator();
sim.ClearDFA(); // throws Exception: does not support clearing the DFA
// after
if (sim is ParserATNSimulator parserSim)
parserSim.ClearDFA();
else
RebuildSimulator(); // recreate simulators and their DFA caches Defensive patterns
Strategy: type-guard
Type guard
static bool CanClearDfa(ATNSimulator sim) => sim is ParserATNSimulator;
Try / catch
try { sim.ClearDFA(); } catch (Exception ex) when (ex.Message.Contains("does not support clearing")) { /* rebuild simulators instead */ RebuildSimulators(); } Prevention
- Type the variable as the concrete simulator class, not ATNSimulator, when you intend to clear caches
- Prefer recreating DFA objects over clearing when the concrete type is unknown
When it happens
Trigger: Calling ClearDFA() on an ATNSimulator subclass that does not override it (e.g. a custom simulator or an older-style instance), or on a simulator whose DFA cache design (shared/immutable DFA) makes clearing invalid. In the C# runtime ParserATNSimulator overrides it; the base and other subclasses do not.
Common situations: Porting Java code that called clearDFA() on a simulator variable statically typed as ATNSimulator; writing warmup/reset logic that clears caches after loading grammars; custom ATN simulator subclasses that forget to override ClearDFA.
Related errors
- Precedence predicates are not supported in lexers.
- cannot consume EOF
- EdgeMap of type {0} is supported yet.
- Only precedence DFAs may contain a precedence start state.
- listener cannot be null.
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/c667e69788e6dcdf.
Report an issue: GitHub.