type checking
play

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


  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-exp 1  array type-exp 1 . type := [num] of type-exp 2 makeTypeNode(array,num.size, type-exp 2 . type)

  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) exp 1  exp 2 + term If not typeEqual ( exp 2 .type , integer) and typeEqual ( term.type , integer) then type-error(stmt); exp 1 .type := integer exp  term exp.type = term.type

  3. Type Checking Grammar Rule Semantic Rule exp 1  exp 2 or exp 3 If not typeEqual ( exp 2 .type , boolean ) and typeEqual ( exp 3 .type , boolean ) then type-error(exp 1 ); exp 1 .type := boolean exp 1  exp 2 [ exp 3 ] If isArray ( exp 2 .type ) and typeEqual ( exp 3 .type , integer ) then exp 1 .type := exp 2 .type.child else type-error(exp 1 );

  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)

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

  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 }

  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; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend