{"record":{"id":"3b083f5dd62a11ab","repo":"antlr/antlr4","slug":"can-t-create-parser-to-match-incoming-parserclass","errorCode":null,"errorMessage":"can't create parser to match incoming {parserClass}","messagePattern":"can't create parser to match incoming (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"tool/src/org/antlr/v4/tool/GrammarParserInterpreter.java","lineNumber":401,"sourceCode":"\n\t\treturn trees;\n\t}\n\n\t/** Derive a new parser from an old one that has knowledge of the grammar.\n\t *  The Grammar object is used to correctly compute outer alternative\n\t *  numbers for parse tree nodes. A parser of the same type is created\n\t *  for subclasses of {@link ParserInterpreter}.\n\t */\n\tpublic static ParserInterpreter deriveTempParserInterpreter(Grammar g, Parser originalParser, TokenStream tokens) {\n\t\tParserInterpreter parser;\n\t\tif (originalParser instanceof ParserInterpreter) {\n\t\t\tClass<? extends ParserInterpreter> c = originalParser.getClass().asSubclass(ParserInterpreter.class);\n\t\t\ttry {\n\t\t\t\tConstructor<? extends ParserInterpreter> ctor = c.getConstructor(Grammar.class, ATN.class, TokenStream.class);\n\t\t\t\tparser = ctor.newInstance(g, originalParser.getATN(), originalParser.getTokenStream());\n\t\t\t}\n\t\t\tcatch (Exception e) {\n\t\t\t\tthrow new IllegalArgumentException(\"can't create parser to match incoming \"+originalParser.getClass().getSimpleName(), e);\n\t\t\t}\n\t\t}\n\t\telse { // must've been a generated parser\n//\t\t\tIntegerList serialized = ATNSerializer.getSerialized(originalParser.getATN(), g.getLanguage());\n//\t\t\tATN deserialized = new ATNDeserializer().deserialize(serialized.toArray());\n\t\t\tparser = new ParserInterpreter(originalParser.getGrammarFileName(),\n\t\t\t\t\t\t\t\t\t\t   originalParser.getVocabulary(),\n\t\t\t\t\t\t\t\t\t\t   Arrays.asList(originalParser.getRuleNames()),\n\t\t\t\t\t                       originalParser.getATN(),\n\t\t\t\t\t\t\t\t\t\t   tokens);\n\t\t}\n\n\t\tparser.setInputStream(tokens);\n\n\t\t// Make sure that we don't get any error messages from using this temporary parser\n\t\tparser.setErrorHandler(new BailErrorStrategy());\n\t\tparser.removeErrorListeners();\n\t\tparser.removeParseListeners();","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/tool/src/org/antlr/v4/tool/GrammarParserInterpreter.java#L383-L419","documentation":"deriveTempParserInterpreter reflects on the incoming parser's class to find a (Grammar, ATN, TokenStream) constructor for subclasses of ParserInterpreter. If the subclass lacks that exact constructor (or construction fails), the reflective exception is wrapped in IllegalArgumentException naming the offending parser class.","triggerScenarios":"Passing a custom ParserInterpreter subclass into deriveTempParserInterpreter (directly or via GrammarParserInterpreter.parseFromStartRule / profiling paths) when the subclass does not declare a public ParserInterpreter(Grammar, ATN, TokenStream) constructor.","commonSituations":"User-defined ParserInterpreter subclasses for instrumentation or error recovery that add constructors but forget the (Grammar, ATN, TokenStream) one, or make it non-public.","solutions":["Add a public constructor with signature (Grammar, ATN, TokenStream) to your ParserInterpreter subclass that calls super(...)","Alternatively pass a plain generated parser or base ParserInterpreter, which takes the generated-parser branch and avoids reflection"],"exampleFix":"// before\nclass MyParserInterpreter extends ParserInterpreter {\n    MyParserInterpreter(Grammar g, ATN atn, TokenStream ts, ExtraConfig cfg) { super(g, atn, ts); }\n}\n\n// after\nclass MyParserInterpreter extends ParserInterpreter {\n    public MyParserInterpreter(Grammar g, ATN atn, TokenStream ts) { super(g, atn, ts); }\n    MyParserInterpreter(Grammar g, ATN atn, TokenStream ts, ExtraConfig cfg) { this(g, atn, ts); }\n}","handlingStrategy":"validation","validationCode":"Constructor<?> ctor = parserClass.getConstructor(Grammar.class, ATN.class, TokenStream.class); // throws NoSuchMethodException if missing","typeGuard":"static boolean hasInterpreterCtor(Class<? extends ParserInterpreter> c) {\n    try { c.getConstructor(Grammar.class, ATN.class, TokenStream.class); return true; }\n    catch (NoSuchMethodException e) { return false; }\n}","tryCatchPattern":"try { deriveTempParserInterpreter(g, parser, tokens); } catch (IllegalArgumentException e) { /* add (Grammar,ATN,TokenStream) ctor to subclass or pass base ParserInterpreter */ }","preventionTips":["Give every ParserInterpreter subclass a public (Grammar, ATN, TokenStream) constructor","Prefer base ParserInterpreter or generated parsers when custom subclasses are not essential"],"tags":["antlr","reflection","interpreter","java"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}