-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPDA.py
executable file
·75 lines (68 loc) · 2.89 KB
/
PDA.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
74
75
from FileHandler import FileHandler
import time
class PDA:
def __init__(self):
self.stack = []
def compute(self, inputString, parsedLines):
#Retrieve all information
inputString += 'e'
initStackSymbol = parsedLines['initial_stack']
self.stack.append(initStackSymbol)
finalStates = parsedLines['final_states']
initialState = parsedLines['initial_state']
stackSymbols = parsedLines['stack_symbols']
productions = parsedLines['productions']
currentStackSymbol = initStackSymbol
currentState = initialState
print('State\tInput\tStack\tMove')
print('{}\t {}\t {}\t ({}, {})'.format(currentState, '_', 'Z', currentStackSymbol, self.stack))
for char in inputString:
#print('Current TOS', currentStackSymbol)
for production in productions:
if ((production[0] == currentState) and (production[1] == char) and (production[2] == currentStackSymbol)):
currentState = production[3]
if(len(production[4]) == 2):
self.stack.append(char)
elif(len(production[4]) == 3):
self.stack.append(char)
self.stack.append(char)
elif ((production[4] == 'e') and (len(self.stack) != 1)):
self.stack.pop()
break
previousStackSymbol = currentStackSymbol
currentStackSymbol = self.stack[len(self.stack)-1]
print('{}\t {}\t {}\t ({}, {})'.format(currentState, char, previousStackSymbol, currentStackSymbol, self.stack))
#time.sleep(2)
if(currentState in finalStates):
print('String accepted by PDA.')
else:
print('String rejected by PDA.')
def main():
fh = FileHandler()
pda = PDA()
automataFilePath = input('Enter the automata file path: ')
lines = fh.readFile(automataFilePath)
print('Reading Automata File')
#time.sleep(2)
print('Automata File Successfully Read')
#time.sleep(2)
inputString = input('Enter input String: ')
inputString = inputString.rstrip()
print('Loading Details from Automata File: ')
#time.sleep(3)
parsedLines = fh.parseFile(lines)
print('States: ', parsedLines['states'])
print('Input Symbols: ', parsedLines['input_symbols'])
print('Stack Symbols: ', parsedLines['stack_symbols'])
print('Initial State: ', parsedLines['initial_state'])
print('Initial Stack Symbol: ', parsedLines['initial_stack'])
print('Final States: ', parsedLines['final_states'])
print('Productions List:')
for production in parsedLines['productions']:
print('\t', production)
#time.sleep(2)
print('Details loaded')
print('Computing the Transition Table:')
pda.compute(inputString, parsedLines)
if __name__ == '__main__':
main()