When I inherit rules from another grammar, the generated output parser actually includes the combined rules of my grammar and the supergrammar. Why doesn't the generated parser class just inherit from the class generated from the supergrammar?

Terence Parr

In Java, when one class inherits from another, you override methods to change the behavior. The same is true for grammars except that changes a rule in a subgrammar can actually change the lookahead sets of rules in the supergrammar. I solve this problem by making grammar inheritance work more like an include, but you get the same results.

class Super extends Parser;

a : b | c;
b : B | BB ;
c : C ;

class Sub extends Super;

b : B ;

Rule Super.a predicts rule b upon B or BB, however, when rule a is inherited into grammar Sub, ANTLR needs to change its lookahead sets to have only B predicting rule b.