-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileHandler.py
More file actions
executable file
·49 lines (44 loc) · 1.73 KB
/
FileHandler.py
File metadata and controls
executable file
·49 lines (44 loc) · 1.73 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
import os
class FileHandler:
def __init__(self):
pass
def readFile(self, filePath):
lines=[]
if(os.path.isfile(filePath)):
try:
with open(filePath) as file:
lines = [line.rstrip() for line in file]
except IOError as e:
print("File could not be opened.")
exit(0)
else:
print('{} :File was not found in the specified path.'.format(filePath))
exit(0)
return lines
def parseFile(self,lines):
''' Line 1: Total States
Line 2: Input Word Symbols
Line 3: Stack Symbols
Line 4: Initial State Symbol
Line 5: Initial Stack Symbol
Line 6: List of Final States
Line 7 and onwards: Productions in form of
(Current State, Current Input Symbol, Current Top of Stack, Next State, Push/Pop Operation Symbol)
'''
states = lines[0].rstrip().split()
input_symbols = lines[1].rstrip().split()
stack_symbols = lines[2].rstrip().split()
initial_state = lines[3][0]
initial_stack = lines[4][0]
final_states = lines[5].rstrip().split()
productions = lines[6:]
for i in range(len(productions)):
productions[i] = productions[i].rstrip().split()
parsedLines = {'states':states,
'input_symbols':input_symbols,
'stack_symbols':stack_symbols,
'initial_state':initial_state,
'initial_stack':initial_stack,
'final_states':final_states,
'productions':productions}
return parsedLines