2 ms·
> instead of faithfully implementing the grammar from the specification, allow numbers with leading zeroes and then produce an error for them. But that's the
by raverbashing 7y ago
> instead of faithfully implementing the grammar from the specification, allow numbers with leading zeroes and then produce an error for them.
But that's the problem. The tokenizer doesn't talk to the grammar parser (and vice versa)
The tokenizer could understand numbers with leading zeroes and throw an error there.
Something to think about: do languages - not json - interpret -1.2 as [MINUS][NUMBER] or just [NUMBER]. Or how does languages deal with 1.0-2.0 compared to 1.0+-2.0
- ufo 7y agoExactly. I was referring to the grammar the tokenizer uses for tokenizing numbers. Instead of faithfully copying the JSON spec, you could use a simpler rule that also accepts leading zeros. As for the leading "-", in languages that have expressions it is common to parse the "-" as a prefix operator because that covers both negative numbers (-1.0) and negating variables (-x). But in JSON there are no expressions like 1.0-2.0 so the leading "-" is parsed as part of the number.
- zwkrt 7y agoIf you know you are expecting an expression, then it is easy for the parser to understand ‘-‘ as a unary negation if it is the first symbol, otherwise it must be a binary subtraction operator. Expr: ‘-‘ Expr { return -1 * $2; } | Expr ‘-‘ Expr { return $1 - $2; } | ‘(‘ Expr ‘)’ { return $2; } | ...
- deleted 7y ago[deleted]