stanfordnlp/CoreNLP · error · ParseException

No named tregex nodes allowed in the scope of negation.

Error message

No named tregex nodes allowed in the scope of negation.

What it means

TregexParseException: tregex allows naming nodes with "=name" so matches can be captured, but names are not permitted inside the scope of a negation ("!"). The parser tracks an underNegation flag while parsing and rejects any named node encountered there, because capturing from a negated (absent) subtree is semantically meaningless.

Solutions

  1. Move the "=name" label out of the negated subtree onto a node outside the negation.
  2. Split the pattern: match positively with the label, then filter results in code, or use a separate negated conjunction.
  3. If the name is unused (only for grouping), drop it entirely.

Example fix

// before
TregexPattern p = TregexPattern.compile("NP !<< (S << VP=verb)");
// after
TregexPattern p = TregexPattern.compile("NP !<< S"); // capture outside negation instead
Defensive patterns

Strategy: validation

Validate before calling

boolean hasNameInsideNegation(String pattern) {
  int depth = 0; boolean inNeg = false;
  for (int i = 0; i < pattern.length(); i++) {
    char c = pattern.charAt(i);
    if (c == '!') inNeg = true;
    if (c == '(' && inNeg) depth++;
    if (c == ')' && inNeg && depth > 0 && --depth == 0) inNeg = false;
    if (c == '=' && inNeg) return true;
  }
  return false;
}

Try / catch

try { return TregexPattern.compile(pattern); } catch (TregexParseException e) { throw new IllegalArgumentException("Names are not allowed under negation: " + pattern, e); }

Prevention

When it happens

Trigger: Compiling a pattern like "A !<< (B << C=name)" where a "=name" label appears inside a negated subexpression; the check `if (underNegation)` at TregexParser.jj:169 fires on the name declaration inside the negation scope.

Common situations: Writing patterns like "no child matches X" and additionally trying to capture a node inside that negative context; translating regex-like intuition ('capture what did NOT match') into tregex.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/trees/tregex/TregexParser.jj:169

  boolean link = false;
  Token groupNum;
  Token groupVar;
  List<Pair<Integer,String>> varGroups = new ArrayList<Pair<Integer,String>>();
} {
// this is how we match tokens
// the return value of tokens is a Token object
  ( ( ( desc = <IDENTIFIER> | desc = <REGEX> | desc = <BLANK> | desc = <ROOTNODE> )
    ( ( "#" groupNum = <NUMBER> "%" groupVar = <IDENTIFIER> ) {
        varGroups.add(new Pair<Integer,String>(Integer.parseInt(groupNum.image),groupVar.image));
      } )*
      ( ( "=" name = <IDENTIFIER> )
        { if (knownVariables.contains(name.image)) {
            throw new ParseException("Variable " + name.image + " has been declared twice, which makes no sense");
          } else {
            knownVariables.add(name.image);
          }
          if (underNegation)
            throw new ParseException("No named tregex nodes allowed in the scope of negation.");
        } )? ) |
    ( ( "~" linkedName = <IDENTIFIER> ) ( "=" name = <IDENTIFIER> )? {
        if (!knownVariables.contains(linkedName.image)) {
          throw new ParseException("Variable " + linkedName.image +
                                   " was referenced before it was declared");
        }
        if (name != null) {
          if (knownVariables.contains(name.image)) {
            throw new ParseException("Variable " + name.image + " has been declared twice, which makes no sense");
          } else {
            knownVariables.add(name.image);
          }
        }
        link = true;
      } ) |
    ( ( "=" ) name = <IDENTIFIER> {
        if (!knownVariables.contains(name.image)) {
          throw new ParseException("Variable " + name.image +

View on GitHub (pinned to 1b7edd19c4)