Skip to content

Commit cefbde4

Browse files
Copilothzhangxyz
andauthored
Add BNF package documentation and support packages section (#83)
* Initial plan * Add comprehensive BNF README and Support Packages section to main README Co-authored-by: hzhangxyz <[email protected]> * Fix BNF README to match actual parse/unparse behavior from tests Co-authored-by: hzhangxyz <[email protected]> * Simplify documentation per feedback: condense Support Packages section and remove API Reference, Examples, Testing, and Development sections Co-authored-by: hzhangxyz <[email protected]> * Fix some issues. * Fix some mistakes. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: hzhangxyz <[email protected]> Co-authored-by: Hao Zhang <[email protected]>
1 parent a8e31bd commit cefbde4

File tree

2 files changed

+194
-3
lines changed

2 files changed

+194
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ Example programs are provided in the `examples/` directory:
344344

345345
Each example demonstrates logical inference using propositional logic axioms.
346346

347+
## Support Packages
348+
349+
- **BNF Conversion Library** ([apyds-bnf](https://pypi.org/project/apyds-bnf/), [atsds-bnf](https://www.npmjs.com/package/atsds-bnf)): Bidirectional conversion between DS syntax formats. See [/bnf](/bnf) for details.
350+
347351
## Development
348352

349353
### Code Formatting

bnf/README.md

Lines changed: 190 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,193 @@
11
# BNF Support Package for DS
22

3-
This package provides bidirectional conversion between two syntax formats for the DS deductive system:
3+
A bidirectional conversion library for the DS deductive system, providing seamless translation between two syntax formats:
44

5-
- **Ds**: The lisp-like syntax currently used in DS
6-
- **Dsp**: A traditional readable syntax with infix operators
5+
- **Ds**: The S-expression (lisp-like) syntax used internally by DS
6+
- **Dsp**: A traditional, human-readable syntax with infix operators
7+
8+
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.
9+
10+
## Features
11+
12+
- **Bidirectional Conversion**: Convert between Ds and Dsp syntax formats
13+
- **Multi-Language Support**: Available for both Python and JavaScript/TypeScript
14+
- **Parse**: Convert from readable Dsp syntax to DS internal format
15+
- **Unparse**: Convert from DS internal format to readable Dsp syntax
16+
- **Comprehensive Operator Support**: Functions, subscripts, unary and binary operators
17+
- **ANTLR-Based**: Built on ANTLR 4.13.2 for robust parsing
18+
19+
## Installation
20+
21+
### Python (pip)
22+
23+
```bash
24+
pip install apyds-bnf
25+
```
26+
27+
Requires Python 3.10-3.14.
28+
29+
### JavaScript/TypeScript (npm)
30+
31+
```bash
32+
npm install atsds-bnf
33+
```
34+
35+
## Quick Start
36+
37+
### Python Example
38+
39+
```python
40+
from apyds_bnf import parse, unparse
41+
42+
# Parse: Convert from readable Dsp to DS format
43+
dsp_input = "a, b -> c"
44+
ds_output = parse(dsp_input)
45+
print(ds_output)
46+
# Output:
47+
# a
48+
# b
49+
# ----
50+
# c
51+
52+
# Unparse: Convert from DS format to readable Dsp
53+
ds_input = "a\nb\n----\nc"
54+
dsp_output = unparse(ds_input)
55+
print(dsp_output)
56+
# Output: a, b -> c
57+
```
58+
59+
### JavaScript Example
60+
61+
```javascript
62+
import { parse, unparse } from "atsds-bnf";
63+
64+
// Parse: Convert from readable Dsp to DS format
65+
const dsp_input = "a, b -> c";
66+
const ds_output = parse(dsp_input);
67+
console.log(ds_output);
68+
// Output:
69+
// a
70+
// b
71+
// ----
72+
// c
73+
74+
// Unparse: Convert from DS format to readable Dsp
75+
const ds_input = "a\nb\n----\nc";
76+
const dsp_output = unparse(ds_input);
77+
console.log(dsp_output);
78+
// Output: a, b -> c
79+
```
80+
81+
## Syntax Formats
82+
83+
### Ds Format (Internal)
84+
85+
The Ds format uses S-expressions (lisp-like syntax) for representing logical rules:
86+
87+
```
88+
premise1
89+
premise2
90+
----------
91+
conclusion
92+
```
93+
94+
For structured terms:
95+
- Functions: `(function f a b)`
96+
- Subscripts: `(subscript a i j)`
97+
- Binary operators: `(binary + a b)`
98+
- Unary operators: `(unary ~ a)`
99+
100+
### Dsp Format (Human-Readable)
101+
102+
The Dsp format uses traditional mathematical notation:
103+
104+
```
105+
premise1, premise2 -> conclusion
106+
```
107+
108+
For structured terms:
109+
- Functions: `f(a, b)`
110+
- Subscripts: `a[i, j]`
111+
- Binary operators: `(a + b)` (parenthesized)
112+
- Unary operators: `(~ a)` (parenthesized)
113+
114+
### Syntax Comparison Table
115+
116+
| Description | Dsp Format (parse input / unparse output) | Ds Format |
117+
|-------------|-------------------|-------------------|
118+
| Simple rule | `a, b -> c` | `a\nb\n----\nc` |
119+
| Axiom | `a` | `----\na` |
120+
| Function call | `f(a, b) -> c` | `(function f a b)\n----------------\nc` |
121+
| Subscript | `a[i, j] -> b` | `(subscript a i j)\n-----------------\nb` |
122+
| Binary operator | `(a + b) -> c` | `(binary + a b)\n--------------\nc` |
123+
| Unary operator | `~ a -> b` | `(unary ~ a)\n-----------\nb` |
124+
| 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)` |
125+
126+
## Building from Source
127+
128+
### Prerequisites
129+
130+
- Python 3.10-3.14 (for Python package)
131+
- Node.js (for JavaScript package)
132+
- Java (for ANTLR parser generation)
133+
- ANTLR 4.13.2
134+
135+
### Python Package
136+
137+
```bash
138+
cd bnf
139+
140+
# Install dependencies
141+
uv sync --extra dev
142+
143+
# Build package
144+
uv build
145+
146+
# Run tests
147+
pytest
148+
149+
# Run with coverage
150+
pytest --cov
151+
```
152+
153+
### JavaScript Package
154+
155+
```bash
156+
cd bnf
157+
158+
# Install dependencies
159+
npm install
160+
161+
# Build package
162+
npm run build
163+
164+
# Run tests
165+
npm test
166+
```
167+
168+
## Grammar Files
169+
170+
The conversion is based on ANTLR grammars:
171+
172+
- **Ds.g4**: Grammar for the Ds format (S-expression syntax)
173+
- **Dsp.g4**: Grammar for the Dsp format (human-readable syntax)
174+
175+
These grammars are used to generate parsers for both Python and JavaScript.
176+
177+
## License
178+
179+
This project is licensed under the GNU Affero General Public License v3.0 or later (AGPL-3.0-or-later).
180+
181+
## Repository
182+
183+
- **GitHub**: [USTC-KnowledgeComputingLab/ds](https://github.com/USTC-KnowledgeComputingLab/ds) (in `/bnf` directory)
184+
- **Python Package**: [apyds-bnf](https://pypi.org/project/apyds-bnf/)
185+
- **npm Package**: [atsds-bnf](https://www.npmjs.com/package/atsds-bnf)
186+
187+
## Author
188+
189+
Hao Zhang <[email protected]>
190+
191+
## Related
192+
193+
This package is a support library for the [DS (Deductive System)](https://github.com/USTC-KnowledgeComputingLab/ds) project. For the main DS library with C++ core and bindings, see the [main repository](https://github.com/USTC-KnowledgeComputingLab/ds).

0 commit comments

Comments
 (0)