{"record":{"id":"b4be5363743cbeaa","repo":"antlr/antlr4","slug":"parser-can-t-discover-a-lexer-to-use-b4be53","errorCode":null,"errorMessage":"Parser can't discover a lexer to use","messagePattern":"Parser can't discover a lexer to use","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/Parser.cs","lineNumber":605,"sourceCode":"        /// <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        /// </remarks>\n        public virtual ParseTreePattern CompileParseTreePattern(string pattern, int patternRuleIndex)\n        {\n            if (((ITokenStream)InputStream) != null)\n            {\n                ITokenSource tokenSource = ((ITokenStream)InputStream).TokenSource;\n                if (tokenSource is Lexer)\n                {\n                    Lexer lexer = (Lexer)tokenSource;\n                    return CompileParseTreePattern(pattern, patternRuleIndex, lexer);\n                }\n            }\n            throw new NotSupportedException(\"Parser can't discover a lexer to use\");\n        }\n\n        /// <summary>\n        /// The same as\n        /// <see cref=\"CompileParseTreePattern(string, int)\"/>\n        /// but specify a\n        /// <see cref=\"Lexer\"/>\n        /// rather than trying to deduce it from this parser.\n        /// </summary>\n        public virtual ParseTreePattern CompileParseTreePattern(string pattern, int patternRuleIndex, Lexer lexer)\n        {\n            ParseTreePatternMatcher m = new ParseTreePatternMatcher(lexer, this);\n            return m.Compile(pattern, patternRuleIndex);\n        }\n\n        public virtual IAntlrErrorStrategy ErrorHandler\n        {\n            get","sourceCodeStart":587,"sourceCodeEnd":623,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/Parser.cs#L587-L623","documentation":"Error \"Parser can't discover a lexer to use\" thrown in antlr/antlr4.","triggerScenarios":"Parser.BypassAltsAtnCache/lexer discovery path cannot locate a lexer type for the combined grammar.","commonSituations":"Ensure the combined grammar's generated lexer class is available and named per convention (GrammarNameLexer).","solutions":["Provide the matching lexer explicitly: create the lexer, feed it a CharStream, wrap it in a CommonTokenStream, and pass that to the parser constructor.","Ensure the lexer class name follows the grammar naming convention (<grammarName>Lexer) so discovery can succeed."],"exampleFix":"var lexer = new MyGrammarLexer(new AntlrInputStream(input));\nvar parser = new MyGrammarParser(new CommonTokenStream(lexer));","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}