-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.py
More file actions
150 lines (128 loc) · 5.19 KB
/
Parser.py
File metadata and controls
150 lines (128 loc) · 5.19 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from Scanner import Scanner
from Token import TokenType
class Parser:
def __init__(self, scanner, input_path='', output_path=''):
self._input_path = input_path
self._output_path = output_path
scanner.set_files(input_path, 'scanner_output.txt')
self.tokens = scanner.run()
self.current_token = None
def write_to_output_file(self,string):
with open(self._output_path, 'a') as file:
file.write(string + ' Found\n')
def advance_input(self):
try:
self.current_token = self.tokens.__next__()
except StopIteration:
return False
return True
def start_parsing(self):
self.advance_input()
self.program()
def match(self, expected_token_value, expected_token_type):
if expected_token_type == TokenType.ID or expected_token_type == TokenType.NUM:
if expected_token_type == self.current_token.token_type:
self.advance_input()
else:
raise ValueError('match error')
else:
if expected_token_value == self.current_token.string_value:
self.advance_input()
else:
raise ValueError('match error')
def program(self):
self.stmt_sequence()
self.write_to_output_file('Program')
def stmt_sequence(self):
self.statement()
while self.current_token.string_value == ';':
self.match(';', TokenType.SPSYMB)
self.statement()
self.write_to_output_file('Statement_Sequence')
def statement(self):
if self.current_token.string_value == 'if':
self.if_stmt()
elif self.current_token.string_value == 'repeat':
self.repeat_stmt()
elif self.current_token.token_type == TokenType.ID:
self.assign_stmt()
elif self.current_token.string_value == 'read':
self.read_stmt()
elif self.current_token.string_value == 'write':
self.write_stmt()
self.write_to_output_file('Statement')
def if_stmt(self):
self.match('if', TokenType.RESWORD)
self.exp()
self.match('then', TokenType.RESWORD)
self.stmt_sequence()
if self.current_token.string_value == 'else':
self.match('else', TokenType.RESWORD)
self.stmt_sequence()
self.match('end', TokenType.RESWORD)
self.write_to_output_file('IF_Statement')
def repeat_stmt(self):
self.match('repeat', TokenType.RESWORD)
self.stmt_sequence()
self.match('until', TokenType.RESWORD)
self.exp()
self.write_to_output_file('Repeat_Statement')
def assign_stmt(self):
self.match('', TokenType.ID)
self.match(':=', TokenType.SPSYMB)
self.exp()
self.write_to_output_file('Assignment_Statement')
def read_stmt(self):
self.match('read', TokenType.RESWORD)
self.match('', TokenType.ID)
self.write_to_output_file('Read_Statement')
def write_stmt(self):
self.match('write', TokenType.RESWORD)
self.exp()
self.write_to_output_file('Write_Statement')
def exp(self):
self.simple_exp()
if self.current_token.string_value == '<' or self.current_token.string_value == '=':
self.comparison_op()
self.simple_exp()
self.write_to_output_file('Expression')
def simple_exp(self):
self.term()
while self.current_token.string_value == '+' or self.current_token.string_value == '-':
self.add_op()
self.term()
self.write_to_output_file('Simple_Expression')
def comparison_op(self):
if self.current_token.string_value == '<' or self.current_token.string_value == '=':
self.match(self.current_token.string_value, TokenType.SPSYMB)
self.write_to_output_file('Comparator_Operator')
def term(self):
self.factor()
while self.current_token.string_value == '*' or self.current_token.string_value == '/':
self.mul_op()
self.factor()
self.write_to_output_file('Term')
def add_op(self):
if self.current_token.string_value == '+' or self.current_token.string_value == '-':
self.match(self.current_token.string_value, TokenType.SPSYMB)
self.write_to_output_file('Add_Operator')
def factor(self):
if self.current_token.string_value == '(':
self.match('(', TokenType.SPSYMB)
self.exp()
self.match(')', TokenType.SPSYMB)
elif self.current_token.token_type == TokenType.NUM:
self.match('', TokenType.NUM)
elif self.current_token.token_type == TokenType.ID:
self.match('', TokenType.ID)
self.write_to_output_file('Factor')
def mul_op(self):
if self.current_token.string_value == '*' or self.current_token.string_value == '/':
self.match(self.current_token.string_value, TokenType.SPSYMB)
self.write_to_output_file('Mul_Operator')
if __name__ == "__main__":
input_file = 'tiny_sample_code.txt'
output_file = 'parser_output.txt'
scanner = Scanner()
parser = Parser(scanner, input_file, output_file)
parser.start_parsing()