简体   繁体   中英

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, ie, 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
    else:
        return False
    if word[1] == 'b':
        atual = 2
    else:
        return False
    accepted = K(atual, word)
    if accepted == True:
        return True
    else:
        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
        else:
            K(atual, word) # keeps increasing the value of atual, satisfying the rule xK
    else:
        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
    else:
        return False

print(S(['a','b','x','x']))

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)
  try:
    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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM