-
Notifications
You must be signed in to change notification settings - Fork 13
/
gen.py
64 lines (50 loc) · 1.88 KB
/
gen.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
# -*- coding: utf8 -*-
'''
© 2009 Denis Derman (former developer) <[email protected]>
© 2011 Peter Potrowl (current developer) <[email protected]>
This file is part of PIJNU.
PIJNU is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PIJNU is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PIJNU: see the file called 'GPL'.
If not, see <http://www.gnu.org/licenses/>.
'''
''' PIJNU command-line parser generator
A helper to generate a python parser from a grammar, on the command line.
Avoids writing python code to do that; also may be handy for end-users.
call format:
python gen.py grammarFileName [parserFileName]
If parserFileName is not given, the parser is written into
grammarTitle + "Parser.py"
'''
### import/export
from pijnu.generator import makeParser
from sys import argv as arguments
from library.tools import *
def do():
''' write parser from grammar '''
# get source grammar from command line argument
try:
grammar = file(arguments[1], 'r').read()
except IndexError:
message = ("\nCall Format:\n"
"\tpython gen.py grammar_file_name\n"
"or\n"
"\tpython gen.py grammar_file_name parserFileName\n\n"
)
write(message)
end()
# write parser into target file
try:
parserFile = file(arguments[2], 'w')
makeParser(grammar, parserFile)
except Exception:
makeParser(grammar)
if __name__ == "__main__":
do()