Browse Source

Use loops for flow control instead of catching exceptions

While optimizing hot spots in a tool I wrote I saw this issue. Changing this to use a for loop granted a minor speed boost to my script.
tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.5.5
Parker 6 years ago
committed by GitHub
parent
commit
ce26c7cced
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 7 deletions
  1. +2
    -7
      lark/parsers/lalr_parser.py

+ 2
- 7
lark/parsers/lalr_parser.py View File

@@ -66,9 +66,7 @@ class _Parser:
value_stack.append(value)

# Main LALR-parser loop
try:
token = next(stream)
i += 1
for i, token in enumerate(stream):
while True:
action, arg = get_action(token.type)
assert arg != self.end_state
@@ -77,12 +75,9 @@ class _Parser:
state_stack.append(arg)
value_stack.append(token)
if set_state: set_state(arg)
token = next(stream)
i += 1
break # next token
else:
reduce(arg)
except StopIteration:
pass

while True:
_action, arg = get_action('$END')


Loading…
Cancel
Save