Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Syntactic Confectionery Delight
 
PerlMonks  

how to automate the regular expression match from a file?

by vikashiiitdm (Novice)
on Apr 06, 2011 at 18:55 UTC ( #897856=perlquestion: print w/ replies, xml ) Need Help??
vikashiiitdm has asked for the wisdom of the Perl Monks concerning the following question:

hello everyone, i had been trying to make a lexical analyzer for c, where all the keywords were to be inputted in a text file keywords. this i stored in an array. but this process is not being automated. the code i wrote is as follows :-
#!/usr/bin/env perl @keywd=`cat /home/vikash/maya/keywords`; @operatr=`cat /home/vikash/maya/operators`; print "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> +>"; foreach $keyword (@keywd){print "$keyword";} print "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> +>"; print "\nthe total no. of keywords specified in the file are :($#keywd +)\n"; print "hello $ENV{'USER'}, greetings !!! you are "; print "using display $ENV{'DISPLAY'} \n"; $file='/home/vikash/maya/new.c'; print "\n\n:Lexical Analysis Report:\n\n"; print " |-> Input Code File:- $file\n"; print <<"EOF"; \nThe code used in the lexical analysis is :- \n EOF print `cat /home/vikash/maya/new.c`; for($i=0;$i<$#keywd;$i++) { print if /\b^int$\b/; } __END__
the keywords file contains:- auto continue enum if short break switch volatile default extern int signed typedef while case do float long sizeof union char double for register static unsigned const else goto return struct void the problem i'm having is that the keywords are not being taken automatically one after the another from this list of keywords............. if i do
open(INFO,$file); while(<INFO>){ print if /\bint\b/; print if /\bfloat\b/; } close(INFO)
everything's fine........ but i don't want to enter all these keywords one by one. what if in future i were to add a new keyword. i would simply like to modify the keywords file not the whole code........

Comment on how to automate the regular expression match from a file?
Select or Download Code
Re: how to automate the regular expression match from a file?
by toolic (Chancellor) on Apr 06, 2011 at 19:07 UTC
Re: how to automate the regular expression match from a file?
by wind (Priest) on Apr 06, 2011 at 19:29 UTC
    use Data::Dumper; use strict; my @keywords = qw(auto continue enum if short break switch volatile de +fault extern int signed typedef while case do float long sizeof union + char double for register static unsigned const else goto return stru +ct void); my $keywords_re = join '|', @keywords; my $file = '/home/vikash/maya/new.c'; open my $fh, $file or die $!; my %keywords; while (<$fh>) { while (m{\b($keywords_re)\b}g) { $keywords{$1}++; } } print Dumper(\%keywords);

      thanx for the code....but plz tell me why is this code not producing what i want. not a single keyword is displayed......

Re: how to automate the regular expression match from a file?
by eff_i_g (Curate) on Apr 06, 2011 at 19:37 UTC
    I recommend Regexp::Assemble for something like this. An example:
    use warnings; use strict; use Regexp::Assemble; my @keywords = qw( a an the then that this ); my $key_rea = Regexp::Assemble->new; $key_rea->add($_) for @keywords; my $key_re = $key_rea->re; my $data = do { undef $/; <DATA> }; print "$1\n" while $data =~ m/(\b$key_re\b)/g; __DATA__ hello everyone, i had been trying to make a lexical analyzer for c, wh +ere all the keywords were to be inputted in a text file keywords. thi +s i stored in an array. but this process is not being automated. the +code i wrote is as follows :- ... the keywords file contains:- auto continue enum if short break switch +volatile default extern int signed typedef while case do float long s +izeof union char double for register static unsigned const else goto +return struct void
Re: how to automate the regular expression match from a file?
by vikashiiitdm (Novice) on Apr 07, 2011 at 07:26 UTC

    thanx for the code , but plz explain me whatz the problem with the code i wrote.......no errors are displayed......but then no keywords are displayed either....

      For starters...
      use warnings; use strict;
      ...after your shebang line and work out any issues.

      Secondly, add print statements to see what is actually happening (within your loop, for example).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://897856]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-04-20 11:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls