Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about Modifier in Java grammar

The Java grammar defines ModifiersOpt: { Modifier }. Modifier is defined as one of public, protected, private, static etc .... {x} denotes zero or more occurrences of x.

We know that public public is not a valid identifier. Does it mean that any element of the Modifier list (e.g. public) appears only once in {Modifier} ? Is there any "standard" parser combinator for { Modifier } ?

like image 613
Michael Avatar asked Jul 18 '26 17:07

Michael


2 Answers

The modifiers can appear zero or more times. As aioobe said, while syntactically acceptable, public private for example is semantically invalid. There are lots of little situations that are fine strictly according to the grammar but are not allowed by the compiler.

The grammar below uses the following BNF-style conventions:

{x} denotes zero or more occurrences of x.

like image 176
Jonathon Faust Avatar answered Jul 21 '26 05:07

Jonathon Faust


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.

like image 20
aioobe Avatar answered Jul 21 '26 06:07

aioobe