HyperDbg Debugger
Loading...
Searching...
No Matches
lalr_parsing.lalr_one Namespace Reference

Classes

class  LrZeroItemTableEntry
 
class  ParsingTable
 

Functions

 get_canonical_collection (gr)
 
 closure (gr, item_set)
 
 goto (gr, item_set, inp)
 
 kernels (item_set)
 
 drop_itemset_lookaheads (itemset)
 

Variables

int STATUS_OK = 0
 
int STATUS_SR_CONFLICT = 1
 
int STATUS_RR_CONFLICT = 2
 

Function Documentation

◆ closure()

lalr_parsing.lalr_one.closure ( gr,
item_set )
261def closure(gr, item_set):
262 result = set(item_set)
263 current = item_set
264
265 while len(current) > 0:
266 new_elements = []
267
268 for ((prod_index, dot), lookahead) in current:
269 pname, pbody = gr.productions[prod_index]
270 if dot == len(pbody) or pbody[dot] not in gr.nonterms:
271 continue
272
273 nt = pbody[dot]
274 nt_offset = gr.nonterm_offset[nt]
275 following_symbols = pbody[dot+1:] + [lookahead]
276 following_terminals = gr.first_set(following_symbols) - {None}
277
278 for idx in range(len(nt.productions)):
279 for term in following_terminals:
280 new_item_set = ((nt_offset + idx, 0), term)
281 if new_item_set not in result:
282 result.add(new_item_set)
283 new_elements += [new_item_set]
284
285 current = new_elements
286
287 return frozenset(result)
288
289
set(SourceFiles "hyperdbg-cli.cpp" "../include/platform/user/header/Environment.h") include_directories("../include" "../dependencies") add_executable(hyperdbg-cli $
Definition CMakeLists.txt:1

◆ drop_itemset_lookaheads()

lalr_parsing.lalr_one.drop_itemset_lookaheads ( itemset)
309def drop_itemset_lookaheads(itemset):
310 return frozenset((x[0], x[1]) for x, y in itemset)
311
312

◆ get_canonical_collection()

lalr_parsing.lalr_one.get_canonical_collection ( gr)
192def get_canonical_collection(gr):
193 # See Dragonbook, page 272, "Algorithm 4.63"
194
195 # STEP 1
196 # ======
197 dfa = lr_zero.get_automaton(gr)
198 kstates = [lr_zero.kernels(st) for st in dfa.states]
199 n_states = len(kstates)
200
201 # STEPS 2, 3
202 # ==========
203 table = [{item: LrZeroItemTableEntry() for item in kstates[i]} for i in range(n_states)]
204 table[0][(0, 0)].lookaheads.add(grammar.EOF_SYMBOL)
205
206 for i_state_id in range(n_states):
207 state_symbols = [x[1] for x, y in dfa.goto.items() if x[0] == i_state_id]
208
209 for i_item in kstates[i_state_id]:
210 closure_set = closure(gr, [(i_item, grammar.FREE_SYMBOL)])
211
212 for sym in state_symbols:
213 j_state_id = dfa.goto[(i_state_id, sym)]
214
215 # For each item in closure_set whose . (dot) points to a symbol equal to 'sym'
216 # i.e. a production expecting to see 'sym' next
217 for ((prod_index, dot), next_symbol) in closure_set:
218 pname, pbody = gr.productions[prod_index]
219 if dot == len(pbody) or pbody[dot] != sym:
220 continue
221
222 j_item = (prod_index, dot + 1)
223 if next_symbol == grammar.FREE_SYMBOL:
224 table[i_state_id][i_item].propagates_to.add((j_state_id, j_item))
225 else:
226 table[j_state_id][j_item].lookaheads.add(next_symbol)
227
228 # STEP 4
229 # ======
230 repeat = True
231 while repeat:
232 repeat = False
233 # For every item set, kernel item
234 for i_state_id in range(len(table)):
235 for i_item, i_cell in table[i_state_id].items():
236 # For every kernel item i_item's lookaheads propagate to
237 for j_state_id, j_item in i_cell.propagates_to:
238 # Do propagate the lookaheads
239 j_cell = table[j_state_id][j_item]
240 j_cell_lookaheads_len = len(j_cell.lookaheads)
241 j_cell.lookaheads.update(i_cell.lookaheads)
242 # Check if they changed, so we can decide whether to iterate again
243 if j_cell_lookaheads_len < len(j_cell.lookaheads):
244 repeat = True
245
246 # Build the collection
247 # ====================
248 result = [set() for i in range(n_states)]
249 for i_state_id in range(n_states):
250 # Add kernel items
251 for i_item, i_cell in table[i_state_id].items():
252 for sym in i_cell.lookaheads:
253 item_set = (i_item, sym)
254 result[i_state_id].add(item_set)
255 # Add non-kernel kernel items
256 result[i_state_id] = closure(gr, result[i_state_id])
257
258 return result
259
260

◆ goto()

lalr_parsing.lalr_one.goto ( gr,
item_set,
inp )
290def goto(gr, item_set, inp):
291 result_set = set()
292 for (item, lookahead) in item_set:
293 prod_id, dot = item
294 pname, pbody = gr.productions[prod_id]
295 if dot == len(pbody) or pbody[dot] != inp:
296 continue
297
298 new_item = ((prod_id, dot + 1), lookahead)
299 result_set.add(new_item)
300
301 result_set = closure(gr, result_set)
302 return result_set
303
304

◆ kernels()

lalr_parsing.lalr_one.kernels ( item_set)
305def kernels(item_set):
306 return frozenset((item, nextsym) for item, nextsym in item_set if item[1] > 0 or item[0] == 0)
307
308

Variable Documentation

◆ STATUS_OK

int lalr_parsing.lalr_one.STATUS_OK = 0

◆ STATUS_RR_CONFLICT

int lalr_parsing.lalr_one.STATUS_RR_CONFLICT = 2

◆ STATUS_SR_CONFLICT

int lalr_parsing.lalr_one.STATUS_SR_CONFLICT = 1