Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java and the Java Virtual Machine 3. The imperative Core Java I of Java Pslab 오민경.

Similar presentations


Presentation on theme: "Java and the Java Virtual Machine 3. The imperative Core Java I of Java Pslab 오민경."— Presentation transcript:

1 Java and the Java Virtual Machine 3. The imperative Core Java I of Java Pslab 오민경

2 Static semantics of Java I Primitive type  Numeric type Integral type : byte, short, int long, char Floating point type : float, double  Conditional type boolean Literal  External representations of values of primitive type  i.e. 11, 3.14f, true …

3 Static semantics of Java I Relation ‘ ⊆ ’  A ⊆ B A is subtype of B ( A, B : Type) exist a widening primitive conversion from A to B Each value of A can used as a value of B, however, information may be lost.  Relation ‘ ⊆ ’ Byte ⊆ short ⊆ int ⊆ long ⊆ float ⊆ double, char ⊆ int Reflexive : A ⊆ A Transitive : if A ⊂ B and B ⊂ C, then A ⊂ C

4 Syntax of Java I Domain  Domains (or universes) : words beginning with a capital letter i.e. Exp … expressions, Stm … statements  Elements of ‘Dom’ : ‘dom’ Local variable  Local variable declaration : ‘A loc;’  The scope of loc consists of the statements following the variable declaration in the block.

5 Type checking of Java I Position  Position : small Greek letters α, β, γ. i.e. α exp, β stm  The universe of positions is called ‘Pos’. Compiler verification : type checking  The compiler has to verify that a program is well-typed.  As a result of pasing and elaboration the parse tree annotated with type information : T(α).  During compilation and type checking explicit unary conversion operators (type cast) are inserted at places where they are necessary.

6 Transition rules for Java I execJava I = execJavaExp I execJava Stm I execJavaExp I  All expressions are evaluated from innermost to outermost.  For this purpose, the current control is transferred, by updating pos. from unevaluated expressions to the appropriate subexpressions. until an atom (a literal or a variable) is reached.

7 Transition rules for Java I execJava Stm I  The syntactical structure of the statement to be computed by transferring.  For this purpose through updates of pos, the current control from structured statements to the appropriate substatements until the current statement has been computed normally or abrupts the computation. Abruption : Jump statement (break, continue)  Not a labeled phrase, not degree labeled phrase : Abruption is propagated upwards.  Break(lab b ) : execution proceeds normally at the next phrase of the target  Continue(lab c ) : execution proceeds with the next iteration of the corresponding while statement.

8 Derived language constructs Java constructs that can be syntactically reduced to the core language  ++loc : loc=(A)(loc+1)  --loc : loc=(A)(loc-1)  If (exp) stm : if (exp) stm else; The ‘if statement without else’ suffers from the so- called ‘dangling else problem’  If (exp1) if (exp2) stm1 else stm2 If (exp1) { if (exp2) stm1 else stm2 } => O If (exp1) { if (exp2) stm1 } else stm2 => X

9 Syntax of Java Exp := Lit | Loc | Uop Exp | Exp Bop Exp | Exp ? Exp : Exp | Asgn Asgn := Loc = Exp Stm := ; | Asgn; | Lab : Stm | break Lab; | continue Lab; | if (Exp) Stm else Stm | while (Exp) Stm | Block Block := {Bstm1 … Bstmn} Bstm := Type Loc; | Stm Phrase := Exp | Bstm | Val | Abr | Norm

10 Execution of Java I expressions execJavaExpI = case context(pos) of lit → yield(JLS(lit)) loc → yield(locals(loc)) uop α exp → pos := α uop ► val → yieldUp(JLS(uop, val)) α exp1 bop β exp2 → pos := α ► val bop β exp → pos := β α val1 bop ► val2 → if ¬(bop 2 divMod ∧ isZero(val2)) then yieldUp(JLS(bop, val1, val2)) loc = α exp → pos := α loc = ► val → locals := locals + {(loc, val)} yieldUp(val) α exp0 ? β exp1 : γ exp2 → pos := α ► val ? β exp1 : γ exp2 → if val then pos := β else pos := γ α True ? ► val : γ exp → yieldUp(val) α False ? β exp : ► val → yieldUp(val)

11 Execution of Java I statements execJavaStmI = case context(pos) of ; → yield(Norm) α exp; → pos := α ► val; → yieldUp(Norm) break lab; → yield(Break(lab)) continue lab; → yield(Continue(lab)) lab : α stm → pos := α lab : ► Norm → yieldUp(Norm) lab : ► Break(lab b ) → if lab = lab b then yieldUp(Norm) else yieldUp(Break(lab b )) lab : ► Continue(lab c ) → if lab = lab c then yield(body/pos) else yieldUp(Continue(lab c )) phrase( ► abr) → if pos ≠ firstPos ∧ propagatesAbr(restbody/up(pos)) then yieldUp(abr)

12 Execution of Java I statements { } → yield(Norm) { α1 stm 1 … αn stm n } → pos := α 1 { α1 Norm … ► Norm} → yieldUp(Norm) { α1 Norm … ► Norm αi+1 stm i+1 … αn stm n } → pos := α i+1 if ( α exp) β stm1 else γ stm2 → pos := α if ( ► val) β stm1 else γ stm2 → if val then pos := β else pos := γ if ( α True) ► Norm else γ stm → yieldUp(Norm) if ( α False) β stm else ► Norm → yieldUp(Norm) while ( α exp) β stm → pos := α while ( ► val) β stm → if val then pos := β else yieldUp(Norm) while ( α True) ► Norm → yieldUp(body/up(pos)) Type x ; → yield(Norm)


Download ppt "Java and the Java Virtual Machine 3. The imperative Core Java I of Java Pslab 오민경."

Similar presentations


Ads by Google