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?
ANTLR will simply give precedence to the rule defined first. This means that if you have a lexer grammar
G, that imports lexer grammarsG3and after that grammarG2:or similarly:
the rules from
G3would get precedence overG2, but the rules fromGwould get precedence over bothG3andG2.Just as with single lexer grammars, the rules like
IDENTIFIERyou’d define after rules that match keywords like"if","then", …