-
Notifications
You must be signed in to change notification settings - Fork 0
Add BNF support package documentation #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+125
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # BNF Support Package | ||
|
|
||
| The BNF support package provides bidirectional conversion between DS's 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. | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Python | ||
|
|
||
| ```bash | ||
| pip install apyds-bnf | ||
| ``` | ||
|
|
||
| Requires Python 3.10-3.14. | ||
|
|
||
| ### JavaScript/TypeScript | ||
|
|
||
| ```bash | ||
| npm install atsds-bnf | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Python Example | ||
|
|
||
| ```python | ||
| 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" | ||
| dsp_output = unparse(ds_input) | ||
| print(dsp_output) | ||
| # Output: a, b -> c | ||
| ``` | ||
|
|
||
| ### JavaScript/TypeScript Example | ||
|
|
||
| ```javascript | ||
| 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"; | ||
| 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 | ||
|
|
||
| | Description | Dsp Format | Ds Format | | ||
| |-------------|------------|-----------| | ||
| | Simple rule | `a, b -> c` | `a\nb\n----\nc` | | ||
| | Axiom | `a` | `----\na` | | ||
| | Function call | `f(a, b) -> c` | `(function f a b)\n----------------\nc` | | ||
| | Subscript | `a[i, j] -> b` | `(subscript a i j)\n-----------------\nb` | | ||
| | Binary operator | `(a + b) -> c` | `(binary + a b)\n--------------\nc` | | ||
| | Unary operator | `~ a -> b` | `(unary ~ a)\n-----------\nb` | | ||
| | 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)` | | ||
|
|
||
| ## Package Information | ||
|
|
||
| - **Python Package**: [apyds-bnf](https://pypi.org/project/apyds-bnf/) | ||
| - **npm Package**: [atsds-bnf](https://www.npmjs.com/package/atsds-bnf) | ||
| - **Source Code**: [GitHub - bnf directory](https://github.com/USTC-KnowledgeComputingLab/ds/tree/main/bnf) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation for unary operators is inconsistent. The description states unary operators are
(~ a)(parenthesized), but the syntax comparison table on line 116 shows~ a -> bwithout parentheses. According to the test files, when parsing (Dsp → Ds), unary operators don't require parentheses (~ a -> b), but when unparsing (Ds → Dsp), the output includes parentheses ((~ a) -> b). Consider clarifying this by noting that parentheses are optional for input but included in output, or showing both forms in the table.