关于 java:ANTLR4 如何根据规则更改隐藏字符? | 珊瑚贝

ANTLR4 how do change hidden characters based on rule?


我正在尝试解析结构错误的输入文件,因为通常会跳过换行符,但在某些情况下它用于终止语句,因此有时必须匹配它。然而在这种情况下,换行符似乎成为一般的正常标记,不能被跳过。

为了说明我的问题,请考虑以下语法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
text
    : (line \
)+
    ;

line
    : ( ID )+
    | ‘(‘ ID* ‘)’
    ;

ID  : [azAZ]+
    ;

WS  : [ \\t\
\
]+ -> skip
    ;

在这个语法中,我想解析如下语句:

1
2
3
4
a b
c d
(e
f)

但我收到以下错误:

1
2
line 3:2 extraneous input \
expecting {‘)’, ID}

因为括号内的换行符没有被跳过。语法本身要复杂得多,因此不可能简单地放入”‘\\
‘?” 在任何需要它的地方。

处理此问题的最佳方法是什么?


对于我的两个建议,您需要将空格设置为隐藏通道(而不是跳过它)。

要灵活控制空格(或换行符),您可以应用以下解决方案允许空格部分 ANTLR4。您可以在语法中的每个点启用/禁用空格。

另一种方法是将 \
设置为隐藏通道,但不将其作为标记而是作为语义谓词包含在规则中。

1
2
3
text
  : (line {/*check that the last whitespace contained a newline*/}?)+
  ;

为了实现,你可以使用 BufferedTokenStream#getHiddenTokensToRight 或 BufferedTokenStream#getHiddenTokensToLeft (两者都允许读取隐藏的频道标记)。


来源:https://www.codenong.com/29150621/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8978.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?