Comment detail

/*コメント*/を取り除く (Nested Flatten)
ocamllex で。(fslex でも通る)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
rule proc = parse
| "/*" { skip lexbuf }
| _    { Lexing.lexeme_char lexbuf 0 }
| eof  { raise End_of_file }
and skip = parse
| "*/" { proc lexbuf }
| _    { skip lexbuf }
| eof  { raise End_of_file }

{
  let remove_comment src = 
    let s = Lexing.from_string src in
    let buf  = Buffer.create 256 in
    try 
      while true do Buffer.add_char buf (proc s) done; ""
    with End_of_file -> Buffer.contents buf;;

  print_endline (remove_comment "AAA");
  print_endline (remove_comment "AAA/*BBB*/");
  print_endline (remove_comment "AAA/*BBB");
  print_endline (remove_comment "AAA/*BBB*/CCC");
  print_endline (remove_comment "AAA/*BBB/*CCC*/DDD*/EEE");
  print_endline (remove_comment "AAA/a//*BB*B**/CCC");

}

Index

Feed

Other

Link

Pathtraq

loading...