6.1 Data Types and Type Checking

The usual data types are integer, float (we purposefully avoid calling them real, as they really are not), character, string, boolean, label, pointer. Some languages have sub-types within some of these types, for example, in C and Java we have short, long and long long sub-types associated with the type integer. Similarly, type double is a sub-type associated with the type float. Usually, arrays of these basic types are also available and form their own type. In many languages, data structures are available as complex data type.

In C, we can have user defined types for arbitrary data structures and complex data types built-up recursively for defined types. For example, in C we can have a declaration float(*pf[]()) denoting “pf: an array of functions re turning pointer to float”. In spite of this complexity of data types in C, it does not enforce types strictly. For example, a programmer can declare a function without specifying its argument types and the compiler will happily allow any call argument type. That is why C is called a weakly typed language. A good C language compiler issues warnings, but not errors, when doubtful constructs are encountered.

Remember that the Symbol Table that we have been using in Chapters 3, 4 and 5, did have a field type to record the type of an identifier or number. For your ready reference we have given the Node data structure below.

typedef union{
  struct node_struct*N;
  char*S;
  long I;
  float F;
  void *P;
}util;
typedef struct node_struct{
struct node_struct *link[2]; // left = 0, right = 1
util u,v,w,x,y,z;
}Node;
#define name(n)   ((n)–>w.S)
#define type(n)   ((n)–>v.I)

We have also seen in Chapter 5 how the type of a declaration was transmitted to the variables being declared.

decl:    dtype ′:′ dlist ;
dtype:   IVAR | VAR | SVAR ;
dlist:   VAR                       { type($l) = type($<sym>0); }
       | dlist ′,′ VAR             { type($3) = type($<sym>0); }
       ;

In languages like C which allow complex data types, we require more sophisticated methods for defining the type of an identifier or expression.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.107.31