如何在ANTLR中管理可选的空格? [英] How do I manage optional whitespace in ANTLR?
问题描述
我正在尝试在ANTLR中解析数据文件-它具有可选的空白,示例为
I am trying to parse a data file in ANTLR - it has optional whitespace exemplified by
3 6
97 12
15 18
下面显示了该行的起点和终点.末尾有换行符,没有选项卡.
The following shows where the line starts and ends are. There is a newline at the end and there are no tabs.
^ 3 6$
^ 97 12$
^ 15 18$
^
我的语法是:
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 : (' ')* ;
给出
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)
(即,领先的WS未被识别并且错过了最后一行).
(i.e. the leading WS has not been recognised and the last line has been missed).
我想解析以空格开头的行,例如:
I would like to parse lines which start without whitespace, such as:
^12 34$
^ 23 97$
但是我随后收到诸如以下错误:
but I then get errors such as:
line 1:0 required (...)+ loop did not match anything at input ' '
我很欣赏在ANTLR中解析WS的一般解释.
I'd appreciate general explanations of parsing WS in ANTLR.
编辑 @jitter有一个有用的答案-{ignore=WS}
并未出现在我正在研究的权威ANTLR参考"书中,因此这显然是一个棘手的领域.
EDIT @jitter has a useful answer - {ignore=WS}
does not appear in the "Definitive ANTLR reference" book that I am working from so it is clearly a tricky area.
仍需要帮助 我已将其修改为:
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
;
但收到错误消息:
illegal option ignore
EDIT 显然已从V3中删除: http://www.antlr.org/pipermail/antlr-interest/2007-February/019423.html
EDIT apparently this has been removed from V3: http://www.antlr.org/pipermail/antlr-interest/2007-February/019423.html
推荐答案
我设法使用lexer构造使此工作正常进行,例如:
I have managed to get this working using lexer constructs such as:
WS : (' ')+ {skip();};
WSOPT : (' ')* {skip();};
,但不在NEWLINE中.然后在解析器中构造诸如:
but not in the NEWLINE. Then in the parser constructs such as:
num1 num2 NEWLINE;
关键是要除去除NEWLINE之外的词法分析器中的所有WS.
The key was to strip all WS in the lexer except the NEWLINE.
这篇关于如何在ANTLR中管理可选的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!