Type Checking Grammar Rule Semantic Rule var-decl id : type-exp - - PowerPoint PPT Presentation

type checking
SMART_READER_LITE
LIVE PREVIEW

Type Checking Grammar Rule Semantic Rule var-decl id : type-exp - - PowerPoint PPT Presentation

Type Checking Grammar Rule Semantic Rule var-decl id : type-exp Insert (id.name, type-exp . type ) type-exp int type-exp . type := integer type-exp bool type-exp . type := boolean type-exp 1 array type-exp 1 . type := [num] of


slide-1
SLIDE 1

Type Checking

Grammar Rule Semantic Rule var-decl  id : type-exp Insert (id.name, type-exp.type) type-exp  int type-exp.type := integer type-exp  bool type-exp.type := boolean type-exp1  array [num] of type-exp2 type-exp1.type := makeTypeNode(array,num.size, type-exp2.type)

slide-2
SLIDE 2

Type Checking

Grammar Rule Semantic Rule stmt  if exp then stmt If not typeEqual (exp.type, boolean) then type-error(stmt) stmt  id := exp If not typeEqual (lookup(id.name), exp.type) then type-error(stmt) exp1  exp2 + term If not typeEqual (exp2.type, integer) and typeEqual (term.type, integer) then type-error(stmt); exp1.type := integer exp  term exp.type = term.type

slide-3
SLIDE 3

Type Checking

Grammar Rule Semantic Rule exp1  exp2 or exp3 If not typeEqual (exp2.type, boolean) and typeEqual (exp3.type, boolean) then type-error(exp1); exp1.type := boolean exp1  exp2 [ exp3 ] If isArray (exp2.type) and typeEqual (exp3.type, integer) then exp1.type := exp2.type.child else type-error(exp1 );

slide-4
SLIDE 4

Type Checking

Grammar Rule Semantic Rule exp  num exp.type := integer exp  true exp.type := boolean exp  false exp.type := boolean exp  id exp.type := get_type (id. name)

slide-5
SLIDE 5
  • Var. Declaration Imp. Example

Procedure var-decl ( ) Begin match (id); match (‘:’); type-exp (ts ); create( id.name, ts); End Procedure type-exp ( string t) Begin Case token of “integer”: begin match (“integer”); t= “integer”; end “Bool:”: begin match “bool”; t=“Bool”; end Else syntax_err( ); End

slide-6
SLIDE 6

Type Declaration C++ Implementation Example

void var_decl ( ) { string ts; // Added for implementation purpose token = scan (); string idst r= tokenstring; match (ID); match (COL); type_exp (ts ); // ts is a synthesized attribute for type_exp ST.create_entry(idstr,ts); // semantic action }

slide-7
SLIDE 7

Type Checking Imp. Example

void expr(string &exp_typ, int &val) { //Declare local variable that will be used as parameters ("type" synthesized attributes) of the term function string left_type, right_type; //Declare local variable that will be used as parameters ("val" synthesized attributes) of the term function int left_val, right_val; // The BNF of expr is converted to EBNF to avoid the left recursion term (left_type, left_val); //term() will return type and value of term // This loop is the check the types of the operands and evaluate the expression while (token == PLUS) { match(PLUS); term(right_type, right_val); // Check left and right operands types if (right_type != left_type) semantic_err("Operand are not the same type"); // Compute the left and right operands and put the results in the variable left_val used to accumulate the results left_val = left_val + right_val; }; exp_typ = left_type; val= left_val; }