Recursive Descent Parser - Problem
Build a recursive descent parser for arithmetic expressions that correctly handles operator precedence and parentheses.
Your parser should parse expressions with:
- Numbers: Non-negative integers (0-9 digits)
- Operators:
+(addition),-(subtraction),*(multiplication),/(integer division) - Parentheses:
(and)for grouping
The parser should follow standard mathematical precedence:
- Parentheses (highest priority)
- Multiplication and Division (left-to-right)
- Addition and Subtraction (left-to-right, lowest priority)
Return the evaluated result of the parsed expression as an integer.
Input & Output
Example 1 — Basic Precedence
$
Input:
expression = "2+3*4"
›
Output:
14
💡 Note:
Multiplication has higher precedence: 3*4 = 12, then 2+12 = 14
Example 2 — Parentheses Override
$
Input:
expression = "(2+3)*4"
›
Output:
20
💡 Note:
Parentheses first: (2+3) = 5, then 5*4 = 20
Example 3 — Division and Subtraction
$
Input:
expression = "10-2*3/2"
›
Output:
7
💡 Note:
Left-to-right for same precedence: 2*3 = 6, 6/2 = 3, then 10-3 = 7
Constraints
- 1 ≤ expression.length ≤ 1000
- expression contains only digits, +, -, *, /, (, ), and spaces
- All intermediate results fit in 32-bit integer range
- No division by zero
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code