{"record":{"id":"038f3185f9fea273","repo":"stanfordnlp/CoreNLP","slug":"variable-linkedname-was-referenced-before-it-wa","errorCode":null,"errorMessage":"Variable ${linkedName} 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":281,"sourceCode":"        ;\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:{\n        jj_consume_token(21);\n        name = jj_consume_token(IDENTIFIER);\n        break;\n        }\n      default:\n        jj_la1[9] = jj_gen;\n        ;\n      }\nif (!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\");}","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/tregex/TregexParser.java#L263-L299","documentation":"When a tregex pattern links a node to a previously named node with ~${linkedName}, the linked variable must already have been declared earlier in the pattern. This ParseException is thrown when ~${linkedName} references a variable that is not yet in knownVariables.","triggerScenarios":"Compiling a pattern whose ~${linkedName} link appears before the =${linkedName} declaration it refers to (forward reference).","commonSituations":"Reordering clauses of a query during editing; generated patterns emitting the link clause before the naming clause; misunderstanding that links are directional (must reference earlier declarations).","solutions":["Move the =${linkedName} declaration before the ~${linkedName} usage in the pattern","Fix the variable name if it is a typo (references a never-declared variable)","Validate ordering in pattern-generation code before compiling"],"exampleFix":"// before\nString pattern = \"A ~${x} >> B=${x}\";\n// after\nString pattern = \"B=${x} A ~${x} >> B\"; // declare x before linking","handlingStrategy":"validation","validationCode":"// every ~${x} must be preceded by a =x declaration\nSet<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);\nif (r.find() && !declared.contains(r.group(1))) throw new IllegalArgumentException(\"link before declaration: \" + r.group(1));","typeGuard":null,"tryCatchPattern":"try { TregexPattern.compile(pattern); } catch (ParseException e) { /* forward-referenced link variable; reorder clauses */ }","preventionTips":["Declare variables (=x) before linking (~${x}) in pattern order","Emit naming clauses before link clauses in generated patterns","Check for typos between declaration and link names"],"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"}