Commit 05104a9
authored
[GEODE-10508] Remedation of ANTLR nondeterminism warnings in OQL grammar (#7942)
* GEODE-10508: Fix ANTLR nondeterminism warnings in OQL grammar
This commit resolves four nondeterminism warnings generated by ANTLR during
the OQL grammar compilation process. These warnings indicated parser ambiguity
that could lead to unpredictable parsing behavior.
Problem Analysis:
-----------------
1. Lines 574 & 578 (projection rule):
The parser could not distinguish between aggregateExpr and expr alternatives
when encountering aggregate function keywords (sum, avg, min, max, count).
These keywords are valid both as:
- Aggregate function identifiers: sum(field)
- Regular identifiers in expressions: sum as a field name
Without lookahead, ANTLR could not deterministically choose which production
rule to apply, resulting in nondeterminism warnings.
2. Lines 961 & 979 (aggregateExpr rule):
Optional 'distinct' keyword created ambiguity in aggregate function parsing.
The parser could not decide whether to:
- Match the optional 'distinct' keyword, or
- Skip it and proceed directly to the expression
Both paths were valid, but ANTLR's default behavior doesn't specify
preference, causing nondeterminism.
Solution Implemented:
--------------------
1. Added syntactic predicates to projection rule (lines 574, 578):
Predicate: (('sum'|'avg'|'min'|'max'|'count') TOK_LPAREN)=>
This instructs the parser to look ahead and check if an aggregate keyword
is followed by a left parenthesis. If true, it chooses aggregateExpr;
otherwise, it chooses expr. This resolves the ambiguity by providing
explicit lookahead logic.
2. Added greedy option to aggregateExpr rule (lines 961, 979):
Option: options {greedy=true;}
This tells the parser to greedily match the 'distinct' keyword whenever
it appears, rather than being ambiguous about whether to match or skip.
The greedy option eliminates the nondeterminism by establishing clear
matching priority.
3. Updated test to use token constants (AbstractCompiledValueTestJUnitTest):
Changed: hardcoded value 89 -> OQLLexerTokenTypes.LITERAL_or
Rationale: Adding syntactic predicates changes ANTLR's token numbering
in the generated lexer (LITERAL_or shifted from 89 to 94). Using the
constant ensures test correctness regardless of future grammar changes.
This is a best practice for maintaining test stability.
Impact:
-------
- Zero nondeterminism warnings from ANTLR grammar generation
- No changes to OQL syntax or semantics (fully backward compatible)
- No runtime behavior changes (modifications only affect parser generation)
- All existing tests pass with updated token reference
- Improved parser determinism and maintainability
Technical Details:
-----------------
- Syntactic predicates (=>) are standard ANTLR 2 feature for lookahead
- Greedy option is standard ANTLR feature for optional subrule disambiguation
- Token constant usage follows best practices for generated code references
- Changes are compile-time only with no runtime performance impact
Files Modified:
--------------
- geode-core/src/main/antlr/org/apache/geode/cache/query/internal/parse/oql.g
- geode-core/src/test/java/org/apache/geode/cache/query/internal/AbstractCompiledValueTestJUnitTest.java
* GEODE-10508: Apply code formatting to test file
Fix line length formatting for improved readability.1 parent 98c2ec6 commit 05104a9
File tree
2 files changed
+21
-5
lines changed- geode-core/src
- main/antlr/org/apache/geode/cache/query/internal/parse
- test/java/org/apache/geode/cache/query/internal
2 files changed
+21
-5
lines changedLines changed: 13 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
571 | 571 | | |
572 | 572 | | |
573 | 573 | | |
574 | | - | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
575 | 579 | | |
576 | 580 | | |
577 | 581 | | |
578 | | - | |
| 582 | + | |
| 583 | + | |
579 | 584 | | |
580 | 585 | | |
581 | 586 | | |
| |||
958 | 963 | | |
959 | 964 | | |
960 | 965 | | |
961 | | - | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
962 | 970 | | |
963 | 971 | | |
964 | 972 | | |
| |||
975 | 983 | | |
976 | 984 | | |
977 | 985 | | |
| 986 | + | |
978 | 987 | | |
979 | | - | |
| 988 | + | |
980 | 989 | | |
981 | 990 | | |
982 | 991 | | |
| |||
Lines changed: 8 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
51 | 58 | | |
52 | 59 | | |
53 | 60 | | |
| |||
0 commit comments