IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> [RegExp] Finding Fortran functions in a directory
Options
guerom00
post Oct 12 2006, 03:23 PM
Post #1


Apple Addict
*

Group: Members
Posts: 18
Joined: 29-September 06
Member No.: 86



Hi there,

Given a directory full of Fortran files, I want to list all the functions using grep. A Fortran function is declared as follow :

CODE
[type] function the_name(the_args,another_args,...)


where “[type]” is optional. To sum up, one has to match
1) the beginning of the line
2) any number of spaces
3) any characters up to a space
4) the word “function”
5) the name of the function
6) the opening brace

Steps 5) and 6) are here to avoid matchind the “end function” statement wink.gif

In Oniguruma, the following does the job :

CODE
(?ix)^\s*(.*)\s*\b(function)\b\s*[a-zA-Z0-9_]*\(


but I don't know which RegExp format is used by grep (damn : it does not seem to know “(.*)” blink.gif ) but the above expression does not work.

Any help would be much appreciated jap.gif

TIA
Go to the top of the page
 
+Quote Post
Philjfry
post Oct 18 2006, 12:57 PM
Post #2


Green Apple


Group: MacBidouille Team
Posts: 6
Joined: 29-September 06
From: Switzerland
Member No.: 83



Hi guerom00 !
Why dont you just
grep "function" | grep -v "end"
?
You might have to add stuff to weed out "function" written in comments.

This post has been edited by Philjfry: Oct 18 2006, 01:51 PM
Go to the top of the page
 
+Quote Post
guerom00
post Oct 18 2006, 01:21 PM
Post #3


Apple Addict
*

Group: Members
Posts: 18
Joined: 29-September 06
Member No.: 86



QUOTE (Philjfry @ Oct 18 2006, 01:57 PM) *
Hi guerom00 !
Why dont you just
grep "function" | grep -v "end"
?
You might have to add stuff out "function" written in comments.

Thanks Philjfry biggrin.gif Didn't know about the -v flag smile.gif

It appears that
CODE
grep "function [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end"
do what I want.

w00t.gif Here is the end result biggrin.gif
CODE
#!/usr/bin/env csh
echo "Finding tag "$2" in directory "$1
cd "$1"
if(-e .tags) goto exist
grep "subroutine [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end" > .tags
grep "function [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end" >> .tags
grep "entry [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end" >> .tags
echo >> .tags
set nlines=`cat .tags | wc -l`
@ nlines = $nlines - 1
if($nlines == 0) then
echo 'No functions or subroutine found'
exit 1
endif
set files=`cat .tags | cut -f 1 -d :`
set names=`cat .tags | cut -f 1 -d :`
set subfun=`cat .tags | cut -f 2 -d :`
rm .tags
touch .tags
set i=0
set j=0
while(1 < 2)
@ i ++
if("$subfun[$i]" == "subroutine" || "$subfun[$i]" == "function" || "$subfun[$i]" == "SUBROUTINE"\\
|| "$subfun[$i]" == "FUNCTION" || "$subfun[$i]" == "entry" || "$subfun[$i]" == "ENTRY") then
@ j ++
@ k = $i + 1
set names[$j]=`echo "$subfun[$k]" | cut -f 1 -d "("`
@ i = $k
echo "$files[$j]""|""$names[$j]" >> .tags
endif
if($j == $nlines) break
end
exist:
set file_name=`cat .tags | grep "|$2\b" | cut -f 1 -d "|"`
echo "opening "$file_name
if($file_name == '') exit
set line=`grep -n "subroutine $2" "$file_name" | grep -v "end" | cut -f 1 -d :`
echo 'line '$line
if($line == '') set line=`grep -n "function $2" "$file_name" | grep -v "end" | cut -f 1 -d :`
if($line == '') set line=`grep -n "entry $2" "$file_name" | grep -v "end" | cut -f 1 -d :`
echo $line
if($line != '') then
mate -l $line $file_name
else
mate $file_name
endif

An equivalent, not perfect, very ugly (tongue.gif) but which works not bad…, of the “etags” command and its integration with XEmacs, for TextMate smile.gif
I know : it could be more easy/beautiful/efficient using Ruby or Python but what can I do : I only know csh scripts biggrin.gif

Edo : The last refinement is
CODE
grep "^[^c\!].*function [A-Za-z0-9_]*(" *.f *.f90 | grep -v "end"
because I picked up some spurious things before…

This post has been edited by guerom00: Oct 18 2006, 10:38 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 26th July 2014 - 04:03 PM