我正在用PLY编写自己的解析器。我想分别封装lex和yacc
这是Lex类的代码:
class Lex:
tokens = (
'NAME', 'NUMBER',
)
literals = ['=', '+', '-', '*', '/', '(', ')']
# Tokens
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
...
Run Code Online (Sandbox Code Playgroud)
解析器代码(使用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")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误?必须以Lex实例作为第一个参数调用未绑定的方法t_NUMBER()(改为使用LexToken实例)
我尝试使用module=Lexlex,就像一样yacc.yacc(module=self),但是它没有用,任何人都可以告诉解决方案。
在C++中,我们都知道数组可以作为局部变量在"主"范围内:
int main(){
int arr[10000]; //on the stack, size can't be very large
....
}
Run Code Online (Sandbox Code Playgroud)
或者作为全局变量的"主"范围:
int arr[10000000]; //on BSS, sie can be very large
int main{
....
}
Run Code Online (Sandbox Code Playgroud)
但我想要更多这个问题.
在我的代码中,类A具有属性,但是类B不继承它。是否@property支持继承?还是我的错?
class A(object):
def __init__(self):
self._x = 100
@property
def x(self):
return self._x
@x.setter
def x(self, v):
self._x = v
class B(A):
@x.setter
def x(self, v):
self._x = v
Run Code Online (Sandbox Code Playgroud)
错误消息如下:
Traceback (most recent call last):
File "test.py", line 9, in <module>
class B(A):
File "test.py", line 10, in B
@x.setter
NameError: name 'x' is not defined
Run Code Online (Sandbox Code Playgroud) 我的问题是关于类数据成员的初始化.我想知道初始化规则,例如内置类型(int,double,float)和用户定义类型.如果我们没有初始化它们,它们的内容是未定义的,还是会使用默认的构造函数?
c++ ×2
arrays ×1
class ×1
constructor ×1
inheritance ×1
memory ×1
ply ×1
properties ×1
python ×1
python-2.7 ×1
stack ×1