문제

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 }
도움이 되었습니까?

해결책

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#.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top