From ce26c7ccedd506cec1c6c9d49cc31c6a47461d44 Mon Sep 17 00:00:00 2001 From: Parker <6646825+psboyce@users.noreply.github.com> Date: Tue, 20 Feb 2018 20:40:13 -0700 Subject: [PATCH] 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. --- lark/parsers/lalr_parser.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lark/parsers/lalr_parser.py b/lark/parsers/lalr_parser.py index 0e2b110..f4e0942 100644 --- a/lark/parsers/lalr_parser.py +++ b/lark/parsers/lalr_parser.py @@ -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')