Grammar modulation: a way to import lexer rules after defining others first?

Let's say I have a simple grammar (lexer and parser) for parsing and evaluating simple mathematical expressions (as in several antlr examples), which also allow you to define simple variables (i.e. assign float values) and use these variables. For instance. You can do the following:

r = 2.5; PI = 3.14; PI * r * r; 

This is supposed to be used in more complex grammar. In fact, several different. The problem is that the lexer for the above contains basically recognizes each line as an identifier of the type of token, that is, the name of a potential variable, but more complex grammars may contain other keywords.

If i do

 lexer grammar ComplexLexer; import SimpleMathExprLexer; // ... IF : 'if'|'IF'; THEN : 'then'|'THEN'; // ... 

it’s not very useful that the ID already matches these keywords. Simply moving the import statement below these rules does not work. Is there any way around this, or am I on the wrong track when I look at a composition?

+4
source share
1 answer

ANTLR will simply give priority to the rule defined first. This means that if you have a lexer G grammar that imports G3 lexical grammar and after that G2 grammar:

 lexer grammar G; import G3, G2; ... 

or similarly:

 lexer grammar G; import G3; ... 


 lexer grammar G3; import G2; ... 

rules from G3 will take precedence over G2 , but rules from G will take precedence over G3 and G2 .

As with single lexer grammars, you define rules such as IDENTIFIER after rules that match keywords such as "if" , "then" , ...

+2
source

Source: https://habr.com/ru/post/1445136/


All Articles