dcsimg
How can I write a catch all error rule in my lexer?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   a_b
Posted On:   Friday, August 5, 2005 03:55 AM

I'm generating an ANTLR lexer/parser to use within an Eclipse plugin. In Eclipse, all syntax errors (tokens that could not be matched with a parser rule or chars that are not part of a valid token) are underlined with a red sguiggly line. Therefore it's important that the lexer and parser both resume their operation after they run into an error, so they can find the next error too etc. I've managed to implement this partly but some work remain. Currently the parser seems to report errors and then continue but when the lexer finds an obscure char it's stumped and does not continue. Instead I would like it to report an error on that obscure char, then ignore it and see if it can get a new token. After some help from the ANTLR   More>>

I'm generating an ANTLR lexer/parser to use within an Eclipse plugin. In Eclipse, all syntax errors (tokens that could not be matched with a parser rule or chars that are not part of a valid token) are underlined with a red sguiggly line.

Therefore it's important that the lexer and parser both resume their operation after they run into an error, so they can find the next error too etc. I've managed to implement this partly but some work remain. Currently the parser seems to report errors and then continue but when the lexer finds an obscure char it's stumped and does not continue. Instead I would like it to report an error on that obscure char, then ignore it and see if it can get a new token.

After some help from the ANTLR mailing list I was able to create the following lexer rule. For the particular obscure char "¤" this rule will get the job done. Now, I would like to generalize this rule into something that will catch anything that the other lexer rules don't catch, report and error and then ignore whatever caused the error and resume lexing. See my lexer rule below:

ERROR: "¤"

{

   errorHandler.handleError(

      new NoViableAltForCharException(

         LA(0), "", getLine(), getColumn() - 1

      )

   );

   $setType(Token.SKIP);

};




Thanks for making ANTLR open source. It's a great tool!

Sincerly,
Martin

   <<Less
About | Sitemap | Contact