stanfordnlp/CoreNLP · error · IllegalArgumentException

Error -- no such thing as zeroth leaf!

Error message

Error -- no such thing as zeroth leaf!

What it means

Tregex's AncestorOfIthLeaf relation (<<i) matches the ancestor of the i-th leaf of a tree, using 1-based indexing. Constructing it with i == 0 is meaningless (there is no zeroth leaf), so the constructor throws IllegalArgumentException to fail fast during relation construction.

Solutions

  1. Use 1-based indices: change <<0 to <<1 for the first leaf
  2. Clamp or start programmatic index generation at 1
  3. Catch IllegalArgumentException from TregexPattern.compile and report the offending pattern to the user

Example fix

// before
String pattern = "NP <<" + i; // i starts at 0
// after
String pattern = "NP <<" + (i + 1); // 1-based leaf index
Defensive patterns

Strategy: validation

Validate before calling

if (i < 1) throw new IllegalArgumentException("leaf index must be >= 1, got " + i);
TregexPattern.compile(pattern); // then catch IllegalArgumentException

Type guard

boolean validLeafIndex(int i) { return i >= 1; }

Try / catch

try { TregexPattern.compile(pattern); } catch (IllegalArgumentException e) { /* report bad <<i index */ }

Prevention

When it happens

Trigger: A tregex pattern contains the relation '<<0' (e.g. "A<<0"), causing the parser to construct AncestorOfIthLeaf(0), which throws.

Common situations: Users writing patterns by hand or generating them programmatically with 0-based loop counters forget that tregex leaf/child indices are 1-based; a loop starting at i=0 emits <<0.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/trees/tregex/Relation.java:1009

          }
        }
      };
    }
  };

  /**
   * Looks for the ith leaf of the current node
   */
  private static class AncestorOfIthLeaf extends Relation {

    private static final long serialVersionUID = -6495191354526L;

    private final int leafNum;

    AncestorOfIthLeaf(int i) {
      super("<<<" + String.valueOf(i));
      if (i == 0) {
        throw new IllegalArgumentException("Error -- no such thing as zeroth leaf!");
      }
      leafNum = i;
    }

    @Override
    Iterator<Tree> searchNodeIterator(final Tree t,
                                      final TregexMatcher matcher) {
      return new SearchNodeIterator() {
        @Override
        void initialize() {
          // this is a little lazy in terms of coding
          // would be a bit faster to actually recurse through the tree
          // this is unlikely to ever be a performance limitation, though
          List<Tree> leaves = t.getLeaves();
          if (leaves.size() >= Math.abs(leafNum)) {
            final int index;
            if (leafNum > 0) {
              index = leafNum - 1;

View on GitHub (pinned to 1b7edd19c4)