Sujet : Re: Top 10 most common hard skills listed on resumes...
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.cDate : 01. Sep 2024, 00:37:49
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vb09gd$16mr5$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
User-Agent : Mozilla Thunderbird
On 31/08/2024 23:31, Keith Thompson wrote:
Bart <bc@freeuk.com> writes:
[...]
Given this:
>
x + y = z;
(x + y) = z;
>
My compiler produces the same AST for the LHS of '=' in both cases.
[...]
If that's the case (and I don't doubt that it is), then your compiler is
not following the grammar specified by the ISO C standard. Since
`x + y` is not a unary-expression, `x + y = z` is not a syntactically
valid assignment expression.
Yet no compiler out of the dozen I tried reported a syntax error (except SDCC).
So what AST is produced by gcc, if any?
Most compilers, including mine, complain that an lvalue is expected.
A parser that strictly follows the ISO C grammar would reject
(diagnose, flag, whatever) `x + y = z;` just as it would reject `x = y +;`.
Which one does that (apart from SDCC)?
This is an observation, not a complaint. It doesn't imply that your
compiler is non-conforming or buggy. A parser that doesn't strictly
follow the ISO C grammar could still be part of a conforming compiler.
I can also say that the C grammar is buggy:
assignment-expression:
conditional-expression
unary-expression asssignment-operator assignment-expression
When attempting to parse an assignment-expression, do you go for a conditional-expression or unary-expression?
The latter is a subset of the former. If you go for a conditional-expression and find that an assignment-operator follows, now you have to perform some analysis on the LHS to see if that conditional-expression contains only a unary-expression.
However, if it's not a unary-expression, it will fail for other reasons
anyway, because all those other things that a conditional-expression will be, can't be lvalues.
That also applies to many unary-expressions, such as 42, or a++; those can't be lvalues either, even though the syntax is valid.
So it makes sense to do only an lvalue test.