[fpc-pascal] "Unexpected end of file" when having an open comment after the final "end."

Sven Barth pascaldragon at googlemail.com
Sat Feb 10 09:24:24 CET 2024


Ralf Quint via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am Fr.,
9. Feb. 2024, 20:36:

> 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.


> 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

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20240210/90c7d7bd/attachment.htm>


More information about the fpc-pascal mailing list