From 2a0bfe366af9943f95abec02257acb59b8cbbf38 Mon Sep 17 00:00:00 2001 From: Jitendra Mishra Date: Tue, 6 Dec 2022 02:31:03 +0530 Subject: [PATCH] Improve logic and performance use fromkeys --- lark/parsers/lalr_analysis.py | 8 ++------ lark/utils.py | 18 +++++------------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/lark/parsers/lalr_analysis.py b/lark/parsers/lalr_analysis.py index 216371e5..06b06657 100644 --- a/lark/parsers/lalr_analysis.py +++ b/lark/parsers/lalr_analysis.py @@ -91,9 +91,7 @@ def from_ParseTable(cls, parse_table): def digraph(X, R, G): F = {} S = [] - N = {} - for x in X: - N[x] = 0 + N = dict.fromkeys(X, 0) for x in X: # this is always true for the first iteration, but N[x] may be updated in traverse below if N[x] == 0: @@ -247,9 +245,7 @@ def compute_lalr1_states(self): m = {} reduce_reduce = [] for state in self.lr0_states: - actions = {} - for la, next_state in state.transitions.items(): - actions[la] = (Shift, next_state.closure) + actions = {la: (Shift, next_state.closure) for la, next_state in state.transitions.items()} for la, rules in state.lookaheads.items(): if len(rules) > 1: # Try to resolve conflict based on priority diff --git a/lark/utils.py b/lark/utils.py index 6781e6fb..35d11197 100644 --- a/lark/utils.py +++ b/lark/utils.py @@ -1,6 +1,7 @@ import unicodedata import os from functools import reduce +from itertools import product from collections import deque from typing import Callable, Iterator, List, Optional, Tuple, Type, TypeVar, Union, Dict, Any, Sequence @@ -25,9 +26,9 @@ def classify(seq: Sequence, key: Optional[Callable] = None, value: Optional[Call for item in seq: k = key(item) if (key is not None) else item v = value(item) if (value is not None) else item - if k in d: + try: d[k].append(v) - else: + except KeyError: d[k] = [v] return d @@ -228,9 +229,7 @@ def combine_alternatives(lists): if not lists: return [[]] assert all(l for l in lists), lists - init = [[x] for x in lists[0]] - return reduce(lambda a,b: [i+[j] for i in a for j in b], lists[1:], init) - + return list(product(*lists)) try: import atomicwrites @@ -268,15 +267,8 @@ def __repr__(self): def classify_bool(seq: Sequence, pred: Callable) -> Any: - true_elems = [] false_elems = [] - - for elem in seq: - if pred(elem): - true_elems.append(elem) - else: - false_elems.append(elem) - + true_elems = [elem for elem in seq if pred(elem) or false_elems.append(elem)] # type: ignore[func-returns-value] return true_elems, false_elems