Deutsch   English   Français   Italiano  
<vs34ot$3tfl0$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Integral types and own type definitions (was Re: Suggested method
 for returning a string from a C program?)
Date: Thu, 27 Mar 2025 10:11:57 +0100
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <vs34ot$3tfl0$1@dont-email.me>
References: <vrd77d$3nvtf$2@dont-email.me> <86r02roqdq.fsf@linuxsc.com>
 <vrh1br$35029$2@dont-email.me> <LRUCP.2$541.0@fx47.iad>
 <vrh71t$3be42$1@dont-email.me> <KFVCP.594649$SZca.498578@fx13.iad>
 <vrhb77$3frk8$1@dont-email.me> <vrru8f$174q6$1@dont-email.me>
 <86o6xpk8sn.fsf@linuxsc.com> <vrtmu4$2s1q2$1@dont-email.me>
 <20250325011327.41@kylheku.com> <20250325131110.000056bd@yahoo.com>
 <86bjtpjp22.fsf@linuxsc.com> <vruid4$3iuvq$1@dont-email.me>
 <87iknw7sz8.fsf@nosuchdomain.example.com> <vs0e6r$1avlb$2@dont-email.me>
 <87sen05cza.fsf@nosuchdomain.example.com> <vs0olv$1lodm$1@dont-email.me>
 <vs14p5$20ejf$1@dont-email.me> <87o6xn60qc.fsf@nosuchdomain.example.com>
 <vs1t0p$2m8ne$1@dont-email.me> <87zfh74exe.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 27 Mar 2025 10:12:00 +0100 (CET)
Injection-Info: dont-email.me; posting-host="b2d1c6626a8808d30934e2be192f8c1c";
	logging-data="4112032"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18BelEjG7FzkbhFAap61/b5Set90toh+Z8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
 Thunderbird/102.11.0
Cancel-Lock: sha1:rHpe7S/Yo5+68FIfARXKKKzs0OU=
Content-Language: en-GB
In-Reply-To: <87zfh74exe.fsf@nosuchdomain.example.com>
Bytes: 4249

On 26/03/2025 23:15, Keith Thompson wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
>> On 26.03.2025 20:38, Keith Thompson wrote:
> [...]
>>> If -5 were a single token (an integer constant), then 6-5 would be an
>>> integer constant 6 followed by an integer constant -5, a syntax error.
>>
>> This is generally not true; that depends on the syntax rules. It is
>> perfectly fine to have rules for 2-ary addition and subtraction and
>> have also negative (and positive) literals or expressions with unary
>> plus and minus operators.
>>
>> Disclaimer: As usual I'm not speaking about any peculiarity that "C"
>> may (or may not) have defined in its standard, but generally.
> 
> I suppose it would be possible to define a language syntax that makes
> -5 a single token and still parses 6-5 as (literal 6, binary-minus,
> literal-5).  

I'm sure a language could have syntax and parsing rules that interpreted 
the minus sign as binary minus in "x-y", unary minus in "-x", and part 
of the literal in "-5" if it is not preceded by an operand that has a 
suitable subtraction operation.  But I think the rules would get fairly 
complicated - much more so than the rules of C syntax and parsing.

It would be a lot easier in a language that required spaces to separate 
tokens.  Then it would be simple to distinguish "x - y" and "-5" - the 
binary subtraction operator is always space-minus-space, while in a 
literal, the minus sign has no space on its right.  "- 5" would then be 
parsed as unary minus applied to the literal "5", and "-x" would be a 
single token unrelated to "x".  This is how it is handled in Forth - 
there, "-5" is a single token for the literal value of minus five 
(unless some silly bugger has redefined -5 to something else...).  If 
you want to apply unary minus to 5, you write "5 NEGATE" (as Forth uses 
RPN notation).


> 
> I don't know of any language that uses "-" for both negation (prefix,
> one operand) and subtraction (infix, two operands) and treats -5
> as a single token rather than a unary minus operator applied to the
> constant/literal "5".
> 

For the most part, the important part of the grammar is the "constant 
expression" or "integer constant expression" - and you get that whether 
"-5" is parsed as one token (in a hypothetical C variant) or two tokens.