SF3PRETTY 1L "29 April 1994" "University of Utah "

Table of contents


sf3pretty - SFTRAN3 and Fortran lexical analyzer


sf3pretty [-a] [-bx] [-cx] [-debug] [-d#] [-h] [-i#] [-k#,#,...] [-lkeystring] [-m] [-s#] [-ukeystring] <infile >outfile


sf3pretty is the beginning of a powerful SFTRAN3 program prettyprinter. Since SFTRAN3 is a superset of Fortran 66 and Fortran 77, it will handle Fortran code as well. Because it is based on a rigorous language grammar, it can do a correct job of prettyprinting, since every language token is recognized. Most other prettyprinters, particularly for Fortran, attempt only limited recognition of the input. sf3pretty at present has no facilities for putting statement numbers in order, changing precision, or regrouping FORMAT statements; use pretty(1L) for that job. Such features, and many others, will be added as it evolves. However, it can convert DEC-style tabs to blanks, regularize letter case and token spacing, and supply control structure indentation.

In particular, it can be applied to the output of pretty(1L) to supply indentation for Fortran 77 block-IF statements, which pretty(1L) presently does not recognize as candidates for indentation. It is at least 20 times faster than pretty (1L), and processes about 275 lines/sec on a Sun 3/50 (15MHz Motorola 68020) system, so it can be routinely applied to code without serious time penalties.

Following the conventions of DEC Fortran compilers, an initial tab on a line blank-fills through column 6 if the next character is not a digit, and otherwise fills through column 5, placing the following digit in column 6 as a continuation character. Tabs embedded in lines are expanded by blank padding through the next column which is a multiple of 8 (thus preserving layout in comments). The output contains no tab characters.

In order to preserve neat indentation according to control structure level, long character strings are split, preferably at a blank. In executable statements, the concatenation operator, //, is used to merge them back into one string (usually at compile time). In FORMAT statements, comma separators are inserted. In DATA statements, where string expressions are not permitted by Fortran 77, the break is made at column 72, and the same string continues in column 6 of the next line. Because some compilers (erroneously) fail to pad input statements to column 72 before merging continuation lines, trailing blanks on a line in such a split string can be lost. To prevent this, if column 72 is blank when a string is wrapped, a hyphen will be output in column 73; it will be ignored by the compiler, but will protect against erroneous string truncation. Otherwise, trailing blanks are trimmed from the output.


The lexical analyzer used by sf3pretty recognizes the complete Fortran 66 and 77 languages as defined in the ANSI Fortran X3.9-1966 and ANSI Fortran X3.9-1978 Standards. It also recognizes the SFTRAN3 language keywords and directives as described in the SFTRAN3 Programmer's Reference Manual by Charles L. Lawson and John A. Flynn (JPL Document No. 1846-98, 1978, Jet Propulsion Laboratory, Pasadena, CA).

To increase its utility, the analyzer also recognizes numerous language extensions present in commonly-used Fortran compilers. These are summarized in the following table. Note that some extensions are supported by multiple vendors.


<letter>=, Rw, and nVw.d FORMAT items, nRxxxx right-adjusted Hollerith constant, "xxx" quoted strings
n/ FORMAT item
ACCEPT, DO ... ENDDO, IMPLICIT NONE, INCLUDE, REREAD, and TYPE statements, E, F, G, I, Ow.d, Q, Zw.d, and $ FORMAT items, long variable names, $ and % in variable names, tabbed source format, lower-case source format
&nnn alternate return specifier, NAMELIST I/O
B, P, nR, SU, T, and nT FORMAT items


The order and letter case of command options is not significant; sf3pretty has been designed to run on multiple operating systems, most of which ignore letter case.
Display author and copyright information on stderr.
Set the comment character to x, which must be `*', `C', or `c'. If not given, the original comment characters are preserved.
Set the statement continuation character (for column 6) to x, which may be any character in the Fortran character set (upper-case letters, digits, space, = + - * ( ) / , . $ ' : ), except space or 0. The default is -cX.
Set optional space-deletion bits (see -s# below). A value -d0 removes internal defaults, so that input space will be exactly preserved in the output, except at the beginning of lines, which are indented according to the setting of the -i# switch. The default is -d0xffffffff, which will remove all whitespace around non-alphanumeric tokens (the default -s# switch setting restores appropriate spacing).
Convert Fortran 66 Hollerith strings (like 6HO'hara) to Fortran 77 quoted character strings (like 'O''hara').
Set the statement indentation value to #. Values in the range 2..5 are suggested. The default is -i5 if this switch is not specified.
Set the declaration columns to the indicated values; the defaults are equivalent to -k26,39,52,65 matching the output of the Extended PFORT Verifier (pfort(1L)) declaration file. Commas, colons, semicolons, or whitespace may separate the numbers. If whitespace separators are used, the argument must be quoted to prevent it from being interpreted as multiple arguments.

If a negative number appears in the list, then all columns from the preceding number to the absolute value of the negative one are declaration columns. The shorthand of, say, -k26,-72 says that any column in the range 26 through 72 is a declaration column; in other words, variables will appear successfully without intervening space.

Output in lower case the tokens of the type specified by the keystring value. The characters in the keystring may be in either letter case, and may occur in any order (even repeated). They may be one or more of the following:
constants (except quoted and Hollerith strings)
SFTRAN3 directives
FORMAT fields
Fortran INCLUDE statements
variables, subroutines, functions, and other user-defined names

Letter case of comments and character strings is never altered. By default, input letter case is preserved. However, -ucdfikov could be used to convert all but comments and strings to upper case (ANSI standard), and -lCDFKIOV would correspondingly convert to lower case. If both switches are given with the same letter options, the last one specified is used.

If no keystring is specified, CDFKOV is assumed; this selects all options except the one affecting Fortran INCLUDE statements. Because they contain file names whose letter case may be significant on some operating systems, they are never modified unless explicitly requested.
Display dynamic memory usage on stderr on termination.
The -d# and -s# switches set a group of bit flags for selecting spacing options. The -s switch selects space insertion, and the -d switch selects space deletion. Spacing is left unchanged for those tokens for which no option is given. The deletion values apply only to whitespace tokens present in the input stream; they do not remove space inserted by the spacing values.

The value is selected by or'ing together any of the following values, or by specifying the desired values in separate options, and it may be specified in decimal, octal (0nn), or hexadecimal (0xnnn):

+ -
* /
logical operators (.and., .eqv., .neqv., .not., .or., .xor.)
relational operators (.eq., .ge., .gt., .le., .lt., .ne.)
logical constants (.false., .true.)
SFTRAN3 structure labels
SFTRAN3 inline comments
concatenation operator (//)
exponentiation operator (**)
left parenthesis
right parenthesis
user-defined names

If a value 0 is specified, the internal defaults are cleared, so that original spacing will be preserved. The default value is -s0x183fb.

Specify token types to be converted to upper case (see -l above).

To support consistent prettyprinting style, it is convenient to be able to supply standardly-used options in a default startup file. This file is called .sf3pretty on UNIX systems, or sf3pretty.opt on others, and it is searched for in the current directory, or if not found there, in the login directory. If the file is found, up to 72 characters of its first line are read and parsed into command line options. These should not be protected by shell quotes; for example, the command line argument '-c*' should appear simply as -c*.


Standard input and standard output.
default option file
default option file


fortlex(1L), ftnchek(1L), pfort(1L), pretty(1L), sf3(1L), sf3lex(1L), sf3xref(1L), xsf3(1L).


Although a complete grammar for SFTRAN3 and Fortran 66/77 is implemented, with extensions from DEC, UNIX, Cray, CDC, and IBM Fortran, the constraints of doing this with a machine-generated lexical analyzer are that FORMAT is a reserved word. Use of the word FORMAT as a variable name may lead to incorrect output formatting; however, since no tokens are discarded, the output should still compile correctly.


Nelson H. F. Beebe
Center for Scientific Computing
Department of Mathematics
University of Utah
Salt Lake City, UT 84112

Tel: +1 801 581 5254 FAX: +1 801 581 4148

Email: beebe@math.utah.edu