Download presentation
Presentation is loading. Please wait.
1
Semantic Analysis (Types)
COMP 640 Week 4
2
Topics Type Systems Naming and Variables Scope Inheritance
Jay (formal) Java (informal) JavaScript (informal) Ruby (informal) Naming and Variables Scope Inheritance COMP 640 – CWB
3
Major Stages in the Compiling Process
Figure 2.4 Jflex CUP Type analysis and other concerns From T & N Alt: source code for another language COMP 640 – CWB
4
Types Type: defines the set of legal values
Statically typed Dynamically typed Strongly typed: compile time or run time Type validation rules (typical) Uniqueness of variables (scoping) Legal types used (built-in and user-defined) Referenced variable is declared Complicated rules for terms and results of expressions Syntactic contexts where types must match (assignment) Syntactic contexts where a given type is required (while or if) COMP 640 – CWB
5
Type Map tm = {<v1, t1>, <v2, t2>, … <vn, tn>}
Jay example: {<i, int>, <j, int>, <p, boolean>} Types in C C++ Java Perl JavaScript Might represent in a compiler by a hashtable COMP 640 – CWB
6
Jay Type Checking Rules
Each Variable has unique Identifier Each Variable’s type is int or boolean A Variable referenced in an Expression must have been declared Expression: If Variable or Value: result is same type If operator is arithmetic: all terms must be int and result is int If operator is relational: all terms must be int and result is boolean If operator is Boolean: all terms must be boolean and result is boolean Assignment: type of target is same as type of source Conditional or loop: test expression must have type boolean COMP 640 – CWB
7
Declarations Abstract syntax (Jay): Typing function:
Declarations = Declaration* Declaration = Variable v; Type t Typing function: typing: Declarations TypeMap typing(Declarations d) = U <di.v, di.t> COMP 640 – CWB
8
Static Type Checking “Valid” function, V For the declarations:
Program = Declarations decpart; Block body “Valid” function, V V: Program B V(Program p) = V(p.decpart) ^ V(p.pody, typing(p.decpart) For the declarations: V: Declarations B V(Declarations d) = i, j{1,…,n} : (i j di.v dj.v) COMP 640 – CWB
9
Static Type Checking, Statement
Statement = Skip | Assignment | Conditional | Loop | Block V: Statement X TypeMap B V(SkipStmt s, TypeMap tm) = true V(AssignmentStmt s, TypeMap tm) = s.target tm ^ V(s.source, tm) ^ tm(s.target) = typeOf(s.source, tm) V(ConditionaltStmt s, TypeMap tm) = V(s.test, tm) ^ typeOf(s.test, tm) = boolean ^ V(s.thenbranch, tm) ^ V(s.elsebranch, tm) V(LoopStmt s, TypeMap tm) = V(s.test, tm) ^ typeOf(s.test, tm) = boolean ^ V(s.body, tm) V(Block s, TypeMap tm) = V(b1, tm) ^ V(b2, tm) ^ … ^ V(bn, tm) ) COMP 640 – CWB
10
Types, Values, and Expressions in Jay
Types: int and boolean Operators arithmetic: + - * / relational: < <= == != > >= boolean: && || unary: ! Type of expression that is a value or a variable: the type of that value or variable a binary: boolean for boolean or relational operator; int if arithmetic operator a unary: boolean (! is the only unary operator) COMP 640 – CWB
11
Static Type Checking, typeOf function
Expression = Value | Variable | Binary | Unary typeOf: Expression X TypeMap Type typeOf(Value e, TypeMap tm) = e.type typeOf(Variable e, TypeMap tm) = tm(e) typeOf(Binary e, TypeMap tm) = (e.op is arithmetic)? int: boolean typeOf(Unary e, TypeMap tm) = boolean COMP 640 – CWB
12
typeOf Examples Given tm= {<x,int>, <y, int>}
typeOf(x+2*y, tm) = int typeOf(x<2*y, tm) = boolean Source: T&N, p. 87 COMP 640 – CWB
13
Static Type Checking, Expression
Expression = Value | Variable | Binary | Unary Value = int intValue | boolean boolValue Variable = String id Binary = Operator op; Expression term1, term2 Unary = UnaryOp op; Expression term Static Type Checking, Expression V: Expression X TypeMap B V(Value e, TypeMap tm) = true V(Variable e, TypeMap tm) = e tm V(Binary e, TypeMap tm) = e.op is arith or rel ( V(e.term1, tm) ^ V(e.term2, tm) ^ typeOf(e.term1, tm) = int ^ typeOf(e.term2, tm) = int ) ^ e.op is boolean ( V(e.term1, tm) ^ V(e.term2, tm) ^ typeOf(e.term1, tm) = boolean ^ typeOf(e.term2, tm) = boolean ) V(Unary e, TypeMap tm) = V(e.term, tm) ^ typeOf(e.term, tm) = boolean COMP 640 – CWB
14
Real Languages Java JavaScript Ruby COMP 640 – CWB
15
Types in Java Primitive Reference Numeric Boolean Integral
byte, short, int, long, char Floating point (IEEE 754) float, double include NaN and infinity Boolean Reference COMP 640 – CWB
16
IEEE 754 Floating Point Number Representation
Figure 4.1 NaN: e all 1s, m 0 Infinity: e all 1s, m = 0 Zero: e = 0, m = 0 single precision: value = 1.m X e double precision: value = 1.m' X e' COMP 640 – CWB Source: T&N Figure 4.1
17
Reference Types in Java
ClassOrInterfaceType ArrayType ClassOrInterfaceType: ClassType InterfaceType ClassType: TypeName InterfaceType: ArrayType: Type [ ] object: an instance of a class or an array reference value: pointer to an object or null COMP 640 – CWB source:
18
Kinds of Variables in Java
Class variable Instance variable Array component Method parameter Constructor parameter Exception handler parameter Local variables COMP 640 – CWB
19
Java Rules for Automatic Type Conversion and Promotion
Reference (pre-generic types): Implicit conversion of type S to type T: May require runtime check: Object Thread Might not require runtime check: Thread Object May require runtime value change: int long COMP 640 – CWB
20
Java Conversion Contexts
Assignment (rules imply never get an exception) Method invocation (never exception) Casting (may cause exception) String (any type to String) Numeric promotion COMP 640 – CWB
21
Numeric Promotion in Java
Binary operator: promote numeric type to a common type as required for operator float + double double + double int + float float + float short + short int + int Unary operator byte, short, char int COMP 640 – CWB
22
Kinds of Conversions in Java
Identity (e.g., allow unnecessary cast) Widening Primitives (e.g., byte to short, int, long, float, or double) Reference (class type to superclass or implemented interface type, array to object, etc.) Narrowing Primitives (may lose information or generate negative values) Reference (requires runtime check) String (well-defined from any type, including null) Beware the sign bit! COMP 640 – CWB
23
Types in JavaScript (ECMA Script)
Dynamically typed 9 types: undefined, null, boolean, string, number, object, reference, list, completion Number: floating point only Object: collection of properties Reference, list, completion: for formal description only – cannot be specified by the language Almost all combinations of conversions are defined COMP 640 – CWB
24
Conversions in JavaScript
To boolean: Undefined: false Null: false Number: The result is false if the argument is +0, −0, or NaN; otherwise the result is true. String: The result is false if the argument is the empty string (its length is zero); otherwise the result is true. ("false" is true!) Object: true COMP 640 – CWB
25
Conversions in JavaScript
To number: Undefined: NaN Null: +0 Boolean: The result is 1 if the argument is true. The result is +0 if the argument is false. String: Interpret as numerical; if fails, NaN Object: Convert the “default value” of the object to a number (usually results in NaN) COMP 640 – CWB
26
Conversions in JavaScript
To string: Undefined: "undefined" Null: "null" Boolean: If the argument is true, then the result is "true". If the argument is false, then the result is "false". Number: NaN “NaN” +0 or -0 “0” infinity “infinity” other number: use a complex rule that tries to produce what the user would expect Object: convert the “default value” of the object to string COMP 640 – CWB
27
Conversions in JavaScript
To object: Undefined: Throw a TypeError exception. Null: Throw a TypeError exception. Boolean: Create a new Boolean object Number: Create a new Number object. String Create a new String object. COMP 640 – CWB
28
Objects in JavaScript Collection of properties Object literal:
{ name: "Alice" ,: age: 31, address: { street: "123 Main St.", city: "Why", state: "AZ"} } Initial properties determined by a prototype object Properties may be dynamically added and deleted JSON: a protocol based on this notation: COMP 640 – CWB
29
Types in Ruby Dynamically typed All variables are objects or nil
Variable names imply scope Globals start with $ Instance variables start Constants start with upper case Local variables start with lower case or underscore No declarations Explicit and implicit operator overloading COMP 640 – CWB
30
Types in Ruby, cont. Selected predefined types (classes) Array
Exception (various) Hash IO Numeric (big and little integral, floating point) Proc (procedure object) Range RegExp Struct NilClass COMP 640 – CWB
31
"Range" Type in Ruby Created with … or .. operator:
0 … 10 The range 0 through 9 The range 0 through 10 Used, for example, in for loop: for i in 0…n statements end Operands to … or .. can be any objects with the methods <=> Comparison (-1, 0, 1) succ Produces the successor COMP 640 – CWB
32
"Regexp" Type in Ruby Literal: /abc?d*/
New method: Regexp.net("abc?d*") Match operator (assume r= /abc?d*/) r =~ "xxabddd" Returns 2 COMP 640 – CWB
33
Hash Type in Ruby Literal: h= { a=>x, b=>y, c=>z }
Access: h["b"] Set/add: h["d"]= "q" COMP 640 – CWB
34
Types in Perl Types: Scalars Arrays Hashes
Distinguish numbers and strings by using different operators Arrays Hashes COMP 640 – CWB
35
Common Concerns Strings Names Scope Operator overloading
Objects and Inheritance COMP 640 – CWB
36
Strings C: Pointer to character (char*), initialized to point to a null-terminated array of bytes C++: char* or the String class Java: String class Plus string-specific language features Characters are 2 bytes (Unicode 2000) COMP 640 – CWB
37
Unicode "Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language. " source: Character map: Some fonts: COMP 640 – CWB
38
Unicode Encoding Version 4.0: 97,447 characters (~870,000 unassigned)
Basic Multilingual Plane (Unicode 2000 – Java): 1st 64K of code space (with ~6000 unassigned codes) 16 bit (4 hex digits) First page (0x0000 to 0x00ff) is 8-bit Latin First half of first page is ASCII (0x0000 to 0x007f) Encodings UTF-8 – 1 to 4 bytes UTF-16 – a single 16-bit unit, or a pair of them UTF-32 – a single 32-bit unit Character sequences (e.g., a followed by non-spacing ^ to get â) Alternative: precomposed characters COMP 640 – CWB
39
Naming Jay identifiers: letter followed by letters and digits
Java identifiers: Add _ and $ Ruby identifiers: Start with upper case letter, lower case or $ The start character has semantic meaning COMP 640 – CWB
40
Names in Java Name categories:
package type expression (e.g., "a" in a = b; or a[3] = c;) method package-or-type (e.g., "a" in a.b x = …) ambiguous The latter two are resolved in semantic analysis COMP 640 – CWB
41
Reserved Words Almost all languages have them Parsing sanity
Exception: PL/I Partial exceptions: Pascal, Scheme Others? Parsing sanity Readability COMP 640 – CWB
42
Scope Static: variable's scope is known at compile time
Dynamic: variable's scope is a function of the current call stack COMP 640 – CWB
43
Scope in Jay One scope All variables known throughout the program
COMP 640 – CWB
44
Scope in Java "scope" means "where an entity can be referred to with a simple name" Scopes of Imported types: the compilation unit Nested types: the enclosing block Labels: the labeled statement Member: entire class body, including nested classes (same for interfaces) Parameter: method body Local variable in block: rest of enclosing block, including its own initializer Local variable in for-statement initializer: rest of the for statement and its enclosed statement Exception handler parameter: the catch block COMP 640 – CWB
45
Shadowing in Java Declaration in inner scope "shadows" name declared in an outer scope Distinct from "hiding", which refers to overriding members in a subclass Distinct from "obscuring", where a variable name obscures a type name COMP 640 – CWB
46
Operator Overloading Implicit: k= i/j; (/ means different things depending on the types of i and j) Explicit: e.g., C++ MyClass operator + (MyClass other) { MyClass result; //code to compute the "sum" of this and other return result; } COMP 640 – CWB
47
Objects, Classes, Inheritance, and Polymorphism
Objects: data + methods Inheritance – B is subclass of A means B can be used wherever an A is expected (B "is-a" A) B inherits all the stuff of A, but may modify or add to it Polymorphism – When a B is used where an A is expected, its behavior may be different than for an A. How is this accomplished? COMP 640 – CWB
48
Objects, Inheritance, and Polymorphism – C++
The first part of the layout of B is identical to the layout of A. A* x= new A(); B* y= new B(); A* z= new B(); The compiler allows x and z to access the a, b, and c members and to call the f and g methods y to access a, b, c, d, and e and to call f, g, h, i int a char* b float c Object of class A f() g() Object of class B, a subclass of A int a char* b float c double d short e; f() g() h() i() Multiple inheritance complicates things – a lot COMP 640 – CWB
49
Objects, Inheritance, and Polymorphism – Java
The JVM spec does not specify the layout of objects The diagram to the right represents Sun's JVM implementation* int a char* b float c Object of class A Class object for class A Reference to A A r; f() g() h() i() COMP 640 – CWB *Source: footnote 8
50
Objects, Inheritance, and Polymorphism – JavaScript
JavaScript is object-oriented but dynamically typed An object resembles a hashtable. Inheritance uses a prototype scheme. Polymorphism uses function properties. new F(a, b, c) invokes the [[Construct]] method of function F [[Prototype]] a 23 b "hello" x 19 Object Prototype Object [[Prototype]] x 3 y "world" f Function Object [[Prototype]] [[Call]] prototype [[Construct]] COMP 640 – CWB
51
Constructing a JavaScript Object
When the [[Construct]] property for a Function object F is called, the following steps are taken: 1. Create a new native ECMAScript object. 2. Set the [[Class]] property of Result(1) to "Object". 3. Get the value of the prototype property of the F. 4. If Result(3) is an object, set the [[Prototype]] property of Result(1) to Result(3). 5. If Result(3) is not an object, set the [[Prototype]] property of Result(1) to the original Object prototype object as described in 6. Invoke the [[Call]] property of F, providing Result(1) as the this value and providing the argument list passed into [[Construct]] as the argument values. 7. If Type(Result(6)) is Object then return Result(6). 8. Return Result(1). Source: COMP 640 – CWB
52
Objects, Inheritance, and Polymorphism – JavaScript
Reading a property p in an object: If p exists, return it's value If not, return [[Prototype]].p Recurse up the prototype chain as necessary Setting property p to x If p does not exist, create a new property p Set property p to x Do NOT follow the prototype chain [[Prototype]] a 23 b "hello" x 19 Object Prototype Object [[Prototype]] x 3 y "world" f Function Object [[Prototype]] [[Call]] prototype [[Construct]] COMP 640 – CWB
53
Next Week Memory Management (Chapter 5) Exceptions (Chapter 6)
COMP 640 – CWB
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.