Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: please help me to resolve the Line comments and appending issue

by suno (Acolyte)
on Aug 09, 2012 at 07:48 UTC ( [id://986435]=note: print w/replies, xml ) Need Help??


in reply to Re: please help me to resolve the Line comments and appending issue
in thread please help me to resolve the Line comments and appending issue

Hi, perhaps i am working on a complex task... my actual source is : The following is my actual input.
*** *************************************************************** ** +* *** THE FOLLOWING IS THE SAMPLE CODE ** +* *** -------------------- ** +* *** ** +* *** ** +* *** ALL COMMENTS,LINE COMMENTS AND LINE SPACES HAS TO BE REMOVED ** +* *** ** +* *** ** +* *** ** +* *** ** +* *** *************************************************************** ** +* + + BEGIN EQU * + L R1,0(R1) LOAD REGISTER R1 TO R1 + CLI R3,0(,R1) BNE R4,2(,R1) LTR R3,R3 REGISTER R3 > 0? + @PUT FFILE,ADR=ZEILE MACRO + TKT100 DC x'0300' TG-ID + TKT300 DC X'000000' SYSTEMZEIT + JSKDAT DS XL4 DATUM FUER JAHRESSCHLUSSKURS + + KURSDA DS XL4 KURS-DATUM 30.06.XXXX

my requirements are: generate 3 files 1)variable file , 2)code file ,3)macro file things has to be done: 1) in VARIABLE FILE: in statement

"KURSDA   DS    XL4'1000'   KURS-DATUM 30.06.XXXX " i have to move the following values to the variable file...
vairable length datatype value description + KURSDA 4 X 1000 KURS-DATUM 30.06.XX +XX
2)in CODE FILE: here i have to modify the code: 1)remove line comments 2)remove "EQU *" from the code 3)append statement starting with BNE to the previous statement starting with CLI 4)all statements with DC,DS and EQU should be removed the final output should look like:
BEGIN + L R1,0(R1) CLI R3,0(,R1) BNE R4,2(,R1) LTR R3,R3 @PUT FFILE,ADR=ZEILE

3)in MACRO FILE: and L,CLI,BNE,LTR,DC,DS all are keywords... i need to capture all other than keywords and push to the variable file. foe example: "@PUT  FFILE,ADR=ZEILE            MACRO   " here @PUT is not a keyword. so that should be pushed to the variable file and the next argument under the column value and the 3rd argument"MACRO" under description..that is,it should look like,

vairable value description @PUT FFILE,ADR=ZEILE MACRO

Replies are listed 'Best First'.
Re^3: please help me to resolve the Line comments and appending issue
by aitap (Curate) on Aug 09, 2012 at 10:43 UTC
    Here is the code based on your example:
    #!/usr/bin/perl use warnings; use strict; use Getopt::Std; my %options; getopt("ivcm",\%options); do { die "Usage: $0 -i <input file> -v <variable file> -c <code file> -m < +macro file>\n" unless defined $options{$_} } for qw/i v c m/; open my $input, "<", $options{i} || die "$options{i}: $!\n"; open my $code, ">", $options{c} || die "$options{c}: $!\n"; open my $var, ">", $options{v} || die "$options{v}: $!\n"; open my $macro, ">", $options{m} || die "$options{m}: $!\n"; while (defined(my $line = <$input>)) { chomp $line; next if $line=~/^\s*$/; next if $line=~/^\s*\*/; $line=~s/EQU\s+\*//; $line=~s/\s*$//; my @fields = split /\s+/,$line,4; shift @fields until $fields[0] ne ''; if ($fields[0] eq 'BEGIN') { print $code join ("\t",@fields),"\n"; } elsif ($fields[0] =~ /^(BEGIN|L|CLI|BNE|LTR)$/) { #code pop @fields; print $code (join "\t",@fields),($fields[0] eq 'CLI' ? "" : "\ +n"); } elsif ($fields[0] =~ /^@/) { # macro print $macro (join "\t",@fields),"\n"; pop @fields; print $code (join "\t",@fields),"\n"; } elsif ($fields[1] =~ /^D[CS]$/) { #variable? my ($len) = ($fields[2] =~ /L(\d+)/); #??? my ($type) = $fields[2] =~ /^(.)/; # ??? my ($value) = ($fields[2] =~ /'(\d+)'$/); #??? print $var (join "\t",$fields[0],$len,$type,$value,$fields[3]) +,"\n"; } else { warn "Don't know what to do with line\n$line\n"; } } close $_ || die $! for ($input,$code,$var,$macro);
    It sure contains some mistakes because I had to guess some things about CLI, BNE and variables. Feel free to fix it. If you would state more clearly how to distinguish between code and variables, it will be easier to write the proper code.
    See split, join, shift, pop for more info on this script.
    Sorry if my advice was wrong.
      hi, ...

      i am not still clear with the macro file..

      my requirement is :

      I have hardcoded all keywords to an array(keywords come only at the begining of the sentence).I want to capture all words other than the keywords and push them to the macro file.

      So i thought of comparing each word in my input file(only the first word) with the keyword array and if it is not present in the array then push it to the macro file..

      in the above example : CLI is a keyword ...where else @PUT is a macro

      but i couldnt able to figure it out clearly.Can you please help me out in this ?

        Array search can be done using grep, but if you want just to check whether an item exists in the list, using hash is faster:
        my %keywords = map { $_ => 1 } qw/L CLI BNE LTR .../; ... #later in the code if (exists $keywords{$whatever_is_suspected_to_be_a_keyword}) { # it is a keyword ... } ...
        Sorry if my advice was wrong.
Re^3: please help me to resolve the Line comments and appending issue
by suno (Acolyte) on Aug 09, 2012 at 08:43 UTC
    little correctionin the 3rd point: 3)in MACRO FILE: and L,CLI,BNE,LTR,DC,DS all are keywords... i need to capture all other than keywords and push to the macro file. foe example: "@PUT FFILE,ADR=ZEILE MACRO " here @PUT is not a keyword. so that should be pushed to the macro file and the next argument under the column value and the 3rd argument"MACRO" under description..that is,it should look like,
    vairable value description @PUT FFILE,ADR=ZEILE MACRO

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://986435]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found