Created
November 17, 2014 10:49
-
-
Save tyrion/fbbf63a7b4fc35051de7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
a a | |
end <---(in)--->[1]<--->[2]--> end | |
""" | |
EOF = '' | |
# aa* | |
language = [ | |
{EOF: True, 'a': 1}, | |
{'a': 2}, | |
{'a': 1, EOF: True} | |
] | |
def match(input: iter, language, state=0): | |
node = language[state] | |
try: | |
state = node[next(input)] | |
except StopIteration: | |
return node.get(EOF, False) | |
except KeyError: | |
return False | |
return match(input, language, state) | |
def match_iter(input, language, state=0): | |
for c in input: | |
try: | |
state = language[state][c] | |
except KeyError: | |
return False | |
return language[state].get(EOF, False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment