ANTLR4 '+' 操作 [英] ANTLR4 '+' operation

查看:33
本文介绍了ANTLR4 '+' 操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ANTLR4 来学习我现在正在上课的课程,我似乎理解其中的大部分内容,但我无法弄清楚+"的作用.我只能说它通常在括号中的一组字符之后.

I'm using ANTLR4 for a class I'm taking right now and I seem to understand most of it, but I can't figure out what '+' does. All I can tell is that it's usually after a set of characters in brackets.

推荐答案

plus 是 ANTLR 中的 BNF 运算符之一,可以确定表达式的基数.其中有 3 个:加号、星号(又名 kleene 运算符)和问号.意思很容易理解:

The plus is one of the BNF operators in ANTLR that allow to determine the cardinality of an expression. There are 3 of them: plus, star (aka. kleene operator) and question mark. The meaning is easily understandable:

  • 问号代表:零或一
  • Plus 代表:一个或多个
  • 星号代表:零个或多个
  • Question mark stands for: zero or one
  • Plus stands for: one or more
  • Star stands for: zero or more

这样的运算符适用于它之前的表达式,例如ab+(一个 a 和一个或多个 b),[AB]?(零或 AB) 或 a (b | c | d)* (a 后跟零次或多次出现的bcd).

Such an operator applies to the expression that directly preceeds it, e.g. ab+ (one a and one or more b), [AB]? (zero or one of either A or B) or a (b | c | d)* (a followed by zero or more occurences of either b, c or d).

ANTLR4 还使用一个特殊的结构来表示非贪婪匹配.语法是 BNF 运算符之一加一个问号(+?*???).这在您有以下情况时很有用:介绍人匹配、任何内容,然后匹配结束标记.以字符串(引号、任何字符、引号)为例.使用贪婪匹配 ANTLR4 会将多个字符串匹配为一个(直到最后一个引号).然而,非贪婪匹配只匹配到第一个找到的结束标记(这里是引号字符).

ANTLR4 also uses a special construct to denote ungreedy matches. The syntax is one of the BNF operators plus a question mark (+?, *?, ??). This is useful when you have: an introducer match, any content, and then match a closing token. Take for instance a string (quote, any char, quote). With a greedy match ANTLR4 would match multiple strings as one (until the final quote). An ungreedy match however only matches until the first found end token (here the quote char).

旁注:我不知道 ?? 有什么用,因为它匹配单个条目,因此贪婪在这里不起作用.

Side note: I don't know what ?? could be useful for, since it matches a single entry, hence greedyness doesn't play a role here.

实际上,这些算子不是传统 BNF 的一部分,而是扩展巴科斯-诺尔形式的一部分.这些是在 EBNF 中记录某些语法比在缺少许多此类运算符的老式 BNF 中更容易(甚至可能)的原因之一.

Actually, these operators are not part of traditional BNF, but rather of the Extended Backus-Naur Form. These are one of the reasons it's easier (or even possible) to document certain grammars in EBNF than in old-school BNF, which lacks many of these operators.

这篇关于ANTLR4 '+' 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆