Fortran Parser Question

Hassan Aurag aurag at geocities.com
Sun Apr 15 01:17:59 EDT 2001


Tim Roberts wrote:

> "Hassan Aurag" <aurag at geocities.com> wrote:
> >
> > I am trying to write a Fortran parser in python.
> >
> > What I need is really simple. Given a fortran file, get all variables
> >with their type and decompose arthmetic operations. Stuff like a = b+c
> >.... and check their types to see if those ops can be dangerous when
> >ported on various platforms.
>
> Mark is correct; this is a huge problem.  You basically need a full-fledged
> compiler.  For example, to do a complete job, you're going to have to parse
> statement functions and expand them when encountered.  IMPLICIT statements
> are another interesting twist.  Most of the simple parsers you can download
> are for Pascal-type languages.  Fortran's "rules" are quite different.
>
> Now that we've discouraged you, can you give me a concrete example of the
> kind of situation you want to detect?
>
> > So, I won't need a full fledged compiler, but really something simple. I
> >am new to regular exps and friends, so if anyone can help with something
> >simple I can start with, please drop me a note!
>
> Fortran is likely to defy analysis with regular expressions.  You're going
> to need a state-driven lexical analyzer.
>
> Have you done a web search for Fortran parsers?
> --
> - Tim Roberts, timr at probo.com
>   Providenza & Boekelheide, Inc.

 Ok, I will try to be clearer. But first thanks for your answers, but I
already discovered how fortran can be tricky.
I already encountered a case where I had something like AEBF := 2*WHATEVER.
Now that : was allowed as a character in a variable and of course you noticed
the damned space there.

 Now let's get down to business. I am not really interested in getting the
program flow. What I started to do is gather all variables found in
declarations like:

 INTEGER*4 FOO, BAR .....

 What I really want is to scan lines like the following:

 A = B+C * D/(F+2.0)**2

 The reason is that I am looking for specific cases where those vars are of
different type. To give you but a small example, INT1's are not the same on
all machines, and so I need to look for those operations involving int1's.
That's an example. I also need to check for stuff like:

 EQUIVALENCE(FOO,BAR)

 and try to follow the evolution of both vars in some very special cases.
Regular expression can help me get the assignment lines. But my real problem
is in dissecting them. For example the line A = ... above is easily obtainable
with regular expressions. I can even follow continued lines. But how do I
decompose those operations. My problem is how do I parse those? It can get
quite complicated eg:

 A = (.NOT.B .AND. (C.GE.D) .OR. F)

 where I need to catch the C.GE.D thinggy.

For the second tracking issue, I am not in that much of a hurry.  Maybe now
the whole issue will seem trivial to you.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: aurag.vcf
Type: text/x-vcard
Size: 249 bytes
Desc: Card for Hassan Aurag
URL: <http://mail.python.org/pipermail/python-list/attachments/20010415/9b41337d/attachment.vcf>


More information about the Python-list mailing list