有单行和多行注释可用,就像在 C 中一样。
如何描述词法分析器忽略所有注释的规则,甚至是嵌套的,例如:
// comment /* nested comment /* and nested again? */ */
或像这些:
/* comment // one more comment /* and more... */ */
升级版:
这是解析嵌套注释的有效代码(感谢Sam):
rule token = parse
| "/*" { comments 0 lexbuf }
| [' ' '\t' '\n'] { token lexbuf }
| eof { raise End_of_file }
and comments level = parse
| "*/" {
if level = 0 then token lexbuf
else comments (level-1) lexbuf
}
| "/*" { comments (level+1) lexbuf }
| _ { comments level lexbuf }