Skip to content
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

[optimizer] don't merge A+B|B => A?+B for ChoiceRule #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Palatis
Copy link
Contributor

@Palatis Palatis commented Dec 3, 2024

we actually can't do this.

for A+B1|B2:

  • when A matches, tries to match B1.
    • when B1 matches, A+B1 matches, and A+B1|B2 matches.
    • when B1 fails, A+B1 fails, fallback to B2
      • when B2 matches, A+B1|B2 matches
      • when B2 fails, A+B1|B2 fails.
  • when A fails, try to match B2.
    • when B2 matches, A+B1|B2 matches
    • when B2 fails, A+B1|B2 fails.

however, for A?+B:

  • when A matches, tries to match B
    • when B matches, A?+B matches
    • when B fails, fail. (no fallback path to match B only)
  • when A fails, tries to match B
    • when B matches, A?+B matches
    • when B fails, fail.

the problem is for A?+B, when A matches, it wants to match a B, and success only when a valid B matches.
if A matches but B doesn't, A?+B fails.
this actually behaves different than A+B|B.

@Palatis Palatis changed the title [optimizer] don't merge "A+B|B => A?+B" for ChoiceRule [optimizer] don't merge A+B|B => A?+B for ChoiceRule Dec 3, 2024
@Palatis
Copy link
Contributor Author

Palatis commented Dec 3, 2024

some rules, for example CSharpGrammar.LambdaParameter is in this form.

public Rule LambdaParameter => Node(TypeExpr + Identifier | Identifier);

CSharpTests will fail if the code is re-written as

public Rule LambdaParameter => Node(TypeExpr.Optional() + Identifier);

for example:

public Rule LambdaParameter => Node(TypeExpr + Identifier | Identifier);
int a => "TypeExpr + Identifier" matches => passed
a => "TypeExpr + Identifier" fails, fallback to "Identifier" which matches => passed
public Rule LambdaParameter => Node(TypeExpr.Optional() + Identifier);
int a => "TypeExpr + Identifier" both match => passed
a => "TypeExpr" matches, but "Identifier" fails => failed

@Palatis Palatis force-pushed the bugfix/optimizer branch 3 times, most recently from 63db9cf to ae36d72 Compare December 7, 2024 13:47
we actually can't do this.

for `A+B1|B2`:
   - when `A` matches, tries to match `B1`.
      - when `B1` matches, `A+B1` matches, and `A+B1|B2` matches.
      - when `B1` fails, `A+B1` fails, fallback to `B2`
          - when `B2` matches, `A+B1|B2` matches
          - when `B2` fails, `A+B1|B2` fails.
   - when `A` fails, try to match B2.
      - when `B2` matches, `A+B1|B2` matches
      - when `B2` fails, `A+B1|B2` fails.

however, for `A?+B`:
   - when `A` matches, tries to match `B`
      - when `B` matches, `A?+B` matches
      - when `B` fails, fail. (no fallback path to match `B` only)
   - when `A` fails, tries to match `B`
      - when `B` matches, `A?+B` matches
      - when `B` fails, fail.
should be "(A|_) => A", we're optimizing an `ChoiceRule`
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.

1 participant