Recent

Author Topic: Curly brackets in strings  (Read 2251 times)

simsee

  • Full Member
  • ***
  • Posts: 184
Curly brackets in strings
« on: October 02, 2022, 11:18:31 am »
Consider the following trivial program:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. begin
  3.   {
  4.   writeln('}');
  5.  }
  6.  writeln;
  7. end.

The compiler gives an error, as it is deceived by the presence of the closing brace inside the string in writeln.

Is this a bug? How should curly brackets be handled in strings? Is it mandatory to use #123 and #125?

In the documentation:

https://www.freepascal.org/docs-html/current/ref/refse8.html#x19-180001.8)

there is no mention of this issue.

Shouldn't the compiler parser ignore what's inside the quotes?

Thanks.
« Last Edit: October 02, 2022, 11:26:39 am by simsee »

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Curly brackets in strings
« Reply #1 on: October 02, 2022, 11:29:40 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. begin
  3.   { <---- this is a multiline comment
  4.   writeln('}'); // this would compile if not surrounded by the multiline comment....
  5.  and this is the end of a multiline comment --->}
  6. writeln;
  7. end.
Code: Pascal  [Select][+][-]
  1. begin
  2. writeln('{');
  3. end.
« Last Edit: October 02, 2022, 11:31:50 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Curly brackets in strings
« Reply #2 on: October 02, 2022, 11:31:01 am »
Hoo boy :-(

Not strictly a bug: use (* *) for the outer comments.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Curly brackets in strings
« Reply #3 on: October 02, 2022, 11:32:38 am »
(* and *) are equal to { and } only older...
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Curly brackets in strings
« Reply #4 on: October 02, 2022, 11:34:44 am »
(* and *) are equal to { and } only older...

No, they were introduced at the same time. Wirth's original comments were /* */ but (I suspect) he realised that that style was also being used by the B language.

Somewhat later: https://www.research-collection.ethz.ch/bitstream/handle/20.500.11850/68910/eth-3059-01.pdf page 50 which shows that comments were in braces with /* */ as alternatives. I believe that at that time Wirth was working largely on CDC kit which had a fairly comprehensive character set, but swathes of the industry still relied on (rebadged) IBM 026 and 029 cardpunches for code entry.

MarkMLl
« Last Edit: October 02, 2022, 12:02:19 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

simsee

  • Full Member
  • ***
  • Posts: 184
Re: Curly brackets in strings
« Reply #5 on: October 02, 2022, 12:53:16 pm »
Thanks for the good explanations. But back to one of my initial questions: Shouldn't the compiler ignore what is in quotes?

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Curly brackets in strings
« Reply #6 on: October 02, 2022, 12:55:59 pm »
Well, /* or divide then multiply has always been nonsense... :-X as is multiply then divide...
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Curly brackets in strings
« Reply #7 on: October 02, 2022, 12:58:53 pm »
Thanks for the good explanations. But back to one of my initial questions: Shouldn't the compiler ignore what is in quotes?
But it does ignore it.... Always has.... It translates to a string literal and is stored as is. Except for escaped characters which are first evaluated and then stored.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Curly brackets in strings
« Reply #8 on: October 02, 2022, 01:03:25 pm »
Short: No.

Longer: Any attempt to do this would mean that the lexer would have to scan the content of the comment and decide that the closing brace was inside a string. That is, broadly speaking, anathema since it implies that the content of the comment would have to be valid and correct Pascal source. I believe that the only situation in which anything inside the comment is interpreted is if the opening brace (or equivalent digraph) is followed by a $directive.

So it you want to exclude a block of code, either use a non-matching multiline comment (i.e. (* *) if your comment contains braces, or vice versa) or a {$ifdef ... $endif } pair. But even that can be fooled if there is an embedded $endif, so the "comment of last recourse" is a // at the start of every line.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

simsee

  • Full Member
  • ***
  • Posts: 184
Re: Curly brackets in strings
« Reply #9 on: October 02, 2022, 01:04:17 pm »
Thanks Thaddy.  I rephrase my question: are curly brackets, as markers for the start and end of comments, to be considered active also inside the quotes?

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Curly brackets in strings
« Reply #10 on: October 02, 2022, 01:09:42 pm »
No. The quotes are parsed first and that is even if the contain itself a curly bracket.
If it does not work, that would be a serious parser bug. (Even with nested comments)

@Mark: you are wrong, see the docs about chicken and egg, (* was first, {} is TP style.:
https://www.freepascal.org/docs-html/ref/refse2.html
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Curly brackets in strings
« Reply #11 on: October 02, 2022, 01:15:48 pm »
@Mark: you are wrong, see the docs about chicken and egg, (* was first, {} is TP style.:
https://www.freepascal.org/docs-html/ref/refse2.html

No Thaddy, YOU ARE WRONG: I cited a 1973 Wirth paper. Page 50 of the PDF, page 44 of the original.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

simsee

  • Full Member
  • ***
  • Posts: 184
Re: Curly brackets in strings
« Reply #12 on: October 02, 2022, 01:16:48 pm »
I'm sorry, I don't want to make you lose your patience, but from my example it would seem that the curly bracket inside the quotes is active and pairs with the previous open one, closing the multiline comment.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Curly brackets in strings
« Reply #13 on: October 02, 2022, 01:32:52 pm »
The code inside the start of a comment is ignored until the compiler sees a closing of the comment. The compiler does not evaluate complex comments (well, it evaluates nested comments.) So the compiler thinks: Oh start of a comment, just look for the closing.. Code inside comments is never compiled, not even evaluated.
In your case you can use proper real old school comments as already suggested: (*...*).
« Last Edit: October 02, 2022, 01:39:15 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Curly brackets in strings
« Reply #14 on: October 02, 2022, 01:36:21 pm »
FWIW: Delphi 7 also does not compile the example code.

Bart

 

TinyPortal © 2005-2018