Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
12010: Compilers
Semantic Analysis 
Dr. Licia Capra
UCL/CS
TODAY
ƒ Goals
– Scoping
• Has variable a been declared prior to its use? 
• Has variable a been declared multiple times?
• …
– Type Checking
• Has variable a been used according to its declared type? 
• Have the parameters passed in input to method m the expected 
type?
• …
ƒ Type System: define type expressions and type 
checking rules for a given language
– Type Expressions: all possible types in the program 
(e.g., int, bool, String, etc.)
TYPE CHECKING
– Type Rules: constraints that the language constructs 
must satisfy (e.g., assignment rule, arithmetic 
expression rules, etc.)
2TYPE EXPRESSIONS
ƒ Type Expressions: describe the possible types in 
the program 
– Basic types (also primitive types, ground types)
• Examples: int, double, boolean
– Build types from basic types using
• Type constructors (e.g., array types, structure types, pointer 
types)
• Function types
• Class types
TYPE EXPRESSIONS
ƒ Arrays
– array(T): array without bounds
• Java: int[]
– array(T,S): array with size
• Java: int[10]
TYPE EXPRESSIONS
ƒ Structures
– Given identifiers idi of types ti, a structure type has form 
{id1:t1, …, idn:tn}
– Support access operations on each field, with 
dicorrespon ng type
• C: struct {int a; float b;}
• Pascal: record a:integer; b:real end
• Java: instance variables in objects
3TYPE EXPRESSIONS
ƒ Pointers
– Pointer types represent addresses of variables of other 
types
– Pointer(T): pointer to an object of type T
• C: int *x
• Pascal: ^integer
• Java: object references
TYPE EXPRESSIONS
ƒ Functions
– Type T1 x T2 x … x TnÆ Tr
• Java: double foo(int i, double j)
foo: int x double Æ double
TYPE EXPRESSIONS
ƒ Classes
– Structure type together with a list of function types
• Java:
class A {
int i; 
public float f;
public void foo1(int a){…}
public int foo2(){…}
}
{i:int; f:float}
foo1:intÆvoid, foo2:voidÆint
4TYPE EXPRESSIONS – EXAMPLES
ƒ Write a type expression for the following declarations:
–
–
public A foo(int i, B[] b, double[] d) {...}
public class A {
int[10] a;
boolean b;
C[] c;
}
TYPE EXPRESSIONS
ƒ Type Information in the Symbol Table
– Bindings between each variable name and its type
– Bindings between each formal-parameter name and its type
– Bindings between each method name and its parameters and 
result type
– Bindings between each class name and its instance variables and 
method declarations
int pow (int n,int m) {
int i=0;
int result=1;
while (i 0) && (height >0)) {
9:             int tmp = width + height;
10:        } else {
11:            int tmp=0;
12:        }
13:        p = tmp * 2;
14:        return p;
15:    }
16: }
[…]
ƒ Discuss why semantic analysis returns a scope error at line 13: what scope rule has been violated, 
and how does the symbol table help in detecting the error?
ƒ Assume that lexical and syntax analysis have been performed on your input program, and that an 
abstract syntax tree has been created. Describe how scope semantic checks can be implemented, by 
means of a visit to the tree.