作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 PostgreSQL SQL 定义词法分析器规则。
问题在于运算符定义和行注释相互冲突。
例如 @---
是一个运算符标记 @-
后跟 --
注释而不是运算符标记 @---
在 grako
中,可以为 -
片段定义否定前瞻,例如:
OP_MINUS: '-' ! ( '-' ) .
在 ANTLR4 中,我找不到任何方法来回滚已经消耗的片段。
有什么想法吗?
这里是 PostgreSQL 运算符的原始定义:
The operator name is a sequence of up to NAMEDATALEN-1
(63 by default) characters from the following list:
+ - * / < > = ~ ! @ # % ^ & | ` ?
There are a few restrictions on your choice of name:
-- and /* cannot appear anywhere in an operator name,
since they will be taken as the start of a comment.
A multicharacter operator name cannot end in + or -,
unless the name also contains at least one of these
characters:
~ ! @ # % ^ & | ` ?
For example, @- is an allowed operator name, but *- is not.
This restriction allows PostgreSQL to parse SQL-compliant
commands without requiring spaces between tokens.
最佳答案
您可以在词法分析器规则中使用语义谓词来执行前瞻(或后视)而不消耗字符。例如,以下涵盖了运算符的几个规则。
OPERATOR
: ( [+*<>=~!@#%^&|`?]
| '-' {_input.LA(1) != '-'}?
| '/' {_input.LA(1) != '*'}?
)+
;
但是,上述规则并未解决在运算符末尾包含 +
或 -
的限制。为了尽可能以最简单的方式处理这个问题,我可能会将这两种情况分成不同的规则。
// this rule does not allow + or - at the end of a rule
OPERATOR
: ( [*<>=~!@#%^&|`?]
| ( '+'
| '-' {_input.LA(1) != '-'}?
)+
[*<>=~!@#%^&|`?]
| '/' {_input.LA(1) != '*'}?
)+
;
// this rule allows + or - at the end of a rule and sets the type to OPERATOR
// it requires a character from the special subset to appear
OPERATOR2
: ( [*<>=+]
| '-' {_input.LA(1) != '-'}?
| '/' {_input.LA(1) != '*'}?
)*
[~!@#%^&|`?]
OPERATOR?
( '+'
| '-' {_input.LA(1) != '-'}?
)+
-> type(OPERATOR)
;
关于词法分析器中的 ANTLR4 负前瞻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24194110/
我有一个看起来像这样的字符串: text = "9) 9 的文本\r\n10) 10 的文本\r\n11) 11 的文本\r\n12) ...\r\n123) 123 的文本" 我正在尝试将其拆分如下
下一代的 3D Tiles 前瞻 原文:Introducing 3D Tiles Next, Streaming Geospatial to the Metaverse 原文发布时间:2021年11月
我有一个使用正则表达式回顾的 string.replace() 函数。 myString.replace(/(?
我是一名优秀的程序员,十分优秀!