Alternative not to use 'Default Mode' in antlr.

47 views
Skip to first unread message

Cristiana Araújo

unread,
Jul 10, 2017, 4:58:18 AM7/10/17
to antlr-discussion, Pedro Rangel Henriques
I'm doing a grammar lexer in antlr, and I wonder if there is any alternative to not using 'DEFAULT MODE' in antlr syntax.
I then display a fragment of code, where I use the 'DEFAULT MODE'.
But I would like to use it in java syntax, in the last line of the java code (after 'phrase1 = phrase2 = "";').
Is it possible to use 'DEFAULT MODE' in java syntax?


mode sBOILER_11;
OutMMBOILER11        :  '\r\n\r\n'    -> mode(DEFAULT_MODE);

GetBOILER_11        : ~('.')   { frase3+=getText(); }
                    ;  
OutBOILER_11        :  ('.')   
                       { countMatch1++;         
                         System.out.println("Mais uma ocorrencia do Boilerplate tipo 1 -- OBLIGATION /fixada|inclui"); 
                         System.out.println("Entidade 1: " + frase1); System.out.println("Entidade 2: " + frase2 ); System.out.println("Entidade 3: " + frase3 );
                         frase1 = frase2 = "";
                       } 
                    ;     

Eric Vergnaud

unread,
Jul 10, 2017, 11:26:23 AM7/10/17
to antlr-discussion, pedrorange...@gmail.com
Not sure we understand what you're trying to achieve.
Can you elaborate a little?

Gerald Rosenberg

unread,
Jul 10, 2017, 5:36:06 PM7/10/17
to antlr-discussion, pedrorange...@gmail.com
On Monday, July 10, 2017 at 1:58:18 AM UTC-7, Cristiana Araújo wrote:
But I would like to use it in java syntax, in the last line of the java code (after 'phrase1 = phrase2 = "";').
Is it possible to use 'DEFAULT MODE' in java syntax?


Yes.

DEFAULT_MODE is defined as a Java constant in the Lexer:

    public static final int DEFAULT_MODE = 0;

However, what you may be looking for is the string name. If so, the generated lexer (extends Lexer) will contain the method `getModeNames()` that returns an array of strings giving the names of the modes in the order encountered in the grammar. And, every token in attributed with the mode index that yielded that token.
   

Eric Vergnaud

unread,
Jul 12, 2017, 10:59:47 AM7/12/17
to Pedro Rangel Henriques, antlr-discussion
Hi,
The use of actions in the grammar is discouraged in antlr4 in favor of listeners or visitors.
Maybe moving to a listener or visitor would avoid the problem you're facing?
Eric

Envoyé de mon iPhone

Le 11 juil. 2017 à 04:56, Pedro Rangel Henriques <pedrorange...@gmail.com> a écrit :

Dear Eric

thanks for your prompt reply

2017-07-10 16:26 GMT+01:00 Eric Vergnaud <eric.v...@wanadoo.fr>:
Not sure we understand what you're trying to achieve.
Can you elaborate a little?

yes, of course: in many moments of our processor, we need to use left context (what we implement with START CONDITIONS (%x) in the context of Flex) to analyze generic RE's (for the same RE we want to execute different semantic actions according to the context where the REs are found).

ANTLR "mode" is a suitable resource to solve those situations, 
but the problem is that the new ANTLR syntaxto enter a mode. 
                               ER -> mode(sBOILER_11);
is incompatible with the use of Java code to describe the semantic action, for instance, we can not write 
mode sBOILER_11;
OutBOILER_11        :  ('.')   
                                               { countMatch1++;         
                                                  ..........................
                                                 frase1 = frase2 = "";
                                               }  -> mode(DEFAULT_MODE);

the question is: is it possible to use mode controlling the mode flags (for instance sBOILER_11 or DEFAULT_MODE) inside Java code 
to control de flow of modes (getting out of a mode and opening another mode) mixed within Java statements ?

best regards
pedro
Reply all
Reply to author
Forward
0 new messages