java 如何管理 ANTLR 中的可选空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1654186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I manage optional whitespace in ANTLR?
提问by peter.murray.rust
I am trying to parse a data file in ANTLR - it has optional whitespace exemplified by
我正在尝试解析 ANTLR 中的数据文件 - 它具有可选的空格,例如
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$
^
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 : (' ')* ;
which gives
这使
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)
(i.e. the leading WS has not been recognised and the last line has been missed).
(即前导 WS 未被识别,最后一行被遗漏)。
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 ' '
I'd appreciate general explanations of parsing WS in ANTLR.
我很欣赏在 ANTLR 中解析 WS 的一般解释。
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.
编辑@jitter 有一个有用的答案 -{ignore=WS}没有出现在我正在工作的“权威 ANTLR 参考”书中,所以这显然是一个棘手的领域。
HELP still neededI have modified this 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 get the error:
但得到错误:
illegal option ignore
EDITapparently this has been removed from V3: http://www.antlr.org/pipermail/antlr-interest/2007-February/019423.html
编辑显然这已从 V3 中删除:http: //www.antlr.org/pipermail/antlr-interest/2007-February/019423.html
采纳答案by peter.murray.rust
I have managed to get this working using lexer constructs such as:
我已经设法使用词法分析器结构来完成这项工作,例如:
WS : (' ')+ {skip();};
WSOPT : (' ')* {skip();};
but not in the NEWLINE. Then in the parser constructs such as:
但不是在 NEWLINE 中。然后在解析器构造中,例如:
num1 num2 NEWLINE;
The key was to strip all WS in the lexer except the NEWLINE.
关键是去除词法分析器中除 NEWLINE 之外的所有 WS。
回答by Sam Harwell
WS : (' ' | '\t')+
{$channel = HIDDEN;}
;
回答by jitter
Check Lexical Analysis with ANTLRand then search the part which starts with this heading
Ignoring whitespace in the lexer
忽略词法分析器中的空格
You need to use the { ignore=WS; }rule
你需要使用{ ignore=WS; }规则

