0

Here's an excerpt from an ANTLR grammar I'm working with:

expression: // ... some other stuff ...
            (
                { switch_expression_enabled() }?=> switch_expression
              | { complex_expression_enabled() }? complex_expression
              | simple_expression
            )

The functions switch_expression_enabled() and complex_expression_enabled() check compiler flags to figure out whether the corresponding language features should be enabled. As you can see, the first alternative uses a gated predicate (which seems to be the correct one to use according to the documentation), while the second one uses a disambiguating predicate.

Judging from the descriptions in the official documentation as well as here and here, I'd expect the definition of the second alternative to be incorrect. However, it turns out that it works in exactly the same way: If complex_expression_enabled() returns false, then I get a syntax error if I use a complex_expression, even if the input is not ambiguous, so the term "disambiguating predicate" seems to be a bit misleading. The only difference I can see in the generated code is that in case of gated predicates, the condition is checked twice (before and after choosing alternative 1), while the "disambiguating" predicate is only checked after choosing alternative 2.

So my question is: Is there any practical difference between using gated and disambiguating predicates for disabling grammar based on compiler flags?

mb-lang
  • 73
  • 5
  • One of the reasons I'm asking instead of simply using the "more correct" gated predicate everywhere is that gated predicates seem to trigger error C1061 on MSVC more frequently. I'm using the C++ target and version 3.5.2, just in case that matters. – mb-lang Jul 30 '20 at 09:23
  • try this branch. I made several fixes in artlr3 C++ target, but those were never merged. https://github.com/ibre5041/antlr3 – ibre5041 Aug 06 '20 at 11:05
  • For Antlr3, you can check https://stackoverflow.com/questions/3056441/what-is-a-semantic-predicate-in-antlr Otherwise, move on to V4. – peter.cyc Aug 09 '20 at 08:06
  • @peter.cyc I had already linked that question in my post. It doesn't provide an answer. Moving to ANTLRv4 is not an option for us for various reasons. – mb-lang Aug 10 '20 at 06:11

0 Answers0