Header Ads Widget

Translation of Assignment Statements

Translation of Assignment Statements

In the syntax directed translation, assignment statement is mainly deals with expressions. The expression can be of type real, integer, array and records.

Consider the grammar

  1. S  →    id := E  
  2. E    →  E1 + E2  
  3. E   →   E1 * E2  
  4. E   →   (E1)  
  5. E   →   id  

The translation scheme of above grammar is given below:

Production ruleSemantic actions
S → id :=E{p = look_up(id.name);
 If p ≠ nil then
 Emit (p = E.place)
 Else
 Error;
}
E → E1 + E2{E.place = newtemp();
 Emit (E.place = E1.place '+' E2.place)
}
E → E1 * E2{E.place = newtemp();
 Emit (E.place = E1.place '*' E2.place)
}
E → (E1){E.place = E1.place}
E → id{p = look_up(id.name);
 If p ≠ nil then
 Emit (p = E.place)
 Else
 Error;
}
  • The p returns the entry for id.name in the symbol table.
  • The Emit function is used for appending the three address code to the output file. Otherwise it will report an error.
  • The newtemp() is a function used to generate new temporary variables.
  • E.place holds the value of E.

Post a Comment

0 Comments