grammars-v4 icon indicating copy to clipboard operation
grammars-v4 copied to clipboard

[rexx] Fix ambiguity between "assignment" and "command_", and search for others.

Open RossPatterson opened this issue 3 years ago • 1 comments

In ANTLR discussion 3321, @kaby76 wrote:

But, even before this change, there is an ambiguity for input "binary_good1 = '1'b " in single_instruction, in choosing between assignment and command_ => expression => and_expression => comparison. The way expressions are written is an "anti-pattern" for Antlr in itself not even considering assignment. But, with both involved, it will cause adaptivePredict() to run down the entire derivation chain of command_ until the end of the assignment because it's trying to find a difference based on context.

This ambiguity was copied directly from the ANSI standard, and needs to be investigated. There may be other ambiguities that can be resolved by optimizing the grammar for ANTLR's use (and moving further away from a textual match to ANSI), and the entire grammar should be analyzed looking for them.

RossPatterson avatar Sep 14 '22 13:09 RossPatterson

The rules in question look like this:

single_instruction          :   assignment
                            |   keyword_instruction
                            |   command_
                            ;
assignment                  :   ( VAR_SYMBOL | SPECIAL_VAR ) EQ expression ;
command_                    :   expression ;

grun Rexx file_ --diagnostics does indeed indicate an ambiguity with the binary_good1 = '1'b input:

line 1:15 reportAttemptingFullContext d=11 (single_instruction), input='binary_good1 = '1'b'
line 1:19 reportAmbiguity d=11 (single_instruction): ambigAlts={1, 3}, input='binary_good1 = '1'b
'

Which makes sense - you can parse that either as assignment ("Set binarygood1 to '1'b") or as command_ -> expression -> and_expression -> comparison ("Execute the command composed of the value of the comparison of binarygood1 to '1'b"). But isn't ANTLR's processing of alternatives in the order they're written supposed to solve that ambuguity? single_instruction is firstly an assignment, and there the parse should stop - no need to consider command_. In fact, even without the input string, the grammar itself isn't ambiguous, knowing that ANTLR processes alternatives in order. No matter what the input, one token of look-ahead should identify the EQ and class it as an assignment, or not, and class it as a command_.

RossPatterson avatar Oct 19 '22 00:10 RossPatterson