stanfordnlp/CoreNLP · error · ParseException

Variable has been declared twice, which makes no sense

Error message

Variable ${name} has been declared twice, which makes no sense

What it means

In a tregex pattern, a node can be named with =name, and separately a variable can be declared with ${name} in link annotations. This ParseException is thrown when the same variable name is declared twice in the pattern, which the parser rejects as nonsensical (the second declaration would overwrite or shadow the first).

Solutions

  1. Rename the second occurrence to a unique variable name
  2. Remove the redundant declaration if both refer to the same node
  3. Unify pattern-building code to track already-used variable names

Example fix

// before
String pattern = "A=${x} < B ${x} < C ${x}";
// after
String pattern = "A=${x} < B ${x} < C ${y}";
Defensive patterns

Strategy: validation

Validate before calling

Set<String> seen = new HashSet<>();
for (String v : declaredVars) { if (!seen.add(v)) throw new IllegalArgumentException("duplicate variable: " + v); }

Try / catch

try { TregexPattern.compile(pattern); } catch (ParseException e) { /* duplicate variable declaration; show pattern */ }

Prevention

When it happens

Trigger: Compiling a pattern where the same ${name} variable is declared in two Description nodes, e.g. two linked-node declarations both declaring the same variable name.

Common situations: Pattern generation code that reuses a variable-name template across clauses; copy-pasted query fragments each declaring the same variable; programmatic composition of patterns without unique naming.

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/ffdf69a290f57e59. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/trees/tregex/TregexParser.java:253

          ;
          break;
          }
        default:
          jj_la1[7] = jj_gen;
          break label_2;
        }
        jj_consume_token(19);
        groupNum = jj_consume_token(NUMBER);
        jj_consume_token(20);
        groupVar = jj_consume_token(IDENTIFIER);
varGroups.add(new Pair<Integer,String>(Integer.parseInt(groupNum.image),groupVar.image));
      }
      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
      case 21:{
        jj_consume_token(21);
        name = jj_consume_token(IDENTIFIER);
if (knownVariables.contains(name.image)) {
            {if (true) throw new ParseException("Variable " + name.image + " has been declared twice, which makes no sense");}
          } else {
            knownVariables.add(name.image);
          }
          if (underNegation)
            {if (true) throw new ParseException("No named tregex nodes allowed in the scope of negation.");}
        break;
        }
      default:
        jj_la1[8] = jj_gen;
        ;
      }
      break;
      }
    case 22:{
      jj_consume_token(22);
      linkedName = jj_consume_token(IDENTIFIER);
      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
      case 21:{

View on GitHub (pinned to 1b7edd19c4)