I'm using ANTLR 4 to parse an SQL subset and currently, I'm faced with a problem. I need a rule for predicate with the following structure:
predicate
:
expr relation expr
| between_clause
| predicate OR predicate
| predicate AND predicate
| '(' predicate ')'
;
The issue here is an expr
rule. There are different types of predicates where expr
should be different, but the whole aforementioned structure is preserved.
I would like to parametrize the predicate
rule in some way to automatically instantiate several rules for different predicate types (the rule above where specific expr
types are substituted).
Is it possible in ANTLR 4?
P.S. I see two alternative options:
- Copy-paste
predicate
rule and substitute differentexpr
rules manually - Have all possible alternatives in the
expr
rule and verify that only one type of them is used for a specific predicate in the target language code
But both of them are looking bad enough. The first case leads to a combinatorial explosion, and the second one leads to accepting a lot of incorrect predicates on the grammar level.