Skip to content

Add -> operator to Dsp.g4 grammar - #126

Merged
hzhangxyz merged 3 commits into
mainfrom
copilot/add-operator-to-bnf-syntax
Dec 20, 2025
Merged

Add -> operator to Dsp.g4 grammar#126
hzhangxyz merged 3 commits into
mainfrom
copilot/add-operator-to-bnf-syntax

Conversation

Copilot AI commented Dec 20, 2025

Copy link
Copy Markdown
Contributor

Adds the -> (arrow) binary operator to the Dsp grammar with the same precedence as the . (dot) operator.

Changes

  • Modified line 16 in bnf/Dsp.g4 to | term ('.' | '->') term # binary, placing both . and -> operators on the same line with equal precedence
  • Positioned above the [] (subscript) operator per requirements

Example

from apyds_bnf import parse, unparse

# Parse arrow operator
parse("p -> q => r")  # '(binary -> p q)\n----\nr\n'

# Unparse arrow operator
unparse("(binary -> a b)\n----\nc\n")  # '(a -> b) => c'

# Both operators have same precedence
parse("p . x -> q")  # '=> ((p . x) -> q)'
parse("p -> q . y")  # '=> (p -> (q . y))'

[!WARNING]

Original prompt

This section details on the original issue you should resolve

<filter_complete></filter_complete>

<issue_title>bnf的语法中添加 -> 这个operator</issue_title>
<issue_description>在

term '.' term 

下面

term '[' term (',' term)* ']'  

上面

不需要添加readme或者docs</issue_description>

Comments on the Issue (you are @copilot in this section)

Original prompt

This section details on the original issue you should resolve

<issue_title>bnf的语法中添加 -> 这个operator</issue_title>
<issue_description>在

term '.' term 

下面

term '[' term (',' term)* ']'  

上面

不需要添加readme或者docs</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@hzhangxyz

Copy link
Copy Markdown
Member

@copilot 搞错了, 应该和 . 在同一行。

Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add -> operator to BNF grammar Add -> operator to Dsp.g4 grammar Dec 20, 2025
Copilot AI requested a review from hzhangxyz December 20, 2025 04:05
Co-authored-by: hzhangxyz <11623447+hzhangxyz@users.noreply.github.com>

Copilot AI commented Dec 20, 2025

Copy link
Copy Markdown
Contributor Author

@copilot 搞错了, 应该和 . 在同一行。

Fixed in bb0fd5d. Changed from separate lines to | term ('.' | '->') term # binary so both operators are on the same line with the same precedence.

@hzhangxyz
hzhangxyz marked this pull request as ready for review December 20, 2025 04:11
Copilot AI review requested due to automatic review settings December 20, 2025 04:11
@hzhangxyz
hzhangxyz merged commit b58fb66 into main Dec 20, 2025
77 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds the -> (arrow) binary operator to the Dsp grammar file with the same precedence as the . (dot) operator by modifying the grammar rule to accept either operator at the same precedence level.

  • Modified line 16 in the grammar to support both . and -> operators using ANTLR4's alternation syntax
  • Positioned the new operator above the subscript operator as required

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread bnf/Dsp.g4
| '(' term ')' # parentheses
| term '::' term # binary
| term '.' term # binary
| term ('.' | '->') term # binary

Copilot AI Dec 20, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples in the PR description suggest that p -> q . y should parse as p -> (q . y) (right-associative), but the grammar implementation makes both . and -> left-associative by default in ANTLR4. This means p -> q . y will actually parse as (p -> q) . y. If right-associativity is intended for these operators, they would need separate grammar rules or explicit associativity directives. Please verify the expected associativity behavior and update the PR description examples if needed.

Suggested change
| term ('.' | '->') term # binary
| <assoc=right> term ('.' | '->') term # binary

Copilot uses AI. Check for mistakes.
Comment thread bnf/Dsp.g4
| '(' term ')' # parentheses
| term '::' term # binary
| term '.' term # binary
| term ('.' | '->') term # binary

Copilot AI Dec 20, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new -> operator lacks test coverage. Given that comprehensive tests exist for other operators (binary, unary, subscript, function), tests should be added for the -> operator to verify parsing and unparsing behavior, especially its precedence relationship with . operator. Consider adding tests similar to test_parse_binary_operator and test_unparse_binary_operator that specifically test the -> operator.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bnf的语法中添加 -> 这个operator

3 participants