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

Odd behavior: Alternation fails instead of trying second alternative #489

Open
theHacker opened this issue Aug 19, 2024 · 0 comments
Open

Comments

@theHacker
Copy link

Hi. I am just exploring Ohm in the online editor.

When using this grammar:

Demo {

  // does not work as expected
  Query = EmptyQuery | NotEmptyQuery
  
  // works as expected
  // Query = NotEmptyQuery | EmptyQuery

  EmptyQuery = ""
  NotEmptyQuery = any+
}

I am confused to as why it makes a difference in which order I put the alternations.

With Query = NotEmptyQuery | EmptyQuery:

  • the empty string will match the EmptyQuery
  • any non-empty string will match the NotEmptyQuery

However, with Query = EmptyQuery | NotEmptyQuery :

  • the empty string will match as EmptyQuery
  • any non-empty string will fail as EmptyQuery and Expected end of input.

Why?! The reference says about the Alternation

Matches the expression expr1, and if that does not succeed, matches the expression expr2.

Should the parser not try the second alternative, i.e. NotEmptyQuery?

(When I alter the rule to EmptyQuery = "" end, it works as expected).

Is this a bug? Or am I misunderstanding something here?

@theHacker theHacker changed the title Odd behavior: Should an Alternation not be commutative? Odd behavior: Alternation fails instead of trying second alternative Aug 19, 2024
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

No branches or pull requests

1 participant