Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2024-02

"Unexpected end of file" when having an open comment after the final "end."

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Thomas Kurz

Hello all,

I'm unsure about whether or not to report this as a bug. Imho, it is a bug, but maybe there's a good reason to handle this.

Please take the following example:

---start---

program test;
begin
end.

{

---end---

Result:

Z:\>ppc386.exe testproj.pas
Free Pascal Compiler version 3.2.2 [2022/05/15] for i386 Copyright (c) 1993-2021 by Florian Klaempfl and others Target OS: Win32 for i386
Compiling testproj.pas
testproj.pas(6,1) Fatal: Unexpected end of file
Fatal: Compilation aborted

I'd say the code should compile without any errors for the following reasons:

1. The "end." tells the compiler that the file is finished here. Whatever comes after this should not be considered by the compiler.
2. The compiler doesn't consider any other statement after "end.". For example, replacing the open comment with something like "vra" doesn't cause an error either.

Kind regards,
Thomas

Reply by email

Sven Barth
to Thomas Kurz

Thomas Kurz via fpc-pascal schrieb am
Do., 8. Feb. 2024, 13:31:

Show quoted text 5 lines
Hello all, I'm unsure about whether or not to report this as a bug. Imho, it is a bug, but maybe there's a good reason to handle this.

Please report a bug.

Regards,
Sven

Reply by email

Thomas Kurz
to Martin Wynne

Hello Martin,

I've been using Pascal for more than 30 years now, but I must admit I've never before seen the construct you posted.

And it doesn't compile either:

project1.lpr(4,4) Error: Syntax error, "." expected but ";" found

Kind regards,
Thomas

Earlier message 28 lines
----- Original Message ----- From: Martin Wynne via fpc-pascal To: Sent: Thursday, February 8, 2024, 20:01:17 Subject: [fpc-pascal] "Unexpected end of file" when having an open comment after the final "end." Hi Thomas, The error is not the file content after "end.". The error is not having the expected "end;" after "begin". This works ok: _________________________ program test; begin end; end. abc 123 _________________________ Martin.

Reply by email

Unknown author
to Martin Wynne: The error is not the file content after "end.". The error is not having the expected…

No, the original post has the correct program, dropping a ";" in that spot is incorrect syntax.

The problem comes in because there is an open comment, but no close comment.  If you add the closing } the compiler behaves properly.

On the other hand, I do understand that once the "end." is encountered, further processing is unnecessary, and so the error shouldn't occur, because any text showing up after that should be ignored.

Honestly, it's is a case of six of one, half dozen of the other. It's all in the design of the compiler.  I personally wouldn't consider it a bug, but I can see why some folks would, so <shrug>

Martin Wynne wrote 25 lines
Hi Thomas, The error is not the file content after "end.". The error is not having the expected "end;" after "begin". This works ok: _________________________ program test; begin end; end. abc 123 _________________________ Martin. _______________________________________________ fpc-pascal maillist  -  https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Sven Barth
to Martin Wynne: The error is not the file content after "end.". The error is not having the expected…

Martin Wynne via fpc-pascal schrieb am
Do., 8. Feb. 2024, 22:03:

Show quoted text 19 lines
Hi Thomas, The error is not the file content after "end.". The error is not having the expected "end;" after "begin". This works ok: _________________________ program test; begin end; end. abc 123

If that works then that is a bug as well, because the begin of the main *must* be terminated with a "end.", not a "end;".

Regards,
Sven

Reply by email

Ralf Quint
to Martin Wynne: The error is not the file content after "end.". The error is not having the expected…
Martin Wynne wrote 22 lines
Hi Thomas, The error is not the file content after "end.". The error is not having the expected "end;" after "begin". This works ok: _________________________ program test; begin end; end. abc 123 _________________________ Martin.

This is not a valid Pascal source code to begin with (pun intended)...

The "begin" is the start of the actual Pascal program and by definition, that program is terminated by matching that with an "end." That's what the code completion in Lazarus for example is adding into a new "simple program" project source code.

Just adding a random "end;" should also just yield an error message...

Well, I actually did just tested this and it gives as expected an "Error: Syntax error,  "." expected but ";" found. It Doesn't even process past the "end." in that case.

What is however interesting is that an open comment, as mention by the OP,  immediately after the "end." results in the "Error: unexpected end of file" message, however any other addition text past that "end." will result in no error message and completing to compile the program successfully...

I just tried a couple more things, and it seems it is just the "{" or "(*" opening of a comment that is causing the error message, having a "//" comment until end of  line after the "end."  will also compile just fine

Ralf

Reply by email

Sven Barth
to Ralf Quint

Ralf Quint via fpc-pascal schrieb am Fr.,
9. Feb. 2024, 20:36:

Show quoted text 37 lines
On 2/8/2024 11:01 AM, Martin Wynne via fpc-pascal wrote: > Hi Thomas, > > The error is not the file content after "end.". > > The error is not having the expected "end;" after "begin". > > This works ok: > > _________________________ > > program test; > > begin > end; > > end. > > abc 123 > > _________________________ > > Martin. This is not a valid Pascal source code to begin with (pun intended)... The "begin" is the start of the actual Pascal program and by definition, that program is terminated by matching that with an "end." That's what the code completion in Lazarus for example is adding into a new "simple program" project source code. Just adding a random "end;" should also just yield an error message... Well, I actually did just tested this and it gives as expected an "Error: Syntax error, "." expected but ";" found. It Doesn't even process past the "end." in that case.

That's what I had expected.

Show quoted text 11 lines
What is however interesting is that an open comment, as mention by the OP, immediately after the "end." results in the "Error: unexpected end of file" message, however any other addition text past that "end." will result in no error message and completing to compile the program successfully... I just tried a couple more things, and it seems it is just the "{" or "(*" opening of a comment that is causing the error message, having a "//" comment until end of line after the "end." will also compile just fine

If one knows how the compiler is structured (I do) then it isn't that surprising that it behaves that way with a dangling comment due to the interaction between the scanner (which is responsible for the comments) and the parser (which triggers the consumption of the final point). The problem is that the parser does not know that the end of the program has been reached when that point is consumed and happily searches for the next token, consuming all whitespace and comments along the way.

Regards,
Sven

Reply by email