-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner_C.py
73 lines (59 loc) · 2.36 KB
/
scanner_C.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import re
import tkinter as tk
from tkinter import scrolledtext
c_keywords = {
"continue", "sizeof", "while", "case", "union", "printf", "unsigned", "if", "switch", "static", "return",
"for", "default", "do", "struct", "signed", "float", "char", "break", "else", "double", "scanf", "typedef",
"int", "void", "const"
}
tokens_regex = [
('keyword', r'#\s*include\s*<[^>]+>'),
('comment', r'//.*|/\*[\s\S]*?\*/'),
('character_constant', r"'([^'\\]|\\.)'|\"([^\"\\]|\\.)*\""),
('numerical_constant', r'\d+(\.\d*)?([eE][+-]?\d+)?'),
('identifier', r'[A-Za-z_]\w*'),
('operator', r'&&|\|\||==|!=|<=|>=|<|>|[+\-*/%&|=]'),
('special_character', r'[(){},;]'),
('whitespace', r'[ \t]+'),
('newline', r'\n'),
('unknown', r'.'),
]
tok_regex = '|'.join(f"(?P<{name}>{pattern})" for name, pattern in tokens_regex)
def code_tokenization(code):
tokens = []
line_num = 1
for match_obj in re.finditer(tok_regex, code):
token_type = match_obj.lastgroup
token_value = match_obj.group()
if token_type == 'newline':
line_num += 1
continue
elif token_type == 'identifier' and token_value in c_keywords:
token_type = 'keyword'
elif token_type == 'whitespace':
continue
elif token_type == 'unknown':
raise RuntimeError(f'Unexpected character {token_value} on line {line_num}')
tokens.append((token_type, token_value))
return tokens
def c_code_analysis():
code_content = code_text.get("1.0", tk.END)
try:
tokens = code_tokenization(code_content)
result_text.delete("1.0", tk.END)
for token_type, token_value in tokens:
result_text.insert(tk.END, f"{token_type}: {token_value}\n")
except RuntimeError as error:
result_text.delete("1.0", tk.END)
result_text.insert(tk.END, str(error))
root = tk.Tk()
root.title("Scanner of Sbubest of C")
tk.Label(root, text="enter the code:").pack()
code_text = scrolledtext.ScrolledText(root, width=60, height=12)
code_text.pack()
analyze_button = tk.Button(root, text="scan the code", command=c_code_analysis)
analyze_button.pack()
tk.Label(root, text="Tokens:").pack()
result_text = scrolledtext.ScrolledText(root, width=60, height=17)
result_text.pack()
root.mainloop()