In p_error(), LexToken' object has no attribute 'lexer'

68 views
Skip to first unread message

Bart Whiteley

unread,
Sep 23, 2008, 5:40:29 PM9/23/08
to ply-hack
I'm using ply-2.5.

I have defined a p_error(p) function. For some parse errors, p (a
LexToken) has a 'lexer' attribute. For other parse errors, p (still a
LexToken) does not have a 'lexer' attribute. If I set a break point
with pdb within p_error(), the stack traces look the same for both
types of parse errors.

Any idea why the LexToken argument to p_error() would lack the 'lexer'
attribute at times?

David Beazley

unread,
Sep 24, 2008, 9:57:15 AM9/24/08
to ply-hack, Bart Whiteley

Nope. I'll have to look at it in more detail to know why anything would be happening with this attribute---especially since in most rules, the exact same 'p' instance is used for
everything. Hmmm. Curious.

Cheers,
Dave

On Tue 23/09/08 5:40 PM , "Bart Whiteley" bwhi...@gmail.com sent:

Bart Whiteley

unread,
Sep 24, 2008, 12:05:52 PM9/24/08
to ply-hack
This fixes the problem.

--- ./lex.py.orig 2008-09-24 09:51:26.000000000 -0600
+++ ./lex.py 2008-09-24 09:52:32.000000000 -0600
@@ -281,6 +281,7 @@

# Create a token for return
tok = LexToken()
+ tok.lexer = self
tok.value = m.group()
tok.lineno = self.lineno
tok.lexpos = lexpos
@@ -305,7 +306,7 @@

# If token is processed by a function, call it

- tok.lexer = self # Set additional attributes
useful in token rules
+ # Set additional attributes useful in token rules
self.lexmatch = m
self.lexpos = lexpos

@@ -329,6 +330,7 @@
# No match, see if in literals
if lexdata[lexpos] in self.lexliterals:
tok = LexToken()
+ tok.lexer = self
tok.value = lexdata[lexpos]
tok.lineno = self.lineno
tok.type = tok.value

Reply all
Reply to author
Forward
0 new messages