// -*- sugar -*- // 2004-0412: This grammar is still work in progress. // Some rules are incomplete and incorrect. unit: package? import* class_decl+; package: 'package' name ';'; import: = Import: 'import' name ';' | Importx: 'import' name '.*' ';' ; class_decl: = Class: modifier* 'class' id extend? implement? '{' class_body* '}' | Iface: modifier* 'interface' id extends? '{' class_body* '}' ; extend: 'extends' name; extends: 'extends' name ++ ','; implement: 'implements' name; ptyp: < ArrayAtype: ptyp '[' ']' = ArrayName: name '[' ']' = Tchar: 'char' | Tbyte: 'byte' | Tshort: 'short' | Tint: 'int' | Tbool: 'boolean' | Tfloat: 'float' | Tdouble: 'double' | Tvoid: 'void' ; typ: = Tname: name = Ptyp: ptyp ; class_body: = Init: 'static' block | Inner: class_decl | Ctor: modifier* id '(' formal ** ',' ')' throws? block | Method0: modifier* typ id '(' formal ** ',' ')' throws? ';' | Method: modifier* typ id '(' formal ** ',' ')' throws? block | Field: modifier* typ field_decl ++ ',' ';' ; field_decl: = FdeclId: id | FdeclInit: id '=' exp ; stmt: > For: 'for' '(' stmt ';' exp? ';' exp? ')' stmt | If: 'if' '(' exp ')' stmt els | While: 'while' '(' exp ')' stmt ';' | Switch: 'switch' '(' exp ')' block = Throw: 'throw' exp ';' | Do: 'do' stmt 'while' '(' exp ')' ';' | Continue: 'continue' ';' | Case: 'case' id ':' stmt';' | Default: 'default' ':' stmt';' | Break: 'break' id? ';' | Return: 'return' exp? ';' | Var: 'final'? typ field_decl ++ ',' ';' | Label: 'final'? id ':' ';' | Local: class_decl ';' | Exp: 'final'? exp ';' | Sync: 'synchronized' '(' exp ')' block | Try: 'try' block catch* finally? | Assert1: 'assert' exp ';' | Assert2: 'assert' exp ':' exp ';' ; els: 'else' stmt; block: '{' stmt ** ';' '}'; catch: 'catch' '(' formal ++ ',' ')' block; finally: 'finally' block; throws: 'throws' name ++ ','; array: '[' ']'; formal: 'final'? typ exp; exp: > Assign: exp assign_op exp > Cond: exp '?' exp ':' exp < Or: exp '||' exp < And: exp '&&' exp < BitOr: exp '|' exp < BitXor: exp '^' exp < BitAnd: exp '&' exp < Eq: exp '==' exp | Neq: exp '!=' exp < Lt: exp '<' exp | Gt: exp '>' exp | Le: exp '<=' exp | Ge: exp '>=' exp | Inof: exp 'instanceof' typ < Lsh: exp '<<' exp | Rsh: exp '>>' exp | Ursh: exp '>>>' exp < Add: exp '+' exp | Sub: exp '-' exp < Mul: exp '*' exp | Div: exp '/' exp | Mod: exp '%' exp > NewObj: 'new' name '(' exp ** ',' ')' | NewArr: 'new' name dimen* | Cast0: '(' ptyp ')' exp > PreIncr: '++' exp | PreDecr: '--' exp | Not: '!' exp | Inv: '~' exp | Pos: '+' exp | Neg: '-' exp < Call: exp '(' exp ** ',' ')' | Select: exp '.' id | PostIncr: exp '++' | PostDecr: exp '--' = Name: name < OffsetAexp: exp '[' exp ']' | OffsetName: name '[' exp ']' = Int: num | This: 'this' | Super: 'super' | Null: 'null' | True: 'true' | False: 'false' | Atom: '(' exp ')' ; dimen: = Dimen: '[' ']' | Dimenx: '[' exp ']' ; assign_op: = Set: '=' | AddSet: '+=' | SubSet: '-=' | MulSet: '*=' | DivSet: '/=' | ModSet: '%=' | AndSet: '&=' | OrSet: '|=' | XorSet: '^=' | LshSet: '<<=' | RshSet: '>>=' | UrshSet: '>>>=' ; modifier: = Public: 'public' | Protected: 'protected' | Private: 'private' | Static: 'static' | Abstract: 'abstract' | Final: 'final' | Native: 'native' | Synchronized: 'synchronized' | Transient: 'transient' | Volatile: 'volatile' | Strictf: 'strictf' ; name: id ++ '.'; // ** Reference // grammar - jls (first edition) // http://java.sun.com/docs/books/ // http://home.bredband.no/gaulyk/java/grammar/scjp.html // http://home.bredband.no/gaulyk/java/grammar/JavaGrammar1.html // http://java.sun.com/docs/books/jls/first_edition/html/19.doc.html#52996 // http://java.sun.com/docs/books/jls/second_edition/html/syntax.doc.html#44467 // http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#44591 // http://www.isi.edu/~frank/javaOperatorPrecedence.html