Skip to content

Instantly share code, notes, and snippets.

@tyrion
Created November 17, 2014 10:49
Show Gist options
  • Save tyrion/fbbf63a7b4fc35051de7 to your computer and use it in GitHub Desktop.
Save tyrion/fbbf63a7b4fc35051de7 to your computer and use it in GitHub Desktop.
#!/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