CS241

Inference Rules for Types

Type checking & is int, else is error, if is when it matches the type Result is a pointer

Multiplication, division and %: both needs to be type int, recurse to check

Addition and Subtraction:

  • Addition
    • not the same types
    • both can be int
  • Subtraction
    • not allowed to do int - pointer
    • allowed to do pointer - pointer: to know where in the array for instance

Procedures:

  • all types in the declarations tau1, tau2, … tau_n needs to match the types in the procedures
  • find procedure name in , check the number of parameters = arguments and types

First we traverse the tree is checking the denominator, then we apply the numerator.

Inference Rules For Well-Typed

  • Expression have a type
  • Statements: we ask a boolean questions, is it well-typed or not.
  • Comparisons: check both sides they need to be the same type

Statements:

  • Symbol table already took care of what it should’ve taken care of.
  • Procedures: well-typed: statment lies must be well-typed and must return type int.
    • control statements: if, while

Assignments

  • LeftHandSide: has the same form as a general , but have a different function: store data lvalue is a non-terminal
  • comes back to haunt us in code generation.
  • Now only need to check left and right hand side having the same type.

Brush up on Recursion.

An Example

Consider:

int foo(int a){
	int x = 0;
	return x + a;
}
int wain(int *a, int b*{
	return foo(b) + a;
}
  • Need to check s is well-typed.
  • Return expression is type int.
  • Return is type int
  • When it recurse, we find out it’s additive, check addition rules for types.
    • Check rhs and lhs, recurse + for type checking x.
    • Is x ID in our declarations, does it have type int? yes
    • Do same thing on recurse a. return int.

Type-Checking Recommendations…

  • A5
  • A6 turn parse tree into a typed tree (intermediate file)