> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt
> Use this file to discover all available pages before exploring further.

# Using Parsers - Overview

> Learn how to use Tree-sitter parsers in your applications

## Introduction

This guide covers the fundamental concepts of using Tree-sitter, which is applicable across all programming languages. Although we'll explore some C-specific details that are valuable for direct C API usage or creating new language bindings, the core concepts remain the same.

Tree-sitter's parsing functionality is implemented through its C API, with all functions documented in the [tree\_sitter/api.h](https://github.com/tree-sitter/tree-sitter/blob/master/lib/include/tree_sitter/api.h) header file. If you're working in another language, you can use one of the official language bindings:

## Language Bindings

* **[Go](https://pkg.go.dev/github.com/tree-sitter/go-tree-sitter)** - Idiomatic Go API
* **[Java](https://tree-sitter.github.io/java-tree-sitter)** - Java bindings with full API support
* **[JavaScript (Node.js)](https://tree-sitter.github.io/node-tree-sitter)** - Native Node.js addon
* **[Kotlin](https://tree-sitter.github.io/kotlin-tree-sitter)** - Kotlin/JVM bindings
* **[Python](https://tree-sitter.github.io/py-tree-sitter)** - Python bindings with comprehensive API
* **[Rust](https://docs.rs/tree-sitter)** - Safe Rust API with zero-cost abstractions
* **[Zig](https://tree-sitter.github.io/zig-tree-sitter)** - Zig language bindings

Each binding provides idiomatic access to Tree-sitter's functionality in that language.

## The Basic Objects

There are four main types of objects involved when using Tree-sitter:

<CardGroup cols={2}>
  <Card title="TSLanguage" icon="book">
    An opaque object that defines how to parse a particular programming language. The code for each language is generated by Tree-sitter.
  </Card>

  <Card title="TSParser" icon="gears">
    A stateful object that can be assigned a language and used to produce syntax trees based on source code.
  </Card>

  <Card title="TSTree" icon="tree">
    Represents the syntax tree of an entire source code file. It contains nodes that indicate the structure of the source code.
  </Card>

  <Card title="TSNode" icon="node">
    Represents a single node in the syntax tree. It tracks its start and end positions in the source code, as well as its relation to other nodes.
  </Card>
</CardGroup>

## Building the Library

To build the library on a POSIX system, just run `make` in the Tree-sitter directory. This will create both static and dynamic libraries:

```bash theme={null}
cd tree-sitter
make
```

This creates:

* `libtree-sitter.a` - Static library
* `libtree-sitter.so` / `libtree-sitter.dylib` - Dynamic library

### Embedding in Your Project

Alternatively, you can incorporate the library in a larger project's build system by adding one source file:

**Source file:**

* `tree-sitter/lib/src/lib.c`

**Include directories:**

* `tree-sitter/lib/src`
* `tree-sitter/lib/include`

## Getting Started

Here's a minimal example of using Tree-sitter to parse JSON:

<CodeGroup>
  ```c C theme={null}
  #include <assert.h>
  #include <string.h>
  #include <stdio.h>
  #include <tree_sitter/api.h>

  const TSLanguage *tree_sitter_json(void);

  int main() {
    // Create a parser
    TSParser *parser = ts_parser_new();
    
    // Set the parser's language
    ts_parser_set_language(parser, tree_sitter_json());
    
    // Parse source code
    const char *source_code = "[1, null]";
    TSTree *tree = ts_parser_parse_string(
      parser,
      NULL,
      source_code,
      strlen(source_code)
    );
    
    // Get the root node
    TSNode root_node = ts_tree_root_node(tree);
    
    // Print the syntax tree
    char *string = ts_node_string(root_node);
    printf("Syntax tree: %s\n", string);
    
    // Free memory
    free(string);
    ts_tree_delete(tree);
    ts_parser_delete(parser);
    return 0;
  }
  ```

  ```rust Rust theme={null}
  use tree_sitter::{Parser, Language};

  extern "C" { fn tree_sitter_json() -> Language; }

  fn main() {
      // Create a parser
      let mut parser = Parser::new();
      
      // Set the parser's language
      let language = unsafe { tree_sitter_json() };
      parser.set_language(&language).unwrap();
      
      // Parse source code
      let source_code = "[1, null]";
      let tree = parser.parse(source_code, None).unwrap();
      
      // Get the root node
      let root_node = tree.root_node();
      
      // Print the syntax tree
      println!("Syntax tree: {}", root_node.to_sexp());
  }
  ```

  ```javascript JavaScript theme={null}
  const Parser = require('tree-sitter');
  const JavaScript = require('tree-sitter-javascript');

  const parser = new Parser();
  parser.setLanguage(JavaScript);

  const sourceCode = '[1, null]';
  const tree = parser.parse(sourceCode);

  console.log('Syntax tree:', tree.rootNode.toString());
  ```
</CodeGroup>

## What's Next?

<CardGroup cols={2}>
  <Card title="Basic Parsing" icon="play" href="/using-parsers/basic-parsing">
    Learn how to parse source code and work with syntax nodes
  </Card>

  <Card title="Advanced Parsing" icon="rocket" href="/using-parsers/advanced-parsing">
    Explore incremental parsing, editing, and multi-language documents
  </Card>

  <Card title="Walking Trees" icon="shoe-prints" href="/using-parsers/walking-trees">
    Use tree cursors for efficient tree traversal
  </Card>

  <Card title="Pattern Matching" icon="magnifying-glass" href="/using-parsers/pattern-matching">
    Query syntax trees with powerful pattern matching
  </Card>
</CardGroup>

<Note>
  Many languages are already available in the [Tree-sitter GitHub organization](https://github.com/tree-sitter) and the [Tree-sitter grammars organization](https://github.com/tree-sitter-grammars).
</Note>
