How to force antlr3 to parse "forward"?

I am trying to parse the following grammar with Antlr3:

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

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

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

This does not work. Antlr3 treats the '.'after ATOMas part name, not the beginning DOTS. How can I solve it?

+3
source share
2 answers

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 analyze 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: it seems to be doing what you want.

+2
source

- ? , . , ATOM .

doc:    
     name dots EOF;

name: ATOM (DOT ATOM)*;

dots    :   
    DOT*;

DOT :   '.';
ATOM: ('a' .. 'z' | 'A' .. 'Z')+; 
0

Source: https://habr.com/ru/post/1793668/


All Articles