[comp.lang.pascal] ISO Pascal scope question

firth@sei.cmu.edu (Robert Firth) (03/30/88)

[Sorry to post to the net: mailer trouble again]

In article <718@virginia.acc.virginia.edu> you write:

>program test(output);
>	const c = 10;
>
>procedure junk;
>	const c = c;  { Shouldn't this be an error ? }

Yes, if you accept the ISO definition, this is an error.

In general, an identifier is in scope throughout the block in
which it is declared, and hence hides any outer definition.
It is visible, however, only from the end of its declaration
(with minor exceptions).  This means that an identifier cannot
occur in "its own" definition, and binding such an occurrence
to an outer declaration is incorrect.

Unfortunately, few Pascal compilers get this right; I won't
speculate about the reasons.

For your compiler, I believe you should follow the ISO definition,
and do it right.  There are many simple ways to do this; the one
I used in another compiler was to add the identifiers to the scope
list but mark them "not yet defined", then evaluate their
definitions, and finally remove the mark.  That also catches
things like

	CONST a = b;
	      b = 0;

where there is an outer "b".  If you're doing it one-pass, it is
a bit harder: you must temporarily mark an identifier "referenced"
if it has an APPLIED occurrence, and then check that any newly
declared identifier isn't so marked, and finally remove the marks
at the end of the declaration list.

Hope that helps

Robert Firth

catone@dsl.cis.upenn.edu (Tony Catone) (04/06/88)

In article <4793@aw.sei.cmu.edu> firth@sei.cmu.edu (Robert Firth) writes:
>
>In article <718@virginia.acc.virginia.edu> you write:
>
>>program test(output);
>>	const c = 10;
>>
>>procedure junk;
>>	const c = c;  { Shouldn't this be an error ? }
>
>Yes, if you accept the ISO definition, this is an error.
> ..........
>Unfortunately, few Pascal compilers get this right; I won't
>speculate about the reasons.
> ..........
>For your compiler, I believe you should follow the ISO definition,
>and do it right..........

Does anyone really believe ISO definitions are right? :-)
But seriously, why should the compiler blindly follow the ISO
standard when Berkley's behavior seems reasonable and intuitive?
People sometimes argue portability in this matter.  The few
projects I've worked on using "standard" Pascal have as a 
result been so inefficient as to substantially sabotage their
chances for success.  Besides, no one forces you to use non-
standard features if you don't want to.

(Extraneous lines
included because of 
dumb News restrictions)


					- Tony
					  catone@dsl.cis.upenn.edu
					  catone@wharton.upenn.edu