-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLSParser.py
More file actions
53 lines (43 loc) · 1.32 KB
/
Copy pathSLSParser.py
File metadata and controls
53 lines (43 loc) · 1.32 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
#!/usr/bin/python
from ply.lex import lex
from ply.yacc import yacc
from lexer_rules import *
from parser_rules import *
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print('SLSParser.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('SLSParser.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
if(inputfile == '' or outputfile == ''):
print('SLSParser.py -i <inputfile> -o <outputfile>')
sys.exit()
print('Archivo entrada: ' + inputfile)
print('Archivo salida: ' + outputfile)
with open(inputfile, 'r') as content_file:
content = content_file.read()
parser = yacc()
lexer = lex()
try:
output = parser.parse(content, lexer)
except TypeError as e:
print("Error de tipo:\n\t" + str(e))
sys.exit()
except SyntaxError as e:
print("Error de sintaxis:\n\t" + str(e))
sys.exit()
f = open(outputfile, 'w+')
f.write(output.formatted_code)
if __name__ == "__main__":
main(sys.argv[1:])