You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
theHacker
changed the title
Odd behavior: Should an Alternation not be commutative?
Odd behavior: Alternation fails instead of trying second alternative
Aug 19, 2024
Hi. I am just exploring Ohm in the online editor.
When using this grammar:
I am confused to as why it makes a difference in which order I put the alternations.
With
Query = NotEmptyQuery | EmptyQuery
:EmptyQuery
NotEmptyQuery
However, with
Query = EmptyQuery | NotEmptyQuery
:EmptyQuery
EmptyQuery
andExpected end of input
.Why?! The reference says about the Alternation
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?
The text was updated successfully, but these errors were encountered: