Lisp Expression Parser
Learn this problemProblem statement
Parse expression as one Lisp-style expression and return the canonical JSON serialization of its syntax tree as a string.
- An atom is a nonempty maximal sequence of ASCII letters, digits, underscore, plus, minus, asterisk or slash. Preserve its exact characters and case, including leading zeros and signs.
- A list consists of an opening parenthesis, zero or more expressions, and a closing parenthesis. Lists may nest.
- ASCII space, tab, line feed and carriage return separate tokens and may occur before or after the root. Parentheses are standalone tokens even when adjacent to atoms or other parentheses.
- The input is valid and contains exactly one root expression. Adjacent atoms must be separated by whitespace; parentheses already provide token boundaries.
Serialize every atom as a JSON string and every list as a JSON array of its children, preserving their order and nesting. Use commas between children and no whitespace outside quoted atom strings. The allowed atom characters never contain a quote or backslash. An empty list becomes [].
Do not evaluate operators, resolve names, change numeric-looking atoms into numbers, or flatten nested lists. For example, the atom 001 remains the JSON string "001". The function returns the serialized text, not a native nested array.
Function
parseLispExpression(expression: String) → StringExamples
Example 1
expression = " (add 1 (mul 2 3)) "return = "[\"add\",\"1\",[\"mul\",\"2\",\"3\"]]"The outer list has three children. Its last child is another list. Operator names and numbers are all strings; no multiplication or addition is performed.
Example 2
expression = "(a (b) ())"return = "[\"a\",[\"b\"],[]]"The nested one-element list and the empty list remain distinct child arrays. Whitespace outside atoms is not part of the canonical output.
Constraints
1 <= expression.length <= 5000.- There are at most
2000tokens, counting each parenthesis and each atom once. - At most
100lists are open at the same time. - The expression follows the stated grammar and contains exactly one root.