-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.py
204 lines (156 loc) · 6.14 KB
/
grammar.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import collections
import dataclasses
import json
from typing import Dict, Optional, Set, Tuple
class GrammarJSONEncoder(json.JSONEncoder):
def default(self, o):
if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)
if isinstance(o, set):
return list(o)
return super().default(o)
@dataclasses.dataclass(frozen=True, order=True)
class Symbol:
image: str
@dataclasses.dataclass(frozen=True, order=True)
class NonTerminal(Symbol):
pass
@dataclasses.dataclass(frozen=True, order=True)
class Terminal(Symbol):
pass
EPSILON: Terminal = Terminal("<EPSILON>")
EOF: Terminal = Terminal("<EOF>")
@dataclasses.dataclass(frozen=True, order=True)
class Production:
lhs: NonTerminal
rhs: Tuple[Symbol, ...]
def __len__(self):
return len(self.rhs)
@dataclasses.dataclass(frozen=True, order=True)
class LRProduction(Production):
lookahead: Terminal
# period is in front of rhs[cursor]
cursor: int = 0
@property
def next(self):
if self.cursor > len(self):
return None
return LRProduction(self.lhs, self.rhs, self.lookahead, self.cursor + 1)
@property
def current_symbol(self) -> Optional[Symbol]:
if self.is_finished:
return None
return self.rhs[self.cursor]
@property
def beta(self):
return self.rhs[self.cursor + 1:]
@property
def is_finished(self):
return self.cursor >= len(self)
@property
def p(self):
return Production(self.lhs, self.rhs)
@dataclasses.dataclass
class Grammar:
symbols: Set[Symbol]
start: NonTerminal
productions: Set[Production]
def __post_init__(self):
self.calculate_first()
self.calculate_follow()
def remove_direct_left_recursion(self):
left_recursions = []
productionByLhs = collections.defaultdict(list)
for production in self.productions:
if production.rhs and production.lhs == production.rhs[0]:
left_recursions.append(production)
continue
productionByLhs[production.lhs].append(production)
# lr_production:
# Y ::= YA
# Y ::= B
for lr_production in left_recursions:
self.productions.remove(lr_production)
new_image: str = lr_production.lhs.image + "'"
while new_image in self.symbols:
new_image += "'"
new_lhs: NonTerminal = NonTerminal(new_image)
self.symbols.add(new_lhs)
# Y' ::= AY'
self.productions.add(Production(
new_lhs, lr_production.rhs[1:] + (new_lhs, )
))
# Y' ::= <epsilon>
self.productions.add(Production(
new_lhs, (EPSILON,)
))
for production in productionByLhs[lr_production.lhs]:
self.productions.remove(production)
# Y ::= BY'
self.productions.add(Production(
lr_production.lhs, production.rhs + (new_lhs, )
))
self.calculate_first()
self.calculate_follow()
def to_json(self):
return json.dumps(self, cls=GrammarJSONEncoder, indent=4, sort_keys=True)
def calculate_first(self):
# use Dict[str, Set[Terminal]] instead of Dict[Symbol, Set[Terminal]]
# so that it's easier for json encoding
self.first_for: Dict[str, Set[Terminal]] = collections.defaultdict(set)
# first for a terminal is just itself
for symbol in self.symbols:
if isinstance(symbol, Terminal):
self.first_for[symbol.image].add(symbol)
# if there is epsilon production for NonTerminal X, add epsilon to first(X)
for production in self.productions:
if len(production) == 1 and production.rhs[0] == EPSILON:
self.first_for[production.lhs.image].add(EPSILON)
while True:
revised = False
for production in self.productions:
image = production.lhs.image
len_before = len(self.first_for[image])
# add first(rhs) to first(lhs)
self.first_for[image] |= self.first(production.rhs)
if len_before != len(self.first_for[image]):
revised = True
if not revised:
break
def first(self, rhs: Tuple[Symbol, ...]) -> Set[Terminal]:
if len(rhs) == 0:
return set()
current_first = self.first_for[rhs[0].image].copy()
# added for first(Beta a) in LR1Parser.closure
if isinstance(rhs[0], Terminal):
current_first.add(rhs[0])
for symbol in rhs[1:]:
if EPSILON not in current_first:
break
current_first.discard(EPSILON)
current_first |= self.first_for[symbol.image]
return current_first
def calculate_follow(self):
# use Dict[str, Set[Terminal]] instead of Dict[Symbol, Set[Terminal]]
# so that it's easier for json encoding
self.follow_for: Dict[str, Set[Terminal]] = collections.defaultdict(set)
# add # to follow of start
self.follow_for[self.start.image].add(EOF)
while True:
revised = False
for production in self.productions:
for i, symbol in enumerate(production.rhs):
if isinstance(symbol, Terminal):
continue
len_before = len(self.follow_for[symbol.image])
next_first = self.first(production.rhs[i + 1:])
# add first(X1X2X3...) to follow(X0)
self.follow_for[symbol.image] |= next_first - {EPSILON}
# if epsilon is in first(X1X2X3...) or if Xi is the last symbol
# add follow(Y) to follow(Xi)
if EPSILON in next_first or i == len(production) - 1:
self.follow_for[symbol.image] |= self.follow_for[production.lhs.image]
if len_before != len(self.follow_for[symbol.image]):
revised = True
if not revised:
break