{"record":{"id":"c039a2a00ed1bc37","repo":"stanfordnlp/CoreNLP","slug":"cannot-add-new-variable-names-under-negation-nod","errorCode":null,"errorMessage":"Cannot add new variable names under negation.  Node '","messagePattern":"Cannot add new variable names under negation\\.  Node '","errorType":"exception","errorClass":"ParseException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/semgraph/semgrex/SemgrexParser.jj","lineNumber":359,"sourceCode":"\t( attr = <EMPTY> { attributes.setEmpty(true); } )\n}\n\nNodePattern Description(GraphRelation r) : {\n\tToken name = null;\n\tboolean link = false;\n        NodeAttributes attributes = new NodeAttributes();\n\tNodePattern pat;\n} {\n\n\t( \"{\" ( AddAttribute(attributes)\n                (\";\" AddAttribute(attributes))* )? \"}\"\n\n\t(( (\"=\" { link = true; }) name = identifier() )\n            {\n              String nodeName = name.image;\n              if (underNegation) {\n                if (!knownVariables.contains(nodeName)) {\n                  throw new ParseException(\"Cannot add new variable names under negation.  Node '\" + nodeName + \"' not seen before\");\n                }\n              } else {\n                knownVariables.add(nodeName);\n              }\n            }\n\t  )?\n\t)\n  \t{ pat = new NodePattern(r, underNodeNegation, attributes, link, name != null ? name.image : null);\n\t  return pat;\n\t}\n}\n\nToken identifier() : {\n  Token t ;\n}\n{\n  ( t = < UNIQ > | t = < IDENTIFIER > ) { return t; }\n}","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/semgraph/semgrex/SemgrexParser.jj#L341-L377","documentation":"Semgrex forbids binding a NEW node variable name inside a negated context, because a match whose negation holds cannot consistently record variable bindings. If '=' name appears under negation and the name was not already bound earlier in the pattern, the parser throws ParseException.","triggerScenarios":"Compiling patterns like '{} !(<nsubj =newName)' where '=newName' is first introduced inside the negated subexpression, i.e. the name is not in knownVariables.","commonSituations":"Writing negated relation patterns that also try to capture a node; converting positive patterns to negated ones by wrapping them in '!' without hoisting variable definitions.","solutions":["Bind the variable outside the negation first (e.g. match '=x' in a positive clause, then negate a different part)","Remove the '=name' binding from under the negation if the value is not needed","Restructure the pattern to express the intended semantics without new bindings in '!' scope"],"exampleFix":"// before\nSemgrexPattern p = SemgrexPattern.compile(\"{} !(>nsubj =subj)\");\n// after\nSemgrexPattern p = SemgrexPattern.compile(\"{} >nsubj =subj\"); // then filter subj in code, or bind earlier and negate a different element","handlingStrategy":"validation","validationCode":"// reject '=name' occurrences inside '!( ... )' unless the name appears earlier outside negation\nint neg = pattern.indexOf(\"!(\");\nif (neg >= 0) {\n  String before = pattern.substring(0, neg);\n  for (Matcher m = Pattern.compile(\"=(\\\\w+)\").matcher(pattern.substring(neg)); m.find(); )\n    if (!before.contains(\"=\" + m.group(1))) throw new IllegalArgumentException(\"new variable under negation: \" + m.group(1));\n}","typeGuard":null,"tryCatchPattern":"try {\n  p = SemgrexPattern.compile(pattern);\n} catch (ParseException e) {\n  if (e.getMessage().startsWith(\"Cannot add new variable names under negation\")) { /* hoist the binding out of the negation */ }\n  throw e;\n}","preventionTips":["Bind variables in positive context before using/negating them","Avoid '=' bindings inside '!( ... )' blocks entirely when possible","Review negated patterns during code review for capture semantics"],"tags":["semgrex","parser","negation"],"backgroundTag":"invalid-regex-pattern","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"}