简体   繁体   English

是否有替代 \b 和/或 JFLEX 的负前瞻?

[英]Is there an alternative to \b and/or negative lookahead for JFLEX?

I am building a Scanner and can't seem to find a way to identify operators like "if" or "else" using JFlex & Regex.我正在构建一个扫描仪,但似乎无法找到一种方法来使用 JFlex 和 Regex 来识别“if”或“else”等运算符。 Since JFlex doesn't fully conform I can't use word-boundary or (?<=\s|^) + (?=\s|$) because neither?由于 JFlex不完全符合我不能使用字边界或 (?<=\s|^) + (?=\s|$) 因为两者都不是? or $ are allowed.或 $ 是允许的。 The idea is to find the correctly written operators not ifo or elso.这个想法是找到正确编写的运算符,而不是 ifo 或 elso。 Thanks in advance.提前致谢。

The idea is to find the correctly written operators not ifo or elso.这个想法是找到正确编写的运算符,而不是 ifo 或 elso。 Thanks in advance.提前致谢。

Just use "if" and "else" and have another pattern that would match ifo and elseo (like for identifiers) and that comes after the patterns for if and else :只需使用“if”和“else”,并有另一个匹配ifoelseo的模式(比如标识符),并且在ifelse的模式之后:

"if"                   { /* it's an if */ }
"else"                 { /* it's an else */ }
[a-zA-Z_][a-zA-Z_0-9]* { /* it's an identifier */ }

Following the maximal munch rule, this will match the identifier rule for inputs like ifo and elseo and will only match the "if" and "else" rules when there is no longer prefix of the input that would match the identifier rule.遵循最大咀嚼规则,这将匹配ifoelseo等输入的标识符规则,并且仅当不再有匹配标识符规则的输入前缀时才匹配“if”和“else”规则。

If your language doesn't have identifiers and ifo and elseo are just supposed to be invalid tokens, you can keep the pattern and just change the action to treat it as an invalid token rather than an identifier.如果您的语言没有标识符,而ifoelseo只是应该是无效的标记,您可以保留模式并更改操作以将其视为无效标记而不是标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM