Skip to main content

lambda-graphs: Multi-View Code Representation Tools

lambda-graphs aims to generate combined multi-code view graphs that can be used with various types of machine learning models (sequence models, graph neural networks, etc).

Overview

lambda-graphs is a CLI tool and Python library for generating code representations (AST, CFG, DFG) from source code. It supports C, C++, Java, and JavaScript, at both method-level and file-level granularity.

  • AST (Abstract Syntax Tree)
  • CFG (Control Flow Graph)
  • DFG (Data Flow Graph)
  • Combined graphs (any combination of the above)

lambda-graphs provides both a CLI and a Python API so you can generate graphs programmatically without shelling out. It is designed to be easily extendable to various programming languages. This is primarily because we use tree-sitter, a highly efficient incremental parser that supports over 40 languages.


Setup

1. Create a conda environment:

conda create -n lambda-graphs python=3.11

2. Activate the environment:

conda activate lambda-graphs

3. Install the package in development mode:

pip install -e .

4. Install GraphViz (Optional - for visualization):

GraphViz is only required if you want to generate DOT, PNG, or SVG output files.

Ubuntu/Debian:

sudo apt install graphviz

MacOS:

brew install graphviz

Windows: Download from graphviz.org


Generating Graphs

Using CLI

After setup, use the lambda-graphs command directly.

The attributes and options supported by the CLI are well documented and can be viewed by running:

lambda-graphs --help

Single File Analysis:

Generate a combined CFG and DFG graph for a C++ file:

lambda-graphs --lang "cpp" --code-file ./test.cpp --graphs "cfg,dfg"

Generate an AST for a C file with output in JSON format:

lambda-graphs --lang "c" --code-file ./example.c --graphs "ast" --output "json"

Folder Analysis (Multi-file Projects):

lambda-graphs can analyze entire projects by combining multiple source files from a folder:

lambda-graphs --lang "c" --code-folder ./project/src --graphs "cfg,dfg" --output "json"

This will:

  1. Recursively scan the folder for all .c and .h files
  2. Combine them into a single temporary file (preserving includes, declarations, definitions)
  3. Generate the requested codeviews from the combined source
  4. Output results to the output/ directory

You can customize the combined output file name:

lambda-graphs --lang "cpp" --code-folder ./mylib --combined-name "myproject" --graphs "ast,cfg"

Inline Code Analysis:

You can also analyze code snippets directly without a file:

lambda-graphs --lang "c" --code "int main() { int x = 5; return x; }" --graphs "ast,cfg"

Additional CLI Options:

Option Description
--output Output format: json, dot, svg, or all (dot generates PNG; svg generates SVG). Default: dot
--collapsed Collapse duplicate variable nodes into a single node in AST
--last-def Add last definition information to DFG edges (shows where variables were last defined)
--blacklisted Comma-separated list of AST node types to exclude from the graph

Flag-Codeview Compatibility:

Flag AST CFG DFG
--collapsed ✓ ✗ ✗
--blacklisted ✓ ✗ ✗
--last-def ✗ ✗ ✓
--last-use ✗ ✗ ✓

Examples:

# Generate all output formats (DOT, JSON, PNG)
lambda-graphs --lang "c" --code-file test.c --graphs "cfg" --output "all"

# Collapse duplicate variable nodes in DFG
lambda-graphs --lang "cpp" --code-file test.cpp --graphs "ast" --collapsed

# Add last definition tracking to DFG
lambda-graphs --lang "c" --code-file test.c --graphs "dfg" --last-def

# Exclude specific AST node types
lambda-graphs --lang "c" --code-file test.c --graphs "ast,cfg" --blacklisted "comment,string_literal"

Using the Python API

You can also use lambda-graphs as a library to get graph objects directly (no file I/O):

from lambda_graphs import generate

# -- Generate from a code string -------------------------------------------
result = generate(
    "cpp",
    code="int main() { int x = 5; return x; }",
    graphs=["ast", "cfg", "dfg"],
)

# Each graph is a networkx.MultiDiGraph
print(result.ast.nodes(data=True))
print(result.cfg.nodes(data=True))
print(result.dfg.nodes(data=True))
print(result.combined.nodes(data=True))

# Graph-level metadata (the "graph" key in JSON output)
print(result.combined.graph)  # {"language": "cpp", "views": ["ast", "cfg", "dfg"]}

# -- Generate from a file or folder ------------------------------------------
result = generate("cpp", code_file="./test.cpp", graphs=["cfg", "dfg"])
result = generate("c", code_folder="./project/src", graphs=["cfg", "dfg"])

# -- With extra options ------------------------------------------------------
result = generate(
    "cpp",
    code="...",
    graphs=["ast", "dfg"],
    collapsed=True,                              # collapse duplicate variable nodes
    last_def=True,                               # add last-def info to DFG edges
    blacklisted=["comment", "number_literal"],   # exclude AST node types
)

# -- Export to disk ----------------------------------------------------------
result.to_json("output.json")   # JSON
result.to_dot("output.dot")     # DOT
result.to_png("output.png")     # PNG image
result.to_svg("output.svg")     # SVG image

generate() returns a GraphsResult object with the following attributes:

Attribute Type Description
.ast nx.MultiDiGraph | None AST graph
.cfg nx.MultiDiGraph | None CFG graph
.dfg nx.MultiDiGraph | None DFG graph
.combined nx.MultiDiGraph Combined multi-view graph (always present)
.language str Source language

generate() parameters:

Parameter Type Required Description
language str ✓ Source language: "c" / "cpp" / "java" / "javascript"
code str pick one Source code as a string
code_file str|Path pick one Path to a source file
code_folder str|Path pick one Path to a source folder (auto-merges multi-file projects)
graphs list[str] Which graphs to generate, default ["ast", "cfg", "dfg"]
collapsed bool Collapse duplicate variable nodes, default False
last_def bool Add last-def annotation to DFG edges, default False
last_use bool Add last-use annotation to DFG edges, default False
blacklisted list[str] AST node types to exclude
combined_name str Custom name for merged file (code_folder mode only)

See docs/json-output-format.md for details on the JSON output format.


Limitations

While lambda-graphs provides method-level and file-level support, it's important to note the following limitations and known issues:

General Limitations

  • Syntax Errors in Code: To ensure accurate codeviews, the input code must be free of syntax errors. Code with syntax errors may not be correctly parsed and displayed in the generated codeviews. Note that the code does not need to be compilable, only syntactically valid.

C++ Specific Limitations

In addition to the general limitations, the tool has the following limitations specific to C++:

  • Limited Template Metaprogramming Support: Complex template metaprogramming patterns may not be fully captured in the generated codeviews.

  • Partial Preprocessor Directive Support: Preprocessor directives (e.g., #define, #ifdef) are parsed but not fully processed. Conditional compilation may not be accurately reflected in the codeviews.

  • Limited Support for Advanced C++ Features: Some advanced C++ features such as:

    • Complex inheritance hierarchies
    • Multiple inheritance with virtual functions
    • Template specializations
    • SFINAE patterns
    • Concepts (C++20)

    may not be fully represented in the generated codeviews.

Acknowledgments

This tool builds upon the tree-sitter parsing framework and is inspired by research on source code representation learning for AI-driven software engineering tasks.

Metadata

Release files for lambda-graphs 0.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lambda-graphs 0.5.3
File Size Uploaded Provenance Actions
lambda_graphs-0.5.3.tar.gz 147.0 kB Without provenance Details for lambda_graphs-0.5.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lambda-graphs 0.5.3
File Interpreter ABI Platform Actions
lambda_graphs-0.5.3-py3-none-any.whl Python 3 none any Details for lambda_graphs-0.5.3-py3-none-any.whl

Total release size: 308.5 kB

Release files / lambda_graphs-0.5.3.tar.gz

Download URL lambda_graphs-0.5.3.tar.gz
Size 147.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d85a00ce838fba775c2144df8529ae5098c6fe70f6b31e475d8e0a6a270265cc
BLAKE2b-256 checksum
How to use checksums
27eb841dc63ee112e134959afa9899bfba2ddccfdf1bf1cb024913c35cd304f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / lambda_graphs-0.5.3-py3-none-any.whl

Download URL lambda_graphs-0.5.3-py3-none-any.whl
Size 161.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ad810b47c67e9375ca50454c1240daec4d0ea11fad05fe323394997e88dd250
BLAKE2b-256 checksum
How to use checksums
8ab823adc6b144df7df091ba5d3d39fa29cd6a0ee97303a922bfb8b24f46a1a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Security for lambda-graphs 0.5.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page