stanfordnlp/CoreNLP · error · ParseException

Variable

Error message

Variable 

What it means

TregexParseException thrown while parsing a tregex node description. When a node description carries a "=name" assignment, the parser checks its table of already-declared variables and rejects a name that has been assigned to two different nodes in the same pattern, since back-references by name would be ambiguous. The message is a bare prefix "Variable " because the exception text continues with the variable name and the fixed suffix "has been declared twice, which makes no sense".

Solutions

  1. Rename one of the two "=label" assignments so every node name in the pattern is unique.
  2. Remove the redundant "=name" from the second node if you do not need to capture it.
  3. If the pattern is generated, add a check that emitted labels are unique before compiling the pattern.

Example fix

// before
TregexPattern p = TregexPattern.compile("NP < (NN=name) < (NN=name)");
// after
TregexPattern p = TregexPattern.compile("NP < (NN=name1) < (NN=name2)");
Defensive patterns

Strategy: validation

Validate before calling

boolean hasUniqueNames(String pattern) {
  java.util.regex.Matcher m = java.util.regex.Pattern.compile("=([A-Za-z_][A-Za-z0-9_]*)").matcher(pattern);
  java.util.Set<String> seen = new java.util.HashSet<>();
  while (m.find()) { if (!seen.add(m.group(1))) return false; }
  return true;
}

Try / catch

try { TregexPattern p = TregexPattern.compile(pattern); } catch (TregexParseException e) { reportDuplicateNames(pattern, e); }

Prevention

When it happens

Trigger: Parsing a tregex pattern where two node descriptions in the same relation tree are both named with the same "=name" label, e.g. "A=B < C=B"; the second declaration of B hits knownVariables.contains(name.image) at TregexParser.jj:164.

Common situations: Copy-pasting or editing a long tregex pattern and reusing a node label; programmatically generating patterns where a template variable collides with a hand-written label.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

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

DescriptionPattern Description(Relation r, boolean negateDesc, boolean cat) : {
  Token desc = null;
  Token name = null;
  Token linkedName = null;
  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);
          }
        }

View on GitHub (pinned to 1b7edd19c4)