{"record":{"id":"8b67bcd49c37f8ef","repo":"antlr/antlr4","slug":"the-current-parser-does-not-support-an-atn-with-by","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/Java/src/org/antlr/v4/runtime/Parser.java","lineNumber":447,"sourceCode":"\n\t/** Tell our token source and error strategy about a new way to create tokens. */\n\t@Override\n\tpublic void setTokenFactory(TokenFactory<?> factory) {\n\t\t_input.getTokenSource().setTokenFactory(factory);\n\t}\n\n\t/**\n\t * The ATN with bypass alternatives is expensive to create so we create it\n\t * lazily.\n\t *\n\t * @throws UnsupportedOperationException if the current parser does not\n\t * implement the {@link #getSerializedATN()} method.\n\t */\n\n\tpublic ATN getATNWithBypassAlts() {\n\t\tString serializedAtn = getSerializedATN();\n\t\tif (serializedAtn == null) {\n\t\t\tthrow new UnsupportedOperationException(\"The current parser does not support an ATN with bypass alternatives.\");\n\t\t}\n\n\t\tsynchronized (this) {\n\t\t\tif ( bypassAltsAtnCache!=null ) {\n\t\t\t\treturn bypassAltsAtnCache;\n\t\t\t}\n\t\t\tATNDeserializationOptions deserializationOptions = new ATNDeserializationOptions();\n\t\t\tdeserializationOptions.setGenerateRuleBypassTransitions(true);\n\t\t\tbypassAltsAtnCache = new ATNDeserializer(deserializationOptions).deserialize(serializedAtn.toCharArray());\n\t\t\treturn bypassAltsAtnCache;\n\t\t}\n\t}\n\n\t/**\n\t * The preferred method of getting a tree pattern. For example, here's a\n\t * sample use:\n\t *\n\t * <pre>","sourceCodeStart":429,"sourceCodeEnd":465,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/Java/src/org/antlr/v4/runtime/Parser.java#L429-L465","documentation":"Parser.getATNWithBypassAlts() builds a special ATN where each rule gets a bypass alternative, needed for parse-tree pattern matching (trees that skip parts of a rule). It requires the parser to expose its serialized ATN via getSerializedATN(); if that method returns null (the base Parser default), it throws UnsupportedOperationException. Hand-written Parser subclasses that don't implement getSerializedATN() therefore cannot support pattern matching with bypass alts.","triggerScenarios":"Calling getATNWithBypassAlts() on a custom Parser subclass that does not override getSerializedATN(); using ParseTreePatternMatcher machinery that internally requests the bypass ATN on such a parser; partially generated parsers where the serialized ATN field was stripped.","commonSituations":"Advanced tooling built on parse-tree patterns (e.g. custom refactoring or lint rules); parsers hand-instantiated instead of generated; environments where generated code was minimized/obfuscated, dropping the serialized ATN.","solutions":["Use a normally generated parser: generated classes implement getSerializedATN() with the static _SERIALIZED_ATN data","If you subclass a generated parser, inherit (don't hide) getSerializedATN()","For custom parsers, implement getSerializedATN() to return the serialized ATN data so the bypass ATN can be built"],"exampleFix":"// before\nclass MyParser extends Parser { /* no getSerializedATN() */ }\nmyParser.getATNWithBypassAlts(); // throws\n\n// after\nclass MyParser extends Parser {\n  @Override public String getSerializedATN() { return _SERIALIZED_ATN; }\n}","handlingStrategy":"type-guard","validationCode":"if (parser.getSerializedATN() != null) {\n  ATN bypass = parser.getATNWithBypassAlts();\n}","typeGuard":"boolean supportsBypassAlts(Parser p) { return p.getSerializedATN() != null; }","tryCatchPattern":null,"preventionTips":["Check getSerializedATN() != null before requesting the bypass ATN","Use fully generated parsers, which always carry _SERIALIZED_ATN","Don't hide getSerializedATN() when subclassing a generated parser"],"tags":["antlr","parser","pattern-matching","atn","unsupported-operation"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}