{"record":{"id":"162c2fd667f90e76","repo":"stanfordnlp/CoreNLP","slug":"expecting-begin-block-got","errorCode":null,"errorMessage":"expecting BEGIN block; got ","messagePattern":"expecting BEGIN block; got ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java","lineNumber":508,"sourceCode":"        dg.writeData(out);\n      }\n      out.println();\n      log.info(\".\");\n\n      out.flush();\n      out.close();\n      log.info(\"done.\");\n    } catch (IOException e) {\n      log.info(\"Trouble saving parser data to ASCII format.\");\n      throw new RuntimeIOException(e);\n    }\n  }\n\n  private static void confirmBeginBlock(String file, String line) {\n    if (line == null) {\n      throw new RuntimeException(file + \": expecting BEGIN block; got end of file.\");\n    } else if (! line.startsWith(\"BEGIN\")) {\n      throw new RuntimeException(file + \": expecting BEGIN block; got \" + line);\n    }\n  }\n\n  protected static LexicalizedParser getParserFromTextFile(String textFileOrUrl, Options op) {\n    try (BufferedReader in = IOUtils.readerFromString(textFileOrUrl)) {\n      Timing tim = new Timing();\n\n      String line = in.readLine();\n      confirmBeginBlock(textFileOrUrl, line);\n      op.readData(in);\n\n      line = in.readLine();\n      confirmBeginBlock(textFileOrUrl, line);\n      Index<String> stateIndex = HashIndex.loadFromReader(in);\n\n      line = in.readLine();\n      confirmBeginBlock(textFileOrUrl, line);\n      Index<String> wordIndex = HashIndex.loadFromReader(in);","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java#L490-L526","documentation":"confirmBeginBlock validates that the current line of a text-format parser grammar file begins a BEGIN block. If the file ends before any line is read (line == null) or the line does not start with \"BEGIN\", a RuntimeException is thrown naming the grammar file and the offending line. This means the grammar text file is malformed or is not a text grammar at all.","triggerScenarios":"Calling LexicalizedParser.getParserFromTextFile (via loadParserFromTextFile / the main pd path after a serialized load fails) on a file whose structure is not the expected BEGIN/END sectioned grammar text format.","commonSituations":"Passing a serialized (binary) parser model path or an unrelated text file as a text grammar; a grammar file that was truncated during download/copy; editing a grammar file and deleting or renaming the BEGIN header; CRLF or BOM corruption changing the first characters of the header line.","solutions":["Check the file at the path printed in the exception: it must be a text grammar whose sections start with BEGIN lines (e.g. BEGIN_GENERIC, BEGIN_STATE)","If you actually have a serialized model, load it with getParserFromSerializedFile / LexicalizedParser.loadModel instead of the text-file path","Re-download or restore the original grammar file — it may be truncated or corrupted","Ensure the file is plain UTF-8 with no BOM and no stray leading blank/garbage line before the first BEGIN","Enable parser verbosity (-vv or set op.testOptions.verbose) and compare with a known-good shipped grammar like englishPCFG.ser.gz format"],"exampleFix":"// before\nLexicalizedParser lp = LexicalizedParser.loadModel(\"myGrammar.txt\"); // binary file read as text grammar\n// after\nLexicalizedParser lp = LexicalizedParser.loadModel(\"englishPCFG.ser.gz\"); // correct serialized model","handlingStrategy":"validation","validationCode":"// Java: sanity-check the file looks like a text grammar before loading\nBufferedReader r = Files.newBufferedReader(Paths.get(path), StandardCharsets.UTF_8);\nString first = r.readLine();\nr.close();\nif (first == null || !first.startsWith(\"BEGIN\")) throw new IllegalArgumentException(\"not a text grammar: \" + path);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only pass text grammars (BEGIN/END sectioned) to the text-file loader; use loadModel for .ser/.ser.gz","Verify checksums of downloaded grammar files to catch truncation","Strip BOMs and normalize line endings on edited grammar files"],"tags":["parser","grammar-file","file-format","java"],"backgroundTag":"invalid-argument-format","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}