{"record":{"id":"c6ba311e103e932e","repo":"antlr/antlr4","slug":"a-parser-interpreter-can-only-be-created-for-a-par","errorCode":null,"errorMessage":"A parser interpreter can only be created for a parser or combined grammar.","messagePattern":"A parser interpreter can only be created for a parser or combined grammar\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"tool/src/org/antlr/v4/tool/Grammar.java","lineNumber":1347,"sourceCode":"\t\tallChannels.addAll(channelValueToNameList);\n\n\t\t// must run ATN through serializer to set some state flags\n\t\tIntegerList serialized = ATNSerializer.getSerialized(atn);\n\t\tATN deserializedATN = new ATNDeserializer().deserialize(serialized.toArray());\n\t\treturn new LexerInterpreter(\n\t\t\t\tfileName,\n\t\t\t\tgetVocabulary(),\n\t\t\t\tArrays.asList(getRuleNames()),\n\t\t\t\tallChannels,\n\t\t\t\t((LexerGrammar)this).modes.keySet(),\n\t\t\t\tdeserializedATN,\n\t\t\t\tinput);\n\t}\n\n\t/** @since 4.5.1 */\n\tpublic GrammarParserInterpreter createGrammarParserInterpreter(TokenStream tokenStream) {\n\t\tif (this.isLexer()) {\n\t\t\tthrow new IllegalStateException(\"A parser interpreter can only be created for a parser or combined grammar.\");\n\t\t}\n\t\t// must run ATN through serializer to set some state flags\n\t\tIntegerList serialized = ATNSerializer.getSerialized(atn);\n\t\tATN deserializedATN = new ATNDeserializer().deserialize(serialized.toArray());\n\n\t\treturn new GrammarParserInterpreter(this, deserializedATN, tokenStream);\n\t}\n\n\tpublic ParserInterpreter createParserInterpreter(TokenStream tokenStream) {\n\t\tif (this.isLexer()) {\n\t\t\tthrow new IllegalStateException(\"A parser interpreter can only be created for a parser or combined grammar.\");\n\t\t}\n\n\t\t// must run ATN through serializer to set some state flags\n\t\tIntegerList serialized = ATNSerializer.getSerialized(atn);\n\t\tATN deserializedATN = new ATNDeserializer().deserialize(serialized.toArray());\n\n\t\treturn new ParserInterpreter(fileName, getVocabulary(), Arrays.asList(getRuleNames()), deserializedATN, tokenStream);","sourceCodeStart":1329,"sourceCodeEnd":1365,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/tool/src/org/antlr/v4/tool/Grammar.java#L1329-L1365","documentation":"Grammar.createGrammarParserInterpreter(TokenStream) constructs a GrammarParserInterpreter (rule-name-aware subclass) but a lexer grammar has no parser rules or parser ATN, so it throws IllegalStateException immediately when this.isLexer().","triggerScenarios":"Calling createGrammarParserInterpreter(tokens) on a Grammar loaded from a lexer-only grammar file (grammar type LEXER).","commonSituations":"Generic grammar-processing tooling that calls the interpreter factory on every loaded grammar without checking its type.","solutions":["Guard with if (g.isLexer()) skip/return before calling the method","Load the parser/combined grammar when you need a parser interpreter"],"exampleFix":"// before\nGrammarParserInterpreter it = g.createGrammarParserInterpreter(tokens);\n\n// after\nif (g.isLexer()) throw new IllegalStateException(\"not a parser grammar\");\nGrammarParserInterpreter it = g.createGrammarParserInterpreter(tokens);","handlingStrategy":"type-guard","validationCode":"boolean canParse = !g.isLexer();","typeGuard":"static boolean supportsParserInterpreter(Grammar g) { return !g.isLexer(); }","tryCatchPattern":"try { g.createGrammarParserInterpreter(tokens); } catch (IllegalStateException e) { /* skip: grammar is lexer-only */ }","preventionTips":["Check grammar type before calling interpreter factories","Write harnesses that dispatch on g.getType()"],"tags":["antlr","grammar","interpreter","java"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}