-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils_cfg.py
45 lines (36 loc) · 1.43 KB
/
utils_cfg.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
""" Internal Usage only"""
from typing import List, AbstractSet
from .production import Production
from .epsilon import Epsilon
from .cfg_object import CFGObject
def remove_nullable_production_sub(body: List[CFGObject],
nullables: AbstractSet[CFGObject]) \
-> List[List[CFGObject]]:
""" Recursive sub function to remove nullable objects """
if not body:
return [[]]
all_next = remove_nullable_production_sub(body[1:], nullables)
res = []
for body_temp in all_next:
if body[0] in nullables:
res.append(body_temp)
if body[0] != Epsilon():
res.append([body[0]] + body_temp.copy())
return res
def remove_nullable_production(production: Production,
nullables: AbstractSet[CFGObject]) \
-> List[Production]:
""" Get all combinations of productions rules after removing nullable """
next_prod_l = remove_nullable_production_sub(production.body,
nullables)
res = [Production(production.head, prod_l)
for prod_l in next_prod_l
if prod_l]
return res
def get_productions_d(productions):
""" Get productions as a dictionary """
productions_d = {}
for production in productions:
production_head = productions_d.setdefault(production.head, [])
production_head.append(production)
return productions_d