You are given it automatically if there is an expected list that can be calculated. You just need to implement your own displayRecognitionError and pick up the expected field in the exception, it will be a token for some exceptions, and bitmap for others and not available for others. The default method shows which case is when.

In the case of NVA though, the set of expected cannot be calculated. In your example below, I would expect that it might calculate this, but the problem is the lack of left factoring is probably interfering with the set calculation. Both your rules start with ID and ANTLR cannot select the rule to follow because of the invalid token so you get NVA. I suspect that if you did this:

statement
    : ID
            ( Min | Plus ) ID
    ;

You would get what you need.

Also, I suspect that you are asking the wrong question and that you may really want to know how to organize the grammar to deal with a language where the assignment operator is the same as the equivalence operator. You  can either just make all statements expressions, then distinguish statement vs _expression_ via a signal such as a parameter or scope, or more easily, create the minimum syntactic predicate to predict assignment, which even in complicated languages tends not to be too difficult:

statement
   : (assignPred)=>assignment
   | _expression_
   ;

assignPred
   : ID Assign
   ;

In other words, look to the construction of your grammar for better error identification, the closer you get to a fully left factored grammar, the better the recovery and error mechanisms can provide/cope with information.

Jim


Floor Goddijn wrote:

Hi,

 

I was wondering whether it is possible to get a list of expected (possible) tokens at a NoViableAltException.

Consider this small example grammar:

 

grammar NoViableAltGrammar;

 

statement

                :    statementA

                |   statementB

                ;

               

statementA

                :   ID Min ID Semi

                ;

               

statementB

                :   ID Plus ID Semi

                ;              

 

fragment

CHAR :  'a'..'z' ;

 

ID : CHAR+;

Semi :    ';'    ;

Plus:    '+'    ;

Min :    '-'    ;          

Assign:    '='    ; 

 

WS:        (' '|'\t'|'\n'|'\r')+ {$channel=HIDDEN;}    ;

 

 

Starting with “statement”  and  this input:

 monkey = banana

 

I get a NoViableAlt  at token ‘=’ .

In a user error message I would like to tell that  “a  ‘-‘ or ‘+’  was expected” .

The information is available in the generated parser code of the rule statement, but as it seems, I cannot access this information through the noViableAltException.

 

Best regards,

 

Floor

 

 

 

CONFIDENTIALITY: This e-mail and any attachments are confidential and may be privileged. If you are not a named recipient, please notify the sender immediately and do not disclose the contents to another person, use it for any purpose or store or copy the information in any medium.


List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---



List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to