Created
January 10, 2017 11:01
-
-
Save gauthamzz/73239a9e19e147d16f58f10b897019c2 to your computer and use it in GitHub Desktop.
Python DFA code to recognize the reserved keywords.
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
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