{"record":{"id":"8baa93ced8f3b105","repo":"antlr/antlr4","slug":"the-current-parser-does-not-support-an-atn-with-by-8baa93","errorCode":null,"errorMessage":"The current parser does not support an ATN with bypass alternatives.","messagePattern":"The current parser does not support an ATN with bypass alternatives\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"runtime/Python3/src/antlr4/Parser.py","lineNumber":262,"sourceCode":"        return self._syntaxErrors\n\n    def getTokenFactory(self):\n        return self._input.tokenSource._factory\n\n    # Tell our token source and error strategy about a new way to create tokens.#\n    def setTokenFactory(self, factory:TokenFactory):\n        self._input.tokenSource._factory = factory\n\n    # The ATN with bypass alternatives is expensive to create so we create it\n    # lazily.\n    #\n    # @throws UnsupportedOperationException if the current parser does not\n    # implement the {@link #getSerializedATN()} method.\n    #\n    def getATNWithBypassAlts(self):\n        serializedAtn = self.getSerializedATN()\n        if serializedAtn is None:\n            raise UnsupportedOperationException(\"The current parser does not support an ATN with bypass alternatives.\")\n        result = self.bypassAltsAtnCache.get(serializedAtn, None)\n        if result is None:\n            deserializationOptions = ATNDeserializationOptions()\n            deserializationOptions.generateRuleBypassTransitions = True\n            result = ATNDeserializer(deserializationOptions).deserialize(serializedAtn)\n            self.bypassAltsAtnCache[serializedAtn] = result\n        return result\n\n    # The preferred method of getting a tree pattern. For example, here's a\n    # sample use:\n    #\n    # <pre>\n    # ParseTree t = parser.expr();\n    # ParseTreePattern p = parser.compileParseTreePattern(\"&lt;ID&gt;+0\", MyParser.RULE_expr);\n    # ParseTreeMatch m = p.match(t);\n    # String id = m.get(\"ID\");\n    # </pre>\n    #","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Python3/src/antlr4/Parser.py#L244-L280","documentation":"Parser.getATNWithBypassAlts() lazily builds a second ATN with bypass-alternative transitions (used by ParseTreePatternMatcher for '<tag>' wildcards) from the parser's serialized ATN. It raises UnsupportedOperationException when getSerializedATN() returns None, i.e., the recognizer does not carry the serialized ATN string that generated parsers embed.","triggerScenarios":"Calling compileParseTreePattern()/getTokenStream() utilities on a hand-written Parser subclass that never implements getSerializedATN(); a custom Recognizer base where the serialized ATN field was stripped; generated-code variants that store the ATN only as deserialized objects.","commonSituations":"Tree pattern matching or XPath on a manually created parser; using a parser class post-processed/minified to remove the big serialized-ATN string; mixing runtime versions where older generated parsers lack the expected serialization.","solutions":["Regenerate the parser with the ANTLR 4 tool so getSerializedATN() returns the embedded serialized ATN","If the parser is hand-written, implement getSerializedATN() to return the same string the tool would emit (or reuse the generated class instead)","For pure tree-pattern use, prefer a generated parser even in tests, since bypass-ATN construction is required"],"exampleFix":"# before\nparser = MyHandWrittenParser(tokens)\npattern = parser.compileParseTreePattern(\"<ID>\", MyHandWrittenParser.RULE_expr)  # raises\n\n# after\nfrom gen.MyParser import MyParser  # tool-generated, has serializedATN\nparser = MyParser(tokens)\npattern = parser.compileParseTreePattern(\"<ID>\", MyParser.RULE_expr)","handlingStrategy":"type-guard","validationCode":"# Python: confirm serialized ATN exists before pattern compilation\nif parser.getSerializedATN() is None:\n    raise ValueError(\"use a tool-generated parser for tree patterns\")\npattern = parser.compileParseTreePattern(\"<ID>\", rule_index, lexer)","typeGuard":"# Python\ndef supports_bypass_atn(recognizer) -> bool:\n    return recognizer.getSerializedATN() is not None","tryCatchPattern":"try:\n    atn = parser.getATNWithBypassAlts()\nexcept UnsupportedOperationException:\n    # regenerate parser with the ANTLR tool, or implement getSerializedATN()\n    raise","preventionTips":["Always compile tree patterns against generated parser classes","Regenerate parsers after tool upgrades instead of reusing hand-copied classes","Keep the serialized ATN string intact when minifying generated code"],"tags":["antlr","python","parser","atn","tree-patterns"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}