A couple of ideas.
Bart is correct that you can't use negation of a rule match in your grammar definition.
A coupe of thoughts to toss out: (I can't quite make enough of the detail of you grammar out to be able to put an example together).
As I see it, there are two possibilities for this being "invalid":
1 - if you encounter this construct, it's an error, period.
If this is the case then you can simply change the rule to:
bad_coda
: onset consonant;
coda
: consonant;
And then, in a Listener or Visitor, if you encounter a bad_coda
context, you can throw your own error message.
In this way you're using ANTLR as a "Recognizer" (actually the name of one of the superclasses of Parser for a reason), that "recognizes" this error situation.
Lesson: You're not limited to putting ONLY rules for what is valid in your grammar.
2 - If you encounter this sequence of tokens, you want to not recognize it as a coda, but perhaps there is another rule that would match (so you just need this rule to fail so that another might be attempted). In this case, the previous approach would not work, as bad_coda
would match.
In that case, you might get away with making onset
optional, and writing a semantic predicate that rejects the rule if the onset
is actually present.
coda:
: onset? { /* predicate that rejects if onset is present */} consonant
(NOTE: not having enough to work with here, I haven't tried this out, but it may be worth a shot.)