-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
49 lines (40 loc) · 1.5 KB
/
parser.py
File metadata and controls
49 lines (40 loc) · 1.5 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
"""
Author: Charanjit Singh
Website: https://charanjit-singh.github.io
Description: This module parses the given 'program file' using "Context Free Grammar" and generates
the syntax Tree from input Token Stream.
"""
from utils.ParserUtils import *
from utils.LL1Parser import *
from utils.SLRParser import *
class Parser():
def __init__(self, **kwargs):
self.grammar_file = kwargs.get('input_file', None)
self.grammar = Grammar()
self.grammar.parse_from_file(self.grammar_file)
print("Original",end=" ")
self.grammar.print_grammar()
self.grammar.remove_indirect_left_recursion()
self.grammar.do_left_factoring()
firsts = self.grammar.get_firsts()
follows = self.grammar.get_follows()
print("FIRSTS")
for first in firsts:
print(' >>> ',first['lhs'] ,':',first['firsts'])
print("FOLLOWS")
for follow in follows:
print(' >>> ',follow.symbol ,':',list(follow.follows))
parser = LL1Parser(self.grammar)
stream = []
stream.append(GrammarSymbol("'a'", GrammarSymbol.TYPE_TERMINAL))
stream.append(GrammarSymbol("'b'", GrammarSymbol.TYPE_TERMINAL))
stream.append(GrammarSymbol("'a'", GrammarSymbol.TYPE_TERMINAL))
# stream.append(GrammarSymbol("'id'", GrammarSymbol.TYPE_TERMINAL))
# stream.append(GrammarSymbol("'*'", GrammarSymbol.TYPE_TERMINAL))
# stream.append(GrammarSymbol("'id'", GrammarSymbol.TYPE_TERMINAL))
try:
parser.parse(stream)
except Exception as e:
print("\nError: ",str(e))
# Parser = SLRParser(self.grammar)
# self.grammar.print_grammar()