Skip to content

Commit 1929a8b

Browse files
Copilothzhangxyz
andauthored
Add BNF support package documentation (#97)
* Initial plan * Add BNF support package documentation in new Support Packages section Co-authored-by: hzhangxyz <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: hzhangxyz <[email protected]>
1 parent 8e7d82e commit 1929a8b

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

docs/support-packages/bnf.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# BNF Support Package
2+
3+
The BNF support package provides bidirectional conversion between DS's two syntax formats:
4+
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+
## Installation
11+
12+
### Python
13+
14+
```bash
15+
pip install apyds-bnf
16+
```
17+
18+
Requires Python 3.10-3.14.
19+
20+
### JavaScript/TypeScript
21+
22+
```bash
23+
npm install atsds-bnf
24+
```
25+
26+
## Usage
27+
28+
### Python Example
29+
30+
```python
31+
from apyds_bnf import parse, unparse
32+
33+
# Parse: Convert from readable Dsp to DS format
34+
dsp_input = "a, b -> c"
35+
ds_output = parse(dsp_input)
36+
print(ds_output)
37+
# Output:
38+
# a
39+
# b
40+
# ----
41+
# c
42+
43+
# Unparse: Convert from DS format to readable Dsp
44+
ds_input = "a\nb\n----\nc"
45+
dsp_output = unparse(ds_input)
46+
print(dsp_output)
47+
# Output: a, b -> c
48+
```
49+
50+
### JavaScript/TypeScript Example
51+
52+
```javascript
53+
import { parse, unparse } from "atsds-bnf";
54+
55+
// Parse: Convert from readable Dsp to DS format
56+
const dsp_input = "a, b -> c";
57+
const ds_output = parse(dsp_input);
58+
console.log(ds_output);
59+
// Output:
60+
// a
61+
// b
62+
// ----
63+
// c
64+
65+
// Unparse: Convert from DS format to readable Dsp
66+
const ds_input = "a\nb\n----\nc";
67+
const dsp_output = unparse(ds_input);
68+
console.log(dsp_output);
69+
// Output: a, b -> c
70+
```
71+
72+
## Syntax Formats
73+
74+
### Ds Format (Internal)
75+
76+
The Ds format uses S-expressions (lisp-like syntax) for representing logical rules:
77+
78+
```
79+
premise1
80+
premise2
81+
----------
82+
conclusion
83+
```
84+
85+
For structured terms:
86+
87+
- Functions: `(function f a b)`
88+
- Subscripts: `(subscript a i j)`
89+
- Binary operators: `(binary + a b)`
90+
- Unary operators: `(unary ~ a)`
91+
92+
### Dsp Format (Human-Readable)
93+
94+
The Dsp format uses traditional mathematical notation:
95+
96+
```
97+
premise1, premise2 -> conclusion
98+
```
99+
100+
For structured terms:
101+
102+
- Functions: `f(a, b)`
103+
- Subscripts: `a[i, j]`
104+
- Binary operators: `(a + b)` (parenthesized)
105+
- Unary operators: `(~ a)` (parenthesized)
106+
107+
### Syntax Comparison
108+
109+
| Description | Dsp Format | Ds Format |
110+
|-------------|------------|-----------|
111+
| Simple rule | `a, b -> c` | `a\nb\n----\nc` |
112+
| Axiom | `a` | `----\na` |
113+
| Function call | `f(a, b) -> c` | `(function f a b)\n----------------\nc` |
114+
| Subscript | `a[i, j] -> b` | `(subscript a i j)\n-----------------\nb` |
115+
| Binary operator | `(a + b) -> c` | `(binary + a b)\n--------------\nc` |
116+
| Unary operator | `~ a -> b` | `(unary ~ a)\n-----------\nb` |
117+
| 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)` |
118+
119+
## Package Information
120+
121+
- **Python Package**: [apyds-bnf](https://pypi.org/project/apyds-bnf/)
122+
- **npm Package**: [atsds-bnf](https://www.npmjs.com/package/atsds-bnf)
123+
- **Source Code**: [GitHub - bnf directory](https://github.com/USTC-KnowledgeComputingLab/ds/tree/main/bnf)

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ nav:
5959
- TypeScript API: api/typescript.md
6060
- Python API: api/python.md
6161
- C++ API: api/cpp.md
62+
- Support Packages:
63+
- BNF: support-packages/bnf.md
6264
- Examples:
6365
- Basis Examples: examples/basic.md
6466
- Sudoku: examples/sudoku.md

0 commit comments

Comments
 (0)