I've done parser stuff before but never under interview pressure and never from scratch in one sitting.
Start by clarifying the language grammar and requirements, then outline a modular pipeline: tokenizer, parser (using recursive descent or Pratt parsing for precedence), and evaluator. Discuss design choices like AST node types, environment for variables, and error handling, and mention testing each component.
Pro tip: Mention that you would use a Pratt parser (or precedence climbing) to elegantly handle operator precedence and unary minus, and that you'd write unit tests for each stage to catch edge cases early.
Ask clarifying questions about the language features (e.g., variable scoping, integer overflow, error handling) and define a formal grammar (e.g., EBNF) to guide implementation.
Specify token types (integers, identifiers, operators, keywords) and implement a lexer that scans input and produces a token stream, handling whitespace and errors.
Choose a parsing strategy (e.g., recursive descent with precedence climbing) and define AST node classes for expressions, assignments, and print statements.
Create an evaluator that walks the AST, maintains an environment for variable bindings, and executes print statements, ensuring correct arithmetic and precedence.
Write unit tests for each component (tokenizer, parser, evaluator) and integration tests for full programs, covering edge cases like unary minus and operator precedence.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.