|
| 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) |
0 commit comments