Skip to content

Instantly share code, notes, and snippets.

@gauthamzz
Created January 10, 2017 11:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauthamzz/73239a9e19e147d16f58f10b897019c2 to your computer and use it in GitHub Desktop.
Save gauthamzz/73239a9e19e147d16f58f10b897019c2 to your computer and use it in GitHub Desktop.
Python DFA code to recognize the reserved keywords.
class Automaton:
def __init__(self, nstates):
self.transitions = [{} for i in range(nstates)]
self.accept_states = [False] * nstates
def register(self, source_state, char, target_state):
self.transitions[source_state][char] = target_state
def register_accept(self, state):
self.accept_states[state] = True
def accept(self, input):
state = 0
try:
for char in input:
state = self.transitions[state][char]
return self.accept_states[state]
except KeyError:
return False
# add your keywords and texts here
keyword=["if ","while ","then ","do ","for ","else ","goto ","break ","malloc ","return "]
texts=["while","gautham","if","do"]
# checking
for text in texts:
for key in keyword:
automaton= Automaton(len(key))
i=0
for k in key:
automaton.register(i,k,i+1)
i=i+1
automaton.register_accept(len(key)-1)
if automaton.accept(text):
print "keyword detected : " + text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment