-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
38 lines (32 loc) · 1.27 KB
/
main.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
import sys
import argparse
from parse import parse_grammar
from grammar import Grammar
import grammar_parse
import lexer
#initializes the command line argument parser
def init_clp():
parser = argparse.ArgumentParser(
description='parses a grammar and validates syntatically user entries.')
parser.add_argument('-g', '--grammar', metavar='grammar', type=str,
help='the grammar file to be parsed.', dest='grammar', required=True)
parser.add_argument('-f', '--file', metavar='source', type=str, dest='source',
help='path to file containing the source code to be validated')
return parser
if __name__ == '__main__':
parser = init_clp()
args = parser.parse_args()
source = sys.stdin
lexer_g = grammar_parse.create_lexer()
with open(args.grammar) as grammar_input:
g_token_stream = lexer_g.lex_input(grammar_input.read())
grammar_ast = grammar_parse.parse_grammar(g_token_stream)
print(str(grammar_ast))
g = Grammar()
g.load_from_ast(grammar_ast)
if args.source != None:
with open(args.source) as code:
s_token_stream = g.lexer.lex_input(code.read())
parse_grammar(g, s_token_stream)
else:
parse_grammar(g, g.lexer.lex_input(sys.stdin.read()))