加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

用Perl编写的一个小的词法分析器

发布时间:2020-12-15 21:10:27 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++##@File : Lexical_Analyzer.pl#@Author: Roc#@Date : 2013/10/21#@Func

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
#	@File  : Lexical_Analyzer.pl
#	@Author: Roc
#	@Date  : 2013/10/21
#	@Func  : Lexical analyser
#	@Idea  :  In this program,we cut sentences to words.
#			  Then,we will match each word.
#
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#!/usr/bin/perl
use warnings;
use 5.010;

say "Please input your program here.(Input '__END__' to stop inputing)";

##
#input here
#
open (FH,">LexicalAnalyser.txt") or die "can't open the file: $!";
while (<STDIN>) {
	if (/__END__/){
		#Input over.Close file 
		close (FH) or die "Can't close file: $!";
		last;	#Interrupt here
	} else {	
		#debug
		#print "I saw $_";
		chomp ($_);	#delete 'Enter'
		say FH $_;

	}
} #End of while

##
#output program here.We just test our input.
#
$count = 1;
say "n+------------------------------------------------+";
say "tInput over.Here is your program.n";
#
#Read file here.
#We just want to see if we write into the file
#
open (FH,"<LexicalAnalyser.txt") or die "can't open the file: $!";
foreach (<FH>) {
	chomp;
	say "Line $count: $_";
	$count++;
}
say "+------------------------------------------------+n";
close (FH) or die "can't close file: $!";
#debug
#say "@Tmpn";


##
#Exchange special flags here.I just list a little:like,} { ) ( and ;
#
open (FH,"<LexicalAnalyser.txt") or die "can't open the file: $!";
foreach (<FH>){
	foreach (split){
		#debug
		#print "!!!!!!!!!!!!!!!!!!!!n";
		
		#We have already separated each word.
		#Thus,it doesn't matter if we add g here.
		s/(w+)(,|;|)|(|}|{)/$1 $2/g;
		s/(,|;|(|)|{|}|:|[|])(w+)/$1 $2/g;
		s/(;|(|)|{|})/ $1/g;
		#Store in Tmp2
		push @Tmp2,$_;
	}
}
close (FH) or die "can't close file: $!";


##
#We match here
#
foreach (@Tmp2){
	#debug
	#say "$_";
 foreach (split){
	 #debug
	 #print "!!!!!!!!!!!!!!!!!!!!n";
	 if ($_ =~ /main()/){
		 push @Tmp3,"(2,"main")";
     } elsif ($_ =~ /(int|float|for|while|do|return|break|continue|unsigned|public|void)/){
		 push @Tmp3,"(1,"$_")";
	 } elsif ($_ =~ /[a-z]+/){
		 push @Tmp3,"$_")";
	 } elsif ($_ =~ /p{Digit}.*/){
   		 push @Tmp3,"(3,"$_")";
     } elsif ($_ =~ /(=|>=|<=|!=|!|>|<|+|-|*|/)/){
		 push @Tmp3,"(4,"$_")";
	 } elsif ($_ =~ /((|)|{|}|;|,|:)/){
		 push @Tmp3,"(5,"$_")";
	 }
	 #debug
	 #print "$_n";
	# push @Tmp1,$_;    #再设置一个暂存区
  } #End of foreach(split)
} #End of foreach(@Tmp2)

##
#Output here
#
say "++++++++++++Final Answer+++++++++++";
say "-----------------------------------";
foreach (@Tmp3){
	say "$_";
}
say "+---------------------------------+";
say "+++++++++++++++END+++++++++++++++++";

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读