-2

i am trying to parse a list of words/phrases using ply and using lex.lex from that library.

i have used lex.lex on a list of words before and it worked fine, just using a for loop to input into the lexer.

but i keep getting the following error

Traceback (most recent call last):
File "<pyshell#56>", line 2, in <module>
mylexer.input(a)
File "ply\lex.py", line 253, in input
c = s[:1]
TypeError: 'NoneType' object has no attribute '__getitem__'

the list i am trying to lex on this occasion is parsed json and as far as i can tell is the only difference, from the previous lexing that actually did work?

thanks for any help.

DSM
  • 342,061
  • 65
  • 592
  • 494
tanky
  • 115
  • 1
  • 2
  • 10

2 Answers2

0

The relevant code from ply.lex is:

def input(self,s):
    # Pull off the first character to see if s looks like a string
    c = s[:1]
    if not isinstance(c,StringTypes):
        raise ValueError("Expected a string")
    self.lexdata = s
    self.lexpos = 0
    self.lexlen = len(s)

It looks like your problem is in your call mylexer.input(a). The a variable is None rather than a list of words.

Sometimes this is caused by how you generated the word list. For example, this would cause the same error as you're seeing:

words = 'the quick brown fox'.split()
a = words.sort()                   # Note, this returns None
mylexer.input(a)                   # The lexer won't be happy with None
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
0

OK

thanks, after reading through the list by hand, it appears that one of the json keys didnt have a value, which became None.

tanky
  • 115
  • 1
  • 2
  • 10