{"record":{"id":"ffdf69a290f57e59","repo":"stanfordnlp/CoreNLP","slug":"variable-name-has-been-declared-twice-which-ma","errorCode":null,"errorMessage":"Variable ${name} has been declared twice, which makes no sense","messagePattern":"Variable (.+?) has been declared twice, which makes no sense","errorType":"validation","errorClass":"ParseException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/tregex/TregexParser.java","lineNumber":253,"sourceCode":"          ;\n          break;\n          }\n        default:\n          jj_la1[7] = jj_gen;\n          break label_2;\n        }\n        jj_consume_token(19);\n        groupNum = jj_consume_token(NUMBER);\n        jj_consume_token(20);\n        groupVar = jj_consume_token(IDENTIFIER);\nvarGroups.add(new Pair<Integer,String>(Integer.parseInt(groupNum.image),groupVar.image));\n      }\n      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {\n      case 21:{\n        jj_consume_token(21);\n        name = jj_consume_token(IDENTIFIER);\nif (knownVariables.contains(name.image)) {\n            {if (true) throw new ParseException(\"Variable \" + name.image + \" has been declared twice, which makes no sense\");}\n          } else {\n            knownVariables.add(name.image);\n          }\n          if (underNegation)\n            {if (true) throw new ParseException(\"No named tregex nodes allowed in the scope of negation.\");}\n        break;\n        }\n      default:\n        jj_la1[8] = jj_gen;\n        ;\n      }\n      break;\n      }\n    case 22:{\n      jj_consume_token(22);\n      linkedName = jj_consume_token(IDENTIFIER);\n      switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {\n      case 21:{","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/tregex/TregexParser.java#L235-L271","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Rename the second occurrence to a unique variable name","Remove the redundant declaration if both refer to the same node","Unify pattern-building code to track already-used variable names"],"exampleFix":"// before\nString pattern = \"A=${x} < B ${x} < C ${x}\";\n// after\nString pattern = \"A=${x} < B ${x} < C ${y}\";","handlingStrategy":"validation","validationCode":"Set<String> seen = new HashSet<>();\nfor (String v : declaredVars) { if (!seen.add(v)) throw new IllegalArgumentException(\"duplicate variable: \" + v); }","typeGuard":null,"tryCatchPattern":"try { TregexPattern.compile(pattern); } catch (ParseException e) { /* duplicate variable declaration; show pattern */ }","preventionTips":["Use a unique-name generator when composing patterns programmatically","Keep a registry of declared variable names in query builders","Avoid copy-pasting query fragments that each declare variables"],"tags":["tregex","parse-error","variables"],"backgroundTag":"invalid-identifier-format","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}