Skip to main content
The most important command for grammar development. It reads the grammar and outputs C files that can be compiled into a shared or static library.
Aliases: gen, g

Grammar Path

The optional GRAMMAR_PATH argument should point to the structured grammar in one of two forms:
  • grammar.js - A JavaScript file (ESM or CJS). If omitted, defaults to ./grammar.js.
  • grammar.json - A structured representation created as a byproduct of generate. Can regenerate a missing parser.c without requiring a JavaScript runtime.

Conflict Detection

If there is an ambiguity or local ambiguity in your grammar, Tree-sitter will detect it during parser generation and exit with an Unresolved conflict error message. See Structuring Rules Well for information on handling conflicts.

Generated Files

src/parser.c

Implements the parser logic specified in the grammar.

src/tree_sitter/parser.h

Provides basic C definitions used in the generated parser.c file.

src/tree_sitter/alloc.h

Provides memory allocation macros that can be used in an external scanner.

src/tree_sitter/array.h

Provides array macros that can be used in an external scanner.

src/grammar.json

Contains a structured representation of the grammar. Can regenerate the parser without re-evaluating grammar.js.

src/node-types.json

Provides type information about individual syntax nodes. See Static Node Types.

Options

flag
Print the log of the parser generation process. Includes information about tokens in error recovery state, extracted keywords, state splits, and entry point state.
version
The ABI version to use for parser generation. Default is ABI 15, with ABI 14 being a supported target. Use latest to generate the newest supported version.Can also be set via TREE_SITTER_ABI_VERSION environment variable.
flag
Only generate grammar.json and node-types.json without generating the parser.
directory
The directory to place the generated parser in. Default is src/ in the current directory.
rule
Print the overview of states from the given rule. Useful for debugging and understanding the generated parser’s item sets.
  • Pass - to view state count numbers for all rules
  • Pass * to view the overview of states for every rule
flag
Report conflicts in a JSON format.
executable
default:"node"
Path to the JavaScript runtime executable to use when generating the parser.Can also be set via TREE_SITTER_JS_RUNTIME environment variable.Starting from version 0.26, you can pass native to use the experimental native QuickJS runtime bundled with the CLI. This avoids the Node.js dependency entirely.The native QuickJS runtime is compatible with ESM and CommonJS in strict mode. If your grammar depends on npm packages, run npm install before using the native runtime.
flag
Disable optimizations when generating the parser. Currently only affects the merging of compatible parse states.

Examples

Basic Generation

Generate with Debug Logging

Generate for Specific ABI

Use Native JavaScript Runtime

Generate Only Metadata