# Type Conversion in Compiler Design

## Type Conversions

- Conversion of one data type to another automatically by the compiler is called "Type Conversion".
- For Example, Consider expressions like
**x + i**, where x is of type float and i is of type integer. - Since the representation of integers and floating-point numbers is different within a computer and different machine instructions are used for operations on integers and floats, the
**compiler**may need to convert one of the operands of + to ensure that both operands are of the same type when the addition occurs. - Suppose that integers are converted to floats when necessary, using a
**unary**operator (float).

For example, the integer 2 is converted to a **float** in the code for the expression 2 * 3 .14:

- The attribute
**E .type**, whose value is either integer or float. - The
**rule**associated with E → >E_{1}+ E_{2}builds on the pseudocode

- Type conversion rules vary from language to language. The rules for Java in the distinguish between widening conversions, which are intended to preserve information, and narrowing conversions, which can lose information.

Widening and Narrowing Conversions

Type Conversions

## Coercions

- When the type conversion is performed automatically by the compiler without the programmer’s intervention, the type conversion is referred to as implicit type conversion.
- Implicit type conversions, also called
**coercions**, are limited in many languages to widening conversions. Conversion is said to be explicit if the programmer must write something to cause the conversion. Explicit conversions are also called casts. - The
**semantic**action for checking E → E_{1}+ E_{2}uses two functions: - max
**(t**takes two types t_{1},t_{2})_{1}and t_{2}and returns the maximum (or least upper bound) of the two types in the widening hierarchy. It declares an error if either t_{1}or t_{2}is not in the hierarchy. e.g., if either type is an array or a pointer type. - widen
**(a, t, w)**generates type conversions if needed to widen an address a of type t into a value of type w. It returns**a**itself if**t**and**w**are the same type. Otherwise, it generates an instruction to do the conversion and place the result in a temporary t, which is returned as the result.

Pseudocode for **widen**, assuming that the only types are **integer** and **float**.

- Introducing
**type conversions**into expression evaluation

E -> E_{1} + E_{2} |
{ E.type = max(E1.type,E2.type); a1 = widen(E1. addr, E1 .type, E.type); a2 = widen(E2. addr, E2 .type, E.type); E.addr = new Temp(); gen(E. addr '=' a1 '+' a2); } |

Convert Variable into Specified DataType

**Read Also**

## Example

- Consider expressions formed by applying an arithmetic operator op to constants and identifiers, as in the grammar. Suppose there are two types - real and integer, with integers converted to reals when necessary. Attribute type of
**nonterminal**E can be either integer or real, and the type-checking rules are shown, function lookup(e) returns the type saved in the symbol table entry pointed to by e.

Production | Semantic Rule |
---|---|

E -> num | E.type = integer |

E -> num.num | E.type = real |

E -> id | E.type = lookup(id.entry) |

E -> E_{1} op E_{2} |
E.type = if ( E_{1}.type = integer and E_{2}.type = integer )
then integer else if ( E _{1}.type = integer and E_{2}.type = real )then real else if ( E _{1}.type = real and E_{2}.type = integer ) then real else if ( E _{1}.type = real and E_{2}.type = real )then real else type_error |