Start using the term nonterminal

This commit is contained in:
Dave Halter
2018-06-17 16:40:11 +02:00
parent 6b391af071
commit 23362ec2d3
2 changed files with 13 additions and 13 deletions
+6 -6
View File
@@ -134,20 +134,20 @@ class GrammarParser():
class NFAArc(object): class NFAArc(object):
def __init__(self, next_, label_or_string): def __init__(self, next_, nonterminal_or_string):
self.next = next_ self.next = next_
self.label_or_string = label_or_string self.nonterminal_or_string = nonterminal_or_string
class NFAState(object): class NFAState(object):
def __init__(self, from_rule): def __init__(self, from_rule):
self.from_rule = from_rule self.from_rule = from_rule
self.arcs = [] # list of (label, NFAState) pairs self.arcs = [] # List[nonterminal (str), NFAState]
def add_arc(self, next_, label=None): def add_arc(self, next_, nonterminal_or_string=None):
assert label is None or isinstance(label, str) assert nonterminal_or_string is None or isinstance(nonterminal_or_string, str)
assert isinstance(next_, NFAState) assert isinstance(next_, NFAState)
self.arcs.append(NFAArc(next_, label)) self.arcs.append(NFAArc(next_, nonterminal_or_string))
def __repr__(self): def __repr__(self):
return '<%s: from %s>' % (self.__class__.__name__, self.from_rule) return '<%s: from %s>' % (self.__class__.__name__, self.from_rule)
+7 -7
View File
@@ -218,7 +218,7 @@ def _make_dfas(start, finish):
return return
base_nfa_set.add(nfa_state) base_nfa_set.add(nfa_state)
for nfa_arc in nfa_state.arcs: for nfa_arc in nfa_state.arcs:
if nfa_arc.label_or_string is None: if nfa_arc.nonterminal_or_string is None:
addclosure(nfa_arc.next, base_nfa_set) addclosure(nfa_arc.next, base_nfa_set)
base_nfa_set = set() base_nfa_set = set()
@@ -229,14 +229,14 @@ def _make_dfas(start, finish):
# Find state transitions and store them in arcs. # Find state transitions and store them in arcs.
for nfa_state in state.nfa_set: for nfa_state in state.nfa_set:
for nfa_arc in nfa_state.arcs: for nfa_arc in nfa_state.arcs:
if nfa_arc.label_or_string is not None: if nfa_arc.nonterminal_or_string is not None:
nfa_set = arcs.setdefault(nfa_arc.label_or_string, set()) nfa_set = arcs.setdefault(nfa_arc.nonterminal_or_string, set())
addclosure(nfa_arc.next, nfa_set) addclosure(nfa_arc.next, nfa_set)
# Now create the dfa's with no None's in arcs anymore. All Nones have # Now create the dfa's with no None's in arcs anymore. All Nones have
# been eliminated and state transitions (arcs) are properly defined, we # been eliminated and state transitions (arcs) are properly defined, we
# just need to create the dfa's. # just need to create the dfa's.
for label_or_string, nfa_set in arcs.items(): for nonterminal_or_string, nfa_set in arcs.items():
for nested_state in states: for nested_state in states:
if nested_state.nfa_set == nfa_set: if nested_state.nfa_set == nfa_set:
# The DFA state already exists for this rule. # The DFA state already exists for this rule.
@@ -245,7 +245,7 @@ def _make_dfas(start, finish):
nested_state = DFAState(start.from_rule, nfa_set, finish) nested_state = DFAState(start.from_rule, nfa_set, finish)
states.append(nested_state) states.append(nested_state)
state.add_arc(nested_state, label_or_string) state.add_arc(nested_state, nonterminal_or_string)
return states # List of DFAState instances; first one is start return states # List of DFAState instances; first one is start
@@ -270,8 +270,8 @@ def _dump_dfas(dfas):
print("Dump of DFA for", dfas[0].from_rule) print("Dump of DFA for", dfas[0].from_rule)
for i, state in enumerate(dfas): for i, state in enumerate(dfas):
print(" State", i, state.isfinal and "(final)" or "") print(" State", i, state.isfinal and "(final)" or "")
for label, next in state.arcs.items(): for nonterminal, next in state.arcs.items():
print(" %s -> %d" % (label, dfas.index(next))) print(" %s -> %d" % (nonterminal, dfas.index(next)))
def generate_grammar(bnf_grammar, token_namespace): def generate_grammar(bnf_grammar, token_namespace):