{"record":{"id":"c667e69788e6dcdf","repo":"antlr/antlr4","slug":"this-atn-simulator-does-not-support-clearing-the-d-c667e6","errorCode":null,"errorMessage":"This ATN simulator does not support clearing the DFA.","messagePattern":"This ATN simulator does not support clearing the DFA\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/Atn/ATNSimulator.cs","lineNumber":75,"sourceCode":"\t\t\tthis.sharedContextCache = sharedContextCache;\n\t\t}\n\n\t\tpublic abstract void Reset();\n\n\t\t/**\n\t\t * Clear the DFA cache used by the current instance. Since the DFA cache may\n\t\t * be shared by multiple ATN simulators, this method may affect the\n\t\t * performance (but not accuracy) of other parsers which are being used\n\t\t * concurrently.\n\t\t *\n\t\t * @throws UnsupportedOperationException if the current instance does not\n\t\t * support clearing the DFA.\n\t\t *\n\t\t * @since 4.3\n\t\t */\n\t\tpublic virtual void ClearDFA()\n\t\t{\n\t\t\tthrow new Exception(\"This ATN simulator does not support clearing the DFA.\");\n\t\t}\n\n        public PredictionContextCache getSharedContextCache()\n\t\t{\n\t\t\treturn sharedContextCache;\n\t\t}\n\n\t\tpublic PredictionContext getCachedContext(PredictionContext context)\n\t\t{\n\t\t\tif (sharedContextCache == null) return context;\n\n\t\t\tlock (sharedContextCache)\n\t\t\t{\n\t\t\t\tPredictionContext.IdentityHashMap visited =\n\t\t\t\t\tnew PredictionContext.IdentityHashMap();\n\t\t\t\treturn PredictionContext.GetCachedContext(context,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t  sharedContextCache,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t  visited);","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/Atn/ATNSimulator.cs#L57-L93","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nATNSimulator sim = GetSimulator();\nsim.ClearDFA(); // throws Exception: does not support clearing the DFA\n\n// after\nif (sim is ParserATNSimulator parserSim)\n    parserSim.ClearDFA();\nelse\n    RebuildSimulator(); // recreate simulators and their DFA caches","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static bool CanClearDfa(ATNSimulator sim) => sim is ParserATNSimulator;","tryCatchPattern":"try { sim.ClearDFA(); } catch (Exception ex) when (ex.Message.Contains(\"does not support clearing\")) { /* rebuild simulators instead */ RebuildSimulators(); }","preventionTips":["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"],"tags":["antlr","csharp","dfa","simulator","api-misuse"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}