Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #141764

Re: [PC] Simple AST

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: [PC] Simple AST
Date 2019-03-06 22:12 -0800
Organization None to speak of
Message-ID <lnva0vkyb2.fsf@kst-u.example.com> (permalink)
References (4 earlier) <m2pnrm97mc.fsf@despina.home> <86ef7kufly.fsf@mailhub.linuxsc.com> <m27edb52f3.fsf@despina.home> <ln36nznb3r.fsf@kst-u.example.com> <m2wolb3kj4.fsf@despina.home>

Show all headers | View raw


"Pascal J. Bourguignon" <pjb@informatimago.com> writes:
> Keith Thompson <kst-u@mib.org> writes:
>> "Pascal J. Bourguignon" <pjb@informatimago.com> writes:
>> [...]
>>> We are not looking at landscape and scanning for predators here.
>>>
>>> We're looking at code that needs to give the human reader a conceptual
>>> structure isomorphe to what the "semantics" implemented by the language
>>> implementation produces.
>>>
>>>      a = b+c * d; // spaces fail all the time.
>>
>> So don't do that.  How often do you actually see code like that?
>>
>>     a = b + c*d;
>>
>> It would be nice if compilers warned about misleading spaces in
>> expressions (perhaps some do).
>
> Often enough.  But the worst part, is that I never encoutered any C
> programmer able to recite from memory the list of all C opertors sorted
> by their exact priority level.  Therefore not using systematically
> parentheses in any C expressions is a sure way to introduce slimy bugs.
>
> (I'm not able to do it for C either, when I programmed in pascal, I
> could do it for pascal operators).

If you want to argue that it's good to use parentheses to make
precedence explicit when using operators whose precedence a reader might
not reasonably be expected to know, I agree.  We might differ on which
operators those are, but for example I'd certainly parenthesize
    a | (b ^ c)
even though it's equivalent to
    a | b ^ c

If I saw
    a | b^c
I'd assume that the author *thinks* that ^ binds more tightly than | --
but I'd probably check that assumption, and consider adding parentheses
if I were in a position to do so.  If I saw
    a|b ^ c
I'd make a similar assumption and find that the author either was
mistaken or was being deliberately misleading.

But we were talking about
    a = b+c * d;
which is clearly misleading to anyone who knows the relative
precedence of + and * (and I'm confortable assuming that any
reasonably skilled C programmer does know that).  If I actually saw
that, I wouldn't assume that either changing the spacing or adding
the parentheses would fix the problem.  I'd have to consider that
there might be other mistakes.

But to get back to the point, I find this:
    a = b + c*d;
or even this:
    a = b + c * d;
more legible than what you suggested upthread:
    a=(b+(c*d));
I usually put spaces around binary operators, but I might omit them
to emphasize known precedence.

The possibility of adding spaces *and getting them wrong* is,
I believe, something of a straw man argument.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PC] Simple AST luser droog <luser.droog@gmail.com> - 2019-02-16 14:23 -0800
  Re: [PC] Simple AST BGB <cr88192@hotmail.com> - 2019-02-19 15:14 -0600
    Re: [PC] Simple AST luser droog <luser.droog@gmail.com> - 2019-02-19 19:51 -0800
      Re: [PC] Simple AST Rosario19 <Ros@invalid.invalid> - 2019-02-20 09:28 +0100
        Re: [PC] Simple AST "Pascal J. Bourguignon" <pjb@informatimago.com> - 2019-02-20 09:39 +0100
          Re: [PC] Simple AST Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-03-06 08:34 -0800
            Re: [PC] Simple AST "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-06 09:02 -0800
              Re: [PC] Simple AST "I Set My Girlfriend's Pantyhose on Fire" <cdalten@gmail.com> - 2019-03-06 11:14 -0800
              Re: [PC] Simple AST Ian Collins <ian-news@hotmail.com> - 2019-03-07 14:11 +1300
                Re: [PC] Simple AST "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-06 17:48 -0800
                Re: [PC] Simple AST Ian Collins <ian-news@hotmail.com> - 2019-03-07 15:21 +1300
                Re: [PC] Simple AST luser droog <luser.droog@gmail.com> - 2019-03-06 18:28 -0800
                Re: [PC] Simple AST luser droog <luser.droog@gmail.com> - 2019-03-06 18:37 -0800
                Re: [PC] Simple AST "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-06 18:47 -0800
                Re: [PC] Simple AST luser droog <luser.droog@gmail.com> - 2019-03-06 18:53 -0800
            Re: [PC] Simple AST "Pascal J. Bourguignon" <pjb@informatimago.com> - 2019-03-06 18:38 +0100
              Re: [PC] Simple AST Keith Thompson <kst-u@mib.org> - 2019-03-06 09:53 -0800
                Re: [PC] Simple AST "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-06 09:59 -0800
                Re: [PC] Simple AST "Pascal J. Bourguignon" <pjb@informatimago.com> - 2019-03-06 19:49 +0100
                Re: [PC] Simple AST Bart <bc@freeuk.com> - 2019-03-06 19:23 +0000
                Re: [PC] Simple AST David Brown <david.brown@hesbynett.no> - 2019-03-06 23:29 +0100
                Re: [PC] Simple AST BGB <cr88192@hotmail.com> - 2019-03-06 21:54 -0600
                Re: [PC] Simple AST James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-03-08 18:52 -0500
                Re: [PC] Simple AST "Pascal J. Bourguignon" <pjb@informatimago.com> - 2019-03-09 01:56 +0100
                Re: [PC] Simple AST Keith Thompson <kst-u@mib.org> - 2019-03-08 18:22 -0800
                Re: [PC] Simple AST David Brown <david.brown@hesbynett.no> - 2019-03-09 14:02 +0100
                Re: [PC] Simple AST Keith Thompson <kst-u@mib.org> - 2019-03-09 12:20 -0800
                Re: [PC] Simple AST David Brown <david.brown@hesbynett.no> - 2019-03-10 00:04 +0100
                Re: [PC] Simple AST Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-03-09 02:28 +0000
                Re: [PC] Simple AST BGB <cr88192@gmail.com> - 2019-03-08 23:06 -0600
                Re: [PC] Simple AST Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-03-12 23:06 -0700
                Re: [PC] Simple AST Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-03-14 06:48 +0000
                Re: [PC] Simple AST James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-03-14 07:14 -0400
                Re: [PC] Simple AST Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-03-14 14:58 +0000
                Re: [PC] Simple AST James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-03-14 10:13 -0700
                Re: [PC] Simple AST Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-03-18 08:34 -0700
                Re: [PC] Simple AST Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-03-23 10:57 -0700
                Re: [PC] Simple AST James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-03-25 11:42 -0700
                Re: [PC] Simple AST Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-03-18 07:18 -0700
                Re: [PC] Simple AST Bart <bc@freeuk.com> - 2019-03-14 15:34 +0000
                Divorcing rabbits with Tim R (Was: [PC] Simple AST) gazelle@shell.xmission.com (Kenny McCormack) - 2019-03-14 16:52 +0000
                Re: [PC] Simple AST Keith Thompson <kst-u@mib.org> - 2019-03-06 22:12 -0800
                Re: [PC] Simple AST David Brown <david.brown@hesbynett.no> - 2019-03-07 08:54 +0100
                Re: [PC] Simple AST "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-07 03:20 -0800
                Re: [PC] Simple AST Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-03-12 23:08 -0700
                Re: [PC] Simple AST luser droog <luser.droog@gmail.com> - 2019-03-06 14:08 -0800
                Re: [PC] Simple AST Keith Thompson <kst-u@mib.org> - 2019-03-06 22:13 -0800
              Re: [PC] Simple AST Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-03-06 19:38 +0000
                Re: [PC] Simple AST "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-06 11:54 -0800
                Re: [PC] Simple AST Anton Shepelev <anton.txt@g{oogle}mail.com> - 2019-03-07 14:43 +0300
                Re: [PC] Simple AST "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2019-03-07 03:58 -0800
                Re: [PC] Simple AST David Brown <david.brown@hesbynett.no> - 2019-03-07 13:07 +0100
        Re: [PC] Simple AST Rosario19 <Ros@invalid.invalid> - 2019-02-20 10:35 +0100
    Re: [PC] Simple AST Bart <bc@freeuk.com> - 2019-02-20 14:26 +0000
      Re: [PC] Simple AST BGB <cr88192@hotmail.com> - 2019-02-20 14:32 -0600
        Re: [PC] Simple AST "I Set My Girlfriend's Pantyhose on Fire" <cdalten@gmail.com> - 2019-02-21 13:38 -0800
        Re: [PC] Simple AST Thiago Adams <thiago.adams@gmail.com> - 2019-02-22 04:03 -0800

csiph-web