HyperDbg Debugger
Loading...
Searching...
No Matches
lalr_parsing.lalr_one.ParsingTable Class Reference

Public Member Functions

 __init__ (self, gr)
 
 stringify_state (self, state_id)
 
 stringify (self)
 
 get_single_state_conflict_status (self, state_id)
 
 get_conflict_status (self)
 
 is_lalr_one (self)
 
 save_to_csv (self, filepath)
 

Public Attributes

 grammar
 
 terminals
 
 nonterms
 
 n_states
 
 goto
 
 action
 

Constructor & Destructor Documentation

◆ __init__()

lalr_parsing.lalr_one.ParsingTable.__init__ ( self,
gr )
7 def __init__(self, gr):
8 self.grammar = gr
9
10 self.terminals = () # terminals of 'gr', union grammar.EOF_SYMBOL
11 self.nonterms = () # non-terminals of 'gr', except grammar.START_SYMBOL
12
13 self.__ccol = ()
14 self.n_states = 0 # Taken from the cardinality of the LALR(1) canonical collection
15
16 # goto will be a list of dictionaries in order to easy its usage:
17 # goto[state_id][nonterminal]
18 # Each dictionary will map a non-terminal to an int: the index of the next state.
19 # If for a given non-terminal there's no transition, it will map to None instead.
20 #
21 # action will be a list of dictionaries in order to easy its usage:
22 # action[state_id][terminal]
23 # Each dictionary will map a terminal to a set of "table entries".
24 # The type of a table entry varies according to the kind of entry:
25 # a "shift #state_id" entry is a 2-tuple ('shift and go to state', state_id)
26 # a "reduce #prod_index" entry is a 2-tuple ('reduce using rule', prod_index)
27 # an "accept" entry is just a 2-tuple ('accept', '')
28 # As a special case: an "error" entry is detoned just by the empty set of table
29 # entries.
30 #
31 # See Dragonbook, page 265, "Canonical LR(1) parsing tables" for reference.
32
33 self.goto = ()
34 self.action = ()
35
36 self.__setup_from_grammar(self.grammar)
37

Member Function Documentation

◆ get_conflict_status()

lalr_parsing.lalr_one.ParsingTable.get_conflict_status ( self)
148 def get_conflict_status(self):
149 return [self.get_single_state_conflict_status(i) for i in range(self.n_states)]
150

◆ get_single_state_conflict_status()

lalr_parsing.lalr_one.ParsingTable.get_single_state_conflict_status ( self,
state_id )
144 def get_single_state_conflict_status(self, state_id):
145 seq = [self.__get_entry_status(e) for t, e in self.action[state_id].items()]
146 return STATUS_OK if len(seq) == 0 else max(seq)
147

◆ is_lalr_one()

lalr_parsing.lalr_one.ParsingTable.is_lalr_one ( self)
151 def is_lalr_one(self):
152 seq = self.get_conflict_status()
153 return (STATUS_OK if len(seq) == 0 else max(seq)) == STATUS_OK
154

◆ save_to_csv()

lalr_parsing.lalr_one.ParsingTable.save_to_csv ( self,
filepath )
155 def save_to_csv(self, filepath):
156 with open(filepath, 'w', newline='') as csvfile:
157 writer = csv.writer(csvfile, dialect='excel')
158
159 headers = tuple(' ') + self.terminals + self.nonterms
160 writer.writerow(headers)
161
162 def stringify_action_entries(entries):
163 return ', '.join(e[0].split()[0][0] + str(e[1]) for e in entries)
164
165 for state_id in range(self.n_states):
166 row = [''] * len(headers)
167 row[0] = state_id
168
169 for col in range(1, 1 + len(self.terminals)):
170 if not headers[col] in self.action[state_id]:
171 continue
172 row[col] = stringify_action_entries(self.action[state_id][headers[col]])
173
174 for col in range(1 + len(self.terminals), len(headers)):
175 if not headers[col] in self.goto[state_id]:
176 continue
177 row[col] = self.goto[state_id][headers[col]]
178
179 writer.writerow(row)
180
181

◆ stringify()

lalr_parsing.lalr_one.ParsingTable.stringify ( self)
132 def stringify(self):
133 states_str = '\n'.join(self.stringify_state(i) for i in range(self.n_states))
134 return states_str
135

◆ stringify_state()

lalr_parsing.lalr_one.ParsingTable.stringify_state ( self,
state_id )
108 def stringify_state(self, state_id):
109 state_title = 'State %d\n' % state_id
110
111 items = drop_itemset_lookaheads(kernels(self.__ccol[state_id]))
112 items = sorted(items, key=lambda elem: elem[0])
113
114 items_str = '\n'.join('\t' + self.__stringify_lr_zero_item(item) for item in items) + '\n\n'
115
116 actions = [(t, e) for t, e in self.action[state_id].items() if len(e) > 0]
117 actions = sorted(actions, key=lambda elem: elem[0])
118
119 actions_str = '\n'.join(self.__stringify_action_entries(t, e) for t, e in actions)
120 actions_str += ('\n' if len(actions_str) > 0 else '')
121
122 gotos = [(nt, sid) for nt, sid in self.goto[state_id].items() if sid is not None]
123 gotos = sorted(gotos, key=lambda elem: elem[0].name)
124
125 gotos_str = '\n'.join(self.__stringify_goto_entry(nt, sid) for nt, sid in gotos)
126 gotos_str += ('\n' if len(gotos_str) > 0 else '')
127
128 action_goto_separator = ('\n' if len(actions_str) > 0 and len(gotos_str) > 0 else '')
129
130 return state_title + items_str + actions_str + action_goto_separator + gotos_str
131

Member Data Documentation

◆ action

lalr_parsing.lalr_one.ParsingTable.action

◆ goto

lalr_parsing.lalr_one.ParsingTable.goto

◆ grammar

lalr_parsing.lalr_one.ParsingTable.grammar

◆ n_states

lalr_parsing.lalr_one.ParsingTable.n_states

◆ nonterms

lalr_parsing.lalr_one.ParsingTable.nonterms

◆ terminals

lalr_parsing.lalr_one.ParsingTable.terminals

The documentation for this class was generated from the following file: