[antlr-interest] distinction between newline and ws

Sven Busse mail at ghost23.de
Sat Oct 20 07:28:55 PDT 2007


hello,

 

i am very new to antlr and language recognition. So i bought the book

from Terence Parr and now i am currently working through the first

example, the calculator. And unfortunately already, i don't understand

something. The grammar looks like this:

 

grammar Expr;

 

prog  :     stat+ ;

 

stat  :     expr NEWLINE

      |     ID '=' expr NEWLINE

      |     NEWLINE

      ;

 

expr  :     multExpr (('+'|'-') multExpr)* ;

 

multExpr:   atom ('*' atom)* ;

 

atom  :     INT

      |     ID

      |     '(' expr ')'

      ;

 

ID    :     ('a'..'z'|'A'..'Z')+;

INT   :     '0'..'9'+;

NEWLINE     :     '\r'? '\n';

WS    :     (' '|'\t'|'\n'|'\r')+ {skip();};

 

My Question now is, how does antrl know, that "\n" should match to a NEWLINE
instead

of WS (which would mean, it would skip it)? I would have thought, this
grammar is

ambiguous, but apparantly, it isn't. Why not?

 

Thank you

Sven

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


More information about the antlr-interest mailing list