🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #156

Re: Comment delimiters

#156
From: Thomas Koenig
Date: Fri, 16 Oct 2026 20:35
117 lines
7193 bytes
EricP <ThatWouldBeTelling@thevillage.com> schrieb:
> On 2026-Oct-15 02:17, Anton Ertl wrote:
>> EricP <ThatWouldBeTelling@thevillage.com> writes:
>>> Old FORTRAN is difficult to parse because
>>> a) it did not separate the language into lexical and syntax levels
>>> b) it was white space oblivious except in quoted strings
>>> c) there were no reserved words - keywords were statement
>>>     position and sometimes semantic context dependent.
>>> d) statements were based on punch cards BUT because
>>>     continuation cards used the absence of a C in column 6
>>>     to indicate the start of a new statement, parsers have to
>>>     always look ahead to see in the next card has a C and
>>>     then "unget" the card if not.
>> 
>> Given that the original FORTRAN worked on an IBM 704, I think that the
>> designers made it as easy (and with as little memory) to parse as they
>> could, given the state of the art at the time.  Concerning the points:
>> 
>> a) I guess that the division into lexical and syntax levels had not
>>     been developed at the time; and even later, e.g., Wirth does not
>>     make this distinction in his language definitions, using EBNF down
>>     to the lexical level (although he does define reserved words).
>> 
>> b) Without this distinction, if you ignore white space at all, you
>>     either ignore it everywhere, or give it a special role in your
>>     syntax.  Ignoring is probably simpler.
>> 
>> c) The programming language community took quite a while until it
>>     arrived at reserved words.  E.g., while Algol 60 was specified in
>>     BNF, being modern in that regard, it does not have reserved words,
>>     just symbols that are distinct from all identifiers; i.e., you
>>     could have an identifier like IF in Algol 60, distinct from the
>>     symbol 'IF', and it's up to the implementation how to distinguish
>>     them.  Anyway, without a) reserved words make no sense.
>> 
>> d) Does the original FORTRAN support continuation cards?
>
> Yes. Fortran's continuation card is LL(1) and behaves the same as
> an ISA with start bits - you have to look ahead to find out
> that you didn't need to look ahead, and unget the card.
>
> Statement terminators like ; act like ISA stop bits and don't need lookahead.
>
>>> The classic ambiguous parsing example being
>>>
>>>    DO I = 1,100
>>>    DOI = 1.100
>>>
>>> depends on the presence or absence of a "naked comma" later in the
>>> statement to decide whether there is a keyword DO and a variable I,
>>> or a variable DOI. A "naked comma" is a comma not enclosed inside
>>> a quoted character or string, or inside a possibly nested parenthesis.
>> 
>> My guess as to how this stuff was parsed:
>> 
>> Deal with the first columns as appropriate.  For the rest: Remove any
>> white space.  Use a backtracking parser that tries to match a rule at
>> a time; for the DO statement, simply compare the start with "DO".  If
>> a rule does not match, look at the next rule.
>> 
>> The result is messy, especially when you want to extend the language.
>
> A backtracking parser sounds like way overkill.

I haven't worked on a Fortran 77 compiler, but gfortran, a Fortran
20xx compiler, does exactly that.

It tries matching constructs until they are accepted.  If somethig
does not match, an error message is queued, and the next
match is tried.  If everything failes, then the last error message
is output, and the next statement is looked at.

But that is only a part.  After the AST has been generated, a
lot of checks are run, and details are filled in (the so-called
resolution stage).  Then, some manipulation of the AST and finally
transfer to gcc's GENERIC, a C-like intermediate language.  This is
where things like scalarization are handled.  GCC later lowers the
GENERIC to GIMPLE, which is an SSA variant.

> I am under the impression that many Fortran 77 compilers used
> hand written lexer and a recursive decent parser.
> The parser passes some local context to the lexer to let it know,
> for example, which set of keywords are active at that point.
> Anyway, that is how I would do it.

I think John L. wrote a FORTRAN compiler once.

>
> The first thing a Fortran compiler does is scan ahead loading card
> statements into a card buffer array from the (possibly nested INCLUDE)
> source files until it sees a non-continue card, which it ungets.
>
> Then it checks the format of the cards is ok and extracts things like
> a label from columns 2:5.
>
> Compacting out comments and white space is unnecessary -
> the hand written lexer knows to skip over them.
> 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.
>
> The parser knows which statement it is working on so can tell the
> lexer what options it should handle at the point.
> There could be a post pass over the AST to patch up ambiguous
> constructs that require semantic context to fix.
>
> After that it is pretty standard AST processing.
> The only semantic difference I can think of immediately is
> Fortran's parenthesis rule that forces an order of evaluation
> and would interact with optimizations like common subexpressions.

The gfortran front end has INTRINSIC_PARENTHESES , which it then
translates into PAREN_EXPR in the intermediate language.

-- 
This USENET posting was made without artificial intelligence,
artificial impertinence, artificial arrogance, artificial stupidity,
artificial flavorings or artificial colorants.
Technical Details
Message-ID: <11au1ql$3ks5m$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <1791742457-5857@newsgrouper.org> <11agm4p$2tfut$1@dont-email.me> <11ahs8q$38ir0$4@dont-email.me> <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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Thomas Koenig <tkoenig@netcologne.de>
Newsgroups: comp.arch
Subject: Re: Comment delimiters
Date: Fri, 16 Oct 2026 20:35:33 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 117
Message-ID: <11au1ql$3ks5m$1@dont-email.me>
References: <1791742457-5857@newsgrouper.org>
 <11agm4p$2tfut$1@dont-email.me> <11ahs8q$38ir0$4@dont-email.me>
 <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>
Injection-Date: Fri, 16 Oct 2026 20:35:34 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3829942"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18VFMxz+dUw2kwYJAxu4l0qli+9Zmz7Yt4="; posting-host="30fac5b319aa2fbebe3c08be7bd4d749"
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:IA8QK1IymMxFKwabzDM7NTG3yUA= sha256:PRApVIJ48eGi4a0eT9YXDiK8K5sEgea0jZ57vAi0gBQ=
	sha1:zKsaFCfVIF82r80wjMhnJKOjnqM= sha256:sylDaKegJd4vPLCb6dj4pDAaJAc8/XgwJaJJVJo3NhE=