-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse.py
58 lines (45 loc) · 1.35 KB
/
parse.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 sys
from typing import List
from lex import lex
from printer import Printer
class VPParser:
def __init__(self):
self.ans = ""
self.error_code = 0
def parse(self, tokens) -> str:
stack = []
for tok in tokens:
if tok.type == 'OPENING':
stack.append(tok)
elif tok.type == 'CLOSING':
if len(stack) == 0:
printer.print_error(outfile, tokens, 'Unexpected closing bracket', tok.lexpos)
self.error_code = 1
else:
stack.pop()
self.ans += tok.value
if len(stack) != 0:
while len(stack) != 0:
tok = stack.pop()
printer.print_error(outfile, tokens, 'Opening bracket is not closed', tok.lexpos)
self.error_code = 2
return self.ans
printer = Printer()
outfile = str()
def main():
filename = sys.argv[1]
try:
file = open(filename)
except OSError:
print("Unable to open file: ", filename)
return
text = file.read()
tokens = lex(text)
parser = VPParser()
result = parser.parse(tokens)
if parser.error_code == 0:
printer.print_condition(result, outfile)
if __name__ == "__main__":
outfile = sys.argv[1] + ".out"
open(outfile, 'w').close()
main()