Sujet : Re: how to make a macro work as a single line if stmt without braces
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 24. Sep 2024, 21:12:28
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcv6fa$3aom3$2@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Mozilla Thunderbird
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.
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:
if a then
else
if b then
else
if c then
else
fi
fi
fi
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).