Grammar for micro-Java * sestoft@dina.kvl.dk * 2002-04-21 --------------------------------------------------------- Micro-Java (uJava) is a sublanguage of Java. By design, a syntactically well-formed micro-Java program is also a syntactically well-formed Java program, and the intention is that the meaning of the program should be the same in both languages. Many simplifications have been made: * only non-static fields and methods * a class must explicitly declare its (unique) immediate superclass * method overriding, but no method overloading * no redeclaration of fields (no field hiding) * a field or method reference must be qualified: this.f, this.m() * no constructors (hence object construction only by new C()) * no arrays * no interfaces * no access modifiers * no nested classes * no initializers in variable and field declarations * no exceptions * ... To generate and compile the lexer and parser for micro-Java, and load the parser and interpreter: mosmllex Oolex.lex mosmlyac -v Oopar.grm mosmlc -c Absyn.sml mosmlc -c -liberal Oopar.sig Oopar.sml mosmlc -c Oolex.sml mosml -I .. parse.sml oo.sml Informal grammar ---------------- program ::= program classdecs classdecs ::= zero or more class declarations classdec classdecs classdec ::= class declaration class NAME extends NAME { memberdecs } memberdecs ::= member declarations memberdec memberdecs memberdec ::= fielddec methoddec fielddec ::= field declaration typ NAME ; methoddec ::= method declaration void NAME(parameterdecs) block typ NAME(parameterdecs) block parameterdecs ::= comma-separated parameter list parameterdec1 parameterdec1 ::= parameterdec parameterdec , parameterdec1 parameterdec ::= typ NAME typ ::= int integer type NAME class name stmt ::= NAME = expr ; local variable or parameter assignment expr.NAME = expr ; field assignment if (expr) stmt if-statement if (expr) stmt else stmt if-else statement while (expr) stmt while-loop expr ; expression as statement return ; return return expr ; return block block statement block ::= block statement { stmtordecseq } stmtordecseq ::= statements and declarations empty sequence stmt stmtordecseq statement vardec stmtordecseq local variable declaration expr ::= this current object reference NAME local variable or parameter expr.NAME field access const constant literal new NAME() contruct object NAME(exprs) call to built-in method ( expr ) parenthesized expression ! expr logical negation expr op expr arithmetic, comparison, logical expr.NAME(exprs) call to user-defined method exprs ::= comma-separated expressions expr1 expr1 ::= expr expr, expr1 const ::= constant literals CSTINT integer literal - CSTINT negative integer CSTBOOL boolean literal CSTSTRING string literal null the null literal Lexical matters: tokens and comments ------------------------------------ NAME: [`a`-`z``A`-`Z`][`a`-`z``A`-`Z``0`-`9`]* except for the keywords, which are: boolean, class, extends, else, false, for, if, int, new, null, return, this, true, void, while CSTINT: [0-9]+ CSTBOOL: false | true CSTSTRING: `"`...`"` with string escapes \b \t \n \r \" \' \\ \ddd OP: + - * / % == != < <= > >= && || Two kinds of comments (not inside strings, not nested): // comment extends to end of line /* ... */ delimited comment