Article View: comp.arch
Article #169Re: Comment delimiters
From: EricP
Date: Sat, 17 Oct 2026 14:06
Date: Sat, 17 Oct 2026 14:06
72 lines
6311 bytes
6311 bytes
On 2026-Oct-16 13:56, Anton Ertl wrote:
> EricP <ThatWouldBeTelling@thevillage.com> writes:
>> >> There is a method called "Sale's Algorithm" which was published
>> in CACM in the 1960's which describes how to lex up to Fortran 77.
>
> Interesting, but did the FORTRAN team come up with that? According to
> <https://fortran-lang.discourse.group/t/has-anyone-played-with-sales-fortran-classification-algorithm-lately/7802>
> the algorithm was published in 1971.
I have a copy of a summary web page saved which seems to have disappeared
but this is what it said:
by Keith Seymour, 1998
"Sale's algorithm
Sale's algorithm (Giles, pers. communication) was published in CACM in the sixties - with an update for Fortran 77 republished in the eighties. Sale's algorithm requires a prepass of each statement to determine whether or not the statement begins with a keyword. The prepass is simple and can be done while scanning to remove comments and white space, and catenating continuation statements together, prior to normal lexical processing.
During the prepass, any characters in a Hollerith, character string (that is, between quotes or apostrophes), and any characters between matching parenthesis are ignored. Those characters are irrelevant to the purpose of the prepass. Of the remaining characters, the scanner must keep track of whether there are any equal signs (=) and whether there are any commas (,). Given the results of the prepass, the lexer should work according to the following rules, illustrated with examples.
If there was neither a comma nor an equal sign, the statement must begin with a keyword:
REAL X
No comma or equal sign, so a keyword must be first and lexer should find the REAL keyword;
FORMAT(I5,F10.3)
No comma or equal sign (none outside parenthesis), lexer should find keyword FORMAT.
If there was an equal sign, but not a comma, the statements must not begin with a keyword:
REAL X = 5
No comma, but an equal sign, so no keyword is allowed, lexer should find identifier REALX;
FORMAT(I5) = 7
An equal sign, lexer finds identifier FORMAT;
DO 10 I = 1.10
Famous, no comma but equal sign is present, identifier DO10I found.
If there was a comma (equal sign or not), the statement must begin with a keyword:
DO 10 I = 1, 10
Both comma and equal sign, keyword DO found.
However, there are still exceptions, This is resolved by collecting more information during the Sale's prepass. If the statement contains parenthetical lists or expressions, look at the first non-blank character after the close of the first parenthetical list/expression and record whether it's a letter. Now add two more rules to the lexer:
If there was a letter following a parenthetical, then the statement must begin with the keyword IF (very specific rule).
IF(LOGFLG) X = 5.0
Has an equal sign, but begins with keyword!
If there is such a letter, but no equal sign, the statement must end with the keyword THEN (another very specific rule).
IF(LOGFLG) THEN
No comma or equal, so IF keyword is found, but there is a second keyword.
Another lexically ambiguous situation occurs with the FUNCTION keyword. To resolve this, it must be known whether this is the first statement of a procedure, or whether it's a subsequent statement. If this is the first statement in the source, or if it's the first statement after an END statement, then the form with an identifier inside the parenthesis is a FUNCTION declaration. In all other circumstances, the statement declares an array.
This will always declare an array.
INTEGER FUNCTION A(5)
An array declaration FUNCTIONA of length 5 (this is not ambiguous).
If this is the first statement in the source, or if it's the first statement after an END statement, a function is declared.
INTEGER FUNCTION A(I)
Either an array or a function is declared.
Except for the keywords inside of an I/O control list (not implemented in f2j), these rules specify how to find all Fortran 77 keywords. Except for THEN and FUNCTION, all non-I/O keywords must be the first token of the statement they're in (with the caveat that they can be the first token of the sub-statement controlled by a logical IF - to which rules 1 to 3 still apply).
Sale's algorithm resolves many of the issues that are frequently cited as major difficulties of lexically scanning Fortran. The algorithm can be implemented during a prepass to quickly identify most of the information that would normally require lookahead when during lexing. From there on, FORTRAN can be quickly and efficiently parsed with the same compiler tools used for more modern languages. The algorithm is easy to code, fast, and the rest of Fortran's syntax is fairly regular once resolved in the lexer."
Technical Details
Message-ID:
<6iLAS.37515$1l6.36769@fx42.iad>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer04.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx42.iad.POSTED!not-for-mailReferences:
<1791742457-5857@newsgrouper.org> <11aig3c$3hdcn$1@dont-email.me> <11aiqc0$3kr94$3@dont-email.me> <179190779286.25645.14465927900754526737@media.vsta.org> <179190779286.25645.14465927900754526737@media.vsta.org> <vandys@vsta.org> <jwvjynl2cg0.fsf-monnier+comp.arch@gnu.org> <808vclt9i2mso89n1itvnvos5j88g1mopl@4ax.com> <6acfc2b2.1547984@news.eternal-september.org> <HZQzS.6$TYg.3@fx21.iad> <2026Oct15.081750@mips.complang.tuwien.ac.at> <9ysAS.182538$QPx3.16043@fx17.iad> <2026Oct16.195605@mips.complang.tuwien.ac.at>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer04.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx42.iad.POSTED!not-for-mail MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: EricP <ThatWouldBeTelling@thevillage.com> Subject: Re: Comment delimiters Newsgroups: comp.arch References: <1791742457-5857@newsgrouper.org> <11aig3c$3hdcn$1@dont-email.me> <11aiqc0$3kr94$3@dont-email.me> <179190779286.25645.14465927900754526737@media.vsta.org> <179190779286.25645.14465927900754526737@media.vsta.org> <vandys@vsta.org> <jwvjynl2cg0.fsf-monnier+comp.arch@gnu.org> <808vclt9i2mso89n1itvnvos5j88g1mopl@4ax.com> <6acfc2b2.1547984@news.eternal-september.org> <HZQzS.6$TYg.3@fx21.iad> <2026Oct15.081750@mips.complang.tuwien.ac.at> <9ysAS.182538$QPx3.16043@fx17.iad> <2026Oct16.195605@mips.complang.tuwien.ac.at> Content-Language: en-CA In-Reply-To: <2026Oct16.195605@mips.complang.tuwien.ac.at> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Lines: 72 Message-ID: <6iLAS.37515$1l6.36769@fx42.iad> X-Complaints-To: abuse@UsenetServer.com NNTP-Posting-Date: Sat, 17 Oct 2026 14:06:58 UTC Date: Sat, 17 Oct 2026 10:06:56 -0400 X-Received-Bytes: 6079