2011-08-19 78 views
2

有單和多行註釋可用,像C.如何處理嵌套評論中FsLex

如何描述詞法分析器忽略所有的評論,甚至嵌套的,像這樣的規則:

// 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 } 

回答

1

當我和FsLex玩弄我發現Ocamllex Tutorial有很大的幫助,特別是nested comments部分很容易變成F#。

+0

謝謝你,我想,這就是我所需要的。 我會盡力分享結果。 –

+0

它的工作原理,再次感謝你:) –