skip to navigation
skip to content

open-fortran-parser 0.3.0

Python wrapper for XML output generator for Open Fortran Parser

Latest Version: 0.5.2

XML output generator for Open Fortran Parser

Implementation has 2 parts: the XML generator written in Java, and Python wrapper for the generator.

The implementation is tested on Linux, OS X and Windows.

In this file, first the Java implementation is described and then the Python wrapper.

Java XML generator for OFP

This is an extension of Open Fortran Parser (OFP), which outputs abstract syntaxt tree (AST) of parsed Fortran file in XML format - to a file or to System.out.


how to build

Get dependencies, either manually, or using the provided script:

pip3 install -U -r requirements.txt
python3 -m open_fortran_parser --dev-deps
export CLASSPATH="${CLASSPATH}:$(pwd)/lib/*"


export CLASSPATH="${CLASSPATH}:$(pwd)/dist/*"

This will create a .jar file in dist directory.

how to run

java fortran.ofp.FrontEnd --class fortran.ofp.XMLPrinter \
  --output output.xml --verbosity 0~100 input.f


  • The --verbosity flag controls verbosity of the parse tree. Defaluts to 100 when omitted.
    • Maximum, 100, means that all details picked up by Open Fortran Parser will be preserved.
    • Minimum, 0, means that tree will contain only what is needed to reconstruct the program without changing it’s meaning.
  • The --output flag controls where the XML should be written. Defaults to standard output when omitted.

and remaining command-line options are exactly as defined in OFP 0.8.4.

To parse some_fortran_file.f and save XML output in tree.xml with minimum verbosity:

java fortran.ofp.FrontEnd --class fortran.ofp.XMLPrinter \
  --output tree.xml --verbosity 0 some_fortran_file.f

And to dump XML with maximum verbosity to console:

java fortran.ofp.FrontEnd --class fortran.ofp.XMLPrinter \
  --verbosity 100 some_fortran_file.f

AST specification

Root node is <ofp>, it has one subnode <file>.

Inside the <file>, there might be one or many of the following nodes:

  • <program>
  • <subroutine>
  • <module>
  • <interface>

Each of which has <header> and <body>. Additionally, <module> has <members>.

In the body, a special node <specification>, followed by a collection of statements can be found.

The <specification> contains a collection of <declaraion> nodes.

And, each of the statements listed after the specification, can be either compound or simple.

Compound statements, e.g.:

  • <if>
  • <loop>
  • <select>

each have <header> and <body>.

In the header of the <loop>, at least one <index-variable> is present. It has <lower-bound>, <upper-bound> and <step>.

In the header of <if>, an expression is present.

Expressions are built from the <operation> nodes, each of which contains a collection of <operand> and <operator> nodes. Each operand can be also an expression, or a simple node like:

  • <name>
  • <literal>

All simple statements are using <statement> node, which wraps around nodes like:

  • <assignment>
  • <call>
  • <open>
  • <close>
  • <write>
  • <return>
  • <stop>
  • <continue>
  • <cycle>

Remaining details of AST are not decided yet. For the time being, to see implementation details, please take a look into src/fortran/ofp/

Python wrapper for the generator

Using the wrapper should not require any special knowledge about the generator itself, other than knowing the abstract syntax tree (AST) specification.


Java XML generator for OFP and all of its dependencies.

Python version >= 3.6.

Python libraries as specified in requirements.txt.

Building and running tests additionally requires packages listed in test_requirements.txt.

how to build

pip3 install -U -r test_requirements.txt
python3 sdist --formats=gztar,zip
python3 bdist_wheel

how to install

You can simply install from PyPI:

pip3 install open_fortran parser

Or using any of below commands, when installing from source:

pip3 install .
pip3 install dist/<filename>.whl
pip3 install dist/<filename>.tar.gz
pip3 install dist/<filename>.zip

how to run

The wrapper can be used as a script, or as a library.

When running any installed version, even if installed from source, dependencies are automatically installed together with the wrapper.

Before running from source (without installation), however, please follow “how to build” section for Java implementation above. You can make sure that dependencies are configured correctly by running:

python3 -m open_fortran_parser --deps

as script

$ python3 -m open_fortran_parser -h
usage: open_fortran_parser [-h] [--version] [-v VERBOSITY]
                           [input] [output]

Python wrapper around XML generator for Open Fortran Parser

positional arguments:
  input                 path to Fortran source code file (default: None)
  output                writable path for where to store resulting XML,
                        defaults to stdout if no path provided (default: None)

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v VERBOSITY, --verbosity VERBOSITY
                        level of verbosity, from 0 to 100 (default: 100)
  --get-dependencies, --deps
                        download dependencies and exit (default: False)

Copyright 2017 Mateusz Bysiek Apache License 2.0

as library

from open_fortran_parser import parse

xml = parse('my_legacy_code.f', verbosity=0)


python3 -m pylint --load-plugins=pylint.extensions.mccabe --docstring-min-length 5 \
  --no-docstring-rgx "^(test)?_|.*Tests$" --unsafe-load-any-extension y \
  --output-format colorized  --reports y $(find . -name "*.py")
python3 -m coverage run --branch --source . -m unittest discover --verbose
python3 -m coverage report --show-missing
python3 -m coverage html
File Type Py Version Uploaded on Size
open-fortran-parser-0.3.0.tar.gz (md5) Source 2017-10-13 3MB
open_fortran_parser-0.3.0-py3-none-any.whl (md5) Python Wheel py3 2017-10-13 3MB