Mats Ekberg wrote:
Nope, dont work. There is no errors while generating the lexer/parser,  
but no catch-code is generated.

  
See past posts on this but basically there is only really one thing that the lexer can throw exceptions for and that is mismatched character. There is also not much the lexer can do in terms of recovery but eat the character it was not expecting and start again with the next character. Hence, rather than trying to catch this exception and work out what to do, you need to code your lexer as follows:

1) Create a rule called somethign like BAD and make it the last rule of your lexer set:

BAD : .  { System.err.println("The character '" + $text + "' is not a legal character in this language or something."); } ;

2) The part that people leave out is to code your lexer rules so that they handle all the errors and typos that you can think of. Create paths in the lexer rule that catch errors, issue an error message that is then much more obvious to your user, and return the token with whatever text you managed to accumulate (record the error count and so on).

A classic example is unterminated strings, to give you an idea:

STRING
    :  '"'
                ( ~('"'|'\n'|'\r') ) *

                      (
                           '"'  // Properly terminated
                         |  { System.err.println("Unterminated string at line " + getLine() + " offset " + getCharPositionInLine()); }
   ;

For more ideas take a look at the example lexer grammar for parsing numbers, dots and ranges in the Wiki:

http://www.antlr.org/wiki/display/ANTLR3/Lexer+grammar+for+floating+point%2C+dot%2C+range%2C+time+specs


For instance you will see there that when parsing hex and octal numbers the lexer also looks for characters that are illegal, consumes them, but raises a malformed number error. This way, the parser still sees a HEX token and can parse the rest of the file looking for errors, but because an error was raised, the compiler it self does not try to generate code beyond performing semantic checks.

The general idea is to make as much sense as you can of the input and go as far as you can in the parsing process, while at the same time issuing the most accurate error messages. Other than pure formatting errors, move as much of the error trapping as you can to as late in the process as you can - this will give you semantic errors rather than syntax errors, syntax errors rather than 'Mismatched character' and allow recovery to provide as many sensible errors as possible in one parse. There are occasions when it is useful to have a recognizer stop immediately it finds an error, but I personally have never found that to be useful.

Hope that helps,

Jim


/mats

Skickat från min iPhone

18 jan 2009 kl. 01.30 skrev Terence Parr <[email protected]>:

  
try @lexer::rulecatch...not sure the lexers respond to this,  
however. They do not individually trap exceptions I think. Look at  
the nextToken() method. It should be the thing that handles errors  
emitted from calling mTokens() rule method.

Ter
On Jan 17, 2009, at 4:17 PM, Mats Ekberg wrote:

    
Hi!

To generate a custom catch clause in the parser one can write:

@rulecatch {
   ..code..
}

How do I accomplish the same thing in the lexer?

/mats

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

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