{"record":{"id":"3ea77620a442bffc","repo":"stanfordnlp/CoreNLP","slug":"error-illegal-node-code-s","errorCode":null,"errorMessage":"Error: illegal node code \" + s","messagePattern":"Error: illegal node code \" \\+ s","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/tregex/TregexPattern.java","lineNumber":538,"sourceCode":"  /**\n   * Print a multi-line representation of the pattern illustrating\n   * it's syntax to System.out.\n   */\n  public void prettyPrint() {\n    prettyPrint(System.out);\n  }\n\n\n  private static final Pattern codePattern = Pattern.compile(\"([0-9]+):([0-9]+)\");\n\n  private static void extractSubtrees(List<String> codeStrings, String treeFile) {\n    List<Pair<Integer,Integer>> codes = new ArrayList<>();\n    for(String s : codeStrings) {\n      Matcher m = codePattern.matcher(s);\n      if(m.matches())\n        codes.add(new Pair<>(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2))));\n      else\n        throw new RuntimeException(\"Error: illegal node code \" + s);\n    }\n    TreeReaderFactory trf = new TRegexTreeReaderFactory();\n    MemoryTreebank treebank = new MemoryTreebank(trf);\n    treebank.loadPath(treeFile,null, true);\n    for (Pair<Integer,Integer> code : codes) {\n      Tree t = treebank.get(code.first()-1);\n      t.getNodeNumber(code.second()).pennPrint();\n    }\n  }\n\n  /**\n   * Prints out all matches of a tree pattern on each tree in the path. Usage:\n   *\n   * {@code\n   * java edu.stanford.nlp.trees.tregex.TregexPattern [[-TCwfosnu] [-filter] [-h <node-name>]]* pattern filepath\n   * }\n   *\n   *","sourceCodeStart":520,"sourceCodeEnd":556,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/tregex/TregexPattern.java#L520-L556","documentation":"TregexPattern's extractSubtrees utility reads node codes of the form 'i:j' (tree index i, node index j) from a file and extracts the corresponding subtrees. If any code string does not match the expected numeric pattern, it throws RuntimeException('Error: illegal node code ' + s).","triggerScenarios":"Running extractSubtrees/main with a -code file containing lines that are not 'i:j' numeric pairs (e.g. blank lines, prose, comma-separated values, or non-numeric tokens).","commonSituations":"Hand-edited or tool-generated code files with formatting drift; saving matcher output in a different format; CSV export pasted into the codes file.","solutions":["Fix the codes file so every line matches the pattern 'i:j' with positive integers","Regenerate the codes file from the tool that produced the matches","Pre-validate each line with a regex before feeding it to extractSubtrees"],"exampleFix":"// before\ncodes file line: \"12, 3\"\n// after\ncodes file line: \"12:3\"","handlingStrategy":"validation","validationCode":"Pattern codePattern = Pattern.compile(\"(\\\\d+):(\\\\d+)\");\nfor (String s : lines) if (!codePattern.matcher(s.trim()).matches()) throw new IllegalArgumentException(\"illegal node code: \" + s);","typeGuard":"boolean isNodeCode(String s) { return s != null && s.matches(\"\\\\d+:\\\\d+\"); }","tryCatchPattern":"try { extractSubtrees(args); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Error: illegal node code\")) { /* fix codes file line */ } }","preventionTips":["Keep codes files strictly in 'i:j' format, one per line","Regenerate codes files from the matching tool rather than hand-editing","Strip blank lines/whitespace before processing"],"tags":["tregex","cli","parsing"],"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-17T15:17:12.973Z"}