NOTE: I am by no means fluent in regular expressions, but I think this will work.
Provisos:
(1) It assumes that the 'real' or 'integer' statements will be properly ignored, either by having no spaces or commas preceding it in the string, or by explicitly junking anything that looks like a declaration.
(2) It assumes no continuation lines. (I leave that to you).
(3) I haven't tested it thoroughly with spaces etc.
Here it is.
Sandy#!/usr/bin/perl -w # # Scenario: Given lines of f77 code, # parse out the comma delimited var+iables. # use Data::Dumper; # A real application will read in f77 source, # this is just a sample. my $string = "real*8 Eparams(0:maxParam),". "Emvm(0:3),". "YourArray(12,(j,k),m),". "MyArray(row,col)"; my $open = 0; my $close = 0; my @fields = (); my $a; # --- get rid or 'real*8' $string =~ /(\w+)(\*[0-9]*)?/gc; # --- keep looping until entire string is processed while (not $string =~ /\G\z/gc) { # --- get variable name if ((not $open) and ($string =~ /\G(?:\s*|,)(\w+)/gc)) { push @fields, $1; print "$1\t"; } # --- find opening brackets, and keep track of level elsif ($string =~ /\G([^\)]*\()/gc) { $a = $1; $open++; } # --- find closing brackets, and print info elsif ($open and ($string =~/\G\s*([^\(]*\))/gc)) { print "-> $a$1\n"; $open--; } } #print "Fields:",Dumper(\@fields); exit(0);
In reply to Re: f77 variable list parsing
by Sandy
in thread f77 variable list parsing
by SlugMass
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |