-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parser.py
58 lines (48 loc) · 1.91 KB
/
Parser.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
import re
keywords = ['begin','define', 'print', 'if', 'update']
mathematic_operators = ['+','-','*','/','%','=']
comparison_operators = ['==','!=','<>','<=','>=''<','>']
def num_parser(data):
match = re.match(r'\d+',data) or re.match(r'-\d+',data) # matches for numbers and returns a match object
if(match):
return(int(data[:match.end()]), data[match.end():])
def space_parser(data):
match=re.match(r'\s+',data) # matches for white spaces and returns a match object
if(match):
return([],data[match.end():])
def word_parser(data):
match=re.match(r'[a-zA-Z_?]+',data) # matches for string and returns a match object
if(match):
return(data[:match.end()], data[match.end():])
def keyword_parser(data): # matches for keywords
for keyword in keywords:
if(data.startswith(keyword)):
return(data[:len(keyword)],data[len(keyword):])
def mathematic_parser(data):
for operator in mathematic_operators:
if(data.startswith(operator)):
return(data[:len(operator)],data[len(operator):])
def comparison_parser(data):
for operator in comparison_operators:
if(data.startswith(operator)):
return(data[:len(operator)],data[len(operator):])
def comment_parser(data):
if(data[0] == '#'):
return(data[0],data[1:])
def parser(data):
if(data[0]=='('):
L=[]
data=data[1:]
while(data[0]!=')'):
output, data = parser(data)
if(not output):
continue
else:
L.append(output)
return(L,data[1:])
else:
return(space_parser(data) or num_parser(data) or keyword_parser(data) or comment_parser(data)
or mathematic_parser(data) or comparison_parser(data) or word_parser(data))
#data="(begin (define circle_area (lambda r (* pi (* r r)))))"
#A,_ = parser(data)
#print(A)