Reading — step 1 of 5
Read
Error Reporting with Positions
A parser that says "syntax error" is a puzzle; a parser that says where is a tool:
Bad: ERR unexpected character
Good: ERR line=2 col=19 unexpected character ','
Every production parser tracks positions — Python raises json.JSONDecodeError carrying .lineno (1-based), .colno (1-based), and .pos (0-based offset); compilers, linters, and editors all consume exactly this triple to jump you to the fault.
Tracking line and column
Record the position at the start of each token, before consuming it, then advance one character at a time:
Both counters are 1-based; a newline bumps line and resets col to 1. (Tabs count as one column here — real tools argue about tab width forever; pick a rule and document it.)
The error contract — exactly what this grader expects
Precise messages need precise rules for which position and which words. This exercise fixes both. Learn the contract from the failing inputs:
| Situation | Report at | Message |
|---|---|---|
a value or key is expected but , is found | that comma | unexpected character ',' |
} appears right after a , | the } itself | trailing comma |
{ and then the input ends | one past the { | object key must be a string |
| a complete value, then leftover text | first leftover character | trailing data |
| document parses clean | — | OK |
Walk the hardest one by hand. Document:
{
"name": "alice",,
}
Line 2 is "name": "alice",,. Count columns: two spaces (1–2), "name" (3–8), : (9), space (10), "alice" (11–17), first comma (18), second comma (19). The comma at col 18 is legal — it separates members and promises another key. The error is the second comma, found where a key should start: ERR line=2 col=19 unexpected character ','. Off-by-one answers (col=18) blame the wrong character.
Two contrasts worth noticing. {"a": 1,} also has a comma promising a member — but here the next token is }, and the contract calls that trailing comma, reported at the } (col 9). And whitespace is never an error: [1, 2, followed by 3] on the next line is a perfectly valid two-line document — a comma at end-of-line is fine as long as a value eventually follows.
The trap: counting positions only when you feel like it. If string scanning or whitespace skipping advances i without going through advance(), your line/col silently drift and every message after the first multi-character token is wrong. Route every character through one choke point.
Your exercise
Documents arrive separated by --- lines; print OK or one ERR line each. The grader compares whole lines, so every piece must match: ERR line=2 col=19 unexpected character ',' for the double comma (col 19, the second comma); ERR line=1 col=9 trailing comma for {"a": 1,} (position of the }, not the comma); ERR line=1 col=2 object key must be a string for a lone { (one past it); ERR line=1 col=4 trailing data for 123abc (the a); and for a comma where an array value should be — line 3 of [, 1,, ,, 2, ] — ERR line=3 col=1 unexpected character ','. Escapes still work in this exercise: "\u00e9" is simply OK.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…