A Much Simplified Java Grammar

Here's another attempt at a Java grammar, now even more simplified. This grammar takes no account of public/private access, has only simple qualified names, doesn't allow embedded assignment and uses ellipsis ( ... ) wherever possible (meaning zero or more of the specified forms, separated by the given punctuation). Square brackets ( [   ] )mean an optional form a vertical bar ( | ) means choice of one form.

Style and color are used to distinguish various kinds of symbols as follows:

Classes and declarations

classdecl ::= class   classname   [ extends   classname ]   { decl ... decl }

decl ::= vardecl   |   methdecl   |   constrdecl

vardecl ::= type   varname   [ =   expr ]   ;

methdecl ::= rettype   methname ( param , ... , param )   { methbody }

constrdecl ::=           classname ( param , ... , param )   { methbody }

param ::= type varname

Methods bodies, statements and expressions

methbody ::= vardecl ... vardecl   statement ... statement

statement ::= varname   =   expr ;

| return   expr ;

| if ( expr ) statement   [ else   statement ]

| for ( type var : expr ) statement

| for ( type var = expr ;   [ expr   ] ;   [ expr   ] ) statement

| { statement ... statement }

expr ::= literal

| varname

| varname . varname

| expr   oper   expr

| varname . methname ( expr , ... , expr )

| new   classname ( expr , ... , expr )

oper ::= +   |   -   |   *   |   /   |   %   |   ==   |   <   |   >   |   &&   |   ||

Types and primitive types

rettype ::= type   |   void

type ::= primtype   |   classname

primtype ::= int   |   float   |   double   |   boolean   |   char

Literals

literal ::= 'char'   |   "char ... char"   |   numeral   |   decimal   |   true   |   false

char ::= (any non-quote character)   |   \'   |   \"  |   \n  |   \t

numeral ::= digit ... digit

decimal ::= numeral . numeral [ e numeral ]

classname ::= capital   alphanum ... alphanum

varname ::= small   alphanum ... alphanum

methname ::= small   alphanum ... alphanum

Individual characters

capital ::= A   |   B   |   C   |   ...   |   Z

small ::= a   |   b   |   c   |   ...   |   z

digit ::= 0   |   1   |   2   |   ...   |   9

letter ::= small   |   capital

alphanum ::= letter   |   digit