gabriel belini gabriel belini - 27 days ago 4
Python Question

Python code to accept pre-defined grammar

I'm trying to make a code to recognize a chain of characters following the grammar rules:

  • S-> abK

  • K-> x|xK|H

  • H-> c|d

So that words like abx, abxxx, abc, abxd, abxc, etc... are all accepted and words like ab, abb, xxx, etc... aren't accepted.

I wrote a code to do that and in my analysis it should do the trick, but there is something wrong in it, i.e, it returns False for abxx, a sentence that should be accepted by the grammar and I think it has to do with nested return values from functions, which I don't understand very well.

The code will be pasted below, if you guys can figure out or point me what I'm doing wrong I will be thankful.

def S(word):
if word[0] == 'a':
atual = 1
return False
if word[1] == 'b':
atual = 2
return False
accepted = K(atual, word)
if accepted == True:
return True
return False

def K(atual, word):
if word[atual] == 'x':
atual += 1
if len(word) <= atual: # checks to see if the word ended and accept by the first rule of the set K.
return True
K(atual, word) # keeps increasing the value of atual, satisfying the rule xK
value = H(atual, word) # if no more 'x' are found, try the rule H
return value

def H(atual, word):
if word[atual] == 'c' or word[atual] == 'd':
return True
return False



Your implementation is unnecessarily verbose and repetitive: there is no need to pass around the index, for instance, when you can just pass to the next function the relevant part of the word. Here is a quick implementation I threw together that should resolve it:

def S(chars):
  word = ''.join(chars)
    return word[:2] == 'ab' and K(word[2:])
  except IndexError:
    return False

def K(word):
  return word == 'x' or (word[0] == 'x' and K(word[1:])) or H(word)

def H(word):
  return word in ['c', 'd']

Using this function, I get:

>>> list(map(S, ['abx', 'abxxx', 'abc', 'abxd', 'abxc']))
[True, True, True, True, True]