3

I'm trying to parse the following grammar with Antlr3:

String...
java.lang.String
java.lang.Object...

This is my .g file (part of it):

doc: name DOTS? EOF;
name: ATOM ('.' ATOM)*;
ATOM: ('a' .. 'z' | 'A' .. 'Z')+;
DOTS: '...';

It doesn't work. Antlr3 treats '.' after ATOM as part of name, not the beginning of DOTS. How can I solve it?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
yegor256
  • 102,010
  • 123
  • 446
  • 597

2 Answers2

2

When I use your grammar:

grammar T;

parse : doc+  EOF;
doc   : name DOTS?;
name  : ATOM ('.' ATOM)*;
ATOM  : ('a' .. 'z' | 'A' .. 'Z')+;
DOTS  : '...';
WS    : (' ' | '\n') {skip();};

to parse the source:

String...
java.lang.String
java.lang.Object...

I get the following parse tree: enter image description here

So, I'm not sure what the problem is exactly: it seems to do what you want.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • looks like you're right.. I need to investigate more. Btw, how did you produce this graph? What is the tool? – yegor256 Feb 22 '11 at 11:04
  • @yegor, I did that in [ANTLRWorks](http://www.antlr.org/works/index.html): there's an interpreter-tab at the lower part of the windows. Note that it only works with the simplest of grammars. When predicates come into play, the interpreter does not works properly. – Bart Kiers Feb 22 '11 at 12:15
0

Would something like this be OK? I'm sure there are better ways of representing your required grammar. I assume would like to match any number of dots after the last ATOM as dots.

doc:    
     name dots EOF;

name: ATOM (DOT ATOM)*;

dots    :   
    DOT*;

DOT :   '.';
ATOM: ('a' .. 'z' | 'A' .. 'Z')+; 
Jimmy
  • 6,001
  • 1
  • 22
  • 21
  • @yegor256, it works for me on AntlrWorks 1.4.2 - you do have dots in lowecase? – Jimmy Feb 21 '11 at 19:53
  • I was just about to write something like that - the grammar stops the dots attaching themselves to the name, but fails on multiline files. – Jimmy Feb 21 '11 at 20:03