I am writing my own parser with PLY. I want to encapsulate the lex and yacc respectively
Here is the code for class Lex:
class Lex:
tokens = (
'NAME', 'NUMBER',
)
literals = ['=', '+', '-', '*', '/', '(', ')']
# Tokens
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
...
the Parser code (which use yacc):
class Parser:
# Parsing rules
tokens = Lex.tokens
def p_statement_assign(self, p):
'statement : NAME "=" expression'
self.names[p[1]] = p[3]
...
def run(self):
print self.tokens
lex.lex(module=Lex) # ----what should I do here?-----
parser = yacc.yacc(module=self)
parser.parse("1+2")
And I got the following error: unbound method t_NUMBER() must be called with Lex instance as first argument (got LexToken instance instead)
I tried using module=Lex
for the lex, just like yacc.yacc(module=self)
,but it didn't work, anyone could tell the solution.