Pascal评论是否应该嵌套? [英] Are Pascal comments supposed to nest?

查看:272
本文介绍了Pascal评论是否应该嵌套?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编译器家庭作业问题,希望我绘制一个DFA的Pascal评论,但我从来没有(也可能永远不会)使用Pascal。这个问题没有指定我们应该使用ANSI Pascal还是Turbo Pascal,所以我要为它们做一个。



一些Google搜索结果显示,Turbo Pascal允许嵌套注释,只要不使用相同的分隔符,因此 {(* comment *)} 是确定的,因此是(* {comment} *) ,但 {{comment}} (*(* comment *)*)不行。我的问题是,如果 {(* {comment} *)} (* {(* comment *)} code>是确定,因为相同的分隔符没有在一行中使用。



我也Googled看看是否ANSI Pascal允许嵌套评论,但我还没有找到一个确定的答案。如果ANSI Pascal允许嵌套注释,有什么规则?



在一个侧面的注释,我不担心不同的开始和结束分隔符的奇怪的注释约定,

$ p
$ b

更新:清楚的是,我并不真正关心具体的编译器如何处理Pascal注释,我关心编译器应该处理Pascal注释,如果编译器坚持100%到ANSI或TURBO Pascal标准。

解决方案

ISO 7185: 1990年

说明:


任何序列的字符和行的分隔,不包含}或*),构造



('{'|'(*')commentary '}')



应该是一个注释,如果没有{也不是(*出现在一个字符串或注释内。 / p>

注意


  1. 注释可能开始于 { 并以 *)结尾,或以(* $ c>}

  2. 序列(*)不会出现在评论中,即使序列 {)可以。


据此,基本上只有一种注释。虽然您可以使用 {(* )开始注释,但不能使用不同的字符集换句话说,Turbo Pascal有两种注释,一种是使用大括号,另一种是使用括号。



如果你有一个大括号,样式注释并将其包含在括号风格注释中以创建(* {} *),ISO表示注释是(* {} code> with *)剩余,这将是一个语法错误在你的代码,而Turbo Pascal说的评论是(*



两种样式都不能将已经包装的注释包装在另一组注释分隔符中。因为一旦你封装了一个注释,内部注释不再被视为一个注释,只是普通的文本如果你有 {(**)} 想要将其括在括号式注释中以使(* {(**)} *),ISO和Turbo Pascal都说该注释是

$($)

在fpc和objfpc模式下,Free Pascal支持嵌套注释,因此我最近的示例将被接受为没有剩余文本的有效注释。 关于此事的FPC文档实际上并没有表明这一点。它给出了所谓的嵌套注释的六个示例:

 
{注释1(*注释2 *)}
评论2} *)
{评论1 //评论2}
(*评论1 //评论2 *)
//评论1(*评论2 *)
/ / comment 1 {comment 2}

但是Turbo Pascal(或Delphi,最后两行),我们已经建立不支持嵌套,则会接受所有完全有效的注释! FPC确实支持嵌套注释,但这些例子没有说明它。以下是嵌套评论的一些示例:

 
{注释1 {comment 2}}
(*注释1 *)*)
{comment 1(* comment 2 {comment 3} *)}

FPC的第三和第四示例实际上是对支持嵌套评论的声称的反例。这两行不应该是完全的意见。在该行中间的 // 引入一个注释,直到行末(EOL)才结束。 EOL在} *)之后,因此内部的斜线风格注释不可能在我们到达大括号或圆括号分隔符时终止。就像< a> < b> < / a> < / b> 是不正确的嵌套XML,我们不正确地嵌套注释: {//} EOL



经过进一步的实验,我得出结论,在FPC,只有同一类型的评论巢。例如,一旦您用 {打开一个注释,(* 字符不再是特殊的。如果是,那么 {(*} 将是一个语法错误,由于未终止的括号注释,我们可以解释这个通过说,当评论是异质的时,内部评论的终止是可选的,但是更简单的解释说,内部评论没有被检测为一个评论一旦你打开大括号式评论,只有大括号有意义。此外, // 在大括号内部不会启动斜杠式注释。这是一个更简单的解释,说FPC允许斜杠式注释被不正确地嵌套您可以说斜杠式评论根本不嵌套,或者嵌套在其他斜杠式注释中的斜杠式注释共享单个EOL终止符



您的作业可能意味着您使用Turbo Pascal风格,注释分隔符必须匹配。它绝对不希望你使用自由Pascal风格,因为它会使分配不可能完成 - 一个DFA不能接受任意嵌套的结构。


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.

解决方案

ISO 7185:1990 says this about comments in §6.1.8:

Where a commentary shall be any sequence of characters and separations of lines, containing neither } nor *), the construct

( '{' | '(*' ) commentary ( '*)' | '}' )

shall be a comment if neither the { nor the (* occurs within a character-string or within a commentary.

NOTES

  1. A comment may thus commence with { and end with *), or commence with (* and end with }.
  2. The sequence (*) cannot occur in a commentary even though the sequence {) can.

According to that, there's essentially just one kind of comment. Although you can start a comment with either { or (*, you can't use different sets of characters to "wrap" other comments. Turbo Pascal, on the other hand, has two kinds of comments, those that use braces and those that use parentheses.

If you have a brace-style comment and enclose it in a parenthesis-style comment to create (*{}*), ISO says the comment is (*{} with *) left over, which will be a syntax error in your code, whereas Turbo Pascal says the comment is (*{}*) with no text left over.

Neither style lets you wrap an already-wrapped comment in yet another set of comment delimiters. That's because once you wrap a comment, the "inner" comment ceases to be treated as a comment anymore. It's just ordinary text. If you have {(**)} and you want to wrap it in a parenthesis-style comment to make (*{(**)}*), ISO and Turbo Pascal both say the comment is (*{(**) with }*) left over.

In fpc and objfpc modes, Free Pascal supports nesting comments, so my most recent example would be accepted as a valid comment with no leftover text. The FPC documentation on the matter doesn't actually demonstrate that, though. It gives six examples of supposedly nested comments:

{ Comment 1 (* comment 2 *) }  
(* Comment 1 { comment 2 } *)  
{ comment 1 // Comment 2 }  
(* comment 1 // Comment 2 *)  
// comment 1 (* comment 2 *)  
// comment 1 { comment 2 }

But Turbo Pascal (or Delphi, for the last two lines), which we've already established doesn't support nesting, would accept all of those as perfectly valid comments! FPC really does support nested comments, but those examples don't illustrate it. Here are some examples of nested comments:

{ Comment 1 { comment 2 } }
(* Comment 1 (* comment 2 *) *)
{ Comment 1 (* comment 2 { comment 3 } *) }

I'd even argue that FPC's third and fourth examples are actually counterexamples to the claim that FPC supports nested comments. Those two lines shouldn't be complete comments at all. The // in the middle of the line introduces a comment that doesn't terminate until the end of the line (EOL). The EOL comes after the } and *), so the inner, slash-style comment can't possibly have terminated by the time we reach the brace or parenthesis delimiters. Just as <a> <b> </a> </b> is improperly nested XML, we have improperly nested comments: { // } EOL.

After further experimentation, I conclude that in FPC, only comments of the same type nest. For example, once you open a comment with {, the (* characters cease to be special. FPC will not open a parenthesis-style comment inside the brace-style comment. If it did, then {(*} would be a syntax error due to the unterminated parenthesis comment. We could explain this by saying that termination of inner comments is optional when the comments are heterogeneous, but it's a simpler explanation to say that the inner comment isn't detected as a comment at all. Once you open a brace-style comment, only braces have meaning. Furthermore, // doesn't actually start a slash-style comment when it's inside braces. That's a simpler explanation that to say that FPC allows slash-style comments to be improperly nested. You can either say that slash-style comments don't nest at all, or that slash-style comments nested within other slash-style comments share a single EOL terminator.

Your assignment probably meant for you to use the Turbo Pascal style, where the comment delimiters must match. It definitely didn't expect you to use the Free Pascal style since it would have made the assignment impossible to complete — a DFA cannot accept arbitrarily nested constructs.

这篇关于Pascal评论是否应该嵌套?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆