[antlr-interest] more newbie help required

Martin Probst mail at martin-probst.com
Tue Feb 14 06:42:09 PST 2006


Hi,

your currently doing all the work in the lexer and don't use a parser at
all. If you have a completely flat file format, that's exactly what you
want to do though.

The 'normal' way of using a Lexer however is something like this:
Lexer myLexer = new Lexer(input);
Token tok = null;
while ((tok = myLexer.nextToken()).getType() != Token.EOF) {
  // do something with the token
}

You would then try to break up input into single parts (e.g. NAME,
SEASON, EPISODE, etc., that's why the Lexer is also called Tokenizer) so
you can easily handle the single parts from the outside. But that might
not work four you, at least not with your current way of parsing.

Martin

On Tue, 2006-02-14 at 13:21 +0100, karl wettin wrote:
> 14 feb 2006 kl. 10.13 skrev Martin Probst:
> 
> Hi, and thanks for your reply!
> 
> >>
> >> I just want a simple Visitor
> >
> > You can create member variables in your parser class and custom
> > constructors, methods etc., so you have all Java-power at your
> > fingertips.
> 
>   I hacked a visitor to my Lexer. If it's as easy it sounds to be I  
> guess I could just copy my code to the parser. A simple example,  
> perhaps based on my grammar, would be appreciated.
> 
> >> If the input is bad formatted  data and WELL_FORMATTED is
> >> before the BAD_FORMATTED in the lexer, it  will not match.
> >
> > Could you post the Lexer rules for WELL_FORMATTED and for  
> > BAD_FORMATTED?
> > I'd guess that your input text is matching both of them (e.g. your
> > language is non-deterministic - both rules can match the same  
> > input) or
> > at least the same prefix. Matching something "slightly wrong" can be
> > very difficult.
> 
> Attached is the generated vistor hack-lexer, the visitor and the  
> visitor coupled grammar and a test. I think it should run without any  
> problems.
> 
> 



More information about the antlr-interest mailing list