开发者

Python metaclass and ModGrammar

开发者 https://www.devze.com 2023-03-27 23:29 出处:网络
I found (after another question here on StackOverflow) this interesting library written in Python which goal is the grammar parsing.

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


class Grammar(metaclass=GrammarClass)

is using Python3 syntax. The equivalent Python2 syntax would be

class Grammar(object):
    __metaclass__=GrammarClass

but since there may be lots of other Python3-isms, you may have to use Python3 to use modgrammar.


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.


Yeah, this is Python3 library http://code.google.com/p/modgrammar/source/browse/setup.py#32

Python2x doesn't support class Grammar(metaclass=GrammarClass)

0

精彩评论

暂无评论...
验证码 换一张
取 消