# // 行释
# /* 块注释 */
use strict;
use warnings;
use Encode;
my $cnt=0; # counter
my $str;
my $char;
my @chars;
my $fsm = "text";
# text
# slash
# lc_comment
# bc_comment
# start
while (<DATA>)
{
#print "$_";
$cnt++;
$str = decode("gb2312", $_);
@chars = split //, $str;
#print "@chars";
foreach $char (@chars)
{
# print encode("gb2312", $char);
$char = encode("gb2312", $char);
unless ($fsm eq "text")
{
print "$char"; # comment
}
if($fsm eq "text")
{
if($char eq "/")
{
$fsm = "slash";
print "$char"; # comment
}
}
elsif($fsm eq "bc_comment")
{
if($char eq "*")
{
$fsm = "start";
}
}
elsif($fsm eq "lc_comment")
{
if($char eq "\n")
{
$fsm = "text";
}
}
elsif($fsm eq "slash")
{
if($char eq "/")
{
$fsm = "lc_comment";
}
elsif($char eq "*")
{
$fsm = "bc_comment";
}
else
{
$fsm = "text";
}
}
elsif($fsm eq "start")
{
if($char eq "/")
{
$fsm = "text";
}
else
{
$fsm = "bc_comment";
}
}
else
{
# fsm error
}
}
}
print "FSM:$fsm\n"; # comment-error if not "text"
__DATA__
行注释
// this is a line comment
// this is a line /* comment * /
块注释(只占一行)
/* this is a block comment, in one line. */
/* this is a block comment, // in one line. */
块注释(跨行)
/*
this is a block comment, in multi-line.
*/
块注释中的//属于注释内容
/* this is a
block comment, but
// not a line comment */
块注释中的/*属于注释内容
/* this is a
/* block
/* comment */
块注释不能嵌套,如下面是非法的块注释
/*
illegal comment, because
/* nest... */
ing
*/
void SetPwm(unsigned long m) // 这个行注释与代码在同一行,再与块注释符混合 /* 用regExp难以识别它。 */
{
printf("hi!");
}
|