[antlr-interest] Another FNG question...

Travis R Rogers travis at jerseyrogers.com
Thu Oct 25 08:48:26 PDT 2007


Yes I'm a newby to antlr and language lexing/parsing in general.  I apologize for the inexperienced nature of these questions and appreciate any guidance that can be provided.
 
I have a grammar (listed at the bottom of the e-mail) that acts odd to me.
 
When I run it through the debugger with the below I get the related (simplified) result:
**NOTE** <nl> = windows newline.
abc10def = string number string
abc10def<nl> = string number string
abc10.2..20.3def = string range string
abc10.2..20.3def<nl> = string range string
10abc = number string
10abc<nl> = number string
10.2..20.3 = range
10.2..20.3<nl> = range
abc = string
abc<nl> = string
10 = number
10<nl> = NoViableAltException
abc10 = string number
abc10<nl> = NoViableAltException
 
Question 1:  I do not understand why I am getting the NoViableAltExceptions in the above when so many other things seem to work.
 
Question 2:  I never get a WS printed out in any of these but the Windows newline should cause 2 of them.  Why not?
 
Next scenario:
abc<nl>def = 1 string of abc
This is true even if I put $channel=HIDDEN or skip() on WS.
 
Question 3:  Why am I only getting only 1 string returned?
 
That should be enough for now.
 
 
grammar mytester;
 
prog      :  stat+;
 
stat      :  range {System.out.println("range");}
          |  number {System.out.println("number");}
          |  string {System.out.println("string");}
          ;
 
string    :  ALPHASTR;
 
range     :  number DOTDOT number;
 
number    :  NUM DOT NUM
          |  NUM
          ;
 
ALPHASTR  :  ALPHA ALPHA*;
 
NUM       :  NUMERIC NUMERIC*;
 
DOTDOT    :  '..';
 
DOT       :  '.';
 
fragment
ALPHA     :  'a'..'z'
          |  'A'..'Z'
          ;
 
fragment
NUMERIC   :  '0'..'9';
 
EOL       :  ';';
WS        :  '\r'|'\n'|' ' {System.out.println("WS");};
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071025/96d7da09/attachment.html 


More information about the antlr-interest mailing list