BNF parser and unparsers for DS - conversion between lisp-like and traditional syntax
Project description
BNF Support Package for DS
A bidirectional conversion library for the DS deductive system, providing seamless translation between two syntax formats:
- Ds: The S-expression (lisp-like) syntax used internally by DS
- Dsp: A traditional, human-readable syntax with infix operators
This package enables you to write logical rules in a more natural, mathematical notation and convert them to the DS internal format, or vice versa.
Features
- Bidirectional Conversion: Convert between Ds and Dsp syntax formats
- Multi-Language Support: Available for both Python and JavaScript/TypeScript
- Parse: Convert from readable Dsp syntax to DS internal format
- Unparse: Convert from DS internal format to readable Dsp syntax
- Comprehensive Operator Support: Functions, subscripts, unary and binary operators
- ANTLR-Based: Built on ANTLR 4.13.2 for robust parsing
Installation
Python (pip)
pip install apyds-bnf
Requires Python 3.11-3.14.
JavaScript/TypeScript (npm)
npm install atsds-bnf
Quick Start
Python Example
from apyds_bnf import parse, unparse
# Parse: Convert from readable Dsp to DS format
dsp_input = "a, b => c"
ds_output = parse(dsp_input)
print(ds_output)
# Output:
# a
# b
# ----
# c
# Unparse: Convert from DS format to readable Dsp
ds_input = "a\nb\n----\nc\n"
dsp_output = unparse(ds_input)
print(dsp_output)
# Output: a, b => c
JavaScript Example
import { parse, unparse } from "atsds-bnf";
// Parse: Convert from readable Dsp to DS format
const dsp_input = "a, b => c";
const ds_output = parse(dsp_input);
console.log(ds_output);
// Output:
// a
// b
// ----
// c
// Unparse: Convert from DS format to readable Dsp
const ds_input = "a\nb\n----\nc\n";
const dsp_output = unparse(ds_input);
console.log(dsp_output);
// Output: a, b => c
Syntax Formats
Ds Format (Internal)
The Ds format uses S-expressions (lisp-like syntax) for representing logical rules:
premise1
premise2
----------
conclusion
For structured terms:
- Functions:
(function f a b) - Subscripts:
(subscript a i j) - Binary operators:
(binary + a b) - Unary operators:
(unary ~ a)
Dsp Format (Human-Readable)
The Dsp format uses traditional mathematical notation:
premise1, premise2 => conclusion
For structured terms:
- Functions:
f(a, b) - Subscripts:
a[i, j] - Binary operators:
(a + b)(parenthesized) - Unary operators:
(~ a)(parenthesized)
Syntax Comparison Table
| Description | Dsp Format (parse input / unparse output) | Ds Format |
|---|---|---|
| Simple rule | a, b => c |
a\nb\n----\nc\n |
| Axiom | a |
----\na\n |
| Function call | f(a, b) => c |
(function f a b)\n----------------\nc\n |
| Subscript | a[i, j] => b |
(subscript a i j)\n-----------------\nb\n |
| Binary operator | (a + b) => c |
(binary + a b)\n--------------\nc\n |
| Unary operator | ~ a => b |
(unary ~ a)\n-----------\nb\n |
| Complex expression | ((a + b) * c), d[i] => f(g, h) |
(binary * (binary + a b) c)\n(subscript d i)\n---------------------------\n(function f g h)\n |
Building from Source
Prerequisites
- Python 3.11-3.14 (for Python package)
- Node.js (for JavaScript package)
- Java (for ANTLR parser generation)
- ANTLR 4.13.2
Python Package
cd bnf
# Install dependencies
uv sync --extra dev
# Build package
uv build
# Run tests
uv run pytest
# Run with coverage
uv run pytest --cov
JavaScript Package
cd bnf
# Install dependencies
npm install
# Build package
npm run build
# Run tests
npm test
Grammar Files
The conversion is based on ANTLR grammars:
- Ds.g4: Grammar for the Ds format (S-expression syntax)
- Dsp.g4: Grammar for the Dsp format (human-readable syntax)
These grammars are used to generate parsers for both Python and JavaScript.
License
This project is licensed under the GNU Affero General Public License v3.0 or later (AGPL-3.0-or-later).
Repository
- GitHub: USTC-KnowledgeComputingLab/ds (in
/bnfdirectory) - Python Package: apyds-bnf
- npm Package: atsds-bnf
Author
Hao Zhang hzhangxyz@outlook.com
Related
This package is a support library for the DS (Deductive System) project. For the main DS library with C++ core and bindings, see the main repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file apyds_bnf-0.0.12.tar.gz.
File metadata
- Download URL: apyds_bnf-0.0.12.tar.gz
- Upload date:
- Size: 87.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
413eb0c7aa92f46c3b6211ae28c9481be38944492209dd916948e0629ac819b9
|
|
| MD5 |
178ea3ffd04fc801d130eab0b2fac1f2
|
|
| BLAKE2b-256 |
b01282cd256755806539484dd588791b54548a185550c863f49f84f0df4fd940
|
Provenance
The following attestation bundles were made for apyds_bnf-0.0.12.tar.gz:
Publisher:
bnf-pytest.yml on USTC-KnowledgeComputingLab/ds
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
apyds_bnf-0.0.12.tar.gz -
Subject digest:
413eb0c7aa92f46c3b6211ae28c9481be38944492209dd916948e0629ac819b9 - Sigstore transparency entry: 779527489
- Sigstore integration time:
-
Permalink:
USTC-KnowledgeComputingLab/ds@909b88dc45986404548e9b55dc9bd84e8458b80f -
Branch / Tag:
refs/tags/v0.0.12 - Owner: https://github.com/USTC-KnowledgeComputingLab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
bnf-pytest.yml@909b88dc45986404548e9b55dc9bd84e8458b80f -
Trigger Event:
push
-
Statement type:
File details
Details for the file apyds_bnf-0.0.12-py3-none-any.whl.
File metadata
- Download URL: apyds_bnf-0.0.12-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e4b2d4d1fa73c1dff7095ba1b74de53575f803964df5573bee8feaac0525990
|
|
| MD5 |
4f1a3ae408d6b131cf4235d5875fcdd2
|
|
| BLAKE2b-256 |
1eb87f46d3dd87d87b6ce04223e85121fbca8efe51d45410183fbb274b3ea721
|
Provenance
The following attestation bundles were made for apyds_bnf-0.0.12-py3-none-any.whl:
Publisher:
bnf-pytest.yml on USTC-KnowledgeComputingLab/ds
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
apyds_bnf-0.0.12-py3-none-any.whl -
Subject digest:
4e4b2d4d1fa73c1dff7095ba1b74de53575f803964df5573bee8feaac0525990 - Sigstore transparency entry: 779527490
- Sigstore integration time:
-
Permalink:
USTC-KnowledgeComputingLab/ds@909b88dc45986404548e9b55dc9bd84e8458b80f -
Branch / Tag:
refs/tags/v0.0.12 - Owner: https://github.com/USTC-KnowledgeComputingLab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
bnf-pytest.yml@909b88dc45986404548e9b55dc9bd84e8458b80f -
Trigger Event:
push
-
Statement type: