Skip to content
ydahPublic

About

Deterministic structural analysis and feature diffs for parser grammars.

Topics

Resources

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

gramin

Deterministic structural analysis and feature diffs for parser grammars.

npm version Node.js version requirement CI status Monthly npm downloads MIT license

Documentation · Try the sandbox · Grammar Atlas · Changelog

gramin turns grammar files into counts, distributions, and rule-level vectors. Inspect a grammar, compare revisions, or bring the same measurements into pull requests, scripts, and agent workflows. Bundled frontends never execute grammar actions or predicates.

Quick start

Requires Node.js 22.12.0 or newer. Save this as grammar.y:

%token NUMBER
%%
expression : term | expression '+' term ;
term       : NUMBER | '(' expression ')' ;

Run without installing:

npx --yes @gramin/cli analyze grammar.y --format md

This sample has 2 rules, 4 alternatives, and 4 terminals. The report also includes recursion, reachability, diagnostics, and per-rule measurements.

For repeated use, install the CLI:

npm install --global @gramin/cli

Prefer a browser? The sandbox includes runnable samples and before/after comparisons. Grammar contents stay in your browser.

Supported grammars

Grammar Frontend ID
Yacc, Bison, Lrama yacc-family
BNF, EBNF bnf
ANTLR4 antlr4
Menhir menhir
Peggy, PEG.js peggy

Detection is automatic. Use --frontend <id> when it is ambiguous, or gramin detect grammar.y to inspect candidates. See the support matrix for supported syntax.

Compare and review

Compare two revisions of a grammar:

gramin diff before.y after.y --format md

Differences include aggregate metrics and named rule vectors. Comparisons check capabilities and measurement context; unavailable measurements retain their reasons. Use --baseline and --fail-on-regression to gate changes against a saved feature report. See comparison semantics.

Review pull requests automatically

Add .github/workflows/grammar.yml:

name: Grammar review
on: [pull_request]

permissions:
  contents: read
  pull-requests: write

jobs:
  grammar:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: ydah/gramin@v1
        with:
          files: grammar.y
          frontend: yacc-family

The Action measures the merge-base, updates one PR comment, and writes a job summary. See the Action guide for SARIF output, regression policies, fork permissions, and pinning to a reviewed release or commit.

Use the data

Export a feature document or measure several independent grammars:

gramin analyze grammar.y --format json
gramin analyze-many first.y second.y --format csv -o grammars.csv

Outputs include JSON, CSV, Markdown, SARIF, and bounded LLM digests. The data output guide covers CSV structure, unavailable values, and a Python standard-library import example.

For agent clients, the MCP server provides analyze, diff, ir, and digest over stdio. To support another grammar format, start with the frontend template and validate it with gramin frontend-test. See the external frontend protocol.

Use gramin --help for the complete command and option reference. Exit status 0 means success, 1 means a diagnostic threshold or regression gate was hit, 2 means a fatal input or frontend failure, and 3 means invalid command usage.

Measurement boundaries

Features have explicit definitions and applicability conditions. gramin measures structure; it does not generate parsers or prove language correctness.

Raw SLR analysis is opt-in with --analysis lr. It omits precedence resolution and cannot be compared with a parser generator's %expect values.

Parser nesting defaults to 256 levels and supports a maximum of 256. Use --max-nesting-depth to set a lower limit. Read the known limitations before interpreting a result.

Explore further

Development

The repository is a pnpm workspace. Use the pnpm version pinned in package.json:

pnpm install --frozen-lockfile
pnpm build
pnpm test

See CONTRIBUTING.md for the complete checks, corpus benchmarks, and metric proposal process.

License

MIT.

About

Deterministic structural analysis and feature diffs for parser grammars.

Topics

Resources

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages