Recent

Author Topic: Nested declarations inside advanced record  (Read 22803 times)

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Nested declarations inside advanced record
« Reply #75 on: January 15, 2025, 11:00:55 pm »
and that is a lie.  The implementation is usually driven by convenience BUT, the concept is NOT a matter of convenience.
You are getting there :) Like the concept of the scope of a variable is the block where it is visible, so as was shown multiple times already, a variable can only be referenced after it has been declared, therefore it's scope starts below it's definition. Thats literally the concept described in the book:
Quote
We must also specify in which part of a block an object can be refer-
enced, This part of the block is called the scope of the object. An object is said to be known in its scope.
Roughly speaking, an object defined in a block is known from its definition to the end of the block.
So the scope of an object starts at it's definition and ends at the end of the block the definition occurs in. This is the concept that PBH describes. But then for the implementation purposes PBH creates a symtable stack of lists, which he visualizes. So the drawing you referring to is already a deviation from the concept PBH has introduced just 4 pages earlier.

As the drawing in PBH's book clearly shows, the parameters and the local variables are in the same scope.  A drawing is NOT an implementation and, in this case, that is rather inconvenient for you. <chuckle>
That is just an abstraction thats convinient for the reference compiler he built in his book. As it deviates from his own definition of a scope above.

The thing is, his reference compiler is just a very simple academic example. Compilers in the real world get messy. Want an example? The compiler in PBHs book is a strict LL(1) recursive descent parser, which is great as an didactic example. If you look at any current Pascal compiler, e.g. Delphi or FPC, they are not strictly LL(1). There are language constructs that require some form of internal backtracking.

You are referencing a single didactic decision PBH made to visualize a very simple compiler, oversimplified infact, to make the introduction into compilers easy. Real compilers are much more complex and require different and more complex abstractions. In FPC the technical scope is much more nuonced, because it makes technical sense for the features of FPC. Again... no one is using that compiler PBH wrote in the real world, because it's just a demo, and you can't take the abstractions chosen for the purposes of explaining compiler construction to absolute beginners, as universal concepts all compilers must adhere to

440bx

  • Hero Member
  • *****
  • Posts: 5002
Re: Nested declarations inside advanced record
« Reply #76 on: January 15, 2025, 11:58:39 pm »
You are referencing a single didactic decision PBH made to visualize a very simple compiler, oversimplified infact, to make the introduction into compilers easy.
Whether a compiler is simple or not, the concepts don't change.  In Pascal, parameters are in the same scope as local variables.  Apparently this simple concept is too complex for you to comprehend.  Your attempt to devalue PBH's work clearly reflects the kind of person you are.

N. Wirth and Hanspeter Mössenböck must also be wrong because as stated in Compiler Construction - The Art of Niklaus Wirth:
Quote
A new scope is opened at the top of the stack whenever the parser encounters a procedure, and it is closed when the parser has finished with this procedure.
Maybe N. Wirth and Hanspeter Mössenböck didn't work on compilers that are complex enough for you.  That must be the reason... no doubt.

Stop misleading people with your trash.  You have no grasp of even the simplest compiler concepts. 

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4689
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Nested declarations inside advanced record
« Reply #77 on: January 16, 2025, 02:33:54 am »
I don't know which one is right...  :o

440bx

  • Hero Member
  • *****
  • Posts: 5002
Re: Nested declarations inside advanced record
« Reply #78 on: January 16, 2025, 04:26:28 am »
I don't know which one is right...  :o
I assure you, Per Brinch Hansen is right as are N. Wirth and Hanspeter Mössenböck.  This should not be a surprise to anyone. 

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Nested declarations inside advanced record
« Reply #79 on: January 16, 2025, 05:52:37 pm »
Brinch Hansen says:
Quote
An object is said to be known in its scope. Roughly speaking, an object defined in a block is known from its definition to the end of the block.
So he clearly writes black and white an objects scope is from its definition to the end of the block. This means any definition creates its own scope, and therefore no two objects have the same scope.
So if you think Brinch Hansen is correct, it means parameters and variables are in different scopes (infact any two local variables or parameters have a different scope from each other, because they are defined at different points)

See this:
Code: Pascal  [Select][+][-]
  1. procedure Foo(x: Integer = SizeOf(y)); // error unknown identifier y
  2. var
  3.   y: Integer = sizeOf(x); // works
  4. begin
  5. End.
Y can reference x but not the other way around, because they have different scopes
« Last Edit: January 16, 2025, 05:56:17 pm by Warfley »

Thaddy

  • Hero Member
  • *****
  • Posts: 16532
  • Kallstadt seems a good place to evict Trump to.
Re: Nested declarations inside advanced record
« Reply #80 on: January 16, 2025, 06:28:20 pm »
Frederic, you are right, but don't be too pedantic. :P
But I am sure they don't want the Trumps back...

440bx

  • Hero Member
  • *****
  • Posts: 5002
Re: Nested declarations inside advanced record
« Reply #81 on: January 16, 2025, 07:21:21 pm »
Brinch Hansen says:
Quote
An object is said to be known in its scope. Roughly speaking, an object defined in a block is known from its definition to the end of the block.
So he clearly writes black and white an objects scope is from its definition to the end of the block. This means any definition creates its own scope, and therefore no two objects have the same scope.
PBH didn't draw a separate scope for every variable and parameter in the section about scoping.  Go look at the drawing again. 

Your problem is that you want to equate visibility with scope.  As far as visibility, an object (_not_ in the sense of oop) is visible from the point where it is defined and not before (and even that is disputed/questioned, there are those who make a good case that the object is visible not from where it's defined but from the beginning of the scope to the end regardless of where in the scope the object is defined.)

Regardless of the view on that matter, parameters and locals are in the same scope which is why your code causes the compiler to emit a "duplicate identifier" message.  You can keep posting garbage but, the fact remains, you cannot explain _why_ the code you posted results in a "duplicate identifier" message, that conclusively proves all your babble is pure _garbage_.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

ASerge

  • Hero Member
  • *****
  • Posts: 2379
Re: Nested declarations inside advanced record
« Reply #82 on: January 16, 2025, 07:59:57 pm »
Code: Pascal  [Select][+][-]
  1. procedure Foo(x: Integer = SizeOf(y)); // error unknown identifier y
  2. var
  3.   y: Integer = sizeOf(x); // works
  4. begin
  5. End.
Y can reference x but not the other way around, because they have different scopes
An unfortunate example, because y defined AFTER x. The same as
Code: Pascal  [Select][+][-]
  1. procedure Foo;
  2. var
  3.   x: Integer = SizeOf(y); // Error: Identifier not found "y"
  4.   y: Integer = sizeOf(x); // works
  5. begin
  6. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 16532
  • Kallstadt seems a good place to evict Trump to.
Re: Nested declarations inside advanced record
« Reply #83 on: January 16, 2025, 08:03:43 pm »
@440bx,
https://www.youtube.com/watch?v=b19PcuJsQbA

This deserves a big sigh for ignorance.

Maybe part 2 will calm you down.
« Last Edit: January 16, 2025, 08:05:35 pm by Thaddy »
But I am sure they don't want the Trumps back...

Zoran

  • Hero Member
  • *****
  • Posts: 1900
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Nested declarations inside advanced record
« Reply #84 on: January 16, 2025, 08:07:05 pm »
I don't believe you have been spending so much energy on something totally unimportant in practice.
Who cares whether parameters and local variables are technically in the same scope?

Of course it is important that programmer understand scope rules, but that actually includes being aware that for all practical purposes parameters and local variables can be seen as being in the same scope.

Your discussion has not been helpful at all. If you want to leave something useful in this forum, it is not about who of you is right, the only thing that really matters is that if they are in different scopes, it is not important.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Nested declarations inside advanced record
« Reply #85 on: January 16, 2025, 08:38:46 pm »
PBH didn't draw a separate scope for every variable and parameter in the section about scoping.  Go look at the drawing again.
Yes the drawings are illustrations of the technical abstractions of this concept he used to implement his compiler. Any technical abstraction will somewhat diverge from the theoretical concept. This is what I'm telling you for the past probably 15 posts now.
PBH chose a certain abstraction which is useful for the compiler he is building for didactic purposes in that book. But a different setting will have different requirements for the abstraction, which is why FPC draws it's scopes differently.

You can either go by the theoretical definition according to which every declaration has it's own scope, or you go by the practical abstraction. If you go by the practical abstraction you must accept that it is going to be different from one compiler to the next. For the Pascal Dialect FPC supports, the same abstraction PBH uses does not make sense, so it puts Parameters and Variables into different scopes.

Note that this is not just FPC, clang does the exact same for C++, here local variables are in a different scope than parameters, even though the theoretical definition of a scope (from the declaration to the end of the block) is exactly the same.

I have worked with the sources of multiple compilers. I do not know of a single real world compiler that puts local variables and parameters into the same scope. Because aside from some reference compilers that are not intended to be used by anyone productively, it does not make sense to do so.

An unfortunate example, because y defined AFTER x. The same as
Code: Pascal  [Select][+][-]
  1. procedure Foo;
  2. var
  3.   x: Integer = SizeOf(y); // Error: Identifier not found "y"
  4.   y: Integer = sizeOf(x); // works
  5. begin
  6. end;
The example shows exactly what I am describing. On a theoretical level, no two (separate) declarations can have the same scope.

As I said multiple times. There is the theoretical way of looking at it (each declaration has it's own scope from the declaration to the end), and a technical scope, which is how it is implemented.

440bx

  • Hero Member
  • *****
  • Posts: 5002
Re: Nested declarations inside advanced record
« Reply #86 on: January 16, 2025, 09:20:54 pm »
The scope of parameters and local variables starts at the beginning of the procedure block and ends at the end of the procedure block.  Where their visibility starts is not the same.

How a compiler implements scopes is completely irrelevant.

Make stuff up all you want but, the fact remains, in the code you showed the compiler declares a "duplicate identifier" because the parameters and the local variables are in the same scope.  This has absolutely nothing to do with how it is implemented.   

From one Pascal compiler to another there will be differences in the implementation but, they  will ALL declare there is a duplicate identifier in that code because, again and again and again ad nauseam, parameters and local variables are in the same scope.  If they weren't, they wouldn't be duplicates, even Homer Simpson can figure that out.   (There might be an exception which is, a compiler that includes code from you or maybe Thaddy, in which case I would suspicious about its implementation of boolean operator precedence too.. LOL)

Maybe this will help you (probably won't but religions claim miracles happen, so just in case):
Code: Pascal  [Select][+][-]
  1. procedure foo1(x: Integer);
  2. var
  3.  { the "x" below is in a different scope than the parameter "x"               }
  4.  
  5.  rec : record
  6.    x : Double; { NO duplicate identifier }
  7.  end;  
  8. begin
  9. end;
  10.  
  11.  
  12. procedure foo2(x: Integer);
  13. var
  14.  { the variable "x" is in the same scope as the parameter "x"                 }
  15.  
  16.  x: Double;  { duplicate identifier - what a surprise!!!... LOL               }
  17. begin
  18. end;
  19.  
in foo1, the identifier "x" is in a different scope than the parameter "x", as a result of that, no duplicate identifier is emitted by the compiler.

in foo2, the compiler emits a duplicate identifier message, the difference ?  in foo1, "x" is not in the same scope as the parameter "x".   Really, really simple.  Comprende ?

if not comprende yet, read this paragraph from "Compiler Construction" (page 7):
Quote
5  Symbol Table Handling

A symbol table maps the names in a program to their attributes such as their types and their addresses. It consists of several subtables (scopes), one for every module, procedure and record type. As a consequence of single pass compilation, which Wirth preferred in his later compilers, the scopes can be organized in a stack-like way. A new scope is opened at the top of the stack whenever the parser encounters a procedure, and it is closed when the parser has finished with this procedure.
In case you "no comprende" yet, that means, one scope per procedure block.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Warfley

  • Hero Member
  • *****
  • Posts: 1865
Re: Nested declarations inside advanced record
« Reply #87 on: January 16, 2025, 11:03:35 pm »
The scope of parameters and local variables starts at the beginning of the procedure block and ends at the end of the procedure block.  Where their visibility starts is not the same.
Now you are just plainly contradicting your own source:
Quote
We must also specify in which part of a block an object can be referenced, This part of the block is called the scope of the object.
Brinch Hansen defines the part of a block where a object can be referenced (i.e. it's visibility) as the scope of the object.
You are now saying the exact oposite of the book you claim to be the authority on what a scope is

440bx

  • Hero Member
  • *****
  • Posts: 5002
Re: Nested declarations inside advanced record
« Reply #88 on: January 16, 2025, 11:21:44 pm »
Quote
We must also specify in which part of a block an object can be referenced, This part of the block is called the scope of the object.
Brinch Hansen defines the part of a block where a object can be referenced (i.e. it's visibility) as the scope of the object.
Nice try but definitely not.  The visibility of an identifier is not the same as the scope in which it is defined.  The scope starts where the procedure block starts, the identifier's visibility starts once it is fully defined.  The scope contains the identifier definitions.

That scope contains all the local definitions which include, among other things, the function/procedure parameters.

It took PBH several hundred pages to explain basic language grammar and compiler construction concepts, you need to read the book to understand _all_ the concepts.  Reading 50 lines of FPC source code doesn't do it, much less when you grossly misinterpret the concepts it implements.

You want to talk about compilers, educate yourself, read some books!  That way you'll stop posting misleading garbage detrimental to forum readers.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

lainz

  • Hero Member
  • *****
  • Posts: 4689
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Nested declarations inside advanced record
« Reply #89 on: January 17, 2025, 12:00:00 am »
And what about this valid program, I don't understand what's going on here:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. procedure a();
  4.   var c: integer = 1;
  5.  
  6.   procedure b();
  7.   var c: integer = 2;
  8.   begin
  9.     writeln(c);
  10.   end;
  11.  
  12. begin
  13.   writeln(c);
  14.   b();
  15.   writeln(c);
  16.   b();
  17. end;
  18.  
  19. begin
  20.   a();
  21.   readln;
  22.  
  23. end.

 

TinyPortal © 2005-2018