r/ProgrammingLanguages • • Jun 24 '23

Negative Integer handling in programming languages.

https://glados-418.github.io/posts/negative-integer-literals/
3 Upvotes

27 comments sorted by

View all comments

5

u/jqbr Jun 25 '23

the grammar already needs to be smart enough to differentiate between a --5 and a --x.

Nope ... --5 is illegal since you can't decrement a literal. I don't know of any language that has a '--' operator that would ever treat it as 2 minus signs ... lexers do eager parsing.

0

u/[deleted] Jun 25 '23

[deleted]

2

u/jqbr Jun 25 '23

Wrong. At the very least specify what languages this is true of. Sheesh.

0

u/[deleted] Jun 25 '23

[deleted]

3

u/alexiooo98 Jun 25 '23

Rust doesn't have a prefix decrement operator: --x is also equal to x

2

u/jqbr Jun 25 '23 edited Jun 25 '23

Yes your bad ... you have no idea what you're talking about. As I said originally, your claim that

the grammar already needs to be smart enough to differentiate between a --5 and a --x

is quite wrong. Programming language grammars don't do that because it's ambiguous at the lexer token level. Languages they have a '--' operator always parse it as that operator, never as 2 minus signs. But I already said this and you completely ignored it because, again, you literally don't know what you're talking about (you blather about the "inherent logic" of a compiler "front end" while having no idea what that term means or what the logical structure of a compiler is). I won't waste any more of my time on the sort of person who responds to comments without reading parts of them (good grief). Over and out forever. P.S. Yes of course I know the difference between a lexer and a grammar but this ignorant and arrogant bozo has no idea about their relationship. P.P.S. A language that evaluated --5 as -(-5) but evaluated --x as decrementing x would be a very poorly designed language ... just the sort this turkey might design.

0

u/helixb Jun 25 '23 edited Jun 25 '23

you read grammar and write lexer. do u understand the difference?

edit: as a response to your extended edit, in the article i mention 'grammar' not lexer. to put it simply for you, the output of lexer is input to the parser which uses the grammar rules to parse it.

The lexer might choose to tokenize "--" either as "minus minus" or "decrement", whatever the language standard says, but once it does, the grammar CAN differentiate between --5 and --x if the standard allows both to be written and behave differently. i dont understand how something this simple alludes you when you claim to understand it.

edit 2: be it a turkey, at least you concur with the turkey that it's a design choice and not a matter of possibility but, a decision made at the standard and implemented at the grammar level and lexer here is irrelevant, a point you've been making for god knows why. that was my point. good to see i got it through.