diff --git a/bnf/Dsp.g4 b/bnf/Dsp.g4 index 1a14094..dac731c 100644 --- a/bnf/Dsp.g4 +++ b/bnf/Dsp.g4 @@ -6,7 +6,7 @@ rule_pool rule : term - | (term (',' term)*)? '->' term + | (term (',' term)*)? '=>' term ; term diff --git a/bnf/README.md b/bnf/README.md index 1bb8c23..ba93fa2 100644 --- a/bnf/README.md +++ b/bnf/README.md @@ -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: @@ -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 @@ -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: @@ -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 @@ -102,7 +102,7 @@ For structured terms: The Dsp format uses traditional mathematical notation: ``` -premise1, premise2 -> conclusion +premise1, premise2 => conclusion ``` For structured terms: @@ -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 diff --git a/bnf/apyds_bnf/__init__.py b/bnf/apyds_bnf/__init__.py index e1feae7..a50c703 100644 --- a/bnf/apyds_bnf/__init__.py +++ b/bnf/apyds_bnf/__init__.py @@ -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() diff --git a/bnf/atsds_bnf/index.mjs b/bnf/atsds_bnf/index.mjs index 9684e2d..9bc9075 100644 --- a/bnf/atsds_bnf/index.mjs +++ b/bnf/atsds_bnf/index.mjs @@ -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) { diff --git a/bnf/tests/test_parse_unparse.mjs b/bnf/tests/test_parse_unparse.mjs index 6c84ec3..063be3f 100644 --- a/bnf/tests/test_parse_unparse.mjs +++ b/bnf/tests/test_parse_unparse.mjs @@ -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); @@ -18,7 +18,7 @@ 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); @@ -26,7 +26,7 @@ test("parse_function", () => { 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); @@ -34,7 +34,7 @@ test("parse_subscript", () => { 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); @@ -42,7 +42,7 @@ test("parse_binary_operator", () => { 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); @@ -50,7 +50,7 @@ test("parse_unary_operator", () => { 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); @@ -58,7 +58,7 @@ test("parse_multiple_rules", () => { 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); @@ -68,7 +68,7 @@ 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); }); @@ -76,7 +76,7 @@ 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); }); @@ -84,7 +84,7 @@ 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); }); @@ -92,7 +92,7 @@ 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); }); @@ -100,7 +100,7 @@ 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); }); @@ -108,7 +108,7 @@ 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); }); @@ -116,7 +116,7 @@ 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); }); @@ -124,13 +124,13 @@ 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); @@ -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/); }); diff --git a/bnf/tests/test_parse_unparse.py b/bnf/tests/test_parse_unparse.py index 0022919..a2374b3 100644 --- a/bnf/tests/test_parse_unparse.py +++ b/bnf/tests/test_parse_unparse.py @@ -4,7 +4,7 @@ def test_parse_simple_rule() -> None: """Test parsing a simple rule with premises and conclusion""" - dsp_input = "a, b -> c" + dsp_input = "a, b => c" ds_output = parse(dsp_input) expected = "a\nb\n----\nc\n" assert ds_output == expected @@ -20,7 +20,7 @@ def test_parse_no_premises() -> None: def test_parse_function() -> None: """Test parsing a function call""" - dsp_input = "f(a, b) -> c" + dsp_input = "f(a, b) => c" ds_output = parse(dsp_input) expected = "(function f a b)\n----------------\nc\n" assert ds_output == expected @@ -28,7 +28,7 @@ def test_parse_function() -> None: def test_parse_subscript() -> None: """Test parsing subscript notation""" - dsp_input = "a[i, j] -> b" + dsp_input = "a[i, j] => b" ds_output = parse(dsp_input) expected = "(subscript a i j)\n-----------------\nb\n" assert ds_output == expected @@ -36,7 +36,7 @@ def test_parse_subscript() -> None: def test_parse_binary_operator() -> None: """Test parsing binary operators""" - dsp_input = "(a + b) -> c" + dsp_input = "(a + b) => c" ds_output = parse(dsp_input) expected = "(binary + a b)\n--------------\nc\n" assert ds_output == expected @@ -44,7 +44,7 @@ def test_parse_binary_operator() -> None: def test_parse_unary_operator() -> None: """Test parsing unary operators""" - dsp_input = "~ a -> b" + dsp_input = "~ a => b" ds_output = parse(dsp_input) expected = "(unary ~ a)\n-----------\nb\n" assert ds_output == expected @@ -52,7 +52,7 @@ def test_parse_unary_operator() -> None: def test_parse_multiple_rules() -> None: """Test parsing multiple rules""" - dsp_input = "a -> b\nc -> d" + dsp_input = "a => b\nc => d" ds_output = parse(dsp_input) expected = "a\n----\nb\n\n\nc\n----\nd\n" assert ds_output == expected @@ -60,7 +60,7 @@ def test_parse_multiple_rules() -> None: def test_parse_complex_expression() -> None: """Test parsing complex nested expressions""" - dsp_input = "(a + b) * c, d[i] -> f(g, h)" + dsp_input = "(a + b) * c, d[i] => f(g, h)" ds_output = parse(dsp_input) expected = "(binary * (binary + a b) c)\n(subscript d i)\n---------------------------\n(function f g h)\n" assert ds_output == expected @@ -70,7 +70,7 @@ def test_unparse_simple_rule() -> None: """Test unparsing a simple rule""" ds_input = "a\nb\n----\nc\n" dsp_output = unparse(ds_input) - expected = "a, b -> c" + expected = "a, b => c" assert dsp_output == expected @@ -78,7 +78,7 @@ def test_unparse_no_premises() -> None: """Test unparsing a rule with no premises""" ds_input = "----\na\n" dsp_output = unparse(ds_input) - expected = " -> a" + expected = " => a" assert dsp_output == expected @@ -86,7 +86,7 @@ def test_unparse_function() -> None: """Test unparsing a function""" ds_input = "(function f a b)\n----\nc\n" dsp_output = unparse(ds_input) - expected = "f(a, b) -> c" + expected = "f(a, b) => c" assert dsp_output == expected @@ -94,7 +94,7 @@ def test_unparse_subscript() -> None: """Test unparsing subscript notation""" ds_input = "(subscript a i j)\n----\nb\n" dsp_output = unparse(ds_input) - expected = "a[i, j] -> b" + expected = "a[i, j] => b" assert dsp_output == expected @@ -102,7 +102,7 @@ def test_unparse_binary_operator() -> None: """Test unparsing binary operators""" ds_input = "(binary + a b)\n----\nc\n" dsp_output = unparse(ds_input) - expected = "(a + b) -> c" + expected = "(a + b) => c" assert dsp_output == expected @@ -110,7 +110,7 @@ def test_unparse_unary_operator() -> None: """Test unparsing unary operators""" ds_input = "(unary ~ a)\n----\nb\n" dsp_output = unparse(ds_input) - expected = "(~ a) -> b" + expected = "(~ a) => b" assert dsp_output == expected @@ -118,7 +118,7 @@ def test_unparse_multiple_rules() -> None: """Test unparsing multiple rules""" ds_input = "a\n----\nb\n\n\nc\n----\nd\n" dsp_output = unparse(ds_input) - expected = "a -> b\nc -> d" + expected = "a => b\nc => d" assert dsp_output == expected @@ -126,13 +126,13 @@ def test_unparse_complex_expression() -> None: """Test unparsing complex nested expressions""" ds_input = "(binary * (binary + a b) c)\n(subscript d i)\n----\n(function f g h)\n" dsp_output = unparse(ds_input) - expected = "((a + b) * c), d[i] -> f(g, h)" + expected = "((a + b) * c), d[i] => f(g, h)" assert dsp_output == expected def test_roundtrip_parse_unparse() -> None: """Test that parse followed by unparse produces consistent results""" - dsp_original = "a, b -> c" + dsp_original = "a, b => c" ds_intermediate = parse(dsp_original) dsp_result = unparse(ds_intermediate) assert dsp_result == dsp_original @@ -148,14 +148,14 @@ def test_roundtrip_unparse_parse() -> None: def test_parse_error_missing_closing_parenthesis() -> None: """Test that parse throws error on missing closing parenthesis""" - dsp_input = "(a + b -> c" + dsp_input = "(a + b => c" with pytest.raises(Exception, match=r"line 1:7.*no viable alternative"): parse(dsp_input) def test_parse_error_bad_syntax() -> None: """Test that parse throws error on bad syntax""" - dsp_input = "a b c -> -> d" + dsp_input = "a b c => => d" with pytest.raises(Exception, match=r"line 1:2.*mismatched input"): parse(dsp_input) diff --git a/docs/support-packages/bnf.md b/docs/support-packages/bnf.md index 140b7dd..a54cf43 100644 --- a/docs/support-packages/bnf.md +++ b/docs/support-packages/bnf.md @@ -31,7 +31,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: @@ -44,7 +44,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/TypeScript Example @@ -53,7 +53,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: @@ -66,7 +66,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 @@ -94,7 +94,7 @@ For structured terms: The Dsp format uses traditional mathematical notation: ``` -premise1, premise2 -> conclusion +premise1, premise2 => conclusion ``` For structured terms: @@ -108,13 +108,13 @@ For structured terms: | Description | Dsp Format | 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` | ## Package Information