Sujet : Re: question about linker
De : ike (at) *nospam* sdf.org (Ike Naar)
Groupes : comp.lang.cDate : 11. Dec 2024, 09:43:00
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <slrnvlik4j.ns4.ike@iceland.freeshell.org>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
User-Agent : slrn/1.0.3 (Patched for libcanlock3) (NetBSD)
On 2024-12-09, Janis Papanagnou <janis_papanagnou+
ng@hotmail.com> wrote:
An unambiguous grammar is something quite essential; how would you
parse code if it were ambiguous?
Here's an ambiguity in the C grammar:
<statement>:
...
<selection-statement>
...
<selection-statement>:
...
if ( <expression> ) <statement>
if ( <expression> ) <statement> else <statement>
...
The following selection-statement is grammatically ambiguous:
if (E1) if (E2) S1 else S2
it has two possible parsings:
if (E1) <statement> else S2
where <statement> expands to
if (E2) S1
or
if (E1) <statement>
where <statement> expands to
if (E2) S1 else S2
The grammatical ambiguity is resolved by an additional rule in the
'Semantics' section for selection-statement:
3 An else is associated with the lexically nearest preceding if that is
allowed by the syntax.
gcc -Wall will issue a warning for such an ambiguous statement:
warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]