views:

134

answers:

3
+1  Q: 

Comments in Pascal

I have a compiler homework question that wants me to draw a DFA for Pascal comments, but I have never (and probably never will) use Pascal. The question does not specify if we should use ANSI Pascal or Turbo Pascal, so I was going to do one for both.

Some Google searches showed me that Turbo Pascal allows nested comments as long as the same delimiter is not used, so {(*comment*)} is ok and so is (*{comment}*), but {{comment}} or (*(*comment*)*) are not ok. My question here is if a comment like {(*{comment}*)} or (*{(*comment*)}*) is ok since the same delimiters are not used in a row.

I also Googled to see if ANSI Pascal allowed nested comments, but I have not found a definitive answer. If ANSI Pascal does allow nested comments, what are the rules?

On a side note, I am not worried about the strange commenting convention of different starting and ending delimiters, a convention that is allowed in ANSI Pascal.

UPDATE:

I would like to be clear that I am not really concerned with how specific compilers handle Pascal comments, I am concerned with how a compiler SHOULD handle Pascal comments if that compiler adhered 100% to either the ANSI or TURBO Pascal standards.

+1  A: 

At least Delphi won't allow {(*{comment}*)} cause the first closing curly bracket will close the first opening curly bracket and not the last one. Same applies to your other sample.

Eugene Mayevski 'EldoS Corp
FPC have got no problem with either of the examples though, it also seems to allow to nest the same symbol i.e. `{{comment}}`. The [docs](http://www.freepascal.org/docs-html/ref/refse2.html) say there's some compiler mode dependancy.
Sertac Akyuz
@Sertac Akyuz I tried various comments in SciTE text editor and it tells me that `{{comment}}` is invalid. I don't know how much I can trust it though. I guess what is and what is not allowed all comes down to the compiler anyways, but I am wondering more about the standards (TURBO and ANSI) rather than what a specific compiler does or does not allow.
typoknig
@typo - Fair enough. If you like have a look at [Pascal ISO 7185:1990](http://www.moorecad.com/standardpascal/iso7185.pdf) standard. That's the only official doc I can refer to.
Sertac Akyuz
A: 

I think in the version of turbo pascal I used, nested comments were not supported, both of these would compile:

{(* }

{{ }

Some Google searches showed me that Turbo Pascal allows nested comments as long as the same delimiter is not used

Effectively, nested comments are "supported", because the alternate syntax is ignored by the compiler.

You could download a pascal compiler and write your dfa to support what that compiler supports. Use one of their example programs, and see if nested comments will compile.

Brian Maltzan
I think that means nested comments are *not* supported. You cannot nest comments; a comment ends at the *first* comment-ending character, no matter how many comment-starting characters came before it.
Rob Kennedy
@Rob Kennedy I don't think that is 100% true for Turbo Pascal because `{(*comment*)}` is valid and so is `(*{comment}*)`. The INNER comment would end at it's matching delimiter in Turbo Pascal, and the OUTER comment would end at it's matching delimiter. That is because Turbo Pascal DOES NOT allow comments made with unmatched delimiters like `(*comment}` or `{comment*)`.
typoknig
But @Typoknig, that doesn't mean comments are nested. Inside a comment, *all* other characters are ignored. The fact that some of those characters happen to look like Pascal's other comment characters is irrelevant. Once you have an "outer" comment, the "inner" comment *ceases to be a comment at all*. It's just plain text in there.
Rob Kennedy
@Rob Kennedy if that is not nesting then what is? A matryoshka doll inside a matryoshka doll is still a matryoshka doll.
typoknig
@Typoknig, nesting is when the syntax rules for opening and closing comments are enforced even when you're already in a comment. If comments could nest, then `{(*}` would be a syntax error because the "inner" comment isn't terminated, and `{{}}` would *not* be a syntax error. Comment delimiters differ from matryoshka dolls in that they change the interpretation of what's inside them. For example, wrapping a variable declaration inside braces means that from the language's perspective, it's no longer a variable declaration; the compiler ignores it.
Rob Kennedy
typknig: what if you put text between the *) and } ? Maybe it just silently skips whitespace-closing comment aggregates. I agree with Rob btw that it is not true nesting. Nesting is _checked_
Marco van de Voort
+4  A: 
Rob Kennedy
Now that you've summed up all *commenting*, I'd like to add in regard of the question that, in [1993](http://en.wikipedia.org/wiki/Pascal_%28programming_language%29#Standards) the ANSI standard have ceased to exist in behalf of the ISO standard. Whereas TURBO, is [regarded](http://pascal-central.com/extpascal.html#anchor-6) rather a non-standard Pascal. Non-standard or not, there seems to be no published specification of it.
Sertac Akyuz
@Rob Kennedy thanks for the very descriptive answer! That made things much clearer. One thing though, where you say `I'd even argue that FPC's...` I am wondering if that is correct or not. I will use your own argument against you that `Comment delimiters differ from matryoshka dolls in that they change the interpretation of what's inside them`. Doesn't the starting comment delimiter of example 3 and 4 `"change the interpretation"` of the internal double-slash `//` comment?
typoknig
@Sertac Akyuz good find about the ANSI standard now being the ISO standard and about Turbo Pascal being a dialect rather than a standard.
typoknig
Rob: This is one of the reasons why Delphi packages often need rebalancing of comments in the header before FPC can compile them without commandline parameters. This because often the {$i whatever.inc} only changes to delphi mode, so above the includefile (the header) is in native FPC mode, and thus expects balancing.
Marco van de Voort
@Typoknig, when comments don't nest (as in ISO and Turbo), the first delimiter changes the interpretation of all further opening delimiters — the `{` causes `//` to no longer start comments. When comments *do* nest, an opening delimiter *shouldn't* change the meaning of other delimiters — a `{` wouldn't change the meaning of `//`. In FPC, where comments nest, we're told that `//` by itself starts a comment ending at EOL. When it's inside another comment, the rules change so the comment ends *before* EOL. In FPC, `{` may cause `//` to start a *different kind* of comment, but not detectably so.
Rob Kennedy