Java – how do I manage optional spaces in ANTLR?

I tried to parse the data file in ANTLR – it has optional spaces

3 6
  97   12
 15 18

The start and end positions of the line are shown below Finally, there is a newline character without a label

^ 3 6$
^  97   12$
^ 15 18$
^

My grammar is:

lines   :   line+;
line    :   ws1 {System.out.println("WSOPT :"+$ws1.text+":");} 
                num1 {System.out.println("NUM1 "+$num1.text);} 
                ws2 {System.out.println("WS :"+$ws2.text+":");}
                num2 {System.out.println("NUM2 "+$num2.text);} 
                NEWLINE
    ;
num1    :    INT    ;
num2    :    INT    ;
ws1 :   WSOPT;
ws2 :   WS;

INT     : '0'..'9'+;
NEWLINE :    '\r'? '\n';
//WS    :   (' '|'\t' )+ ;
WS  :   (' ')+ ;
WSOPT   :   (' ')* ;

This makes

line 1:0 mismatched input ' ' expecting WSOPT
WSOPT :null:
NUM1 3
WS : :
NUM2 6
line 2:0 mismatched input '   ' expecting WSOPT
WSOPT :null:
NUM1 97
WS :   :
NUM2 12
BUILD SUCCESSFUL (total time: 1 second)

(that is, the leading WS has not been recognized, and the last line has been omitted)

I want to parse lines without spaces, for example:

^12    34$
^ 23 97$

But my mistakes are as follows:

line 1:0 required (...)+ loop did not match anything at input ' '

I appreciate the general explanation of parsing WS in ANTLR

The editor @ jitter has a useful answer – {ignore = WS} does not appear in the "authoritative ANTLR reference" book I am using, so it is obviously a thorny field

I also need help to modify it to:

lines   :   line line line;
line
options { ignore=WS; }
        :
                ws1  {System.out.println("WSOPT :"+$ws1.text+":");} 
                num1 {System.out.println("NUM1 "+$num1.text);} 
                ws2  {System.out.println("WS :"+$ws2.text+":");}
                num2 {System.out.println("NUM2 "+$num2.text);} 
                NEWLINE
    ;

But got an error:

illegal option ignore

Edit this has obviously been removed from V3: http://www.antlr.org/pipermail/antlr-interest/2007-February/019423.html

Solution

WS : (' ' | '\t')+
WS : (' ' | '\t')+
     {$channel = HIDDEN;}
   ;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>