H.1 Programming Language
// ----------------------------------------------------------------------
// classes and methods
// ----------------------------------------------------------------------
// a compilation unit
unit: classdecl ;
// a class declaration
classdecl:
( ’package’ name ’;’ )?
( ’import’ name ( ’.’ ’*’ )? ’;’ )*
( ANNOTATION )?
( ’abstract’ | ’final’ | ’public’ )* ’class’ IDENT
( ’extends’ name ’implements’ names )?
( ANNOTATION )?
’{’ ( topdecl )* ’}’
EOF ;
// a top-level declaration
topdecl:
objectvar | classvar | constructor | objectmethod | classmethod ;
// an object variable, possibly with initialization
objectvar:
modifiers typeexp IDENT ( ’=’ valexpnull )? ’;’ ;
// a class variable, possibly with initialization
classvar:
modifiers ’static’ modifiers typeexp IDENT
( ’=’ valexpnull )? ’;’ ;
// a constructor declaration
constructor:
visibility IDENT ’(’ ( params )? ’)’
throwdecls ( ANNOTATION )? block ;
// declaration of an object method
objectmethod:
modifiers ( typeexp | ’void’ ) IDENT ’(’ ( params )? ’)’
throwdecls ( ANNOTATION )? block ;
// declaration of a class method
classmethod:
modifiers ’static’ modifiers
( typeexp | ’void’ ) IDENT ’(’ ( params )? ’)’
throwdecls ( ANNOTATION )? block ;
// ----------------------------------------------------------------------
// statements
// ----------------------------------------------------------------------
// an execution statement
statement:
( an=ANNOTATION )?
( emptystat | block | assignment | methodcall | localvar
| conditional | whileloop | forloop
| continuestat | breakstat | returnstat | throwstat
| trycatch | assertion ) ;
// an empty statement
emptystat: ’;’ ;
// a statement block
block: ’{’ ( statement )* ’}’ ;
// an assignment or method call with return value
assignment: assigncore ’;’ ;
// the core of an assignment statement
assigncore:
lval
( ’=’
( valexpnull | name ’(’ valexps ’)’ | ’new’ name ’(’ valexps ’)’ )
| ’++’
| ’+=’ valexp
| ’--’
| ’-=’ valexp
) ;
// a method call without return value
methodcall: name ’(’ valexps ’)’ ’;’ ;
// a local variable declaration, possibly with initialization
localvar: localvarcore ’;’ ;
// the core of a local variable declaration
localvarcore:
( ’final’ )? typeexp IDENT
( ’=’
( valexpnull
| name ’(’ valexps ’)’
| ’new’ name ’(’ valexps ’)’ )
)? ;
// a conditional statement with one or two branches
conditional: ’if’ ’(’ valexp ’)’ statement ( ’else’ statement )? ;
// a while loop
whileloop: ’while’ ’(’ valexp ’)’ ( ANNOTATION )? statement ;
// a for loop
forloop:
’for’ ’(’
( assigncore | localvarcore )? ’;’ ( valexp )? ’;’ ( assigncore )?
’)’ ( ANNOTATION )? statement ;
// a continue statement
continuestat: ’continue’ ’;’ ;
// a break statement
breakstat: ’break’ ’;’ ;
// a return statement, possibly with return value
returnstat: ’return’ ( valexpnull )? ’;’ ;
// a throw statement
throwstat: ’throw’ ’new’ name ’(’ valexp ’)’ ’;’ ;
// a try catch block
trycatch: ’try’ block ( ’catch’ ’(’ param ’)’ block )+ ;
// an assertion
assertion: ’assert’ valexp ’;’ ;
// ----------------------------------------------------------------------
// value expressions
// ----------------------------------------------------------------------
// a value expression that also includes ~null~
valexpnull: valexp | ’null’ ;
// value expressions
valexp: valexp3 ;
// disjunctions
valexp3: valexp4 ( ’||’ valexp4 )* ;
// conjunctions
valexp4: valexp8 ( ’&&’ v1=valexp8 )* ;
// equalities/inequalities
valexp8:
valexp9
( ’==’ valexp9 | ’!=’ valexp9 | ’==’ ’null’ | ’!=’ ’null’ )* ;
// relations
valexp9:
valexp11
( ’<’ valexp11 | ’<=’ valexp11 | ’>’ valexp11 | ’>=’ valexp11 )* ;
// sums and differences
valexp11: valexp12 ( ’+’ valexp12 | ’-’ valexp12 )* ;
// products and quotients
valexp12: valexp13 ( ’*’ valexp13 | ’/’ valexp13 | ’%’ valexp13 )* ;
// array creation
valexp13: ’new’ typeexp ’[’ valexp ’]’ | valexp14 ;
// unary operators
valexp14: ’+’ valexp14 | ’-’ valexp14 | ’!’ valexp14 | valexp15 ;
// selector operations
valexp15: valexp16 ( rselector ( rselector )* )? ;
// atoms
valexp16:
IDENT | INT | ’true’ | ’false’ | STRING | CHAR | ’(’ valexp ’)’ ;
// ----------------------------------------------------------------------
// auxiliaries
// ----------------------------------------------------------------------
// class-level modifiers
modifiers: visibility ( ’final’ visibility )? ;
// visibility modifiers
visibility: ( ’private’ | ’protected’ | ’public’ )? ;
// throw declarations
throwdecls: ( ’throws’ names )? ;
// a method’s parameter list
params: param ( ’,’ param )* ;
// a method parameter
param: typeexp IDENT ;
// a type expression
typeexp: typeexpbase ( ’[’ ’]’ )? ;
// a type expression
typeexpbase: ’int’ | ’boolean’ | ’char’ | name | IDENT ;
// a value expression list
valexps: ( valexpnull ( ’,’ valexpnull )* )? ;
// a name
name: ( IDENT ’.’ )* IDENT ;
// a sequence of names
names: name ( ’,’ name )* ;
// a location of a variable
lval: IDENT ( lselector )* ;
// an lvalue selector
lselector: ’[’ valexp ’]’ | ’.’ IDENT ;
// an rvalue selector
rselector: ’[’ v=valexp ’]’ | ’.’ ’getMessage’ ’(’ ’)’ | ’.’ IDENT ;
// ----------------------------------------------------------------------
// lexical syntax
// ----------------------------------------------------------------------
IDENT : REALLETTER ( LETTER | DIGIT )* ;
INT : DIGIT ( DIGIT )* ;
STRING : ’~’ (~(’~’ | ’\\’ | EOL) | ESCAPED )* ’~’ ;
CHAR : ’\’’ ( ~(’\’’ | ’\\’ | EOL) | ESCAPED ) ’\’’ ;
WS : (’ ’ | ’\t’ | EOL );
ANNOTATION:
( ’//’ ( ’@’ .* EOL | .* EOL ) WS? )+
| ’/*’ ( ’@’ .* ’@*/’ | .* ’*/’ ) ;
REALLETTER : ( ’a’..’z’ | ’A’..’Z’ );
LETTER : ( ’a’..’z’ | ’A’..’Z’ | ’_’ );
DIGIT : ( ’0’..’9’ );
EOL : (’\n’ | ’\r’ | ’\f’ | ’\uffff’);
ESCAPED : ’\\’
( ’\\’ | ’~’ | ’\’’ | ’n’ | ’t’ | ’b’ | ’f’ | ’r’ |
(’u’ HEX HEX HEX HEX) ) ;
HEX : ’0’..’9’ | ’a’..’f’ | ’A’..’F’ ;