Path: utzoo!mnetor!uunet!husc6!bloom-beacon!mit-eddie!bbn!rochester!PT.CS.CMU.EDU!sei!sei.cmu.edu!firth From: firth@sei.cmu.edu (Robert Firth) Newsgroups: comp.lang.pascal Subject: ISO Pascal scope question Message-ID: <4793@aw.sei.cmu.edu> Date: 30 Mar 88 15:59:25 GMT Sender: netnews@sei.cmu.edu Organization: Carnegie-Mellon University, SEI, Pgh, Pa Lines: 41 In-Reply-To: <718@virginia.acc.virginia.edu> [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