stanfordnlp/CoreNLP · error · RuntimeException

Error: illegal node code " + s

Error message

Error: illegal node code " + s

What it means

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).

Solutions

  1. Fix the codes file so every line matches the pattern 'i:j' with positive integers
  2. Regenerate the codes file from the tool that produced the matches
  3. Pre-validate each line with a regex before feeding it to extractSubtrees

Example fix

// before
codes file line: "12, 3"
// after
codes file line: "12:3"
Defensive patterns

Strategy: validation

Validate before calling

Pattern codePattern = Pattern.compile("(\\d+):(\\d+)");
for (String s : lines) if (!codePattern.matcher(s.trim()).matches()) throw new IllegalArgumentException("illegal node code: " + s);

Type guard

boolean isNodeCode(String s) { return s != null && s.matches("\\d+:\\d+"); }

Try / catch

try { extractSubtrees(args); } catch (RuntimeException e) { if (e.getMessage().startsWith("Error: illegal node code")) { /* fix codes file line */ } }

Prevention

When it happens

Trigger: 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).

Common situations: Hand-edited or tool-generated code files with formatting drift; saving matcher output in a different format; CSV export pasted into the codes file.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/3ea77620a442bffc. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/tregex/TregexPattern.java:538

  /**
   * Print a multi-line representation of the pattern illustrating
   * it's syntax to System.out.
   */
  public void prettyPrint() {
    prettyPrint(System.out);
  }


  private static final Pattern codePattern = Pattern.compile("([0-9]+):([0-9]+)");

  private static void extractSubtrees(List<String> codeStrings, String treeFile) {
    List<Pair<Integer,Integer>> codes = new ArrayList<>();
    for(String s : codeStrings) {
      Matcher m = codePattern.matcher(s);
      if(m.matches())
        codes.add(new Pair<>(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2))));
      else
        throw new RuntimeException("Error: illegal node code " + s);
    }
    TreeReaderFactory trf = new TRegexTreeReaderFactory();
    MemoryTreebank treebank = new MemoryTreebank(trf);
    treebank.loadPath(treeFile,null, true);
    for (Pair<Integer,Integer> code : codes) {
      Tree t = treebank.get(code.first()-1);
      t.getNodeNumber(code.second()).pennPrint();
    }
  }

  /**
   * Prints out all matches of a tree pattern on each tree in the path. Usage:
   *
   * {@code
   * java edu.stanford.nlp.trees.tregex.TregexPattern [[-TCwfosnu] [-filter] [-h <node-name>]]* pattern filepath
   * }
   *
   *

View on GitHub (pinned to 1b7edd19c4)