KatInterview

Build a Streaming Lisp-Style Expression Reader with Exact Error Positions

StringsModel answer
MediumModel answerStringsparsingrecursive-descenterror-handling

Last updated

Write a single-pass reader for a small Lisp-like syntax with integers, symbols and nested lists, reporting the first error with a clear message and exact character index.

Build a reader for a tiny Lisp-like notation. It must scan the text once, left to right, using a moving position in the string. Splitting the whole input into pieces (for example with split()) is not allowed. The focus is on failing well: at the first bad spot, stop and report what is wrong and at which character index.

Working grammar

  • Whitespace (space, tab, \r, \n) separates elements and is otherwise ignored.
  • Integer: an optional - followed by one or more decimal digits (42, -7).
  • Symbol: starts with an ASCII letter or one of + - * / _, followed by any number of ASCII letters, digits, or + - * / _ (x, add, +, set_value). A token that fits the integer rule is an integer, not a symbol.
  • List: (, zero or more expressions separated by whitespace, ). A parenthesis also terminates an atom, so (a(b)) is valid.
  • Any other character (#, ", @, ...) is invalid. A token starting with a digit that is not a full integer (such as 12ab) is invalid.
  • Output: integers become integers, symbols become strings, lists become lists. (add 1 (neg x)) gives ["add", 1, ["neg", "x"]].
  • The input contains exactly one expression, optionally surrounded by whitespace. Anything after it is an error.
  • Every error must state the problem and the character index.

Decide, and state, how you report errors (exception vs. returned value), whether partial results are returned (they should not be), and whether an index is enough or line/column is wanted.

Part 1: a lone atom

Parse an input that is a single atom, optionally padded with whitespace. " -15 " gives -15, "add" gives "add". "12ab", "a#b" and empty input are errors. Design the atom reader so it can be called in the middle of a longer input.

Part 2: a flat list

Accept a parenthesized list of atoms: (add 1 -2 x) gives ["add", 1, -2, "x"], () gives []. The parser must stop at the first problem with a clear message for at least: (a # b), (ab#), (12ab), (a b, ) and (a b) c. Nested lists may be rejected at this stage.

Part 3: arbitrary nesting

Allow lists inside lists at any depth: (define (square x) (* x x)) gives ["define", ["square", "x"], ["*", "x", "x"]]. Still one pass, and every Part 2 error must still be detected with a correct index at any depth, including a deeply nested list that is never closed.

Discuss: how deep can nesting go (recursion limit vs. explicit stack), and which ( an unclosed-list error should point to.

Expected

Working code for all three parts, a set of tests covering valid inputs and each error class (including inside nested lists), and the complexity of your approach.

Hints

Hint 1

Keep one cursor (an index into the string) shared by every routine. Each routine reads from the cursor and leaves it just past what it consumed.

Hint 2

Write the atom reader to consume characters until whitespace, `(`, `)` or end of input, then classify the token: integer, symbol, or invalid. Report invalid characters at their own index and malformed tokens at the token start.

Hint 3

Use two mutually recursive routines: `parse_expr` (skips whitespace, dispatches on `(`, `)`, or atom start) and `parse_list` (loops until `)`). Check for trailing input only once, at the top level after the single expression. For an unclosed list, remember the index of its opening `(`.