Suppose you got a long fortran file full of code, and want to parse it and get the subroutine/functions defined in the file along with its argument.
Here’s a simple python code to extract all this info in one go
import re from collections import defaultdict def processsubroutine(line): if ')' in line: return line.split("(")[1].split(')')[0].split(',') def collectsubroutines(fname): procnam=defaultdict() fort_proc_def = re.compile(r'\s*(RECURSIVE)?\s*(SUBROUTINE|FUNCTION)\s+\S+\(*', re.IGNORECASE) fp = open(fname,"r") datalines=fp.readlines() fp.close() for i,line in enumerate(datalines): if fort_proc_def.match(line): procnam[(line.split()[1].split('(')[0])]=processsubroutine(line) return procnam def main(): for key,item in procnam.items(): print key,item