Deterministic structural analysis and feature diffs for parser grammars.
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.
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 mdThis 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/cliPrefer a browser? The sandbox includes runnable samples and before/after comparisons. Grammar contents stay in your browser.
| 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 two revisions of a grammar:
gramin diff before.y after.y --format mdDifferences 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.
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-familyThe 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.
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.csvOutputs 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.
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.
- Grammar Atlas and the Ruby parse.y chronicle — downloadable JSON/CSV from pinned grammar sources.
- Metrics catalog, invariants, and comparability — what each measurement means.
- Grammar IR, feature schema, and design decisions — the contracts behind the pipeline.
- Compatibility policy and security policy — versioning, trust boundaries, and reporting issues.
The repository is a pnpm workspace. Use the pnpm version pinned in package.json:
pnpm install --frozen-lockfile
pnpm build
pnpm testSee CONTRIBUTING.md for the complete checks, corpus benchmarks, and metric proposal process.
MIT.