Fortran keyword categories

I have been recently trying to improve the Doxygen Fortran documenter (which could hopefully be merged with the Doxygen main branch in the future). Amidst the process, there was a need to modernize and update the list of all Fortran intrinsic keywords. But doing it cleanly requires a clean classification of Fortran keywords. The Fortran WIKI page provides a good chronological summary of intrinsic keywords. But it does not divide them based on their functionalities. For example, some keywords are attributes, while others could be intrinsic functions and instruction keywords. Are there any resources (or anyone) that can help further categorize the Fortran keywords based on their purposes and functionalities?

UPDATE: Further web search resulted in this finding. Still it does not look comprehensive.

I don’t know if you can cleanly categorize the keywords that way. Most attributes can also be statements. REAL is both an intrinsic type and an intrinsic function. An easy way to build a list is to go through the index of the standard and look for entries in upper case that are followed by “statement”, “specifier”, etc…

2 Likes

@shahmoradi Did you visit the lexer for Fortran in pygments (link)? Their classification discerns statements, data types, operators, intrinsics, Booleans.

My knowledge about them is a bycatch of editing Bader’s draft about OOP in Fortran, specifically the different visual results of code blocks by Pandoc (version 3.3). The same block either labeled by f90 for instance recognizes CONTAINS as a keyword, while the label fortran presumes CONTAINS were only a comment. This is because (as outlined by John MacFarlane) Pandoc uses two distinct lexers derived from the KDE project: a fence labeled with fortran presumes fixed-form [hence FORTRAN77, my addition], while a label f90 presumes the free-form syntax [hence Fortran90+].

As test, I copy-pasted the snippet of the GitHub conversation into the discourse mask here. The present setup here does not yield a visible difference for either code fence label f90, or fortran (below with f90) while e.g., GitHub’s support seems incomplete.

TYPE :: sorted_list
   PRIVATE
   TYPE(sortable) :: data
   TYPE(sorted_list), POINTER :: next => null()
CONTAINS
   FINAL :: delete_sorted_list
END TYPE

An other resource may the listings LaTeX usepackage (default dialect Fortran 95, additional dialects supported in the package’s version 1.10b are Fortran 03, 08, 18, 77, 90).

Without using a language parser is that possible? Fortran does not have
reserved words except END in certain situations last I knew. It is common
practice to avoid using them, and most context highlighters and editors
do OK because of that but in the general case a lot of intrinsic names
are used as variable names in a lot of code in particular. After all,
this is a valid program:

program contains
real :: character=kind(0.0)
character(len=*),parameter:: fmt='(*(g0))'
   write(*,fmt=fmt)real(100.0)
contains
function real(in) result(program)
type(real),intent(in)   :: in
integer,parameter :: intent=kind(character)
real(kind=intent) :: program
result: associate (write=>program)
function: block
intrinsic :: real
real real
   write=real(in,kind=kind(in))**2
end block function
end associate result
end function real
end program contains

:laughing:

3 Likes

I raise you a hundred against this one:

program program

if = 0
then = 1
else = 2
endif = 3

if (if == then) then; then = else; else; else = endif; endif

read *, read
read = read + if + then

write = read + else + endif

print = read + write
print *, print
write (*, *) write

end program program

:laughing:

3 Likes

a perfect poetic proof of point!

Many old Fortran codes defined a variable sum, making it impossible to use the Fortran 90 sum intrinsic function in the same program unit. Are there tools that warn about such masking and rename such variables?

You can rename the intrinsic with a module, or put an intrinsic declaration in a
BLOCK construct. Not elegant but you can still get to the intrinsic. Would be nice if there were some other way like sum=intrinsic%sum(a,b,c) as that is an on-going issue as new intrinsics appear.

I know nvfortran warns about using intrinsic names; have seen other compilers warn about it. Do not know of a utility that does outside of the compilers myself.

Would be nice if there were some other way like sum=intrinsic%sum(a,b,c) as that is an on-going issue as new intrinsics appear. I am sure I use the variable names SORT and SPLIT which are intrinsic names as of f2023.

module rename
implicit none
intrinsic sum
end module rename

program summit
use rename, only : summation=>sum
implicit none
real :: sum
   sum=0.0
   sum=sum + summation([1.0,2.0,3.0,4.0,5.0])
   write(*,*)sum
   associate (sum_=>sum); block
      intrinsic sum
      sum_=sum_+sum([10.0,20.0,30.0,40.0])
   endblock; endassociate
   write(*,*)sum
end program summit

How easy is it to use different highlighters with Doxygen? LFortran does highlighting and actually parses the code so when installed it might be a good candidate. If Doxygen could (does it?) read AST there are some interesting possibilities.

No, I did not. I ended up doing what sblionel suggested above your comment, which led to this repository: GitHub - cdslaborg/FortranKeywords: This repository contains a complete list of all Fortran 2018 Standard keywords, including intrinsic procedure argument names (specifiers).
Glancing through pigments, I do not think their keywords list is complete. If I had enough time, I’d certainly create a pull request to expand the Pygments lexer for Fortran.

2 Likes