ITEC 460 - Chapter 7 Notes
7.1 Semantic Errors - Overview
- Example: Functions and variables must be defined/declared
- Example: No type errors (ops only applied to valid values)
- CFG can not generate all and only programs without type
errors
- How to check: not with embedded actions!
- How to check: Visitor Pattern!
- Focus on compile time checks (where do Java and Ada do runtime
checks?)
7.1 Semantic Errors
- Type errors
- Structured Variable Errors
- Type Errors - Examples:
- Arithmetic Operators: generate code for coercion?
- Assignment compatibility
- Name or structural compatibility
- Tests: boolean or true/false values
- Function and Method Errors
- Call must match signature
- Parameters: Assignment compatibility
- Parameters: Subtypes??? Co-contra variant???
- Access Violation Errors:
7.2 Environments
- Need table(s) of definitions
- Problem: nested scopes
- Solution 1: list of key, value pairs (eg name, type)
- Solution 2: Hash table w/ lists of key, value pairs
- Example: Figures 7.3, 4, 6, 7
- Use KeyStack to simulate popping environments (Fig 7.7)
- Solution 3: BST of key, value pairs
7.3 Overall Structure
- Variable Environment - all local and parameter variables
- Type Environment - all built-in and class types
- Built-in types: int, boolean, void
- Array Types: entry for base type
- Class types: each has its own variable environment for its fields
- Function (prototype) environment
- Functions must be defined (or prototyped) before use [is this
documented anywhere]
7.3 Type Checking - Categories of Types
- Built-in types (aka ...)
- Enumerated types and subranges (primitive/subtype?)
- Structured types: collection of other types: array, record,
class, struct, variant records, unions, sets
- Pointer types (and references)
- Function types: First class objects? C, Ada, Java, Haskell
7.3 SimpleJava - Built-in Types
- Builtin types include: INTEGER, BOOLEAN, VOID
- Type Environment: each builtin has entry in environment
- Internal representation entry contains pointer to type object
- Variable entries also contain pointer to type object
- Type checking is simply pointer checking
- Example: Figure 7.9, declare x,y,a,b; x = y; x = a;
7.3 SimpleJava - Class Types
- New kind of type: CLASS
- Add entry to Type environment for the class
- Internal representation value is a new variable environment
- Example: Figures 7.10,11, and a.x = c
7.3 SimpleJava - Array Types
- New kind of type: ARRAY
- Add entry to Type environment for the array type
- Internal representation value is an ARRAY type object
- ARRAY type object has pointer to base type
- Example: Figures 7.12,13
- Example: int a[]
- Is array type (int[]) in type table
- Is base type (int) in type table
- No: error
- Yes: Add new array type to table
- Add variable to variable environment
7.4 Semantic Analysis
- Traverse AST with a Visitor and ...
- Add declarations to table
- Check other code for errors
7.4 Variable and Array Declarations
- int x; - check if int exists and add x to variable environment
- MyClass y; - ...
- int a[]; - ... (how is this different?)
- May have to add several base types
int A[][][];
int B[]
int C[][];
7.4 SimpleJava - Function Prototypes
- New Environment: Functions
- New Kind of Entry: FUNCTION
- FUNCTION contains: return type and param list
- Example: Fig 7.14
7.4 Class Definitions
- Create new variable environment
- Add fields to the new environment
- Create new entry in type environment
- Example: Figure 7.15
class MyClass{
int integerval;
int Array[];
boolean boolval;
}
7.4 Function Definition
- Compare type and number of formal parameters with prototype
- If no prototype exists, add one to table
- Begin a new scope in the variable environment
- Add formal parameters to the variable environment
- Visit function body (with modified variable environment)
- End current scope (removing ...)
7.4 Expressions
- Evaluate operand types
- Determine return type
- Examples: x+y, x++, x < y; foo(x)
- Recursion is your friend - arbitrary complexity
7.4 Variables and Simple Variables
- Check if defined in current scope
- Determine (ie return) type
- Simple variables - simple
7.4 Array Variables
- Check type of index: must be ...
- Check type of base variable: must be ...
- Return type of base variable (from type environment entry
array variable)
- Examples pages 176 and 177
7.4 Instance Variables
- Example: base.field
- Variable
base
must be class variable
- look up
field
in variable environment for base
- Return type for
field
- base can have arbitrary complexity - so use ...
7.4 Statements
- Look at the parts
- Example: If Statements
- Example: Assignment Statements
Topic
Topic
Topic
Topic
Topic
Topic
Topic
Last modified on