1005. Tokenize an Arithmetic Expression
Medium · String
Implement a lexer for a tiny arithmetic language.
Tokens: NUM — one or more digits (e.g. "42", "1000") OP — one of + - * / LPAREN — ( RPAREN — )
Skip whitespace. Output each token on its own line as "TYPE VALUE".
Input (stdin, JSON): { "src": "3 + 4 * (5 - 1)" }
Output (stdout): NUM 3 OP + NUM 4 OP * LPAREN ( NUM 5 OP - NUM 1 RPAREN )
Examples
Example 1
Input: {"src":"3+4"}
Output: NUM 3
OP +
NUM 4Constraints
- src is 1..256 chars; ASCII only.
- No malformed input — assume well-formed token characters.