[antlr-interest] throwing exception with antlr

Ric Klaren ric.klaren at gmail.com
Fri Jun 10 07:18:34 PDT 2005


On 6/8/05, Ilinca Denisa FRECUS <lilincutzy at yahoo.com> wrote:
> I have the following problem when trying to make a
> lexer that recognizes only characters or digits: when
> it encounters something else, it exits of the code but
> doesn't say anything. I would like, for example, to
> print a message each time that this protocole is not
> matched. Here is my code. Do you have any ideas why it
> does't do what I want?
> Thank you very much.
> 
> Ilinca FRECUS
> 
> class SimpleLexer extends Lexer;
> 
> options {k=1; filter=false; }
> 
> ALPHA : ('a'..'z'|'A'..'Z')+
> {System.out.println("Found alpha: "+getText()); }
> ;
> 
> NUMERIC : ('0'..'9')+
> {System.out.println("Found numeric: "+getText()); }
> ;
> 
> 
> NEWLINE : ("\r\n")
>         {newline();
>         $setType(Token.SKIP);
>         };

The above rules look ok....

> EXIT: {System.exit(0); };

But this one looks very fishy. 
a) it recognizes nothing (e.g. it matches 'empty' input which is
always fishy for a parser using recursive descent like antlr).
b) it calls system exit. 

What do you want to accomplish with this rule?

> import java.io.*;
> 
> public class Main{
>         public static void main(String []args){
>                 SimpleLexer simpleLexer=new SimpleLexer(System.in);
>                 while(true)  {
>                         try{
>                            simpleLexer.nextToken();
>                         }catch (Exception e){System.out.println("not
> existant token");}
>                 }
>         }
> }

This looks ok too.

Cheers,

Ric


More information about the antlr-interest mailing list