4 ms·
Many mainstream languages don't actually have negative number literals. They lex `-42` as two tokens: the operator `-` and the integer literal `42`.
by Sharlin 7y ago
Many mainstream languages don't actually have negative number literals. They lex `-42` as two tokens: the operator `-` and the integer literal `42`.
- Doxin 7y agoThe unary minus operator is still different from the binary minus operator depending on context in languages that tackle it that way.
- Sharlin 7y agoYes, but that's a parsing-time distinction. The lexer does not need to care. And even though the token's syntactic meaning "depends on the context", the grammar itself can be (and usually is) perfectly context-free!
- Doxin 7y agoThat depends a lot on your parser. Personally if I write a parser I don't generally bother with having a separate tokenizing/lexing stage. Tokenizing happens as-needed and inline.
- JoeAltmaier 7y agoE.g. in an RPN calculator, one is '-' and the other is a different key 'CHS' for 'CHange Sign'