Python 元类和 ModGrammar
我发现(在 StackOverflow 上的另一个问题之后)这个用 Python 编写的有趣的库,其目标是语法解析。
http://code.google.com/p/modgrammar/
我还找到了这个教程关于它:
http://packages.python.org/modgrammar/tutorial.html
所以,读完所有教程后,我明白了这是什么 我在找!我尝试编写教程中的第一个示例:
from modgrammar import *
class MyGrammar (Grammar):
grammar = (LITERAL("Hello,"), LITERAL("world!"))
但遇到了此错误:
Traceback (most recent call last):
File "test.py", line 1, in <module>
from modgrammar import *
File "/Users/tesi/Desktop/Prova/modgrammar/__init__.py", line 503
class Grammar(metaclass=GrammarClass):
^
SyntaxError: invalid syntax
问题似乎出在元类声明中。当我调用 python 解释器时,可能需要添加一个“编译标志”?有什么好消息吗?! :) 谢谢
I found (after another question here on StackOverflow) this interesting library written in Python which goal is the grammar parsing.
http://code.google.com/p/modgrammar/
And I also found this tutorial regarding it:
http://packages.python.org/modgrammar/tutorial.html
So, after reading all the tutorial, I understood that this is what I'm looking for! I tried to write the first example in the tutorial:
from modgrammar import *
class MyGrammar (Grammar):
grammar = (LITERAL("Hello,"), LITERAL("world!"))
but I ran into this error:
Traceback (most recent call last):
File "test.py", line 1, in <module>
from modgrammar import *
File "/Users/tesi/Desktop/Prova/modgrammar/__init__.py", line 503
class Grammar(metaclass=GrammarClass):
^
SyntaxError: invalid syntax
The problem seems to be in the metaclass declaration. Might be I have to add a "compilation flag" when I call the python interpreter? Some good news?! :) Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用Python3语法。等效的 Python2 语法是,
但由于可能存在许多其他 Python3 主义,因此您可能必须使用 Python3 才能使用 modgrammar。
is using Python3 syntax. The equivalent Python2 syntax would be
but since there may be lots of other Python3-isms, you may have to use Python3 to use
modgrammar
.我已将 modgrammar 向后移植到 Python 2.6+。 Backport 也适用于 Python 3.x。您可以在这里找到它:https://github.com/don-ramon/modgrammar-py2。
I've backport modgrammar to Python 2.6+. Backport will also work with Python 3.x. You can find it here: https://github.com/don-ramon/modgrammar-py2.
是的,这是 Python3 库 http://code.google.com /p/modgrammar/source/browse/setup.py#32
Python2x 不支持
class Grammar(metaclass=GrammarClass)
Yeah, this is Python3 library http://code.google.com/p/modgrammar/source/browse/setup.py#32
Python2x doesn't support
class Grammar(metaclass=GrammarClass)