如何处理 FsLex 中的嵌套注释
有单行和多行注释可用,就像在 C 中一样。
如何描述词法分析器忽略所有注释(甚至是嵌套注释)的规则,例如:
// comment /* nested comment /* and nested again? */ */
或者像这些:
/* comment // one more comment /* and more... */ */
UPD:
这里是解析嵌套注释的有效代码(感谢 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 }
There are single and multi-line comments available, like in C.
How to describe the rules for the lexer to ignore all the comments, even nested, such as these:
// comment /* nested comment /* and nested again? */ */
or like these:
/* comment // one more comment /* and more... */ */
UPD:
Here is the valid code to parse nested comments(thanks 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 }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我在玩 FsLex 时,我发现 Ocamllex 教程 有很大帮助,特别是嵌套评论部分是轻松更改为 F#。
When I was playing around with FsLex I found the Ocamllex Tutorial a great help, in particular the nested comments section was easy to change into F#.