"Use the source, Luke!"
If f2c can translate a bunch of Fortran expressions (a "Fortran program") to a bunch equivalent C expressions (a "C program"), f2c must contain code to convert a single Fortran expression into an equivalent C expression.
Either find and extract that, or construct a workaround: Wrap the Fortran expression in a dummy Fortran program that surrounds the expression in the C output with easy to find markers. Page 8 of the very verbose documentation suggests that Fortran labels may survive as C comments:
Unused labels are retained in comments for orienteering purposes.
So: Place an unused label "AAAAA" before the expression, a second unused label "ZZZZZ" after the expression, then search the generated C code for "/* AAAAA: */" and "/* ZZZZZ: */". Whatever you find in between should be the translated expression.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] [d/l] [select] |
I've experimented it with a bit, but apparently it's not that easy: f2c seems to make all sorts of assumptions about variables, functions and expected output, many of which turn out to be wrong (e.g. by default, it interprets the statement "a = foo(1,2)" as if foo were a function, so it turns that into "a = foo_(&c__1, &c__2)" which is useless for me). To coax it into generating usable output I'd have to declare variables with their correct dimensions and sizes and so on, so in the end I'd have to replace the original non-trivial problem with several more, even more non-trivial problems.
Extracting the expression parsing part from the source might be an avenue worth exploring, but then there is this comment in the linked pdf:
"The program f2c is a horror, based on ancient code and hacked unmercifully. Users are only supposed to look at its C output, not at its appalling inner workings."
| [reply] |
f2c seems to make all sorts of assumptions about variables, functions and expected output, many of which turn out to be wrong (e.g. by default, it interprets the statement "a = foo(1,2)" as if foo were a function, so it turns that into "a = foo_(&c__1, &c__2)"
I've used Fortran exactly twice, for "hello world" class programs, and that was about 20 years ago. But I think this is how Fortran actually works. If there is no matching array in the current scope, it must be a function call. And so on.
What is the context in which you need to parse / translate Fortran expressions? I.e. what problem are you actually trying to solve? My guess is that Fortran expressions should be quite rare outside a Fortran program, hence f2c.
"The program f2c is a horror, based on ancient code and hacked unmercifully. Users are only supposed to look at its C output, not at its appalling inner workings."
How desperate are you? Real world old code almost always looks ugly and has been hacked into a state where at least parts of it "just works, probably by pure magic". f2c looks quite small, so it should not be that hard to find and understand the relevant parts.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] |
Heh, that's thinking outside the particular box I happened to be in.
However, it's not really suitable after all, because it wants to translate complete programs, not just expressions.
| [reply] |
| [reply] [d/l] |