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

查看:159
本文介绍了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.

推荐答案

加号是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:

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

此类运算符适用于直接在其前面的表达式,例如 ab + (一个 a 和一个或多个 b ), [AB]?(零或其中一个 A B )或 a(b | c | d)* ( a b c d ).

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会将多个字符串匹配为一个(直到最后一个报价).然而,不匹配的匹配仅匹配到找到的第一个结束标记(此处为报价char).

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的一部分,而是扩展Backus-Naur形式.这是在EBNF中记录某些语法比在老式BNF中更容易(甚至可能)记录的原因之一,而老式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天全站免登陆