[antlr-interest] V3 How comes this grammer discard whitespace?

Terence Parr parrt at cs.usfca.edu
Sat Dec 16 12:52:29 PST 2006


On Dec 16, 2006, at 12:06 PM, Bill Mayfield wrote:

> How comes this grammar accepts input "1      2      3" ? (remove  
> quotes)

It is matching "1    " and then deciding it has read a single expr  
and quits.

Ter
>
>
> grammar SimpleCalc;
>
> options {
>    language=CSharp;
> }
>
> tokens {
>    PLUS     = '+' ;
>    MINUS    = '-' ;
>    MULT    = '*' ;
>    DIV    = '/' ;
> }
>
> @members {
>    public static void Main(string[] args) {
>        SimpleCalcLexer lex = new SimpleCalcLexer(new ANTLRFileStream 
> (args[0]));
>           CommonTokenStream tokens = new CommonTokenStream(lex);
>
>        SimpleCalcParser parser = new SimpleCalcParser(tokens);
>
>        try {
>            parser.expr();
>        } catch (RecognitionException e)  {
>            Console.Error.WriteLine(e.StackTrace);
>        }
>    }
> }
>
> /*------------------------------------------------------------------
> * PARSER RULES
> *------------------------------------------------------------------*/
>
> expr    : term ( ( PLUS | MINUS )  term )* ;
>   term    : factor ( ( MULT | DIV ) factor )* ;
>   factor    : NUMBER ;
>
>
> /*------------------------------------------------------------------
> * LEXER RULES
> *------------------------------------------------------------------*/
>
> NUMBER    : (DIGIT)+ ;
>   WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+      
> { _channel = 99; } ;
>   fragment DIGIT    : '0'..'9' ;



More information about the antlr-interest mailing list