"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote:What would you guess the value of b is after those three lines execute? Add a line to print it out (actually, print out all three, a, b, and c). The output is going to surprise you.
Stephan van Hulst wrote:+++ is ambiguous because it might mean + ++ or it might mean ++ +. My guess is that the Java lexer tokenizes characters greedily, so in these cases it will always pick the option where the token with the most characters comes first.
"Il y a peu de choses qui me soient impossibles..."
"Il y a peu de choses qui me soient impossibles..."
Stephan van Hulst wrote:The lexer doesn't know anything about operator precedence; in fact, it doesn't know anything about the language's grammar or semantics at all.
"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote:The one time I wrote a compiler, my tokenizer literally read a BNF description of my (or any other context-free) language. That seems pretty intimate with the grammar, to me.
Stephan van Hulst wrote:Did the BNF description include the language's alphabet?
I'm sure that the alphabet is the only part that a tokenizer requires to do its job.
"Il y a peu de choses qui me soient impossibles..."
Stephan van Hulst wrote:Operator precedence is determined by the parser. Before the parser can do it's job, the characters must already have been tokenized by the lexer. The lexer doesn't know anything about operator precedence; in fact, it doesn't know anything about the language's grammar or semantics at all. I'm pretty sure it does this by greedily accepting characters; otherwise it might interpret "double" as an illegal concatenation of the keyword "do" and the identifier "uble".
Wikipedia wrote:If present, these ambiguities are generally resolved by adding precedence rules or other context-sensitive parsing rules, so the overall phrase grammar is unambiguous. [emphasis added]
"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote:No, it solves that problem (sometimes called, "left-substring ambiguity") by applying precedence.
[...]
The classic example is the "dangling else," where a valid BNF would, if turned into code, result in endless recursive attempts to resolve an ambiguity.
Stephan van Hulst wrote:Left-substring ambiguity refers to strings of tokens, not strings of characters.
"Il y a peu de choses qui me soient impossibles..."
Stephan van Hulst wrote:+++ is ambiguous because it might mean + ++ or it might mean ++ +. My guess is that the Java lexer tokenizes characters greedily, so in these cases it will always pick the option where the token with the most characters comes first.
The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would.
Thus, the input characters a--b are tokenized (§3.5) as a, --, b, which is not part of any grammatically correct program, even though the tokenization a, -, -, b could be part of a grammatically correct program.
"Il y a peu de choses qui me soient impossibles..."
Stevens Miller wrote:Interestingly, while b+++a is legal, b++++ is not...,
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
"Il y a peu de choses qui me soient impossibles..."
Stephan van Hulst wrote:The designers added it to provide an operator that's symmetrical to the arithmetic negation operator.
"Il y a peu de choses qui me soient impossibles..."
Paweł showed a use for the unary plus operator last year. Before that I had no idea what you could use it for:-Stephan van Hulst wrote:Nothing.
Campbell Ritchie wrote:Paweł showed a use for the unary plus operator last year.
"Il y a peu de choses qui me soient impossibles..."
Stephan van Hulst wrote:Personally I don't think it's a valid use case, because the cast operator does the same, and communicates intent far more clearly.
"Il y a peu de choses qui me soient impossibles..."
Consider Paul's rocket mass heater. |