ANTLR4 如何根据规则更改隐藏字符?
•浏览 1
ANTLR4 how do change hidden characters based on rule?
我正在尝试解析结构错误的输入文件,因为通常会跳过换行符,但在某些情况下它用于终止语句,因此有时必须匹配它。然而在这种情况下,换行符似乎成为一般的正常标记,不能被跳过。
为了说明我的问题,请考虑以下语法:
text
: (line '\
')+
;
line
: ( ID )+
| '(' ID* ')'
;
ID : [a-zA-Z]+
;
WS : [ \\t\
\
]+ -> skip
;
a b
c d
(e
f)
line 3:2 extraneous input '\
' expecting {')', ID}
text
: (line {/*check that the last whitespace contained a newline*/}?)+
;
在这个语法中,我想解析如下语句:
text
: (line '\
')+
;
line
: ( ID )+
| '(' ID* ')'
;
ID : [a-zA-Z]+
;
WS : [ \\t\
\
]+ -> skip
;
a b
c d
(e
f)
line 3:2 extraneous input '\
' expecting {')', ID}
text
: (line {/*check that the last whitespace contained a newline*/}?)+
;
但我收到以下错误:
text
: (line '\
')+
;
line
: ( ID )+
| '(' ID* ')'
;
ID : [a-zA-Z]+
;
WS : [ \\t\
\
]+ -> skip
;
a b
c d
(e
f)
line 3:2 extraneous input '\
' expecting {')', ID}
text
: (line {/*check that the last whitespace contained a newline*/}?)+
;
因为括号内的换行符没有被跳过。语法本身要复杂得多,因此不可能简单地放入"'\\
'?" 在任何需要它的地方。
处理此问题的最佳方法是什么?
对于我的两个建议,您需要将空格设置为隐藏通道(而不是跳过它)。
要灵活控制空格(或换行符),您可以应用以下解决方案允许空格部分 ANTLR4。您可以在语法中的每个点启用/禁用空格。
另一种方法是将 \
设置为隐藏通道,但不将其作为标记而是作为语义谓词包含在规则中。
text
: (line '\
')+
;
line
: ( ID )+
| '(' ID* ')'
;
ID : [a-zA-Z]+
;
WS : [ \\t\
\
]+ -> skip
;
a b
c d
(e
f)
line 3:2 extraneous input '\
' expecting {')', ID}
text
: (line {/*check that the last whitespace contained a newline*/}?)+
;
为了实现,你可以使用 BufferedTokenStream#getHiddenTokensToRight 或 BufferedTokenStream#getHiddenTokensToLeft (两者都允许读取隐藏的频道标记)。