{"id":9758,"library":"fparser","title":"fparser","description":"fparser is a Python implementation of a Fortran parser that provides the ability to parse Fortran code (F77, Fortran 2003, 2008, 2018 standards) and represent it as an Abstract Syntax Tree (AST). This allows for static analysis, refactoring, or code generation. The current stable version is 0.2.2, with active development and regular releases.","status":"active","version":"0.2.2","language":"en","source_language":"en","source_url":"https://github.com/stfc/fparser","tags":["parser","fortran","AST","static analysis","compiler"],"install":[{"cmd":"pip install fparser","lang":"bash","label":"Install fparser"}],"dependencies":[],"imports":[{"note":"The Fortran standard parsers (e.g., Fortran2003, Fortran2008, Fortran2018) are located directly under 'fparser.two'.","wrong":"from fparser.common.Fortran2003 import Fortran2003","symbol":"Fortran2003","correct":"from fparser.two import Fortran2003"},{"note":"Functions for reading Fortran input (files or strings) are in 'fparser.common.readfortran'.","wrong":"from fparser.two.readfortran import read_file","symbol":"read_file","correct":"from fparser.common.readfortran import read_file"},{"note":"AST traversal utilities like 'walk' are found in 'fparser.two.utils'.","wrong":"from fparser.common.walk import walk","symbol":"walk","correct":"from fparser.two.utils import walk"},{"note":"fparser.one is an older, completely different API. All new development should use fparser.two.","wrong":"import fparser.one","symbol":"fparser.one","correct":"Use 'fparser.two' API instead"}],"quickstart":{"code":"import os\nfrom fparser.two import Fortran2003\nfrom fparser.two.utils import walk\nfrom fparser.common.readfortran import read_string\n\n# Example Fortran code as a string\nfortran_code = \"\"\"\nMODULE my_module\n  INTEGER, PARAMETER :: DP = KIND(1.0D0)\nCONTAINS\n  SUBROUTINE my_sub(a, b)\n    REAL(DP), INTENT(IN) :: a\n    REAL(DP), INTENT(OUT) :: b\n    b = a * 2.0_DP\n  END SUBROUTINE my_sub\nEND MODULE my_module\nPROGRAM main\n  USE my_module\n  IMPLICIT NONE\n  REAL(DP) :: x, y\n  x = 10.0_DP\n  CALL my_sub(x, y)\n  PRINT *, \"Result:\", y\nEND PROGRAM main\n\"\"\"\n\n# Read the Fortran code from a string\nreader = read_string(fortran_code, ignore_comments=True)\n\n# Parse the Fortran code using the Fortran 2003 standard parser\ntree = Fortran2003.parse(reader)\n\n# Walk the AST to find specific nodes, e.g., subroutine calls and variable declarations\nprint(\"Walking the AST to find subroutine calls and type declarations:\")\nfor node in walk(tree):\n    if isinstance(node, Fortran2003.Call_Stmt):\n        print(f\"  Found Call Statement: {node.items[0].name}\")\n    elif isinstance(node, Fortran2003.Declaration_Type_Spec):\n        # For simplicity, just printing the type spec itself. \n        # Inspecting parent/sibling nodes would reveal variable names.\n        print(f\"  Found Type Declaration: {node.items[0]} (raw: {node})\")\n\n# Example: Accessing specific parts of the tree (e.g., the program name)\nif tree.children and isinstance(tree.children[-1], Fortran2003.Main_Program):\n    program_name = tree.children[-1].get_name()\n    print(f\"\\nMain Program name: {program_name}\")\n\n# You can also regenerate the code from the AST\n# print(\"\\nRegenerated Fortran code:\")\n# print(tree.tofortran())","lang":"python","description":"This quickstart demonstrates how to parse a Fortran code string into an AST using the Fortran2003 parser, then walk the tree to identify call statements and type declarations. It also shows how to access specific AST nodes and hints at code regeneration."},"warnings":[{"fix":"Always use 'fparser.two' for parsing, e.g., `from fparser.two import Fortran2003`. Refer to the fparser.two documentation for the correct API.","message":"fparser underwent a complete rewrite for version 2 (fparser.two). The API for fparser.one is incompatible and should not be used for new projects.","severity":"breaking","affected_versions":"<=0.1.x (fparser.one) vs >=0.2.x (fparser.two)"},{"fix":"Choose the parser that matches or exceeds the standard used in your Fortran code. E.g., `from fparser.two import Fortran2018` for Fortran 2018 code.","message":"Selecting the correct Fortran standard parser (Fortran2003, Fortran2008, Fortran2018) is crucial. Using an older standard parser on modern Fortran code will likely result in parse errors or an incomplete AST.","severity":"gotcha","affected_versions":"All versions of fparser.two"},{"fix":"For file paths, use `from fparser.common.readfortran import read_file`. For strings, use `from fparser.common.readfortran import read_string`.","message":"Ensure you use the correct input reader function: `read_file` for physical files and `read_string` for in-memory Fortran code strings. Mixing them will lead to errors.","severity":"gotcha","affected_versions":"All versions of fparser.two"},{"fix":"Monitor memory usage for large projects. Optimize AST traversal algorithms by using specific visitors or limiting the scope of `walk` if possible.","message":"Parsing large Fortran files can be memory-intensive. For very large codebases, consider processing files incrementally or optimizing AST traversal.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"The 'fparser.one' API is completely different and no longer maintained. Update your code to use the 'fparser.two' API, starting with `from fparser.two import Fortran2003` (or Fortran2008/2018).","cause":"Attempting to import from the deprecated 'fparser.one' module after installing `fparser` version 0.2.x or later.","error":"ModuleNotFoundError: No module named 'fparser.one'"},{"fix":"Verify the Fortran code for syntax errors. If the code is valid, try using a newer Fortran standard parser, e.g., `from fparser.two import Fortran2018`, as your code might be using modern Fortran features.","cause":"The Fortran code provided is either syntactically incorrect, or the chosen Fortran standard parser (e.g., Fortran2003) cannot handle the features used in the code.","error":"fparser.two.parser.ParseError: Failed to parse input at line X, column Y"},{"fix":"Double-check the file path provided to `read_file()`. Ensure the file exists and the path is either absolute or correct relative to your current working directory. Use `os.path.exists()` for debugging.","cause":"The `read_file` function could not locate the specified Fortran file at the given path.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'your_fortran_file.f90'"},{"fix":"First, use `read_file()` or `read_string()` to create a reader object. Then pass this object to the parser: `reader = read_string(my_fortran_code); tree = Fortran2003.parse(reader)`.","cause":"The `parse()` method of Fortran standard classes expects a `reader` object (e.g., from `read_file` or `read_string`), not a raw string or file path.","error":"TypeError: parse() missing 1 required positional argument: 'reader'"}]}