Design a utility for expression parser

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2023-02-15

Problem Statement

Design and implement a utility that can parse, validate, and evaluate mathematical expressions. The utility should support basic arithmetic, operator precedence, parentheses, and extensibility for functions or variables.


Functional Requirements (FR)


Parsing & Evaluation


•Parse arithmetic expressions: +, -, *, /.
•Support parentheses to enforce precedence.
•Evaluate expressions and return numeric results.
•Support unary operators (e.g., -5, +7).


Variables & Functions (Optional)


•Support variables: e.g., x = 5, y = x + 2.
•Support custom functions: e.g., max(a, b), sin(x).


Error Handling


•Detect invalid expressions (syntax errors, division by zero).
•Provide meaningful error messages.


Extensibility


•Ability to add new operators or functions without major redesign.
•Configurable operator precedence.


Non-Functional Requirements (NFR)


•Performance → Efficient parsing and evaluation for long expressions.
•Memory Efficiency → Minimal overhead for expression trees.
•Extensibility → Easy to add new operators or functions.
•Reusability → Can be used as a standalone library or integrated into larger systems.


Core Entities & Attributes


ExpressionParser


•parse(expression: String): ExpressionTree → Converts string to expression tree.
•evaluate(tree: ExpressionTree): Number → Evaluates the parsed tree.


ExpressionTree / Node


•NodeType → Operator, Operand, Function, Variable.
•value → Numeric value or operator symbol.
•left, right → Children for binary operators.
•args[] → For functions with multiple arguments.


Tokenizer / Lexer


•Converts input string into tokens: numbers, operators, parentheses, variables.


Operator / Function Registry (Optional)


•Stores operator precedence and available functions.


Core Components


Tokenizer / Lexer

•Breaks input expression into tokens.
•Handles whitespace, numbers, variables, operators, parentheses.

Parser

•Converts tokens into Expression Tree using rules like Shunting Yard Algorithm or Recursive Descent Parsing.
•Enforces operator precedence and associativity.

Evaluator

•Traverses Expression Tree to compute final result.
•Handles unary/binary operators, functions, and variables.

Error Handling Module

•Detects invalid tokens, mismatched parentheses, division by zero, or undefined variables/functions.


APIs / Operations


Core APIs


•parse(expression: String): ExpressionTree
•evaluate(expression: String | ExpressionTree): Number
•validate(expression: String): boolean


Optional Advanced APIs


•addFunction(name: String, function: Callable) → Add custom functions.
•addOperator(symbol: String, precedence: int, associativity: LEFT|RIGHT) → Add new operators.
•setVariable(name: String, value: Number) → Assign variables.


LLD Design Goals


•Class Diagram → ExpressionParser, Tokenizer, ExpressionTree/Node, Evaluator, OperatorRegistry, FunctionRegistry.


Design Patterns:


•Interpreter Pattern → For evaluating expressions.
•Composite Pattern → Expression Tree nodes for operators/functions.
•Factory / Registry → For operators and functions.
•Trade-offs →1. Recursive parsing is simple but can cause stack overflow for very large expressions.2. Shunting Yard algorithm is iterative and memory-efficient.
•Extensibility → Easy to add functions, variables, or new operators without touching parser logic.


Solution

Comments