3

Java语法定义ModifiersOpt: { Modifier }. Modifier被定义为public, protected, private, staticetc ...之一。{x}表示 . 的零次或多次出现x

我们知道这public public不是一个有效的标识符。这是否意味着列表中的任何元素(Modifier例如public只出现一次{Modifier}?是否有任何“标准”解析器组合器{ Modifier }

4

2 回答 2

2

Not all errors in a Java program are syntactical errors. An error like public private for instance may (as you have discovered) be considered as a semantical error.

As another example, I bet the grammar also allows, for instance

int i = "type error";

Still though, it is not a valid snippet of Java code.

于 2011-01-21T14:04:32.260 回答
2

修饰符可以出现零次或多次。正如aioobe所说,虽然在语法上可以接受,但public private例如在语义上是无效的。有很多小情况严格按照语法是没问题的,但是编译器不允许的。

下面的语法使用以下 BNF 风格的约定:

{x} 表示 x 出现零次或多次。

于 2011-01-21T14:07:31.260 回答