Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logic and performance #1228

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions lark/parsers/lalr_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
18 changes: 5 additions & 13 deletions lark/utils.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For combine_alternatives method, more cleaner approach.
image


try:
import atomicwrites
Expand Down Expand Up @@ -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


Expand Down