Sujet : Re: how to make a macro work as a single line if stmt without braces
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 24. Sep 2024, 23:36:53
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240924132256.682@kylheku.com>
References : 1 2 3 4 5 6 7 8 9 10 11
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-09-24, Bart <
bc@freeuk.com> wrote:
On 24/09/2024 20:59, Kaz Kylheku wrote:
On 2024-09-24, Bart <bc@freeuk.com> wrote:
(Maybe the language needs a construct like 'elif', as is used in
The language doesn't need such a construct in order for a compiler
to implement it internally.
You just have to treat the token sequence else if as a supertoken.
Doing it at the grammar level may require two symbols of lookahead,
which is a a problem for off-the-shelf parser generation tooling based
on LALR(1) and whatnot.
>
That sounds as much of a hack as making a special case for 'else if' so
that it doesn't need 'else {if' (and a matching } later on) when
enforcing compound statements for such blocks.
It's a hack, but you can completely hide it at the grammar level; you
can write your grammar with the assumption that there is an ELIF token,
so that the syntax is identical to that found in languages that have
elif or elsif. Just the lexical level, it is spelled by two words.
One consequence of how it works now is that if I write:
>
if (a) {}
else if (b) {}
else if (c) {}
else {}
>
then use a tool I have to visualise C code in my syntax, it generates:
It behooves C processing tools to grok "else if" where it makes sense.
if a then
else
if b then
else
if c then
else
fi
fi
fi
Note that in this type of language, an "elif" is helpful, because
every "else if" introduces an "if", which must be matched by its own
"fi", whereas an "elif" doesn't require its own closing keyword.
In other words, "elif" is more than just a contraction for "else if"; it
eliminates "fi".
In the preprocessor, it is different; there is an #endif.
If the above language has elif, then a conversion can be applied
by the rewrite rule
{if/elif} A then B else if C then [...] fi fi
->
{if/elif} A then B elif C then [...] fi
this can be repeatedly applied until there is no match for the
left hand side.
The nested nature is revealed. If there were 50 'else if' links, the
output would disappear off to the right.
>
With elif, elsif etc, the compiler has the option to keep the internal
structure linear (which also means it will not put pressure on the stack
if somebody decides to write a million of them).
The option to keep the AST structure linear is there regardless,
as I pointed out; compilers writers can easily pretend that "else if"
is special.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca