Write the first phase of a compiler: the lexer.
What to build
A program that splits an arithmetic expression into tokens.
Requirements
- A token is a whole number or one of
+ - * / ( ); spaces are ignored. - Print
count N, then the tokens separated by single spaces.
Input
One line: an arithmetic expression.
Output
Two lines.
Constraints
At most 100 characters; only digits, operators, brackets and spaces.
Example
Input:
2 + 3 * (4 - 1)
Output:
count 9
2 + 3 * ( 4 - 1 )
Why it works
Grouping characters into tokens first means the parser never has to think about digits or spaces.
Examples
Example 1 · Input
2 + 3 * (4 - 1)
Expected output
count 9 2 + 3 * ( 4 - 1 )
Example 2 · Input
12*34
Expected output
count 3 12 * 34
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
re.findall(r"\d+|[-+*/()]", line) returns the tokens in order.
Hint 2
Spaces are not tokens: they simply separate them.
Solution
Show one correct solution (try it yourself first)
import re
tokens = re.findall(r"\d+|[-+*/()]", input())
print("count", len(tokens))
print(" ".join(tokens))