{"record":{"id":"1548abf3b1c7003f","repo":"stanfordnlp/CoreNLP","slug":"error-parsing-semgrex-pattern","errorCode":null,"errorMessage":"Error parsing semgrex pattern ","messagePattern":"Error parsing semgrex pattern ","errorType":"exception","errorClass":"SemgrexParseException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/semgraph/semgrex/SemgrexPattern.java","lineNumber":408,"sourceCode":"\n  // compile method\n  // -------------------------------------------------------------\n\n  /**\n   * Creates a pattern from the given string.\n   *\n   * @param semgrex The pattern string\n   * @return A SemgrexPattern for the string.\n   */\n  public static SemgrexPattern compile(String semgrex, Env env) {\n    try {\n      SemgrexParser parser = new SemgrexParser(new StringReader(semgrex + '\\n'));\n      SemgrexPattern newPattern = parser.Root();\n      newPattern.setEnv(env);\n      newPattern.patternString = semgrex;\n      return newPattern;\n    } catch (ParseException | TokenMgrError ex) {\n      throw new SemgrexParseException(\"Error parsing semgrex pattern \" + semgrex, ex);\n    }\n  }\n\n  public static SemgrexPattern compile(String semgrex) {\n    return compile(semgrex, new Env());\n  }\n\n  public String pattern() {\n    return patternString;\n  }\n\n  /**\n   * Recursively sets the env variable to this pattern in this and in all its children\n   *\n   * @param env An Env\n   */\n  public void setEnv(Env env) {\n    this.env = env;","sourceCodeStart":390,"sourceCodeEnd":426,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/semgraph/semgrex/SemgrexPattern.java#L390-L426","documentation":"SemgrexPattern.compile() wraps any ParseException or TokenMgrError produced by SemgrexParser.Root() into a SemgrexParseException carrying the full offending pattern string and the underlying cause. It is the top-level wrapper for every syntax/lexical error in semgrex, including the deprecated-syntax and uniq-validation errors thrown inside Root().","triggerScenarios":"Calling SemgrexPattern.compile(semgrex[, env]) with any syntactically invalid pattern: unbalanced brackets/parens, illegal tokens, deprecated node conjugation ('< [foo bar]'), uniq of an unknown node, or characters the tokenizer rejects (TokenMgrError).","commonSituations":"User-supplied semgrex from config files or rule lists; patterns written for other regex dialects (expecting standard regex syntax); patterns migrated from old CoreNLP versions using now-illegal syntax; typo'd relation or attribute syntax.","solutions":["Read the cause (getCause()) — the inner ParseException/TokenMgrError pinpoints the offending token or rule (deprecated conjugation, bad uniq key, lexer error).","Validate the pattern against current semgrex syntax; rewrite deprecated constructs like '[foo bar] < zzz' into 'zzz > foo > bar' or ':' branches.","Catch SemgrexParseException at the boundary where user input is compiled and reject/report the pattern instead of letting it propagate.","Print patternString (set on the returned pattern normally) alongside the error for debugging rule files."],"exampleFix":"// before\nSemgrexPattern p = SemgrexPattern.compile(userPattern); // may throw\n// after\nSemgrexPattern p;\ntry {\n  p = SemgrexPattern.compile(userPattern);\n} catch (SemgrexParseException e) {\n  throw new IllegalArgumentException(\"Bad semgrex: \" + userPattern, e);\n}","handlingStrategy":"try-catch","validationCode":"// validate before compiling\nif (semgrex == null || semgrex.isBlank()) throw new IllegalArgumentException(\"Empty semgrex\");","typeGuard":null,"tryCatchPattern":"try {\n  return SemgrexPattern.compile(semgrex, env);\n} catch (SemgrexParseException e) {\n  throw new IllegalArgumentException(\"Bad semgrex pattern: \" + semgrex, e);\n}","preventionTips":["Always catch SemgrexParseException (it wraps both ParseException and TokenMgrError) at the compile boundary","Log getCause() — it identifies the exact token/rule that failed","Validate user-supplied patterns at load time, not at match time","Add a unit test compiling every shipped rule file"],"tags":["java","parser","semgrex","error-wrapping"],"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"}