Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit 72d0bae

Browse files
update the example
1 parent 691caf8 commit 72d0bae

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

‎README.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,40 @@ PS: you need to install [typer](https://pypi.org/project/typer/) to run the comp
1818
python compiler_cli.py path/to/file
1919
```
2020

21-
# Example rules
21+
# Simple example
2222

2323
```py
24+
from scanner import Scanner, UnknownTokenError
25+
26+
2427
rules = [
25-
('IF_KW', r'if'),
26-
('ELSE_KW', r'else'),
27-
('FOR_KW', r'for'),
28-
('CONST_STR', r'".*?"|\'.*?\''),
29-
('CONST_NUMBER', r'\d+'),
30-
31-
('PLUS_OP', r'\+'),
32-
('MINUS_OP', r'\-'),
33-
('MULTIPLY_OP', r'\*'),
34-
('DIVIDE_OP', r'\/'),
35-
('LP', r'\('),
36-
('LCB', r'\{'),
37-
('RP', r'\)'),
38-
('RCB', r'\}'),
39-
40-
('EQUAL_OP', r'=='),
41-
('ASSIGNMENT_OP', r'='),
42-
('SEMICOLON', r';'),
43-
('IDENTIFIER', r'[a-zA-Z_]\w*'),
44-
]
28+
('IF_KW', r'if'),
29+
('ELSE_KW', r'else'),
30+
('FOR_KW', r'for'),
31+
('CONST_STR', r'".*?"|\'.*?\''),
32+
('CONST_NUMBER', r'\d+'),
33+
34+
('PLUS_OP', r'\+'),
35+
('MINUS_OP', r'\-'),
36+
('MULTIPLY_OP', r'\*'),
37+
('DIVIDE_OP', r'\/'),
38+
('LP', r'\('),
39+
('LCB', r'\{'),
40+
('RP', r'\)'),
41+
('RCB', r'\}'),
42+
43+
('EQUAL_OP', r'=='),
44+
('ASSIGNMENT_OP', r'='),
45+
('SEMICOLON', r';'),
46+
('IDENTIFIER', r'[a-zA-Z_]\w*'),
47+
]
48+
49+
scanner = Scanner(rules, "i = 5 + 3;")
50+
try:
51+
for token in scanner.token_generator():
52+
print(token)
53+
54+
except UnknownTokenError as error:
55+
print(error)
56+
4557
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /