Re: newbie question: type checking

Christian Bau <>
8 Nov 2001 23:20:38 -0500

          From comp.compilers

Related articles
newbie question: type checking (2001-11-08)
Re: newbie question: type checking (Matthias Blume) (2001-11-08)
Re: newbie question: type checking (Christian Bau) (2001-11-08)
| List of all articles for this month |

From: Christian Bau <>
Newsgroups: comp.compilers
Date: 8 Nov 2001 23:20:38 -0500
Organization: Compilers Central
References: 01-11-046
Keywords: types
Posted-Date: 08 Nov 2001 23:20:38 EST

guilaume bour wrote:

> In the next example, we can't determine the type of the variable 'res'
> after the instruction 'if' (it can be a string as well as a float):
> read(div)
> if(div = 0)
> res := "division by 0"
> else
> res := 4/div
> endif
> So I would like to know if there is a way to avoid(even suppress) such
> ambiguity.

If you download the Java language specification from you will find that
a Java compiler has to keep track of which variables have defined
values and which ones have not; the algorithm for that is described in
this document (interesting to read if you are interested in
programming languages anyway).

If you take the same algorithm, but examine not only whether a
variable is defined or not, but also which type it has at which
point. Then if control flow merges from to branches, for example at
the point of the endif in your example, if a variable has different
types in both branches then it becomes undefined. So your example
would not be illegal, but after the endif res is actually undefined
and if the programmer tries to use it then your compiler should give
an error message. I would assume that you have to give an error
message anyway if an undefined variable is used, because in your
language an undefined variable wouldn't have any type.

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.