Say I have a simple grammar (lexer and parser) for parsing & evaluating simple mathematical expressions (as in a few antlr examples out there) that also allows simple variable definitions (i.e. assigning float values) and use of these variables. E.g. the following can be handled:
r = 2.5;
PI = 3.14;
PI * r * r;
This is supposed to be used in a more complex grammar. In fact, several different ones. The problem is that the lexer for the above contains recognizes basically every string as token type ID, i.e. potential variable name, but the more complex grammars may contain other keywords.
If I do
lexer grammar ComplexLexer;
import SimpleMathExprLexer;
// ...
IF : 'if'|'IF';
THEN : 'then'|'THEN';
// ...
then it is not terribly helpful that ID already matches these keywords. Simply moving the import statement down below these rules does not work. Is there any way around this, or am I on the wrong path entirely when I look at composition?