Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bnf/Dsp.g4
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rule_pool

rule
: term
| (term (',' term)*)? '->' term
| (term (',' term)*)? '=>' term
;

term
Expand Down
22 changes: 11 additions & 11 deletions bnf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ npm install atsds-bnf
from apyds_bnf import parse, unparse

# Parse: Convert from readable Dsp to DS format
dsp_input = "a, b -> c"
dsp_input = "a, b => c"
ds_output = parse(dsp_input)
print(ds_output)
# Output:
Expand All @@ -53,7 +53,7 @@ print(ds_output)
ds_input = "a\nb\n----\nc\n"
dsp_output = unparse(ds_input)
print(dsp_output)
# Output: a, b -> c
# Output: a, b => c
```

### JavaScript Example
Expand All @@ -62,7 +62,7 @@ print(dsp_output)
import { parse, unparse } from "atsds-bnf";

// Parse: Convert from readable Dsp to DS format
const dsp_input = "a, b -> c";
const dsp_input = "a, b => c";
const ds_output = parse(dsp_input);
console.log(ds_output);
// Output:
Expand All @@ -75,7 +75,7 @@ console.log(ds_output);
const ds_input = "a\nb\n----\nc\n";
const dsp_output = unparse(ds_input);
console.log(dsp_output);
// Output: a, b -> c
// Output: a, b => c
```

## Syntax Formats
Expand All @@ -102,7 +102,7 @@ For structured terms:
The Dsp format uses traditional mathematical notation:

```
premise1, premise2 -> conclusion
premise1, premise2 => conclusion
```

For structured terms:
Expand All @@ -115,13 +115,13 @@ For structured terms:

| Description | Dsp Format (parse input / unparse output) | Ds Format |
|-------------|-------------------|-------------------|
| Simple rule | `a, b -> c` | `a\nb\n----\nc\n` |
| 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` |
| 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

Expand Down
2 changes: 1 addition & 1 deletion bnf/apyds_bnf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def visitRule_pool(self, ctx):
def visitRule(self, ctx):
result = [self.visit(t) for t in ctx.term()]
conclusion = result.pop()
return f"{', '.join(result)} -> {conclusion}"
return f"{', '.join(result)} => {conclusion}"

def visitSymbol(self, ctx):
return ctx.SYMBOL().getText()
Expand Down
2 changes: 1 addition & 1 deletion bnf/atsds_bnf/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class UnparseVisitor extends DsVisitor {
visitRule(ctx) {
const result = ctx.term().map((t) => this.visit(t));
const conclusion = result.pop();
return `${result.join(", ")} -> ${conclusion}`;
return `${result.join(", ")} => ${conclusion}`;
}

visitSymbol(ctx) {
Expand Down
36 changes: 18 additions & 18 deletions bnf/tests/test_parse_unparse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parse, unparse } from "../atsds_bnf/index.mjs";

test("parse_simple_rule", () => {
// Test parsing a simple rule with premises and conclusion
const dsp_input = "a, b -> c";
const dsp_input = "a, b => c";
const ds_output = parse(dsp_input);
const expected = "a\nb\n----\nc\n";
expect(ds_output).toBe(expected);
Expand All @@ -18,47 +18,47 @@ test("parse_no_premises", () => {

test("parse_function", () => {
// Test parsing a function call
const dsp_input = "f(a, b) -> c";
const dsp_input = "f(a, b) => c";
const ds_output = parse(dsp_input);
const expected = "(function f a b)\n----------------\nc\n";
expect(ds_output).toBe(expected);
});

test("parse_subscript", () => {
// Test parsing subscript notation
const dsp_input = "a[i, j] -> b";
const dsp_input = "a[i, j] => b";
const ds_output = parse(dsp_input);
const expected = "(subscript a i j)\n-----------------\nb\n";
expect(ds_output).toBe(expected);
});

test("parse_binary_operator", () => {
// Test parsing binary operators
const dsp_input = "(a + b) -> c";
const dsp_input = "(a + b) => c";
const ds_output = parse(dsp_input);
const expected = "(binary + a b)\n--------------\nc\n";
expect(ds_output).toBe(expected);
});

test("parse_unary_operator", () => {
// Test parsing unary operators
const dsp_input = "~ a -> b";
const dsp_input = "~ a => b";
const ds_output = parse(dsp_input);
const expected = "(unary ~ a)\n-----------\nb\n";
expect(ds_output).toBe(expected);
});

test("parse_multiple_rules", () => {
// Test parsing multiple rules
const dsp_input = "a -> b\nc -> d";
const dsp_input = "a => b\nc => d";
const ds_output = parse(dsp_input);
const expected = "a\n----\nb\n\n\nc\n----\nd\n";
expect(ds_output).toBe(expected);
});

test("parse_complex_expression", () => {
// Test parsing complex nested expressions
const dsp_input = "(a + b) * c, d[i] -> f(g, h)";
const dsp_input = "(a + b) * c, d[i] => f(g, h)";
const ds_output = parse(dsp_input);
const expected = "(binary * (binary + a b) c)\n(subscript d i)\n---------------------------\n(function f g h)\n";
expect(ds_output).toBe(expected);
Expand All @@ -68,69 +68,69 @@ test("unparse_simple_rule", () => {
// Test unparsing a simple rule
const ds_input = "a\nb\n----\nc\n";
const dsp_output = unparse(ds_input);
const expected = "a, b -> c";
const expected = "a, b => c";
expect(dsp_output).toBe(expected);
});

test("unparse_no_premises", () => {
// Test unparsing a rule with no premises
const ds_input = "----\na\n";
const dsp_output = unparse(ds_input);
const expected = " -> a";
const expected = " => a";
expect(dsp_output).toBe(expected);
});

test("unparse_function", () => {
// Test unparsing a function
const ds_input = "(function f a b)\n----\nc\n";
const dsp_output = unparse(ds_input);
const expected = "f(a, b) -> c";
const expected = "f(a, b) => c";
expect(dsp_output).toBe(expected);
});

test("unparse_subscript", () => {
// Test unparsing subscript notation
const ds_input = "(subscript a i j)\n----\nb\n";
const dsp_output = unparse(ds_input);
const expected = "a[i, j] -> b";
const expected = "a[i, j] => b";
expect(dsp_output).toBe(expected);
});

test("unparse_binary_operator", () => {
// Test unparsing binary operators
const ds_input = "(binary + a b)\n----\nc\n";
const dsp_output = unparse(ds_input);
const expected = "(a + b) -> c";
const expected = "(a + b) => c";
expect(dsp_output).toBe(expected);
});

test("unparse_unary_operator", () => {
// Test unparsing unary operators
const ds_input = "(unary ~ a)\n----\nb\n";
const dsp_output = unparse(ds_input);
const expected = "(~ a) -> b";
const expected = "(~ a) => b";
expect(dsp_output).toBe(expected);
});

test("unparse_multiple_rules", () => {
// Test unparsing multiple rules
const ds_input = "a\n----\nb\n\n\nc\n----\nd\n";
const dsp_output = unparse(ds_input);
const expected = "a -> b\nc -> d";
const expected = "a => b\nc => d";
expect(dsp_output).toBe(expected);
});

test("unparse_complex_expression", () => {
// Test unparsing complex nested expressions
const ds_input = "(binary * (binary + a b) c)\n(subscript d i)\n----\n(function f g h)\n";
const dsp_output = unparse(ds_input);
const expected = "((a + b) * c), d[i] -> f(g, h)";
const expected = "((a + b) * c), d[i] => f(g, h)";
expect(dsp_output).toBe(expected);
});

test("roundtrip_parse_unparse", () => {
// Test that parse followed by unparse produces consistent results
const dsp_original = "a, b -> c";
const dsp_original = "a, b => c";
const ds_intermediate = parse(dsp_original);
const dsp_result = unparse(ds_intermediate);
expect(dsp_result).toBe(dsp_original);
Expand All @@ -146,13 +146,13 @@ test("roundtrip_unparse_parse", () => {

test("parse_error_missing_closing_parenthesis", () => {
// Test that parse throws error on missing closing parenthesis
const dsp_input = "(a + b -> c";
const dsp_input = "(a + b => c";
expect(() => parse(dsp_input)).toThrow(/line 1:7 no viable alternative/);
});

test("parse_error_bad_syntax", () => {
// Test that parse throws error on bad syntax
const dsp_input = "a b c -> -> d";
const dsp_input = "a b c => => d";
expect(() => parse(dsp_input)).toThrow(/line 1:2 mismatched input/);
});

Expand Down
Loading