Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Notice that, because the predicate appears after letter+, there is no way it gets hoisted into the prediction for rule stat. It turns out that syntax alone is enough to distinguish the alternatives because "return" it is always followed by an expression but not by '=' or ':'. The order what of the alternatives in stat does not matter; LL(*) correctly handles the decision.

...

It resolves it correctly, however, because the precedent priority is given to the first alternative. If we swap the order of the alternatives, though, "return" would match as an identifier. We could fix this with a semantic predicate, but it would be awkward in this scannerless mode. The predicate would have to march ahead to match an identifier and then test it against the keyword list. The validating predicate we have above in rule id checks the text after we've matched the identifier.

...

The usual ANTLR AST construction mechanism works too, but it's a little bit more work because the individual elements or are characters not tokens. Here I have defined a few imaginary tokens for use as AST nodes. For input:

...