{"record":{"id":"9b093f82a4271561","repo":"stanfordnlp/CoreNLP","slug":"unknown-state-state","errorCode":null,"errorMessage":"Unknown state <state>","messagePattern":"Unknown state <state>","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/GenerateTrees.java","lineNumber":212,"sourceCode":"      Tree root = tf.newTreeNode(state, children);\n      return root;\n    }\n    \n    Counter<List<String>> nonTerminal = nonTerminals.get(state);\n    if (nonTerminal != null) {\n      // found a nonterminal production.  produce a list of\n      // recursive expansions, then attach them all to a node with\n      // the expected state\n      List<String> labels = Counters.sample(nonTerminal, random);\n      List<Tree> children = new ArrayList<>();\n      for (String childLabel : labels) {\n        children.add(produceTree(childLabel));\n      }\n      Tree root = tf.newTreeNode(state, children);\n      return root;\n    }\n    \n    throw new RuntimeException(\"Unknown state \" + state);\n  }\n\n  public static void help() {\n    System.out.println(\"Command line should be \");\n    System.out.println(\"  edu.stanford.nlp.trees.GenerateTrees <input> <output> <numtrees>\");\n  }\n  \n  public static void main(String[] args) {\n    if (args.length == 0 || args[0].equals(\"-h\")) {\n      help();\n      System.exit(0);\n    }\n    GenerateTrees grammar = new GenerateTrees();\n    grammar.readGrammar(args[0]);\n    int numTrees = Integer.valueOf(args[2]);\n    grammar.produceTrees(args[1], numTrees);\n  }\n}","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/GenerateTrees.java#L194-L230","documentation":"GenerateTrees builds synthetic parse trees recursively from a grammar, and produceTree(state) maps each grammar state to a tree node via its children. If the requested state string is not a known nonterminal in the loaded grammar, no branch matches and the method throws this RuntimeException instead of returning a Tree. It signals that the caller (or the input grammar file) referenced a state the generator does not know about.","triggerScenarios":"Calling produceTree(\"S\") (directly or via tree()/the CLI GenerateTrees <input> <output> <numtrees>) with a state name that is absent from the grammar data read from the input file; a grammar file whose nonterminal labels do not include the state being expanded.","commonSituations":"Hand-editing or truncating the grammar input file so some LHS nonterminals are missing; passing a main-class argument that points at the wrong file; typos in state names when scripting tree generation.","solutions":["Check the state string against the nonterminals actually present in the grammar input file and fix the spelling","Regenerate or supply the complete grammar file so every state reachable from the root exists","If calling produceTree programmatically, validate the state against the grammar's known states before invoking it","Run with the correct CLI arguments (input grammar, output, numtrees) via help() guidance"],"exampleFix":"// before\nTree t = GenerateTrees.produceTree(\"NP-OBJ\"); // typo: grammar defines NP-OBJ only\n// after\nTree t = GenerateTrees.produceTree(\"NP-OBJ\");","handlingStrategy":"validation","validationCode":"Set<String> knownStates = readNonterminalsFromGrammar(grammarFile);\nif (!knownStates.contains(state)) throw new IllegalArgumentException(\"State not in grammar: \" + state);","typeGuard":null,"tryCatchPattern":"try {\n    Tree t = GenerateTrees.produceTree(state);\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Unknown state\")) {\n        // fall back to default state or report bad grammar input\n    } else throw e;\n}","preventionTips":["Validate grammar input files against expected nonterminals at startup","Keep state name constants in one shared place instead of string literals","Diff grammar files after edits to catch removed nonterminals","Run the CLI help (GenerateTrees.help) to confirm argument order"],"tags":["java","parser","grammar","runtime-exception"],"backgroundTag":"invalid-enum-value","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"}