Hone logo
Hone
Problems

Simple Expression Parser

This challenge asks you to build a basic expression parser in JavaScript. Parsers are fundamental to many programming language tools, compilers, and interpreters, allowing us to understand and process structured text. Your parser will evaluate simple mathematical expressions involving addition, subtraction, multiplication, and division, with integer operands.

Problem Description

You are to create a JavaScript function parseExpression(expression) that takes a string expression as input and returns the integer result of evaluating that expression. The expression will consist of integers and the operators +, -, *, and /. The expression will be in infix notation (e.g., "2 + 3 * 4"). The parser should handle operator precedence (multiplication and division before addition and subtraction) and left-to-right associativity for operators of the same precedence.

Key Requirements:

  • Operator Precedence: Multiplication and division should be evaluated before addition and subtraction.
  • Left-to-Right Associativity: For operators with the same precedence (e.g., + and -), they should be evaluated from left to right.
  • Integer Arithmetic: All operands should be treated as integers. Division should truncate towards zero (e.g., 5 / 2 should result in 2).
  • Error Handling: The function should return NaN if the input expression is invalid (e.g., contains invalid characters, unbalanced parentheses - although parentheses are not required in this simplified version, invalid operator sequences).

Expected Behavior:

The function should correctly evaluate expressions of varying complexity, respecting operator precedence and associativity.

Edge Cases to Consider:

  • Empty expression string.
  • Expression with only a single number.
  • Expressions with multiple operators.
  • Expressions with leading or trailing spaces (should be ignored).
  • Division by zero (should return NaN).
  • Invalid characters in the expression.

Examples

Example 1:

Input: "2 + 3 * 4"
Output: 14
Explanation: 3 * 4 = 12, then 2 + 12 = 14

Example 2:

Input: "10 - 5 / 2"
Output: 7
Explanation: 5 / 2 = 2 (truncated), then 10 - 2 = 8

Example 3:

Input: "5 * 2 + 3 - 1"
Output: 10
Explanation: 5 * 2 = 10, then 10 + 3 = 13, then 13 - 1 = 12

Example 4:

Input: "1 + 2 + 3"
Output: 6
Explanation: 1 + 2 = 3, then 3 + 3 = 6

Example 5:

Input: "10 / 0"
Output: NaN
Explanation: Division by zero is undefined.

Example 6:

Input: "abc + 5"
Output: NaN
Explanation: Invalid character 'a'

Constraints

  • The input expression will be a string.
  • The expression will contain only integers, the operators +, -, *, /, and spaces.
  • Integers will be non-negative.
  • The length of the expression string will be between 1 and 100 characters (inclusive).
  • The parser should be reasonably efficient; avoid excessively complex or inefficient algorithms. A solution with O(n) time complexity, where n is the length of the expression, is expected.

Notes

  • You can use a stack-based approach to handle operator precedence.
  • Consider breaking the expression into tokens (numbers and operators) before parsing.
  • Regular expressions can be helpful for tokenizing the expression.
  • Focus on correctness first, then optimize for performance if necessary.
  • This is a simplified parser; it does not handle parentheses, functions, or variables. The goal is to demonstrate understanding of operator precedence and associativity.
Loading editor...
javascript