[antlr-interest] Another FNG question...

Jim Idle jimi at temporal-wave.com
Thu Oct 25 08:55:30 PDT 2007


Looks like you are passing through any whitespace and newline characters for
a start, so you can hide them from the parser by adding $channel = HIDDEN;
to your action for the WS rule, then the parser won't see them.  You many
find that you want an EOF token on the end of your main parser rule, to make
sure that it tries to consume the entire input string.

 

 

Hope that helps!

 

Jim

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Travis R Rogers
Sent: Thursday, October 25, 2007 8:48 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Another FNG question...

 

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/1017e30a/attachment-0001.html 


More information about the antlr-interest mailing list