{"record":{"id":"0fa88d054c7ccef1","repo":"stanfordnlp/CoreNLP","slug":"variable-name-was-referenced-before-it-was-decl","errorCode":null,"errorMessage":"Variable ${name} was referenced before it was declared","messagePattern":"Variable (.+?) was referenced before it was declared","errorType":"validation","errorClass":"ParseException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/tregex/TregexParser.java","lineNumber":298,"sourceCode":"if (!knownVariables.contains(linkedName.image)) {\n          {if (true) throw new ParseException(\"Variable \" + linkedName.image +\n                                   \" was referenced before it was declared\");}\n        }\n        if (name != null) {\n          if (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        }\n        link = true;\n      break;\n      }\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 +\n                                   \" was referenced before it was declared\");}\n        }\n      break;\n      }\n    default:\n      jj_la1[10] = jj_gen;\n      jj_consume_token(-1);\n      throw new ParseException();\n    }\nDescriptionPattern ret = new DescriptionPattern(r, negateDesc, desc != null ? desc.image : null, name != null ? name.image : null, cat, basicCatFunction, varGroups, link, linkedName != null ? linkedName.image : null);\n    {if (\"\" != null) return ret;}\n    throw new Error(\"Missing return statement in function\");\n}\n\n  final public TregexPattern ChildrenDisj() throws ParseException {TregexPattern child;\n  List<TregexPattern> children = new ArrayList<TregexPattern>();\n  // When we keep track of the known variables to assert that\n  // variables are not redefined, or that links are only set to known","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/tregex/TregexParser.java#L280-L316","documentation":"A tregex pattern can reference a previously declared variable with ${name} (back-reference). This ParseException is thrown when ${name} is used but that variable was never declared with =name earlier in the pattern.","triggerScenarios":"Compiling a pattern containing ${name} with no preceding =name declaration; typos in variable names so the reference does not match the declaration.","commonSituations":"Typos or case mismatches between =Foo and ${foo}; reordered patterns where the declaration was deleted; generated queries with inconsistent variable naming.","solutions":["Add the missing declaration =name before the reference","Fix the variable name so the reference matches an existing declaration","Validate variable usage/declaration consistency before compiling"],"exampleFix":"// before\nString pattern = \"A < B ${x}\";\n// after\nString pattern = \"A=${x} < B ${x}\";","handlingStrategy":"validation","validationCode":"Set<String> declared = new HashSet<>();\nMatcher d = Pattern.compile(\"=(\\\\w+)\").matcher(pattern);\nwhile (d.find()) declared.add(d.group(1));\nMatcher r = Pattern.compile(\"\\\\$\\\\{(\\\\w+)\\\\}\").matcher(pattern);\nwhile (r.find()) if (!declared.contains(r.group(1))) throw new IllegalArgumentException(\"undeclared: \" + r.group(1));","typeGuard":null,"tryCatchPattern":"try { TregexPattern.compile(pattern); } catch (ParseException e) { /* undeclared variable reference; add =name */ }","preventionTips":["Every ${x} reference must have a preceding =x declaration","Watch for case/typo mismatches between declaration and reference","Lint generated patterns for variable consistency"],"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"}