[antlr-interest] observation on example in "An Introduction To ANTLR"

Paul Tasillo ptasillo at us.ibm.com
Mon Jul 17 13:49:09 PDT 2006


I was following the examples in Terrance Parr's document "An Introduction 
To ANTLR" (http://www.cs.usfca.edu/~parrt/course/652/lectures/antlr.html) 
and couldn't get the tool to recognize end of line characters such that 
the expr() method returned. After reading some of Ashley Mill's ANTLR 
documents, I found the following change the seemed to solve the problem:

The original lexer has:

class ExprLexer extends Lexer;

options {
    k=2; // needed for newline junk
    charVocabulary='\u0000'..'\u007F'; // allow ascii
}

LPAREN: '(' ;
RPAREN: ')' ;
PLUS  : '+' ;
MINUS : '-' ;
STAR  : '*' ;
INT   : ('0'..'9')+ ;
WS    : ( ' '
        | '\r' '\n'
        | '\n'
        | '\t'
        )
        {$setType(Token.SKIP);}
      ; 


I found this change was helpful:

class ExprLexer extends Lexer;

options {
        k=2;
        charVocabulary='\u0000'..'\u007F'; //allow ascii
        }
 
LPAREN  :        '(';
RPAREN  :       ')';
PLUS    :       '+';
MINUS   :       '-';
STAR    :       '*';
INT     :       ('0'..'9')+ ;
NEWLINE :('\r''\n'
        |'\n'
        |'\r'
        )
        {newline();};
WS      : (' '|'\t') 
        {$setType(Token.SKIP);};


I was running on Windows XP Pro with Java 1.4.2 and the latest Beta of 
Antlr 3.02b I think.

-Paul

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060717/b557b981/attachment.html


More information about the antlr-interest mailing list