{"record":{"id":"51356fb08ac3c472","repo":"stanfordnlp/CoreNLP","slug":"could-not-parse-tregex","errorCode":null,"errorMessage":"Could not parse \" + tregex","messagePattern":"Could not parse \" \\+ tregex","errorType":"exception","errorClass":"TregexParseException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/tregex/TregexPatternCompiler.java","lineNumber":143,"sourceCode":"   * requiring clients to catch it, we wrap it in a ParseException.\n   * (The original Error's are thrown in TregexParserTokenManager.)\n   *\n   * @param tregex The pattern to parse\n   * @return A new TregexPattern object based on this string\n   * @throws TregexParseException If the expression is syntactically invalid\n   */\n  public TregexPattern compile(String tregex) {\n    for (Pair<String, String> macro : macros) {\n      tregex = tregex.replaceAll(macro.first(), macro.second());\n    }\n    TregexPattern pattern;\n    try {\n      TregexParser parser = new TregexParser(new StringReader(tregex + '\\n'),\n                                             basicCatFunction, headFinder);\n      pattern = parser.Root();\n      pattern.setKnownVariables(parser.knownVariables);\n    } catch (TokenMgrError tme) {\n      throw new TregexParseException(\"Could not parse \" + tregex, tme);\n    } catch (ParseException e) {\n      throw new TregexParseException(\"Could not parse \" + tregex, e);\n    } catch (IllegalStateException e) {\n      throw new TregexParseException(\"Could not parse \" + tregex, e);\n    }\n    pattern.setPatternString(tregex);\n    return pattern;\n  }\n\n}\n","sourceCodeStart":125,"sourceCodeEnd":154,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/tregex/TregexPatternCompiler.java#L125-L154","documentation":"TregexPatternCompiler.compile parses a tregex expression string into a TregexPattern. When the JavaCC parser throws TokenMgrError (lexical failure) the string is rethrown as a TregexParseException with the pattern text attached.","triggerScenarios":"Calling compile(tregex) with a pattern containing characters that are not valid tregex tokens — e.g. stray unescaped operators like '?', invalid escapes, or illegal characters the tokenizer cannot match before any grammar rule fires.","commonSituations":"Hand-written patterns with unescaped regex metacharacters; patterns built by string concatenation that accidentally include whitespace or control characters; patterns copied from other grammar syntaxes (SQL wildcards, glob).","solutions":["Look at the tregex string in the message and find the illegal character or bad escape at the reported token position.","Escape special characters (e.g. use \\\\. for literal dots, quote node names) so the tokenizer accepts them.","Test the pattern incrementally in the Tregex GUI or with a minimal compile() call to isolate the offending token.","If the pattern comes from a config file, check encoding issues (invisible/non-ASCII characters)."],"exampleFix":"// before\nString tregex = \"NP > (S . VP)\"; // '(' after '>' is a lexical/grammar error\n// after\nString tregex = \"NP > (S . VP)\".replace(\"(\", \"\\\\(\"); // or correct the grammar: NP > (S $+ VP)","handlingStrategy":"try-catch","validationCode":"static boolean isValidTregex(String tregex, Function<String, String> basicCat, HeadFinder hf) {\n  try { TregexPatternCompiler.defaultCompiler.compile(tregex, basicCat, hf); return true; }\n  catch (Exception | TokenMgrError e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n  TregexPattern p = TregexPatternCompiler.defaultCompiler.compile(tregex);\n} catch (TregexParseException e) {\n  log.warn(\"Invalid tregex pattern: \" + tregex, e);\n}","preventionTips":["Escape regex metacharacters and special characters in hand-written node names.","Lint patterns with a small compile() dry-run before committing them to config.","Watch out for invisible/non-ASCII characters when patterns come from files or user input."],"tags":["parser","lexer","regex","invalid-input"],"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-17T15:17:12.973Z"}